Apply %restore_fcommon macro for Address Sanitizer
[platform/upstream/nettle.git] / x86_64 / aesni / aes-encrypt-internal.asm
1 C x86_64/aesni/aes-encrypt-internal.asm
2
3
4 ifelse(<
5    Copyright (C) 2015 Niels Möller
6
7    This file is part of GNU Nettle.
8
9    GNU Nettle is free software: you can redistribute it and/or
10    modify it under the terms of either:
11
12      * the GNU Lesser General Public License as published by the Free
13        Software Foundation; either version 3 of the License, or (at your
14        option) any later version.
15
16    or
17
18      * the GNU General Public License as published by the Free
19        Software Foundation; either version 2 of the License, or (at your
20        option) any later version.
21
22    or both in parallel, as here.
23
24    GNU Nettle is distributed in the hope that it will be useful,
25    but WITHOUT ANY WARRANTY; without even the implied warranty of
26    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
27    General Public License for more details.
28
29    You should have received copies of the GNU General Public License and
30    the GNU Lesser General Public License along with this program.  If
31    not, see http://www.gnu.org/licenses/.
32 >)
33
34 C Input argument
35 define(<ROUNDS>, <%rdi>)
36 define(<KEYS>,  <%rsi>)
37 C define(<TABLE>,       <%rdx>) C Unused here
38 define(<LENGTH>,<%rcx>)
39 define(<DST>,   <%r8>)
40 define(<SRC>,   <%r9>)
41
42 C Round counter
43 define(<CNT>, <%rdx>)
44 C Subkey pointer
45 define(<KEY>, <%rax>)
46
47 dnl aesenc %xmm1, %xmm0
48 define(<AESENC>, <.byte 0x66, 0x0f, 0x38, 0xdc, 0xc1>)
49 dnl aesenclast %xmm1, %xmm0
50 define(<AESENCLAST>, <.byte 0x66, 0x0f, 0x38, 0xdd, 0xc1>)
51         
52         .file "aes-encrypt-internal.asm"
53
54         C _aes_encrypt(unsigned rounds, const uint32_t *keys,
55         C              const struct aes_table *T,
56         C              size_t length, uint8_t *dst,
57         C              uint8_t *src)
58         .text
59         ALIGN(16)
60 PROLOGUE(_nettle_aes_encrypt)
61         W64_ENTRY(6, 2)
62         shr     $4, LENGTH
63         test    LENGTH, LENGTH
64         jz      .Lend
65
66         decl    XREG(ROUNDS)
67
68 .Lblock_loop:
69         mov     ROUNDS, CNT
70         mov     KEYS, KEY
71         movups  (SRC), %xmm0
72         C FIXME: Better alignment of subkeys, so we can use movaps.
73         movups  (KEY), %xmm1
74         pxor    %xmm1, %xmm0
75
76         C FIXME: Could use some unrolling. Also all subkeys fit in
77         C registers, so they could be loaded once (on W64 we would
78         C need to save and restore some xmm registers, though).
79
80 .Lround_loop:
81         add     $16, KEY
82
83         movups  (KEY), %xmm1
84         AESENC  C %xmm1, %xmm0
85         decl    XREG(CNT)
86         jnz     .Lround_loop
87
88         movups  16(KEY), %xmm1
89         AESENCLAST      C %xmm1, %xmm0
90
91         movups  %xmm0, (DST)
92         add     $16, SRC
93         add     $16, DST
94         dec     LENGTH
95         jnz     .Lblock_loop
96
97 .Lend:
98         W64_EXIT(6, 2)
99         ret
100 EPILOGUE(_nettle_aes_encrypt)