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