-fix bad array init
[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_util_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
226   key = GNUNET_new (struct GNUNET_CRYPTO_rsa_PrivateKey);
227   if (0 !=
228       gcry_sexp_new (&key->sexp,
229                      buf,
230                      len,
231                      0))
232   {
233     GNUNET_break_op (0);
234     GNUNET_free (key);
235     return NULL;
236   }
237   /* FIXME: verify that this is an RSA private key */
238   return key;
239 }
240
241
242 /**
243  * Extract the public key of the given private key.
244  *
245  * @param priv the private key
246  * @retur NULL on error, otherwise the public key
247  */
248 struct GNUNET_CRYPTO_rsa_PublicKey *
249 GNUNET_CRYPTO_rsa_private_key_get_public (const struct GNUNET_CRYPTO_rsa_PrivateKey *priv)
250 {
251   struct GNUNET_CRYPTO_rsa_PublicKey *pub;
252   gcry_mpi_t ne[2];
253   int rc;
254   gcry_sexp_t result;
255
256   rc = key_from_sexp (ne, priv->sexp, "public-key", "ne");
257   if (0 != rc)
258     rc = key_from_sexp (ne, priv->sexp, "private-key", "ne");
259   if (0 != rc)
260     rc = key_from_sexp (ne, priv->sexp, "rsa", "ne");
261   if (0 != rc)
262   {
263     GNUNET_break_op (0);
264     return NULL;
265   }
266   rc = gcry_sexp_build (&result,
267                         NULL,
268                         "(public-key(rsa(n %m)(e %m)))",
269                         ne[0],
270                         ne[1]);
271   gcry_mpi_release (ne[0]);
272   gcry_mpi_release (ne[1]);
273   pub = GNUNET_new (struct GNUNET_CRYPTO_rsa_PublicKey);
274   pub->sexp = result;
275   return pub;
276 }
277
278
279 /**
280  * Free memory occupied by the public key.
281  *
282  * @param key pointer to the memory to free
283  */
284 void
285 GNUNET_CRYPTO_rsa_public_key_free (struct GNUNET_CRYPTO_rsa_PublicKey *key)
286 {
287   gcry_sexp_release (key->sexp);
288   GNUNET_free (key);
289 }
290
291
292 /**
293  * Encode the public key in a format suitable for
294  * storing it into a file.
295  *
296  * @param key the private key
297  * @param[out] buffer set to a buffer with the encoded key
298  * @return size of memory allocated in @a buffer
299  */
300 size_t
301 GNUNET_CRYPTO_rsa_public_key_encode (const struct GNUNET_CRYPTO_rsa_PublicKey *key,
302                                      char **buffer)
303 {
304   size_t n;
305   char *b;
306
307   n = gcry_sexp_sprint (key->sexp,
308                         GCRYSEXP_FMT_ADVANCED,
309                         NULL,
310                         0);
311   b = GNUNET_malloc (n);
312   GNUNET_assert ((n -1) ==      /* since the last byte is \0 */
313                  gcry_sexp_sprint (key->sexp,
314                                    GCRYSEXP_FMT_ADVANCED,
315                                    b,
316                                    n));
317   *buffer = b;
318   return n;
319 }
320
321
322 /**
323  * Compute hash over the public key.
324  *
325  * @param key public key to hash
326  * @param hc where to store the hash code
327  */
328 void
329 GNUNET_CRYPTO_rsa_public_key_hash (const struct GNUNET_CRYPTO_rsa_PublicKey *key,
330                                    struct GNUNET_HashCode *hc)
331 {
332   char *buf;
333   size_t buf_size;
334
335   buf_size = GNUNET_CRYPTO_rsa_public_key_encode (key,
336                                                   &buf);
337   GNUNET_CRYPTO_hash (buf,
338                       buf_size,
339                       hc);
340   GNUNET_free (buf);
341 }
342
343
344 /**
345  * Decode the public key from the data-format back
346  * to the "normal", internal format.
347  *
348  * @param buf the buffer where the public key data is stored
349  * @param len the length of the data in @a buf
350  * @return NULL on error
351  */
352 struct GNUNET_CRYPTO_rsa_PublicKey *
353 GNUNET_CRYPTO_rsa_public_key_decode (const char *buf,
354                                      size_t len)
355 {
356   struct GNUNET_CRYPTO_rsa_PublicKey *key;
357   gcry_mpi_t n;
358   int ret;
359
360   key = GNUNET_new (struct GNUNET_CRYPTO_rsa_PublicKey);
361   if (0 !=
362       gcry_sexp_new (&key->sexp,
363                      buf,
364                      len,
365                      0))
366   {
367     GNUNET_break_op (0);
368     GNUNET_free (key);
369     return NULL;
370   }
371   /* verify that this is an RSA public key */
372   ret = key_from_sexp (&n, key->sexp, "public-key", "n");
373   if (0 != ret)
374     ret = key_from_sexp (&n, key->sexp, "rsa", "n");
375   if (0 != ret)
376   {
377     /* this is no public RSA key */
378     GNUNET_break (0);
379     gcry_sexp_release (key->sexp);
380     GNUNET_free (key);
381     return NULL;
382   }
383   gcry_mpi_release (n);
384   return key;
385 }
386
387
388 /**
389  * Create a blinding key
390  *
391  * @param len length of the key in bits (i.e. 2048)
392  * @return the newly created blinding key
393  */
394 struct GNUNET_CRYPTO_rsa_BlindingKey *
395 GNUNET_CRYPTO_rsa_blinding_key_create (unsigned int len)
396 {
397   struct GNUNET_CRYPTO_rsa_BlindingKey *blind;
398
399   blind = GNUNET_new (struct GNUNET_CRYPTO_rsa_BlindingKey);
400   blind->r = gcry_mpi_new (len);
401   gcry_mpi_randomize (blind->r,
402                       len,
403                       GCRY_STRONG_RANDOM);
404   return blind;
405 }
406
407
408 /**
409  * Destroy a blinding key
410  *
411  * @param bkey the blinding key to destroy
412  */
413 void
414 GNUNET_CRYPTO_rsa_blinding_key_free (struct GNUNET_CRYPTO_rsa_BlindingKey *bkey)
415 {
416   gcry_mpi_release (bkey->r);
417   GNUNET_free (bkey);
418 }
419
420
421 /**
422  * Encode the blinding key in a format suitable for
423  * storing it into a file.
424  *
425  * @param bkey the blinding key
426  * @param[out] buffer set to a buffer with the encoded key
427  * @return size of memory allocated in @a buffer
428  */
429 size_t
430 GNUNET_CRYPTO_rsa_blinding_key_encode (const struct GNUNET_CRYPTO_rsa_BlindingKey *bkey,
431                                char **buffer)
432 {
433   size_t n;
434   char *b;
435   size_t rsize;
436
437   gcry_mpi_print (GCRYMPI_FMT_USG,
438                   NULL,
439                   0,
440                   &n,
441                   bkey->r);
442   b = GNUNET_malloc (n);
443   GNUNET_assert (0 ==
444                  gcry_mpi_print (GCRYMPI_FMT_USG,
445                                  (unsigned char *) b,
446                                  n,
447                                  &rsize,
448                                  bkey->r));
449   *buffer = b;
450   return n;
451 }
452
453
454 /**
455  * Decode the blinding key from the data-format back
456  * to the "normal", internal format.
457  *
458  * @param buf the buffer where the public key data is stored
459  * @param len the length of the data in @a buf
460  * @return NULL on error
461  */
462 struct GNUNET_CRYPTO_rsa_BlindingKey *
463 GNUNET_CRYPTO_rsa_blinding_key_decode (const char *buf,
464                                size_t len)
465 {
466   struct GNUNET_CRYPTO_rsa_BlindingKey *bkey;
467   size_t rsize;
468
469   bkey = GNUNET_new (struct GNUNET_CRYPTO_rsa_BlindingKey);
470   if (0 !=
471       gcry_mpi_scan (&bkey->r,
472                      GCRYMPI_FMT_USG,
473                      (const unsigned char *) buf,
474                      len,
475                      &rsize))
476   {
477     GNUNET_break_op (0);
478     GNUNET_free (bkey);
479     return NULL;
480   }
481   return bkey;
482 }
483
484
485 /**
486  * Blinds the given message with the given blinding key
487  *
488  * @param hash hash of the message to sign
489  * @param bkey the blinding key
490  * @param pkey the public key of the signer
491  * @param[out] buffer set to a buffer with the blinded message to be signed
492  * @return number of bytes stored in @a buffer
493  */
494 size_t
495 GNUNET_CRYPTO_rsa_blind (const struct GNUNET_HashCode *hash,
496                  struct GNUNET_CRYPTO_rsa_BlindingKey *bkey,
497                  struct GNUNET_CRYPTO_rsa_PublicKey *pkey,
498                  char **buffer)
499 {
500   gcry_mpi_t data;
501   gcry_mpi_t ne[2];
502   gcry_mpi_t r_e;
503   gcry_mpi_t data_r_e;
504   size_t rsize;
505   size_t n;
506   gcry_error_t rc;
507   char *b;
508   int ret;
509
510   ret = key_from_sexp (ne, pkey->sexp, "public-key", "ne");
511   if (0 != ret)
512     ret = key_from_sexp (ne, pkey->sexp, "rsa", "ne");
513   if (0 != ret)
514   {
515     GNUNET_break (0);
516     *buffer = NULL;
517     return 0;
518   }
519   if (0 != (rc = gcry_mpi_scan (&data,
520                                 GCRYMPI_FMT_USG,
521                                 (const unsigned char *) hash,
522                                 sizeof (struct GNUNET_HashCode),
523                                 &rsize)))
524   {
525     GNUNET_break (0);
526     gcry_mpi_release (ne[0]);
527     gcry_mpi_release (ne[1]);
528     *buffer = NULL;
529     return 0;
530   }
531   r_e = gcry_mpi_new (0);
532   gcry_mpi_powm (r_e,
533                  bkey->r,
534                  ne[1],
535                  ne[0]);
536   data_r_e = gcry_mpi_new (0);
537   gcry_mpi_mulm (data_r_e,
538                  data,
539                  r_e,
540                  ne[0]);
541   gcry_mpi_release (data);
542   gcry_mpi_release (ne[0]);
543   gcry_mpi_release (ne[1]);
544   gcry_mpi_release (r_e);
545
546   gcry_mpi_print (GCRYMPI_FMT_USG,
547                   NULL,
548                   0,
549                   &n,
550                   data_r_e);
551   b = GNUNET_malloc (n);
552   rc = gcry_mpi_print (GCRYMPI_FMT_USG,
553                        (unsigned char *) b,
554                        n,
555                        &rsize,
556                        data_r_e);
557   gcry_mpi_release (data_r_e);
558   *buffer = b;
559   return n;
560 }
561
562
563 /**
564  * Convert the data specified in the given purpose argument to an
565  * S-expression suitable for signature operations.
566  *
567  * @param ptr pointer to the data to convert
568  * @param size the size of the data
569  * @return converted s-expression
570  */
571 static gcry_sexp_t
572 data_to_sexp (const void *ptr, size_t size)
573 {
574   gcry_mpi_t value;
575   gcry_sexp_t data;
576
577   value = NULL;
578   data = NULL;
579   GNUNET_assert (0 ==
580                  gcry_mpi_scan (&value,
581                                 GCRYMPI_FMT_USG,
582                                 ptr,
583                                 size,
584                                 NULL));
585   GNUNET_assert (0 ==
586                  gcry_sexp_build (&data,
587                                   NULL,
588                                   "(data (flags raw) (value %M))",
589                                   value));
590   gcry_mpi_release (value);
591   return data;
592 }
593
594
595 /**
596  * Sign the given message.
597  *
598  * @param key private key to use for the signing
599  * @param msg the message to sign
600  * @param msg_len number of bytes in @a msg to sign
601  * @return NULL on error, signature on success
602  */
603 struct GNUNET_CRYPTO_rsa_Signature *
604 GNUNET_CRYPTO_rsa_sign (const struct GNUNET_CRYPTO_rsa_PrivateKey *key,
605                 const void *msg,
606                 size_t msg_len)
607 {
608   struct GNUNET_CRYPTO_rsa_Signature *sig;
609   gcry_sexp_t result;
610   gcry_sexp_t data;
611
612   data = data_to_sexp (msg,
613                        msg_len);
614   if (0 !=
615       gcry_pk_sign (&result,
616                     data,
617                     key->sexp))
618   {
619     GNUNET_break (0);
620     return NULL;
621   }
622   gcry_sexp_release (data);
623   sig = GNUNET_new (struct GNUNET_CRYPTO_rsa_Signature);
624   sig->sexp = result;
625   return sig;
626 }
627
628
629 /**
630  * Free memory occupied by signature.
631  *
632  * @param sig memory to freee
633  */
634 void
635 GNUNET_CRYPTO_rsa_signature_free (struct GNUNET_CRYPTO_rsa_Signature *sig)
636 {
637   gcry_sexp_release (sig->sexp);
638   GNUNET_free (sig);
639 }
640
641
642 /**
643  * Encode the given signature in a format suitable for storing it into a file.
644  *
645  * @param sig the signature
646  * @param[out] buffer set to a buffer with the encoded key
647  * @return size of memory allocated in @a buffer
648  */
649 size_t
650 GNUNET_CRYPTO_rsa_signature_encode (const struct GNUNET_CRYPTO_rsa_Signature *sig,
651                             char **buffer)
652 {
653   size_t n;
654   char *b;
655
656   n = gcry_sexp_sprint (sig->sexp,
657                         GCRYSEXP_FMT_ADVANCED,
658                         NULL,
659                         0);
660   b = GNUNET_malloc (n);
661   GNUNET_assert ((n - 1) ==     /* since the last byte is \0 */
662                  gcry_sexp_sprint (sig->sexp,
663                                    GCRYSEXP_FMT_ADVANCED,
664                                    b,
665                                    n));
666   *buffer = b;
667   return n;
668 }
669
670
671 /**
672  * Decode the signature from the data-format back to the "normal", internal
673  * format.
674  *
675  * @param buf the buffer where the public key data is stored
676  * @param len the length of the data in @a buf
677  * @return NULL on error
678  */
679 struct GNUNET_CRYPTO_rsa_Signature *
680 GNUNET_CRYPTO_rsa_signature_decode (const char *buf,
681                             size_t len)
682 {
683   struct GNUNET_CRYPTO_rsa_Signature *sig;
684   int ret;
685   gcry_mpi_t s;
686
687   sig = GNUNET_new (struct GNUNET_CRYPTO_rsa_Signature);
688   if (0 !=
689       gcry_sexp_new (&sig->sexp,
690                      buf,
691                      len,
692                      0))
693   {
694     GNUNET_break_op (0);
695     GNUNET_free (sig);
696     return NULL;
697   }
698   /* verify that this is an RSA signature */
699   ret = key_from_sexp (&s, sig->sexp, "sig-val", "s");
700   if (0 != ret)
701     ret = key_from_sexp (&s, sig->sexp, "rsa", "s");
702   if (0 != ret)
703   {
704     /* this is no RSA Signature */
705     GNUNET_break_op (0);
706     gcry_sexp_release (sig->sexp);
707     GNUNET_free (sig);
708     return NULL;
709   }
710   gcry_mpi_release (s);
711   return sig;
712 }
713
714
715 /**
716  * Unblind a blind-signed signature.  The signature should have been generated
717  * with #GNUNET_CRYPTO_rsa_sign() using a hash that was blinded with
718  * #GNUNET_CRYPTO_rsa_blind().
719  *
720  * @param sig the signature made on the blinded signature purpose
721  * @param bkey the blinding key used to blind the signature purpose
722  * @param pkey the public key of the signer
723  * @return unblinded signature on success, NULL on error
724  */
725 struct GNUNET_CRYPTO_rsa_Signature *
726 GNUNET_CRYPTO_rsa_unblind (struct GNUNET_CRYPTO_rsa_Signature *sig,
727                    struct GNUNET_CRYPTO_rsa_BlindingKey *bkey,
728                    struct GNUNET_CRYPTO_rsa_PublicKey *pkey)
729 {
730   gcry_mpi_t n;
731   gcry_mpi_t s;
732   gcry_mpi_t r_inv;
733   gcry_mpi_t ubsig;
734   int ret;
735   struct GNUNET_CRYPTO_rsa_Signature *sret;
736
737   ret = key_from_sexp (&n, pkey->sexp, "public-key", "n");
738   if (0 != ret)
739     ret = key_from_sexp (&n, pkey->sexp, "rsa", "n");
740   if (0 != ret)
741   {
742     GNUNET_break_op (0);
743     return NULL;
744   }
745   ret = key_from_sexp (&s, sig->sexp, "sig-val", "s");
746   if (0 != ret)
747     ret = key_from_sexp (&s, sig->sexp, "rsa", "s");
748   if (0 != ret)
749   {
750     gcry_mpi_release (n);
751     GNUNET_break_op (0);
752     return NULL;
753   }
754   r_inv = gcry_mpi_new (0);
755   if (1 !=
756       gcry_mpi_invm (r_inv,
757                      bkey->r,
758                      n))
759   {
760     GNUNET_break_op (0);
761     gcry_mpi_release (n);
762     gcry_mpi_release (r_inv);
763     gcry_mpi_release (s);
764     return NULL;
765   }
766   ubsig = gcry_mpi_new (0);
767   gcry_mpi_mulm (ubsig, s, r_inv, n);
768   gcry_mpi_release (n);
769   gcry_mpi_release (r_inv);
770   gcry_mpi_release (s);
771
772   sret = GNUNET_new (struct GNUNET_CRYPTO_rsa_Signature);
773   GNUNET_assert (0 ==
774                  gcry_sexp_build (&sret->sexp,
775                                   NULL,
776                                   "(sig-val (rsa (s %M)))",
777                                   ubsig));
778   gcry_mpi_release (ubsig);
779   return sret;
780 }
781
782
783 /**
784  * Verify whether the given hash corresponds to the given signature and the
785  * signature is valid with respect to the given public key.
786  *
787  * @param hash hash of the message to verify to match the @a sig
788  * @param sig signature that is being validated
789  * @param public_key public key of the signer
790  * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
791  */
792 int
793 GNUNET_CRYPTO_rsa_verify (const struct GNUNET_HashCode *hash,
794                   const struct GNUNET_CRYPTO_rsa_Signature *sig,
795                   const struct GNUNET_CRYPTO_rsa_PublicKey *public_key)
796 {
797   gcry_sexp_t data;
798   int rc;
799
800   data = data_to_sexp (hash,
801                        sizeof (struct GNUNET_HashCode));
802   rc = gcry_pk_verify (sig->sexp,
803                        data,
804                        public_key->sexp);
805   gcry_sexp_release (data);
806   if (0 != rc)
807   {
808     LOG (GNUNET_ERROR_TYPE_WARNING,
809          _("RSA signature verification failed at %s:%d: %s\n"),
810          __FILE__,
811          __LINE__,
812          gcry_strerror (rc));
813     return GNUNET_SYSERR;
814   }
815   return GNUNET_OK;
816 }
817
818
819 /* end of util/rsa.c */