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
23 #include "gmp_rsa_public_key.h"
27 #include <asn1/asn1.h>
28 #include <asn1/asn1_parser.h>
29 #include <crypto/hashers/hasher.h>
31 #ifdef HAVE_MPZ_POWM_SEC
33 # define mpz_powm mpz_powm_sec
36 typedef struct private_gmp_rsa_public_key_t private_gmp_rsa_public_key_t
;
39 * Private data structure with signing context.
41 struct private_gmp_rsa_public_key_t
{
43 * Public interface for this signer.
45 gmp_rsa_public_key_t
public;
69 * Shared functions defined in gmp_rsa_private_key.c
71 extern chunk_t
gmp_mpz_to_chunk(const mpz_t value
);
74 * RSAEP algorithm specified in PKCS#1.
76 static chunk_t
rsaep(private_gmp_rsa_public_key_t
*this, chunk_t data
)
84 mpz_import(m
, data
.len
, 1, 1, 1, 0, data
.ptr
);
86 mpz_powm(c
, m
, this->e
, this->n
);
88 encrypted
.len
= this->k
;
89 encrypted
.ptr
= mpz_export(NULL
, NULL
, 1, encrypted
.len
, 1, 0, c
);
90 if (encrypted
.ptr
== NULL
)
102 * RSAVP1 algorithm specified in PKCS#1.
104 static chunk_t
rsavp1(private_gmp_rsa_public_key_t
*this, chunk_t data
)
106 return rsaep(this, data
);
110 * ASN.1 definition of digestInfo
112 static const asn1Object_t digestInfoObjects
[] = {
113 { 0, "digestInfo", ASN1_SEQUENCE
, ASN1_OBJ
}, /* 0 */
114 { 1, "digestAlgorithm", ASN1_EOC
, ASN1_RAW
}, /* 1 */
115 { 1, "digest", ASN1_OCTET_STRING
, ASN1_BODY
}, /* 2 */
116 { 0, "exit", ASN1_EOC
, ASN1_EXIT
}
118 #define DIGEST_INFO 0
119 #define DIGEST_INFO_ALGORITHM 1
120 #define DIGEST_INFO_DIGEST 2
123 * Verification of an EMPSA PKCS1 signature described in PKCS#1
125 static bool verify_emsa_pkcs1_signature(private_gmp_rsa_public_key_t
*this,
126 hash_algorithm_t algorithm
,
127 chunk_t data
, chunk_t signature
)
130 bool success
= FALSE
;
132 /* remove any preceding 0-bytes from signature */
133 while (signature
.len
&& *(signature
.ptr
) == 0x00)
135 signature
= chunk_skip(signature
, 1);
138 if (signature
.len
== 0 || signature
.len
> this->k
)
143 /* unpack signature */
144 em_ori
= em
= rsavp1(this, signature
);
146 /* result should look like this:
147 * EM = 0x00 || 0x01 || PS || 0x00 || T.
148 * PS = 0xFF padding, with length to fill em
152 /* check magic bytes */
153 if (*(em
.ptr
) != 0x00 || *(em
.ptr
+1) != 0x01)
157 em
= chunk_skip(em
, 2);
159 /* find magic 0x00 */
164 /* found magic byte, stop */
165 em
= chunk_skip(em
, 1);
168 else if (*em
.ptr
!= 0xFF)
170 /* bad padding, decryption failed ?!*/
173 em
= chunk_skip(em
, 1);
178 /* no digestInfo found */
182 if (algorithm
== HASH_UNKNOWN
)
183 { /* IKEv1 signatures without digestInfo */
184 if (em
.len
!= data
.len
)
186 DBG1("hash size in signature is %u bytes instead of %u bytes",
190 success
= memeq(em
.ptr
, data
.ptr
, data
.len
);
193 { /* IKEv2 and X.509 certificate signatures */
194 asn1_parser_t
*parser
;
197 hash_algorithm_t hash_algorithm
= HASH_UNKNOWN
;
199 DBG2("signature verification:");
200 parser
= asn1_parser_create(digestInfoObjects
, em
);
202 while (parser
->iterate(parser
, &objectID
, &object
))
208 if (em
.len
> object
.len
)
210 DBG1("digestInfo field in signature is followed by %u surplus bytes",
211 em
.len
- object
.len
);
216 case DIGEST_INFO_ALGORITHM
:
218 int hash_oid
= asn1_parse_algorithmIdentifier(object
,
219 parser
->get_level(parser
)+1, NULL
);
221 hash_algorithm
= hasher_algorithm_from_oid(hash_oid
);
222 if (hash_algorithm
== HASH_UNKNOWN
|| hash_algorithm
!= algorithm
)
224 DBG1("expected hash algorithm %N, but found %N (OID: %#B)",
225 hash_algorithm_names
, algorithm
,
226 hash_algorithm_names
, hash_algorithm
, &object
);
231 case DIGEST_INFO_DIGEST
:
236 hasher
= lib
->crypto
->create_hasher(lib
->crypto
, hash_algorithm
);
239 DBG1("hash algorithm %N not supported",
240 hash_algorithm_names
, hash_algorithm
);
244 if (object
.len
!= hasher
->get_hash_size(hasher
))
246 DBG1("hash size in signature is %u bytes instead of %u "
247 "bytes", object
.len
, hasher
->get_hash_size(hasher
));
248 hasher
->destroy(hasher
);
252 /* build our own hash and compare */
253 hasher
->allocate_hash(hasher
, data
, &hash
);
254 hasher
->destroy(hasher
);
255 success
= memeq(object
.ptr
, hash
.ptr
, hash
.len
);
265 success
&= parser
->success(parser
);
266 parser
->destroy(parser
);
275 * Implementation of public_key_t.get_type.
277 static key_type_t
get_type(private_gmp_rsa_public_key_t
*this)
283 * Implementation of public_key_t.verify.
285 static bool verify(private_gmp_rsa_public_key_t
*this, signature_scheme_t scheme
,
286 chunk_t data
, chunk_t signature
)
290 case SIGN_RSA_EMSA_PKCS1_NULL
:
291 return verify_emsa_pkcs1_signature(this, HASH_UNKNOWN
, data
, signature
);
292 case SIGN_RSA_EMSA_PKCS1_MD5
:
293 return verify_emsa_pkcs1_signature(this, HASH_MD5
, data
, signature
);
294 case SIGN_RSA_EMSA_PKCS1_SHA1
:
295 return verify_emsa_pkcs1_signature(this, HASH_SHA1
, data
, signature
);
296 case SIGN_RSA_EMSA_PKCS1_SHA224
:
297 return verify_emsa_pkcs1_signature(this, HASH_SHA224
, data
, signature
);
298 case SIGN_RSA_EMSA_PKCS1_SHA256
:
299 return verify_emsa_pkcs1_signature(this, HASH_SHA256
, data
, signature
);
300 case SIGN_RSA_EMSA_PKCS1_SHA384
:
301 return verify_emsa_pkcs1_signature(this, HASH_SHA384
, data
, signature
);
302 case SIGN_RSA_EMSA_PKCS1_SHA512
:
303 return verify_emsa_pkcs1_signature(this, HASH_SHA512
, data
, signature
);
305 DBG1("signature scheme %N not supported in RSA",
306 signature_scheme_names
, scheme
);
311 #define MIN_PS_PADDING 8
314 * Implementation of public_key_t.encrypt.
316 static bool encrypt_(private_gmp_rsa_public_key_t
*this, chunk_t plain
,
324 rng
= lib
->crypto
->create_rng(lib
->crypto
, RNG_WEAK
);
327 DBG1("no random generator available");
331 /* number of pseudo-random padding octets */
332 padding
= this->k
- plain
.len
- 3;
333 if (padding
< MIN_PS_PADDING
)
335 DBG1("pseudo-random padding must be at least %d octets", MIN_PS_PADDING
);
339 /* padding according to PKCS#1 7.2.1 (RSAES-PKCS1-v1.5-ENCRYPT) */
340 DBG2("padding %u bytes of data to the rsa modulus size of %u bytes",
343 em
.ptr
= malloc(em
.len
);
348 /* fill with pseudo random octets */
349 rng
->get_bytes(rng
, padding
, pos
);
351 /* replace zero-valued random octets */
352 for (i
= 0; i
< padding
; i
++)
356 rng
->get_bytes(rng
, 1, pos
);
362 /* append the padding terminator */
365 /* now add the data */
366 memcpy(pos
, plain
.ptr
, plain
.len
);
367 DBG3("padded data before rsa encryption: %B", &em
);
369 /* rsa encryption using PKCS#1 RSAEP */
370 *crypto
= rsaep(this, em
);
371 DBG3("rsa encrypted data: %B", crypto
);
377 * Implementation of gmp_rsa_public_key.equals.
379 static bool equals(private_gmp_rsa_public_key_t
*this, public_key_t
*other
)
381 return public_key_equals(&this->public.interface
, other
);
385 * Implementation of public_key_t.get_keysize.
387 static size_t get_keysize(private_gmp_rsa_public_key_t
*this)
393 * Implementation of public_key_t.get_encoding
395 static bool get_encoding(private_gmp_rsa_public_key_t
*this,
396 key_encoding_type_t type
, chunk_t
*encoding
)
401 n
= gmp_mpz_to_chunk(this->n
);
402 e
= gmp_mpz_to_chunk(this->e
);
404 success
= lib
->encoding
->encode(lib
->encoding
, type
, NULL
, encoding
,
405 KEY_PART_RSA_MODULUS
, n
, KEY_PART_RSA_PUB_EXP
, e
, KEY_PART_END
);
413 * Implementation of public_key_t.get_fingerprint
415 static bool get_fingerprint(private_gmp_rsa_public_key_t
*this,
416 key_encoding_type_t type
, chunk_t
*fp
)
421 if (lib
->encoding
->get_cache(lib
->encoding
, type
, this, fp
))
425 n
= gmp_mpz_to_chunk(this->n
);
426 e
= gmp_mpz_to_chunk(this->e
);
428 success
= lib
->encoding
->encode(lib
->encoding
, type
, this, fp
,
429 KEY_PART_RSA_MODULUS
, n
, KEY_PART_RSA_PUB_EXP
, e
, KEY_PART_END
);
437 * Implementation of public_key_t.get_ref.
439 static private_gmp_rsa_public_key_t
* get_ref(private_gmp_rsa_public_key_t
*this)
446 * Implementation of gmp_rsa_public_key.destroy.
448 static void destroy(private_gmp_rsa_public_key_t
*this)
450 if (ref_put(&this->ref
))
454 lib
->encoding
->clear_cache(lib
->encoding
, this);
462 gmp_rsa_public_key_t
*gmp_rsa_public_key_load(key_type_t type
, va_list args
)
464 private_gmp_rsa_public_key_t
*this;
470 switch (va_arg(args
, builder_part_t
))
472 case BUILD_RSA_MODULUS
:
473 n
= va_arg(args
, chunk_t
);
475 case BUILD_RSA_PUB_EXP
:
476 e
= va_arg(args
, chunk_t
);
485 if (!e
.ptr
|| !n
.ptr
)
490 this = malloc_thing(private_gmp_rsa_public_key_t
);
492 this->public.interface
.get_type
= (key_type_t (*) (public_key_t
*))get_type
;
493 this->public.interface
.verify
= (bool (*) (public_key_t
*, signature_scheme_t
, chunk_t
, chunk_t
))verify
;
494 this->public.interface
.encrypt
= (bool (*) (public_key_t
*, chunk_t
, chunk_t
*))encrypt_
;
495 this->public.interface
.equals
= (bool (*) (public_key_t
*, public_key_t
*))equals
;
496 this->public.interface
.get_keysize
= (size_t (*) (public_key_t
*))get_keysize
;
497 this->public.interface
.get_fingerprint
= (bool(*)(public_key_t
*, key_encoding_type_t type
, chunk_t
*fp
))get_fingerprint
;
498 this->public.interface
.has_fingerprint
= (bool(*)(public_key_t
*, chunk_t fp
))public_key_has_fingerprint
;
499 this->public.interface
.get_encoding
= (bool(*)(public_key_t
*, key_encoding_type_t type
, chunk_t
*encoding
))get_encoding
;
500 this->public.interface
.get_ref
= (public_key_t
* (*) (public_key_t
*this))get_ref
;
501 this->public.interface
.destroy
= (void (*) (public_key_t
*this))destroy
;
508 mpz_import(this->n
, n
.len
, 1, 1, 1, 0, n
.ptr
);
509 mpz_import(this->e
, e
.len
, 1, 1, 1, 0, e
.ptr
);
511 this->k
= (mpz_sizeinbase(this->n
, 2) + 7) / BITS_PER_BYTE
;
513 return &this->public;