Initialize Tizen 2.3
[external/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., 59 Temple Place - Suite 330, Boston,
18 C MA 02111-1307, 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(4)
57 PROLOGUE(_nettle_aes_decrypt)
58         test    PARAM_LENGTH, PARAM_LENGTH
59         jz      .Lend
60
61         C save all registers that need to be saved
62         push    %rbx
63         push    %rbp
64         push    %r12
65         push    %r13
66         push    %r14
67         push    %r15    
68
69         mov     PARAM_DST, DST
70         movl    PARAM_LENGTH, BLOCK_COUNT
71         shrl    $4, BLOCK_COUNT
72 .Lblock_loop:
73         mov     CTX,KEY
74         
75         AES_LOAD(SA, SB, SC, SD, SRC, KEY)
76         add     $16, SRC        C Increment src pointer
77
78         C  get number of rounds to do from ctx struct   
79         movl    AES_NROUNDS (CTX), COUNT
80         subl    $1, COUNT
81
82         add     $16,KEY         C  point to next key
83         ALIGN(4)
84 .Lround_loop:
85         AES_ROUND(TABLE, SA,SD,SC,SB, TA, TMP)
86         AES_ROUND(TABLE, SB,SA,SD,SC, TB, TMP)
87         AES_ROUND(TABLE, SC,SB,SA,SD, TC, TMP)
88         AES_ROUND(TABLE, SD,SC,SB,SA, SD, TMP)
89
90         movl    TA, SA
91         movl    TB, SB
92         movl    TC, SC
93
94         xorl    (KEY),SA        C  add current session key to plaintext
95         xorl    4(KEY),SB
96         xorl    8(KEY),SC
97         xorl    12(KEY),SD
98
99         add     $16,KEY C  point to next key
100         decl    COUNT
101         jnz     .Lround_loop
102
103         C last round
104         AES_FINAL_ROUND(SA,SD,SC,SB, TABLE, TA, TMP)
105         AES_FINAL_ROUND(SB,SA,SD,SC, TABLE, TB, TMP)
106         AES_FINAL_ROUND(SC,SB,SA,SD, TABLE, TC, TMP)
107         AES_FINAL_ROUND(SD,SC,SB,SA, TABLE, SD, TMP)
108
109         C Inverse S-box substitution
110         mov     $3, COUNT
111 .Lsubst:
112         AES_SUBST_BYTE(TA,TB,TC,SD, TABLE, TMP)
113
114         decl    COUNT
115         jnz     .Lsubst
116
117         C Add last subkey, and store decrypted data
118         AES_STORE(TA,TB,TC,SD, KEY, DST)
119         
120         add     $16, DST
121         decl    BLOCK_COUNT
122
123         jnz     .Lblock_loop
124
125         pop     %r15    
126         pop     %r14
127         pop     %r13
128         pop     %r12
129         pop     %rbp
130         pop     %rbx
131 .Lend:
132         ret
133 EPILOGUE(_nettle_aes_decrypt)