1/****************************************************************************
2 Copyright (C) 2012 Monty Program AB
3 2016 MariaDB Corporation AB
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not see <http://www.gnu.org/licenses>
17 or write to the Free Software Foundation, Inc.,
18 51 Franklin St., Fifth Floor, Boston, MA 02110, USA
19 *****************************************************************************/
20
21/* This code came from the PHP project, initially written by
22 Stefan Esser */
23
24
25#include "ma_global.h"
26#include "string.h"
27
28/* This code is heavily based on the PHP md5 implementation */
29
30#include "ma_sha1.h"
31
32
33static void ma_SHA1Transform(uint32[5], const unsigned char[64]);
34static void ma_SHA1Encode(unsigned char *, uint32 *, unsigned int);
35static void ma_SHA1Decode(uint32 *, const unsigned char *, unsigned int);
36
37static unsigned char PADDING[64] =
38{
39 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
40 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
41 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
42};
43
44/* F, G, H and I are basic SHA1 functions.
45*/
46#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
47#define G(x, y, z) ((x) ^ (y) ^ (z))
48#define H(x, y, z) (((x) & (y)) | ((z) & ((x) | (y))))
49#define I(x, y, z) ((x) ^ (y) ^ (z))
50
51/* ROTATE_LEFT rotates x left n bits.
52*/
53#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
54
55/* W[i]
56*/
57#define W(i) ( tmp=x[(i-3)&15]^x[(i-8)&15]^x[(i-14)&15]^x[i&15], \
58 (x[i&15]=ROTATE_LEFT(tmp, 1)) )
59
60/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
61*/
62#define FF(a, b, c, d, e, w) { \
63 (e) += F ((b), (c), (d)) + (w) + (uint32)(0x5A827999); \
64 (e) += ROTATE_LEFT ((a), 5); \
65 (b) = ROTATE_LEFT((b), 30); \
66}
67#define GG(a, b, c, d, e, w) { \
68 (e) += G ((b), (c), (d)) + (w) + (uint32)(0x6ED9EBA1); \
69 (e) += ROTATE_LEFT ((a), 5); \
70 (b) = ROTATE_LEFT((b), 30); \
71}
72#define HH(a, b, c, d, e, w) { \
73 (e) += H ((b), (c), (d)) + (w) + (uint32)(0x8F1BBCDC); \
74 (e) += ROTATE_LEFT ((a), 5); \
75 (b) = ROTATE_LEFT((b), 30); \
76}
77#define II(a, b, c, d, e, w) { \
78 (e) += I ((b), (c), (d)) + (w) + (uint32)(0xCA62C1D6); \
79 (e) += ROTATE_LEFT ((a), 5); \
80 (b) = ROTATE_LEFT((b), 30); \
81}
82
83
84/* {{{ ma_SHA1Init
85 * SHA1 initialization. Begins an SHA1 operation, writing a new context.
86 */
87void ma_SHA1Init(_MA_SHA1_CTX * context)
88{
89 context->count[0] = context->count[1] = 0;
90 /* Load magic initialization constants.
91 */
92 context->state[0] = 0x67452301;
93 context->state[1] = 0xefcdab89;
94 context->state[2] = 0x98badcfe;
95 context->state[3] = 0x10325476;
96 context->state[4] = 0xc3d2e1f0;
97}
98/* }}} */
99
100/* {{{ ma_SHA1Update
101 SHA1 block update operation. Continues an SHA1 message-digest
102 operation, processing another message block, and updating the
103 context.
104 */
105void ma_SHA1Update(_MA_SHA1_CTX * context, const unsigned char *input,
106 size_t inputLen)
107{
108 unsigned int i, index, partLen;
109
110 /* Compute number of bytes mod 64 */
111 index = (unsigned int) ((context->count[0] >> 3) & 0x3F);
112
113 /* Update number of bits */
114 if ((context->count[0] += ((uint32) inputLen << 3))
115 < ((uint32) inputLen << 3))
116 context->count[1]++;
117 context->count[1] += ((uint32) inputLen >> 29);
118
119 partLen = 64 - index;
120
121 /* Transform as many times as possible.
122 */
123 if (inputLen >= partLen) {
124 memcpy
125 ((unsigned char*) & context->buffer[index], (unsigned char*) input, partLen);
126 ma_SHA1Transform(context->state, context->buffer);
127
128 for (i = partLen; i + 63 < inputLen; i += 64)
129 ma_SHA1Transform(context->state, &input[i]);
130
131 index = 0;
132 } else
133 i = 0;
134
135 /* Buffer remaining input */
136 memcpy
137 ((unsigned char*) & context->buffer[index], (unsigned char*) & input[i],
138 inputLen - i);
139}
140/* }}} */
141
142/* {{{ ma_SHA1Final
143 SHA1 finalization. Ends an SHA1 message-digest operation, writing the
144 the message digest and zeroizing the context.
145 */
146void ma_SHA1Final(unsigned char digest[20], _MA_SHA1_CTX * context)
147{
148 unsigned char bits[8];
149 unsigned int index, padLen;
150
151 /* Save number of bits */
152 bits[7] = context->count[0] & 0xFF;
153 bits[6] = (context->count[0] >> 8) & 0xFF;
154 bits[5] = (context->count[0] >> 16) & 0xFF;
155 bits[4] = (context->count[0] >> 24) & 0xFF;
156 bits[3] = context->count[1] & 0xFF;
157 bits[2] = (context->count[1] >> 8) & 0xFF;
158 bits[1] = (context->count[1] >> 16) & 0xFF;
159 bits[0] = (context->count[1] >> 24) & 0xFF;
160
161 /* Pad out to 56 mod 64.
162 */
163 index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
164 padLen = (index < 56) ? (56 - index) : (120 - index);
165 ma_SHA1Update(context, PADDING, padLen);
166
167 /* Append length (before padding) */
168 ma_SHA1Update(context, bits, 8);
169
170 /* Store state in digest */
171 ma_SHA1Encode(digest, context->state, 20);
172
173 /* Zeroize sensitive information.
174 */
175 memset((unsigned char*) context, 0, sizeof(*context));
176}
177/* }}} */
178
179/* {{{ ma_SHA1Transform
180 * SHA1 basic transformation. Transforms state based on block.
181 */
182static void ma_SHA1Transform(uint32 state[5], const unsigned char block[64])
183{
184 uint32 a = state[0], b = state[1], c = state[2];
185 uint32 d = state[3], e = state[4], x[16], tmp;
186
187 ma_SHA1Decode(x, block, 64);
188
189 /* Round 1 */
190 FF(a, b, c, d, e, x[0]); /* 1 */
191 FF(e, a, b, c, d, x[1]); /* 2 */
192 FF(d, e, a, b, c, x[2]); /* 3 */
193 FF(c, d, e, a, b, x[3]); /* 4 */
194 FF(b, c, d, e, a, x[4]); /* 5 */
195 FF(a, b, c, d, e, x[5]); /* 6 */
196 FF(e, a, b, c, d, x[6]); /* 7 */
197 FF(d, e, a, b, c, x[7]); /* 8 */
198 FF(c, d, e, a, b, x[8]); /* 9 */
199 FF(b, c, d, e, a, x[9]); /* 10 */
200 FF(a, b, c, d, e, x[10]); /* 11 */
201 FF(e, a, b, c, d, x[11]); /* 12 */
202 FF(d, e, a, b, c, x[12]); /* 13 */
203 FF(c, d, e, a, b, x[13]); /* 14 */
204 FF(b, c, d, e, a, x[14]); /* 15 */
205 FF(a, b, c, d, e, x[15]); /* 16 */
206 FF(e, a, b, c, d, W(16)); /* 17 */
207 FF(d, e, a, b, c, W(17)); /* 18 */
208 FF(c, d, e, a, b, W(18)); /* 19 */
209 FF(b, c, d, e, a, W(19)); /* 20 */
210
211 /* Round 2 */
212 GG(a, b, c, d, e, W(20)); /* 21 */
213 GG(e, a, b, c, d, W(21)); /* 22 */
214 GG(d, e, a, b, c, W(22)); /* 23 */
215 GG(c, d, e, a, b, W(23)); /* 24 */
216 GG(b, c, d, e, a, W(24)); /* 25 */
217 GG(a, b, c, d, e, W(25)); /* 26 */
218 GG(e, a, b, c, d, W(26)); /* 27 */
219 GG(d, e, a, b, c, W(27)); /* 28 */
220 GG(c, d, e, a, b, W(28)); /* 29 */
221 GG(b, c, d, e, a, W(29)); /* 30 */
222 GG(a, b, c, d, e, W(30)); /* 31 */
223 GG(e, a, b, c, d, W(31)); /* 32 */
224 GG(d, e, a, b, c, W(32)); /* 33 */
225 GG(c, d, e, a, b, W(33)); /* 34 */
226 GG(b, c, d, e, a, W(34)); /* 35 */
227 GG(a, b, c, d, e, W(35)); /* 36 */
228 GG(e, a, b, c, d, W(36)); /* 37 */
229 GG(d, e, a, b, c, W(37)); /* 38 */
230 GG(c, d, e, a, b, W(38)); /* 39 */
231 GG(b, c, d, e, a, W(39)); /* 40 */
232
233 /* Round 3 */
234 HH(a, b, c, d, e, W(40)); /* 41 */
235 HH(e, a, b, c, d, W(41)); /* 42 */
236 HH(d, e, a, b, c, W(42)); /* 43 */
237 HH(c, d, e, a, b, W(43)); /* 44 */
238 HH(b, c, d, e, a, W(44)); /* 45 */
239 HH(a, b, c, d, e, W(45)); /* 46 */
240 HH(e, a, b, c, d, W(46)); /* 47 */
241 HH(d, e, a, b, c, W(47)); /* 48 */
242 HH(c, d, e, a, b, W(48)); /* 49 */
243 HH(b, c, d, e, a, W(49)); /* 50 */
244 HH(a, b, c, d, e, W(50)); /* 51 */
245 HH(e, a, b, c, d, W(51)); /* 52 */
246 HH(d, e, a, b, c, W(52)); /* 53 */
247 HH(c, d, e, a, b, W(53)); /* 54 */
248 HH(b, c, d, e, a, W(54)); /* 55 */
249 HH(a, b, c, d, e, W(55)); /* 56 */
250 HH(e, a, b, c, d, W(56)); /* 57 */
251 HH(d, e, a, b, c, W(57)); /* 58 */
252 HH(c, d, e, a, b, W(58)); /* 59 */
253 HH(b, c, d, e, a, W(59)); /* 60 */
254
255 /* Round 4 */
256 II(a, b, c, d, e, W(60)); /* 61 */
257 II(e, a, b, c, d, W(61)); /* 62 */
258 II(d, e, a, b, c, W(62)); /* 63 */
259 II(c, d, e, a, b, W(63)); /* 64 */
260 II(b, c, d, e, a, W(64)); /* 65 */
261 II(a, b, c, d, e, W(65)); /* 66 */
262 II(e, a, b, c, d, W(66)); /* 67 */
263 II(d, e, a, b, c, W(67)); /* 68 */
264 II(c, d, e, a, b, W(68)); /* 69 */
265 II(b, c, d, e, a, W(69)); /* 70 */
266 II(a, b, c, d, e, W(70)); /* 71 */
267 II(e, a, b, c, d, W(71)); /* 72 */
268 II(d, e, a, b, c, W(72)); /* 73 */
269 II(c, d, e, a, b, W(73)); /* 74 */
270 II(b, c, d, e, a, W(74)); /* 75 */
271 II(a, b, c, d, e, W(75)); /* 76 */
272 II(e, a, b, c, d, W(76)); /* 77 */
273 II(d, e, a, b, c, W(77)); /* 78 */
274 II(c, d, e, a, b, W(78)); /* 79 */
275 II(b, c, d, e, a, W(79)); /* 80 */
276
277 state[0] += a;
278 state[1] += b;
279 state[2] += c;
280 state[3] += d;
281 state[4] += e;
282
283 /* Zeroize sensitive information. */
284 memset((unsigned char*) x, 0, sizeof(x));
285}
286/* }}} */
287
288/* {{{ ma_SHA1Encode
289 Encodes input (uint32) into output (unsigned char). Assumes len is
290 a multiple of 4.
291 */
292static void ma_SHA1Encode(unsigned char *output, uint32 *input, unsigned int len)
293{
294 unsigned int i, j;
295
296 for (i = 0, j = 0; j < len; i++, j += 4) {
297 output[j] = (unsigned char) ((input[i] >> 24) & 0xff);
298 output[j + 1] = (unsigned char) ((input[i] >> 16) & 0xff);
299 output[j + 2] = (unsigned char) ((input[i] >> 8) & 0xff);
300 output[j + 3] = (unsigned char) (input[i] & 0xff);
301 }
302}
303/* }}} */
304
305/* {{{ ma_SHA1Decode
306 Decodes input (unsigned char) into output (uint32). Assumes len is
307 a multiple of 4.
308 */
309static void ma_SHA1Decode(uint32 *output, const unsigned char * input, unsigned int len)
310{
311 unsigned int i, j;
312
313 for (i = 0, j = 0; j < len; i++, j += 4)
314 output[i] = ((uint32) input[j + 3]) | (((uint32) input[j + 2]) << 8) |
315 (((uint32) input[j + 1]) << 16) | (((uint32) input[j]) << 24);
316}
317/* }}} */
318
319/*
320 * Local variables:
321 * tab-width: 4
322 * c-basic-offset: 4
323 * End:
324 * vim600: sw=4 ts=4 fdm=marker
325 * vim<600: sw=4 ts=4
326 */
327