The Pedigree Project 0.1
sha1.cc
1/*
2 * Copyright (c) 2008-2014, Pedigree Developers
3 *
4 * Please see the CONTRIB file in the root of the source tree for a full
5 * list of contributors.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20/*
21 * sha1.cpp
22 *
23 * Copyright (C) 1998, 2009
24 * Paul E. Jones <paulej@packetizer.com>
25 * All Rights Reserved.
26 *
27 *****************************************************************************
28 * $Id: sha1.cpp 12 2009-06-22 19:34:25Z paulej $
29 *****************************************************************************
30 *
31 * Description:
32 * This class implements the Secure Hashing Standard as defined
33 * in FIPS PUB 180-1 published April 17, 1995.
34 *
35 * The Secure Hashing Standard, which uses the Secure Hashing
36 * Algorithm (SHA), produces a 160-bit message digest for a
37 * given data stream. In theory, it is highly improbable that
38 * two messages will produce the same message digest. Therefore,
39 * this algorithm can serve as a means of providing a "fingerprint"
40 * for a message.
41 *
42 * Portability Issues:
43 * SHA-1 is defined in terms of 32-bit "words". This code was
44 * written with the expectation that the processor has at least
45 * a 32-bit machine word size. If the machine word size is larger,
46 * the code should still function properly. One caveat to that
47 * is that the input functions taking characters and character arrays
48 * assume that only 8 bits of information are stored in each character.
49 *
50 * Caveats:
51 * SHA-1 is designed to work with messages less than 2^64 bits long.
52 * Although SHA-1 allows a message digest to be generated for
53 * messages of any number of bits less than 2^64, this implementation
54 * only works with messages with a length that is a multiple of 8
55 * bits.
56 *
57 */
58
59#include "pedigree/kernel/utilities/sha1/sha1.h"
60
61/*
62 * SHA1
63 *
64 * Description:
65 * This is the constructor for the sha1 class.
66 *
67 * Parameters:
68 * None.
69 *
70 * Returns:
71 * Nothing.
72 *
73 * Comments:
74 *
75 */
76SHA1::SHA1()
77 : H(),
78 Length_Low(0),
79 Length_High(0),
80 Message_Block(),
81 Message_Block_Index(0),
82 Computed(false),
83 Corrupted(false) {
84 Reset();
85}
86
87/*
88 * ~SHA1
89 *
90 * Description:
91 * This is the destructor for the sha1 class
92 *
93 * Parameters:
94 * None.
95 *
96 * Returns:
97 * Nothing.
98 *
99 * Comments:
100 *
101 */
102SHA1::~SHA1() {
103 // The destructor does nothing
104}
105
106/*
107 * Reset
108 *
109 * Description:
110 * This function will initialize the sha1 class member variables
111 * in preparation for computing a new message digest.
112 *
113 * Parameters:
114 * None.
115 *
116 * Returns:
117 * Nothing.
118 *
119 * Comments:
120 *
121 */
122void SHA1::Reset() {
123 Length_Low = 0;
124 Length_High = 0;
125 Message_Block_Index = 0;
126
127 H[0] = 0x67452301;
128 H[1] = 0xEFCDAB89;
129 H[2] = 0x98BADCFE;
130 H[3] = 0x10325476;
131 H[4] = 0xC3D2E1F0;
132
133 Computed = false;
134 Corrupted = false;
135}
136
137/*
138 * Result
139 *
140 * Description:
141 * This function will return the 160-bit message digest into the
142 * array provided.
143 *
144 * Parameters:
145 * message_digest_array: [out]
146 * This is an array of five unsigned integers which will be filled
147 * with the message digest that has been computed.
148 *
149 * Returns:
150 * True if successful, false if it failed.
151 *
152 * Comments:
153 *
154 */
155bool SHA1::Result(unsigned* message_digest_array) {
156 int i; // Counter
157
158 if (Corrupted) {
159 return false;
160 }
161
162 if (!Computed) {
163 PadMessage();
164 Computed = true;
165 }
166
167 for (i = 0; i < 5; i++) {
168 message_digest_array[i] = H[i];
169 }
170
171 return true;
172}
173
174/*
175 * Input
176 *
177 * Description:
178 * This function accepts an array of octets as the next portion of
179 * the message.
180 *
181 * Parameters:
182 * message_array: [in]
183 * An array of characters representing the next portion of the
184 * message.
185 *
186 * Returns:
187 * Nothing.
188 *
189 * Comments:
190 *
191 */
192void SHA1::Input(const unsigned char* message_array, unsigned length) {
193 if (!length) {
194 return;
195 }
196
197 if (Computed || Corrupted) {
198 Corrupted = true;
199 return;
200 }
201
202 while (length-- && !Corrupted) {
203 Message_Block[Message_Block_Index++] = (*message_array & 0xFF);
204
205 Length_Low += 8;
206 Length_Low &= 0xFFFFFFFF; // Force it to 32 bits
207 if (Length_Low == 0) {
208 Length_High++;
209 Length_High &= 0xFFFFFFFF; // Force it to 32 bits
210 if (Length_High == 0) {
211 Corrupted = true; // Message is too long
212 }
213 }
214
215 if (Message_Block_Index == 64) {
216 ProcessMessageBlock();
217 }
218
219 message_array++;
220 }
221}
222
223/*
224 * Input
225 *
226 * Description:
227 * This function accepts an array of octets as the next portion of
228 * the message.
229 *
230 * Parameters:
231 * message_array: [in]
232 * An array of characters representing the next portion of the
233 * message.
234 * length: [in]
235 * The length of the message_array
236 *
237 * Returns:
238 * Nothing.
239 *
240 * Comments:
241 *
242 */
243void SHA1::Input(const char* message_array, unsigned length) {
244 Input(reinterpret_cast<unsigned char*>(const_cast<char*>(message_array)), length);
245}
246
247/*
248 * Input
249 *
250 * Description:
251 * This function accepts a single octets as the next message element.
252 *
253 * Parameters:
254 * message_element: [in]
255 * The next octet in the message.
256 *
257 * Returns:
258 * Nothing.
259 *
260 * Comments:
261 *
262 */
263void SHA1::Input(unsigned char message_element) {
264 Input(&message_element, 1);
265}
266
267/*
268 * Input
269 *
270 * Description:
271 * This function accepts a single octet as the next message element.
272 *
273 * Parameters:
274 * message_element: [in]
275 * The next octet in the message.
276 *
277 * Returns:
278 * Nothing.
279 *
280 * Comments:
281 *
282 */
283void SHA1::Input(char message_element) {
284 Input(reinterpret_cast<unsigned char*>(&message_element), 1);
285}
286
287/*
288 * operator<<
289 *
290 * Description:
291 * This operator makes it convenient to provide character strings to
292 * the SHA1 object for processing.
293 *
294 * Parameters:
295 * message_array: [in]
296 * The character array to take as input.
297 *
298 * Returns:
299 * A reference to the SHA1 object.
300 *
301 * Comments:
302 * Each character is assumed to hold 8 bits of information.
303 *
304 */
305SHA1& SHA1::operator<<(const char* message_array) {
306 const char* p = message_array;
307
308 while (*p) {
309 Input(*p);
310 p++;
311 }
312
313 return *this;
314}
315
316/*
317 * operator<<
318 *
319 * Description:
320 * This operator makes it convenient to provide character strings to
321 * the SHA1 object for processing.
322 *
323 * Parameters:
324 * message_array: [in]
325 * The character array to take as input.
326 *
327 * Returns:
328 * A reference to the SHA1 object.
329 *
330 * Comments:
331 * Each character is assumed to hold 8 bits of information.
332 *
333 */
334SHA1& SHA1::operator<<(const unsigned char* message_array) {
335 const unsigned char* p = message_array;
336
337 while (*p) {
338 Input(*p);
339 p++;
340 }
341
342 return *this;
343}
344
345/*
346 * operator<<
347 *
348 * Description:
349 * This function provides the next octet in the message.
350 *
351 * Parameters:
352 * message_element: [in]
353 * The next octet in the message
354 *
355 * Returns:
356 * A reference to the SHA1 object.
357 *
358 * Comments:
359 * The character is assumed to hold 8 bits of information.
360 *
361 */
362SHA1& SHA1::operator<<(const char message_element) {
363 Input(reinterpret_cast<unsigned char*>(const_cast<char*>(&message_element)), 1);
364
365 return *this;
366}
367
368/*
369 * operator<<
370 *
371 * Description:
372 * This function provides the next octet in the message.
373 *
374 * Parameters:
375 * message_element: [in]
376 * The next octet in the message
377 *
378 * Returns:
379 * A reference to the SHA1 object.
380 *
381 * Comments:
382 * The character is assumed to hold 8 bits of information.
383 *
384 */
385SHA1& SHA1::operator<<(const unsigned char message_element) {
386 Input(&message_element, 1);
387
388 return *this;
389}
390
391/*
392 * ProcessMessageBlock
393 *
394 * Description:
395 * This function will process the next 512 bits of the message
396 * stored in the Message_Block array.
397 *
398 * Parameters:
399 * None.
400 *
401 * Returns:
402 * Nothing.
403 *
404 * Comments:
405 * Many of the variable names in this function, especially the single
406 * character names, were used because those were the names used
407 * in the publication.
408 *
409 */
410void SHA1::ProcessMessageBlock() {
411 const unsigned K[] = {// Constants defined for SHA-1
412 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6};
413 int t; // Loop counter
414 unsigned temp; // Temporary word value
415 unsigned W[80]; // Word sequence
416 unsigned A, B, C, D, E; // Word buffers
417
418 /*
419 * Initialize the first 16 words in the array W
420 */
421 for (t = 0; t < 16; t++) {
422 W[t] = Message_Block[t * 4] << 24;
423 W[t] |= Message_Block[t * 4 + 1] << 16;
424 W[t] |= Message_Block[t * 4 + 2] << 8;
425 W[t] |= Message_Block[t * 4 + 3];
426 }
427
428 for (t = 16; t < 80; t++) {
429 W[t] = CircularShift(1, W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16]);
430 }
431
432 A = H[0];
433 B = H[1];
434 C = H[2];
435 D = H[3];
436 E = H[4];
437
438 for (t = 0; t < 20; t++) {
439 temp = CircularShift(5, A) + ((B & C) | ((~B) & D)) + E + W[t] + K[0];
440 temp &= 0xFFFFFFFF;
441 E = D;
442 D = C;
443 C = CircularShift(30, B);
444 B = A;
445 A = temp;
446 }
447
448 for (t = 20; t < 40; t++) {
449 temp = CircularShift(5, A) + (B ^ C ^ D) + E + W[t] + K[1];
450 temp &= 0xFFFFFFFF;
451 E = D;
452 D = C;
453 C = CircularShift(30, B);
454 B = A;
455 A = temp;
456 }
457
458 for (t = 40; t < 60; t++) {
459 temp = CircularShift(5, A) + ((B & C) | (B & D) | (C & D)) + E + W[t] + K[2];
460 temp &= 0xFFFFFFFF;
461 E = D;
462 D = C;
463 C = CircularShift(30, B);
464 B = A;
465 A = temp;
466 }
467
468 for (t = 60; t < 80; t++) {
469 temp = CircularShift(5, A) + (B ^ C ^ D) + E + W[t] + K[3];
470 temp &= 0xFFFFFFFF;
471 E = D;
472 D = C;
473 C = CircularShift(30, B);
474 B = A;
475 A = temp;
476 }
477
478 H[0] = (H[0] + A) & 0xFFFFFFFF;
479 H[1] = (H[1] + B) & 0xFFFFFFFF;
480 H[2] = (H[2] + C) & 0xFFFFFFFF;
481 H[3] = (H[3] + D) & 0xFFFFFFFF;
482 H[4] = (H[4] + E) & 0xFFFFFFFF;
483
484 Message_Block_Index = 0;
485}
486
487/*
488 * PadMessage
489 *
490 * Description:
491 * According to the standard, the message must be padded to an even
492 * 512 bits. The first padding bit must be a '1'. The last 64 bits
493 * represent the length of the original message. All bits in between
494 * should be 0. This function will pad the message according to those
495 * rules by filling the message_block array accordingly. It will also
496 * call ProcessMessageBlock() appropriately. When it returns, it
497 * can be assumed that the message digest has been computed.
498 *
499 * Parameters:
500 * None.
501 *
502 * Returns:
503 * Nothing.
504 *
505 * Comments:
506 *
507 */
508void SHA1::PadMessage() {
509 /*
510 * Check to see if the current message block is too small to hold
511 * the initial padding bits and length. If so, we will pad the
512 * block, process it, and then continue padding into a second block.
513 */
514 if (Message_Block_Index > 55) {
515 Message_Block[Message_Block_Index++] = 0x80;
516 while (Message_Block_Index < 64) {
517 Message_Block[Message_Block_Index++] = 0;
518 }
519
520 ProcessMessageBlock();
521
522 while (Message_Block_Index < 56) {
523 Message_Block[Message_Block_Index++] = 0;
524 }
525 } else {
526 Message_Block[Message_Block_Index++] = 0x80;
527 while (Message_Block_Index < 56) {
528 Message_Block[Message_Block_Index++] = 0;
529 }
530 }
531
532 /*
533 * Store the message length as the last 8 octets
534 */
535 Message_Block[56] = (Length_High >> 24) & 0xFF;
536 Message_Block[57] = (Length_High >> 16) & 0xFF;
537 Message_Block[58] = (Length_High >> 8) & 0xFF;
538 Message_Block[59] = (Length_High) & 0xFF;
539 Message_Block[60] = (Length_Low >> 24) & 0xFF;
540 Message_Block[61] = (Length_Low >> 16) & 0xFF;
541 Message_Block[62] = (Length_Low >> 8) & 0xFF;
542 Message_Block[63] = (Length_Low) & 0xFF;
543
544 ProcessMessageBlock();
545}
546
547/*
548 * CircularShift
549 *
550 * Description:
551 * This member function will perform a circular shifting operation.
552 *
553 * Parameters:
554 * bits: [in]
555 * The number of bits to shift (1-31)
556 * word: [in]
557 * The value to shift (assumes a 32-bit integer)
558 *
559 * Returns:
560 * The shifted value.
561 *
562 * Comments:
563 *
564 */
565unsigned SHA1::CircularShift(int bits, unsigned word) {
566 return ((word << bits) & 0xFFFFFFFF) | ((word & 0xFFFFFFFF) >> (32 - bits));
567}