43f2f394dd03eda397c7df9da143851d5516e6e8
[platform/upstream/nettle.git] / x86_64 / aes-decrypt-internal.asm
1 C x86_64/aes-decrypt-internal.asm
2
3 ifelse(<
4    Copyright (C) 2001, 2002, 2005, Rafael R. Sevilla, Niels Möller
5    Copyright (C) 2008, 2013 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 include_src(<x86_64/aes.m4>)
35
36 C Register usage:
37
38 C AES state, use two of them
39 define(<SA>,<%eax>)
40 define(<SB>,<%ebx>)
41 define(<SC>,<%ecx>)
42 define(<SD>,<%edx>)
43
44 define(<TA>,<%r10d>)
45 define(<TB>,<%r11d>)
46 define(<TC>,<%r12d>)
47
48 C Input argument
49 define(<ROUNDS>, <%rdi>)
50 define(<KEYS>,  <%rsi>)
51 define(<PARAM_TABLE>,   <%rdx>)
52 define(<PARAM_LENGTH>,<%rcx>)
53 define(<DST>,   <%r8>)
54 define(<SRC>,   <%r9>)
55
56 define(<TABLE>, <%r13>) 
57 define(<LENGTH>,<%r14>)
58 define(<KEY>,   <%r15>)
59
60 C Must correspond to an old-style register, for movzb from %ah--%dh to
61 C work.
62 define(<TMP>,<%rbp>)
63
64         .file "aes-decrypt-internal.asm"
65         
66         C _aes_decrypt(unsigned rounds, const uint32_t *keys,
67         C              const struct aes_table *T,
68         C              size_t length, uint8_t *dst,
69         C              uint8_t *src)
70         .text
71         ALIGN(16)
72 PROLOGUE(_nettle_aes_decrypt)
73         W64_ENTRY(6, 0)
74         test    PARAM_LENGTH, PARAM_LENGTH
75         jz      .Lend
76
77         C save all registers that need to be saved
78         push    %rbx
79         push    %rbp
80         push    %r12
81         push    %r13
82         push    %r14
83         push    %r15    
84
85         subl    $1, XREG(ROUNDS)
86         push    ROUNDS          C Rounds at (%rsp) 
87         
88         mov     PARAM_TABLE, TABLE
89         mov     PARAM_LENGTH, LENGTH
90         shr     $4, LENGTH
91 .Lblock_loop:
92         mov     KEYS, KEY
93         
94         AES_LOAD(SA, SB, SC, SD, SRC, KEY)
95         add     $16, SRC        C Increment src pointer
96
97         movl    (%rsp), XREG(ROUNDS)
98
99         add     $16, KEY        C  point to next key
100         ALIGN(16)
101 .Lround_loop:
102         AES_ROUND(TABLE, SA,SD,SC,SB, TA, TMP)
103         AES_ROUND(TABLE, SB,SA,SD,SC, TB, TMP)
104         AES_ROUND(TABLE, SC,SB,SA,SD, TC, TMP)
105         AES_ROUND(TABLE, SD,SC,SB,SA, SD, TMP)
106
107         movl    TA, SA
108         movl    TB, SB
109         movl    TC, SC
110
111         xorl    (KEY),SA        C  add current session key to plaintext
112         xorl    4(KEY),SB
113         xorl    8(KEY),SC
114         xorl    12(KEY),SD
115
116         add     $16, KEY        C  point to next key
117         decl    XREG(ROUNDS)
118         jnz     .Lround_loop
119
120         C last round
121         AES_FINAL_ROUND(SA,SD,SC,SB, TABLE, TA, TMP)
122         AES_FINAL_ROUND(SB,SA,SD,SC, TABLE, TB, TMP)
123         AES_FINAL_ROUND(SC,SB,SA,SD, TABLE, TC, TMP)
124         AES_FINAL_ROUND(SD,SC,SB,SA, TABLE, SD, TMP)
125
126         C Inverse S-box substitution
127         mov     $3, XREG(ROUNDS)
128 .Lsubst:
129         AES_SUBST_BYTE(TA,TB,TC,SD, TABLE, TMP)
130
131         decl    XREG(ROUNDS)
132         jnz     .Lsubst
133
134         C Add last subkey, and store decrypted data
135         AES_STORE(TA,TB,TC,SD, KEY, DST)
136         
137         add     $16, DST
138         dec     LENGTH
139
140         jnz     .Lblock_loop
141
142         lea     8(%rsp), %rsp   C Drop ROUNDS
143         pop     %r15
144         pop     %r14
145         pop     %r13
146         pop     %r12
147         pop     %rbp
148         pop     %rbx
149 .Lend:
150         W64_EXIT(6, 0)
151         ret
152 EPILOGUE(_nettle_aes_decrypt)