xref: /linux/arch/riscv/crypto/sha512-riscv64-zvknhb-zvkb.S (revision 79790b6818e96c58fe2bffee1b418c16e64e7b80)
1*b3415925SJerry Shih/* SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause */
2*b3415925SJerry Shih//
3*b3415925SJerry Shih// This file is dual-licensed, meaning that you can use it under your
4*b3415925SJerry Shih// choice of either of the following two licenses:
5*b3415925SJerry Shih//
6*b3415925SJerry Shih// Copyright 2023 The OpenSSL Project Authors. All Rights Reserved.
7*b3415925SJerry Shih//
8*b3415925SJerry Shih// Licensed under the Apache License 2.0 (the "License"). You can obtain
9*b3415925SJerry Shih// a copy in the file LICENSE in the source distribution or at
10*b3415925SJerry Shih// https://www.openssl.org/source/license.html
11*b3415925SJerry Shih//
12*b3415925SJerry Shih// or
13*b3415925SJerry Shih//
14*b3415925SJerry Shih// Copyright (c) 2023, Christoph Müllner <christoph.muellner@vrull.eu>
15*b3415925SJerry Shih// Copyright (c) 2023, Phoebe Chen <phoebe.chen@sifive.com>
16*b3415925SJerry Shih// Copyright 2024 Google LLC
17*b3415925SJerry Shih// All rights reserved.
18*b3415925SJerry Shih//
19*b3415925SJerry Shih// Redistribution and use in source and binary forms, with or without
20*b3415925SJerry Shih// modification, are permitted provided that the following conditions
21*b3415925SJerry Shih// are met:
22*b3415925SJerry Shih// 1. Redistributions of source code must retain the above copyright
23*b3415925SJerry Shih//    notice, this list of conditions and the following disclaimer.
24*b3415925SJerry Shih// 2. Redistributions in binary form must reproduce the above copyright
25*b3415925SJerry Shih//    notice, this list of conditions and the following disclaimer in the
26*b3415925SJerry Shih//    documentation and/or other materials provided with the distribution.
27*b3415925SJerry Shih//
28*b3415925SJerry Shih// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29*b3415925SJerry Shih// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30*b3415925SJerry Shih// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31*b3415925SJerry Shih// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32*b3415925SJerry Shih// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33*b3415925SJerry Shih// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34*b3415925SJerry Shih// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35*b3415925SJerry Shih// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36*b3415925SJerry Shih// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37*b3415925SJerry Shih// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38*b3415925SJerry Shih// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39*b3415925SJerry Shih
40*b3415925SJerry Shih// The generated code of this file depends on the following RISC-V extensions:
41*b3415925SJerry Shih// - RV64I
42*b3415925SJerry Shih// - RISC-V Vector ('V') with VLEN >= 128
43*b3415925SJerry Shih// - RISC-V Vector SHA-2 Secure Hash extension ('Zvknhb')
44*b3415925SJerry Shih// - RISC-V Vector Cryptography Bit-manipulation extension ('Zvkb')
45*b3415925SJerry Shih
46*b3415925SJerry Shih#include <linux/cfi_types.h>
47*b3415925SJerry Shih
48*b3415925SJerry Shih.text
49*b3415925SJerry Shih.option arch, +zvknhb, +zvkb
50*b3415925SJerry Shih
51*b3415925SJerry Shih#define STATEP		a0
52*b3415925SJerry Shih#define DATA		a1
53*b3415925SJerry Shih#define NUM_BLOCKS	a2
54*b3415925SJerry Shih
55*b3415925SJerry Shih#define STATEP_C	a3
56*b3415925SJerry Shih#define K		a4
57*b3415925SJerry Shih
58*b3415925SJerry Shih#define MASK		v0
59*b3415925SJerry Shih#define INDICES		v1
60*b3415925SJerry Shih#define W0		v10	// LMUL=2
61*b3415925SJerry Shih#define W1		v12	// LMUL=2
62*b3415925SJerry Shih#define W2		v14	// LMUL=2
63*b3415925SJerry Shih#define W3		v16	// LMUL=2
64*b3415925SJerry Shih#define VTMP		v20	// LMUL=2
65*b3415925SJerry Shih#define FEBA		v22	// LMUL=2
66*b3415925SJerry Shih#define HGDC		v24	// LMUL=2
67*b3415925SJerry Shih#define PREV_FEBA	v26	// LMUL=2
68*b3415925SJerry Shih#define PREV_HGDC	v28	// LMUL=2
69*b3415925SJerry Shih
70*b3415925SJerry Shih// Do 4 rounds of SHA-512.  w0 contains the current 4 message schedule words.
71*b3415925SJerry Shih//
72*b3415925SJerry Shih// If not all the message schedule words have been computed yet, then this also
73*b3415925SJerry Shih// computes 4 more message schedule words.  w1-w3 contain the next 3 groups of 4
74*b3415925SJerry Shih// message schedule words; this macro computes the group after w3 and writes it
75*b3415925SJerry Shih// to w0.  This means that the next (w0, w1, w2, w3) is the current (w1, w2, w3,
76*b3415925SJerry Shih// w0), so the caller must cycle through the registers accordingly.
77*b3415925SJerry Shih.macro	sha512_4rounds	last, w0, w1, w2, w3
78*b3415925SJerry Shih	vle64.v		VTMP, (K)
79*b3415925SJerry Shih	addi		K, K, 32
80*b3415925SJerry Shih	vadd.vv		VTMP, VTMP, \w0
81*b3415925SJerry Shih	vsha2cl.vv	HGDC, FEBA, VTMP
82*b3415925SJerry Shih	vsha2ch.vv	FEBA, HGDC, VTMP
83*b3415925SJerry Shih.if !\last
84*b3415925SJerry Shih	vmerge.vvm	VTMP, \w2, \w1, MASK
85*b3415925SJerry Shih	vsha2ms.vv	\w0, VTMP, \w3
86*b3415925SJerry Shih.endif
87*b3415925SJerry Shih.endm
88*b3415925SJerry Shih
89*b3415925SJerry Shih.macro	sha512_16rounds	last
90*b3415925SJerry Shih	sha512_4rounds	\last, W0, W1, W2, W3
91*b3415925SJerry Shih	sha512_4rounds	\last, W1, W2, W3, W0
92*b3415925SJerry Shih	sha512_4rounds	\last, W2, W3, W0, W1
93*b3415925SJerry Shih	sha512_4rounds	\last, W3, W0, W1, W2
94*b3415925SJerry Shih.endm
95*b3415925SJerry Shih
96*b3415925SJerry Shih// void sha512_transform_zvknhb_zvkb(u64 state[8], const u8 *data,
97*b3415925SJerry Shih//				     int num_blocks);
98*b3415925SJerry ShihSYM_TYPED_FUNC_START(sha512_transform_zvknhb_zvkb)
99*b3415925SJerry Shih
100*b3415925SJerry Shih	// Setup mask for the vmerge to replace the first word (idx==0) in
101*b3415925SJerry Shih	// message scheduling.  There are 4 words, so an 8-bit mask suffices.
102*b3415925SJerry Shih	vsetivli	zero, 1, e8, m1, ta, ma
103*b3415925SJerry Shih	vmv.v.i		MASK, 0x01
104*b3415925SJerry Shih
105*b3415925SJerry Shih	// Load the state.  The state is stored as {a,b,c,d,e,f,g,h}, but we
106*b3415925SJerry Shih	// need {f,e,b,a},{h,g,d,c}.  The dst vtype is e64m2 and the index vtype
107*b3415925SJerry Shih	// is e8mf4.  We use index-load with the i8 indices {40, 32, 8, 0},
108*b3415925SJerry Shih	// loaded using the 32-bit little endian value 0x00082028.
109*b3415925SJerry Shih	li		t0, 0x00082028
110*b3415925SJerry Shih	vsetivli	zero, 1, e32, m1, ta, ma
111*b3415925SJerry Shih	vmv.v.x		INDICES, t0
112*b3415925SJerry Shih	addi		STATEP_C, STATEP, 16
113*b3415925SJerry Shih	vsetivli	zero, 4, e64, m2, ta, ma
114*b3415925SJerry Shih	vluxei8.v	FEBA, (STATEP), INDICES
115*b3415925SJerry Shih	vluxei8.v	HGDC, (STATEP_C), INDICES
116*b3415925SJerry Shih
117*b3415925SJerry Shih.Lnext_block:
118*b3415925SJerry Shih	la		K, K512
119*b3415925SJerry Shih	addi		NUM_BLOCKS, NUM_BLOCKS, -1
120*b3415925SJerry Shih
121*b3415925SJerry Shih	// Save the previous state, as it's needed later.
122*b3415925SJerry Shih	vmv.v.v		PREV_FEBA, FEBA
123*b3415925SJerry Shih	vmv.v.v		PREV_HGDC, HGDC
124*b3415925SJerry Shih
125*b3415925SJerry Shih	// Load the next 1024-bit message block and endian-swap each 64-bit word
126*b3415925SJerry Shih	vle64.v		W0, (DATA)
127*b3415925SJerry Shih	vrev8.v		W0, W0
128*b3415925SJerry Shih	addi		DATA, DATA, 32
129*b3415925SJerry Shih	vle64.v		W1, (DATA)
130*b3415925SJerry Shih	vrev8.v		W1, W1
131*b3415925SJerry Shih	addi		DATA, DATA, 32
132*b3415925SJerry Shih	vle64.v		W2, (DATA)
133*b3415925SJerry Shih	vrev8.v		W2, W2
134*b3415925SJerry Shih	addi		DATA, DATA, 32
135*b3415925SJerry Shih	vle64.v		W3, (DATA)
136*b3415925SJerry Shih	vrev8.v		W3, W3
137*b3415925SJerry Shih	addi		DATA, DATA, 32
138*b3415925SJerry Shih
139*b3415925SJerry Shih	// Do the 80 rounds of SHA-512.
140*b3415925SJerry Shih	sha512_16rounds 0
141*b3415925SJerry Shih	sha512_16rounds 0
142*b3415925SJerry Shih	sha512_16rounds 0
143*b3415925SJerry Shih	sha512_16rounds 0
144*b3415925SJerry Shih	sha512_16rounds 1
145*b3415925SJerry Shih
146*b3415925SJerry Shih	// Add the previous state.
147*b3415925SJerry Shih	vadd.vv		FEBA, FEBA, PREV_FEBA
148*b3415925SJerry Shih	vadd.vv		HGDC, HGDC, PREV_HGDC
149*b3415925SJerry Shih
150*b3415925SJerry Shih	// Repeat if more blocks remain.
151*b3415925SJerry Shih	bnez		NUM_BLOCKS, .Lnext_block
152*b3415925SJerry Shih
153*b3415925SJerry Shih	// Store the new state and return.
154*b3415925SJerry Shih	vsuxei8.v	FEBA, (STATEP), INDICES
155*b3415925SJerry Shih	vsuxei8.v	HGDC, (STATEP_C), INDICES
156*b3415925SJerry Shih	ret
157*b3415925SJerry ShihSYM_FUNC_END(sha512_transform_zvknhb_zvkb)
158*b3415925SJerry Shih
159*b3415925SJerry Shih.section ".rodata"
160*b3415925SJerry Shih.p2align 3
161*b3415925SJerry Shih.type K512, @object
162*b3415925SJerry ShihK512:
163*b3415925SJerry Shih	.dword		0x428a2f98d728ae22, 0x7137449123ef65cd
164*b3415925SJerry Shih	.dword		0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc
165*b3415925SJerry Shih	.dword		0x3956c25bf348b538, 0x59f111f1b605d019
166*b3415925SJerry Shih	.dword		0x923f82a4af194f9b, 0xab1c5ed5da6d8118
167*b3415925SJerry Shih	.dword		0xd807aa98a3030242, 0x12835b0145706fbe
168*b3415925SJerry Shih	.dword		0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2
169*b3415925SJerry Shih	.dword		0x72be5d74f27b896f, 0x80deb1fe3b1696b1
170*b3415925SJerry Shih	.dword		0x9bdc06a725c71235, 0xc19bf174cf692694
171*b3415925SJerry Shih	.dword		0xe49b69c19ef14ad2, 0xefbe4786384f25e3
172*b3415925SJerry Shih	.dword		0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65
173*b3415925SJerry Shih	.dword		0x2de92c6f592b0275, 0x4a7484aa6ea6e483
174*b3415925SJerry Shih	.dword		0x5cb0a9dcbd41fbd4, 0x76f988da831153b5
175*b3415925SJerry Shih	.dword		0x983e5152ee66dfab, 0xa831c66d2db43210
176*b3415925SJerry Shih	.dword		0xb00327c898fb213f, 0xbf597fc7beef0ee4
177*b3415925SJerry Shih	.dword		0xc6e00bf33da88fc2, 0xd5a79147930aa725
178*b3415925SJerry Shih	.dword		0x06ca6351e003826f, 0x142929670a0e6e70
179*b3415925SJerry Shih	.dword		0x27b70a8546d22ffc, 0x2e1b21385c26c926
180*b3415925SJerry Shih	.dword		0x4d2c6dfc5ac42aed, 0x53380d139d95b3df
181*b3415925SJerry Shih	.dword		0x650a73548baf63de, 0x766a0abb3c77b2a8
182*b3415925SJerry Shih	.dword		0x81c2c92e47edaee6, 0x92722c851482353b
183*b3415925SJerry Shih	.dword		0xa2bfe8a14cf10364, 0xa81a664bbc423001
184*b3415925SJerry Shih	.dword		0xc24b8b70d0f89791, 0xc76c51a30654be30
185*b3415925SJerry Shih	.dword		0xd192e819d6ef5218, 0xd69906245565a910
186*b3415925SJerry Shih	.dword		0xf40e35855771202a, 0x106aa07032bbd1b8
187*b3415925SJerry Shih	.dword		0x19a4c116b8d2d0c8, 0x1e376c085141ab53
188*b3415925SJerry Shih	.dword		0x2748774cdf8eeb99, 0x34b0bcb5e19b48a8
189*b3415925SJerry Shih	.dword		0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb
190*b3415925SJerry Shih	.dword		0x5b9cca4f7763e373, 0x682e6ff3d6b2b8a3
191*b3415925SJerry Shih	.dword		0x748f82ee5defb2fc, 0x78a5636f43172f60
192*b3415925SJerry Shih	.dword		0x84c87814a1f0ab72, 0x8cc702081a6439ec
193*b3415925SJerry Shih	.dword		0x90befffa23631e28, 0xa4506cebde82bde9
194*b3415925SJerry Shih	.dword		0xbef9a3f7b2c67915, 0xc67178f2e372532b
195*b3415925SJerry Shih	.dword		0xca273eceea26619c, 0xd186b8c721c0c207
196*b3415925SJerry Shih	.dword		0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178
197*b3415925SJerry Shih	.dword		0x06f067aa72176fba, 0x0a637dc5a2c898a6
198*b3415925SJerry Shih	.dword		0x113f9804bef90dae, 0x1b710b35131c471b
199*b3415925SJerry Shih	.dword		0x28db77f523047d84, 0x32caab7b40c72493
200*b3415925SJerry Shih	.dword		0x3c9ebe0a15c9bebc, 0x431d67c49c100d4c
201*b3415925SJerry Shih	.dword		0x4cc5d4becb3e42b6, 0x597f299cfc657e2a
202*b3415925SJerry Shih	.dword		0x5fcb6fab3ad6faec, 0x6c44198c4a475817
203*b3415925SJerry Shih.size K512, . - K512
204