2
0

hmactest.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. * 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. if (!TEST_str_eq(p, test[idx].digest))
  92. return 0;
  93. return 1;
  94. }
  95. # endif
  96. static int test_hmac_bad(void)
  97. {
  98. HMAC_CTX *ctx = NULL;
  99. int ret = 0;
  100. ctx = HMAC_CTX_new();
  101. if (!TEST_ptr(ctx)
  102. || !TEST_ptr_null(HMAC_CTX_get_md(ctx))
  103. || !TEST_false(HMAC_Init_ex(ctx, NULL, 0, NULL, NULL))
  104. || !TEST_false(HMAC_Update(ctx, test[4].data, test[4].data_len))
  105. || !TEST_false(HMAC_Init_ex(ctx, NULL, 0, EVP_sha1(), NULL))
  106. || !TEST_false(HMAC_Update(ctx, test[4].data, test[4].data_len)))
  107. goto err;
  108. ret = 1;
  109. err:
  110. HMAC_CTX_free(ctx);
  111. return ret;
  112. }
  113. static int test_hmac_run(void)
  114. {
  115. char *p;
  116. HMAC_CTX *ctx = NULL;
  117. unsigned char buf[EVP_MAX_MD_SIZE];
  118. unsigned int len;
  119. int ret = 0;
  120. ctx = HMAC_CTX_new();
  121. HMAC_CTX_reset(ctx);
  122. if (!TEST_ptr(ctx)
  123. || !TEST_ptr_null(HMAC_CTX_get_md(ctx))
  124. || !TEST_false(HMAC_Init_ex(ctx, NULL, 0, NULL, NULL))
  125. || !TEST_false(HMAC_Update(ctx, test[4].data, test[4].data_len))
  126. || !TEST_false(HMAC_Init_ex(ctx, test[4].key, -1, EVP_sha1(), NULL)))
  127. goto err;
  128. if (!TEST_true(HMAC_Init_ex(ctx, test[4].key, test[4].key_len, EVP_sha1(), NULL))
  129. || !TEST_true(HMAC_Update(ctx, test[4].data, test[4].data_len))
  130. || !TEST_true(HMAC_Final(ctx, buf, &len)))
  131. goto err;
  132. p = pt(buf, len);
  133. if (!TEST_str_eq(p, test[4].digest))
  134. goto err;
  135. if (!TEST_false(HMAC_Init_ex(ctx, NULL, 0, EVP_sha256(), NULL)))
  136. goto err;
  137. if (!TEST_true(HMAC_Init_ex(ctx, test[5].key, test[5].key_len, EVP_sha256(), NULL))
  138. || !TEST_ptr_eq(HMAC_CTX_get_md(ctx), EVP_sha256())
  139. || !TEST_true(HMAC_Update(ctx, test[5].data, test[5].data_len))
  140. || !TEST_true(HMAC_Final(ctx, buf, &len)))
  141. goto err;
  142. p = pt(buf, len);
  143. if (!TEST_str_eq(p, test[5].digest))
  144. goto err;
  145. if (!TEST_true(HMAC_Init_ex(ctx, test[6].key, test[6].key_len, NULL, NULL))
  146. || !TEST_true(HMAC_Update(ctx, test[6].data, test[6].data_len))
  147. || !TEST_true(HMAC_Final(ctx, buf, &len)))
  148. goto err;
  149. p = pt(buf, len);
  150. if (!TEST_str_eq(p, test[6].digest))
  151. goto err;
  152. /* Test reusing a key */
  153. if (!TEST_true(HMAC_Init_ex(ctx, NULL, 0, NULL, NULL))
  154. || !TEST_true(HMAC_Update(ctx, test[6].data, test[6].data_len))
  155. || !TEST_true(HMAC_Final(ctx, buf, &len)))
  156. goto err;
  157. p = pt(buf, len);
  158. if (!TEST_str_eq(p, test[6].digest))
  159. goto err;
  160. /*
  161. * Test reusing a key where the digest is provided again but is the same as
  162. * last time
  163. */
  164. if (!TEST_true(HMAC_Init_ex(ctx, NULL, 0, EVP_sha256(), NULL))
  165. || !TEST_true(HMAC_Update(ctx, test[6].data, test[6].data_len))
  166. || !TEST_true(HMAC_Final(ctx, buf, &len)))
  167. goto err;
  168. p = pt(buf, len);
  169. if (!TEST_str_eq(p, test[6].digest))
  170. goto err;
  171. ret = 1;
  172. err:
  173. HMAC_CTX_free(ctx);
  174. return ret;
  175. }
  176. static int test_hmac_single_shot(void)
  177. {
  178. char *p;
  179. /* Test single-shot with an empty key. */
  180. p = pt(HMAC(EVP_sha1(), NULL, 0, test[4].data, test[4].data_len,
  181. NULL, NULL), SHA_DIGEST_LENGTH);
  182. if (!TEST_str_eq(p, test[4].digest))
  183. return 0;
  184. return 1;
  185. }
  186. static int test_hmac_copy(void)
  187. {
  188. char *p;
  189. HMAC_CTX *ctx = NULL, *ctx2 = NULL;
  190. unsigned char buf[EVP_MAX_MD_SIZE];
  191. unsigned int len;
  192. int ret = 0;
  193. ctx = HMAC_CTX_new();
  194. ctx2 = HMAC_CTX_new();
  195. if (!TEST_ptr(ctx) || !TEST_ptr(ctx2))
  196. goto err;
  197. if (!TEST_true(HMAC_Init_ex(ctx, test[7].key, test[7].key_len, EVP_sha1(), NULL))
  198. || !TEST_true(HMAC_Update(ctx, test[7].data, test[7].data_len))
  199. || !TEST_true(HMAC_CTX_copy(ctx2, ctx))
  200. || !TEST_true(HMAC_Final(ctx2, buf, &len)))
  201. goto err;
  202. p = pt(buf, len);
  203. if (!TEST_str_eq(p, test[7].digest))
  204. goto err;
  205. ret = 1;
  206. err:
  207. HMAC_CTX_free(ctx2);
  208. HMAC_CTX_free(ctx);
  209. return ret;
  210. }
  211. # ifndef OPENSSL_NO_MD5
  212. static char *pt(unsigned char *md, unsigned int len)
  213. {
  214. unsigned int i;
  215. static char buf[80];
  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. }