Debian Codegraph @

cipher/asm-poly1305-amd64.h

raw
1 /* asm-common-amd64.h - Poly1305 macros for AMD64 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_AMD64_H
22 #define GCRY_ASM_POLY1305_AMD64_H
23
24 #include "asm-common-amd64.h"
25
26 /**********************************************************************
27 poly1305 for stitched chacha20-poly1305 AMD64 implementations
28 **********************************************************************/
29
30 #define POLY_RSTATE %r8
31 #define POLY_RSRC %r9
32
33 #define POLY_R_H0 %rbx
34 #define POLY_R_H1 %rcx
35 #define POLY_R_H2 %r10
36 #define POLY_R_H2d %r10d
37 #define POLY_R_R0 %r11
38 #define POLY_R_R1_MUL5 %r12
39 #define POLY_R_X0_HI %r13
40 #define POLY_R_X0_LO %r14
41 #define POLY_R_X1_HI %r15
42 #define POLY_R_X1_LO %rsi
43
44 #define POLY_S_R0 (4 * 4 + 0 * 8)(POLY_RSTATE)
45 #define POLY_S_R1 (4 * 4 + 1 * 8)(POLY_RSTATE)
46 #define POLY_S_H0 (4 * 4 + 2 * 8 + 0 * 8)(POLY_RSTATE)
47 #define POLY_S_H1 (4 * 4 + 2 * 8 + 1 * 8)(POLY_RSTATE)
48 #define POLY_S_H2d (4 * 4 + 2 * 8 + 2 * 8)(POLY_RSTATE)
49
50 #define POLY1305_LOAD_STATE() \
51 movq POLY_S_H0, POLY_R_H0; \
52 movq POLY_S_H1, POLY_R_H1; \
53 movl POLY_S_H2d, POLY_R_H2d; \
54 movq POLY_S_R0, POLY_R_R0; \
55 movq POLY_S_R1, POLY_R_R1_MUL5; \
56 shrq $2, POLY_R_R1_MUL5; \
57 addq POLY_S_R1, POLY_R_R1_MUL5;
58
59 #define POLY1305_STORE_STATE() \
60 movq POLY_R_H0, POLY_S_H0; \
61 movq POLY_R_H1, POLY_S_H1; \
62 movl POLY_R_H2d, POLY_S_H2d;
63
64 /* a = h + m */
65 #define POLY1305_BLOCK_PART1(src_offset) \
66 addq ((src_offset) + 0 * 8)(POLY_RSRC), POLY_R_H0; \
67 adcq ((src_offset) + 1 * 8)(POLY_RSRC), POLY_R_H1; \
68 adcl $1, POLY_R_H2d; \
69 \
70 /* h = a * r (partial mod 2^130-5): */ \
71 \
72 /* h0 * r1 */ \
73 movq POLY_R_H0, %rax; \
74 mulq POLY_S_R1; \
75 movq %rax, POLY_R_X1_LO; \
76 movq %rdx, POLY_R_X1_HI;
77
78 #define POLY1305_BLOCK_PART2() \
79 \
80 /* h0 * r0 */ \
81 movq POLY_R_H0, %rax; \
82 mulq POLY_R_R0; \
83 movq %rax, POLY_R_X0_LO; \
84 movq %rdx, POLY_R_X0_HI;
85
86 #define POLY1305_BLOCK_PART3() \
87 \
88 /* h1 * r0 */ \
89 movq POLY_R_H1, %rax; \
90 mulq POLY_R_R0; \
91 addq %rax, POLY_R_X1_LO; \
92 adcq %rdx, POLY_R_X1_HI; \
93 \
94 /* h1 * r1 mod 2^130-5 */ \
95 movq POLY_R_R1_MUL5, %rax; \
96 mulq POLY_R_H1;
97
98 #define POLY1305_BLOCK_PART4() \
99 movq POLY_R_H2, POLY_R_H1; \
100 imulq POLY_R_R1_MUL5, POLY_R_H1; /* h2 * r1 mod 2^130-5 */ \
101 addq %rax, POLY_R_X0_LO; \
102 adcq %rdx, POLY_R_X0_HI; \
103 imulq POLY_R_R0, POLY_R_H2; /* h2 * r0 */ \
104 addq POLY_R_X1_LO, POLY_R_H1; \
105 adcq POLY_R_X1_HI, POLY_R_H2;
106
107 #define POLY1305_BLOCK_PART5() \
108 \
109 /* carry propagation */ \
110 movq POLY_R_H2, POLY_R_H0; \
111 andl $3, POLY_R_H2d; \
112 shrq $2, POLY_R_H0; \
113 leaq (POLY_R_H0, POLY_R_H0, 4), POLY_R_H0; \
114 addq POLY_R_X0_LO, POLY_R_H0; \
115 adcq POLY_R_X0_HI, POLY_R_H1; \
116 adcl $0, POLY_R_H2d;
117
118 #ifdef TESTING_POLY1305_ASM
119 /* for testing only, mixed C/asm poly1305.c is marginally faster (~2%). */
120 .align 8
121 .globl _gcry_poly1305_amd64_ssse3_blocks1
122 ELF(.type _gcry_poly1305_amd64_ssse3_blocks1,@function;)
123
124 _gcry_poly1305_amd64_ssse3_blocks1:
125 /* input:
126 * %rdi: poly1305-state
127 * %rsi: src
128 * %rdx: nblks
129 */
130 pushq %rbp;
131 movq %rsp, %rbp;
132
133 subq $(10 * 8), %rsp;
134 movq %rbx, (1 * 8)(%rsp);
135 movq %r12, (2 * 8)(%rsp);
136 movq %r13, (3 * 8)(%rsp);
137 movq %r14, (4 * 8)(%rsp);
138 movq %r15, (5 * 8)(%rsp);
139
140 movq %rdx, (8 * 8)(%rsp); # NBLKS
141
142 movq %rdi, POLY_RSTATE;
143 movq %rsi, POLY_RSRC;
144
145 POLY1305_LOAD_STATE();
146
147 .L_poly1:
148 POLY1305_BLOCK_PART1(0 * 16);
149 POLY1305_BLOCK_PART2();
150 POLY1305_BLOCK_PART3();
151 POLY1305_BLOCK_PART4();
152 POLY1305_BLOCK_PART5();
153
154 subq $1, (8 * 8)(%rsp); # NBLKS
155 leaq (16)(POLY_RSRC), POLY_RSRC;
156 jnz .L_poly1;
157
158 POLY1305_STORE_STATE();
159
160 movq (1 * 8)(%rsp), %rbx;
161 movq (2 * 8)(%rsp), %r12;
162 movq (3 * 8)(%rsp), %r13;
163 movq (4 * 8)(%rsp), %r14;
164 movq (5 * 8)(%rsp), %r15;
165
166 xorl %eax, %eax;
167 leave
168 ret;
169 #endif
170
171 #endif /* GCRY_ASM_POLY1305_AMD64_H */