| 1 |
/* ecc-ecdh.c - Elliptic Curve Diffie-Hellman key agreement |
| 2 |
* Copyright (C) 2019 g10 Code GmbH |
| 3 |
* |
| 4 |
* This file is part of Libgcrypt. |
| 5 |
* |
| 6 |
* Libgcrypt is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU Lesser General Public License as |
| 8 |
* published by the Free Software Foundation; either version 2.1 of |
| 9 |
* the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* Libgcrypt is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public License |
| 17 |
* along with this program; if not, see <https://www.gnu.org/licenses/>. |
| 18 |
* SPDX-License-Identifier: LGPL-2.1+ |
| 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 |
#define ECC_CURVE25519_BYTES 32 |
| 35 |
#define ECC_CURVE448_BYTES 56 |
| 36 |
|
| 37 |
static gpg_err_code_t |
| 38 |
prepare_ec (mpi_ec_t *r_ec, const char *name) |
| 39 |
{ |
| 40 |
int flags = 0; |
| 41 |
|
| 42 |
if (!strcmp (name, "Curve25519")) |
| 43 |
flags = PUBKEY_FLAG_DJB_TWEAK; |
| 44 |
|
| 45 |
return _gcry_mpi_ec_internal_new (r_ec, &flags, "ecc_mul_point", NULL, name); |
| 46 |
} |
| 47 |
|
| 48 |
unsigned int |
| 49 |
_gcry_ecc_get_algo_keylen (int curveid) |
| 50 |
{ |
| 51 |
unsigned int len = 0; |
| 52 |
|
| 53 |
if (curveid == GCRY_ECC_CURVE25519) |
| 54 |
len = ECC_CURVE25519_BYTES; |
| 55 |
else if (curveid == GCRY_ECC_CURVE448) |
| 56 |
len = ECC_CURVE448_BYTES; |
| 57 |
|
| 58 |
return len; |
| 59 |
} |
| 60 |
|
| 61 |
/* For Curve25519 and X448, we need to mask the bits and enable the MSB. */ |
| 62 |
static void |
| 63 |
ecc_tweak_bits (unsigned char *seckey, size_t seckey_len) |
| 64 |
{ |
| 65 |
if (seckey_len == 32) |
| 66 |
{ |
| 67 |
seckey[0] &= 0xf8; |
| 68 |
seckey[31] &= 0x7f; |
| 69 |
seckey[31] |= 0x40; |
| 70 |
} |
| 71 |
else |
| 72 |
{ |
| 73 |
seckey[0] &= 0xfc; |
| 74 |
seckey[55] |= 0x80; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
gpg_err_code_t |
| 79 |
_gcry_ecc_curve_keypair (const char *curve, |
| 80 |
unsigned char *pubkey, size_t pubkey_len, |
| 81 |
unsigned char *seckey, size_t seckey_len) |
| 82 |
{ |
| 83 |
gpg_err_code_t err; |
| 84 |
unsigned int nbits; |
| 85 |
unsigned int nbytes; |
| 86 |
gcry_mpi_t mpi_k = NULL; |
| 87 |
mpi_ec_t ec = NULL; |
| 88 |
mpi_point_struct Q = { NULL, NULL, NULL }; |
| 89 |
gcry_mpi_t x; |
| 90 |
unsigned int len; |
| 91 |
unsigned char *buf; |
| 92 |
|
| 93 |
err = prepare_ec (&ec, curve); |
| 94 |
if (err) |
| 95 |
return err; |
| 96 |
|
| 97 |
nbits = ec->nbits; |
| 98 |
nbytes = (nbits + 7)/8; |
| 99 |
|
| 100 |
if (seckey_len != nbytes) |
| 101 |
return GPG_ERR_INV_ARG; |
| 102 |
|
| 103 |
if (ec->model == MPI_EC_WEIERSTRASS) |
| 104 |
{ |
| 105 |
if (pubkey_len != 1 + 2*nbytes) |
| 106 |
return GPG_ERR_INV_ARG; |
| 107 |
|
| 108 |
do |
| 109 |
{ |
| 110 |
mpi_free (mpi_k); |
| 111 |
mpi_k = mpi_new (nbytes*8); |
| 112 |
_gcry_randomize (seckey, nbytes, GCRY_STRONG_RANDOM); |
| 113 |
_gcry_mpi_set_buffer (mpi_k, seckey, nbytes, 0); |
| 114 |
} |
| 115 |
while (mpi_cmp (mpi_k, ec->n) >= 0); |
| 116 |
} |
| 117 |
else if (ec->model == MPI_EC_MONTGOMERY) |
| 118 |
{ |
| 119 |
if (pubkey_len != nbytes) |
| 120 |
return GPG_ERR_INV_ARG; |
| 121 |
|
| 122 |
_gcry_randomize (seckey, nbytes, GCRY_STRONG_RANDOM); |
| 123 |
/* Existing ECC applications with libgcrypt (like gpg-agent in |
| 124 |
GnuPG) assumes that scalar is tweaked at key generation time. |
| 125 |
For the possible use case where generated key with this routine |
| 126 |
may be used with those, we put compatibile behavior here. */ |
| 127 |
ecc_tweak_bits (seckey, nbytes); |
| 128 |
mpi_k = _gcry_mpi_set_opaque_copy (NULL, seckey, nbytes*8); |
| 129 |
} |
| 130 |
else |
| 131 |
return GPG_ERR_UNKNOWN_CURVE; |
| 132 |
|
| 133 |
x = mpi_new (nbits); |
| 134 |
point_init (&Q, ec->nbits); |
| 135 |
|
| 136 |
_gcry_mpi_ec_mul_point (&Q, mpi_k, ec->G, ec); |
| 137 |
|
| 138 |
if (ec->model == MPI_EC_WEIERSTRASS) |
| 139 |
{ |
| 140 |
gcry_mpi_t y = mpi_new (nbits); |
| 141 |
gcry_mpi_t negative = mpi_new (nbits); |
| 142 |
|
| 143 |
_gcry_mpi_ec_get_affine (x, y, &Q, ec); |
| 144 |
/* For the backward compatibility, we check if it's a |
| 145 |
"compliant key". */ |
| 146 |
|
| 147 |
mpi_sub (negative, ec->p, y); |
| 148 |
if (mpi_cmp (negative, y) < 0) /* p - y < p */ |
| 149 |
{ |
| 150 |
mpi_free (y); |
| 151 |
y = negative; |
| 152 |
mpi_sub (mpi_k, ec->n, mpi_k); |
| 153 |
buf = _gcry_mpi_get_buffer (mpi_k, 0, &len, NULL); |
| 154 |
memset (seckey, 0, nbytes - len); |
| 155 |
memcpy (seckey + nbytes - len, buf, len); |
| 156 |
xfree (buf); |
| 157 |
} |
| 158 |
else /* p - y >= p */ |
| 159 |
mpi_free (negative); |
| 160 |
|
| 161 |
buf = _gcry_ecc_ec2os_buf (x, y, ec->p, &len); |
| 162 |
if (!buf) |
| 163 |
{ |
| 164 |
err = gpg_err_code_from_syserror (); |
| 165 |
mpi_free (y); |
| 166 |
} |
| 167 |
else |
| 168 |
{ |
| 169 |
if (len != 1 + 2*nbytes) |
| 170 |
{ |
| 171 |
err = GPG_ERR_INV_ARG; |
| 172 |
} |
| 173 |
else |
| 174 |
{ |
| 175 |
/* (x,y) in SEC1 point encoding. */ |
| 176 |
memcpy (pubkey, buf, len); |
| 177 |
} |
| 178 |
xfree (buf); |
| 179 |
mpi_free (y); |
| 180 |
} |
| 181 |
} |
| 182 |
else /* MPI_EC_MONTGOMERY */ |
| 183 |
{ |
| 184 |
_gcry_mpi_ec_get_affine (x, NULL, &Q, ec); |
| 185 |
|
| 186 |
buf = _gcry_mpi_get_buffer (x, nbytes, &len, NULL); |
| 187 |
if (!buf) |
| 188 |
err = gpg_err_code_from_syserror (); |
| 189 |
else |
| 190 |
{ |
| 191 |
memcpy (pubkey, buf, nbytes); |
| 192 |
xfree (buf); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
mpi_free (x); |
| 197 |
point_free (&Q); |
| 198 |
mpi_free (mpi_k); |
| 199 |
_gcry_mpi_ec_free (ec); |
| 200 |
return err; |
| 201 |
} |
| 202 |
|
| 203 |
gpg_err_code_t |
| 204 |
_gcry_ecc_curve_mul_point (const char *curve, int enable_mont_check, |
| 205 |
unsigned char *result, size_t result_len, |
| 206 |
const unsigned char *scalar, size_t scalar_len, |
| 207 |
const unsigned char *point, size_t point_len) |
| 208 |
{ |
| 209 |
unsigned int nbits; |
| 210 |
unsigned int nbytes; |
| 211 |
gpg_err_code_t err; |
| 212 |
gcry_mpi_t mpi_k = NULL; |
| 213 |
mpi_ec_t ec = NULL; |
| 214 |
mpi_point_struct Q = { NULL, NULL, NULL }; |
| 215 |
gcry_mpi_t x = NULL; |
| 216 |
unsigned int len; |
| 217 |
unsigned char *buf; |
| 218 |
|
| 219 |
err = prepare_ec (&ec, curve); |
| 220 |
if (err) |
| 221 |
return err; |
| 222 |
|
| 223 |
nbits = ec->nbits; |
| 224 |
nbytes = (nbits + 7)/8; |
| 225 |
|
| 226 |
if (ec->model == MPI_EC_WEIERSTRASS) |
| 227 |
{ |
| 228 |
if (scalar_len != nbytes |
| 229 |
|| result_len != 1 + 2*nbytes |
| 230 |
|| point_len != 1 + 2*nbytes) |
| 231 |
{ |
| 232 |
err = GPG_ERR_INV_ARG; |
| 233 |
goto leave; |
| 234 |
} |
| 235 |
|
| 236 |
mpi_k = mpi_new (nbytes*8); |
| 237 |
_gcry_mpi_set_buffer (mpi_k, scalar, nbytes, 0); |
| 238 |
} |
| 239 |
else if (ec->model == MPI_EC_MONTGOMERY) |
| 240 |
{ |
| 241 |
if (scalar_len != nbytes |
| 242 |
|| result_len != nbytes |
| 243 |
|| point_len != nbytes) |
| 244 |
{ |
| 245 |
err = GPG_ERR_INV_ARG; |
| 246 |
goto leave; |
| 247 |
} |
| 248 |
|
| 249 |
mpi_k = _gcry_mpi_set_opaque_copy (NULL, scalar, nbytes*8); |
| 250 |
} |
| 251 |
else |
| 252 |
{ |
| 253 |
err = GPG_ERR_UNKNOWN_CURVE; |
| 254 |
goto leave; |
| 255 |
} |
| 256 |
|
| 257 |
point_init (&Q, ec->nbits); |
| 258 |
|
| 259 |
if (point) |
| 260 |
{ |
| 261 |
gcry_mpi_t mpi_u = _gcry_mpi_set_opaque_copy (NULL, point, point_len*8); |
| 262 |
mpi_point_struct P; |
| 263 |
|
| 264 |
point_init (&P, ec->nbits); |
| 265 |
if (ec->model == MPI_EC_WEIERSTRASS) |
| 266 |
{ |
| 267 |
err = _gcry_ecc_sec_decodepoint (mpi_u, ec, &P); |
| 268 |
if (err) |
| 269 |
{ |
| 270 |
point_free (&P); |
| 271 |
mpi_free (mpi_u); |
| 272 |
goto leave; |
| 273 |
} |
| 274 |
else if (!_gcry_mpi_ec_curve_point (&P, ec)) |
| 275 |
{ |
| 276 |
err = GPG_ERR_INV_DATA; |
| 277 |
point_free (&P); |
| 278 |
mpi_free (mpi_u); |
| 279 |
goto leave; |
| 280 |
} |
| 281 |
} |
| 282 |
else /* MPI_EC_MONTGOMERY */ |
| 283 |
{ |
| 284 |
err = _gcry_ecc_mont_decodepoint (mpi_u, ec, &P); |
| 285 |
if (err) |
| 286 |
{ |
| 287 |
point_free (&P); |
| 288 |
mpi_free (mpi_u); |
| 289 |
goto leave; |
| 290 |
} |
| 291 |
/* See comments in ecc.c. While our implementation has |
| 292 |
improved to be constant-time, we keep this check to be |
| 293 |
conservative. */ |
| 294 |
if (_gcry_mpi_ec_bad_point (&P, ec) && enable_mont_check) |
| 295 |
{ |
| 296 |
err = GPG_ERR_INV_DATA; |
| 297 |
point_free (&P); |
| 298 |
mpi_free (mpi_u); |
| 299 |
goto leave; |
| 300 |
} |
| 301 |
} |
| 302 |
_gcry_mpi_ec_mul_point (&Q, mpi_k, &P, ec); |
| 303 |
point_free (&P); |
| 304 |
mpi_free (mpi_u); |
| 305 |
} |
| 306 |
else |
| 307 |
_gcry_mpi_ec_mul_point (&Q, mpi_k, ec->G, ec); |
| 308 |
|
| 309 |
x = mpi_new (nbits); |
| 310 |
if (ec->model == MPI_EC_WEIERSTRASS) |
| 311 |
{ |
| 312 |
gcry_mpi_t y = mpi_new (nbits); |
| 313 |
|
| 314 |
if (_gcry_mpi_ec_get_affine (x, y, &Q, ec)) |
| 315 |
{ |
| 316 |
err = GPG_ERR_INV_DATA; |
| 317 |
mpi_free (y); |
| 318 |
goto leave; |
| 319 |
} |
| 320 |
|
| 321 |
buf = _gcry_ecc_ec2os_buf (x, y, ec->p, &len); |
| 322 |
if (!buf) |
| 323 |
{ |
| 324 |
err = gpg_err_code_from_syserror (); |
| 325 |
mpi_free (y); |
| 326 |
} |
| 327 |
else |
| 328 |
{ |
| 329 |
if (len != 1 + 2*nbytes) |
| 330 |
{ |
| 331 |
err = GPG_ERR_INV_ARG; |
| 332 |
} |
| 333 |
else |
| 334 |
{ |
| 335 |
/* (x,y) in SEC1 point encoding. */ |
| 336 |
memcpy (result, buf, len); |
| 337 |
} |
| 338 |
xfree (buf); |
| 339 |
mpi_free (y); |
| 340 |
} |
| 341 |
} |
| 342 |
else /* MPI_EC_MONTGOMERY */ |
| 343 |
{ |
| 344 |
if (_gcry_mpi_ec_get_affine (x, NULL, &Q, ec) && enable_mont_check) |
| 345 |
{ |
| 346 |
/* |
| 347 |
* Input validation with _gcry_mpi_ec_bad_point (above) |
| 348 |
* could be removed, when we are sure (no leak from side |
| 349 |
* channel). This output check should be kept for our usage |
| 350 |
* of GnuPG. See the comments in ecc.c for X25519/X448. |
| 351 |
*/ |
| 352 |
err = GPG_ERR_INV_DATA; |
| 353 |
goto leave; |
| 354 |
} |
| 355 |
buf = _gcry_mpi_get_buffer (x, nbytes, &len, NULL); |
| 356 |
if (!buf) |
| 357 |
err = gpg_err_code_from_syserror (); |
| 358 |
else |
| 359 |
{ |
| 360 |
if (len != nbytes) |
| 361 |
err = GPG_ERR_INV_ARG; |
| 362 |
else |
| 363 |
{ |
| 364 |
/* x in little endian. */ |
| 365 |
memcpy (result, buf, nbytes); |
| 366 |
} |
| 367 |
xfree (buf); |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
leave: |
| 372 |
mpi_free (x); |
| 373 |
point_free (&Q); |
| 374 |
mpi_free (mpi_k); |
| 375 |
_gcry_mpi_ec_free (ec); |
| 376 |
return err; |
| 377 |
} |
| 378 |
|
| 379 |
gpg_err_code_t |
| 380 |
_gcry_ecc_mul_point (int curveid, unsigned char *result, |
| 381 |
const unsigned char *scalar, const unsigned char *point) |
| 382 |
{ |
| 383 |
const char *curve; |
| 384 |
size_t pubkey_len, seckey_len; |
| 385 |
|
| 386 |
if (curveid == GCRY_ECC_CURVE25519) |
| 387 |
{ |
| 388 |
curve = "Curve25519"; |
| 389 |
pubkey_len = seckey_len = 32; |
| 390 |
} |
| 391 |
else if (curveid == GCRY_ECC_CURVE448) |
| 392 |
{ |
| 393 |
curve = "X448"; |
| 394 |
pubkey_len = seckey_len = 56; |
| 395 |
} |
| 396 |
else |
| 397 |
return gpg_error (GPG_ERR_UNKNOWN_CURVE); |
| 398 |
|
| 399 |
return _gcry_ecc_curve_mul_point (curve, 0, |
| 400 |
result, pubkey_len, |
| 401 |
scalar, seckey_len, |
| 402 |
point, pubkey_len); |
| 403 |
} |