cmactest.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include "internal/nelem.h"
  13. #include <openssl/cmac.h>
  14. #include <openssl/aes.h>
  15. #include <openssl/evp.h>
  16. #include "testutil.h"
  17. static const char xtskey[32] = {
  18. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  19. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  20. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
  21. };
  22. static struct test_st {
  23. const char key[32];
  24. int key_len;
  25. const unsigned char data[64];
  26. int data_len;
  27. const char *mac;
  28. } test[3] = {
  29. {
  30. {
  31. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
  32. 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  33. },
  34. 16,
  35. "My test data",
  36. 12,
  37. "29cec977c48f63c200bd5c4a6881b224"
  38. },
  39. {
  40. {
  41. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
  42. 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
  43. 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
  44. },
  45. 32,
  46. "My test data",
  47. 12,
  48. "db6493aa04e4761f473b2b453c031c9a"
  49. },
  50. {
  51. {
  52. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
  53. 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
  54. 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
  55. },
  56. 32,
  57. "My test data again",
  58. 18,
  59. "65c11c75ecf590badd0a5e56cbb8af60"
  60. },
  61. };
  62. static char *pt(unsigned char *md, unsigned int len);
  63. static int test_cmac_bad(void)
  64. {
  65. CMAC_CTX *ctx = NULL;
  66. int ret = 0;
  67. ctx = CMAC_CTX_new();
  68. if (!TEST_ptr(ctx)
  69. || !TEST_false(CMAC_Init(ctx, NULL, 0, NULL, NULL))
  70. || !TEST_false(CMAC_Update(ctx, test[0].data, test[0].data_len))
  71. /* Should be able to pass cipher first, and then key */
  72. || !TEST_true(CMAC_Init(ctx, NULL, 0, EVP_aes_128_cbc(), NULL))
  73. /* Must have a key */
  74. || !TEST_false(CMAC_Update(ctx, test[0].data, test[0].data_len))
  75. /* Now supply the key */
  76. || !TEST_true(CMAC_Init(ctx, test[0].key, test[0].key_len, NULL, NULL))
  77. /* Update should now work */
  78. || !TEST_true(CMAC_Update(ctx, test[0].data, test[0].data_len))
  79. /* XTS is not a suitable cipher to use */
  80. || !TEST_false(CMAC_Init(ctx, xtskey, sizeof(xtskey), EVP_aes_128_xts(),
  81. NULL))
  82. || !TEST_false(CMAC_Update(ctx, test[0].data, test[0].data_len)))
  83. goto err;
  84. ret = 1;
  85. err:
  86. CMAC_CTX_free(ctx);
  87. return ret;
  88. }
  89. static int test_cmac_run(void)
  90. {
  91. char *p;
  92. CMAC_CTX *ctx = NULL;
  93. unsigned char buf[AES_BLOCK_SIZE];
  94. size_t len;
  95. int ret = 0;
  96. ctx = CMAC_CTX_new();
  97. if (!TEST_true(CMAC_Init(ctx, test[0].key, test[0].key_len,
  98. EVP_aes_128_cbc(), NULL))
  99. || !TEST_true(CMAC_Update(ctx, test[0].data, test[0].data_len))
  100. || !TEST_true(CMAC_Final(ctx, buf, &len)))
  101. goto err;
  102. p = pt(buf, len);
  103. if (!TEST_str_eq(p, test[0].mac))
  104. goto err;
  105. if (!TEST_true(CMAC_Init(ctx, test[1].key, test[1].key_len,
  106. EVP_aes_256_cbc(), NULL))
  107. || !TEST_true(CMAC_Update(ctx, test[1].data, test[1].data_len))
  108. || !TEST_true(CMAC_Final(ctx, buf, &len)))
  109. goto err;
  110. p = pt(buf, len);
  111. if (!TEST_str_eq(p, test[1].mac))
  112. goto err;
  113. if (!TEST_true(CMAC_Init(ctx, test[2].key, test[2].key_len, NULL, NULL))
  114. || !TEST_true(CMAC_Update(ctx, test[2].data, test[2].data_len))
  115. || !TEST_true(CMAC_Final(ctx, buf, &len)))
  116. goto err;
  117. p = pt(buf, len);
  118. if (!TEST_str_eq(p, test[2].mac))
  119. goto err;
  120. /* Test reusing a key */
  121. if (!TEST_true(CMAC_Init(ctx, NULL, 0, NULL, NULL))
  122. || !TEST_true(CMAC_Update(ctx, test[2].data, test[2].data_len))
  123. || !TEST_true(CMAC_Final(ctx, buf, &len)))
  124. goto err;
  125. p = pt(buf, len);
  126. if (!TEST_str_eq(p, test[2].mac))
  127. goto err;
  128. /* Test setting the cipher and key separately */
  129. if (!TEST_true(CMAC_Init(ctx, NULL, 0, EVP_aes_256_cbc(), NULL))
  130. || !TEST_true(CMAC_Init(ctx, test[2].key, test[2].key_len, NULL, NULL))
  131. || !TEST_true(CMAC_Update(ctx, test[2].data, test[2].data_len))
  132. || !TEST_true(CMAC_Final(ctx, buf, &len)))
  133. goto err;
  134. p = pt(buf, len);
  135. if (!TEST_str_eq(p, test[2].mac))
  136. goto err;
  137. ret = 1;
  138. err:
  139. CMAC_CTX_free(ctx);
  140. return ret;
  141. }
  142. static int test_cmac_copy(void)
  143. {
  144. char *p;
  145. CMAC_CTX *ctx = NULL, *ctx2 = NULL;
  146. unsigned char buf[AES_BLOCK_SIZE];
  147. size_t len;
  148. int ret = 0;
  149. ctx = CMAC_CTX_new();
  150. ctx2 = CMAC_CTX_new();
  151. if (!TEST_ptr(ctx) || !TEST_ptr(ctx2))
  152. goto err;
  153. if (!TEST_true(CMAC_Init(ctx, test[0].key, test[0].key_len,
  154. EVP_aes_128_cbc(), NULL))
  155. || !TEST_true(CMAC_Update(ctx, test[0].data, test[0].data_len))
  156. || !TEST_true(CMAC_CTX_copy(ctx2, ctx))
  157. || !TEST_true(CMAC_Final(ctx2, buf, &len)))
  158. goto err;
  159. p = pt(buf, len);
  160. if (!TEST_str_eq(p, test[0].mac))
  161. goto err;
  162. ret = 1;
  163. err:
  164. CMAC_CTX_free(ctx2);
  165. CMAC_CTX_free(ctx);
  166. return ret;
  167. }
  168. static char *pt(unsigned char *md, unsigned int len)
  169. {
  170. unsigned int i;
  171. static char buf[80];
  172. for (i = 0; i < len; i++)
  173. sprintf(&(buf[i * 2]), "%02x", md[i]);
  174. return buf;
  175. }
  176. int setup_tests(void)
  177. {
  178. ADD_TEST(test_cmac_bad);
  179. ADD_TEST(test_cmac_run);
  180. ADD_TEST(test_cmac_copy);
  181. return 1;
  182. }