2 This file is part of GNUnet
3 Copyright (C) 2014 Christian Grothoff (and other contributing authors)
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.
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.
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/>
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
25 #include "gnunet_util_lib.h"
27 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
31 * The private information of an RSA key pair.
33 struct GNUNET_CRYPTO_rsa_PrivateKey
36 * Libgcrypt S-expression for the RSA private key.
43 * The public information of an RSA key pair.
45 struct GNUNET_CRYPTO_rsa_PublicKey
48 * Libgcrypt S-expression for the RSA public key.
55 * @brief an RSA signature
57 struct GNUNET_CRYPTO_rsa_Signature
60 * Libgcrypt S-expression for the RSA signature.
67 * @brief RSA blinding key
69 struct GNUNET_CRYPTO_rsa_BlindingKey
72 * Random value used for blinding.
79 * Extract values from an S-expression.
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
88 key_from_sexp (gcry_mpi_t *array,
99 if (! (list = gcry_sexp_find_token (sexp, topname, 0)))
101 l2 = gcry_sexp_cadr (list);
102 gcry_sexp_release (list);
107 for (s = elems; *s; s++, idx++)
109 if (! (l2 = gcry_sexp_find_token (list, s, 1)))
111 for (i = 0; i < idx; i++)
113 gcry_free (array[i]);
116 gcry_sexp_release (list);
117 return 3; /* required parameter not found */
119 array[idx] = gcry_sexp_nth_mpi (l2, 1, GCRYMPI_FMT_USG);
120 gcry_sexp_release (l2);
123 for (i = 0; i < idx; i++)
125 gcry_free (array[i]);
128 gcry_sexp_release (list);
129 return 4; /* required parameter is invalid */
132 gcry_sexp_release (list);
138 * Create a new private key. Caller must free return value.
140 * @param len length of the key in bits (i.e. 2048)
141 * @return fresh private key
143 struct GNUNET_CRYPTO_rsa_PrivateKey *
144 GNUNET_CRYPTO_rsa_private_key_create (unsigned int len)
146 struct GNUNET_CRYPTO_rsa_PrivateKey *ret;
148 gcry_sexp_t s_keyparam;
151 gcry_sexp_build (&s_keyparam,
153 "(genkey(rsa(nbits %d)))",
156 gcry_pk_genkey (&s_key,
158 gcry_sexp_release (s_keyparam);
161 gcry_pk_testkey (s_key));
163 ret = GNUNET_new (struct GNUNET_CRYPTO_rsa_PrivateKey);
170 * Free memory occupied by the private key.
172 * @param key pointer to the memory to free
175 GNUNET_CRYPTO_rsa_private_key_free (struct GNUNET_CRYPTO_rsa_PrivateKey *key)
177 gcry_sexp_release (key->sexp);
183 * Encode the private key in a format suitable for
184 * storing it into a file.
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
191 GNUNET_CRYPTO_rsa_private_key_encode (const struct GNUNET_CRYPTO_rsa_PrivateKey *key,
197 n = gcry_sexp_sprint (key->sexp,
198 GCRYSEXP_FMT_DEFAULT,
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,
213 * Decode the private key from the data-format back
214 * to the "normal", internal format.
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
220 struct GNUNET_CRYPTO_rsa_PrivateKey *
221 GNUNET_CRYPTO_rsa_private_key_decode (const char *buf,
224 struct GNUNET_CRYPTO_rsa_PrivateKey *key;
226 key = GNUNET_new (struct GNUNET_CRYPTO_rsa_PrivateKey);
228 gcry_sexp_new (&key->sexp,
237 /* FIXME: verify that this is an RSA private key */
243 * Extract the public key of the given private key.
245 * @param priv the private key
246 * @retur NULL on error, otherwise the public key
248 struct GNUNET_CRYPTO_rsa_PublicKey *
249 GNUNET_CRYPTO_rsa_private_key_get_public (const struct GNUNET_CRYPTO_rsa_PrivateKey *priv)
251 struct GNUNET_CRYPTO_rsa_PublicKey *pub;
256 rc = key_from_sexp (ne, priv->sexp, "public-key", "ne");
258 rc = key_from_sexp (ne, priv->sexp, "private-key", "ne");
260 rc = key_from_sexp (ne, priv->sexp, "rsa", "ne");
266 rc = gcry_sexp_build (&result,
268 "(public-key(rsa(n %m)(e %m)))",
271 gcry_mpi_release (ne[0]);
272 gcry_mpi_release (ne[1]);
273 pub = GNUNET_new (struct GNUNET_CRYPTO_rsa_PublicKey);
280 * Free memory occupied by the public key.
282 * @param key pointer to the memory to free
285 GNUNET_CRYPTO_rsa_public_key_free (struct GNUNET_CRYPTO_rsa_PublicKey *key)
287 gcry_sexp_release (key->sexp);
293 * Encode the public key in a format suitable for
294 * storing it into a file.
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
301 GNUNET_CRYPTO_rsa_public_key_encode (const struct GNUNET_CRYPTO_rsa_PublicKey *key,
307 n = gcry_sexp_sprint (key->sexp,
308 GCRYSEXP_FMT_ADVANCED,
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,
323 * Compute hash over the public key.
325 * @param key public key to hash
326 * @param hc where to store the hash code
329 GNUNET_CRYPTO_rsa_public_key_hash (const struct GNUNET_CRYPTO_rsa_PublicKey *key,
330 struct GNUNET_HashCode *hc)
335 buf_size = GNUNET_CRYPTO_rsa_public_key_encode (key,
337 GNUNET_CRYPTO_hash (buf,
345 * Decode the public key from the data-format back
346 * to the "normal", internal format.
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
352 struct GNUNET_CRYPTO_rsa_PublicKey *
353 GNUNET_CRYPTO_rsa_public_key_decode (const char *buf,
356 struct GNUNET_CRYPTO_rsa_PublicKey *key;
360 key = GNUNET_new (struct GNUNET_CRYPTO_rsa_PublicKey);
362 gcry_sexp_new (&key->sexp,
371 /* verify that this is an RSA public key */
372 ret = key_from_sexp (&n, key->sexp, "public-key", "n");
374 ret = key_from_sexp (&n, key->sexp, "rsa", "n");
377 /* this is no public RSA key */
379 gcry_sexp_release (key->sexp);
383 gcry_mpi_release (n);
389 * Create a blinding key
391 * @param len length of the key in bits (i.e. 2048)
392 * @return the newly created blinding key
394 struct GNUNET_CRYPTO_rsa_BlindingKey *
395 GNUNET_CRYPTO_rsa_blinding_key_create (unsigned int len)
397 struct GNUNET_CRYPTO_rsa_BlindingKey *blind;
399 blind = GNUNET_new (struct GNUNET_CRYPTO_rsa_BlindingKey);
400 blind->r = gcry_mpi_new (len);
401 gcry_mpi_randomize (blind->r,
409 * Destroy a blinding key
411 * @param bkey the blinding key to destroy
414 GNUNET_CRYPTO_rsa_blinding_key_free (struct GNUNET_CRYPTO_rsa_BlindingKey *bkey)
416 gcry_mpi_release (bkey->r);
422 * Encode the blinding key in a format suitable for
423 * storing it into a file.
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
430 GNUNET_CRYPTO_rsa_blinding_key_encode (const struct GNUNET_CRYPTO_rsa_BlindingKey *bkey,
437 gcry_mpi_print (GCRYMPI_FMT_USG,
442 b = GNUNET_malloc (n);
444 gcry_mpi_print (GCRYMPI_FMT_USG,
455 * Decode the blinding key from the data-format back
456 * to the "normal", internal format.
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
462 struct GNUNET_CRYPTO_rsa_BlindingKey *
463 GNUNET_CRYPTO_rsa_blinding_key_decode (const char *buf,
466 struct GNUNET_CRYPTO_rsa_BlindingKey *bkey;
469 bkey = GNUNET_new (struct GNUNET_CRYPTO_rsa_BlindingKey);
471 gcry_mpi_scan (&bkey->r,
473 (const unsigned char *) buf,
486 * Blinds the given message with the given blinding key
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
495 GNUNET_CRYPTO_rsa_blind (const struct GNUNET_HashCode *hash,
496 struct GNUNET_CRYPTO_rsa_BlindingKey *bkey,
497 struct GNUNET_CRYPTO_rsa_PublicKey *pkey,
510 ret = key_from_sexp (ne, pkey->sexp, "public-key", "ne");
512 ret = key_from_sexp (ne, pkey->sexp, "rsa", "ne");
519 if (0 != (rc = gcry_mpi_scan (&data,
521 (const unsigned char *) hash,
522 sizeof (struct GNUNET_HashCode),
526 gcry_mpi_release (ne[0]);
527 gcry_mpi_release (ne[1]);
531 r_e = gcry_mpi_new (0);
536 data_r_e = gcry_mpi_new (0);
537 gcry_mpi_mulm (data_r_e,
541 gcry_mpi_release (data);
542 gcry_mpi_release (ne[0]);
543 gcry_mpi_release (ne[1]);
544 gcry_mpi_release (r_e);
546 gcry_mpi_print (GCRYMPI_FMT_USG,
551 b = GNUNET_malloc (n);
552 rc = gcry_mpi_print (GCRYMPI_FMT_USG,
557 gcry_mpi_release (data_r_e);
564 * Convert the data specified in the given purpose argument to an
565 * S-expression suitable for signature operations.
567 * @param ptr pointer to the data to convert
568 * @param size the size of the data
569 * @return converted s-expression
572 data_to_sexp (const void *ptr, size_t size)
580 gcry_mpi_scan (&value,
586 gcry_sexp_build (&data,
588 "(data (flags raw) (value %M))",
590 gcry_mpi_release (value);
596 * Sign the given message.
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
603 struct GNUNET_CRYPTO_rsa_Signature *
604 GNUNET_CRYPTO_rsa_sign (const struct GNUNET_CRYPTO_rsa_PrivateKey *key,
608 struct GNUNET_CRYPTO_rsa_Signature *sig;
612 data = data_to_sexp (msg,
615 gcry_pk_sign (&result,
622 gcry_sexp_release (data);
623 sig = GNUNET_new (struct GNUNET_CRYPTO_rsa_Signature);
630 * Free memory occupied by signature.
632 * @param sig memory to freee
635 GNUNET_CRYPTO_rsa_signature_free (struct GNUNET_CRYPTO_rsa_Signature *sig)
637 gcry_sexp_release (sig->sexp);
643 * Encode the given signature in a format suitable for storing it into a file.
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
650 GNUNET_CRYPTO_rsa_signature_encode (const struct GNUNET_CRYPTO_rsa_Signature *sig,
656 n = gcry_sexp_sprint (sig->sexp,
657 GCRYSEXP_FMT_ADVANCED,
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,
672 * Decode the signature from the data-format back to the "normal", internal
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
679 struct GNUNET_CRYPTO_rsa_Signature *
680 GNUNET_CRYPTO_rsa_signature_decode (const char *buf,
683 struct GNUNET_CRYPTO_rsa_Signature *sig;
687 sig = GNUNET_new (struct GNUNET_CRYPTO_rsa_Signature);
689 gcry_sexp_new (&sig->sexp,
698 /* verify that this is an RSA signature */
699 ret = key_from_sexp (&s, sig->sexp, "sig-val", "s");
701 ret = key_from_sexp (&s, sig->sexp, "rsa", "s");
704 /* this is no RSA Signature */
706 gcry_sexp_release (sig->sexp);
710 gcry_mpi_release (s);
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().
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
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)
735 struct GNUNET_CRYPTO_rsa_Signature *sret;
737 ret = key_from_sexp (&n, pkey->sexp, "public-key", "n");
739 ret = key_from_sexp (&n, pkey->sexp, "rsa", "n");
745 ret = key_from_sexp (&s, sig->sexp, "sig-val", "s");
747 ret = key_from_sexp (&s, sig->sexp, "rsa", "s");
750 gcry_mpi_release (n);
754 r_inv = gcry_mpi_new (0);
756 gcry_mpi_invm (r_inv,
761 gcry_mpi_release (n);
762 gcry_mpi_release (r_inv);
763 gcry_mpi_release (s);
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);
772 sret = GNUNET_new (struct GNUNET_CRYPTO_rsa_Signature);
774 gcry_sexp_build (&sret->sexp,
776 "(sig-val (rsa (s %M)))",
778 gcry_mpi_release (ubsig);
784 * Verify whether the given hash corresponds to the given signature and the
785 * signature is valid with respect to the given public key.
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
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)
800 data = data_to_sexp (hash,
801 sizeof (struct GNUNET_HashCode));
802 rc = gcry_pk_verify (sig->sexp,
805 gcry_sexp_release (data);
808 LOG (GNUNET_ERROR_TYPE_WARNING,
809 _("RSA signature verification failed at %s:%d: %s\n"),
813 return GNUNET_SYSERR;
819 /* end of util/rsa.c */