Revert "Merge branch 'upstream' into tizen"
[platform/upstream/nettle.git] / arm / v6 / aes-encrypt-internal.asm
1 C nettle, low-level cryptographics library
2
3 C Copyright (C) 2013 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(<arm/aes.m4>)
21
22 C       Benchmarked at at 693, 824, 950 cycles/block on cortex A9,
23 C       for 128, 192 and 256 bit key sizes.
24
25 C       Possible improvements: More efficient load and store with
26 C       aligned accesses. Better scheduling.
27
28 define(<CTX>, <r0>)
29 define(<TABLE>, <r1>)
30 define(<LENGTH>, <r2>)
31 define(<DST>, <r3>)
32 define(<SRC>, <r12>)
33
34 define(<W0>, <r4>)
35 define(<W1>, <r5>)
36 define(<W2>, <r6>)
37 define(<W3>, <r7>)
38 define(<T0>, <r8>)
39 define(<KEY>, <r10>)
40 define(<ROUND>, <r11>)
41
42 define(<X0>, <r2>)      C Overlaps LENGTH, SRC, DST
43 define(<X1>, <r3>)
44 define(<X2>, <r12>)
45 define(<X3>, <r14>)     C lr
46
47
48         .file "aes-encrypt-internal.asm"
49         
50         C _aes_encrypt(struct aes_context *ctx, 
51         C              const struct aes_table *T,
52         C              unsigned length, uint8_t *dst,
53         C              uint8_t *src)
54         .text
55         .align 2
56 PROLOGUE(_nettle_aes_encrypt)
57         teq     LENGTH, #0
58         beq     .Lend
59         ldr     SRC, [sp]
60
61         push    {r4,r5,r6,r7,r8,r10,r11,lr}
62 .Lblock_loop:
63         mov     KEY, CTX
64         AES_LOAD(SRC,KEY,W0)
65         AES_LOAD(SRC,KEY,W1)
66         AES_LOAD(SRC,KEY,W2)
67         AES_LOAD(SRC,KEY,W3)
68
69         push    {LENGTH, DST, SRC}
70         ldr     ROUND, [CTX, #+AES_NROUNDS]
71         add     TABLE, TABLE, #AES_TABLE0
72
73         b       .Lentry
74         .align 2
75 .Lround_loop:
76         C       Transform X -> W
77         AES_ENCRYPT_ROUND(X0, X1, X2, X3, W0, W1, W2, W3, KEY)
78         
79 .Lentry:
80         subs    ROUND, ROUND,#2
81         C       Transform W -> X
82         AES_ENCRYPT_ROUND(W0, W1, W2, W3, X0, X1, X2, X3, KEY)
83
84         bne     .Lround_loop
85
86         sub     TABLE, TABLE, #AES_TABLE0
87         C       Final round
88         AES_FINAL_ROUND(X0, X1, X2, X3, KEY, W0)
89         AES_FINAL_ROUND(X1, X2, X3, X0, KEY, W1)
90         AES_FINAL_ROUND(X2, X3, X0, X1, KEY, W2)
91         AES_FINAL_ROUND(X3, X0, X1, X2, KEY, W3)
92
93         pop     {LENGTH, DST, SRC}
94         
95         AES_STORE(DST,W0)
96         AES_STORE(DST,W1)
97         AES_STORE(DST,W2)
98         AES_STORE(DST,W3)
99
100         subs    LENGTH, LENGTH, #16
101         bhi     .Lblock_loop
102
103         pop     {r4,r5,r6,r7,r8,r10,r11,pc}
104         
105 .Lend:
106         bx      lr
107 EPILOGUE(_nettle_aes_encrypt)