-adding conversion of public key to string and back
authorChristian Grothoff <christian@grothoff.org>
Sun, 4 Mar 2012 14:07:23 +0000 (14:07 +0000)
committerChristian Grothoff <christian@grothoff.org>
Sun, 4 Mar 2012 14:07:23 +0000 (14:07 +0000)
src/include/gnunet_crypto_lib.h
src/util/crypto_hash.c
src/util/crypto_rsa.c

index 640df8b0bb54cd49b257d318084d7eab20f062e7..7224e84f8e5d6e262da65073b8d05d2c15d700ce 100644 (file)
@@ -86,7 +86,7 @@ enum GNUNET_CRYPTO_Quality
 
 
 /**
- * Length of an RSA KEY (d,e,len), 2048 bit (=256 octests) key d, 2 byte e
+ * Length of an RSA KEY (n,e,len), 2048 bit (=256 octests) key n, 2 byte e
  */
 #define GNUNET_CRYPTO_RSA_KEY_LENGTH 258
 
@@ -761,6 +761,31 @@ GNUNET_CRYPTO_kdf (void *result, size_t out_len, const void *xts,
 struct GNUNET_CRYPTO_RsaPrivateKey *
 GNUNET_CRYPTO_rsa_key_create (void);
 
+
+/**
+ * Convert a public key to a string.
+ *
+ * @param pub key to convert
+ * @return string representing  'pub'
+ */
+char *
+GNUNET_CRYPTO_rsa_public_key_to_string (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pub);
+
+
+/**
+ * Convert a string representing a public key to a public key.
+ *
+ * @param enc encoded public key
+ * @param enclen number of bytes in enc (without 0-terminator)
+ * @param pub where to store the public key
+ * @return GNUNET_OK on success
+ */
+int
+GNUNET_CRYPTO_rsa_public_key_from_string (const char *enc, 
+                                         size_t enclen,
+                                         struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pub);
+
+
 /**
  * Encode the private key in a format suitable for
  * storing it into a file.
index dce449ff0303da36364376a658ab21ff58308a0f..572586b34086d1d521dcb8e11b2765f0f59e01de 100644 (file)
@@ -264,7 +264,7 @@ getValue__ (unsigned char a)
  * Convert binary data to ASCII encoding.  The ASCII encoding is rather
  * GNUnet specific.  It was chosen such that it only uses characters
  * in [0-9A-V], can be produced without complex arithmetics and uses a
- * small number of characters.  The GNUnet encoding uses 103 characters.
+ * small number of characters.  
  * Does not append 0-terminator, but returns a pointer to the place where
  * it should be placed, if needed.
  *
index 0b1c9a1282648254594b947a59784c5319fa5043..89351f2801a9bade111d7db31f800ba5fff89d30 100644 (file)
@@ -214,6 +214,70 @@ GNUNET_CRYPTO_rsa_key_get_public (const struct GNUNET_CRYPTO_RsaPrivateKey
 }
 
 
+/**
+ * Convert a public key to a string.
+ *
+ * @param pub key to convert
+ * @return string representing  'pub'
+ */
+char *
+GNUNET_CRYPTO_rsa_public_key_to_string (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pub)
+{
+  char *pubkeybuf;
+  size_t keylen = (sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)) * 8;
+  char *end;
+
+  if (keylen % 5 > 0)
+    keylen += 5 - keylen % 5;
+  keylen /= 5;
+  pubkeybuf = GNUNET_malloc (keylen + 1);
+  end = GNUNET_CRYPTO_data_to_string ((unsigned char *) &pub, 
+                                     sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), 
+                                     pubkeybuf, 
+                                     keylen);
+  if (NULL == end)
+    {
+      GNUNET_free (pubkeybuf);
+      return NULL;
+    }
+  *end = '\0';
+  return pubkeybuf;
+}
+
+
+/**
+ * Convert a string representing a public key to a public key.
+ *
+ * @param enc encoded public key
+ * @param enclen number of bytes in enc (without 0-terminator)
+ * @param pub where to store the public key
+ * @return GNUNET_OK on success
+ */
+int
+GNUNET_CRYPTO_rsa_public_key_from_string (const char *enc, 
+                                         size_t enclen,
+                                         struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pub)
+{
+  size_t keylen = (sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)) * 8;
+
+  if (keylen % 5 > 0)
+    keylen += 5 - keylen % 5;
+  keylen /= 5;
+  if (enclen != keylen)
+    return GNUNET_SYSERR;
+
+  if (GNUNET_OK != GNUNET_CRYPTO_string_to_data (enc, enclen,
+                                                (unsigned char*) pub,
+                                                sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
+    return GNUNET_SYSERR;
+  if ( (ntohs (pub->len) != sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)) ||
+       (ntohs (pub->padding) != 0) ||
+       (ntohs (pub->sizen) != GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH) )
+    return GNUNET_SYSERR;
+  return GNUNET_OK;
+}
+
+
 /**
  * Internal: publicKey => RSA-Key.
  *
@@ -271,6 +335,7 @@ public2PrivateKey (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
   return ret;
 }
 
+
 /**
  * Encode the private key in a format suitable for
  * storing it into a file.
@@ -359,6 +424,7 @@ GNUNET_CRYPTO_rsa_encode_key (const struct GNUNET_CRYPTO_RsaPrivateKey *hostkey)
   return retval;
 }
 
+
 /**
  * Decode the private key from the file-format back
  * to the "normal", internal format.
@@ -797,6 +863,7 @@ GNUNET_CRYPTO_rsa_encrypt (const void *block, size_t size,
   return GNUNET_OK;
 }
 
+
 /**
  * Decrypt a given block with the hostkey.
  *