2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
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
13 #include "internal/cryptlib.h"
14 #include <openssl/hmac.h>
15 #include <openssl/opensslconf.h>
18 int HMAC_Init_ex(HMAC_CTX *ctx, const void *key, int len,
19 const EVP_MD *md, ENGINE *impl)
23 unsigned char pad[HMAC_MAX_MD_CBLOCK_SIZE];
25 /* If we are changing MD then we must have a key */
26 if (md != NULL && md != ctx->md && (key == NULL || len < 0))
40 j = EVP_MD_block_size(md);
41 if (!ossl_assert(j <= (int)sizeof(ctx->key)))
44 if (!EVP_DigestInit_ex(ctx->md_ctx, md, impl)
45 || !EVP_DigestUpdate(ctx->md_ctx, key, len)
46 || !EVP_DigestFinal_ex(ctx->md_ctx, ctx->key,
50 if (len < 0 || len > (int)sizeof(ctx->key))
52 memcpy(ctx->key, key, len);
53 ctx->key_length = len;
55 if (ctx->key_length != HMAC_MAX_MD_CBLOCK_SIZE)
56 memset(&ctx->key[ctx->key_length], 0,
57 HMAC_MAX_MD_CBLOCK_SIZE - ctx->key_length);
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)))
67 for (i = 0; i < HMAC_MAX_MD_CBLOCK_SIZE; i++)
68 pad[i] = 0x5c ^ ctx->key[i];
69 if (!EVP_DigestInit_ex(ctx->o_ctx, md, impl)
70 || !EVP_DigestUpdate(ctx->o_ctx, pad, EVP_MD_block_size(md)))
73 if (!EVP_MD_CTX_copy_ex(ctx->md_ctx, ctx->i_ctx))
78 OPENSSL_cleanse(pad, sizeof(pad));
82 #if !OPENSSL_API_1_1_0
83 int HMAC_Init(HMAC_CTX *ctx, const void *key, int len, const EVP_MD *md)
87 return HMAC_Init_ex(ctx, key, len, md, NULL);
91 int HMAC_Update(HMAC_CTX *ctx, const unsigned char *data, size_t len)
95 return EVP_DigestUpdate(ctx->md_ctx, data, len);
98 int HMAC_Final(HMAC_CTX *ctx, unsigned char *md, unsigned int *len)
101 unsigned char buf[EVP_MAX_MD_SIZE];
106 if (!EVP_DigestFinal_ex(ctx->md_ctx, buf, &i))
108 if (!EVP_MD_CTX_copy_ex(ctx->md_ctx, ctx->o_ctx))
110 if (!EVP_DigestUpdate(ctx->md_ctx, buf, i))
112 if (!EVP_DigestFinal_ex(ctx->md_ctx, md, len))
119 size_t HMAC_size(const HMAC_CTX *ctx)
121 int size = EVP_MD_size((ctx)->md);
123 return (size < 0) ? 0 : size;
126 HMAC_CTX *HMAC_CTX_new(void)
128 HMAC_CTX *ctx = OPENSSL_zalloc(sizeof(HMAC_CTX));
131 if (!HMAC_CTX_reset(ctx)) {
139 static void hmac_ctx_cleanup(HMAC_CTX *ctx)
141 EVP_MD_CTX_reset(ctx->i_ctx);
142 EVP_MD_CTX_reset(ctx->o_ctx);
143 EVP_MD_CTX_reset(ctx->md_ctx);
146 OPENSSL_cleanse(ctx->key, sizeof(ctx->key));
149 void HMAC_CTX_free(HMAC_CTX *ctx)
152 hmac_ctx_cleanup(ctx);
153 EVP_MD_CTX_free(ctx->i_ctx);
154 EVP_MD_CTX_free(ctx->o_ctx);
155 EVP_MD_CTX_free(ctx->md_ctx);
160 static int hmac_ctx_alloc_mds(HMAC_CTX *ctx)
162 if (ctx->i_ctx == NULL)
163 ctx->i_ctx = EVP_MD_CTX_new();
164 if (ctx->i_ctx == NULL)
166 if (ctx->o_ctx == NULL)
167 ctx->o_ctx = EVP_MD_CTX_new();
168 if (ctx->o_ctx == NULL)
170 if (ctx->md_ctx == NULL)
171 ctx->md_ctx = EVP_MD_CTX_new();
172 if (ctx->md_ctx == NULL)
177 int HMAC_CTX_reset(HMAC_CTX *ctx)
179 hmac_ctx_cleanup(ctx);
180 if (!hmac_ctx_alloc_mds(ctx)) {
181 hmac_ctx_cleanup(ctx);
187 int HMAC_CTX_copy(HMAC_CTX *dctx, HMAC_CTX *sctx)
189 if (!hmac_ctx_alloc_mds(dctx))
191 if (!EVP_MD_CTX_copy_ex(dctx->i_ctx, sctx->i_ctx))
193 if (!EVP_MD_CTX_copy_ex(dctx->o_ctx, sctx->o_ctx))
195 if (!EVP_MD_CTX_copy_ex(dctx->md_ctx, sctx->md_ctx))
197 memcpy(dctx->key, sctx->key, HMAC_MAX_MD_CBLOCK_SIZE);
198 dctx->key_length = sctx->key_length;
202 hmac_ctx_cleanup(dctx);
206 unsigned char *HMAC(const EVP_MD *evp_md, const void *key, int key_len,
207 const unsigned char *d, size_t n, unsigned char *md,
208 unsigned int *md_len)
211 static unsigned char m[EVP_MAX_MD_SIZE];
212 static const unsigned char dummy_key[1] = {'\0'};
216 if ((c = HMAC_CTX_new()) == NULL)
219 /* For HMAC_Init_ex, NULL key signals reuse. */
220 if (key == NULL && key_len == 0) {
224 if (!HMAC_Init_ex(c, key, key_len, evp_md, NULL))
226 if (!HMAC_Update(c, d, n))
228 if (!HMAC_Final(c, md, md_len))
237 void HMAC_CTX_set_flags(HMAC_CTX *ctx, unsigned long flags)
239 EVP_MD_CTX_set_flags(ctx->i_ctx, flags);
240 EVP_MD_CTX_set_flags(ctx->o_ctx, flags);
241 EVP_MD_CTX_set_flags(ctx->md_ctx, flags);
244 const EVP_MD *HMAC_CTX_get_md(const HMAC_CTX *ctx)