3d61961219ac698da2ccb4936cc8ade9528b2a61
[crypto.git] / lib / silccrypt / silcpkcs1.c
1 /*
2
3   silcpkcs1.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2003 - 2008 Pekka Riikonen
8
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.
12
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.
17
18 */
19
20 #include "silccrypto.h"
21 #include "rsa.h"
22 #include "silcpkcs1_i.h"
23
24 /************************** PKCS #1 message format ***************************/
25
26 /* Minimum padding in block */
27 #define SILC_PKCS1_MIN_PADDING 8
28
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
36    success. */
37
38 SilcBool silc_pkcs1_encode(SilcPkcs1BlockType bt,
39                            const unsigned char *data,
40                            SilcUInt32 data_len,
41                            unsigned char *dest_data,
42                            SilcUInt32 dest_data_size,
43                            SilcRng rng)
44 {
45   SilcInt32 padlen;
46   int i;
47
48   SILC_LOG_DEBUG(("PKCS#1 encoding, bt %d", bt));
49
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"));
54     return FALSE;
55   }
56
57   /* Start of block */
58   dest_data[0] = 0x00;
59   dest_data[1] = (unsigned char)bt;
60
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"));
64     return FALSE;
65   }
66
67   /* Encode according to block type */
68   switch (bt) {
69   case SILC_PKCS1_BT_PRV0:
70   case SILC_PKCS1_BT_PRV1:
71     /* Signature */
72     memset(dest_data + 2, bt == SILC_PKCS1_BT_PRV1 ? 0xff : 0x00, padlen);
73     break;
74
75   case SILC_PKCS1_BT_PUB:
76     /* Encryption */
77     if (!rng) {
78       SILC_LOG_ERROR(("Cannot encrypt: random number generator not provided"));
79       return FALSE;
80     }
81
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);
85
86     break;
87   }
88
89   /* Copy the data */
90   dest_data[padlen + 2] = 0x00;
91   memcpy(dest_data + padlen + 3, data, data_len);
92
93   return TRUE;
94 }
95
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. */
102
103 SilcBool silc_pkcs1_decode(SilcPkcs1BlockType bt,
104                            const unsigned char *data,
105                            SilcUInt32 data_len,
106                            unsigned char *dest_data,
107                            SilcUInt32 dest_data_size,
108                            SilcUInt32 *dest_len)
109 {
110   int i = 0;
111
112   SILC_LOG_DEBUG(("PKCS#1 decoding, bt %d", bt));
113
114   /* Sanity checks */
115   if (!data || !dest_data || dest_data_size < 3 ||
116       data[0] != 0x00 || data[1] != (unsigned char)bt) {
117     SILC_LOG_DEBUG(("Malformed block"));
118     return FALSE;
119   }
120
121   /* Decode according to block type */
122   switch (bt) {
123   case SILC_PKCS1_BT_PRV0:
124     /* Do nothing */
125     break;
126
127   case SILC_PKCS1_BT_PRV1:
128     /* Verification */
129     for (i = 2; i < data_len; i++)
130       if (data[i] != 0xff)
131         break;
132     break;
133
134   case SILC_PKCS1_BT_PUB:
135     /* Decryption */
136     for (i = 2; i < data_len; i++)
137       if (data[i] == 0x00)
138         break;
139     break;
140   }
141
142   /* Sanity checks */
143   if (data[i++] != 0x00) {
144     SILC_LOG_DEBUG(("Malformed block"));
145     return FALSE;
146   }
147   if (i - 1 < SILC_PKCS1_MIN_PADDING) {
148     SILC_LOG_DEBUG(("Malformed block"));
149     return FALSE;
150   }
151   if (dest_data_size < data_len - i) {
152     SILC_LOG_DEBUG(("Destination buffer too small"));
153     return FALSE;
154   }
155
156   /* Copy the data */
157   memcpy(dest_data, data + i, data_len - i);
158
159   /* Return data length */
160   if (dest_len)
161     *dest_len = data_len - i;
162
163   return TRUE;
164 }
165
166
167 /***************************** PKCS #1 PKCS API ******************************/
168
169 /* Generates RSA key pair. */
170
171 SILC_PKCS_ALG_GENERATE_KEY(silc_pkcs1_generate_key)
172 {
173   SilcUInt32 prime_bits = keylen / 2;
174   SilcMPInt p, q;
175   SilcBool found = FALSE;
176
177   if (keylen < 768 || keylen > 16384)
178     return FALSE;
179
180   silc_mp_init(&p);
181   silc_mp_init(&q);
182
183   /* Find p and q */
184   while (!found) {
185     silc_math_gen_prime(&p, prime_bits, FALSE, rng);
186     silc_math_gen_prime(&q, prime_bits, FALSE, rng);
187     if ((silc_mp_cmp(&p, &q)) != 0)
188       found = TRUE;
189   }
190
191   /* If p is smaller than q, switch them */
192   if ((silc_mp_cmp(&p, &q)) > 0) {
193     SilcMPInt hlp;
194     silc_mp_init(&hlp);
195
196     silc_mp_set(&hlp, &p);
197     silc_mp_set(&p, &q);
198     silc_mp_set(&q, &hlp);
199
200     silc_mp_uninit(&hlp);
201   }
202
203   /* Generate the actual keys */
204   if (!silc_rsa_generate_keys(keylen, &p, &q, ret_public_key, ret_private_key))
205     return FALSE;
206
207   silc_mp_uninit(&p);
208   silc_mp_uninit(&q);
209
210   return TRUE;
211 }
212
213 /* Import PKCS #1 compliant public key */
214
215 SILC_PKCS_ALG_IMPORT_PUBLIC_KEY(silc_pkcs1_import_public_key)
216 {
217   SilcAsn1 asn1 = NULL;
218   SilcBufferStruct alg_key;
219   RsaPublicKey *pubkey;
220
221   if (!ret_public_key)
222     return 0;
223
224   asn1 = silc_asn1_alloc(NULL);
225   if (!asn1)
226     return 0;
227
228   /* Allocate RSA public key */
229   *ret_public_key = pubkey = silc_calloc(1, sizeof(*pubkey));
230   if (!pubkey)
231     goto err;
232
233   /* Parse the PKCS #1 public key */
234   silc_buffer_set(&alg_key, key, key_len);
235   if (!silc_asn1_decode(asn1, &alg_key,
236                         SILC_ASN1_OPTS(SILC_ASN1_ALLOC),
237                         SILC_ASN1_SEQUENCE,
238                           SILC_ASN1_INT(&pubkey->n),
239                           SILC_ASN1_INT(&pubkey->e),
240                         SILC_ASN1_END, SILC_ASN1_END))
241     goto err;
242
243   /* Set key length */
244   pubkey->bits = ((silc_mp_sizeinbase(&pubkey->n, 2) + 7) / 8) * 8;
245
246   silc_asn1_free(asn1);
247
248   return key_len;
249
250  err:
251   silc_free(pubkey);
252   silc_asn1_free(asn1);
253   return 0;
254 }
255
256 /* Export PKCS #1 compliant public key */
257
258 SILC_PKCS_ALG_EXPORT_PUBLIC_KEY(silc_pkcs1_export_public_key)
259 {
260   RsaPublicKey *key = public_key;
261   SilcAsn1 asn1 = NULL;
262   SilcBufferStruct alg_key;
263   unsigned char *ret;
264
265   asn1 = silc_asn1_alloc(stack);
266   if (!asn1)
267     goto err;
268
269   /* Encode to PKCS #1 public key */
270   memset(&alg_key, 0, sizeof(alg_key));
271   if (!silc_asn1_encode(asn1, &alg_key,
272                         SILC_ASN1_OPTS(SILC_ASN1_ALLOC),
273                         SILC_ASN1_SEQUENCE,
274                           SILC_ASN1_INT(&key->n),
275                           SILC_ASN1_INT(&key->e),
276                         SILC_ASN1_END, SILC_ASN1_END))
277     goto err;
278
279   ret = silc_buffer_steal(&alg_key, ret_len);
280   silc_asn1_free(asn1);
281
282   return ret;
283
284  err:
285   if (asn1)
286     silc_asn1_free(asn1);
287   return NULL;
288 }
289
290 /* Returns key length */
291
292 SILC_PKCS_ALG_PUBLIC_KEY_BITLEN(silc_pkcs1_public_key_bitlen)
293 {
294   RsaPublicKey *key = public_key;
295   return key->bits;
296 }
297
298 /* Copy public key */
299
300 SILC_PKCS_ALG_PUBLIC_KEY_COPY(silc_pkcs1_public_key_copy)
301 {
302   RsaPublicKey *key = public_key, *new_key;
303
304   new_key = silc_calloc(1, sizeof(*new_key));
305   if (!new_key)
306     return NULL;
307
308   silc_mp_init(&new_key->n);
309   silc_mp_init(&new_key->e);
310   silc_mp_set(&new_key->n, &key->n);
311   silc_mp_set(&new_key->e, &key->e);
312   new_key->bits = key->bits;
313
314   return new_key;
315 }
316
317 /* Compare public keys */
318
319 SILC_PKCS_ALG_PUBLIC_KEY_COMPARE(silc_pkcs1_public_key_compare)
320 {
321   RsaPublicKey *k1 = key1, *k2 = key2;
322
323   if (k1->bits != k2->bits)
324     return FALSE;
325   if (silc_mp_cmp(&k1->e, &k2->e) != 0)
326     return FALSE;
327   if (silc_mp_cmp(&k1->n, &k2->n) != 0)
328     return FALSE;
329
330   return TRUE;
331 }
332
333 /* Frees public key */
334
335 SILC_PKCS_ALG_PUBLIC_KEY_FREE(silc_pkcs1_public_key_free)
336 {
337   RsaPublicKey *key = public_key;
338
339   silc_mp_uninit(&key->n);
340   silc_mp_uninit(&key->e);
341   silc_free(key);
342 }
343
344 /* Import PKCS #1 compliant private key */
345
346 SILC_PKCS_ALG_IMPORT_PRIVATE_KEY(silc_pkcs1_import_private_key)
347 {
348   SilcAsn1 asn1;
349   SilcBufferStruct alg_key;
350   RsaPrivateKey *privkey;
351   SilcUInt32 ver;
352
353   if (!ret_private_key)
354     return 0;
355
356   asn1 = silc_asn1_alloc(NULL);
357   if (!asn1)
358     return 0;
359
360   /* Allocate RSA private key */
361   *ret_private_key = privkey = silc_calloc(1, sizeof(*privkey));
362   if (!privkey)
363     goto err;
364
365   /* Parse the PKCS #1 private key */
366   silc_buffer_set(&alg_key, key, key_len);
367   if (!silc_asn1_decode(asn1, &alg_key,
368                         SILC_ASN1_OPTS(SILC_ASN1_ALLOC),
369                         SILC_ASN1_SEQUENCE,
370                           SILC_ASN1_SHORT_INT(&ver),
371                           SILC_ASN1_INT(&privkey->n),
372                           SILC_ASN1_INT(&privkey->e),
373                           SILC_ASN1_INT(&privkey->d),
374                           SILC_ASN1_INT(&privkey->p),
375                           SILC_ASN1_INT(&privkey->q),
376                           SILC_ASN1_INT(&privkey->dP),
377                           SILC_ASN1_INT(&privkey->dQ),
378                           SILC_ASN1_INT(&privkey->qP),
379                         SILC_ASN1_END, SILC_ASN1_END))
380     goto err;
381
382   if (ver != 0)
383     goto err;
384
385   /* Set key length */
386   privkey->bits = ((silc_mp_sizeinbase(&privkey->n, 2) + 7) / 8) * 8;
387
388   silc_asn1_free(asn1);
389
390   return key_len;
391
392  err:
393   silc_free(privkey);
394   silc_asn1_free(asn1);
395   return 0;
396 }
397
398 /* Export PKCS #1 compliant private key */
399
400 SILC_PKCS_ALG_EXPORT_PRIVATE_KEY(silc_pkcs1_export_private_key)
401 {
402   RsaPrivateKey *key = private_key;
403   SilcAsn1 asn1;
404   SilcBufferStruct alg_key;
405   unsigned char *ret;
406
407   asn1 = silc_asn1_alloc(stack);
408   if (!asn1)
409     return FALSE;
410
411   /* Encode to PKCS #1 private key */
412   memset(&alg_key, 0, sizeof(alg_key));
413   if (!silc_asn1_encode(asn1, &alg_key,
414                         SILC_ASN1_OPTS(SILC_ASN1_ALLOC),
415                         SILC_ASN1_SEQUENCE,
416                           SILC_ASN1_SHORT_INT(0),
417                           SILC_ASN1_INT(&key->n),
418                           SILC_ASN1_INT(&key->e),
419                           SILC_ASN1_INT(&key->d),
420                           SILC_ASN1_INT(&key->p),
421                           SILC_ASN1_INT(&key->q),
422                           SILC_ASN1_INT(&key->dP),
423                           SILC_ASN1_INT(&key->dQ),
424                           SILC_ASN1_INT(&key->qP),
425                         SILC_ASN1_END, SILC_ASN1_END))
426     goto err;
427
428   ret = silc_buffer_steal(&alg_key, ret_len);
429   silc_asn1_free(asn1);
430
431   return ret;
432
433  err:
434   silc_asn1_free(asn1);
435   return NULL;
436 }
437
438 /* Returns key length */
439
440 SILC_PKCS_ALG_PRIVATE_KEY_BITLEN(silc_pkcs1_private_key_bitlen)
441 {
442   RsaPrivateKey *key = private_key;
443   return key->bits;
444 }
445
446 /* Frees private key */
447
448 SILC_PKCS_ALG_PRIVATE_KEY_FREE(silc_pkcs1_private_key_free)
449 {
450   RsaPrivateKey *key = private_key;
451
452   silc_mp_uninit(&key->n);
453   silc_mp_uninit(&key->e);
454   silc_mp_uninit(&key->d);
455   silc_mp_uninit(&key->dP);
456   silc_mp_uninit(&key->dQ);
457   silc_mp_uninit(&key->qP);
458   silc_mp_uninit(&key->p);
459   silc_mp_uninit(&key->q);
460   silc_free(key);
461 }
462
463 /* PKCS #1 RSA routines */
464
465 SILC_PKCS_ALG_ENCRYPT(silc_pkcs1_encrypt)
466 {
467   RsaPublicKey *key = public_key;
468   SilcMPInt mp_tmp;
469   SilcMPInt mp_dst;
470   unsigned char padded[2048 + 1];
471   SilcUInt32 len = (key->bits + 7) / 8;
472   SilcStack stack;
473
474   if (sizeof(padded) < len) {
475     encrypt_cb(FALSE, NULL, 0, context);
476     return NULL;
477   }
478
479   /* Pad data */
480   if (!silc_pkcs1_encode(SILC_PKCS1_BT_PUB, src, src_len,
481                          padded, len, rng)) {
482     encrypt_cb(FALSE, NULL, 0, context);
483     return NULL;
484   }
485
486   stack = silc_stack_alloc(2048, silc_crypto_stack());
487
488   silc_mp_sinit(stack, &mp_tmp);
489   silc_mp_sinit(stack, &mp_dst);
490
491   /* Data to MP */
492   silc_mp_bin2mp(padded, len, &mp_tmp);
493
494   /* Encrypt */
495   silc_rsa_public_operation(key, &mp_tmp, &mp_dst);
496
497   /* MP to data */
498   silc_mp_mp2bin_noalloc(&mp_dst, padded, len);
499
500   /* Deliver result */
501   encrypt_cb(TRUE, padded, len, context);
502
503   memset(padded, 0, sizeof(padded));
504   silc_mp_uninit(&mp_tmp);
505   silc_mp_uninit(&mp_dst);
506   silc_stack_free(stack);
507
508   return NULL;
509 }
510
511 SILC_PKCS_ALG_DECRYPT(silc_pkcs1_decrypt)
512 {
513   RsaPrivateKey *key = private_key;
514   SilcMPInt mp_tmp;
515   SilcMPInt mp_dst;
516   unsigned char *padded, unpadded[2048 + 1];
517   SilcUInt32 padded_len, dst_len;
518   SilcStack stack;
519
520   if (sizeof(unpadded) < (key->bits + 7) / 8) {
521     decrypt_cb(FALSE, NULL, 0, context);
522     return NULL;
523   }
524
525   stack = silc_stack_alloc(2048, silc_crypto_stack());
526
527   silc_mp_sinit(stack, &mp_tmp);
528   silc_mp_sinit(stack, &mp_dst);
529
530   /* Data to MP */
531   silc_mp_bin2mp(src, src_len, &mp_tmp);
532
533   /* Decrypt */
534   silc_rsa_private_operation(key, &mp_tmp, &mp_dst);
535
536   /* MP to data */
537   padded = silc_mp_mp2bin(&mp_dst, (key->bits + 7) / 8, &padded_len);
538
539   /* Unpad data */
540   if (!silc_pkcs1_decode(SILC_PKCS1_BT_PUB, padded, padded_len,
541                          unpadded, sizeof(unpadded), &dst_len)) {
542     memset(padded, 0, padded_len);
543     silc_free(padded);
544     silc_mp_uninit(&mp_tmp);
545     silc_mp_uninit(&mp_dst);
546     decrypt_cb(FALSE, NULL, 0, context);
547     return NULL;
548   }
549
550   /* Deliver result */
551   decrypt_cb(TRUE, unpadded, dst_len, context);
552
553   memset(padded, 0, padded_len);
554   memset(unpadded, 0, sizeof(unpadded));
555   silc_free(padded);
556   silc_mp_uninit(&mp_tmp);
557   silc_mp_uninit(&mp_dst);
558   silc_stack_free(stack);
559
560   return NULL;
561 }
562
563 /* PKCS #1 sign with appendix, hash OID included in the signature */
564
565 SILC_PKCS_ALG_SIGN(silc_pkcs1_sign)
566 {
567   RsaPrivateKey *key = private_key;
568   unsigned char padded[2048 + 1], hashr[SILC_HASH_MAXLEN];
569   SilcMPInt mp_tmp;
570   SilcMPInt mp_dst;
571   SilcBufferStruct di;
572   SilcUInt32 len = (key->bits + 7) / 8;
573   const char *oid;
574   SilcStack stack;
575   SilcAsn1 asn1;
576
577   SILC_LOG_DEBUG(("Sign"));
578
579   if (sizeof(padded) < len) {
580     sign_cb(FALSE, NULL, 0, context);
581     return NULL;
582   }
583
584   oid = silc_hash_get_oid(hash);
585   if (!oid) {
586     sign_cb(FALSE, NULL, 0, context);
587     return NULL;
588   }
589
590   stack = silc_stack_alloc(2048, silc_crypto_stack());
591
592   asn1 = silc_asn1_alloc(stack);
593   if (!asn1) {
594     silc_stack_free(stack);
595     sign_cb(FALSE, NULL, 0, context);
596     return NULL;
597   }
598
599   /* Compute hash */
600   if (compute_hash) {
601     silc_hash_make(hash, src, src_len, hashr);
602     src = hashr;
603     src_len = silc_hash_len(hash);
604   }
605
606   /* Encode digest info */
607   memset(&di, 0, sizeof(di));
608   if (!silc_asn1_encode(asn1, &di,
609                         SILC_ASN1_SEQUENCE,
610                           SILC_ASN1_SEQUENCE,
611                             SILC_ASN1_OID(oid),
612                             SILC_ASN1_NULL(TRUE),
613                           SILC_ASN1_END,
614                           SILC_ASN1_OCTET_STRING(src, src_len),
615                         SILC_ASN1_END, SILC_ASN1_END)) {
616     silc_asn1_free(asn1);
617     silc_stack_free(stack);
618     sign_cb(FALSE, NULL, 0, context);
619     return NULL;
620   }
621   SILC_LOG_HEXDUMP(("DigestInfo"), silc_buffer_data(&di),
622                    silc_buffer_len(&di));
623
624   /* Pad data */
625   if (!silc_pkcs1_encode(SILC_PKCS1_BT_PRV1, silc_buffer_data(&di),
626                          silc_buffer_len(&di), padded, len, NULL)) {
627     silc_asn1_free(asn1);
628     silc_stack_free(stack);
629     sign_cb(FALSE, NULL, 0, context);
630     return NULL;
631   }
632
633   silc_mp_sinit(stack, &mp_tmp);
634   silc_mp_sinit(stack, &mp_dst);
635
636   /* Data to MP */
637   silc_mp_bin2mp(padded, len, &mp_tmp);
638
639   /* Sign */
640   silc_rsa_private_operation(key, &mp_tmp, &mp_dst);
641
642   /* MP to data */
643   silc_mp_mp2bin_noalloc(&mp_dst, padded, len);
644
645   /* Deliver result */
646   sign_cb(TRUE, padded, len, context);
647
648   memset(padded, 0, sizeof(padded));
649   if (compute_hash)
650     memset(hashr, 0, sizeof(hashr));
651   silc_mp_uninit(&mp_tmp);
652   silc_mp_uninit(&mp_dst);
653   silc_asn1_free(asn1);
654   silc_stack_free(stack);
655
656   return NULL;
657 }
658
659 /* PKCS #1 verification with appendix. */
660
661 SILC_PKCS_ALG_VERIFY(silc_pkcs1_verify)
662 {
663   RsaPublicKey *key = public_key;
664   SilcBool ret = FALSE;
665   SilcMPInt mp_tmp2;
666   SilcMPInt mp_dst;
667   unsigned char *verify, unpadded[2048 + 1], hashr[SILC_HASH_MAXLEN];
668   SilcUInt32 verify_len, len = (key->bits + 7) / 8;
669   SilcBufferStruct di, ldi;
670   SilcBool has_null = TRUE;
671   SilcHash ihash = NULL;
672   SilcStack stack;
673   SilcAsn1 asn1;
674   char *oid;
675
676   SILC_LOG_DEBUG(("Verify signature"));
677
678   stack = silc_stack_alloc(2048, silc_crypto_stack());
679
680   asn1 = silc_asn1_alloc(stack);
681   if (!asn1) {
682     verify_cb(FALSE, context);
683     return NULL;
684   }
685
686   silc_mp_sinit(stack, &mp_tmp2);
687   silc_mp_sinit(stack, &mp_dst);
688
689   /* Format the signature into MP int */
690   silc_mp_bin2mp(signature, signature_len, &mp_tmp2);
691
692   /* Verify */
693   silc_rsa_public_operation(key, &mp_tmp2, &mp_dst);
694
695   /* MP to data */
696   verify = silc_mp_mp2bin(&mp_dst, len, &verify_len);
697
698   /* Unpad data */
699   if (!silc_pkcs1_decode(SILC_PKCS1_BT_PRV1, verify, verify_len,
700                          unpadded, sizeof(unpadded), &len))
701     goto err;
702   silc_buffer_set(&di, unpadded, len);
703
704   /* If hash isn't given, allocate the one given in digest info */
705   if (!hash) {
706     has_null = FALSE;
707
708     /* Decode digest info */
709     if (!silc_asn1_decode(asn1, &di,
710                           SILC_ASN1_OPTS(SILC_ASN1_ACCUMUL),
711                           SILC_ASN1_SEQUENCE,
712                             SILC_ASN1_SEQUENCE,
713                               SILC_ASN1_OID(&oid),
714                               SILC_ASN1_NULL_T(SILC_ASN1_OPTIONAL,
715                                                SILC_ASN1_TAG_NULL, &has_null),
716                             SILC_ASN1_END,
717                           SILC_ASN1_END, SILC_ASN1_END))
718       goto err;
719
720     if (!silc_hash_alloc_by_oid(oid, &ihash)) {
721       SILC_LOG_DEBUG(("Unknown OID %s", oid));
722       goto err;
723     }
724     hash = ihash;
725   }
726
727   /* Hash the data */
728   silc_hash_make(hash, data, data_len, hashr);
729   data = hashr;
730   data_len = silc_hash_len(hash);
731   oid = (char *)silc_hash_get_oid(hash);
732
733   /* Encode digest info for comparison */
734   memset(&ldi, 0, sizeof(ldi));
735   if (!silc_asn1_encode(asn1, &ldi,
736                         SILC_ASN1_OPTS(SILC_ASN1_ACCUMUL),
737                         SILC_ASN1_SEQUENCE,
738                           SILC_ASN1_SEQUENCE,
739                             SILC_ASN1_OID(oid),
740                             SILC_ASN1_NULL(has_null),
741                           SILC_ASN1_END,
742                           SILC_ASN1_OCTET_STRING(data, data_len),
743                         SILC_ASN1_END, SILC_ASN1_END))
744     goto err;
745
746   SILC_LOG_HEXDUMP(("DigestInfo remote"), silc_buffer_data(&di),
747                    silc_buffer_len(&di));
748   SILC_LOG_HEXDUMP(("DigestInfo local"), silc_buffer_data(&ldi),
749                    silc_buffer_len(&ldi));
750
751   /* Compare */
752   if (silc_buffer_len(&di) == silc_buffer_len(&ldi) &&
753       !memcmp(silc_buffer_data(&di), silc_buffer_data(&ldi),
754               silc_buffer_len(&ldi)))
755     ret = TRUE;
756
757   /* Deliver result */
758   verify_cb(ret, context);
759
760   memset(verify, 0, verify_len);
761   memset(unpadded, 0, sizeof(unpadded));
762   silc_free(verify);
763   silc_mp_uninit(&mp_tmp2);
764   silc_mp_uninit(&mp_dst);
765   if (hash)
766     memset(hashr, 0, sizeof(hashr));
767   if (ihash)
768     silc_hash_free(ihash);
769   silc_asn1_free(asn1);
770   silc_stack_free(stack);
771
772   return NULL;
773
774  err:
775   memset(verify, 0, verify_len);
776   silc_free(verify);
777   silc_mp_uninit(&mp_tmp2);
778   silc_mp_uninit(&mp_dst);
779   if (ihash)
780     silc_hash_free(ihash);
781   silc_asn1_free(asn1);
782   silc_stack_free(stack);
783
784   verify_cb(FALSE, context);
785   return NULL;
786 }
787
788 /* PKCS #1 sign without hash oid */
789
790 SILC_PKCS_ALG_SIGN(silc_pkcs1_sign_no_oid)
791 {
792   RsaPrivateKey *key = private_key;
793   SilcMPInt mp_tmp;
794   SilcMPInt mp_dst;
795   unsigned char padded[2048 + 1], hashr[SILC_HASH_MAXLEN];
796   SilcUInt32 len = (key->bits + 7) / 8;
797   SilcStack stack;
798
799   SILC_LOG_DEBUG(("Sign"));
800
801   if (sizeof(padded) < len) {
802     sign_cb(FALSE, NULL, 0, context);
803     return NULL;
804   }
805
806   /* Compute hash if requested */
807   if (compute_hash) {
808     silc_hash_make(hash, src, src_len, hashr);
809     src = hashr;
810     src_len = silc_hash_len(hash);
811   }
812
813   /* Pad data */
814   if (!silc_pkcs1_encode(SILC_PKCS1_BT_PRV1, src, src_len,
815                          padded, len, NULL)) {
816     sign_cb(FALSE, NULL, 0, context);
817     return NULL;
818   }
819
820   stack = silc_stack_alloc(2048, silc_crypto_stack());
821
822   silc_mp_sinit(stack, &mp_tmp);
823   silc_mp_sinit(stack, &mp_dst);
824
825   /* Data to MP */
826   silc_mp_bin2mp(padded, len, &mp_tmp);
827
828   /* Sign */
829   silc_rsa_private_operation(key, &mp_tmp, &mp_dst);
830
831   /* MP to data */
832   silc_mp_mp2bin_noalloc(&mp_dst, padded, len);
833
834   /* Deliver result */
835   sign_cb(TRUE, padded, len, context);
836
837   memset(padded, 0, sizeof(padded));
838   if (compute_hash)
839     memset(hashr, 0, sizeof(hashr));
840   silc_mp_uninit(&mp_tmp);
841   silc_mp_uninit(&mp_dst);
842   silc_stack_free(stack);
843
844   return NULL;
845 }
846
847 /* PKCS #1 verify without hash oid */
848
849 SILC_PKCS_ALG_VERIFY(silc_pkcs1_verify_no_oid)
850 {
851   RsaPublicKey *key = public_key;
852   SilcBool ret = FALSE;
853   SilcMPInt mp_tmp2;
854   SilcMPInt mp_dst;
855   unsigned char *verify, unpadded[2048 + 1], hashr[SILC_HASH_MAXLEN];
856   SilcUInt32 verify_len, len = (key->bits + 7) / 8;
857   SilcStack stack;
858
859   SILC_LOG_DEBUG(("Verify signature"));
860
861   stack = silc_stack_alloc(2048, silc_crypto_stack());
862
863   silc_mp_sinit(stack, &mp_tmp2);
864   silc_mp_sinit(stack, &mp_dst);
865
866   /* Format the signature into MP int */
867   silc_mp_bin2mp(signature, signature_len, &mp_tmp2);
868
869   /* Verify */
870   silc_rsa_public_operation(key, &mp_tmp2, &mp_dst);
871
872   /* MP to data */
873   verify = silc_mp_mp2bin(&mp_dst, len, &verify_len);
874
875   /* Unpad data */
876   if (!silc_pkcs1_decode(SILC_PKCS1_BT_PRV1, verify, verify_len,
877                          unpadded, sizeof(unpadded), &len)) {
878     memset(verify, 0, verify_len);
879     silc_free(verify);
880     silc_mp_uninit(&mp_tmp2);
881     silc_mp_uninit(&mp_dst);
882     silc_stack_free(stack);
883     verify_cb(FALSE, context);
884     return NULL;
885   }
886
887   /* Hash data if requested */
888   if (hash) {
889     silc_hash_make(hash, data, data_len, hashr);
890     data = hashr;
891     data_len = silc_hash_len(hash);
892   }
893
894   /* Compare */
895   if (len == data_len && !memcmp(data, unpadded, len))
896     ret = TRUE;
897
898   /* Deliver result */
899   verify_cb(ret, context);
900
901   memset(verify, 0, verify_len);
902   memset(unpadded, 0, sizeof(unpadded));
903   if (hash)
904     memset(hashr, 0, sizeof(hashr));
905   silc_free(verify);
906   silc_mp_uninit(&mp_tmp2);
907   silc_mp_uninit(&mp_dst);
908   silc_stack_free(stack);
909
910   return NULL;
911 }