1463f7408SEric Biggers // SPDX-License-Identifier: GPL-2.0
2463f7408SEric Biggers /*
3463f7408SEric Biggers * SHA1 routine optimized to do word accesses rather than byte accesses,
4463f7408SEric Biggers * and to avoid unnecessary copies into the context array.
5463f7408SEric Biggers *
6463f7408SEric Biggers * This was based on the git SHA1 implementation.
7463f7408SEric Biggers */
8463f7408SEric Biggers
9463f7408SEric Biggers #include <linux/kernel.h>
10463f7408SEric Biggers #include <linux/export.h>
11*ed221835SEric Biggers #include <linux/module.h>
12463f7408SEric Biggers #include <linux/bitops.h>
13463f7408SEric Biggers #include <linux/string.h>
14463f7408SEric Biggers #include <crypto/sha1.h>
15463f7408SEric Biggers #include <asm/unaligned.h>
16463f7408SEric Biggers
17463f7408SEric Biggers /*
18463f7408SEric Biggers * If you have 32 registers or more, the compiler can (and should)
19463f7408SEric Biggers * try to change the array[] accesses into registers. However, on
20463f7408SEric Biggers * machines with less than ~25 registers, that won't really work,
21463f7408SEric Biggers * and at least gcc will make an unholy mess of it.
22463f7408SEric Biggers *
23463f7408SEric Biggers * So to avoid that mess which just slows things down, we force
24463f7408SEric Biggers * the stores to memory to actually happen (we might be better off
25463f7408SEric Biggers * with a 'W(t)=(val);asm("":"+m" (W(t))' there instead, as
26463f7408SEric Biggers * suggested by Artur Skawina - that will also make gcc unable to
27463f7408SEric Biggers * try to do the silly "optimize away loads" part because it won't
28463f7408SEric Biggers * see what the value will be).
29463f7408SEric Biggers *
30463f7408SEric Biggers * Ben Herrenschmidt reports that on PPC, the C version comes close
31463f7408SEric Biggers * to the optimized asm with this (ie on PPC you don't want that
32463f7408SEric Biggers * 'volatile', since there are lots of registers).
33463f7408SEric Biggers *
34463f7408SEric Biggers * On ARM we get the best code generation by forcing a full memory barrier
35463f7408SEric Biggers * between each SHA_ROUND, otherwise gcc happily get wild with spilling and
36463f7408SEric Biggers * the stack frame size simply explode and performance goes down the drain.
37463f7408SEric Biggers */
38463f7408SEric Biggers
39463f7408SEric Biggers #ifdef CONFIG_X86
40463f7408SEric Biggers #define setW(x, val) (*(volatile __u32 *)&W(x) = (val))
41463f7408SEric Biggers #elif defined(CONFIG_ARM)
42463f7408SEric Biggers #define setW(x, val) do { W(x) = (val); __asm__("":::"memory"); } while (0)
43463f7408SEric Biggers #else
44463f7408SEric Biggers #define setW(x, val) (W(x) = (val))
45463f7408SEric Biggers #endif
46463f7408SEric Biggers
47463f7408SEric Biggers /* This "rolls" over the 512-bit array */
48463f7408SEric Biggers #define W(x) (array[(x)&15])
49463f7408SEric Biggers
50463f7408SEric Biggers /*
51463f7408SEric Biggers * Where do we get the source from? The first 16 iterations get it from
52463f7408SEric Biggers * the input data, the next mix it from the 512-bit array.
53463f7408SEric Biggers */
54463f7408SEric Biggers #define SHA_SRC(t) get_unaligned_be32((__u32 *)data + t)
55463f7408SEric Biggers #define SHA_MIX(t) rol32(W(t+13) ^ W(t+8) ^ W(t+2) ^ W(t), 1)
56463f7408SEric Biggers
57463f7408SEric Biggers #define SHA_ROUND(t, input, fn, constant, A, B, C, D, E) do { \
58463f7408SEric Biggers __u32 TEMP = input(t); setW(t, TEMP); \
59463f7408SEric Biggers E += TEMP + rol32(A,5) + (fn) + (constant); \
60463f7408SEric Biggers B = ror32(B, 2); \
61463f7408SEric Biggers TEMP = E; E = D; D = C; C = B; B = A; A = TEMP; } while (0)
62463f7408SEric Biggers
63463f7408SEric Biggers #define T_0_15(t, A, B, C, D, E) SHA_ROUND(t, SHA_SRC, (((C^D)&B)^D) , 0x5a827999, A, B, C, D, E )
64463f7408SEric Biggers #define T_16_19(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (((C^D)&B)^D) , 0x5a827999, A, B, C, D, E )
65463f7408SEric Biggers #define T_20_39(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (B^C^D) , 0x6ed9eba1, A, B, C, D, E )
66463f7408SEric Biggers #define T_40_59(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, ((B&C)+(D&(B^C))) , 0x8f1bbcdc, A, B, C, D, E )
67463f7408SEric Biggers #define T_60_79(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (B^C^D) , 0xca62c1d6, A, B, C, D, E )
68463f7408SEric Biggers
69463f7408SEric Biggers /**
70463f7408SEric Biggers * sha1_transform - single block SHA1 transform (deprecated)
71463f7408SEric Biggers *
72463f7408SEric Biggers * @digest: 160 bit digest to update
73463f7408SEric Biggers * @data: 512 bits of data to hash
74463f7408SEric Biggers * @array: 16 words of workspace (see note)
75463f7408SEric Biggers *
76463f7408SEric Biggers * This function executes SHA-1's internal compression function. It updates the
77463f7408SEric Biggers * 160-bit internal state (@digest) with a single 512-bit data block (@data).
78463f7408SEric Biggers *
79463f7408SEric Biggers * Don't use this function. SHA-1 is no longer considered secure. And even if
80463f7408SEric Biggers * you do have to use SHA-1, this isn't the correct way to hash something with
81463f7408SEric Biggers * SHA-1 as this doesn't handle padding and finalization.
82463f7408SEric Biggers *
83463f7408SEric Biggers * Note: If the hash is security sensitive, the caller should be sure
84463f7408SEric Biggers * to clear the workspace. This is left to the caller to avoid
85463f7408SEric Biggers * unnecessary clears between chained hashing operations.
86463f7408SEric Biggers */
sha1_transform(__u32 * digest,const char * data,__u32 * array)87463f7408SEric Biggers void sha1_transform(__u32 *digest, const char *data, __u32 *array)
88463f7408SEric Biggers {
89463f7408SEric Biggers __u32 A, B, C, D, E;
90463f7408SEric Biggers unsigned int i = 0;
91463f7408SEric Biggers
92463f7408SEric Biggers A = digest[0];
93463f7408SEric Biggers B = digest[1];
94463f7408SEric Biggers C = digest[2];
95463f7408SEric Biggers D = digest[3];
96463f7408SEric Biggers E = digest[4];
97463f7408SEric Biggers
98463f7408SEric Biggers /* Round 1 - iterations 0-16 take their input from 'data' */
99463f7408SEric Biggers for (; i < 16; ++i)
100463f7408SEric Biggers T_0_15(i, A, B, C, D, E);
101463f7408SEric Biggers
102463f7408SEric Biggers /* Round 1 - tail. Input from 512-bit mixing array */
103463f7408SEric Biggers for (; i < 20; ++i)
104463f7408SEric Biggers T_16_19(i, A, B, C, D, E);
105463f7408SEric Biggers
106463f7408SEric Biggers /* Round 2 */
107463f7408SEric Biggers for (; i < 40; ++i)
108463f7408SEric Biggers T_20_39(i, A, B, C, D, E);
109463f7408SEric Biggers
110463f7408SEric Biggers /* Round 3 */
111463f7408SEric Biggers for (; i < 60; ++i)
112463f7408SEric Biggers T_40_59(i, A, B, C, D, E);
113463f7408SEric Biggers
114463f7408SEric Biggers /* Round 4 */
115463f7408SEric Biggers for (; i < 80; ++i)
116463f7408SEric Biggers T_60_79(i, A, B, C, D, E);
117463f7408SEric Biggers
118463f7408SEric Biggers digest[0] += A;
119463f7408SEric Biggers digest[1] += B;
120463f7408SEric Biggers digest[2] += C;
121463f7408SEric Biggers digest[3] += D;
122463f7408SEric Biggers digest[4] += E;
123463f7408SEric Biggers }
124463f7408SEric Biggers EXPORT_SYMBOL(sha1_transform);
125463f7408SEric Biggers
126463f7408SEric Biggers /**
127463f7408SEric Biggers * sha1_init - initialize the vectors for a SHA1 digest
128463f7408SEric Biggers * @buf: vector to initialize
129463f7408SEric Biggers */
sha1_init(__u32 * buf)130463f7408SEric Biggers void sha1_init(__u32 *buf)
131463f7408SEric Biggers {
132463f7408SEric Biggers buf[0] = 0x67452301;
133463f7408SEric Biggers buf[1] = 0xefcdab89;
134463f7408SEric Biggers buf[2] = 0x98badcfe;
135463f7408SEric Biggers buf[3] = 0x10325476;
136463f7408SEric Biggers buf[4] = 0xc3d2e1f0;
137463f7408SEric Biggers }
138463f7408SEric Biggers EXPORT_SYMBOL(sha1_init);
139*ed221835SEric Biggers
140*ed221835SEric Biggers MODULE_LICENSE("GPL");
141