Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/sha/asm/sha256-loongarch64.pl

raw
1 #! /usr/bin/env perl
2 # This file is dual-licensed, meaning that you can use it under your
3 # choice of either of the following two licenses:
4 #
5 # Copyright 2025 The OpenSSL Project Authors. All Rights Reserved.
6 #
7 # Licensed under the Apache License 2.0 (the "License"). You can obtain
8 # a copy in the file LICENSE in the source distribution or at
9 # https://www.openssl.org/source/license.html
10 #
11 # or
12 #
13 # Copyright (c) 2025, Julian Zhu <[email protected]>
14 # All rights reserved.
15 #
16 # Redistribution and use in source and binary forms, with or without
17 # modification, are permitted provided that the following conditions
18 # are met:
19 # 1. Redistributions of source code must retain the above copyright
20 # notice, this list of conditions and the following disclaimer.
21 # 2. Redistributions in binary form must reproduce the above copyright
22 # notice, this list of conditions and the following disclaimer in the
23 # documentation and/or other materials provided with the distribution.
24 #
25 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
37 use strict;
38 use warnings;
39
40 # $output is the last argument if it looks like a file (it has an extension)
41 # $flavour is the first argument if it doesn't look like a file
42 my $output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
43 my $flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
44
45 $output and open STDOUT,">$output";
46
47 my $code=<<___;
48 .text
49 ___
50
51 my $K256 = "K256";
52
53 # Function arguments
54 my ($zero,$ra,$tp,$sp,$fp)=map("\$r$_",(0..3,22));
55 my ($a0,$a1,$a2,$a3,$a4,$a5,$a6,$a7)=map("\$r$_",(4..11));
56 my ($t0,$t1,$t2,$t3,$t4,$t5,$t6,$t7,$t8,$x)=map("\$r$_",(12..21));
57 my ($s0,$s1,$s2,$s3,$s4,$s5,$s6,$s7,$s8)=map("\$r$_",(23..31));
58
59 my ($INP, $LEN, $ADDR) = ($a1, $a2, $sp);
60 my ($KT, $T1, $T2, $T3, $T4, $T5, $T6) = ($t0, $t1, $t2, $t3, $t4, $t5, $t6);
61 my ($A, $B, $C, $D ,$E ,$F ,$G ,$H) = ($s0, $s1, $s2, $s3, $s4, $s5, $s6, $s7);
62
63 sub MSGSCHEDULE0 {
64 my ($index) = @_;
65 my $code=<<___;
66 ld.w $T1, $INP, 4*$index
67 revb.2w $T1, $T1
68 st.w $T1, $ADDR, 4*$index
69 ___
70 return $code;
71 }
72
73 sub MSGSCHEDULE1 {
74 my ($index) = @_;
75 my $code=<<___;
76 ld.w $T1, $ADDR, (($index-2)&0x0f)*4
77 ld.w $T2, $ADDR, (($index-15)&0x0f)*4
78 ld.w $T3, $ADDR, (($index-7)&0x0f)*4
79 ld.w $T4, $ADDR, ($index&0x0f)*4
80 rotri.w $T5, $T1, 17
81 rotri.w $T6, $T1, 19
82 srli.w $T1, $T1, 10
83 xor $T1, $T1, $T5
84 xor $T1, $T1, $T6
85 add.w $T1, $T1, $T3
86 rotri.w $T5, $T2, 7
87 rotri.w $T6, $T2, 18
88 srli.w $T2, $T2, 3
89 xor $T2, $T2, $T5
90 xor $T2, $T2, $T6
91 add.w $T1, $T1, $T2
92 add.w $T1, $T1, $T4
93 st.w $T1, $ADDR, ($index&0x0f)*4
94 ___
95 return $code;
96 }
97
98 sub sha256_T1 {
99 my ($index, $e, $f, $g, $h) = @_;
100 my $code=<<___;
101 ld.w $T4, $KT, 4*$index
102 add.w $h, $h, $T1
103 add.w $h, $h, $T4
104 rotri.w $T2, $e, 6
105 rotri.w $T3, $e, 11
106 rotri.w $T4, $e, 25
107 xor $T2, $T2, $T3
108 xor $T1, $f, $g
109 xor $T2, $T2, $T4
110 and $T1, $T1, $e
111 add.w $h, $h, $T2
112 xor $T1, $T1, $g
113 add.w $T1, $T1, $h
114 ___
115 return $code;
116 }
117
118 sub sha256_T2 {
119 my ($a, $b, $c) = @_;
120 my $code=<<___;
121 rotri.w $T2, $a, 2
122 rotri.w $T3, $a, 13
123 rotri.w $T4, $a, 22
124 xor $T2, $T2, $T3
125 xor $T2, $T2, $T4
126 xor $T4, $b, $c
127 and $T3, $b, $c
128 and $T4, $T4, $a
129 xor $T4, $T4, $T3
130 add.w $T2, $T2, $T4
131 ___
132 return $code;
133 }
134
135 sub SHA256ROUND {
136 my ($index, $a, $b, $c, $d, $e, $f, $g, $h) = @_;
137 my $code=<<___;
138 @{[sha256_T1 $index, $e, $f, $g, $h]}
139 @{[sha256_T2 $a, $b, $c]}
140 add.w $d, $d, $T1
141 add.w $h, $T2, $T1
142 ___
143 return $code;
144 }
145
146 sub SHA256ROUND0 {
147 my ($index, $a, $b, $c, $d, $e, $f, $g, $h) = @_;
148 my $code=<<___;
149 @{[MSGSCHEDULE0 $index]}
150 @{[SHA256ROUND $index, $a, $b, $c, $d, $e, $f, $g, $h]}
151 ___
152 return $code;
153 }
154
155 sub SHA256ROUND1 {
156 my ($index, $a, $b, $c, $d, $e, $f, $g, $h) = @_;
157 my $code=<<___;
158 @{[MSGSCHEDULE1 $index]}
159 @{[SHA256ROUND $index, $a, $b, $c, $d, $e, $f, $g, $h]}
160 ___
161 return $code;
162 }
163
164 ################################################################################
165 # void sha256_block_data_order(void *c, const void *p, size_t len)
166 $code .= <<___;
167 .p2align 3
168 .globl sha256_block_data_order
169 .type sha256_block_data_order,\@function
170 sha256_block_data_order:
171
172 addi.d $sp, $sp, -80
173
174 st.d $s0, $sp, 0
175 st.d $s1, $sp, 8
176 st.d $s2, $sp, 16
177 st.d $s3, $sp, 24
178 st.d $s4, $sp, 32
179 st.d $s5, $sp, 40
180 st.d $s6, $sp, 48
181 st.d $s7, $sp, 56
182 st.d $s8, $sp, 64
183 st.d $fp, $sp, 72
184
185 addi.d $sp, $sp, -64
186
187 la $KT, $K256
188
189 # load ctx
190 ld.w $A, $a0, 0
191 ld.w $B, $a0, 4
192 ld.w $C, $a0, 8
193 ld.w $D, $a0, 12
194 ld.w $E, $a0, 16
195 ld.w $F, $a0, 20
196 ld.w $G, $a0, 24
197 ld.w $H, $a0, 28
198
199 L_round_loop:
200 # Decrement length by 1
201 addi.d $LEN, $LEN, -1
202
203 @{[SHA256ROUND0 0, $A, $B, $C, $D, $E, $F, $G, $H]}
204 @{[SHA256ROUND0 1, $H, $A, $B, $C, $D, $E, $F, $G]}
205 @{[SHA256ROUND0 2, $G, $H, $A, $B, $C, $D, $E, $F]}
206 @{[SHA256ROUND0 3, $F, $G, $H, $A, $B, $C, $D, $E]}
207
208 @{[SHA256ROUND0 4, $E, $F, $G, $H, $A, $B, $C, $D]}
209 @{[SHA256ROUND0 5, $D, $E, $F, $G, $H, $A, $B, $C]}
210 @{[SHA256ROUND0 6, $C, $D, $E, $F, $G, $H, $A, $B]}
211 @{[SHA256ROUND0 7, $B, $C, $D, $E, $F, $G, $H, $A]}
212
213 @{[SHA256ROUND0 8, $A, $B, $C, $D, $E, $F, $G, $H]}
214 @{[SHA256ROUND0 9, $H, $A, $B, $C, $D, $E, $F, $G]}
215 @{[SHA256ROUND0 10, $G, $H, $A, $B, $C, $D, $E, $F]}
216 @{[SHA256ROUND0 11, $F, $G, $H, $A, $B, $C, $D, $E]}
217
218 @{[SHA256ROUND0 12, $E, $F, $G, $H, $A, $B, $C, $D]}
219 @{[SHA256ROUND0 13, $D, $E, $F, $G, $H, $A, $B, $C]}
220 @{[SHA256ROUND0 14, $C, $D, $E, $F, $G, $H, $A, $B]}
221 @{[SHA256ROUND0 15, $B, $C, $D, $E, $F, $G, $H, $A]}
222
223 @{[SHA256ROUND1 16, $A, $B, $C, $D, $E, $F, $G, $H]}
224 @{[SHA256ROUND1 17, $H, $A, $B, $C, $D, $E, $F, $G]}
225 @{[SHA256ROUND1 18, $G, $H, $A, $B, $C, $D, $E, $F]}
226 @{[SHA256ROUND1 19, $F, $G, $H, $A, $B, $C, $D, $E]}
227
228 @{[SHA256ROUND1 20, $E, $F, $G, $H, $A, $B, $C, $D]}
229 @{[SHA256ROUND1 21, $D, $E, $F, $G, $H, $A, $B, $C]}
230 @{[SHA256ROUND1 22, $C, $D, $E, $F, $G, $H, $A, $B]}
231 @{[SHA256ROUND1 23, $B, $C, $D, $E, $F, $G, $H, $A]}
232
233 @{[SHA256ROUND1 24, $A, $B, $C, $D, $E, $F, $G, $H]}
234 @{[SHA256ROUND1 25, $H, $A, $B, $C, $D, $E, $F, $G]}
235 @{[SHA256ROUND1 26, $G, $H, $A, $B, $C, $D, $E, $F]}
236 @{[SHA256ROUND1 27, $F, $G, $H, $A, $B, $C, $D, $E]}
237
238 @{[SHA256ROUND1 28, $E, $F, $G, $H, $A, $B, $C, $D]}
239 @{[SHA256ROUND1 29, $D, $E, $F, $G, $H, $A, $B, $C]}
240 @{[SHA256ROUND1 30, $C, $D, $E, $F, $G, $H, $A, $B]}
241 @{[SHA256ROUND1 31, $B, $C, $D, $E, $F, $G, $H, $A]}
242
243 @{[SHA256ROUND1 32, $A, $B, $C, $D, $E, $F, $G, $H]}
244 @{[SHA256ROUND1 33, $H, $A, $B, $C, $D, $E, $F, $G]}
245 @{[SHA256ROUND1 34, $G, $H, $A, $B, $C, $D, $E, $F]}
246 @{[SHA256ROUND1 35, $F, $G, $H, $A, $B, $C, $D, $E]}
247
248 @{[SHA256ROUND1 36, $E, $F, $G, $H, $A, $B, $C, $D]}
249 @{[SHA256ROUND1 37, $D, $E, $F, $G, $H, $A, $B, $C]}
250 @{[SHA256ROUND1 38, $C, $D, $E, $F, $G, $H, $A, $B]}
251 @{[SHA256ROUND1 39, $B, $C, $D, $E, $F, $G, $H, $A]}
252
253 @{[SHA256ROUND1 40, $A, $B, $C, $D, $E, $F, $G, $H]}
254 @{[SHA256ROUND1 41, $H, $A, $B, $C, $D, $E, $F, $G]}
255 @{[SHA256ROUND1 42, $G, $H, $A, $B, $C, $D, $E, $F]}
256 @{[SHA256ROUND1 43, $F, $G, $H, $A, $B, $C, $D, $E]}
257
258 @{[SHA256ROUND1 44, $E, $F, $G, $H, $A, $B, $C, $D]}
259 @{[SHA256ROUND1 45, $D, $E, $F, $G, $H, $A, $B, $C]}
260 @{[SHA256ROUND1 46, $C, $D, $E, $F, $G, $H, $A, $B]}
261 @{[SHA256ROUND1 47, $B, $C, $D, $E, $F, $G, $H, $A]}
262
263 @{[SHA256ROUND1 48, $A, $B, $C, $D, $E, $F, $G, $H]}
264 @{[SHA256ROUND1 49, $H, $A, $B, $C, $D, $E, $F, $G]}
265 @{[SHA256ROUND1 50, $G, $H, $A, $B, $C, $D, $E, $F]}
266 @{[SHA256ROUND1 51, $F, $G, $H, $A, $B, $C, $D, $E]}
267
268 @{[SHA256ROUND1 52, $E, $F, $G, $H, $A, $B, $C, $D]}
269 @{[SHA256ROUND1 53, $D, $E, $F, $G, $H, $A, $B, $C]}
270 @{[SHA256ROUND1 54, $C, $D, $E, $F, $G, $H, $A, $B]}
271 @{[SHA256ROUND1 55, $B, $C, $D, $E, $F, $G, $H, $A]}
272
273 @{[SHA256ROUND1 56, $A, $B, $C, $D, $E, $F, $G, $H]}
274 @{[SHA256ROUND1 57, $H, $A, $B, $C, $D, $E, $F, $G]}
275 @{[SHA256ROUND1 58, $G, $H, $A, $B, $C, $D, $E, $F]}
276 @{[SHA256ROUND1 59, $F, $G, $H, $A, $B, $C, $D, $E]}
277
278 @{[SHA256ROUND1 60, $E, $F, $G, $H, $A, $B, $C, $D]}
279 @{[SHA256ROUND1 61, $D, $E, $F, $G, $H, $A, $B, $C]}
280 @{[SHA256ROUND1 62, $C, $D, $E, $F, $G, $H, $A, $B]}
281 @{[SHA256ROUND1 63, $B, $C, $D, $E, $F, $G, $H, $A]}
282
283 ld.w $T1, $a0, 0
284 ld.w $T2, $a0, 4
285 ld.w $T3, $a0, 8
286 ld.w $T4, $a0, 12
287
288 add.w $A, $A, $T1
289 add.w $B, $B, $T2
290 add.w $C, $C, $T3
291 add.w $D, $D, $T4
292
293 st.w $A, $a0, 0
294 st.w $B, $a0, 4
295 st.w $C, $a0, 8
296 st.w $D, $a0, 12
297
298 ld.w $T1, $a0, 16
299 ld.w $T2, $a0, 20
300 ld.w $T3, $a0, 24
301 ld.w $T4, $a0, 28
302
303 add.w $E, $E, $T1
304 add.w $F, $F, $T2
305 add.w $G, $G, $T3
306 add.w $H, $H, $T4
307
308 st.w $E, $a0, 16
309 st.w $F, $a0, 20
310 st.w $G, $a0, 24
311 st.w $H, $a0, 28
312
313 addi.d $INP, $INP, 64
314
315 bnez $LEN, L_round_loop
316
317 addi.d $sp, $sp, 64
318
319 ld.d $s0, $sp, 0
320 ld.d $s1, $sp, 8
321 ld.d $s2, $sp, 16
322 ld.d $s3, $sp, 24
323 ld.d $s4, $sp, 32
324 ld.d $s5, $sp, 40
325 ld.d $s6, $sp, 48
326 ld.d $s7, $sp, 56
327 ld.d $s8, $sp, 64
328 ld.d $fp, $sp, 72
329
330 addi.d $sp, $sp, 80
331
332 ret
333 .size sha256_block_data_order,.-sha256_block_data_order
334
335 .section .rodata
336 .p2align 3
337 .type $K256,\@object
338 $K256:
339 .word 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5
340 .word 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5
341 .word 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3
342 .word 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174
343 .word 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc
344 .word 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da
345 .word 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7
346 .word 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967
347 .word 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13
348 .word 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85
349 .word 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3
350 .word 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070
351 .word 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5
352 .word 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3
353 .word 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208
354 .word 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
355 .size $K256,.-$K256
356 ___
357
358 print $code;
359
360 close STDOUT or die "error closing STDOUT: $!";