hmactest.c 6.4 KB

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