Returns now GNUNET_SYSERR
[oweals/gnunet.git] / src / util / crypto_aes.c
index 8fa935d9f7ce90a22fc5e95716e9d221b9546ba8..1ab4a7301a87526a655476e22cdb0f50c06af5e9 100644 (file)
@@ -72,13 +72,13 @@ GNUNET_CRYPTO_aes_check_session_key (const struct GNUNET_CRYPTO_AesSessionKey
  * @param result the output parameter in which to store the encrypted result
  * @returns the size of the encrypted block, -1 for errors
  */
-int
+ssize_t
 GNUNET_CRYPTO_aes_encrypt (const void *block,
-                           uint16_t len,
+                           size_t len,
                            const struct GNUNET_CRYPTO_AesSessionKey
-                           *sessionkey,
+                           * sessionkey,
                            const struct GNUNET_CRYPTO_AesInitializationVector
-                           *iv, void *result)
+                           * iv, void *result)
 {
   gcry_cipher_hd_t handle;
   int rc;
@@ -88,7 +88,7 @@ GNUNET_CRYPTO_aes_encrypt (const void *block,
              (sessionkey, GNUNET_CRYPTO_AES_KEY_LENGTH)))
     {
       GNUNET_break (0);
-      return GNUNET_SYSERR;
+      return -1;
     }
   GNUNET_assert (0 == gcry_cipher_open (&handle,
                                         GCRY_CIPHER_AES256,
@@ -115,12 +115,13 @@ GNUNET_CRYPTO_aes_encrypt (const void *block,
  * @param result address to store the result at
  * @return -1 on failure, size of decrypted block on success
  */
-int
-GNUNET_CRYPTO_aes_decrypt (const void *block, uint16_t size,
+ssize_t
+GNUNET_CRYPTO_aes_decrypt (const void *block,
+                           size_t size,
                            const struct GNUNET_CRYPTO_AesSessionKey
-                           *sessionkey, 
-                          const struct GNUNET_CRYPTO_AesInitializationVector
-                           *iv, void *result)
+                           * sessionkey,
+                           const struct GNUNET_CRYPTO_AesInitializationVector
+                           * iv, void *result)
 {
   gcry_cipher_hd_t handle;
   int rc;
@@ -130,7 +131,7 @@ GNUNET_CRYPTO_aes_decrypt (const void *block, uint16_t size,
              (sessionkey, GNUNET_CRYPTO_AES_KEY_LENGTH)))
     {
       GNUNET_break (0);
-      return GNUNET_SYSERR;
+      return -1;
     }
   GNUNET_assert (0 == gcry_cipher_open (&handle,
                                         GCRY_CIPHER_AES256,
@@ -147,4 +148,43 @@ GNUNET_CRYPTO_aes_decrypt (const void *block, uint16_t size,
   return size;
 }
 
+/**
+ * @brief Derive an IV
+ * @param iv initialization vector
+ * @param skey session key
+ * @param salt salt for the derivation
+ * @param salt_len size of the salt
+ * @param ... pairs of void * & size_t for context chunks, terminated by NULL
+ */
+void
+GNUNET_CRYPTO_aes_derive_iv (struct GNUNET_CRYPTO_AesInitializationVector *iv,
+    const struct GNUNET_CRYPTO_AesSessionKey *skey,
+    const void *salt, size_t salt_len,
+    ...)
+{
+  va_list argp;
+
+  va_start (argp, salt_len);
+  GNUNET_CRYPTO_aes_derive_iv_v (iv, skey, salt, salt_len, argp);
+  va_end (argp);
+}
+
+/**
+ * @brief Derive an IV
+ * @param iv initialization vector
+ * @param skey session key
+ * @param salt salt for the derivation
+ * @param salt_len size of the salt
+ * @param argp pairs of void * & size_t for context chunks, terminated by NULL
+ */
+void
+GNUNET_CRYPTO_aes_derive_iv_v (struct GNUNET_CRYPTO_AesInitializationVector *iv,
+    const struct GNUNET_CRYPTO_AesSessionKey *skey,
+    const void *salt, size_t salt_len,
+    va_list argp)
+{
+  GNUNET_CRYPTO_kdf_v (iv->iv, sizeof(iv->iv), salt, salt_len, skey->key,
+      sizeof(skey->key), argp);
+}
+
 /* end of crypto_aes.c */