Added support for default hash functions in all PKCS algorithm schemes.
[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_hash_free(key->hash);
342   silc_free(key);
343 }
344
345 /* Import PKCS #1 compliant private key */
346
347 SILC_PKCS_ALG_IMPORT_PRIVATE_KEY(silc_pkcs1_import_private_key)
348 {
349   SilcAsn1 asn1;
350   SilcBufferStruct alg_key;
351   RsaPrivateKey *privkey;
352   SilcUInt32 ver;
353
354   if (!ret_private_key)
355     return 0;
356
357   asn1 = silc_asn1_alloc(NULL);
358   if (!asn1)
359     return 0;
360
361   /* Allocate RSA private key */
362   *ret_private_key = privkey = silc_calloc(1, sizeof(*privkey));
363   if (!privkey)
364     goto err;
365
366   /* Parse the PKCS #1 private key */
367   silc_buffer_set(&alg_key, key, key_len);
368   if (!silc_asn1_decode(asn1, &alg_key,
369                         SILC_ASN1_OPTS(SILC_ASN1_ALLOC),
370                         SILC_ASN1_SEQUENCE,
371                           SILC_ASN1_SHORT_INT(&ver),
372                           SILC_ASN1_INT(&privkey->n),
373                           SILC_ASN1_INT(&privkey->e),
374                           SILC_ASN1_INT(&privkey->d),
375                           SILC_ASN1_INT(&privkey->p),
376                           SILC_ASN1_INT(&privkey->q),
377                           SILC_ASN1_INT(&privkey->dP),
378                           SILC_ASN1_INT(&privkey->dQ),
379                           SILC_ASN1_INT(&privkey->qP),
380                         SILC_ASN1_END, SILC_ASN1_END))
381     goto err;
382
383   if (ver != 0)
384     goto err;
385
386   /* Set key length */
387   privkey->bits = ((silc_mp_sizeinbase(&privkey->n, 2) + 7) / 8) * 8;
388
389   silc_asn1_free(asn1);
390
391   return key_len;
392
393  err:
394   silc_free(privkey);
395   silc_asn1_free(asn1);
396   return 0;
397 }
398
399 /* Export PKCS #1 compliant private key */
400
401 SILC_PKCS_ALG_EXPORT_PRIVATE_KEY(silc_pkcs1_export_private_key)
402 {
403   RsaPrivateKey *key = private_key;
404   SilcAsn1 asn1;
405   SilcBufferStruct alg_key;
406   unsigned char *ret;
407
408   asn1 = silc_asn1_alloc(stack);
409   if (!asn1)
410     return FALSE;
411
412   /* Encode to PKCS #1 private key */
413   memset(&alg_key, 0, sizeof(alg_key));
414   if (!silc_asn1_encode(asn1, &alg_key,
415                         SILC_ASN1_OPTS(SILC_ASN1_ALLOC),
416                         SILC_ASN1_SEQUENCE,
417                           SILC_ASN1_SHORT_INT(0),
418                           SILC_ASN1_INT(&key->n),
419                           SILC_ASN1_INT(&key->e),
420                           SILC_ASN1_INT(&key->d),
421                           SILC_ASN1_INT(&key->p),
422                           SILC_ASN1_INT(&key->q),
423                           SILC_ASN1_INT(&key->dP),
424                           SILC_ASN1_INT(&key->dQ),
425                           SILC_ASN1_INT(&key->qP),
426                         SILC_ASN1_END, SILC_ASN1_END))
427     goto err;
428
429   ret = silc_buffer_steal(&alg_key, ret_len);
430   silc_asn1_free(asn1);
431
432   return ret;
433
434  err:
435   silc_asn1_free(asn1);
436   return NULL;
437 }
438
439 /* Returns key length */
440
441 SILC_PKCS_ALG_PRIVATE_KEY_BITLEN(silc_pkcs1_private_key_bitlen)
442 {
443   RsaPrivateKey *key = private_key;
444   return key->bits;
445 }
446
447 /* Frees private key */
448
449 SILC_PKCS_ALG_PRIVATE_KEY_FREE(silc_pkcs1_private_key_free)
450 {
451   RsaPrivateKey *key = private_key;
452
453   silc_mp_uninit(&key->n);
454   silc_mp_uninit(&key->e);
455   silc_mp_uninit(&key->d);
456   silc_mp_uninit(&key->dP);
457   silc_mp_uninit(&key->dQ);
458   silc_mp_uninit(&key->qP);
459   silc_mp_uninit(&key->p);
460   silc_mp_uninit(&key->q);
461   silc_hash_free(key->hash);
462   silc_free(key);
463 }
464
465 /* PKCS #1 RSA routines */
466
467 SILC_PKCS_ALG_ENCRYPT(silc_pkcs1_encrypt)
468 {
469   RsaPublicKey *key = public_key;
470   SilcMPInt mp_tmp;
471   SilcMPInt mp_dst;
472   unsigned char padded[2048 + 1];
473   SilcUInt32 len = (key->bits + 7) / 8;
474   SilcStack stack;
475
476   if (sizeof(padded) < len) {
477     encrypt_cb(FALSE, NULL, 0, context);
478     return NULL;
479   }
480
481   /* Pad data */
482   if (!silc_pkcs1_encode(SILC_PKCS1_BT_PUB, src, src_len,
483                          padded, len, rng)) {
484     encrypt_cb(FALSE, NULL, 0, context);
485     return NULL;
486   }
487
488   stack = silc_stack_alloc(2048, silc_crypto_stack());
489
490   silc_mp_sinit(stack, &mp_tmp);
491   silc_mp_sinit(stack, &mp_dst);
492
493   /* Data to MP */
494   silc_mp_bin2mp(padded, len, &mp_tmp);
495
496   /* Encrypt */
497   silc_rsa_public_operation(key, &mp_tmp, &mp_dst);
498
499   /* MP to data */
500   silc_mp_mp2bin_noalloc(&mp_dst, padded, len);
501
502   /* Deliver result */
503   encrypt_cb(TRUE, padded, len, context);
504
505   memset(padded, 0, sizeof(padded));
506   silc_mp_uninit(&mp_tmp);
507   silc_mp_uninit(&mp_dst);
508   silc_stack_free(stack);
509
510   return NULL;
511 }
512
513 SILC_PKCS_ALG_DECRYPT(silc_pkcs1_decrypt)
514 {
515   RsaPrivateKey *key = private_key;
516   SilcMPInt mp_tmp;
517   SilcMPInt mp_dst;
518   unsigned char *padded, unpadded[2048 + 1];
519   SilcUInt32 padded_len, dst_len;
520   SilcStack stack;
521
522   if (sizeof(unpadded) < (key->bits + 7) / 8) {
523     decrypt_cb(FALSE, NULL, 0, context);
524     return NULL;
525   }
526
527   stack = silc_stack_alloc(2048, silc_crypto_stack());
528
529   silc_mp_sinit(stack, &mp_tmp);
530   silc_mp_sinit(stack, &mp_dst);
531
532   /* Data to MP */
533   silc_mp_bin2mp(src, src_len, &mp_tmp);
534
535   /* Decrypt */
536   silc_rsa_private_operation(key, &mp_tmp, &mp_dst);
537
538   /* MP to data */
539   padded = silc_mp_mp2bin(&mp_dst, (key->bits + 7) / 8, &padded_len);
540
541   /* Unpad data */
542   if (!silc_pkcs1_decode(SILC_PKCS1_BT_PUB, padded, padded_len,
543                          unpadded, sizeof(unpadded), &dst_len)) {
544     memset(padded, 0, padded_len);
545     silc_free(padded);
546     silc_mp_uninit(&mp_tmp);
547     silc_mp_uninit(&mp_dst);
548     decrypt_cb(FALSE, NULL, 0, context);
549     return NULL;
550   }
551
552   /* Deliver result */
553   decrypt_cb(TRUE, unpadded, dst_len, context);
554
555   memset(padded, 0, padded_len);
556   memset(unpadded, 0, sizeof(unpadded));
557   silc_free(padded);
558   silc_mp_uninit(&mp_tmp);
559   silc_mp_uninit(&mp_dst);
560   silc_stack_free(stack);
561
562   return NULL;
563 }
564
565 /* PKCS #1 sign with appendix, hash OID included in the signature */
566
567 SILC_PKCS_ALG_SIGN(silc_pkcs1_sign)
568 {
569   RsaPrivateKey *key = private_key;
570   unsigned char padded[2048 + 1], hashr[SILC_HASH_MAXLEN];
571   SilcMPInt mp_tmp;
572   SilcMPInt mp_dst;
573   SilcBufferStruct di;
574   SilcUInt32 len = (key->bits + 7) / 8;
575   const char *oid;
576   SilcStack stack;
577   SilcAsn1 asn1;
578
579   SILC_LOG_DEBUG(("Sign"));
580
581   if (sizeof(padded) < len) {
582     sign_cb(FALSE, NULL, 0, context);
583     return NULL;
584   }
585
586   oid = silc_hash_get_oid(hash);
587   if (!oid) {
588     sign_cb(FALSE, NULL, 0, context);
589     return NULL;
590   }
591
592   stack = silc_stack_alloc(2048, silc_crypto_stack());
593
594   asn1 = silc_asn1_alloc(stack);
595   if (!asn1) {
596     silc_stack_free(stack);
597     sign_cb(FALSE, NULL, 0, context);
598     return NULL;
599   }
600
601   /* Compute hash */
602   if (compute_hash) {
603     if (!hash)
604       hash = key->hash;
605     silc_hash_make(hash, src, src_len, hashr);
606     src = hashr;
607     src_len = silc_hash_len(hash);
608   }
609
610   /* Encode digest info */
611   memset(&di, 0, sizeof(di));
612   if (!silc_asn1_encode(asn1, &di,
613                         SILC_ASN1_SEQUENCE,
614                           SILC_ASN1_SEQUENCE,
615                             SILC_ASN1_OID(oid),
616                             SILC_ASN1_NULL(TRUE),
617                           SILC_ASN1_END,
618                           SILC_ASN1_OCTET_STRING(src, src_len),
619                         SILC_ASN1_END, SILC_ASN1_END)) {
620     silc_asn1_free(asn1);
621     silc_stack_free(stack);
622     sign_cb(FALSE, NULL, 0, context);
623     return NULL;
624   }
625   SILC_LOG_HEXDUMP(("DigestInfo"), silc_buffer_data(&di),
626                    silc_buffer_len(&di));
627
628   /* Pad data */
629   if (!silc_pkcs1_encode(SILC_PKCS1_BT_PRV1, silc_buffer_data(&di),
630                          silc_buffer_len(&di), padded, len, NULL)) {
631     silc_asn1_free(asn1);
632     silc_stack_free(stack);
633     sign_cb(FALSE, NULL, 0, context);
634     return NULL;
635   }
636
637   silc_mp_sinit(stack, &mp_tmp);
638   silc_mp_sinit(stack, &mp_dst);
639
640   /* Data to MP */
641   silc_mp_bin2mp(padded, len, &mp_tmp);
642
643   /* Sign */
644   silc_rsa_private_operation(key, &mp_tmp, &mp_dst);
645
646   /* MP to data */
647   silc_mp_mp2bin_noalloc(&mp_dst, padded, len);
648
649   /* Deliver result */
650   sign_cb(TRUE, padded, len, context);
651
652   memset(padded, 0, sizeof(padded));
653   if (compute_hash)
654     memset(hashr, 0, sizeof(hashr));
655   silc_mp_uninit(&mp_tmp);
656   silc_mp_uninit(&mp_dst);
657   silc_asn1_free(asn1);
658   silc_stack_free(stack);
659
660   return NULL;
661 }
662
663 /* PKCS #1 verification with appendix. */
664
665 SILC_PKCS_ALG_VERIFY(silc_pkcs1_verify)
666 {
667   RsaPublicKey *key = public_key;
668   SilcBool ret = FALSE;
669   SilcMPInt mp_tmp2;
670   SilcMPInt mp_dst;
671   unsigned char *verify, unpadded[2048 + 1], hashr[SILC_HASH_MAXLEN];
672   SilcUInt32 verify_len, len = (key->bits + 7) / 8;
673   SilcBufferStruct di, ldi;
674   SilcBool has_null = TRUE;
675   SilcHash ihash = NULL;
676   SilcStack stack;
677   SilcAsn1 asn1;
678   char *oid;
679
680   SILC_LOG_DEBUG(("Verify signature"));
681
682   stack = silc_stack_alloc(2048, silc_crypto_stack());
683
684   asn1 = silc_asn1_alloc(stack);
685   if (!asn1) {
686     verify_cb(FALSE, context);
687     return NULL;
688   }
689
690   silc_mp_sinit(stack, &mp_tmp2);
691   silc_mp_sinit(stack, &mp_dst);
692
693   /* Format the signature into MP int */
694   silc_mp_bin2mp(signature, signature_len, &mp_tmp2);
695
696   /* Verify */
697   silc_rsa_public_operation(key, &mp_tmp2, &mp_dst);
698
699   /* MP to data */
700   verify = silc_mp_mp2bin(&mp_dst, len, &verify_len);
701
702   /* Unpad data */
703   if (!silc_pkcs1_decode(SILC_PKCS1_BT_PRV1, verify, verify_len,
704                          unpadded, sizeof(unpadded), &len))
705     goto err;
706   silc_buffer_set(&di, unpadded, len);
707
708   /* If hash isn't given, allocate the one given in digest info */
709   if (compute_hash) {
710     if (!hash) {
711       has_null = FALSE;
712
713       /* Decode digest info */
714       if (!silc_asn1_decode(asn1, &di,
715                             SILC_ASN1_OPTS(SILC_ASN1_ACCUMUL),
716                             SILC_ASN1_SEQUENCE,
717                               SILC_ASN1_SEQUENCE,
718                                 SILC_ASN1_OID(&oid),
719                                 SILC_ASN1_NULL_T(SILC_ASN1_OPTIONAL,
720                                                  SILC_ASN1_TAG_NULL, &has_null),
721                               SILC_ASN1_END,
722                             SILC_ASN1_END, SILC_ASN1_END))
723         goto err;
724
725       if (!silc_hash_alloc_by_oid(oid, &ihash)) {
726         SILC_LOG_DEBUG(("Unknown OID %s", oid));
727         goto err;
728       }
729       hash = ihash;
730     }
731
732     /* Hash the data */
733     silc_hash_make(hash, data, data_len, hashr);
734     data = hashr;
735     data_len = silc_hash_len(hash);
736     oid = (char *)silc_hash_get_oid(hash);
737   }
738
739   /* Encode digest info for comparison */
740   memset(&ldi, 0, sizeof(ldi));
741   if (!silc_asn1_encode(asn1, &ldi,
742                         SILC_ASN1_OPTS(SILC_ASN1_ACCUMUL),
743                         SILC_ASN1_SEQUENCE,
744                           SILC_ASN1_SEQUENCE,
745                             SILC_ASN1_OID(oid),
746                             SILC_ASN1_NULL(has_null),
747                           SILC_ASN1_END,
748                           SILC_ASN1_OCTET_STRING(data, data_len),
749                         SILC_ASN1_END, SILC_ASN1_END))
750     goto err;
751
752   SILC_LOG_HEXDUMP(("DigestInfo remote"), silc_buffer_data(&di),
753                    silc_buffer_len(&di));
754   SILC_LOG_HEXDUMP(("DigestInfo local"), silc_buffer_data(&ldi),
755                    silc_buffer_len(&ldi));
756
757   /* Compare */
758   if (silc_buffer_len(&di) == silc_buffer_len(&ldi) &&
759       !memcmp(silc_buffer_data(&di), silc_buffer_data(&ldi),
760               silc_buffer_len(&ldi)))
761     ret = TRUE;
762
763   /* Deliver result */
764   verify_cb(ret, context);
765
766   memset(verify, 0, verify_len);
767   memset(unpadded, 0, sizeof(unpadded));
768   silc_free(verify);
769   silc_mp_uninit(&mp_tmp2);
770   silc_mp_uninit(&mp_dst);
771   if (compute_hash)
772     memset(hashr, 0, sizeof(hashr));
773   if (ihash)
774     silc_hash_free(ihash);
775   silc_asn1_free(asn1);
776   silc_stack_free(stack);
777
778   return NULL;
779
780  err:
781   memset(verify, 0, verify_len);
782   silc_free(verify);
783   silc_mp_uninit(&mp_tmp2);
784   silc_mp_uninit(&mp_dst);
785   if (ihash)
786     silc_hash_free(ihash);
787   silc_asn1_free(asn1);
788   silc_stack_free(stack);
789
790   verify_cb(FALSE, context);
791   return NULL;
792 }
793
794 /* PKCS #1 sign without hash oid */
795
796 SILC_PKCS_ALG_SIGN(silc_pkcs1_sign_no_oid)
797 {
798   RsaPrivateKey *key = private_key;
799   SilcMPInt mp_tmp;
800   SilcMPInt mp_dst;
801   unsigned char padded[2048 + 1], hashr[SILC_HASH_MAXLEN];
802   SilcUInt32 len = (key->bits + 7) / 8;
803   SilcStack stack;
804
805   SILC_LOG_DEBUG(("Sign"));
806
807   if (sizeof(padded) < len) {
808     sign_cb(FALSE, NULL, 0, context);
809     return NULL;
810   }
811
812   /* Compute hash if requested */
813   if (compute_hash) {
814     if (!hash)
815       hash = key->hash;
816     silc_hash_make(hash, src, src_len, hashr);
817     src = hashr;
818     src_len = silc_hash_len(hash);
819   }
820
821   /* Pad data */
822   if (!silc_pkcs1_encode(SILC_PKCS1_BT_PRV1, src, src_len,
823                          padded, len, NULL)) {
824     sign_cb(FALSE, NULL, 0, context);
825     return NULL;
826   }
827
828   stack = silc_stack_alloc(2048, silc_crypto_stack());
829
830   silc_mp_sinit(stack, &mp_tmp);
831   silc_mp_sinit(stack, &mp_dst);
832
833   /* Data to MP */
834   silc_mp_bin2mp(padded, len, &mp_tmp);
835
836   /* Sign */
837   silc_rsa_private_operation(key, &mp_tmp, &mp_dst);
838
839   /* MP to data */
840   silc_mp_mp2bin_noalloc(&mp_dst, padded, len);
841
842   /* Deliver result */
843   sign_cb(TRUE, padded, len, context);
844
845   memset(padded, 0, sizeof(padded));
846   if (compute_hash)
847     memset(hashr, 0, sizeof(hashr));
848   silc_mp_uninit(&mp_tmp);
849   silc_mp_uninit(&mp_dst);
850   silc_stack_free(stack);
851
852   return NULL;
853 }
854
855 /* PKCS #1 verify without hash oid */
856
857 SILC_PKCS_ALG_VERIFY(silc_pkcs1_verify_no_oid)
858 {
859   RsaPublicKey *key = public_key;
860   SilcBool ret = FALSE;
861   SilcMPInt mp_tmp2;
862   SilcMPInt mp_dst;
863   unsigned char *verify, unpadded[2048 + 1], hashr[SILC_HASH_MAXLEN];
864   SilcUInt32 verify_len, len = (key->bits + 7) / 8;
865   SilcStack stack;
866
867   SILC_LOG_DEBUG(("Verify signature"));
868
869   stack = silc_stack_alloc(2048, silc_crypto_stack());
870
871   silc_mp_sinit(stack, &mp_tmp2);
872   silc_mp_sinit(stack, &mp_dst);
873
874   /* Format the signature into MP int */
875   silc_mp_bin2mp(signature, signature_len, &mp_tmp2);
876
877   /* Verify */
878   silc_rsa_public_operation(key, &mp_tmp2, &mp_dst);
879
880   /* MP to data */
881   verify = silc_mp_mp2bin(&mp_dst, len, &verify_len);
882
883   /* Unpad data */
884   if (!silc_pkcs1_decode(SILC_PKCS1_BT_PRV1, verify, verify_len,
885                          unpadded, sizeof(unpadded), &len)) {
886     memset(verify, 0, verify_len);
887     silc_free(verify);
888     silc_mp_uninit(&mp_tmp2);
889     silc_mp_uninit(&mp_dst);
890     silc_stack_free(stack);
891     verify_cb(FALSE, context);
892     return NULL;
893   }
894
895   /* Hash data if requested */
896   if (compute_hash) {
897     if (!hash)
898       hash = key->hash;
899     silc_hash_make(hash, data, data_len, hashr);
900     data = hashr;
901     data_len = silc_hash_len(hash);
902   }
903
904   /* Compare */
905   if (len == data_len && !memcmp(data, unpadded, len))
906     ret = TRUE;
907
908   /* Deliver result */
909   verify_cb(ret, context);
910
911   memset(verify, 0, verify_len);
912   memset(unpadded, 0, sizeof(unpadded));
913   if (compute_hash)
914     memset(hashr, 0, sizeof(hashr));
915   silc_free(verify);
916   silc_mp_uninit(&mp_tmp2);
917   silc_mp_uninit(&mp_dst);
918   silc_stack_free(stack);
919
920   return NULL;
921 }