Skip to contentSkip to conversation

merge AES setkey function for i586

PATCH In review Sebastian Siewior 2 participants 11 messages Nov 3, 2007 – Nov 29, 2007

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

cover merge AES setkey function for i586

Hello Herbert,

this patch series unifies the AES setkey function for the i586 asm code. Now the glue code is equal for aes_{32|64} and can be shared as well.

Sebastian

#1 lore raw

PATCH 1/2 AES i586, remove setkey

The setkey() function can be shared with the generic algorithm.

---
 arch/x86/crypto/aes-i586-asm_32.S |   89 ++++----
 arch/x86/crypto/aes_32.c          |  461 +------------------------------------
 crypto/Kconfig                    |    1 +
 3 files changed, 47 insertions(+), 504 deletions(-)
arch/x86/crypto/aes-i586-asm_32.S +43 -46
index f942f0c..1093bed 100644
@@ -46,9 +46,9 @@
 #define in_blk 16
 
 /* offsets in crypto_tfm structure */
-#define ekey (crypto_tfm_ctx_offset + 0)
-#define nrnd (crypto_tfm_ctx_offset + 256)
-#define dkey (crypto_tfm_ctx_offset + 260)
+#define klen (crypto_tfm_ctx_offset + 0)
+#define ekey (crypto_tfm_ctx_offset + 4)
+#define dkey (crypto_tfm_ctx_offset + 244)
 
 // register mapping for encrypt and decrypt subroutines
 
@@ -221,8 +221,8 @@
 
 .global  aes_enc_blk
 
-.extern  ft_tab
-.extern  fl_tab
+.extern  crypto_ft_tab
+.extern  crypto_fl_tab
 
 .align 4
 
@@ -236,7 +236,7 @@ aes_enc_blk:
 1:	push    %ebx
 	mov     in_blk+4(%esp),%r2
 	push    %esi
-	mov     nrnd(%ebp),%r3   // number of rounds
+	mov     klen(%ebp),%r3   // key size
 	push    %edi
 #if ekey != 0
 	lea     ekey(%ebp),%ebp  // key pointer
@@ -255,26 +255,26 @@ aes_enc_blk:
 
 	sub     $8,%esp		// space for register saves on stack
 	add     $16,%ebp	// increment to next round key
-	cmp     $12,%r3
+	cmp     $24,%r3
 	jb      4f		// 10 rounds for 128-bit key
 	lea     32(%ebp),%ebp
 	je      3f		// 12 rounds for 192-bit key
 	lea     32(%ebp),%ebp
 
-2:	fwd_rnd1( -64(%ebp) ,ft_tab)	// 14 rounds for 256-bit key
-	fwd_rnd2( -48(%ebp) ,ft_tab)
-3:	fwd_rnd1( -32(%ebp) ,ft_tab)	// 12 rounds for 192-bit key
-	fwd_rnd2( -16(%ebp) ,ft_tab)
-4:	fwd_rnd1(    (%ebp) ,ft_tab)	// 10 rounds for 128-bit key
-	fwd_rnd2( +16(%ebp) ,ft_tab)
-	fwd_rnd1( +32(%ebp) ,ft_tab)
-	fwd_rnd2( +48(%ebp) ,ft_tab)
-	fwd_rnd1( +64(%ebp) ,ft_tab)
-	fwd_rnd2( +80(%ebp) ,ft_tab)
-	fwd_rnd1( +96(%ebp) ,ft_tab)
-	fwd_rnd2(+112(%ebp) ,ft_tab)
-	fwd_rnd1(+128(%ebp) ,ft_tab)
-	fwd_rnd2(+144(%ebp) ,fl_tab)	// last round uses a different table
+2:	fwd_rnd1( -64(%ebp), crypto_ft_tab)	// 14 rounds for 256-bit key
+	fwd_rnd2( -48(%ebp), crypto_ft_tab)
+3:	fwd_rnd1( -32(%ebp), crypto_ft_tab)	// 12 rounds for 192-bit key
+	fwd_rnd2( -16(%ebp), crypto_ft_tab)
+4:	fwd_rnd1(    (%ebp), crypto_ft_tab)	// 10 rounds for 128-bit key
+	fwd_rnd2( +16(%ebp), crypto_ft_tab)
+	fwd_rnd1( +32(%ebp), crypto_ft_tab)
+	fwd_rnd2( +48(%ebp), crypto_ft_tab)
+	fwd_rnd1( +64(%ebp), crypto_ft_tab)
+	fwd_rnd2( +80(%ebp), crypto_ft_tab)
+	fwd_rnd1( +96(%ebp), crypto_ft_tab)
+	fwd_rnd2(+112(%ebp), crypto_ft_tab)
+	fwd_rnd1(+128(%ebp), crypto_ft_tab)
+	fwd_rnd2(+144(%ebp), crypto_fl_tab)	// last round uses a different table
 
 // move final values to the output array.  CAUTION: the 
 // order of these assigns rely on the register mappings
