sha_hw.c

filter: Statementstmt 66.6% 2 / 3 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 66.6% 2 / 3 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/arch/File/Raw/Hash/Install/Files.pm ↑ Summary Hash.xs »
1 /*
2 * sha_hw.c - SHA-1 and SHA-256 on the ARMv8 SHA instructions.
3 *
4 * Chosen at run time: the build always compiles these (the functions carry
5 * their own target attribute where the compiler does not enable the feature
6 * globally), and frh_hw_*_blocks() return them only when the CPU reports the
7 * instructions. Anything else gets NULL and the portable code runs.
8 */
9  
10 #include "sha_hw.h"
11 #include <string.h>
12  
13 #if !defined(FRH_NO_HW) && defined(__aarch64__) && (defined(__clang__) || defined(__GNUC__))
14 # define FRH_HW_ARM 1
15 # include <arm_neon.h>
16 # if defined(__ARM_FEATURE_SHA2)
17 # define FRH_TARGET_SHA2
18 # elif defined(__clang__)
19 # define FRH_TARGET_SHA2 __attribute__((target("sha2")))
20 # else
21 # define FRH_TARGET_SHA2 __attribute__((target("+sha2")))
22 # endif
23 # if defined(__APPLE__)
24 # include <sys/sysctl.h>
25 # elif defined(__linux__)
26 # include <sys/auxv.h>
27 # ifndef AT_HWCAP
28 # define AT_HWCAP 16
29 # endif
30 # endif
31 # ifndef HWCAP_SHA1
32 # define HWCAP_SHA1 (1UL << 5)
33 # endif
34 # ifndef HWCAP_SHA2
35 # define HWCAP_SHA2 (1UL << 6)
36 # endif
37 #endif
38  
39 #ifdef FRH_HW_ARM
40  
41 static int
42 cpu_has(const char *apple_name, unsigned long linux_bit)
43 {
44 # if defined(__APPLE__)
45 int v = 0;
46 size_t len = sizeof v;
47 (void)linux_bit;
48 if (sysctlbyname(apple_name, &v, &len, NULL, 0) != 0) return 0;
49 return v != 0;
50 # elif defined(__linux__)
51 (void)apple_name;
52 return (getauxval(AT_HWCAP) & linux_bit) != 0;
53 # else
54 (void)apple_name; (void)linux_bit;
55 # if defined(__ARM_FEATURE_SHA2)
56 return 1; /* built for a CPU that has it */
57 # else
58 return 0;
59 # endif
60 # endif
61 }
62  
63 static const uint32_t K256[64] = {
64 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
65 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
66 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
67 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
68 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
69 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
70 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
71 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
72 };
73  
74 static inline uint32x4_t
75 load_be32x4(const unsigned char *p)
76 {
77 return vreinterpretq_u32_u8(vrev32q_u8(vld1q_u8(p)));
78 }
79  
80 /* Four rounds per step: msg[i & 3] holds W[4i .. 4i+3]; once used, it is
81 * replaced by W[4i+16 ..], from the three that follow it. */
82 FRH_TARGET_SHA2 static void
83 sha256_blocks_arm(uint32_t *state, const unsigned char *p, size_t n)
84 {
85 uint32x4_t abcd = vld1q_u32(state);
86 uint32x4_t efgh = vld1q_u32(state + 4);
87  
88 while (n--) {
89 uint32x4_t abcd0 = abcd, efgh0 = efgh;
90 uint32x4_t msg[4];
91 int i;
92 msg[0] = load_be32x4(p);
93 msg[1] = load_be32x4(p + 16);
94 msg[2] = load_be32x4(p + 32);
95 msg[3] = load_be32x4(p + 48);
96 for (i = 0; i < 16; i++) {
97 uint32x4_t wk = vaddq_u32(msg[i & 3], vld1q_u32(K256 + 4 * i));
98 uint32x4_t prev = abcd;
99 abcd = vsha256hq_u32(abcd, efgh, wk);
100 efgh = vsha256h2q_u32(efgh, prev, wk);
101 if (i < 12)
102 msg[i & 3] = vsha256su1q_u32(vsha256su0q_u32(msg[i & 3], msg[(i + 1) & 3]),
103 msg[(i + 2) & 3], msg[(i + 3) & 3]);
104 }
105 abcd = vaddq_u32(abcd, abcd0);
106 efgh = vaddq_u32(efgh, efgh0);
107 p += 64;
108 }
109 vst1q_u32(state, abcd);
110 vst1q_u32(state + 4, efgh);
111 }
112  
113 /* Four rounds per step: choose for 0-19, parity 20-39, majority 40-59,
114 * parity 60-79; e for the next step comes from a before this one. */
115 FRH_TARGET_SHA2 static void
116 sha1_blocks_arm(uint32_t *state, const unsigned char *p, size_t n)
117 {
118 static const uint32_t K[4] = { 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6 };
119 uint32x4_t abcd = vld1q_u32(state);
120 uint32_t e = state[4];
121  
122 while (n--) {
123 uint32x4_t abcd0 = abcd;
124 uint32_t e0 = e;
125 uint32x4_t msg[4];
126 int i;
127 msg[0] = load_be32x4(p);
128 msg[1] = load_be32x4(p + 16);
129 msg[2] = load_be32x4(p + 32);
130 msg[3] = load_be32x4(p + 48);
131 for (i = 0; i < 20; i++) {
132 uint32x4_t wk = vaddq_u32(msg[i & 3], vdupq_n_u32(K[i / 5]));
133 uint32_t e_next = vsha1h_u32(vgetq_lane_u32(abcd, 0));
134 if (i < 5) abcd = vsha1cq_u32(abcd, e, wk);
135 else if (i < 10) abcd = vsha1pq_u32(abcd, e, wk);
136 else if (i < 15) abcd = vsha1mq_u32(abcd, e, wk);
137 else abcd = vsha1pq_u32(abcd, e, wk);
138 e = e_next;
139 if (i < 16)
140 msg[i & 3] = vsha1su1q_u32(vsha1su0q_u32(msg[i & 3], msg[(i + 1) & 3], msg[(i + 2) & 3]),
141 msg[(i + 3) & 3]);
142 }
143 abcd = vaddq_u32(abcd, abcd0);
144 e += e0;
145 p += 64;
146 }
147 vst1q_u32(state, abcd);
148 state[4] = e;
149 }
150  
151 frh_blocks32_fn
152 frh_hw_sha256_blocks(void)
153 {
154 return cpu_has("hw.optional.arm.FEAT_SHA256", HWCAP_SHA2) ? sha256_blocks_arm : NULL;
155 }
156  
157 frh_blocks32_fn
158 frh_hw_sha1_blocks(void)
159 {
160 return cpu_has("hw.optional.arm.FEAT_SHA1", HWCAP_SHA1) ? sha1_blocks_arm : NULL;
161 }
162  
163 const char *
164 frh_hw_name(void)
165 {
166 return "armv8";
167 }
168  
169 #else /* no hardware path in this build */
170  
171 18 frh_blocks32_fn frh_hw_sha256_blocks(void) { return NULL; }
172 18 frh_blocks32_fn frh_hw_sha1_blocks(void) { return NULL; }
173 0 const char *frh_hw_name(void) { return "portable"; }
174  
175 #endif
« blib/arch/File/Raw/Hash/Install/Files.pm ↑ Summary Hash.xs »