| 1 |
/* rsa.c - RSA implementation |
| 2 |
* Copyright (C) 1997, 1998, 1999 by Werner Koch (dd9jn) |
| 3 |
* Copyright (C) 2000, 2001, 2002, 2003, 2008 Free Software Foundation, Inc. |
| 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 |
/* This code uses an algorithm protected by U.S. Patent #4,405,829 |
| 22 |
which expired on September 20, 2000. The patent holder placed that |
| 23 |
patent into the public domain on Sep 6th, 2000. |
| 24 |
*/ |
| 25 |
|
| 26 |
#include <config.h> |
| 27 |
#include <stdio.h> |
| 28 |
#include <stdlib.h> |
| 29 |
#include <string.h> |
| 30 |
#include <errno.h> |
| 31 |
|
| 32 |
#include "g10lib.h" |
| 33 |
#include "mpi.h" |
| 34 |
#include "cipher.h" |
| 35 |
#include "pubkey-internal.h" |
| 36 |
#include "const-time.h" |
| 37 |
|
| 38 |
|
| 39 |
typedef struct |
| 40 |
{ |
| 41 |
gcry_mpi_t n; /* modulus */ |
| 42 |
gcry_mpi_t e; /* exponent */ |
| 43 |
} RSA_public_key; |
| 44 |
|
| 45 |
|
| 46 |
typedef struct |
| 47 |
{ |
| 48 |
gcry_mpi_t n; /* public modulus */ |
| 49 |
gcry_mpi_t e; /* public exponent */ |
| 50 |
gcry_mpi_t d; /* exponent */ |
| 51 |
gcry_mpi_t p; /* prime p. */ |
| 52 |
gcry_mpi_t q; /* prime q. */ |
| 53 |
gcry_mpi_t u; /* inverse of p mod q. */ |
| 54 |
} RSA_secret_key; |
| 55 |
|
| 56 |
|
| 57 |
static const char *rsa_names[] = |
| 58 |
{ |
| 59 |
"rsa", |
| 60 |
"openpgp-rsa", |
| 61 |
"oid.1.2.840.113549.1.1.1", |
| 62 |
NULL, |
| 63 |
}; |
| 64 |
|
| 65 |
|
| 66 |
/* A sample 2048 bit RSA key used for the selftests. */ |
| 67 |
static const char sample_secret_key[] = |
| 68 |
" (private-key" |
| 69 |
" (rsa" |
| 70 |
" (n #009F56231A3D82E3E7D613D59D53E9AB921BEF9F08A782AED0B6E46ADBC853EC" |
| 71 |
" 7C71C422435A3CD8FA0DB9EFD55CD3295BADC4E8E2E2B94E15AE82866AB8ADE8" |
| 72 |
" 7E469FAE76DC3577DE87F1F419C4EB41123DFAF8D16922D5EDBAD6E9076D5A1C" |
| 73 |
" 958106F0AE5E2E9193C6B49124C64C2A241C4075D4AF16299EB87A6585BAE917" |
| 74 |
" DEF27FCDD165764D069BC18D16527B29DAAB549F7BBED4A7C6A842D203ED6613" |
| 75 |
" 6E2411744E432CD26D940132F25874483DCAEECDFD95744819CBCF1EA810681C" |
| 76 |
" 42907EBCB1C7EAFBE75C87EC32C5413EA10476545D3FC7B2ADB1B66B7F200918" |
| 77 |
" 664B0E5261C2895AA28B0DE321E921B3F877172CCCAB81F43EF98002916156F6CB#)" |
| 78 |
" (e #010001#)" |
| 79 |
" (d #07EF82500C403899934FE993AC5A36F14FF2DF38CF1EF315F205EE4C83EDAA19" |
| 80 |
" 8890FC23DE9AA933CAFB37B6A8A8DBA675411958337287310D3FF2F1DDC0CB93" |
| 81 |
" 7E70F57F75F833C021852B631D2B9A520E4431A03C5C3FCB5742DCD841D9FB12" |
| 82 |
" 771AA1620DCEC3F1583426066ED9DC3F7028C5B59202C88FDF20396E2FA0EC4F" |
| 83 |
" 5A22D9008F3043673931BC14A5046D6327398327900867E39CC61B2D1AFE2F48" |
| 84 |
" EC8E1E3861C68D257D7425F4E6F99ABD77D61F10CA100EFC14389071831B33DD" |
| 85 |
" 69CC8EABEF860D1DC2AAA84ABEAE5DFC91BC124DAF0F4C8EF5BBEA436751DE84" |
| 86 |
" 3A8063E827A024466F44C28614F93B0732A100D4A0D86D532FE1E22C7725E401#)" |
| 87 |
" (p #00C29D438F115825779631CD665A5739367F3E128ADC29766483A46CA80897E0" |
| 88 |
" 79B32881860B8F9A6A04C2614A904F6F2578DAE13EA67CD60AE3D0AA00A1FF9B" |
| 89 |
" 441485E44B2DC3D0B60260FBFE073B5AC72FAF67964DE15C8212C389D20DB9CF" |
| 90 |
" 54AF6AEF5C4196EAA56495DD30CF709F499D5AB30CA35E086C2A1589D6283F1783#)" |
| 91 |
" (q #00D1984135231CB243FE959C0CBEF551EDD986AD7BEDF71EDF447BE3DA27AF46" |
| 92 |
" 79C974A6FA69E4D52FE796650623DE70622862713932AA2FD9F2EC856EAEAA77" |
| 93 |
" 88B4EA6084DC81C902F014829B18EA8B2666EC41586818E0589E18876065F97E" |
| 94 |
" 8D22CE2DA53A05951EC132DCEF41E70A9C35F4ACC268FFAC2ADF54FA1DA110B919#)" |
| 95 |
" (u #67CF0FD7635205DD80FA814EE9E9C267C17376BF3209FB5D1BC42890D2822A04" |
| 96 |
" 479DAF4D5B6ED69D0F8D1AF94164D07F8CD52ECEFE880641FA0F41DDAB1785E4" |
| 97 |
" A37A32F997A516480B4CD4F6482B9466A1765093ED95023CA32D5EDC1E34CEE9" |
| 98 |
" AF595BC51FE43C4BF810FA225AF697FB473B83815966188A4312C048B885E3F7#)))"; |
| 99 |
|
| 100 |
/* A sample 2048 bit RSA key used for the selftests (public only). */ |
| 101 |
static const char sample_public_key[] = |
| 102 |
" (public-key" |
| 103 |
" (rsa" |
| 104 |
" (n #009F56231A3D82E3E7D613D59D53E9AB921BEF9F08A782AED0B6E46ADBC853EC" |
| 105 |
" 7C71C422435A3CD8FA0DB9EFD55CD3295BADC4E8E2E2B94E15AE82866AB8ADE8" |
| 106 |
" 7E469FAE76DC3577DE87F1F419C4EB41123DFAF8D16922D5EDBAD6E9076D5A1C" |
| 107 |
" 958106F0AE5E2E9193C6B49124C64C2A241C4075D4AF16299EB87A6585BAE917" |
| 108 |
" DEF27FCDD165764D069BC18D16527B29DAAB549F7BBED4A7C6A842D203ED6613" |
| 109 |
" 6E2411744E432CD26D940132F25874483DCAEECDFD95744819CBCF1EA810681C" |
| 110 |
" 42907EBCB1C7EAFBE75C87EC32C5413EA10476545D3FC7B2ADB1B66B7F200918" |
| 111 |
" 664B0E5261C2895AA28B0DE321E921B3F877172CCCAB81F43EF98002916156F6CB#)" |
| 112 |
" (e #010001#)))"; |
| 113 |
|
| 114 |
|
| 115 |
static int test_keys (RSA_secret_key *sk, unsigned nbits); |
| 116 |
static int check_secret_key (RSA_secret_key *sk); |
| 117 |
static void public (gcry_mpi_t output, gcry_mpi_t input, RSA_public_key *skey); |
| 118 |
static void secret (gcry_mpi_t output, gcry_mpi_t input, RSA_secret_key *skey); |
| 119 |
static unsigned int rsa_get_nbits (gcry_sexp_t parms); |
| 120 |
|
| 121 |
|
| 122 |
/* Check that a freshly generated key actually works. Returns 0 on success. */ |
| 123 |
static int |
| 124 |
test_keys (RSA_secret_key *sk, unsigned int nbits) |
| 125 |
{ |
| 126 |
int result = -1; /* Default to failure. */ |
| 127 |
RSA_public_key pk; |
| 128 |
gcry_mpi_t plaintext = mpi_new (nbits); |
| 129 |
gcry_mpi_t ciphertext = mpi_new (nbits); |
| 130 |
gcry_mpi_t decr_plaintext = mpi_new (nbits); |
| 131 |
gcry_mpi_t signature = mpi_new (nbits); |
| 132 |
|
| 133 |
/* Put the relevant parameters into a public key structure. */ |
| 134 |
pk.n = sk->n; |
| 135 |
pk.e = sk->e; |
| 136 |
|
| 137 |
/* Create a random plaintext. */ |
| 138 |
_gcry_mpi_randomize (plaintext, nbits, GCRY_WEAK_RANDOM); |
| 139 |
|
| 140 |
/* Encrypt using the public key. */ |
| 141 |
public (ciphertext, plaintext, &pk); |
| 142 |
|
| 143 |
/* Check that the cipher text does not match the plaintext. */ |
| 144 |
if (!mpi_cmp (ciphertext, plaintext)) |
| 145 |
goto leave; /* Ciphertext is identical to the plaintext. */ |
| 146 |
|
| 147 |
/* Decrypt using the secret key. */ |
| 148 |
secret (decr_plaintext, ciphertext, sk); |
| 149 |
|
| 150 |
/* Check that the decrypted plaintext matches the original plaintext. */ |
| 151 |
if (mpi_cmp (decr_plaintext, plaintext)) |
| 152 |
goto leave; /* Plaintext does not match. */ |
| 153 |
|
| 154 |
/* Create another random plaintext as data for signature checking. */ |
| 155 |
_gcry_mpi_randomize (plaintext, nbits, GCRY_WEAK_RANDOM); |
| 156 |
|
| 157 |
/* Use the RSA secret function to create a signature of the plaintext. */ |
| 158 |
secret (signature, plaintext, sk); |
| 159 |
|
| 160 |
/* Use the RSA public function to verify this signature. */ |
| 161 |
public (decr_plaintext, signature, &pk); |
| 162 |
if (mpi_cmp (decr_plaintext, plaintext)) |
| 163 |
goto leave; /* Signature does not match. */ |
| 164 |
|
| 165 |
/* Modify the signature and check that the signing fails. */ |
| 166 |
mpi_add_ui (signature, signature, 1); |
| 167 |
public (decr_plaintext, signature, &pk); |
| 168 |
if (!mpi_cmp (decr_plaintext, plaintext)) |
| 169 |
goto leave; /* Signature matches but should not. */ |
| 170 |
|
| 171 |
result = 0; /* All tests succeeded. */ |
| 172 |
|
| 173 |
leave: |
| 174 |
_gcry_mpi_release (signature); |
| 175 |
_gcry_mpi_release (decr_plaintext); |
| 176 |
_gcry_mpi_release (ciphertext); |
| 177 |
_gcry_mpi_release (plaintext); |
| 178 |
return result; |
| 179 |
} |
| 180 |
|
| 181 |
static int |
| 182 |
test_keys_fips (gcry_sexp_t skey) |
| 183 |
{ |
| 184 |
int result = -1; /* Default to failure. */ |
| 185 |
char plaintext[128]; |
| 186 |
gcry_sexp_t sig = NULL; |
| 187 |
const char *data_tmpl = "(data (flags pkcs1) (hash %s %b))"; |
| 188 |
gcry_md_hd_t hd = NULL; |
| 189 |
int ec; |
| 190 |
|
| 191 |
/* Create a random plaintext. */ |
| 192 |
_gcry_randomize (plaintext, sizeof plaintext, GCRY_WEAK_RANDOM); |
| 193 |
|
| 194 |
/* Open MD context and feed the random data in */ |
| 195 |
ec = _gcry_md_open (&hd, GCRY_MD_SHA256, 0); |
| 196 |
if (ec) |
| 197 |
goto leave; |
| 198 |
_gcry_md_write (hd, plaintext, sizeof(plaintext)); |
| 199 |
|
| 200 |
/* Use the RSA secret function to create a signature of the plaintext. */ |
| 201 |
ec = _gcry_pk_sign_md (&sig, data_tmpl, hd, skey, NULL); |
| 202 |
if (ec) |
| 203 |
goto leave; |
| 204 |
|
| 205 |
/* Use the RSA public function to verify this signature. */ |
| 206 |
ec = _gcry_pk_verify_md (sig, data_tmpl, hd, skey, NULL); |
| 207 |
if (ec) |
| 208 |
goto leave; |
| 209 |
|
| 210 |
/* Modify the data and check that the signing fails. */ |
| 211 |
_gcry_md_reset(hd); |
| 212 |
plaintext[sizeof plaintext / 2] ^= 1; |
| 213 |
_gcry_md_write (hd, plaintext, sizeof(plaintext)); |
| 214 |
ec = _gcry_pk_verify_md (sig, data_tmpl, hd, skey, NULL); |
| 215 |
if (ec != GPG_ERR_BAD_SIGNATURE) |
| 216 |
goto leave; /* Signature verification worked on modified data */ |
| 217 |
|
| 218 |
result = 0; /* All tests succeeded. */ |
| 219 |
leave: |
| 220 |
sexp_release (sig); |
| 221 |
_gcry_md_close (hd); |
| 222 |
return result; |
| 223 |
} |
| 224 |
|
| 225 |
/* Callback used by the prime generation to test whether the exponent |
| 226 |
is suitable. Returns 0 if the test has been passed. */ |
| 227 |
static int |
| 228 |
check_exponent (void *arg, gcry_mpi_t a) |
| 229 |
{ |
| 230 |
gcry_mpi_t e = arg; |
| 231 |
gcry_mpi_t tmp; |
| 232 |
int result; |
| 233 |
|
| 234 |
mpi_sub_ui (a, a, 1); |
| 235 |
tmp = _gcry_mpi_alloc_like (a); |
| 236 |
result = !mpi_gcd(tmp, e, a); /* GCD is not 1. */ |
| 237 |
_gcry_mpi_release (tmp); |
| 238 |
mpi_add_ui (a, a, 1); |
| 239 |
return result; |
| 240 |
} |
| 241 |
|
| 242 |
/**************** |
| 243 |
* Generate a key pair with a key of size NBITS. |
| 244 |
* USE_E = 0 let Libcgrypt decide what exponent to use. |
| 245 |
* = 1 request the use of a "secure" exponent; this is required by some |
| 246 |
* specification to be 65537. |
| 247 |
* > 2 Use this public exponent. If the given exponent |
| 248 |
* is not odd one is internally added to it. |
| 249 |
* TRANSIENT_KEY: If true, generate the primes using the standard RNG. |
| 250 |
* Returns: 2 structures filled with all needed values |
| 251 |
*/ |
| 252 |
static gpg_err_code_t |
| 253 |
generate_std (RSA_secret_key *sk, unsigned int nbits, unsigned long use_e, |
| 254 |
int transient_key) |
| 255 |
{ |
| 256 |
gcry_mpi_t p, q; /* the two primes */ |
| 257 |
gcry_mpi_t d; /* the private key */ |
| 258 |
gcry_mpi_t u; |
| 259 |
gcry_mpi_t t1, t2; |
| 260 |
gcry_mpi_t n; /* the public key */ |
| 261 |
gcry_mpi_t e; /* the exponent */ |
| 262 |
gcry_mpi_t phi; /* helper: (p-1)(q-1) */ |
| 263 |
gcry_mpi_t g; |
| 264 |
gcry_mpi_t f; |
| 265 |
gcry_random_level_t random_level; |
| 266 |
|
| 267 |
/* The random quality depends on the transient_key flag. */ |
| 268 |
random_level = transient_key ? GCRY_STRONG_RANDOM : GCRY_VERY_STRONG_RANDOM; |
| 269 |
|
| 270 |
/* Make sure that nbits is even so that we generate p, q of equal size. */ |
| 271 |
if ( (nbits&1) ) |
| 272 |
nbits++; |
| 273 |
|
| 274 |
if (use_e == 1) /* Alias for a secure value */ |
| 275 |
use_e = 65537; /* as demanded by Sphinx. */ |
| 276 |
|
| 277 |
/* Public exponent: |
| 278 |
In general we use 41 as this is quite fast and more secure than the |
| 279 |
commonly used 17. Benchmarking the RSA verify function |
| 280 |
with a 1024 bit key yields (2001-11-08): |
| 281 |
e=17 0.54 ms |
| 282 |
e=41 0.75 ms |
| 283 |
e=257 0.95 ms |
| 284 |
e=65537 1.80 ms |
| 285 |
*/ |
| 286 |
e = mpi_alloc( (32+BITS_PER_MPI_LIMB-1)/BITS_PER_MPI_LIMB ); |
| 287 |
if (!use_e) |
| 288 |
mpi_set_ui (e, 41); /* This is a reasonable secure and fast value */ |
| 289 |
else |
| 290 |
{ |
| 291 |
use_e |= 1; /* make sure this is odd */ |
| 292 |
mpi_set_ui (e, use_e); |
| 293 |
} |
| 294 |
|
| 295 |
n = mpi_new (nbits); |
| 296 |
|
| 297 |
p = q = NULL; |
| 298 |
do |
| 299 |
{ |
| 300 |
/* select two (very secret) primes */ |
| 301 |
if (p) |
| 302 |
_gcry_mpi_release (p); |
| 303 |
if (q) |
| 304 |
_gcry_mpi_release (q); |
| 305 |
if (use_e) |
| 306 |
{ /* Do an extra test to ensure that the given exponent is |
| 307 |
suitable. */ |
| 308 |
p = _gcry_generate_secret_prime (nbits/2, random_level, |
| 309 |
check_exponent, e); |
| 310 |
q = _gcry_generate_secret_prime (nbits/2, random_level, |
| 311 |
check_exponent, e); |
| 312 |
} |
| 313 |
else |
| 314 |
{ /* We check the exponent later. */ |
| 315 |
p = _gcry_generate_secret_prime (nbits/2, random_level, NULL, NULL); |
| 316 |
q = _gcry_generate_secret_prime (nbits/2, random_level, NULL, NULL); |
| 317 |
} |
| 318 |
if (mpi_cmp (p, q) > 0 ) /* p shall be smaller than q (for calc of u)*/ |
| 319 |
mpi_swap(p,q); |
| 320 |
/* calculate the modulus */ |
| 321 |
mpi_mul( n, p, q ); |
| 322 |
} |
| 323 |
while ( mpi_get_nbits(n) != nbits ); |
| 324 |
|
| 325 |
/* calculate Euler totient: phi = (p-1)(q-1) */ |
| 326 |
t1 = mpi_alloc_secure( mpi_get_nlimbs(p) ); |
| 327 |
t2 = mpi_alloc_secure( mpi_get_nlimbs(p) ); |
| 328 |
phi = mpi_snew ( nbits ); |
| 329 |
g = mpi_snew ( nbits ); |
| 330 |
f = mpi_snew ( nbits ); |
| 331 |
mpi_sub_ui( t1, p, 1 ); |
| 332 |
mpi_sub_ui( t2, q, 1 ); |
| 333 |
mpi_mul( phi, t1, t2 ); |
| 334 |
mpi_gcd (g, t1, t2); |
| 335 |
mpi_fdiv_q(f, phi, g); |
| 336 |
|
| 337 |
while (!mpi_gcd(t1, e, phi)) /* (while gcd is not 1) */ |
| 338 |
{ |
| 339 |
if (use_e) |
| 340 |
BUG (); /* The prime generator already made sure that we |
| 341 |
never can get to here. */ |
| 342 |
mpi_add_ui (e, e, 2); |
| 343 |
} |
| 344 |
|
| 345 |
/* calculate the secret key d = e^-1 mod phi */ |
| 346 |
d = mpi_snew ( nbits ); |
| 347 |
mpi_invm (d, e, f ); |
| 348 |
/* calculate the inverse of p and q (used for chinese remainder theorem)*/ |
| 349 |
u = mpi_snew ( nbits ); |
| 350 |
mpi_invm(u, p, q ); |
| 351 |
|
| 352 |
if( DBG_CIPHER ) |
| 353 |
{ |
| 354 |
log_mpidump(" p= ", p ); |
| 355 |
log_mpidump(" q= ", q ); |
| 356 |
log_mpidump("phi= ", phi ); |
| 357 |
log_mpidump(" g= ", g ); |
| 358 |
log_mpidump(" f= ", f ); |
| 359 |
log_mpidump(" n= ", n ); |
| 360 |
log_mpidump(" e= ", e ); |
| 361 |
log_mpidump(" d= ", d ); |
| 362 |
log_mpidump(" u= ", u ); |
| 363 |
} |
| 364 |
|
| 365 |
_gcry_mpi_release (t1); |
| 366 |
_gcry_mpi_release (t2); |
| 367 |
_gcry_mpi_release (phi); |
| 368 |
_gcry_mpi_release (f); |
| 369 |
_gcry_mpi_release (g); |
| 370 |
|
| 371 |
sk->n = n; |
| 372 |
sk->e = e; |
| 373 |
sk->p = p; |
| 374 |
sk->q = q; |
| 375 |
sk->d = d; |
| 376 |
sk->u = u; |
| 377 |
|
| 378 |
/* Now we can test our keys. */ |
| 379 |
if (test_keys (sk, nbits - 64)) |
| 380 |
{ |
| 381 |
_gcry_mpi_release (sk->n); sk->n = NULL; |
| 382 |
_gcry_mpi_release (sk->e); sk->e = NULL; |
| 383 |
_gcry_mpi_release (sk->p); sk->p = NULL; |
| 384 |
_gcry_mpi_release (sk->q); sk->q = NULL; |
| 385 |
_gcry_mpi_release (sk->d); sk->d = NULL; |
| 386 |
_gcry_mpi_release (sk->u); sk->u = NULL; |
| 387 |
fips_signal_error ("self-test after key generation failed"); |
| 388 |
return GPG_ERR_SELFTEST_FAILED; |
| 389 |
} |
| 390 |
|
| 391 |
return 0; |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
/* Check the RSA key length is acceptable for key generation or usage */ |
| 396 |
static gpg_err_code_t |
| 397 |
rsa_check_keysize (unsigned int nbits) |
| 398 |
{ |
| 399 |
if (fips_mode () && nbits < 2048) |
| 400 |
return GPG_ERR_INV_VALUE; |
| 401 |
|
| 402 |
return GPG_ERR_NO_ERROR; |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
/* Check the RSA key length is acceptable for signature verification |
| 407 |
* |
| 408 |
* FIPS allows signature verification with RSA keys of size |
| 409 |
* 1024, 1280, 1536 and 1792 in legacy mode, but this is up to the |
| 410 |
* calling application to decide if the signature is legacy and |
| 411 |
* should be accepted. |
| 412 |
*/ |
| 413 |
static gpg_err_code_t |
| 414 |
rsa_check_verify_keysize (unsigned int nbits) |
| 415 |
{ |
| 416 |
if (fips_mode ()) |
| 417 |
{ |
| 418 |
if ((nbits >= 1024 && (nbits % 256) == 0) || nbits >= 2048) |
| 419 |
return GPG_ERR_NO_ERROR; |
| 420 |
|
| 421 |
return GPG_ERR_INV_VALUE; |
| 422 |
} |
| 423 |
|
| 424 |
return GPG_ERR_NO_ERROR; |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
/**************** |
| 429 |
* Generate a key pair with a key of size NBITS. |
| 430 |
* USE_E = 0 let Libcgrypt decide what exponent to use. |
| 431 |
* = 1 request the use of a "secure" exponent; this is required by some |
| 432 |
* specification to be 65537. |
| 433 |
* > 2 Use this public exponent. If the given exponent |
| 434 |
* is not odd one is internally added to it. |
| 435 |
* TESTPARMS: If set, do not generate but test whether the p,q is probably prime |
| 436 |
* Returns key with zeroes to not break code calling this function. |
| 437 |
* TRANSIENT_KEY: If true, generate the primes using the standard RNG. |
| 438 |
* Returns: 2 structures filled with all needed values |
| 439 |
*/ |
| 440 |
static gpg_err_code_t |
| 441 |
generate_fips (RSA_secret_key *sk, unsigned int nbits, unsigned long use_e, |
| 442 |
gcry_sexp_t testparms, int transient_key) |
| 443 |
{ |
| 444 |
gcry_mpi_t p, q; /* the two primes */ |
| 445 |
gcry_mpi_t d; /* the private key */ |
| 446 |
gcry_mpi_t u; |
| 447 |
gcry_mpi_t p1, q1; |
| 448 |
gcry_mpi_t n; /* the public key */ |
| 449 |
gcry_mpi_t e; /* the exponent */ |
| 450 |
gcry_mpi_t g; |
| 451 |
gcry_mpi_t minp; |
| 452 |
gcry_mpi_t diff, mindiff; |
| 453 |
gcry_random_level_t random_level; |
| 454 |
unsigned int pbits = nbits/2; |
| 455 |
unsigned int i; |
| 456 |
int pqswitch; |
| 457 |
gpg_err_code_t ec; |
| 458 |
|
| 459 |
if (nbits <= 1024 || (nbits & 0x1FF)) |
| 460 |
return GPG_ERR_INV_VALUE; |
| 461 |
ec = rsa_check_keysize (nbits); |
| 462 |
if (ec) |
| 463 |
return ec; |
| 464 |
|
| 465 |
/* Set default error code. */ |
| 466 |
ec = GPG_ERR_NO_PRIME; |
| 467 |
|
| 468 |
/* The random quality depends on the transient_key flag. */ |
| 469 |
random_level = transient_key ? GCRY_STRONG_RANDOM : GCRY_VERY_STRONG_RANDOM; |
| 470 |
|
| 471 |
if (testparms) |
| 472 |
{ |
| 473 |
/* Parameters to derive the key are given. */ |
| 474 |
/* Note that we explicitly need to setup the values of tbl |
| 475 |
because some compilers (e.g. OpenWatcom, IRIX) don't allow |
| 476 |
initializing a structure with automatic variables. */ |
| 477 |
struct { const char *name; gcry_mpi_t *value; } tbl[] = { |
| 478 |
{ "e" }, |
| 479 |
{ "p" }, |
| 480 |
{ "q" }, |
| 481 |
{ NULL } |
| 482 |
}; |
| 483 |
int idx; |
| 484 |
gcry_sexp_t oneparm; |
| 485 |
|
| 486 |
e = p = q = NULL; |
| 487 |
tbl[0].value = &e; |
| 488 |
tbl[1].value = &p; |
| 489 |
tbl[2].value = &q; |
| 490 |
|
| 491 |
for (idx=0; tbl[idx].name; idx++) |
| 492 |
{ |
| 493 |
oneparm = sexp_find_token (testparms, tbl[idx].name, 0); |
| 494 |
if (oneparm) |
| 495 |
{ |
| 496 |
*tbl[idx].value = sexp_nth_mpi (oneparm, 1, GCRYMPI_FMT_USG); |
| 497 |
sexp_release (oneparm); |
| 498 |
} |
| 499 |
} |
| 500 |
for (idx=0; tbl[idx].name; idx++) |
| 501 |
if (!*tbl[idx].value) |
| 502 |
break; |
| 503 |
if (tbl[idx].name) |
| 504 |
{ |
| 505 |
/* At least one parameter is missing. */ |
| 506 |
for (idx=0; tbl[idx].name; idx++) |
| 507 |
_gcry_mpi_release (*tbl[idx].value); |
| 508 |
return GPG_ERR_MISSING_VALUE; |
| 509 |
} |
| 510 |
} |
| 511 |
else |
| 512 |
{ |
| 513 |
if (use_e < 65537) |
| 514 |
use_e = 65537; /* This is the smallest value allowed by FIPS */ |
| 515 |
|
| 516 |
e = mpi_alloc ((32+BITS_PER_MPI_LIMB-1)/BITS_PER_MPI_LIMB); |
| 517 |
|
| 518 |
use_e |= 1; /* make sure this is odd */ |
| 519 |
mpi_set_ui (e, use_e); |
| 520 |
|
| 521 |
p = mpi_snew (pbits); |
| 522 |
q = mpi_snew (pbits); |
| 523 |
} |
| 524 |
|
| 525 |
n = mpi_new (nbits); |
| 526 |
d = mpi_snew (nbits); |
| 527 |
u = mpi_snew (nbits); |
| 528 |
|
| 529 |
/* prepare approximate minimum p and q */ |
| 530 |
minp = mpi_new (pbits); |
| 531 |
mpi_set_ui (minp, 0xB504F334); |
| 532 |
mpi_lshift (minp, minp, pbits - 32); |
| 533 |
|
| 534 |
/* prepare minimum p and q difference */ |
| 535 |
diff = mpi_new (pbits); |
| 536 |
mindiff = mpi_new (pbits - 99); |
| 537 |
mpi_set_ui (mindiff, 1); |
| 538 |
mpi_lshift (mindiff, mindiff, pbits - 100); |
| 539 |
|
| 540 |
p1 = mpi_snew (pbits); |
| 541 |
q1 = mpi_snew (pbits); |
| 542 |
g = mpi_snew (pbits); |
| 543 |
|
| 544 |
retry: |
| 545 |
/* generate p and q */ |
| 546 |
for (i = 0; i < 10 * pbits; i++) |
| 547 |
{ |
| 548 |
ploop: |
| 549 |
if (!testparms) |
| 550 |
{ |
| 551 |
_gcry_mpi_randomize (p, pbits, random_level); |
| 552 |
mpi_set_bit (p, 0); |
| 553 |
} |
| 554 |
if (mpi_cmp (p, minp) < 0) |
| 555 |
{ |
| 556 |
if (testparms) |
| 557 |
goto err; |
| 558 |
goto ploop; |
| 559 |
} |
| 560 |
|
| 561 |
mpi_sub_ui (p1, p, 1); |
| 562 |
if (mpi_gcd (g, p1, e)) |
| 563 |
{ |
| 564 |
if (_gcry_fips186_4_prime_check (p, pbits) != GPG_ERR_NO_ERROR) |
| 565 |
{ |
| 566 |
/* not a prime */ |
| 567 |
if (testparms) |
| 568 |
goto err; |
| 569 |
} |
| 570 |
else |
| 571 |
break; |
| 572 |
} |
| 573 |
else if (testparms) |
| 574 |
goto err; |
| 575 |
} |
| 576 |
if (i >= 10 * pbits) |
| 577 |
goto err; |
| 578 |
|
| 579 |
for (i = 0; i < 20 * pbits; i++) |
| 580 |
{ |
| 581 |
qloop: |
| 582 |
if (!testparms) |
| 583 |
{ |
| 584 |
_gcry_mpi_randomize (q, pbits, random_level); |
| 585 |
mpi_set_bit (q, 0); |
| 586 |
} |
| 587 |
if (mpi_cmp (q, minp) < 0) |
| 588 |
{ |
| 589 |
if (testparms) |
| 590 |
goto err; |
| 591 |
goto qloop; |
| 592 |
} |
| 593 |
if (mpi_cmp (p, q) > 0) |
| 594 |
{ |
| 595 |
pqswitch = 1; |
| 596 |
mpi_sub (diff, p, q); |
| 597 |
} |
| 598 |
else |
| 599 |
{ |
| 600 |
pqswitch = 0; |
| 601 |
mpi_sub (diff, q, p); |
| 602 |
} |
| 603 |
if (mpi_cmp (diff, mindiff) < 0) |
| 604 |
{ |
| 605 |
if (testparms) |
| 606 |
goto err; |
| 607 |
goto qloop; |
| 608 |
} |
| 609 |
|
| 610 |
mpi_sub_ui (q1, q, 1); |
| 611 |
if (mpi_gcd (g, q1, e)) |
| 612 |
{ |
| 613 |
if (_gcry_fips186_4_prime_check (q, pbits) != GPG_ERR_NO_ERROR) |
| 614 |
{ |
| 615 |
/* not a prime */ |
| 616 |
if (testparms) |
| 617 |
goto err; |
| 618 |
} |
| 619 |
else |
| 620 |
break; |
| 621 |
} |
| 622 |
else if (testparms) |
| 623 |
goto err; |
| 624 |
} |
| 625 |
if (i >= 20 * pbits) |
| 626 |
goto err; |
| 627 |
|
| 628 |
if (testparms) |
| 629 |
{ |
| 630 |
mpi_clear (p); |
| 631 |
mpi_clear (q); |
| 632 |
} |
| 633 |
else |
| 634 |
{ |
| 635 |
gcry_mpi_t f; |
| 636 |
|
| 637 |
if (pqswitch) |
| 638 |
{ |
| 639 |
gcry_mpi_t tmp; |
| 640 |
|
| 641 |
tmp = p; |
| 642 |
p = q; |
| 643 |
q = tmp; |
| 644 |
} |
| 645 |
|
| 646 |
f = mpi_snew (nbits); |
| 647 |
|
| 648 |
/* calculate the modulus */ |
| 649 |
mpi_mul (n, p, q); |
| 650 |
|
| 651 |
/* calculate the secret key d = e^1 mod phi */ |
| 652 |
mpi_gcd (g, p1, q1); |
| 653 |
mpi_fdiv_q (f, p1, g); |
| 654 |
mpi_mul (f, f, q1); |
| 655 |
|
| 656 |
mpi_invm (d, e, f); |
| 657 |
|
| 658 |
_gcry_mpi_release (f); |
| 659 |
|
| 660 |
if (mpi_get_nbits (d) < pbits) |
| 661 |
goto retry; |
| 662 |
|
| 663 |
/* calculate the inverse of p and q (used for chinese remainder theorem)*/ |
| 664 |
mpi_invm (u, p, q ); |
| 665 |
} |
| 666 |
|
| 667 |
ec = 0; /* Success. */ |
| 668 |
|
| 669 |
if (DBG_CIPHER) |
| 670 |
{ |
| 671 |
log_mpidump(" p= ", p ); |
| 672 |
log_mpidump(" q= ", q ); |
| 673 |
log_mpidump(" n= ", n ); |
| 674 |
log_mpidump(" e= ", e ); |
| 675 |
log_mpidump(" d= ", d ); |
| 676 |
log_mpidump(" u= ", u ); |
| 677 |
} |
| 678 |
|
| 679 |
err: |
| 680 |
|
| 681 |
_gcry_mpi_release (p1); |
| 682 |
_gcry_mpi_release (q1); |
| 683 |
_gcry_mpi_release (g); |
| 684 |
_gcry_mpi_release (minp); |
| 685 |
_gcry_mpi_release (mindiff); |
| 686 |
_gcry_mpi_release (diff); |
| 687 |
|
| 688 |
sk->n = n; |
| 689 |
sk->e = e; |
| 690 |
sk->p = p; |
| 691 |
sk->q = q; |
| 692 |
sk->d = d; |
| 693 |
sk->u = u; |
| 694 |
|
| 695 |
if (ec) |
| 696 |
{ |
| 697 |
_gcry_mpi_release (sk->n); sk->n = NULL; |
| 698 |
_gcry_mpi_release (sk->e); sk->e = NULL; |
| 699 |
_gcry_mpi_release (sk->p); sk->p = NULL; |
| 700 |
_gcry_mpi_release (sk->q); sk->q = NULL; |
| 701 |
_gcry_mpi_release (sk->d); sk->d = NULL; |
| 702 |
_gcry_mpi_release (sk->u); sk->u = NULL; |
| 703 |
} |
| 704 |
|
| 705 |
return ec; |
| 706 |
} |
| 707 |
|
| 708 |
|
| 709 |
/* Helper for generate_x931. */ |
| 710 |
static gcry_mpi_t |
| 711 |
gen_x931_parm_xp (unsigned int nbits) |
| 712 |
{ |
| 713 |
gcry_mpi_t xp; |
| 714 |
|
| 715 |
xp = mpi_snew (nbits); |
| 716 |
_gcry_mpi_randomize (xp, nbits, GCRY_VERY_STRONG_RANDOM); |
| 717 |
|
| 718 |
/* The requirement for Xp is: |
| 719 |
|
| 720 |
sqrt{2}*2^{nbits-1} <= xp <= 2^{nbits} - 1 |
| 721 |
|
| 722 |
We set the two high order bits to 1 to satisfy the lower bound. |
| 723 |
By using mpi_set_highbit we make sure that the upper bound is |
| 724 |
satisfied as well. */ |
| 725 |
mpi_set_highbit (xp, nbits-1); |
| 726 |
mpi_set_bit (xp, nbits-2); |
| 727 |
gcry_assert ( mpi_get_nbits (xp) == nbits ); |
| 728 |
|
| 729 |
return xp; |
| 730 |
} |
| 731 |
|
| 732 |
|
| 733 |
/* Helper for generate_x931. */ |
| 734 |
static gcry_mpi_t |
| 735 |
gen_x931_parm_xi (void) |
| 736 |
{ |
| 737 |
gcry_mpi_t xi; |
| 738 |
|
| 739 |
xi = mpi_snew (101); |
| 740 |
_gcry_mpi_randomize (xi, 101, GCRY_VERY_STRONG_RANDOM); |
| 741 |
mpi_set_highbit (xi, 100); |
| 742 |
gcry_assert ( mpi_get_nbits (xi) == 101 ); |
| 743 |
|
| 744 |
return xi; |
| 745 |
} |
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
/* Variant of the standard key generation code using the algorithm |
| 750 |
from X9.31. Using this algorithm has the advantage that the |
| 751 |
generation can be made deterministic which is required for CAVS |
| 752 |
testing. */ |
| 753 |
static gpg_err_code_t |
| 754 |
generate_x931 (RSA_secret_key *sk, unsigned int nbits, unsigned long e_value, |
| 755 |
gcry_sexp_t deriveparms, int *swapped) |
| 756 |
{ |
| 757 |
gcry_mpi_t p, q; /* The two primes. */ |
| 758 |
gcry_mpi_t e; /* The public exponent. */ |
| 759 |
gcry_mpi_t n; /* The public key. */ |
| 760 |
gcry_mpi_t d; /* The private key */ |
| 761 |
gcry_mpi_t u; /* The inverse of p and q. */ |
| 762 |
gcry_mpi_t pm1; /* p - 1 */ |
| 763 |
gcry_mpi_t qm1; /* q - 1 */ |
| 764 |
gcry_mpi_t phi; /* Euler totient. */ |
| 765 |
gcry_mpi_t f, g; /* Helper. */ |
| 766 |
|
| 767 |
*swapped = 0; |
| 768 |
|
| 769 |
if (e_value == 0) /* 65537 is the libgcrypt's selection. */ |
| 770 |
e_value = 65537; |
| 771 |
else if (e_value == 1) /* Alias for a secure value. */ |
| 772 |
e_value = 65537; |
| 773 |
|
| 774 |
/* Point 1 of section 4.1: k = 1024 + 256s with S >= 0 */ |
| 775 |
if (nbits < 1024 || (nbits % 256)) |
| 776 |
return GPG_ERR_INV_VALUE; |
| 777 |
|
| 778 |
/* Point 2: 2 <= bitlength(e) < 2^{k-2} |
| 779 |
Note that we do not need to check the upper bound because we use |
| 780 |
an unsigned long for E and thus there is no way for E to reach |
| 781 |
that limit. */ |
| 782 |
if (e_value < 3) |
| 783 |
return GPG_ERR_INV_VALUE; |
| 784 |
|
| 785 |
/* Our implementation requires E to be odd. */ |
| 786 |
if (!(e_value & 1)) |
| 787 |
return GPG_ERR_INV_VALUE; |
| 788 |
|
| 789 |
/* Point 3: e > 0 or e 0 if it is to be randomly generated. |
| 790 |
We support only a fixed E and thus there is no need for an extra test. */ |
| 791 |
|
| 792 |
|
| 793 |
/* Compute or extract the derive parameters. */ |
| 794 |
{ |
| 795 |
gcry_mpi_t xp1 = NULL; |
| 796 |
gcry_mpi_t xp2 = NULL; |
| 797 |
gcry_mpi_t xp = NULL; |
| 798 |
gcry_mpi_t xq1 = NULL; |
| 799 |
gcry_mpi_t xq2 = NULL; |
| 800 |
gcry_mpi_t xq = NULL; |
| 801 |
gcry_mpi_t tmpval; |
| 802 |
|
| 803 |
if (!deriveparms) |
| 804 |
{ |
| 805 |
/* Not given: Generate them. */ |
| 806 |
xp = gen_x931_parm_xp (nbits/2); |
| 807 |
/* Make sure that |xp - xq| > 2^{nbits - 100} holds. */ |
| 808 |
tmpval = mpi_snew (nbits/2); |
| 809 |
do |
| 810 |
{ |
| 811 |
_gcry_mpi_release (xq); |
| 812 |
xq = gen_x931_parm_xp (nbits/2); |
| 813 |
mpi_sub (tmpval, xp, xq); |
| 814 |
} |
| 815 |
while (mpi_get_nbits (tmpval) <= (nbits/2 - 100)); |
| 816 |
_gcry_mpi_release (tmpval); |
| 817 |
|
| 818 |
xp1 = gen_x931_parm_xi (); |
| 819 |
xp2 = gen_x931_parm_xi (); |
| 820 |
xq1 = gen_x931_parm_xi (); |
| 821 |
xq2 = gen_x931_parm_xi (); |
| 822 |
|
| 823 |
} |
| 824 |
else |
| 825 |
{ |
| 826 |
/* Parameters to derive the key are given. */ |
| 827 |
/* Note that we explicitly need to setup the values of tbl |
| 828 |
because some compilers (e.g. OpenWatcom, IRIX) don't allow |
| 829 |
initializing a structure with automatic variables. */ |
| 830 |
struct { const char *name; gcry_mpi_t *value; } tbl[] = { |
| 831 |
{ "Xp1" }, |
| 832 |
{ "Xp2" }, |
| 833 |
{ "Xp" }, |
| 834 |
{ "Xq1" }, |
| 835 |
{ "Xq2" }, |
| 836 |
{ "Xq" }, |
| 837 |
{ NULL } |
| 838 |
}; |
| 839 |
int idx; |
| 840 |
gcry_sexp_t oneparm; |
| 841 |
|
| 842 |
tbl[0].value = &xp1; |
| 843 |
tbl[1].value = &xp2; |
| 844 |
tbl[2].value = &xp; |
| 845 |
tbl[3].value = &xq1; |
| 846 |
tbl[4].value = &xq2; |
| 847 |
tbl[5].value = &xq; |
| 848 |
|
| 849 |
for (idx=0; tbl[idx].name; idx++) |
| 850 |
{ |
| 851 |
oneparm = sexp_find_token (deriveparms, tbl[idx].name, 0); |
| 852 |
if (oneparm) |
| 853 |
{ |
| 854 |
*tbl[idx].value = sexp_nth_mpi (oneparm, 1, GCRYMPI_FMT_USG); |
| 855 |
sexp_release (oneparm); |
| 856 |
} |
| 857 |
} |
| 858 |
for (idx=0; tbl[idx].name; idx++) |
| 859 |
if (!*tbl[idx].value) |
| 860 |
break; |
| 861 |
if (tbl[idx].name) |
| 862 |
{ |
| 863 |
/* At least one parameter is missing. */ |
| 864 |
for (idx=0; tbl[idx].name; idx++) |
| 865 |
_gcry_mpi_release (*tbl[idx].value); |
| 866 |
return GPG_ERR_MISSING_VALUE; |
| 867 |
} |
| 868 |
} |
| 869 |
|
| 870 |
e = mpi_alloc_set_ui (e_value); |
| 871 |
|
| 872 |
/* Find two prime numbers. */ |
| 873 |
p = _gcry_derive_x931_prime (xp, xp1, xp2, e, NULL, NULL); |
| 874 |
q = _gcry_derive_x931_prime (xq, xq1, xq2, e, NULL, NULL); |
| 875 |
_gcry_mpi_release (xp); xp = NULL; |
| 876 |
_gcry_mpi_release (xp1); xp1 = NULL; |
| 877 |
_gcry_mpi_release (xp2); xp2 = NULL; |
| 878 |
_gcry_mpi_release (xq); xq = NULL; |
| 879 |
_gcry_mpi_release (xq1); xq1 = NULL; |
| 880 |
_gcry_mpi_release (xq2); xq2 = NULL; |
| 881 |
if (!p || !q) |
| 882 |
{ |
| 883 |
_gcry_mpi_release (p); |
| 884 |
_gcry_mpi_release (q); |
| 885 |
_gcry_mpi_release (e); |
| 886 |
return GPG_ERR_NO_PRIME; |
| 887 |
} |
| 888 |
} |
| 889 |
|
| 890 |
|
| 891 |
/* Compute the public modulus. We make sure that p is smaller than |
| 892 |
q to allow the use of the CRT. */ |
| 893 |
if (mpi_cmp (p, q) > 0 ) |
| 894 |
{ |
| 895 |
mpi_swap (p, q); |
| 896 |
*swapped = 1; |
| 897 |
} |
| 898 |
n = mpi_new (nbits); |
| 899 |
mpi_mul (n, p, q); |
| 900 |
|
| 901 |
/* Compute the Euler totient: phi = (p-1)(q-1) */ |
| 902 |
pm1 = mpi_snew (nbits/2); |
| 903 |
qm1 = mpi_snew (nbits/2); |
| 904 |
phi = mpi_snew (nbits); |
| 905 |
mpi_sub_ui (pm1, p, 1); |
| 906 |
mpi_sub_ui (qm1, q, 1); |
| 907 |
mpi_mul (phi, pm1, qm1); |
| 908 |
|
| 909 |
g = mpi_snew (nbits); |
| 910 |
gcry_assert (mpi_gcd (g, e, phi)); |
| 911 |
|
| 912 |
/* Compute: f = lcm(p-1,q-1) = phi / gcd(p-1,q-1) */ |
| 913 |
mpi_gcd (g, pm1, qm1); |
| 914 |
f = pm1; pm1 = NULL; |
| 915 |
_gcry_mpi_release (qm1); qm1 = NULL; |
| 916 |
mpi_fdiv_q (f, phi, g); |
| 917 |
_gcry_mpi_release (phi); phi = NULL; |
| 918 |
d = g; g = NULL; |
| 919 |
/* Compute the secret key: d = e^{-1} mod lcm(p-1,q-1) */ |
| 920 |
mpi_invm (d, e, f); |
| 921 |
|
| 922 |
/* Compute the inverse of p and q. */ |
| 923 |
u = f; f = NULL; |
| 924 |
mpi_invm (u, p, q ); |
| 925 |
|
| 926 |
if( DBG_CIPHER ) |
| 927 |
{ |
| 928 |
if (*swapped) |
| 929 |
log_debug ("p and q are swapped\n"); |
| 930 |
log_mpidump(" p", p ); |
| 931 |
log_mpidump(" q", q ); |
| 932 |
log_mpidump(" n", n ); |
| 933 |
log_mpidump(" e", e ); |
| 934 |
log_mpidump(" d", d ); |
| 935 |
log_mpidump(" u", u ); |
| 936 |
} |
| 937 |
|
| 938 |
|
| 939 |
sk->n = n; |
| 940 |
sk->e = e; |
| 941 |
sk->p = p; |
| 942 |
sk->q = q; |
| 943 |
sk->d = d; |
| 944 |
sk->u = u; |
| 945 |
|
| 946 |
/* Now we can test our keys. */ |
| 947 |
if (test_keys (sk, nbits - 64)) |
| 948 |
{ |
| 949 |
_gcry_mpi_release (sk->n); sk->n = NULL; |
| 950 |
_gcry_mpi_release (sk->e); sk->e = NULL; |
| 951 |
_gcry_mpi_release (sk->p); sk->p = NULL; |
| 952 |
_gcry_mpi_release (sk->q); sk->q = NULL; |
| 953 |
_gcry_mpi_release (sk->d); sk->d = NULL; |
| 954 |
_gcry_mpi_release (sk->u); sk->u = NULL; |
| 955 |
fips_signal_error ("self-test after key generation failed"); |
| 956 |
return GPG_ERR_SELFTEST_FAILED; |
| 957 |
} |
| 958 |
|
| 959 |
return 0; |
| 960 |
} |
| 961 |
|
| 962 |
|
| 963 |
/* Uncomment following line to enable stronger check. */ |
| 964 |
/* #define ENABLE_STRONGER_CHECK 1 */ |
| 965 |
#ifndef ENABLE_STRONGER_CHECK |
| 966 |
/**************** |
| 967 |
* Test whether the secret key is valid. |
| 968 |
* Returns: true if this is a valid key. |
| 969 |
*/ |
| 970 |
static int |
| 971 |
check_secret_key( RSA_secret_key *sk ) |
| 972 |
{ |
| 973 |
int rc; |
| 974 |
gcry_mpi_t temp = mpi_alloc( mpi_get_nlimbs(sk->p)*2 ); |
| 975 |
|
| 976 |
mpi_mul(temp, sk->p, sk->q ); |
| 977 |
rc = mpi_cmp( temp, sk->n ); |
| 978 |
mpi_free(temp); |
| 979 |
return !rc; |
| 980 |
} |
| 981 |
#endif |
| 982 |
|
| 983 |
|
| 984 |
/**************** |
| 985 |
* Public key operation. Encrypt INPUT with PKEY and put result into OUTPUT. |
| 986 |
* |
| 987 |
* c = m^e mod n |
| 988 |
* |
| 989 |
* Where c is OUTPUT, m is INPUT and e,n are elements of PKEY. |
| 990 |
*/ |
| 991 |
static void |
| 992 |
public(gcry_mpi_t output, gcry_mpi_t input, RSA_public_key *pkey ) |
| 993 |
{ |
| 994 |
if( output == input ) /* powm doesn't like output and input the same */ |
| 995 |
{ |
| 996 |
gcry_mpi_t x = mpi_alloc( mpi_get_nlimbs(input)*2 ); |
| 997 |
mpi_powm( x, input, pkey->e, pkey->n ); |
| 998 |
mpi_set(output, x); |
| 999 |
mpi_free(x); |
| 1000 |
} |
| 1001 |
else |
| 1002 |
mpi_powm( output, input, pkey->e, pkey->n ); |
| 1003 |
} |
| 1004 |
|
| 1005 |
#ifdef ENABLE_STRONGER_CHECK |
| 1006 |
/**************** |
| 1007 |
* Test whether the secret key is valid. |
| 1008 |
* Returns: true if this is a valid key. |
| 1009 |
*/ |
| 1010 |
static int |
| 1011 |
check_secret_key ( RSA_secret_key *skey ) |
| 1012 |
{ |
| 1013 |
gcry_mpi_t t = mpi_alloc_secure ( 0 ); |
| 1014 |
gcry_mpi_t t1 = mpi_alloc_secure ( 0 ); |
| 1015 |
gcry_mpi_t t2 = mpi_alloc_secure ( 0 ); |
| 1016 |
gcry_mpi_t phi = mpi_alloc_secure ( 0 ); |
| 1017 |
int rc = 1; |
| 1018 |
|
| 1019 |
/* check that n == p * q */ |
| 1020 |
mpi_mul( t, skey->p, skey->q); |
| 1021 |
if (mpi_cmp( t, skey->n) ) |
| 1022 |
{ |
| 1023 |
rc = 0; |
| 1024 |
goto leave; |
| 1025 |
} |
| 1026 |
|
| 1027 |
/* check that p is less than q */ |
| 1028 |
if( mpi_cmp( skey->p, skey->q ) > 0 ) |
| 1029 |
{ |
| 1030 |
rc = 0; |
| 1031 |
goto leave; |
| 1032 |
} |
| 1033 |
|
| 1034 |
/* check that e divides neither p-1 nor q-1 */ |
| 1035 |
mpi_sub_ui(t, skey->p, 1 ); |
| 1036 |
mpi_fdiv_r(t, t, skey->e ); |
| 1037 |
if ( !mpi_cmp_ui( t, 0) ) |
| 1038 |
{ |
| 1039 |
rc = 0; |
| 1040 |
goto leave; |
| 1041 |
} |
| 1042 |
mpi_sub_ui(t, skey->q, 1 ); |
| 1043 |
mpi_fdiv_r(t, t, skey->e ); |
| 1044 |
if ( !mpi_cmp_ui( t, 0) ) |
| 1045 |
{ |
| 1046 |
rc = 0; |
| 1047 |
goto leave; |
| 1048 |
} |
| 1049 |
|
| 1050 |
/* check that d is correct */ |
| 1051 |
mpi_sub_ui( t1, skey->p, 1 ); |
| 1052 |
mpi_sub_ui( t2, skey->q, 1 ); |
| 1053 |
mpi_mul( phi, t1, t2 ); |
| 1054 |
_gcry_mpi_gcd(t, t1, t2); |
| 1055 |
mpi_fdiv_q(t, phi, t); |
| 1056 |
mpi_invm(t, skey->e, t ); |
| 1057 |
if ( mpi_cmp(t, skey->d ) ) |
| 1058 |
{ |
| 1059 |
rc = 0; |
| 1060 |
goto leave; |
| 1061 |
} |
| 1062 |
|
| 1063 |
/* check for correctness of u */ |
| 1064 |
mpi_invm(t, skey->p, skey->q ); |
| 1065 |
if ( mpi_cmp(t, skey->u ) ) |
| 1066 |
{ |
| 1067 |
rc = 0; |
| 1068 |
goto leave; |
| 1069 |
} |
| 1070 |
|
| 1071 |
leave: |
| 1072 |
mpi_free (t); |
| 1073 |
mpi_free (t1); |
| 1074 |
mpi_free (t2); |
| 1075 |
mpi_free (phi); |
| 1076 |
|
| 1077 |
return rc; |
| 1078 |
} |
| 1079 |
#endif |
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
/* Secret key operation - standard version. |
| 1084 |
* |
| 1085 |
* m = c^d mod n |
| 1086 |
*/ |
| 1087 |
static void |
| 1088 |
secret_core_std (gcry_mpi_t M, gcry_mpi_t C, |
| 1089 |
gcry_mpi_t D, gcry_mpi_t N) |
| 1090 |
{ |
| 1091 |
mpi_powm (M, C, D, N); |
| 1092 |
} |
| 1093 |
|
| 1094 |
|
| 1095 |
/* Secret key operation - using the CRT. |
| 1096 |
* |
| 1097 |
* m1 = c ^ (d mod (p-1)) mod p |
| 1098 |
* m2 = c ^ (d mod (q-1)) mod q |
| 1099 |
* h = u * (m2 - m1) mod q |
| 1100 |
* m = m1 + h * p |
| 1101 |
*/ |
| 1102 |
static void |
| 1103 |
secret_core_crt (gcry_mpi_t M, gcry_mpi_t C, |
| 1104 |
gcry_mpi_t D, unsigned int Nlimbs, |
| 1105 |
gcry_mpi_t P, gcry_mpi_t Q, gcry_mpi_t U) |
| 1106 |
{ |
| 1107 |
gcry_mpi_t m1 = mpi_alloc_secure ( Nlimbs + 1 ); |
| 1108 |
gcry_mpi_t m2 = mpi_alloc_secure ( Nlimbs + 1 ); |
| 1109 |
gcry_mpi_t h = mpi_alloc_secure ( Nlimbs + 1 ); |
| 1110 |
gcry_mpi_t D_blind = mpi_alloc_secure ( Nlimbs + 1 ); |
| 1111 |
gcry_mpi_t r; |
| 1112 |
unsigned int r_nbits; |
| 1113 |
|
| 1114 |
r_nbits = mpi_get_nbits (P) / 4; |
| 1115 |
if (r_nbits < 96) |
| 1116 |
r_nbits = 96; |
| 1117 |
r = mpi_secure_new (r_nbits); |
| 1118 |
|
| 1119 |
/* d_blind = (d mod (p-1)) + (p-1) * r */ |
| 1120 |
/* m1 = c ^ d_blind mod p */ |
| 1121 |
_gcry_mpi_randomize (r, r_nbits, GCRY_WEAK_RANDOM); |
| 1122 |
mpi_set_highbit (r, r_nbits - 1); |
| 1123 |
mpi_sub_ui ( h, P, 1 ); |
| 1124 |
mpi_mul ( D_blind, h, r ); |
| 1125 |
mpi_fdiv_r ( h, D, h ); |
| 1126 |
mpi_add ( D_blind, D_blind, h ); |
| 1127 |
mpi_powm ( m1, C, D_blind, P ); |
| 1128 |
|
| 1129 |
/* d_blind = (d mod (q-1)) + (q-1) * r */ |
| 1130 |
/* m2 = c ^ d_blind mod q */ |
| 1131 |
_gcry_mpi_randomize (r, r_nbits, GCRY_WEAK_RANDOM); |
| 1132 |
mpi_set_highbit (r, r_nbits - 1); |
| 1133 |
mpi_sub_ui ( h, Q, 1 ); |
| 1134 |
mpi_mul ( D_blind, h, r ); |
| 1135 |
mpi_fdiv_r ( h, D, h ); |
| 1136 |
mpi_add ( D_blind, D_blind, h ); |
| 1137 |
mpi_powm ( m2, C, D_blind, Q ); |
| 1138 |
|
| 1139 |
mpi_free ( r ); |
| 1140 |
mpi_free ( D_blind ); |
| 1141 |
|
| 1142 |
/* h = u * ( m2 - m1 ) mod q */ |
| 1143 |
mpi_sub ( h, m2, m1 ); |
| 1144 |
if ( mpi_has_sign ( h ) ) |
| 1145 |
mpi_add ( h, h, Q ); |
| 1146 |
mpi_mulm ( h, U, h, Q ); |
| 1147 |
|
| 1148 |
/* m = m1 + h * p */ |
| 1149 |
mpi_mul ( h, h, P ); |
| 1150 |
mpi_add ( M, m1, h ); |
| 1151 |
|
| 1152 |
mpi_free ( h ); |
| 1153 |
mpi_free ( m1 ); |
| 1154 |
mpi_free ( m2 ); |
| 1155 |
} |
| 1156 |
|
| 1157 |
|
| 1158 |
/* Secret key operation. |
| 1159 |
* Encrypt INPUT with SKEY and put result into |
| 1160 |
* OUTPUT. SKEY has the secret key parameters. |
| 1161 |
*/ |
| 1162 |
static void |
| 1163 |
secret (gcry_mpi_t output, gcry_mpi_t input, RSA_secret_key *skey ) |
| 1164 |
{ |
| 1165 |
/* Remove superfluous leading zeroes from INPUT. */ |
| 1166 |
mpi_normalize (input); |
| 1167 |
|
| 1168 |
if (!skey->p || !skey->q || !skey->u) |
| 1169 |
{ |
| 1170 |
secret_core_std (output, input, skey->d, skey->n); |
| 1171 |
} |
| 1172 |
else |
| 1173 |
{ |
| 1174 |
secret_core_crt (output, input, skey->d, mpi_get_nlimbs (skey->n), |
| 1175 |
skey->p, skey->q, skey->u); |
| 1176 |
} |
| 1177 |
} |
| 1178 |
|
| 1179 |
|
| 1180 |
static void |
| 1181 |
secret_blinded (gcry_mpi_t output, gcry_mpi_t input, |
| 1182 |
RSA_secret_key *sk, unsigned int nbits) |
| 1183 |
{ |
| 1184 |
gcry_mpi_t r; /* Random number needed for blinding. */ |
| 1185 |
gcry_mpi_t ri; /* Modular multiplicative inverse of r. */ |
| 1186 |
gcry_mpi_t bldata; /* Blinded data to decrypt. */ |
| 1187 |
|
| 1188 |
/* First, we need a random number r between 0 and n - 1, which is |
| 1189 |
* relatively prime to n (i.e. it is neither p nor q). The random |
| 1190 |
* number needs to be only unpredictable, thus we employ the |
| 1191 |
* gcry_create_nonce function by using GCRY_WEAK_RANDOM with |
| 1192 |
* gcry_mpi_randomize. */ |
| 1193 |
r = mpi_snew (nbits); |
| 1194 |
ri = mpi_snew (nbits); |
| 1195 |
bldata = mpi_snew (nbits); |
| 1196 |
|
| 1197 |
do |
| 1198 |
{ |
| 1199 |
_gcry_mpi_randomize (r, nbits, GCRY_WEAK_RANDOM); |
| 1200 |
mpi_mod (r, r, sk->n); |
| 1201 |
} |
| 1202 |
while (!mpi_invm (ri, r, sk->n)); |
| 1203 |
|
| 1204 |
/* Do blinding. We calculate: y = (x * r^e) mod n, where r is the |
| 1205 |
* random number, e is the public exponent, x is the non-blinded |
| 1206 |
* input data and n is the RSA modulus. */ |
| 1207 |
mpi_powm (bldata, r, sk->e, sk->n); |
| 1208 |
mpi_mulm (bldata, bldata, input, sk->n); |
| 1209 |
|
| 1210 |
/* Perform decryption. */ |
| 1211 |
secret (output, bldata, sk); |
| 1212 |
_gcry_mpi_release (bldata); |
| 1213 |
|
| 1214 |
/* Undo blinding. Here we calculate: y = (x * r^-1) mod n, where x |
| 1215 |
* is the blinded decrypted data, ri is the modular multiplicative |
| 1216 |
* inverse of r and n is the RSA modulus. */ |
| 1217 |
mpi_mulm (output, output, ri, sk->n); |
| 1218 |
|
| 1219 |
_gcry_mpi_release (r); |
| 1220 |
_gcry_mpi_release (ri); |
| 1221 |
} |
| 1222 |
|
| 1223 |
|
| 1224 |
/********************************************* |
| 1225 |
************** interface ****************** |
| 1226 |
*********************************************/ |
| 1227 |
|
| 1228 |
static gcry_err_code_t |
| 1229 |
rsa_generate (const gcry_sexp_t genparms, gcry_sexp_t *r_skey) |
| 1230 |
{ |
| 1231 |
gpg_err_code_t ec; |
| 1232 |
unsigned int nbits; |
| 1233 |
unsigned long evalue; |
| 1234 |
RSA_secret_key sk; |
| 1235 |
gcry_sexp_t deriveparms; |
| 1236 |
int flags = 0; |
| 1237 |
gcry_sexp_t l1; |
| 1238 |
gcry_sexp_t swap_info = NULL; |
| 1239 |
int testparms = 0; |
| 1240 |
|
| 1241 |
memset (&sk, 0, sizeof sk); |
| 1242 |
|
| 1243 |
ec = _gcry_pk_util_get_nbits (genparms, &nbits); |
| 1244 |
if (ec) |
| 1245 |
return ec; |
| 1246 |
|
| 1247 |
ec = _gcry_pk_util_get_rsa_use_e (genparms, &evalue); |
| 1248 |
if (ec) |
| 1249 |
return ec; |
| 1250 |
|
| 1251 |
/* Parse the optional flags list. */ |
| 1252 |
l1 = sexp_find_token (genparms, "flags", 0); |
| 1253 |
if (l1) |
| 1254 |
{ |
| 1255 |
ec = _gcry_pk_util_parse_flaglist (l1, &flags, NULL); |
| 1256 |
sexp_release (l1); |
| 1257 |
if (ec) |
| 1258 |
return ec; |
| 1259 |
} |
| 1260 |
|
| 1261 |
deriveparms = (genparms? |
| 1262 |
sexp_find_token (genparms, "derive-parms", 0) : NULL); |
| 1263 |
if (!deriveparms) |
| 1264 |
{ |
| 1265 |
/* Parse the optional "use-x931" flag. */ |
| 1266 |
l1 = sexp_find_token (genparms, "use-x931", 0); |
| 1267 |
if (l1) |
| 1268 |
{ |
| 1269 |
flags |= PUBKEY_FLAG_USE_X931; |
| 1270 |
sexp_release (l1); |
| 1271 |
} |
| 1272 |
} |
| 1273 |
|
| 1274 |
if (deriveparms || (flags & PUBKEY_FLAG_USE_X931)) |
| 1275 |
{ |
| 1276 |
int swapped; |
| 1277 |
if (fips_mode ()) |
| 1278 |
{ |
| 1279 |
sexp_release (deriveparms); |
| 1280 |
return GPG_ERR_INV_SEXP; |
| 1281 |
} |
| 1282 |
ec = generate_x931 (&sk, nbits, evalue, deriveparms, &swapped); |
| 1283 |
sexp_release (deriveparms); |
| 1284 |
if (!ec && swapped) |
| 1285 |
ec = sexp_new (&swap_info, "(misc-key-info(p-q-swapped))", 0, 1); |
| 1286 |
} |
| 1287 |
else |
| 1288 |
{ |
| 1289 |
/* Parse the optional "transient-key" flag. */ |
| 1290 |
if (!(flags & PUBKEY_FLAG_TRANSIENT_KEY)) |
| 1291 |
{ |
| 1292 |
l1 = sexp_find_token (genparms, "transient-key", 0); |
| 1293 |
if (l1) |
| 1294 |
{ |
| 1295 |
flags |= PUBKEY_FLAG_TRANSIENT_KEY; |
| 1296 |
sexp_release (l1); |
| 1297 |
} |
| 1298 |
} |
| 1299 |
deriveparms = (genparms? sexp_find_token (genparms, "test-parms", 0) |
| 1300 |
/**/ : NULL); |
| 1301 |
if (deriveparms) |
| 1302 |
testparms = 1; |
| 1303 |
|
| 1304 |
/* Generate. */ |
| 1305 |
if (deriveparms || fips_mode ()) |
| 1306 |
{ |
| 1307 |
ec = generate_fips (&sk, nbits, evalue, deriveparms, |
| 1308 |
!!(flags & PUBKEY_FLAG_TRANSIENT_KEY)); |
| 1309 |
} |
| 1310 |
else |
| 1311 |
{ |
| 1312 |
ec = generate_std (&sk, nbits, evalue, |
| 1313 |
!!(flags & PUBKEY_FLAG_TRANSIENT_KEY)); |
| 1314 |
} |
| 1315 |
sexp_release (deriveparms); |
| 1316 |
} |
| 1317 |
|
| 1318 |
if (!ec) |
| 1319 |
{ |
| 1320 |
ec = sexp_build (r_skey, NULL, |
| 1321 |
"(key-data" |
| 1322 |
" (public-key" |
| 1323 |
" (rsa(n%m)(e%m)))" |
| 1324 |
" (private-key" |
| 1325 |
" (rsa(n%m)(e%m)(d%m)(p%m)(q%m)(u%m)))" |
| 1326 |
" %S)", |
| 1327 |
sk.n, sk.e, |
| 1328 |
sk.n, sk.e, sk.d, sk.p, sk.q, sk.u, |
| 1329 |
swap_info); |
| 1330 |
} |
| 1331 |
|
| 1332 |
mpi_free (sk.n); |
| 1333 |
mpi_free (sk.e); |
| 1334 |
mpi_free (sk.p); |
| 1335 |
mpi_free (sk.q); |
| 1336 |
mpi_free (sk.d); |
| 1337 |
mpi_free (sk.u); |
| 1338 |
sexp_release (swap_info); |
| 1339 |
|
| 1340 |
if (!ec && !testparms && fips_mode () && test_keys_fips (*r_skey)) |
| 1341 |
{ |
| 1342 |
sexp_release (*r_skey); *r_skey = NULL; |
| 1343 |
fips_signal_error ("self-test after key generation failed"); |
| 1344 |
return GPG_ERR_SELFTEST_FAILED; |
| 1345 |
} |
| 1346 |
|
| 1347 |
return ec; |
| 1348 |
} |
| 1349 |
|
| 1350 |
|
| 1351 |
static gcry_err_code_t |
| 1352 |
rsa_check_secret_key (gcry_sexp_t keyparms) |
| 1353 |
{ |
| 1354 |
gcry_err_code_t rc; |
| 1355 |
RSA_secret_key sk = {NULL, NULL, NULL, NULL, NULL, NULL}; |
| 1356 |
|
| 1357 |
/* To check the key we need the optional parameters. */ |
| 1358 |
rc = sexp_extract_param (keyparms, NULL, "nedpqu", |
| 1359 |
&sk.n, &sk.e, &sk.d, &sk.p, &sk.q, &sk.u, |
| 1360 |
NULL); |
| 1361 |
if (rc) |
| 1362 |
goto leave; |
| 1363 |
|
| 1364 |
if (!check_secret_key (&sk)) |
| 1365 |
rc = GPG_ERR_BAD_SECKEY; |
| 1366 |
|
| 1367 |
leave: |
| 1368 |
_gcry_mpi_release (sk.n); |
| 1369 |
_gcry_mpi_release (sk.e); |
| 1370 |
_gcry_mpi_release (sk.d); |
| 1371 |
_gcry_mpi_release (sk.p); |
| 1372 |
_gcry_mpi_release (sk.q); |
| 1373 |
_gcry_mpi_release (sk.u); |
| 1374 |
if (DBG_CIPHER) |
| 1375 |
log_debug ("rsa_testkey => %s\n", gpg_strerror (rc)); |
| 1376 |
return rc; |
| 1377 |
} |
| 1378 |
|
| 1379 |
|
| 1380 |
static gcry_err_code_t |
| 1381 |
rsa_encrypt (gcry_sexp_t *r_ciph, gcry_sexp_t s_data, gcry_sexp_t keyparms) |
| 1382 |
{ |
| 1383 |
gcry_err_code_t rc; |
| 1384 |
struct pk_encoding_ctx ctx; |
| 1385 |
gcry_mpi_t data = NULL; |
| 1386 |
RSA_public_key pk = {NULL, NULL}; |
| 1387 |
gcry_mpi_t ciph = NULL; |
| 1388 |
unsigned int nbits = rsa_get_nbits (keyparms); |
| 1389 |
|
| 1390 |
rc = rsa_check_keysize (nbits); |
| 1391 |
if (rc) |
| 1392 |
return rc; |
| 1393 |
|
| 1394 |
_gcry_pk_util_init_encoding_ctx (&ctx, PUBKEY_OP_ENCRYPT, nbits); |
| 1395 |
|
| 1396 |
/* Extract the data. */ |
| 1397 |
rc = _gcry_pk_util_data_to_mpi (s_data, &data, &ctx); |
| 1398 |
if (rc) |
| 1399 |
goto leave; |
| 1400 |
if (DBG_CIPHER) |
| 1401 |
log_mpidump ("rsa_encrypt data", data); |
| 1402 |
if (!data || mpi_is_opaque (data)) |
| 1403 |
{ |
| 1404 |
rc = GPG_ERR_INV_DATA; |
| 1405 |
goto leave; |
| 1406 |
} |
| 1407 |
|
| 1408 |
/* Extract the key. */ |
| 1409 |
rc = sexp_extract_param (keyparms, NULL, "ne", &pk.n, &pk.e, NULL); |
| 1410 |
if (rc) |
| 1411 |
goto leave; |
| 1412 |
if (DBG_CIPHER) |
| 1413 |
{ |
| 1414 |
log_mpidump ("rsa_encrypt n", pk.n); |
| 1415 |
log_mpidump ("rsa_encrypt e", pk.e); |
| 1416 |
} |
| 1417 |
|
| 1418 |
/* Do RSA computation and build result. */ |
| 1419 |
ciph = mpi_new (0); |
| 1420 |
public (ciph, data, &pk); |
| 1421 |
if (DBG_CIPHER) |
| 1422 |
log_mpidump ("rsa_encrypt res", ciph); |
| 1423 |
if ((ctx.flags & PUBKEY_FLAG_FIXEDLEN)) |
| 1424 |
{ |
| 1425 |
/* We need to make sure to return the correct length to avoid |
| 1426 |
problems with missing leading zeroes. */ |
| 1427 |
unsigned char *em; |
| 1428 |
size_t emlen = (mpi_get_nbits (pk.n)+7)/8; |
| 1429 |
|
| 1430 |
rc = _gcry_mpi_to_octet_string (&em, NULL, ciph, emlen); |
| 1431 |
if (!rc) |
| 1432 |
{ |
| 1433 |
rc = sexp_build (r_ciph, NULL, "(enc-val(rsa(a%b)))", (int)emlen, em); |
| 1434 |
xfree (em); |
| 1435 |
} |
| 1436 |
} |
| 1437 |
else |
| 1438 |
rc = sexp_build (r_ciph, NULL, "(enc-val(rsa(a%m)))", ciph); |
| 1439 |
|
| 1440 |
leave: |
| 1441 |
_gcry_mpi_release (ciph); |
| 1442 |
_gcry_mpi_release (pk.n); |
| 1443 |
_gcry_mpi_release (pk.e); |
| 1444 |
_gcry_mpi_release (data); |
| 1445 |
_gcry_pk_util_free_encoding_ctx (&ctx); |
| 1446 |
if (DBG_CIPHER) |
| 1447 |
log_debug ("rsa_encrypt => %s\n", gpg_strerror (rc)); |
| 1448 |
return rc; |
| 1449 |
} |
| 1450 |
|
| 1451 |
|
| 1452 |
static gcry_err_code_t |
| 1453 |
rsa_decrypt (gcry_sexp_t *r_plain, gcry_sexp_t s_data, gcry_sexp_t keyparms) |
| 1454 |
|
| 1455 |
{ |
| 1456 |
gpg_err_code_t rc, rc_sexp; |
| 1457 |
struct pk_encoding_ctx ctx; |
| 1458 |
gcry_sexp_t l1 = NULL; |
| 1459 |
gcry_mpi_t data = NULL; |
| 1460 |
RSA_secret_key sk = {NULL, NULL, NULL, NULL, NULL, NULL}; |
| 1461 |
gcry_mpi_t plain = NULL; |
| 1462 |
unsigned char *unpad = NULL; |
| 1463 |
size_t unpadlen = 0; |
| 1464 |
unsigned int nbits = rsa_get_nbits (keyparms); |
| 1465 |
gcry_sexp_t result = NULL; |
| 1466 |
gcry_sexp_t dummy = NULL; |
| 1467 |
|
| 1468 |
rc = rsa_check_keysize (nbits); |
| 1469 |
if (rc) |
| 1470 |
return rc; |
| 1471 |
|
| 1472 |
_gcry_pk_util_init_encoding_ctx (&ctx, PUBKEY_OP_DECRYPT, nbits); |
| 1473 |
|
| 1474 |
/* Extract the data. */ |
| 1475 |
rc = _gcry_pk_util_preparse_encval (s_data, rsa_names, &l1, &ctx); |
| 1476 |
if (rc) |
| 1477 |
goto leave; |
| 1478 |
rc = sexp_extract_param (l1, NULL, "a", &data, NULL); |
| 1479 |
if (rc) |
| 1480 |
goto leave; |
| 1481 |
if (DBG_CIPHER) |
| 1482 |
log_printmpi ("rsa_decrypt data", data); |
| 1483 |
if (mpi_is_opaque (data)) |
| 1484 |
{ |
| 1485 |
rc = GPG_ERR_INV_DATA; |
| 1486 |
goto leave; |
| 1487 |
} |
| 1488 |
|
| 1489 |
/* Extract the key. */ |
| 1490 |
rc = sexp_extract_param (keyparms, NULL, "nedp?q?u?", |
| 1491 |
&sk.n, &sk.e, &sk.d, &sk.p, &sk.q, &sk.u, |
| 1492 |
NULL); |
| 1493 |
if (rc) |
| 1494 |
goto leave; |
| 1495 |
if (DBG_CIPHER) |
| 1496 |
{ |
| 1497 |
log_printmpi ("rsa_decrypt n", sk.n); |
| 1498 |
log_printmpi ("rsa_decrypt e", sk.e); |
| 1499 |
if (!fips_mode ()) |
| 1500 |
{ |
| 1501 |
log_printmpi ("rsa_decrypt d", sk.d); |
| 1502 |
log_printmpi ("rsa_decrypt p", sk.p); |
| 1503 |
log_printmpi ("rsa_decrypt q", sk.q); |
| 1504 |
log_printmpi ("rsa_decrypt u", sk.u); |
| 1505 |
} |
| 1506 |
} |
| 1507 |
|
| 1508 |
/* Better make sure that there are no superfluous leading zeroes in |
| 1509 |
the input and it has not been "padded" using multiples of N. |
| 1510 |
This mitigates side-channel attacks (CVE-2013-4576). */ |
| 1511 |
mpi_normalize (data); |
| 1512 |
mpi_fdiv_r (data, data, sk.n); |
| 1513 |
|
| 1514 |
/* Allocate MPI for the plaintext. */ |
| 1515 |
plain = mpi_snew (nbits); |
| 1516 |
|
| 1517 |
/* We use blinding by default to mitigate timing attacks which can |
| 1518 |
be practically mounted over the network as shown by Brumley and |
| 1519 |
Boney in 2003. */ |
| 1520 |
if ((ctx.flags & PUBKEY_FLAG_NO_BLINDING)) |
| 1521 |
{ |
| 1522 |
if (fips_mode ()) |
| 1523 |
{ |
| 1524 |
if (fips_check_rejection (GCRY_FIPS_FLAG_REJECT_PK_FLAGS)) |
| 1525 |
{ |
| 1526 |
rc = GPG_ERR_INV_FLAG; |
| 1527 |
goto leave; |
| 1528 |
} |
| 1529 |
else |
| 1530 |
fips_service_indicator_mark_non_compliant (); |
| 1531 |
} |
| 1532 |
secret (plain, data, &sk); |
| 1533 |
} |
| 1534 |
else |
| 1535 |
secret_blinded (plain, data, &sk, nbits); |
| 1536 |
|
| 1537 |
if (DBG_CIPHER) |
| 1538 |
log_printmpi ("rsa_decrypt res", plain); |
| 1539 |
|
| 1540 |
/* Reverse the encoding and build the s-expression. */ |
| 1541 |
switch (ctx.encoding) |
| 1542 |
{ |
| 1543 |
case PUBKEY_ENC_PKCS1: |
| 1544 |
rc = _gcry_rsa_pkcs1_decode_for_enc (&unpad, &unpadlen, nbits, plain); |
| 1545 |
mpi_free (plain); |
| 1546 |
plain = NULL; |
| 1547 |
rc_sexp = sexp_build (&result, NULL, "(value %b)", (int)unpadlen, unpad); |
| 1548 |
*r_plain = sexp_null_cond (result, ct_is_not_zero (rc)); |
| 1549 |
dummy = sexp_null_cond (result, ct_is_zero (rc)); |
| 1550 |
sexp_release (dummy); |
| 1551 |
rc = ct_ulong_select (rc_sexp, rc, |
| 1552 |
ct_is_zero (rc) & ct_is_not_zero (rc_sexp)); |
| 1553 |
break; |
| 1554 |
|
| 1555 |
case PUBKEY_ENC_OAEP: |
| 1556 |
rc = _gcry_rsa_oaep_decode (&unpad, &unpadlen, |
| 1557 |
nbits, ctx.hash_algo, |
| 1558 |
plain, ctx.label, ctx.labellen); |
| 1559 |
mpi_free (plain); |
| 1560 |
plain = NULL; |
| 1561 |
rc_sexp = sexp_build (&result, NULL, "(value %b)", (int)unpadlen, unpad); |
| 1562 |
*r_plain = sexp_null_cond (result, ct_is_not_zero (rc)); |
| 1563 |
dummy = sexp_null_cond (result, ct_is_zero (rc)); |
| 1564 |
sexp_release (dummy); |
| 1565 |
rc = ct_ulong_select (rc_sexp, rc, |
| 1566 |
ct_is_zero (rc) & ct_is_not_zero (rc_sexp)); |
| 1567 |
break; |
| 1568 |
|
| 1569 |
default: |
| 1570 |
/* Raw format. For backward compatibility we need to assume a |
| 1571 |
signed mpi by using the sexp format string "%m". */ |
| 1572 |
rc = sexp_build (r_plain, NULL, |
| 1573 |
(ctx.flags & PUBKEY_FLAG_LEGACYRESULT) |
| 1574 |
? "%m":"(value %m)", plain); |
| 1575 |
break; |
| 1576 |
} |
| 1577 |
|
| 1578 |
leave: |
| 1579 |
xfree (unpad); |
| 1580 |
_gcry_mpi_release (plain); |
| 1581 |
_gcry_mpi_release (sk.n); |
| 1582 |
_gcry_mpi_release (sk.e); |
| 1583 |
_gcry_mpi_release (sk.d); |
| 1584 |
_gcry_mpi_release (sk.p); |
| 1585 |
_gcry_mpi_release (sk.q); |
| 1586 |
_gcry_mpi_release (sk.u); |
| 1587 |
_gcry_mpi_release (data); |
| 1588 |
sexp_release (l1); |
| 1589 |
_gcry_pk_util_free_encoding_ctx (&ctx); |
| 1590 |
if (DBG_CIPHER) |
| 1591 |
log_debug ("rsa_decrypt => %s\n", gpg_strerror (rc)); |
| 1592 |
return rc; |
| 1593 |
} |
| 1594 |
|
| 1595 |
|
| 1596 |
static gcry_err_code_t |
| 1597 |
rsa_sign (gcry_sexp_t *r_sig, gcry_sexp_t s_data, gcry_sexp_t keyparms) |
| 1598 |
{ |
| 1599 |
gpg_err_code_t rc; |
| 1600 |
struct pk_encoding_ctx ctx; |
| 1601 |
gcry_mpi_t data = NULL; |
| 1602 |
RSA_secret_key sk = {NULL, NULL, NULL, NULL, NULL, NULL}; |
| 1603 |
RSA_public_key pk; |
| 1604 |
gcry_mpi_t sig = NULL; |
| 1605 |
gcry_mpi_t result = NULL; |
| 1606 |
unsigned int nbits = rsa_get_nbits (keyparms); |
| 1607 |
|
| 1608 |
rc = rsa_check_keysize (nbits); |
| 1609 |
if (rc) |
| 1610 |
return rc; |
| 1611 |
|
| 1612 |
_gcry_pk_util_init_encoding_ctx (&ctx, PUBKEY_OP_SIGN, nbits); |
| 1613 |
|
| 1614 |
/* Extract the data. */ |
| 1615 |
rc = _gcry_pk_util_data_to_mpi (s_data, &data, &ctx); |
| 1616 |
if (rc) |
| 1617 |
goto leave; |
| 1618 |
if (DBG_CIPHER) |
| 1619 |
log_printmpi ("rsa_sign data", data); |
| 1620 |
if (mpi_is_opaque (data)) |
| 1621 |
{ |
| 1622 |
rc = GPG_ERR_INV_DATA; |
| 1623 |
goto leave; |
| 1624 |
} |
| 1625 |
|
| 1626 |
/* Extract the key. */ |
| 1627 |
rc = sexp_extract_param (keyparms, NULL, "nedp?q?u?", |
| 1628 |
&sk.n, &sk.e, &sk.d, &sk.p, &sk.q, &sk.u, |
| 1629 |
NULL); |
| 1630 |
if (rc) |
| 1631 |
goto leave; |
| 1632 |
if (DBG_CIPHER) |
| 1633 |
{ |
| 1634 |
log_printmpi ("rsa_sign n", sk.n); |
| 1635 |
log_printmpi ("rsa_sign e", sk.e); |
| 1636 |
if (!fips_mode ()) |
| 1637 |
{ |
| 1638 |
log_printmpi ("rsa_sign d", sk.d); |
| 1639 |
log_printmpi ("rsa_sign p", sk.p); |
| 1640 |
log_printmpi ("rsa_sign q", sk.q); |
| 1641 |
log_printmpi ("rsa_sign u", sk.u); |
| 1642 |
} |
| 1643 |
} |
| 1644 |
|
| 1645 |
/* Check if use of the hash is compliant. */ |
| 1646 |
if (fips_mode ()) |
| 1647 |
{ |
| 1648 |
/* SHA1 is approved hash function, but not for digital signature. */ |
| 1649 |
if (_gcry_md_algo_info (ctx.hash_algo, GCRYCTL_TEST_ALGO, NULL, NULL) |
| 1650 |
|| ctx.hash_algo == GCRY_MD_SHA1) |
| 1651 |
{ |
| 1652 |
if (fips_check_rejection (GCRY_FIPS_FLAG_REJECT_PK_MD)) |
| 1653 |
{ |
| 1654 |
rc = GPG_ERR_DIGEST_ALGO; |
| 1655 |
goto leave; |
| 1656 |
} |
| 1657 |
else |
| 1658 |
fips_service_indicator_mark_non_compliant (); |
| 1659 |
} |
| 1660 |
} |
| 1661 |
|
| 1662 |
/* Do RSA computation. */ |
| 1663 |
sig = mpi_new (0); |
| 1664 |
|
| 1665 |
if ((ctx.flags & PUBKEY_FLAG_NO_BLINDING)) |
| 1666 |
{ |
| 1667 |
if (fips_mode ()) |
| 1668 |
{ |
| 1669 |
if (fips_check_rejection (GCRY_FIPS_FLAG_REJECT_PK_FLAGS)) |
| 1670 |
{ |
| 1671 |
rc = GPG_ERR_INV_FLAG; |
| 1672 |
goto leave; |
| 1673 |
} |
| 1674 |
else |
| 1675 |
fips_service_indicator_mark_non_compliant (); |
| 1676 |
} |
| 1677 |
|
| 1678 |
secret (sig, data, &sk); |
| 1679 |
} |
| 1680 |
else |
| 1681 |
secret_blinded (sig, data, &sk, nbits); |
| 1682 |
if (DBG_CIPHER) |
| 1683 |
log_printmpi ("rsa_sign res", sig); |
| 1684 |
|
| 1685 |
/* Check that the created signature is good. This detects a failure |
| 1686 |
of the CRT algorithm (Lenstra's attack on RSA's use of the CRT). */ |
| 1687 |
result = mpi_new (0); |
| 1688 |
pk.n = sk.n; |
| 1689 |
pk.e = sk.e; |
| 1690 |
public (result, sig, &pk); |
| 1691 |
if (mpi_cmp (result, data)) |
| 1692 |
{ |
| 1693 |
rc = GPG_ERR_BAD_SIGNATURE; |
| 1694 |
goto leave; |
| 1695 |
} |
| 1696 |
|
| 1697 |
/* Convert the result. */ |
| 1698 |
if ((ctx.flags & PUBKEY_FLAG_FIXEDLEN)) |
| 1699 |
{ |
| 1700 |
/* We need to make sure to return the correct length to avoid |
| 1701 |
problems with missing leading zeroes. */ |
| 1702 |
unsigned char *em; |
| 1703 |
size_t emlen = (mpi_get_nbits (sk.n)+7)/8; |
| 1704 |
|
| 1705 |
rc = _gcry_mpi_to_octet_string (&em, NULL, sig, emlen); |
| 1706 |
if (!rc) |
| 1707 |
{ |
| 1708 |
rc = sexp_build (r_sig, NULL, "(sig-val(rsa(s%b)))", (int)emlen, em); |
| 1709 |
xfree (em); |
| 1710 |
} |
| 1711 |
} |
| 1712 |
else |
| 1713 |
rc = sexp_build (r_sig, NULL, "(sig-val(rsa(s%M)))", sig); |
| 1714 |
|
| 1715 |
|
| 1716 |
leave: |
| 1717 |
_gcry_mpi_release (result); |
| 1718 |
_gcry_mpi_release (sig); |
| 1719 |
_gcry_mpi_release (sk.n); |
| 1720 |
_gcry_mpi_release (sk.e); |
| 1721 |
_gcry_mpi_release (sk.d); |
| 1722 |
_gcry_mpi_release (sk.p); |
| 1723 |
_gcry_mpi_release (sk.q); |
| 1724 |
_gcry_mpi_release (sk.u); |
| 1725 |
_gcry_mpi_release (data); |
| 1726 |
_gcry_pk_util_free_encoding_ctx (&ctx); |
| 1727 |
if (DBG_CIPHER) |
| 1728 |
log_debug ("rsa_sign => %s\n", gpg_strerror (rc)); |
| 1729 |
return rc; |
| 1730 |
} |
| 1731 |
|
| 1732 |
|
| 1733 |
static gcry_err_code_t |
| 1734 |
rsa_verify (gcry_sexp_t s_sig, gcry_sexp_t s_data, gcry_sexp_t keyparms) |
| 1735 |
{ |
| 1736 |
gcry_err_code_t rc; |
| 1737 |
struct pk_encoding_ctx ctx; |
| 1738 |
gcry_sexp_t l1 = NULL; |
| 1739 |
gcry_mpi_t sig = NULL; |
| 1740 |
gcry_mpi_t data = NULL; |
| 1741 |
RSA_public_key pk = { NULL, NULL }; |
| 1742 |
gcry_mpi_t result = NULL; |
| 1743 |
unsigned int nbits = rsa_get_nbits (keyparms); |
| 1744 |
|
| 1745 |
rc = rsa_check_verify_keysize (nbits); |
| 1746 |
if (rc) |
| 1747 |
return rc; |
| 1748 |
|
| 1749 |
_gcry_pk_util_init_encoding_ctx (&ctx, PUBKEY_OP_VERIFY, nbits); |
| 1750 |
|
| 1751 |
/* Extract the data. */ |
| 1752 |
rc = _gcry_pk_util_data_to_mpi (s_data, &data, &ctx); |
| 1753 |
if (rc) |
| 1754 |
goto leave; |
| 1755 |
if (DBG_CIPHER) |
| 1756 |
log_printmpi ("rsa_verify data", data); |
| 1757 |
if (ctx.encoding != PUBKEY_ENC_PSS && mpi_is_opaque (data)) |
| 1758 |
{ |
| 1759 |
rc = GPG_ERR_INV_DATA; |
| 1760 |
goto leave; |
| 1761 |
} |
| 1762 |
|
| 1763 |
/* Extract the signature value. */ |
| 1764 |
rc = _gcry_pk_util_preparse_sigval (s_sig, rsa_names, &l1, NULL); |
| 1765 |
if (rc) |
| 1766 |
goto leave; |
| 1767 |
rc = sexp_extract_param (l1, NULL, "s", &sig, NULL); |
| 1768 |
if (rc) |
| 1769 |
goto leave; |
| 1770 |
if (DBG_CIPHER) |
| 1771 |
log_printmpi ("rsa_verify sig", sig); |
| 1772 |
|
| 1773 |
/* Extract the key. */ |
| 1774 |
rc = sexp_extract_param (keyparms, NULL, "ne", &pk.n, &pk.e, NULL); |
| 1775 |
if (rc) |
| 1776 |
goto leave; |
| 1777 |
if (DBG_CIPHER) |
| 1778 |
{ |
| 1779 |
log_printmpi ("rsa_verify n", pk.n); |
| 1780 |
log_printmpi ("rsa_verify e", pk.e); |
| 1781 |
} |
| 1782 |
|
| 1783 |
/* Check if use of the hash is compliant. */ |
| 1784 |
if (fips_mode ()) |
| 1785 |
{ |
| 1786 |
/* SHA1 is approved hash function, but not for digital signature. */ |
| 1787 |
if (_gcry_md_algo_info (ctx.hash_algo, GCRYCTL_TEST_ALGO, NULL, NULL) |
| 1788 |
|| ctx.hash_algo == GCRY_MD_SHA1) |
| 1789 |
{ |
| 1790 |
if (fips_check_rejection (GCRY_FIPS_FLAG_REJECT_PK_MD)) |
| 1791 |
{ |
| 1792 |
rc = GPG_ERR_DIGEST_ALGO; |
| 1793 |
goto leave; |
| 1794 |
} |
| 1795 |
else |
| 1796 |
fips_service_indicator_mark_non_compliant (); |
| 1797 |
} |
| 1798 |
} |
| 1799 |
|
| 1800 |
/* Do RSA computation and compare. */ |
| 1801 |
result = mpi_new (0); |
| 1802 |
public (result, sig, &pk); |
| 1803 |
if (DBG_CIPHER) |
| 1804 |
log_printmpi ("rsa_verify cmp", result); |
| 1805 |
if (ctx.verify_cmp) |
| 1806 |
rc = ctx.verify_cmp (&ctx, result); |
| 1807 |
else |
| 1808 |
rc = mpi_cmp (result, data) ? GPG_ERR_BAD_SIGNATURE : 0; |
| 1809 |
|
| 1810 |
leave: |
| 1811 |
_gcry_mpi_release (result); |
| 1812 |
_gcry_mpi_release (pk.n); |
| 1813 |
_gcry_mpi_release (pk.e); |
| 1814 |
_gcry_mpi_release (data); |
| 1815 |
_gcry_mpi_release (sig); |
| 1816 |
sexp_release (l1); |
| 1817 |
_gcry_pk_util_free_encoding_ctx (&ctx); |
| 1818 |
if (DBG_CIPHER) |
| 1819 |
log_debug ("rsa_verify => %s\n", rc?gpg_strerror (rc):"Good"); |
| 1820 |
return rc; |
| 1821 |
} |
| 1822 |
|
| 1823 |
|
| 1824 |
|
| 1825 |
/* Return the number of bits for the key described by PARMS. On error |
| 1826 |
* 0 is returned. The format of PARMS starts with the algorithm name; |
| 1827 |
* for example: |
| 1828 |
* |
| 1829 |
* (rsa |
| 1830 |
* (n <mpi>) |
| 1831 |
* (e <mpi>)) |
| 1832 |
* |
| 1833 |
* More parameters may be given but we only need N here. |
| 1834 |
*/ |
| 1835 |
static unsigned int |
| 1836 |
rsa_get_nbits (gcry_sexp_t parms) |
| 1837 |
{ |
| 1838 |
gcry_sexp_t l1; |
| 1839 |
gcry_mpi_t n; |
| 1840 |
unsigned int nbits; |
| 1841 |
|
| 1842 |
l1 = sexp_find_token (parms, "n", 1); |
| 1843 |
if (!l1) |
| 1844 |
return 0; /* Parameter N not found. */ |
| 1845 |
|
| 1846 |
n = sexp_nth_mpi (l1, 1, GCRYMPI_FMT_USG); |
| 1847 |
sexp_release (l1); |
| 1848 |
nbits = n? mpi_get_nbits (n) : 0; |
| 1849 |
_gcry_mpi_release (n); |
| 1850 |
return nbits; |
| 1851 |
} |
| 1852 |
|
| 1853 |
|
| 1854 |
/* Compute a keygrip. MD is the hash context which we are going to |
| 1855 |
update. KEYPARAM is an S-expression with the key parameters, this |
| 1856 |
is usually a public key but may also be a secret key. An example |
| 1857 |
of such an S-expression is: |
| 1858 |
|
| 1859 |
(rsa |
| 1860 |
(n #00B...#) |
| 1861 |
(e #010001#)) |
| 1862 |
|
| 1863 |
PKCS-15 says that for RSA only the modulus should be hashed - |
| 1864 |
however, it is not clear whether this is meant to use the raw bytes |
| 1865 |
(assuming this is an unsigned integer) or whether the DER required |
| 1866 |
0 should be prefixed. We hash the raw bytes. */ |
| 1867 |
static gpg_err_code_t |
| 1868 |
compute_keygrip (gcry_md_hd_t md, gcry_sexp_t keyparam) |
| 1869 |
{ |
| 1870 |
gcry_sexp_t l1; |
| 1871 |
const char *data; |
| 1872 |
size_t datalen; |
| 1873 |
|
| 1874 |
l1 = sexp_find_token (keyparam, "n", 1); |
| 1875 |
if (!l1) |
| 1876 |
return GPG_ERR_NO_OBJ; |
| 1877 |
|
| 1878 |
data = sexp_nth_data (l1, 1, &datalen); |
| 1879 |
if (!data) |
| 1880 |
{ |
| 1881 |
sexp_release (l1); |
| 1882 |
return GPG_ERR_NO_OBJ; |
| 1883 |
} |
| 1884 |
|
| 1885 |
_gcry_md_write (md, data, datalen); |
| 1886 |
sexp_release (l1); |
| 1887 |
|
| 1888 |
return 0; |
| 1889 |
} |
| 1890 |
|
| 1891 |
|
| 1892 |
|
| 1893 |
|
| 1894 |
/* |
| 1895 |
Self-test section. |
| 1896 |
*/ |
| 1897 |
|
| 1898 |
static const char * |
| 1899 |
selftest_hash_sign_2048 (gcry_sexp_t pkey, gcry_sexp_t skey) |
| 1900 |
{ |
| 1901 |
int md_algo = GCRY_MD_SHA256; |
| 1902 |
gcry_md_hd_t hd = NULL; |
| 1903 |
const char *data_tmpl = "(data (flags pkcs1) (hash %s %b))"; |
| 1904 |
static const char sample_data[] = |
| 1905 |
"11223344556677889900aabbccddeeff" |
| 1906 |
"102030405060708090a0b0c0d0f01121"; |
| 1907 |
static const char sample_data_bad[] = |
| 1908 |
"11223344556677889900aabbccddeeff" |
| 1909 |
"802030405060708090a0b0c0d0f01121"; |
| 1910 |
|
| 1911 |
const char *errtxt = NULL; |
| 1912 |
gcry_error_t err; |
| 1913 |
gcry_sexp_t sig = NULL; |
| 1914 |
/* raw signature data reference */ |
| 1915 |
const char ref_data[] = |
| 1916 |
"518f41dea3ad884e93eefff8d7ca68a6f4c30d923632e35673651d675cebd652" |
| 1917 |
"a44ed66f6879b18f3d48b2d235b1dd78f6189be1440352cc94231a55c1f93109" |
| 1918 |
"84616b2841c42fe9a6e37be34cd188207209bd028e2fa93e721fbac40c31a068" |
| 1919 |
"1253b312d4e07addb9c7f3d508fa89f218ea7c7f7b9f6a9b1e522c19fa1cd839" |
| 1920 |
"93f9d4ca2f16c3d0b9abafe5e63e848152afc72ce7ee19ea45353116f85209ea" |
| 1921 |
"b9de42129dbccdac8faa461e8e8cc2ae801101cc6add4ba76ccb752030b0e827" |
| 1922 |
"7352b11cdecebae9cdc9a626c4701cd9c85cd287618888c5fae8b4d0ba48915d" |
| 1923 |
"e5cc64e3aee2ba2862d04348ea71f65454f74f9fd1e3108005cc367ca41585a4"; |
| 1924 |
gcry_mpi_t ref_mpi = NULL; |
| 1925 |
gcry_mpi_t sig_mpi = NULL; |
| 1926 |
|
| 1927 |
err = _gcry_md_open (&hd, md_algo, 0); |
| 1928 |
if (err) |
| 1929 |
{ |
| 1930 |
errtxt = "gcry_md_open failed"; |
| 1931 |
goto leave; |
| 1932 |
} |
| 1933 |
|
| 1934 |
_gcry_md_write (hd, sample_data, sizeof(sample_data)); |
| 1935 |
|
| 1936 |
err = _gcry_pk_sign_md (&sig, data_tmpl, hd, skey, NULL); |
| 1937 |
if (err) |
| 1938 |
{ |
| 1939 |
errtxt = "signing failed"; |
| 1940 |
goto leave; |
| 1941 |
} |
| 1942 |
|
| 1943 |
err = _gcry_mpi_scan(&ref_mpi, GCRYMPI_FMT_HEX, ref_data, 0, NULL); |
| 1944 |
if (err) |
| 1945 |
{ |
| 1946 |
errtxt = "converting ref_data to mpi failed"; |
| 1947 |
goto leave; |
| 1948 |
} |
| 1949 |
|
| 1950 |
err = _gcry_sexp_extract_param(sig, "sig-val!rsa", "s", &sig_mpi, NULL); |
| 1951 |
if (err) |
| 1952 |
{ |
| 1953 |
errtxt = "extracting signature data failed"; |
| 1954 |
goto leave; |
| 1955 |
} |
| 1956 |
|
| 1957 |
if (mpi_cmp (sig_mpi, ref_mpi)) |
| 1958 |
{ |
| 1959 |
errtxt = "signature does not match reference data"; |
| 1960 |
goto leave; |
| 1961 |
} |
| 1962 |
|
| 1963 |
err = _gcry_pk_verify_md (sig, data_tmpl, hd, pkey, NULL); |
| 1964 |
if (err) |
| 1965 |
{ |
| 1966 |
errtxt = "verify failed"; |
| 1967 |
goto leave; |
| 1968 |
} |
| 1969 |
|
| 1970 |
_gcry_md_reset(hd); |
| 1971 |
_gcry_md_write (hd, sample_data_bad, sizeof(sample_data_bad)); |
| 1972 |
err = _gcry_pk_verify_md (sig, data_tmpl, hd, pkey, NULL); |
| 1973 |
if (gcry_err_code (err) != GPG_ERR_BAD_SIGNATURE) |
| 1974 |
{ |
| 1975 |
errtxt = "bad signature not detected"; |
| 1976 |
goto leave; |
| 1977 |
} |
| 1978 |
|
| 1979 |
|
| 1980 |
leave: |
| 1981 |
sexp_release (sig); |
| 1982 |
_gcry_md_close (hd); |
| 1983 |
_gcry_mpi_release (ref_mpi); |
| 1984 |
_gcry_mpi_release (sig_mpi); |
| 1985 |
return errtxt; |
| 1986 |
} |
| 1987 |
|
| 1988 |
static const char * |
| 1989 |
selftest_sign_2048 (gcry_sexp_t pkey, gcry_sexp_t skey) |
| 1990 |
{ |
| 1991 |
static const char sample_data[] = |
| 1992 |
"(data (flags pkcs1)" |
| 1993 |
" (hash sha256 #11223344556677889900aabbccddeeff" |
| 1994 |
/**/ "102030405060708090a0b0c0d0f01121#))"; |
| 1995 |
static const char sample_data_bad[] = |
| 1996 |
"(data (flags pkcs1)" |
| 1997 |
" (hash sha256 #11223344556677889900aabbccddeeff" |
| 1998 |
/**/ "802030405060708090a0b0c0d0f01121#))"; |
| 1999 |
|
| 2000 |
const char *errtxt = NULL; |
| 2001 |
gcry_error_t err; |
| 2002 |
gcry_sexp_t data = NULL; |
| 2003 |
gcry_sexp_t data_bad = NULL; |
| 2004 |
gcry_sexp_t sig = NULL; |
| 2005 |
/* raw signature data reference */ |
| 2006 |
const char ref_data[] = |
| 2007 |
"6252a19a11e1d5155ed9376036277193d644fa239397fff03e9b92d6f86415d6" |
| 2008 |
"d30da9273775f290e580d038295ff8ff89522becccfa6ae870bf76b76df402a8" |
| 2009 |
"54f69347e3db3de8e1e7d4dada281ec556810c7a8ecd0b5f51f9b1c0e7aa7557" |
| 2010 |
"61aa2b8ba5f811304acc6af0eca41fe49baf33bf34eddaf44e21e036ac7f0b68" |
| 2011 |
"03cdef1c60021fb7b5b97ebacdd88ab755ce29af568dbc5728cc6e6eff42618d" |
| 2012 |
"62a0386ca8beed46402bdeeef29b6a3feded906bace411a06a39192bf516ae10" |
| 2013 |
"67e4320fa8ea113968525f4574d022a3ceeaafdc41079efe1f22cc94bf59d8d3" |
| 2014 |
"328085da9674857db56de5978a62394aab48aa3b72e23a1b16260cfd9daafe65"; |
| 2015 |
gcry_mpi_t ref_mpi = NULL; |
| 2016 |
gcry_mpi_t sig_mpi = NULL; |
| 2017 |
|
| 2018 |
err = sexp_sscan (&data, NULL, sample_data, strlen (sample_data)); |
| 2019 |
if (!err) |
| 2020 |
err = sexp_sscan (&data_bad, NULL, |
| 2021 |
sample_data_bad, strlen (sample_data_bad)); |
| 2022 |
if (err) |
| 2023 |
{ |
| 2024 |
errtxt = "converting data failed"; |
| 2025 |
goto leave; |
| 2026 |
} |
| 2027 |
|
| 2028 |
err = _gcry_pk_sign (&sig, data, skey); |
| 2029 |
if (err) |
| 2030 |
{ |
| 2031 |
errtxt = "signing failed"; |
| 2032 |
goto leave; |
| 2033 |
} |
| 2034 |
|
| 2035 |
err = _gcry_mpi_scan(&ref_mpi, GCRYMPI_FMT_HEX, ref_data, 0, NULL); |
| 2036 |
if (err) |
| 2037 |
{ |
| 2038 |
errtxt = "converting ref_data to mpi failed"; |
| 2039 |
goto leave; |
| 2040 |
} |
| 2041 |
|
| 2042 |
err = _gcry_sexp_extract_param(sig, "sig-val!rsa", "s", &sig_mpi, NULL); |
| 2043 |
if (err) |
| 2044 |
{ |
| 2045 |
errtxt = "extracting signature data failed"; |
| 2046 |
goto leave; |
| 2047 |
} |
| 2048 |
|
| 2049 |
if (mpi_cmp (sig_mpi, ref_mpi)) |
| 2050 |
{ |
| 2051 |
errtxt = "signature does not match reference data"; |
| 2052 |
goto leave; |
| 2053 |
} |
| 2054 |
|
| 2055 |
err = _gcry_pk_verify (sig, data, pkey); |
| 2056 |
if (err) |
| 2057 |
{ |
| 2058 |
errtxt = "verify failed"; |
| 2059 |
goto leave; |
| 2060 |
} |
| 2061 |
err = _gcry_pk_verify (sig, data_bad, pkey); |
| 2062 |
if (gcry_err_code (err) != GPG_ERR_BAD_SIGNATURE) |
| 2063 |
{ |
| 2064 |
errtxt = "bad signature not detected"; |
| 2065 |
goto leave; |
| 2066 |
} |
| 2067 |
|
| 2068 |
|
| 2069 |
leave: |
| 2070 |
sexp_release (sig); |
| 2071 |
sexp_release (data_bad); |
| 2072 |
sexp_release (data); |
| 2073 |
_gcry_mpi_release (ref_mpi); |
| 2074 |
_gcry_mpi_release (sig_mpi); |
| 2075 |
return errtxt; |
| 2076 |
} |
| 2077 |
|
| 2078 |
|
| 2079 |
|
| 2080 |
/* Given an S-expression ENCR_DATA of the form: |
| 2081 |
|
| 2082 |
(enc-val |
| 2083 |
(rsa |
| 2084 |
(a a-value))) |
| 2085 |
|
| 2086 |
as returned by gcry_pk_decrypt, return the the A-VALUE. On error, |
| 2087 |
return NULL. */ |
| 2088 |
static gcry_mpi_t |
| 2089 |
extract_a_from_sexp (gcry_sexp_t encr_data) |
| 2090 |
{ |
| 2091 |
gcry_sexp_t l1, l2, l3; |
| 2092 |
gcry_mpi_t a_value; |
| 2093 |
|
| 2094 |
l1 = sexp_find_token (encr_data, "enc-val", 0); |
| 2095 |
if (!l1) |
| 2096 |
return NULL; |
| 2097 |
l2 = sexp_find_token (l1, "rsa", 0); |
| 2098 |
sexp_release (l1); |
| 2099 |
if (!l2) |
| 2100 |
return NULL; |
| 2101 |
l3 = sexp_find_token (l2, "a", 0); |
| 2102 |
sexp_release (l2); |
| 2103 |
if (!l3) |
| 2104 |
return NULL; |
| 2105 |
a_value = sexp_nth_mpi (l3, 1, 0); |
| 2106 |
sexp_release (l3); |
| 2107 |
|
| 2108 |
return a_value; |
| 2109 |
} |
| 2110 |
|
| 2111 |
|
| 2112 |
static const char * |
| 2113 |
selftest_encr_2048 (gcry_sexp_t pkey, gcry_sexp_t skey) |
| 2114 |
{ |
| 2115 |
const char *errtxt = NULL; |
| 2116 |
gcry_error_t err; |
| 2117 |
static const char plaintext[] = |
| 2118 |
"Jim quickly realized that the beautiful gowns are expensive."; |
| 2119 |
gcry_sexp_t plain = NULL; |
| 2120 |
gcry_sexp_t encr = NULL; |
| 2121 |
gcry_mpi_t ciphertext = NULL; |
| 2122 |
gcry_sexp_t decr = NULL; |
| 2123 |
char *decr_plaintext = NULL; |
| 2124 |
gcry_sexp_t tmplist = NULL; |
| 2125 |
/* expected result of encrypting the plaintext with sample_secret_key */ |
| 2126 |
static const char ref_data[] = |
| 2127 |
"18022e2593a402a737caaa93b4c7e750e20ca265452980e1d6b7710fbd3e" |
| 2128 |
"7dce72be5c2110fb47691cb38f42170ee3b4a37f2498d4a51567d762585e" |
| 2129 |
"4cb81d04fbc7df4144f8e5eac2d4b8688521b64011f11d7ad53f4c874004" |
| 2130 |
"819856f2e2a6f83d1c9c4e73ac26089789c14482b0b8d44139133c88c4a5" |
| 2131 |
"2dba9dd6d6ffc622666b7d129168333d999706af30a2d7d272db7734e5ed" |
| 2132 |
"fb8c64ea3018af3ad20f4a013a5060cb0f5e72753967bebe294280a6ed0d" |
| 2133 |
"dbd3c4f11d0a8696e9d32a0dc03deb0b5e49b2cbd1503392642d4e1211f3" |
| 2134 |
"e8e2ee38abaa3671ccd57fcde8ca76e85fd2cb77c35706a970a213a27352" |
| 2135 |
"cec92a9604d543ddb5fc478ff50e0622"; |
| 2136 |
gcry_mpi_t ref_mpi = NULL; |
| 2137 |
|
| 2138 |
/* Put the plaintext into an S-expression. */ |
| 2139 |
err = sexp_build (&plain, NULL, "(data (flags raw) (value %s))", plaintext); |
| 2140 |
if (err) |
| 2141 |
{ |
| 2142 |
errtxt = "converting data failed"; |
| 2143 |
goto leave; |
| 2144 |
} |
| 2145 |
|
| 2146 |
/* Encrypt. */ |
| 2147 |
err = _gcry_pk_encrypt (&encr, plain, pkey); |
| 2148 |
if (err) |
| 2149 |
{ |
| 2150 |
errtxt = "encrypt failed"; |
| 2151 |
goto leave; |
| 2152 |
} |
| 2153 |
|
| 2154 |
err = _gcry_mpi_scan(&ref_mpi, GCRYMPI_FMT_HEX, ref_data, 0, NULL); |
| 2155 |
if (err) |
| 2156 |
{ |
| 2157 |
errtxt = "converting encrydata to mpi failed"; |
| 2158 |
goto leave; |
| 2159 |
} |
| 2160 |
|
| 2161 |
/* Extraxt the ciphertext from the returned S-expression. */ |
| 2162 |
/*sexp_dump (encr);*/ |
| 2163 |
ciphertext = extract_a_from_sexp (encr); |
| 2164 |
if (!ciphertext) |
| 2165 |
{ |
| 2166 |
errtxt = "gcry_pk_encrypt returned garbage"; |
| 2167 |
goto leave; |
| 2168 |
} |
| 2169 |
|
| 2170 |
/* Check that the ciphertext does no match the plaintext. */ |
| 2171 |
/* _gcry_log_printmpi ("plaintext", plaintext); */ |
| 2172 |
/* _gcry_log_printmpi ("ciphertxt", ciphertext); */ |
| 2173 |
if (mpi_cmp (ref_mpi, ciphertext)) |
| 2174 |
{ |
| 2175 |
errtxt = "ciphertext doesn't match reference data"; |
| 2176 |
goto leave; |
| 2177 |
} |
| 2178 |
|
| 2179 |
/* Decrypt. */ |
| 2180 |
err = _gcry_pk_decrypt (&decr, encr, skey); |
| 2181 |
if (err) |
| 2182 |
{ |
| 2183 |
errtxt = "decrypt failed"; |
| 2184 |
goto leave; |
| 2185 |
} |
| 2186 |
|
| 2187 |
/* Extract the decrypted data from the S-expression. Note that the |
| 2188 |
output of gcry_pk_decrypt depends on whether a flags lists occurs |
| 2189 |
in its input data. Because we passed the output of |
| 2190 |
gcry_pk_encrypt directly to gcry_pk_decrypt, such a flag value |
| 2191 |
won't be there as of today. To be prepared for future changes we |
| 2192 |
take care of it anyway. */ |
| 2193 |
tmplist = sexp_find_token (decr, "value", 0); |
| 2194 |
if (tmplist) |
| 2195 |
decr_plaintext = sexp_nth_string (tmplist, 1); |
| 2196 |
else |
| 2197 |
decr_plaintext = sexp_nth_string (decr, 0); |
| 2198 |
if (!decr_plaintext) |
| 2199 |
{ |
| 2200 |
errtxt = "decrypt returned no plaintext"; |
| 2201 |
goto leave; |
| 2202 |
} |
| 2203 |
|
| 2204 |
/* Check that the decrypted plaintext matches the original plaintext. */ |
| 2205 |
if (strcmp (plaintext, decr_plaintext)) |
| 2206 |
{ |
| 2207 |
errtxt = "mismatch"; |
| 2208 |
goto leave; |
| 2209 |
} |
| 2210 |
|
| 2211 |
leave: |
| 2212 |
sexp_release (tmplist); |
| 2213 |
xfree (decr_plaintext); |
| 2214 |
sexp_release (decr); |
| 2215 |
_gcry_mpi_release (ciphertext); |
| 2216 |
_gcry_mpi_release (ref_mpi); |
| 2217 |
sexp_release (encr); |
| 2218 |
sexp_release (plain); |
| 2219 |
return errtxt; |
| 2220 |
} |
| 2221 |
|
| 2222 |
|
| 2223 |
static gpg_err_code_t |
| 2224 |
selftests_rsa (selftest_report_func_t report, int extended) |
| 2225 |
{ |
| 2226 |
const char *what; |
| 2227 |
const char *errtxt; |
| 2228 |
gcry_error_t err; |
| 2229 |
gcry_sexp_t skey = NULL; |
| 2230 |
gcry_sexp_t pkey = NULL; |
| 2231 |
|
| 2232 |
/* Convert the S-expressions into the internal representation. */ |
| 2233 |
what = "convert"; |
| 2234 |
err = sexp_sscan (&skey, NULL, sample_secret_key, strlen (sample_secret_key)); |
| 2235 |
if (!err) |
| 2236 |
err = sexp_sscan (&pkey, NULL, |
| 2237 |
sample_public_key, strlen (sample_public_key)); |
| 2238 |
if (err) |
| 2239 |
{ |
| 2240 |
errtxt = _gcry_strerror (err); |
| 2241 |
goto failed; |
| 2242 |
} |
| 2243 |
|
| 2244 |
what = "key consistency"; |
| 2245 |
err = _gcry_pk_testkey (skey); |
| 2246 |
if (err) |
| 2247 |
{ |
| 2248 |
errtxt = _gcry_strerror (err); |
| 2249 |
goto failed; |
| 2250 |
} |
| 2251 |
|
| 2252 |
if (extended) |
| 2253 |
{ |
| 2254 |
what = "sign"; |
| 2255 |
errtxt = selftest_sign_2048 (pkey, skey); |
| 2256 |
if (errtxt) |
| 2257 |
goto failed; |
| 2258 |
} |
| 2259 |
|
| 2260 |
what = "digest sign"; |
| 2261 |
errtxt = selftest_hash_sign_2048 (pkey, skey); |
| 2262 |
if (errtxt) |
| 2263 |
goto failed; |
| 2264 |
|
| 2265 |
if (extended) |
| 2266 |
{ |
| 2267 |
what = "encrypt"; |
| 2268 |
errtxt = selftest_encr_2048 (pkey, skey); |
| 2269 |
if (errtxt) |
| 2270 |
goto failed; |
| 2271 |
} |
| 2272 |
|
| 2273 |
sexp_release (pkey); |
| 2274 |
sexp_release (skey); |
| 2275 |
return 0; /* Succeeded. */ |
| 2276 |
|
| 2277 |
failed: |
| 2278 |
sexp_release (pkey); |
| 2279 |
sexp_release (skey); |
| 2280 |
if (report) |
| 2281 |
report ("pubkey", GCRY_PK_RSA, what, errtxt); |
| 2282 |
return GPG_ERR_SELFTEST_FAILED; |
| 2283 |
} |
| 2284 |
|
| 2285 |
|
| 2286 |
/* Run a full self-test for ALGO and return 0 on success. */ |
| 2287 |
static gpg_err_code_t |
| 2288 |
run_selftests (int algo, int extended, selftest_report_func_t report) |
| 2289 |
{ |
| 2290 |
gpg_err_code_t ec; |
| 2291 |
|
| 2292 |
switch (algo) |
| 2293 |
{ |
| 2294 |
case GCRY_PK_RSA: |
| 2295 |
ec = selftests_rsa (report, extended); |
| 2296 |
break; |
| 2297 |
default: |
| 2298 |
ec = GPG_ERR_PUBKEY_ALGO; |
| 2299 |
break; |
| 2300 |
|
| 2301 |
} |
| 2302 |
return ec; |
| 2303 |
} |
| 2304 |
|
| 2305 |
|
| 2306 |
|
| 2307 |
|
| 2308 |
gcry_pk_spec_t _gcry_pubkey_spec_rsa = |
| 2309 |
{ |
| 2310 |
GCRY_PK_RSA, { 0, 1 }, |
| 2311 |
(GCRY_PK_USAGE_SIGN | GCRY_PK_USAGE_ENCR), |
| 2312 |
"RSA", rsa_names, |
| 2313 |
"ne", "nedpqu", "a", "s", "n", |
| 2314 |
rsa_generate, |
| 2315 |
rsa_check_secret_key, |
| 2316 |
rsa_encrypt, |
| 2317 |
rsa_decrypt, |
| 2318 |
rsa_sign, |
| 2319 |
rsa_verify, |
| 2320 |
rsa_get_nbits, |
| 2321 |
run_selftests, |
| 2322 |
compute_keygrip |
| 2323 |
}; |