]> git.proxmox.com Git - ceph.git/blob - ceph/src/crypto/isa-l/isa-l_crypto/sha1_mb/sha1_ref.c
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / crypto / isa-l / isa-l_crypto / sha1_mb / sha1_ref.c
1 /**********************************************************************
2 Copyright(c) 2011-2016 Intel Corporation All rights reserved.
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in
11 the documentation and/or other materials provided with the
12 distribution.
13 * Neither the name of Intel Corporation nor the names of its
14 contributors may be used to endorse or promote products derived
15 from this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 **********************************************************************/
29
30 #include <string.h>
31 #include "sha1_mb.h"
32
33 ////////////////////////////////////////////////////////////////////////
34 ////////////////////////////////////////////////////////////////////////
35 // Reference SHA1 Functions
36 ////////////////////////////////////////////////////////////////////////
37 ////////////////////////////////////////////////////////////////////////
38
39 #define H0 0x67452301
40 #define H1 0xefcdab89
41 #define H2 0x98badcfe
42 #define H3 0x10325476
43 #define H4 0xc3d2e1f0
44
45 #define F1(b,c,d) (d ^ (b & (c ^ d)))
46 #define F2(b,c,d) (b ^ c ^ d)
47 #define F3(b,c,d) ((b & c) | (d & (b | c)))
48 #define F4(b,c,d) (b ^ c ^ d)
49
50 #define rol32(x, r) (((x)<<(r)) ^ ((x)>>(32-(r))))
51 #define bswap(x) (((x)<<24) | (((x)&0xff00)<<8) | (((x)&0xff0000)>>8) | ((x)>>24))
52
53 #define W(x) w[(x) & 15]
54
55 #define step00_19(i,a,b,c,d,e) \
56 if (i>15) W(i) = rol32(W(i-3)^W(i-8)^W(i-14)^W(i-16), 1); \
57 else W(i) = bswap(ww[i]); \
58 e += rol32(a,5) + F1(b,c,d) + 0x5A827999 + W(i); \
59 b = rol32(b,30)
60
61 #define step20_39(i,a,b,c,d,e) \
62 W(i) = rol32(W(i-3)^W(i-8)^W(i-14)^W(i-16), 1); \
63 e += rol32(a,5) + F2(b,c,d) + 0x6ED9EBA1 + W(i); \
64 b = rol32(b,30)
65
66 #define step40_59(i,a,b,c,d,e) \
67 W(i) = rol32(W(i-3)^W(i-8)^W(i-14)^W(i-16), 1); \
68 e += rol32(a,5) + F3(b,c,d) + 0x8F1BBCDC + W(i); \
69 b = rol32(b,30)
70
71 #define step60_79(i,a,b,c,d,e) \
72 W(i) = rol32(W(i-3)^W(i-8)^W(i-14)^W(i-16), 1); \
73 e += rol32(a,5) + F4(b,c,d) + 0xCA62C1D6 + W(i); \
74 b = rol32(b,30)
75
76 void sha1_single(const uint8_t * data, uint32_t digest[]);
77
78 void sha1_ref(const uint8_t * input_data, uint32_t * digest, const uint32_t len)
79 {
80 uint32_t i, j;
81 uint8_t buf[2 * SHA1_BLOCK_SIZE];
82 union {
83 uint64_t uint;
84 uint8_t uchar[8];
85 } convert;
86 uint8_t *p;
87
88 digest[0] = H0;
89 digest[1] = H1;
90 digest[2] = H2;
91 digest[3] = H3;
92 digest[4] = H4;
93
94 i = len;
95 while (i >= SHA1_BLOCK_SIZE) {
96 sha1_single(input_data, digest);
97 input_data += SHA1_BLOCK_SIZE;
98 i -= SHA1_BLOCK_SIZE;
99 }
100
101 memcpy(buf, input_data, i);
102 buf[i++] = 0x80;
103 for (j = i; j < ((2 * SHA1_BLOCK_SIZE) - SHA1_PADLENGTHFIELD_SIZE); j++)
104 buf[j] = 0;
105
106 if (i > SHA1_BLOCK_SIZE - SHA1_PADLENGTHFIELD_SIZE)
107 i = 2 * SHA1_BLOCK_SIZE;
108 else
109 i = SHA1_BLOCK_SIZE;
110
111 convert.uint = 8 * len;
112 p = buf + i - 8;
113 p[0] = convert.uchar[7];
114 p[1] = convert.uchar[6];
115 p[2] = convert.uchar[5];
116 p[3] = convert.uchar[4];
117 p[4] = convert.uchar[3];
118 p[5] = convert.uchar[2];
119 p[6] = convert.uchar[1];
120 p[7] = convert.uchar[0];
121
122 sha1_single(buf, digest);
123 if (i == (2 * SHA1_BLOCK_SIZE))
124 sha1_single(buf + SHA1_BLOCK_SIZE, digest);
125 }
126
127 void sha1_single(const uint8_t * data, uint32_t digest[])
128 {
129 uint32_t a, b, c, d, e;
130 uint32_t w[16] = { 0 };
131 uint32_t *ww = (uint32_t *) data;
132
133 a = digest[0];
134 b = digest[1];
135 c = digest[2];
136 d = digest[3];
137 e = digest[4];
138
139 step00_19(0, a, b, c, d, e);
140 step00_19(1, e, a, b, c, d);
141 step00_19(2, d, e, a, b, c);
142 step00_19(3, c, d, e, a, b);
143 step00_19(4, b, c, d, e, a);
144 step00_19(5, a, b, c, d, e);
145 step00_19(6, e, a, b, c, d);
146 step00_19(7, d, e, a, b, c);
147 step00_19(8, c, d, e, a, b);
148 step00_19(9, b, c, d, e, a);
149 step00_19(10, a, b, c, d, e);
150 step00_19(11, e, a, b, c, d);
151 step00_19(12, d, e, a, b, c);
152 step00_19(13, c, d, e, a, b);
153 step00_19(14, b, c, d, e, a);
154 step00_19(15, a, b, c, d, e);
155 step00_19(16, e, a, b, c, d);
156 step00_19(17, d, e, a, b, c);
157 step00_19(18, c, d, e, a, b);
158 step00_19(19, b, c, d, e, a);
159
160 step20_39(20, a, b, c, d, e);
161 step20_39(21, e, a, b, c, d);
162 step20_39(22, d, e, a, b, c);
163 step20_39(23, c, d, e, a, b);
164 step20_39(24, b, c, d, e, a);
165 step20_39(25, a, b, c, d, e);
166 step20_39(26, e, a, b, c, d);
167 step20_39(27, d, e, a, b, c);
168 step20_39(28, c, d, e, a, b);
169 step20_39(29, b, c, d, e, a);
170 step20_39(30, a, b, c, d, e);
171 step20_39(31, e, a, b, c, d);
172 step20_39(32, d, e, a, b, c);
173 step20_39(33, c, d, e, a, b);
174 step20_39(34, b, c, d, e, a);
175 step20_39(35, a, b, c, d, e);
176 step20_39(36, e, a, b, c, d);
177 step20_39(37, d, e, a, b, c);
178 step20_39(38, c, d, e, a, b);
179 step20_39(39, b, c, d, e, a);
180
181 step40_59(40, a, b, c, d, e);
182 step40_59(41, e, a, b, c, d);
183 step40_59(42, d, e, a, b, c);
184 step40_59(43, c, d, e, a, b);
185 step40_59(44, b, c, d, e, a);
186 step40_59(45, a, b, c, d, e);
187 step40_59(46, e, a, b, c, d);
188 step40_59(47, d, e, a, b, c);
189 step40_59(48, c, d, e, a, b);
190 step40_59(49, b, c, d, e, a);
191 step40_59(50, a, b, c, d, e);
192 step40_59(51, e, a, b, c, d);
193 step40_59(52, d, e, a, b, c);
194 step40_59(53, c, d, e, a, b);
195 step40_59(54, b, c, d, e, a);
196 step40_59(55, a, b, c, d, e);
197 step40_59(56, e, a, b, c, d);
198 step40_59(57, d, e, a, b, c);
199 step40_59(58, c, d, e, a, b);
200 step40_59(59, b, c, d, e, a);
201
202 step60_79(60, a, b, c, d, e);
203 step60_79(61, e, a, b, c, d);
204 step60_79(62, d, e, a, b, c);
205 step60_79(63, c, d, e, a, b);
206 step60_79(64, b, c, d, e, a);
207 step60_79(65, a, b, c, d, e);
208 step60_79(66, e, a, b, c, d);
209 step60_79(67, d, e, a, b, c);
210 step60_79(68, c, d, e, a, b);
211 step60_79(69, b, c, d, e, a);
212 step60_79(70, a, b, c, d, e);
213 step60_79(71, e, a, b, c, d);
214 step60_79(72, d, e, a, b, c);
215 step60_79(73, c, d, e, a, b);
216 step60_79(74, b, c, d, e, a);
217 step60_79(75, a, b, c, d, e);
218 step60_79(76, e, a, b, c, d);
219 step60_79(77, d, e, a, b, c);
220 step60_79(78, c, d, e, a, b);
221 step60_79(79, b, c, d, e, a);
222
223 digest[0] += a;
224 digest[1] += b;
225 digest[2] += c;
226 digest[3] += d;
227 digest[4] += e;
228 }