C the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
C MA 02111-1307, USA.
+
.file "aes.asm"
.data
.text
-.globl print_word
+C Register usage:
+C
+C The aes state is kept in %eax, %ebx, %ecx and %edx
+C
+C %esi is used as temporary, to point to the input, and to the
+C subkeys, etc.
+C
+C %ebp is used as the round counter, and as a temporary in the final round.
+C
+C %edi is a temporary, often used as an accumulator.
C aes_encrypt(struct aes_context *ctx,
C unsigned length, uint8_t *dst,
xorl 4(%esi),%ebx
xorl 8(%esi),%ecx
xorl 12(%esi),%edx
-aes_xored_initial:
+aes_xored_initial:
+ C FIXME: Use %esi instead
movl 20(%esp),%ebp C address of context struct
movl AES_NROUNDS (%ebp),%ebp C get number of rounds to do from struct
C ^ table[1][B1(%ebx)]
C ^ table[2][B2(%ebx)]
C ^ table[3][B3(%ebx)]
+ C
+ C a b c d
movl %eax, %esi
andl $0xff, %esi
shll $2,%esi C index in dtbl1
pushl %edi C save first on stack
C // Second column
+ C b c d a
movl %ebx,%esi C copy first in
andl $0x000000ff,%esi C clear all but offset
shll $2,%esi C index in dtbl1
pushl %edi C save first on stack
C // Third column
+ C c d a b
movl %ecx,%esi C copy first in
andl $0x000000ff,%esi C clear all but offset
shll $2,%esi C index in dtbl1
pushl %edi C save first on stack
C // Fourth column
+ C d a b c
movl %edx,%esi C copy first in
andl $0x000000ff,%esi C clear all but offset
shll $2,%esi C index in dtbl1
C // last round
C // first column
+ C a b c d
movl %eax,%edi
andl $0x000000ff,%edi
movl %ebx,%ebp
pushl %edi
C // second column
+ C d a b c
movl %eax,%edi
andl $0x0000ff00,%edi
movl %ebx,%ebp
orl %ebp,%edi
pushl %edi
+ C c d a b
C // third column
movl %eax,%edi
andl $0x00ff0000,%edi
pushl %edi
C // fourth column
+ C b c d a
movl %eax,%edi
andl $0xff000000,%edi
movl %ebx,%ebp
.size aes_encrypt,.eore-aes_encrypt
- C // aes_decrypt(AES_context *ctx, const UINT8 *ciphertext
- C // UINT8 *plaintext)
+ C aes_encrypt(struct aes_context *ctx,
+ C unsigned length, uint8_t *dst,
+ C uint8_t *src)
.align 16
.globl aes_decrypt
.type aes_decrypt,@function
pushl %ebp C 12(%esp)
pushl %esi C 8(%esp)
pushl %edi C 4(%esp)
- movl 24(%esp),%esi C address of ciphertext
+
+ C ctx = 20(%esp)
+ C length = 24(%esp)
+ C dst = 28(%esp)
+ C src = 32(%esp)
+
+ movl 32(%esp),%esi C address of ciphertext
movl (%esi),%eax C load ciphertext into registers
movl 4(%esi),%ebx
movl 8(%esi),%ecx
movl 12(%esi),%edx
+
movl 20(%esp),%esi C address of context struct ctx
- movl 480(%esi),%ebp C get number of rounds to do from struct
- shll $4,%ebp
- leal 240(%esi, %ebp),%esi
- shrl $4,%ebp
- xorl (%esi),%eax C add last key to ciphertext
+ xorl (%esi),%eax C add first key to ciphertext
xorl 4(%esi),%ebx
xorl 8(%esi),%ecx
xorl 12(%esi),%edx
+ movl AES_NROUNDS (%esi),%ebp C get number of rounds to do from struct
+ C shll $4,%ebp
+ C leal 240(%esi, %ebp),%esi
+ C shrl $4,%ebp
+ C xorl (%esi),%eax C add last key to ciphertext
+ C xorl 4(%esi),%ebx
+ C xorl 8(%esi),%ecx
+ C xorl 12(%esi),%edx
subl $1,%ebp C one round is complete
- subl $16,%esi C point to previous key
-.decrypt_loop:
+ addl $16,%esi C point to next key
+Ldecrypt_loop:
pushl %esi C save this first: we'll clobber it later
+
+ C Why???
xchgl %ebx,%edx
C // First column
+ C a b c d
movl %eax,%esi C copy first in
andl $0x000000ff,%esi C clear all but offset
shll $2,%esi C index in itbl1
pushl %edi C save first on stack
C // Second column
+ C d a b c
movl %edx,%esi C copy first in
andl $0x000000ff,%esi C clear all but offset
shll $2,%esi C index in itbl1
pushl %edi
C // Third column
+ C c d a b
movl %ecx,%esi C copy first in
andl $0x000000ff,%esi C clear all but offset
shll $2,%esi C index in itbl1
pushl %edi C save first on stack
C // Fourth column
+ C b c d a
movl %ebx,%esi C copy first in
andl $0x000000ff,%esi C clear all but offset
shll $2,%esi C index in itbl1
shrl $22,%esi
andl $0x000003fc,%esi
xorl itbl4(%esi),%edi
+
movl %edi,%edx
popl %ecx
popl %ebx
xorl 4(%esi),%ebx
xorl 8(%esi),%ecx
xorl 12(%esi),%edx
- subl $16,%esi C point to previous key
+ addl $16,%esi C point to next key
decl %ebp
- jnz .decrypt_loop
+ jnz Ldecrypt_loop
+ C Foo?
xchgl %ebx,%edx
C // last round
C // first column
+ C a b c d
movl %eax,%edi
andl $0x000000ff,%edi
movl %ebx,%ebp
pushl %edi
C // second column
+ C b c d a
movl %eax,%edi
andl $0xff000000,%edi
movl %ebx,%ebp
pushl %edi
C // third column
+ C c d a b
movl %eax,%edi
andl $0x00ff0000,%edi
movl %ebx,%ebp
orl %ebp,%edi
pushl %edi
- C // second column
+ C // fourth column
+ C d a b c
movl %eax,%edi
andl $0x0000ff00,%edi
movl %ebx,%ebp
decl %edi
jnz .isb_sub
- xorl (%esi),%eax C add first key to plaintext
+ xorl (%esi),%eax C add last key to plaintext
xorl 4(%esi),%ebx
xorl 8(%esi),%ecx
xorl 12(%esi),%edx