Skip to contentSkip to conversation

cipher: add AES-CTR

PATCH In review James Prestwood 2 participants 3 messages Apr 16, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 cipher: add AES-CTR

The kernel already supports this with ctr(aes), so adding a new cipher type was all that was needed.

---
 ell/cipher.c | 2 ++
 ell/cipher.h | 1 +
 2 files changed, 3 insertions(+)
ell/cipher.c +2 -0
index 977ebc8..7b909df 100644
@@ -148,6 +148,8 @@ static const char *cipher_type_to_name(enum l_cipher_type type)
 		return "ecb(aes)";
 	case L_CIPHER_AES_CBC:
 		return "cbc(aes)";
+	case L_CIPHER_AES_CTR:
+		return "ctr(aes)";
 	case L_CIPHER_ARC4:
 		return "ecb(arc4)";
 	case L_CIPHER_DES:
ell/cipher.h +1 -0
index 946b9ad..84f2988 100644
@@ -32,6 +32,7 @@ struct l_cipher;
 enum l_cipher_type {
 	L_CIPHER_AES = 0,
 	L_CIPHER_AES_CBC,
+	L_CIPHER_AES_CTR,
 	L_CIPHER_ARC4,
 	L_CIPHER_DES,
 	L_CIPHER_DES_CBC,
Signature · 1 line
2.17.1
#1 lore raw

PATCH 2/2 unit: add test for AES-CTR

---
 unit/test-cipher.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
unit/test-cipher.c +30 -0
index fe9ae72..bd8c05d 100644
@@ -71,6 +71,33 @@ static void test_aes(const void *data)
 	l_cipher_free(cipher);
 }
 
+static void test_aes_ctr(const void *data)
+{
+	struct l_cipher *cipher;
+	uint8_t iv[8] = { 0 };
+	char buf[256];
+	int r;
+
+	cipher = l_cipher_new(L_CIPHER_AES_CTR, KEY_STR, KEY_LEN);
+	assert(cipher);
+
+	l_cipher_set_iv(cipher, iv, sizeof(iv));
+
+	memcpy(buf, FIXED_STR, FIXED_LEN);
+
+	l_cipher_encrypt(cipher, buf, buf, FIXED_LEN);
+
+	r = memcmp(buf, FIXED_STR, FIXED_LEN);
+	assert(r);
+
+	l_cipher_decrypt(cipher, buf, buf, FIXED_LEN);
+
+	r = memcmp(buf, FIXED_STR, FIXED_LEN);
+	assert(!r);
+
+	l_cipher_free(cipher);
+}
+
 static void test_arc4(const void *data)
 {
 	struct l_cipher *cipher;
@@ -335,6 +362,9 @@ int main(int argc, char *argv[])
 	if (l_cipher_is_supported(L_CIPHER_AES))
 		l_test_add("aes", test_aes, NULL);
 
+	if (l_cipher_is_supported(L_CIPHER_AES_CTR))
+		l_test_add("aes_ctr", test_aes_ctr, NULL);
+
 	if (l_cipher_is_supported(L_CIPHER_ARC4))
 		l_test_add("arc4", test_arc4, NULL);
 
Signature · 1 line
2.17.1
#2 lore raw
In reply to James Prestwood

Hi James,

Quoted 7 lines · James Prestwood

The kernel already supports this with ctr(aes), so adding a new cipher type was all that was needed.

---
  ell/cipher.c | 2 ++
  ell/cipher.h | 1 +
  2 files changed, 3 insertions(+)

Both applied, thanks.

Regards,
-Denis