hmac.c 6.5 KB

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