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 typedef struct private_gmp_rsa_public_key_t private_gmp_rsa_public_key_t
;
34 * Private data structure with signing context.
36 struct private_gmp_rsa_public_key_t
{
38 * Public interface for this signer.
40 gmp_rsa_public_key_t
public;
64 * Shared functions defined in gmp_rsa_private_key.c
66 extern chunk_t
gmp_mpz_to_chunk(const mpz_t value
);
69 * RSAEP algorithm specified in PKCS#1.
71 static chunk_t
rsaep(private_gmp_rsa_public_key_t
*this, chunk_t data
)
79 mpz_import(m
, data
.len
, 1, 1, 1, 0, data
.ptr
);
81 mpz_powm(c
, m
, this->e
, this->n
);
83 encrypted
.len
= this->k
;
84 encrypted
.ptr
= mpz_export(NULL
, NULL
, 1, encrypted
.len
, 1, 0, c
);
85 if (encrypted
.ptr
== NULL
)
97 * RSAVP1 algorithm specified in PKCS#1.
99 static chunk_t
rsavp1(private_gmp_rsa_public_key_t
*this, chunk_t data
)
101 return rsaep(this, data
);
105 * ASN.1 definition of digestInfo
107 static const asn1Object_t digestInfoObjects
[] = {
108 { 0, "digestInfo", ASN1_SEQUENCE
, ASN1_OBJ
}, /* 0 */
109 { 1, "digestAlgorithm", ASN1_EOC
, ASN1_RAW
}, /* 1 */
110 { 1, "digest", ASN1_OCTET_STRING
, ASN1_BODY
}, /* 2 */
111 { 0, "exit", ASN1_EOC
, ASN1_EXIT
}
113 #define DIGEST_INFO 0
114 #define DIGEST_INFO_ALGORITHM 1
115 #define DIGEST_INFO_DIGEST 2
118 * Verification of an EMPSA PKCS1 signature described in PKCS#1
120 static bool verify_emsa_pkcs1_signature(private_gmp_rsa_public_key_t
*this,
121 hash_algorithm_t algorithm
,
122 chunk_t data
, chunk_t signature
)
125 bool success
= FALSE
;
127 /* remove any preceding 0-bytes from signature */
128 while (signature
.len
&& *(signature
.ptr
) == 0x00)
130 signature
= chunk_skip(signature
, 1);
133 if (signature
.len
== 0 || signature
.len
> this->k
)
138 /* unpack signature */
139 em_ori
= em
= rsavp1(this, signature
);
141 /* result should look like this:
142 * EM = 0x00 || 0x01 || PS || 0x00 || T.
143 * PS = 0xFF padding, with length to fill em
147 /* check magic bytes */
148 if (*(em
.ptr
) != 0x00 || *(em
.ptr
+1) != 0x01)
152 em
= chunk_skip(em
, 2);
154 /* find magic 0x00 */
159 /* found magic byte, stop */
160 em
= chunk_skip(em
, 1);
163 else if (*em
.ptr
!= 0xFF)
165 /* bad padding, decryption failed ?!*/
168 em
= chunk_skip(em
, 1);
173 /* no digestInfo found */
177 if (algorithm
== HASH_UNKNOWN
)
178 { /* IKEv1 signatures without digestInfo */
179 if (em
.len
!= data
.len
)
181 DBG1("hash size in signature is %u bytes instead of %u bytes",
185 success
= memeq(em
.ptr
, data
.ptr
, data
.len
);
188 { /* IKEv2 and X.509 certificate signatures */
189 asn1_parser_t
*parser
;
192 hash_algorithm_t hash_algorithm
= HASH_UNKNOWN
;
194 DBG2("signature verification:");
195 parser
= asn1_parser_create(digestInfoObjects
, em
);
197 while (parser
->iterate(parser
, &objectID
, &object
))
203 if (em
.len
> object
.len
)
205 DBG1("digestInfo field in signature is followed by %u surplus bytes",
206 em
.len
- object
.len
);
211 case DIGEST_INFO_ALGORITHM
:
213 int hash_oid
= asn1_parse_algorithmIdentifier(object
,
214 parser
->get_level(parser
)+1, NULL
);
216 hash_algorithm
= hasher_algorithm_from_oid(hash_oid
);
217 if (hash_algorithm
== HASH_UNKNOWN
|| hash_algorithm
!= algorithm
)
219 DBG1("expected hash algorithm %N, but found %N (OID: %#B)",
220 hash_algorithm_names
, algorithm
,
221 hash_algorithm_names
, hash_algorithm
, &object
);
226 case DIGEST_INFO_DIGEST
:
231 hasher
= lib
->crypto
->create_hasher(lib
->crypto
, hash_algorithm
);
234 DBG1("hash algorithm %N not supported",
235 hash_algorithm_names
, hash_algorithm
);
239 if (object
.len
!= hasher
->get_hash_size(hasher
))
241 DBG1("hash size in signature is %u bytes instead of %u "
242 "bytes", object
.len
, hasher
->get_hash_size(hasher
));
243 hasher
->destroy(hasher
);
247 /* build our own hash and compare */
248 hasher
->allocate_hash(hasher
, data
, &hash
);
249 hasher
->destroy(hasher
);
250 success
= memeq(object
.ptr
, hash
.ptr
, hash
.len
);
260 success
&= parser
->success(parser
);
261 parser
->destroy(parser
);
270 * Implementation of public_key_t.get_type.
272 static key_type_t
get_type(private_gmp_rsa_public_key_t
*this)
278 * Implementation of public_key_t.verify.
280 static bool verify(private_gmp_rsa_public_key_t
*this, signature_scheme_t scheme
,
281 chunk_t data
, chunk_t signature
)
285 case SIGN_RSA_EMSA_PKCS1_NULL
:
286 return verify_emsa_pkcs1_signature(this, HASH_UNKNOWN
, data
, signature
);
287 case SIGN_RSA_EMSA_PKCS1_MD5
:
288 return verify_emsa_pkcs1_signature(this, HASH_MD5
, data
, signature
);
289 case SIGN_RSA_EMSA_PKCS1_SHA1
:
290 return verify_emsa_pkcs1_signature(this, HASH_SHA1
, data
, signature
);
291 case SIGN_RSA_EMSA_PKCS1_SHA224
:
292 return verify_emsa_pkcs1_signature(this, HASH_SHA224
, data
, signature
);
293 case SIGN_RSA_EMSA_PKCS1_SHA256
:
294 return verify_emsa_pkcs1_signature(this, HASH_SHA256
, data
, signature
);
295 case SIGN_RSA_EMSA_PKCS1_SHA384
:
296 return verify_emsa_pkcs1_signature(this, HASH_SHA384
, data
, signature
);
297 case SIGN_RSA_EMSA_PKCS1_SHA512
:
298 return verify_emsa_pkcs1_signature(this, HASH_SHA512
, data
, signature
);
300 DBG1("signature scheme %N not supported in RSA",
301 signature_scheme_names
, scheme
);
306 #define MIN_PS_PADDING 8
309 * Implementation of public_key_t.encrypt.
311 static bool encrypt_(private_gmp_rsa_public_key_t
*this, chunk_t plain
,
319 rng
= lib
->crypto
->create_rng(lib
->crypto
, RNG_WEAK
);
322 DBG1("no random generator available");
326 /* number of pseudo-random padding octets */
327 padding
= this->k
- plain
.len
- 3;
328 if (padding
< MIN_PS_PADDING
)
330 DBG1("pseudo-random padding must be at least %d octets", MIN_PS_PADDING
);
334 /* padding according to PKCS#1 7.2.1 (RSAES-PKCS1-v1.5-ENCRYPT) */
335 DBG2("padding %u bytes of data to the rsa modulus size of %u bytes",
338 em
.ptr
= malloc(em
.len
);
343 /* fill with pseudo random octets */
344 rng
->get_bytes(rng
, padding
, pos
);
346 /* replace zero-valued random octets */
347 for (i
= 0; i
< padding
; i
++)
351 rng
->get_bytes(rng
, 1, pos
);
357 /* append the padding terminator */
360 /* now add the data */
361 memcpy(pos
, plain
.ptr
, plain
.len
);
362 DBG3("padded data before rsa encryption: %B", &em
);
364 /* rsa encryption using PKCS#1 RSAEP */
365 *crypto
= rsaep(this, em
);
366 DBG3("rsa encrypted data: %B", crypto
);
372 * Implementation of gmp_rsa_public_key.equals.
374 static bool equals(private_gmp_rsa_public_key_t
*this, public_key_t
*other
)
376 return public_key_equals(&this->public.interface
, other
);
380 * Implementation of public_key_t.get_keysize.
382 static size_t get_keysize(private_gmp_rsa_public_key_t
*this)
388 * Implementation of public_key_t.get_encoding
390 static bool get_encoding(private_gmp_rsa_public_key_t
*this,
391 key_encoding_type_t type
, chunk_t
*encoding
)
396 n
= gmp_mpz_to_chunk(this->n
);
397 e
= gmp_mpz_to_chunk(this->e
);
399 success
= lib
->encoding
->encode(lib
->encoding
, type
, NULL
, encoding
,
400 KEY_PART_RSA_MODULUS
, n
, KEY_PART_RSA_PUB_EXP
, e
, KEY_PART_END
);
408 * Implementation of public_key_t.get_fingerprint
410 static bool get_fingerprint(private_gmp_rsa_public_key_t
*this,
411 key_encoding_type_t type
, chunk_t
*fp
)
416 if (lib
->encoding
->get_cache(lib
->encoding
, type
, this, fp
))
420 n
= gmp_mpz_to_chunk(this->n
);
421 e
= gmp_mpz_to_chunk(this->e
);
423 success
= lib
->encoding
->encode(lib
->encoding
, type
, this, fp
,
424 KEY_PART_RSA_MODULUS
, n
, KEY_PART_RSA_PUB_EXP
, e
, KEY_PART_END
);
432 * Implementation of public_key_t.get_ref.
434 static private_gmp_rsa_public_key_t
* get_ref(private_gmp_rsa_public_key_t
*this)
441 * Implementation of gmp_rsa_public_key.destroy.
443 static void destroy(private_gmp_rsa_public_key_t
*this)
445 if (ref_put(&this->ref
))
449 lib
->encoding
->clear_cache(lib
->encoding
, this);
457 gmp_rsa_public_key_t
*gmp_rsa_public_key_load(key_type_t type
, va_list args
)
459 private_gmp_rsa_public_key_t
*this;
465 switch (va_arg(args
, builder_part_t
))
467 case BUILD_RSA_MODULUS
:
468 n
= va_arg(args
, chunk_t
);
470 case BUILD_RSA_PUB_EXP
:
471 e
= va_arg(args
, chunk_t
);
480 if (!e
.ptr
|| !n
.ptr
)
485 this = malloc_thing(private_gmp_rsa_public_key_t
);
487 this->public.interface
.get_type
= (key_type_t (*) (public_key_t
*))get_type
;
488 this->public.interface
.verify
= (bool (*) (public_key_t
*, signature_scheme_t
, chunk_t
, chunk_t
))verify
;
489 this->public.interface
.encrypt
= (bool (*) (public_key_t
*, chunk_t
, chunk_t
*))encrypt_
;
490 this->public.interface
.equals
= (bool (*) (public_key_t
*, public_key_t
*))equals
;
491 this->public.interface
.get_keysize
= (size_t (*) (public_key_t
*))get_keysize
;
492 this->public.interface
.get_fingerprint
= (bool(*)(public_key_t
*, key_encoding_type_t type
, chunk_t
*fp
))get_fingerprint
;
493 this->public.interface
.has_fingerprint
= (bool(*)(public_key_t
*, chunk_t fp
))public_key_has_fingerprint
;
494 this->public.interface
.get_encoding
= (bool(*)(public_key_t
*, key_encoding_type_t type
, chunk_t
*encoding
))get_encoding
;
495 this->public.interface
.get_ref
= (public_key_t
* (*) (public_key_t
*this))get_ref
;
496 this->public.interface
.destroy
= (void (*) (public_key_t
*this))destroy
;
503 mpz_import(this->n
, n
.len
, 1, 1, 1, 0, n
.ptr
);
504 mpz_import(this->e
, e
.len
, 1, 1, 1, 0, e
.ptr
);
506 this->k
= (mpz_sizeinbase(this->n
, 2) + 7) / BITS_PER_BYTE
;
508 return &this->public;