hmactest.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright 1995-2017 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. ret = 1;
  152. err:
  153. HMAC_CTX_free(ctx);
  154. return ret;
  155. }
  156. static int test_hmac_single_shot(void)
  157. {
  158. char *p;
  159. /* Test single-shot with an empty key. */
  160. p = pt(HMAC(EVP_sha1(), NULL, 0, test[4].data, test[4].data_len,
  161. NULL, NULL), SHA_DIGEST_LENGTH);
  162. if (!TEST_str_eq(p, (char *)test[4].digest))
  163. return 0;
  164. return 1;
  165. }
  166. static int test_hmac_copy(void)
  167. {
  168. char *p;
  169. HMAC_CTX *ctx = NULL, *ctx2 = NULL;
  170. unsigned char buf[EVP_MAX_MD_SIZE];
  171. unsigned int len;
  172. int ret = 0;
  173. ctx = HMAC_CTX_new();
  174. ctx2 = HMAC_CTX_new();
  175. if (!TEST_ptr(ctx) || !TEST_ptr(ctx2))
  176. goto err;
  177. if (!TEST_true(HMAC_Init_ex(ctx, test[7].key, test[7].key_len, EVP_sha1(), NULL))
  178. || !TEST_true(HMAC_Update(ctx, test[7].data, test[7].data_len))
  179. || !TEST_true(HMAC_CTX_copy(ctx2, ctx))
  180. || !TEST_true(HMAC_Final(ctx2, buf, &len)))
  181. goto err;
  182. p = pt(buf, len);
  183. if (!TEST_str_eq(p, (char *)test[7].digest))
  184. goto err;
  185. ret = 1;
  186. err:
  187. HMAC_CTX_free(ctx2);
  188. HMAC_CTX_free(ctx);
  189. return ret;
  190. }
  191. # ifndef OPENSSL_NO_MD5
  192. static char *pt(unsigned char *md, unsigned int len)
  193. {
  194. unsigned int i;
  195. static char buf[80];
  196. for (i = 0; i < len; i++)
  197. sprintf(&(buf[i * 2]), "%02x", md[i]);
  198. return buf;
  199. }
  200. # endif
  201. int setup_tests(void)
  202. {
  203. ADD_ALL_TESTS(test_hmac_md5, 4);
  204. ADD_TEST(test_hmac_single_shot);
  205. ADD_TEST(test_hmac_bad);
  206. ADD_TEST(test_hmac_run);
  207. ADD_TEST(test_hmac_copy);
  208. return 1;
  209. }