dfb498f5d52a4005db5c4afbf032146d9e6b08b3
[platform/upstream/nettle.git] / x86_64 / aes-encrypt-internal.asm
1 C x86_64/aes-encrypt-internal.asm
2
3
4 ifelse(<
5    Copyright (C) 2001, 2002, 2005, Rafael R. Sevilla, Niels Möller
6    Copyright (C) 2008, 2013 Niels Möller
7
8    This file is part of GNU Nettle.
9
10    GNU Nettle is free software: you can redistribute it and/or
11    modify it under the terms of either:
12
13      * the GNU Lesser General Public License as published by the Free
14        Software Foundation; either version 3 of the License, or (at your
15        option) any later version.
16
17    or
18
19      * the GNU General Public License as published by the Free
20        Software Foundation; either version 2 of the License, or (at your
21        option) any later version.
22
23    or both in parallel, as here.
24
25    GNU Nettle is distributed in the hope that it will be useful,
26    but WITHOUT ANY WARRANTY; without even the implied warranty of
27    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
28    General Public License for more details.
29
30    You should have received copies of the GNU General Public License and
31    the GNU Lesser General Public License along with this program.  If
32    not, see http://www.gnu.org/licenses/.
33 >)
34
35 include_src(<x86_64/aes.m4>)
36
37 C Register usage:
38
39 C AES state, use two of them
40 define(<SA>,<%eax>)
41 define(<SB>,<%ebx>)
42 define(<SC>,<%ecx>)
43 define(<SD>,<%edx>)
44
45 define(<TA>,<%r10d>)
46 define(<TB>,<%r11d>)
47 define(<TC>,<%r12d>)
48
49 C Input argument
50 define(<ROUNDS>, <%rdi>)
51 define(<KEYS>,  <%rsi>)
52 define(<PARAM_TABLE>,   <%rdx>)
53 define(<PARAM_LENGTH>,<%rcx>)
54 define(<DST>,   <%r8>)
55 define(<SRC>,   <%r9>)
56
57 define(<TABLE>, <%r13>) 
58 define(<LENGTH>,<%r14>)
59 define(<KEY>,   <%r15>)
60
61 C Must correspond to an old-style register, for movzb from %ah--%dh to
62 C work.
63 define(<TMP>,<%rbp>)
64
65         .file "aes-encrypt-internal.asm"
66         
67         C _aes_encrypt(unsigned rounds, const uint32_t *keys,
68         C              const struct aes_table *T,
69         C              size_t length, uint8_t *dst,
70         C              uint8_t *src)
71         .text
72         ALIGN(16)
73 PROLOGUE(_nettle_aes_encrypt)
74         W64_ENTRY(6, 0)
75         test    PARAM_LENGTH, PARAM_LENGTH
76         jz      .Lend
77
78         C save all registers that need to be saved
79         push    %rbx
80         push    %rbp
81         push    %r12
82         push    %r13
83         push    %r14
84         push    %r15    
85
86         subl    $1, XREG(ROUNDS)
87         push    ROUNDS          C Rounds at (%rsp) 
88         
89         mov     PARAM_TABLE, TABLE
90         mov     PARAM_LENGTH, LENGTH
91         shr     $4, LENGTH
92 .Lblock_loop:
93         mov     KEYS, KEY
94         
95         AES_LOAD(SA, SB, SC, SD, SRC, KEY)
96         add     $16, SRC        C Increment src pointer
97
98         movl    (%rsp), XREG(ROUNDS)
99
100         add     $16, KEY        C  point to next key
101         ALIGN(16)
102 .Lround_loop:
103         AES_ROUND(TABLE, SA,SB,SC,SD, TA, TMP)
104         AES_ROUND(TABLE, SB,SC,SD,SA, TB, TMP)
105         AES_ROUND(TABLE, SC,SD,SA,SB, TC, TMP)
106         AES_ROUND(TABLE, SD,SA,SB,SC, SD, TMP)
107
108         movl    TA, SA
109         movl    TB, SB
110         movl    TC, SC
111
112         xorl    (KEY),SA        C  add current session key to plaintext
113         xorl    4(KEY),SB
114         xorl    8(KEY),SC
115         xorl    12(KEY),SD
116
117         add     $16, KEY        C  point to next key
118         decl    XREG(ROUNDS)
119         jnz     .Lround_loop
120
121         C last round
122         AES_FINAL_ROUND(SA,SB,SC,SD, TABLE, TA, TMP)
123         AES_FINAL_ROUND(SB,SC,SD,SA, TABLE, TB, TMP)
124         AES_FINAL_ROUND(SC,SD,SA,SB, TABLE, TC, TMP)
125         AES_FINAL_ROUND(SD,SA,SB,SC, TABLE, SD, TMP)
126
127         C S-box substitution
128         mov     $3, XREG(ROUNDS)
129 .Lsubst:
130         AES_SUBST_BYTE(TA,TB,TC,SD, TABLE, TMP)
131
132         decl    XREG(ROUNDS)
133         jnz     .Lsubst
134
135         C Add last subkey, and store encrypted data
136         AES_STORE(TA,TB,TC,SD, KEY, DST)
137         
138         add     $16, DST
139         dec     LENGTH
140
141         jnz     .Lblock_loop
142
143         lea     8(%rsp), %rsp   C Drop ROUNDS
144         pop     %r15
145         pop     %r14
146         pop     %r13
147         pop     %r12
148         pop     %rbp
149         pop     %rbx
150 .Lend:
151         W64_EXIT(6, 0)
152         ret
153 EPILOGUE(_nettle_aes_encrypt)