summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/gost/gost89imit_pmeth.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/gost/gost89imit_pmeth.c')
-rw-r--r--src/lib/libcrypto/gost/gost89imit_pmeth.c255
1 files changed, 0 insertions, 255 deletions
diff --git a/src/lib/libcrypto/gost/gost89imit_pmeth.c b/src/lib/libcrypto/gost/gost89imit_pmeth.c
deleted file mode 100644
index 3caa58ac86..0000000000
--- a/src/lib/libcrypto/gost/gost89imit_pmeth.c
+++ /dev/null
@@ -1,255 +0,0 @@
1/* $OpenBSD: gost89imit_pmeth.c,v 1.6 2023/12/28 21:47:17 tb Exp $ */
2/*
3 * Copyright (c) 2014 Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
4 * Copyright (c) 2005-2006 Cryptocom LTD
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 *
18 * 3. All advertising materials mentioning features or use of this
19 * software must display the following acknowledgment:
20 * "This product includes software developed by the OpenSSL Project
21 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
22 *
23 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
24 * endorse or promote products derived from this software without
25 * prior written permission. For written permission, please contact
26 * openssl-core@openssl.org.
27 *
28 * 5. Products derived from this software may not be called "OpenSSL"
29 * nor may "OpenSSL" appear in their names without prior written
30 * permission of the OpenSSL Project.
31 *
32 * 6. Redistributions of any form whatsoever must retain the following
33 * acknowledgment:
34 * "This product includes software developed by the OpenSSL Project
35 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
36 *
37 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
38 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
40 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
43 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
44 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
46 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
47 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
48 * OF THE POSSIBILITY OF SUCH DAMAGE.
49 * ====================================================================
50 */
51
52#include <string.h>
53
54#include <openssl/opensslconf.h>
55
56#ifndef OPENSSL_NO_GOST
57#include <openssl/evp.h>
58#include <openssl/err.h>
59#include <openssl/gost.h>
60#include <openssl/x509v3.h> /* For string_to_hex */
61
62#include "evp_local.h"
63#include "gost_local.h"
64
65struct gost_mac_pmeth_data {
66 EVP_MD *md;
67 unsigned char key[32];
68 unsigned key_set :1;
69};
70
71static int
72pkey_gost_mac_init(EVP_PKEY_CTX *ctx)
73{
74 struct gost_mac_pmeth_data *data;
75
76 data = calloc(1, sizeof(struct gost_mac_pmeth_data));
77 if (data == NULL)
78 return 0;
79 EVP_PKEY_CTX_set_data(ctx, data);
80 return 1;
81}
82
83static void
84pkey_gost_mac_cleanup(EVP_PKEY_CTX *ctx)
85{
86 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
87 free(data);
88}
89
90static int
91pkey_gost_mac_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
92{
93 struct gost_mac_pmeth_data *dst_data, *src_data;
94
95 if (pkey_gost_mac_init(dst) == 0)
96 return 0;
97
98 src_data = EVP_PKEY_CTX_get_data(src);
99 dst_data = EVP_PKEY_CTX_get_data(dst);
100
101 *dst_data = *src_data;
102
103 return 1;
104}
105
106static int
107pkey_gost_mac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
108{
109 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
110 unsigned char *keydata = NULL;
111 int ret = 0;
112
113 if (!data->key_set) {
114 GOSTerror(GOST_R_MAC_KEY_NOT_SET);
115 goto err;
116 }
117
118 if ((keydata = malloc(32)) == NULL) {
119 GOSTerror(ERR_R_MALLOC_FAILURE);
120 goto err;
121 }
122 memcpy(keydata, data->key, 32);
123 if (!EVP_PKEY_assign(pkey, NID_id_Gost28147_89_MAC, keydata))
124 goto err;
125 keydata = NULL;
126
127 ret = 1;
128
129 err:
130 freezero(keydata, 32);
131
132 return ret;
133}
134
135static int
136pkey_gost_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
137{
138 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
139
140 switch (type) {
141 case EVP_PKEY_CTRL_MD:
142 if (EVP_MD_type(p2) != NID_id_Gost28147_89_MAC) {
143 GOSTerror(GOST_R_INVALID_DIGEST_TYPE);
144 return 0;
145 }
146 data->md = p2;
147 return 1;
148
149 case EVP_PKEY_CTRL_SET_MAC_KEY:
150 if (p1 != 32) {
151 GOSTerror(GOST_R_INVALID_MAC_KEY_LENGTH);
152 return 0;
153 }
154
155 memcpy(data->key, p2, 32);
156 data->key_set = 1;
157 return 1;
158
159 case EVP_PKEY_CTRL_DIGESTINIT:
160 {
161 EVP_MD_CTX *mctx = p2;
162 void *key;
163
164 if (!data->key_set) {
165 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
166 if (pkey == NULL) {
167 GOSTerror(GOST_R_MAC_KEY_NOT_SET);
168 return 0;
169 }
170 key = EVP_PKEY_get0(pkey);
171 if (key == NULL) {
172 GOSTerror(GOST_R_MAC_KEY_NOT_SET);
173 return 0;
174 }
175 } else {
176 key = &(data->key);
177 }
178 if (mctx->digest->md_ctrl == NULL)
179 return 0;
180 return mctx->digest->md_ctrl(mctx, EVP_MD_CTRL_SET_KEY, 32 * 8,
181 key);
182 }
183
184 }
185
186 return -2;
187}
188
189static int
190pkey_gost_mac_ctrl_str(EVP_PKEY_CTX *ctx, const char *type, const char *value)
191{
192 if (value == NULL)
193 return 0;
194 if (strcmp(type, "key") == 0) {
195 void *p = (void *)value;
196 return pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY,
197 strlen(value), p);
198 }
199 if (strcmp(type, "hexkey") == 0) {
200 unsigned char *key;
201 int r;
202 long keylen;
203
204 key = string_to_hex(value, &keylen);
205 if (key == NULL)
206 return 0;
207 r = pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, keylen,
208 key);
209 free(key);
210 return r;
211 }
212 return -2;
213}
214
215static int
216pkey_gost_mac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
217{
218 return 1;
219}
220
221static int
222pkey_gost_mac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
223 EVP_MD_CTX *mctx)
224{
225 /* for platforms where sizeof(int) != sizeof(size_t)*/
226 unsigned int tmpsiglen = *siglen;
227 int ret;
228
229 if (sig == NULL) {
230 *siglen = 4;
231 return 1;
232 }
233
234 ret = EVP_DigestFinal_ex(mctx, sig, &tmpsiglen);
235 *siglen = tmpsiglen;
236 return ret;
237}
238
239const EVP_PKEY_METHOD gostimit_pkey_meth = {
240 .pkey_id = EVP_PKEY_GOSTIMIT,
241
242 .init = pkey_gost_mac_init,
243 .cleanup = pkey_gost_mac_cleanup,
244 .copy = pkey_gost_mac_copy,
245
246 .keygen = pkey_gost_mac_keygen,
247
248 .signctx_init = pkey_gost_mac_signctx_init,
249 .signctx = pkey_gost_mac_signctx,
250
251 .ctrl = pkey_gost_mac_ctrl,
252 .ctrl_str = pkey_gost_mac_ctrl_str,
253};
254
255#endif