2 This file is part of GNUnet.
3 (C) 2001, 2002, 2003, 2004, 2005, 2006, 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file util/crypto_rsa.c
23 * @brief public key cryptography (RSA) with libgcrypt
24 * @author Christian Grothoff
26 * Note that the code locks often needlessly on the gcrypt-locking api.
27 * One would think that simple MPI operations should not require locking
28 * (since only global operations on the random pool must be locked,
29 * strictly speaking). But libgcrypt does sometimes require locking in
30 * unexpected places, so the safe solution is to always lock even if it
31 * is not required. The performance impact is minimal anyway.
36 #include "gnunet_common.h"
37 #include "gnunet_crypto_lib.h"
38 #include "gnunet_disk_lib.h"
41 * The private information of an RSA key pair.
42 * NOTE: this must match the definition in crypto_ksk.c
44 struct GNUNET_CRYPTO_RsaPrivateKey
51 * GNUnet mandates a certain format for the encoding
52 * of private RSA key information that is provided
53 * by the RSA implementations. This format is used
54 * to serialize a private RSA key (typically when
55 * writing it to disk).
57 struct RsaPrivateKeyBinaryEncoded
60 * Total size of the structure, in bytes, in big-endian!
62 uint16_t len GNUNET_PACKED;
63 uint16_t sizen GNUNET_PACKED; /* in big-endian! */
64 uint16_t sizee GNUNET_PACKED; /* in big-endian! */
65 uint16_t sized GNUNET_PACKED; /* in big-endian! */
66 uint16_t sizep GNUNET_PACKED; /* in big-endian! */
67 uint16_t sizeq GNUNET_PACKED; /* in big-endian! */
68 uint16_t sizedmp1 GNUNET_PACKED; /* in big-endian! */
69 uint16_t sizedmq1 GNUNET_PACKED; /* in big-endian! */
70 /* followed by the actual values */
74 #define HOSTKEY_LEN 2048
76 #define EXTRA_CHECKS ALLOW_EXTRA_CHECKS
80 * Log an error message at log-level 'level' that indicates
81 * a failure of the command 'cmd' with the message given
82 * by gcry_strerror(rc).
84 #define LOG_GCRY(level, cmd, rc) do { GNUNET_log(level, _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, gcry_strerror(rc)); } while(0);
87 * If target != size, move target bytes to the
88 * end of the size-sized buffer and zero out the
89 * first target-size bytes.
92 adjust (unsigned char *buf, size_t size, size_t target)
96 memmove (&buf[target - size], buf, size);
97 memset (buf, 0, target - size);
102 * This HostKey implementation uses RSA.
104 struct GNUNET_CRYPTO_RsaPrivateKey *
105 GNUNET_CRYPTO_rsa_key_create ()
107 struct GNUNET_CRYPTO_RsaPrivateKey *ret;
109 gcry_sexp_t s_keyparam;
111 GNUNET_assert (0 == gcry_sexp_build (&s_keyparam,
113 "(genkey(rsa(nbits %d)(rsa-use-e 3:257)))",
115 GNUNET_assert (0 == gcry_pk_genkey (&s_key, s_keyparam));
116 gcry_sexp_release (s_keyparam);
118 GNUNET_assert (0 == gcry_pk_testkey (s_key));
120 ret = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_RsaPrivateKey));
126 * Free memory occupied by hostkey
129 GNUNET_CRYPTO_rsa_key_free (struct GNUNET_CRYPTO_RsaPrivateKey *hostkey)
131 gcry_sexp_release (hostkey->sexp);
132 GNUNET_free (hostkey);
136 key_from_sexp (gcry_mpi_t * array,
137 gcry_sexp_t sexp, const char *topname, const char *elems)
139 gcry_sexp_t list, l2;
143 list = gcry_sexp_find_token (sexp, topname, 0);
148 l2 = gcry_sexp_cadr (list);
149 gcry_sexp_release (list);
157 for (s = elems; *s; s++, idx++)
159 l2 = gcry_sexp_find_token (list, s, 1);
162 for (i = 0; i < idx; i++)
164 gcry_free (array[i]);
167 gcry_sexp_release (list);
168 return 3; /* required parameter not found */
170 array[idx] = gcry_sexp_nth_mpi (l2, 1, GCRYMPI_FMT_USG);
171 gcry_sexp_release (l2);
174 for (i = 0; i < idx; i++)
176 gcry_free (array[i]);
179 gcry_sexp_release (list);
180 return 4; /* required parameter is invalid */
183 gcry_sexp_release (list);
188 * Extract the public key of the host.
189 * @param priv the private key
190 * @param pub where to write the public key
193 GNUNET_CRYPTO_rsa_key_get_public (const struct GNUNET_CRYPTO_RsaPrivateKey
196 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
203 rc = key_from_sexp (skey, priv->sexp, "public-key", "ne");
205 rc = key_from_sexp (skey, priv->sexp, "private-key", "ne");
207 rc = key_from_sexp (skey, priv->sexp, "rsa", "ne");
208 GNUNET_assert (0 == rc);
210 htons (sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) -
211 sizeof (pub->padding));
212 pub->sizen = htons (GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH);
214 size = GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH;
215 GNUNET_assert (0 == gcry_mpi_print (GCRYMPI_FMT_USG,
216 &pub->key[0], size, &size, skey[0]));
217 adjust (&pub->key[0], size, GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH);
219 GNUNET_CRYPTO_RSA_KEY_LENGTH - GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH;
221 gcry_mpi_print (GCRYMPI_FMT_USG,
223 [GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH],
224 size, &size, skey[1]));
225 adjust (&pub->key[GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH], size,
226 GNUNET_CRYPTO_RSA_KEY_LENGTH -
227 GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH);
228 gcry_mpi_release (skey[0]);
229 gcry_mpi_release (skey[1]);
234 * Internal: publicKey => RSA-Key.
236 * Note that the return type is not actually a private
237 * key but rather an sexpression for the public key!
239 static struct GNUNET_CRYPTO_RsaPrivateKey *
240 public2PrivateKey (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
243 struct GNUNET_CRYPTO_RsaPrivateKey *ret;
251 if ((ntohs (publicKey->sizen) != GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH) ||
252 (ntohs (publicKey->len) !=
253 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) -
254 sizeof (publicKey->padding)))
259 size = GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH;
260 rc = gcry_mpi_scan (&n, GCRYMPI_FMT_USG, &publicKey->key[0], size, &size);
263 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
267 GNUNET_CRYPTO_RSA_KEY_LENGTH - GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH;
269 gcry_mpi_scan (&e, GCRYMPI_FMT_USG,
270 &publicKey->key[GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH],
274 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
275 gcry_mpi_release (n);
278 rc = gcry_sexp_build (&result,
279 &erroff, "(public-key(rsa(n %m)(e %m)))", n, e);
280 gcry_mpi_release (n);
281 gcry_mpi_release (e);
284 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc); /* erroff gives more info */
287 ret = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_RsaPrivateKey));
294 * Encode the private key in a format suitable for
295 * storing it into a file.
296 * @returns encoding of the private key.
297 * The first 4 bytes give the size of the array, as usual.
299 static struct RsaPrivateKeyBinaryEncoded *
300 rsa_encode_key (const struct GNUNET_CRYPTO_RsaPrivateKey *hostkey)
302 struct RsaPrivateKeyBinaryEncoded *retval;
311 if (gcry_pk_testkey (hostkey->sexp))
318 memset (pkv, 0, sizeof (gcry_mpi_t) * 6);
319 rc = key_from_sexp (pkv, hostkey->sexp, "private-key", "nedpqu");
321 rc = key_from_sexp (pkv, hostkey->sexp, "rsa", "nedpqu");
323 rc = key_from_sexp (pkv, hostkey->sexp, "private-key", "nedpq");
325 rc = key_from_sexp (pkv, hostkey->sexp, "rsa", "nedpq");
327 rc = key_from_sexp (pkv, hostkey->sexp, "private-key", "ned");
329 rc = key_from_sexp (pkv, hostkey->sexp, "rsa", "ned");
330 GNUNET_assert (0 == rc);
331 size = sizeof (struct RsaPrivateKeyBinaryEncoded);
332 for (i = 0; i < 6; i++)
336 GNUNET_assert (0 == gcry_mpi_aprint (GCRYMPI_FMT_USG,
337 (unsigned char **) &pbu[i],
347 GNUNET_assert (size < 65536);
348 retval = GNUNET_malloc (size);
349 retval->len = htons (size);
351 retval->sizen = htons (sizes[0]);
352 memcpy (&((char *) (&retval[1]))[i], pbu[0], sizes[0]);
354 retval->sizee = htons (sizes[1]);
355 memcpy (&((char *) (&retval[1]))[i], pbu[1], sizes[1]);
357 retval->sized = htons (sizes[2]);
358 memcpy (&((char *) (&retval[1]))[i], pbu[2], sizes[2]);
361 retval->sizep = htons (sizes[4]);
362 memcpy (&((char *) (&retval[1]))[i], pbu[4], sizes[4]);
364 retval->sizeq = htons (sizes[3]);
365 memcpy (&((char *) (&retval[1]))[i], pbu[3], sizes[3]);
367 retval->sizedmp1 = htons (0);
368 retval->sizedmq1 = htons (0);
369 memcpy (&((char *) (&retval[1]))[i], pbu[5], sizes[5]);
370 for (i = 0; i < 6; i++)
373 gcry_mpi_release (pkv[i]);
381 * Decode the private key from the file-format back
382 * to the "normal", internal format.
384 static struct GNUNET_CRYPTO_RsaPrivateKey *
385 rsa_decode_key (const struct RsaPrivateKeyBinaryEncoded *encoding)
387 struct GNUNET_CRYPTO_RsaPrivateKey *ret;
389 gcry_mpi_t n, e, d, p, q, u;
395 size = ntohs (encoding->sizen);
396 rc = gcry_mpi_scan (&n,
398 &((const unsigned char *) (&encoding[1]))[pos],
400 pos += ntohs (encoding->sizen);
403 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
406 size = ntohs (encoding->sizee);
407 rc = gcry_mpi_scan (&e,
409 &((const unsigned char *) (&encoding[1]))[pos],
411 pos += ntohs (encoding->sizee);
414 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
415 gcry_mpi_release (n);
418 size = ntohs (encoding->sized);
419 rc = gcry_mpi_scan (&d,
421 &((const unsigned char *) (&encoding[1]))[pos],
423 pos += ntohs (encoding->sized);
426 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
427 gcry_mpi_release (n);
428 gcry_mpi_release (e);
432 size = ntohs (encoding->sizep);
435 rc = gcry_mpi_scan (&q,
437 &((const unsigned char *) (&encoding[1]))[pos],
439 pos += ntohs (encoding->sizep);
442 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
443 gcry_mpi_release (n);
444 gcry_mpi_release (e);
445 gcry_mpi_release (d);
451 size = ntohs (encoding->sizeq);
454 rc = gcry_mpi_scan (&p,
456 &((const unsigned char *) (&encoding[1]))[pos],
458 pos += ntohs (encoding->sizeq);
461 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
462 gcry_mpi_release (n);
463 gcry_mpi_release (e);
464 gcry_mpi_release (d);
466 gcry_mpi_release (q);
472 pos += ntohs (encoding->sizedmp1);
473 pos += ntohs (encoding->sizedmq1);
475 ntohs (encoding->len) - sizeof (struct RsaPrivateKeyBinaryEncoded) - pos;
478 rc = gcry_mpi_scan (&u,
480 &((const unsigned char *) (&encoding[1]))[pos],
484 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
485 gcry_mpi_release (n);
486 gcry_mpi_release (e);
487 gcry_mpi_release (d);
489 gcry_mpi_release (p);
491 gcry_mpi_release (q);
498 if ((p != NULL) && (q != NULL) && (u != NULL))
500 rc = gcry_sexp_build (&res, &size, /* erroff */
501 "(private-key(rsa(n %m)(e %m)(d %m)(p %m)(q %m)(u %m)))",
506 if ((p != NULL) && (q != NULL))
508 rc = gcry_sexp_build (&res, &size, /* erroff */
509 "(private-key(rsa(n %m)(e %m)(d %m)(p %m)(q %m)))",
514 rc = gcry_sexp_build (&res, &size, /* erroff */
515 "(private-key(rsa(n %m)(e %m)(d %m)))",
519 gcry_mpi_release (n);
520 gcry_mpi_release (e);
521 gcry_mpi_release (d);
523 gcry_mpi_release (p);
525 gcry_mpi_release (q);
527 gcry_mpi_release (u);
530 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc);
532 if (gcry_pk_testkey (res))
534 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_testkey", rc);
538 ret = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_RsaPrivateKey));
545 * Create a new private key by reading it from a file. If the
546 * files does not exist, create a new key and write it to the
547 * file. Caller must free return value. Note that this function
548 * can not guarantee that another process might not be trying
549 * the same operation on the same file at the same time. The
550 * caller must somehow know that the file either already exists
551 * with a valid key OR be sure that no other process is calling
552 * this function at the same time. If the contents of the file
553 * are invalid the old file is deleted and a fresh key is
556 * @return new private key, NULL on error (for example,
559 struct GNUNET_CRYPTO_RsaPrivateKey *
560 GNUNET_CRYPTO_rsa_key_create_from_file (const char *filename)
562 struct GNUNET_CRYPTO_RsaPrivateKey *ret;
563 struct RsaPrivateKeyBinaryEncoded *enc;
565 struct GNUNET_DISK_FileHandle *fd;
569 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
570 struct GNUNET_PeerIdentity pid;
572 if (GNUNET_SYSERR == GNUNET_DISK_directory_create_for_file (filename))
574 while (GNUNET_YES != GNUNET_DISK_file_test (filename))
576 fd = GNUNET_DISK_file_open (filename,
577 GNUNET_DISK_OPEN_WRITE |
578 GNUNET_DISK_OPEN_CREATE |
579 GNUNET_DISK_OPEN_FAILIFEXISTS,
580 GNUNET_DISK_PERM_USER_READ |
581 GNUNET_DISK_PERM_USER_WRITE |
582 GNUNET_DISK_PERM_GROUP_READ);
587 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
594 GNUNET_DISK_file_lock (fd, 0,
596 RsaPrivateKeyBinaryEncoded),
603 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
605 ("Could not aquire lock on file `%s': %s...\n"),
606 filename, STRERROR (ec));
609 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
610 _("Creating a new private key. This may take a while.\n"));
611 ret = GNUNET_CRYPTO_rsa_key_create ();
612 GNUNET_assert (ret != NULL);
613 enc = rsa_encode_key (ret);
614 GNUNET_assert (enc != NULL);
615 GNUNET_assert (ntohs (enc->len) ==
616 GNUNET_DISK_file_write (fd, enc, ntohs (enc->len)));
619 GNUNET_DISK_file_sync (fd);
621 GNUNET_DISK_file_unlock (fd, 0,
623 RsaPrivateKeyBinaryEncoded)))
624 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fcntl",
626 GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd));
627 GNUNET_CRYPTO_rsa_key_get_public (ret, &pub);
628 GNUNET_CRYPTO_hash (&pub, sizeof (pub), &pid.hashPubKey);
629 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
630 _("I am host `%s'. Stored new private key in `%s'.\n"),
635 /* hostkey file exists already, read it! */
636 fd = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ,
637 GNUNET_DISK_PERM_NONE);
640 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", filename);
647 GNUNET_DISK_file_lock (fd, 0,
648 sizeof (struct RsaPrivateKeyBinaryEncoded),
654 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
655 _("Could not aquire lock on file `%s': %s...\n"),
656 filename, STRERROR (ec));
657 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
659 ("This may be ok if someone is currently generating a hostkey.\n"));
664 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
666 /* eh, what!? File we opened is now gone!? */
667 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
670 GNUNET_DISK_file_unlock (fd, 0,
672 RsaPrivateKeyBinaryEncoded)))
673 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fcntl",
675 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd));
679 if (GNUNET_YES != GNUNET_DISK_file_size (filename, &fs, GNUNET_YES))
681 if (fs < sizeof (struct RsaPrivateKeyBinaryEncoded))
683 /* maybe we got the read lock before the hostkey generating
684 process had a chance to get the write lock; give it up! */
686 GNUNET_DISK_file_unlock (fd, 0,
688 RsaPrivateKeyBinaryEncoded)))
689 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fcntl",
693 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
695 ("When trying to read hostkey file `%s' I found %u bytes but I need at least %u.\n"),
696 filename, (unsigned int) fs,
697 (unsigned int) sizeof (struct
698 RsaPrivateKeyBinaryEncoded));
699 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
701 ("This may be ok if someone is currently generating a hostkey.\n"));
703 sleep (2); /* wait a bit longer! */
708 enc = GNUNET_malloc (fs);
709 GNUNET_assert (fs == GNUNET_DISK_file_read (fd, enc, fs));
710 len = ntohs (enc->len);
712 if ((len != fs) || (NULL == (ret = rsa_decode_key (enc))))
714 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
716 ("File `%s' does not contain a valid private key. You should delete it.\n"),
721 GNUNET_DISK_file_unlock (fd, 0,
722 sizeof (struct RsaPrivateKeyBinaryEncoded)))
723 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
724 GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd));
725 GNUNET_CRYPTO_rsa_key_get_public (ret, &pub);
726 GNUNET_CRYPTO_hash (&pub, sizeof (pub), &pid.hashPubKey);
727 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
728 _("I am host `%s'. Read private key from `%s'.\n"),
736 * Encrypt a block with the public key of another host that uses the
739 * @param block the block to encrypt
740 * @param size the size of block
741 * @param publicKey the encoded public key used to encrypt
742 * @param target where to store the encrypted block
743 * @returns GNUNET_SYSERR on error, GNUNET_OK if ok
746 GNUNET_CRYPTO_rsa_encrypt (const void *block,
749 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey,
750 struct GNUNET_CRYPTO_RsaEncryptedData *target)
754 struct GNUNET_CRYPTO_RsaPrivateKey *pubkey;
760 GNUNET_assert (size <= sizeof (GNUNET_HashCode));
761 pubkey = public2PrivateKey (publicKey);
764 gcry_mpi_scan (&val, GCRYMPI_FMT_USG, block, isize, &isize));
766 gcry_sexp_build (&data, &erroff,
767 "(data (flags pkcs1)(value %m))", val));
768 gcry_mpi_release (val);
769 GNUNET_assert (0 == gcry_pk_encrypt (&result, data, pubkey->sexp));
770 gcry_sexp_release (data);
771 GNUNET_CRYPTO_rsa_key_free (pubkey);
773 GNUNET_assert (0 == key_from_sexp (&rval, result, "rsa", "a"));
774 gcry_sexp_release (result);
775 isize = sizeof (struct GNUNET_CRYPTO_RsaEncryptedData);
776 GNUNET_assert (0 == gcry_mpi_print (GCRYMPI_FMT_USG,
777 (unsigned char *) target, isize, &isize,
779 gcry_mpi_release (rval);
780 adjust (&target->encoding[0], isize,
781 sizeof (struct GNUNET_CRYPTO_RsaEncryptedData));
786 * Decrypt a given block with the hostkey.
788 * @param key the key with which to decrypt this block
789 * @param block the data to decrypt, encoded as returned by encrypt
790 * @param result pointer to a location where the result can be stored
791 * @param max the maximum number of bits to store for the result, if
792 * the decrypted block is bigger, an error is returned
793 * @return the size of the decrypted block, -1 on error
796 GNUNET_CRYPTO_rsa_decrypt (const struct GNUNET_CRYPTO_RsaPrivateKey * key,
797 const struct GNUNET_CRYPTO_RsaEncryptedData *
798 block, void *result, size_t max)
800 gcry_sexp_t resultsexp;
809 GNUNET_assert (0 == gcry_pk_testkey (key->sexp));
811 size = sizeof (struct GNUNET_CRYPTO_RsaEncryptedData);
812 GNUNET_assert (0 == gcry_mpi_scan (&val,
813 GCRYMPI_FMT_USG, &block->encoding[0],
816 gcry_sexp_build (&data, &erroff,
817 "(enc-val(flags)(rsa(a %m)))", val));
818 gcry_mpi_release (val);
819 GNUNET_assert (0 == gcry_pk_decrypt (&resultsexp, data, key->sexp));
820 gcry_sexp_release (data);
821 /* resultsexp has format "(value %m)" */
822 GNUNET_assert (NULL !=
823 (val = gcry_sexp_nth_mpi (resultsexp, 1, GCRYMPI_FMT_USG)));
824 gcry_sexp_release (resultsexp);
825 tmp = GNUNET_malloc (max + HOSTKEY_LEN / 8);
826 size = max + HOSTKEY_LEN / 8;
828 gcry_mpi_print (GCRYMPI_FMT_USG, tmp, size, &size, val));
829 gcry_mpi_release (val);
831 endp += (size - max);
833 memcpy (result, endp, size);
840 * Sign a given block.
842 * @param key private key to use for the signing
843 * @param purpose what to sign (size, purpose)
844 * @param sig where to write the signature
845 * @return GNUNET_SYSERR on error, GNUNET_OK on success
848 GNUNET_CRYPTO_rsa_sign (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
849 const struct GNUNET_CRYPTO_RsaSignaturePurpose
850 *purpose, struct GNUNET_CRYPTO_RsaSignature *sig)
860 GNUNET_CRYPTO_hash (purpose, ntohl (purpose->size), &hc);
861 #define FORMATSTRING "(4:data(5:flags5:pkcs1)(4:hash6:sha51264:0123456789012345678901234567890123456789012345678901234567890123))"
862 bufSize = strlen (FORMATSTRING) + 1;
863 buff = GNUNET_malloc (bufSize);
864 memcpy (buff, FORMATSTRING, bufSize);
868 ("0123456789012345678901234567890123456789012345678901234567890123))")
869 - 1], &hc, sizeof (GNUNET_HashCode));
870 GNUNET_assert (0 == gcry_sexp_new (&data, buff, bufSize, 0));
872 GNUNET_assert (0 == gcry_pk_sign (&result, data, key->sexp));
873 gcry_sexp_release (data);
874 GNUNET_assert (0 == key_from_sexp (&rval, result, "rsa", "s"));
875 gcry_sexp_release (result);
876 ssize = sizeof (struct GNUNET_CRYPTO_RsaSignature);
877 GNUNET_assert (0 == gcry_mpi_print (GCRYMPI_FMT_USG,
878 (unsigned char *) sig, ssize, &ssize,
880 gcry_mpi_release (rval);
881 adjust (sig->sig, ssize, sizeof (struct GNUNET_CRYPTO_RsaSignature));
889 * @param purpose what is the purpose that the signature should have?
890 * @param validate block to validate (size, purpose, data)
891 * @param sig signature that is being validated
892 * @param publicKey public key of the signer
893 * @returns GNUNET_OK if ok, GNUNET_SYSERR if invalid
896 GNUNET_CRYPTO_rsa_verify (uint32_t purpose,
897 const struct GNUNET_CRYPTO_RsaSignaturePurpose
899 const struct GNUNET_CRYPTO_RsaSignature *sig,
900 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
907 struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
914 if (purpose != ntohl (validate->purpose))
915 return GNUNET_SYSERR; /* purpose mismatch */
916 GNUNET_CRYPTO_hash (validate, ntohl (validate->size), &hc);
917 size = sizeof (struct GNUNET_CRYPTO_RsaSignature);
918 GNUNET_assert (0 == gcry_mpi_scan (&val,
920 (const unsigned char *) sig, size,
923 gcry_sexp_build (&sigdata, &erroff, "(sig-val(rsa(s %m)))",
925 gcry_mpi_release (val);
926 bufSize = strlen (FORMATSTRING) + 1;
927 buff = GNUNET_malloc (bufSize);
928 memcpy (buff, FORMATSTRING, bufSize);
929 memcpy (&buff[strlen (FORMATSTRING) -
931 ("0123456789012345678901234567890123456789012345678901234567890123))")],
932 &hc, sizeof (GNUNET_HashCode));
933 GNUNET_assert (0 == gcry_sexp_new (&data, buff, bufSize, 0));
935 hostkey = public2PrivateKey (publicKey);
938 gcry_sexp_release (data);
939 gcry_sexp_release (sigdata);
940 return GNUNET_SYSERR;
942 rc = gcry_pk_verify (sigdata, data, hostkey->sexp);
943 GNUNET_CRYPTO_rsa_key_free (hostkey);
944 gcry_sexp_release (data);
945 gcry_sexp_release (sigdata);
948 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
949 _("RSA signature verification failed at %s:%d: %s\n"),
950 __FILE__, __LINE__, gcry_strerror (rc));
951 return GNUNET_SYSERR;
957 /* end of crypto_rsa.c */