cmactest.c 6.0 KB

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