2 * Copyright 1995-2016 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)
22 unsigned char pad[HMAC_MAX_MD_CBLOCK];
24 /* If we are changing MD then we must have a key */
25 if (md != NULL && md != ctx->md && (key == NULL || len < 0))
39 j = EVP_MD_block_size(md);
40 if (!ossl_assert(j <= (int)sizeof(ctx->key)))
43 if (!EVP_DigestInit_ex(ctx->md_ctx, md, impl))
45 if (!EVP_DigestUpdate(ctx->md_ctx, key, len))
47 if (!EVP_DigestFinal_ex(ctx->md_ctx, ctx->key,
51 if (len < 0 || len > (int)sizeof(ctx->key))
53 memcpy(ctx->key, key, len);
54 ctx->key_length = len;
56 if (ctx->key_length != HMAC_MAX_MD_CBLOCK)
57 memset(&ctx->key[ctx->key_length], 0,
58 HMAC_MAX_MD_CBLOCK - ctx->key_length);
62 for (i = 0; i < HMAC_MAX_MD_CBLOCK; i++)
63 pad[i] = 0x36 ^ ctx->key[i];
64 if (!EVP_DigestInit_ex(ctx->i_ctx, md, impl))
66 if (!EVP_DigestUpdate(ctx->i_ctx, pad, EVP_MD_block_size(md)))
69 for (i = 0; i < HMAC_MAX_MD_CBLOCK; i++)
70 pad[i] = 0x5c ^ ctx->key[i];
71 if (!EVP_DigestInit_ex(ctx->o_ctx, md, impl))
73 if (!EVP_DigestUpdate(ctx->o_ctx, pad, EVP_MD_block_size(md)))
76 if (!EVP_MD_CTX_copy_ex(ctx->md_ctx, ctx->i_ctx))
83 #if OPENSSL_API_COMPAT < 0x10100000L
84 int HMAC_Init(HMAC_CTX *ctx, const void *key, int len, const EVP_MD *md)
88 return HMAC_Init_ex(ctx, key, len, md, NULL);
92 int HMAC_Update(HMAC_CTX *ctx, const unsigned char *data, size_t len)
96 return EVP_DigestUpdate(ctx->md_ctx, data, len);
99 int HMAC_Final(HMAC_CTX *ctx, unsigned char *md, unsigned int *len)
102 unsigned char buf[EVP_MAX_MD_SIZE];
107 if (!EVP_DigestFinal_ex(ctx->md_ctx, buf, &i))
109 if (!EVP_MD_CTX_copy_ex(ctx->md_ctx, ctx->o_ctx))
111 if (!EVP_DigestUpdate(ctx->md_ctx, buf, i))
113 if (!EVP_DigestFinal_ex(ctx->md_ctx, md, len))
120 size_t HMAC_size(const HMAC_CTX *ctx)
122 int size = EVP_MD_size((ctx)->md);
124 return (size < 0) ? 0 : size;
127 HMAC_CTX *HMAC_CTX_new(void)
129 HMAC_CTX *ctx = OPENSSL_zalloc(sizeof(HMAC_CTX));
132 if (!HMAC_CTX_reset(ctx)) {
140 static void hmac_ctx_cleanup(HMAC_CTX *ctx)
142 EVP_MD_CTX_reset(ctx->i_ctx);
143 EVP_MD_CTX_reset(ctx->o_ctx);
144 EVP_MD_CTX_reset(ctx->md_ctx);
147 OPENSSL_cleanse(ctx->key, sizeof(ctx->key));
150 void HMAC_CTX_free(HMAC_CTX *ctx)
153 hmac_ctx_cleanup(ctx);
154 EVP_MD_CTX_free(ctx->i_ctx);
155 EVP_MD_CTX_free(ctx->o_ctx);
156 EVP_MD_CTX_free(ctx->md_ctx);
161 static int hmac_ctx_alloc_mds(HMAC_CTX *ctx)
163 if (ctx->i_ctx == NULL)
164 ctx->i_ctx = EVP_MD_CTX_new();
165 if (ctx->i_ctx == NULL)
167 if (ctx->o_ctx == NULL)
168 ctx->o_ctx = EVP_MD_CTX_new();
169 if (ctx->o_ctx == NULL)
171 if (ctx->md_ctx == NULL)
172 ctx->md_ctx = EVP_MD_CTX_new();
173 if (ctx->md_ctx == NULL)
178 int HMAC_CTX_reset(HMAC_CTX *ctx)
180 hmac_ctx_cleanup(ctx);
181 if (!hmac_ctx_alloc_mds(ctx)) {
182 hmac_ctx_cleanup(ctx);
188 int HMAC_CTX_copy(HMAC_CTX *dctx, HMAC_CTX *sctx)
190 if (!hmac_ctx_alloc_mds(dctx))
192 if (!EVP_MD_CTX_copy_ex(dctx->i_ctx, sctx->i_ctx))
194 if (!EVP_MD_CTX_copy_ex(dctx->o_ctx, sctx->o_ctx))
196 if (!EVP_MD_CTX_copy_ex(dctx->md_ctx, sctx->md_ctx))
198 memcpy(dctx->key, sctx->key, HMAC_MAX_MD_CBLOCK);
199 dctx->key_length = sctx->key_length;
203 hmac_ctx_cleanup(dctx);
207 unsigned char *HMAC(const EVP_MD *evp_md, const void *key, int key_len,
208 const unsigned char *d, size_t n, unsigned char *md,
209 unsigned int *md_len)
212 static unsigned char m[EVP_MAX_MD_SIZE];
213 static const unsigned char dummy_key[1] = {'\0'};
217 if ((c = HMAC_CTX_new()) == NULL)
220 /* For HMAC_Init_ex, NULL key signals reuse. */
221 if (key == NULL && key_len == 0) {
225 if (!HMAC_Init_ex(c, key, key_len, evp_md, NULL))
227 if (!HMAC_Update(c, d, n))
229 if (!HMAC_Final(c, md, md_len))
238 void HMAC_CTX_set_flags(HMAC_CTX *ctx, unsigned long flags)
240 EVP_MD_CTX_set_flags(ctx->i_ctx, flags);
241 EVP_MD_CTX_set_flags(ctx->o_ctx, flags);
242 EVP_MD_CTX_set_flags(ctx->md_ctx, flags);
245 const EVP_MD *HMAC_CTX_get_md(const HMAC_CTX *ctx)