[epiphany/wip/ephy-sync] sync-crypto: Only use Nettle specific random generator



commit 14209f11b0adfcc08e4bc475c283b045d170c786
Author: Gabriel Ivascu <ivascu gabriel59 gmail com>
Date:   Tue Aug 30 17:16:56 2016 +0300

    sync-crypto: Only use Nettle specific random generator

 src/ephy-bookmark.c    |    5 ++++-
 src/ephy-sync-crypto.c |   35 +++++++++++++++++------------------
 src/ephy-sync-crypto.h |    4 +++-
 3 files changed, 24 insertions(+), 20 deletions(-)
---
diff --git a/src/ephy-bookmark.c b/src/ephy-bookmark.c
index 8fc1a6c..99af5e5 100644
--- a/src/ephy-bookmark.c
+++ b/src/ephy-bookmark.c
@@ -25,6 +25,8 @@
 
 #include <string.h>
 
+#define ID_LEN 32
+
 struct _EphyBookmark {
   GObject      parent_instance;
 
@@ -192,7 +194,8 @@ ephy_bookmark_class_init (EphyBookmarkClass *klass)
 static void
 ephy_bookmark_init (EphyBookmark *self)
 {
-  self->id = ephy_sync_crypto_generate_random_hex (32);
+  self->id = g_malloc0 (ID_LEN + 1);
+  ephy_sync_crypto_random_hex_gen (NULL, ID_LEN, (guint8 *)self->id);
 }
 
 static JsonNode *
diff --git a/src/ephy-sync-crypto.c b/src/ephy-sync-crypto.c
index 61c81ea..3e43796 100644
--- a/src/ephy-sync-crypto.c
+++ b/src/ephy-sync-crypto.c
@@ -28,6 +28,7 @@
 #include <string.h>
 
 #define HAWK_VERSION  1
+#define NONCE_LEN     6
 
 static const char hex_digits[] = "0123456789abcdef";
 
@@ -422,15 +423,6 @@ ephy_sync_crypto_hkdf (guint8 *in,
 }
 
 static void
-ephy_sync_crypto_random_gen (void   *ctx,
-                             gsize   length,
-                             guint8 *dst)
-{
-  for (gsize i = 0; i < length; i++)
-    dst[i] = g_random_int ();
-}
-
-static void
 ephy_sync_crypto_b64_to_b64_urlsafe (char *text)
 {
   g_assert (text != NULL);
@@ -618,13 +610,19 @@ ephy_sync_crypto_compute_hawk_header (const char                *url,
   g_return_val_if_fail (key != NULL, NULL);
 
   ts = ephy_sync_utils_current_time_seconds ();
-  nonce = options && options->nonce ? options->nonce : ephy_sync_crypto_generate_random_hex (6);
   hash = options ? options->hash : NULL;
   payload = options ? options->payload : NULL;
   timestamp = options ? options->timestamp : NULL;
   uri = soup_uri_new (url);
   resource = (char *)soup_uri_get_path (uri);
 
+  if (options != NULL && options->nonce != NULL) {
+    nonce = options->nonce;
+  } else {
+    nonce = g_malloc0 (NONCE_LEN + 1);
+    ephy_sync_crypto_random_hex_gen (NULL, NONCE_LEN, (guint8 *)nonce);
+  }
+
   if (soup_uri_get_query (uri) != NULL)
     resource = g_strconcat (resource, "?", soup_uri_get_query (uri), NULL);
 
@@ -710,7 +708,7 @@ ephy_sync_crypto_generate_rsa_key_pair (void)
 
   /* Key sizes below 2048 are considered breakable and should not be used */
   retval = rsa_generate_keypair (&public, &private,
-                                 NULL, ephy_sync_crypto_random_gen,
+                                 NULL, ephy_sync_crypto_random_hex_gen,
                                  NULL, NULL, 2048, 0);
   if (retval == 0) {
     g_warning ("Failed to generate RSA key pair");
@@ -758,7 +756,7 @@ ephy_sync_crypto_create_assertion (const char               *certificate,
   digest = ephy_sync_crypto_decode_hex (digest_hex);
 
   if (rsa_sha256_sign_digest_tr (&keypair->public, &keypair->private,
-                                 NULL, ephy_sync_crypto_random_gen,
+                                 NULL, ephy_sync_crypto_random_hex_gen,
                                  digest, signature) == 0) {
     g_warning ("Failed to sign the message. Giving up.");
     goto out;
@@ -790,14 +788,15 @@ out:
   return assertion;
 }
 
-char *
-ephy_sync_crypto_generate_random_hex (gsize length)
+void
+ephy_sync_crypto_random_hex_gen (void   *ctx,
+                                 gsize   length,
+                                 guint8 *dst)
 {
   FILE *fp;
   gsize num_bytes;
   guint8 *bytes;
   char *hex;
-  char *out;
 
   g_assert (length > 0);
   num_bytes = (length + 1) / 2;
@@ -806,13 +805,13 @@ ephy_sync_crypto_generate_random_hex (gsize length)
   fp = fopen ("/dev/urandom", "r");
   fread (bytes, sizeof (guint8), num_bytes, fp);
   hex = ephy_sync_crypto_encode_hex (bytes, num_bytes);
-  out = g_strndup (hex, length);
+
+  for (gsize i = 0; i < length; i++)
+    dst[i] = hex[i];
 
   g_free (bytes);
   g_free (hex);
   fclose (fp);
-
-  return out;
 }
 
 char *
diff --git a/src/ephy-sync-crypto.h b/src/ephy-sync-crypto.h
index a2dfb90..1ae6478 100644
--- a/src/ephy-sync-crypto.h
+++ b/src/ephy-sync-crypto.h
@@ -104,7 +104,9 @@ char                      *ephy_sync_crypto_create_assertion        (const char
                                                                      const char               *audience,
                                                                      guint64                   duration,
                                                                      EphySyncCryptoRSAKeyPair *keypair);
-char                      *ephy_sync_crypto_generate_random_hex     (gsize length);
+void                       ephy_sync_crypto_random_hex_gen          (void   *ctx,
+                                                                     gsize   length,
+                                                                     guint8 *dst);
 char                      *ephy_sync_crypto_base64_urlsafe_encode   (guint8   *data,
                                                                      gsize     data_len,
                                                                      gboolean  strip);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]