hmactest.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Copyright 1995-2021 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. * HMAC 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/hmac.h>
  19. # include <openssl/sha.h>
  20. # ifndef OPENSSL_NO_MD5
  21. # include <openssl/md5.h>
  22. # endif
  23. # ifdef CHARSET_EBCDIC
  24. # include <openssl/ebcdic.h>
  25. # endif
  26. #include "testutil.h"
  27. # ifndef OPENSSL_NO_MD5
  28. static struct test_st {
  29. const char key[16];
  30. int key_len;
  31. const unsigned char data[64];
  32. int data_len;
  33. const char *digest;
  34. } test[8] = {
  35. {
  36. "", 0, "More text test vectors to stuff up EBCDIC machines :-)", 54,
  37. "e9139d1e6ee064ef8cf514fc7dc83e86",
  38. },
  39. {
  40. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  41. 16, "Hi There", 8,
  42. "9294727a3638bb1c13f48ef8158bfc9d",
  43. },
  44. {
  45. "Jefe", 4, "what do ya want for nothing?", 28,
  46. "750c783e6ab0b503eaa86e310a5db738",
  47. },
  48. {
  49. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
  50. 16, {
  51. 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
  52. 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
  53. 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
  54. 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
  55. 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd
  56. }, 50, "56be34521d144c88dbb8c733f0e8b3f6",
  57. },
  58. {
  59. "", 0, "My test data", 12,
  60. "61afdecb95429ef494d61fdee15990cabf0826fc"
  61. },
  62. {
  63. "", 0, "My test data", 12,
  64. "2274b195d90ce8e03406f4b526a47e0787a88a65479938f1a5baa3ce0f079776"
  65. },
  66. {
  67. "123456", 6, "My test data", 12,
  68. "bab53058ae861a7f191abe2d0145cbb123776a6369ee3f9d79ce455667e411dd"
  69. },
  70. {
  71. "12345", 5, "My test data again", 18,
  72. "a12396ceddd2a85f4c656bc1e0aa50c78cffde3e"
  73. }
  74. };
  75. # endif
  76. static char *pt(unsigned char *md, unsigned int len);
  77. # ifndef OPENSSL_NO_MD5
  78. static int test_hmac_md5(int idx)
  79. {
  80. char *p;
  81. # ifdef CHARSET_EBCDIC
  82. ebcdic2ascii(test[0].data, test[0].data, test[0].data_len);
  83. ebcdic2ascii(test[1].data, test[1].data, test[1].data_len);
  84. ebcdic2ascii(test[2].key, test[2].key, test[2].key_len);
  85. ebcdic2ascii(test[2].data, test[2].data, test[2].data_len);
  86. # endif
  87. p = pt(HMAC(EVP_md5(),
  88. test[idx].key, test[idx].key_len,
  89. test[idx].data, test[idx].data_len, NULL, NULL),
  90. MD5_DIGEST_LENGTH);
  91. return TEST_ptr(p) && TEST_str_eq(p, test[idx].digest);
  92. }
  93. # endif
  94. static int test_hmac_bad(void)
  95. {
  96. HMAC_CTX *ctx = NULL;
  97. int ret = 0;
  98. ctx = HMAC_CTX_new();
  99. if (!TEST_ptr(ctx)
  100. || !TEST_ptr_null(HMAC_CTX_get_md(ctx))
  101. || !TEST_false(HMAC_Init_ex(ctx, NULL, 0, NULL, NULL))
  102. || !TEST_false(HMAC_Update(ctx, test[4].data, test[4].data_len))
  103. || !TEST_false(HMAC_Init_ex(ctx, NULL, 0, EVP_sha1(), NULL))
  104. || !TEST_false(HMAC_Update(ctx, test[4].data, test[4].data_len)))
  105. goto err;
  106. ret = 1;
  107. err:
  108. HMAC_CTX_free(ctx);
  109. return ret;
  110. }
  111. static int test_hmac_run(void)
  112. {
  113. char *p;
  114. HMAC_CTX *ctx = NULL;
  115. unsigned char buf[EVP_MAX_MD_SIZE];
  116. unsigned int len;
  117. int ret = 0;
  118. ctx = HMAC_CTX_new();
  119. HMAC_CTX_reset(ctx);
  120. if (!TEST_ptr(ctx)
  121. || !TEST_ptr_null(HMAC_CTX_get_md(ctx))
  122. || !TEST_false(HMAC_Init_ex(ctx, NULL, 0, NULL, NULL))
  123. || !TEST_false(HMAC_Update(ctx, test[4].data, test[4].data_len))
  124. || !TEST_false(HMAC_Init_ex(ctx, test[4].key, -1, EVP_sha1(), NULL)))
  125. goto err;
  126. if (!TEST_true(HMAC_Init_ex(ctx, test[4].key, test[4].key_len, EVP_sha1(), NULL))
  127. || !TEST_true(HMAC_Update(ctx, test[4].data, test[4].data_len))
  128. || !TEST_true(HMAC_Final(ctx, buf, &len)))
  129. goto err;
  130. p = pt(buf, len);
  131. if (!TEST_ptr(p) || !TEST_str_eq(p, test[4].digest))
  132. goto err;
  133. if (!TEST_false(HMAC_Init_ex(ctx, NULL, 0, EVP_sha256(), NULL)))
  134. goto err;
  135. if (!TEST_true(HMAC_Init_ex(ctx, test[5].key, test[5].key_len, EVP_sha256(), NULL))
  136. || !TEST_ptr_eq(HMAC_CTX_get_md(ctx), EVP_sha256())
  137. || !TEST_true(HMAC_Update(ctx, test[5].data, test[5].data_len))
  138. || !TEST_true(HMAC_Final(ctx, buf, &len)))
  139. goto err;
  140. p = pt(buf, len);
  141. if (!TEST_ptr(p) || !TEST_str_eq(p, test[5].digest))
  142. goto err;
  143. if (!TEST_true(HMAC_Init_ex(ctx, test[6].key, test[6].key_len, NULL, NULL))
  144. || !TEST_true(HMAC_Update(ctx, test[6].data, test[6].data_len))
  145. || !TEST_true(HMAC_Final(ctx, buf, &len)))
  146. goto err;
  147. p = pt(buf, len);
  148. if (!TEST_ptr(p) || !TEST_str_eq(p, test[6].digest))
  149. goto err;
  150. /* Test reusing a key */
  151. if (!TEST_true(HMAC_Init_ex(ctx, NULL, 0, NULL, NULL))
  152. || !TEST_true(HMAC_Update(ctx, test[6].data, test[6].data_len))
  153. || !TEST_true(HMAC_Final(ctx, buf, &len)))
  154. goto err;
  155. p = pt(buf, len);
  156. if (!TEST_ptr(p) || !TEST_str_eq(p, test[6].digest))
  157. goto err;
  158. /*
  159. * Test reusing a key where the digest is provided again but is the same as
  160. * last time
  161. */
  162. if (!TEST_true(HMAC_Init_ex(ctx, NULL, 0, EVP_sha256(), NULL))
  163. || !TEST_true(HMAC_Update(ctx, test[6].data, test[6].data_len))
  164. || !TEST_true(HMAC_Final(ctx, buf, &len)))
  165. goto err;
  166. p = pt(buf, len);
  167. if (!TEST_ptr(p) || !TEST_str_eq(p, test[6].digest))
  168. goto err;
  169. ret = 1;
  170. err:
  171. HMAC_CTX_free(ctx);
  172. return ret;
  173. }
  174. static int test_hmac_single_shot(void)
  175. {
  176. char *p;
  177. /* Test single-shot with NULL key. */
  178. p = pt(HMAC(EVP_sha1(), NULL, 0, test[4].data, test[4].data_len,
  179. NULL, NULL), SHA_DIGEST_LENGTH);
  180. if (!TEST_ptr(p) || !TEST_str_eq(p, test[4].digest))
  181. return 0;
  182. return 1;
  183. }
  184. static int test_hmac_copy(void)
  185. {
  186. char *p;
  187. HMAC_CTX *ctx = NULL, *ctx2 = NULL;
  188. unsigned char buf[EVP_MAX_MD_SIZE];
  189. unsigned int len;
  190. int ret = 0;
  191. ctx = HMAC_CTX_new();
  192. ctx2 = HMAC_CTX_new();
  193. if (!TEST_ptr(ctx) || !TEST_ptr(ctx2))
  194. goto err;
  195. if (!TEST_true(HMAC_Init_ex(ctx, test[7].key, test[7].key_len, EVP_sha1(), NULL))
  196. || !TEST_true(HMAC_Update(ctx, test[7].data, test[7].data_len))
  197. || !TEST_true(HMAC_CTX_copy(ctx2, ctx))
  198. || !TEST_true(HMAC_Final(ctx2, buf, &len)))
  199. goto err;
  200. p = pt(buf, len);
  201. if (!TEST_ptr(p) || !TEST_str_eq(p, test[7].digest))
  202. goto err;
  203. ret = 1;
  204. err:
  205. HMAC_CTX_free(ctx2);
  206. HMAC_CTX_free(ctx);
  207. return ret;
  208. }
  209. # ifndef OPENSSL_NO_MD5
  210. static char *pt(unsigned char *md, unsigned int len)
  211. {
  212. unsigned int i;
  213. static char buf[80];
  214. if (md == NULL)
  215. return NULL;
  216. for (i = 0; i < len; i++)
  217. sprintf(&(buf[i * 2]), "%02x", md[i]);
  218. return buf;
  219. }
  220. # endif
  221. int setup_tests(void)
  222. {
  223. ADD_ALL_TESTS(test_hmac_md5, 4);
  224. ADD_TEST(test_hmac_single_shot);
  225. ADD_TEST(test_hmac_bad);
  226. ADD_TEST(test_hmac_run);
  227. ADD_TEST(test_hmac_copy);
  228. return 1;
  229. }