aes_tests.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include "aes_internal.h"
  4. #include "array.h"
  5. #define TEST_DATA { 72, 101, 114, 101, 32, 105, 115, 32, 115, 111, 109, 101, 32, 116, 101, 120, 116, 32, 105, 110, 32, 97, 115, 99, 105, 105, 32, 115, 105, 110, 99, 101, 32, 116, 104, 101, 115, 101, 32, 97, 114, 101, 32, 97, 108, 108, 32, 98, 121, 116, 101, 115, 0 }
  6. #define TEST_DATA_LENGTH 53
  7. #define TEST_BLOCK { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, }
  8. #define TEST_KEY { 130, 191, 5, 162, 175, 104, 200, 14, 32, 0, 97, 170, 10, 83, 159, 90, }
  9. void aes_test_shift_block() {
  10. uint8_t original_data[BLOCK_LENGTH] = TEST_BLOCK;
  11. uint8_t test_data[BLOCK_LENGTH] = TEST_BLOCK;
  12. uint8_t expected_shift[BLOCK_LENGTH] = {
  13. 0, 1, 2, 3, 5, 6, 7, 4, 10, 11, 8, 9, 15, 12, 13, 14,
  14. };
  15. aes__shift_rows(test_data, encryption);
  16. assert(arrays_eq(test_data, expected_shift, BLOCK_LENGTH));
  17. aes__shift_rows(test_data, decryption);
  18. assert(arrays_eq(test_data, original_data, BLOCK_LENGTH));
  19. }
  20. void aes_test_xtime() {
  21. assert(aes__xtime(84) == 168);
  22. assert(aes__xtime(255) == 229);
  23. assert(aes__xtime(0) == 0);
  24. assert(aes__xtime(240) == 251);
  25. }
  26. void aes_test_mix_words() {
  27. uint8_t test_data[BLOCK_LENGTH] = TEST_BLOCK;
  28. uint8_t original_data[BLOCK_LENGTH] = TEST_BLOCK;
  29. aes__mix_words(test_data, encryption);
  30. aes__mix_words(test_data, decryption);
  31. assert(arrays_eq(test_data, original_data, BLOCK_LENGTH));
  32. }
  33. void aes_test_expand_key() {
  34. uint8_t test_key[BLOCK_LENGTH] = TEST_KEY;
  35. uint8_t expected_round_keys[BLOCK_LENGTH * 11] = {
  36. 130, 191, 5, 162, 175, 104, 200, 14, 32, 0, 97, 170, 10, 83, 159, 90, 110, 100, 187, 197, 193, 12, 115, 203, 225, 12, 18, 97, 235, 95, 141, 59, 163, 57, 89, 44, 98, 53, 42, 231, 131, 57, 56, 134, 104, 102, 181, 189, 148, 236, 35, 105, 246, 217, 9, 142, 117, 224, 49, 8, 29, 134, 132, 181, 216, 179, 246, 205, 46, 106, 255, 67, 91, 138, 206, 75, 70, 12, 74, 254, 54, 101, 77, 151, 24, 15, 178, 212, 67, 133, 124, 159, 5, 137, 54, 97, 177, 96, 162, 252, 169, 111, 16, 40, 234, 234, 108, 183, 239, 99, 90, 214, 10, 222, 84, 35, 163, 177, 68, 11, 73, 91, 40, 188, 166, 56, 114, 106, 141, 158, 86, 7, 46, 47, 18, 12, 103, 116, 58, 176, 193, 76, 72, 218, 191, 204, 1, 127, 145, 227, 19, 115, 246, 151, 41, 195, 55, 219, 97, 25, 48, 35, 213, 229, 161, 192, 198, 150, 87, 87, 239, 85, 96, 140, 142, 76
  37. };
  38. uint8_t* round_keys = aes__expand_key(test_key);
  39. int result = arrays_eq(round_keys, expected_round_keys, BLOCK_LENGTH);
  40. free(round_keys);
  41. assert(result);
  42. }
  43. void aes_test_block_cipher() {
  44. uint8_t test_key[BLOCK_LENGTH] = TEST_KEY;
  45. uint8_t* round_keys = aes__expand_key(test_key);
  46. uint8_t original_data[BLOCK_LENGTH] = TEST_BLOCK;
  47. uint8_t test_data[BLOCK_LENGTH] = TEST_BLOCK;
  48. aes__encrypt_block(test_data, round_keys);
  49. aes__decrypt_block(test_data, round_keys);
  50. free(round_keys);
  51. assert(arrays_eq(test_data, original_data, BLOCK_LENGTH));
  52. }
  53. void aes_test_padding_validation() {
  54. uint8_t data_valid[16] = {
  55. 12, 63, 3, 76, 10, 92,
  56. 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
  57. };
  58. assert(aes__is_valid_padding(data_valid) == TRUE);
  59. uint8_t data_invalid[16] = {
  60. 12, 63, 3, 76, 10, 92,
  61. 10, 10, 10, 10, 10, 10, 10, 10, 10, 0,
  62. };
  63. assert(aes__is_valid_padding(data_invalid) == FALSE);
  64. uint8_t data_valid_all_pad[16] = {
  65. 16, 16, 16, 16, 16, 16, 16, 16,
  66. 16, 16, 16, 16, 16, 16, 16, 16,
  67. };
  68. assert(aes__is_valid_padding(data_valid_all_pad) == TRUE);
  69. }
  70. void aes_test_create_padding() {
  71. uint8_t data[24] = {
  72. 12, 63, 3, 76, 10, 92, 34, 11,
  73. 12, 63, 3, 76, 10, 92, 34, 11,
  74. 12, 63, 3, 76, 10, 92, 34, 11,
  75. };
  76. uint8_t expected_padding[BLOCK_LENGTH] = {
  77. 12, 63, 3, 76, 10, 92, 34, 11,
  78. 8 , 8, 8, 8, 8, 8, 8, 8,
  79. };
  80. aes_padded_data_t padded = aes_create_padded_data_container();
  81. int code = aes_pad_data(&padded, data, 24);
  82. assert(code == 0);
  83. assert(padded.data == data);
  84. assert(padded.data_length == 24);
  85. assert(padded.data_length_before_pad == 16);
  86. assert(padded.length == 32);
  87. assert(arrays_eq(padded.padded_block, expected_padding, BLOCK_LENGTH));
  88. uint8_t* data_ret = aes_drop_padded_data_container(&padded);
  89. assert(data_ret == data);
  90. }
  91. // TODO: Doesn't test padded block
  92. void aes_test_cipher() {
  93. uint8_t original[TEST_DATA_LENGTH] = TEST_DATA;
  94. uint8_t data[TEST_DATA_LENGTH] = TEST_DATA;
  95. uint8_t key[BLOCK_LENGTH] = TEST_KEY;
  96. aes_padded_data_t padded = aes_create_padded_data_container();
  97. int code = aes_pad_data(&padded, data, TEST_DATA_LENGTH);
  98. assert(code == 0);
  99. // ECB
  100. aes_ecb(encryption, &padded, key);
  101. assert(!arrays_eq(padded.data, original, padded.data_length_before_pad));
  102. aes_ecb(decryption, &padded, key);
  103. assert(arrays_eq(padded.data, original, padded.data_length_before_pad));
  104. // CBC
  105. aes_cbc(encryption, &padded, key);
  106. assert(!arrays_eq(padded.data, original, padded.data_length_before_pad));
  107. aes_cbc(decryption, &padded, key);
  108. assert(arrays_eq(padded.data, original, padded.data_length_before_pad));
  109. aes_drop_padded_data_container(&padded);
  110. }