@@ -297,8 +297,8 @@ aes_enc_blk:
 
 .global  aes_dec_blk
 
-.extern  it_tab
-.extern  il_tab
+.extern  crypto_it_tab
+.extern  crypto_il_tab
 
 .align 4
 
@@ -312,14 +312,11 @@ aes_dec_blk:
 1:	push    %ebx
 	mov     in_blk+4(%esp),%r2
 	push    %esi
-	mov     nrnd(%ebp),%r3   // number of rounds
+	mov     klen(%ebp),%r3   // key size
 	push    %edi
 #if dkey != 0
 	lea     dkey(%ebp),%ebp  // key pointer
 #endif
-	mov     %r3,%r0
-	shl     $4,%r0
-	add     %r0,%ebp
 	
 // input four columns and xor in first round key
 
@@ -333,27 +330,27 @@ aes_dec_blk:
 	xor     12(%ebp),%r5
 
 	sub     $8,%esp		// space for register saves on stack
-	sub     $16,%ebp	// increment to next round key
-	cmp     $12,%r3
+	add     $16,%ebp	// increment to next round key
+	cmp     $24,%r3
 	jb      4f		// 10 rounds for 128-bit key
-	lea     -32(%ebp),%ebp
+	lea     32(%ebp),%ebp
 	je      3f		// 12 rounds for 192-bit key
-	lea     -32(%ebp),%ebp
-
-2:	inv_rnd1( +64(%ebp), it_tab)	// 14 rounds for 256-bit key
-	inv_rnd2( +48(%ebp), it_tab)
-3:	inv_rnd1( +32(%ebp), it_tab)	// 12 rounds for 192-bit key
-	inv_rnd2( +16(%ebp), it_tab)
-4:	inv_rnd1(    (%ebp), it_tab)	// 10 rounds for 128-bit key
-	inv_rnd2( -16(%ebp), it_tab)
-	inv_rnd1( -32(%ebp), it_tab)
-	inv_rnd2( -48(%ebp), it_tab)
-	inv_rnd1( -64(%ebp), it_tab)
-	inv_rnd2( -80(%ebp), it_tab)
-	inv_rnd1( -96(%ebp), it_tab)
-	inv_rnd2(-112(%ebp), it_tab)
-	inv_rnd1(-128(%ebp), it_tab)
-	inv_rnd2(-144(%ebp), il_tab)	// last round uses a different table
+	lea     32(%ebp),%ebp
+
+2:	inv_rnd1( -64(%ebp), crypto_it_tab)	// 14 rounds for 256-bit key
+	inv_rnd2( -48(%ebp), crypto_it_tab)
+3:	inv_rnd1( -32(%ebp), crypto_it_tab)	// 12 rounds for 192-bit key
+	inv_rnd2( -16(%ebp), crypto_it_tab)
+4:	inv_rnd1(    (%ebp), crypto_it_tab)	// 10 rounds for 128-bit key
+	inv_rnd2( +16(%ebp), crypto_it_tab)
+	inv_rnd1( +32(%ebp), crypto_it_tab)
+	inv_rnd2( +48(%ebp), crypto_it_tab)
+	inv_rnd1( +64(%ebp), crypto_it_tab)
+	inv_rnd2( +80(%ebp), crypto_it_tab)
+	inv_rnd1( +96(%ebp), crypto_it_tab)
+	inv_rnd2(+112(%ebp), crypto_it_tab)
+	inv_rnd1(+128(%ebp), crypto_it_tab)
+	inv_rnd2(+144(%ebp), crypto_il_tab)	// last round uses a different table
 
 // move final values to the output array.  CAUTION: the 
 // order of these assigns rely on the register mappings
