aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2012-03-04 14:07:23 +0000
committerChristian Grothoff <christian@grothoff.org>2012-03-04 14:07:23 +0000
commitfc646798891d075673e2ad8f2011c1e15160b6c1 (patch)
tree8e39b151eb710c1b7adcef75e47942f335565a44 /src
parent6a9425ddc6fa5de32bb97f05b46ab47c01106f80 (diff)
downloadgnunet-fc646798891d075673e2ad8f2011c1e15160b6c1.tar.gz
gnunet-fc646798891d075673e2ad8f2011c1e15160b6c1.zip
-adding conversion of public key to string and back
Diffstat (limited to 'src')
-rw-r--r--src/include/gnunet_crypto_lib.h27
-rw-r--r--src/util/crypto_hash.c2
-rw-r--r--src/util/crypto_rsa.c67
3 files changed, 94 insertions, 2 deletions
diff --git a/src/include/gnunet_crypto_lib.h b/src/include/gnunet_crypto_lib.h
index 640df8b0b..7224e84f8 100644
--- a/src/include/gnunet_crypto_lib.h
+++ b/src/include/gnunet_crypto_lib.h
@@ -86,7 +86,7 @@ enum GNUNET_CRYPTO_Quality
86 86
87 87
88/** 88/**
89 * Length of an RSA KEY (d,e,len), 2048 bit (=256 octests) key d, 2 byte e 89 * Length of an RSA KEY (n,e,len), 2048 bit (=256 octests) key n, 2 byte e
90 */ 90 */
91#define GNUNET_CRYPTO_RSA_KEY_LENGTH 258 91#define GNUNET_CRYPTO_RSA_KEY_LENGTH 258
92 92
@@ -761,6 +761,31 @@ GNUNET_CRYPTO_kdf (void *result, size_t out_len, const void *xts,
761struct GNUNET_CRYPTO_RsaPrivateKey * 761struct GNUNET_CRYPTO_RsaPrivateKey *
762GNUNET_CRYPTO_rsa_key_create (void); 762GNUNET_CRYPTO_rsa_key_create (void);
763 763
764
765/**
766 * Convert a public key to a string.
767 *
768 * @param pub key to convert
769 * @return string representing 'pub'
770 */
771char *
772GNUNET_CRYPTO_rsa_public_key_to_string (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pub);
773
774
775/**
776 * Convert a string representing a public key to a public key.
777 *
778 * @param enc encoded public key
779 * @param enclen number of bytes in enc (without 0-terminator)
780 * @param pub where to store the public key
781 * @return GNUNET_OK on success
782 */
783int
784GNUNET_CRYPTO_rsa_public_key_from_string (const char *enc,
785 size_t enclen,
786 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pub);
787
788
764/** 789/**
765 * Encode the private key in a format suitable for 790 * Encode the private key in a format suitable for
766 * storing it into a file. 791 * storing it into a file.
diff --git a/src/util/crypto_hash.c b/src/util/crypto_hash.c
index dce449ff0..572586b34 100644
--- a/src/util/crypto_hash.c
+++ b/src/util/crypto_hash.c
@@ -264,7 +264,7 @@ getValue__ (unsigned char a)
264 * Convert binary data to ASCII encoding. The ASCII encoding is rather 264 * Convert binary data to ASCII encoding. The ASCII encoding is rather
265 * GNUnet specific. It was chosen such that it only uses characters 265 * GNUnet specific. It was chosen such that it only uses characters
266 * in [0-9A-V], can be produced without complex arithmetics and uses a 266 * in [0-9A-V], can be produced without complex arithmetics and uses a
267 * small number of characters. The GNUnet encoding uses 103 characters. 267 * small number of characters.
268 * Does not append 0-terminator, but returns a pointer to the place where 268 * Does not append 0-terminator, but returns a pointer to the place where
269 * it should be placed, if needed. 269 * it should be placed, if needed.
270 * 270 *
diff --git a/src/util/crypto_rsa.c b/src/util/crypto_rsa.c
index 0b1c9a128..89351f280 100644
--- a/src/util/crypto_rsa.c
+++ b/src/util/crypto_rsa.c
@@ -215,6 +215,70 @@ GNUNET_CRYPTO_rsa_key_get_public (const struct GNUNET_CRYPTO_RsaPrivateKey
215 215
216 216
217/** 217/**
218 * Convert a public key to a string.
219 *
220 * @param pub key to convert
221 * @return string representing 'pub'
222 */
223char *
224GNUNET_CRYPTO_rsa_public_key_to_string (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pub)
225{
226 char *pubkeybuf;
227 size_t keylen = (sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)) * 8;
228 char *end;
229
230 if (keylen % 5 > 0)
231 keylen += 5 - keylen % 5;
232 keylen /= 5;
233 pubkeybuf = GNUNET_malloc (keylen + 1);
234 end = GNUNET_CRYPTO_data_to_string ((unsigned char *) &pub,
235 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
236 pubkeybuf,
237 keylen);
238 if (NULL == end)
239 {
240 GNUNET_free (pubkeybuf);
241 return NULL;
242 }
243 *end = '\0';
244 return pubkeybuf;
245}
246
247
248/**
249 * Convert a string representing a public key to a public key.
250 *
251 * @param enc encoded public key
252 * @param enclen number of bytes in enc (without 0-terminator)
253 * @param pub where to store the public key
254 * @return GNUNET_OK on success
255 */
256int
257GNUNET_CRYPTO_rsa_public_key_from_string (const char *enc,
258 size_t enclen,
259 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pub)
260{
261 size_t keylen = (sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)) * 8;
262
263 if (keylen % 5 > 0)
264 keylen += 5 - keylen % 5;
265 keylen /= 5;
266 if (enclen != keylen)
267 return GNUNET_SYSERR;
268
269 if (GNUNET_OK != GNUNET_CRYPTO_string_to_data (enc, enclen,
270 (unsigned char*) pub,
271 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
272 return GNUNET_SYSERR;
273 if ( (ntohs (pub->len) != sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)) ||
274 (ntohs (pub->padding) != 0) ||
275 (ntohs (pub->sizen) != GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH) )
276 return GNUNET_SYSERR;
277 return GNUNET_OK;
278}
279
280
281/**
218 * Internal: publicKey => RSA-Key. 282 * Internal: publicKey => RSA-Key.
219 * 283 *
220 * Note that the return type is not actually a private 284 * Note that the return type is not actually a private
@@ -271,6 +335,7 @@ public2PrivateKey (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
271 return ret; 335 return ret;
272} 336}
273 337
338
274/** 339/**
275 * Encode the private key in a format suitable for 340 * Encode the private key in a format suitable for
276 * storing it into a file. 341 * storing it into a file.
@@ -359,6 +424,7 @@ GNUNET_CRYPTO_rsa_encode_key (const struct GNUNET_CRYPTO_RsaPrivateKey *hostkey)
359 return retval; 424 return retval;
360} 425}
361 426
427
362/** 428/**
363 * Decode the private key from the file-format back 429 * Decode the private key from the file-format back
364 * to the "normal", internal format. 430 * to the "normal", internal format.
@@ -797,6 +863,7 @@ GNUNET_CRYPTO_rsa_encrypt (const void *block, size_t size,
797 return GNUNET_OK; 863 return GNUNET_OK;
798} 864}
799 865
866
800/** 867/**
801 * Decrypt a given block with the hostkey. 868 * Decrypt a given block with the hostkey.
802 * 869 *