2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
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
11 * HMAC low level APIs are deprecated for public use, but still ok for internal
14 #include "internal/deprecated.h"
19 #include "internal/cryptlib.h"
20 #include <openssl/hmac.h>
21 #include <openssl/opensslconf.h>
22 #include "hmac_local.h"
24 int HMAC_Init_ex(HMAC_CTX *ctx, const void *key, int len,
25 const EVP_MD *md, ENGINE *impl)
27 int rv = 0, reset = 0;
29 unsigned char pad[HMAC_MAX_MD_CBLOCK_SIZE];
30 unsigned int keytmp_length;
31 unsigned char keytmp[HMAC_MAX_MD_CBLOCK_SIZE];
33 /* If we are changing MD then we must have a key */
34 if (md != NULL && md != ctx->md && (key == NULL || len < 0))
46 * The HMAC construction is not allowed to be used with the
47 * extendable-output functions (XOF) shake128 and shake256.
49 if ((EVP_MD_flags(md) & EVP_MD_FLAG_XOF) != 0)
55 j = EVP_MD_block_size(md);
56 if (!ossl_assert(j <= (int)sizeof(keytmp)))
59 if (!EVP_DigestInit_ex(ctx->md_ctx, md, impl)
60 || !EVP_DigestUpdate(ctx->md_ctx, key, len)
61 || !EVP_DigestFinal_ex(ctx->md_ctx, keytmp,
65 if (len < 0 || len > (int)sizeof(keytmp))
67 memcpy(keytmp, key, len);
70 if (keytmp_length != HMAC_MAX_MD_CBLOCK_SIZE)
71 memset(&keytmp[keytmp_length], 0,
72 HMAC_MAX_MD_CBLOCK_SIZE - keytmp_length);
74 for (i = 0; i < HMAC_MAX_MD_CBLOCK_SIZE; i++)
75 pad[i] = 0x36 ^ keytmp[i];
76 if (!EVP_DigestInit_ex(ctx->i_ctx, md, impl)
77 || !EVP_DigestUpdate(ctx->i_ctx, pad, EVP_MD_block_size(md)))
80 for (i = 0; i < HMAC_MAX_MD_CBLOCK_SIZE; i++)
81 pad[i] = 0x5c ^ keytmp[i];
82 if (!EVP_DigestInit_ex(ctx->o_ctx, md, impl)
83 || !EVP_DigestUpdate(ctx->o_ctx, pad, EVP_MD_block_size(md)))
86 if (!EVP_MD_CTX_copy_ex(ctx->md_ctx, ctx->i_ctx))
91 OPENSSL_cleanse(keytmp, sizeof(keytmp));
92 OPENSSL_cleanse(pad, sizeof(pad));
97 #ifndef OPENSSL_NO_DEPRECATED_1_1_0
98 int HMAC_Init(HMAC_CTX *ctx, const void *key, int len, const EVP_MD *md)
102 return HMAC_Init_ex(ctx, key, len, md, NULL);
106 int HMAC_Update(HMAC_CTX *ctx, const unsigned char *data, size_t len)
110 return EVP_DigestUpdate(ctx->md_ctx, data, len);
113 int HMAC_Final(HMAC_CTX *ctx, unsigned char *md, unsigned int *len)
116 unsigned char buf[EVP_MAX_MD_SIZE];
121 if (!EVP_DigestFinal_ex(ctx->md_ctx, buf, &i))
123 if (!EVP_MD_CTX_copy_ex(ctx->md_ctx, ctx->o_ctx))
125 if (!EVP_DigestUpdate(ctx->md_ctx, buf, i))
127 if (!EVP_DigestFinal_ex(ctx->md_ctx, md, len))
134 size_t HMAC_size(const HMAC_CTX *ctx)
136 int size = EVP_MD_size((ctx)->md);
138 return (size < 0) ? 0 : size;
141 HMAC_CTX *HMAC_CTX_new(void)
143 HMAC_CTX *ctx = OPENSSL_zalloc(sizeof(HMAC_CTX));
146 if (!HMAC_CTX_reset(ctx)) {
154 static void hmac_ctx_cleanup(HMAC_CTX *ctx)
156 EVP_MD_CTX_reset(ctx->i_ctx);
157 EVP_MD_CTX_reset(ctx->o_ctx);
158 EVP_MD_CTX_reset(ctx->md_ctx);
162 void HMAC_CTX_free(HMAC_CTX *ctx)
165 hmac_ctx_cleanup(ctx);
166 EVP_MD_CTX_free(ctx->i_ctx);
167 EVP_MD_CTX_free(ctx->o_ctx);
168 EVP_MD_CTX_free(ctx->md_ctx);
173 static int hmac_ctx_alloc_mds(HMAC_CTX *ctx)
175 if (ctx->i_ctx == NULL)
176 ctx->i_ctx = EVP_MD_CTX_new();
177 if (ctx->i_ctx == NULL)
179 if (ctx->o_ctx == NULL)
180 ctx->o_ctx = EVP_MD_CTX_new();
181 if (ctx->o_ctx == NULL)
183 if (ctx->md_ctx == NULL)
184 ctx->md_ctx = EVP_MD_CTX_new();
185 if (ctx->md_ctx == NULL)
190 int HMAC_CTX_reset(HMAC_CTX *ctx)
192 hmac_ctx_cleanup(ctx);
193 if (!hmac_ctx_alloc_mds(ctx)) {
194 hmac_ctx_cleanup(ctx);
200 int HMAC_CTX_copy(HMAC_CTX *dctx, HMAC_CTX *sctx)
202 if (!hmac_ctx_alloc_mds(dctx))
204 if (!EVP_MD_CTX_copy_ex(dctx->i_ctx, sctx->i_ctx))
206 if (!EVP_MD_CTX_copy_ex(dctx->o_ctx, sctx->o_ctx))
208 if (!EVP_MD_CTX_copy_ex(dctx->md_ctx, sctx->md_ctx))
213 hmac_ctx_cleanup(dctx);
217 unsigned char *HMAC(const EVP_MD *evp_md, const void *key, int key_len,
218 const unsigned char *d, size_t n, unsigned char *md,
219 unsigned int *md_len)
222 static unsigned char m[EVP_MAX_MD_SIZE];
223 static const unsigned char dummy_key[1] = {'\0'};
227 if ((c = HMAC_CTX_new()) == NULL)
230 /* For HMAC_Init_ex, NULL key signals reuse. */
231 if (key == NULL && key_len == 0) {
235 if (!HMAC_Init_ex(c, key, key_len, evp_md, NULL))
237 if (!HMAC_Update(c, d, n))
239 if (!HMAC_Final(c, md, md_len))
248 void HMAC_CTX_set_flags(HMAC_CTX *ctx, unsigned long flags)
250 EVP_MD_CTX_set_flags(ctx->i_ctx, flags);
251 EVP_MD_CTX_set_flags(ctx->o_ctx, flags);
252 EVP_MD_CTX_set_flags(ctx->md_ctx, flags);
255 const EVP_MD *HMAC_CTX_get_md(const HMAC_CTX *ctx)