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