| 1 |
/* ecc-eddsa.c - Elliptic Curve EdDSA signatures |
| 2 |
* Copyright (C) 2013, 2014 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 |
| 17 |
* License along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <config.h> |
| 21 |
#include <stdio.h> |
| 22 |
#include <stdlib.h> |
| 23 |
#include <string.h> |
| 24 |
#include <errno.h> |
| 25 |
|
| 26 |
#include "g10lib.h" |
| 27 |
#include "mpi.h" |
| 28 |
#include "cipher.h" |
| 29 |
#include "context.h" |
| 30 |
#include "ec-context.h" |
| 31 |
#include "ecc-common.h" |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
void |
| 36 |
reverse_buffer (unsigned char *buffer, unsigned int length) |
| 37 |
{ |
| 38 |
unsigned int tmp, i; |
| 39 |
|
| 40 |
for (i=0; i < length/2; i++) |
| 41 |
{ |
| 42 |
tmp = buffer[i]; |
| 43 |
buffer[i] = buffer[length-1-i]; |
| 44 |
buffer[length-1-i] = tmp; |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
/* Helper to scan a hex string. */ |
| 50 |
static gcry_mpi_t |
| 51 |
scanval (const char *string) |
| 52 |
{ |
| 53 |
gpg_err_code_t rc; |
| 54 |
gcry_mpi_t val; |
| 55 |
|
| 56 |
rc = _gcry_mpi_scan (&val, GCRYMPI_FMT_HEX, string, 0, NULL); |
| 57 |
if (rc) |
| 58 |
log_fatal ("scanning ECC parameter failed: %s\n", gpg_strerror (rc)); |
| 59 |
return val; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
/* Encode MPI using the EdDSA scheme. MINLEN specifies the required |
| 65 |
length of the buffer in bytes. On success 0 is returned an a |
| 66 |
malloced buffer with the encoded point is stored at R_BUFFER; the |
| 67 |
length of this buffer is stored at R_BUFLEN. */ |
| 68 |
static gpg_err_code_t |
| 69 |
eddsa_encodempi (gcry_mpi_t mpi, unsigned int nbits, |
| 70 |
unsigned char **r_buffer, unsigned int *r_buflen) |
| 71 |
{ |
| 72 |
unsigned char *rawmpi; |
| 73 |
unsigned int rawmpilen; |
| 74 |
unsigned int minlen = (nbits%8) == 0 ? (nbits/8 + 1): (nbits+7)/8; |
| 75 |
|
| 76 |
rawmpi = _gcry_mpi_get_buffer (mpi, minlen, &rawmpilen, NULL); |
| 77 |
if (!rawmpi) |
| 78 |
return gpg_err_code_from_syserror (); |
| 79 |
|
| 80 |
*r_buffer = rawmpi; |
| 81 |
*r_buflen = rawmpilen; |
| 82 |
return 0; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
/* Encode (X,Y) using the EdDSA scheme. NBITS is the number of bits |
| 87 |
of the field of the curve. If WITH_PREFIX is set the returned |
| 88 |
buffer is prefixed with a 0x40 byte. On success 0 is returned and |
| 89 |
a malloced buffer with the encoded point is stored at R_BUFFER; the |
| 90 |
length of this buffer is stored at R_BUFLEN. */ |
| 91 |
static gpg_err_code_t |
| 92 |
eddsa_encode_x_y (gcry_mpi_t x, gcry_mpi_t y, unsigned int nbits, |
| 93 |
int with_prefix, |
| 94 |
unsigned char **r_buffer, unsigned int *r_buflen) |
| 95 |
{ |
| 96 |
unsigned char *rawmpi; |
| 97 |
unsigned int rawmpilen; |
| 98 |
int off = with_prefix? 1:0; |
| 99 |
unsigned int minlen = (nbits%8) == 0 ? (nbits/8 + 1): (nbits+7)/8; |
| 100 |
|
| 101 |
rawmpi = _gcry_mpi_get_buffer_extra (y, minlen, off?-1:0, &rawmpilen, NULL); |
| 102 |
if (!rawmpi) |
| 103 |
return gpg_err_code_from_syserror (); |
| 104 |
if (mpi_test_bit (x, 0) && rawmpilen) |
| 105 |
rawmpi[off + rawmpilen - 1] |= 0x80; /* Set sign bit. */ |
| 106 |
if (off) |
| 107 |
rawmpi[0] = 0x40; |
| 108 |
|
| 109 |
*r_buffer = rawmpi; |
| 110 |
*r_buflen = rawmpilen + off; |
| 111 |
return 0; |
| 112 |
} |
| 113 |
|
| 114 |
/* Encode POINT using the EdDSA scheme. X and Y are either scratch |
| 115 |
variables supplied by the caller or NULL. CTX is the usual |
| 116 |
context. If WITH_PREFIX is set the returned buffer is prefixed |
| 117 |
with a 0x40 byte. On success 0 is returned and a malloced buffer |
| 118 |
with the encoded point is stored at R_BUFFER; the length of this |
| 119 |
buffer is stored at R_BUFLEN. */ |
| 120 |
gpg_err_code_t |
| 121 |
_gcry_ecc_eddsa_encodepoint (mpi_point_t point, mpi_ec_t ec, |
| 122 |
gcry_mpi_t x_in, gcry_mpi_t y_in, |
| 123 |
int with_prefix, |
| 124 |
unsigned char **r_buffer, unsigned int *r_buflen) |
| 125 |
{ |
| 126 |
gpg_err_code_t rc; |
| 127 |
gcry_mpi_t x, y; |
| 128 |
|
| 129 |
x = x_in? x_in : mpi_new (ec->nbits); |
| 130 |
y = y_in? y_in : mpi_new (ec->nbits); |
| 131 |
|
| 132 |
if (_gcry_mpi_ec_get_affine (x, y, point, ec)) |
| 133 |
{ |
| 134 |
log_error ("eddsa_encodepoint: Failed to get affine coordinates\n"); |
| 135 |
rc = GPG_ERR_INTERNAL; |
| 136 |
} |
| 137 |
else |
| 138 |
rc = eddsa_encode_x_y (x, y, ec->nbits, with_prefix, r_buffer, r_buflen); |
| 139 |
|
| 140 |
if (!x_in) |
| 141 |
mpi_free (x); |
| 142 |
if (!y_in) |
| 143 |
mpi_free (y); |
| 144 |
return rc; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
/* Make sure that the opaque MPI VALUE is in compact EdDSA format. |
| 149 |
This function updates MPI if needed. */ |
| 150 |
gpg_err_code_t |
| 151 |
_gcry_ecc_eddsa_ensure_compact (gcry_mpi_t value, unsigned int nbits) |
| 152 |
{ |
| 153 |
gpg_err_code_t rc; |
| 154 |
const unsigned char *buf; |
| 155 |
unsigned int rawmpilen; |
| 156 |
gcry_mpi_t x, y; |
| 157 |
|
| 158 |
if (!mpi_is_opaque (value)) |
| 159 |
return GPG_ERR_INV_OBJ; |
| 160 |
buf = mpi_get_opaque (value, &rawmpilen); |
| 161 |
if (!buf) |
| 162 |
return GPG_ERR_INV_OBJ; |
| 163 |
rawmpilen = (rawmpilen + 7)/8; |
| 164 |
|
| 165 |
if (rawmpilen > 1 && (rawmpilen%2)) |
| 166 |
{ |
| 167 |
if (buf[0] == 0x04) |
| 168 |
{ |
| 169 |
unsigned char *enc = NULL; |
| 170 |
unsigned int enclen = 0; |
| 171 |
|
| 172 |
/* Buffer is in SEC1 uncompressed format. Extract y and |
| 173 |
compress. */ |
| 174 |
rc = _gcry_mpi_scan (&x, GCRYMPI_FMT_USG, |
| 175 |
buf+1, (rawmpilen-1)/2, NULL); |
| 176 |
if (rc) |
| 177 |
return rc; |
| 178 |
rc = _gcry_mpi_scan (&y, GCRYMPI_FMT_USG, |
| 179 |
buf+1+(rawmpilen-1)/2, (rawmpilen-1)/2, NULL); |
| 180 |
if (rc) |
| 181 |
{ |
| 182 |
mpi_free (x); |
| 183 |
return rc; |
| 184 |
} |
| 185 |
|
| 186 |
rc = eddsa_encode_x_y (x, y, nbits, 0, &enc, &enclen); |
| 187 |
mpi_free (x); |
| 188 |
mpi_free (y); |
| 189 |
if (rc) |
| 190 |
return rc; |
| 191 |
|
| 192 |
mpi_set_opaque (value, enc, 8*enclen); |
| 193 |
} |
| 194 |
else if (buf[0] == 0x40) |
| 195 |
{ |
| 196 |
/* Buffer is compressed but with our SEC1 alike compression |
| 197 |
indicator. Remove that byte. FIXME: We should write and |
| 198 |
use a function to manipulate an opaque MPI in place. */ |
| 199 |
if (!_gcry_mpi_set_opaque_copy (value, buf + 1, (rawmpilen - 1)*8)) |
| 200 |
return gpg_err_code_from_syserror (); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
return 0; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
static gpg_err_code_t |
| 209 |
ecc_ed448_recover_x (gcry_mpi_t x, gcry_mpi_t y, int x_0, mpi_ec_t ec) |
| 210 |
{ |
| 211 |
gpg_err_code_t rc = 0; |
| 212 |
gcry_mpi_t u, v, u3, v3, t; |
| 213 |
static gcry_mpi_t p34; /* Hard coded (P-3)/4 */ |
| 214 |
|
| 215 |
if (mpi_cmp (y, ec->p) >= 0) |
| 216 |
rc = GPG_ERR_INV_OBJ; |
| 217 |
|
| 218 |
if (!p34) |
| 219 |
p34 = scanval ("3FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" |
| 220 |
"BFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"); |
| 221 |
|
| 222 |
u = mpi_new (ec->nbits); |
| 223 |
v = mpi_new (ec->nbits); |
| 224 |
u3 = mpi_new (ec->nbits); |
| 225 |
v3 = mpi_new (ec->nbits); |
| 226 |
t = mpi_new (ec->nbits); |
| 227 |
|
| 228 |
/* Compute u and v */ |
| 229 |
/* u = y^2 */ |
| 230 |
mpi_mulm (u, y, y, ec->p); |
| 231 |
/* v = b*y^2 */ |
| 232 |
mpi_mulm (v, ec->b, u, ec->p); |
| 233 |
/* u = y^2-1 */ |
| 234 |
mpi_sub_ui (u, u, 1); |
| 235 |
/* v = b*y^2-1 */ |
| 236 |
mpi_sub_ui (v, v, 1); |
| 237 |
|
| 238 |
/* Compute sqrt(u/v) */ |
| 239 |
/* u3 = u^3 */ |
| 240 |
mpi_powm (u3, u, mpi_const (MPI_C_THREE), ec->p); |
| 241 |
mpi_powm (v3, v, mpi_const (MPI_C_THREE), ec->p); |
| 242 |
/* t = u^4 * u * v3 = u^5 * v^3 */ |
| 243 |
mpi_powm (t, u, mpi_const (MPI_C_FOUR), ec->p); |
| 244 |
mpi_mulm (t, t, u, ec->p); |
| 245 |
mpi_mulm (t, t, v3, ec->p); |
| 246 |
/* t = t^((p-3)/4) = (u^5 * v^3)^((p-3)/4) */ |
| 247 |
mpi_powm (t, t, p34, ec->p); |
| 248 |
/* x = t * u^3 * v = (u^3 * v) * (u^5 * v^3)^((p-3)/4) */ |
| 249 |
mpi_mulm (t, t, u3, ec->p); |
| 250 |
mpi_mulm (x, t, v, ec->p); |
| 251 |
|
| 252 |
/* t = v * x^2 */ |
| 253 |
mpi_mulm (t, x, x, ec->p); |
| 254 |
mpi_mulm (t, t, v, ec->p); |
| 255 |
|
| 256 |
if (mpi_cmp (t, u) != 0) |
| 257 |
rc = GPG_ERR_INV_OBJ; |
| 258 |
else |
| 259 |
{ |
| 260 |
if (!mpi_cmp_ui (x, 0) && x_0) |
| 261 |
rc = GPG_ERR_INV_OBJ; |
| 262 |
|
| 263 |
/* Choose the desired square root according to parity */ |
| 264 |
if (mpi_test_bit (x, 0) != !!x_0) |
| 265 |
mpi_sub (x, ec->p, x); |
| 266 |
} |
| 267 |
|
| 268 |
mpi_free (t); |
| 269 |
mpi_free (u3); |
| 270 |
mpi_free (v3); |
| 271 |
mpi_free (v); |
| 272 |
mpi_free (u); |
| 273 |
|
| 274 |
return rc; |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
/* Recover X from Y and SIGN (which actually is a parity bit). */ |
| 279 |
gpg_err_code_t |
| 280 |
_gcry_ecc_eddsa_recover_x (gcry_mpi_t x, gcry_mpi_t y, int sign, mpi_ec_t ec) |
| 281 |
{ |
| 282 |
gpg_err_code_t rc = 0; |
| 283 |
gcry_mpi_t u, v, v3, t; |
| 284 |
static gcry_mpi_t p58, seven; |
| 285 |
|
| 286 |
/* |
| 287 |
* This routine is actually curve specific. Now, only supports |
| 288 |
* Ed25519 and Ed448. |
| 289 |
*/ |
| 290 |
|
| 291 |
if (ec->dialect != ECC_DIALECT_ED25519) |
| 292 |
/* For now, it's only Ed448. */ |
| 293 |
return ecc_ed448_recover_x (x, y, sign, ec); |
| 294 |
|
| 295 |
/* It's Ed25519. */ |
| 296 |
|
| 297 |
if (!p58) |
| 298 |
p58 = scanval ("0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" |
| 299 |
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFD"); |
| 300 |
if (!seven) |
| 301 |
seven = mpi_set_ui (NULL, 7); |
| 302 |
|
| 303 |
u = mpi_new (ec->nbits); |
| 304 |
v = mpi_new (ec->nbits); |
| 305 |
v3 = mpi_new (ec->nbits); |
| 306 |
t = mpi_new (ec->nbits); |
| 307 |
|
| 308 |
/* Compute u and v */ |
| 309 |
/* u = y^2 */ |
| 310 |
mpi_mulm (u, y, y, ec->p); |
| 311 |
/* v = b*y^2 */ |
| 312 |
mpi_mulm (v, ec->b, u, ec->p); |
| 313 |
/* u = y^2-1 */ |
| 314 |
mpi_sub_ui (u, u, 1); |
| 315 |
/* v = b*y^2+1 */ |
| 316 |
mpi_add_ui (v, v, 1); |
| 317 |
|
| 318 |
/* Compute sqrt(u/v) */ |
| 319 |
/* v3 = v^3 */ |
| 320 |
mpi_powm (v3, v, mpi_const (MPI_C_THREE), ec->p); |
| 321 |
/* t = v3 * v3 * u * v = u * v^7 */ |
| 322 |
mpi_powm (t, v, seven, ec->p); |
| 323 |
mpi_mulm (t, t, u, ec->p); |
| 324 |
/* t = t^((p-5)/8) = (u * v^7)^((p-5)/8) */ |
| 325 |
mpi_powm (t, t, p58, ec->p); |
| 326 |
/* x = t * u * v^3 = (u * v^3) * (u * v^7)^((p-5)/8) */ |
| 327 |
mpi_mulm (t, t, u, ec->p); |
| 328 |
mpi_mulm (x, t, v3, ec->p); |
| 329 |
|
| 330 |
/* Adjust if needed. */ |
| 331 |
/* t = v * x^2 */ |
| 332 |
mpi_mulm (t, x, x, ec->p); |
| 333 |
mpi_mulm (t, t, v, ec->p); |
| 334 |
/* -t == u ? x = x * sqrt(-1) */ |
| 335 |
mpi_sub (t, ec->p, t); |
| 336 |
if (!mpi_cmp (t, u)) |
| 337 |
{ |
| 338 |
static gcry_mpi_t m1; /* Fixme: this is not thread-safe. */ |
| 339 |
if (!m1) |
| 340 |
m1 = scanval ("2B8324804FC1DF0B2B4D00993DFBD7A7" |
| 341 |
"2F431806AD2FE478C4EE1B274A0EA0B0"); |
| 342 |
mpi_mulm (x, x, m1, ec->p); |
| 343 |
/* t = v * x^2 */ |
| 344 |
mpi_mulm (t, x, x, ec->p); |
| 345 |
mpi_mulm (t, t, v, ec->p); |
| 346 |
/* -t == u ? x = x * sqrt(-1) */ |
| 347 |
mpi_sub (t, ec->p, t); |
| 348 |
if (!mpi_cmp (t, u)) |
| 349 |
rc = GPG_ERR_INV_OBJ; |
| 350 |
} |
| 351 |
|
| 352 |
/* Choose the desired square root according to parity */ |
| 353 |
if (mpi_test_bit (x, 0) != !!sign) |
| 354 |
mpi_sub (x, ec->p, x); |
| 355 |
|
| 356 |
mpi_free (t); |
| 357 |
mpi_free (v3); |
| 358 |
mpi_free (v); |
| 359 |
mpi_free (u); |
| 360 |
|
| 361 |
return rc; |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
/* Decode the EdDSA style encoded PK and set it into RESULT. CTX is |
| 366 |
the usual curve context. If R_ENCPK is not NULL, the encoded PK is |
| 367 |
stored at that address; this is a new copy to be released by the |
| 368 |
caller. In contrast to the supplied PK, this is not an MPI and |
| 369 |
thus guaranteed to be properly padded. R_ENCPKLEN receives the |
| 370 |
length of that encoded key. */ |
| 371 |
gpg_err_code_t |
| 372 |
_gcry_ecc_eddsa_decodepoint (gcry_mpi_t pk, mpi_ec_t ctx, mpi_point_t result, |
| 373 |
unsigned char **r_encpk, unsigned int *r_encpklen) |
| 374 |
{ |
| 375 |
gpg_err_code_t rc; |
| 376 |
unsigned char *rawmpi; |
| 377 |
unsigned int rawmpilen; |
| 378 |
int sign; |
| 379 |
|
| 380 |
if (mpi_is_opaque (pk)) |
| 381 |
{ |
| 382 |
const unsigned char *buf; |
| 383 |
unsigned int len; |
| 384 |
|
| 385 |
len = (ctx->nbits%8) == 0 ? (ctx->nbits/8 + 1): (ctx->nbits+7)/8; |
| 386 |
|
| 387 |
buf = mpi_get_opaque (pk, &rawmpilen); |
| 388 |
if (!buf) |
| 389 |
return GPG_ERR_INV_OBJ; |
| 390 |
rawmpilen = (rawmpilen + 7)/8; |
| 391 |
|
| 392 |
if (!(rawmpilen == len |
| 393 |
|| rawmpilen == len + 1 |
| 394 |
|| rawmpilen == len * 2 + 1)) |
| 395 |
return GPG_ERR_INV_OBJ; |
| 396 |
|
| 397 |
/* Handle compression prefixes. The size of the buffer will be |
| 398 |
odd in this case. */ |
| 399 |
if (rawmpilen > 1 && (rawmpilen == len + 1 || rawmpilen == len * 2 + 1)) |
| 400 |
{ |
| 401 |
/* First check whether the public key has been given in |
| 402 |
standard uncompressed format (SEC1). No need to recover |
| 403 |
x in this case. */ |
| 404 |
if (buf[0] == 0x04) |
| 405 |
{ |
| 406 |
gcry_mpi_t x, y; |
| 407 |
|
| 408 |
rc = _gcry_mpi_scan (&x, GCRYMPI_FMT_USG, |
| 409 |
buf+1, (rawmpilen-1)/2, NULL); |
| 410 |
if (rc) |
| 411 |
return rc; |
| 412 |
rc = _gcry_mpi_scan (&y, GCRYMPI_FMT_USG, |
| 413 |
buf+1+(rawmpilen-1)/2, (rawmpilen-1)/2,NULL); |
| 414 |
if (rc) |
| 415 |
{ |
| 416 |
mpi_free (x); |
| 417 |
return rc; |
| 418 |
} |
| 419 |
|
| 420 |
if (r_encpk) |
| 421 |
{ |
| 422 |
rc = eddsa_encode_x_y (x, y, ctx->nbits, 0, |
| 423 |
r_encpk, r_encpklen); |
| 424 |
if (rc) |
| 425 |
{ |
| 426 |
mpi_free (x); |
| 427 |
mpi_free (y); |
| 428 |
return rc; |
| 429 |
} |
| 430 |
} |
| 431 |
mpi_snatch (result->x, x); |
| 432 |
mpi_snatch (result->y, y); |
| 433 |
mpi_set_ui (result->z, 1); |
| 434 |
return 0; |
| 435 |
} |
| 436 |
|
| 437 |
/* Check whether the public key has been prefixed with a 0x40 |
| 438 |
byte to explicitly indicate compressed format using a SEC1 |
| 439 |
alike prefix byte. This is a Libgcrypt extension. */ |
| 440 |
if (buf[0] == 0x40) |
| 441 |
{ |
| 442 |
rawmpilen--; |
| 443 |
buf++; |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
/* EdDSA compressed point. */ |
| 448 |
rawmpi = xtrymalloc (rawmpilen); |
| 449 |
if (!rawmpi) |
| 450 |
return gpg_err_code_from_syserror (); |
| 451 |
memcpy (rawmpi, buf, rawmpilen); |
| 452 |
reverse_buffer (rawmpi, rawmpilen); |
| 453 |
} |
| 454 |
else |
| 455 |
{ |
| 456 |
/* Note: Without using an opaque MPI it is not reliable possible |
| 457 |
to find out whether the public key has been given in |
| 458 |
uncompressed format. Thus we expect native EdDSA format. */ |
| 459 |
rawmpi = _gcry_mpi_get_buffer (pk, (ctx->nbits+7)/8, &rawmpilen, NULL); |
| 460 |
if (!rawmpi) |
| 461 |
return gpg_err_code_from_syserror (); |
| 462 |
} |
| 463 |
|
| 464 |
if (rawmpilen) |
| 465 |
{ |
| 466 |
sign = !!(rawmpi[0] & 0x80); |
| 467 |
rawmpi[0] &= 0x7f; |
| 468 |
} |
| 469 |
else |
| 470 |
sign = 0; |
| 471 |
_gcry_mpi_set_buffer (result->y, rawmpi, rawmpilen, 0); |
| 472 |
if (r_encpk) |
| 473 |
{ |
| 474 |
/* Revert to little endian. */ |
| 475 |
if (sign && rawmpilen) |
| 476 |
rawmpi[0] |= 0x80; |
| 477 |
reverse_buffer (rawmpi, rawmpilen); |
| 478 |
*r_encpk = rawmpi; |
| 479 |
if (r_encpklen) |
| 480 |
*r_encpklen = rawmpilen; |
| 481 |
} |
| 482 |
else |
| 483 |
xfree (rawmpi); |
| 484 |
|
| 485 |
rc = _gcry_ecc_eddsa_recover_x (result->x, result->y, sign, ctx); |
| 486 |
mpi_set_ui (result->z, 1); |
| 487 |
|
| 488 |
return rc; |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
/* Compute the A value as used by EdDSA. The caller needs to provide |
| 493 |
the context EC and the actual secret D as an MPI. The function |
| 494 |
returns a newly allocated 64 byte buffer at r_digest; the first 32 |
| 495 |
bytes represent the A value. NULL is returned on error and NULL |
| 496 |
stored at R_DIGEST. */ |
| 497 |
gpg_err_code_t |
| 498 |
_gcry_ecc_eddsa_compute_h_d (unsigned char **r_digest, mpi_ec_t ec) |
| 499 |
{ |
| 500 |
gpg_err_code_t rc; |
| 501 |
unsigned char *rawmpi = NULL; |
| 502 |
unsigned int rawmpilen; |
| 503 |
unsigned char *digest; |
| 504 |
int hashalgo, b, digestlen; |
| 505 |
gcry_buffer_t hvec[2]; |
| 506 |
|
| 507 |
*r_digest = NULL; |
| 508 |
|
| 509 |
b = (ec->nbits+7)/8; |
| 510 |
|
| 511 |
/* |
| 512 |
* Choice of hashalgo is curve specific. |
| 513 |
* For now, it's determine by the bit size of the field. |
| 514 |
*/ |
| 515 |
if (ec->nbits == 255) |
| 516 |
{ |
| 517 |
hashalgo = GCRY_MD_SHA512; |
| 518 |
digestlen = 64; |
| 519 |
} |
| 520 |
else if (ec->nbits == 448) |
| 521 |
{ |
| 522 |
b++; |
| 523 |
hashalgo = GCRY_MD_SHAKE256; |
| 524 |
digestlen = 2 * b; |
| 525 |
} |
| 526 |
else |
| 527 |
return GPG_ERR_NOT_IMPLEMENTED; |
| 528 |
|
| 529 |
/* Note that we clear DIGEST so we can use it as input to left pad |
| 530 |
the key with zeroes for hashing. */ |
| 531 |
digest = xtrycalloc_secure (2, b); |
| 532 |
if (!digest) |
| 533 |
return gpg_err_code_from_syserror (); |
| 534 |
|
| 535 |
rawmpi = _gcry_mpi_get_buffer (ec->d, 0, &rawmpilen, NULL); |
| 536 |
if (!rawmpi) |
| 537 |
{ |
| 538 |
xfree (digest); |
| 539 |
return gpg_err_code_from_syserror (); |
| 540 |
} |
| 541 |
|
| 542 |
memset (hvec, 0, sizeof hvec); |
| 543 |
|
| 544 |
hvec[0].data = digest; |
| 545 |
hvec[0].len = (hashalgo == GCRY_MD_SHA512 && b > rawmpilen) |
| 546 |
? b - rawmpilen : 0; |
| 547 |
hvec[1].data = rawmpi; |
| 548 |
hvec[1].len = rawmpilen; |
| 549 |
rc = _gcry_md_hash_buffers_extract (hashalgo, 0, digest, digestlen, hvec, 2); |
| 550 |
|
| 551 |
xfree (rawmpi); |
| 552 |
if (rc) |
| 553 |
{ |
| 554 |
xfree (digest); |
| 555 |
return rc; |
| 556 |
} |
| 557 |
|
| 558 |
/* Compute the A value. */ |
| 559 |
reverse_buffer (digest, b); /* Only the first half of the hash. */ |
| 560 |
|
| 561 |
/* Field specific handling of clearing/setting bits. */ |
| 562 |
if (ec->nbits == 255) |
| 563 |
{ |
| 564 |
digest[0] = (digest[0] & 0x7f) | 0x40; |
| 565 |
digest[31] &= 0xf8; |
| 566 |
} |
| 567 |
else |
| 568 |
{ |
| 569 |
digest[0] = 0; |
| 570 |
digest[1] |= 0x80; |
| 571 |
digest[56] &= 0xfc; |
| 572 |
} |
| 573 |
|
| 574 |
*r_digest = digest; |
| 575 |
return 0; |
| 576 |
} |
| 577 |
|
| 578 |
|
| 579 |
/** |
| 580 |
* _gcry_ecc_eddsa_genkey - EdDSA version of the key generation. |
| 581 |
* |
| 582 |
* @ec: Elliptic curve computation context. |
| 583 |
* @flags: Flags controlling aspects of the creation. |
| 584 |
* |
| 585 |
* Return: An error code. |
| 586 |
* |
| 587 |
* The only @flags bit used by this function is %PUBKEY_FLAG_TRANSIENT |
| 588 |
* to use a faster RNG. |
| 589 |
*/ |
| 590 |
gpg_err_code_t |
| 591 |
_gcry_ecc_eddsa_genkey (mpi_ec_t ec, int flags) |
| 592 |
{ |
| 593 |
gpg_err_code_t rc; |
| 594 |
int b; |
| 595 |
gcry_mpi_t a; |
| 596 |
mpi_point_struct Q; |
| 597 |
gcry_random_level_t random_level; |
| 598 |
char *dbuf; |
| 599 |
size_t dlen; |
| 600 |
unsigned char *hash_d = NULL; |
| 601 |
|
| 602 |
if ((flags & PUBKEY_FLAG_TRANSIENT_KEY)) |
| 603 |
random_level = GCRY_STRONG_RANDOM; |
| 604 |
else |
| 605 |
random_level = GCRY_VERY_STRONG_RANDOM; |
| 606 |
|
| 607 |
b = (ec->nbits+7)/8; |
| 608 |
|
| 609 |
if (ec->nbits == 255) |
| 610 |
; |
| 611 |
else if (ec->nbits == 448) |
| 612 |
b++; |
| 613 |
else |
| 614 |
return GPG_ERR_NOT_IMPLEMENTED; |
| 615 |
|
| 616 |
dlen = b; |
| 617 |
|
| 618 |
a = mpi_snew (0); |
| 619 |
|
| 620 |
/* Generate a secret. */ |
| 621 |
dbuf = _gcry_random_bytes_secure (dlen, random_level); |
| 622 |
ec->d = _gcry_mpi_set_opaque (NULL, dbuf, dlen*8); |
| 623 |
rc = _gcry_ecc_eddsa_compute_h_d (&hash_d, ec); |
| 624 |
if (rc) |
| 625 |
goto leave; |
| 626 |
|
| 627 |
_gcry_mpi_set_buffer (a, hash_d, b, 0); |
| 628 |
xfree (hash_d); |
| 629 |
/* log_printmpi ("ecgen a", a); */ |
| 630 |
|
| 631 |
/* Compute Q. */ |
| 632 |
point_init (&Q, ec->nbits); |
| 633 |
_gcry_mpi_ec_mul_point (&Q, a, ec->G, ec); |
| 634 |
if (DBG_CIPHER) |
| 635 |
log_printpnt ("ecgen pk", &Q, ec); |
| 636 |
|
| 637 |
ec->Q = mpi_point_snatch_set (NULL, Q.x, Q.y, Q.z); |
| 638 |
Q.x = NULL; |
| 639 |
Q.y = NULL; |
| 640 |
Q.x = NULL; |
| 641 |
|
| 642 |
leave: |
| 643 |
_gcry_mpi_release (a); |
| 644 |
return rc; |
| 645 |
} |
| 646 |
|
| 647 |
|
| 648 |
/* Compute an EdDSA signature. See: |
| 649 |
* [ed25519] 23pp. (PDF) Daniel J. Bernstein, Niels Duif, Tanja |
| 650 |
* Lange, Peter Schwabe, Bo-Yin Yang. High-speed high-security |
| 651 |
* signatures. Journal of Cryptographic Engineering 2 (2012), 77-89. |
| 652 |
* Document ID: a1a62a2f76d23f65d622484ddd09caf8. |
| 653 |
* URL: http://cr.yp.to/papers.html#ed25519. Date: 2011.09.26. |
| 654 |
* |
| 655 |
* Despite that this function requires the specification of a hash |
| 656 |
* algorithm, we only support what has been specified by the paper. |
| 657 |
* This may change in the future. |
| 658 |
* |
| 659 |
* Return the signature struct (r,s) from the message hash. The caller |
| 660 |
* must have allocated R_R and S. |
| 661 |
*/ |
| 662 |
|
| 663 |
/* String to be used with Ed448 */ |
| 664 |
#define DOM25519 "SigEd25519 no Ed25519 collisions" |
| 665 |
#define DOM25519_LEN 32 |
| 666 |
#define DOM448 "SigEd448" |
| 667 |
#define DOM448_LEN 8 |
| 668 |
|
| 669 |
gpg_err_code_t |
| 670 |
_gcry_ecc_eddsa_sign (gcry_mpi_t input, mpi_ec_t ec, |
| 671 |
gcry_mpi_t r_r, gcry_mpi_t s, |
| 672 |
struct pk_encoding_ctx *ctx) |
| 673 |
{ |
| 674 |
int rc; |
| 675 |
unsigned int tmp; |
| 676 |
unsigned char *digest = NULL; |
| 677 |
const void *mbuf; |
| 678 |
size_t mlen; |
| 679 |
unsigned char *rawmpi = NULL; |
| 680 |
unsigned int rawmpilen = 0; |
| 681 |
unsigned char *encpk = NULL; /* Encoded public key. */ |
| 682 |
unsigned int encpklen = 0; |
| 683 |
mpi_point_struct I; /* Intermediate value. */ |
| 684 |
gcry_mpi_t a, x, y, r; |
| 685 |
const char *dom; |
| 686 |
int domlen, digestlen; |
| 687 |
int b, i; |
| 688 |
unsigned char x_olen[2]; |
| 689 |
unsigned char prehashed_msg[64]; |
| 690 |
gcry_buffer_t hvec[6]; |
| 691 |
gcry_buffer_t hvec2[1]; |
| 692 |
|
| 693 |
b = (ec->nbits+7)/8; |
| 694 |
|
| 695 |
if (ec->nbits == 255) |
| 696 |
{ |
| 697 |
dom = DOM25519; |
| 698 |
domlen = DOM25519_LEN; |
| 699 |
digestlen = 64; |
| 700 |
} |
| 701 |
else if (ec->nbits == 448) |
| 702 |
{ |
| 703 |
b++; |
| 704 |
dom = DOM448; |
| 705 |
domlen = DOM448_LEN; |
| 706 |
digestlen = 2 * b; |
| 707 |
} |
| 708 |
else |
| 709 |
return GPG_ERR_NOT_IMPLEMENTED; |
| 710 |
|
| 711 |
if (!mpi_is_opaque (input)) |
| 712 |
return GPG_ERR_INV_DATA; |
| 713 |
|
| 714 |
/* Initialize some helpers. */ |
| 715 |
point_init (&I, ec->nbits); |
| 716 |
a = mpi_snew (0); |
| 717 |
x = mpi_new (ec->nbits); |
| 718 |
y = mpi_new (ec->nbits); |
| 719 |
r = mpi_snew (ec->nbits); |
| 720 |
|
| 721 |
rc = _gcry_ecc_eddsa_compute_h_d (&digest, ec); |
| 722 |
if (rc) |
| 723 |
goto leave; |
| 724 |
_gcry_mpi_set_buffer (a, digest, b, 0); |
| 725 |
|
| 726 |
/* Compute the public key if it's not available (only secret part). */ |
| 727 |
if (ec->Q == NULL) |
| 728 |
{ |
| 729 |
mpi_point_struct Q; |
| 730 |
|
| 731 |
point_init (&Q, ec->nbits); |
| 732 |
_gcry_mpi_ec_mul_point (&Q, a, ec->G, ec); |
| 733 |
ec->Q = mpi_point_snatch_set (NULL, Q.x, Q.y, Q.z); |
| 734 |
} |
| 735 |
rc = _gcry_ecc_eddsa_encodepoint (ec->Q, ec, x, y, 0, &encpk, &encpklen); |
| 736 |
if (rc) |
| 737 |
goto leave; |
| 738 |
if (DBG_CIPHER) |
| 739 |
log_printhex (" e_pk", encpk, encpklen); |
| 740 |
|
| 741 |
/* Compute R. */ |
| 742 |
mbuf = mpi_get_opaque (input, &tmp); |
| 743 |
mlen = (tmp +7)/8; |
| 744 |
if (DBG_CIPHER) |
| 745 |
log_printhex (" m", mbuf, mlen); |
| 746 |
|
| 747 |
memset (hvec, 0, sizeof hvec); |
| 748 |
i = 0; |
| 749 |
|
| 750 |
if ((ctx->flags & PUBKEY_FLAG_PREHASH) || ctx->labellen || ec->nbits == 448) |
| 751 |
{ |
| 752 |
hvec[i].data = (void *)dom; |
| 753 |
hvec[i].len = domlen; |
| 754 |
i++; |
| 755 |
x_olen[0] = !!(ctx->flags & PUBKEY_FLAG_PREHASH); |
| 756 |
x_olen[1] = ctx->labellen; |
| 757 |
hvec[i].data = x_olen; |
| 758 |
hvec[i].len = 2; |
| 759 |
i++; |
| 760 |
if (ctx->labellen) |
| 761 |
{ |
| 762 |
hvec[i].data = ctx->label; |
| 763 |
hvec[i].len = ctx->labellen; |
| 764 |
i++; |
| 765 |
} |
| 766 |
} |
| 767 |
|
| 768 |
hvec[i].data = digest; |
| 769 |
hvec[i].off = b; |
| 770 |
hvec[i].len = b; |
| 771 |
i++; |
| 772 |
if ((ctx->flags & PUBKEY_FLAG_PREHASH)) |
| 773 |
{ |
| 774 |
memset (hvec2, 0, sizeof hvec2); |
| 775 |
|
| 776 |
hvec2[0].data = (char*)mbuf; |
| 777 |
hvec2[0].len = mlen; |
| 778 |
|
| 779 |
_gcry_md_hash_buffers_extract (ctx->hash_algo, 0, prehashed_msg, 64, |
| 780 |
hvec2, 1); |
| 781 |
hvec[i].data = (char*)prehashed_msg; |
| 782 |
hvec[i].len = 64; |
| 783 |
} |
| 784 |
else |
| 785 |
{ |
| 786 |
hvec[i].data = (char*)mbuf; |
| 787 |
hvec[i].len = mlen; |
| 788 |
} |
| 789 |
i++; |
| 790 |
|
| 791 |
rc = _gcry_md_hash_buffers_extract (ctx->hash_algo, 0, digest, digestlen, |
| 792 |
hvec, i); |
| 793 |
if (rc) |
| 794 |
goto leave; |
| 795 |
reverse_buffer (digest, digestlen); |
| 796 |
if (DBG_CIPHER) |
| 797 |
log_printhex (" r", digest, digestlen); |
| 798 |
_gcry_mpi_set_buffer (r, digest, digestlen, 0); |
| 799 |
mpi_mod (r, r, ec->n); |
| 800 |
_gcry_mpi_ec_mul_point (&I, r, ec->G, ec); |
| 801 |
if (DBG_CIPHER) |
| 802 |
log_printpnt (" r", &I, ec); |
| 803 |
|
| 804 |
/* Convert R into affine coordinates and apply encoding. */ |
| 805 |
rc = _gcry_ecc_eddsa_encodepoint (&I, ec, x, y, 0, &rawmpi, &rawmpilen); |
| 806 |
if (rc) |
| 807 |
goto leave; |
| 808 |
if (DBG_CIPHER) |
| 809 |
log_printhex (" e_r", rawmpi, rawmpilen); |
| 810 |
|
| 811 |
memset (hvec, 0, sizeof hvec); |
| 812 |
i = 0; |
| 813 |
|
| 814 |
if ((ctx->flags & PUBKEY_FLAG_PREHASH) || ctx->labellen || ec->nbits == 448) |
| 815 |
{ |
| 816 |
hvec[i].data = (void *)dom; |
| 817 |
hvec[i].len = domlen; |
| 818 |
i++; |
| 819 |
x_olen[0] = !!(ctx->flags & PUBKEY_FLAG_PREHASH); |
| 820 |
x_olen[1] = ctx->labellen; |
| 821 |
hvec[i].data = x_olen; |
| 822 |
hvec[i].len = 2; |
| 823 |
i++; |
| 824 |
if (ctx->labellen) |
| 825 |
{ |
| 826 |
hvec[i].data = ctx->label; |
| 827 |
hvec[i].len = ctx->labellen; |
| 828 |
i++; |
| 829 |
} |
| 830 |
} |
| 831 |
|
| 832 |
/* S = r + a * H(dom2(F,C)+encodepoint(R)+encodepoint(pk)+m) mod n */ |
| 833 |
hvec[i].data = rawmpi; /* (this is R) */ |
| 834 |
hvec[i].len = rawmpilen; |
| 835 |
i++; |
| 836 |
hvec[i].data = encpk; |
| 837 |
hvec[i].len = encpklen; |
| 838 |
i++; |
| 839 |
if ((ctx->flags & PUBKEY_FLAG_PREHASH)) |
| 840 |
{ |
| 841 |
hvec[i].data = (char*)prehashed_msg; |
| 842 |
hvec[i].len = 64; |
| 843 |
} |
| 844 |
else |
| 845 |
{ |
| 846 |
hvec[i].data = (char*)mbuf; |
| 847 |
hvec[i].len = mlen; |
| 848 |
} |
| 849 |
i++; |
| 850 |
|
| 851 |
rc = _gcry_md_hash_buffers_extract (ctx->hash_algo, 0, digest, digestlen, |
| 852 |
hvec, i); |
| 853 |
if (rc) |
| 854 |
goto leave; |
| 855 |
|
| 856 |
/* No more need for RAWMPI thus we now transfer it to R_R. */ |
| 857 |
mpi_set_opaque (r_r, rawmpi, rawmpilen*8); |
| 858 |
rawmpi = NULL; |
| 859 |
|
| 860 |
reverse_buffer (digest, digestlen); |
| 861 |
if (DBG_CIPHER) |
| 862 |
log_printhex (" H(R+)", digest, digestlen); |
| 863 |
_gcry_mpi_set_buffer (s, digest, digestlen, 0); |
| 864 |
mpi_mulm (s, s, a, ec->n); |
| 865 |
mpi_addm (s, s, r, ec->n); |
| 866 |
rc = eddsa_encodempi (s, ec->nbits, &rawmpi, &rawmpilen); |
| 867 |
if (rc) |
| 868 |
goto leave; |
| 869 |
if (DBG_CIPHER) |
| 870 |
log_printhex (" e_s", rawmpi, rawmpilen); |
| 871 |
mpi_set_opaque (s, rawmpi, rawmpilen*8); |
| 872 |
rawmpi = NULL; |
| 873 |
|
| 874 |
rc = 0; |
| 875 |
|
| 876 |
leave: |
| 877 |
_gcry_mpi_release (a); |
| 878 |
_gcry_mpi_release (x); |
| 879 |
_gcry_mpi_release (y); |
| 880 |
_gcry_mpi_release (r); |
| 881 |
xfree (digest); |
| 882 |
point_free (&I); |
| 883 |
xfree (encpk); |
| 884 |
xfree (rawmpi); |
| 885 |
return rc; |
| 886 |
} |
| 887 |
|
| 888 |
|
| 889 |
/* Verify an EdDSA signature. See sign_eddsa for the reference. |
| 890 |
* Check if R_IN and S_IN verifies INPUT. |
| 891 |
*/ |
| 892 |
gpg_err_code_t |
| 893 |
_gcry_ecc_eddsa_verify (gcry_mpi_t input, mpi_ec_t ec, |
| 894 |
gcry_mpi_t r_in, gcry_mpi_t s_in, |
| 895 |
struct pk_encoding_ctx *ctx) |
| 896 |
{ |
| 897 |
int rc; |
| 898 |
int b; |
| 899 |
unsigned int tmp; |
| 900 |
unsigned char *encpk = NULL; /* Encoded public key. */ |
| 901 |
unsigned int encpklen = 0; |
| 902 |
const void *mbuf, *rbuf; |
| 903 |
unsigned char *tbuf = NULL; |
| 904 |
size_t mlen, rlen; |
| 905 |
unsigned int tlen; |
| 906 |
unsigned char digest[114]; |
| 907 |
gcry_mpi_t h, s; |
| 908 |
mpi_point_struct Ia, Ib; |
| 909 |
const char *dom; |
| 910 |
int domlen, digestlen; |
| 911 |
int i; |
| 912 |
unsigned char x_olen[2]; |
| 913 |
unsigned char prehashed_msg[64]; |
| 914 |
gcry_buffer_t hvec[6]; |
| 915 |
gcry_buffer_t hvec2[1]; |
| 916 |
|
| 917 |
if (!mpi_is_opaque (input) || !mpi_is_opaque (r_in) || !mpi_is_opaque (s_in)) |
| 918 |
return GPG_ERR_INV_DATA; |
| 919 |
|
| 920 |
b = (ec->nbits+7)/8; |
| 921 |
|
| 922 |
if (ec->nbits == 255) |
| 923 |
{ |
| 924 |
dom = DOM25519; |
| 925 |
domlen = DOM25519_LEN; |
| 926 |
digestlen = 64; |
| 927 |
} |
| 928 |
else if (ec->nbits == 448) |
| 929 |
{ |
| 930 |
b++; |
| 931 |
dom = DOM448; |
| 932 |
domlen = DOM448_LEN; |
| 933 |
digestlen = 2 * b; |
| 934 |
} |
| 935 |
else |
| 936 |
return GPG_ERR_NOT_IMPLEMENTED; |
| 937 |
|
| 938 |
point_init (&Ia, ec->nbits); |
| 939 |
point_init (&Ib, ec->nbits); |
| 940 |
h = mpi_new (0); |
| 941 |
s = mpi_new (0); |
| 942 |
|
| 943 |
/* Encode and check the public key. */ |
| 944 |
rc = _gcry_ecc_eddsa_encodepoint (ec->Q, ec, NULL, NULL, 0, |
| 945 |
&encpk, &encpklen); |
| 946 |
if (rc) |
| 947 |
goto leave; |
| 948 |
if (!_gcry_mpi_ec_curve_point (ec->Q, ec)) |
| 949 |
{ |
| 950 |
rc = GPG_ERR_BROKEN_PUBKEY; |
| 951 |
goto leave; |
| 952 |
} |
| 953 |
if (DBG_CIPHER) |
| 954 |
log_printhex (" e_pk", encpk, encpklen); |
| 955 |
if (encpklen != b) |
| 956 |
{ |
| 957 |
rc = GPG_ERR_INV_LENGTH; |
| 958 |
goto leave; |
| 959 |
} |
| 960 |
|
| 961 |
/* Convert the other input parameters. */ |
| 962 |
mbuf = mpi_get_opaque (input, &tmp); |
| 963 |
mlen = (tmp +7)/8; |
| 964 |
if (DBG_CIPHER) |
| 965 |
log_printhex (" m", mbuf, mlen); |
| 966 |
rbuf = mpi_get_opaque (r_in, &tmp); |
| 967 |
rlen = (tmp +7)/8; |
| 968 |
if (DBG_CIPHER) |
| 969 |
log_printhex (" r", rbuf, rlen); |
| 970 |
if (rlen != b) |
| 971 |
{ |
| 972 |
rc = GPG_ERR_INV_LENGTH; |
| 973 |
goto leave; |
| 974 |
} |
| 975 |
|
| 976 |
memset (hvec, 0, sizeof hvec); |
| 977 |
i = 0; |
| 978 |
|
| 979 |
/* h = H(dom2(F,C)+encodepoint(R)+encodepoint(pk)+m) */ |
| 980 |
if ((ctx->flags & PUBKEY_FLAG_PREHASH) || ctx->labellen || ec->nbits == 448) |
| 981 |
{ |
| 982 |
hvec[i].data = (void *)dom; |
| 983 |
hvec[i].len = domlen; |
| 984 |
i++; |
| 985 |
x_olen[0] = !!(ctx->flags & PUBKEY_FLAG_PREHASH); |
| 986 |
x_olen[1] = ctx->labellen; |
| 987 |
hvec[i].data = x_olen; |
| 988 |
hvec[i].len = 2; |
| 989 |
i++; |
| 990 |
if (ctx->labellen) |
| 991 |
{ |
| 992 |
hvec[i].data = ctx->label; |
| 993 |
hvec[i].len = ctx->labellen; |
| 994 |
i++; |
| 995 |
} |
| 996 |
} |
| 997 |
|
| 998 |
hvec[i].data = (char*)rbuf; |
| 999 |
hvec[i].len = rlen; |
| 1000 |
i++; |
| 1001 |
hvec[i].data = encpk; |
| 1002 |
hvec[i].len = encpklen; |
| 1003 |
i++; |
| 1004 |
if ((ctx->flags & PUBKEY_FLAG_PREHASH)) |
| 1005 |
{ |
| 1006 |
memset (hvec2, 0, sizeof hvec2); |
| 1007 |
|
| 1008 |
hvec2[0].data = (char*)mbuf; |
| 1009 |
hvec2[0].len = mlen; |
| 1010 |
|
| 1011 |
_gcry_md_hash_buffers_extract (ctx->hash_algo, 0, prehashed_msg, 64, |
| 1012 |
hvec2, 1); |
| 1013 |
hvec[i].data = (char*)prehashed_msg; |
| 1014 |
hvec[i].len = 64; |
| 1015 |
} |
| 1016 |
else |
| 1017 |
{ |
| 1018 |
hvec[i].data = (char*)mbuf; |
| 1019 |
hvec[i].len = mlen; |
| 1020 |
} |
| 1021 |
i++; |
| 1022 |
|
| 1023 |
rc = _gcry_md_hash_buffers_extract (ctx->hash_algo, 0, digest, digestlen, |
| 1024 |
hvec, i); |
| 1025 |
if (rc) |
| 1026 |
goto leave; |
| 1027 |
reverse_buffer (digest, digestlen); |
| 1028 |
if (DBG_CIPHER) |
| 1029 |
log_printhex (" H(R+)", digest, digestlen); |
| 1030 |
_gcry_mpi_set_buffer (h, digest, digestlen, 0); |
| 1031 |
|
| 1032 |
/* According to the paper the best way for verification is: |
| 1033 |
encodepoint(sG - h·Q) = encodepoint(r) |
| 1034 |
because we don't need to decode R. */ |
| 1035 |
{ |
| 1036 |
void *sbuf; |
| 1037 |
unsigned int slen; |
| 1038 |
|
| 1039 |
sbuf = _gcry_mpi_get_opaque_copy (s_in, &tmp); |
| 1040 |
slen = (tmp +7)/8; |
| 1041 |
reverse_buffer (sbuf, slen); |
| 1042 |
if (DBG_CIPHER) |
| 1043 |
log_printhex (" s", sbuf, slen); |
| 1044 |
_gcry_mpi_set_buffer (s, sbuf, slen, 0); |
| 1045 |
xfree (sbuf); |
| 1046 |
if (slen != b) |
| 1047 |
{ |
| 1048 |
rc = GPG_ERR_INV_LENGTH; |
| 1049 |
goto leave; |
| 1050 |
} |
| 1051 |
} |
| 1052 |
|
| 1053 |
_gcry_mpi_ec_mul_point (&Ia, s, ec->G, ec); |
| 1054 |
_gcry_mpi_ec_mul_point (&Ib, h, ec->Q, ec); |
| 1055 |
_gcry_mpi_sub (Ib.x, ec->p, Ib.x); |
| 1056 |
_gcry_mpi_ec_add_points (&Ia, &Ia, &Ib, ec); |
| 1057 |
rc = _gcry_ecc_eddsa_encodepoint (&Ia, ec, s, h, 0, &tbuf, &tlen); |
| 1058 |
if (rc) |
| 1059 |
goto leave; |
| 1060 |
if (tlen != rlen || memcmp (tbuf, rbuf, tlen)) |
| 1061 |
{ |
| 1062 |
rc = GPG_ERR_BAD_SIGNATURE; |
| 1063 |
goto leave; |
| 1064 |
} |
| 1065 |
|
| 1066 |
rc = 0; |
| 1067 |
|
| 1068 |
leave: |
| 1069 |
xfree (encpk); |
| 1070 |
xfree (tbuf); |
| 1071 |
_gcry_mpi_release (s); |
| 1072 |
_gcry_mpi_release (h); |
| 1073 |
point_free (&Ia); |
| 1074 |
point_free (&Ib); |
| 1075 |
return rc; |
| 1076 |
} |