aes-encrypt-internal.asm 2.38 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
C x86_64/aesni/aes-encrypt-internal.asm


ifelse(<
   Copyright (C) 2015 Niels Möller

   This file is part of GNU Nettle.

   GNU Nettle is free software: you can redistribute it and/or
   modify it under the terms of either:

     * the GNU Lesser General Public License as published by the Free
       Software Foundation; either version 3 of the License, or (at your
       option) any later version.

   or

     * the GNU General Public License as published by the Free
       Software Foundation; either version 2 of the License, or (at your
       option) any later version.

   or both in parallel, as here.

   GNU Nettle is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received copies of the GNU General Public License and
   the GNU Lesser General Public License along with this program.  If
   not, see http://www.gnu.org/licenses/.
>)

C Input argument
define(<ROUNDS>, <%rdi>)
define(<KEYS>,	<%rsi>)
C define(<TABLE>,	<%rdx>) C Unused here
define(<LENGTH>,<%rcx>)
define(<DST>,	<%r8>)
define(<SRC>,	<%r9>)

C Round counter
define(<CNT>, <%rdx>)
C Subkey pointer
define(<KEY>, <%rax>)

47 48 49 50 51
dnl aesenc %xmm1, %xmm0
define(<AESENC>, <.byte 0x66, 0x0f, 0x38, 0xdc, 0xc1>)
dnl aesenclast %xmm1, %xmm0
define(<AESENCLAST>, <.byte 0x66, 0x0f, 0x38, 0xdd, 0xc1>)
	
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
	.file "aes-encrypt-internal.asm"

	C _aes_encrypt(unsigned rounds, const uint32_t *keys,
	C	       const struct aes_table *T,
	C	       size_t length, uint8_t *dst,
	C	       uint8_t *src)
	.text
	ALIGN(16)
PROLOGUE(_nettle_aes_encrypt)
	W64_ENTRY(6, 2)
	shr	$4, LENGTH
	test	LENGTH, LENGTH
	jz	.Lend

	decl	XREG(ROUNDS)

.Lblock_loop:
	mov	ROUNDS, CNT
	mov	KEYS, KEY
	movups	(SRC), %xmm0
	C FIXME: Better alignment of subkeys, so we can use movaps.
	movups	(KEY), %xmm1
	pxor	%xmm1, %xmm0

	C FIXME: Could use some unrolling. Also all subkeys fit in
	C registers, so they could be loaded once (on W64 we would
	C need to save and restore some xmm registers, though).

.Lround_loop:
	add	$16, KEY

	movups	(KEY), %xmm1
84
	AESENC	C %xmm1, %xmm0
85 86 87 88
	decl	XREG(CNT)
	jnz	.Lround_loop

	movups	16(KEY), %xmm1
89
	AESENCLAST	C %xmm1, %xmm0
90 91 92 93 94 95 96 97 98 99 100

	movups	%xmm0, (DST)
	add	$16, SRC
	add	$16, DST
	dec	LENGTH
	jnz	.Lblock_loop

.Lend:
	W64_EXIT(6, 2)
	ret
EPILOGUE(_nettle_aes_encrypt)