xref: /freebsd/sys/crypto/sha2/sha256c_arm64.c (revision fdafd315)
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/types.h>
30 
31 #include <arm_neon.h>
32 
33 #include "sha256c_impl.h"
34 
35 void __hidden
SHA256_Transform_arm64_impl(uint32_t * state,const unsigned char block[64],const uint32_t K[64])36 SHA256_Transform_arm64_impl(uint32_t * state, const unsigned char block[64],
37     const uint32_t K[64])
38 {
39 	uint32x4_t W[4];
40 	uint32x4_t S[2];
41 	uint32x4_t S_start[2];
42 	uint32x4_t K_tmp, S_tmp;
43 	int i;
44 
45 #define	A64_LOAD_W(x)							\
46     W[x] = vld1q_u32((const uint32_t *)(&block[(x) * 16]));		\
47     W[x] = vreinterpretq_u32_u8(vrev32q_u8(vreinterpretq_u8_u32(W[x])))
48 
49 	/* 1. Prepare the first part of the message schedule W. */
50 	A64_LOAD_W(0);
51 	A64_LOAD_W(1);
52 	A64_LOAD_W(2);
53 	A64_LOAD_W(3);
54 
55 	/* 2. Initialize working variables. */
56 	S[0] = vld1q_u32(&state[0]);
57 	S[1] = vld1q_u32(&state[4]);
58 
59 	S_start[0] = S[0];
60 	S_start[1] = S[1];
61 
62 	/* 3. Mix. */
63 	for (i = 0; i < 64; i += 16) {
64 #define	A64_RNDr(i, ii)							\
65     K_tmp = vaddq_u32(W[i], vld1q_u32(&K[ii + i * 4]));			\
66     S_tmp = vsha256hq_u32(S[0], S[1], K_tmp);				\
67     S[1] = vsha256h2q_u32(S[1], S[0], K_tmp);				\
68     S[0] = S_tmp
69 
70 		A64_RNDr(0, i);
71 		A64_RNDr(1, i);
72 		A64_RNDr(2, i);
73 		A64_RNDr(3, i);
74 
75 		if (i == 48)
76 			break;
77 
78 #define	A64_MSCH(x)							\
79     W[x] = vsha256su0q_u32(W[x], W[(x + 1) % 4]);			\
80     W[x] = vsha256su1q_u32(W[x], W[(x + 2) % 4], W[(x + 3) % 4])
81 
82 		A64_MSCH(0);
83 		A64_MSCH(1);
84 		A64_MSCH(2);
85 		A64_MSCH(3);
86 	}
87 
88 	/* 4. Mix local working variables into global state */
89 	S[0] = vaddq_u32(S[0], S_start[0]);
90 	S[1] = vaddq_u32(S[1], S_start[1]);
91 
92 	vst1q_u32(&state[0], S[0]);
93 	vst1q_u32(&state[4], S[1]);
94 }
95