hmac.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Copyright 1995-2019 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 <stdlib.h>
  11. #include <string.h>
  12. #include "internal/cryptlib.h"
  13. #include <openssl/hmac.h>
  14. #include <openssl/opensslconf.h>
  15. #include "hmac_lcl.h"
  16. int HMAC_Init_ex(HMAC_CTX *ctx, const void *key, int len,
  17. const EVP_MD *md, ENGINE *impl)
  18. {
  19. int rv = 0;
  20. int i, j, reset = 0;
  21. unsigned char pad[HMAC_MAX_MD_CBLOCK_SIZE];
  22. /* If we are changing MD then we must have a key */
  23. if (md != NULL && md != ctx->md && (key == NULL || len < 0))
  24. return 0;
  25. if (md != NULL) {
  26. reset = 1;
  27. ctx->md = md;
  28. } else if (ctx->md) {
  29. md = ctx->md;
  30. } else {
  31. return 0;
  32. }
  33. /*
  34. * The HMAC construction is not allowed to be used with the
  35. * extendable-output functions (XOF) shake128 and shake256.
  36. */
  37. if ((EVP_MD_meth_get_flags(md) & EVP_MD_FLAG_XOF) != 0)
  38. return 0;
  39. if (key != NULL) {
  40. reset = 1;
  41. j = EVP_MD_block_size(md);
  42. if (!ossl_assert(j <= (int)sizeof(ctx->key)))
  43. return 0;
  44. if (j < len) {
  45. if (!EVP_DigestInit_ex(ctx->md_ctx, md, impl)
  46. || !EVP_DigestUpdate(ctx->md_ctx, key, len)
  47. || !EVP_DigestFinal_ex(ctx->md_ctx, ctx->key,
  48. &ctx->key_length))
  49. return 0;
  50. } else {
  51. if (len < 0 || len > (int)sizeof(ctx->key))
  52. return 0;
  53. memcpy(ctx->key, key, len);
  54. ctx->key_length = len;
  55. }
  56. if (ctx->key_length != HMAC_MAX_MD_CBLOCK_SIZE)
  57. memset(&ctx->key[ctx->key_length], 0,
  58. HMAC_MAX_MD_CBLOCK_SIZE - ctx->key_length);
  59. }
  60. if (reset) {
  61. for (i = 0; i < HMAC_MAX_MD_CBLOCK_SIZE; i++)
  62. pad[i] = 0x36 ^ ctx->key[i];
  63. if (!EVP_DigestInit_ex(ctx->i_ctx, md, impl)
  64. || !EVP_DigestUpdate(ctx->i_ctx, pad, EVP_MD_block_size(md)))
  65. goto err;
  66. for (i = 0; i < HMAC_MAX_MD_CBLOCK_SIZE; i++)
  67. pad[i] = 0x5c ^ ctx->key[i];
  68. if (!EVP_DigestInit_ex(ctx->o_ctx, md, impl)
  69. || !EVP_DigestUpdate(ctx->o_ctx, pad, EVP_MD_block_size(md)))
  70. goto err;
  71. }
  72. if (!EVP_MD_CTX_copy_ex(ctx->md_ctx, ctx->i_ctx))
  73. goto err;
  74. rv = 1;
  75. err:
  76. if (reset)
  77. OPENSSL_cleanse(pad, sizeof(pad));
  78. return rv;
  79. }
  80. #if OPENSSL_API_COMPAT < 0x10100000L
  81. int HMAC_Init(HMAC_CTX *ctx, const void *key, int len, const EVP_MD *md)
  82. {
  83. if (key && md)
  84. HMAC_CTX_reset(ctx);
  85. return HMAC_Init_ex(ctx, key, len, md, NULL);
  86. }
  87. #endif
  88. int HMAC_Update(HMAC_CTX *ctx, const unsigned char *data, size_t len)
  89. {
  90. if (!ctx->md)
  91. return 0;
  92. return EVP_DigestUpdate(ctx->md_ctx, data, len);
  93. }
  94. int HMAC_Final(HMAC_CTX *ctx, unsigned char *md, unsigned int *len)
  95. {
  96. unsigned int i;
  97. unsigned char buf[EVP_MAX_MD_SIZE];
  98. if (!ctx->md)
  99. goto err;
  100. if (!EVP_DigestFinal_ex(ctx->md_ctx, buf, &i))
  101. goto err;
  102. if (!EVP_MD_CTX_copy_ex(ctx->md_ctx, ctx->o_ctx))
  103. goto err;
  104. if (!EVP_DigestUpdate(ctx->md_ctx, buf, i))
  105. goto err;
  106. if (!EVP_DigestFinal_ex(ctx->md_ctx, md, len))
  107. goto err;
  108. return 1;
  109. err:
  110. return 0;
  111. }
  112. size_t HMAC_size(const HMAC_CTX *ctx)
  113. {
  114. int size = EVP_MD_size((ctx)->md);
  115. return (size < 0) ? 0 : size;
  116. }
  117. HMAC_CTX *HMAC_CTX_new(void)
  118. {
  119. HMAC_CTX *ctx = OPENSSL_zalloc(sizeof(HMAC_CTX));
  120. if (ctx != NULL) {
  121. if (!HMAC_CTX_reset(ctx)) {
  122. HMAC_CTX_free(ctx);
  123. return NULL;
  124. }
  125. }
  126. return ctx;
  127. }
  128. static void hmac_ctx_cleanup(HMAC_CTX *ctx)
  129. {
  130. EVP_MD_CTX_reset(ctx->i_ctx);
  131. EVP_MD_CTX_reset(ctx->o_ctx);
  132. EVP_MD_CTX_reset(ctx->md_ctx);
  133. ctx->md = NULL;
  134. ctx->key_length = 0;
  135. OPENSSL_cleanse(ctx->key, sizeof(ctx->key));
  136. }
  137. void HMAC_CTX_free(HMAC_CTX *ctx)
  138. {
  139. if (ctx != NULL) {
  140. hmac_ctx_cleanup(ctx);
  141. EVP_MD_CTX_free(ctx->i_ctx);
  142. EVP_MD_CTX_free(ctx->o_ctx);
  143. EVP_MD_CTX_free(ctx->md_ctx);
  144. OPENSSL_free(ctx);
  145. }
  146. }
  147. static int hmac_ctx_alloc_mds(HMAC_CTX *ctx)
  148. {
  149. if (ctx->i_ctx == NULL)
  150. ctx->i_ctx = EVP_MD_CTX_new();
  151. if (ctx->i_ctx == NULL)
  152. return 0;
  153. if (ctx->o_ctx == NULL)
  154. ctx->o_ctx = EVP_MD_CTX_new();
  155. if (ctx->o_ctx == NULL)
  156. return 0;
  157. if (ctx->md_ctx == NULL)
  158. ctx->md_ctx = EVP_MD_CTX_new();
  159. if (ctx->md_ctx == NULL)
  160. return 0;
  161. return 1;
  162. }
  163. int HMAC_CTX_reset(HMAC_CTX *ctx)
  164. {
  165. hmac_ctx_cleanup(ctx);
  166. if (!hmac_ctx_alloc_mds(ctx)) {
  167. hmac_ctx_cleanup(ctx);
  168. return 0;
  169. }
  170. return 1;
  171. }
  172. int HMAC_CTX_copy(HMAC_CTX *dctx, HMAC_CTX *sctx)
  173. {
  174. if (!hmac_ctx_alloc_mds(dctx))
  175. goto err;
  176. if (!EVP_MD_CTX_copy_ex(dctx->i_ctx, sctx->i_ctx))
  177. goto err;
  178. if (!EVP_MD_CTX_copy_ex(dctx->o_ctx, sctx->o_ctx))
  179. goto err;
  180. if (!EVP_MD_CTX_copy_ex(dctx->md_ctx, sctx->md_ctx))
  181. goto err;
  182. memcpy(dctx->key, sctx->key, HMAC_MAX_MD_CBLOCK_SIZE);
  183. dctx->key_length = sctx->key_length;
  184. dctx->md = sctx->md;
  185. return 1;
  186. err:
  187. hmac_ctx_cleanup(dctx);
  188. return 0;
  189. }
  190. unsigned char *HMAC(const EVP_MD *evp_md, const void *key, int key_len,
  191. const unsigned char *d, size_t n, unsigned char *md,
  192. unsigned int *md_len)
  193. {
  194. HMAC_CTX *c = NULL;
  195. static unsigned char m[EVP_MAX_MD_SIZE];
  196. static const unsigned char dummy_key[1] = {'\0'};
  197. if (md == NULL)
  198. md = m;
  199. if ((c = HMAC_CTX_new()) == NULL)
  200. goto err;
  201. /* For HMAC_Init_ex, NULL key signals reuse. */
  202. if (key == NULL && key_len == 0) {
  203. key = dummy_key;
  204. }
  205. if (!HMAC_Init_ex(c, key, key_len, evp_md, NULL))
  206. goto err;
  207. if (!HMAC_Update(c, d, n))
  208. goto err;
  209. if (!HMAC_Final(c, md, md_len))
  210. goto err;
  211. HMAC_CTX_free(c);
  212. return md;
  213. err:
  214. HMAC_CTX_free(c);
  215. return NULL;
  216. }
  217. void HMAC_CTX_set_flags(HMAC_CTX *ctx, unsigned long flags)
  218. {
  219. EVP_MD_CTX_set_flags(ctx->i_ctx, flags);
  220. EVP_MD_CTX_set_flags(ctx->o_ctx, flags);
  221. EVP_MD_CTX_set_flags(ctx->md_ctx, flags);
  222. }
  223. const EVP_MD *HMAC_CTX_get_md(const HMAC_CTX *ctx)
  224. {
  225. return ctx->md;
  226. }