1 /* SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause */
3 // This file is dual-licensed, meaning that you can use it under your
4 // choice of either of the following two licenses:
6 // Copyright 2023 The OpenSSL Project Authors. All Rights Reserved.
8 // Licensed under the Apache License 2.0 (the "License"). You can obtain
9 // a copy in the file LICENSE in the source distribution or at
10 // https://www.openssl.org/source/license.html
14 // Copyright (c) 2023, Christoph Müllner <christoph.muellner@vrull.eu>
15 // Copyright (c) 2023, Phoebe Chen <phoebe.chen@sifive.com>
16 // Copyright 2024 Google LLC
17 // All rights reserved.
19 // Redistribution and use in source and binary forms, with or without
20 // modification, are permitted provided that the following conditions
22 // 1. Redistributions of source code must retain the above copyright
23 // notice, this list of conditions and the following disclaimer.
24 // 2. Redistributions in binary form must reproduce the above copyright
25 // notice, this list of conditions and the following disclaimer in the
26 // documentation and/or other materials provided with the distribution.
28 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 // The generated code of this file depends on the following RISC-V extensions:
42 // - RISC-V Vector ('V') with VLEN >= 128
43 // - RISC-V Vector SHA-2 Secure Hash extension ('Zvknha' or 'Zvknhb')
44 // - RISC-V Vector Cryptography Bit-manipulation extension ('Zvkb')
46 #include <linux/cfi_types.h>
49 .option arch, +zvknha, +zvkb
85 // Do 4 rounds of SHA-256. w0 contains the current 4 message schedule words.
87 // If not all the message schedule words have been computed yet, then this also
88 // computes 4 more message schedule words. w1-w3 contain the next 3 groups of 4
89 // message schedule words; this macro computes the group after w3 and writes it
90 // to w0. This means that the next (w0, w1, w2, w3) is the current (w1, w2, w3,
91 // w0), so the caller must cycle through the registers accordingly.
92 .macro sha256_4rounds last, k, w0, w1, w2, w3
94 vsha2cl.vv HGDC, FEBA, VTMP
95 vsha2ch.vv FEBA, HGDC, VTMP
97 vmerge.vvm VTMP, \w2, \w1, MASK
98 vsha2ms.vv \w0, VTMP, \w3
102 .macro sha256_16rounds last, k0, k1, k2, k3
103 sha256_4rounds \last, \k0, W0, W1, W2, W3
104 sha256_4rounds \last, \k1, W1, W2, W3, W0
105 sha256_4rounds \last, \k2, W2, W3, W0, W1
106 sha256_4rounds \last, \k3, W3, W0, W1, W2
109 // void sha256_transform_zvknha_or_zvknhb_zvkb(u32 state[8], const u8 *data,
111 SYM_TYPED_FUNC_START(sha256_transform_zvknha_or_zvknhb_zvkb)
113 // Load the round constants into K0-K15.
114 vsetivli zero, 4, e32, m1, ta, ma
148 // Setup mask for the vmerge to replace the first word (idx==0) in
149 // message scheduling. There are 4 words, so an 8-bit mask suffices.
150 vsetivli zero, 1, e8, m1, ta, ma
153 // Load the state. The state is stored as {a,b,c,d,e,f,g,h}, but we
154 // need {f,e,b,a},{h,g,d,c}. The dst vtype is e32m1 and the index vtype
155 // is e8mf4. We use index-load with the i8 indices {20, 16, 4, 0},
156 // loaded using the 32-bit little endian value 0x00041014.
158 vsetivli zero, 1, e32, m1, ta, ma
160 addi STATEP_C, STATEP, 8
161 vsetivli zero, 4, e32, m1, ta, ma
162 vluxei8.v FEBA, (STATEP), INDICES
163 vluxei8.v HGDC, (STATEP_C), INDICES
166 addi NUM_BLOCKS, NUM_BLOCKS, -1
168 // Save the previous state, as it's needed later.
169 vmv.v.v PREV_FEBA, FEBA
170 vmv.v.v PREV_HGDC, HGDC
172 // Load the next 512-bit message block and endian-swap each 32-bit word.
186 // Do the 64 rounds of SHA-256.
187 sha256_16rounds 0, K0, K1, K2, K3
188 sha256_16rounds 0, K4, K5, K6, K7
189 sha256_16rounds 0, K8, K9, K10, K11
190 sha256_16rounds 1, K12, K13, K14, K15
192 // Add the previous state.
193 vadd.vv FEBA, FEBA, PREV_FEBA
194 vadd.vv HGDC, HGDC, PREV_HGDC
196 // Repeat if more blocks remain.
197 bnez NUM_BLOCKS, .Lnext_block
199 // Store the new state and return.
200 vsuxei8.v FEBA, (STATEP), INDICES
201 vsuxei8.v HGDC, (STATEP_C), INDICES
203 SYM_FUNC_END(sha256_transform_zvknha_or_zvknhb_zvkb)
209 .word 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5
210 .word 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5
211 .word 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3
212 .word 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174
213 .word 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc
214 .word 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da
215 .word 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7
216 .word 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967
217 .word 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13
218 .word 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85
219 .word 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3
220 .word 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070
221 .word 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5
222 .word 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3
223 .word 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208
224 .word 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2