| 1 |
/* |
| 2 |
* Copyright 2011-2025 The OpenSSL Project Authors. All Rights Reserved. |
| 3 |
* Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved |
| 4 |
* |
| 5 |
* Licensed under the Apache License 2.0 (the "License"). You may not use |
| 6 |
* this file except in compliance with the License. You can obtain a copy |
| 7 |
* in the file LICENSE in the source distribution or at |
| 8 |
* https://www.openssl.org/source/license.html |
| 9 |
*/ |
| 10 |
|
| 11 |
/* |
| 12 |
* ECDSA low level APIs are deprecated for public use, but still ok for |
| 13 |
* internal use. |
| 14 |
*/ |
| 15 |
#include "internal/deprecated.h" |
| 16 |
|
| 17 |
#include <openssl/err.h> |
| 18 |
|
| 19 |
#include "ec_local.h" |
| 20 |
|
| 21 |
#ifndef OPENSSL_NO_EC2M |
| 22 |
|
| 23 |
/*- |
| 24 |
* Calculates and sets the affine coordinates of an EC_POINT from the given |
| 25 |
* compressed coordinates. Uses algorithm 2.3.4 of SEC 1. |
| 26 |
* Note that the simple implementation only uses affine coordinates. |
| 27 |
* |
| 28 |
* The method is from the following publication: |
| 29 |
* |
| 30 |
* Harper, Menezes, Vanstone: |
| 31 |
* "Public-Key Cryptosystems with Very Small Key Lengths", |
| 32 |
* EUROCRYPT '92, Springer-Verlag LNCS 658, |
| 33 |
* published February 1993 |
| 34 |
* |
| 35 |
* US Patents 6,141,420 and 6,618,483 (Vanstone, Mullin, Agnew) describe |
| 36 |
* the same method, but claim no priority date earlier than July 29, 1994 |
| 37 |
* (and additionally fail to cite the EUROCRYPT '92 publication as prior art). |
| 38 |
*/ |
| 39 |
int ossl_ec_GF2m_simple_set_compressed_coordinates(const EC_GROUP *group, |
| 40 |
EC_POINT *point, |
| 41 |
const BIGNUM *x_, int y_bit, |
| 42 |
BN_CTX *ctx) |
| 43 |
{ |
| 44 |
BIGNUM *tmp, *x, *y, *z; |
| 45 |
int ret = 0, z0; |
| 46 |
#ifndef FIPS_MODULE |
| 47 |
BN_CTX *new_ctx = NULL; |
| 48 |
|
| 49 |
if (ctx == NULL) { |
| 50 |
ctx = new_ctx = BN_CTX_new(); |
| 51 |
if (ctx == NULL) |
| 52 |
return 0; |
| 53 |
} |
| 54 |
#endif |
| 55 |
|
| 56 |
y_bit = (y_bit != 0) ? 1 : 0; |
| 57 |
|
| 58 |
BN_CTX_start(ctx); |
| 59 |
tmp = BN_CTX_get(ctx); |
| 60 |
x = BN_CTX_get(ctx); |
| 61 |
y = BN_CTX_get(ctx); |
| 62 |
z = BN_CTX_get(ctx); |
| 63 |
if (z == NULL) |
| 64 |
goto err; |
| 65 |
|
| 66 |
if (!BN_GF2m_mod_arr(x, x_, group->poly)) |
| 67 |
goto err; |
| 68 |
if (BN_is_zero(x)) { |
| 69 |
if (!BN_GF2m_mod_sqrt_arr(y, group->b, group->poly, ctx)) |
| 70 |
goto err; |
| 71 |
} else { |
| 72 |
if (!group->meth->field_sqr(group, tmp, x, ctx)) |
| 73 |
goto err; |
| 74 |
if (!group->meth->field_div(group, tmp, group->b, tmp, ctx)) |
| 75 |
goto err; |
| 76 |
if (!BN_GF2m_add(tmp, group->a, tmp)) |
| 77 |
goto err; |
| 78 |
if (!BN_GF2m_add(tmp, x, tmp)) |
| 79 |
goto err; |
| 80 |
ERR_set_mark(); |
| 81 |
if (!BN_GF2m_mod_solve_quad_arr(z, tmp, group->poly, ctx)) { |
| 82 |
#ifndef FIPS_MODULE |
| 83 |
unsigned long err = ERR_peek_last_error(); |
| 84 |
|
| 85 |
if (ERR_GET_LIB(err) == ERR_LIB_BN |
| 86 |
&& ERR_GET_REASON(err) == BN_R_NO_SOLUTION) { |
| 87 |
ERR_pop_to_mark(); |
| 88 |
ERR_raise(ERR_LIB_EC, EC_R_INVALID_COMPRESSED_POINT); |
| 89 |
} else |
| 90 |
#endif |
| 91 |
{ |
| 92 |
ERR_clear_last_mark(); |
| 93 |
ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB); |
| 94 |
} |
| 95 |
goto err; |
| 96 |
} |
| 97 |
ERR_clear_last_mark(); |
| 98 |
z0 = (BN_is_odd(z)) ? 1 : 0; |
| 99 |
if (!group->meth->field_mul(group, y, x, z, ctx)) |
| 100 |
goto err; |
| 101 |
if (z0 != y_bit) { |
| 102 |
if (!BN_GF2m_add(y, y, x)) |
| 103 |
goto err; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx)) |
| 108 |
goto err; |
| 109 |
|
| 110 |
ret = 1; |
| 111 |
|
| 112 |
err: |
| 113 |
BN_CTX_end(ctx); |
| 114 |
#ifndef FIPS_MODULE |
| 115 |
BN_CTX_free(new_ctx); |
| 116 |
#endif |
| 117 |
return ret; |
| 118 |
} |
| 119 |
|
| 120 |
/* |
| 121 |
* Converts an EC_POINT to an octet string. If buf is NULL, the encoded |
| 122 |
* length will be returned. If the length len of buf is smaller than required |
| 123 |
* an error will be returned. |
| 124 |
*/ |
| 125 |
size_t ossl_ec_GF2m_simple_point2oct(const EC_GROUP *group, |
| 126 |
const EC_POINT *point, |
| 127 |
point_conversion_form_t form, |
| 128 |
unsigned char *buf, size_t len, BN_CTX *ctx) |
| 129 |
{ |
| 130 |
size_t ret; |
| 131 |
int used_ctx = 0; |
| 132 |
BIGNUM *x, *y, *yxi; |
| 133 |
size_t field_len, i, skip; |
| 134 |
#ifndef FIPS_MODULE |
| 135 |
BN_CTX *new_ctx = NULL; |
| 136 |
#endif |
| 137 |
|
| 138 |
if ((form != POINT_CONVERSION_COMPRESSED) |
| 139 |
&& (form != POINT_CONVERSION_UNCOMPRESSED) |
| 140 |
&& (form != POINT_CONVERSION_HYBRID)) { |
| 141 |
ERR_raise(ERR_LIB_EC, EC_R_INVALID_FORM); |
| 142 |
goto err; |
| 143 |
} |
| 144 |
|
| 145 |
if (EC_POINT_is_at_infinity(group, point)) { |
| 146 |
/* encodes to a single 0 octet */ |
| 147 |
if (buf != NULL) { |
| 148 |
if (len < 1) { |
| 149 |
ERR_raise(ERR_LIB_EC, EC_R_BUFFER_TOO_SMALL); |
| 150 |
return 0; |
| 151 |
} |
| 152 |
buf[0] = 0; |
| 153 |
} |
| 154 |
return 1; |
| 155 |
} |
| 156 |
|
| 157 |
/* ret := required output buffer length */ |
| 158 |
field_len = (EC_GROUP_get_degree(group) + 7) / 8; |
| 159 |
ret = (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len; |
| 160 |
|
| 161 |
/* if 'buf' is NULL, just return required length */ |
| 162 |
if (buf != NULL) { |
| 163 |
if (len < ret) { |
| 164 |
ERR_raise(ERR_LIB_EC, EC_R_BUFFER_TOO_SMALL); |
| 165 |
goto err; |
| 166 |
} |
| 167 |
|
| 168 |
#ifndef FIPS_MODULE |
| 169 |
if (ctx == NULL) { |
| 170 |
ctx = new_ctx = BN_CTX_new(); |
| 171 |
if (ctx == NULL) |
| 172 |
return 0; |
| 173 |
} |
| 174 |
#endif |
| 175 |
|
| 176 |
BN_CTX_start(ctx); |
| 177 |
used_ctx = 1; |
| 178 |
x = BN_CTX_get(ctx); |
| 179 |
y = BN_CTX_get(ctx); |
| 180 |
yxi = BN_CTX_get(ctx); |
| 181 |
if (yxi == NULL) |
| 182 |
goto err; |
| 183 |
|
| 184 |
if (!EC_POINT_get_affine_coordinates(group, point, x, y, ctx)) |
| 185 |
goto err; |
| 186 |
|
| 187 |
buf[0] = form; |
| 188 |
if ((form != POINT_CONVERSION_UNCOMPRESSED) && !BN_is_zero(x)) { |
| 189 |
if (!group->meth->field_div(group, yxi, y, x, ctx)) |
| 190 |
goto err; |
| 191 |
if (BN_is_odd(yxi)) |
| 192 |
buf[0]++; |
| 193 |
} |
| 194 |
|
| 195 |
i = 1; |
| 196 |
|
| 197 |
skip = field_len - BN_num_bytes(x); |
| 198 |
if (skip > field_len) { |
| 199 |
ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR); |
| 200 |
goto err; |
| 201 |
} |
| 202 |
while (skip > 0) { |
| 203 |
buf[i++] = 0; |
| 204 |
skip--; |
| 205 |
} |
| 206 |
skip = BN_bn2bin(x, buf + i); |
| 207 |
i += skip; |
| 208 |
if (i != 1 + field_len) { |
| 209 |
ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR); |
| 210 |
goto err; |
| 211 |
} |
| 212 |
|
| 213 |
if (form == POINT_CONVERSION_UNCOMPRESSED |
| 214 |
|| form == POINT_CONVERSION_HYBRID) { |
| 215 |
skip = field_len - BN_num_bytes(y); |
| 216 |
if (skip > field_len) { |
| 217 |
ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR); |
| 218 |
goto err; |
| 219 |
} |
| 220 |
while (skip > 0) { |
| 221 |
buf[i++] = 0; |
| 222 |
skip--; |
| 223 |
} |
| 224 |
skip = BN_bn2bin(y, buf + i); |
| 225 |
i += skip; |
| 226 |
} |
| 227 |
|
| 228 |
if (i != ret) { |
| 229 |
ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR); |
| 230 |
goto err; |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
if (used_ctx) |
| 235 |
BN_CTX_end(ctx); |
| 236 |
#ifndef FIPS_MODULE |
| 237 |
BN_CTX_free(new_ctx); |
| 238 |
#endif |
| 239 |
return ret; |
| 240 |
|
| 241 |
err: |
| 242 |
if (used_ctx) |
| 243 |
BN_CTX_end(ctx); |
| 244 |
#ifndef FIPS_MODULE |
| 245 |
BN_CTX_free(new_ctx); |
| 246 |
#endif |
| 247 |
return 0; |
| 248 |
} |
| 249 |
|
| 250 |
/* |
| 251 |
* Converts an octet string representation to an EC_POINT. Note that the |
| 252 |
* simple implementation only uses affine coordinates. |
| 253 |
*/ |
| 254 |
int ossl_ec_GF2m_simple_oct2point(const EC_GROUP *group, EC_POINT *point, |
| 255 |
const unsigned char *buf, size_t len, |
| 256 |
BN_CTX *ctx) |
| 257 |
{ |
| 258 |
point_conversion_form_t form; |
| 259 |
int y_bit, m; |
| 260 |
BIGNUM *x, *y, *yxi; |
| 261 |
int field_len, enc_len; |
| 262 |
int ret = 0; |
| 263 |
#ifndef FIPS_MODULE |
| 264 |
BN_CTX *new_ctx = NULL; |
| 265 |
#endif |
| 266 |
|
| 267 |
if (len == 0) { |
| 268 |
ERR_raise(ERR_LIB_EC, EC_R_BUFFER_TOO_SMALL); |
| 269 |
return 0; |
| 270 |
} |
| 271 |
|
| 272 |
/* |
| 273 |
* The first octet is the point conversion octet PC, see X9.62, page 4 |
| 274 |
* and section 4.4.2. It must be: |
| 275 |
* 0x00 for the point at infinity |
| 276 |
* 0x02 or 0x03 for compressed form |
| 277 |
* 0x04 for uncompressed form |
| 278 |
* 0x06 or 0x07 for hybrid form. |
| 279 |
* For compressed or hybrid forms, we store the last bit of buf[0] as |
| 280 |
* y_bit and clear it from buf[0] so as to obtain a POINT_CONVERSION_*. |
| 281 |
* We error if buf[0] contains any but the above values. |
| 282 |
*/ |
| 283 |
y_bit = buf[0] & 1; |
| 284 |
form = buf[0] & ~1U; |
| 285 |
|
| 286 |
if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED) |
| 287 |
&& (form != POINT_CONVERSION_UNCOMPRESSED) |
| 288 |
&& (form != POINT_CONVERSION_HYBRID)) { |
| 289 |
ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING); |
| 290 |
return 0; |
| 291 |
} |
| 292 |
if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit) { |
| 293 |
ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING); |
| 294 |
return 0; |
| 295 |
} |
| 296 |
|
| 297 |
/* The point at infinity is represented by a single zero octet. */ |
| 298 |
if (form == 0) { |
| 299 |
if (len != 1) { |
| 300 |
ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING); |
| 301 |
return 0; |
| 302 |
} |
| 303 |
|
| 304 |
return EC_POINT_set_to_infinity(group, point); |
| 305 |
} |
| 306 |
|
| 307 |
m = EC_GROUP_get_degree(group); |
| 308 |
field_len = (m + 7) / 8; |
| 309 |
enc_len = (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len; |
| 310 |
|
| 311 |
if (len != (size_t)enc_len) { |
| 312 |
ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING); |
| 313 |
return 0; |
| 314 |
} |
| 315 |
|
| 316 |
#ifndef FIPS_MODULE |
| 317 |
if (ctx == NULL) { |
| 318 |
ctx = new_ctx = BN_CTX_new(); |
| 319 |
if (ctx == NULL) |
| 320 |
return 0; |
| 321 |
} |
| 322 |
#endif |
| 323 |
|
| 324 |
BN_CTX_start(ctx); |
| 325 |
x = BN_CTX_get(ctx); |
| 326 |
y = BN_CTX_get(ctx); |
| 327 |
yxi = BN_CTX_get(ctx); |
| 328 |
if (yxi == NULL) |
| 329 |
goto err; |
| 330 |
|
| 331 |
if (!BN_bin2bn(buf + 1, field_len, x)) |
| 332 |
goto err; |
| 333 |
if (BN_num_bits(x) > m) { |
| 334 |
ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING); |
| 335 |
goto err; |
| 336 |
} |
| 337 |
|
| 338 |
if (form == POINT_CONVERSION_COMPRESSED) { |
| 339 |
if (!EC_POINT_set_compressed_coordinates(group, point, x, y_bit, ctx)) |
| 340 |
goto err; |
| 341 |
} else { |
| 342 |
if (!BN_bin2bn(buf + 1 + field_len, field_len, y)) |
| 343 |
goto err; |
| 344 |
if (BN_num_bits(y) > m) { |
| 345 |
ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING); |
| 346 |
goto err; |
| 347 |
} |
| 348 |
if (form == POINT_CONVERSION_HYBRID) { |
| 349 |
/* |
| 350 |
* Check that the form in the encoding was set correctly |
| 351 |
* according to X9.62 4.4.2.a, 4(c), see also first paragraph |
| 352 |
* of X9.62, 4.4.1.b. |
| 353 |
*/ |
| 354 |
if (BN_is_zero(x)) { |
| 355 |
if (y_bit != 0) { |
| 356 |
ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING); |
| 357 |
goto err; |
| 358 |
} |
| 359 |
} else { |
| 360 |
if (!group->meth->field_div(group, yxi, y, x, ctx)) |
| 361 |
goto err; |
| 362 |
if (y_bit != BN_is_odd(yxi)) { |
| 363 |
ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING); |
| 364 |
goto err; |
| 365 |
} |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
/* |
| 370 |
* EC_POINT_set_affine_coordinates is responsible for checking that |
| 371 |
* the point is on the curve. |
| 372 |
*/ |
| 373 |
if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx)) |
| 374 |
goto err; |
| 375 |
} |
| 376 |
|
| 377 |
ret = 1; |
| 378 |
|
| 379 |
err: |
| 380 |
BN_CTX_end(ctx); |
| 381 |
#ifndef FIPS_MODULE |
| 382 |
BN_CTX_free(new_ctx); |
| 383 |
#endif |
| 384 |
return ret; |
| 385 |
} |
| 386 |
#endif |