]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/hmac/hmac.c
Reorganize local header files
[thirdparty/openssl.git] / crypto / hmac / hmac.c
CommitLineData
d2e9e320 1/*
2d28a42f 2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
58964a49 3 *
6f888e05 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
d2e9e320
RS
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
58964a49 8 */
6dff52e8 9
58964a49
RE
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
b39fc560 13#include "internal/cryptlib.h"
25a66ee3 14#include <openssl/hmac.h>
98186eb4 15#include <openssl/opensslconf.h>
706457b7 16#include "hmac_local.h"
58964a49 17
6343829a 18int HMAC_Init_ex(HMAC_CTX *ctx, const void *key, int len,
0f113f3e
MC
19 const EVP_MD *md, ENGINE *impl)
20{
2d28a42f 21 int rv = 0;
0f113f3e 22 int i, j, reset = 0;
e0810e35 23 unsigned char pad[HMAC_MAX_MD_CBLOCK_SIZE];
0f113f3e 24
4b464e7b
MC
25 /* If we are changing MD then we must have a key */
26 if (md != NULL && md != ctx->md && (key == NULL || len < 0))
27 return 0;
28
0f113f3e
MC
29 if (md != NULL) {
30 reset = 1;
31 ctx->md = md;
61986d32 32 } else if (ctx->md) {
0f113f3e 33 md = ctx->md;
e2095c65
MC
34 } else {
35 return 0;
36 }
37
48fdeca0
MC
38 /*
39 * The HMAC construction is not allowed to be used with the
40 * extendable-output functions (XOF) shake128 and shake256.
41 */
5acb2be5 42 if ((EVP_MD_flags(md) & EVP_MD_FLAG_XOF) != 0)
48fdeca0
MC
43 return 0;
44
0f113f3e
MC
45 if (key != NULL) {
46 reset = 1;
fa0c23de 47 j = EVP_MD_block_size(md);
8f9ee7a3 48 if (!ossl_assert(j <= (int)sizeof(ctx->key)))
2d28a42f 49 return 0;
0f113f3e 50 if (j < len) {
2d28a42f
SL
51 if (!EVP_DigestInit_ex(ctx->md_ctx, md, impl)
52 || !EVP_DigestUpdate(ctx->md_ctx, key, len)
53 || !EVP_DigestFinal_ex(ctx->md_ctx, ctx->key,
54 &ctx->key_length))
55 return 0;
0f113f3e 56 } else {
61986d32 57 if (len < 0 || len > (int)sizeof(ctx->key))
c62e94d8 58 return 0;
0f113f3e
MC
59 memcpy(ctx->key, key, len);
60 ctx->key_length = len;
61 }
e0810e35 62 if (ctx->key_length != HMAC_MAX_MD_CBLOCK_SIZE)
0f113f3e 63 memset(&ctx->key[ctx->key_length], 0,
e0810e35 64 HMAC_MAX_MD_CBLOCK_SIZE - ctx->key_length);
0f113f3e
MC
65 }
66
67 if (reset) {
e0810e35 68 for (i = 0; i < HMAC_MAX_MD_CBLOCK_SIZE; i++)
0f113f3e 69 pad[i] = 0x36 ^ ctx->key[i];
2d28a42f
SL
70 if (!EVP_DigestInit_ex(ctx->i_ctx, md, impl)
71 || !EVP_DigestUpdate(ctx->i_ctx, pad, EVP_MD_block_size(md)))
0f113f3e
MC
72 goto err;
73
e0810e35 74 for (i = 0; i < HMAC_MAX_MD_CBLOCK_SIZE; i++)
0f113f3e 75 pad[i] = 0x5c ^ ctx->key[i];
2d28a42f
SL
76 if (!EVP_DigestInit_ex(ctx->o_ctx, md, impl)
77 || !EVP_DigestUpdate(ctx->o_ctx, pad, EVP_MD_block_size(md)))
0f113f3e
MC
78 goto err;
79 }
fa0c23de 80 if (!EVP_MD_CTX_copy_ex(ctx->md_ctx, ctx->i_ctx))
0f113f3e 81 goto err;
2d28a42f 82 rv = 1;
0f113f3e 83 err:
2d28a42f
SL
84 if (reset)
85 OPENSSL_cleanse(pad, sizeof(pad));
86 return rv;
0f113f3e 87}
58964a49 88
fcd2d5a6 89#if !OPENSSL_API_1_1_0
6343829a 90int HMAC_Init(HMAC_CTX *ctx, const void *key, int len, const EVP_MD *md)
0f113f3e
MC
91{
92 if (key && md)
a87a0a6e 93 HMAC_CTX_reset(ctx);
0f113f3e
MC
94 return HMAC_Init_ex(ctx, key, len, md, NULL);
95}
00a5a74b 96#endif
ff3fa48f 97
87d52468 98int HMAC_Update(HMAC_CTX *ctx, const unsigned char *data, size_t len)
0f113f3e 99{
4b464e7b 100 if (!ctx->md)
e2095c65 101 return 0;
fa0c23de 102 return EVP_DigestUpdate(ctx->md_ctx, data, len);
0f113f3e 103}
58964a49 104
87d52468 105int HMAC_Final(HMAC_CTX *ctx, unsigned char *md, unsigned int *len)
0f113f3e
MC
106{
107 unsigned int i;
108 unsigned char buf[EVP_MAX_MD_SIZE];
109
4b464e7b 110 if (!ctx->md)
e2095c65
MC
111 goto err;
112
fa0c23de 113 if (!EVP_DigestFinal_ex(ctx->md_ctx, buf, &i))
0f113f3e 114 goto err;
fa0c23de 115 if (!EVP_MD_CTX_copy_ex(ctx->md_ctx, ctx->o_ctx))
0f113f3e 116 goto err;
fa0c23de 117 if (!EVP_DigestUpdate(ctx->md_ctx, buf, i))
0f113f3e 118 goto err;
fa0c23de 119 if (!EVP_DigestFinal_ex(ctx->md_ctx, md, len))
0f113f3e
MC
120 goto err;
121 return 1;
122 err:
123 return 0;
124}
dbad1690 125
2194351f 126size_t HMAC_size(const HMAC_CTX *ctx)
3f43aecc 127{
708e06c5 128 int size = EVP_MD_size((ctx)->md);
d0ee717c
MC
129
130 return (size < 0) ? 0 : size;
3f43aecc
RL
131}
132
133HMAC_CTX *HMAC_CTX_new(void)
134{
43ecb9c3
RS
135 HMAC_CTX *ctx = OPENSSL_zalloc(sizeof(HMAC_CTX));
136
137 if (ctx != NULL) {
dc0099e1 138 if (!HMAC_CTX_reset(ctx)) {
3f43aecc 139 HMAC_CTX_free(ctx);
43ecb9c3 140 return NULL;
3f43aecc 141 }
43ecb9c3 142 }
3f43aecc
RL
143 return ctx;
144}
145
32fd54a9
RL
146static void hmac_ctx_cleanup(HMAC_CTX *ctx)
147{
bfb0641f
RL
148 EVP_MD_CTX_reset(ctx->i_ctx);
149 EVP_MD_CTX_reset(ctx->o_ctx);
150 EVP_MD_CTX_reset(ctx->md_ctx);
32fd54a9
RL
151 ctx->md = NULL;
152 ctx->key_length = 0;
85cbc182 153 OPENSSL_cleanse(ctx->key, sizeof(ctx->key));
32fd54a9
RL
154}
155
3f43aecc
RL
156void HMAC_CTX_free(HMAC_CTX *ctx)
157{
158 if (ctx != NULL) {
32fd54a9 159 hmac_ctx_cleanup(ctx);
bfb0641f
RL
160 EVP_MD_CTX_free(ctx->i_ctx);
161 EVP_MD_CTX_free(ctx->o_ctx);
162 EVP_MD_CTX_free(ctx->md_ctx);
3f43aecc
RL
163 OPENSSL_free(ctx);
164 }
165}
166
b98530d6 167static int hmac_ctx_alloc_mds(HMAC_CTX *ctx)
0f113f3e 168{
fa0c23de 169 if (ctx->i_ctx == NULL)
bfb0641f 170 ctx->i_ctx = EVP_MD_CTX_new();
fa0c23de 171 if (ctx->i_ctx == NULL)
b98530d6 172 return 0;
fa0c23de 173 if (ctx->o_ctx == NULL)
bfb0641f 174 ctx->o_ctx = EVP_MD_CTX_new();
fa0c23de 175 if (ctx->o_ctx == NULL)
b98530d6 176 return 0;
fa0c23de 177 if (ctx->md_ctx == NULL)
bfb0641f 178 ctx->md_ctx = EVP_MD_CTX_new();
fa0c23de 179 if (ctx->md_ctx == NULL)
b98530d6 180 return 0;
fa0c23de 181 return 1;
b98530d6
GN
182}
183
184int HMAC_CTX_reset(HMAC_CTX *ctx)
185{
32fd54a9 186 hmac_ctx_cleanup(ctx);
b98530d6
GN
187 if (!hmac_ctx_alloc_mds(ctx)) {
188 hmac_ctx_cleanup(ctx);
189 return 0;
190 }
191 return 1;
0f113f3e 192}
58964a49 193
87d52468 194int HMAC_CTX_copy(HMAC_CTX *dctx, HMAC_CTX *sctx)
0f113f3e 195{
b98530d6 196 if (!hmac_ctx_alloc_mds(dctx))
fa0c23de
RL
197 goto err;
198 if (!EVP_MD_CTX_copy_ex(dctx->i_ctx, sctx->i_ctx))
0f113f3e 199 goto err;
fa0c23de 200 if (!EVP_MD_CTX_copy_ex(dctx->o_ctx, sctx->o_ctx))
0f113f3e 201 goto err;
fa0c23de 202 if (!EVP_MD_CTX_copy_ex(dctx->md_ctx, sctx->md_ctx))
0f113f3e 203 goto err;
e0810e35 204 memcpy(dctx->key, sctx->key, HMAC_MAX_MD_CBLOCK_SIZE);
4b464e7b 205 dctx->key_length = sctx->key_length;
0f113f3e
MC
206 dctx->md = sctx->md;
207 return 1;
208 err:
32fd54a9 209 hmac_ctx_cleanup(dctx);
0f113f3e
MC
210 return 0;
211}
74633553 212
6343829a 213unsigned char *HMAC(const EVP_MD *evp_md, const void *key, int key_len,
0f113f3e
MC
214 const unsigned char *d, size_t n, unsigned char *md,
215 unsigned int *md_len)
216{
3f43aecc 217 HMAC_CTX *c = NULL;
0f113f3e 218 static unsigned char m[EVP_MAX_MD_SIZE];
b1413d9b 219 static const unsigned char dummy_key[1] = {'\0'};
0f113f3e
MC
220
221 if (md == NULL)
222 md = m;
3f43aecc
RL
223 if ((c = HMAC_CTX_new()) == NULL)
224 goto err;
b1413d9b
EK
225
226 /* For HMAC_Init_ex, NULL key signals reuse. */
227 if (key == NULL && key_len == 0) {
228 key = dummy_key;
229 }
230
3f43aecc 231 if (!HMAC_Init_ex(c, key, key_len, evp_md, NULL))
0f113f3e 232 goto err;
3f43aecc 233 if (!HMAC_Update(c, d, n))
0f113f3e 234 goto err;
3f43aecc 235 if (!HMAC_Final(c, md, md_len))
0f113f3e 236 goto err;
3f43aecc 237 HMAC_CTX_free(c);
0f113f3e
MC
238 return md;
239 err:
3f43aecc 240 HMAC_CTX_free(c);
0f113f3e
MC
241 return NULL;
242}
58964a49 243
e92f9f45 244void HMAC_CTX_set_flags(HMAC_CTX *ctx, unsigned long flags)
0f113f3e 245{
fa0c23de
RL
246 EVP_MD_CTX_set_flags(ctx->i_ctx, flags);
247 EVP_MD_CTX_set_flags(ctx->o_ctx, flags);
248 EVP_MD_CTX_set_flags(ctx->md_ctx, flags);
0f113f3e 249}
a6211814
MC
250
251const EVP_MD *HMAC_CTX_get_md(const HMAC_CTX *ctx)
252{
253 return ctx->md;
254}