summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/gost/gostr341001_key.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/gost/gostr341001_key.c')
-rw-r--r--src/lib/libcrypto/gost/gostr341001_key.c320
1 files changed, 0 insertions, 320 deletions
diff --git a/src/lib/libcrypto/gost/gostr341001_key.c b/src/lib/libcrypto/gost/gostr341001_key.c
deleted file mode 100644
index dbe360620a..0000000000
--- a/src/lib/libcrypto/gost/gostr341001_key.c
+++ /dev/null
@@ -1,320 +0,0 @@
1/* $OpenBSD: gostr341001_key.c,v 1.5 2015/02/14 06:40:04 jsing 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 <openssl/opensslconf.h>
53
54#ifndef OPENSSL_NO_GOST
55#include <openssl/bn.h>
56#include <openssl/err.h>
57#include <openssl/gost.h>
58#include <openssl/objects.h>
59#include "gost_locl.h"
60
61struct gost_key_st {
62 EC_GROUP *group;
63
64 EC_POINT *pub_key;
65 BIGNUM *priv_key;
66
67 int references;
68
69 int digest_nid;
70};
71
72GOST_KEY *
73GOST_KEY_new(void)
74{
75 GOST_KEY *ret;
76
77 ret = malloc(sizeof(GOST_KEY));
78 if (ret == NULL) {
79 GOSTerr(GOST_F_GOST_KEY_NEW, ERR_R_MALLOC_FAILURE);
80 return (NULL);
81 }
82 ret->group = NULL;
83 ret->pub_key = NULL;
84 ret->priv_key = NULL;
85 ret->references = 1;
86 ret->digest_nid = NID_undef;
87 return (ret);
88}
89
90void
91GOST_KEY_free(GOST_KEY *r)
92{
93 int i;
94
95 if (r == NULL)
96 return;
97
98 i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_EC);
99 if (i > 0)
100 return;
101
102 EC_GROUP_free(r->group);
103 EC_POINT_free(r->pub_key);
104 BN_clear_free(r->priv_key);
105
106 OPENSSL_cleanse((void *)r, sizeof(GOST_KEY));
107 free(r);
108}
109
110int
111GOST_KEY_check_key(const GOST_KEY *key)
112{
113 int ok = 0;
114 BN_CTX *ctx = NULL;
115 BIGNUM *order = NULL;
116 EC_POINT *point = NULL;
117
118 if (key == NULL || key->group == NULL || key->pub_key == NULL) {
119 GOSTerr(GOST_F_GOST_KEY_CHECK_KEY, ERR_R_PASSED_NULL_PARAMETER);
120 return 0;
121 }
122 if (EC_POINT_is_at_infinity(key->group, key->pub_key) != 0) {
123 GOSTerr(GOST_F_GOST_KEY_CHECK_KEY, EC_R_POINT_AT_INFINITY);
124 goto err;
125 }
126 if ((ctx = BN_CTX_new()) == NULL)
127 goto err;
128 if ((point = EC_POINT_new(key->group)) == NULL)
129 goto err;
130
131 /* testing whether the pub_key is on the elliptic curve */
132 if (EC_POINT_is_on_curve(key->group, key->pub_key, ctx) == 0) {
133 GOSTerr(GOST_F_GOST_KEY_CHECK_KEY, EC_R_POINT_IS_NOT_ON_CURVE);
134 goto err;
135 }
136 /* testing whether pub_key * order is the point at infinity */
137 if ((order = BN_new()) == NULL)
138 goto err;
139 if (EC_GROUP_get_order(key->group, order, ctx) == 0) {
140 GOSTerr(GOST_F_GOST_KEY_CHECK_KEY, EC_R_INVALID_GROUP_ORDER);
141 goto err;
142 }
143 if (EC_POINT_mul(key->group, point, NULL, key->pub_key, order,
144 ctx) == 0) {
145 GOSTerr(GOST_F_GOST_KEY_CHECK_KEY, ERR_R_EC_LIB);
146 goto err;
147 }
148 if (EC_POINT_is_at_infinity(key->group, point) == 0) {
149 GOSTerr(GOST_F_GOST_KEY_CHECK_KEY, EC_R_WRONG_ORDER);
150 goto err;
151 }
152 /*
153 * in case the priv_key is present : check if generator * priv_key ==
154 * pub_key
155 */
156 if (key->priv_key != NULL) {
157 if (BN_cmp(key->priv_key, order) >= 0) {
158 GOSTerr(GOST_F_GOST_KEY_CHECK_KEY, EC_R_WRONG_ORDER);
159 goto err;
160 }
161 if (EC_POINT_mul(key->group, point, key->priv_key, NULL, NULL,
162 ctx) == 0) {
163 GOSTerr(GOST_F_GOST_KEY_CHECK_KEY, ERR_R_EC_LIB);
164 goto err;
165 }
166 if (EC_POINT_cmp(key->group, point, key->pub_key, ctx) != 0) {
167 GOSTerr(GOST_F_GOST_KEY_CHECK_KEY,
168 EC_R_INVALID_PRIVATE_KEY);
169 goto err;
170 }
171 }
172 ok = 1;
173err:
174 BN_free(order);
175 BN_CTX_free(ctx);
176 EC_POINT_free(point);
177 return (ok);
178}
179
180int
181GOST_KEY_set_public_key_affine_coordinates(GOST_KEY *key, BIGNUM *x, BIGNUM *y)
182{
183 BN_CTX *ctx = NULL;
184 BIGNUM *tx, *ty;
185 EC_POINT *point = NULL;
186 int ok = 0;
187
188 if (key == NULL || key->group == NULL || x == NULL || y == NULL) {
189 GOSTerr(GOST_F_GOST_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
190 ERR_R_PASSED_NULL_PARAMETER);
191 return 0;
192 }
193 ctx = BN_CTX_new();
194 if (ctx == NULL)
195 goto err;
196
197 point = EC_POINT_new(key->group);
198 if (point == NULL)
199 goto err;
200
201 if ((tx = BN_CTX_get(ctx)) == NULL)
202 goto err;
203 if ((ty = BN_CTX_get(ctx)) == NULL)
204 goto err;
205 if (EC_POINT_set_affine_coordinates_GFp(key->group, point, x, y,
206 ctx) == 0)
207 goto err;
208 if (EC_POINT_get_affine_coordinates_GFp(key->group, point, tx, ty,
209 ctx) == 0)
210 goto err;
211 /*
212 * Check if retrieved coordinates match originals: if not, values are
213 * out of range.
214 */
215 if (BN_cmp(x, tx) != 0 || BN_cmp(y, ty) != 0) {
216 GOSTerr(GOST_F_GOST_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
217 EC_R_COORDINATES_OUT_OF_RANGE);
218 goto err;
219 }
220 if (GOST_KEY_set_public_key(key, point) == 0)
221 goto err;
222
223 if (GOST_KEY_check_key(key) == 0)
224 goto err;
225
226 ok = 1;
227
228err:
229 EC_POINT_free(point);
230 BN_CTX_free(ctx);
231 return ok;
232
233}
234
235const EC_GROUP *
236GOST_KEY_get0_group(const GOST_KEY *key)
237{
238 return key->group;
239}
240
241int
242GOST_KEY_set_group(GOST_KEY *key, const EC_GROUP *group)
243{
244 EC_GROUP_free(key->group);
245 key->group = EC_GROUP_dup(group);
246 return (key->group == NULL) ? 0 : 1;
247}
248
249const BIGNUM *
250GOST_KEY_get0_private_key(const GOST_KEY *key)
251{
252 return key->priv_key;
253}
254
255int
256GOST_KEY_set_private_key(GOST_KEY *key, const BIGNUM *priv_key)
257{
258 BN_clear_free(key->priv_key);
259 key->priv_key = BN_dup(priv_key);
260 return (key->priv_key == NULL) ? 0 : 1;
261}
262
263const EC_POINT *
264GOST_KEY_get0_public_key(const GOST_KEY *key)
265{
266 return key->pub_key;
267}
268
269int
270GOST_KEY_set_public_key(GOST_KEY *key, const EC_POINT *pub_key)
271{
272 EC_POINT_free(key->pub_key);
273 key->pub_key = EC_POINT_dup(pub_key, key->group);
274 return (key->pub_key == NULL) ? 0 : 1;
275}
276
277int
278GOST_KEY_get_digest(const GOST_KEY *key)
279{
280 return key->digest_nid;
281}
282int
283GOST_KEY_set_digest(GOST_KEY *key, int digest_nid)
284{
285 if (digest_nid == NID_id_GostR3411_94_CryptoProParamSet ||
286 digest_nid == NID_id_tc26_gost3411_2012_256 ||
287 digest_nid == NID_id_tc26_gost3411_2012_512) {
288 key->digest_nid = digest_nid;
289 return 1;
290 }
291
292 return 0;
293}
294
295size_t
296GOST_KEY_get_size(const GOST_KEY *r)
297{
298 int i;
299 BIGNUM *order = NULL;
300 const EC_GROUP *group;
301
302 if (r == NULL)
303 return 0;
304 group = GOST_KEY_get0_group(r);
305 if (group == NULL)
306 return 0;
307
308 if ((order = BN_new()) == NULL)
309 return 0;
310
311 if (EC_GROUP_get_order(group, order, NULL) == 0) {
312 BN_clear_free(order);
313 return 0;
314 }
315
316 i = BN_num_bytes(order);
317 BN_clear_free(order);
318 return (i);
319}
320#endif