| Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/libressl/src/crypto/gost/gostr341001.c |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /* $OpenBSD: gostr341001.c,v 1.7 2017/01/29 17:49:23 beck 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 | - | |||||||||||||
| 61 | #include "bn_lcl.h" | - | ||||||||||||
| 62 | #include "gost_locl.h" | - | ||||||||||||
| 63 | - | |||||||||||||
| 64 | /* Convert little-endian byte array into bignum */ | - | ||||||||||||
| 65 | BIGNUM * | - | ||||||||||||
| 66 | GOST_le2bn(const unsigned char *buf, size_t len, BIGNUM *bn) | - | ||||||||||||
| 67 | { | - | ||||||||||||
| 68 | unsigned char temp[64]; | - | ||||||||||||
| 69 | int i; | - | ||||||||||||
| 70 | - | |||||||||||||
| 71 | if (len > 64)
| 0 | ||||||||||||
| 72 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||||||||
| 73 | - | |||||||||||||
| 74 | for (i = 0; i < len; i++) {
| 0 | ||||||||||||
| 75 | temp[len - 1 - i] = buf[i]; | - | ||||||||||||
| 76 | } never executed: end of block | 0 | ||||||||||||
| 77 | - | |||||||||||||
| 78 | return BN_bin2bn(temp, len, bn); never executed: return BN_bin2bn(temp, len, bn); | 0 | ||||||||||||
| 79 | } | - | ||||||||||||
| 80 | - | |||||||||||||
| 81 | int | - | ||||||||||||
| 82 | GOST_bn2le(BIGNUM *bn, unsigned char *buf, int len) | - | ||||||||||||
| 83 | { | - | ||||||||||||
| 84 | unsigned char temp[64]; | - | ||||||||||||
| 85 | int i, bytes; | - | ||||||||||||
| 86 | - | |||||||||||||
| 87 | bytes = BN_num_bytes(bn); | - | ||||||||||||
| 88 | if (len > 64 || bytes > len)
| 0 | ||||||||||||
| 89 | return 0; never executed: return 0; | 0 | ||||||||||||
| 90 | - | |||||||||||||
| 91 | BN_bn2bin(bn, temp); | - | ||||||||||||
| 92 | - | |||||||||||||
| 93 | for (i = 0; i < bytes; i++) {
| 0 | ||||||||||||
| 94 | buf[bytes - 1 - i] = temp[i]; | - | ||||||||||||
| 95 | } never executed: end of block | 0 | ||||||||||||
| 96 | - | |||||||||||||
| 97 | memset(buf + bytes, 0, len - bytes); | - | ||||||||||||
| 98 | - | |||||||||||||
| 99 | return 1; never executed: return 1; | 0 | ||||||||||||
| 100 | } | - | ||||||||||||
| 101 | - | |||||||||||||
| 102 | int | - | ||||||||||||
| 103 | gost2001_compute_public(GOST_KEY *ec) | - | ||||||||||||
| 104 | { | - | ||||||||||||
| 105 | const EC_GROUP *group = GOST_KEY_get0_group(ec); | - | ||||||||||||
| 106 | EC_POINT *pub_key = NULL; | - | ||||||||||||
| 107 | const BIGNUM *priv_key = NULL; | - | ||||||||||||
| 108 | BN_CTX *ctx = NULL; | - | ||||||||||||
| 109 | int ok = 0; | - | ||||||||||||
| 110 | - | |||||||||||||
| 111 | if (group == NULL) {
| 0 | ||||||||||||
| 112 | GOSTerror(GOST_R_KEY_IS_NOT_INITIALIZED); | - | ||||||||||||
| 113 | return 0; never executed: return 0; | 0 | ||||||||||||
| 114 | } | - | ||||||||||||
| 115 | ctx = BN_CTX_new(); | - | ||||||||||||
| 116 | if (ctx == NULL) {
| 0 | ||||||||||||
| 117 | GOSTerror(ERR_R_MALLOC_FAILURE); | - | ||||||||||||
| 118 | return 0; never executed: return 0; | 0 | ||||||||||||
| 119 | } | - | ||||||||||||
| 120 | BN_CTX_start(ctx); | - | ||||||||||||
| 121 | if ((priv_key = GOST_KEY_get0_private_key(ec)) == NULL)
| 0 | ||||||||||||
| 122 | goto err; never executed: goto err; | 0 | ||||||||||||
| 123 | - | |||||||||||||
| 124 | pub_key = EC_POINT_new(group); | - | ||||||||||||
| 125 | if (pub_key == NULL)
| 0 | ||||||||||||
| 126 | goto err; never executed: goto err; | 0 | ||||||||||||
| 127 | if (EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, ctx) == 0)
| 0 | ||||||||||||
| 128 | goto err; never executed: goto err; | 0 | ||||||||||||
| 129 | if (GOST_KEY_set_public_key(ec, pub_key) == 0)
| 0 | ||||||||||||
| 130 | goto err; never executed: goto err; | 0 | ||||||||||||
| 131 | ok = 1; | - | ||||||||||||
| 132 | - | |||||||||||||
| 133 | if (ok == 0) {
| 0 | ||||||||||||
| 134 | err: | - | ||||||||||||
| 135 | GOSTerror(ERR_R_EC_LIB); | - | ||||||||||||
| 136 | } never executed: end of block | 0 | ||||||||||||
| 137 | EC_POINT_free(pub_key); | - | ||||||||||||
| 138 | if (ctx != NULL) {
| 0 | ||||||||||||
| 139 | BN_CTX_end(ctx); | - | ||||||||||||
| 140 | BN_CTX_free(ctx); | - | ||||||||||||
| 141 | } never executed: end of block | 0 | ||||||||||||
| 142 | return ok; never executed: return ok; | 0 | ||||||||||||
| 143 | } | - | ||||||||||||
| 144 | - | |||||||||||||
| 145 | ECDSA_SIG * | - | ||||||||||||
| 146 | gost2001_do_sign(BIGNUM *md, GOST_KEY *eckey) | - | ||||||||||||
| 147 | { | - | ||||||||||||
| 148 | ECDSA_SIG *newsig = NULL; | - | ||||||||||||
| 149 | BIGNUM *order = NULL; | - | ||||||||||||
| 150 | const EC_GROUP *group; | - | ||||||||||||
| 151 | const BIGNUM *priv_key; | - | ||||||||||||
| 152 | BIGNUM *r = NULL, *s = NULL, *X = NULL, *tmp = NULL, *tmp2 = NULL, *k = | - | ||||||||||||
| 153 | NULL, *e = NULL; | - | ||||||||||||
| 154 | EC_POINT *C = NULL; | - | ||||||||||||
| 155 | BN_CTX *ctx = BN_CTX_new(); | - | ||||||||||||
| 156 | int ok = 0; | - | ||||||||||||
| 157 | - | |||||||||||||
| 158 | if (ctx == NULL) {
| 0 | ||||||||||||
| 159 | GOSTerror(ERR_R_MALLOC_FAILURE); | - | ||||||||||||
| 160 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||||||||
| 161 | } | - | ||||||||||||
| 162 | BN_CTX_start(ctx); | - | ||||||||||||
| 163 | newsig = ECDSA_SIG_new(); | - | ||||||||||||
| 164 | if (newsig == NULL) {
| 0 | ||||||||||||
| 165 | GOSTerror(ERR_R_MALLOC_FAILURE); | - | ||||||||||||
| 166 | goto err; never executed: goto err; | 0 | ||||||||||||
| 167 | } | - | ||||||||||||
| 168 | s = newsig->s; | - | ||||||||||||
| 169 | r = newsig->r; | - | ||||||||||||
| 170 | group = GOST_KEY_get0_group(eckey); | - | ||||||||||||
| 171 | if ((order = BN_CTX_get(ctx)) == NULL)
| 0 | ||||||||||||
| 172 | goto err; never executed: goto err; | 0 | ||||||||||||
| 173 | if (EC_GROUP_get_order(group, order, ctx) == 0)
| 0 | ||||||||||||
| 174 | goto err; never executed: goto err; | 0 | ||||||||||||
| 175 | priv_key = GOST_KEY_get0_private_key(eckey); | - | ||||||||||||
| 176 | if ((e = BN_CTX_get(ctx)) == NULL)
| 0 | ||||||||||||
| 177 | goto err; never executed: goto err; | 0 | ||||||||||||
| 178 | if (BN_mod_ct(e, md, order, ctx) == 0)
| 0 | ||||||||||||
| 179 | goto err; never executed: goto err; | 0 | ||||||||||||
| 180 | if (BN_is_zero(e))
| 0 | ||||||||||||
| 181 | BN_one(e); never executed: (BN_set_word((e),1)); | 0 | ||||||||||||
| 182 | if ((k = BN_CTX_get(ctx)) == NULL)
| 0 | ||||||||||||
| 183 | goto err; never executed: goto err; | 0 | ||||||||||||
| 184 | if ((X = BN_CTX_get(ctx)) == NULL)
| 0 | ||||||||||||
| 185 | goto err; never executed: goto err; | 0 | ||||||||||||
| 186 | if ((C = EC_POINT_new(group)) == NULL)
| 0 | ||||||||||||
| 187 | goto err; never executed: goto err; | 0 | ||||||||||||
| 188 | do { | - | ||||||||||||
| 189 | do { | - | ||||||||||||
| 190 | if (!BN_rand_range(k, order)) {
| 0 | ||||||||||||
| 191 | GOSTerror(GOST_R_RANDOM_NUMBER_GENERATOR_FAILED); | - | ||||||||||||
| 192 | goto err; never executed: goto err; | 0 | ||||||||||||
| 193 | } | - | ||||||||||||
| 194 | /* | - | ||||||||||||
| 195 | * We do not want timing information to leak the length | - | ||||||||||||
| 196 | * of k, so we compute G*k using an equivalent scalar | - | ||||||||||||
| 197 | * of fixed bit-length. | - | ||||||||||||
| 198 | */ | - | ||||||||||||
| 199 | if (BN_add(k, k, order) == 0)
| 0 | ||||||||||||
| 200 | goto err; never executed: goto err; | 0 | ||||||||||||
| 201 | if (BN_num_bits(k) <= BN_num_bits(order))
| 0 | ||||||||||||
| 202 | if (BN_add(k, k, order) == 0)
| 0 | ||||||||||||
| 203 | goto err; never executed: goto err; | 0 | ||||||||||||
| 204 | - | |||||||||||||
| 205 | if (EC_POINT_mul(group, C, k, NULL, NULL, ctx) == 0) {
| 0 | ||||||||||||
| 206 | GOSTerror(ERR_R_EC_LIB); | - | ||||||||||||
| 207 | goto err; never executed: goto err; | 0 | ||||||||||||
| 208 | } | - | ||||||||||||
| 209 | if (EC_POINT_get_affine_coordinates_GFp(group, C, X,
| 0 | ||||||||||||
| 210 | NULL, ctx) == 0) {
| 0 | ||||||||||||
| 211 | GOSTerror(ERR_R_EC_LIB); | - | ||||||||||||
| 212 | goto err; never executed: goto err; | 0 | ||||||||||||
| 213 | } | - | ||||||||||||
| 214 | if (BN_nnmod(r, X, order, ctx) == 0)
| 0 | ||||||||||||
| 215 | goto err; never executed: goto err; | 0 | ||||||||||||
| 216 | } while (BN_is_zero(r)); never executed: end of block
| 0 | ||||||||||||
| 217 | /* s = (r*priv_key+k*e) mod order */ | - | ||||||||||||
| 218 | if (tmp == NULL) {
| 0 | ||||||||||||
| 219 | if ((tmp = BN_CTX_get(ctx)) == NULL)
| 0 | ||||||||||||
| 220 | goto err; never executed: goto err; | 0 | ||||||||||||
| 221 | } never executed: end of block | 0 | ||||||||||||
| 222 | if (BN_mod_mul(tmp, priv_key, r, order, ctx) == 0)
| 0 | ||||||||||||
| 223 | goto err; never executed: goto err; | 0 | ||||||||||||
| 224 | if (tmp2 == NULL) {
| 0 | ||||||||||||
| 225 | if ((tmp2 = BN_CTX_get(ctx)) == NULL)
| 0 | ||||||||||||
| 226 | goto err; never executed: goto err; | 0 | ||||||||||||
| 227 | } never executed: end of block | 0 | ||||||||||||
| 228 | if (BN_mod_mul(tmp2, k, e, order, ctx) == 0)
| 0 | ||||||||||||
| 229 | goto err; never executed: goto err; | 0 | ||||||||||||
| 230 | if (BN_mod_add(s, tmp, tmp2, order, ctx) == 0)
| 0 | ||||||||||||
| 231 | goto err; never executed: goto err; | 0 | ||||||||||||
| 232 | } while (BN_is_zero(s)); never executed: end of block
| 0 | ||||||||||||
| 233 | ok = 1; | - | ||||||||||||
| 234 | - | |||||||||||||
| 235 | err: code before this statement never executed: err: | 0 | ||||||||||||
| 236 | EC_POINT_free(C); | - | ||||||||||||
| 237 | if (ctx != NULL) {
| 0 | ||||||||||||
| 238 | BN_CTX_end(ctx); | - | ||||||||||||
| 239 | BN_CTX_free(ctx); | - | ||||||||||||
| 240 | } never executed: end of block | 0 | ||||||||||||
| 241 | if (ok == 0) {
| 0 | ||||||||||||
| 242 | ECDSA_SIG_free(newsig); | - | ||||||||||||
| 243 | newsig = NULL; | - | ||||||||||||
| 244 | } never executed: end of block | 0 | ||||||||||||
| 245 | return newsig; never executed: return newsig; | 0 | ||||||||||||
| 246 | } | - | ||||||||||||
| 247 | - | |||||||||||||
| 248 | int | - | ||||||||||||
| 249 | gost2001_do_verify(BIGNUM *md, ECDSA_SIG *sig, GOST_KEY *ec) | - | ||||||||||||
| 250 | { | - | ||||||||||||
| 251 | BN_CTX *ctx = BN_CTX_new(); | - | ||||||||||||
| 252 | const EC_GROUP *group = GOST_KEY_get0_group(ec); | - | ||||||||||||
| 253 | BIGNUM *order; | - | ||||||||||||
| 254 | BIGNUM *e = NULL, *R = NULL, *v = NULL, *z1 = NULL, *z2 = NULL; | - | ||||||||||||
| 255 | BIGNUM *X = NULL, *tmp = NULL; | - | ||||||||||||
| 256 | EC_POINT *C = NULL; | - | ||||||||||||
| 257 | const EC_POINT *pub_key = NULL; | - | ||||||||||||
| 258 | int ok = 0; | - | ||||||||||||
| 259 | - | |||||||||||||
| 260 | if (ctx == NULL)
| 0 | ||||||||||||
| 261 | goto err; never executed: goto err; | 0 | ||||||||||||
| 262 | BN_CTX_start(ctx); | - | ||||||||||||
| 263 | if ((order = BN_CTX_get(ctx)) == NULL)
| 0 | ||||||||||||
| 264 | goto err; never executed: goto err; | 0 | ||||||||||||
| 265 | if ((e = BN_CTX_get(ctx)) == NULL)
| 0 | ||||||||||||
| 266 | goto err; never executed: goto err; | 0 | ||||||||||||
| 267 | if ((z1 = BN_CTX_get(ctx)) == NULL)
| 0 | ||||||||||||
| 268 | goto err; never executed: goto err; | 0 | ||||||||||||
| 269 | if ((z2 = BN_CTX_get(ctx)) == NULL)
| 0 | ||||||||||||
| 270 | goto err; never executed: goto err; | 0 | ||||||||||||
| 271 | if ((tmp = BN_CTX_get(ctx)) == NULL)
| 0 | ||||||||||||
| 272 | goto err; never executed: goto err; | 0 | ||||||||||||
| 273 | if ((X = BN_CTX_get(ctx)) == NULL)
| 0 | ||||||||||||
| 274 | goto err; never executed: goto err; | 0 | ||||||||||||
| 275 | if ((R = BN_CTX_get(ctx)) == NULL)
| 0 | ||||||||||||
| 276 | goto err; never executed: goto err; | 0 | ||||||||||||
| 277 | if ((v = BN_CTX_get(ctx)) == NULL)
| 0 | ||||||||||||
| 278 | goto err; never executed: goto err; | 0 | ||||||||||||
| 279 | - | |||||||||||||
| 280 | if (EC_GROUP_get_order(group, order, ctx) == 0)
| 0 | ||||||||||||
| 281 | goto err; never executed: goto err; | 0 | ||||||||||||
| 282 | pub_key = GOST_KEY_get0_public_key(ec); | - | ||||||||||||
| 283 | if (BN_is_zero(sig->s) || BN_is_zero(sig->r) ||
| 0 | ||||||||||||
| 284 | BN_cmp(sig->s, order) >= 1 || BN_cmp(sig->r, order) >= 1) {
| 0 | ||||||||||||
| 285 | GOSTerror(GOST_R_SIGNATURE_PARTS_GREATER_THAN_Q); | - | ||||||||||||
| 286 | goto err; never executed: goto err; | 0 | ||||||||||||
| 287 | } | - | ||||||||||||
| 288 | - | |||||||||||||
| 289 | if (BN_mod_ct(e, md, order, ctx) == 0)
| 0 | ||||||||||||
| 290 | goto err; never executed: goto err; | 0 | ||||||||||||
| 291 | if (BN_is_zero(e))
| 0 | ||||||||||||
| 292 | BN_one(e); never executed: (BN_set_word((e),1)); | 0 | ||||||||||||
| 293 | if ((v = BN_mod_inverse_ct(v, e, order, ctx)) == NULL)
| 0 | ||||||||||||
| 294 | goto err; never executed: goto err; | 0 | ||||||||||||
| 295 | if (BN_mod_mul(z1, sig->s, v, order, ctx) == 0)
| 0 | ||||||||||||
| 296 | goto err; never executed: goto err; | 0 | ||||||||||||
| 297 | if (BN_sub(tmp, order, sig->r) == 0)
| 0 | ||||||||||||
| 298 | goto err; never executed: goto err; | 0 | ||||||||||||
| 299 | if (BN_mod_mul(z2, tmp, v, order, ctx) == 0)
| 0 | ||||||||||||
| 300 | goto err; never executed: goto err; | 0 | ||||||||||||
| 301 | if ((C = EC_POINT_new(group)) == NULL)
| 0 | ||||||||||||
| 302 | goto err; never executed: goto err; | 0 | ||||||||||||
| 303 | if (EC_POINT_mul(group, C, z1, pub_key, z2, ctx) == 0) {
| 0 | ||||||||||||
| 304 | GOSTerror(ERR_R_EC_LIB); | - | ||||||||||||
| 305 | goto err; never executed: goto err; | 0 | ||||||||||||
| 306 | } | - | ||||||||||||
| 307 | if (EC_POINT_get_affine_coordinates_GFp(group, C, X, NULL, ctx) == 0) {
| 0 | ||||||||||||
| 308 | GOSTerror(ERR_R_EC_LIB); | - | ||||||||||||
| 309 | goto err; never executed: goto err; | 0 | ||||||||||||
| 310 | } | - | ||||||||||||
| 311 | if (BN_mod_ct(R, X, order, ctx) == 0)
| 0 | ||||||||||||
| 312 | goto err; never executed: goto err; | 0 | ||||||||||||
| 313 | if (BN_cmp(R, sig->r) != 0) {
| 0 | ||||||||||||
| 314 | GOSTerror(GOST_R_SIGNATURE_MISMATCH); | - | ||||||||||||
| 315 | } else { never executed: end of block | 0 | ||||||||||||
| 316 | ok = 1; | - | ||||||||||||
| 317 | } never executed: end of block | 0 | ||||||||||||
| 318 | err: code before this statement never executed: err: | 0 | ||||||||||||
| 319 | EC_POINT_free(C); | - | ||||||||||||
| 320 | if (ctx != NULL) {
| 0 | ||||||||||||
| 321 | BN_CTX_end(ctx); | - | ||||||||||||
| 322 | BN_CTX_free(ctx); | - | ||||||||||||
| 323 | } never executed: end of block | 0 | ||||||||||||
| 324 | return ok; never executed: return ok; | 0 | ||||||||||||
| 325 | } | - | ||||||||||||
| 326 | - | |||||||||||||
| 327 | /* Implementation of CryptoPro VKO 34.10-2001 algorithm */ | - | ||||||||||||
| 328 | int | - | ||||||||||||
| 329 | VKO_compute_key(BIGNUM *X, BIGNUM *Y, const GOST_KEY *pkey, GOST_KEY *priv_key, | - | ||||||||||||
| 330 | const BIGNUM *ukm) | - | ||||||||||||
| 331 | { | - | ||||||||||||
| 332 | BIGNUM *p = NULL, *order = NULL; | - | ||||||||||||
| 333 | const BIGNUM *key = GOST_KEY_get0_private_key(priv_key); | - | ||||||||||||
| 334 | const EC_GROUP *group = GOST_KEY_get0_group(priv_key); | - | ||||||||||||
| 335 | const EC_POINT *pub_key = GOST_KEY_get0_public_key(pkey); | - | ||||||||||||
| 336 | EC_POINT *pnt; | - | ||||||||||||
| 337 | BN_CTX *ctx = NULL; | - | ||||||||||||
| 338 | int ok = 0; | - | ||||||||||||
| 339 | - | |||||||||||||
| 340 | pnt = EC_POINT_new(group); | - | ||||||||||||
| 341 | if (pnt == NULL)
| 0 | ||||||||||||
| 342 | goto err; never executed: goto err; | 0 | ||||||||||||
| 343 | ctx = BN_CTX_new(); | - | ||||||||||||
| 344 | if (ctx == NULL)
| 0 | ||||||||||||
| 345 | goto err; never executed: goto err; | 0 | ||||||||||||
| 346 | BN_CTX_start(ctx); | - | ||||||||||||
| 347 | if ((p = BN_CTX_get(ctx)) == NULL)
| 0 | ||||||||||||
| 348 | goto err; never executed: goto err; | 0 | ||||||||||||
| 349 | if ((order = BN_CTX_get(ctx)) == NULL)
| 0 | ||||||||||||
| 350 | goto err; never executed: goto err; | 0 | ||||||||||||
| 351 | if (EC_GROUP_get_order(group, order, ctx) == 0)
| 0 | ||||||||||||
| 352 | goto err; never executed: goto err; | 0 | ||||||||||||
| 353 | if (BN_mod_mul(p, key, ukm, order, ctx) == 0)
| 0 | ||||||||||||
| 354 | goto err; never executed: goto err; | 0 | ||||||||||||
| 355 | if (EC_POINT_mul(group, pnt, NULL, pub_key, p, ctx) == 0)
| 0 | ||||||||||||
| 356 | goto err; never executed: goto err; | 0 | ||||||||||||
| 357 | if (EC_POINT_get_affine_coordinates_GFp(group, pnt, X, Y, ctx) == 0)
| 0 | ||||||||||||
| 358 | goto err; never executed: goto err; | 0 | ||||||||||||
| 359 | ok = 1; | - | ||||||||||||
| 360 | - | |||||||||||||
| 361 | err: code before this statement never executed: err: | 0 | ||||||||||||
| 362 | if (ctx != NULL) {
| 0 | ||||||||||||
| 363 | BN_CTX_end(ctx); | - | ||||||||||||
| 364 | BN_CTX_free(ctx); | - | ||||||||||||
| 365 | } never executed: end of block | 0 | ||||||||||||
| 366 | EC_POINT_free(pnt); | - | ||||||||||||
| 367 | return ok; never executed: return ok; | 0 | ||||||||||||
| 368 | } | - | ||||||||||||
| 369 | - | |||||||||||||
| 370 | int | - | ||||||||||||
| 371 | gost2001_keygen(GOST_KEY *ec) | - | ||||||||||||
| 372 | { | - | ||||||||||||
| 373 | BIGNUM *order = BN_new(), *d = BN_new(); | - | ||||||||||||
| 374 | const EC_GROUP *group = GOST_KEY_get0_group(ec); | - | ||||||||||||
| 375 | int rc = 0; | - | ||||||||||||
| 376 | - | |||||||||||||
| 377 | if (order == NULL || d == NULL)
| 0 | ||||||||||||
| 378 | goto err; never executed: goto err; | 0 | ||||||||||||
| 379 | if (EC_GROUP_get_order(group, order, NULL) == 0)
| 0 | ||||||||||||
| 380 | goto err; never executed: goto err; | 0 | ||||||||||||
| 381 | - | |||||||||||||
| 382 | do { | - | ||||||||||||
| 383 | if (BN_rand_range(d, order) == 0) {
| 0 | ||||||||||||
| 384 | GOSTerror(GOST_R_RANDOM_NUMBER_GENERATOR_FAILED); | - | ||||||||||||
| 385 | goto err; never executed: goto err; | 0 | ||||||||||||
| 386 | } | - | ||||||||||||
| 387 | } while (BN_is_zero(d)); never executed: end of block
| 0 | ||||||||||||
| 388 | - | |||||||||||||
| 389 | if (GOST_KEY_set_private_key(ec, d) == 0)
| 0 | ||||||||||||
| 390 | goto err; never executed: goto err; | 0 | ||||||||||||
| 391 | rc = gost2001_compute_public(ec); | - | ||||||||||||
| 392 | - | |||||||||||||
| 393 | err: code before this statement never executed: err: | 0 | ||||||||||||
| 394 | BN_free(d); | - | ||||||||||||
| 395 | BN_free(order); | - | ||||||||||||
| 396 | return rc; never executed: return rc; | 0 | ||||||||||||
| 397 | } | - | ||||||||||||
| 398 | #endif | - | ||||||||||||
| Source code | Switch to Preprocessed file |