test_aes.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2019 Philippe Reynes <philippe.reynes@softathome.com>
  4. *
  5. * Unit tests for aes functions
  6. */
  7. #include <common.h>
  8. #include <command.h>
  9. #include <hexdump.h>
  10. #include <rand.h>
  11. #include <uboot_aes.h>
  12. #include <test/lib.h>
  13. #include <test/test.h>
  14. #include <test/ut.h>
  15. #define TEST_AES_ONE_BLOCK 0
  16. #define TEST_AES_CBC_CHAIN 1
  17. struct test_aes_s {
  18. int key_len;
  19. int key_exp_len;
  20. int type;
  21. int num_block;
  22. };
  23. static struct test_aes_s test_aes[] = {
  24. { AES128_KEY_LENGTH, AES128_EXPAND_KEY_LENGTH, TEST_AES_ONE_BLOCK, 1 },
  25. { AES128_KEY_LENGTH, AES128_EXPAND_KEY_LENGTH, TEST_AES_CBC_CHAIN, 16 },
  26. { AES192_KEY_LENGTH, AES192_EXPAND_KEY_LENGTH, TEST_AES_ONE_BLOCK, 1 },
  27. { AES192_KEY_LENGTH, AES192_EXPAND_KEY_LENGTH, TEST_AES_CBC_CHAIN, 16 },
  28. { AES256_KEY_LENGTH, AES256_EXPAND_KEY_LENGTH, TEST_AES_ONE_BLOCK, 1 },
  29. { AES256_KEY_LENGTH, AES256_EXPAND_KEY_LENGTH, TEST_AES_CBC_CHAIN, 16 },
  30. };
  31. static void rand_buf(u8 *buf, int size)
  32. {
  33. int i;
  34. for (i = 0; i < size; i++)
  35. buf[i] = rand() & 0xff;
  36. }
  37. static int lib_test_aes_one_block(struct unit_test_state *uts, int key_len,
  38. u8 *key_exp, u8 *iv, int num_block,
  39. u8 *nocipher, u8 *ciphered, u8 *uncipher)
  40. {
  41. aes_encrypt(key_len, nocipher, key_exp, ciphered);
  42. aes_decrypt(key_len, ciphered, key_exp, uncipher);
  43. ut_asserteq_mem(nocipher, uncipher, AES_BLOCK_LENGTH);
  44. /* corrupt the expanded key */
  45. key_exp[0]++;
  46. aes_decrypt(key_len, ciphered, key_exp, uncipher);
  47. ut_assertf(memcmp(nocipher, uncipher, AES_BLOCK_LENGTH),
  48. "nocipher and uncipher should be different\n");
  49. return 0;
  50. }
  51. static int lib_test_aes_cbc_chain(struct unit_test_state *uts, int key_len,
  52. u8 *key_exp, u8 *iv, int num_block,
  53. u8 *nocipher, u8 *ciphered, u8 *uncipher)
  54. {
  55. aes_cbc_encrypt_blocks(key_len, key_exp, iv,
  56. nocipher, ciphered, num_block);
  57. aes_cbc_decrypt_blocks(key_len, key_exp, iv,
  58. ciphered, uncipher, num_block);
  59. ut_asserteq_mem(nocipher, uncipher, num_block * AES_BLOCK_LENGTH);
  60. /* corrupt the expanded key */
  61. key_exp[0]++;
  62. aes_cbc_decrypt_blocks(key_len, key_exp, iv,
  63. ciphered, uncipher, num_block);
  64. ut_assertf(memcmp(nocipher, uncipher, num_block * AES_BLOCK_LENGTH),
  65. "nocipher and uncipher should be different\n");
  66. return 0;
  67. }
  68. static int _lib_test_aes_run(struct unit_test_state *uts, int key_len,
  69. int key_exp_len, int type, int num_block)
  70. {
  71. u8 *key, *key_exp, *iv;
  72. u8 *nocipher, *ciphered, *uncipher;
  73. int ret;
  74. /* Allocate all the buffer */
  75. key = malloc(key_len);
  76. key_exp = malloc(key_exp_len);
  77. iv = malloc(AES_BLOCK_LENGTH);
  78. nocipher = malloc(num_block * AES_BLOCK_LENGTH);
  79. ciphered = malloc((num_block + 1) * AES_BLOCK_LENGTH);
  80. uncipher = malloc((num_block + 1) * AES_BLOCK_LENGTH);
  81. if (!key || !key_exp || !iv || !nocipher || !ciphered || !uncipher) {
  82. printf("%s: can't allocate memory\n", __func__);
  83. ret = -1;
  84. goto out;
  85. }
  86. /* Initialize all buffer */
  87. rand_buf(key, key_len);
  88. rand_buf(iv, AES_BLOCK_LENGTH);
  89. rand_buf(nocipher, num_block * AES_BLOCK_LENGTH);
  90. memset(ciphered, 0, (num_block + 1) * AES_BLOCK_LENGTH);
  91. memset(uncipher, 0, (num_block + 1) * AES_BLOCK_LENGTH);
  92. /* Expand the key */
  93. aes_expand_key(key, key_len, key_exp);
  94. /* Encrypt and decrypt */
  95. switch (type) {
  96. case TEST_AES_ONE_BLOCK:
  97. ret = lib_test_aes_one_block(uts, key_len, key_exp, iv,
  98. num_block, nocipher,
  99. ciphered, uncipher);
  100. break;
  101. case TEST_AES_CBC_CHAIN:
  102. ret = lib_test_aes_cbc_chain(uts, key_len, key_exp, iv,
  103. num_block, nocipher,
  104. ciphered, uncipher);
  105. break;
  106. default:
  107. printf("%s: unknown type (type=%d)\n", __func__, type);
  108. ret = -1;
  109. };
  110. out:
  111. /* Free all the data */
  112. free(key);
  113. free(key_exp);
  114. free(iv);
  115. free(nocipher);
  116. free(ciphered);
  117. free(uncipher);
  118. return ret;
  119. }
  120. static int lib_test_aes_run(struct unit_test_state *uts,
  121. struct test_aes_s *test)
  122. {
  123. int key_len = test->key_len;
  124. int key_exp_len = test->key_exp_len;
  125. int type = test->type;
  126. int num_block = test->num_block;
  127. return _lib_test_aes_run(uts, key_len, key_exp_len,
  128. type, num_block);
  129. }
  130. static int lib_test_aes(struct unit_test_state *uts)
  131. {
  132. int i, ret = 0;
  133. for (i = 0; i < ARRAY_SIZE(test_aes); i++) {
  134. ret = lib_test_aes_run(uts, &test_aes[i]);
  135. if (ret)
  136. break;
  137. }
  138. return ret;
  139. }
  140. LIB_TEST(lib_test_aes, 0);