Coverage Report

Created: 2022-08-24 06:30

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