[epiphany/wip/ephy-sync: 59/126] Crypto functions belong to the crypto module
- From: Gabriel - Cristian Ivascu <gabrielivascu src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [epiphany/wip/ephy-sync: 59/126] Crypto functions belong to the crypto module
- Date: Fri, 19 Aug 2016 17:36:31 +0000 (UTC)
commit 114bd910df6ae6b3c7c8b792c32cf7f17d66c36c
Author: Gabriel Ivascu <ivascu gabriel59 gmail com>
Date: Fri Jul 22 13:43:15 2016 +0300
Crypto functions belong to the crypto module
src/ephy-sync-crypto.c | 65 +++++++++++++++++++++++++++++++++++++++++------
src/ephy-sync-crypto.h | 5 +++
src/ephy-sync-service.c | 18 ++++++------
src/ephy-sync-utils.c | 51 ------------------------------------
src/ephy-sync-utils.h | 7 -----
5 files changed, 71 insertions(+), 75 deletions(-)
---
diff --git a/src/ephy-sync-crypto.c b/src/ephy-sync-crypto.c
index 83bb6bb..5c3818f 100644
--- a/src/ephy-sync-crypto.c
+++ b/src/ephy-sync-crypto.c
@@ -19,6 +19,7 @@
#include "ephy-sync-crypto.h"
#include "ephy-sync-utils.h"
+#include <glib/gstdio.h>
#include <libsoup/soup.h>
#include <nettle/hmac.h>
#include <nettle/sha2.h>
@@ -26,6 +27,8 @@
#define HAWK_VERSION 1
+static const gchar hex_digits[] = "0123456789abcdef";
+
EphySyncCryptoHawkOptions *
ephy_sync_crypto_hawk_options_new (gchar *app,
gchar *dlg,
@@ -252,6 +255,12 @@ ephy_sync_crypto_rsa_key_pair_free (EphySyncCryptoRSAKeyPair *keypair)
}
static gchar *
+kw (const gchar *name)
+{
+ return g_strconcat ("identity.mozilla.com/picl/v1/", name, NULL);
+}
+
+static gchar *
base64_urlsafe_strip (guint8 *data,
gsize data_length)
{
@@ -304,8 +313,8 @@ are_equal (guint8 *a,
gchar *b_hex;
gboolean retval;
- a_hex = ephy_sync_utils_encode_hex (a, 0);
- b_hex = ephy_sync_utils_encode_hex (b, 0);
+ a_hex = ephy_sync_crypto_encode_hex (a, 0);
+ b_hex = ephy_sync_crypto_encode_hex (b, 0);
retval = g_str_equal (a_hex, b_hex);
g_free (a_hex);
@@ -602,9 +611,9 @@ ephy_sync_crypto_process_key_fetch_token (const gchar *keyFetchToken)
gchar *info_kft;
gchar *info_keys;
- kft = ephy_sync_utils_decode_hex (keyFetchToken);
- info_kft = ephy_sync_utils_kw ("keyFetchToken");
- info_keys = ephy_sync_utils_kw ("account/keys");
+ kft = ephy_sync_crypto_decode_hex (keyFetchToken);
+ info_kft = kw ("keyFetchToken");
+ info_keys = kw ("account/keys");
out1 = g_malloc (3 * EPHY_SYNC_TOKEN_LENGTH);
out2 = g_malloc (3 * EPHY_SYNC_TOKEN_LENGTH);
@@ -653,8 +662,8 @@ ephy_sync_crypto_process_session_token (const gchar *sessionToken)
guint8 *requestKey;
gchar *info;
- st = ephy_sync_utils_decode_hex (sessionToken);
- info = ephy_sync_utils_kw ("sessionToken");
+ st = ephy_sync_crypto_decode_hex (sessionToken);
+ info = kw ("sessionToken");
out = g_malloc (3 * EPHY_SYNC_TOKEN_LENGTH);
hkdf (st, EPHY_SYNC_TOKEN_LENGTH,
@@ -694,7 +703,7 @@ ephy_sync_crypto_retrieve_sync_keys (const gchar *bundle,
guint8 *kB;
EphySyncCryptoSyncKeys *retval = NULL;
- bdl = ephy_sync_utils_decode_hex (bundle);
+ bdl = ephy_sync_crypto_decode_hex (bundle);
ciphertext = g_malloc (2 * EPHY_SYNC_TOKEN_LENGTH);
respMAC = g_malloc (EPHY_SYNC_TOKEN_LENGTH);
wrapKB = g_malloc (EPHY_SYNC_TOKEN_LENGTH);
@@ -918,3 +927,43 @@ out:
return assertion;
}
+
+gchar *
+ephy_sync_crypto_encode_hex (guint8 *data,
+ gsize data_length)
+{
+ gchar *retval;
+ gsize length;
+
+ length = data_length == 0 ? EPHY_SYNC_TOKEN_LENGTH : data_length;
+ retval = g_malloc (length * 2 + 1);
+
+ for (gsize i = 0; i < length; i++) {
+ guint8 byte = data[i];
+
+ retval[2 * i] = hex_digits[byte >> 4];
+ retval[2 * i + 1] = hex_digits[byte & 0xf];
+ }
+
+ retval[length * 2] = 0;
+
+ return retval;
+}
+
+guint8 *
+ephy_sync_crypto_decode_hex (const gchar *hex_string)
+{
+ guint8 *retval;
+ gsize hex_length;
+
+ hex_length = strlen (hex_string);
+ g_return_val_if_fail (hex_length % 2 == 0, NULL);
+
+ retval = g_malloc (hex_length / 2);
+
+ for (gsize i = 0, j = 0; i < hex_length; i += 2, j++) {
+ sscanf(hex_string + i, "%2hhx", retval + j);
+ }
+
+ return retval;
+}
\ No newline at end of file
diff --git a/src/ephy-sync-crypto.h b/src/ephy-sync-crypto.h
index a498415..f1ac01d 100644
--- a/src/ephy-sync-crypto.h
+++ b/src/ephy-sync-crypto.h
@@ -123,6 +123,11 @@ gchar *ephy_sync_crypto_create_assertion (const gcha
guint64 duration,
EphySyncCryptoRSAKeyPair *keypair);
+gchar *ephy_sync_crypto_encode_hex (guint8 *data,
+ gsize data_length);
+
+guint8 *ephy_sync_crypto_decode_hex (const gchar *hex_string);
+
G_END_DECLS
#endif
diff --git a/src/ephy-sync-service.c b/src/ephy-sync-service.c
index 2633c13..f36e164 100644
--- a/src/ephy-sync-service.c
+++ b/src/ephy-sync-service.c
@@ -308,7 +308,7 @@ query_token_server (EphySyncService *self,
g_return_val_if_fail (assertion != NULL, FALSE);
- kB = ephy_sync_utils_decode_hex (ephy_sync_service_get_token (self, EPHY_SYNC_TOKEN_KB));
+ kB = ephy_sync_crypto_decode_hex (ephy_sync_service_get_token (self, EPHY_SYNC_TOKEN_KB));
hashed_kB = g_compute_checksum_for_data (G_CHECKSUM_SHA256, kB, EPHY_SYNC_TOKEN_LENGTH);
client_state = g_strndup (hashed_kB, EPHY_SYNC_TOKEN_LENGTH);
authorization = g_strdup_printf ("BrowserID %s", assertion);
@@ -523,7 +523,7 @@ ephy_sync_service_destroy_session (EphySyncService *self,
url = g_strdup_printf ("%s%s%s", FXA_BASEURL, FXA_VERSION, endpoint);
processed_st = ephy_sync_crypto_process_session_token (sessionToken);
- tokenID = ephy_sync_utils_encode_hex (processed_st->tokenID, 0);
+ tokenID = ephy_sync_crypto_encode_hex (processed_st->tokenID, 0);
message = soup_message_new (SOUP_METHOD_POST, url);
soup_message_set_request (message, content_type,
@@ -568,9 +568,9 @@ ephy_sync_service_fetch_sync_keys (EphySyncService *self,
guint status_code;
gboolean retval = FALSE;
- unwrapKB = ephy_sync_utils_decode_hex (unwrapBKey);
+ unwrapKB = ephy_sync_crypto_decode_hex (unwrapBKey);
processed_kft = ephy_sync_crypto_process_key_fetch_token (keyFetchToken);
- tokenID = ephy_sync_utils_encode_hex (processed_kft->tokenID, 0);
+ tokenID = ephy_sync_crypto_encode_hex (processed_kft->tokenID, 0);
status_code = synchronous_hawk_get_request (self,
"account/keys",
tokenID,
@@ -599,13 +599,13 @@ ephy_sync_service_fetch_sync_keys (EphySyncService *self,
ephy_sync_service_set_and_store_tokens (self,
g_strdup (keyFetchToken), EPHY_SYNC_TOKEN_KEYFETCHTOKEN,
g_strdup (unwrapBKey), EPHY_SYNC_TOKEN_UNWRAPBKEY,
- ephy_sync_utils_encode_hex (sync_keys->kA, 0), EPHY_SYNC_TOKEN_KA,
- ephy_sync_utils_encode_hex (sync_keys->kB, 0), EPHY_SYNC_TOKEN_KB,
+ ephy_sync_crypto_encode_hex (sync_keys->kA, 0), EPHY_SYNC_TOKEN_KA,
+ ephy_sync_crypto_encode_hex (sync_keys->kB, 0), EPHY_SYNC_TOKEN_KB,
NULL);
retval = TRUE;
-LOG ("kA: %s", ephy_sync_utils_encode_hex (sync_keys->kA, 0));
-LOG ("kB: %s", ephy_sync_utils_encode_hex (sync_keys->kB, 0));
+LOG ("kA: %s", ephy_sync_crypto_encode_hex (sync_keys->kA, 0));
+LOG ("kB: %s", ephy_sync_crypto_encode_hex (sync_keys->kB, 0));
out:
ephy_sync_crypto_processed_kft_free (processed_kft);
@@ -641,7 +641,7 @@ ephy_sync_service_sign_certificate (EphySyncService *self)
g_return_val_if_fail (keypair != NULL, FALSE);
processed_st = ephy_sync_crypto_process_session_token (sessionToken);
- tokenID = ephy_sync_utils_encode_hex (processed_st->tokenID, 0);
+ tokenID = ephy_sync_crypto_encode_hex (processed_st->tokenID, 0);
n_str = mpz_get_str (NULL, 10, keypair->public.n);
e_str = mpz_get_str (NULL, 10, keypair->public.e);
diff --git a/src/ephy-sync-utils.c b/src/ephy-sync-utils.c
index 3eca35b..11e96c2 100644
--- a/src/ephy-sync-utils.c
+++ b/src/ephy-sync-utils.c
@@ -18,57 +18,6 @@
#include "ephy-sync-utils.h"
-#include <glib/gstdio.h>
-#include <string.h>
-
-static const gchar hex_digits[] = "0123456789abcdef";
-
-gchar *
-ephy_sync_utils_kw (const gchar *name)
-{
- return g_strconcat ("identity.mozilla.com/picl/v1/", name, NULL);
-}
-
-gchar *
-ephy_sync_utils_encode_hex (guint8 *data,
- gsize data_length)
-{
- gchar *retval;
- gsize length;
-
- length = data_length == 0 ? EPHY_SYNC_TOKEN_LENGTH : data_length;
- retval = g_malloc (length * 2 + 1);
-
- for (gsize i = 0; i < length; i++) {
- guint8 byte = data[i];
-
- retval[2 * i] = hex_digits[byte >> 4];
- retval[2 * i + 1] = hex_digits[byte & 0xf];
- }
-
- retval[length * 2] = 0;
-
- return retval;
-}
-
-guint8 *
-ephy_sync_utils_decode_hex (const gchar *hex_string)
-{
- guint8 *retval;
- gsize hex_length;
-
- hex_length = strlen (hex_string);
- g_return_val_if_fail (hex_length % 2 == 0, NULL);
-
- retval = g_malloc (hex_length / 2);
-
- for (gsize i = 0, j = 0; i < hex_length; i += 2, j++) {
- sscanf(hex_string + i, "%2hhx", retval + j);
- }
-
- return retval;
-}
-
const gchar *
ephy_sync_utils_token_name_from_type (EphySyncTokenType token_type)
{
diff --git a/src/ephy-sync-utils.h b/src/ephy-sync-utils.h
index 908f161..3b8cf51 100644
--- a/src/ephy-sync-utils.h
+++ b/src/ephy-sync-utils.h
@@ -34,13 +34,6 @@ typedef enum {
EPHY_SYNC_TOKEN_KB,
} EphySyncTokenType;
-gchar *ephy_sync_utils_kw (const gchar *name);
-
-gchar *ephy_sync_utils_encode_hex (guint8 *data,
- gsize data_length);
-
-guint8 *ephy_sync_utils_decode_hex (const gchar *hex_string);
-
const gchar *ephy_sync_utils_token_name_from_type (EphySyncTokenType token_type);
gchar *ephy_sync_utils_build_json_string (const gchar *first_key,
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]