Imported Upstream version 2.4
[platform/upstream/nettle.git] / sparc32 / aes-encrypt-internal.asm
1 C -*- mode: asm; asm-comment-char: ?C; -*-  
2 C nettle, low-level cryptographics library
3
4 C Copyright (C) 2002, 2005 Niels Möller
5 C  
6 C The nettle library is free software; you can redistribute it and/or modify
7 C it under the terms of the GNU Lesser General Public License as published by
8 C the Free Software Foundation; either version 2.1 of the License, or (at your
9 C option) any later version.
10
11 C The nettle library is distributed in the hope that it will be useful, but
12 C WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 C or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14 C License for more details.
15
16 C You should have received a copy of the GNU Lesser General Public License
17 C along with the nettle library; see the file COPYING.LIB.  If not, write to
18 C the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19 C MA 02111-1307, USA.
20
21 include_src(<sparc32/aes.m4>)
22
23 C       Arguments
24 define(<CTX>,   <%i0>)
25 define(<T>,     <%i1>)
26 define(<LENGTH>,<%i2>)
27 define(<DST>,   <%i3>)
28 define(<SRC>,   <%i4>)
29
30 C       AES state, two copies for unrolling
31
32 define(<W0>,    <%l0>)
33 define(<W1>,    <%l1>)
34 define(<W2>,    <%l2>)
35 define(<W3>,    <%l3>)
36
37 define(<X0>,    <%l4>)
38 define(<X1>,    <%l5>)
39 define(<X2>,    <%l6>)
40 define(<X3>,    <%l7>)
41
42 C       %o0-%03 are used for loop invariants T0-T3
43 define(<KEY>,   <%o4>)
44 define(<ROUND>, <%o5>)
45
46 C %g1, %g2, %g3 are TMP1, TMP2 and TMP3
47
48 C I'm still slightly confused by the frame layout, specified in
49 C "SYSTEM V APPLICATION BINARY INTERFACE SPARC Processor Supplement".
50 C However, Sun's cc generates a 104 byte stack frame for a function
51 C with no local variables, so that should be good enough for us too.
52
53 C The sparc32 stack frame looks like
54 C
55 C %fp -   4: OS-dependent link field
56 C %fp -   8: OS-dependent link field
57 C %fp - 104: OS register save area 
58 define(<FRAME_SIZE>, 104)
59
60         .file "aes-encrypt-internal.asm"
61
62         C _aes_encrypt(struct aes_context *ctx, 
63         C              const struct aes_table *T,
64         C              unsigned length, uint8_t *dst,
65         C              uint8_t *src)
66
67         .section        ".text"
68         .align 16
69         .proc   020
70         
71 PROLOGUE(_nettle_aes_encrypt)
72
73         save    %sp, -FRAME_SIZE, %sp
74         cmp     LENGTH, 0
75         be      .Lend
76
77         C       Loop invariants
78         add     T, AES_TABLE0, T0
79         add     T, AES_TABLE1, T1
80         add     T, AES_TABLE2, T2
81         add     T, AES_TABLE3, T3
82
83 .Lblock_loop:
84         C  Read src, and add initial subkey
85         add     CTX, AES_KEYS, KEY
86         AES_LOAD(0, SRC, KEY, W0)
87         AES_LOAD(1, SRC, KEY, W1)
88         AES_LOAD(2, SRC, KEY, W2)
89         AES_LOAD(3, SRC, KEY, W3)
90
91         C       Must be even, and includes the final round
92         ld      [AES_NROUNDS + CTX], ROUND
93         add     SRC, 16, SRC
94         add     KEY, 16, KEY
95
96         srl     ROUND, 1, ROUND
97         C       Last two rounds handled specially
98         sub     ROUND, 1, ROUND
99 .Lround_loop:
100         C The AES_ROUND macro uses T0,... T3
101         C       Transform W -> X
102         AES_ROUND(0, W0, W1, W2, W3, KEY, X0)
103         AES_ROUND(1, W1, W2, W3, W0, KEY, X1)
104         AES_ROUND(2, W2, W3, W0, W1, KEY, X2)
105         AES_ROUND(3, W3, W0, W1, W2, KEY, X3)
106
107         C       Transform X -> W
108         AES_ROUND(4, X0, X1, X2, X3, KEY, W0)
109         AES_ROUND(5, X1, X2, X3, X0, KEY, W1)
110         AES_ROUND(6, X2, X3, X0, X1, KEY, W2)
111         AES_ROUND(7, X3, X0, X1, X2, KEY, W3)
112
113         subcc   ROUND, 1, ROUND
114         bne     .Lround_loop
115         add     KEY, 32, KEY
116
117         C       Penultimate round
118         AES_ROUND(0, W0, W1, W2, W3, KEY, X0)
119         AES_ROUND(1, W1, W2, W3, W0, KEY, X1)
120         AES_ROUND(2, W2, W3, W0, W1, KEY, X2)
121         AES_ROUND(3, W3, W0, W1, W2, KEY, X3)
122
123         add     KEY, 16, KEY
124         C       Final round
125         AES_FINAL_ROUND(0, T, X0, X1, X2, X3, KEY, DST)
126         AES_FINAL_ROUND(1, T, X1, X2, X3, X0, KEY, DST)
127         AES_FINAL_ROUND(2, T, X2, X3, X0, X1, KEY, DST)
128         AES_FINAL_ROUND(3, T, X3, X0, X1, X2, KEY, DST)
129
130         subcc   LENGTH, 16, LENGTH
131         bne     .Lblock_loop
132         add     DST, 16, DST
133
134 .Lend:
135         ret
136         restore
137 EPILOGUE(_nettle_aes_encrypt)
138
139 C Some stats from adriana.lysator.liu.se (SS1000$, 85 MHz), for AES 128
140
141 C 1:    nettle-1.13 C-code
142 C 2:    nettle-1.13 assembler
143 C 3:    New C-code
144 C 4:    New assembler, first correct version
145 C 5:    New assembler, with basic scheduling of AES_ROUND.
146 C 6:    New assembpler, with loop invariants T0-T3.
147 C 7:    New assembler, with basic scheduling also of AES_FINAL_ROUND.
148         
149 C       MB/s    cycles/block    Code size (bytes)
150 C 1     1.2     1107            592
151 C 2     2.3     572             1032
152 C 3     2.1     627
153 C 4     1.8     722
154 C 5     2.6     496
155 C 6     3.0     437
156 C 7     3.1     415             1448