diff options
author | Christian Grothoff <christian@grothoff.org> | 2012-03-04 14:07:23 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2012-03-04 14:07:23 +0000 |
commit | fc646798891d075673e2ad8f2011c1e15160b6c1 (patch) | |
tree | 8e39b151eb710c1b7adcef75e47942f335565a44 /src/util/crypto_rsa.c | |
parent | 6a9425ddc6fa5de32bb97f05b46ab47c01106f80 (diff) | |
download | gnunet-fc646798891d075673e2ad8f2011c1e15160b6c1.tar.gz gnunet-fc646798891d075673e2ad8f2011c1e15160b6c1.zip |
-adding conversion of public key to string and back
Diffstat (limited to 'src/util/crypto_rsa.c')
-rw-r--r-- | src/util/crypto_rsa.c | 67 |
1 files changed, 67 insertions, 0 deletions
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 | */ | ||
223 | char * | ||
224 | GNUNET_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 | */ | ||
256 | int | ||
257 | GNUNET_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 | * |