Revert "Merge branch 'upstream' into tizen"
[platform/upstream/nettle.git] / x86_64 / aes-decrypt-internal.asm
1 C nettle, low-level cryptographics library
2
3 C Copyright (C) 2001, 2002, 2005, 2008 Rafael R. Sevilla, Niels Möller
4 C  
5 C The nettle library is free software; you can redistribute it and/or modify
6 C it under the terms of the GNU Lesser General Public License as published by
7 C the Free Software Foundation; either version 2.1 of the License, or (at your
8 C option) any later version.
9
10 C The nettle library is distributed in the hope that it will be useful, but
11 C WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 C or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
13 C License for more details.
14
15 C You should have received a copy of the GNU Lesser General Public License
16 C along with the nettle library; see the file COPYING.LIB.  If not, write to
17 C the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 C MA 02111-1301, USA.
19
20 include_src(<x86_64/aes.m4>)
21
22 C Register usage:
23
24 C AES state, use two of them
25 define(<SA>,<%eax>)
26 define(<SB>,<%ebx>)
27 define(<SC>,<%ecx>)
28 define(<SD>,<%edx>)
29
30 define(<TA>,<%r10d>)
31 define(<TB>,<%r11d>)
32 define(<TC>,<%r12d>)
33
34 define(<CTX>,   <%rdi>)
35 define(<TABLE>, <%rsi>)
36 define(<PARAM_LENGTH>,<%edx>)           C Length is only 32 bits
37 define(<PARAM_DST>,     <%rcx>)
38 define(<SRC>,   <%r8>)
39
40 define(<DST>, <%r9>) 
41 define(<KEY>,<%r14>)
42 define(<COUNT>, <%r15d>)
43 define(<BLOCK_COUNT>, <%r13d>)
44
45 C Must correspond to an old-style register, for movzb from %ah--%dh to
46 C work.
47 define(<TMP>,<%rbp>)
48
49         .file "aes-decrypt-internal.asm"
50         
51         C _aes_decrypt(struct aes_context *ctx, 
52         C              const struct aes_table *T,
53         C              unsigned length, uint8_t *dst,
54         C              uint8_t *src)
55         .text
56         ALIGN(16)
57 PROLOGUE(_nettle_aes_decrypt)
58         W64_ENTRY(5, 0)
59         test    PARAM_LENGTH, PARAM_LENGTH
60         jz      .Lend
61
62         C save all registers that need to be saved
63         push    %rbx
64         push    %rbp
65         push    %r12
66         push    %r13
67         push    %r14
68         push    %r15    
69
70         mov     PARAM_DST, DST
71         movl    PARAM_LENGTH, BLOCK_COUNT
72         shrl    $4, BLOCK_COUNT
73 .Lblock_loop:
74         mov     CTX,KEY
75         
76         AES_LOAD(SA, SB, SC, SD, SRC, KEY)
77         add     $16, SRC        C Increment src pointer
78
79         C  get number of rounds to do from ctx struct   
80         movl    AES_NROUNDS (CTX), COUNT
81         subl    $1, COUNT
82
83         add     $16,KEY         C  point to next key
84         ALIGN(16)
85 .Lround_loop:
86         AES_ROUND(TABLE, SA,SD,SC,SB, TA, TMP)
87         AES_ROUND(TABLE, SB,SA,SD,SC, TB, TMP)
88         AES_ROUND(TABLE, SC,SB,SA,SD, TC, TMP)
89         AES_ROUND(TABLE, SD,SC,SB,SA, SD, TMP)
90
91         movl    TA, SA
92         movl    TB, SB
93         movl    TC, SC
94
95         xorl    (KEY),SA        C  add current session key to plaintext
96         xorl    4(KEY),SB
97         xorl    8(KEY),SC
98         xorl    12(KEY),SD
99
100         add     $16,KEY C  point to next key
101         decl    COUNT
102         jnz     .Lround_loop
103
104         C last round
105         AES_FINAL_ROUND(SA,SD,SC,SB, TABLE, TA, TMP)
106         AES_FINAL_ROUND(SB,SA,SD,SC, TABLE, TB, TMP)
107         AES_FINAL_ROUND(SC,SB,SA,SD, TABLE, TC, TMP)
108         AES_FINAL_ROUND(SD,SC,SB,SA, TABLE, SD, TMP)
109
110         C Inverse S-box substitution
111         mov     $3, COUNT
112 .Lsubst:
113         AES_SUBST_BYTE(TA,TB,TC,SD, TABLE, TMP)
114
115         decl    COUNT
116         jnz     .Lsubst
117
118         C Add last subkey, and store decrypted data
119         AES_STORE(TA,TB,TC,SD, KEY, DST)
120         
121         add     $16, DST
122         decl    BLOCK_COUNT
123
124         jnz     .Lblock_loop
125
126         pop     %r15    
127         pop     %r14
128         pop     %r13
129         pop     %r12
130         pop     %rbp
131         pop     %rbx
132 .Lend:
133         W64_EXIT(5, 0)
134         ret
135 EPILOGUE(_nettle_aes_decrypt)