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
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>
32 * Public exponent to use for key generation.
34 #define PUBLIC_EXPONENT 0x10001
36 typedef struct private_gmp_rsa_private_key_t private_gmp_rsa_private_key_t
;
39 * Private data of a gmp_rsa_private_key_t object.
41 struct private_gmp_rsa_private_key_t
{
43 * Public interface for this signer.
45 gmp_rsa_private_key_t
public;
48 * Version of key, as encoded in PKCS#1
88 * Private coefficient.
98 * Keyid formed as a SHA-1 hash of a publicKey object
100 identification_t
* keyid
;
103 * Keyid formed as a SHA-1 hash of a publicKeyInfo object
105 identification_t
* keyid_info
;
114 * Shared functions defined in gmp_rsa_public_key.c
116 extern bool gmp_rsa_public_key_build_id(mpz_t n
, mpz_t e
,
117 identification_t
**keyid
,
118 identification_t
**keyid_info
);
119 extern gmp_rsa_public_key_t
*gmp_rsa_public_key_create_from_n_e(mpz_t n
, mpz_t e
);
122 * Auxiliary function overwriting private key material with zero bytes
124 static void mpz_clear_randomized(mpz_t z
)
126 size_t len
= mpz_size(z
) * GMP_LIMB_BITS
/ BITS_PER_BYTE
;
127 u_int8_t
*random
= alloca(len
);
129 memset(random
, 0, len
);
130 /* overwrite mpz_t with zero bytes before clearing it */
131 mpz_import(z
, len
, 1, 1, 1, 0, random
);
136 * Create a mpz prime of at least prime_size
138 static status_t
compute_prime(private_gmp_rsa_private_key_t
*this,
139 size_t prime_size
, mpz_t
*prime
)
142 chunk_t random_bytes
;
144 rng
= lib
->crypto
->create_rng(lib
->crypto
, RNG_TRUE
);
147 DBG1("no RNG of quality %N found", rng_quality_names
, RNG_TRUE
);
154 rng
->allocate_bytes(rng
, prime_size
, &random_bytes
);
155 /* make sure most significant bit is set */
156 random_bytes
.ptr
[0] = random_bytes
.ptr
[0] | 0x80;
158 mpz_import(*prime
, random_bytes
.len
, 1, 1, 1, 0, random_bytes
.ptr
);
159 mpz_nextprime (*prime
, *prime
);
160 chunk_clear(&random_bytes
);
162 /* check if it isn't too large */
163 while (((mpz_sizeinbase(*prime
, 2) + 7) / 8) > prime_size
);
170 * PKCS#1 RSADP function
172 static chunk_t
rsadp(private_gmp_rsa_private_key_t
*this, chunk_t data
)
180 mpz_import(t1
, data
.len
, 1, 1, 1, 0, data
.ptr
);
182 mpz_powm(t2
, t1
, this->exp1
, this->p
); /* m1 = c^dP mod p */
183 mpz_powm(t1
, t1
, this->exp2
, this->q
); /* m2 = c^dQ mod Q */
184 mpz_sub(t2
, t2
, t1
); /* h = qInv (m1 - m2) mod p */
185 mpz_mod(t2
, t2
, this->p
);
186 mpz_mul(t2
, t2
, this->coeff
);
187 mpz_mod(t2
, t2
, this->p
);
189 mpz_mul(t2
, t2
, this->q
); /* m = m2 + h q */
192 decrypted
.len
= this->k
;
193 decrypted
.ptr
= mpz_export(NULL
, NULL
, 1, decrypted
.len
, 1, 0, t1
);
194 if (decrypted
.ptr
== NULL
)
199 mpz_clear_randomized(t1
);
200 mpz_clear_randomized(t2
);
206 * PKCS#1 RSASP1 function
208 static chunk_t
rsasp1(private_gmp_rsa_private_key_t
*this, chunk_t data
)
210 return rsadp(this, data
);
214 * Implementation of gmp_rsa_private_key_t.build_emsa_pkcs1_signature.
216 static bool build_emsa_pkcs1_signature(private_gmp_rsa_private_key_t
*this,
217 hash_algorithm_t hash_algorithm
,
218 chunk_t data
, chunk_t
*signature
)
220 chunk_t digestInfo
= chunk_empty
;
223 if (hash_algorithm
!= HASH_UNKNOWN
)
227 int hash_oid
= hasher_algorithm_to_oid(hash_algorithm
);
229 if (hash_oid
== OID_UNKNOWN
)
234 hasher
= lib
->crypto
->create_hasher(lib
->crypto
, hash_algorithm
);
239 hasher
->allocate_hash(hasher
, data
, &hash
);
240 hasher
->destroy(hasher
);
242 /* build DER-encoded digestInfo */
243 digestInfo
= asn1_wrap(ASN1_SEQUENCE
, "cm",
244 asn1_algorithmIdentifier(hash_oid
),
245 asn1_simple_object(ASN1_OCTET_STRING
, hash
)
251 /* build chunk to rsa-decrypt:
252 * EM = 0x00 || 0x01 || PS || 0x00 || T.
253 * PS = 0xFF padding, with length to fill em
257 em
.ptr
= malloc(em
.len
);
259 /* fill em with padding */
260 memset(em
.ptr
, 0xFF, em
.len
);
261 /* set magic bytes */
264 *(em
.ptr
+ em
.len
- data
.len
- 1) = 0x00;
265 /* set DER-encoded hash */
266 memcpy(em
.ptr
+ em
.len
- data
.len
, data
.ptr
, data
.len
);
268 /* build signature */
269 *signature
= rsasp1(this, em
);
271 free(digestInfo
.ptr
);
278 * Implementation of gmp_rsa_private_key.get_type.
280 static key_type_t
get_type(private_gmp_rsa_private_key_t
*this)
286 * Implementation of gmp_rsa_private_key.sign.
288 static bool sign(private_gmp_rsa_private_key_t
*this, signature_scheme_t scheme
,
289 chunk_t data
, chunk_t
*signature
)
293 case SIGN_RSA_EMSA_PKCS1_NULL
:
294 return build_emsa_pkcs1_signature(this, HASH_UNKNOWN
, data
, signature
);
296 case SIGN_RSA_EMSA_PKCS1_SHA1
:
297 return build_emsa_pkcs1_signature(this, HASH_SHA1
, 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,
317 chunk_t crypto
, chunk_t
*plain
)
319 DBG1("RSA private key decryption not implemented");
324 * Implementation of gmp_rsa_private_key.get_keysize.
326 static size_t get_keysize(private_gmp_rsa_private_key_t
*this)
332 * Implementation of gmp_rsa_private_key.get_id.
334 static identification_t
* get_id(private_gmp_rsa_private_key_t
*this,
339 case ID_PUBKEY_INFO_SHA1
:
340 return this->keyid_info
;
349 * Implementation of gmp_rsa_private_key.get_public_key.
351 static gmp_rsa_public_key_t
* get_public_key(private_gmp_rsa_private_key_t
*this)
353 return gmp_rsa_public_key_create_from_n_e(this->n
, this->e
);
357 * Implementation of gmp_rsa_private_key.equals.
359 static bool equals(private_gmp_rsa_private_key_t
*this, private_key_t
*other
)
361 identification_t
*keyid
;
363 if (&this->public.interface
== other
)
367 if (other
->get_type(other
) != KEY_RSA
)
371 keyid
= other
->get_id(other
, ID_PUBKEY_SHA1
);
372 if (keyid
&& keyid
->equals(keyid
, this->keyid
))
376 keyid
= other
->get_id(other
, ID_PUBKEY_INFO_SHA1
);
377 if (keyid
&& keyid
->equals(keyid
, this->keyid_info
))
385 * Implementation of gmp_rsa_private_key.belongs_to.
387 static bool belongs_to(private_gmp_rsa_private_key_t
*this, public_key_t
*public)
389 identification_t
*keyid
;
391 if (public->get_type(public) != KEY_RSA
)
395 keyid
= public->get_id(public, ID_PUBKEY_SHA1
);
396 if (keyid
&& keyid
->equals(keyid
, this->keyid
))
400 keyid
= public->get_id(public, ID_PUBKEY_INFO_SHA1
);
401 if (keyid
&& keyid
->equals(keyid
, this->keyid_info
))
409 * Convert a MP integer into a chunk_t
411 chunk_t
gmp_mpz_to_chunk(const mpz_t value
)
415 n
.len
= 1 + mpz_sizeinbase(value
, 2) / BITS_PER_BYTE
;
416 n
.ptr
= mpz_export(NULL
, NULL
, 1, n
.len
, 1, 0, value
);
418 { /* if we have zero in "value", gmp returns NULL */
425 * Convert a MP integer into a DER coded ASN.1 object
427 chunk_t
gmp_mpz_to_asn1(const mpz_t value
)
429 return asn1_wrap(ASN1_INTEGER
, "m", gmp_mpz_to_chunk(value
));
433 * Implementation of private_key_t.get_encoding.
435 static chunk_t
get_encoding(private_gmp_rsa_private_key_t
*this)
437 return asn1_wrap(ASN1_SEQUENCE
, "cmmmmmmmm",
439 gmp_mpz_to_asn1(this->n
),
440 gmp_mpz_to_asn1(this->e
),
441 gmp_mpz_to_asn1(this->d
),
442 gmp_mpz_to_asn1(this->p
),
443 gmp_mpz_to_asn1(this->q
),
444 gmp_mpz_to_asn1(this->exp1
),
445 gmp_mpz_to_asn1(this->exp2
),
446 gmp_mpz_to_asn1(this->coeff
));
450 * Implementation of gmp_rsa_private_key.get_ref.
452 static private_gmp_rsa_private_key_t
* get_ref(private_gmp_rsa_private_key_t
*this)
460 * Implementation of gmp_rsa_private_key.destroy.
462 static void destroy(private_gmp_rsa_private_key_t
*this)
464 if (ref_put(&this->ref
))
466 mpz_clear_randomized(this->n
);
467 mpz_clear_randomized(this->e
);
468 mpz_clear_randomized(this->p
);
469 mpz_clear_randomized(this->q
);
470 mpz_clear_randomized(this->d
);
471 mpz_clear_randomized(this->exp1
);
472 mpz_clear_randomized(this->exp2
);
473 mpz_clear_randomized(this->coeff
);
474 DESTROY_IF(this->keyid
);
475 DESTROY_IF(this->keyid_info
);
481 * Check the loaded key if it is valid and usable
483 static status_t
check(private_gmp_rsa_private_key_t
*this)
486 status_t status
= SUCCESS
;
488 /* PKCS#1 1.5 section 6 requires modulus to have at least 12 octets.
489 * We actually require more (for security).
491 if (this->k
< 512 / BITS_PER_BYTE
)
493 DBG1("key shorter than 512 bits");
497 /* we picked a max modulus size to simplify buffer allocation */
498 if (this->k
> 8192 / BITS_PER_BYTE
)
500 DBG1("key larger than 8192 bits");
508 /* check that n == p * q */
509 mpz_mul(u
, this->p
, this->q
);
510 if (mpz_cmp(u
, this->n
) != 0)
515 /* check that e divides neither p-1 nor q-1 */
516 mpz_sub_ui(t
, this->p
, 1);
517 mpz_mod(t
, t
, this->e
);
518 if (mpz_cmp_ui(t
, 0) == 0)
523 mpz_sub_ui(t
, this->q
, 1);
524 mpz_mod(t
, t
, this->e
);
525 if (mpz_cmp_ui(t
, 0) == 0)
530 /* check that d is e^-1 (mod lcm(p-1, q-1)) */
531 /* see PKCS#1v2, aka RFC 2437, for the "lcm" */
532 mpz_sub_ui(q1
, this->q
, 1);
533 mpz_sub_ui(u
, this->p
, 1);
534 mpz_gcd(t
, u
, q1
); /* t := gcd(p-1, q-1) */
535 mpz_mul(u
, u
, q1
); /* u := (p-1) * (q-1) */
536 mpz_divexact(u
, u
, t
); /* u := lcm(p-1, q-1) */
538 mpz_mul(t
, this->d
, this->e
);
540 if (mpz_cmp_ui(t
, 1) != 0)
545 /* check that exp1 is d mod (p-1) */
546 mpz_sub_ui(u
, this->p
, 1);
547 mpz_mod(t
, this->d
, u
);
548 if (mpz_cmp(t
, this->exp1
) != 0)
553 /* check that exp2 is d mod (q-1) */
554 mpz_sub_ui(u
, this->q
, 1);
555 mpz_mod(t
, this->d
, u
);
556 if (mpz_cmp(t
, this->exp2
) != 0)
561 /* check that coeff is (q^-1) mod p */
562 mpz_mul(t
, this->coeff
, this->q
);
563 mpz_mod(t
, t
, this->p
);
564 if (mpz_cmp_ui(t
, 1) != 0)
569 mpz_clear_randomized(t
);
570 mpz_clear_randomized(u
);
571 mpz_clear_randomized(q1
);
572 if (status
!= SUCCESS
)
574 DBG1("key integrity tests failed");
580 * Internal generic constructor
582 static private_gmp_rsa_private_key_t
*gmp_rsa_private_key_create_empty(void)
584 private_gmp_rsa_private_key_t
*this = malloc_thing(private_gmp_rsa_private_key_t
);
586 this->public.interface
.get_type
= (key_type_t (*) (private_key_t
*))get_type
;
587 this->public.interface
.sign
= (bool (*) (private_key_t
*, signature_scheme_t
, chunk_t
, chunk_t
*))sign
;
588 this->public.interface
.decrypt
= (bool (*) (private_key_t
*, chunk_t
, chunk_t
*))decrypt
;
589 this->public.interface
.get_keysize
= (size_t (*) (private_key_t
*))get_keysize
;
590 this->public.interface
.get_id
= (identification_t
* (*) (private_key_t
*, id_type_t
))get_id
;
591 this->public.interface
.get_public_key
= (public_key_t
* (*) (private_key_t
*))get_public_key
;
592 this->public.interface
.equals
= (bool (*) (private_key_t
*, private_key_t
*))equals
;
593 this->public.interface
.belongs_to
= (bool (*) (private_key_t
*, public_key_t
*))belongs_to
;
594 this->public.interface
.get_encoding
= (chunk_t (*) (private_key_t
*))get_encoding
;
595 this->public.interface
.get_ref
= (private_key_t
* (*) (private_key_t
*))get_ref
;
596 this->public.interface
.destroy
= (void (*) (private_key_t
*))destroy
;
599 this->keyid_info
= NULL
;
606 * Generate an RSA key of specified key size
608 static gmp_rsa_private_key_t
*generate(size_t key_size
)
610 mpz_t p
, q
, n
, e
, d
, exp1
, exp2
, coeff
;
612 private_gmp_rsa_private_key_t
*this = gmp_rsa_private_key_create_empty();
614 key_size
= key_size
/ BITS_PER_BYTE
;
616 /* Get values of primes p and q */
617 if (compute_prime(this, key_size
/2, &p
) != SUCCESS
)
622 if (compute_prime(this, key_size
/2, &q
) != SUCCESS
)
636 /* Swapping Primes so p is larger then q */
637 if (mpz_cmp(p
, q
) < 0)
642 mpz_mul(n
, p
, q
); /* n = p*q */
643 mpz_init_set_ui(e
, PUBLIC_EXPONENT
); /* assign public exponent */
644 mpz_init_set(m
, p
); /* m = p */
645 mpz_sub_ui(m
, m
, 1); /* m = m -1 */
646 mpz_init_set(q1
, q
); /* q1 = q */
647 mpz_sub_ui(q1
, q1
, 1); /* q1 = q1 -1 */
648 mpz_gcd(t
, m
, q1
); /* t = gcd(p-1, q-1) */
649 mpz_mul(m
, m
, q1
); /* m = (p-1)*(q-1) */
650 mpz_divexact(m
, m
, t
); /* m = m / t */
651 mpz_gcd(t
, m
, e
); /* t = gcd(m, e) */
653 mpz_invert(d
, e
, m
); /* e has an inverse mod m */
654 if (mpz_cmp_ui(d
, 0) < 0) /* make sure d is positive */
658 mpz_sub_ui(t
, p
, 1); /* t = p-1 */
659 mpz_mod(exp1
, d
, t
); /* exp1 = d mod p-1 */
660 mpz_sub_ui(t
, q
, 1); /* t = q-1 */
661 mpz_mod(exp2
, d
, t
); /* exp2 = d mod q-1 */
663 mpz_invert(coeff
, q
, p
); /* coeff = q^-1 mod p */
664 if (mpz_cmp_ui(coeff
, 0) < 0) /* make coeff d is positive */
666 mpz_add(coeff
, coeff
, p
);
669 mpz_clear_randomized(q1
);
670 mpz_clear_randomized(m
);
671 mpz_clear_randomized(t
);
679 *(this->exp1
) = *exp1
;
680 *(this->exp2
) = *exp2
;
681 *(this->coeff
) = *coeff
;
683 /* set key size in bytes */
686 return &this->public;
690 * ASN.1 definition of a PKCS#1 RSA private key
692 static const asn1Object_t privkeyObjects
[] = {
693 { 0, "RSAPrivateKey", ASN1_SEQUENCE
, ASN1_NONE
}, /* 0 */
694 { 1, "version", ASN1_INTEGER
, ASN1_BODY
}, /* 1 */
695 { 1, "modulus", ASN1_INTEGER
, ASN1_BODY
}, /* 2 */
696 { 1, "publicExponent", ASN1_INTEGER
, ASN1_BODY
}, /* 3 */
697 { 1, "privateExponent", ASN1_INTEGER
, ASN1_BODY
}, /* 4 */
698 { 1, "prime1", ASN1_INTEGER
, ASN1_BODY
}, /* 5 */
699 { 1, "prime2", ASN1_INTEGER
, ASN1_BODY
}, /* 6 */
700 { 1, "exponent1", ASN1_INTEGER
, ASN1_BODY
}, /* 7 */
701 { 1, "exponent2", ASN1_INTEGER
, ASN1_BODY
}, /* 8 */
702 { 1, "coefficient", ASN1_INTEGER
, ASN1_BODY
}, /* 9 */
703 { 1, "otherPrimeInfos", ASN1_SEQUENCE
, ASN1_OPT
|
704 ASN1_LOOP
}, /* 10 */
705 { 2, "otherPrimeInfo", ASN1_SEQUENCE
, ASN1_NONE
}, /* 11 */
706 { 3, "prime", ASN1_INTEGER
, ASN1_BODY
}, /* 12 */
707 { 3, "exponent", ASN1_INTEGER
, ASN1_BODY
}, /* 13 */
708 { 3, "coefficient", ASN1_INTEGER
, ASN1_BODY
}, /* 14 */
709 { 1, "end opt or loop", ASN1_EOC
, ASN1_END
}, /* 15 */
710 { 0, "exit", ASN1_EOC
, ASN1_EXIT
}
712 #define PRIV_KEY_VERSION 1
713 #define PRIV_KEY_MODULUS 2
714 #define PRIV_KEY_PUB_EXP 3
715 #define PRIV_KEY_PRIV_EXP 4
716 #define PRIV_KEY_PRIME1 5
717 #define PRIV_KEY_PRIME2 6
718 #define PRIV_KEY_EXP1 7
719 #define PRIV_KEY_EXP2 8
720 #define PRIV_KEY_COEFF 9
723 * load private key from a ASN1 encoded blob
725 static gmp_rsa_private_key_t
*load_asn1_der(chunk_t blob
)
727 asn1_parser_t
*parser
;
730 bool success
= FALSE
;
732 private_gmp_rsa_private_key_t
*this = gmp_rsa_private_key_create_empty();
739 mpz_init(this->exp1
);
740 mpz_init(this->exp2
);
741 mpz_init(this->coeff
);
743 parser
= asn1_parser_create(privkeyObjects
, blob
);
744 parser
->set_flags(parser
, FALSE
, TRUE
);
746 while (parser
->iterate(parser
, &objectID
, &object
))
750 case PRIV_KEY_VERSION
:
751 if (object
.len
> 0 && *object
.ptr
!= 0)
753 DBG1("PKCS#1 private key format is not version 1");
757 case PRIV_KEY_MODULUS
:
758 mpz_import(this->n
, object
.len
, 1, 1, 1, 0, object
.ptr
);
760 case PRIV_KEY_PUB_EXP
:
761 mpz_import(this->e
, object
.len
, 1, 1, 1, 0, object
.ptr
);
763 case PRIV_KEY_PRIV_EXP
:
764 mpz_import(this->d
, object
.len
, 1, 1, 1, 0, object
.ptr
);
766 case PRIV_KEY_PRIME1
:
767 mpz_import(this->p
, object
.len
, 1, 1, 1, 0, object
.ptr
);
769 case PRIV_KEY_PRIME2
:
770 mpz_import(this->q
, object
.len
, 1, 1, 1, 0, object
.ptr
);
773 mpz_import(this->exp1
, object
.len
, 1, 1, 1, 0, object
.ptr
);
776 mpz_import(this->exp2
, object
.len
, 1, 1, 1, 0, object
.ptr
);
779 mpz_import(this->coeff
, object
.len
, 1, 1, 1, 0, object
.ptr
);
783 success
= parser
->success(parser
);
786 parser
->destroy(parser
);
795 this->k
= (mpz_sizeinbase(this->n
, 2) + 7) / BITS_PER_BYTE
;
797 if (!gmp_rsa_public_key_build_id(this->n
, this->e
,
798 &this->keyid
, &this->keyid_info
))
803 if (check(this) != SUCCESS
)
808 return &this->public;
812 * load private key from an OpenPGP blob coded according to section
814 static gmp_rsa_private_key_t
*load_pgp(chunk_t blob
)
819 chunk_t packet
= blob
;
820 private_gmp_rsa_private_key_t
*this = gmp_rsa_private_key_create_empty();
827 mpz_init(this->exp1
);
828 mpz_init(this->exp2
);
829 mpz_init(this->coeff
);
831 /* string-to-key usage */
832 s2k
= pgp_length(&packet
, 1);
833 DBG2("L3 - string-to-key: %d", s2k
);
835 if (s2k
== 255 || s2k
== 254)
837 DBG1("string-to-key specifiers not supported");
840 DBG2(" %N", pgp_sym_alg_names
, s2k
);
842 if (s2k
!= PGP_SYM_ALG_PLAIN
)
844 DBG1("%N encryption not supported", pgp_sym_alg_names
, s2k
);
848 for (objectID
= PRIV_KEY_MODULUS
; objectID
<= PRIV_KEY_PRIME2
; objectID
++)
852 object
.len
= pgp_length(&packet
, 2);
854 if (object
.len
== PGP_INVALID_LENGTH
)
856 DBG1("OpenPGP length is invalid");
859 object
.len
= (object
.len
+ 7) / BITS_PER_BYTE
;
860 if (object
.len
> packet
.len
)
862 DBG1("OpenPGP field is too short");
865 object
.ptr
= packet
.ptr
;
866 packet
.ptr
+= object
.len
;
867 packet
.len
-= object
.len
;
871 case PRIV_KEY_MODULUS
:
872 mpz_import(this->n
, object
.len
, 1, 1, 1, 0, object
.ptr
);
874 case PRIV_KEY_PUB_EXP
:
875 mpz_import(this->e
, object
.len
, 1, 1, 1, 0, object
.ptr
);
877 case PRIV_KEY_PRIV_EXP
:
878 mpz_import(this->d
, object
.len
, 1, 1, 1, 0, object
.ptr
);
880 case PRIV_KEY_PRIME1
:
881 mpz_import(this->p
, object
.len
, 1, 1, 1, 0, object
.ptr
);
883 case PRIV_KEY_PRIME2
:
884 mpz_import(this->q
, object
.len
, 1, 1, 1, 0, object
.ptr
);
889 /* auxiliary variable */
892 /* exp1 = d mod (p-1) */
893 mpz_sub_ui(u
, this->p
, 1);
894 mpz_mod(this->exp1
, this->d
, u
);
896 /* exp2 = d mod (q-1) */
897 mpz_sub_ui(u
, this->q
, 1);
898 mpz_mod(this->exp2
, this->d
, u
);
900 /* coeff = (q^-1) mod p */
901 mpz_invert(this->coeff
, this->q
, this->p
);
902 if (mpz_cmp_ui(this->coeff
, 0) < 0)
904 mpz_add(this->coeff
, this->coeff
, this->p
);
909 this->k
= (mpz_sizeinbase(this->n
, 2) + 7) / BITS_PER_BYTE
;
911 if (!gmp_rsa_public_key_build_id(this->n
, this->e
,
912 &this->keyid
, &this->keyid_info
))
917 if (check(this) != SUCCESS
)
922 return &this->public;
930 typedef struct private_builder_t private_builder_t
;
932 * Builder implementation for key loading/generation
934 struct private_builder_t
{
935 /** implements the builder interface */
937 /** loaded/generated private key */
938 gmp_rsa_private_key_t
*key
;
942 * Implementation of builder_t.build
944 static gmp_rsa_private_key_t
*build(private_builder_t
*this)
946 gmp_rsa_private_key_t
*key
= this->key
;
953 * Implementation of builder_t.add
955 static void add(private_builder_t
*this, builder_part_t part
, ...)
964 case BUILD_BLOB_ASN1_DER
:
966 va_start(args
, part
);
967 chunk
= va_arg(args
, chunk_t
);
968 this->key
= load_asn1_der(chunk_clone(chunk
));
974 va_start(args
, part
);
975 chunk
= va_arg(args
, chunk_t
);
976 this->key
= load_pgp(chunk_clone(chunk
));
982 va_start(args
, part
);
983 this->key
= generate(va_arg(args
, u_int
));
993 destroy((private_gmp_rsa_private_key_t
*)this->key
);
995 builder_cancel(&this->public);
999 * Builder construction function
1001 builder_t
*gmp_rsa_private_key_builder(key_type_t type
)
1003 private_builder_t
*this;
1005 if (type
!= KEY_RSA
)
1010 this = malloc_thing(private_builder_t
);
1013 this->public.add
= (void(*)(builder_t
*this, builder_part_t part
, ...))add
;
1014 this->public.build
= (void*(*)(builder_t
*this))build
;
1016 return &this->public;