2 * Copyright (C) 2005-2008 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
24 #include "gmp_rsa_private_key.h"
25 #include "gmp_rsa_public_key.h"
28 #include <asn1/asn1.h>
29 #include <utils/randomizer.h>
30 #include <crypto/hashers/hasher.h>
33 * Public exponent to use for key generation.
35 #define PUBLIC_EXPONENT 0x10001
37 typedef struct private_gmp_rsa_private_key_t private_gmp_rsa_private_key_t
;
40 * Private data of a gmp_rsa_private_key_t object.
42 struct private_gmp_rsa_private_key_t
{
44 * Public interface for this signer.
46 gmp_rsa_private_key_t
public;
49 * Version of key, as encoded in PKCS#1
89 * Private coefficient.
99 * Keyid formed as a SHA-1 hash of a publicKey object
101 identification_t
* keyid
;
104 * Keyid formed as a SHA-1 hash of a publicKeyInfo object
106 identification_t
* keyid_info
;
114 /* ASN.1 definition of a PKCS#1 RSA private key */
115 static const asn1Object_t privkey_objects
[] = {
116 { 0, "RSAPrivateKey", ASN1_SEQUENCE
, ASN1_NONE
}, /* 0 */
117 { 1, "version", ASN1_INTEGER
, ASN1_BODY
}, /* 1 */
118 { 1, "modulus", ASN1_INTEGER
, ASN1_BODY
}, /* 2 */
119 { 1, "publicExponent", ASN1_INTEGER
, ASN1_BODY
}, /* 3 */
120 { 1, "privateExponent", ASN1_INTEGER
, ASN1_BODY
}, /* 4 */
121 { 1, "prime1", ASN1_INTEGER
, ASN1_BODY
}, /* 5 */
122 { 1, "prime2", ASN1_INTEGER
, ASN1_BODY
}, /* 6 */
123 { 1, "exponent1", ASN1_INTEGER
, ASN1_BODY
}, /* 7 */
124 { 1, "exponent2", ASN1_INTEGER
, ASN1_BODY
}, /* 8 */
125 { 1, "coefficient", ASN1_INTEGER
, ASN1_BODY
}, /* 9 */
126 { 1, "otherPrimeInfos", ASN1_SEQUENCE
, ASN1_OPT
|
127 ASN1_LOOP
}, /* 10 */
128 { 2, "otherPrimeInfo", ASN1_SEQUENCE
, ASN1_NONE
}, /* 11 */
129 { 3, "prime", ASN1_INTEGER
, ASN1_BODY
}, /* 12 */
130 { 3, "exponent", ASN1_INTEGER
, ASN1_BODY
}, /* 13 */
131 { 3, "coefficient", ASN1_INTEGER
, ASN1_BODY
}, /* 14 */
132 { 1, "end opt or loop", ASN1_EOC
, ASN1_END
} /* 15 */
135 #define PRIV_KEY_VERSION 1
136 #define PRIV_KEY_MODULUS 2
137 #define PRIV_KEY_PUB_EXP 3
138 #define PRIV_KEY_PRIV_EXP 4
139 #define PRIV_KEY_PRIME1 5
140 #define PRIV_KEY_PRIME2 6
141 #define PRIV_KEY_EXP1 7
142 #define PRIV_KEY_EXP2 8
143 #define PRIV_KEY_COEFF 9
144 #define PRIV_KEY_ROOF 16
147 * shared functions, implemented in gmp_rsa_public_key.c
149 bool gmp_rsa_public_key_build_id(mpz_t n
, mpz_t e
, identification_t
**keyid
,
150 identification_t
**keyid_info
);
151 gmp_rsa_public_key_t
*gmp_rsa_public_key_create_from_n_e(mpz_t n
, mpz_t e
);
154 * Auxiliary function overwriting private key material with
155 * pseudo-random bytes before releasing it
157 static void mpz_clear_randomized(mpz_t z
)
159 size_t len
= mpz_size(z
) * GMP_LIMB_BITS
/ BITS_PER_BYTE
;
160 u_int8_t
*random_bytes
= alloca(len
);
162 randomizer_t
*randomizer
= randomizer_create();
164 randomizer
->get_pseudo_random_bytes(randomizer
, len
, random_bytes
);
166 /* overwrite mpz_t with pseudo-random bytes before clearing it */
167 mpz_import(z
, len
, 1, 1, 1, 0, random_bytes
);
170 randomizer
->destroy(randomizer
);
174 * Create a mpz prime of at least prime_size
176 static status_t
compute_prime(private_gmp_rsa_private_key_t
*this,
177 size_t prime_size
, mpz_t
*prime
)
179 randomizer_t
*randomizer
;
180 chunk_t random_bytes
;
183 randomizer
= randomizer_create();
188 status
= randomizer
->allocate_random_bytes(randomizer
, prime_size
,
190 if (status
!= SUCCESS
)
192 randomizer
->destroy(randomizer
);
196 /* make sure most significant bit is set */
197 random_bytes
.ptr
[0] = random_bytes
.ptr
[0] | 0x80;
199 mpz_import(*prime
, random_bytes
.len
, 1, 1, 1, 0, random_bytes
.ptr
);
200 mpz_nextprime (*prime
, *prime
);
201 chunk_free_randomized(&random_bytes
);
203 /* check if it isn't too large */
204 while (((mpz_sizeinbase(*prime
, 2) + 7) / 8) > prime_size
);
206 randomizer
->destroy(randomizer
);
211 * PKCS#1 RSADP function
213 static chunk_t
rsadp(private_gmp_rsa_private_key_t
*this, chunk_t data
)
221 mpz_import(t1
, data
.len
, 1, 1, 1, 0, data
.ptr
);
223 mpz_powm(t2
, t1
, this->exp1
, this->p
); /* m1 = c^dP mod p */
224 mpz_powm(t1
, t1
, this->exp2
, this->q
); /* m2 = c^dQ mod Q */
225 mpz_sub(t2
, t2
, t1
); /* h = qInv (m1 - m2) mod p */
226 mpz_mod(t2
, t2
, this->p
);
227 mpz_mul(t2
, t2
, this->coeff
);
228 mpz_mod(t2
, t2
, this->p
);
230 mpz_mul(t2
, t2
, this->q
); /* m = m2 + h q */
233 decrypted
.len
= this->k
;
234 decrypted
.ptr
= mpz_export(NULL
, NULL
, 1, decrypted
.len
, 1, 0, t1
);
236 mpz_clear_randomized(t1
);
237 mpz_clear_randomized(t2
);
243 * PKCS#1 RSASP1 function
245 static chunk_t
rsasp1(private_gmp_rsa_private_key_t
*this, chunk_t data
)
247 return rsadp(this, data
);
251 * Implementation of gmp_rsa_private_key_t.build_emsa_pkcs1_signature.
253 static bool build_emsa_pkcs1_signature(private_gmp_rsa_private_key_t
*this,
254 hash_algorithm_t hash_algorithm
,
255 chunk_t data
, chunk_t
*signature
)
258 chunk_t em
, digestInfo
, hash
;
259 int hash_oid
= hasher_algorithm_to_oid(hash_algorithm
);
261 if (hash_oid
== OID_UNKNOWN
)
267 hasher
= lib
->crypto
->create_hasher(lib
->crypto
, hash_algorithm
);
274 hasher
->allocate_hash(hasher
, data
, &hash
);
275 hasher
->destroy(hasher
);
277 /* build DER-encoded digestInfo */
278 digestInfo
= asn1_wrap(ASN1_SEQUENCE
, "cm",
279 asn1_algorithmIdentifier(hash_oid
),
280 asn1_simple_object(ASN1_OCTET_STRING
, hash
)
284 /* build chunk to rsa-decrypt:
285 * EM = 0x00 || 0x01 || PS || 0x00 || T.
286 * PS = 0xFF padding, with length to fill em
290 em
.ptr
= malloc(em
.len
);
292 /* fill em with padding */
293 memset(em
.ptr
, 0xFF, em
.len
);
294 /* set magic bytes */
297 *(em
.ptr
+ em
.len
- digestInfo
.len
- 1) = 0x00;
298 /* set DER-encoded hash */
299 memcpy(em
.ptr
+ em
.len
- digestInfo
.len
, digestInfo
.ptr
, digestInfo
.len
);
301 /* build signature */
302 *signature
= rsasp1(this, em
);
304 free(digestInfo
.ptr
);
311 * Implementation of gmp_rsa_private_key.destroy.
313 static key_type_t
get_type(private_gmp_rsa_private_key_t
*this)
319 * Implementation of gmp_rsa_private_key.destroy.
321 static bool sign(private_gmp_rsa_private_key_t
*this, signature_scheme_t scheme
,
322 chunk_t data
, chunk_t
*signature
)
327 /* default is EMSA-PKCS1 using SHA1 */
328 case SIGN_RSA_EMSA_PKCS1_SHA1
:
329 return build_emsa_pkcs1_signature(this, HASH_SHA1
, data
, signature
);
330 case SIGN_RSA_EMSA_PKCS1_SHA256
:
331 return build_emsa_pkcs1_signature(this, HASH_SHA256
, data
, signature
);
332 case SIGN_RSA_EMSA_PKCS1_SHA384
:
333 return build_emsa_pkcs1_signature(this, HASH_SHA384
, data
, signature
);
334 case SIGN_RSA_EMSA_PKCS1_SHA512
:
335 return build_emsa_pkcs1_signature(this, HASH_SHA512
, data
, signature
);
336 case SIGN_RSA_EMSA_PKCS1_MD5
:
337 return build_emsa_pkcs1_signature(this, HASH_MD5
, data
, signature
);
339 DBG1("signature scheme %N not supported in RSA",
340 signature_scheme_names
, scheme
);
346 * Implementation of gmp_rsa_private_key.destroy.
348 static bool decrypt(private_gmp_rsa_private_key_t
*this,
349 chunk_t crypto
, chunk_t
*plain
)
351 DBG1("RSA private key decryption not implemented");
356 * Implementation of gmp_rsa_private_key.destroy.
358 static size_t get_keysize(private_gmp_rsa_private_key_t
*this)
364 * Implementation of gmp_rsa_private_key.destroy.
366 static identification_t
* get_id(private_gmp_rsa_private_key_t
*this,
371 case ID_PUBKEY_INFO_SHA1
:
372 return this->keyid_info
;
381 * Implementation of gmp_rsa_private_key.destroy.
383 static gmp_rsa_public_key_t
* get_public_key(private_gmp_rsa_private_key_t
*this)
385 return gmp_rsa_public_key_create_from_n_e(this->n
, this->e
);
389 * Implementation of gmp_rsa_private_key.destroy.
391 static bool belongs_to(private_gmp_rsa_private_key_t
*this, public_key_t
*public)
393 identification_t
*keyid
;
395 if (public->get_type(public) != KEY_RSA
)
399 keyid
= public->get_id(public, ID_PUBKEY_SHA1
);
400 if (keyid
&& keyid
->equals(keyid
, this->keyid
))
404 keyid
= public->get_id(public, ID_PUBKEY_INFO_SHA1
);
405 if (keyid
&& keyid
->equals(keyid
, this->keyid_info
))
413 * convert a MP integer into a DER coded ASN.1 object
415 chunk_t
gmp_mpz_to_asn1(const mpz_t value
)
417 size_t bits
= mpz_sizeinbase(value
, 2); /* size in bits */
420 n
.len
= 1 + bits
/ 8; /* size in bytes */
421 n
.ptr
= mpz_export(NULL
, NULL
, 1, n
.len
, 1, 0, value
);
423 return asn1_wrap(ASN1_INTEGER
, "m", n
);
427 * Implementation of private_key_t.get_encoding.
429 static chunk_t
get_encoding(private_gmp_rsa_private_key_t
*this)
431 return asn1_wrap(ASN1_SEQUENCE
, "cmmmmmmmm",
433 gmp_mpz_to_asn1(this->n
),
434 gmp_mpz_to_asn1(this->e
),
435 gmp_mpz_to_asn1(this->d
),
436 gmp_mpz_to_asn1(this->p
),
437 gmp_mpz_to_asn1(this->q
),
438 gmp_mpz_to_asn1(this->exp1
),
439 gmp_mpz_to_asn1(this->exp2
),
440 gmp_mpz_to_asn1(this->coeff
));
444 * Implementation of gmp_rsa_private_key.destroy.
446 static private_gmp_rsa_private_key_t
* get_ref(private_gmp_rsa_private_key_t
*this)
454 * Implementation of gmp_rsa_private_key.destroy.
456 static void destroy(private_gmp_rsa_private_key_t
*this)
458 if (ref_put(&this->ref
))
460 mpz_clear_randomized(this->n
);
461 mpz_clear_randomized(this->e
);
462 mpz_clear_randomized(this->p
);
463 mpz_clear_randomized(this->q
);
464 mpz_clear_randomized(this->d
);
465 mpz_clear_randomized(this->exp1
);
466 mpz_clear_randomized(this->exp2
);
467 mpz_clear_randomized(this->coeff
);
468 DESTROY_IF(this->keyid
);
469 DESTROY_IF(this->keyid_info
);
475 * Check the loaded key if it is valid and usable
477 static status_t
check(private_gmp_rsa_private_key_t
*this)
480 status_t status
= SUCCESS
;
482 /* PKCS#1 1.5 section 6 requires modulus to have at least 12 octets.
483 * We actually require more (for security).
487 DBG1("key shorter than 512 bits");
491 /* we picked a max modulus size to simplify buffer allocation */
492 if (this->k
> 8192/8)
494 DBG1("key larger thant 8192 bits");
502 /* check that n == p * q */
503 mpz_mul(u
, this->p
, this->q
);
504 if (mpz_cmp(u
, this->n
) != 0)
509 /* check that e divides neither p-1 nor q-1 */
510 mpz_sub_ui(t
, this->p
, 1);
511 mpz_mod(t
, t
, this->e
);
512 if (mpz_cmp_ui(t
, 0) == 0)
517 mpz_sub_ui(t
, this->q
, 1);
518 mpz_mod(t
, t
, this->e
);
519 if (mpz_cmp_ui(t
, 0) == 0)
524 /* check that d is e^-1 (mod lcm(p-1, q-1)) */
525 /* see PKCS#1v2, aka RFC 2437, for the "lcm" */
526 mpz_sub_ui(q1
, this->q
, 1);
527 mpz_sub_ui(u
, this->p
, 1);
528 mpz_gcd(t
, u
, q1
); /* t := gcd(p-1, q-1) */
529 mpz_mul(u
, u
, q1
); /* u := (p-1) * (q-1) */
530 mpz_divexact(u
, u
, t
); /* u := lcm(p-1, q-1) */
532 mpz_mul(t
, this->d
, this->e
);
534 if (mpz_cmp_ui(t
, 1) != 0)
539 /* check that exp1 is d mod (p-1) */
540 mpz_sub_ui(u
, this->p
, 1);
541 mpz_mod(t
, this->d
, u
);
542 if (mpz_cmp(t
, this->exp1
) != 0)
547 /* check that exp2 is d mod (q-1) */
548 mpz_sub_ui(u
, this->q
, 1);
549 mpz_mod(t
, this->d
, u
);
550 if (mpz_cmp(t
, this->exp2
) != 0)
555 /* check that coeff is (q^-1) mod p */
556 mpz_mul(t
, this->coeff
, this->q
);
557 mpz_mod(t
, t
, this->p
);
558 if (mpz_cmp_ui(t
, 1) != 0)
563 mpz_clear_randomized(t
);
564 mpz_clear_randomized(u
);
565 mpz_clear_randomized(q1
);
566 if (status
!= SUCCESS
)
568 DBG1("key integrity tests failed");
574 * Internal generic constructor
576 static private_gmp_rsa_private_key_t
*gmp_rsa_private_key_create_empty(void)
578 private_gmp_rsa_private_key_t
*this = malloc_thing(private_gmp_rsa_private_key_t
);
580 this->public.interface
.get_type
= (key_type_t (*)(private_key_t
*this))get_type
;
581 this->public.interface
.sign
= (bool (*)(private_key_t
*this, signature_scheme_t scheme
, chunk_t data
, chunk_t
*signature
))sign
;
582 this->public.interface
.decrypt
= (bool (*)(private_key_t
*this, chunk_t crypto
, chunk_t
*plain
))decrypt
;
583 this->public.interface
.get_keysize
= (size_t (*) (private_key_t
*this))get_keysize
;
584 this->public.interface
.get_id
= (identification_t
* (*) (private_key_t
*this,id_type_t
))get_id
;
585 this->public.interface
.get_public_key
= (public_key_t
* (*)(private_key_t
*this))get_public_key
;
586 this->public.interface
.belongs_to
= (bool (*) (private_key_t
*this, public_key_t
*public))belongs_to
;
587 this->public.interface
.get_encoding
= (chunk_t(*)(private_key_t
*))get_encoding
;
588 this->public.interface
.get_ref
= (private_key_t
* (*)(private_key_t
*this))get_ref
;
589 this->public.interface
.destroy
= (void (*)(private_key_t
*this))destroy
;
592 this->keyid_info
= NULL
;
599 * Generate an RSA key of specified key size
601 static gmp_rsa_private_key_t
*generate(size_t key_size
)
603 mpz_t p
, q
, n
, e
, d
, exp1
, exp2
, coeff
;
605 private_gmp_rsa_private_key_t
*this = gmp_rsa_private_key_create_empty();
607 key_size
= key_size
/ 8;
609 /* Get values of primes p and q */
610 if (compute_prime(this, key_size
/2, &p
) != SUCCESS
)
615 if (compute_prime(this, key_size
/2, &q
) != SUCCESS
)
629 /* Swapping Primes so p is larger then q */
630 if (mpz_cmp(p
, q
) < 0)
635 mpz_mul(n
, p
, q
); /* n = p*q */
636 mpz_init_set_ui(e
, PUBLIC_EXPONENT
); /* assign public exponent */
637 mpz_init_set(m
, p
); /* m = p */
638 mpz_sub_ui(m
, m
, 1); /* m = m -1 */
639 mpz_init_set(q1
, q
); /* q1 = q */
640 mpz_sub_ui(q1
, q1
, 1); /* q1 = q1 -1 */
641 mpz_gcd(t
, m
, q1
); /* t = gcd(p-1, q-1) */
642 mpz_mul(m
, m
, q1
); /* m = (p-1)*(q-1) */
643 mpz_divexact(m
, m
, t
); /* m = m / t */
644 mpz_gcd(t
, m
, e
); /* t = gcd(m, e) */
646 mpz_invert(d
, e
, m
); /* e has an inverse mod m */
647 if (mpz_cmp_ui(d
, 0) < 0) /* make sure d is positive */
651 mpz_sub_ui(t
, p
, 1); /* t = p-1 */
652 mpz_mod(exp1
, d
, t
); /* exp1 = d mod p-1 */
653 mpz_sub_ui(t
, q
, 1); /* t = q-1 */
654 mpz_mod(exp2
, d
, t
); /* exp2 = d mod q-1 */
656 mpz_invert(coeff
, q
, p
); /* coeff = q^-1 mod p */
657 if (mpz_cmp_ui(coeff
, 0) < 0) /* make coeff d is positive */
659 mpz_add(coeff
, coeff
, p
);
662 mpz_clear_randomized(q1
);
663 mpz_clear_randomized(m
);
664 mpz_clear_randomized(t
);
672 *(this->exp1
) = *exp1
;
673 *(this->exp2
) = *exp2
;
674 *(this->coeff
) = *coeff
;
676 /* set key size in bytes */
679 return &this->public;
683 * load private key from a ASN1 encoded blob
685 static gmp_rsa_private_key_t
*load(chunk_t blob
)
691 private_gmp_rsa_private_key_t
*this = gmp_rsa_private_key_create_empty();
698 mpz_init(this->exp1
);
699 mpz_init(this->exp2
);
700 mpz_init(this->coeff
);
702 asn1_init(&ctx
, blob
, 0, FALSE
, TRUE
);
704 while (objectID
< PRIV_KEY_ROOF
)
706 if (!extract_object(privkey_objects
, &objectID
, &object
, &level
, &ctx
))
708 chunk_free_randomized(&blob
);
714 case PRIV_KEY_VERSION
:
715 if (object
.len
> 0 && *object
.ptr
!= 0)
717 chunk_free_randomized(&blob
);
722 case PRIV_KEY_MODULUS
:
723 mpz_import(this->n
, object
.len
, 1, 1, 1, 0, object
.ptr
);
725 case PRIV_KEY_PUB_EXP
:
726 mpz_import(this->e
, object
.len
, 1, 1, 1, 0, object
.ptr
);
728 case PRIV_KEY_PRIV_EXP
:
729 mpz_import(this->d
, object
.len
, 1, 1, 1, 0, object
.ptr
);
731 case PRIV_KEY_PRIME1
:
732 mpz_import(this->p
, object
.len
, 1, 1, 1, 0, object
.ptr
);
734 case PRIV_KEY_PRIME2
:
735 mpz_import(this->q
, object
.len
, 1, 1, 1, 0, object
.ptr
);
738 mpz_import(this->exp1
, object
.len
, 1, 1, 1, 0, object
.ptr
);
741 mpz_import(this->exp2
, object
.len
, 1, 1, 1, 0, object
.ptr
);
744 mpz_import(this->coeff
, object
.len
, 1, 1, 1, 0, object
.ptr
);
749 chunk_free_randomized(&blob
);
751 this->k
= (mpz_sizeinbase(this->n
, 2) + 7) / BITS_PER_BYTE
;
752 if (!gmp_rsa_public_key_build_id(this->n
, this->e
,
753 &this->keyid
, &this->keyid_info
))
759 if (check(this) != SUCCESS
)
764 return &this->public;
767 typedef struct private_builder_t private_builder_t
;
769 * Builder implementation for key loading/generation
771 struct private_builder_t
{
772 /** implements the builder interface */
774 /** loaded/generated private key */
775 gmp_rsa_private_key_t
*key
;
779 * Implementation of builder_t.build
781 static gmp_rsa_private_key_t
*build(private_builder_t
*this)
783 gmp_rsa_private_key_t
*key
= this->key
;
790 * Implementation of builder_t.add
792 static void add(private_builder_t
*this, builder_part_t part
, ...)
798 DBG1("ignoring surplus build part %N", builder_part_names
, part
);
804 case BUILD_BLOB_ASN1_DER
:
806 va_start(args
, part
);
807 this->key
= load(va_arg(args
, chunk_t
));
813 va_start(args
, part
);
814 this->key
= generate(va_arg(args
, u_int
));
819 DBG1("ignoring unsupported build part %N", builder_part_names
, part
);
825 * Builder construction function
827 builder_t
*gmp_rsa_private_key_builder(key_type_t type
)
829 private_builder_t
*this;
836 this = malloc_thing(private_builder_t
);
839 this->public.add
= (void(*)(builder_t
*this, builder_part_t part
, ...))add
;
840 this->public.build
= (void*(*)(builder_t
*this))build
;
842 return &this->public;