arch/x86/crypto/aes_32.c +3 -458

492 lines not on this page · raw message

crypto/Kconfig +1 -0
index 3f0bc0e..12b4542 100644
@@ -329,6 +329,7 @@ config CRYPTO_AES_586
 	tristate "AES cipher algorithms (i586)"
 	depends on (X86 || UML_X86) && !64BIT
 	select CRYPTO_ALGAPI
+	select CRYPTO_AES
 	help
 	  AES cipher algorithms (FIPS-197). AES uses the Rijndael 
 	  algorithm.
Signature · 1 line
1.5.3.4
maintainer
#2 lore raw

Re: [patch 1/2] AES i586, remove setkey

Quoted 3 lines · Sebastian Siewior

The setkey() function can be shared with the generic algorithm.

Appled. Thanks!

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

PATCH 2/2 remove similar aes glue code

In reply to Sebastian Siewior

32 bit and 64 bit glue code is using (now) the same gleue code. Make it one.

---
 arch/x86/crypto/Makefile                 |    4 +-
 arch/x86/crypto/aes_32.c                 |   58 ------------------------------
 arch/x86/crypto/{aes_64.c => aes_glue.c} |   23 ++++++------
 3 files changed, 14 insertions(+), 71 deletions(-)
 delete mode 100644 arch/x86/crypto/aes_32.c
 rename arch/x86/crypto/{aes_64.c => aes_glue.c} (69%)
arch/x86/crypto/Makefile +2 -2
index 46bb609..b8fbb43 100644
@@ -8,8 +8,8 @@ obj-$(CONFIG_CRYPTO_TWOFISH_586) += twofish-i586.o
 obj-$(CONFIG_CRYPTO_AES_X86_64) += aes-x86_64.o
 obj-$(CONFIG_CRYPTO_TWOFISH_X86_64) += twofish-x86_64.o
 
-aes-i586-y := aes-i586-asm_32.o aes_32.o
+aes-i586-y := aes-i586-asm_32.o aes_glue.o
 twofish-i586-y := twofish-i586-asm_32.o twofish_32.o
 
-aes-x86_64-y := aes-x86_64-asm_64.o aes_64.o
+aes-x86_64-y := aes-x86_64-asm_64.o aes_glue.o
 twofish-x86_64-y := twofish-x86_64-asm_64.o twofish_64.o
arch/x86/crypto/aes_32.c deleted +0 -58

58 lines not on this page · raw message

arch/x86/crypto/aes_glue.c renamed from arch/x86/crypto/aes_64.c +12 -11
similarity index 69%
rename from arch/x86/crypto/aes_64.c
rename to arch/x86/crypto/aes_glue.c
index d7a41a9..5a283a4 100644
@@ -4,6 +4,7 @@
  */
 
 #include <crypto/aes.h>
+#include <linux/module.h>
 
 asmlinkage void aes_enc_blk(struct crypto_tfm *tfm, u8 *out, const u8 *in);
 asmlinkage void aes_dec_blk(struct crypto_tfm *tfm, u8 *out, const u8 *in);
