/src/openssl/crypto/hmac/hmac.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2024 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/opensslconf.h> |
21 | | #include <openssl/hmac.h> |
22 | | #include <openssl/core_names.h> |
23 | | #include "hmac_local.h" |
24 | | |
25 | | int HMAC_Init_ex(HMAC_CTX *ctx, const void *key, int len, |
26 | | const EVP_MD *md, ENGINE *impl) |
27 | 23.3k | { |
28 | 23.3k | int rv = 0, reset = 0; |
29 | 23.3k | int i, j; |
30 | 23.3k | unsigned char pad[HMAC_MAX_MD_CBLOCK_SIZE]; |
31 | 23.3k | unsigned int keytmp_length; |
32 | 23.3k | unsigned char keytmp[HMAC_MAX_MD_CBLOCK_SIZE]; |
33 | | |
34 | | /* If we are changing MD then we must have a key */ |
35 | 23.3k | if (md != NULL && md != ctx->md && (key == NULL || len < 0)) |
36 | 16 | return 0; |
37 | | |
38 | 23.3k | if (md != NULL) |
39 | 3.35k | ctx->md = md; |
40 | 19.9k | else if (ctx->md != NULL) |
41 | 19.9k | md = ctx->md; |
42 | 0 | else |
43 | 0 | return 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 | 23.3k | if (EVP_MD_xof(md)) |
50 | 166 | return 0; |
51 | | |
52 | | #ifdef OPENSSL_HMAC_S390X |
53 | | rv = s390x_HMAC_init(ctx, key, len, impl); |
54 | | if (rv >= 1) |
55 | | return rv; |
56 | | #endif |
57 | | |
58 | 23.1k | if (key != NULL) { |
59 | 3.18k | reset = 1; |
60 | | |
61 | 3.18k | j = EVP_MD_get_block_size(md); |
62 | 3.18k | if (!ossl_assert(j <= (int)sizeof(keytmp))) |
63 | 0 | return 0; |
64 | 3.18k | if (j < 0) |
65 | 0 | return 0; |
66 | 3.18k | if (j < len) { |
67 | 751 | if (!EVP_DigestInit_ex(ctx->md_ctx, md, impl) |
68 | 751 | || !EVP_DigestUpdate(ctx->md_ctx, key, len) |
69 | 751 | || !EVP_DigestFinal_ex(ctx->md_ctx, keytmp, |
70 | 725 | &keytmp_length)) |
71 | 26 | return 0; |
72 | 2.43k | } else { |
73 | 2.43k | if (len < 0 || len > (int)sizeof(keytmp)) |
74 | 0 | return 0; |
75 | 2.43k | memcpy(keytmp, key, len); |
76 | 2.43k | keytmp_length = len; |
77 | 2.43k | } |
78 | 3.16k | if (keytmp_length != HMAC_MAX_MD_CBLOCK_SIZE) |
79 | 3.06k | memset(&keytmp[keytmp_length], 0, |
80 | 3.06k | HMAC_MAX_MD_CBLOCK_SIZE - keytmp_length); |
81 | | |
82 | 458k | for (i = 0; i < HMAC_MAX_MD_CBLOCK_SIZE; i++) |
83 | 455k | pad[i] = 0x36 ^ keytmp[i]; |
84 | 3.16k | if (!EVP_DigestInit_ex(ctx->i_ctx, md, impl) |
85 | 3.16k | || !EVP_DigestUpdate(ctx->i_ctx, pad, |
86 | 3.06k | EVP_MD_get_block_size(md))) |
87 | 97 | goto err; |
88 | | |
89 | 444k | for (i = 0; i < HMAC_MAX_MD_CBLOCK_SIZE; i++) |
90 | 441k | pad[i] = 0x5c ^ keytmp[i]; |
91 | 3.06k | if (!EVP_DigestInit_ex(ctx->o_ctx, md, impl) |
92 | 3.06k | || !EVP_DigestUpdate(ctx->o_ctx, pad, |
93 | 3.06k | EVP_MD_get_block_size(md))) |
94 | 0 | goto err; |
95 | 3.06k | } |
96 | 23.0k | if (!EVP_MD_CTX_copy_ex(ctx->md_ctx, ctx->i_ctx)) |
97 | 0 | goto err; |
98 | 23.0k | rv = 1; |
99 | 23.1k | err: |
100 | 23.1k | if (reset) { |
101 | 3.16k | OPENSSL_cleanse(keytmp, sizeof(keytmp)); |
102 | 3.16k | OPENSSL_cleanse(pad, sizeof(pad)); |
103 | 3.16k | } |
104 | 23.1k | return rv; |
105 | 23.0k | } |
106 | | |
107 | | #ifndef OPENSSL_NO_DEPRECATED_1_1_0 |
108 | | int HMAC_Init(HMAC_CTX *ctx, const void *key, int len, const EVP_MD *md) |
109 | | { |
110 | | if (key && md) |
111 | | HMAC_CTX_reset(ctx); |
112 | | return HMAC_Init_ex(ctx, key, len, md, NULL); |
113 | | } |
114 | | #endif |
115 | | |
116 | | int HMAC_Update(HMAC_CTX *ctx, const unsigned char *data, size_t len) |
117 | 183k | { |
118 | 183k | if (!ctx->md) |
119 | 0 | return 0; |
120 | | |
121 | | #ifdef OPENSSL_HMAC_S390X |
122 | | if (ctx->plat.s390x.fc) |
123 | | return s390x_HMAC_update(ctx, data, len); |
124 | | #endif |
125 | | |
126 | 183k | return EVP_DigestUpdate(ctx->md_ctx, data, len); |
127 | 183k | } |
128 | | |
129 | | int HMAC_Final(HMAC_CTX *ctx, unsigned char *md, unsigned int *len) |
130 | 41.2k | { |
131 | 41.2k | unsigned int i; |
132 | 41.2k | unsigned char buf[EVP_MAX_MD_SIZE]; |
133 | | |
134 | 41.2k | if (!ctx->md) |
135 | 0 | goto err; |
136 | | |
137 | | #ifdef OPENSSL_HMAC_S390X |
138 | | if (ctx->plat.s390x.fc) |
139 | | return s390x_HMAC_final(ctx, md, len); |
140 | | #endif |
141 | | |
142 | 41.2k | if (!EVP_DigestFinal_ex(ctx->md_ctx, buf, &i)) |
143 | 0 | goto err; |
144 | 41.2k | if (!EVP_MD_CTX_copy_ex(ctx->md_ctx, ctx->o_ctx)) |
145 | 0 | goto err; |
146 | 41.2k | if (!EVP_DigestUpdate(ctx->md_ctx, buf, i)) |
147 | 0 | goto err; |
148 | 41.2k | if (!EVP_DigestFinal_ex(ctx->md_ctx, md, len)) |
149 | 0 | goto err; |
150 | 41.2k | return 1; |
151 | 0 | err: |
152 | 0 | return 0; |
153 | 41.2k | } |
154 | | |
155 | | size_t HMAC_size(const HMAC_CTX *ctx) |
156 | 16.4k | { |
157 | 16.4k | int size = EVP_MD_get_size((ctx)->md); |
158 | | |
159 | 16.4k | return (size < 0) ? 0 : size; |
160 | 16.4k | } |
161 | | |
162 | | HMAC_CTX *HMAC_CTX_new(void) |
163 | 77.9k | { |
164 | 77.9k | HMAC_CTX *ctx = OPENSSL_zalloc(sizeof(HMAC_CTX)); |
165 | | |
166 | 77.9k | if (ctx != NULL) { |
167 | 77.9k | if (!HMAC_CTX_reset(ctx)) { |
168 | 0 | HMAC_CTX_free(ctx); |
169 | 0 | return NULL; |
170 | 0 | } |
171 | 77.9k | } |
172 | 77.9k | return ctx; |
173 | 77.9k | } |
174 | | |
175 | | static void hmac_ctx_cleanup(HMAC_CTX *ctx) |
176 | 156k | { |
177 | 156k | EVP_MD_CTX_reset(ctx->i_ctx); |
178 | 156k | EVP_MD_CTX_reset(ctx->o_ctx); |
179 | 156k | EVP_MD_CTX_reset(ctx->md_ctx); |
180 | 156k | ctx->md = NULL; |
181 | | |
182 | | #ifdef OPENSSL_HMAC_S390X |
183 | | s390x_HMAC_CTX_cleanup(ctx); |
184 | | #endif |
185 | 156k | } |
186 | | |
187 | | void HMAC_CTX_free(HMAC_CTX *ctx) |
188 | 77.9k | { |
189 | 77.9k | if (ctx != NULL) { |
190 | 77.9k | hmac_ctx_cleanup(ctx); |
191 | 77.9k | EVP_MD_CTX_free(ctx->i_ctx); |
192 | 77.9k | EVP_MD_CTX_free(ctx->o_ctx); |
193 | 77.9k | EVP_MD_CTX_free(ctx->md_ctx); |
194 | 77.9k | OPENSSL_free(ctx); |
195 | 77.9k | } |
196 | 77.9k | } |
197 | | |
198 | | static int hmac_ctx_alloc_mds(HMAC_CTX *ctx) |
199 | 156k | { |
200 | 156k | if (ctx->i_ctx == NULL) |
201 | 77.9k | ctx->i_ctx = EVP_MD_CTX_new(); |
202 | 156k | if (ctx->i_ctx == NULL) |
203 | 0 | return 0; |
204 | 156k | if (ctx->o_ctx == NULL) |
205 | 77.9k | ctx->o_ctx = EVP_MD_CTX_new(); |
206 | 156k | if (ctx->o_ctx == NULL) |
207 | 0 | return 0; |
208 | 156k | if (ctx->md_ctx == NULL) |
209 | 77.9k | ctx->md_ctx = EVP_MD_CTX_new(); |
210 | 156k | if (ctx->md_ctx == NULL) |
211 | 0 | return 0; |
212 | 156k | return 1; |
213 | 156k | } |
214 | | |
215 | | int HMAC_CTX_reset(HMAC_CTX *ctx) |
216 | 78.5k | { |
217 | 78.5k | hmac_ctx_cleanup(ctx); |
218 | 78.5k | if (!hmac_ctx_alloc_mds(ctx)) { |
219 | 0 | hmac_ctx_cleanup(ctx); |
220 | 0 | return 0; |
221 | 0 | } |
222 | 78.5k | return 1; |
223 | 78.5k | } |
224 | | |
225 | | int HMAC_CTX_copy(HMAC_CTX *dctx, HMAC_CTX *sctx) |
226 | 78.2k | { |
227 | 78.2k | if (!hmac_ctx_alloc_mds(dctx)) |
228 | 0 | goto err; |
229 | 78.2k | if (!EVP_MD_CTX_copy_ex(dctx->i_ctx, sctx->i_ctx)) |
230 | 0 | goto err; |
231 | 78.2k | if (!EVP_MD_CTX_copy_ex(dctx->o_ctx, sctx->o_ctx)) |
232 | 0 | goto err; |
233 | 78.2k | if (!EVP_MD_CTX_copy_ex(dctx->md_ctx, sctx->md_ctx)) |
234 | 0 | goto err; |
235 | 78.2k | dctx->md = sctx->md; |
236 | | |
237 | | #ifdef OPENSSL_HMAC_S390X |
238 | | if (s390x_HMAC_CTX_copy(dctx, sctx) == 0) |
239 | | goto err; |
240 | | #endif |
241 | | |
242 | 78.2k | return 1; |
243 | 0 | err: |
244 | 0 | hmac_ctx_cleanup(dctx); |
245 | 0 | return 0; |
246 | 78.2k | } |
247 | | |
248 | | unsigned char *HMAC(const EVP_MD *evp_md, const void *key, int key_len, |
249 | | const unsigned char *data, size_t data_len, |
250 | | unsigned char *md, unsigned int *md_len) |
251 | | { |
252 | | static unsigned char static_md[EVP_MAX_MD_SIZE]; |
253 | | int size = EVP_MD_get_size(evp_md); |
254 | | size_t temp_md_len = 0; |
255 | | unsigned char *ret = NULL; |
256 | | |
257 | | if (size > 0) { |
258 | | ret = EVP_Q_mac(NULL, "HMAC", NULL, EVP_MD_get0_name(evp_md), NULL, |
259 | | key, key_len, data, data_len, |
260 | | md == NULL ? static_md : md, size, &temp_md_len); |
261 | | if (md_len != NULL) |
262 | | *md_len = (unsigned int)temp_md_len; |
263 | | } |
264 | | return ret; |
265 | | } |
266 | | |
267 | | void HMAC_CTX_set_flags(HMAC_CTX *ctx, unsigned long flags) |
268 | 0 | { |
269 | 0 | EVP_MD_CTX_set_flags(ctx->i_ctx, flags); |
270 | 0 | EVP_MD_CTX_set_flags(ctx->o_ctx, flags); |
271 | 0 | EVP_MD_CTX_set_flags(ctx->md_ctx, flags); |
272 | 0 | } |
273 | | |
274 | | const EVP_MD *HMAC_CTX_get_md(const HMAC_CTX *ctx) |
275 | 0 | { |
276 | 0 | return ctx->md; |
277 | 0 | } |