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.
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++)
143 if (data[i++] != 0x00) {
144 SILC_LOG_DEBUG(("Malformed block"));
147 if (i - 1 < SILC_PKCS1_MIN_PADDING) {
148 SILC_LOG_DEBUG(("Malformed block"));
151 if (dest_data_size < data_len - i) {
152 SILC_LOG_DEBUG(("Destination buffer too small"));
157 memcpy(dest_data, data + i, data_len - i);
159 /* Return data length */
161 *dest_len = data_len - i;
167 /***************************** PKCS #1 PKCS API ******************************/
169 /* Generates RSA key pair. */
171 SilcBool silc_pkcs1_generate_key(SilcUInt32 keylen,
173 void **ret_public_key,
174 void **ret_private_key)
176 SilcUInt32 prime_bits = keylen / 2;
178 SilcBool found = FALSE;
180 if (keylen < 768 || keylen > 16384)
188 silc_math_gen_prime(&p, prime_bits, FALSE, rng);
189 silc_math_gen_prime(&q, prime_bits, FALSE, rng);
190 if ((silc_mp_cmp(&p, &q)) != 0)
194 /* If p is smaller than q, switch them */
195 if ((silc_mp_cmp(&p, &q)) > 0) {
199 silc_mp_set(&hlp, &p);
201 silc_mp_set(&q, &hlp);
203 silc_mp_uninit(&hlp);
206 /* Generate the actual keys */
207 if (!rsa_generate_keys(keylen, &p, &q, ret_public_key, ret_private_key))
216 /* Import PKCS #1 compliant public key */
218 SilcBool silc_pkcs1_import_public_key(unsigned char *key,
220 void **ret_public_key)
222 SilcAsn1 asn1 = NULL;
223 SilcBufferStruct alg_key;
224 RsaPublicKey *pubkey;
229 asn1 = silc_asn1_alloc();
233 /* Allocate RSA public key */
234 *ret_public_key = pubkey = silc_calloc(1, sizeof(*pubkey));
238 /* Parse the PKCS #1 public key */
239 silc_buffer_set(&alg_key, key, key_len);
240 if (!silc_asn1_decode(asn1, &alg_key,
241 SILC_ASN1_OPTS(SILC_ASN1_ALLOC),
243 SILC_ASN1_INT(&pubkey->n),
244 SILC_ASN1_INT(&pubkey->e),
245 SILC_ASN1_END, SILC_ASN1_END))
249 pubkey->bits = silc_mp_sizeinbase(&pubkey->n, 2);
251 silc_asn1_free(asn1);
256 silc_asn1_free(asn1);
260 /* Export PKCS #1 compliant public key */
262 unsigned char *silc_pkcs1_export_public_key(void *public_key,
265 RsaPublicKey *key = public_key;
266 SilcAsn1 asn1 = NULL;
267 SilcBufferStruct alg_key;
270 asn1 = silc_asn1_alloc();
274 /* Encode to PKCS #1 public key */
275 memset(&alg_key, 0, sizeof(alg_key));
276 if (!silc_asn1_encode(asn1, &alg_key,
277 SILC_ASN1_OPTS(SILC_ASN1_ALLOC),
279 SILC_ASN1_INT(&key->n),
280 SILC_ASN1_INT(&key->e),
281 SILC_ASN1_END, SILC_ASN1_END))
284 ret = silc_buffer_steal(&alg_key, ret_len);
285 silc_asn1_free(asn1);
291 silc_asn1_free(asn1);
295 /* Returns key length */
297 SilcUInt32 silc_pkcs1_public_key_bitlen(void *public_key)
299 RsaPublicKey *key = public_key;
303 /* Copy public key */
305 void *silc_pkcs1_public_key_copy(void *public_key)
307 RsaPublicKey *key = public_key, *new_key;
309 new_key = silc_calloc(1, sizeof(*new_key));
313 silc_mp_init(&new_key->n);
314 silc_mp_init(&new_key->e);
315 silc_mp_set(&new_key->n, &key->n);
316 silc_mp_set(&new_key->e, &key->e);
317 new_key->bits = key->bits;
322 /* Compare public keys */
324 SilcBool silc_pkcs1_public_key_compare(void *key1, void *key2)
326 RsaPublicKey *k1 = key1, *k2 = key2;
328 if (k1->bits != k2->bits)
330 if (silc_mp_cmp(&k1->e, &k2->e) != 0)
332 if (silc_mp_cmp(&k1->n, &k2->n) != 0)
338 /* Frees public key */
340 void silc_pkcs1_public_key_free(void *public_key)
342 RsaPublicKey *key = public_key;
344 silc_mp_uninit(&key->n);
345 silc_mp_uninit(&key->e);
349 /* Import PKCS #1 compliant private key */
351 SilcBool silc_pkcs1_import_private_key(unsigned char *key,
353 void **ret_private_key)
356 SilcBufferStruct alg_key;
357 RsaPrivateKey *privkey;
360 if (!ret_private_key)
363 asn1 = silc_asn1_alloc();
367 /* Allocate RSA private key */
368 *ret_private_key = privkey = silc_calloc(1, sizeof(*privkey));
372 /* Parse the PKCS #1 private key */
373 silc_buffer_set(&alg_key, key, key_len);
374 if (!silc_asn1_decode(asn1, &alg_key,
375 SILC_ASN1_OPTS(SILC_ASN1_ALLOC),
377 SILC_ASN1_SHORT_INT(&ver),
378 SILC_ASN1_INT(&privkey->n),
379 SILC_ASN1_INT(&privkey->e),
380 SILC_ASN1_INT(&privkey->d),
381 SILC_ASN1_INT(&privkey->p),
382 SILC_ASN1_INT(&privkey->q),
383 SILC_ASN1_INT(&privkey->dP),
384 SILC_ASN1_INT(&privkey->dQ),
385 SILC_ASN1_INT(&privkey->qP),
386 SILC_ASN1_END, SILC_ASN1_END))
393 privkey->bits = silc_mp_sizeinbase(&privkey->n, 2);
395 silc_asn1_free(asn1);
400 silc_asn1_free(asn1);
404 /* Export PKCS #1 compliant private key */
406 unsigned char *silc_pkcs1_export_private_key(void *private_key,
409 RsaPrivateKey *key = private_key;
411 SilcBufferStruct alg_key;
414 asn1 = silc_asn1_alloc();
418 /* Encode to PKCS #1 private key */
419 memset(&alg_key, 0, sizeof(alg_key));
420 if (!silc_asn1_encode(asn1, &alg_key,
421 SILC_ASN1_OPTS(SILC_ASN1_ALLOC),
423 SILC_ASN1_SHORT_INT(0),
424 SILC_ASN1_INT(&key->n),
425 SILC_ASN1_INT(&key->e),
426 SILC_ASN1_INT(&key->d),
427 SILC_ASN1_INT(&key->p),
428 SILC_ASN1_INT(&key->q),
429 SILC_ASN1_INT(&key->dP),
430 SILC_ASN1_INT(&key->dQ),
431 SILC_ASN1_INT(&key->qP),
432 SILC_ASN1_END, SILC_ASN1_END))
435 ret = silc_buffer_steal(&alg_key, ret_len);
436 silc_asn1_free(asn1);
441 silc_asn1_free(asn1);
445 /* Returns key length */
447 SilcUInt32 silc_pkcs1_private_key_bitlen(void *private_key)
449 RsaPrivateKey *key = private_key;
453 /* Frees private key */
455 void silc_pkcs1_private_key_free(void *private_key)
457 RsaPrivateKey *key = private_key;
459 silc_mp_uninit(&key->n);
460 silc_mp_uninit(&key->e);
461 silc_mp_uninit(&key->d);
462 silc_mp_uninit(&key->dP);
463 silc_mp_uninit(&key->dQ);
464 silc_mp_uninit(&key->qP);
465 silc_mp_uninit(&key->p);
466 silc_mp_uninit(&key->q);
470 /* PKCS #1 RSA routines */
472 SilcBool silc_pkcs1_encrypt(void *public_key,
477 SilcUInt32 *ret_dst_len,
480 RsaPublicKey *key = public_key;
483 unsigned char padded[2048 + 1];
484 SilcUInt32 len = (key->bits + 7) / 8;
486 if (sizeof(padded) < len)
492 if (!silc_pkcs1_encode(SILC_PKCS1_BT_PUB, src, src_len,
496 silc_mp_init(&mp_tmp);
497 silc_mp_init(&mp_dst);
500 silc_mp_bin2mp(padded, len, &mp_tmp);
503 rsa_public_operation(key, &mp_tmp, &mp_dst);
506 silc_mp_mp2bin_noalloc(&mp_dst, dst, len);
509 memset(padded, 0, sizeof(padded));
510 silc_mp_uninit(&mp_tmp);
511 silc_mp_uninit(&mp_dst);
516 SilcBool silc_pkcs1_decrypt(void *private_key,
521 SilcUInt32 *ret_dst_len)
523 RsaPrivateKey *key = private_key;
526 unsigned char *padded, unpadded[2048 + 1];
527 SilcUInt32 padded_len;
529 if (dst_size < (key->bits + 7) / 8)
532 silc_mp_init(&mp_tmp);
533 silc_mp_init(&mp_dst);
536 silc_mp_bin2mp(src, src_len, &mp_tmp);
539 rsa_private_operation(key, &mp_tmp, &mp_dst);
542 padded = silc_mp_mp2bin(&mp_dst, (key->bits + 7) / 8, &padded_len);
545 if (!silc_pkcs1_decode(SILC_PKCS1_BT_PUB, padded, padded_len,
546 unpadded, sizeof(unpadded), ret_dst_len)) {
547 memset(padded, 0, padded_len);
549 silc_mp_uninit(&mp_tmp);
550 silc_mp_uninit(&mp_dst);
554 /* Copy to destination */
555 memcpy(dst, unpadded, *ret_dst_len);
557 memset(padded, 0, padded_len);
558 memset(unpadded, 0, sizeof(unpadded));
560 silc_mp_uninit(&mp_tmp);
561 silc_mp_uninit(&mp_dst);
566 SilcBool silc_pkcs1_sign(void *private_key,
569 unsigned char *signature,
570 SilcUInt32 signature_size,
571 SilcUInt32 *ret_signature_len,
577 SilcBool silc_pkcs1_verify(void *public_key,
578 unsigned char *signature,
579 SilcUInt32 signature_len,
587 /* PKCS #1 sign without hash oid */
589 SilcBool silc_pkcs1_sign_no_oid(void *private_key,
592 unsigned char *signature,
593 SilcUInt32 signature_size,
594 SilcUInt32 *ret_signature_len,
597 RsaPrivateKey *key = private_key;
600 unsigned char padded[2048 + 1], hashr[SILC_HASH_MAXLEN];
601 SilcUInt32 len = (key->bits + 7) / 8;
603 if (sizeof(padded) < len)
605 if (signature_size < len)
608 /* Compute hash if requested */
610 silc_hash_make(hash, src, src_len, hashr);
612 src_len = silc_hash_len(hash);
616 if (!silc_pkcs1_encode(SILC_PKCS1_BT_PRV1, src, src_len,
620 silc_mp_init(&mp_tmp);
621 silc_mp_init(&mp_dst);
624 silc_mp_bin2mp(padded, len, &mp_tmp);
627 rsa_private_operation(key, &mp_tmp, &mp_dst);
630 silc_mp_mp2bin_noalloc(&mp_dst, signature, len);
631 *ret_signature_len = len;
633 memset(padded, 0, sizeof(padded));
634 silc_mp_uninit(&mp_tmp);
635 silc_mp_uninit(&mp_dst);
637 memset(hashr, 0, sizeof(hashr));
642 /* PKCS #1 verify without hash oid */
644 SilcBool silc_pkcs1_verify_no_oid(void *public_key,
645 unsigned char *signature,
646 SilcUInt32 signature_len,
651 RsaPublicKey *key = public_key;
655 unsigned char *verify, unpadded[2048 + 1], hashr[SILC_HASH_MAXLEN];
656 SilcUInt32 verify_len, len = (key->bits + 7) / 8;
658 silc_mp_init(&mp_tmp2);
659 silc_mp_init(&mp_dst);
661 /* Format the signature into MP int */
662 silc_mp_bin2mp(signature, signature_len, &mp_tmp2);
665 rsa_public_operation(key, &mp_tmp2, &mp_dst);
668 verify = silc_mp_mp2bin(&mp_dst, len, &verify_len);
671 if (!silc_pkcs1_decode(SILC_PKCS1_BT_PRV1, verify, verify_len,
672 unpadded, sizeof(unpadded), &len)) {
673 memset(verify, 0, verify_len);
675 silc_mp_uninit(&mp_tmp2);
676 silc_mp_uninit(&mp_dst);
680 /* Hash data if requested */
682 silc_hash_make(hash, data, data_len, hashr);
684 data_len = silc_hash_len(hash);
690 else if (memcmp(data, unpadded, len))
693 memset(verify, 0, verify_len);
694 memset(unpadded, 0, sizeof(unpadded));
696 silc_mp_uninit(&mp_tmp2);
697 silc_mp_uninit(&mp_dst);
699 memset(hashr, 0, sizeof(hashr));