1 /* Support of PKCS#1 private key data structures
2 * Copyright (C) 2005 Jan Hutter, Martin Willi
3 * Copyright (C) 2002-2005 Andreas Steffen
4 * Hochschule fuer Technik Rapperswil, Switzerland
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
16 * RCSID $Id: pkcs1.c,v 1.17 2006/01/04 21:00:43 as Exp $
24 #include <libsha2/sha2.h>
26 #include "constants.h"
38 const struct fld RSA_private_field
[] =
40 { "Modulus", offsetof(RSA_private_key_t
, pub
.n
) },
41 { "PublicExponent", offsetof(RSA_private_key_t
, pub
.e
) },
43 { "PrivateExponent", offsetof(RSA_private_key_t
, d
) },
44 { "Prime1", offsetof(RSA_private_key_t
, p
) },
45 { "Prime2", offsetof(RSA_private_key_t
, q
) },
46 { "Exponent1", offsetof(RSA_private_key_t
, dP
) },
47 { "Exponent2", offsetof(RSA_private_key_t
, dQ
) },
48 { "Coefficient", offsetof(RSA_private_key_t
, qInv
) },
51 /* ASN.1 definition of a PKCS#1 RSA private key */
53 static const asn1Object_t privkeyObjects
[] = {
54 { 0, "RSAPrivateKey", ASN1_SEQUENCE
, ASN1_NONE
}, /* 0 */
55 { 1, "version", ASN1_INTEGER
, ASN1_BODY
}, /* 1 */
56 { 1, "modulus", ASN1_INTEGER
, ASN1_BODY
}, /* 2 */
57 { 1, "publicExponent", ASN1_INTEGER
, ASN1_BODY
}, /* 3 */
58 { 1, "privateExponent", ASN1_INTEGER
, ASN1_BODY
}, /* 4 */
59 { 1, "prime1", ASN1_INTEGER
, ASN1_BODY
}, /* 5 */
60 { 1, "prime2", ASN1_INTEGER
, ASN1_BODY
}, /* 6 */
61 { 1, "exponent1", ASN1_INTEGER
, ASN1_BODY
}, /* 7 */
62 { 1, "exponent2", ASN1_INTEGER
, ASN1_BODY
}, /* 8 */
63 { 1, "coefficient", ASN1_INTEGER
, ASN1_BODY
}, /* 9 */
64 { 1, "otherPrimeInfos", ASN1_SEQUENCE
, ASN1_OPT
|
66 { 2, "otherPrimeInfo", ASN1_SEQUENCE
, ASN1_NONE
}, /* 11 */
67 { 3, "prime", ASN1_INTEGER
, ASN1_BODY
}, /* 12 */
68 { 3, "exponent", ASN1_INTEGER
, ASN1_BODY
}, /* 13 */
69 { 3, "coefficient", ASN1_INTEGER
, ASN1_BODY
}, /* 14 */
70 { 1, "end opt or loop", ASN1_EOC
, ASN1_END
} /* 15 */
73 #define PKCS1_PRIV_KEY_VERSION 1
74 #define PKCS1_PRIV_KEY_MODULUS 2
75 #define PKCS1_PRIV_KEY_PUB_EXP 3
76 #define PKCS1_PRIV_KEY_COEFF 9
77 #define PKCS1_PRIV_KEY_ROOF 16
81 * forms the FreeS/WAN keyid from the public exponent e and modulus n
84 form_keyid(chunk_t e
, chunk_t n
, char* keyid
, unsigned *keysize
)
86 /* eliminate leading zero bytes in modulus from ASN.1 coding */
87 while (n
.len
> 1 && *n
.ptr
== 0x00)
92 /* form the FreeS/WAN keyid */
93 keyid
[0] = '\0'; /* in case of splitkeytoid failure */
94 splitkeytoid(e
.ptr
, e
.len
, n
.ptr
, n
.len
, keyid
, KEYID_BUF
);
96 /* return the RSA modulus size in octets */
101 * initialize an RSA_public_key_t object
104 init_RSA_public_key(RSA_public_key_t
*rsa
, chunk_t e
, chunk_t n
)
106 n_to_mpz(&rsa
->e
, e
.ptr
, e
.len
);
107 n_to_mpz(&rsa
->n
, n
.ptr
, n
.len
);
109 form_keyid(e
, n
, rsa
->keyid
, &rsa
->k
);
114 RSA_show_key_fields(RSA_private_key_t
*k
, int fieldcnt
)
118 DBG_log(" keyid: *%s", k
->pub
.keyid
);
120 for (p
= RSA_private_field
; p
< &RSA_private_field
[fieldcnt
]; p
++)
122 MP_INT
*n
= (MP_INT
*) ((char *)k
+ p
->offset
);
123 size_t sz
= mpz_sizeinbase(n
, 16);
124 char buf
[RSA_MAX_OCTETS
* 2 + 2]; /* ought to be big enough */
126 passert(sz
<= sizeof(buf
));
127 mpz_get_str(buf
, 16, n
);
129 DBG_log(" %s: 0x%s", p
->name
, buf
);
133 /* debugging info that compromises security! */
135 RSA_show_private_key(RSA_private_key_t
*k
)
137 RSA_show_key_fields(k
, elemsof(RSA_private_field
));
141 RSA_show_public_key(RSA_public_key_t
*k
)
143 /* Kludge: pretend that it is a private key, but only display the
144 * first two fields (which are the public key).
146 passert(offsetof(RSA_private_key_t
, pub
) == 0);
147 RSA_show_key_fields((RSA_private_key_t
*)k
, 2);
152 RSA_private_key_sanity(RSA_private_key_t
*k
)
154 /* note that the *last* error found is reported */
158 #ifdef DEBUG /* debugging info that compromises security */
159 DBG(DBG_PRIVATE
, RSA_show_private_key(k
));
162 /* PKCS#1 1.5 section 6 requires modulus to have at least 12 octets.
163 * We actually require more (for security).
165 if (k
->pub
.k
< RSA_MIN_OCTETS
)
166 return RSA_MIN_OCTETS_UGH
;
168 /* we picked a max modulus size to simplify buffer allocation */
169 if (k
->pub
.k
> RSA_MAX_OCTETS
)
170 return RSA_MAX_OCTETS_UGH
;
176 /* check that n == p * q */
177 mpz_mul(u
, &k
->p
, &k
->q
);
178 if (mpz_cmp(u
, &k
->pub
.n
) != 0)
181 /* check that e divides neither p-1 nor q-1 */
182 mpz_sub_ui(t
, &k
->p
, 1);
183 mpz_mod(t
, t
, &k
->pub
.e
);
184 if (mpz_cmp_ui(t
, 0) == 0)
185 ugh
= "e divides p-1";
187 mpz_sub_ui(t
, &k
->q
, 1);
188 mpz_mod(t
, t
, &k
->pub
.e
);
189 if (mpz_cmp_ui(t
, 0) == 0)
190 ugh
= "e divides q-1";
192 /* check that d is e^-1 (mod lcm(p-1, q-1)) */
193 /* see PKCS#1v2, aka RFC 2437, for the "lcm" */
194 mpz_sub_ui(q1
, &k
->q
, 1);
195 mpz_sub_ui(u
, &k
->p
, 1);
196 mpz_gcd(t
, u
, q1
); /* t := gcd(p-1, q-1) */
197 mpz_mul(u
, u
, q1
); /* u := (p-1) * (q-1) */
198 mpz_divexact(u
, u
, t
); /* u := lcm(p-1, q-1) */
200 mpz_mul(t
, &k
->d
, &k
->pub
.e
);
202 if (mpz_cmp_ui(t
, 1) != 0)
203 ugh
= "(d * e) mod (lcm(p-1, q-1)) != 1";
205 /* check that dP is d mod (p-1) */
206 mpz_sub_ui(u
, &k
->p
, 1);
207 mpz_mod(t
, &k
->d
, u
);
208 if (mpz_cmp(t
, &k
->dP
) != 0)
209 ugh
= "dP is not congruent to d mod (p-1)";
211 /* check that dQ is d mod (q-1) */
212 mpz_sub_ui(u
, &k
->q
, 1);
213 mpz_mod(t
, &k
->d
, u
);
214 if (mpz_cmp(t
, &k
->dQ
) != 0)
215 ugh
= "dQ is not congruent to d mod (q-1)";
217 /* check that qInv is (q^-1) mod p */
218 mpz_mul(t
, &k
->qInv
, &k
->q
);
219 mpz_mod(t
, t
, &k
->p
);
220 if (mpz_cmp_ui(t
, 1) != 0)
221 ugh
= "qInv is not conguent ot (q^-1) mod p";
230 * Check the equality of two RSA public keys
233 same_RSA_public_key(const RSA_public_key_t
*a
, const RSA_public_key_t
*b
)
236 || (a
->k
== b
->k
&& mpz_cmp(&a
->n
, &b
->n
) == 0 && mpz_cmp(&a
->e
, &b
->e
) == 0);
240 * Parses a PKCS#1 private key
243 pkcs1_parse_private_key(chunk_t blob
, RSA_private_key_t
*key
)
247 chunk_t object
, modulus
, exp
;
251 asn1_init(&ctx
, blob
, 0, FALSE
, DBG_PRIVATE
);
253 while (objectID
< PKCS1_PRIV_KEY_ROOF
) {
255 if (!extract_object(privkeyObjects
, &objectID
, &object
, &level
, &ctx
))
258 if (objectID
== PKCS1_PRIV_KEY_VERSION
)
260 if (object
.len
> 0 && *object
.ptr
!= 0)
262 plog(" wrong PKCS#1 private key version");
266 else if (objectID
>= PKCS1_PRIV_KEY_MODULUS
&&
267 objectID
<= PKCS1_PRIV_KEY_COEFF
)
269 MP_INT
*u
= (MP_INT
*) ((char *)key
270 + RSA_private_field
[objectID
- PKCS1_PRIV_KEY_MODULUS
].offset
);
272 n_to_mpz(u
, object
.ptr
, object
.len
);
274 if (objectID
== PKCS1_PRIV_KEY_MODULUS
)
276 else if (objectID
== PKCS1_PRIV_KEY_PUB_EXP
)
281 form_keyid(exp
, modulus
, key
->pub
.keyid
, &key
->pub
.k
);
282 ugh
= RSA_private_key_sanity(key
);
283 return (ugh
== NULL
);
287 * compute a digest over a binary blob
290 compute_digest(chunk_t tbs
, int alg
, chunk_t
*digest
)
295 case OID_MD2_WITH_RSA
:
300 MD2Update(&context
, tbs
.ptr
, tbs
.len
);
301 MD2Final(digest
->ptr
, &context
);
302 digest
->len
= MD2_DIGEST_SIZE
;
306 case OID_MD5_WITH_RSA
:
311 MD5Update(&context
, tbs
.ptr
, tbs
.len
);
312 MD5Final(digest
->ptr
, &context
);
313 digest
->len
= MD5_DIGEST_SIZE
;
317 case OID_SHA1_WITH_RSA
:
318 case OID_SHA1_WITH_RSA_OIW
:
323 SHA1Update(&context
, tbs
.ptr
, tbs
.len
);
324 SHA1Final(digest
->ptr
, &context
);
325 digest
->len
= SHA1_DIGEST_SIZE
;
329 case OID_SHA256_WITH_RSA
:
331 sha256_context context
;
333 sha256_init(&context
);
334 sha256_write(&context
, tbs
.ptr
, tbs
.len
);
335 sha256_final(&context
);
336 memcpy(digest
->ptr
, context
.sha_out
, SHA2_256_DIGEST_SIZE
);
337 digest
->len
= SHA2_256_DIGEST_SIZE
;
341 case OID_SHA384_WITH_RSA
:
343 sha512_context context
;
345 sha384_init(&context
);
346 sha512_write(&context
, tbs
.ptr
, tbs
.len
);
347 sha512_final(&context
);
348 memcpy(digest
->ptr
, context
.sha_out
, SHA2_384_DIGEST_SIZE
);
349 digest
->len
= SHA2_384_DIGEST_SIZE
;
353 case OID_SHA512_WITH_RSA
:
355 sha512_context context
;
357 sha512_init(&context
);
358 sha512_write(&context
, tbs
.ptr
, tbs
.len
);
359 sha512_final(&context
);
360 memcpy(digest
->ptr
, context
.sha_out
, SHA2_512_DIGEST_SIZE
);
361 digest
->len
= SHA2_512_DIGEST_SIZE
;
371 * compute an RSA signature with PKCS#1 padding
374 sign_hash(const RSA_private_key_t
*k
, const u_char
*hash_val
, size_t hash_len
375 , u_char
*sig_val
, size_t sig_len
)
382 DBG(DBG_CONTROL
| DBG_CRYPT
,
383 DBG_log("signing hash with RSA Key *%s", k
->pub
.keyid
)
385 /* PKCS#1 v1.5 8.1 encryption-block formatting */
387 *p
++ = 0x01; /* BT (block type) 01 */
388 padlen
= sig_len
- 3 - hash_len
;
389 memset(p
, 0xFF, padlen
);
392 memcpy(p
, hash_val
, hash_len
);
393 passert(p
+ hash_len
- sig_val
== (ptrdiff_t)sig_len
);
395 /* PKCS#1 v1.5 8.2 octet-string-to-integer conversion */
396 n_to_mpz(t1
, sig_val
, sig_len
); /* (could skip leading 0x00) */
398 /* PKCS#1 v1.5 8.3 RSA computation y = x^c mod n
399 * Better described in PKCS#1 v2.0 5.1 RSADP.
400 * There are two methods, depending on the form of the private key.
401 * We use the one based on the Chinese Remainder Theorem.
405 mpz_powm(t2
, t1
, &k
->dP
, &k
->p
); /* m1 = c^dP mod p */
407 mpz_powm(t1
, t1
, &k
->dQ
, &k
->q
); /* m2 = c^dQ mod Q */
409 mpz_sub(t2
, t2
, t1
); /* h = qInv (m1 - m2) mod p */
410 mpz_mod(t2
, t2
, &k
->p
);
411 mpz_mul(t2
, t2
, &k
->qInv
);
412 mpz_mod(t2
, t2
, &k
->p
);
414 mpz_mul(t2
, t2
, &k
->q
); /* m = m2 + h q */
417 /* PKCS#1 v1.5 8.4 integer-to-octet-string conversion */
418 ch
= mpz_to_n(t1
, sig_len
);
419 memcpy(sig_val
, ch
.ptr
, sig_len
);
427 * encrypt data with an RSA public key after padding
430 RSA_encrypt(const RSA_public_key_t
*key
, chunk_t in
)
432 u_char padded
[RSA_MAX_OCTETS
];
433 u_char
*pos
= padded
;
434 int padding
= key
->k
- in
.len
- 3;
437 if (padding
< 8 || key
->k
> RSA_MAX_OCTETS
)
440 /* add padding according to PKCS#1 7.2.1 1.+2. */
444 /* pad with pseudo random bytes unequal to zero */
445 get_rnd_bytes(pos
, padding
);
446 for (i
= 0; i
< padding
; i
++)
449 get_rnd_bytes(pos
, 1);
453 /* append the padding terminator */
456 /* now add the data */
457 memcpy(pos
, in
.ptr
, in
.len
);
459 DBG_dump_chunk("data for rsa encryption:\n", in
);
460 DBG_dump("padded data for rsa encryption:\n", padded
, key
->k
)
463 /* convert chunk to integer (PKCS#1 7.2.1 3.a) */
469 n_to_mpz(m
, padded
, key
->k
);
471 /* encrypt(PKCS#1 7.2.1 3.b) */
472 mpz_powm(c
, m
, &key
->e
, &key
->n
);
474 /* convert integer back to a chunk (PKCS#1 7.2.1 3.c) */
475 out
= mpz_to_n(c
, key
->k
);
480 DBG_dump_chunk("rsa encrypted data:\n", out
)
487 * decrypt data with an RSA private key and remove padding
490 RSA_decrypt(const RSA_private_key_t
*key
, chunk_t in
, chunk_t
*out
)
496 n_to_mpz(t1
, in
.ptr
,in
.len
);
498 /* PKCS#1 v1.5 8.3 RSA computation y = x^c mod n
499 * Better described in PKCS#1 v2.0 5.1 RSADP.
500 * There are two methods, depending on the form of the private key.
501 * We use the one based on the Chinese Remainder Theorem.
505 mpz_powm(t2
, t1
, &key
->dP
, &key
->p
); /* m1 = c^dP mod p */
506 mpz_powm(t1
, t1
, &key
->dQ
, &key
->q
); /* m2 = c^dQ mod Q */
508 mpz_sub(t2
, t2
, t1
); /* h = qInv (m1 - m2) mod p */
509 mpz_mod(t2
, t2
, &key
->p
);
510 mpz_mul(t2
, t2
, &key
->qInv
);
511 mpz_mod(t2
, t2
, &key
->p
);
513 mpz_mul(t2
, t2
, &key
->q
); /* m = m2 + h q */
516 padded
= mpz_to_n(t1
, key
->pub
.k
);
521 DBG_dump_chunk("rsa decrypted data with padding:\n", padded
)
525 /* PKCS#1 v1.5 8.1 encryption-block formatting (EB = 00 || 02 || PS || 00 || D) */
527 /* check for hex pattern 00 02 in decrypted message */
528 if ((*pos
++ != 0x00) || (*(pos
++) != 0x02))
530 plog("incorrect padding - probably wrong RSA key");
531 freeanychunk(padded
);
536 /* the plaintext data starts after first 0x00 byte */
537 while (padded
.len
-- > 0 && *pos
++ != 0x00)
541 plog("no plaintext data");
542 freeanychunk(padded
);
546 clonetochunk(*out
, pos
, padded
.len
, "decrypted data");
547 freeanychunk(padded
);
552 * build signatureValue
555 pkcs1_build_signature(chunk_t tbs
, int hash_alg
, const RSA_private_key_t
*key
559 size_t siglen
= key
->pub
.k
;
561 u_char digest_buf
[MAX_DIGEST_LEN
];
562 chunk_t digest
= { digest_buf
, MAX_DIGEST_LEN
};
563 chunk_t digestInfo
, alg_id
, signatureValue
;
569 case OID_MD5_WITH_RSA
:
570 alg_id
= ASN1_md5_id
;
573 case OID_SHA1_WITH_RSA
:
574 alg_id
= ASN1_sha1_id
;
579 compute_digest(tbs
, hash_alg
, &digest
);
581 /* according to PKCS#1 v2.1 digest must be packaged into
582 * an ASN.1 structure for encryption
584 digestInfo
= asn1_wrap(ASN1_SEQUENCE
, "cm"
586 , asn1_simple_object(ASN1_OCTET_STRING
, digest
));
588 /* generate the RSA signature */
591 pos
= build_asn1_object(&signatureValue
, ASN1_BIT_STRING
, 1 + siglen
);
596 pos
= build_asn1_object(&signatureValue
, ASN1_OCTET_STRING
, siglen
);
598 sign_hash(key
, digestInfo
.ptr
, digestInfo
.len
, pos
, siglen
);
599 pfree(digestInfo
.ptr
);
601 return signatureValue
;
605 * build a DER-encoded PKCS#1 private key object
608 pkcs1_build_private_key(const RSA_private_key_t
*key
)
610 chunk_t pkcs1
= asn1_wrap(ASN1_SEQUENCE
, "cmmmmmmmm"
612 , asn1_integer_from_mpz(&key
->pub
.n
)
613 , asn1_integer_from_mpz(&key
->pub
.e
)
614 , asn1_integer_from_mpz(&key
->d
)
615 , asn1_integer_from_mpz(&key
->p
)
616 , asn1_integer_from_mpz(&key
->q
)
617 , asn1_integer_from_mpz(&key
->dP
)
618 , asn1_integer_from_mpz(&key
->dQ
)
619 , asn1_integer_from_mpz(&key
->qInv
));
622 DBG_dump_chunk("PKCS#1 encoded private key:", pkcs1
)
628 * build a DER-encoded PKCS#1 public key object
631 pkcs1_build_public_key(const RSA_public_key_t
*rsa
)
633 return asn1_wrap(ASN1_SEQUENCE
, "mm"
634 , asn1_integer_from_mpz(&rsa
->n
)
635 , asn1_integer_from_mpz(&rsa
->e
));
639 * build a DER-encoded publicKeyInfo object
642 pkcs1_build_publicKeyInfo(const RSA_public_key_t
*rsa
)
645 chunk_t rawKey
= pkcs1_build_public_key(rsa
);
647 u_char
*pos
= build_asn1_object(&publicKey
, ASN1_BIT_STRING
650 mv_chunk(&pos
, rawKey
);
652 return asn1_wrap(ASN1_SEQUENCE
, "cm"
653 , ASN1_rsaEncryption_id
657 free_RSA_public_content(RSA_public_key_t
*rsa
)
664 free_RSA_private_content(RSA_private_key_t
*rsak
)
666 free_RSA_public_content(&rsak
->pub
);
670 mpz_clear(&rsak
->dP
);
671 mpz_clear(&rsak
->dQ
);
672 mpz_clear(&rsak
->qInv
);