[epiphany/wip/sync-rebase: 75/86] sync-crypto: Only use Nettle specific random generator
- From: Michael Catanzaro <mcatanzaro src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [epiphany/wip/sync-rebase: 75/86] sync-crypto: Only use Nettle specific random generator
- Date: Fri, 7 Oct 2016 22:55:01 +0000 (UTC)
commit 51d668819287bd66d87ddab612f851f034933043
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/bookmarks/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/bookmarks/ephy-bookmark.c b/src/bookmarks/ephy-bookmark.c
index d8e4e8c..dbba875 100644
--- a/src/bookmarks/ephy-bookmark.c
+++ b/src/bookmarks/ephy-bookmark.c
@@ -26,6 +26,8 @@
#include <string.h>
+#define ID_LEN 32
+
struct _EphyBookmark {
GObject parent_instance;
@@ -195,7 +197,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]