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 if (data
.len
> this->k
- 3)
253 free(digestInfo
.ptr
);
254 DBG1("unable to sign %d bytes using a %dbit key", data
.len
, this->k
* 8);
258 /* build chunk to rsa-decrypt:
259 * EM = 0x00 || 0x01 || PS || 0x00 || T.
260 * PS = 0xFF padding, with length to fill em
264 em
.ptr
= malloc(em
.len
);
266 /* fill em with padding */
267 memset(em
.ptr
, 0xFF, em
.len
);
268 /* set magic bytes */
271 *(em
.ptr
+ em
.len
- data
.len
- 1) = 0x00;
272 /* set DER-encoded hash */
273 memcpy(em
.ptr
+ em
.len
- data
.len
, data
.ptr
, data
.len
);
275 /* build signature */
276 *signature
= rsasp1(this, em
);
278 free(digestInfo
.ptr
);
285 * Implementation of gmp_rsa_private_key.get_type.
287 static key_type_t
get_type(private_gmp_rsa_private_key_t
*this)
293 * Implementation of gmp_rsa_private_key.sign.
295 static bool sign(private_gmp_rsa_private_key_t
*this, signature_scheme_t scheme
,
296 chunk_t data
, chunk_t
*signature
)
300 case SIGN_RSA_EMSA_PKCS1_NULL
:
301 return build_emsa_pkcs1_signature(this, HASH_UNKNOWN
, data
, signature
);
302 case SIGN_RSA_EMSA_PKCS1_SHA1
:
303 return build_emsa_pkcs1_signature(this, HASH_SHA1
, data
, signature
);
304 case SIGN_RSA_EMSA_PKCS1_SHA256
:
305 return build_emsa_pkcs1_signature(this, HASH_SHA256
, data
, signature
);
306 case SIGN_RSA_EMSA_PKCS1_SHA384
:
307 return build_emsa_pkcs1_signature(this, HASH_SHA384
, data
, signature
);
308 case SIGN_RSA_EMSA_PKCS1_SHA512
:
309 return build_emsa_pkcs1_signature(this, HASH_SHA512
, data
, signature
);
310 case SIGN_RSA_EMSA_PKCS1_MD5
:
311 return build_emsa_pkcs1_signature(this, HASH_MD5
, data
, signature
);
313 DBG1("signature scheme %N not supported in RSA",
314 signature_scheme_names
, scheme
);
320 * Implementation of gmp_rsa_private_key.decrypt.
322 static bool decrypt(private_gmp_rsa_private_key_t
*this, chunk_t crypto
,
325 chunk_t em
, stripped
;
326 bool success
= FALSE
;
328 /* rsa decryption using PKCS#1 RSADP */
329 stripped
= em
= rsadp(this, crypto
);
331 /* PKCS#1 v1.5 8.1 encryption-block formatting (EB = 00 || 02 || PS || 00 || D) */
333 /* check for hex pattern 00 02 in decrypted message */
334 if ((*stripped
.ptr
++ != 0x00) || (*(stripped
.ptr
++) != 0x02))
336 DBG1("incorrect padding - probably wrong rsa key");
341 /* the plaintext data starts after first 0x00 byte */
342 while (stripped
.len
-- > 0 && *stripped
.ptr
++ != 0x00)
344 if (stripped
.len
== 0)
346 DBG1("no plaintext data");
350 *plain
= chunk_clone(stripped
);
359 * Implementation of gmp_rsa_private_key.get_keysize.
361 static size_t get_keysize(private_gmp_rsa_private_key_t
*this)
367 * Implementation of gmp_rsa_private_key.get_id.
369 static identification_t
* get_id(private_gmp_rsa_private_key_t
*this,
374 case ID_PUBKEY_INFO_SHA1
:
375 return this->keyid_info
;
384 * Implementation of gmp_rsa_private_key.get_public_key.
386 static gmp_rsa_public_key_t
* get_public_key(private_gmp_rsa_private_key_t
*this)
388 return gmp_rsa_public_key_create_from_n_e(this->n
, this->e
);
392 * Implementation of gmp_rsa_private_key.equals.
394 static bool equals(private_gmp_rsa_private_key_t
*this, private_key_t
*other
)
396 identification_t
*keyid
;
398 if (&this->public.interface
== other
)
402 if (other
->get_type(other
) != KEY_RSA
)
406 keyid
= other
->get_id(other
, ID_PUBKEY_SHA1
);
407 if (keyid
&& keyid
->equals(keyid
, this->keyid
))
411 keyid
= other
->get_id(other
, ID_PUBKEY_INFO_SHA1
);
412 if (keyid
&& keyid
->equals(keyid
, this->keyid_info
))
420 * Implementation of gmp_rsa_private_key.belongs_to.
422 static bool belongs_to(private_gmp_rsa_private_key_t
*this, public_key_t
*public)
424 identification_t
*keyid
;
426 if (public->get_type(public) != KEY_RSA
)
430 keyid
= public->get_id(public, ID_PUBKEY_SHA1
);
431 if (keyid
&& keyid
->equals(keyid
, this->keyid
))
435 keyid
= public->get_id(public, ID_PUBKEY_INFO_SHA1
);
436 if (keyid
&& keyid
->equals(keyid
, this->keyid_info
))
444 * Convert a MP integer into a chunk_t
446 chunk_t
gmp_mpz_to_chunk(const mpz_t value
)
450 n
.len
= 1 + mpz_sizeinbase(value
, 2) / BITS_PER_BYTE
;
451 n
.ptr
= mpz_export(NULL
, NULL
, 1, n
.len
, 1, 0, value
);
453 { /* if we have zero in "value", gmp returns NULL */
460 * Convert a MP integer into a DER coded ASN.1 object
462 chunk_t
gmp_mpz_to_asn1(const mpz_t value
)
464 return asn1_wrap(ASN1_INTEGER
, "m", gmp_mpz_to_chunk(value
));
468 * Implementation of private_key_t.get_encoding.
470 static chunk_t
get_encoding(private_gmp_rsa_private_key_t
*this)
472 return asn1_wrap(ASN1_SEQUENCE
, "cmmmmmmmm",
474 gmp_mpz_to_asn1(this->n
),
475 gmp_mpz_to_asn1(this->e
),
476 gmp_mpz_to_asn1(this->d
),
477 gmp_mpz_to_asn1(this->p
),
478 gmp_mpz_to_asn1(this->q
),
479 gmp_mpz_to_asn1(this->exp1
),
480 gmp_mpz_to_asn1(this->exp2
),
481 gmp_mpz_to_asn1(this->coeff
));
485 * Implementation of gmp_rsa_private_key.get_ref.
487 static private_gmp_rsa_private_key_t
* get_ref(private_gmp_rsa_private_key_t
*this)
495 * Implementation of gmp_rsa_private_key.destroy.
497 static void destroy(private_gmp_rsa_private_key_t
*this)
499 if (ref_put(&this->ref
))
501 mpz_clear_randomized(this->n
);
502 mpz_clear_randomized(this->e
);
503 mpz_clear_randomized(this->p
);
504 mpz_clear_randomized(this->q
);
505 mpz_clear_randomized(this->d
);
506 mpz_clear_randomized(this->exp1
);
507 mpz_clear_randomized(this->exp2
);
508 mpz_clear_randomized(this->coeff
);
509 DESTROY_IF(this->keyid
);
510 DESTROY_IF(this->keyid_info
);
516 * Check the loaded key if it is valid and usable
518 static status_t
check(private_gmp_rsa_private_key_t
*this)
521 status_t status
= SUCCESS
;
523 /* PKCS#1 1.5 section 6 requires modulus to have at least 12 octets.
524 * We actually require more (for security).
526 if (this->k
< 512 / BITS_PER_BYTE
)
528 DBG1("key shorter than 512 bits");
532 /* we picked a max modulus size to simplify buffer allocation */
533 if (this->k
> 8192 / BITS_PER_BYTE
)
535 DBG1("key larger than 8192 bits");
543 /* check that n == p * q */
544 mpz_mul(u
, this->p
, this->q
);
545 if (mpz_cmp(u
, this->n
) != 0)
550 /* check that e divides neither p-1 nor q-1 */
551 mpz_sub_ui(t
, this->p
, 1);
552 mpz_mod(t
, t
, this->e
);
553 if (mpz_cmp_ui(t
, 0) == 0)
558 mpz_sub_ui(t
, this->q
, 1);
559 mpz_mod(t
, t
, this->e
);
560 if (mpz_cmp_ui(t
, 0) == 0)
565 /* check that d is e^-1 (mod lcm(p-1, q-1)) */
566 /* see PKCS#1v2, aka RFC 2437, for the "lcm" */
567 mpz_sub_ui(q1
, this->q
, 1);
568 mpz_sub_ui(u
, this->p
, 1);
569 mpz_gcd(t
, u
, q1
); /* t := gcd(p-1, q-1) */
570 mpz_mul(u
, u
, q1
); /* u := (p-1) * (q-1) */
571 mpz_divexact(u
, u
, t
); /* u := lcm(p-1, q-1) */
573 mpz_mul(t
, this->d
, this->e
);
575 if (mpz_cmp_ui(t
, 1) != 0)
580 /* check that exp1 is d mod (p-1) */
581 mpz_sub_ui(u
, this->p
, 1);
582 mpz_mod(t
, this->d
, u
);
583 if (mpz_cmp(t
, this->exp1
) != 0)
588 /* check that exp2 is d mod (q-1) */
589 mpz_sub_ui(u
, this->q
, 1);
590 mpz_mod(t
, this->d
, u
);
591 if (mpz_cmp(t
, this->exp2
) != 0)
596 /* check that coeff is (q^-1) mod p */
597 mpz_mul(t
, this->coeff
, this->q
);
598 mpz_mod(t
, t
, this->p
);
599 if (mpz_cmp_ui(t
, 1) != 0)
604 mpz_clear_randomized(t
);
605 mpz_clear_randomized(u
);
606 mpz_clear_randomized(q1
);
607 if (status
!= SUCCESS
)
609 DBG1("key integrity tests failed");
615 * Internal generic constructor
617 static private_gmp_rsa_private_key_t
*gmp_rsa_private_key_create_empty(void)
619 private_gmp_rsa_private_key_t
*this = malloc_thing(private_gmp_rsa_private_key_t
);
621 this->public.interface
.get_type
= (key_type_t (*) (private_key_t
*))get_type
;
622 this->public.interface
.sign
= (bool (*) (private_key_t
*, signature_scheme_t
, chunk_t
, chunk_t
*))sign
;
623 this->public.interface
.decrypt
= (bool (*) (private_key_t
*, chunk_t
, chunk_t
*))decrypt
;
624 this->public.interface
.get_keysize
= (size_t (*) (private_key_t
*))get_keysize
;
625 this->public.interface
.get_id
= (identification_t
* (*) (private_key_t
*, id_type_t
))get_id
;
626 this->public.interface
.get_public_key
= (public_key_t
* (*) (private_key_t
*))get_public_key
;
627 this->public.interface
.equals
= (bool (*) (private_key_t
*, private_key_t
*))equals
;
628 this->public.interface
.belongs_to
= (bool (*) (private_key_t
*, public_key_t
*))belongs_to
;
629 this->public.interface
.get_encoding
= (chunk_t (*) (private_key_t
*))get_encoding
;
630 this->public.interface
.get_ref
= (private_key_t
* (*) (private_key_t
*))get_ref
;
631 this->public.interface
.destroy
= (void (*) (private_key_t
*))destroy
;
634 this->keyid_info
= NULL
;
641 * Generate an RSA key of specified key size
643 static gmp_rsa_private_key_t
*generate(size_t key_size
)
645 mpz_t p
, q
, n
, e
, d
, exp1
, exp2
, coeff
;
647 private_gmp_rsa_private_key_t
*this = gmp_rsa_private_key_create_empty();
649 key_size
= key_size
/ BITS_PER_BYTE
;
651 /* Get values of primes p and q */
652 if (compute_prime(this, key_size
/2, &p
) != SUCCESS
)
657 if (compute_prime(this, key_size
/2, &q
) != SUCCESS
)
671 /* Swapping Primes so p is larger then q */
672 if (mpz_cmp(p
, q
) < 0)
677 mpz_mul(n
, p
, q
); /* n = p*q */
678 mpz_init_set_ui(e
, PUBLIC_EXPONENT
); /* assign public exponent */
679 mpz_init_set(m
, p
); /* m = p */
680 mpz_sub_ui(m
, m
, 1); /* m = m -1 */
681 mpz_init_set(q1
, q
); /* q1 = q */
682 mpz_sub_ui(q1
, q1
, 1); /* q1 = q1 -1 */
683 mpz_gcd(t
, m
, q1
); /* t = gcd(p-1, q-1) */
684 mpz_mul(m
, m
, q1
); /* m = (p-1)*(q-1) */
685 mpz_divexact(m
, m
, t
); /* m = m / t */
686 mpz_gcd(t
, m
, e
); /* t = gcd(m, e) */
688 mpz_invert(d
, e
, m
); /* e has an inverse mod m */
689 if (mpz_cmp_ui(d
, 0) < 0) /* make sure d is positive */
693 mpz_sub_ui(t
, p
, 1); /* t = p-1 */
694 mpz_mod(exp1
, d
, t
); /* exp1 = d mod p-1 */
695 mpz_sub_ui(t
, q
, 1); /* t = q-1 */
696 mpz_mod(exp2
, d
, t
); /* exp2 = d mod q-1 */
698 mpz_invert(coeff
, q
, p
); /* coeff = q^-1 mod p */
699 if (mpz_cmp_ui(coeff
, 0) < 0) /* make coeff d is positive */
701 mpz_add(coeff
, coeff
, p
);
704 mpz_clear_randomized(q1
);
705 mpz_clear_randomized(m
);
706 mpz_clear_randomized(t
);
714 *(this->exp1
) = *exp1
;
715 *(this->exp2
) = *exp2
;
716 *(this->coeff
) = *coeff
;
718 /* set key size in bytes */
721 return &this->public;
725 * ASN.1 definition of a PKCS#1 RSA private key
727 static const asn1Object_t privkeyObjects
[] = {
728 { 0, "RSAPrivateKey", ASN1_SEQUENCE
, ASN1_NONE
}, /* 0 */
729 { 1, "version", ASN1_INTEGER
, ASN1_BODY
}, /* 1 */
730 { 1, "modulus", ASN1_INTEGER
, ASN1_BODY
}, /* 2 */
731 { 1, "publicExponent", ASN1_INTEGER
, ASN1_BODY
}, /* 3 */
732 { 1, "privateExponent", ASN1_INTEGER
, ASN1_BODY
}, /* 4 */
733 { 1, "prime1", ASN1_INTEGER
, ASN1_BODY
}, /* 5 */
734 { 1, "prime2", ASN1_INTEGER
, ASN1_BODY
}, /* 6 */
735 { 1, "exponent1", ASN1_INTEGER
, ASN1_BODY
}, /* 7 */
736 { 1, "exponent2", ASN1_INTEGER
, ASN1_BODY
}, /* 8 */
737 { 1, "coefficient", ASN1_INTEGER
, ASN1_BODY
}, /* 9 */
738 { 1, "otherPrimeInfos", ASN1_SEQUENCE
, ASN1_OPT
|
739 ASN1_LOOP
}, /* 10 */
740 { 2, "otherPrimeInfo", ASN1_SEQUENCE
, ASN1_NONE
}, /* 11 */
741 { 3, "prime", ASN1_INTEGER
, ASN1_BODY
}, /* 12 */
742 { 3, "exponent", ASN1_INTEGER
, ASN1_BODY
}, /* 13 */
743 { 3, "coefficient", ASN1_INTEGER
, ASN1_BODY
}, /* 14 */
744 { 1, "end opt or loop", ASN1_EOC
, ASN1_END
}, /* 15 */
745 { 0, "exit", ASN1_EOC
, ASN1_EXIT
}
747 #define PRIV_KEY_VERSION 1
748 #define PRIV_KEY_MODULUS 2
749 #define PRIV_KEY_PUB_EXP 3
750 #define PRIV_KEY_PRIV_EXP 4
751 #define PRIV_KEY_PRIME1 5
752 #define PRIV_KEY_PRIME2 6
753 #define PRIV_KEY_EXP1 7
754 #define PRIV_KEY_EXP2 8
755 #define PRIV_KEY_COEFF 9
758 * load private key from a ASN1 encoded blob
760 static gmp_rsa_private_key_t
*load_asn1_der(chunk_t blob
)
762 asn1_parser_t
*parser
;
765 bool success
= FALSE
;
767 private_gmp_rsa_private_key_t
*this = gmp_rsa_private_key_create_empty();
774 mpz_init(this->exp1
);
775 mpz_init(this->exp2
);
776 mpz_init(this->coeff
);
778 parser
= asn1_parser_create(privkeyObjects
, blob
);
779 parser
->set_flags(parser
, FALSE
, TRUE
);
781 while (parser
->iterate(parser
, &objectID
, &object
))
785 case PRIV_KEY_VERSION
:
786 if (object
.len
> 0 && *object
.ptr
!= 0)
788 DBG1("PKCS#1 private key format is not version 1");
792 case PRIV_KEY_MODULUS
:
793 mpz_import(this->n
, object
.len
, 1, 1, 1, 0, object
.ptr
);
795 case PRIV_KEY_PUB_EXP
:
796 mpz_import(this->e
, object
.len
, 1, 1, 1, 0, object
.ptr
);
798 case PRIV_KEY_PRIV_EXP
:
799 mpz_import(this->d
, object
.len
, 1, 1, 1, 0, object
.ptr
);
801 case PRIV_KEY_PRIME1
:
802 mpz_import(this->p
, object
.len
, 1, 1, 1, 0, object
.ptr
);
804 case PRIV_KEY_PRIME2
:
805 mpz_import(this->q
, object
.len
, 1, 1, 1, 0, object
.ptr
);
808 mpz_import(this->exp1
, object
.len
, 1, 1, 1, 0, object
.ptr
);
811 mpz_import(this->exp2
, object
.len
, 1, 1, 1, 0, object
.ptr
);
814 mpz_import(this->coeff
, object
.len
, 1, 1, 1, 0, object
.ptr
);
818 success
= parser
->success(parser
);
821 parser
->destroy(parser
);
830 this->k
= (mpz_sizeinbase(this->n
, 2) + 7) / BITS_PER_BYTE
;
832 if (!gmp_rsa_public_key_build_id(this->n
, this->e
,
833 &this->keyid
, &this->keyid_info
))
838 if (check(this) != SUCCESS
)
843 return &this->public;
847 * load private key from an OpenPGP blob coded according to section
849 static gmp_rsa_private_key_t
*load_pgp(chunk_t blob
)
853 chunk_t packet
= blob
;
854 private_gmp_rsa_private_key_t
*this = gmp_rsa_private_key_create_empty();
861 mpz_init(this->exp1
);
862 mpz_init(this->exp2
);
863 mpz_init(this->coeff
);
865 for (objectID
= PRIV_KEY_MODULUS
; objectID
<= PRIV_KEY_COEFF
; objectID
++)
871 case PRIV_KEY_PRIV_EXP
:
875 /* string-to-key usage */
876 s2k
= pgp_length(&packet
, 1);
877 DBG2("L3 - string-to-key: %d", s2k
);
879 if (s2k
== 255 || s2k
== 254)
881 DBG1("string-to-key specifiers not supported");
884 DBG2(" %N", pgp_sym_alg_names
, s2k
);
886 if (s2k
!= PGP_SYM_ALG_PLAIN
)
888 DBG1("%N encryption not supported", pgp_sym_alg_names
, s2k
);
895 /* not contained in OpenPGP secret key payload */
901 DBG2("L3 - %s:", privkeyObjects
[objectID
].name
);
902 object
.len
= pgp_length(&packet
, 2);
904 if (object
.len
== PGP_INVALID_LENGTH
)
906 DBG1("OpenPGP length is invalid");
909 object
.len
= (object
.len
+ 7) / BITS_PER_BYTE
;
910 if (object
.len
> packet
.len
)
912 DBG1("OpenPGP field is too short");
915 object
.ptr
= packet
.ptr
;
916 packet
.ptr
+= object
.len
;
917 packet
.len
-= object
.len
;
922 case PRIV_KEY_MODULUS
:
923 mpz_import(this->n
, object
.len
, 1, 1, 1, 0, object
.ptr
);
925 case PRIV_KEY_PUB_EXP
:
926 mpz_import(this->e
, object
.len
, 1, 1, 1, 0, object
.ptr
);
928 case PRIV_KEY_PRIV_EXP
:
929 mpz_import(this->d
, object
.len
, 1, 1, 1, 0, object
.ptr
);
931 case PRIV_KEY_PRIME1
:
932 mpz_import(this->q
, object
.len
, 1, 1, 1, 0, object
.ptr
);
934 case PRIV_KEY_PRIME2
:
935 mpz_import(this->p
, object
.len
, 1, 1, 1, 0, object
.ptr
);
938 mpz_import(this->coeff
, object
.len
, 1, 1, 1, 0, object
.ptr
);
943 /* auxiliary variable */
946 /* exp1 = d mod (p-1) */
947 mpz_sub_ui(u
, this->p
, 1);
948 mpz_mod(this->exp1
, this->d
, u
);
950 /* exp2 = d mod (q-1) */
951 mpz_sub_ui(u
, this->q
, 1);
952 mpz_mod(this->exp2
, this->d
, u
);
957 this->k
= (mpz_sizeinbase(this->n
, 2) + 7) / BITS_PER_BYTE
;
959 if (!gmp_rsa_public_key_build_id(this->n
, this->e
,
960 &this->keyid
, &this->keyid_info
))
965 if (check(this) != SUCCESS
)
970 return &this->public;
978 typedef struct private_builder_t private_builder_t
;
980 * Builder implementation for key loading/generation
982 struct private_builder_t
{
983 /** implements the builder interface */
985 /** loaded/generated private key */
986 gmp_rsa_private_key_t
*key
;
990 * Implementation of builder_t.build
992 static gmp_rsa_private_key_t
*build(private_builder_t
*this)
994 gmp_rsa_private_key_t
*key
= this->key
;
1001 * Implementation of builder_t.add
1003 static void add(private_builder_t
*this, builder_part_t part
, ...)
1012 case BUILD_BLOB_ASN1_DER
:
1014 va_start(args
, part
);
1015 chunk
= va_arg(args
, chunk_t
);
1016 this->key
= load_asn1_der(chunk_clone(chunk
));
1020 case BUILD_BLOB_PGP
:
1022 va_start(args
, part
);
1023 chunk
= va_arg(args
, chunk_t
);
1024 this->key
= load_pgp(chunk_clone(chunk
));
1028 case BUILD_KEY_SIZE
:
1030 va_start(args
, part
);
1031 this->key
= generate(va_arg(args
, u_int
));
1041 destroy((private_gmp_rsa_private_key_t
*)this->key
);
1043 builder_cancel(&this->public);
1047 * Builder construction function
1049 builder_t
*gmp_rsa_private_key_builder(key_type_t type
)
1051 private_builder_t
*this;
1053 if (type
!= KEY_RSA
)
1058 this = malloc_thing(private_builder_t
);
1061 this->public.add
= (void(*)(builder_t
*this, builder_part_t part
, ...))add
;
1062 this->public.build
= (void*(*)(builder_t
*this))build
;
1064 return &this->public;