5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2003 - 2007 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
23 #include "silcpkcs1_i.h"
25 /************************** PKCS #1 message format ***************************/
27 /* Minimum padding in block */
28 #define SILC_PKCS1_MIN_PADDING 8
30 /* Encodes PKCS#1 data block from the `data' according to the block type
31 indicated by `bt'. When encoding signatures the `bt' must be
32 SILC_PKCS1_BT_PRV1 and when encoding encryption blocks the `bt' must
33 be SILC_PKCS1_BT_PUB. The encoded data is copied into the `dest_data'
34 buffer which is size of `dest_data_size'. If the `dest_data' is not
35 able to hold the encoded block this returns FALSE. The `rng' must be
36 set when `bt' is SILC_PKCS1_BT_PUB. This function returns TRUE on
39 SilcBool silc_pkcs1_encode(SilcPkcs1BlockType bt,
40 const unsigned char *data,
42 unsigned char *dest_data,
43 SilcUInt32 dest_data_size,
49 SILC_LOG_DEBUG(("PKCS#1 encoding, bt %d", bt));
51 if (!data || !dest_data ||
52 dest_data_size < SILC_PKCS1_MIN_PADDING + 3 ||
53 dest_data_size < data_len) {
54 SILC_LOG_DEBUG(("Data to be encoded is too long"));
60 dest_data[1] = (unsigned char)bt;
62 padlen = (SilcInt32)dest_data_size - (SilcInt32)data_len - 3;
63 if (padlen < SILC_PKCS1_MIN_PADDING) {
64 SILC_LOG_DEBUG(("Data to be encoded is too long"));
68 /* Encode according to block type */
70 case SILC_PKCS1_BT_PRV0:
71 case SILC_PKCS1_BT_PRV1:
73 memset(dest_data + 2, bt == SILC_PKCS1_BT_PRV1 ? 0xff : 0x00, padlen);
76 case SILC_PKCS1_BT_PUB:
79 SILC_LOG_ERROR(("Cannot encrypt: random number generator not provided"));
83 /* It is guaranteed this routine does not return zero byte. */
84 for (i = 2; i < padlen; i++)
85 dest_data[i] = silc_rng_get_byte_fast(rng);
91 dest_data[padlen + 2] = 0x00;
92 memcpy(dest_data + padlen + 3, data, data_len);
97 /* Decodes the PKCS#1 encoded block according to the block type `bt'.
98 When verifying signatures the `bt' must be SILC_PKCS1_BT_PRV1 and
99 when decrypting it must be SILC_PKCS1_BT_PUB. This copies the
100 decoded data into `dest_data' which is size of `dest_data_size'. If
101 the deocded block does not fit to `dest_data' this returns FALSE.
102 Returns TRUE on success. */
104 SilcBool silc_pkcs1_decode(SilcPkcs1BlockType bt,
105 const unsigned char *data,
107 unsigned char *dest_data,
108 SilcUInt32 dest_data_size,
109 SilcUInt32 *dest_len)
113 SILC_LOG_DEBUG(("PKCS#1 decoding, bt %d", bt));
116 if (!data || !dest_data || dest_data_size < 3 ||
117 data[0] != 0x00 || data[1] != (unsigned char)bt) {
118 SILC_LOG_DEBUG(("Malformed block"));
122 /* Decode according to block type */
124 case SILC_PKCS1_BT_PRV0:
128 case SILC_PKCS1_BT_PRV1:
130 for (i = 2; i < data_len; i++)
135 case SILC_PKCS1_BT_PUB:
137 for (i = 2; i < data_len; i++)
144 if (data[i++] != 0x00) {
145 SILC_LOG_DEBUG(("Malformed block"));
148 if (i - 1 < SILC_PKCS1_MIN_PADDING) {
149 SILC_LOG_DEBUG(("Malformed block"));
152 if (dest_data_size < data_len - i) {
153 SILC_LOG_DEBUG(("Destination buffer too small"));
158 memcpy(dest_data, data + i, data_len - i);
160 /* Return data length */
162 *dest_len = data_len - i;
168 /***************************** PKCS #1 PKCS API ******************************/
170 /* Generates RSA key pair. */
172 SilcBool silc_pkcs1_generate_key(SilcUInt32 keylen,
174 void **ret_public_key,
175 void **ret_private_key)
177 SilcUInt32 prime_bits = keylen / 2;
179 SilcBool found = FALSE;
181 if (keylen < 768 || keylen > 16384)
189 silc_math_gen_prime(&p, prime_bits, FALSE, rng);
190 silc_math_gen_prime(&q, prime_bits, FALSE, rng);
191 if ((silc_mp_cmp(&p, &q)) != 0)
195 /* If p is smaller than q, switch them */
196 if ((silc_mp_cmp(&p, &q)) > 0) {
200 silc_mp_set(&hlp, &p);
202 silc_mp_set(&q, &hlp);
204 silc_mp_uninit(&hlp);
207 /* Generate the actual keys */
208 if (!silc_rsa_generate_keys(keylen, &p, &q, ret_public_key, ret_private_key))
217 /* Import PKCS #1 compliant public key */
219 int silc_pkcs1_import_public_key(unsigned char *key,
221 void **ret_public_key)
223 SilcAsn1 asn1 = NULL;
224 SilcBufferStruct alg_key;
225 RsaPublicKey *pubkey;
230 asn1 = silc_asn1_alloc();
234 /* Allocate RSA public key */
235 *ret_public_key = pubkey = silc_calloc(1, sizeof(*pubkey));
239 /* Parse the PKCS #1 public key */
240 silc_buffer_set(&alg_key, key, key_len);
241 if (!silc_asn1_decode(asn1, &alg_key,
242 SILC_ASN1_OPTS(SILC_ASN1_ALLOC),
244 SILC_ASN1_INT(&pubkey->n),
245 SILC_ASN1_INT(&pubkey->e),
246 SILC_ASN1_END, SILC_ASN1_END))
250 pubkey->bits = silc_mp_sizeinbase(&pubkey->n, 2);
252 silc_asn1_free(asn1);
258 silc_asn1_free(asn1);
262 /* Export PKCS #1 compliant public key */
264 unsigned char *silc_pkcs1_export_public_key(void *public_key,
267 RsaPublicKey *key = public_key;
268 SilcAsn1 asn1 = NULL;
269 SilcBufferStruct alg_key;
272 asn1 = silc_asn1_alloc();
276 /* Encode to PKCS #1 public key */
277 memset(&alg_key, 0, sizeof(alg_key));
278 if (!silc_asn1_encode(asn1, &alg_key,
279 SILC_ASN1_OPTS(SILC_ASN1_ALLOC),
281 SILC_ASN1_INT(&key->n),
282 SILC_ASN1_INT(&key->e),
283 SILC_ASN1_END, SILC_ASN1_END))
286 ret = silc_buffer_steal(&alg_key, ret_len);
287 silc_asn1_free(asn1);
293 silc_asn1_free(asn1);
297 /* Returns key length */
299 SilcUInt32 silc_pkcs1_public_key_bitlen(void *public_key)
301 RsaPublicKey *key = public_key;
305 /* Copy public key */
307 void *silc_pkcs1_public_key_copy(void *public_key)
309 RsaPublicKey *key = public_key, *new_key;
311 new_key = silc_calloc(1, sizeof(*new_key));
315 silc_mp_init(&new_key->n);
316 silc_mp_init(&new_key->e);
317 silc_mp_set(&new_key->n, &key->n);
318 silc_mp_set(&new_key->e, &key->e);
319 new_key->bits = key->bits;
324 /* Compare public keys */
326 SilcBool silc_pkcs1_public_key_compare(void *key1, void *key2)
328 RsaPublicKey *k1 = key1, *k2 = key2;
330 if (k1->bits != k2->bits)
332 if (silc_mp_cmp(&k1->e, &k2->e) != 0)
334 if (silc_mp_cmp(&k1->n, &k2->n) != 0)
340 /* Frees public key */
342 void silc_pkcs1_public_key_free(void *public_key)
344 RsaPublicKey *key = public_key;
346 silc_mp_uninit(&key->n);
347 silc_mp_uninit(&key->e);
351 /* Import PKCS #1 compliant private key */
353 int silc_pkcs1_import_private_key(unsigned char *key,
355 void **ret_private_key)
358 SilcBufferStruct alg_key;
359 RsaPrivateKey *privkey;
362 if (!ret_private_key)
365 asn1 = silc_asn1_alloc();
369 /* Allocate RSA private key */
370 *ret_private_key = privkey = silc_calloc(1, sizeof(*privkey));
374 /* Parse the PKCS #1 private key */
375 silc_buffer_set(&alg_key, key, key_len);
376 if (!silc_asn1_decode(asn1, &alg_key,
377 SILC_ASN1_OPTS(SILC_ASN1_ALLOC),
379 SILC_ASN1_SHORT_INT(&ver),
380 SILC_ASN1_INT(&privkey->n),
381 SILC_ASN1_INT(&privkey->e),
382 SILC_ASN1_INT(&privkey->d),
383 SILC_ASN1_INT(&privkey->p),
384 SILC_ASN1_INT(&privkey->q),
385 SILC_ASN1_INT(&privkey->dP),
386 SILC_ASN1_INT(&privkey->dQ),
387 SILC_ASN1_INT(&privkey->qP),
388 SILC_ASN1_END, SILC_ASN1_END))
395 privkey->bits = silc_mp_sizeinbase(&privkey->n, 2);
397 silc_asn1_free(asn1);
403 silc_asn1_free(asn1);
407 /* Export PKCS #1 compliant private key */
409 unsigned char *silc_pkcs1_export_private_key(void *private_key,
412 RsaPrivateKey *key = private_key;
414 SilcBufferStruct alg_key;
417 asn1 = silc_asn1_alloc();
421 /* Encode to PKCS #1 private key */
422 memset(&alg_key, 0, sizeof(alg_key));
423 if (!silc_asn1_encode(asn1, &alg_key,
424 SILC_ASN1_OPTS(SILC_ASN1_ALLOC),
426 SILC_ASN1_SHORT_INT(0),
427 SILC_ASN1_INT(&key->n),
428 SILC_ASN1_INT(&key->e),
429 SILC_ASN1_INT(&key->d),
430 SILC_ASN1_INT(&key->p),
431 SILC_ASN1_INT(&key->q),
432 SILC_ASN1_INT(&key->dP),
433 SILC_ASN1_INT(&key->dQ),
434 SILC_ASN1_INT(&key->qP),
435 SILC_ASN1_END, SILC_ASN1_END))
438 ret = silc_buffer_steal(&alg_key, ret_len);
439 silc_asn1_free(asn1);
444 silc_asn1_free(asn1);
448 /* Returns key length */
450 SilcUInt32 silc_pkcs1_private_key_bitlen(void *private_key)
452 RsaPrivateKey *key = private_key;
456 /* Frees private key */
458 void silc_pkcs1_private_key_free(void *private_key)
460 RsaPrivateKey *key = private_key;
462 silc_mp_uninit(&key->n);
463 silc_mp_uninit(&key->e);
464 silc_mp_uninit(&key->d);
465 silc_mp_uninit(&key->dP);
466 silc_mp_uninit(&key->dQ);
467 silc_mp_uninit(&key->qP);
468 silc_mp_uninit(&key->p);
469 silc_mp_uninit(&key->q);
473 /* PKCS #1 RSA routines */
475 SilcBool silc_pkcs1_encrypt(void *public_key,
480 SilcUInt32 *ret_dst_len,
483 RsaPublicKey *key = public_key;
486 unsigned char padded[2048 + 1];
487 SilcUInt32 len = (key->bits + 7) / 8;
489 if (sizeof(padded) < len)
495 if (!silc_pkcs1_encode(SILC_PKCS1_BT_PUB, src, src_len,
499 silc_mp_init(&mp_tmp);
500 silc_mp_init(&mp_dst);
503 silc_mp_bin2mp(padded, len, &mp_tmp);
506 silc_rsa_public_operation(key, &mp_tmp, &mp_dst);
509 silc_mp_mp2bin_noalloc(&mp_dst, dst, len);
512 memset(padded, 0, sizeof(padded));
513 silc_mp_uninit(&mp_tmp);
514 silc_mp_uninit(&mp_dst);
519 SilcBool silc_pkcs1_decrypt(void *private_key,
524 SilcUInt32 *ret_dst_len)
526 RsaPrivateKey *key = private_key;
529 unsigned char *padded, unpadded[2048 + 1];
530 SilcUInt32 padded_len;
532 if (dst_size < (key->bits + 7) / 8)
535 silc_mp_init(&mp_tmp);
536 silc_mp_init(&mp_dst);
539 silc_mp_bin2mp(src, src_len, &mp_tmp);
542 silc_rsa_private_operation(key, &mp_tmp, &mp_dst);
545 padded = silc_mp_mp2bin(&mp_dst, (key->bits + 7) / 8, &padded_len);
548 if (!silc_pkcs1_decode(SILC_PKCS1_BT_PUB, padded, padded_len,
549 unpadded, sizeof(unpadded), ret_dst_len)) {
550 memset(padded, 0, padded_len);
552 silc_mp_uninit(&mp_tmp);
553 silc_mp_uninit(&mp_dst);
557 /* Copy to destination */
558 memcpy(dst, unpadded, *ret_dst_len);
560 memset(padded, 0, padded_len);
561 memset(unpadded, 0, sizeof(unpadded));
563 silc_mp_uninit(&mp_tmp);
564 silc_mp_uninit(&mp_dst);
569 SilcBool silc_pkcs1_sign(void *private_key,
572 unsigned char *signature,
573 SilcUInt32 signature_size,
574 SilcUInt32 *ret_signature_len,
580 SilcBool silc_pkcs1_verify(void *public_key,
581 unsigned char *signature,
582 SilcUInt32 signature_len,
590 /* PKCS #1 sign without hash oid */
592 SilcBool silc_pkcs1_sign_no_oid(void *private_key,
595 unsigned char *signature,
596 SilcUInt32 signature_size,
597 SilcUInt32 *ret_signature_len,
600 RsaPrivateKey *key = private_key;
603 unsigned char padded[2048 + 1], hashr[SILC_HASH_MAXLEN];
604 SilcUInt32 len = (key->bits + 7) / 8;
606 if (sizeof(padded) < len)
608 if (signature_size < len)
611 /* Compute hash if requested */
613 silc_hash_make(hash, src, src_len, hashr);
615 src_len = silc_hash_len(hash);
619 if (!silc_pkcs1_encode(SILC_PKCS1_BT_PRV1, src, src_len,
623 silc_mp_init(&mp_tmp);
624 silc_mp_init(&mp_dst);
627 silc_mp_bin2mp(padded, len, &mp_tmp);
630 silc_rsa_private_operation(key, &mp_tmp, &mp_dst);
633 silc_mp_mp2bin_noalloc(&mp_dst, signature, len);
634 *ret_signature_len = len;
636 memset(padded, 0, sizeof(padded));
637 silc_mp_uninit(&mp_tmp);
638 silc_mp_uninit(&mp_dst);
640 memset(hashr, 0, sizeof(hashr));
645 /* PKCS #1 verify without hash oid */
647 SilcBool silc_pkcs1_verify_no_oid(void *public_key,
648 unsigned char *signature,
649 SilcUInt32 signature_len,
654 RsaPublicKey *key = public_key;
658 unsigned char *verify, unpadded[2048 + 1], hashr[SILC_HASH_MAXLEN];
659 SilcUInt32 verify_len, len = (key->bits + 7) / 8;
661 silc_mp_init(&mp_tmp2);
662 silc_mp_init(&mp_dst);
664 /* Format the signature into MP int */
665 silc_mp_bin2mp(signature, signature_len, &mp_tmp2);
668 silc_rsa_public_operation(key, &mp_tmp2, &mp_dst);
671 verify = silc_mp_mp2bin(&mp_dst, len, &verify_len);
674 if (!silc_pkcs1_decode(SILC_PKCS1_BT_PRV1, verify, verify_len,
675 unpadded, sizeof(unpadded), &len)) {
676 memset(verify, 0, verify_len);
678 silc_mp_uninit(&mp_tmp2);
679 silc_mp_uninit(&mp_dst);
683 /* Hash data if requested */
685 silc_hash_make(hash, data, data_len, hashr);
687 data_len = silc_hash_len(hash);
693 else if (memcmp(data, unpadded, len))
696 memset(verify, 0, verify_len);
697 memset(unpadded, 0, sizeof(unpadded));
699 silc_mp_uninit(&mp_tmp2);
700 silc_mp_uninit(&mp_dst);
702 memset(hashr, 0, sizeof(hashr));