1 | /* |
2 | * Copyright 1995-2018 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 | #include <stdio.h> |
11 | #include <stdlib.h> |
12 | #include <string.h> |
13 | #include "internal/cryptlib.h" |
14 | #include <openssl/hmac.h> |
15 | #include <openssl/opensslconf.h> |
16 | #include "hmac_local.h" |
17 | |
18 | int HMAC_Init_ex(HMAC_CTX *ctx, const void *key, int len, |
19 | const EVP_MD *md, ENGINE *impl) |
20 | { |
21 | int rv = 0; |
22 | int i, j, reset = 0; |
23 | unsigned char pad[HMAC_MAX_MD_CBLOCK_SIZE]; |
24 | |
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 | |
29 | if (md != NULL) { |
30 | reset = 1; |
31 | ctx->md = md; |
32 | } else if (ctx->md) { |
33 | md = ctx->md; |
34 | } else { |
35 | return 0; |
36 | } |
37 | |
38 | /* |
39 | * The HMAC construction is not allowed to be used with the |
40 | * extendable-output functions (XOF) shake128 and shake256. |
41 | */ |
42 | if ((EVP_MD_flags(md) & EVP_MD_FLAG_XOF) != 0) |
43 | return 0; |
44 | |
45 | if (key != NULL) { |
46 | reset = 1; |
47 | j = EVP_MD_block_size(md); |
48 | if (!ossl_assert(j <= (int)sizeof(ctx->key))) |
49 | return 0; |
50 | if (j < len) { |
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; |
56 | } else { |
57 | if (len < 0 || len > (int)sizeof(ctx->key)) |
58 | return 0; |
59 | memcpy(ctx->key, key, len); |
60 | ctx->key_length = len; |
61 | } |
62 | if (ctx->key_length != HMAC_MAX_MD_CBLOCK_SIZE) |
63 | memset(&ctx->key[ctx->key_length], 0, |
64 | HMAC_MAX_MD_CBLOCK_SIZE - ctx->key_length); |
65 | } |
66 | |
67 | if (reset) { |
68 | for (i = 0; i < HMAC_MAX_MD_CBLOCK_SIZE; i++) |
69 | pad[i] = 0x36 ^ ctx->key[i]; |
70 | if (!EVP_DigestInit_ex(ctx->i_ctx, md, impl) |
71 | || !EVP_DigestUpdate(ctx->i_ctx, pad, EVP_MD_block_size(md))) |
72 | goto err; |
73 | |
74 | for (i = 0; i < HMAC_MAX_MD_CBLOCK_SIZE; i++) |
75 | pad[i] = 0x5c ^ ctx->key[i]; |
76 | if (!EVP_DigestInit_ex(ctx->o_ctx, md, impl) |
77 | || !EVP_DigestUpdate(ctx->o_ctx, pad, EVP_MD_block_size(md))) |
78 | goto err; |
79 | } |
80 | if (!EVP_MD_CTX_copy_ex(ctx->md_ctx, ctx->i_ctx)) |
81 | goto err; |
82 | rv = 1; |
83 | err: |
84 | if (reset) |
85 | OPENSSL_cleanse(pad, sizeof(pad)); |
86 | return rv; |
87 | } |
88 | |
89 | #ifndef OPENSSL_NO_DEPRECATED_1_1_0 |
90 | int HMAC_Init(HMAC_CTX *ctx, const void *key, int len, const EVP_MD *md) |
91 | { |
92 | if (key && md) |
93 | HMAC_CTX_reset(ctx); |
94 | return HMAC_Init_ex(ctx, key, len, md, NULL); |
95 | } |
96 | #endif |
97 | |
98 | int HMAC_Update(HMAC_CTX *ctx, const unsigned char *data, size_t len) |
99 | { |
100 | if (!ctx->md) |
101 | return 0; |
102 | return EVP_DigestUpdate(ctx->md_ctx, data, len); |
103 | } |
104 | |
105 | int HMAC_Final(HMAC_CTX *ctx, unsigned char *md, unsigned int *len) |
106 | { |
107 | unsigned int i; |
108 | unsigned char buf[EVP_MAX_MD_SIZE]; |
109 | |
110 | if (!ctx->md) |
111 | goto err; |
112 | |
113 | if (!EVP_DigestFinal_ex(ctx->md_ctx, buf, &i)) |
114 | goto err; |
115 | if (!EVP_MD_CTX_copy_ex(ctx->md_ctx, ctx->o_ctx)) |
116 | goto err; |
117 | if (!EVP_DigestUpdate(ctx->md_ctx, buf, i)) |
118 | goto err; |
119 | if (!EVP_DigestFinal_ex(ctx->md_ctx, md, len)) |
120 | goto err; |
121 | return 1; |
122 | err: |
123 | return 0; |
124 | } |
125 | |
126 | size_t HMAC_size(const HMAC_CTX *ctx) |
127 | { |
128 | int size = EVP_MD_size((ctx)->md); |
129 | |
130 | return (size < 0) ? 0 : size; |
131 | } |
132 | |
133 | HMAC_CTX *HMAC_CTX_new(void) |
134 | { |
135 | HMAC_CTX *ctx = OPENSSL_zalloc(sizeof(HMAC_CTX)); |
136 | |
137 | if (ctx != NULL) { |
138 | if (!HMAC_CTX_reset(ctx)) { |
139 | HMAC_CTX_free(ctx); |
140 | return NULL; |
141 | } |
142 | } |
143 | return ctx; |
144 | } |
145 | |
146 | static void hmac_ctx_cleanup(HMAC_CTX *ctx) |
147 | { |
148 | EVP_MD_CTX_reset(ctx->i_ctx); |
149 | EVP_MD_CTX_reset(ctx->o_ctx); |
150 | EVP_MD_CTX_reset(ctx->md_ctx); |
151 | ctx->md = NULL; |
152 | ctx->key_length = 0; |
153 | OPENSSL_cleanse(ctx->key, sizeof(ctx->key)); |
154 | } |
155 | |
156 | void HMAC_CTX_free(HMAC_CTX *ctx) |
157 | { |
158 | if (ctx != NULL) { |
159 | hmac_ctx_cleanup(ctx); |
160 | EVP_MD_CTX_free(ctx->i_ctx); |
161 | EVP_MD_CTX_free(ctx->o_ctx); |
162 | EVP_MD_CTX_free(ctx->md_ctx); |
163 | OPENSSL_free(ctx); |
164 | } |
165 | } |
166 | |
167 | static int hmac_ctx_alloc_mds(HMAC_CTX *ctx) |
168 | { |
169 | if (ctx->i_ctx == NULL) |
170 | ctx->i_ctx = EVP_MD_CTX_new(); |
171 | if (ctx->i_ctx == NULL) |
172 | return 0; |
173 | if (ctx->o_ctx == NULL) |
174 | ctx->o_ctx = EVP_MD_CTX_new(); |
175 | if (ctx->o_ctx == NULL) |
176 | return 0; |
177 | if (ctx->md_ctx == NULL) |
178 | ctx->md_ctx = EVP_MD_CTX_new(); |
179 | if (ctx->md_ctx == NULL) |
180 | return 0; |
181 | return 1; |
182 | } |
183 | |
184 | int HMAC_CTX_reset(HMAC_CTX *ctx) |
185 | { |
186 | hmac_ctx_cleanup(ctx); |
187 | if (!hmac_ctx_alloc_mds(ctx)) { |
188 | hmac_ctx_cleanup(ctx); |
189 | return 0; |
190 | } |
191 | return 1; |
192 | } |
193 | |
194 | int HMAC_CTX_copy(HMAC_CTX *dctx, HMAC_CTX *sctx) |
195 | { |
196 | if (!hmac_ctx_alloc_mds(dctx)) |
197 | goto err; |
198 | if (!EVP_MD_CTX_copy_ex(dctx->i_ctx, sctx->i_ctx)) |
199 | goto err; |
200 | if (!EVP_MD_CTX_copy_ex(dctx->o_ctx, sctx->o_ctx)) |
201 | goto err; |
202 | if (!EVP_MD_CTX_copy_ex(dctx->md_ctx, sctx->md_ctx)) |
203 | goto err; |
204 | memcpy(dctx->key, sctx->key, HMAC_MAX_MD_CBLOCK_SIZE); |
205 | dctx->key_length = sctx->key_length; |
206 | dctx->md = sctx->md; |
207 | return 1; |
208 | err: |
209 | hmac_ctx_cleanup(dctx); |
210 | return 0; |
211 | } |
212 | |
213 | unsigned char *HMAC(const EVP_MD *evp_md, const void *key, int key_len, |
214 | const unsigned char *d, size_t n, unsigned char *md, |
215 | unsigned int *md_len) |
216 | { |
217 | HMAC_CTX *c = NULL; |
218 | static unsigned char m[EVP_MAX_MD_SIZE]; |
219 | static const unsigned char dummy_key[1] = {'\0'}; |
220 | |
221 | if (md == NULL) |
222 | md = m; |
223 | if ((c = HMAC_CTX_new()) == NULL) |
224 | goto err; |
225 | |
226 | /* For HMAC_Init_ex, NULL key signals reuse. */ |
227 | if (key == NULL && key_len == 0) { |
228 | key = dummy_key; |
229 | } |
230 | |
231 | if (!HMAC_Init_ex(c, key, key_len, evp_md, NULL)) |
232 | goto err; |
233 | if (!HMAC_Update(c, d, n)) |
234 | goto err; |
235 | if (!HMAC_Final(c, md, md_len)) |
236 | goto err; |
237 | HMAC_CTX_free(c); |
238 | return md; |
239 | err: |
240 | HMAC_CTX_free(c); |
241 | return NULL; |
242 | } |
243 | |
244 | void HMAC_CTX_set_flags(HMAC_CTX *ctx, unsigned long flags) |
245 | { |
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); |
249 | } |
250 | |
251 | const EVP_MD *HMAC_CTX_get_md(const HMAC_CTX *ctx) |
252 | { |
253 | return ctx->md; |
254 | } |
255 | |