OpenCoverage

gostr341001_key.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/libressl/src/crypto/gost/gostr341001_key.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/* $OpenBSD: gostr341001_key.c,v 1.8 2017/05/02 03:59:44 deraadt 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/bn.h>-
58#include <openssl/err.h>-
59#include <openssl/gost.h>-
60#include <openssl/objects.h>-
61#include "gost_locl.h"-
62-
63struct gost_key_st {-
64 EC_GROUP *group;-
65-
66 EC_POINT *pub_key;-
67 BIGNUM *priv_key;-
68-
69 int references;-
70-
71 int digest_nid;-
72};-
73-
74GOST_KEY *-
75GOST_KEY_new(void)-
76{-
77 GOST_KEY *ret;-
78-
79 ret = malloc(sizeof(GOST_KEY));-
80 if (ret == NULL) {
ret == ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
0
81 GOSTerror(ERR_R_MALLOC_FAILURE);-
82 return (NULL);
never executed: return ( ((void *)0) );
0
83 }-
84 ret->group = NULL;-
85 ret->pub_key = NULL;-
86 ret->priv_key = NULL;-
87 ret->references = 1;-
88 ret->digest_nid = NID_undef;-
89 return (ret);
never executed: return (ret);
0
90}-
91-
92void-
93GOST_KEY_free(GOST_KEY *r)-
94{-
95 int i;-
96-
97 if (r == NULL)
r == ((void *)0)Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • freenull
FALSEnever evaluated
0-1
98 return;
executed 1 time by 1 test: return;
Executed by:
  • freenull
1
99-
100 i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_EC);-
101 if (i > 0)
i > 0Description
TRUEnever evaluated
FALSEnever evaluated
0
102 return;
never executed: return;
0
103-
104 EC_GROUP_free(r->group);-
105 EC_POINT_free(r->pub_key);-
106 BN_clear_free(r->priv_key);-
107-
108 freezero(r, sizeof(GOST_KEY));-
109}
never executed: end of block
0
110-
111int-
112GOST_KEY_check_key(const GOST_KEY *key)-
113{-
114 int ok = 0;-
115 BN_CTX *ctx = NULL;-
116 BIGNUM *order = NULL;-
117 EC_POINT *point = NULL;-
118-
119 if (key == NULL || key->group == NULL || key->pub_key == NULL) {
key == ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
key->group == ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
key->pub_key == ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
0
120 GOSTerror(ERR_R_PASSED_NULL_PARAMETER);-
121 return 0;
never executed: return 0;
0
122 }-
123 if (EC_POINT_is_at_infinity(key->group, key->pub_key) != 0) {
EC_POINT_is_at...>pub_key) != 0Description
TRUEnever evaluated
FALSEnever evaluated
0
124 GOSTerror(EC_R_POINT_AT_INFINITY);-
125 goto err;
never executed: goto err;
0
126 }-
127 if ((ctx = BN_CTX_new()) == NULL)
(ctx = BN_CTX_...== ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
0
128 goto err;
never executed: goto err;
0
129 if ((point = EC_POINT_new(key->group)) == NULL)
(point = EC_PO...== ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
0
130 goto err;
never executed: goto err;
0
131-
132 /* testing whether the pub_key is on the elliptic curve */-
133 if (EC_POINT_is_on_curve(key->group, key->pub_key, ctx) == 0) {
EC_POINT_is_on...key, ctx) == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
134 GOSTerror(EC_R_POINT_IS_NOT_ON_CURVE);-
135 goto err;
never executed: goto err;
0
136 }-
137 /* testing whether pub_key * order is the point at infinity */-
138 if ((order = BN_new()) == NULL)
(order = BN_ne...== ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
0
139 goto err;
never executed: goto err;
0
140 if (EC_GROUP_get_order(key->group, order, ctx) == 0) {
EC_GROUP_get_o...der, ctx) == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
141 GOSTerror(EC_R_INVALID_GROUP_ORDER);-
142 goto err;
never executed: goto err;
0
143 }-
144 if (EC_POINT_mul(key->group, point, NULL, key->pub_key, order,
EC_POINT_mul(k...der, ctx) == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
145 ctx) == 0) {
EC_POINT_mul(k...der, ctx) == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
146 GOSTerror(ERR_R_EC_LIB);-
147 goto err;
never executed: goto err;
0
148 }-
149 if (EC_POINT_is_at_infinity(key->group, point) == 0) {
EC_POINT_is_at...p, point) == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
150 GOSTerror(EC_R_WRONG_ORDER);-
151 goto err;
never executed: goto err;
0
152 }-
153 /*-
154 * in case the priv_key is present : check if generator * priv_key ==-
155 * pub_key-
156 */-
157 if (key->priv_key != NULL) {
key->priv_key != ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
0
158 if (BN_cmp(key->priv_key, order) >= 0) {
BN_cmp(key->pr...y, order) >= 0Description
TRUEnever evaluated
FALSEnever evaluated
0
159 GOSTerror(EC_R_WRONG_ORDER);-
160 goto err;
never executed: goto err;
0
161 }-
162 if (EC_POINT_mul(key->group, point, key->priv_key, NULL, NULL,
EC_POINT_mul(k...0) , ctx) == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
163 ctx) == 0) {
EC_POINT_mul(k...0) , ctx) == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
164 GOSTerror(ERR_R_EC_LIB);-
165 goto err;
never executed: goto err;
0
166 }-
167 if (EC_POINT_cmp(key->group, point, key->pub_key, ctx) != 0) {
EC_POINT_cmp(k...key, ctx) != 0Description
TRUEnever evaluated
FALSEnever evaluated
0
168 GOSTerror(EC_R_INVALID_PRIVATE_KEY);-
169 goto err;
never executed: goto err;
0
170 }-
171 }
never executed: end of block
0
172 ok = 1;-
173err:
code before this statement never executed: err:
0
174 BN_free(order);-
175 BN_CTX_free(ctx);-
176 EC_POINT_free(point);-
177 return (ok);
never executed: return (ok);
0
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) {
key == ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
key->group == ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
x == ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
y == ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
0
189 GOSTerror(ERR_R_PASSED_NULL_PARAMETER);-
190 return 0;
never executed: return 0;
0
191 }-
192 ctx = BN_CTX_new();-
193 if (ctx == NULL)
ctx == ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
0
194 goto err;
never executed: goto err;
0
195-
196 point = EC_POINT_new(key->group);-
197 if (point == NULL)
point == ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
0
198 goto err;
never executed: goto err;
0
199-
200 if ((tx = BN_CTX_get(ctx)) == NULL)
(tx = BN_CTX_g...== ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
0
201 goto err;
never executed: goto err;
0
202 if ((ty = BN_CTX_get(ctx)) == NULL)
(ty = BN_CTX_g...== ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
0
203 goto err;
never executed: goto err;
0
204 if (EC_POINT_set_affine_coordinates_GFp(key->group, point, x, y,
EC_POINT_set_a..., y, ctx) == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
205 ctx) == 0)
EC_POINT_set_a..., y, ctx) == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
206 goto err;
never executed: goto err;
0
207 if (EC_POINT_get_affine_coordinates_GFp(key->group, point, tx, ty,
EC_POINT_get_a... ty, ctx) == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
208 ctx) == 0)
EC_POINT_get_a... ty, ctx) == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
209 goto err;
never executed: goto err;
0
210 /*-
211 * Check if retrieved coordinates match originals: if not, values are-
212 * out of range.-
213 */-
214 if (BN_cmp(x, tx) != 0 || BN_cmp(y, ty) != 0) {
BN_cmp(x, tx) != 0Description
TRUEnever evaluated
FALSEnever evaluated
BN_cmp(y, ty) != 0Description
TRUEnever evaluated
FALSEnever evaluated
0
215 GOSTerror(EC_R_COORDINATES_OUT_OF_RANGE);-
216 goto err;
never executed: goto err;
0
217 }-
218 if (GOST_KEY_set_public_key(key, point) == 0)
GOST_KEY_set_p...y, point) == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
219 goto err;
never executed: goto err;
0
220-
221 if (GOST_KEY_check_key(key) == 0)
GOST_KEY_check_key(key) == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
222 goto err;
never executed: goto err;
0
223-
224 ok = 1;-
225-
226err:
code before this statement never executed: err:
0
227 EC_POINT_free(point);-
228 BN_CTX_free(ctx);-
229 return ok;
never executed: return ok;
0
230-
231}-
232-
233const EC_GROUP *-
234GOST_KEY_get0_group(const GOST_KEY *key)-
235{-
236 return key->group;
never executed: return key->group;
0
237}-
238-
239int-
240GOST_KEY_set_group(GOST_KEY *key, const EC_GROUP *group)-
241{-
242 EC_GROUP_free(key->group);-
243 key->group = EC_GROUP_dup(group);-
244 return (key->group == NULL) ? 0 : 1;
never executed: return (key->group == ((void *)0) ) ? 0 : 1;
(key->group == ((void *)0) )Description
TRUEnever evaluated
FALSEnever evaluated
0
245}-
246-
247const BIGNUM *-
248GOST_KEY_get0_private_key(const GOST_KEY *key)-
249{-
250 return key->priv_key;
never executed: return key->priv_key;
0
251}-
252-
253int-
254GOST_KEY_set_private_key(GOST_KEY *key, const BIGNUM *priv_key)-
255{-
256 BN_clear_free(key->priv_key);-
257 key->priv_key = BN_dup(priv_key);-
258 return (key->priv_key == NULL) ? 0 : 1;
never executed: return (key->priv_key == ((void *)0) ) ? 0 : 1;
(key->priv_key... ((void *)0) )Description
TRUEnever evaluated
FALSEnever evaluated
0
259}-
260-
261const EC_POINT *-
262GOST_KEY_get0_public_key(const GOST_KEY *key)-
263{-
264 return key->pub_key;
never executed: return key->pub_key;
0
265}-
266-
267int-
268GOST_KEY_set_public_key(GOST_KEY *key, const EC_POINT *pub_key)-
269{-
270 EC_POINT_free(key->pub_key);-
271 key->pub_key = EC_POINT_dup(pub_key, key->group);-
272 return (key->pub_key == NULL) ? 0 : 1;
never executed: return (key->pub_key == ((void *)0) ) ? 0 : 1;
(key->pub_key == ((void *)0) )Description
TRUEnever evaluated
FALSEnever evaluated
0
273}-
274-
275int-
276GOST_KEY_get_digest(const GOST_KEY *key)-
277{-
278 return key->digest_nid;
never executed: return key->digest_nid;
0
279}-
280int-
281GOST_KEY_set_digest(GOST_KEY *key, int digest_nid)-
282{-
283 if (digest_nid == NID_id_GostR3411_94_CryptoProParamSet ||
digest_nid == 822Description
TRUEnever evaluated
FALSEnever evaluated
0
284 digest_nid == NID_id_tc26_gost3411_2012_256 ||
digest_nid == 941Description
TRUEnever evaluated
FALSEnever evaluated
0
285 digest_nid == NID_id_tc26_gost3411_2012_512) {
digest_nid == 942Description
TRUEnever evaluated
FALSEnever evaluated
0
286 key->digest_nid = digest_nid;-
287 return 1;
never executed: return 1;
0
288 }-
289-
290 return 0;
never executed: return 0;
0
291}-
292-
293size_t-
294GOST_KEY_get_size(const GOST_KEY *r)-
295{-
296 int i;-
297 BIGNUM *order = NULL;-
298 const EC_GROUP *group;-
299-
300 if (r == NULL)
r == ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
0
301 return 0;
never executed: return 0;
0
302 group = GOST_KEY_get0_group(r);-
303 if (group == NULL)
group == ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
0
304 return 0;
never executed: return 0;
0
305-
306 if ((order = BN_new()) == NULL)
(order = BN_ne...== ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
0
307 return 0;
never executed: return 0;
0
308-
309 if (EC_GROUP_get_order(group, order, NULL) == 0) {
EC_GROUP_get_o...id *)0) ) == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
310 BN_clear_free(order);-
311 return 0;
never executed: return 0;
0
312 }-
313-
314 i = BN_num_bytes(order);-
315 BN_clear_free(order);-
316 return (i);
never executed: return (i);
0
317}-
318#endif-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.2.2