[epiphany/wip/sync: 47/86] sync-crypto: Add AES256 CBC mode encrypt/decrypt functions



commit d6e274b5bad7c052acfab0f298eb24c32d18eb60
Author: Gabriel Ivascu <ivascu gabriel59 gmail com>
Date:   Thu Mar 9 01:11:31 2017 +0200

    sync-crypto: Add AES256 CBC mode encrypt/decrypt functions

 src/sync/ephy-sync-crypto.c |  104 +++++++++++++++++++++++++++++++++++++++++++
 src/sync/ephy-sync-crypto.h |    8 +++
 2 files changed, 112 insertions(+), 0 deletions(-)
---
diff --git a/src/sync/ephy-sync-crypto.c b/src/sync/ephy-sync-crypto.c
index d8ba368..28fbd92 100644
--- a/src/sync/ephy-sync-crypto.c
+++ b/src/sync/ephy-sync-crypto.c
@@ -26,6 +26,7 @@
 #include <glib/gstdio.h>
 #include <inttypes.h>
 #include <libsoup/soup.h>
+#include <nettle/cbc.h>
 #include <nettle/aes.h>
 #include <string.h>
 
@@ -944,6 +945,109 @@ ephy_sync_crypto_aes_256 (SyncCryptoAES256Mode  mode,
   return out;
 }
 
+static guint8 *
+ephy_sync_crypto_pad (const char *text,
+                      gsize       block_len,
+                      gsize      *out_len)
+{
+  guint8 *out;
+  gsize text_len = strlen (text);
+
+  g_assert (text);
+  g_assert (out_len);
+
+  if (text_len % block_len == 0)
+    *out_len = text_len;
+  else
+    *out_len = text_len + block_len - text_len % block_len;
+
+  out = g_malloc (*out_len);
+
+  if (text_len % block_len != 0)
+    memset (out, block_len - text_len % block_len, *out_len);
+
+  memcpy (out, text, text_len);
+
+  return out;
+}
+
+guint8 *
+ephy_sync_crypto_aes_256_encrypt (const char   *text,
+                                  const guint8 *key,
+                                  const guint8 *iv,
+                                  gsize        *out_len)
+{
+  guint8 *padded;
+  guint8 *encrypted;
+  gsize padded_len;
+  struct CBC_CTX(struct aes256_ctx, AES_BLOCK_SIZE) ctx;
+
+  g_return_val_if_fail (text, NULL);
+  g_return_val_if_fail (key, NULL);
+  g_return_val_if_fail (iv, NULL);
+  g_return_val_if_fail (out_len, NULL);
+
+  padded = ephy_sync_crypto_pad (text, AES_BLOCK_SIZE, &padded_len);
+  encrypted = g_malloc (padded_len);
+
+  aes256_set_encrypt_key(&ctx.ctx, key);
+  CBC_SET_IV(&ctx, iv);
+  CBC_ENCRYPT(&ctx, aes256_encrypt, padded_len, encrypted, padded);
+
+  *out_len = padded_len;
+  g_free (padded);
+
+  return encrypted;
+}
+
+static char *
+ephy_sync_crypto_unpad (const guint8 *data,
+                        gsize         data_len,
+                        gsize         block_len)
+{
+  char *out;
+  gsize out_len;
+  gsize padding = data[data_len - 1];
+
+  g_assert (data);
+
+  if (padding >= 1 && padding <= block_len - 1)
+    out_len = data_len - padding;
+  else
+    out_len = data_len;
+
+  out = g_malloc0 (out_len + 1);
+  memcpy (out, data, out_len);
+
+  return out;
+}
+
+char *
+ephy_sync_crypto_aes_256_decrypt (const guint8 *data,
+                                  gsize         data_len,
+                                  const guint8 *key,
+                                  const guint8 *iv)
+{
+  guint8 *decrypted;
+  char *unpadded;
+  struct CBC_CTX(struct aes256_ctx, AES_BLOCK_SIZE) ctx;
+
+  g_return_val_if_fail (data, NULL);
+  g_return_val_if_fail (key, NULL);
+  g_return_val_if_fail (iv, NULL);
+
+  decrypted = g_malloc (data_len);
+
+  aes256_set_decrypt_key (&ctx.ctx, key);
+  CBC_SET_IV (&ctx, iv);
+  CBC_DECRYPT (&ctx, aes256_decrypt, data_len, decrypted, data);
+
+  unpadded = ephy_sync_crypto_unpad (decrypted, data_len, AES_BLOCK_SIZE);
+  g_free (decrypted);
+
+  return unpadded;
+}
+
 char *
 ephy_sync_crypto_encode_hex (guint8 *data,
                              gsize   data_len)
diff --git a/src/sync/ephy-sync-crypto.h b/src/sync/ephy-sync-crypto.h
index ce3935c..8131ccb 100644
--- a/src/sync/ephy-sync-crypto.h
+++ b/src/sync/ephy-sync-crypto.h
@@ -119,6 +119,14 @@ guint8                 *ephy_sync_crypto_aes_256                  (SyncCryptoAES
                                                                    const guint8           *data,
                                                                    gsize                   data_len,
                                                                    gsize                  *out_len);
+guint8                 *ephy_sync_crypto_aes_256_encrypt          (const char             *text,
+                                                                   const guint8           *key,
+                                                                   const guint8           *iv,
+                                                                   gsize                  *out_len);
+char                   *ephy_sync_crypto_aes_256_decrypt          (const guint8           *data,
+                                                                   gsize                   data_len,
+                                                                   const guint8           *key,
+                                                                   const guint8           *iv);
 char                   *ephy_sync_crypto_encode_hex               (guint8                 *data,
                                                                    gsize                   data_len);
 guint8                 *ephy_sync_crypto_decode_hex               (const char             *hex);


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