patch from Nicolas Fournier to add some _dup and _cmp functions for RSA signatures...
[oweals/gnunet.git] / src / util / crypto_rsa.c
1 /*
2   This file is part of GNUnet
3   Copyright (C) 2014 Christian Grothoff (and other contributing authors)
4
5   GNUnet is free software; you can redistribute it and/or modify it under the
6   terms of the GNU General Public License as published by the Free Software
7   Foundation; either version 3, or (at your option) any later version.
8
9   GNUnet is distributed in the hope that it will be useful, but WITHOUT ANY
10   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11   A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
12
13   You should have received a copy of the GNU General Public License along with
14   GNUnet; see the file COPYING.  If not, If not, see <http://www.gnu.org/licenses/>
15 */
16
17 /**
18  * @file util/crypto_rsa.c
19  * @brief Chaum-style Blind signatures based on RSA
20  * @author Sree Harsha Totakura <sreeharsha@totakura.in>
21  * @author Christian Grothoff
22  */
23 #include "platform.h"
24 #include <gcrypt.h>
25 #include "gnunet_crypto_lib.h"
26
27 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
28
29
30 /**
31  * The private information of an RSA key pair.
32  */
33 struct GNUNET_CRYPTO_rsa_PrivateKey
34 {
35   /**
36    * Libgcrypt S-expression for the RSA private key.
37    */
38   gcry_sexp_t sexp;
39 };
40
41
42 /**
43  * The public information of an RSA key pair.
44  */
45 struct GNUNET_CRYPTO_rsa_PublicKey
46 {
47   /**
48    * Libgcrypt S-expression for the RSA public key.
49    */
50   gcry_sexp_t sexp;
51 };
52
53
54 /**
55  * @brief an RSA signature
56  */
57 struct GNUNET_CRYPTO_rsa_Signature
58 {
59   /**
60    * Libgcrypt S-expression for the RSA signature.
61    */
62   gcry_sexp_t sexp;
63 };
64
65
66 /**
67  * @brief RSA blinding key
68  */
69 struct GNUNET_CRYPTO_rsa_BlindingKey
70 {
71   /**
72    * Random value used for blinding.
73    */
74   gcry_mpi_t r;
75 };
76
77
78 /**
79  * Extract values from an S-expression.
80  *
81  * @param array where to store the result(s)
82  * @param sexp S-expression to parse
83  * @param topname top-level name in the S-expression that is of interest
84  * @param elems names of the elements to extract
85  * @return 0 on success
86  */
87 static int
88 key_from_sexp (gcry_mpi_t *array,
89                gcry_sexp_t sexp,
90                const char *topname,
91                const char *elems)
92 {
93   gcry_sexp_t list;
94   gcry_sexp_t l2;
95   const char *s;
96   unsigned int i;
97   unsigned int idx;
98
99   if (! (list = gcry_sexp_find_token (sexp, topname, 0)))
100     return 1;
101   l2 = gcry_sexp_cadr (list);
102   gcry_sexp_release (list);
103   list = l2;
104   if (! list)
105     return 2;
106   idx = 0;
107   for (s = elems; *s; s++, idx++)
108   {
109     if (! (l2 = gcry_sexp_find_token (list, s, 1)))
110     {
111       for (i = 0; i < idx; i++)
112       {
113         gcry_free (array[i]);
114         array[i] = NULL;
115       }
116       gcry_sexp_release (list);
117       return 3;                 /* required parameter not found */
118     }
119     array[idx] = gcry_sexp_nth_mpi (l2, 1, GCRYMPI_FMT_USG);
120     gcry_sexp_release (l2);
121     if (! array[idx])
122     {
123       for (i = 0; i < idx; i++)
124       {
125         gcry_free (array[i]);
126         array[i] = NULL;
127       }
128       gcry_sexp_release (list);
129       return 4;                 /* required parameter is invalid */
130     }
131   }
132   gcry_sexp_release (list);
133   return 0;
134 }
135
136
137 /**
138  * Create a new private key. Caller must free return value.
139  *
140  * @param len length of the key in bits (i.e. 2048)
141  * @return fresh private key
142  */
143 struct GNUNET_CRYPTO_rsa_PrivateKey *
144 GNUNET_CRYPTO_rsa_private_key_create (unsigned int len)
145 {
146   struct GNUNET_CRYPTO_rsa_PrivateKey *ret;
147   gcry_sexp_t s_key;
148   gcry_sexp_t s_keyparam;
149
150   GNUNET_assert (0 ==
151                  gcry_sexp_build (&s_keyparam,
152                                   NULL,
153                                   "(genkey(rsa(nbits %d)))",
154                                   len));
155   GNUNET_assert (0 ==
156                  gcry_pk_genkey (&s_key,
157                                  s_keyparam));
158   gcry_sexp_release (s_keyparam);
159 #if EXTRA_CHECKS
160   GNUNET_assert (0 ==
161                  gcry_pk_testkey (s_key));
162 #endif
163   ret = GNUNET_new (struct GNUNET_CRYPTO_rsa_PrivateKey);
164   ret->sexp = s_key;
165   return ret;
166 }
167
168
169 /**
170  * Free memory occupied by the private key.
171  *
172  * @param key pointer to the memory to free
173  */
174 void
175 GNUNET_CRYPTO_rsa_private_key_free (struct GNUNET_CRYPTO_rsa_PrivateKey *key)
176 {
177   gcry_sexp_release (key->sexp);
178   GNUNET_free (key);
179 }
180
181
182 /**
183  * Encode the private key in a format suitable for
184  * storing it into a file.
185  *
186  * @param key the private key
187  * @param[out] buffer set to a buffer with the encoded key
188  * @return size of memory allocated in @a buffer
189  */
190 size_t
191 GNUNET_CRYPTO_rsa_private_key_encode (const struct GNUNET_CRYPTO_rsa_PrivateKey *key,
192                               char **buffer)
193 {
194   size_t n;
195   char *b;
196
197   n = gcry_sexp_sprint (key->sexp,
198                         GCRYSEXP_FMT_DEFAULT,
199                         NULL,
200                         0);
201   b = GNUNET_malloc (n);
202   GNUNET_assert ((n - 1) ==     /* since the last byte is \0 */
203                  gcry_sexp_sprint (key->sexp,
204                                    GCRYSEXP_FMT_DEFAULT,
205                                    b,
206                                    n));
207   *buffer = b;
208   return n;
209 }
210
211
212 /**
213  * Decode the private key from the data-format back
214  * to the "normal", internal format.
215  *
216  * @param buf the buffer where the private key data is stored
217  * @param len the length of the data in @a buf
218  * @return NULL on error
219  */
220 struct GNUNET_CRYPTO_rsa_PrivateKey *
221 GNUNET_CRYPTO_rsa_private_key_decode (const char *buf,
222                               size_t len)
223 {
224   struct GNUNET_CRYPTO_rsa_PrivateKey *key;
225   key = GNUNET_new (struct GNUNET_CRYPTO_rsa_PrivateKey);
226   if (0 !=
227       gcry_sexp_new (&key->sexp,
228                      buf,
229                      len,
230                      0))
231   {
232     LOG (GNUNET_ERROR_TYPE_WARNING,
233          "Decoded private key is not valid\n");
234     GNUNET_free (key);
235     return NULL;
236   }
237   if (0 != gcry_pk_testkey (key->sexp))
238   {
239     LOG (GNUNET_ERROR_TYPE_WARNING,
240          "Decoded private key is not valid\n");
241     GNUNET_CRYPTO_rsa_private_key_free (key);
242     return NULL;
243   }
244   return key;
245 }
246
247
248 /**
249  * Extract the public key of the given private key.
250  *
251  * @param priv the private key
252  * @retur NULL on error, otherwise the public key
253  */
254 struct GNUNET_CRYPTO_rsa_PublicKey *
255 GNUNET_CRYPTO_rsa_private_key_get_public (const struct GNUNET_CRYPTO_rsa_PrivateKey *priv)
256 {
257   struct GNUNET_CRYPTO_rsa_PublicKey *pub;
258   gcry_mpi_t ne[2];
259   int rc;
260   gcry_sexp_t result;
261
262   rc = key_from_sexp (ne, priv->sexp, "public-key", "ne");
263   if (0 != rc)
264     rc = key_from_sexp (ne, priv->sexp, "private-key", "ne");
265   if (0 != rc)
266     rc = key_from_sexp (ne, priv->sexp, "rsa", "ne");
267   if (0 != rc)
268   {
269     GNUNET_break_op (0);
270     return NULL;
271   }
272   rc = gcry_sexp_build (&result,
273                         NULL,
274                         "(public-key(rsa(n %m)(e %m)))",
275                         ne[0],
276                         ne[1]);
277   gcry_mpi_release (ne[0]);
278   gcry_mpi_release (ne[1]);
279   pub = GNUNET_new (struct GNUNET_CRYPTO_rsa_PublicKey);
280   pub->sexp = result;
281   return pub;
282 }
283
284
285 /**
286  * Free memory occupied by the public key.
287  *
288  * @param key pointer to the memory to free
289  */
290 void
291 GNUNET_CRYPTO_rsa_public_key_free (struct GNUNET_CRYPTO_rsa_PublicKey *key)
292 {
293   gcry_sexp_release (key->sexp);
294   GNUNET_free (key);
295 }
296
297
298 /**
299  * Encode the public key in a format suitable for
300  * storing it into a file.
301  *
302  * @param key the private key
303  * @param[out] buffer set to a buffer with the encoded key
304  * @return size of memory allocated in @a buffer
305  */
306 size_t
307 GNUNET_CRYPTO_rsa_public_key_encode (const struct GNUNET_CRYPTO_rsa_PublicKey *key,
308                                      char **buffer)
309 {
310   size_t n;
311   char *b;
312
313   n = gcry_sexp_sprint (key->sexp,
314                         GCRYSEXP_FMT_ADVANCED,
315                         NULL,
316                         0);
317   b = GNUNET_malloc (n);
318   GNUNET_assert ((n -1) ==      /* since the last byte is \0 */
319                  gcry_sexp_sprint (key->sexp,
320                                    GCRYSEXP_FMT_ADVANCED,
321                                    b,
322                                    n));
323   *buffer = b;
324   return n;
325 }
326
327
328 /**
329  * Compute hash over the public key.
330  *
331  * @param key public key to hash
332  * @param hc where to store the hash code
333  */
334 void
335 GNUNET_CRYPTO_rsa_public_key_hash (const struct GNUNET_CRYPTO_rsa_PublicKey *key,
336                                    struct GNUNET_HashCode *hc)
337 {
338   char *buf;
339   size_t buf_size;
340
341   buf_size = GNUNET_CRYPTO_rsa_public_key_encode (key,
342                                                   &buf);
343   GNUNET_CRYPTO_hash (buf,
344                       buf_size,
345                       hc);
346   GNUNET_free (buf);
347 }
348
349
350 /**
351  * Decode the public key from the data-format back
352  * to the "normal", internal format.
353  *
354  * @param buf the buffer where the public key data is stored
355  * @param len the length of the data in @a buf
356  * @return NULL on error
357  */
358 struct GNUNET_CRYPTO_rsa_PublicKey *
359 GNUNET_CRYPTO_rsa_public_key_decode (const char *buf,
360                                      size_t len)
361 {
362   struct GNUNET_CRYPTO_rsa_PublicKey *key;
363   gcry_mpi_t n;
364   int ret;
365
366   key = GNUNET_new (struct GNUNET_CRYPTO_rsa_PublicKey);
367   if (0 !=
368       gcry_sexp_new (&key->sexp,
369                      buf,
370                      len,
371                      0))
372   {
373     GNUNET_break_op (0);
374     GNUNET_free (key);
375     return NULL;
376   }
377   /* verify that this is an RSA public key */
378   ret = key_from_sexp (&n, key->sexp, "public-key", "n");
379   if (0 != ret)
380     ret = key_from_sexp (&n, key->sexp, "rsa", "n");
381   if (0 != ret)
382   {
383     /* this is no public RSA key */
384     GNUNET_break (0);
385     gcry_sexp_release (key->sexp);
386     GNUNET_free (key);
387     return NULL;
388   }
389   gcry_mpi_release (n);
390   return key;
391 }
392
393
394 /**
395  * Create a blinding key
396  *
397  * @param len length of the key in bits (i.e. 2048)
398  * @return the newly created blinding key
399  */
400 struct GNUNET_CRYPTO_rsa_BlindingKey *
401 GNUNET_CRYPTO_rsa_blinding_key_create (unsigned int len)
402 {
403   struct GNUNET_CRYPTO_rsa_BlindingKey *blind;
404
405   blind = GNUNET_new (struct GNUNET_CRYPTO_rsa_BlindingKey);
406   blind->r = gcry_mpi_new (len);
407   gcry_mpi_randomize (blind->r,
408                       len,
409                       GCRY_STRONG_RANDOM);
410   return blind;
411 }
412
413
414 /**
415  * Compare the values of two blinding keys.
416  *
417  * @param b1 one key
418  * @param b2 the other key
419  * @return 0 if the two are equal
420  */
421 int
422 GNUNET_CRYPTO_rsa_blinding_key_cmp (struct GNUNET_CRYPTO_rsa_BlindingKey *b1,
423                                     struct GNUNET_CRYPTO_rsa_BlindingKey *b2)
424 {
425   return gcry_mpi_cmp (b1->r,
426                        b2->r);
427 }
428
429
430 /**
431  * Compare the values of two signatures.
432  *
433  * @param s1 one signature
434  * @param s2 the other signature
435  * @return 0 if the two are equal
436  */
437 int
438 GNUNET_CRYPTO_rsa_signature_cmp (struct GNUNET_CRYPTO_rsa_Signature *s1,
439                                  struct GNUNET_CRYPTO_rsa_Signature *s2)
440 {
441   char *b1;
442   char *b2;
443   size_t z1;
444   size_t z2;
445   int ret;
446
447   z1 = GNUNET_CRYPTO_rsa_signature_encode (s1,
448                                            &b1);
449   z2 = GNUNET_CRYPTO_rsa_signature_encode (s2,
450                                            &b2);
451   if (z1 != z2)
452     ret = 1;
453   else
454     ret = memcmp (b1,
455                   b2,
456                   z1);
457   GNUNET_free (b1);
458   GNUNET_free (b2);
459   return ret;
460 }
461
462
463 /**
464  * Compare the values of two public keys.
465  *
466  * @param p1 one public key
467  * @param p2 the other public key
468  * @return 0 if the two are equal
469  */
470 int
471 GNUNET_CRYPTO_rsa_public_key_cmp (struct GNUNET_CRYPTO_rsa_PublicKey *p1,
472                                   struct GNUNET_CRYPTO_rsa_PublicKey *p2)
473 {
474   char *b1;
475   char *b2;
476   size_t z1;
477   size_t z2;
478   int ret;
479
480   z1 = GNUNET_CRYPTO_rsa_public_key_encode (p1,
481                                             &b1);
482   z2 = GNUNET_CRYPTO_rsa_public_key_encode (p2,
483                                             &b2);
484   if (z1 != z2)
485     ret = 1;
486   else
487     ret = memcmp (b1,
488                   b2,
489                   z1);
490   GNUNET_free (b1);
491   GNUNET_free (b2);
492   return ret;
493 }
494
495
496 /**
497  * Compare the values of two private keys.
498  *
499  * @param p1 one private key
500  * @param p2 the other private key
501  * @return 0 if the two are equal
502  */
503 int
504 GNUNET_CRYPTO_rsa_private_key_cmp (struct GNUNET_CRYPTO_rsa_PrivateKey *p1,
505                                   struct GNUNET_CRYPTO_rsa_PrivateKey *p2)
506 {
507   char *b1;
508   char *b2;
509   size_t z1;
510   size_t z2;
511   int ret;
512
513   z1 = GNUNET_CRYPTO_rsa_private_key_encode (p1,
514                                             &b1);
515   z2 = GNUNET_CRYPTO_rsa_private_key_encode (p2,
516                                             &b2);
517   if (z1 != z2)
518     ret = 1;
519   else
520     ret = memcmp (b1,
521                   b2,
522                   z1);
523   GNUNET_free (b1);
524   GNUNET_free (b2);
525   return ret;
526 }
527
528
529 /**
530  * Destroy a blinding key
531  *
532  * @param bkey the blinding key to destroy
533  */
534 void
535 GNUNET_CRYPTO_rsa_blinding_key_free (struct GNUNET_CRYPTO_rsa_BlindingKey *bkey)
536 {
537   gcry_mpi_release (bkey->r);
538   GNUNET_free (bkey);
539 }
540
541
542 /**
543  * Encode the blinding key in a format suitable for
544  * storing it into a file.
545  *
546  * @param bkey the blinding key
547  * @param[out] buffer set to a buffer with the encoded key
548  * @return size of memory allocated in @a buffer
549  */
550 size_t
551 GNUNET_CRYPTO_rsa_blinding_key_encode (const struct GNUNET_CRYPTO_rsa_BlindingKey *bkey,
552                                char **buffer)
553 {
554   size_t n;
555   char *b;
556   size_t rsize;
557
558   gcry_mpi_print (GCRYMPI_FMT_USG,
559                   NULL,
560                   0,
561                   &n,
562                   bkey->r);
563   b = GNUNET_malloc (n);
564   GNUNET_assert (0 ==
565                  gcry_mpi_print (GCRYMPI_FMT_USG,
566                                  (unsigned char *) b,
567                                  n,
568                                  &rsize,
569                                  bkey->r));
570   *buffer = b;
571   return n;
572 }
573
574
575 /**
576  * Decode the blinding key from the data-format back
577  * to the "normal", internal format.
578  *
579  * @param buf the buffer where the public key data is stored
580  * @param len the length of the data in @a buf
581  * @return NULL on error
582  */
583 struct GNUNET_CRYPTO_rsa_BlindingKey *
584 GNUNET_CRYPTO_rsa_blinding_key_decode (const char *buf,
585                                size_t len)
586 {
587   struct GNUNET_CRYPTO_rsa_BlindingKey *bkey;
588   size_t rsize;
589
590   bkey = GNUNET_new (struct GNUNET_CRYPTO_rsa_BlindingKey);
591   if (0 !=
592       gcry_mpi_scan (&bkey->r,
593                      GCRYMPI_FMT_USG,
594                      (const unsigned char *) buf,
595                      len,
596                      &rsize))
597   {
598     GNUNET_break_op (0);
599     GNUNET_free (bkey);
600     return NULL;
601   }
602   return bkey;
603 }
604
605
606 /**
607  * Blinds the given message with the given blinding key
608  *
609  * @param hash hash of the message to sign
610  * @param bkey the blinding key
611  * @param pkey the public key of the signer
612  * @param[out] buffer set to a buffer with the blinded message to be signed
613  * @return number of bytes stored in @a buffer
614  */
615 size_t
616 GNUNET_CRYPTO_rsa_blind (const struct GNUNET_HashCode *hash,
617                  struct GNUNET_CRYPTO_rsa_BlindingKey *bkey,
618                  struct GNUNET_CRYPTO_rsa_PublicKey *pkey,
619                  char **buffer)
620 {
621   gcry_mpi_t data;
622   gcry_mpi_t ne[2];
623   gcry_mpi_t r_e;
624   gcry_mpi_t data_r_e;
625   size_t rsize;
626   size_t n;
627   gcry_error_t rc;
628   char *b;
629   int ret;
630
631   ret = key_from_sexp (ne, pkey->sexp, "public-key", "ne");
632   if (0 != ret)
633     ret = key_from_sexp (ne, pkey->sexp, "rsa", "ne");
634   if (0 != ret)
635   {
636     GNUNET_break (0);
637     *buffer = NULL;
638     return 0;
639   }
640   if (0 != (rc = gcry_mpi_scan (&data,
641                                 GCRYMPI_FMT_USG,
642                                 (const unsigned char *) hash,
643                                 sizeof (struct GNUNET_HashCode),
644                                 &rsize)))
645   {
646     GNUNET_break (0);
647     gcry_mpi_release (ne[0]);
648     gcry_mpi_release (ne[1]);
649     *buffer = NULL;
650     return 0;
651   }
652   r_e = gcry_mpi_new (0);
653   gcry_mpi_powm (r_e,
654                  bkey->r,
655                  ne[1],
656                  ne[0]);
657   data_r_e = gcry_mpi_new (0);
658   gcry_mpi_mulm (data_r_e,
659                  data,
660                  r_e,
661                  ne[0]);
662   gcry_mpi_release (data);
663   gcry_mpi_release (ne[0]);
664   gcry_mpi_release (ne[1]);
665   gcry_mpi_release (r_e);
666
667   gcry_mpi_print (GCRYMPI_FMT_USG,
668                   NULL,
669                   0,
670                   &n,
671                   data_r_e);
672   b = GNUNET_malloc (n);
673   rc = gcry_mpi_print (GCRYMPI_FMT_USG,
674                        (unsigned char *) b,
675                        n,
676                        &rsize,
677                        data_r_e);
678   gcry_mpi_release (data_r_e);
679   *buffer = b;
680   return n;
681 }
682
683
684 /**
685  * Convert the data specified in the given purpose argument to an
686  * S-expression suitable for signature operations.
687  *
688  * @param ptr pointer to the data to convert
689  * @param size the size of the data
690  * @return converted s-expression
691  */
692 static gcry_sexp_t
693 data_to_sexp (const void *ptr, size_t size)
694 {
695   gcry_mpi_t value;
696   gcry_sexp_t data;
697
698   value = NULL;
699   data = NULL;
700   GNUNET_assert (0 ==
701                  gcry_mpi_scan (&value,
702                                 GCRYMPI_FMT_USG,
703                                 ptr,
704                                 size,
705                                 NULL));
706   GNUNET_assert (0 ==
707                  gcry_sexp_build (&data,
708                                   NULL,
709                                   "(data (flags raw) (value %M))",
710                                   value));
711   gcry_mpi_release (value);
712   return data;
713 }
714
715
716 /**
717  * Sign the given message.
718  *
719  * @param key private key to use for the signing
720  * @param msg the message to sign
721  * @param msg_len number of bytes in @a msg to sign
722  * @return NULL on error, signature on success
723  */
724 struct GNUNET_CRYPTO_rsa_Signature *
725 GNUNET_CRYPTO_rsa_sign (const struct GNUNET_CRYPTO_rsa_PrivateKey *key,
726                         const void *msg,
727                         size_t msg_len)
728 {
729   struct GNUNET_CRYPTO_rsa_Signature *sig;
730   gcry_sexp_t result;
731   gcry_sexp_t data;
732
733   data = data_to_sexp (msg,
734                        msg_len);
735   if (0 !=
736       gcry_pk_sign (&result,
737                     data,
738                     key->sexp))
739   {
740     GNUNET_break (0);
741     return NULL;
742   }
743   gcry_sexp_release (data);
744   sig = GNUNET_new (struct GNUNET_CRYPTO_rsa_Signature);
745   sig->sexp = result;
746   return sig;
747 }
748
749
750 /**
751  * Free memory occupied by signature.
752  *
753  * @param sig memory to freee
754  */
755 void
756 GNUNET_CRYPTO_rsa_signature_free (struct GNUNET_CRYPTO_rsa_Signature *sig)
757 {
758   gcry_sexp_release (sig->sexp);
759   GNUNET_free (sig);
760 }
761
762
763 /**
764  * Encode the given signature in a format suitable for storing it into a file.
765  *
766  * @param sig the signature
767  * @param[out] buffer set to a buffer with the encoded key
768  * @return size of memory allocated in @a buffer
769  */
770 size_t
771 GNUNET_CRYPTO_rsa_signature_encode (const struct GNUNET_CRYPTO_rsa_Signature *sig,
772                                     char **buffer)
773 {
774   size_t n;
775   char *b;
776
777   n = gcry_sexp_sprint (sig->sexp,
778                         GCRYSEXP_FMT_ADVANCED,
779                         NULL,
780                         0);
781   b = GNUNET_malloc (n);
782   GNUNET_assert ((n - 1) ==     /* since the last byte is \0 */
783                  gcry_sexp_sprint (sig->sexp,
784                                    GCRYSEXP_FMT_ADVANCED,
785                                    b,
786                                    n));
787   *buffer = b;
788   return n;
789 }
790
791
792 /**
793  * Decode the signature from the data-format back to the "normal", internal
794  * format.
795  *
796  * @param buf the buffer where the public key data is stored
797  * @param len the length of the data in @a buf
798  * @return NULL on error
799  */
800 struct GNUNET_CRYPTO_rsa_Signature *
801 GNUNET_CRYPTO_rsa_signature_decode (const char *buf,
802                             size_t len)
803 {
804   struct GNUNET_CRYPTO_rsa_Signature *sig;
805   int ret;
806   gcry_mpi_t s;
807
808   sig = GNUNET_new (struct GNUNET_CRYPTO_rsa_Signature);
809   if (0 !=
810       gcry_sexp_new (&sig->sexp,
811                      buf,
812                      len,
813                      0))
814   {
815     GNUNET_break_op (0);
816     GNUNET_free (sig);
817     return NULL;
818   }
819   /* verify that this is an RSA signature */
820   ret = key_from_sexp (&s, sig->sexp, "sig-val", "s");
821   if (0 != ret)
822     ret = key_from_sexp (&s, sig->sexp, "rsa", "s");
823   if (0 != ret)
824   {
825     /* this is no RSA Signature */
826     GNUNET_break_op (0);
827     gcry_sexp_release (sig->sexp);
828     GNUNET_free (sig);
829     return NULL;
830   }
831   gcry_mpi_release (s);
832   return sig;
833 }
834
835
836 /**
837  * Duplicate the given public key
838  *
839  * @param key the public key to duplicate
840  * @return the duplicate key; NULL upon error
841  */
842 struct GNUNET_CRYPTO_rsa_PublicKey *
843 GNUNET_CRYPTO_rsa_public_key_dup (const struct GNUNET_CRYPTO_rsa_PublicKey *key)
844 {
845   struct GNUNET_CRYPTO_rsa_PublicKey *dup;
846   gcry_sexp_t dup_sexp;
847   size_t erroff;
848
849   /* check if we really are exporting a public key */
850   dup_sexp = gcry_sexp_find_token (key->sexp, "public-key", 0);
851   GNUNET_assert (NULL != dup_sexp);
852   gcry_sexp_release (dup_sexp);
853   /* copy the sexp */
854   GNUNET_assert (0 == gcry_sexp_build (&dup_sexp, &erroff, "%S", key->sexp));
855   dup = GNUNET_new (struct GNUNET_CRYPTO_rsa_PublicKey);
856   dup->sexp = dup_sexp;
857   return dup;
858 }
859
860
861 /**
862  * Unblind a blind-signed signature.  The signature should have been generated
863  * with #GNUNET_CRYPTO_rsa_sign() using a hash that was blinded with
864  * #GNUNET_CRYPTO_rsa_blind().
865  *
866  * @param sig the signature made on the blinded signature purpose
867  * @param bkey the blinding key used to blind the signature purpose
868  * @param pkey the public key of the signer
869  * @return unblinded signature on success, NULL on error
870  */
871 struct GNUNET_CRYPTO_rsa_Signature *
872 GNUNET_CRYPTO_rsa_unblind (struct GNUNET_CRYPTO_rsa_Signature *sig,
873                    struct GNUNET_CRYPTO_rsa_BlindingKey *bkey,
874                    struct GNUNET_CRYPTO_rsa_PublicKey *pkey)
875 {
876   gcry_mpi_t n;
877   gcry_mpi_t s;
878   gcry_mpi_t r_inv;
879   gcry_mpi_t ubsig;
880   int ret;
881   struct GNUNET_CRYPTO_rsa_Signature *sret;
882
883   ret = key_from_sexp (&n, pkey->sexp, "public-key", "n");
884   if (0 != ret)
885     ret = key_from_sexp (&n, pkey->sexp, "rsa", "n");
886   if (0 != ret)
887   {
888     GNUNET_break_op (0);
889     return NULL;
890   }
891   ret = key_from_sexp (&s, sig->sexp, "sig-val", "s");
892   if (0 != ret)
893     ret = key_from_sexp (&s, sig->sexp, "rsa", "s");
894   if (0 != ret)
895   {
896     gcry_mpi_release (n);
897     GNUNET_break_op (0);
898     return NULL;
899   }
900   r_inv = gcry_mpi_new (0);
901   if (1 !=
902       gcry_mpi_invm (r_inv,
903                      bkey->r,
904                      n))
905   {
906     GNUNET_break_op (0);
907     gcry_mpi_release (n);
908     gcry_mpi_release (r_inv);
909     gcry_mpi_release (s);
910     return NULL;
911   }
912   ubsig = gcry_mpi_new (0);
913   gcry_mpi_mulm (ubsig, s, r_inv, n);
914   gcry_mpi_release (n);
915   gcry_mpi_release (r_inv);
916   gcry_mpi_release (s);
917
918   sret = GNUNET_new (struct GNUNET_CRYPTO_rsa_Signature);
919   GNUNET_assert (0 ==
920                  gcry_sexp_build (&sret->sexp,
921                                   NULL,
922                                   "(sig-val (rsa (s %M)))",
923                                   ubsig));
924   gcry_mpi_release (ubsig);
925   return sret;
926 }
927
928
929 /**
930  * Verify whether the given hash corresponds to the given signature and the
931  * signature is valid with respect to the given public key.
932  *
933  * @param hash hash of the message to verify to match the @a sig
934  * @param sig signature that is being validated
935  * @param public_key public key of the signer
936  * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
937  */
938 int
939 GNUNET_CRYPTO_rsa_verify (const struct GNUNET_HashCode *hash,
940                   const struct GNUNET_CRYPTO_rsa_Signature *sig,
941                   const struct GNUNET_CRYPTO_rsa_PublicKey *public_key)
942 {
943   gcry_sexp_t data;
944   int rc;
945
946   data = data_to_sexp (hash,
947                        sizeof (struct GNUNET_HashCode));
948   rc = gcry_pk_verify (sig->sexp,
949                        data,
950                        public_key->sexp);
951   gcry_sexp_release (data);
952   if (0 != rc)
953   {
954     LOG (GNUNET_ERROR_TYPE_WARNING,
955          _("RSA signature verification failed at %s:%d: %s\n"),
956          __FILE__,
957          __LINE__,
958          gcry_strerror (rc));
959     return GNUNET_SYSERR;
960   }
961   return GNUNET_OK;
962 }
963
964
965 /**
966  * Duplicate the given private key
967  *
968  * @param key the private key to duplicate
969  * @return the duplicate key; NULL upon error
970  */
971 struct GNUNET_CRYPTO_rsa_PrivateKey *
972 GNUNET_CRYPTO_rsa_private_key_dup (const struct GNUNET_CRYPTO_rsa_PrivateKey *key)
973 {
974   struct GNUNET_CRYPTO_rsa_PrivateKey *dup;
975   gcry_sexp_t dup_sexp;
976   size_t erroff;
977
978   /* check if we really are exporting a private key */
979   dup_sexp = gcry_sexp_find_token (key->sexp, "private-key", 0);
980   GNUNET_assert (NULL != dup_sexp);
981   gcry_sexp_release (dup_sexp);
982   /* copy the sexp */
983   GNUNET_assert (0 == gcry_sexp_build (&dup_sexp, &erroff, "%S", key->sexp));
984   dup = GNUNET_new (struct GNUNET_CRYPTO_rsa_PrivateKey);
985   dup->sexp = dup_sexp;
986   return dup;
987 }
988
989
990 /**
991  * Duplicate the given private key
992  *
993  * @param key the private key to duplicate
994  * @return the duplicate key; NULL upon error
995  */
996 struct GNUNET_CRYPTO_rsa_Signature *
997 GNUNET_CRYPTO_rsa_signature_dup (const struct GNUNET_CRYPTO_rsa_Signature *sig)
998 {
999   struct GNUNET_CRYPTO_rsa_Signature *dup;
1000   gcry_sexp_t dup_sexp;
1001   size_t erroff;
1002   gcry_mpi_t s;
1003   int ret;
1004
1005   /* verify that this is an RSA signature */
1006   ret = key_from_sexp (&s, sig->sexp, "sig-val", "s");
1007   GNUNET_assert (0 == ret);
1008   ret = key_from_sexp (&s, sig->sexp, "rsa", "s");
1009   GNUNET_assert (0==ret);
1010   /* copy the sexp */
1011   GNUNET_assert (0 == gcry_sexp_build (&dup_sexp, &erroff, "%S", sig->sexp));
1012   dup = GNUNET_new (struct GNUNET_CRYPTO_rsa_Signature);
1013   dup->sexp = dup_sexp;
1014   return dup;
1015 }
1016
1017
1018 /* end of util/rsa.c */