Debian Codegraph @

cipher/asm-poly1305-aarch64.h

raw
1 /* asm-common-aarch64.h - Poly1305 macros for ARMv8/AArch64 assembly
2 *
3 * Copyright (C) 2019 Jussi Kivilinna <[email protected]>
4 *
5 * This file is part of Libgcrypt.
6 *
7 * Libgcrypt is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * Libgcrypt is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #ifndef GCRY_ASM_POLY1305_AARCH64_H
22 #define GCRY_ASM_POLY1305_AARCH64_H
23
24 #include "asm-common-aarch64.h"
25
26 #ifdef __AARCH64EL__
27 #define le_to_host(reg) /*_*/
28 #else
29 #define le_to_host(reg) rev reg, reg;
30 #endif
31
32 /**********************************************************************
33 poly1305 for stitched chacha20-poly1305 Aarch64 implementations
34 **********************************************************************/
35
36 #define POLY_RSTATE x8
37 #define POLY_RSRC x9
38
39 #define POLY_R_H0 x10
40 #define POLY_R_H1 x11
41 #define POLY_R_H2 x12
42 #define POLY_R_H2d w12
43 #define POLY_R_R0 x13
44 #define POLY_R_R1 x14
45 #define POLY_R_R1_MUL5 x15
46 #define POLY_R_X0_HI x16
47 #define POLY_R_X0_LO x17
48 #define POLY_R_X1_HI x19
49 #define POLY_R_X1_LO x20
50 #define POLY_R_ONE x21
51 #define POLY_R_ONEd w21
52
53 #define POLY_TMP0 x22
54 #define POLY_TMP1 x23
55 #define POLY_TMP2 x24
56 #define POLY_TMP3 x25
57
58 #define POLY_CHACHA_ROUND x26
59
60 #define POLY_S_R0 (4 * 4 + 0 * 8)
61 #define POLY_S_R1 (4 * 4 + 1 * 8)
62 #define POLY_S_H0 (4 * 4 + 2 * 8 + 0 * 8)
63 #define POLY_S_H1 (4 * 4 + 2 * 8 + 1 * 8)
64 #define POLY_S_H2d (4 * 4 + 2 * 8 + 2 * 8)
65
66 #define POLY1305_PUSH_REGS() \
67 stp x19, x20, [sp, #-16]!; \
68 CFI_ADJUST_CFA_OFFSET(16); \
69 CFI_REG_ON_STACK(19, 0); \
70 CFI_REG_ON_STACK(20, 8); \
71 stp x21, x22, [sp, #-16]!; \
72 CFI_ADJUST_CFA_OFFSET(16); \
73 CFI_REG_ON_STACK(21, 0); \
74 CFI_REG_ON_STACK(22, 8); \
75 stp x23, x24, [sp, #-16]!; \
76 CFI_ADJUST_CFA_OFFSET(16); \
77 CFI_REG_ON_STACK(23, 0); \
78 CFI_REG_ON_STACK(24, 8); \
79 stp x25, x26, [sp, #-16]!; \
80 CFI_ADJUST_CFA_OFFSET(16); \
81 CFI_REG_ON_STACK(25, 0); \
82 CFI_REG_ON_STACK(26, 8);
83
84 #define POLY1305_POP_REGS() \
85 ldp x25, x26, [sp], #16; \
86 CFI_ADJUST_CFA_OFFSET(-16); \
87 CFI_RESTORE(x25); \
88 CFI_RESTORE(x26); \
89 ldp x23, x24, [sp], #16; \
90 CFI_ADJUST_CFA_OFFSET(-16); \
91 CFI_RESTORE(x23); \
92 CFI_RESTORE(x24); \
93 ldp x21, x22, [sp], #16; \
94 CFI_ADJUST_CFA_OFFSET(-16); \
95 CFI_RESTORE(x21); \
96 CFI_RESTORE(x22); \
97 ldp x19, x20, [sp], #16; \
98 CFI_ADJUST_CFA_OFFSET(-16); \
99 CFI_RESTORE(x19); \
100 CFI_RESTORE(x20);
101
102 #define POLY1305_LOAD_STATE() \
103 ldr POLY_R_R1, [POLY_RSTATE, #(POLY_S_R1)]; \
104 ldr POLY_R_H0, [POLY_RSTATE, #(POLY_S_H0)]; \
105 ldr POLY_R_H1, [POLY_RSTATE, #(POLY_S_H1)]; \
106 ldr POLY_R_H2d, [POLY_RSTATE, #(POLY_S_H2d)]; \
107 ldr POLY_R_R0, [POLY_RSTATE, #(POLY_S_R0)]; \
108 add POLY_R_R1_MUL5, POLY_R_R1, POLY_R_R1, lsr #2; \
109 mov POLY_R_ONE, #1;
110
111 #define POLY1305_STORE_STATE() \
112 str POLY_R_H0, [POLY_RSTATE, #(POLY_S_H0)]; \
113 str POLY_R_H1, [POLY_RSTATE, #(POLY_S_H1)]; \
114 str POLY_R_H2d, [POLY_RSTATE, #(POLY_S_H2d)];
115
116 #define POLY1305_BLOCK_PART1(src_offset) \
117 /* a = h + m */ \
118 ldr POLY_TMP0, [POLY_RSRC, #((src_offset) + 0 * 8)];
119 #define POLY1305_BLOCK_PART2(src_offset) \
120 ldr POLY_TMP1, [POLY_RSRC, #((src_offset) + 1 * 8)];
121 #define POLY1305_BLOCK_PART3() \
122 le_to_host(POLY_TMP0);
123 #define POLY1305_BLOCK_PART4() \
124 le_to_host(POLY_TMP1);
125 #define POLY1305_BLOCK_PART5() \
126 adds POLY_R_H0, POLY_R_H0, POLY_TMP0;
127 #define POLY1305_BLOCK_PART6() \
128 adcs POLY_R_H1, POLY_R_H1, POLY_TMP1;
129 #define POLY1305_BLOCK_PART7() \
130 adc POLY_R_H2d, POLY_R_H2d, POLY_R_ONEd;
131
132 #define POLY1305_BLOCK_PART8() \
133 /* h = a * r (partial mod 2^130-5): */ \
134 mul POLY_R_X1_LO, POLY_R_H0, POLY_R_R1; /* lo: h0 * r1 */
135 #define POLY1305_BLOCK_PART9() \
136 mul POLY_TMP0, POLY_R_H1, POLY_R_R0; /* lo: h1 * r0 */
137 #define POLY1305_BLOCK_PART10() \
138 mul POLY_R_X0_LO, POLY_R_H0, POLY_R_R0; /* lo: h0 * r0 */
139 #define POLY1305_BLOCK_PART11() \
140 umulh POLY_R_X1_HI, POLY_R_H0, POLY_R_R1; /* hi: h0 * r1 */
141 #define POLY1305_BLOCK_PART12() \
142 adds POLY_R_X1_LO, POLY_R_X1_LO, POLY_TMP0;
143 #define POLY1305_BLOCK_PART13() \
144 umulh POLY_TMP1, POLY_R_H1, POLY_R_R0; /* hi: h1 * r0 */
145 #define POLY1305_BLOCK_PART14() \
146 mul POLY_TMP2, POLY_R_H1, POLY_R_R1_MUL5; /* lo: h1 * r1 mod 2^130-5 */
147 #define POLY1305_BLOCK_PART15() \
148 umulh POLY_R_X0_HI, POLY_R_H0, POLY_R_R0; /* hi: h0 * r0 */
149 #define POLY1305_BLOCK_PART16() \
150 adc POLY_R_X1_HI, POLY_R_X1_HI, POLY_TMP1;
151 #define POLY1305_BLOCK_PART17() \
152 umulh POLY_TMP3, POLY_R_H1, POLY_R_R1_MUL5; /* hi: h1 * r1 mod 2^130-5 */
153 #define POLY1305_BLOCK_PART18() \
154 adds POLY_R_X0_LO, POLY_R_X0_LO, POLY_TMP2;
155 #define POLY1305_BLOCK_PART19() \
156 mul POLY_R_H1, POLY_R_H2, POLY_R_R1_MUL5; /* h2 * r1 mod 2^130-5 */
157 #define POLY1305_BLOCK_PART20() \
158 adc POLY_R_X0_HI, POLY_R_X0_HI, POLY_TMP3;
159 #define POLY1305_BLOCK_PART21() \
160 mul POLY_R_H2, POLY_R_H2, POLY_R_R0; /* h2 * r0 */
161 #define POLY1305_BLOCK_PART22() \
162 adds POLY_R_H1, POLY_R_H1, POLY_R_X1_LO;
163 #define POLY1305_BLOCK_PART23() \
164 adc POLY_R_H0, POLY_R_H2, POLY_R_X1_HI;
165
166 #define POLY1305_BLOCK_PART24() \
167 /* carry propagation */ \
168 and POLY_R_H2, POLY_R_H0, #3;
169 #define POLY1305_BLOCK_PART25() \
170 lsr POLY_R_H0, POLY_R_H0, #2;
171 #define POLY1305_BLOCK_PART26() \
172 add POLY_R_H0, POLY_R_H0, POLY_R_H0, lsl #2;
173 #define POLY1305_BLOCK_PART27() \
174 adds POLY_R_H0, POLY_R_H0, POLY_R_X0_LO;
175 #define POLY1305_BLOCK_PART28() \
176 adcs POLY_R_H1, POLY_R_H1, POLY_R_X0_HI;
177 #define POLY1305_BLOCK_PART29() \
178 adc POLY_R_H2d, POLY_R_H2d, wzr;
179
180 //#define TESTING_POLY1305_ASM
181 #ifdef TESTING_POLY1305_ASM
182 /* for testing only. */
183 .align 3
184 .globl _gcry_poly1305_aarch64_blocks1
185 ELF(.type _gcry_poly1305_aarch64_blocks1,%function;)
186 _gcry_poly1305_aarch64_blocks1:
187 /* input:
188 * x0: poly1305-state
189 * x1: src
190 * x2: nblks
191 */
192 CFI_STARTPROC()
193 POLY1305_PUSH_REGS();
194
195 mov POLY_RSTATE, x0;
196 mov POLY_RSRC, x1;
197
198 POLY1305_LOAD_STATE();
199
200 .L_gcry_poly1305_aarch64_loop1:
201 POLY1305_BLOCK_PART1(0 * 16);
202 POLY1305_BLOCK_PART2(0 * 16);
203 add POLY_RSRC, POLY_RSRC, #16;
204 POLY1305_BLOCK_PART3();
205 POLY1305_BLOCK_PART4();
206 POLY1305_BLOCK_PART5();
207 POLY1305_BLOCK_PART6();
208 POLY1305_BLOCK_PART7();
209 POLY1305_BLOCK_PART8();
210 POLY1305_BLOCK_PART9();
211 POLY1305_BLOCK_PART10();
212 POLY1305_BLOCK_PART11();
213 POLY1305_BLOCK_PART12();
214 POLY1305_BLOCK_PART13();
215 POLY1305_BLOCK_PART14();
216 POLY1305_BLOCK_PART15();
217 POLY1305_BLOCK_PART16();
218 POLY1305_BLOCK_PART17();
219 POLY1305_BLOCK_PART18();
220 POLY1305_BLOCK_PART19();
221 POLY1305_BLOCK_PART20();
222 POLY1305_BLOCK_PART21();
223 POLY1305_BLOCK_PART22();
224 POLY1305_BLOCK_PART23();
225 POLY1305_BLOCK_PART24();
226 POLY1305_BLOCK_PART25();
227 POLY1305_BLOCK_PART26();
228 POLY1305_BLOCK_PART27();
229 POLY1305_BLOCK_PART28();
230 POLY1305_BLOCK_PART29();
231
232 subs x2, x2, #1;
233 b.ne .L_gcry_poly1305_aarch64_loop1;
234
235 POLY1305_STORE_STATE();
236
237 mov x0, #0;
238
239 POLY1305_POP_REGS();
240 ret_spec_stop;
241 CFI_ENDPROC()
242 ELF(.size _gcry_poly1305_aarch64_blocks1, .-_gcry_poly1305_aarch64_blocks1;)
243 #endif
244
245 #endif /* GCRY_ASM_POLY1305_AARCH64_H */