hmac.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Copyright 1995-2024 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_xof(md))
  44. return 0;
  45. #ifdef OPENSSL_HMAC_S390X
  46. rv = s390x_HMAC_init(ctx, key, len, impl);
  47. if (rv >= 1)
  48. return rv;
  49. #endif
  50. if (key != NULL) {
  51. reset = 1;
  52. j = EVP_MD_get_block_size(md);
  53. if (!ossl_assert(j <= (int)sizeof(keytmp)))
  54. return 0;
  55. if (j < 0)
  56. return 0;
  57. if (j < len) {
  58. if (!EVP_DigestInit_ex(ctx->md_ctx, md, impl)
  59. || !EVP_DigestUpdate(ctx->md_ctx, key, len)
  60. || !EVP_DigestFinal_ex(ctx->md_ctx, keytmp,
  61. &keytmp_length))
  62. return 0;
  63. } else {
  64. if (len < 0 || len > (int)sizeof(keytmp))
  65. return 0;
  66. memcpy(keytmp, key, len);
  67. keytmp_length = len;
  68. }
  69. if (keytmp_length != HMAC_MAX_MD_CBLOCK_SIZE)
  70. memset(&keytmp[keytmp_length], 0,
  71. HMAC_MAX_MD_CBLOCK_SIZE - keytmp_length);
  72. for (i = 0; i < HMAC_MAX_MD_CBLOCK_SIZE; i++)
  73. pad[i] = 0x36 ^ keytmp[i];
  74. if (!EVP_DigestInit_ex(ctx->i_ctx, md, impl)
  75. || !EVP_DigestUpdate(ctx->i_ctx, pad,
  76. EVP_MD_get_block_size(md)))
  77. goto err;
  78. for (i = 0; i < HMAC_MAX_MD_CBLOCK_SIZE; i++)
  79. pad[i] = 0x5c ^ keytmp[i];
  80. if (!EVP_DigestInit_ex(ctx->o_ctx, md, impl)
  81. || !EVP_DigestUpdate(ctx->o_ctx, pad,
  82. EVP_MD_get_block_size(md)))
  83. goto err;
  84. }
  85. if (!EVP_MD_CTX_copy_ex(ctx->md_ctx, ctx->i_ctx))
  86. goto err;
  87. rv = 1;
  88. err:
  89. if (reset) {
  90. OPENSSL_cleanse(keytmp, sizeof(keytmp));
  91. OPENSSL_cleanse(pad, sizeof(pad));
  92. }
  93. return rv;
  94. }
  95. #ifndef OPENSSL_NO_DEPRECATED_1_1_0
  96. int HMAC_Init(HMAC_CTX *ctx, const void *key, int len, const EVP_MD *md)
  97. {
  98. if (key && md)
  99. HMAC_CTX_reset(ctx);
  100. return HMAC_Init_ex(ctx, key, len, md, NULL);
  101. }
  102. #endif
  103. int HMAC_Update(HMAC_CTX *ctx, const unsigned char *data, size_t len)
  104. {
  105. if (!ctx->md)
  106. return 0;
  107. #ifdef OPENSSL_HMAC_S390X
  108. if (ctx->plat.s390x.fc)
  109. return s390x_HMAC_update(ctx, data, len);
  110. #endif
  111. return EVP_DigestUpdate(ctx->md_ctx, data, len);
  112. }
  113. int HMAC_Final(HMAC_CTX *ctx, unsigned char *md, unsigned int *len)
  114. {
  115. unsigned int i;
  116. unsigned char buf[EVP_MAX_MD_SIZE];
  117. if (!ctx->md)
  118. goto err;
  119. #ifdef OPENSSL_HMAC_S390X
  120. if (ctx->plat.s390x.fc)
  121. return s390x_HMAC_final(ctx, md, len);
  122. #endif
  123. if (!EVP_DigestFinal_ex(ctx->md_ctx, buf, &i))
  124. goto err;
  125. if (!EVP_MD_CTX_copy_ex(ctx->md_ctx, ctx->o_ctx))
  126. goto err;
  127. if (!EVP_DigestUpdate(ctx->md_ctx, buf, i))
  128. goto err;
  129. if (!EVP_DigestFinal_ex(ctx->md_ctx, md, len))
  130. goto err;
  131. return 1;
  132. err:
  133. return 0;
  134. }
  135. size_t HMAC_size(const HMAC_CTX *ctx)
  136. {
  137. int size = EVP_MD_get_size((ctx)->md);
  138. return (size < 0) ? 0 : size;
  139. }
  140. HMAC_CTX *HMAC_CTX_new(void)
  141. {
  142. HMAC_CTX *ctx = OPENSSL_zalloc(sizeof(HMAC_CTX));
  143. if (ctx != NULL) {
  144. if (!HMAC_CTX_reset(ctx)) {
  145. HMAC_CTX_free(ctx);
  146. return NULL;
  147. }
  148. }
  149. return ctx;
  150. }
  151. static void hmac_ctx_cleanup(HMAC_CTX *ctx)
  152. {
  153. EVP_MD_CTX_reset(ctx->i_ctx);
  154. EVP_MD_CTX_reset(ctx->o_ctx);
  155. EVP_MD_CTX_reset(ctx->md_ctx);
  156. ctx->md = NULL;
  157. #ifdef OPENSSL_HMAC_S390X
  158. s390x_HMAC_CTX_cleanup(ctx);
  159. #endif
  160. }
  161. void HMAC_CTX_free(HMAC_CTX *ctx)
  162. {
  163. if (ctx != NULL) {
  164. hmac_ctx_cleanup(ctx);
  165. EVP_MD_CTX_free(ctx->i_ctx);
  166. EVP_MD_CTX_free(ctx->o_ctx);
  167. EVP_MD_CTX_free(ctx->md_ctx);
  168. OPENSSL_free(ctx);
  169. }
  170. }
  171. static int hmac_ctx_alloc_mds(HMAC_CTX *ctx)
  172. {
  173. if (ctx->i_ctx == NULL)
  174. ctx->i_ctx = EVP_MD_CTX_new();
  175. if (ctx->i_ctx == NULL)
  176. return 0;
  177. if (ctx->o_ctx == NULL)
  178. ctx->o_ctx = EVP_MD_CTX_new();
  179. if (ctx->o_ctx == NULL)
  180. return 0;
  181. if (ctx->md_ctx == NULL)
  182. ctx->md_ctx = EVP_MD_CTX_new();
  183. if (ctx->md_ctx == NULL)
  184. return 0;
  185. return 1;
  186. }
  187. int HMAC_CTX_reset(HMAC_CTX *ctx)
  188. {
  189. hmac_ctx_cleanup(ctx);
  190. if (!hmac_ctx_alloc_mds(ctx)) {
  191. hmac_ctx_cleanup(ctx);
  192. return 0;
  193. }
  194. return 1;
  195. }
  196. int HMAC_CTX_copy(HMAC_CTX *dctx, HMAC_CTX *sctx)
  197. {
  198. if (!hmac_ctx_alloc_mds(dctx))
  199. goto err;
  200. if (!EVP_MD_CTX_copy_ex(dctx->i_ctx, sctx->i_ctx))
  201. goto err;
  202. if (!EVP_MD_CTX_copy_ex(dctx->o_ctx, sctx->o_ctx))
  203. goto err;
  204. if (!EVP_MD_CTX_copy_ex(dctx->md_ctx, sctx->md_ctx))
  205. goto err;
  206. dctx->md = sctx->md;
  207. #ifdef OPENSSL_HMAC_S390X
  208. if (s390x_HMAC_CTX_copy(dctx, sctx) == 0)
  209. goto err;
  210. #endif
  211. return 1;
  212. err:
  213. hmac_ctx_cleanup(dctx);
  214. return 0;
  215. }
  216. unsigned char *HMAC(const EVP_MD *evp_md, const void *key, int key_len,
  217. const unsigned char *data, size_t data_len,
  218. unsigned char *md, unsigned int *md_len)
  219. {
  220. static unsigned char static_md[EVP_MAX_MD_SIZE];
  221. int size = EVP_MD_get_size(evp_md);
  222. size_t temp_md_len = 0;
  223. unsigned char *ret = NULL;
  224. if (size > 0) {
  225. ret = EVP_Q_mac(NULL, "HMAC", NULL, EVP_MD_get0_name(evp_md), NULL,
  226. key, key_len, data, data_len,
  227. md == NULL ? static_md : md, size, &temp_md_len);
  228. if (md_len != NULL)
  229. *md_len = (unsigned int)temp_md_len;
  230. }
  231. return ret;
  232. }
  233. void HMAC_CTX_set_flags(HMAC_CTX *ctx, unsigned long flags)
  234. {
  235. EVP_MD_CTX_set_flags(ctx->i_ctx, flags);
  236. EVP_MD_CTX_set_flags(ctx->o_ctx, flags);
  237. EVP_MD_CTX_set_flags(ctx->md_ctx, flags);
  238. }
  239. const EVP_MD *HMAC_CTX_get_md(const HMAC_CTX *ctx)
  240. {
  241. return ctx->md;
  242. }