test_aes_block.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: CC0-1.0
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include "soc/soc_caps.h"
  10. #include "esp_heap_caps.h"
  11. #include "unity.h"
  12. #include "test_params.h"
  13. #include "memory_checks.h"
  14. #include "unity_fixture.h"
  15. #define CBC_AES_BUFFER_SIZE 1600
  16. #define CTR_AES_BUFFER_SIZE 1000
  17. #if SOC_AES_SUPPORTED
  18. #include "aes_block.h"
  19. TEST_GROUP(aes);
  20. TEST_SETUP(aes)
  21. {
  22. test_utils_record_free_mem();
  23. TEST_ESP_OK(test_utils_set_leak_level(0, ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_GENERAL));
  24. }
  25. TEST_TEAR_DOWN(aes)
  26. {
  27. test_utils_finish_and_evaluate_leaks(test_utils_get_leak_level(ESP_LEAK_TYPE_WARNING, ESP_COMP_LEAK_ALL),
  28. test_utils_get_leak_level(ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_ALL));
  29. }
  30. TEST(aes, cbc_aes_256_test)
  31. {
  32. uint8_t key_bytes = 256 / 8;
  33. uint8_t nonce[16];
  34. const uint8_t expected_cipher_end[] = {
  35. 0x3e, 0x68, 0x8a, 0x02, 0xe6, 0xf2, 0x6a, 0x9e,
  36. 0x9b, 0xb2, 0xc0, 0xc4, 0x63, 0x63, 0xd9, 0x25,
  37. 0x51, 0xdc, 0xc2, 0x71, 0x96, 0xb3, 0xe5, 0xcd,
  38. 0xbd, 0x0e, 0xf2, 0xef, 0xa9, 0xab, 0xab, 0x2d,
  39. };
  40. uint8_t *chipertext = heap_caps_calloc(CBC_AES_BUFFER_SIZE, sizeof(uint8_t), MALLOC_CAP_INTERNAL);
  41. TEST_ASSERT_NOT_NULL(chipertext);
  42. uint8_t *plaintext = heap_caps_calloc(CBC_AES_BUFFER_SIZE, sizeof(uint8_t), MALLOC_CAP_INTERNAL);
  43. TEST_ASSERT_NOT_NULL(plaintext);
  44. uint8_t *decryptedtext = heap_caps_calloc(CBC_AES_BUFFER_SIZE, sizeof(uint8_t), MALLOC_CAP_INTERNAL);
  45. TEST_ASSERT_NOT_NULL(decryptedtext);
  46. memset(plaintext, 0x3A, CBC_AES_BUFFER_SIZE);
  47. memset(decryptedtext, 0x0, CBC_AES_BUFFER_SIZE);
  48. // Encrypt
  49. memcpy(nonce, iv, 16);
  50. aes_crypt_cbc_block(ESP_AES_ENCRYPT, key_bytes, key_256, CBC_AES_BUFFER_SIZE, nonce, plaintext, chipertext);
  51. TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_cipher_end, chipertext + CBC_AES_BUFFER_SIZE - 32, 32);
  52. // Decrypt
  53. memcpy(nonce, iv, 16);
  54. aes_crypt_cbc_block(ESP_AES_DECRYPT, key_bytes, key_256, CBC_AES_BUFFER_SIZE, nonce, chipertext, decryptedtext);
  55. TEST_ASSERT_EQUAL_HEX8_ARRAY(plaintext, decryptedtext, CBC_AES_BUFFER_SIZE);
  56. // Free dynamically allocated memory
  57. heap_caps_free(chipertext);
  58. heap_caps_free(plaintext);
  59. heap_caps_free(decryptedtext);
  60. }
  61. TEST(aes, ctr_aes_256_test)
  62. {
  63. uint8_t key_bytes = 256 / 8;
  64. uint8_t nonce[16];
  65. uint8_t stream_block[16];
  66. size_t nc_off = 0;
  67. const uint8_t expected_cipher_end[] = {
  68. 0xd4, 0xdc, 0x4f, 0x8f, 0xfe, 0x86, 0xee, 0xb5,
  69. 0x14, 0x7f, 0xba, 0x30, 0x25, 0xa6, 0x7f, 0x6c,
  70. 0xb5, 0x73, 0xaf, 0x90, 0xd7, 0xff, 0x36, 0xba,
  71. 0x2b, 0x1d, 0xec, 0xb9, 0x38, 0xfa, 0x0d, 0xeb,
  72. };
  73. uint8_t *chipertext = heap_caps_calloc(CTR_AES_BUFFER_SIZE, sizeof(uint8_t), MALLOC_CAP_INTERNAL);
  74. TEST_ASSERT_NOT_NULL(chipertext);
  75. uint8_t *plaintext = heap_caps_calloc(CTR_AES_BUFFER_SIZE, sizeof(uint8_t), MALLOC_CAP_INTERNAL);
  76. TEST_ASSERT_NOT_NULL(plaintext);
  77. uint8_t *decryptedtext = heap_caps_calloc(CTR_AES_BUFFER_SIZE, sizeof(uint8_t), MALLOC_CAP_INTERNAL);
  78. TEST_ASSERT_NOT_NULL(decryptedtext);
  79. memset(plaintext, 0x3A, CTR_AES_BUFFER_SIZE);
  80. memset(decryptedtext, 0x0, CTR_AES_BUFFER_SIZE);
  81. // Encrypt
  82. memcpy(nonce, iv, 16);
  83. aes_crypt_ctr_block(key_bytes, key_256, CTR_AES_BUFFER_SIZE, &nc_off, nonce, stream_block, plaintext, chipertext);
  84. TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_cipher_end, chipertext + CTR_AES_BUFFER_SIZE - 32, 32);
  85. // Decrypt
  86. nc_off = 0;
  87. memcpy(nonce, iv, 16);
  88. aes_crypt_ctr_block(key_bytes, key_256, CTR_AES_BUFFER_SIZE, &nc_off, nonce, stream_block, chipertext, decryptedtext);
  89. TEST_ASSERT_EQUAL_HEX8_ARRAY(plaintext, decryptedtext, CTR_AES_BUFFER_SIZE);
  90. // Free dynamically allocated memory
  91. heap_caps_free(chipertext);
  92. heap_caps_free(plaintext);
  93. heap_caps_free(decryptedtext);
  94. }
  95. #endif // SOC_AES_SUPPORTED
  96. TEST_GROUP_RUNNER(aes)
  97. {
  98. #if SOC_AES_SUPPORTED
  99. RUN_TEST_CASE(aes, cbc_aes_256_test);
  100. RUN_TEST_CASE(aes, ctr_aes_256_test);
  101. #endif // SOC_AES_SUPPORTED
  102. }