hmactest.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * Copyright 1995-2016 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 "../e_os.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. # ifndef OPENSSL_NO_MD5
  22. static struct test_st {
  23. unsigned char key[16];
  24. int key_len;
  25. unsigned char data[64];
  26. int data_len;
  27. unsigned char *digest;
  28. } test[8] = {
  29. {
  30. "", 0, "More text test vectors to stuff up EBCDIC machines :-)", 54,
  31. (unsigned char *)"e9139d1e6ee064ef8cf514fc7dc83e86",
  32. },
  33. {
  34. {
  35. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  36. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  37. }, 16, "Hi There", 8,
  38. (unsigned char *)"9294727a3638bb1c13f48ef8158bfc9d",
  39. },
  40. {
  41. "Jefe", 4, "what do ya want for nothing?", 28,
  42. (unsigned char *)"750c783e6ab0b503eaa86e310a5db738",
  43. },
  44. {
  45. {
  46. 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
  47. 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
  48. }, 16, {
  49. 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
  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
  54. }, 50, (unsigned char *)"56be34521d144c88dbb8c733f0e8b3f6",
  55. },
  56. {
  57. "", 0, "My test data", 12,
  58. (unsigned char *)"61afdecb95429ef494d61fdee15990cabf0826fc"
  59. },
  60. {
  61. "", 0, "My test data", 12,
  62. (unsigned char *)"2274b195d90ce8e03406f4b526a47e0787a88a65479938f1a5baa3ce0f079776"
  63. },
  64. {
  65. "123456", 6, "My test data", 12,
  66. (unsigned char *)"bab53058ae861a7f191abe2d0145cbb123776a6369ee3f9d79ce455667e411dd"
  67. },
  68. {
  69. "12345", 5, "My test data again", 18,
  70. (unsigned char *)"a12396ceddd2a85f4c656bc1e0aa50c78cffde3e"
  71. }
  72. };
  73. # endif
  74. static char *pt(unsigned char *md, unsigned int len);
  75. int main(int argc, char *argv[])
  76. {
  77. # ifndef OPENSSL_NO_MD5
  78. int i;
  79. char *p;
  80. # endif
  81. int err = 0;
  82. HMAC_CTX *ctx = NULL, *ctx2 = NULL;
  83. unsigned char buf[EVP_MAX_MD_SIZE];
  84. unsigned int len;
  85. # ifdef OPENSSL_NO_MD5
  86. printf("test skipped: MD5 disabled\n");
  87. # else
  88. # ifdef CHARSET_EBCDIC
  89. ebcdic2ascii(test[0].data, test[0].data, test[0].data_len);
  90. ebcdic2ascii(test[1].data, test[1].data, test[1].data_len);
  91. ebcdic2ascii(test[2].key, test[2].key, test[2].key_len);
  92. ebcdic2ascii(test[2].data, test[2].data, test[2].data_len);
  93. # endif
  94. for (i = 0; i < 4; i++) {
  95. p = pt(HMAC(EVP_md5(),
  96. test[i].key, test[i].key_len,
  97. test[i].data, test[i].data_len, NULL, NULL),
  98. MD5_DIGEST_LENGTH);
  99. if (strcmp(p, (char *)test[i].digest) != 0) {
  100. printf("Error calculating HMAC on %d entry'\n", i);
  101. printf("got %s instead of %s\n", p, test[i].digest);
  102. err++;
  103. } else
  104. printf("test %d ok\n", i);
  105. }
  106. # endif /* OPENSSL_NO_MD5 */
  107. /* test4 */
  108. ctx = HMAC_CTX_new();
  109. if (ctx == NULL) {
  110. printf("HMAC malloc failure (test 4)\n");
  111. err++;
  112. goto end;
  113. }
  114. if (HMAC_CTX_get_md(ctx) != NULL) {
  115. printf("Message digest not NULL for HMAC (test 4)\n");
  116. err++;
  117. goto test5;
  118. }
  119. if (HMAC_Init_ex(ctx, NULL, 0, NULL, NULL)) {
  120. printf("Should fail to initialise HMAC with empty MD and key (test 4)\n");
  121. err++;
  122. goto test5;
  123. }
  124. if (HMAC_Update(ctx, test[4].data, test[4].data_len)) {
  125. printf("Should fail HMAC_Update with ctx not set up (test 4)\n");
  126. err++;
  127. goto test5;
  128. }
  129. if (HMAC_Init_ex(ctx, NULL, 0, EVP_sha1(), NULL)) {
  130. printf("Should fail to initialise HMAC with empty key (test 4)\n");
  131. err++;
  132. goto test5;
  133. }
  134. if (HMAC_Update(ctx, test[4].data, test[4].data_len)) {
  135. printf("Should fail HMAC_Update with ctx not set up (test 4)\n");
  136. err++;
  137. goto test5;
  138. }
  139. printf("test 4 ok\n");
  140. test5:
  141. /* Test 5 has empty key; test that single-shot accepts a NULL key. */
  142. p = pt(HMAC(EVP_sha1(), NULL, 0, test[4].data, test[4].data_len,
  143. NULL, NULL), SHA_DIGEST_LENGTH);
  144. if (strcmp(p, (char *)test[4].digest) != 0) {
  145. printf("Error calculating HMAC on %d entry'\n", i);
  146. printf("got %s instead of %s\n", p, test[4].digest);
  147. err++;
  148. }
  149. HMAC_CTX_reset(ctx);
  150. if (HMAC_CTX_get_md(ctx) != NULL) {
  151. printf("Message digest not NULL for HMAC (test 5)\n");
  152. err++;
  153. goto test6;
  154. }
  155. if (HMAC_Init_ex(ctx, test[4].key, test[4].key_len, NULL, NULL)) {
  156. printf("Should fail to initialise HMAC with empty MD (test 5)\n");
  157. err++;
  158. goto test6;
  159. }
  160. if (HMAC_Update(ctx, test[4].data, test[4].data_len)) {
  161. printf("Should fail HMAC_Update with ctx not set up (test 5)\n");
  162. err++;
  163. goto test6;
  164. }
  165. if (HMAC_Init_ex(ctx, test[4].key, -1, EVP_sha1(), NULL)) {
  166. printf("Should fail to initialise HMAC with invalid key len(test 5)\n");
  167. err++;
  168. goto test6;
  169. }
  170. if (!HMAC_Init_ex(ctx, test[4].key, test[4].key_len, EVP_sha1(), NULL)) {
  171. printf("Failed to initialise HMAC (test 5)\n");
  172. err++;
  173. goto test6;
  174. }
  175. if (!HMAC_Update(ctx, test[4].data, test[4].data_len)) {
  176. printf("Error updating HMAC with data (test 5)\n");
  177. err++;
  178. goto test6;
  179. }
  180. if (!HMAC_Final(ctx, buf, &len)) {
  181. printf("Error finalising data (test 5)\n");
  182. err++;
  183. goto test6;
  184. }
  185. p = pt(buf, len);
  186. if (strcmp(p, (char *)test[4].digest) != 0) {
  187. printf("Error calculating interim HMAC on test 5\n");
  188. printf("got %s instead of %s\n", p, test[4].digest);
  189. err++;
  190. goto test6;
  191. }
  192. if (HMAC_Init_ex(ctx, NULL, 0, EVP_sha256(), NULL)) {
  193. printf("Should disallow changing MD without a new key (test 5)\n");
  194. err++;
  195. goto test6;
  196. }
  197. if (!HMAC_Init_ex(ctx, test[5].key, test[5].key_len, EVP_sha256(), NULL)) {
  198. printf("Failed to reinitialise HMAC (test 5)\n");
  199. err++;
  200. goto test6;
  201. }
  202. if (HMAC_CTX_get_md(ctx) != EVP_sha256()) {
  203. printf("Unexpected message digest for HMAC (test 5)\n");
  204. err++;
  205. goto test6;
  206. }
  207. if (!HMAC_Update(ctx, test[5].data, test[5].data_len)) {
  208. printf("Error updating HMAC with data (sha256) (test 5)\n");
  209. err++;
  210. goto test6;
  211. }
  212. if (!HMAC_Final(ctx, buf, &len)) {
  213. printf("Error finalising data (sha256) (test 5)\n");
  214. err++;
  215. goto test6;
  216. }
  217. p = pt(buf, len);
  218. if (strcmp(p, (char *)test[5].digest) != 0) {
  219. printf("Error calculating 2nd interim HMAC on test 5\n");
  220. printf("got %s instead of %s\n", p, test[5].digest);
  221. err++;
  222. goto test6;
  223. }
  224. if (!HMAC_Init_ex(ctx, test[6].key, test[6].key_len, NULL, NULL)) {
  225. printf("Failed to reinitialise HMAC with key (test 5)\n");
  226. err++;
  227. goto test6;
  228. }
  229. if (!HMAC_Update(ctx, test[6].data, test[6].data_len)) {
  230. printf("Error updating HMAC with data (new key) (test 5)\n");
  231. err++;
  232. goto test6;
  233. }
  234. if (!HMAC_Final(ctx, buf, &len)) {
  235. printf("Error finalising data (new key) (test 5)\n");
  236. err++;
  237. goto test6;
  238. }
  239. p = pt(buf, len);
  240. if (strcmp(p, (char *)test[6].digest) != 0) {
  241. printf("error calculating HMAC on test 5\n");
  242. printf("got %s instead of %s\n", p, test[6].digest);
  243. err++;
  244. } else {
  245. printf("test 5 ok\n");
  246. }
  247. test6:
  248. HMAC_CTX_reset(ctx);
  249. ctx2 = HMAC_CTX_new();
  250. if (ctx2 == NULL) {
  251. printf("HMAC malloc failure (test 6)\n");
  252. err++;
  253. goto end;
  254. }
  255. if (!HMAC_Init_ex(ctx, test[7].key, test[7].key_len, EVP_sha1(), NULL)) {
  256. printf("Failed to initialise HMAC (test 6)\n");
  257. err++;
  258. goto end;
  259. }
  260. if (!HMAC_Update(ctx, test[7].data, test[7].data_len)) {
  261. printf("Error updating HMAC with data (test 6)\n");
  262. err++;
  263. goto end;
  264. }
  265. if (!HMAC_CTX_copy(ctx2, ctx)) {
  266. printf("Failed to copy HMAC_CTX (test 6)\n");
  267. err++;
  268. goto end;
  269. }
  270. if (!HMAC_Final(ctx2, buf, &len)) {
  271. printf("Error finalising data (test 6)\n");
  272. err++;
  273. goto end;
  274. }
  275. p = pt(buf, len);
  276. if (strcmp(p, (char *)test[7].digest) != 0) {
  277. printf("Error calculating HMAC on test 6\n");
  278. printf("got %s instead of %s\n", p, test[7].digest);
  279. err++;
  280. } else {
  281. printf("test 6 ok\n");
  282. }
  283. end:
  284. HMAC_CTX_free(ctx2);
  285. HMAC_CTX_free(ctx);
  286. EXIT(err);
  287. }
  288. # ifndef OPENSSL_NO_MD5
  289. static char *pt(unsigned char *md, unsigned int len)
  290. {
  291. unsigned int i;
  292. static char buf[80];
  293. for (i = 0; i < len; i++)
  294. sprintf(&(buf[i * 2]), "%02x", md[i]);
  295. return (buf);
  296. }
  297. # endif