| 1 |
/* ecc-misc.c - Elliptic Curve miscellaneous functions |
| 2 |
* Copyright (C) 2007, 2008, 2010, 2011 Free Software Foundation, Inc. |
| 3 |
* Copyright (C) 2013 g10 Code GmbH |
| 4 |
* |
| 5 |
* This file is part of Libgcrypt. |
| 6 |
* |
| 7 |
* Libgcrypt is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU Lesser General Public License as |
| 9 |
* published by the Free Software Foundation; either version 2.1 of |
| 10 |
* the License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* Libgcrypt is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU Lesser General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Lesser General Public |
| 18 |
* License along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include <config.h> |
| 22 |
#include <stdio.h> |
| 23 |
#include <stdlib.h> |
| 24 |
#include <string.h> |
| 25 |
#include <errno.h> |
| 26 |
|
| 27 |
#include "g10lib.h" |
| 28 |
#include "mpi.h" |
| 29 |
#include "cipher.h" |
| 30 |
#include "context.h" |
| 31 |
#include "ec-context.h" |
| 32 |
#include "ecc-common.h" |
| 33 |
|
| 34 |
|
| 35 |
/* |
| 36 |
* Release a curve object. |
| 37 |
*/ |
| 38 |
void |
| 39 |
_gcry_ecc_curve_free (elliptic_curve_t *E) |
| 40 |
{ |
| 41 |
mpi_free (E->p); E->p = NULL; |
| 42 |
mpi_free (E->a); E->a = NULL; |
| 43 |
mpi_free (E->b); E->b = NULL; |
| 44 |
_gcry_mpi_point_free_parts (&E->G); |
| 45 |
mpi_free (E->n); E->n = NULL; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
/* |
| 50 |
* Return a copy of a curve object. |
| 51 |
*/ |
| 52 |
elliptic_curve_t |
| 53 |
_gcry_ecc_curve_copy (elliptic_curve_t E) |
| 54 |
{ |
| 55 |
elliptic_curve_t R; |
| 56 |
|
| 57 |
R.model = E.model; |
| 58 |
R.dialect = E.dialect; |
| 59 |
R.name = E.name; |
| 60 |
R.p = mpi_copy (E.p); |
| 61 |
R.a = mpi_copy (E.a); |
| 62 |
R.b = mpi_copy (E.b); |
| 63 |
_gcry_mpi_point_init (&R.G, 0); |
| 64 |
point_set (&R.G, &E.G); |
| 65 |
R.n = mpi_copy (E.n); |
| 66 |
R.h = E.h; |
| 67 |
|
| 68 |
return R; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
/* |
| 73 |
* Return a description of the curve model. |
| 74 |
*/ |
| 75 |
const char * |
| 76 |
_gcry_ecc_model2str (enum gcry_mpi_ec_models model) |
| 77 |
{ |
| 78 |
const char *str = "?"; |
| 79 |
switch (model) |
| 80 |
{ |
| 81 |
case MPI_EC_WEIERSTRASS: str = "Weierstrass"; break; |
| 82 |
case MPI_EC_MONTGOMERY: str = "Montgomery"; break; |
| 83 |
case MPI_EC_EDWARDS: str = "Edwards"; break; |
| 84 |
} |
| 85 |
return str; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
/* |
| 90 |
* Return a description of the curve dialect. |
| 91 |
*/ |
| 92 |
const char * |
| 93 |
_gcry_ecc_dialect2str (enum ecc_dialects dialect) |
| 94 |
{ |
| 95 |
const char *str = "?"; |
| 96 |
switch (dialect) |
| 97 |
{ |
| 98 |
case ECC_DIALECT_STANDARD: str = "Standard"; break; |
| 99 |
case ECC_DIALECT_ED25519: str = "Ed25519"; break; |
| 100 |
case ECC_DIALECT_SAFECURVE: str = "SafeCurve"; break; |
| 101 |
} |
| 102 |
return str; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
/* Return an uncompressed point (X,Y) in P as a malloced buffer with |
| 107 |
* its byte length stored at R_LENGTH. May not be used for sensitive |
| 108 |
* data. */ |
| 109 |
unsigned char * |
| 110 |
_gcry_ecc_ec2os_buf (gcry_mpi_t x, gcry_mpi_t y, gcry_mpi_t p, |
| 111 |
unsigned int *r_length) |
| 112 |
{ |
| 113 |
gpg_err_code_t rc; |
| 114 |
int pbytes = (mpi_get_nbits (p)+7)/8; |
| 115 |
size_t n; |
| 116 |
unsigned char *buf, *ptr; |
| 117 |
|
| 118 |
buf = xmalloc ( 1 + 2*pbytes ); |
| 119 |
*buf = 04; /* Uncompressed point. */ |
| 120 |
ptr = buf+1; |
| 121 |
rc = _gcry_mpi_print (GCRYMPI_FMT_USG, ptr, pbytes, &n, x); |
| 122 |
if (rc) |
| 123 |
log_fatal ("mpi_print failed: %s\n", gpg_strerror (rc)); |
| 124 |
if (n < pbytes) |
| 125 |
{ |
| 126 |
memmove (ptr+(pbytes-n), ptr, n); |
| 127 |
memset (ptr, 0, (pbytes-n)); |
| 128 |
} |
| 129 |
ptr += pbytes; |
| 130 |
rc = _gcry_mpi_print (GCRYMPI_FMT_USG, ptr, pbytes, &n, y); |
| 131 |
if (rc) |
| 132 |
log_fatal ("mpi_print failed: %s\n", gpg_strerror (rc)); |
| 133 |
if (n < pbytes) |
| 134 |
{ |
| 135 |
memmove (ptr+(pbytes-n), ptr, n); |
| 136 |
memset (ptr, 0, (pbytes-n)); |
| 137 |
} |
| 138 |
|
| 139 |
*r_length = 1 + 2*pbytes; |
| 140 |
return buf; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
gcry_mpi_t |
| 145 |
_gcry_ecc_ec2os (gcry_mpi_t x, gcry_mpi_t y, gcry_mpi_t p) |
| 146 |
{ |
| 147 |
unsigned char *buf; |
| 148 |
unsigned int buflen; |
| 149 |
|
| 150 |
buf = _gcry_ecc_ec2os_buf (x, y, p, &buflen); |
| 151 |
return mpi_set_opaque (NULL, buf, 8*buflen); |
| 152 |
} |
| 153 |
|
| 154 |
/* Convert POINT into affine coordinates using the context CTX and |
| 155 |
return a newly allocated MPI. If the conversion is not possible |
| 156 |
NULL is returned. This function won't print an error message. */ |
| 157 |
gcry_mpi_t |
| 158 |
_gcry_mpi_ec_ec2os (gcry_mpi_point_t point, mpi_ec_t ec) |
| 159 |
{ |
| 160 |
gcry_mpi_t g_x, g_y, result; |
| 161 |
|
| 162 |
g_x = mpi_new (ec->nbits); |
| 163 |
g_y = mpi_new (ec->nbits); |
| 164 |
if (_gcry_mpi_ec_get_affine (g_x, g_y, point, ec)) |
| 165 |
result = NULL; |
| 166 |
else |
| 167 |
result = _gcry_ecc_ec2os (g_x, g_y, ec->p); |
| 168 |
mpi_free (g_x); |
| 169 |
mpi_free (g_y); |
| 170 |
|
| 171 |
return result; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
/* Decode octet string in VALUE into RESULT, in the format defined by SEC 1. |
| 176 |
RESULT must have been initialized and is set on success to the |
| 177 |
point given by VALUE. */ |
| 178 |
gpg_err_code_t |
| 179 |
_gcry_ecc_sec_decodepoint (gcry_mpi_t value, mpi_ec_t ec, mpi_point_t result) |
| 180 |
{ |
| 181 |
gpg_err_code_t rc; |
| 182 |
size_t n; |
| 183 |
const unsigned char *buf; |
| 184 |
unsigned char *buf_memory; |
| 185 |
gcry_mpi_t x, y; |
| 186 |
|
| 187 |
if (mpi_is_opaque (value)) |
| 188 |
{ |
| 189 |
unsigned int nbits; |
| 190 |
|
| 191 |
buf = mpi_get_opaque (value, &nbits); |
| 192 |
if (!buf) |
| 193 |
return GPG_ERR_INV_OBJ; |
| 194 |
n = (nbits + 7)/8; |
| 195 |
buf_memory = NULL; |
| 196 |
} |
| 197 |
else |
| 198 |
{ |
| 199 |
n = (mpi_get_nbits (value)+7)/8; |
| 200 |
buf_memory = xmalloc (n); |
| 201 |
rc = _gcry_mpi_print (GCRYMPI_FMT_USG, buf_memory, n, &n, value); |
| 202 |
if (rc) |
| 203 |
{ |
| 204 |
xfree (buf_memory); |
| 205 |
return rc; |
| 206 |
} |
| 207 |
buf = buf_memory; |
| 208 |
} |
| 209 |
|
| 210 |
if (n < 1) |
| 211 |
{ |
| 212 |
xfree (buf_memory); |
| 213 |
return GPG_ERR_INV_OBJ; |
| 214 |
} |
| 215 |
|
| 216 |
if (*buf == 2 || *buf == 3) |
| 217 |
{ |
| 218 |
gcry_mpi_t x3; |
| 219 |
gcry_mpi_t t; |
| 220 |
gcry_mpi_t p1_4; |
| 221 |
int y_bit = (*buf == 3); |
| 222 |
|
| 223 |
if (ec == NULL || !mpi_test_bit (ec->p, 1)) |
| 224 |
{ |
| 225 |
xfree (buf_memory); |
| 226 |
return GPG_ERR_NOT_IMPLEMENTED; /* No support for point compression. */ |
| 227 |
} |
| 228 |
|
| 229 |
n = n - 1; |
| 230 |
rc = _gcry_mpi_scan (&x, GCRYMPI_FMT_USG, buf+1, n, NULL); |
| 231 |
xfree (buf_memory); |
| 232 |
if (rc) |
| 233 |
return rc; |
| 234 |
|
| 235 |
/* |
| 236 |
* Recover Y. The Weierstrass curve: y^2 = x^3 + a*x + b |
| 237 |
*/ |
| 238 |
|
| 239 |
x3 = mpi_new (ec->nbits); |
| 240 |
t = mpi_new (ec->nbits); |
| 241 |
p1_4 = mpi_new (ec->nbits); |
| 242 |
y = mpi_new (ec->nbits); |
| 243 |
|
| 244 |
/* Compute right hand side. */ |
| 245 |
mpi_powm (x3, x, mpi_const (MPI_C_THREE), ec->p); |
| 246 |
mpi_mul (t, ec->a, x); |
| 247 |
mpi_mod (t, t, ec->p); |
| 248 |
mpi_add (t, t, ec->b); |
| 249 |
mpi_mod (t, t, ec->p); |
| 250 |
mpi_add (t, t, x3); |
| 251 |
mpi_mod (t, t, ec->p); |
| 252 |
|
| 253 |
/* |
| 254 |
* When p mod 4 = 3, modular square root of A can be computed by |
| 255 |
* A^((p+1)/4) mod p |
| 256 |
*/ |
| 257 |
|
| 258 |
/* Compute (p+1)/4 into p1_4 */ |
| 259 |
mpi_rshift (p1_4, ec->p, 2); |
| 260 |
_gcry_mpi_add_ui (p1_4, p1_4, 1); |
| 261 |
|
| 262 |
mpi_powm (y, t, p1_4, ec->p); |
| 263 |
|
| 264 |
if (y_bit != mpi_test_bit (y, 0)) |
| 265 |
mpi_sub (y, ec->p, y); |
| 266 |
|
| 267 |
mpi_free (p1_4); |
| 268 |
mpi_free (t); |
| 269 |
mpi_free (x3); |
| 270 |
} |
| 271 |
else if (*buf == 4) |
| 272 |
{ |
| 273 |
if ( ((n-1)%2) ) |
| 274 |
{ |
| 275 |
xfree (buf_memory); |
| 276 |
return GPG_ERR_INV_OBJ; |
| 277 |
} |
| 278 |
n = (n-1)/2; |
| 279 |
rc = _gcry_mpi_scan (&x, GCRYMPI_FMT_USG, buf+1, n, NULL); |
| 280 |
if (rc) |
| 281 |
{ |
| 282 |
xfree (buf_memory); |
| 283 |
return rc; |
| 284 |
} |
| 285 |
rc = _gcry_mpi_scan (&y, GCRYMPI_FMT_USG, buf+1+n, n, NULL); |
| 286 |
xfree (buf_memory); |
| 287 |
if (rc) |
| 288 |
{ |
| 289 |
mpi_free (x); |
| 290 |
return rc; |
| 291 |
} |
| 292 |
} |
| 293 |
else |
| 294 |
{ |
| 295 |
xfree (buf_memory); |
| 296 |
return GPG_ERR_INV_OBJ; |
| 297 |
} |
| 298 |
|
| 299 |
mpi_set (result->x, x); |
| 300 |
mpi_set (result->y, y); |
| 301 |
mpi_set_ui (result->z, 1); |
| 302 |
|
| 303 |
mpi_free (x); |
| 304 |
mpi_free (y); |
| 305 |
|
| 306 |
return 0; |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
/* Compute the public key from the the context EC. Obviously a |
| 311 |
requirement is that the secret key is available in EC. On success |
| 312 |
Q is returned; on error NULL. If Q is NULL a newly allocated point |
| 313 |
is returned. If G or D are given they override the values taken |
| 314 |
from EC. */ |
| 315 |
mpi_point_t |
| 316 |
_gcry_ecc_compute_public (mpi_point_t Q, mpi_ec_t ec) |
| 317 |
{ |
| 318 |
if (!ec->d || !ec->G || !ec->p || !ec->a) |
| 319 |
return NULL; |
| 320 |
if (ec->model == MPI_EC_EDWARDS && !ec->b) |
| 321 |
return NULL; |
| 322 |
|
| 323 |
if ((ec->dialect == ECC_DIALECT_ED25519 && (ec->flags & PUBKEY_FLAG_EDDSA)) |
| 324 |
|| (ec->model == MPI_EC_EDWARDS && ec->dialect == ECC_DIALECT_SAFECURVE)) |
| 325 |
{ |
| 326 |
gcry_mpi_t a; |
| 327 |
unsigned char *digest; |
| 328 |
int b; |
| 329 |
|
| 330 |
b = (ec->nbits+7)/8; |
| 331 |
if (ec->nbits == 255) |
| 332 |
; |
| 333 |
else if (ec->nbits == 448) |
| 334 |
b++; |
| 335 |
else |
| 336 |
return NULL; /* Not implemented. */ |
| 337 |
|
| 338 |
if (_gcry_ecc_eddsa_compute_h_d (&digest, ec)) |
| 339 |
return NULL; |
| 340 |
|
| 341 |
a = mpi_snew (0); |
| 342 |
_gcry_mpi_set_buffer (a, digest, b, 0); |
| 343 |
xfree (digest); |
| 344 |
|
| 345 |
/* And finally the public key. */ |
| 346 |
if (!Q) |
| 347 |
Q = mpi_point_new (ec->nbits); |
| 348 |
if (Q) |
| 349 |
_gcry_mpi_ec_mul_point (Q, a, ec->G, ec); |
| 350 |
mpi_free (a); |
| 351 |
} |
| 352 |
else |
| 353 |
{ |
| 354 |
if (!Q) |
| 355 |
Q = mpi_point_new (ec->nbits); |
| 356 |
if (Q) |
| 357 |
_gcry_mpi_ec_mul_point (Q, ec->d, ec->G, ec); |
| 358 |
} |
| 359 |
|
| 360 |
return Q; |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
gpg_err_code_t |
| 365 |
_gcry_ecc_mont_encodepoint (gcry_mpi_t x, unsigned int nbits, |
| 366 |
int with_prefix, |
| 367 |
unsigned char **r_buffer, unsigned int *r_buflen) |
| 368 |
{ |
| 369 |
unsigned char *rawmpi; |
| 370 |
unsigned int rawmpilen; |
| 371 |
|
| 372 |
rawmpi = _gcry_mpi_get_buffer_extra (x, (nbits+7)/8, |
| 373 |
with_prefix? -1 : 0, &rawmpilen, NULL); |
| 374 |
if (rawmpi == NULL) |
| 375 |
return gpg_err_code_from_syserror (); |
| 376 |
|
| 377 |
if (with_prefix) |
| 378 |
{ |
| 379 |
rawmpi[0] = 0x40; |
| 380 |
rawmpilen++; |
| 381 |
} |
| 382 |
|
| 383 |
*r_buffer = rawmpi; |
| 384 |
*r_buflen = rawmpilen; |
| 385 |
return 0; |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
gpg_err_code_t |
| 390 |
_gcry_ecc_mont_decodepoint (gcry_mpi_t pk, mpi_ec_t ec, mpi_point_t result) |
| 391 |
{ |
| 392 |
unsigned char *rawmpi; |
| 393 |
unsigned int rawmpilen; |
| 394 |
unsigned int nbytes = (ec->nbits+7)/8; |
| 395 |
|
| 396 |
/* |
| 397 |
* It is not reliable to assume that the first byte of 0x40 |
| 398 |
* means the prefix. |
| 399 |
* |
| 400 |
* For newer implementation, it is reliable since we always put |
| 401 |
* 0x40 for x-only coordinate. |
| 402 |
* |
| 403 |
* For data by older implementation (non-released development |
| 404 |
* version in 2015), there is no 0x40 prefix added. |
| 405 |
* |
| 406 |
* So, it is possible to have shorter length of data when it was |
| 407 |
* handled as MPI, removing preceding zeros. |
| 408 |
* |
| 409 |
* Besides, when data was parsed as MPI, we might have 0x00 |
| 410 |
* prefix (when the MSB in the first byte is set). |
| 411 |
*/ |
| 412 |
|
| 413 |
if (mpi_is_opaque (pk)) |
| 414 |
{ |
| 415 |
const unsigned char *buf; |
| 416 |
unsigned char *p; |
| 417 |
|
| 418 |
buf = mpi_get_opaque (pk, &rawmpilen); |
| 419 |
if (!buf) |
| 420 |
return GPG_ERR_INV_OBJ; |
| 421 |
rawmpilen = (rawmpilen + 7)/8; |
| 422 |
|
| 423 |
if (rawmpilen == nbytes + 1 |
| 424 |
&& (buf[0] == 0x00 || buf[0] == 0x40)) |
| 425 |
{ |
| 426 |
rawmpilen--; |
| 427 |
buf++; |
| 428 |
} |
| 429 |
else if (rawmpilen > nbytes) |
| 430 |
return GPG_ERR_INV_OBJ; |
| 431 |
|
| 432 |
rawmpi = xtrymalloc (nbytes); |
| 433 |
if (!rawmpi) |
| 434 |
return gpg_err_code_from_syserror (); |
| 435 |
|
| 436 |
p = rawmpi + rawmpilen; |
| 437 |
while (p > rawmpi) |
| 438 |
*--p = *buf++; |
| 439 |
|
| 440 |
if (rawmpilen < nbytes) |
| 441 |
{ |
| 442 |
memset (rawmpi + rawmpilen, 0, nbytes - rawmpilen); |
| 443 |
rawmpilen = nbytes; |
| 444 |
} |
| 445 |
} |
| 446 |
else |
| 447 |
{ |
| 448 |
rawmpi = _gcry_mpi_get_buffer (pk, nbytes, &rawmpilen, NULL); |
| 449 |
if (!rawmpi) |
| 450 |
return gpg_err_code_from_syserror (); |
| 451 |
if (rawmpilen > nbytes + BYTES_PER_MPI_LIMB) |
| 452 |
{ |
| 453 |
xfree (rawmpi); |
| 454 |
return GPG_ERR_INV_OBJ; |
| 455 |
} |
| 456 |
/* |
| 457 |
* When we have the prefix (0x40 or 0x00), it comes at the end, |
| 458 |
* since it is taken by _gcry_mpi_get_buffer with little endian. |
| 459 |
* Just setting RAWMPILEN to NBYTES is enough in this case. |
| 460 |
* Othewise, RAWMPILEN is NBYTES already. |
| 461 |
*/ |
| 462 |
rawmpilen = nbytes; |
| 463 |
} |
| 464 |
|
| 465 |
if ((ec->nbits % 8)) |
| 466 |
rawmpi[0] &= (1 << (ec->nbits % 8)) - 1; |
| 467 |
_gcry_mpi_set_buffer (result->x, rawmpi, rawmpilen, 0); |
| 468 |
xfree (rawmpi); |
| 469 |
mpi_set_ui (result->z, 1); |
| 470 |
|
| 471 |
return 0; |
| 472 |
} |