Debian Codegraph @

cipher/sha1-armv8-aarch64-ce.S

raw
1 /* sha1-armv8-aarch64-ce.S - ARM/CE accelerated SHA-1 transform function
2 * Copyright (C) 2016 Jussi Kivilinna <[email protected]>
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "asm-common-aarch64.h"
21
22 #if defined(__AARCH64EL__) && \
23 defined(HAVE_COMPATIBLE_GCC_AARCH64_PLATFORM_AS) && \
24 defined(HAVE_GCC_INLINE_ASM_AARCH64_CRYPTO) && defined(USE_SHA1)
25
26 .cpu generic+simd+crypto
27
28
29 /* Constants */
30
31 SECTION_RODATA
32
33 #define K1 0x5A827999
34 #define K2 0x6ED9EBA1
35 #define K3 0x8F1BBCDC
36 #define K4 0xCA62C1D6
37 .align 4
38 ELF(.type gcry_sha1_aarch64_ce_K_VEC,%object;)
39 gcry_sha1_aarch64_ce_K_VEC:
40 .LK_VEC:
41 .LK1: .long K1, K1, K1, K1
42 .LK2: .long K2, K2, K2, K2
43 .LK3: .long K3, K3, K3, K3
44 .LK4: .long K4, K4, K4, K4
45
46
47 /* Register macros */
48
49 #define sH4 s0
50 #define vH4 v0
51 #define vH0123 v1
52
53 #define qABCD q2
54 #define sABCD s2
55 #define vABCD v2
56 #define sE0 s3
57 #define vE0 v3
58 #define sE1 s4
59 #define vE1 v4
60
61 #define vT0 v5
62 #define vT1 v6
63
64 #define vW0 v16
65 #define vW1 v17
66 #define vW2 v18
67 #define vW3 v19
68
69 #define vK1 v20
70 #define vK2 v21
71 #define vK3 v22
72 #define vK4 v23
73
74
75 /* Round macros */
76
77 #define _(...) /*_*/
78 #define do_add(dst, src0, src1) add dst.4s, src0.4s, src1.4s;
79 #define do_sha1su0(w0,w1,w2) sha1su0 w0.4s,w1.4s,w2.4s;
80 #define do_sha1su1(w0,w3) sha1su1 w0.4s,w3.4s;
81
82 #define do_rounds(f, e0, e1, t, k, w0, w1, w2, w3, add_fn, sha1su0_fn, sha1su1_fn) \
83 sha1su1_fn( v##w3, v##w2 ); \
84 sha1h e0, sABCD; \
85 sha1##f qABCD, e1, v##t.4s; \
86 add_fn( v##t, v##w2, v##k ); \
87 sha1su0_fn( v##w0, v##w1, v##w2 );
88
89
90 /* Other functional macros */
91
92 #define CLEAR_REG(reg) movi reg.16b, #0;
93
94
95 .text
96
97 /*
98 * unsigned int
99 * _gcry_sha1_transform_armv8_ce (void *ctx, const unsigned char *data,
100 * size_t nblks)
101 */
102 .align 4
103 .globl _gcry_sha1_transform_armv8_ce
104 ELF(.type _gcry_sha1_transform_armv8_ce,%function;)
105 _gcry_sha1_transform_armv8_ce:
106 /* input:
107 * x0: ctx, CTX
108 * x1: data (64*nblks bytes)
109 * x2: nblks
110 */
111 CFI_STARTPROC();
112
113 cbz x2, .Ldo_nothing;
114
115 GET_DATA_POINTER(x4, .LK_VEC);
116
117 ld1 {vH0123.4s}, [x0] /* load h0,h1,h2,h3 */
118 ld1 {vK1.4s-vK4.4s}, [x4] /* load K1,K2,K3,K4 */
119 ldr sH4, [x0, #16] /* load h4 */
120
121 ld1 {vW0.16b-vW3.16b}, [x1], #64
122 mov vABCD.16b, vH0123.16b
123
124 rev32 vW0.16b, vW0.16b
125 rev32 vW1.16b, vW1.16b
126 rev32 vW2.16b, vW2.16b
127 do_add(vT0, vW0, vK1)
128 rev32 vW3.16b, vW3.16b
129 do_add(vT1, vW1, vK1)
130
131 .Loop:
132 do_rounds(c, sE1, sH4, T0, K1, W0, W1, W2, W3, do_add, do_sha1su0, _)
133 sub x2, x2, #1
134 do_rounds(c, sE0, sE1, T1, K1, W1, W2, W3, W0, do_add, do_sha1su0, do_sha1su1)
135 do_rounds(c, sE1, sE0, T0, K1, W2, W3, W0, W1, do_add, do_sha1su0, do_sha1su1)
136 do_rounds(c, sE0, sE1, T1, K2, W3, W0, W1, W2, do_add, do_sha1su0, do_sha1su1)
137 do_rounds(c, sE1, sE0, T0, K2, W0, W1, W2, W3, do_add, do_sha1su0, do_sha1su1)
138
139 do_rounds(p, sE0, sE1, T1, K2, W1, W2, W3, W0, do_add, do_sha1su0, do_sha1su1)
140 do_rounds(p, sE1, sE0, T0, K2, W2, W3, W0, W1, do_add, do_sha1su0, do_sha1su1)
141 do_rounds(p, sE0, sE1, T1, K2, W3, W0, W1, W2, do_add, do_sha1su0, do_sha1su1)
142 do_rounds(p, sE1, sE0, T0, K3, W0, W1, W2, W3, do_add, do_sha1su0, do_sha1su1)
143 do_rounds(p, sE0, sE1, T1, K3, W1, W2, W3, W0, do_add, do_sha1su0, do_sha1su1)
144
145 do_rounds(m, sE1, sE0, T0, K3, W2, W3, W0, W1, do_add, do_sha1su0, do_sha1su1)
146 do_rounds(m, sE0, sE1, T1, K3, W3, W0, W1, W2, do_add, do_sha1su0, do_sha1su1)
147 do_rounds(m, sE1, sE0, T0, K3, W0, W1, W2, W3, do_add, do_sha1su0, do_sha1su1)
148 do_rounds(m, sE0, sE1, T1, K4, W1, W2, W3, W0, do_add, do_sha1su0, do_sha1su1)
149 do_rounds(m, sE1, sE0, T0, K4, W2, W3, W0, W1, do_add, do_sha1su0, do_sha1su1)
150
151 do_rounds(p, sE0, sE1, T1, K4, W3, W0, W1, W2, do_add, do_sha1su0, do_sha1su1)
152 cbz x2, .Lend
153
154 ld1 {vW0.16b-vW1.16b}, [x1], #32 /* preload */
155 do_rounds(p, sE1, sE0, T0, K4, _ , _ , W2, W3, do_add, _, do_sha1su1)
156 rev32 vW0.16b, vW0.16b
157 ld1 {vW2.16b}, [x1], #16
158 rev32 vW1.16b, vW1.16b
159 do_rounds(p, sE0, sE1, T1, K4, _ , _ , W3, _ , do_add, _, _)
160 ld1 {vW3.16b}, [x1], #16
161 rev32 vW2.16b, vW2.16b
162 do_rounds(p, sE1, sE0, T0, _, _, _, _, _, _, _, _)
163 rev32 vW3.16b, vW3.16b
164 do_rounds(p, sE0, sE1, T1, _, _, _, _, _, _, _, _)
165
166 do_add(vT0, vW0, vK1)
167 add vH4.2s, vH4.2s, vE0.2s
168 add vABCD.4s, vABCD.4s, vH0123.4s
169 do_add(vT1, vW1, vK1)
170
171 mov vH0123.16b, vABCD.16b
172
173 b .Loop
174
175 .Lend:
176 do_rounds(p, sE1, sE0, T0, K4, _ , _ , W2, W3, do_add, _, do_sha1su1)
177 do_rounds(p, sE0, sE1, T1, K4, _ , _ , W3, _ , do_add, _, _)
178 do_rounds(p, sE1, sE0, T0, _, _, _, _, _, _, _, _)
179 do_rounds(p, sE0, sE1, T1, _, _, _, _, _, _, _, _)
180
181 add vH4.2s, vH4.2s, vE0.2s
182 add vH0123.4s, vH0123.4s, vABCD.4s
183
184 CLEAR_REG(vW0)
185 CLEAR_REG(vW1)
186 CLEAR_REG(vW2)
187 CLEAR_REG(vW3)
188 CLEAR_REG(vABCD)
189 CLEAR_REG(vE1)
190 CLEAR_REG(vE0)
191
192 str sH4, [x0, #16] /* store h4 */
193 st1 {vH0123.4s}, [x0] /* store h0,h1,h2,h3 */
194
195 CLEAR_REG(vH0123)
196 CLEAR_REG(vH4)
197
198 .Ldo_nothing:
199 mov x0, #0
200 ret_spec_stop
201 CFI_ENDPROC();
202 ELF(.size _gcry_sha1_transform_armv8_ce,.-_gcry_sha1_transform_armv8_ce;)
203
204 #endif