aes_tests.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. assert(arrays_eq(round_keys, expected_round_keys, BLOCK_LENGTH));
  40. free(round_keys);
  41. }
  42. void aes_test_block_cipher() {
  43. uint8_t test_key[BLOCK_LENGTH] = TEST_KEY;
  44. uint8_t* round_keys = aes__expand_key(test_key);
  45. uint8_t original_data[BLOCK_LENGTH] = TEST_BLOCK;
  46. uint8_t test_data[BLOCK_LENGTH] = TEST_BLOCK;
  47. aes__encrypt_block(test_data, round_keys);
  48. aes__decrypt_block(test_data, round_keys);
  49. assert(arrays_eq(test_data, original_data, BLOCK_LENGTH));
  50. free(round_keys);
  51. }
  52. void aes_test_padding_validation() {
  53. uint8_t data_valid[16] = {
  54. 12, 63, 3, 76, 10, 92,
  55. 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
  56. };
  57. assert(aes__is_valid_padding(data_valid) == TRUE);
  58. uint8_t data_invalid[16] = {
  59. 12, 63, 3, 76, 10, 92,
  60. 10, 10, 10, 10, 10, 10, 10, 10, 10, 0,
  61. };
  62. assert(aes__is_valid_padding(data_invalid) == FALSE);
  63. uint8_t data_valid_all_pad[16] = {
  64. 16, 16, 16, 16, 16, 16, 16, 16,
  65. 16, 16, 16, 16, 16, 16, 16, 16,
  66. };
  67. assert(aes__is_valid_padding(data_valid_all_pad) == TRUE);
  68. }
  69. void aes_test_create_padding() {
  70. uint8_t data[24] = {
  71. 12, 63, 3, 76, 10, 92, 34, 11,
  72. 12, 63, 3, 76, 10, 92, 34, 11,
  73. 12, 63, 3, 76, 10, 92, 34, 11,
  74. };
  75. uint8_t expected_padding[BLOCK_LENGTH] = {
  76. 12, 63, 3, 76, 10, 92, 34, 11,
  77. 8 , 8, 8, 8, 8, 8, 8, 8,
  78. };
  79. aes_padded_data_t padded = aes_create_padded_data_container();
  80. int code = aes_pad_data(&padded, data, 24);
  81. assert(code == 0);
  82. assert(padded.data == data);
  83. assert(padded.data_length == 24);
  84. assert(padded.data_length_before_pad == 16);
  85. assert(padded.length == 32);
  86. assert(arrays_eq(padded.padded_block, expected_padding, BLOCK_LENGTH));
  87. }
  88. // TODO: Doesn't test padded block
  89. void aes_test_cipher() {
  90. uint8_t original[TEST_DATA_LENGTH] = TEST_DATA;
  91. uint8_t data[TEST_DATA_LENGTH] = TEST_DATA;
  92. uint8_t key[BLOCK_LENGTH] = TEST_KEY;
  93. aes_padded_data_t padded = aes_create_padded_data_container();
  94. int code = aes_pad_data(&padded, data, TEST_DATA_LENGTH);
  95. assert(code == 0);
  96. // ECB
  97. aes_ecb(encryption, &padded, key);
  98. assert(!arrays_eq(padded.data, original, padded.data_length_before_pad));
  99. aes_ecb(decryption, &padded, key);
  100. assert(arrays_eq(padded.data, original, padded.data_length_before_pad));
  101. // CBC
  102. aes_cbc(encryption, &padded, key);
  103. assert(!arrays_eq(padded.data, original, padded.data_length_before_pad));
  104. aes_cbc(decryption, &padded, key);
  105. assert(arrays_eq(padded.data, original, padded.data_length_before_pad));
  106. }