Skip to contentSkip to conversation

group common used functions in software AES modules

RFC In review Sebastian Siewior 2 participants 5 messages Oct 22, 2007 – Oct 24, 2007

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

Hello Herbert,

this little patch series moves the the key generation routine and the four tables which are used encrypt/decrypt function into a separate module. Currently I converted the generic AES and the x86_64 variant. My 32bit asm is rusted so the i586 assembly has to wait for a while :)

Sebastian

#1 lore raw

RFC 1/3 AES: group common functions

From: Sebastian Siewior <[email protected]>

There are three AES softwore modules in the kernel: One C version and two arch specific assembly versions. The key expansion routine as well as the the four lookup tables which are used during encryption and/or decryption are the same.
This patch simply creates an independent module which shares the data structures across the modules.

---
 crypto/Kconfig       |    3 +
 crypto/Makefile      |    1 +
 crypto/aes_common.c  |  277 ++++++++++++++++++++++++++++++++++++++++++++++++++
 include/crypto/aes.h |   17 +++
 4 files changed, 298 insertions(+), 0 deletions(-)
 create mode 100644 crypto/aes_common.c
crypto/Kconfig +3 -0
index 1f32071..26130d0 100644
@@ -325,6 +325,9 @@ config CRYPTO_AES
 
 	  See <http://csrc.nist.gov/CryptoToolkit/aes/> for more information.
 
+config CRYPTO_AES_COMMON
+	tristate
+
 config CRYPTO_AES_586
 	tristate "AES cipher algorithms (i586)"
 	depends on (X86 || UML_X86) && !64BIT
crypto/Makefile +1 -0
index 1f87db2..59b6a7e 100644
@@ -41,6 +41,7 @@ obj-$(CONFIG_CRYPTO_TWOFISH) += twofish.o
 obj-$(CONFIG_CRYPTO_TWOFISH_COMMON) += twofish_common.o
 obj-$(CONFIG_CRYPTO_SERPENT) += serpent.o
 obj-$(CONFIG_CRYPTO_AES) += aes_generic.o
+obj-$(CONFIG_CRYPTO_AES_COMMON) += aes_common.o
 obj-$(CONFIG_CRYPTO_CAMELLIA) += camellia.o
 obj-$(CONFIG_CRYPTO_CAST5) += cast5.o
 obj-$(CONFIG_CRYPTO_CAST6) += cast6.o
crypto/aes_common.c new file +277 -0

277 lines not on this page · raw message

include/crypto/aes.h +17 -0
index 9ff842f..ab7c7c8 100644
@@ -5,6 +5,9 @@
 #ifndef _CRYPTO_AES_H
 #define _CRYPTO_AES_H
 
+#include <linux/types.h>
+#include <linux/crypto.h>
+
 #define AES_MIN_KEY_SIZE	16
 #define AES_MAX_KEY_SIZE	32
 #define AES_KEYSIZE_128		16
@@ -12,4 +15,18 @@
 #define AES_KEYSIZE_256		32
 #define AES_BLOCK_SIZE		16
 
+struct crypto_aes_ctx {
+	u32 key_length;
+	u32 key_enc[60];
+	u32 key_dec[60];
+};
+
+extern u32 crypto_ft_tab[4][256];
+extern u32 crypto_it_tab[4][256];
+extern u32 crypto_fl_tab[4][256];
+extern u32 crypto_il_tab[4][256];
+
+int crypto_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
+		unsigned int key_len);
+
 #endif
Signature · 1 line
1.5.3.4
maintainer
#2 lore raw

Re: [RFC 1/3] AES: group common functions

Quoted 10 lines · Sebastian Siewior

From: Sebastian Siewior <[email protected]>

There are three AES softwore modules in the kernel: One C version and two arch specific assembly versions. The key expansion routine as well as the the four lookup tables which are used during encryption and/or decryption are the same.
This patch simply creates an independent module which shares the data structures across the modules.

Thanks, the patch looks fine.

However, instead of creating yet another module like twofish, how about just making the assembly versions depend on the generic version for these functions?

In fact, that's how we should've handled twofish as well.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#3 lore raw

RFC 2/3 generic AES use new common module

In reply to Sebastian Siewior

From: Sebastian Siewior <[email protected]>

Share the tables and set_key function.

---
 crypto/Kconfig       |    1 +
 crypto/aes_generic.c |  281 ++++++--------------------------------------------
 2 files changed, 35 insertions(+), 247 deletions(-)
crypto/Kconfig +1 -0
index 26130d0..17b11b8 100644
@@ -308,6 +308,7 @@ config CRYPTO_SERPENT
 config CRYPTO_AES
 	tristate "AES cipher algorithms"
 	select CRYPTO_ALGAPI
+	select CRYPTO_AES_COMMON
 	help
 	  AES cipher algorithms (FIPS-197). AES uses the Rijndael 
 	  algorithm.
crypto/aes_generic.c +34 -247

356 lines not on this page · raw message

Signature · 1 line
1.5.3.4
#4 lore raw

RFC 3/3 x86_64 asm AES use new common module

In reply to Sebastian Siewior

From: Sebastian Siewior <[email protected]>

Share the tables and set_key function.

---
 arch/x86/crypto/aes-x86_64-asm_64.S |   68 ++++----
 arch/x86/crypto/aes_64.c            |  282 +----------------------------------
 crypto/Kconfig                      |    1 +
 3 files changed, 38 insertions(+), 313 deletions(-)
