hmactest.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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/hmac.h>
  14. # include <openssl/sha.h>
  15. # ifndef OPENSSL_NO_MD5
  16. # include <openssl/md5.h>
  17. # endif
  18. # ifdef CHARSET_EBCDIC
  19. # include <openssl/ebcdic.h>
  20. # endif
  21. #include "testutil.h"
  22. # ifndef OPENSSL_NO_MD5
  23. static struct test_st {
  24. unsigned char key[16];
  25. int key_len;
  26. unsigned char data[64];
  27. int data_len;
  28. unsigned char *digest;
  29. } test[8] = {
  30. {
  31. "", 0, "More text test vectors to stuff up EBCDIC machines :-)", 54,
  32. (unsigned char *)"e9139d1e6ee064ef8cf514fc7dc83e86",
  33. },
  34. {
  35. {
  36. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  37. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  38. }, 16, "Hi There", 8,
  39. (unsigned char *)"9294727a3638bb1c13f48ef8158bfc9d",
  40. },
  41. {
  42. "Jefe", 4, "what do ya want for nothing?", 28,
  43. (unsigned char *)"750c783e6ab0b503eaa86e310a5db738",
  44. },
  45. {
  46. {
  47. 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
  48. 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
  49. }, 16, {
  50. 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
  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
  55. }, 50, (unsigned char *)"56be34521d144c88dbb8c733f0e8b3f6",
  56. },
  57. {
  58. "", 0, "My test data", 12,
  59. (unsigned char *)"61afdecb95429ef494d61fdee15990cabf0826fc"
  60. },
  61. {
  62. "", 0, "My test data", 12,
  63. (unsigned char *)"2274b195d90ce8e03406f4b526a47e0787a88a65479938f1a5baa3ce0f079776"
  64. },
  65. {
  66. "123456", 6, "My test data", 12,
  67. (unsigned char *)"bab53058ae861a7f191abe2d0145cbb123776a6369ee3f9d79ce455667e411dd"
  68. },
  69. {
  70. "12345", 5, "My test data again", 18,
  71. (unsigned char *)"a12396ceddd2a85f4c656bc1e0aa50c78cffde3e"
  72. }
  73. };
  74. # endif
  75. static char *pt(unsigned char *md, unsigned int len);
  76. # ifndef OPENSSL_NO_MD5
  77. static int test_hmac_md5(int idx)
  78. {
  79. char *p;
  80. # ifdef CHARSET_EBCDIC
  81. ebcdic2ascii(test[0].data, test[0].data, test[0].data_len);
  82. ebcdic2ascii(test[1].data, test[1].data, test[1].data_len);
  83. ebcdic2ascii(test[2].key, test[2].key, test[2].key_len);
  84. ebcdic2ascii(test[2].data, test[2].data, test[2].data_len);
  85. # endif
  86. p = pt(HMAC(EVP_md5(),
  87. test[idx].key, test[idx].key_len,
  88. test[idx].data, test[idx].data_len, NULL, NULL),
  89. MD5_DIGEST_LENGTH);
  90. if (!TEST_str_eq(p, (char *)test[idx].digest))
  91. return 0;
  92. return 1;
  93. }
  94. # endif
  95. static int test_hmac_bad(void)
  96. {
  97. HMAC_CTX *ctx = NULL;
  98. int ret = 0;
  99. ctx = HMAC_CTX_new();
  100. if (!TEST_ptr(ctx)
  101. || !TEST_ptr_null(HMAC_CTX_get_md(ctx))
  102. || !TEST_false(HMAC_Init_ex(ctx, NULL, 0, NULL, NULL))
  103. || !TEST_false(HMAC_Update(ctx, test[4].data, test[4].data_len))
  104. || !TEST_false(HMAC_Init_ex(ctx, NULL, 0, EVP_sha1(), NULL))
  105. || !TEST_false(HMAC_Update(ctx, test[4].data, test[4].data_len)))
  106. goto err;
  107. ret = 1;
  108. err:
  109. HMAC_CTX_free(ctx);
  110. return ret;
  111. }
  112. static int test_hmac_run(void)
  113. {
  114. char *p;
  115. HMAC_CTX *ctx = NULL;
  116. unsigned char buf[EVP_MAX_MD_SIZE];
  117. unsigned int len;
  118. int ret = 0;
  119. ctx = HMAC_CTX_new();
  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_str_eq(p, (char *)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_str_eq(p, (char *)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_str_eq(p, (char *)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_str_eq(p, (char *)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_str_eq(p, (char *)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 an empty 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_str_eq(p, (char *)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_str_eq(p, (char *)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. # ifndef OPENSSL_NO_MD5
  211. static char *pt(unsigned char *md, unsigned int len)
  212. {
  213. unsigned int i;
  214. static char buf[80];
  215. for (i = 0; i < len; i++)
  216. sprintf(&(buf[i * 2]), "%02x", md[i]);
  217. return buf;
  218. }
  219. # endif
  220. int setup_tests(void)
  221. {
  222. ADD_ALL_TESTS(test_hmac_md5, 4);
  223. ADD_TEST(test_hmac_single_shot);
  224. ADD_TEST(test_hmac_bad);
  225. ADD_TEST(test_hmac_run);
  226. ADD_TEST(test_hmac_copy);
  227. return 1;
  228. }