2 * Copyright (C) 2005-2009 Martin Willi
3 * Copyright (C) 2005 Jan Hutter
4 * Hochschule fuer Technik Rapperswil
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 #include "gmp_rsa_private_key.h"
23 #include "gmp_rsa_public_key.h"
27 #include <asn1/asn1.h>
28 #include <asn1/asn1_parser.h>
31 * Public exponent to use for key generation.
33 #define PUBLIC_EXPONENT 0x10001
35 typedef struct private_gmp_rsa_private_key_t private_gmp_rsa_private_key_t
;
38 * Private data of a gmp_rsa_private_key_t object.
40 struct private_gmp_rsa_private_key_t
{
42 * Public interface for this signer.
44 gmp_rsa_private_key_t
public;
82 * Private coefficient.
98 * Convert a MP integer into a chunk_t
100 chunk_t
gmp_mpz_to_chunk(const mpz_t value
)
104 n
.len
= 1 + mpz_sizeinbase(value
, 2) / BITS_PER_BYTE
;
105 n
.ptr
= mpz_export(NULL
, NULL
, 1, n
.len
, 1, 0, value
);
107 { /* if we have zero in "value", gmp returns NULL */
114 * Auxiliary function overwriting private key material with zero bytes
116 static void mpz_clear_sensitive(mpz_t z
)
118 size_t len
= mpz_size(z
) * GMP_LIMB_BITS
/ BITS_PER_BYTE
;
119 u_int8_t
*random
= alloca(len
);
121 memset(random
, 0, len
);
122 /* overwrite mpz_t with zero bytes before clearing it */
123 mpz_import(z
, len
, 1, 1, 1, 0, random
);
128 * Create a mpz prime of at least prime_size
130 static status_t
compute_prime(private_gmp_rsa_private_key_t
*this,
131 size_t prime_size
, mpz_t
*prime
)
134 chunk_t random_bytes
;
136 rng
= lib
->crypto
->create_rng(lib
->crypto
, RNG_TRUE
);
139 DBG1("no RNG of quality %N found", rng_quality_names
, RNG_TRUE
);
146 rng
->allocate_bytes(rng
, prime_size
, &random_bytes
);
147 /* make sure most significant bit is set */
148 random_bytes
.ptr
[0] = random_bytes
.ptr
[0] | 0x80;
150 mpz_import(*prime
, random_bytes
.len
, 1, 1, 1, 0, random_bytes
.ptr
);
151 mpz_nextprime (*prime
, *prime
);
152 chunk_clear(&random_bytes
);
154 /* check if it isn't too large */
155 while (((mpz_sizeinbase(*prime
, 2) + 7) / 8) > prime_size
);
162 * PKCS#1 RSADP function
164 static chunk_t
rsadp(private_gmp_rsa_private_key_t
*this, chunk_t data
)
172 mpz_import(t1
, data
.len
, 1, 1, 1, 0, data
.ptr
);
174 mpz_powm(t2
, t1
, this->exp1
, this->p
); /* m1 = c^dP mod p */
175 mpz_powm(t1
, t1
, this->exp2
, this->q
); /* m2 = c^dQ mod Q */
176 mpz_sub(t2
, t2
, t1
); /* h = qInv (m1 - m2) mod p */
177 mpz_mod(t2
, t2
, this->p
);
178 mpz_mul(t2
, t2
, this->coeff
);
179 mpz_mod(t2
, t2
, this->p
);
181 mpz_mul(t2
, t2
, this->q
); /* m = m2 + h q */
184 decrypted
.len
= this->k
;
185 decrypted
.ptr
= mpz_export(NULL
, NULL
, 1, decrypted
.len
, 1, 0, t1
);
186 if (decrypted
.ptr
== NULL
)
191 mpz_clear_sensitive(t1
);
192 mpz_clear_sensitive(t2
);
198 * PKCS#1 RSASP1 function
200 static chunk_t
rsasp1(private_gmp_rsa_private_key_t
*this, chunk_t data
)
202 return rsadp(this, data
);
206 * Implementation of gmp_rsa_private_key_t.build_emsa_pkcs1_signature.
208 static bool build_emsa_pkcs1_signature(private_gmp_rsa_private_key_t
*this,
209 hash_algorithm_t hash_algorithm
,
210 chunk_t data
, chunk_t
*signature
)
212 chunk_t digestInfo
= chunk_empty
;
215 if (hash_algorithm
!= HASH_UNKNOWN
)
219 int hash_oid
= hasher_algorithm_to_oid(hash_algorithm
);
221 if (hash_oid
== OID_UNKNOWN
)
226 hasher
= lib
->crypto
->create_hasher(lib
->crypto
, hash_algorithm
);
231 hasher
->allocate_hash(hasher
, data
, &hash
);
232 hasher
->destroy(hasher
);
234 /* build DER-encoded digestInfo */
235 digestInfo
= asn1_wrap(ASN1_SEQUENCE
, "mm",
236 asn1_algorithmIdentifier(hash_oid
),
237 asn1_simple_object(ASN1_OCTET_STRING
, hash
)
243 if (data
.len
> this->k
- 3)
245 free(digestInfo
.ptr
);
246 DBG1("unable to sign %d bytes using a %dbit key", data
.len
, this->k
* 8);
250 /* build chunk to rsa-decrypt:
251 * EM = 0x00 || 0x01 || PS || 0x00 || T.
252 * PS = 0xFF padding, with length to fill em
256 em
.ptr
= malloc(em
.len
);
258 /* fill em with padding */
259 memset(em
.ptr
, 0xFF, em
.len
);
260 /* set magic bytes */
263 *(em
.ptr
+ em
.len
- data
.len
- 1) = 0x00;
264 /* set DER-encoded hash */
265 memcpy(em
.ptr
+ em
.len
- data
.len
, data
.ptr
, data
.len
);
267 /* build signature */
268 *signature
= rsasp1(this, em
);
270 free(digestInfo
.ptr
);
277 * Implementation of gmp_rsa_private_key.get_type.
279 static key_type_t
get_type(private_gmp_rsa_private_key_t
*this)
285 * Implementation of gmp_rsa_private_key.sign.
287 static bool sign(private_gmp_rsa_private_key_t
*this, signature_scheme_t scheme
,
288 chunk_t data
, chunk_t
*signature
)
292 case SIGN_RSA_EMSA_PKCS1_NULL
:
293 return build_emsa_pkcs1_signature(this, HASH_UNKNOWN
, data
, signature
);
294 case SIGN_RSA_EMSA_PKCS1_SHA1
:
295 return build_emsa_pkcs1_signature(this, HASH_SHA1
, data
, signature
);
296 case SIGN_RSA_EMSA_PKCS1_SHA224
:
297 return build_emsa_pkcs1_signature(this, HASH_SHA224
, data
, signature
);
298 case SIGN_RSA_EMSA_PKCS1_SHA256
:
299 return build_emsa_pkcs1_signature(this, HASH_SHA256
, data
, signature
);
300 case SIGN_RSA_EMSA_PKCS1_SHA384
:
301 return build_emsa_pkcs1_signature(this, HASH_SHA384
, data
, signature
);
302 case SIGN_RSA_EMSA_PKCS1_SHA512
:
303 return build_emsa_pkcs1_signature(this, HASH_SHA512
, data
, signature
);
304 case SIGN_RSA_EMSA_PKCS1_MD5
:
305 return build_emsa_pkcs1_signature(this, HASH_MD5
, data
, signature
);
307 DBG1("signature scheme %N not supported in RSA",
308 signature_scheme_names
, scheme
);
314 * Implementation of gmp_rsa_private_key.decrypt.
316 static bool decrypt(private_gmp_rsa_private_key_t
*this, chunk_t crypto
,
319 chunk_t em
, stripped
;
320 bool success
= FALSE
;
322 /* rsa decryption using PKCS#1 RSADP */
323 stripped
= em
= rsadp(this, crypto
);
325 /* PKCS#1 v1.5 8.1 encryption-block formatting (EB = 00 || 02 || PS || 00 || D) */
327 /* check for hex pattern 00 02 in decrypted message */
328 if ((*stripped
.ptr
++ != 0x00) || (*(stripped
.ptr
++) != 0x02))
330 DBG1("incorrect padding - probably wrong rsa key");
335 /* the plaintext data starts after first 0x00 byte */
336 while (stripped
.len
-- > 0 && *stripped
.ptr
++ != 0x00)
338 if (stripped
.len
== 0)
340 DBG1("no plaintext data");
344 *plain
= chunk_clone(stripped
);
353 * Implementation of gmp_rsa_private_key.get_keysize.
355 static size_t get_keysize(private_gmp_rsa_private_key_t
*this)
361 * Implementation of gmp_rsa_private_key.get_public_key.
363 static public_key_t
* get_public_key(private_gmp_rsa_private_key_t
*this)
366 public_key_t
*public;
368 n
= gmp_mpz_to_chunk(this->n
);
369 e
= gmp_mpz_to_chunk(this->e
);
371 public = lib
->creds
->create(lib
->creds
, CRED_PUBLIC_KEY
, KEY_RSA
,
372 BUILD_RSA_MODULUS
, n
, BUILD_RSA_PUB_EXP
, e
, BUILD_END
);
380 * Implementation of gmp_rsa_private_key.equals.
382 static bool equals(private_gmp_rsa_private_key_t
*this, private_key_t
*other
)
384 return private_key_equals(&this->public.interface
, other
);
388 * Implementation of gmp_rsa_private_key.belongs_to.
390 static bool belongs_to(private_gmp_rsa_private_key_t
*this, public_key_t
*public)
392 return private_key_belongs_to(&this->public.interface
, public);
396 * Implementation of private_key_t.get_encoding
398 static bool get_encoding(private_gmp_rsa_private_key_t
*this,
399 key_encoding_type_t type
, chunk_t
*encoding
)
401 chunk_t n
, e
, d
, p
, q
, exp1
, exp2
, coeff
;
404 n
= gmp_mpz_to_chunk(this->n
);
405 e
= gmp_mpz_to_chunk(this->e
);
406 d
= gmp_mpz_to_chunk(this->d
);
407 p
= gmp_mpz_to_chunk(this->p
);
408 q
= gmp_mpz_to_chunk(this->q
);
409 exp1
= gmp_mpz_to_chunk(this->exp1
);
410 exp2
= gmp_mpz_to_chunk(this->exp2
);
411 coeff
= gmp_mpz_to_chunk(this->coeff
);
413 success
= lib
->encoding
->encode(lib
->encoding
,
414 type
, NULL
, encoding
, KEY_PART_RSA_MODULUS
, n
,
415 KEY_PART_RSA_PUB_EXP
, e
, KEY_PART_RSA_PRIV_EXP
, d
,
416 KEY_PART_RSA_PRIME1
, p
, KEY_PART_RSA_PRIME2
, q
,
417 KEY_PART_RSA_EXP1
, exp1
, KEY_PART_RSA_EXP2
, exp2
,
418 KEY_PART_RSA_COEFF
, coeff
, KEY_PART_END
);
432 * Implementation of private_key_t.get_fingerprint
434 static bool get_fingerprint(private_gmp_rsa_private_key_t
*this,
435 key_encoding_type_t type
, chunk_t
*fp
)
440 if (lib
->encoding
->get_cache(lib
->encoding
, type
, this, fp
))
444 n
= gmp_mpz_to_chunk(this->n
);
445 e
= gmp_mpz_to_chunk(this->e
);
447 success
= lib
->encoding
->encode(lib
->encoding
, type
, this, fp
,
448 KEY_PART_RSA_MODULUS
, n
, KEY_PART_RSA_PUB_EXP
, e
, KEY_PART_END
);
456 * Implementation of gmp_rsa_private_key.get_ref.
458 static private_gmp_rsa_private_key_t
* get_ref(private_gmp_rsa_private_key_t
*this)
465 * Implementation of gmp_rsa_private_key.destroy.
467 static void destroy(private_gmp_rsa_private_key_t
*this)
469 if (ref_put(&this->ref
))
471 mpz_clear_sensitive(this->n
);
472 mpz_clear_sensitive(this->e
);
473 mpz_clear_sensitive(this->p
);
474 mpz_clear_sensitive(this->q
);
475 mpz_clear_sensitive(this->d
);
476 mpz_clear_sensitive(this->exp1
);
477 mpz_clear_sensitive(this->exp2
);
478 mpz_clear_sensitive(this->coeff
);
479 lib
->encoding
->clear_cache(lib
->encoding
, this);
485 * Check the loaded key if it is valid and usable
487 static status_t
check(private_gmp_rsa_private_key_t
*this)
490 status_t status
= SUCCESS
;
492 /* PKCS#1 1.5 section 6 requires modulus to have at least 12 octets.
493 * We actually require more (for security).
495 if (this->k
< 512 / BITS_PER_BYTE
)
497 DBG1("key shorter than 512 bits");
501 /* we picked a max modulus size to simplify buffer allocation */
502 if (this->k
> 8192 / BITS_PER_BYTE
)
504 DBG1("key larger than 8192 bits");
512 /* check that n == p * q */
513 mpz_mul(u
, this->p
, this->q
);
514 if (mpz_cmp(u
, this->n
) != 0)
519 /* check that e divides neither p-1 nor q-1 */
520 mpz_sub_ui(t
, this->p
, 1);
521 mpz_mod(t
, t
, this->e
);
522 if (mpz_cmp_ui(t
, 0) == 0)
527 mpz_sub_ui(t
, this->q
, 1);
528 mpz_mod(t
, t
, this->e
);
529 if (mpz_cmp_ui(t
, 0) == 0)
534 /* check that d is e^-1 (mod lcm(p-1, q-1)) */
535 /* see PKCS#1v2, aka RFC 2437, for the "lcm" */
536 mpz_sub_ui(q1
, this->q
, 1);
537 mpz_sub_ui(u
, this->p
, 1);
538 mpz_gcd(t
, u
, q1
); /* t := gcd(p-1, q-1) */
539 mpz_mul(u
, u
, q1
); /* u := (p-1) * (q-1) */
540 mpz_divexact(u
, u
, t
); /* u := lcm(p-1, q-1) */
542 mpz_mul(t
, this->d
, this->e
);
544 if (mpz_cmp_ui(t
, 1) != 0)
549 /* check that exp1 is d mod (p-1) */
550 mpz_sub_ui(u
, this->p
, 1);
551 mpz_mod(t
, this->d
, u
);
552 if (mpz_cmp(t
, this->exp1
) != 0)
557 /* check that exp2 is d mod (q-1) */
558 mpz_sub_ui(u
, this->q
, 1);
559 mpz_mod(t
, this->d
, u
);
560 if (mpz_cmp(t
, this->exp2
) != 0)
565 /* check that coeff is (q^-1) mod p */
566 mpz_mul(t
, this->coeff
, this->q
);
567 mpz_mod(t
, t
, this->p
);
568 if (mpz_cmp_ui(t
, 1) != 0)
573 mpz_clear_sensitive(t
);
574 mpz_clear_sensitive(u
);
575 mpz_clear_sensitive(q1
);
576 if (status
!= SUCCESS
)
578 DBG1("key integrity tests failed");
584 * Internal generic constructor
586 static private_gmp_rsa_private_key_t
*gmp_rsa_private_key_create_empty(void)
588 private_gmp_rsa_private_key_t
*this = malloc_thing(private_gmp_rsa_private_key_t
);
590 this->public.interface
.get_type
= (key_type_t (*) (private_key_t
*))get_type
;
591 this->public.interface
.sign
= (bool (*) (private_key_t
*, signature_scheme_t
, chunk_t
, chunk_t
*))sign
;
592 this->public.interface
.decrypt
= (bool (*) (private_key_t
*, chunk_t
, chunk_t
*))decrypt
;
593 this->public.interface
.get_keysize
= (size_t (*) (private_key_t
*))get_keysize
;
594 this->public.interface
.get_public_key
= (public_key_t
* (*) (private_key_t
*))get_public_key
;
595 this->public.interface
.equals
= (bool (*) (private_key_t
*, private_key_t
*))equals
;
596 this->public.interface
.belongs_to
= (bool (*) (private_key_t
*, public_key_t
*))belongs_to
;
597 this->public.interface
.get_fingerprint
= (bool(*)(private_key_t
*, key_encoding_type_t type
, chunk_t
*fp
))get_fingerprint
;
598 this->public.interface
.get_encoding
= (bool(*)(private_key_t
*, key_encoding_type_t type
, chunk_t
*encoding
))get_encoding
;
599 this->public.interface
.get_ref
= (private_key_t
* (*) (private_key_t
*))get_ref
;
600 this->public.interface
.destroy
= (void (*) (private_key_t
*))destroy
;
608 * Generate an RSA key of specified key size
610 static gmp_rsa_private_key_t
*generate(size_t key_size
)
612 mpz_t p
, q
, n
, e
, d
, exp1
, exp2
, coeff
;
614 private_gmp_rsa_private_key_t
*this = gmp_rsa_private_key_create_empty();
616 key_size
= key_size
/ BITS_PER_BYTE
;
618 /* Get values of primes p and q */
619 if (compute_prime(this, key_size
/2, &p
) != SUCCESS
)
624 if (compute_prime(this, key_size
/2, &q
) != SUCCESS
)
638 /* Swapping Primes so p is larger then q */
639 if (mpz_cmp(p
, q
) < 0)
644 mpz_mul(n
, p
, q
); /* n = p*q */
645 mpz_init_set_ui(e
, PUBLIC_EXPONENT
); /* assign public exponent */
646 mpz_init_set(m
, p
); /* m = p */
647 mpz_sub_ui(m
, m
, 1); /* m = m -1 */
648 mpz_init_set(q1
, q
); /* q1 = q */
649 mpz_sub_ui(q1
, q1
, 1); /* q1 = q1 -1 */
650 mpz_gcd(t
, m
, q1
); /* t = gcd(p-1, q-1) */
651 mpz_mul(m
, m
, q1
); /* m = (p-1)*(q-1) */
652 mpz_divexact(m
, m
, t
); /* m = m / t */
653 mpz_gcd(t
, m
, e
); /* t = gcd(m, e) */
655 mpz_invert(d
, e
, m
); /* e has an inverse mod m */
656 if (mpz_cmp_ui(d
, 0) < 0) /* make sure d is positive */
660 mpz_sub_ui(t
, p
, 1); /* t = p-1 */
661 mpz_mod(exp1
, d
, t
); /* exp1 = d mod p-1 */
662 mpz_sub_ui(t
, q
, 1); /* t = q-1 */
663 mpz_mod(exp2
, d
, t
); /* exp2 = d mod q-1 */
665 mpz_invert(coeff
, q
, p
); /* coeff = q^-1 mod p */
666 if (mpz_cmp_ui(coeff
, 0) < 0) /* make coeff d is positive */
668 mpz_add(coeff
, coeff
, p
);
671 mpz_clear_sensitive(q1
);
672 mpz_clear_sensitive(m
);
673 mpz_clear_sensitive(t
);
681 *(this->exp1
) = *exp1
;
682 *(this->exp2
) = *exp2
;
683 *(this->coeff
) = *coeff
;
685 /* set key size in bytes */
688 return &this->public;
692 * load private key from a RSA components
694 static gmp_rsa_private_key_t
*load(chunk_t n
, chunk_t e
, chunk_t d
,
695 chunk_t p
, chunk_t q
, chunk_t exp1
, chunk_t exp2
, chunk_t coeff
)
697 private_gmp_rsa_private_key_t
*this = gmp_rsa_private_key_create_empty();
704 mpz_init(this->exp1
);
705 mpz_init(this->exp2
);
706 mpz_init(this->coeff
);
708 mpz_import(this->n
, n
.len
, 1, 1, 1, 0, n
.ptr
);
709 mpz_import(this->e
, e
.len
, 1, 1, 1, 0, e
.ptr
);
710 mpz_import(this->d
, d
.len
, 1, 1, 1, 0, d
.ptr
);
711 mpz_import(this->p
, p
.len
, 1, 1, 1, 0, p
.ptr
);
712 mpz_import(this->q
, q
.len
, 1, 1, 1, 0, q
.ptr
);
713 mpz_import(this->coeff
, coeff
.len
, 1, 1, 1, 0, coeff
.ptr
);
715 { /* exp1 missing in key, recalculate: exp1 = d mod (p-1) */
716 mpz_sub_ui(this->exp1
, this->p
, 1);
717 mpz_mod(this->exp1
, this->d
, this->exp1
);
721 mpz_import(this->exp1
, exp1
.len
, 1, 1, 1, 0, exp1
.ptr
);
724 { /* exp2 missing in key, recalculate: exp2 = d mod (q-1) */
725 mpz_sub_ui(this->exp2
, this->q
, 1);
726 mpz_mod(this->exp2
, this->d
, this->exp2
);
730 mpz_import(this->exp2
, exp2
.len
, 1, 1, 1, 0, exp2
.ptr
);
732 this->k
= (mpz_sizeinbase(this->n
, 2) + 7) / BITS_PER_BYTE
;
733 if (check(this) != SUCCESS
)
738 return &this->public;
741 typedef struct private_builder_t private_builder_t
;
743 * Builder implementation for key loading/generation
745 struct private_builder_t
{
746 /** implements the builder interface */
748 /** key size, if generating */
750 /** rsa key parameters */
751 chunk_t n
, e
, d
, p
, q
, exp1
, exp2
, coeff
;
755 * Implementation of builder_t.build
757 static gmp_rsa_private_key_t
*build(private_builder_t
*this)
759 gmp_rsa_private_key_t
*key
= NULL
;
763 key
= generate(this->key_size
);
767 key
= load(this->n
, this->e
, this->d
, this->p
, this->q
,
768 this->exp1
, this->exp2
, this->coeff
);
775 * Implementation of builder_t.add
777 static void add(private_builder_t
*this, builder_part_t part
, ...)
781 va_start(args
, part
);
785 this->key_size
= va_arg(args
, u_int
);
787 case BUILD_RSA_MODULUS
:
788 this->n
= va_arg(args
, chunk_t
);
790 case BUILD_RSA_PUB_EXP
:
791 this->e
= va_arg(args
, chunk_t
);
793 case BUILD_RSA_PRIV_EXP
:
794 this->d
= va_arg(args
, chunk_t
);
796 case BUILD_RSA_PRIME1
:
797 this->p
= va_arg(args
, chunk_t
);
799 case BUILD_RSA_PRIME2
:
800 this->q
= va_arg(args
, chunk_t
);
803 this->exp1
= va_arg(args
, chunk_t
);
806 this->exp2
= va_arg(args
, chunk_t
);
808 case BUILD_RSA_COEFF
:
809 this->coeff
= va_arg(args
, chunk_t
);
812 builder_cancel(&this->public);
819 * Builder construction function
821 builder_t
*gmp_rsa_private_key_builder(key_type_t type
)
823 private_builder_t
*this;
830 this = malloc_thing(private_builder_t
);
832 this->n
= this->e
= this->d
= this->p
= this->q
= chunk_empty
;
833 this->exp1
= this->exp2
= this->coeff
= chunk_empty
;
835 this->public.add
= (void(*)(builder_t
*this, builder_part_t part
, ...))add
;
836 this->public.build
= (void*(*)(builder_t
*this))build
;
838 return &this->public;