arch/x86/crypto/aes-x86_64-asm_64.S +34 -34
index 26b40de..a120f52 100644
@@ -8,10 +8,10 @@
  * including this sentence is retained in full.
  */
 
-.extern aes_ft_tab
-.extern aes_it_tab
-.extern aes_fl_tab
-.extern aes_il_tab
+.extern crypto_ft_tab
+.extern crypto_it_tab
+.extern crypto_fl_tab
+.extern crypto_il_tab
 
 .text
 
@@ -56,13 +56,13 @@
 	.align	8;			\
 FUNC:	movq	r1,r2;			\
 	movq	r3,r4;			\
-	leaq	BASE+KEY+52(r8),r9;	\
+	leaq	BASE+KEY+48+4(r8),r9;	\
 	movq	r10,r11;		\
 	movl	(r7),r5 ## E;		\
 	movl	4(r7),r1 ## E;		\
 	movl	8(r7),r6 ## E;		\
 	movl	12(r7),r7 ## E;		\
-	movl	BASE(r8),r10 ## E;	\
+	movl	BASE+0(r8),r10 ## E;	\
 	xorl	-48(r9),r5 ## E;	\
 	xorl	-44(r9),r1 ## E;	\
 	xorl	-40(r9),r6 ## E;	\
@@ -154,37 +154,37 @@ FUNC:	movq	r1,r2;			\
 /* void aes_enc_blk(stuct crypto_tfm *tfm, u8 *out, const u8 *in) */
 
 	entry(aes_enc_blk,0,enc128,enc192)
-	encrypt_round(aes_ft_tab,-96)
-	encrypt_round(aes_ft_tab,-80)
-enc192:	encrypt_round(aes_ft_tab,-64)
-	encrypt_round(aes_ft_tab,-48)
-enc128:	encrypt_round(aes_ft_tab,-32)
-	encrypt_round(aes_ft_tab,-16)
-	encrypt_round(aes_ft_tab,  0)
-	encrypt_round(aes_ft_tab, 16)
-	encrypt_round(aes_ft_tab, 32)
-	encrypt_round(aes_ft_tab, 48)
-	encrypt_round(aes_ft_tab, 64)
-	encrypt_round(aes_ft_tab, 80)
-	encrypt_round(aes_ft_tab, 96)
-	encrypt_final(aes_fl_tab,112)
+	encrypt_round(crypto_ft_tab,-96)
+	encrypt_round(crypto_ft_tab,-80)
+enc192:	encrypt_round(crypto_ft_tab,-64)
+	encrypt_round(crypto_ft_tab,-48)
+enc128:	encrypt_round(crypto_ft_tab,-32)
+	encrypt_round(crypto_ft_tab,-16)
+	encrypt_round(crypto_ft_tab,  0)
+	encrypt_round(crypto_ft_tab, 16)
+	encrypt_round(crypto_ft_tab, 32)
+	encrypt_round(crypto_ft_tab, 48)
+	encrypt_round(crypto_ft_tab, 64)
+	encrypt_round(crypto_ft_tab, 80)
+	encrypt_round(crypto_ft_tab, 96)
+	encrypt_final(crypto_fl_tab,112)
 	return
 
 /* void aes_dec_blk(struct crypto_tfm *tfm, u8 *out, const u8 *in) */
 
 	entry(aes_dec_blk,240,dec128,dec192)
-	decrypt_round(aes_it_tab,-96)
-	decrypt_round(aes_it_tab,-80)
-dec192:	decrypt_round(aes_it_tab,-64)
-	decrypt_round(aes_it_tab,-48)
-dec128:	decrypt_round(aes_it_tab,-32)
-	decrypt_round(aes_it_tab,-16)
-	decrypt_round(aes_it_tab,  0)
-	decrypt_round(aes_it_tab, 16)
-	decrypt_round(aes_it_tab, 32)
-	decrypt_round(aes_it_tab, 48)
-	decrypt_round(aes_it_tab, 64)
-	decrypt_round(aes_it_tab, 80)
-	decrypt_round(aes_it_tab, 96)
-	decrypt_final(aes_il_tab,112)
+	decrypt_round(crypto_it_tab,-96)
+	decrypt_round(crypto_it_tab,-80)
+dec192:	decrypt_round(crypto_it_tab,-64)
+	decrypt_round(crypto_it_tab,-48)
+dec128:	decrypt_round(crypto_it_tab,-32)
+	decrypt_round(crypto_it_tab,-16)
+	decrypt_round(crypto_it_tab,  0)
+	decrypt_round(crypto_it_tab, 16)
+	decrypt_round(crypto_it_tab, 32)
+	decrypt_round(crypto_it_tab, 48)
+	decrypt_round(crypto_it_tab, 64)
+	decrypt_round(crypto_it_tab, 80)
+	decrypt_round(crypto_it_tab, 96)
+	decrypt_final(crypto_il_tab,112)
 	return
arch/x86/crypto/aes_64.c +3 -279

308 lines not on this page · raw message

crypto/Kconfig +1 -0
index 17b11b8..0e17ddb 100644
@@ -354,6 +354,7 @@ config CRYPTO_AES_X86_64
 	tristate "AES cipher algorithms (x86_64)"
 	depends on (X86 || UML_X86) && 64BIT
 	select CRYPTO_ALGAPI
+	select CRYPTO_AES_COMMON
 	help
 	  AES cipher algorithms (FIPS-197). AES uses the Rijndael 
 	  algorithm.
Signature · 1 line
1.5.3.4