summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkokke <spam@rowdy.dk>2017-12-01 01:00:07 +0100
committerGitHub <noreply@github.com>2017-12-01 01:00:07 +0100
commit4b91b70c3522408be976fdda361a740fadd36606 (patch)
tree134c6fffd2dc4b9600197c14eb6cbb4ee44e429a
parentUpdate README.md (diff)
downloadtiny-AES-c-4b91b70c3522408be976fdda361a740fadd36606.tar
tiny-AES-c-4b91b70c3522408be976fdda361a740fadd36606.tar.gz
tiny-AES-c-4b91b70c3522408be976fdda361a740fadd36606.tar.bz2
tiny-AES-c-4b91b70c3522408be976fdda361a740fadd36606.tar.lz
tiny-AES-c-4b91b70c3522408be976fdda361a740fadd36606.tar.xz
tiny-AES-c-4b91b70c3522408be976fdda361a740fadd36606.tar.zst
tiny-AES-c-4b91b70c3522408be976fdda361a740fadd36606.zip
-rw-r--r--aes.c41
1 files changed, 40 insertions, 1 deletions
diff --git a/aes.c b/aes.c
index 0ff8367..4d240b0 100644
--- a/aes.c
+++ b/aes.c
@@ -1,6 +1,6 @@
/*
-This is an implementation of the AES algorithm, specifically ECB and CBC mode.
+This is an implementation of the AES algorithm, specifically ECB, CTR and CBC mode.
Block size can be chosen in aes.h - available choices are AES128, AES192, AES256.
The implementation is verified against the test vectors in:
@@ -595,3 +595,42 @@ void AES_CBC_decrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, co
}
#endif // #if defined(CBC) && (CBC == 1)
+
+
+
+#if defined(CTR) && (CTR == 1)
+
+void AES_CTR_xcrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, const uint8_t* key, const uint8_t* iv)
+{
+ uint8_t buffer[BLOCKLEN], counter[BLOCKLEN];
+
+ memcpy(counter, iv, BLOCKLEN);
+ Key = key;
+ KeyExpansion();
+
+ int i, j;
+ for (i = 0; i < length; ++i)
+ {
+ if ((i & 0x0F) == 0)
+ {
+ memcpy(buffer, counter, BLOCKLEN);
+ state = (state_t *) buffer;
+ Cipher();
+
+ for (j = (BLOCKLEN - 1); j >= 0; --j)
+ {
+ counter[j] += 1;
+
+ if (counter[j] != 0)
+ {
+ break;
+ }
+ }
+ }
+
+ output[i] = (input[i]) ^ (buffer[i & 0x0F]);
+ }
+}
+
+#endif // #if defined(CTR) && (CTR == 1)
+