test_ctr_mode.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* test_ctr_mode.c - TinyCrypt implementation of some AES-CTR tests */
  2. /*
  3. * Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * - Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * - Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * - Neither the name of Intel Corporation nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. /*
  32. DESCRIPTION
  33. This module tests the following AES-CTR Mode routines:
  34. Scenarios tested include:
  35. - AES128 CTR mode encryption SP 800-38a tests
  36. */
  37. #include <tinycrypt/ctr_mode.h>
  38. #include <tinycrypt/aes.h>
  39. #include <tinycrypt/constants.h>
  40. #include <test_utils.h>
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. /*
  45. * NIST SP 800-38a CTR Test for encryption and decryption.
  46. */
  47. unsigned int test_1_and_2(void)
  48. {
  49. const uint8_t key[16] = {
  50. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88,
  51. 0x09, 0xcf, 0x4f, 0x3c
  52. };
  53. uint8_t ctr[16] = {
  54. 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb,
  55. 0xfc, 0xfd, 0xfe, 0xff
  56. };
  57. const uint8_t plaintext[64] = {
  58. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11,
  59. 0x73, 0x93, 0x17, 0x2a, 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  60. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51, 0x30, 0xc8, 0x1c, 0x46,
  61. 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
  62. 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b,
  63. 0xe6, 0x6c, 0x37, 0x10
  64. };
  65. const uint8_t ciphertext[80] = {
  66. 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb,
  67. 0xfc, 0xfd, 0xfe, 0xff, 0x87, 0x4d, 0x61, 0x91, 0xb6, 0x20, 0xe3, 0x26,
  68. 0x1b, 0xef, 0x68, 0x64, 0x99, 0x0d, 0xb6, 0xce, 0x98, 0x06, 0xf6, 0x6b,
  69. 0x79, 0x70, 0xfd, 0xff, 0x86, 0x17, 0x18, 0x7b, 0xb9, 0xff, 0xfd, 0xff,
  70. 0x5a, 0xe4, 0xdf, 0x3e, 0xdb, 0xd5, 0xd3, 0x5e, 0x5b, 0x4f, 0x09, 0x02,
  71. 0x0d, 0xb0, 0x3e, 0xab, 0x1e, 0x03, 0x1d, 0xda, 0x2f, 0xbe, 0x03, 0xd1,
  72. 0x79, 0x21, 0x70, 0xa0, 0xf3, 0x00, 0x9c, 0xee
  73. };
  74. struct tc_aes_key_sched_struct sched;
  75. uint8_t out[80];
  76. uint8_t decrypted[64];
  77. unsigned int result = TC_PASS;
  78. TC_PRINT("CTR test #1 (encryption SP 800-38a tests):\n");
  79. (void)tc_aes128_set_encrypt_key(&sched, key);
  80. (void)memcpy(out, ctr, sizeof(ctr));
  81. if (tc_ctr_mode(&out[TC_AES_BLOCK_SIZE], sizeof(plaintext), plaintext,
  82. sizeof(plaintext), ctr, &sched) == 0) {
  83. TC_ERROR("CTR test #1 (encryption SP 800-38a tests) failed in %s.\n", __func__);
  84. result = TC_FAIL;
  85. goto exitTest1;
  86. }
  87. result = check_result(1, ciphertext, sizeof(out), out, sizeof(out));
  88. TC_END_RESULT(result);
  89. TC_PRINT("CTR test #2 (decryption SP 800-38a tests):\n");
  90. (void) memcpy(ctr, out, sizeof(ctr));
  91. if (tc_ctr_mode(decrypted, sizeof(decrypted), &out[TC_AES_BLOCK_SIZE],
  92. sizeof(decrypted), ctr, &sched) == 0) {
  93. TC_ERROR("CTR test #2 (decryption SP 800-38a tests) failed in %s.\n", __func__);
  94. result = TC_FAIL;
  95. goto exitTest1;
  96. }
  97. result = check_result(2, plaintext, sizeof(plaintext),
  98. decrypted, sizeof(plaintext));
  99. exitTest1:
  100. TC_END_RESULT(result);
  101. return result;
  102. }
  103. /*
  104. * Main task to test AES
  105. */
  106. int main(void)
  107. {
  108. unsigned int result = TC_PASS;
  109. TC_START("Performing AES128-CTR mode tests:");
  110. TC_PRINT("Performing CTR tests:\n");
  111. result = test_1_and_2();
  112. if (result == TC_FAIL) { /* terminate test */
  113. TC_ERROR("CBC test #1 failed.\n");
  114. goto exitTest;
  115. }
  116. TC_PRINT("All CTR tests succeeded!\n");
  117. exitTest:
  118. TC_END_RESULT(result);
  119. TC_END_REPORT(result);
  120. }