2
0

hmactest.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. if (!TEST_ptr(ctx = HMAC_CTX_new()))
  119. return 0;
  120. HMAC_CTX_reset(ctx);
  121. if (!TEST_ptr(ctx)
  122. || !TEST_ptr_null(HMAC_CTX_get_md(ctx))
  123. || !TEST_false(HMAC_Init_ex(ctx, NULL, 0, NULL, NULL))
  124. || !TEST_false(HMAC_Update(ctx, test[4].data, test[4].data_len))
  125. || !TEST_false(HMAC_Init_ex(ctx, test[4].key, -1, EVP_sha1(), NULL)))
  126. goto err;
  127. if (!TEST_true(HMAC_Init_ex(ctx, test[4].key, test[4].key_len, EVP_sha1(), NULL))
  128. || !TEST_true(HMAC_Update(ctx, test[4].data, test[4].data_len))
  129. || !TEST_true(HMAC_Final(ctx, buf, &len)))
  130. goto err;
  131. p = pt(buf, len);
  132. if (!TEST_ptr(p) || !TEST_str_eq(p, test[4].digest))
  133. goto err;
  134. if (!TEST_false(HMAC_Init_ex(ctx, NULL, 0, EVP_sha256(), NULL)))
  135. goto err;
  136. if (!TEST_true(HMAC_Init_ex(ctx, test[5].key, test[5].key_len, EVP_sha256(), NULL))
  137. || !TEST_ptr_eq(HMAC_CTX_get_md(ctx), EVP_sha256())
  138. || !TEST_true(HMAC_Update(ctx, test[5].data, test[5].data_len))
  139. || !TEST_true(HMAC_Final(ctx, buf, &len)))
  140. goto err;
  141. p = pt(buf, len);
  142. if (!TEST_ptr(p) || !TEST_str_eq(p, test[5].digest))
  143. goto err;
  144. if (!TEST_true(HMAC_Init_ex(ctx, test[6].key, test[6].key_len, NULL, NULL))
  145. || !TEST_true(HMAC_Update(ctx, test[6].data, test[6].data_len))
  146. || !TEST_true(HMAC_Final(ctx, buf, &len)))
  147. goto err;
  148. p = pt(buf, len);
  149. if (!TEST_ptr(p) || !TEST_str_eq(p, test[6].digest))
  150. goto err;
  151. /* Test reusing a key */
  152. if (!TEST_true(HMAC_Init_ex(ctx, NULL, 0, NULL, NULL))
  153. || !TEST_true(HMAC_Update(ctx, test[6].data, test[6].data_len))
  154. || !TEST_true(HMAC_Final(ctx, buf, &len)))
  155. goto err;
  156. p = pt(buf, len);
  157. if (!TEST_ptr(p) || !TEST_str_eq(p, test[6].digest))
  158. goto err;
  159. /*
  160. * Test reusing a key where the digest is provided again but is the same as
  161. * last time
  162. */
  163. if (!TEST_true(HMAC_Init_ex(ctx, NULL, 0, EVP_sha256(), NULL))
  164. || !TEST_true(HMAC_Update(ctx, test[6].data, test[6].data_len))
  165. || !TEST_true(HMAC_Final(ctx, buf, &len)))
  166. goto err;
  167. p = pt(buf, len);
  168. if (!TEST_ptr(p) || !TEST_str_eq(p, test[6].digest))
  169. goto err;
  170. ret = 1;
  171. err:
  172. HMAC_CTX_free(ctx);
  173. return ret;
  174. }
  175. static int test_hmac_single_shot(void)
  176. {
  177. char *p;
  178. /* Test single-shot with NULL key. */
  179. p = pt(HMAC(EVP_sha1(), NULL, 0, test[4].data, test[4].data_len,
  180. NULL, NULL), SHA_DIGEST_LENGTH);
  181. if (!TEST_ptr(p) || !TEST_str_eq(p, test[4].digest))
  182. return 0;
  183. return 1;
  184. }
  185. static int test_hmac_copy(void)
  186. {
  187. char *p;
  188. HMAC_CTX *ctx = NULL, *ctx2 = NULL;
  189. unsigned char buf[EVP_MAX_MD_SIZE];
  190. unsigned int len;
  191. int ret = 0;
  192. ctx = HMAC_CTX_new();
  193. ctx2 = HMAC_CTX_new();
  194. if (!TEST_ptr(ctx) || !TEST_ptr(ctx2))
  195. goto err;
  196. if (!TEST_true(HMAC_Init_ex(ctx, test[7].key, test[7].key_len, EVP_sha1(), NULL))
  197. || !TEST_true(HMAC_Update(ctx, test[7].data, test[7].data_len))
  198. || !TEST_true(HMAC_CTX_copy(ctx2, ctx))
  199. || !TEST_true(HMAC_Final(ctx2, buf, &len)))
  200. goto err;
  201. p = pt(buf, len);
  202. if (!TEST_ptr(p) || !TEST_str_eq(p, test[7].digest))
  203. goto err;
  204. ret = 1;
  205. err:
  206. HMAC_CTX_free(ctx2);
  207. HMAC_CTX_free(ctx);
  208. return ret;
  209. }
  210. static int test_hmac_copy_uninited(void)
  211. {
  212. const unsigned char key[24] = {0};
  213. const unsigned char ct[166] = {0};
  214. EVP_PKEY *pkey = NULL;
  215. EVP_MD_CTX *ctx = NULL;
  216. EVP_MD_CTX *ctx_tmp = NULL;
  217. int res = 0;
  218. if (!TEST_ptr(ctx = EVP_MD_CTX_new())
  219. || !TEST_ptr(pkey = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  220. key, sizeof(key)))
  221. || !TEST_true(EVP_DigestSignInit(ctx, NULL, EVP_sha1(), NULL, pkey))
  222. || !TEST_ptr(ctx_tmp = EVP_MD_CTX_new())
  223. || !TEST_true(EVP_MD_CTX_copy(ctx_tmp, ctx)))
  224. goto err;
  225. EVP_MD_CTX_free(ctx);
  226. ctx = ctx_tmp;
  227. ctx_tmp = NULL;
  228. if (!TEST_true(EVP_DigestSignUpdate(ctx, ct, sizeof(ct))))
  229. goto err;
  230. res = 1;
  231. err:
  232. EVP_MD_CTX_free(ctx);
  233. EVP_MD_CTX_free(ctx_tmp);
  234. EVP_PKEY_free(pkey);
  235. return res;
  236. }
  237. # ifndef OPENSSL_NO_MD5
  238. static char *pt(unsigned char *md, unsigned int len)
  239. {
  240. unsigned int i;
  241. static char buf[80];
  242. if (md == NULL)
  243. return NULL;
  244. for (i = 0; i < len; i++)
  245. sprintf(&(buf[i * 2]), "%02x", md[i]);
  246. return buf;
  247. }
  248. # endif
  249. int setup_tests(void)
  250. {
  251. ADD_ALL_TESTS(test_hmac_md5, 4);
  252. ADD_TEST(test_hmac_single_shot);
  253. ADD_TEST(test_hmac_bad);
  254. ADD_TEST(test_hmac_run);
  255. ADD_TEST(test_hmac_copy);
  256. ADD_TEST(test_hmac_copy_uninited);
  257. return 1;
  258. }