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