5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2003 - 2008 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.
20 #include "silccrypto.h"
22 #include "silcpkcs1_i.h"
24 /************************** PKCS #1 message format ***************************/
26 /* Minimum padding in block */
27 #define SILC_PKCS1_MIN_PADDING 8
29 /* Encodes PKCS#1 data block from the `data' according to the block type
30 indicated by `bt'. When encoding signatures the `bt' must be
31 SILC_PKCS1_BT_PRV1 and when encoding encryption blocks the `bt' must
32 be SILC_PKCS1_BT_PUB. The encoded data is copied into the `dest_data'
33 buffer which is size of `dest_data_size'. If the `dest_data' is not
34 able to hold the encoded block this returns FALSE. The `rng' must be
35 set when `bt' is SILC_PKCS1_BT_PUB. This function returns TRUE on
38 SilcBool silc_pkcs1_encode(SilcPkcs1BlockType bt,
39 const unsigned char *data,
41 unsigned char *dest_data,
42 SilcUInt32 dest_data_size,
48 SILC_LOG_DEBUG(("PKCS#1 encoding, bt %d", bt));
50 if (!data || !dest_data ||
51 dest_data_size < SILC_PKCS1_MIN_PADDING + 3 ||
52 dest_data_size < data_len) {
53 SILC_LOG_DEBUG(("Data to be encoded is too long"));
59 dest_data[1] = (unsigned char)bt;
61 padlen = (SilcInt32)dest_data_size - (SilcInt32)data_len - 3;
62 if (padlen < SILC_PKCS1_MIN_PADDING) {
63 SILC_LOG_DEBUG(("Data to be encoded is too long"));
67 /* Encode according to block type */
69 case SILC_PKCS1_BT_PRV0:
70 case SILC_PKCS1_BT_PRV1:
72 memset(dest_data + 2, bt == SILC_PKCS1_BT_PRV1 ? 0xff : 0x00, padlen);
75 case SILC_PKCS1_BT_PUB:
78 SILC_LOG_ERROR(("Cannot encrypt: random number generator not provided"));
82 /* It is guaranteed this routine does not return zero byte. */
83 for (i = 2; i < padlen; i++)
84 dest_data[i] = silc_rng_get_byte_fast(rng);
90 dest_data[padlen + 2] = 0x00;
91 memcpy(dest_data + padlen + 3, data, data_len);
96 /* Decodes the PKCS#1 encoded block according to the block type `bt'.
97 When verifying signatures the `bt' must be SILC_PKCS1_BT_PRV1 and
98 when decrypting it must be SILC_PKCS1_BT_PUB. This copies the
99 decoded data into `dest_data' which is size of `dest_data_size'. If
100 the deocded block does not fit to `dest_data' this returns FALSE.
101 Returns TRUE on success. */
103 SilcBool silc_pkcs1_decode(SilcPkcs1BlockType bt,
104 const unsigned char *data,
106 unsigned char *dest_data,
107 SilcUInt32 dest_data_size,
108 SilcUInt32 *dest_len)
112 SILC_LOG_DEBUG(("PKCS#1 decoding, bt %d", bt));
115 if (!data || !dest_data || dest_data_size < 3 ||
116 data[0] != 0x00 || data[1] != (unsigned char)bt) {
117 SILC_LOG_DEBUG(("Malformed block"));
121 /* Decode according to block type */
123 case SILC_PKCS1_BT_PRV0:
127 case SILC_PKCS1_BT_PRV1:
129 for (i = 2; i < data_len; i++)
134 case SILC_PKCS1_BT_PUB:
136 for (i = 2; i < data_len; i++)
144 SILC_LOG_DEBUG(("Malformed block, too short message"));
147 if (i < SILC_PKCS1_MIN_PADDING) {
148 SILC_LOG_DEBUG(("Malformed block, too short padding"));
151 if (data[i++] != 0x00) {
152 SILC_LOG_DEBUG(("Malformed block"));
156 SILC_LOG_DEBUG(("Malformed block, too short message"));
159 if (dest_data_size < data_len - i) {
160 SILC_LOG_DEBUG(("Destination buffer too small"));
165 memcpy(dest_data, data + i, data_len - i);
167 /* Return data length */
169 *dest_len = data_len - i;
175 /***************************** PKCS #1 PKCS API ******************************/
177 /* Generates RSA key pair. */
179 SILC_PKCS_ALG_GENERATE_KEY(silc_pkcs1_generate_key)
181 SilcUInt32 prime_bits = keylen / 2;
183 SilcBool found = FALSE;
185 if (keylen < 768 || keylen > 16384)
193 silc_math_gen_prime(&p, prime_bits, FALSE, rng);
194 silc_math_gen_prime(&q, prime_bits, FALSE, rng);
195 if ((silc_mp_cmp(&p, &q)) != 0)
199 /* If p is smaller than q, switch them */
200 if ((silc_mp_cmp(&p, &q)) > 0) {
204 silc_mp_set(&hlp, &p);
206 silc_mp_set(&q, &hlp);
208 silc_mp_uninit(&hlp);
211 /* Generate the actual keys */
212 if (!silc_rsa_generate_keys(keylen, &p, &q, ret_public_key, ret_private_key))
221 /* Import PKCS #1 compliant public key */
223 SILC_PKCS_ALG_IMPORT_PUBLIC_KEY(silc_pkcs1_import_public_key)
225 SilcAsn1 asn1 = NULL;
226 SilcBufferStruct alg_key;
227 RsaPublicKey *pubkey;
232 asn1 = silc_asn1_alloc(NULL);
236 /* Allocate RSA public key */
237 *ret_public_key = pubkey = silc_calloc(1, sizeof(*pubkey));
241 /* Parse the PKCS #1 public key */
242 silc_buffer_set(&alg_key, key, key_len);
243 if (!silc_asn1_decode(asn1, &alg_key,
244 SILC_ASN1_OPTS(SILC_ASN1_ALLOC),
246 SILC_ASN1_INT(&pubkey->n),
247 SILC_ASN1_INT(&pubkey->e),
248 SILC_ASN1_END, SILC_ASN1_END))
252 pubkey->bits = ((silc_mp_sizeinbase(&pubkey->n, 2) + 7) / 8) * 8;
254 silc_asn1_free(asn1);
260 silc_asn1_free(asn1);
264 /* Export PKCS #1 compliant public key */
266 SILC_PKCS_ALG_EXPORT_PUBLIC_KEY(silc_pkcs1_export_public_key)
268 RsaPublicKey *key = public_key;
269 SilcAsn1 asn1 = NULL;
270 SilcBufferStruct alg_key;
273 asn1 = silc_asn1_alloc(stack);
277 /* Encode to PKCS #1 public key */
278 memset(&alg_key, 0, sizeof(alg_key));
279 if (!silc_asn1_encode(asn1, &alg_key,
280 SILC_ASN1_OPTS(SILC_ASN1_ALLOC),
282 SILC_ASN1_INT(&key->n),
283 SILC_ASN1_INT(&key->e),
284 SILC_ASN1_END, SILC_ASN1_END))
287 ret = silc_buffer_steal(&alg_key, ret_len);
288 silc_asn1_free(asn1);
294 silc_asn1_free(asn1);
298 /* Returns key length */
300 SILC_PKCS_ALG_PUBLIC_KEY_BITLEN(silc_pkcs1_public_key_bitlen)
302 RsaPublicKey *key = public_key;
306 /* Copy public key */
308 SILC_PKCS_ALG_PUBLIC_KEY_COPY(silc_pkcs1_public_key_copy)
310 RsaPublicKey *key = public_key, *new_key;
312 new_key = silc_calloc(1, sizeof(*new_key));
316 silc_mp_init(&new_key->n);
317 silc_mp_init(&new_key->e);
318 silc_mp_set(&new_key->n, &key->n);
319 silc_mp_set(&new_key->e, &key->e);
320 new_key->bits = key->bits;
325 /* Compare public keys */
327 SILC_PKCS_ALG_PUBLIC_KEY_COMPARE(silc_pkcs1_public_key_compare)
329 RsaPublicKey *k1 = key1, *k2 = key2;
331 if (k1->bits != k2->bits)
333 if (silc_mp_cmp(&k1->e, &k2->e) != 0)
335 if (silc_mp_cmp(&k1->n, &k2->n) != 0)
341 /* Frees public key */
343 SILC_PKCS_ALG_PUBLIC_KEY_FREE(silc_pkcs1_public_key_free)
345 RsaPublicKey *key = public_key;
347 silc_mp_uninit(&key->n);
348 silc_mp_uninit(&key->e);
349 silc_hash_free(key->hash);
353 /* Import PKCS #1 compliant private key */
355 SILC_PKCS_ALG_IMPORT_PRIVATE_KEY(silc_pkcs1_import_private_key)
358 SilcBufferStruct alg_key;
359 RsaPrivateKey *privkey;
362 if (!ret_private_key)
365 asn1 = silc_asn1_alloc(NULL);
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) + 7) / 8) * 8;
397 silc_asn1_free(asn1);
403 silc_asn1_free(asn1);
407 /* Export PKCS #1 compliant private key */
409 SILC_PKCS_ALG_EXPORT_PRIVATE_KEY(silc_pkcs1_export_private_key)
411 RsaPrivateKey *key = private_key;
413 SilcBufferStruct alg_key;
416 asn1 = silc_asn1_alloc(stack);
420 /* Encode to PKCS #1 private key */
421 memset(&alg_key, 0, sizeof(alg_key));
422 if (!silc_asn1_encode(asn1, &alg_key,
423 SILC_ASN1_OPTS(SILC_ASN1_ALLOC),
425 SILC_ASN1_SHORT_INT(0),
426 SILC_ASN1_INT(&key->n),
427 SILC_ASN1_INT(&key->e),
428 SILC_ASN1_INT(&key->d),
429 SILC_ASN1_INT(&key->p),
430 SILC_ASN1_INT(&key->q),
431 SILC_ASN1_INT(&key->dP),
432 SILC_ASN1_INT(&key->dQ),
433 SILC_ASN1_INT(&key->qP),
434 SILC_ASN1_END, SILC_ASN1_END))
437 ret = silc_buffer_steal(&alg_key, ret_len);
438 silc_asn1_free(asn1);
443 silc_asn1_free(asn1);
447 /* Returns key length */
449 SILC_PKCS_ALG_PRIVATE_KEY_BITLEN(silc_pkcs1_private_key_bitlen)
451 RsaPrivateKey *key = private_key;
455 /* Frees private key */
457 SILC_PKCS_ALG_PRIVATE_KEY_FREE(silc_pkcs1_private_key_free)
459 RsaPrivateKey *key = private_key;
461 silc_mp_uninit(&key->n);
462 silc_mp_uninit(&key->e);
463 silc_mp_uninit(&key->d);
464 silc_mp_uninit(&key->dP);
465 silc_mp_uninit(&key->dQ);
466 silc_mp_uninit(&key->qP);
467 silc_mp_uninit(&key->p);
468 silc_mp_uninit(&key->q);
469 silc_hash_free(key->hash);
473 /* PKCS #1 RSA routines */
475 SILC_PKCS_ALG_ENCRYPT(silc_pkcs1_encrypt)
477 RsaPublicKey *key = public_key;
480 unsigned char padded[2048 + 1];
481 SilcUInt32 len = (key->bits + 7) / 8;
484 if (sizeof(padded) < len) {
485 encrypt_cb(FALSE, NULL, 0, context);
490 if (!silc_pkcs1_encode(SILC_PKCS1_BT_PUB, src, src_len,
492 encrypt_cb(FALSE, NULL, 0, context);
496 stack = silc_stack_alloc(2048, silc_crypto_stack());
498 silc_mp_sinit(stack, &mp_tmp);
499 silc_mp_sinit(stack, &mp_dst);
502 silc_mp_bin2mp(padded, len, &mp_tmp);
505 silc_rsa_public_operation(key, &mp_tmp, &mp_dst);
508 silc_mp_mp2bin_noalloc(&mp_dst, padded, len);
511 encrypt_cb(TRUE, padded, len, context);
513 memset(padded, 0, sizeof(padded));
514 silc_mp_uninit(&mp_tmp);
515 silc_mp_uninit(&mp_dst);
516 silc_stack_free(stack);
521 SILC_PKCS_ALG_DECRYPT(silc_pkcs1_decrypt)
523 RsaPrivateKey *key = private_key;
526 unsigned char *padded, unpadded[2048 + 1];
527 SilcUInt32 padded_len, dst_len;
530 if (sizeof(unpadded) < (key->bits + 7) / 8) {
531 decrypt_cb(FALSE, NULL, 0, context);
535 stack = silc_stack_alloc(2048, silc_crypto_stack());
537 silc_mp_sinit(stack, &mp_tmp);
538 silc_mp_sinit(stack, &mp_dst);
541 silc_mp_bin2mp(src, src_len, &mp_tmp);
544 silc_rsa_private_operation(key, &mp_tmp, &mp_dst);
547 padded = silc_mp_mp2bin(&mp_dst, (key->bits + 7) / 8, &padded_len);
550 if (!silc_pkcs1_decode(SILC_PKCS1_BT_PUB, padded, padded_len,
551 unpadded, sizeof(unpadded), &dst_len)) {
552 memset(padded, 0, padded_len);
554 silc_mp_uninit(&mp_tmp);
555 silc_mp_uninit(&mp_dst);
556 decrypt_cb(FALSE, NULL, 0, context);
561 decrypt_cb(TRUE, unpadded, dst_len, context);
563 memset(padded, 0, padded_len);
564 memset(unpadded, 0, sizeof(unpadded));
566 silc_mp_uninit(&mp_tmp);
567 silc_mp_uninit(&mp_dst);
568 silc_stack_free(stack);
573 /* PKCS #1 sign with appendix, hash OID included in the signature */
575 SILC_PKCS_ALG_SIGN(silc_pkcs1_sign)
577 RsaPrivateKey *key = private_key;
578 unsigned char padded[2048 + 1], hashr[SILC_HASH_MAXLEN];
582 SilcUInt32 len = (key->bits + 7) / 8;
587 SILC_LOG_DEBUG(("Sign"));
589 if (sizeof(padded) < len) {
590 sign_cb(FALSE, NULL, 0, context);
594 oid = silc_hash_get_oid(hash);
596 sign_cb(FALSE, NULL, 0, context);
600 stack = silc_stack_alloc(2048, silc_crypto_stack());
602 asn1 = silc_asn1_alloc(stack);
604 silc_stack_free(stack);
605 sign_cb(FALSE, NULL, 0, context);
613 silc_hash_make(hash, src, src_len, hashr);
615 src_len = silc_hash_len(hash);
618 /* Encode digest info */
619 memset(&di, 0, sizeof(di));
620 if (!silc_asn1_encode(asn1, &di,
624 SILC_ASN1_NULL(TRUE),
626 SILC_ASN1_OCTET_STRING(src, src_len),
627 SILC_ASN1_END, SILC_ASN1_END)) {
628 silc_asn1_free(asn1);
629 silc_stack_free(stack);
630 sign_cb(FALSE, NULL, 0, context);
633 SILC_LOG_HEXDUMP(("DigestInfo"), silc_buffer_data(&di),
634 silc_buffer_len(&di));
637 if (!silc_pkcs1_encode(SILC_PKCS1_BT_PRV1, silc_buffer_data(&di),
638 silc_buffer_len(&di), padded, len, NULL)) {
639 silc_asn1_free(asn1);
640 silc_stack_free(stack);
641 sign_cb(FALSE, NULL, 0, context);
645 silc_mp_sinit(stack, &mp_tmp);
646 silc_mp_sinit(stack, &mp_dst);
649 silc_mp_bin2mp(padded, len, &mp_tmp);
652 silc_rsa_private_operation(key, &mp_tmp, &mp_dst);
655 silc_mp_mp2bin_noalloc(&mp_dst, padded, len);
658 sign_cb(TRUE, padded, len, context);
660 memset(padded, 0, sizeof(padded));
662 memset(hashr, 0, sizeof(hashr));
663 silc_mp_uninit(&mp_tmp);
664 silc_mp_uninit(&mp_dst);
665 silc_asn1_free(asn1);
666 silc_stack_free(stack);
671 /* PKCS #1 verification with appendix. */
673 SILC_PKCS_ALG_VERIFY(silc_pkcs1_verify)
675 RsaPublicKey *key = public_key;
676 SilcBool ret = FALSE;
679 unsigned char *verify, unpadded[2048 + 1], hashr[SILC_HASH_MAXLEN];
680 SilcUInt32 verify_len, len = (key->bits + 7) / 8;
681 SilcBufferStruct di, ldi;
682 SilcBool has_null = TRUE;
683 SilcHash ihash = NULL;
688 SILC_LOG_DEBUG(("Verify signature"));
690 stack = silc_stack_alloc(2048, silc_crypto_stack());
692 asn1 = silc_asn1_alloc(stack);
694 verify_cb(FALSE, context);
698 silc_mp_sinit(stack, &mp_tmp2);
699 silc_mp_sinit(stack, &mp_dst);
701 /* Format the signature into MP int */
702 silc_mp_bin2mp(signature, signature_len, &mp_tmp2);
705 silc_rsa_public_operation(key, &mp_tmp2, &mp_dst);
708 verify = silc_mp_mp2bin(&mp_dst, len, &verify_len);
711 if (!silc_pkcs1_decode(SILC_PKCS1_BT_PRV1, verify, verify_len,
712 unpadded, sizeof(unpadded), &len))
714 silc_buffer_set(&di, unpadded, len);
716 /* If hash isn't given, allocate the one given in digest info */
721 /* Decode digest info */
722 if (!silc_asn1_decode(asn1, &di,
723 SILC_ASN1_OPTS(SILC_ASN1_ACCUMUL),
727 SILC_ASN1_NULL_T(SILC_ASN1_OPTIONAL,
728 SILC_ASN1_TAG_NULL, &has_null),
730 SILC_ASN1_END, SILC_ASN1_END))
733 if (!silc_hash_alloc_by_oid(oid, &ihash)) {
734 SILC_LOG_DEBUG(("Unknown OID %s", oid));
741 silc_hash_make(hash, data, data_len, hashr);
743 data_len = silc_hash_len(hash);
744 oid = (char *)silc_hash_get_oid(hash);
747 /* Encode digest info for comparison */
748 memset(&ldi, 0, sizeof(ldi));
749 if (!silc_asn1_encode(asn1, &ldi,
750 SILC_ASN1_OPTS(SILC_ASN1_ACCUMUL),
754 SILC_ASN1_NULL(has_null),
756 SILC_ASN1_OCTET_STRING(data, data_len),
757 SILC_ASN1_END, SILC_ASN1_END))
760 SILC_LOG_HEXDUMP(("DigestInfo remote"), silc_buffer_data(&di),
761 silc_buffer_len(&di));
762 SILC_LOG_HEXDUMP(("DigestInfo local"), silc_buffer_data(&ldi),
763 silc_buffer_len(&ldi));
766 if (silc_buffer_len(&di) == silc_buffer_len(&ldi) &&
767 !memcmp(silc_buffer_data(&di), silc_buffer_data(&ldi),
768 silc_buffer_len(&ldi)))
772 verify_cb(ret, context);
774 memset(verify, 0, verify_len);
775 memset(unpadded, 0, sizeof(unpadded));
777 silc_mp_uninit(&mp_tmp2);
778 silc_mp_uninit(&mp_dst);
780 memset(hashr, 0, sizeof(hashr));
782 silc_hash_free(ihash);
783 silc_asn1_free(asn1);
784 silc_stack_free(stack);
789 memset(verify, 0, verify_len);
791 silc_mp_uninit(&mp_tmp2);
792 silc_mp_uninit(&mp_dst);
794 silc_hash_free(ihash);
795 silc_asn1_free(asn1);
796 silc_stack_free(stack);
798 verify_cb(FALSE, context);
802 /* PKCS #1 sign without hash oid */
804 SILC_PKCS_ALG_SIGN(silc_pkcs1_sign_no_oid)
806 RsaPrivateKey *key = private_key;
809 unsigned char padded[2048 + 1], hashr[SILC_HASH_MAXLEN];
810 SilcUInt32 len = (key->bits + 7) / 8;
813 SILC_LOG_DEBUG(("Sign"));
815 if (sizeof(padded) < len) {
816 sign_cb(FALSE, NULL, 0, context);
820 /* Compute hash if requested */
824 silc_hash_make(hash, src, src_len, hashr);
826 src_len = silc_hash_len(hash);
830 if (!silc_pkcs1_encode(SILC_PKCS1_BT_PRV1, src, src_len,
831 padded, len, NULL)) {
832 sign_cb(FALSE, NULL, 0, context);
836 stack = silc_stack_alloc(2048, silc_crypto_stack());
838 silc_mp_sinit(stack, &mp_tmp);
839 silc_mp_sinit(stack, &mp_dst);
842 silc_mp_bin2mp(padded, len, &mp_tmp);
845 silc_rsa_private_operation(key, &mp_tmp, &mp_dst);
848 silc_mp_mp2bin_noalloc(&mp_dst, padded, len);
851 sign_cb(TRUE, padded, len, context);
853 memset(padded, 0, sizeof(padded));
855 memset(hashr, 0, sizeof(hashr));
856 silc_mp_uninit(&mp_tmp);
857 silc_mp_uninit(&mp_dst);
858 silc_stack_free(stack);
863 /* PKCS #1 verify without hash oid */
865 SILC_PKCS_ALG_VERIFY(silc_pkcs1_verify_no_oid)
867 RsaPublicKey *key = public_key;
868 SilcBool ret = FALSE;
871 unsigned char *verify, unpadded[2048 + 1], hashr[SILC_HASH_MAXLEN];
872 SilcUInt32 verify_len, len = (key->bits + 7) / 8;
875 SILC_LOG_DEBUG(("Verify signature"));
877 stack = silc_stack_alloc(2048, silc_crypto_stack());
879 silc_mp_sinit(stack, &mp_tmp2);
880 silc_mp_sinit(stack, &mp_dst);
882 /* Format the signature into MP int */
883 silc_mp_bin2mp(signature, signature_len, &mp_tmp2);
886 silc_rsa_public_operation(key, &mp_tmp2, &mp_dst);
889 verify = silc_mp_mp2bin(&mp_dst, len, &verify_len);
892 if (!silc_pkcs1_decode(SILC_PKCS1_BT_PRV1, verify, verify_len,
893 unpadded, sizeof(unpadded), &len)) {
894 memset(verify, 0, verify_len);
896 silc_mp_uninit(&mp_tmp2);
897 silc_mp_uninit(&mp_dst);
898 silc_stack_free(stack);
899 verify_cb(FALSE, context);
903 /* Hash data if requested */
907 silc_hash_make(hash, data, data_len, hashr);
909 data_len = silc_hash_len(hash);
913 if (len == data_len && !memcmp(data, unpadded, len))
917 verify_cb(ret, context);
919 memset(verify, 0, verify_len);
920 memset(unpadded, 0, sizeof(unpadded));
922 memset(hashr, 0, sizeof(hashr));
924 silc_mp_uninit(&mp_tmp2);
925 silc_mp_uninit(&mp_dst);
926 silc_stack_free(stack);