xref: /freebsd/sys/crypto/sha2/sha256c_arm64.c (revision c7a063741720ef81d4caa4613242579d12f1d605)
1 /*-
2  * Copyright (c) 2021 The FreeBSD Foundation
3  *
4  * This software was developed by Andrew Turner under sponsorship from
5  * the FreeBSD Foundation.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/types.h>
33 
34 #include <arm_neon.h>
35 
36 #include "sha256c_impl.h"
37 
38 void __hidden
39 SHA256_Transform_arm64_impl(uint32_t * state, const unsigned char block[64],
40     const uint32_t K[64])
41 {
42 	uint32x4_t W[4];
43 	uint32x4_t S[2];
44 	uint32x4_t S_start[2];
45 	uint32x4_t K_tmp, S_tmp;
46 	int i;
47 
48 #define	A64_LOAD_W(x)							\
49     W[x] = vld1q_u32((const uint32_t *)(&block[(x) * 16]));		\
50     W[x] = vreinterpretq_u32_u8(vrev32q_u8(vreinterpretq_u8_u32(W[x])))
51 
52 	/* 1. Prepare the first part of the message schedule W. */
53 	A64_LOAD_W(0);
54 	A64_LOAD_W(1);
55 	A64_LOAD_W(2);
56 	A64_LOAD_W(3);
57 
58 	/* 2. Initialize working variables. */
59 	S[0] = vld1q_u32(&state[0]);
60 	S[1] = vld1q_u32(&state[4]);
61 
62 	S_start[0] = S[0];
63 	S_start[1] = S[1];
64 
65 	/* 3. Mix. */
66 	for (i = 0; i < 64; i += 16) {
67 #define	A64_RNDr(i, ii)							\
68     K_tmp = vaddq_u32(W[i], vld1q_u32(&K[ii + i * 4]));			\
69     S_tmp = vsha256hq_u32(S[0], S[1], K_tmp);				\
70     S[1] = vsha256h2q_u32(S[1], S[0], K_tmp);				\
71     S[0] = S_tmp
72 
73 		A64_RNDr(0, i);
74 		A64_RNDr(1, i);
75 		A64_RNDr(2, i);
76 		A64_RNDr(3, i);
77 
78 		if (i == 48)
79 			break;
80 
81 #define	A64_MSCH(x)							\
82     W[x] = vsha256su0q_u32(W[x], W[(x + 1) % 4]);			\
83     W[x] = vsha256su1q_u32(W[x], W[(x + 2) % 4], W[(x + 3) % 4])
84 
85 		A64_MSCH(0);
86 		A64_MSCH(1);
87 		A64_MSCH(2);
88 		A64_MSCH(3);
89 	}
90 
91 	/* 4. Mix local working variables into global state */
92 	S[0] = vaddq_u32(S[0], S_start[0]);
93 	S[1] = vaddq_u32(S[1], S_start[1]);
94 
95 	vst1q_u32(&state[0], S[0]);
96 	vst1q_u32(&state[4], S[1]);
97 }
98