From 433df6f53252a5f4e8b97f1d905ba37928145a96 Mon Sep 17 00:00:00 2001
From: Egor Tensin <Egor.Tensin@gmail.com>
Date: Fri, 19 Jun 2015 01:31:25 +0300
Subject: add CTR mode to "boxes"

---
 src/box.c     | 17 ++++++++++++++---
 src/box_aes.c | 11 +++++++++++
 2 files changed, 25 insertions(+), 3 deletions(-)

(limited to 'src')

diff --git a/src/box.c b/src/box.c
index e48c5d7..c74100c 100644
--- a/src/box.c
+++ b/src/box.c
@@ -132,7 +132,19 @@ static AesNI_StatusCode aesni_box_encrypt_ctr(
     AesNI_BoxBlock* output,
     AesNI_ErrorDetails* err_details)
 {
-    return aesni_error_not_implemented(err_details, "box encryption in CTR mode");
+    AesNI_StatusCode status = box->algorithm_iface->encrypt(
+        &box->iv,
+        &box->encrypt_params,
+        output,
+        err_details);
+    if (aesni_is_error(status))
+        return status;
+
+    status = box->algorithm_iface->xor_block(output, input, err_details);
+    if (aesni_is_error(status))
+        return status;
+
+    return box->algorithm_iface->inc_counter(&box->iv, err_details);
 }
 
 typedef AesNI_StatusCode (*AesNI_BoxEncryptMode)(
@@ -219,7 +231,6 @@ static AesNI_StatusCode aesni_box_decrypt_cfb(
         return status;
 
     box->iv = *input;
-
     return status;
 }
 
@@ -255,7 +266,7 @@ static AesNI_StatusCode aesni_box_decrypt_ctr(
     AesNI_BoxBlock* output,
     AesNI_ErrorDetails* err_details)
 {
-    return aesni_error_not_implemented(err_details, "box decryption in CTR mode");
+    return aesni_box_encrypt_ctr(box, input, output, err_details);
 }
 
 typedef AesNI_BoxEncryptMode AesNI_BoxDecryptMode;
diff --git a/src/box_aes.c b/src/box_aes.c
index fa8bece..aa2aa21 100644
--- a/src/box_aes.c
+++ b/src/box_aes.c
@@ -64,6 +64,14 @@ static AesNI_StatusCode aesni_box_xor_block_aes(
     return AESNI_SUCCESS;
 }
 
+static AesNI_StatusCode aesni_box_inc_counter_aes(
+    AesNI_BoxBlock* ctr,
+    AesNI_ErrorDetails* err_details)
+{
+    ctr->aes_block = aesni_aes_inc_counter(ctr->aes_block);
+    return AESNI_SUCCESS;
+}
+
 static AesNI_StatusCode aesni_box_encrypt_aes128(
     const AesNI_BoxBlock* input,
     const AesNI_BoxEncryptionParams* params,
@@ -142,6 +150,7 @@ AesNI_BoxAlgorithmInterface aesni_box_aes128_iface =
     &aesni_box_encrypt_aes128,
     &aesni_box_decrypt_aes128,
     &aesni_box_xor_block_aes,
+    &aesni_box_inc_counter_aes,
 };
 
 AesNI_BoxAlgorithmInterface aesni_box_aes192_iface =
@@ -150,6 +159,7 @@ AesNI_BoxAlgorithmInterface aesni_box_aes192_iface =
     &aesni_box_encrypt_aes192,
     &aesni_box_decrypt_aes192,
     &aesni_box_xor_block_aes,
+    &aesni_box_inc_counter_aes,
 };
 
 AesNI_BoxAlgorithmInterface aesni_box_aes256_iface =
@@ -158,4 +168,5 @@ AesNI_BoxAlgorithmInterface aesni_box_aes256_iface =
     &aesni_box_encrypt_aes256,
     &aesni_box_decrypt_aes256,
     &aesni_box_xor_block_aes,
+    &aesni_box_inc_counter_aes,
 };
-- 
cgit v1.2.3