summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/hmac
diff options
context:
space:
mode:
authorcvs2svn <admin@example.com>2025-04-14 17:32:06 +0000
committercvs2svn <admin@example.com>2025-04-14 17:32:06 +0000
commiteb8dd9dca1228af0cd132f515509051ecfabf6f6 (patch)
treeedb6da6af7e865d488dc1a29309f1e1ec226e603 /src/lib/libcrypto/hmac
parent247f0352e0ed72a4f476db9dc91f4d982bc83eb2 (diff)
downloadopenbsd-tb_20250414.tar.gz
openbsd-tb_20250414.tar.bz2
openbsd-tb_20250414.zip
This commit was manufactured by cvs2git to create tag 'tb_20250414'.tb_20250414
Diffstat (limited to 'src/lib/libcrypto/hmac')
-rw-r--r--src/lib/libcrypto/hmac/hm_ameth.c171
-rw-r--r--src/lib/libcrypto/hmac/hm_pmeth.c261
-rw-r--r--src/lib/libcrypto/hmac/hmac.c276
-rw-r--r--src/lib/libcrypto/hmac/hmac.h101
-rw-r--r--src/lib/libcrypto/hmac/hmac_local.h83
5 files changed, 0 insertions, 892 deletions
diff --git a/src/lib/libcrypto/hmac/hm_ameth.c b/src/lib/libcrypto/hmac/hm_ameth.c
deleted file mode 100644
index 8bb1dc786f..0000000000
--- a/src/lib/libcrypto/hmac/hm_ameth.c
+++ /dev/null
@@ -1,171 +0,0 @@
1/* $OpenBSD: hm_ameth.c,v 1.20 2024/01/04 17:01:26 tb Exp $ */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2007.
4 */
5/* ====================================================================
6 * Copyright (c) 2007 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59#include <limits.h>
60#include <stdio.h>
61#include <string.h>
62
63#include <openssl/evp.h>
64#include <openssl/hmac.h>
65
66#include "asn1_local.h"
67#include "bytestring.h"
68#include "evp_local.h"
69#include "hmac_local.h"
70
71static int
72hmac_pkey_public_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
73{
74 /* The ameth pub_cmp must return 1 on match, 0 on mismatch. */
75 return ASN1_OCTET_STRING_cmp(a->pkey.ptr, b->pkey.ptr) == 0;
76}
77
78static int
79hmac_size(const EVP_PKEY *pkey)
80{
81 return EVP_MAX_MD_SIZE;
82}
83
84static void
85hmac_key_free(EVP_PKEY *pkey)
86{
87 ASN1_OCTET_STRING *os;
88
89 if ((os = pkey->pkey.ptr) == NULL)
90 return;
91
92 if (os->data != NULL)
93 explicit_bzero(os->data, os->length);
94
95 ASN1_OCTET_STRING_free(os);
96}
97
98static int
99hmac_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
100{
101 switch (op) {
102 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
103 *(int *)arg2 = NID_sha1;
104 return 1;
105 default:
106 return -2;
107 }
108}
109
110static int
111hmac_set_priv_key(EVP_PKEY *pkey, const unsigned char *priv, size_t len)
112{
113 ASN1_OCTET_STRING *os = NULL;
114
115 if (pkey->pkey.ptr != NULL)
116 goto err;
117
118 if (len > INT_MAX)
119 goto err;
120
121 if ((os = ASN1_OCTET_STRING_new()) == NULL)
122 goto err;
123
124 if (!ASN1_OCTET_STRING_set(os, priv, len))
125 goto err;
126
127 pkey->pkey.ptr = os;
128
129 return 1;
130
131 err:
132 ASN1_OCTET_STRING_free(os);
133
134 return 0;
135}
136
137static int
138hmac_get_priv_key(const EVP_PKEY *pkey, unsigned char *priv, size_t *len)
139{
140 ASN1_OCTET_STRING *os;
141 CBS cbs;
142
143 if ((os = pkey->pkey.ptr) == NULL)
144 return 0;
145
146 if (priv == NULL) {
147 *len = os->length;
148 return 1;
149 }
150
151 CBS_init(&cbs, os->data, os->length);
152 return CBS_write_bytes(&cbs, priv, *len, len);
153}
154
155const EVP_PKEY_ASN1_METHOD hmac_asn1_meth = {
156 .base_method = &hmac_asn1_meth,
157 .pkey_id = EVP_PKEY_HMAC,
158
159 .pem_str = "HMAC",
160 .info = "OpenSSL HMAC method",
161
162 .pub_cmp = hmac_pkey_public_cmp,
163
164 .pkey_size = hmac_size,
165
166 .pkey_free = hmac_key_free,
167 .pkey_ctrl = hmac_pkey_ctrl,
168
169 .set_priv_key = hmac_set_priv_key,
170 .get_priv_key = hmac_get_priv_key,
171};
diff --git a/src/lib/libcrypto/hmac/hm_pmeth.c b/src/lib/libcrypto/hmac/hm_pmeth.c
deleted file mode 100644
index 05eb1bf85d..0000000000
--- a/src/lib/libcrypto/hmac/hm_pmeth.c
+++ /dev/null
@@ -1,261 +0,0 @@
1/* $OpenBSD: hm_pmeth.c,v 1.17 2023/12/28 22:00:56 tb Exp $ */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2007.
4 */
5/* ====================================================================
6 * Copyright (c) 2007 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59#include <stdio.h>
60#include <string.h>
61
62#include <openssl/evp.h>
63#include <openssl/hmac.h>
64#include <openssl/x509.h>
65#include <openssl/x509v3.h>
66
67#include "evp_local.h"
68#include "hmac_local.h"
69
70/* HMAC pkey context structure */
71
72typedef struct {
73 const EVP_MD *md; /* MD for HMAC use */
74 ASN1_OCTET_STRING ktmp; /* Temp storage for key */
75 HMAC_CTX ctx;
76} HMAC_PKEY_CTX;
77
78static int
79pkey_hmac_init(EVP_PKEY_CTX *ctx)
80{
81 HMAC_PKEY_CTX *hctx;
82
83 if ((hctx = calloc(1, sizeof(HMAC_PKEY_CTX))) == NULL)
84 return 0;
85
86 hctx->ktmp.type = V_ASN1_OCTET_STRING;
87 HMAC_CTX_init(&hctx->ctx);
88
89 ctx->data = hctx;
90 ctx->keygen_info_count = 0;
91
92 return 1;
93}
94
95static int
96pkey_hmac_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
97{
98 HMAC_PKEY_CTX *sctx, *dctx;
99
100 if (!pkey_hmac_init(dst))
101 return 0;
102 sctx = src->data;
103 dctx = dst->data;
104 dctx->md = sctx->md;
105 HMAC_CTX_init(&dctx->ctx);
106 if (!HMAC_CTX_copy(&dctx->ctx, &sctx->ctx))
107 return 0;
108 if (sctx->ktmp.data) {
109 if (!ASN1_OCTET_STRING_set(&dctx->ktmp, sctx->ktmp.data,
110 sctx->ktmp.length))
111 return 0;
112 }
113 return 1;
114}
115
116static void
117pkey_hmac_cleanup(EVP_PKEY_CTX *ctx)
118{
119 HMAC_PKEY_CTX *hctx;
120
121 if ((hctx = ctx->data) == NULL)
122 return;
123
124 HMAC_CTX_cleanup(&hctx->ctx);
125 freezero(hctx->ktmp.data, hctx->ktmp.length);
126 free(hctx);
127}
128
129static int
130pkey_hmac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
131{
132 ASN1_OCTET_STRING *hkey = NULL;
133 HMAC_PKEY_CTX *hctx = ctx->data;
134 int ret = 0;
135
136 if (hctx->ktmp.data == NULL)
137 goto err;
138 if ((hkey = ASN1_OCTET_STRING_dup(&hctx->ktmp)) == NULL)
139 goto err;
140 if (!EVP_PKEY_assign(pkey, EVP_PKEY_HMAC, hkey))
141 goto err;
142 hkey = NULL;
143
144 ret = 1;
145
146 err:
147 ASN1_OCTET_STRING_free(hkey);
148
149 return ret;
150}
151
152static int
153int_update(EVP_MD_CTX *ctx, const void *data, size_t count)
154{
155 HMAC_PKEY_CTX *hctx = ctx->pctx->data;
156
157 if (!HMAC_Update(&hctx->ctx, data, count))
158 return 0;
159 return 1;
160}
161
162static int
163hmac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
164{
165 HMAC_PKEY_CTX *hctx = ctx->data;
166
167 HMAC_CTX_set_flags(&hctx->ctx, mctx->flags & ~EVP_MD_CTX_FLAG_NO_INIT);
168 EVP_MD_CTX_set_flags(mctx, EVP_MD_CTX_FLAG_NO_INIT);
169 mctx->update = int_update;
170 return 1;
171}
172
173static int
174hmac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
175 EVP_MD_CTX *mctx)
176{
177 unsigned int hlen;
178 HMAC_PKEY_CTX *hctx = ctx->data;
179 int l = EVP_MD_CTX_size(mctx);
180
181 if (l < 0)
182 return 0;
183 *siglen = l;
184 if (!sig)
185 return 1;
186
187 if (!HMAC_Final(&hctx->ctx, sig, &hlen))
188 return 0;
189 *siglen = (size_t)hlen;
190 return 1;
191}
192
193static int
194pkey_hmac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
195{
196 HMAC_PKEY_CTX *hctx = ctx->data;
197 ASN1_OCTET_STRING *key;
198
199 switch (type) {
200 case EVP_PKEY_CTRL_SET_MAC_KEY:
201 if ((!p2 && p1 > 0) || (p1 < -1))
202 return 0;
203 if (!ASN1_OCTET_STRING_set(&hctx->ktmp, p2, p1))
204 return 0;
205 break;
206
207 case EVP_PKEY_CTRL_MD:
208 hctx->md = p2;
209 break;
210
211 case EVP_PKEY_CTRL_DIGESTINIT:
212 key = ctx->pkey->pkey.ptr;
213 if (!HMAC_Init_ex(&hctx->ctx, key->data, key->length, hctx->md,
214 NULL))
215 return 0;
216 break;
217
218 default:
219 return -2;
220 }
221 return 1;
222}
223
224static int
225pkey_hmac_ctrl_str(EVP_PKEY_CTX *ctx, const char *type, const char *value)
226{
227 if (!value)
228 return 0;
229 if (!strcmp(type, "key")) {
230 void *p = (void *)value;
231 return pkey_hmac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, -1, p);
232 }
233 if (!strcmp(type, "hexkey")) {
234 unsigned char *key;
235 int r;
236 long keylen;
237 key = string_to_hex(value, &keylen);
238 if (!key)
239 return 0;
240 r = pkey_hmac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, keylen, key);
241 free(key);
242 return r;
243 }
244 return -2;
245}
246
247const EVP_PKEY_METHOD hmac_pkey_meth = {
248 .pkey_id = EVP_PKEY_HMAC,
249
250 .init = pkey_hmac_init,
251 .copy = pkey_hmac_copy,
252 .cleanup = pkey_hmac_cleanup,
253
254 .keygen = pkey_hmac_keygen,
255
256 .signctx_init = hmac_signctx_init,
257 .signctx = hmac_signctx,
258
259 .ctrl = pkey_hmac_ctrl,
260 .ctrl_str = pkey_hmac_ctrl_str
261};
diff --git a/src/lib/libcrypto/hmac/hmac.c b/src/lib/libcrypto/hmac/hmac.c
deleted file mode 100644
index dc1614d3ce..0000000000
--- a/src/lib/libcrypto/hmac/hmac.c
+++ /dev/null
@@ -1,276 +0,0 @@
1/* $OpenBSD: hmac.c,v 1.36 2024/08/31 10:42:21 tb Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <stdlib.h>
61#include <string.h>
62
63#include <openssl/err.h>
64#include <openssl/hmac.h>
65
66#include "evp_local.h"
67#include "hmac_local.h"
68
69int
70HMAC_Init_ex(HMAC_CTX *ctx, const void *key, int len, const EVP_MD *md,
71 ENGINE *impl)
72{
73 int i, j, reset = 0;
74 unsigned char pad[HMAC_MAX_MD_CBLOCK];
75
76 /* If we are changing MD then we must have a key */
77 if (md != NULL && md != ctx->md && (key == NULL || len < 0))
78 return 0;
79
80 if (md != NULL) {
81 reset = 1;
82 ctx->md = md;
83 } else if (ctx->md != NULL)
84 md = ctx->md;
85 else
86 return 0;
87
88 if (key != NULL) {
89 reset = 1;
90 j = EVP_MD_block_size(md);
91 if ((size_t)j > sizeof(ctx->key)) {
92 EVPerror(EVP_R_BAD_BLOCK_LENGTH);
93 goto err;
94 }
95 if (j < len) {
96 if (!EVP_DigestInit_ex(&ctx->md_ctx, md, impl))
97 goto err;
98 if (!EVP_DigestUpdate(&ctx->md_ctx, key, len))
99 goto err;
100 if (!EVP_DigestFinal_ex(&(ctx->md_ctx), ctx->key,
101 &ctx->key_length))
102 goto err;
103 } else {
104 if (len < 0 || (size_t)len > sizeof(ctx->key)) {
105 EVPerror(EVP_R_BAD_KEY_LENGTH);
106 goto err;
107 }
108 memcpy(ctx->key, key, len);
109 ctx->key_length = len;
110 }
111 if (ctx->key_length != HMAC_MAX_MD_CBLOCK)
112 memset(&ctx->key[ctx->key_length], 0,
113 HMAC_MAX_MD_CBLOCK - ctx->key_length);
114 }
115
116 if (reset) {
117 for (i = 0; i < HMAC_MAX_MD_CBLOCK; i++)
118 pad[i] = 0x36 ^ ctx->key[i];
119 if (!EVP_DigestInit_ex(&ctx->i_ctx, md, impl))
120 goto err;
121 if (!EVP_DigestUpdate(&ctx->i_ctx, pad, EVP_MD_block_size(md)))
122 goto err;
123
124 for (i = 0; i < HMAC_MAX_MD_CBLOCK; i++)
125 pad[i] = 0x5c ^ ctx->key[i];
126 if (!EVP_DigestInit_ex(&ctx->o_ctx, md, impl))
127 goto err;
128 if (!EVP_DigestUpdate(&ctx->o_ctx, pad, EVP_MD_block_size(md)))
129 goto err;
130 }
131 if (!EVP_MD_CTX_copy_ex(&ctx->md_ctx, &ctx->i_ctx))
132 goto err;
133 return 1;
134err:
135 return 0;
136}
137LCRYPTO_ALIAS(HMAC_Init_ex);
138
139int
140HMAC_Update(HMAC_CTX *ctx, const unsigned char *data, size_t len)
141{
142 if (ctx->md == NULL)
143 return 0;
144
145 return EVP_DigestUpdate(&ctx->md_ctx, data, len);
146}
147LCRYPTO_ALIAS(HMAC_Update);
148
149int
150HMAC_Final(HMAC_CTX *ctx, unsigned char *md, unsigned int *len)
151{
152 unsigned int i;
153 unsigned char buf[EVP_MAX_MD_SIZE];
154
155 if (ctx->md == NULL)
156 goto err;
157
158 if (!EVP_DigestFinal_ex(&ctx->md_ctx, buf, &i))
159 goto err;
160 if (!EVP_MD_CTX_copy_ex(&ctx->md_ctx, &ctx->o_ctx))
161 goto err;
162 if (!EVP_DigestUpdate(&ctx->md_ctx, buf, i))
163 goto err;
164 if (!EVP_DigestFinal_ex(&ctx->md_ctx, md, len))
165 goto err;
166 return 1;
167err:
168 return 0;
169}
170LCRYPTO_ALIAS(HMAC_Final);
171
172HMAC_CTX *
173HMAC_CTX_new(void)
174{
175 return calloc(1, sizeof(HMAC_CTX));
176}
177LCRYPTO_ALIAS(HMAC_CTX_new);
178
179void
180HMAC_CTX_free(HMAC_CTX *ctx)
181{
182 if (ctx == NULL)
183 return;
184
185 HMAC_CTX_cleanup(ctx);
186
187 free(ctx);
188}
189LCRYPTO_ALIAS(HMAC_CTX_free);
190
191int
192HMAC_CTX_reset(HMAC_CTX *ctx)
193{
194 HMAC_CTX_cleanup(ctx);
195 HMAC_CTX_init(ctx);
196 return 1;
197}
198LCRYPTO_ALIAS(HMAC_CTX_reset);
199
200void
201HMAC_CTX_init(HMAC_CTX *ctx)
202{
203 EVP_MD_CTX_legacy_clear(&ctx->i_ctx);
204 EVP_MD_CTX_legacy_clear(&ctx->o_ctx);
205 EVP_MD_CTX_legacy_clear(&ctx->md_ctx);
206 ctx->md = NULL;
207}
208
209int
210HMAC_CTX_copy(HMAC_CTX *dctx, HMAC_CTX *sctx)
211{
212 if (!EVP_MD_CTX_copy(&dctx->i_ctx, &sctx->i_ctx))
213 goto err;
214 if (!EVP_MD_CTX_copy(&dctx->o_ctx, &sctx->o_ctx))
215 goto err;
216 if (!EVP_MD_CTX_copy(&dctx->md_ctx, &sctx->md_ctx))
217 goto err;
218 memcpy(dctx->key, sctx->key, HMAC_MAX_MD_CBLOCK);
219 dctx->key_length = sctx->key_length;
220 dctx->md = sctx->md;
221 return 1;
222err:
223 return 0;
224}
225LCRYPTO_ALIAS(HMAC_CTX_copy);
226
227void
228HMAC_CTX_cleanup(HMAC_CTX *ctx)
229{
230 EVP_MD_CTX_cleanup(&ctx->i_ctx);
231 EVP_MD_CTX_cleanup(&ctx->o_ctx);
232 EVP_MD_CTX_cleanup(&ctx->md_ctx);
233 explicit_bzero(ctx, sizeof(*ctx));
234}
235
236void
237HMAC_CTX_set_flags(HMAC_CTX *ctx, unsigned long flags)
238{
239 EVP_MD_CTX_set_flags(&ctx->i_ctx, flags);
240 EVP_MD_CTX_set_flags(&ctx->o_ctx, flags);
241 EVP_MD_CTX_set_flags(&ctx->md_ctx, flags);
242}
243LCRYPTO_ALIAS(HMAC_CTX_set_flags);
244
245const EVP_MD *
246HMAC_CTX_get_md(const HMAC_CTX *ctx)
247{
248 return ctx->md;
249}
250LCRYPTO_ALIAS(HMAC_CTX_get_md);
251
252unsigned char *
253HMAC(const EVP_MD *evp_md, const void *key, int key_len, const unsigned char *d,
254 size_t n, unsigned char *md, unsigned int *md_len)
255{
256 HMAC_CTX c;
257 const unsigned char dummy_key[1] = { 0 };
258
259 if (key == NULL) {
260 key = dummy_key;
261 key_len = 0;
262 }
263 HMAC_CTX_init(&c);
264 if (!HMAC_Init_ex(&c, key, key_len, evp_md, NULL))
265 goto err;
266 if (!HMAC_Update(&c, d, n))
267 goto err;
268 if (!HMAC_Final(&c, md, md_len))
269 goto err;
270 HMAC_CTX_cleanup(&c);
271 return md;
272err:
273 HMAC_CTX_cleanup(&c);
274 return NULL;
275}
276LCRYPTO_ALIAS(HMAC);
diff --git a/src/lib/libcrypto/hmac/hmac.h b/src/lib/libcrypto/hmac/hmac.h
deleted file mode 100644
index 2216fd9258..0000000000
--- a/src/lib/libcrypto/hmac/hmac.h
+++ /dev/null
@@ -1,101 +0,0 @@
1/* $OpenBSD: hmac.h,v 1.21 2025/01/25 17:59:44 tb Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58#ifndef HEADER_HMAC_H
59#define HEADER_HMAC_H
60
61#include <openssl/opensslconf.h>
62
63#if !defined(HAVE_ATTRIBUTE__BOUNDED__) && !defined(__OpenBSD__)
64#define __bounded__(x, y, z)
65#endif
66
67#include <openssl/evp.h>
68
69#define HMAC_MAX_MD_CBLOCK 144 /* largest known is SHA3-224 */
70
71#ifdef __cplusplus
72extern "C" {
73#endif
74
75#define HMAC_size(e) (EVP_MD_size(HMAC_CTX_get_md((e))))
76
77HMAC_CTX *HMAC_CTX_new(void);
78void HMAC_CTX_free(HMAC_CTX *ctx);
79int HMAC_CTX_reset(HMAC_CTX *ctx);
80
81int HMAC_Init_ex(HMAC_CTX *ctx, const void *key, int len, const EVP_MD *md,
82 ENGINE *impl)
83 __attribute__ ((__bounded__(__buffer__, 2, 3)));
84int HMAC_Update(HMAC_CTX *ctx, const unsigned char *data, size_t len)
85 __attribute__ ((__bounded__(__buffer__, 2, 3)));
86int HMAC_Final(HMAC_CTX *ctx, unsigned char *md, unsigned int *len);
87unsigned char *HMAC(const EVP_MD *evp_md, const void *key, int key_len,
88 const unsigned char *d, size_t n, unsigned char *md, unsigned int *md_len)
89 __attribute__ ((__bounded__(__buffer__, 2, 3)))
90 __attribute__ ((__bounded__(__buffer__, 4, 5)))
91 __attribute__((__nonnull__ (6)));
92int HMAC_CTX_copy(HMAC_CTX *dctx, HMAC_CTX *sctx);
93
94void HMAC_CTX_set_flags(HMAC_CTX *ctx, unsigned long flags);
95const EVP_MD *HMAC_CTX_get_md(const HMAC_CTX *ctx);
96
97#ifdef __cplusplus
98}
99#endif
100
101#endif
diff --git a/src/lib/libcrypto/hmac/hmac_local.h b/src/lib/libcrypto/hmac/hmac_local.h
deleted file mode 100644
index e06cd6a6c7..0000000000
--- a/src/lib/libcrypto/hmac/hmac_local.h
+++ /dev/null
@@ -1,83 +0,0 @@
1/* $OpenBSD: hmac_local.h,v 1.4 2022/11/26 16:08:53 tb Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58#ifndef HEADER_HMAC_LOCAL_H
59#define HEADER_HMAC_LOCAL_H
60
61#include <openssl/opensslconf.h>
62
63#include <openssl/evp.h>
64
65#include "evp_local.h"
66
67__BEGIN_HIDDEN_DECLS
68
69struct hmac_ctx_st {
70 const EVP_MD *md;
71 EVP_MD_CTX md_ctx;
72 EVP_MD_CTX i_ctx;
73 EVP_MD_CTX o_ctx;
74 unsigned int key_length;
75 unsigned char key[HMAC_MAX_MD_CBLOCK];
76} /* HMAC_CTX */;
77
78void HMAC_CTX_init(HMAC_CTX *ctx);
79void HMAC_CTX_cleanup(HMAC_CTX *ctx);
80
81__END_HIDDEN_DECLS
82
83#endif /* !HEADER_HMAC_LOCAL_H */