testmngr.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /****************************************************************************
  2. * crypto/testmngr.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. #include <sys/types.h>
  25. #include <stdbool.h>
  26. #include <string.h>
  27. #include <poll.h>
  28. #include <errno.h>
  29. #include <debug.h>
  30. #include <nuttx/fs/fs.h>
  31. #include <nuttx/kmalloc.h>
  32. #include <nuttx/crypto/crypto.h>
  33. #ifdef CONFIG_CRYPTO_ALGTEST
  34. #include "testmngr.h"
  35. /****************************************************************************
  36. * Pre-processor Definitions
  37. ****************************************************************************/
  38. #ifndef ARRAY_SIZE
  39. # define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  40. #endif
  41. #if defined(CONFIG_CRYPTO_AES)
  42. /****************************************************************************
  43. * Private Functions
  44. ****************************************************************************/
  45. static int do_test_aes(FAR struct cipher_testvec *test,
  46. int mode,
  47. int encrypt)
  48. {
  49. FAR void *out = kmm_zalloc(test->rlen);
  50. int res = aes_cypher(out, test->input, test->ilen, test->iv, test->key,
  51. test->klen, mode, encrypt);
  52. if (res == OK)
  53. {
  54. res = memcmp(out, test->result, test->rlen);
  55. }
  56. kmm_free(out);
  57. return res;
  58. }
  59. #define AES_CYPHER_TEST_ENCRYPT(mode, mode_str, count, template) \
  60. for (i = 0; i < count; i++) { \
  61. if (do_test_aes(template + i, mode, CYPHER_ENCRYPT)) { \
  62. crypterr("ERROR: Failed " mode_str " encrypt test #%i\n", i); \
  63. return -1; \
  64. } \
  65. }
  66. #define AES_CYPHER_TEST_DECRYPT(mode, mode_str, count, template) \
  67. for (i = 0; i < count; i++) { \
  68. if (do_test_aes(template + i, mode, CYPHER_DECRYPT)) { \
  69. crypterr("ERROR: Failed " mode_str " decrypt test #%i\n", i); \
  70. return -1; \
  71. } \
  72. }
  73. #define AES_CYPHER_TEST(mode, mode_str, enc_count, dec_count, enc_template, dec_template) \
  74. AES_CYPHER_TEST_ENCRYPT(mode, mode_str, enc_count, enc_template)\
  75. AES_CYPHER_TEST_DECRYPT(mode, mode_str, dec_count, dec_template)
  76. static int test_aes(void)
  77. {
  78. int i;
  79. AES_CYPHER_TEST(AES_MODE_ECB, "ECB", ARRAY_SIZE(aes_enc_tv_template),
  80. ARRAY_SIZE(aes_dec_tv_template), aes_enc_tv_template,
  81. aes_dec_tv_template)
  82. AES_CYPHER_TEST(AES_MODE_CBC, "CBC", ARRAY_SIZE(aes_cbc_enc_tv_template),
  83. ARRAY_SIZE(aes_cbc_dec_tv_template),
  84. aes_cbc_enc_tv_template, aes_cbc_dec_tv_template)
  85. AES_CYPHER_TEST(AES_MODE_CTR, "CTR", ARRAY_SIZE(aes_ctr_enc_tv_template),
  86. ARRAY_SIZE(aes_ctr_dec_tv_template),
  87. aes_ctr_enc_tv_template, aes_ctr_dec_tv_template)
  88. return OK;
  89. }
  90. #endif
  91. int crypto_test(void)
  92. {
  93. #if defined(CONFIG_CRYPTO_AES)
  94. if (test_aes())
  95. {
  96. return -1;
  97. }
  98. #endif
  99. return OK;
  100. }
  101. #else /* CONFIG_CRYPTO_ALGTEST */
  102. int crypto_test(void)
  103. {
  104. return OK;
  105. }
  106. #endif /* CONFIG_CRYPTO_ALGTEST */