@@ -19,21 +20,21 @@ static void aes_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
 }
 
 static struct crypto_alg aes_alg = {
-	.cra_name		=	"aes",
-	.cra_driver_name	=	"aes-x86_64",
+	.cra_name			=	"aes",
+	.cra_driver_name	=	"aes-asm",
 	.cra_priority		=	200,
-	.cra_flags		=	CRYPTO_ALG_TYPE_CIPHER,
+	.cra_flags			=	CRYPTO_ALG_TYPE_CIPHER,
 	.cra_blocksize		=	AES_BLOCK_SIZE,
 	.cra_ctxsize		=	sizeof(struct crypto_aes_ctx),
-	.cra_module		=	THIS_MODULE,
-	.cra_list		=	LIST_HEAD_INIT(aes_alg.cra_list),
-	.cra_u			=	{
-		.cipher = {
+	.cra_module			=	THIS_MODULE,
+	.cra_list			=	LIST_HEAD_INIT(aes_alg.cra_list),
+	.cra_u	=	{
+		.cipher	=	{
 			.cia_min_keysize	=	AES_MIN_KEY_SIZE,
 			.cia_max_keysize	=	AES_MAX_KEY_SIZE,
-			.cia_setkey		=	crypto_aes_set_key,
-			.cia_encrypt	 	=	aes_encrypt,
-			.cia_decrypt	  	=	aes_decrypt
+			.cia_setkey			=	crypto_aes_set_key,
+			.cia_encrypt		=	aes_encrypt,
+			.cia_decrypt		=	aes_decrypt
 		}
 	}
 };
@@ -51,6 +52,6 @@ static void __exit aes_fini(void)
 module_init(aes_init);
 module_exit(aes_fini);
 
-MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm");
+MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm, asm optimized");
 MODULE_LICENSE("GPL");
 MODULE_ALIAS("aes");
Signature · 1 line
1.5.3.4
maintainer
#4 lore raw

Re: [patch 2/2] remove similar aes glue code

arch/x86/crypto/aes_glue.c static void aes_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) · line 24

Quoted 6 lines · Sebastian Siewior

static struct crypto_alg aes_alg = {

arch/x86/crypto/aes_glue.c renamed from arch/x86/crypto/aes_64.c +2 -2
-	.cra_name		=	"aes",
-	.cra_driver_name	=	"aes-x86_64",
+	.cra_name			=	"aes",
+	.cra_driver_name	=	"aes-asm",

This is bad because the algorithm name no longer matches the module name. I suppose we could rename the modules too, or perhaps just add an alias?

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
#5 lore raw

Re: [patch 2/2] remove similar aes glue code

Quoted 10 lines · * Herbert Xu | 2007-11-10 19:19:55 [+0800]:

arch/x86/crypto/aes_glue.c static void aes_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) · line 24

Quoted 6 lines · Sebastian Siewior

static struct crypto_alg aes_alg = {

arch/x86/crypto/aes_glue.c renamed from arch/x86/crypto/aes_64.c +2 -2
-	.cra_name		=	"aes",
-	.cra_driver_name	=	"aes-x86_64",
+	.cra_name			=	"aes",
+	.cra_driver_name	=	"aes-asm",

This is bad because the algorithm name no longer matches the module name.

This is case for every aes driver after I renamed them. Or am I missing something? You are not talking about cra_drive_name are you?

Quoted 2 lines

I suppose we could rename the modules too, or perhaps just add an alias?

We can't rename the asm optimized module to aes.ko. Well we could but this might be little ugly since we allready haven an alias for AES provided by the generic algorithm (on which this algorithm depends on). If you are talking about adding an alias which one should it be? We allready have an AES alias in the glue code.

Quoted 1 line

Cheers,

Sebastian

maintainer
#6 lore raw

Re: [patch 2/2] remove similar aes glue code

Quoted 12 lines · Sebastian Siewior
Quoted 10 lines · * Herbert Xu | 2007-11-10 19:19:55 [+0800]:

arch/x86/crypto/aes_glue.c static void aes_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) · line 24

Quoted 6 lines · Sebastian Siewior

static struct crypto_alg aes_alg = {

arch/x86/crypto/aes_glue.c renamed from arch/x86/crypto/aes_64.c +2 -2
-	.cra_name		=	"aes",
-	.cra_driver_name	=	"aes-x86_64",
+	.cra_name			=	"aes",
+	.cra_driver_name	=	"aes-asm",

This is bad because the algorithm name no longer matches the module name.

Quoted 2 lines

This is case for every aes driver after I renamed them. Or am I missing something? You are not talking about cra_drive_name are you?

I meant the cra_driver_name. Both names can be used to find the algorithm.

Thanks,

Quoted 8 lines
Quoted 2 lines

I suppose we could rename the modules too, or perhaps just add an alias?

We can't rename the asm optimized module to aes.ko. Well we could but this might be little ugly since we allready haven an alias for AES provided by the generic algorithm (on which this algorithm depends on). If you are talking about adding an alias which one should it be? We allready have an AES alias in the glue code.

You could add an aes-asm alias.

Thanks,

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
#7 lore raw

Re: [patch 2/2] remove similar aes glue code

Quoted 13 lines · * Herbert Xu | 2007-11-11 09:18:56 [+0800]:
Quoted 12 lines · Sebastian Siewior
Quoted 10 lines · * Herbert Xu | 2007-11-10 19:19:55 [+0800]:

arch/x86/crypto/aes_glue.c static void aes_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) · line 24

Quoted 6 lines · Sebastian Siewior

static struct crypto_alg aes_alg = {

arch/x86/crypto/aes_glue.c renamed from arch/x86/crypto/aes_64.c +2 -2
-	.cra_name		=	"aes",
-	.cra_driver_name	=	"aes-x86_64",
+	.cra_name			=	"aes",
+	.cra_driver_name	=	"aes-asm",

This is bad because the algorithm name no longer matches the module name.

Quoted 5 lines
Quoted 2 lines

This is case for every aes driver after I renamed them. Or am I missing something? You are not talking about cra_drive_name are you?

I meant the cra_driver_name. Both names can be used to find the algorithm.

I checked it and cra_driver_name is only used for compare. I think you could use something like "cbc(aes-asm)" but this would only work on x86. Is there something I am overlooking?
I have no problem with adding another alias but atleast I would like understand it :)

Quoted 2 lines

Thanks,

Sebastian

maintainer
#8 lore raw

Re: [patch 2/2] remove similar aes glue code

Quoted 4 lines · Sebastian Siewior

I checked it and cra_driver_name is only used for compare. I think you could use something like "cbc(aes-asm)" but this would only work on x86. Is there something I am overlooking?

That's the whole point of cra_driver_name. Consider the case of someone who has a higher priority version of AES available, e.g., padlock or an async device. If they want to specify the assembly version of AES then they would use cbc(aes-asm). Just as they would use cbc(aes-generic) to select the C version.

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
#9 lore raw

PATCH 1/1 remove similar aes glue code

32 bit and 64 bit glue code is using (now) the same piece code. This patch unifies them.

---
 arch/x86/crypto/Makefile                 |    4 +-
 arch/x86/crypto/aes_32.c                 |   58 ------------------------------
 arch/x86/crypto/{aes_64.c => aes_glue.c} |   35 +++++++++---------
 3 files changed, 20 insertions(+), 77 deletions(-)
 delete mode 100644 arch/x86/crypto/aes_32.c
 rename arch/x86/crypto/{aes_64.c => aes_glue.c} (52%)
arch/x86/crypto/Makefile +2 -2
index 46bb609..b8fbb43 100644
@@ -8,8 +8,8 @@ obj-$(CONFIG_CRYPTO_TWOFISH_586) += twofish-i586.o
 obj-$(CONFIG_CRYPTO_AES_X86_64) += aes-x86_64.o
 obj-$(CONFIG_CRYPTO_TWOFISH_X86_64) += twofish-x86_64.o
 
-aes-i586-y := aes-i586-asm_32.o aes_32.o
+aes-i586-y := aes-i586-asm_32.o aes_glue.o
 twofish-i586-y := twofish-i586-asm_32.o twofish_32.o
 
-aes-x86_64-y := aes-x86_64-asm_64.o aes_64.o
+aes-x86_64-y := aes-x86_64-asm_64.o aes_glue.o
 twofish-x86_64-y := twofish-x86_64-asm_64.o twofish_64.o
arch/x86/crypto/aes_32.c deleted +0 -58

58 lines not on this page · raw message

arch/x86/crypto/aes_glue.c renamed from arch/x86/crypto/aes_64.c +18 -17
similarity index 52%
rename from arch/x86/crypto/aes_64.c
rename to arch/x86/crypto/aes_glue.c
index d7a41a9..71f4578 100644
@@ -1,5 +1,5 @@
 /*
- * Glue Code for AES Cipher Algorithm
+ * Glue Code for the asm optimized version of the AES Cipher Algorithm
  *
  */
 
@@ -19,21 +19,21 @@ static void aes_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
 }
 
 static struct crypto_alg aes_alg = {
-	.cra_name		=	"aes",
-	.cra_driver_name	=	"aes-x86_64",
-	.cra_priority		=	200,
-	.cra_flags		=	CRYPTO_ALG_TYPE_CIPHER,
-	.cra_blocksize		=	AES_BLOCK_SIZE,
-	.cra_ctxsize		=	sizeof(struct crypto_aes_ctx),
-	.cra_module		=	THIS_MODULE,
-	.cra_list		=	LIST_HEAD_INIT(aes_alg.cra_list),
-	.cra_u			=	{
-		.cipher = {
-			.cia_min_keysize	=	AES_MIN_KEY_SIZE,
-			.cia_max_keysize	=	AES_MAX_KEY_SIZE,
-			.cia_setkey		=	crypto_aes_set_key,
-			.cia_encrypt	 	=	aes_encrypt,
-			.cia_decrypt	  	=	aes_decrypt
+	.cra_name		= "aes",
+	.cra_driver_name	= "aes-asm",
+	.cra_priority		= 200,
+	.cra_flags		= CRYPTO_ALG_TYPE_CIPHER,
+	.cra_blocksize		= AES_BLOCK_SIZE,
+	.cra_ctxsize		= sizeof(struct crypto_aes_ctx),
+	.cra_module		= THIS_MODULE,
+	.cra_list		= LIST_HEAD_INIT(aes_alg.cra_list),
+	.cra_u	= {
+		.cipher	= {
+			.cia_min_keysize	= AES_MIN_KEY_SIZE,
+			.cia_max_keysize	= AES_MAX_KEY_SIZE,
+			.cia_setkey		= crypto_aes_set_key,
+			.cia_encrypt		= aes_encrypt,
+			.cia_decrypt		= aes_decrypt
 		}
 	}
 };
@@ -51,6 +51,7 @@ static void __exit aes_fini(void)
 module_init(aes_init);
 module_exit(aes_fini);
 
-MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm");
+MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm, asm optimized");
 MODULE_LICENSE("GPL");
 MODULE_ALIAS("aes");
+MODULE_ALIAS("aes-asm");
Signature · 1 line
1.5.2.5
maintainer
#10 lore raw

Re: [PATCH] [crypto] remove similar aes glue code

Quoted 4 lines · Sebastian Siewior

32 bit and 64 bit glue code is using (now) the same piece code. This patch unifies them.

Patch applied. Thanks Sebastian!

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