blob: 8b70d2fefa96ef3233cc7ce8f970227174e1ca36 [file] [log] [blame]
Patrick Williamsb48b7b42016-08-17 15:04:38 -05001diff --git a/common/sha1.c b/common/sha1.c
2new file mode 100644
3index 0000000..988b188
4--- /dev/null
5+++ b/common/sha1.c
6@@ -0,0 +1,411 @@
7+/*
8+ * Copyright (C) The Internet Society (2001). All Rights Reserved.
9+ *
10+ * This document and translations of it may be copied and furnished to
11+ * others, and derivative works that comment on or otherwise explain it
12+ * or assist in its implementation may be prepared, copied, published
13+ * and distributed, in whole or in part, without restriction of any
14+ * kind, provided that the above copyright notice and this paragraph are
15+ * included on all such copies and derivative works. However, this
16+ * document itself may not be modified in any way, such as by removing
17+ * the copyright notice or references to the Internet Society or other
18+ * Internet organizations, except as needed for the purpose of
19+ * developing Internet standards in which case the procedures for
20+ * copyrights defined in the Internet Standards process must be
21+ * followed, or as required to translate it into languages other than
22+ * English.
23+ *
24+ * The limited permissions granted above are perpetual and will not be
25+ * revoked by the Internet Society or its successors or assigns.
26+ *
27+ * This document and the information contained herein is provided on an
28+ * "AS IS" basis and THE INTERNET SOCIETY AND THE INTERNET ENGINEERING
29+ * TASK FORCE DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
30+ * BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION
31+ * HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF
32+ * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
33+ */
34+
35+/*
36+ * sha1.c
37+ *
38+ * Description:
39+ * This file implements the Secure Hashing Algorithm 1 as
40+ * defined in FIPS PUB 180-1 published April 17, 1995.
41+ *
42+ * The SHA-1, produces a 160-bit message digest for a given
43+ * data stream. It should take about 2**n steps to find a
44+ * message with the same digest as a given message and
45+ * 2**(n/2) to find any two messages with the same digest,
46+ * when n is the digest size in bits. Therefore, this
47+ * algorithm can serve as a means of providing a
48+ * "fingerprint" for a message.
49+ *
50+ * Portability Issues:
51+ * SHA-1 is defined in terms of 32-bit "words". This code
52+ * uses <stdint.h> (included via "sha1.h" to define 32 and 8
53+ * bit unsigned integer types. If your C compiler does not
54+ * support 32 bit unsigned integers, this code is not
55+ * appropriate.
56+ *
57+ * Caveats:
58+ * SHA-1 is designed to work with messages less than 2^64 bits
59+ * long. Although SHA-1 allows a message digest to be generated
60+ * for messages of any number of bits less than 2^64, this
61+ * implementation only works with messages with a length that is
62+ * a multiple of the size of an 8-bit character.
63+ *
64+ */
65+
66+#include "sha1.h"
67+
68+/*
69+ * Define the SHA1 circular left shift macro
70+ */
71+#define SHA1CircularShift(bits,word) \
72+ (((word) << (bits)) | ((word) >> (32-(bits))))
73+
74+/* Local Function Prototyptes */
75+void SHA1PadMessage(SHA1Context *);
76+void SHA1ProcessMessageBlock(SHA1Context *);
77+
78+/*
79+ * SHA1Reset
80+ *
81+ * Description:
82+ * This function will initialize the SHA1Context in preparation
83+ * for computing a new SHA1 message digest.
84+ *
85+ * Parameters:
86+ * context: [in/out]
87+ * The context to reset.
88+ *
89+ * Returns:
90+ * sha Error Code.
91+ *
92+ */
93+int SHA1Reset(SHA1Context *context)
94+{
95+ if (!context)
96+ {
97+ return shaNull;
98+ }
99+
100+ context->Length_Low = 0;
101+ context->Length_High = 0;
102+ context->Message_Block_Index = 0;
103+
104+ context->Intermediate_Hash[0] = 0x67452301;
105+ context->Intermediate_Hash[1] = 0xEFCDAB89;
106+ context->Intermediate_Hash[2] = 0x98BADCFE;
107+ context->Intermediate_Hash[3] = 0x10325476;
108+ context->Intermediate_Hash[4] = 0xC3D2E1F0;
109+
110+ context->Computed = 0;
111+ context->Corrupted = 0;
112+ return shaSuccess;
113+}
114+
115+/*
116+ * SHA1Result
117+ *
118+ * Description:
119+ * This function will return the 160-bit message digest into the
120+ * Message_Digest array provided by the caller.
121+ * NOTE: The first octet of hash is stored in the 0th element,
122+ * the last octet of hash in the 19th element.
123+ *
124+ * Parameters:
125+ * context: [in/out]
126+ * The context to use to calculate the SHA-1 hash.
127+ * Message_Digest: [out]
128+ * Where the digest is returned.
129+ *
130+ * Returns:
131+ * sha Error Code.
132+ *
133+ */
134+int SHA1Result( SHA1Context *context,
135+ uint8_t Message_Digest[SHA1HashSize])
136+{
137+ int i;
138+
139+ if (!context || !Message_Digest)
140+ {
141+ return shaNull;
142+ }
143+
144+ if (context->Corrupted)
145+ {
146+ return context->Corrupted;
147+ }
148+
149+ if (!context->Computed)
150+ {
151+ SHA1PadMessage(context);
152+ for(i=0; i<64; ++i)
153+ {
154+ /* message may be sensitive, clear it out */
155+ context->Message_Block[i] = 0;
156+ }
157+ context->Length_Low = 0; /* and clear length */
158+ context->Length_High = 0;
159+ context->Computed = 1;
160+ }
161+
162+ for(i = 0; i < SHA1HashSize; ++i)
163+ {
164+ Message_Digest[i] = context->Intermediate_Hash[i>>2]
165+ >> 8 * ( 3 - ( i & 0x03 ) );
166+ }
167+
168+ return shaSuccess;
169+}
170+
171+/*
172+ * SHA1Input
173+ *
174+ * Description:
175+ * This function accepts an array of octets as the next portion
176+ * of the message.
177+ *
178+ * Parameters:
179+ * context: [in/out]
180+ * The SHA context to update
181+ * message_array: [in]
182+ * An array of characters representing the next portion of
183+ * the message.
184+ * length: [in]
185+ * The length of the message in message_array
186+ *
187+ * Returns:
188+ * sha Error Code.
189+ *
190+ */
191+int SHA1Input( SHA1Context *context,
192+ const uint8_t *message_array,
193+ unsigned length)
194+{
195+ if (!length)
196+ {
197+ return shaSuccess;
198+ }
199+
200+ if (!context || !message_array)
201+ {
202+ return shaNull;
203+ }
204+
205+ if (context->Computed)
206+ {
207+ context->Corrupted = shaStateError;
208+ return shaStateError;
209+ }
210+
211+ if (context->Corrupted)
212+ {
213+ return context->Corrupted;
214+ }
215+ while(length-- && !context->Corrupted)
216+ {
217+ context->Message_Block[context->Message_Block_Index++] =
218+ (*message_array & 0xFF);
219+
220+ context->Length_Low += 8;
221+ if (context->Length_Low == 0)
222+ {
223+ context->Length_High++;
224+ if (context->Length_High == 0)
225+ {
226+ /* Message is too long */
227+ context->Corrupted = 1;
228+ }
229+ }
230+
231+ if (context->Message_Block_Index == 64)
232+ {
233+ SHA1ProcessMessageBlock(context);
234+ }
235+
236+ message_array++;
237+ }
238+
239+ return shaSuccess;
240+}
241+
242+/*
243+ * SHA1ProcessMessageBlock
244+ *
245+ * Description:
246+ * This function will process the next 512 bits of the message
247+ * stored in the Message_Block array.
248+ *
249+ * Parameters:
250+ * None.
251+ *
252+ * Returns:
253+ * Nothing.
254+ *
255+ * Comments:
256+ * Many of the variable names in this code, especially the
257+ * single character names, were used because those were the
258+ * names used in the publication.
259+ *
260+ *
261+ */
262+void SHA1ProcessMessageBlock(SHA1Context *context)
263+{
264+ const uint32_t K[] = { /* Constants defined in SHA-1 */
265+ 0x5A827999,
266+ 0x6ED9EBA1,
267+ 0x8F1BBCDC,
268+ 0xCA62C1D6
269+ };
270+ int t; /* Loop counter */
271+ uint32_t temp; /* Temporary word value */
272+ uint32_t W[80]; /* Word sequence */
273+ uint32_t A, B, C, D, E; /* Word buffers */
274+
275+ /*
276+ * Initialize the first 16 words in the array W
277+ */
278+ for(t = 0; t < 16; t++)
279+ {
280+ W[t] = context->Message_Block[t * 4] << 24;
281+ W[t] |= context->Message_Block[t * 4 + 1] << 16;
282+ W[t] |= context->Message_Block[t * 4 + 2] << 8;
283+ W[t] |= context->Message_Block[t * 4 + 3];
284+ }
285+
286+ for(t = 16; t < 80; t++)
287+ {
288+ W[t] = SHA1CircularShift(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]);
289+ }
290+
291+ A = context->Intermediate_Hash[0];
292+ B = context->Intermediate_Hash[1];
293+ C = context->Intermediate_Hash[2];
294+ D = context->Intermediate_Hash[3];
295+ E = context->Intermediate_Hash[4];
296+
297+ for(t = 0; t < 20; t++)
298+ {
299+ temp = SHA1CircularShift(5,A) +
300+ ((B & C) | ((~B) & D)) + E + W[t] + K[0];
301+ E = D;
302+ D = C;
303+ C = SHA1CircularShift(30,B);
304+ B = A;
305+ A = temp;
306+ }
307+
308+ for(t = 20; t < 40; t++)
309+ {
310+ temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[1];
311+ E = D;
312+ D = C;
313+ C = SHA1CircularShift(30,B);
314+ B = A;
315+ A = temp;
316+ }
317+
318+ for(t = 40; t < 60; t++)
319+ {
320+ temp = SHA1CircularShift(5,A) +
321+ ((B & C) | (B & D) | (C & D)) + E + W[t] + K[2];
322+ E = D;
323+ D = C;
324+ C = SHA1CircularShift(30,B);
325+ B = A;
326+ A = temp;
327+ }
328+
329+ for(t = 60; t < 80; t++)
330+ {
331+ temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[3];
332+ E = D;
333+ D = C;
334+ C = SHA1CircularShift(30,B);
335+ B = A;
336+ A = temp;
337+ }
338+
339+ context->Intermediate_Hash[0] += A;
340+ context->Intermediate_Hash[1] += B;
341+ context->Intermediate_Hash[2] += C;
342+ context->Intermediate_Hash[3] += D;
343+ context->Intermediate_Hash[4] += E;
344+
345+ context->Message_Block_Index = 0;
346+}
347+
348+
349+/*
350+ * SHA1PadMessage
351+ *
352+ * Description:
353+ * According to the standard, the message must be padded to an even
354+ * 512 bits. The first padding bit must be a '1'. The last 64
355+ * bits represent the length of the original message. All bits in
356+ * between should be 0. This function will pad the message
357+ * according to those rules by filling the Message_Block array
358+ * accordingly. It will also call the ProcessMessageBlock function
359+ * provided appropriately. When it returns, it can be assumed that
360+ * the message digest has been computed.
361+ *
362+ * Parameters:
363+ * context: [in/out]
364+ * The context to pad
365+ * ProcessMessageBlock: [in]
366+ * The appropriate SHA*ProcessMessageBlock function
367+ * Returns:
368+ * Nothing.
369+ *
370+ */
371+
372+void SHA1PadMessage(SHA1Context *context)
373+{
374+ /*
375+ * Check to see if the current message block is too small to hold
376+ * the initial padding bits and length. If so, we will pad the
377+ * block, process it, and then continue padding into a second
378+ * block.
379+ */
380+ if (context->Message_Block_Index > 55)
381+ {
382+ context->Message_Block[context->Message_Block_Index++] = 0x80;
383+ while(context->Message_Block_Index < 64)
384+ {
385+ context->Message_Block[context->Message_Block_Index++] = 0;
386+ }
387+
388+ SHA1ProcessMessageBlock(context);
389+
390+ while(context->Message_Block_Index < 56)
391+ {
392+ context->Message_Block[context->Message_Block_Index++] = 0;
393+ }
394+ }
395+ else
396+ {
397+ context->Message_Block[context->Message_Block_Index++] = 0x80;
398+ while(context->Message_Block_Index < 56)
399+ {
400+ context->Message_Block[context->Message_Block_Index++] = 0;
401+ }
402+ }
403+
404+ /*
405+ * Store the message length as the last 8 octets
406+ */
407+ context->Message_Block[56] = context->Length_High >> 24;
408+ context->Message_Block[57] = context->Length_High >> 16;
409+ context->Message_Block[58] = context->Length_High >> 8;
410+ context->Message_Block[59] = context->Length_High;
411+ context->Message_Block[60] = context->Length_Low >> 24;
412+ context->Message_Block[61] = context->Length_Low >> 16;
413+ context->Message_Block[62] = context->Length_Low >> 8;
414+ context->Message_Block[63] = context->Length_Low;
415+
416+ SHA1ProcessMessageBlock(context);
417+}
418diff --git a/common/sha1.h b/common/sha1.h
419new file mode 100644
420index 0000000..1d49b1b
421--- /dev/null
422+++ b/common/sha1.h
423@@ -0,0 +1,101 @@
424+/*
425+ * Copyright (C) The Internet Society (2001). All Rights Reserved.
426+ *
427+ * This document and translations of it may be copied and furnished to
428+ * others, and derivative works that comment on or otherwise explain it
429+ * or assist in its implementation may be prepared, copied, published
430+ * and distributed, in whole or in part, without restriction of any
431+ * kind, provided that the above copyright notice and this paragraph are
432+ * included on all such copies and derivative works. However, this
433+ * document itself may not be modified in any way, such as by removing
434+ * the copyright notice or references to the Internet Society or other
435+ * Internet organizations, except as needed for the purpose of
436+ * developing Internet standards in which case the procedures for
437+ * copyrights defined in the Internet Standards process must be
438+ * followed, or as required to translate it into languages other than
439+ * English.
440+ *
441+ * The limited permissions granted above are perpetual and will not be
442+ * revoked by the Internet Society or its successors or assigns.
443+ *
444+ * This document and the information contained herein is provided on an
445+ * "AS IS" basis and THE INTERNET SOCIETY AND THE INTERNET ENGINEERING
446+ * TASK FORCE DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
447+ * BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION
448+ * HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF
449+ * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
450+ */
451+
452+/*
453+ * sha1.h
454+ *
455+ * Description:
456+ * This is the header file for code which implements the Secure
457+ * Hashing Algorithm 1 as defined in FIPS PUB 180-1 published
458+ * April 17, 1995.
459+ *
460+ * Many of the variable names in this code, especially the
461+ * single character names, were used because those were the names
462+ * used in the publication.
463+ *
464+ * Please read the file sha1.c for more information.
465+ *
466+ */
467+
468+
469+#ifndef _SHA1_H_
470+#define _SHA1_H_
471+
472+#include <stdint.h>
473+/*
474+ * If you do not have the ISO standard stdint.h header file, then you
475+ * must typdef the following:
476+ * name meaning
477+ * uint32_t unsigned 32 bit integer
478+ * uint8_t unsigned 8 bit integer (i.e., unsigned char)
479+ * int_least16_t integer of >= 16 bits
480+ *
481+ */
482+
483+#ifndef _SHA_enum_
484+#define _SHA_enum_
485+enum
486+{
487+ shaSuccess = 0,
488+ shaNull, /* Null pointer parameter */
489+ shaInputTooLong, /* input data too long */
490+ shaStateError /* called Input after Result */
491+};
492+#endif
493+#define SHA1HashSize 20
494+
495+/*
496+ * This structure will hold context information for the SHA-1
497+ * hashing operation
498+ */
499+typedef struct SHA1Context
500+{
501+ uint32_t Intermediate_Hash[SHA1HashSize/4]; /* Message Digest */
502+
503+ uint32_t Length_Low; /* Message length in bits */
504+ uint32_t Length_High; /* Message length in bits */
505+
506+ /* Index into message block array */
507+ int_least16_t Message_Block_Index;
508+ uint8_t Message_Block[64]; /* 512-bit message blocks */
509+
510+ int Computed; /* Is the digest computed? */
511+ int Corrupted; /* Is the message digest corrupted? */
512+} SHA1Context;
513+
514+/*
515+ * Function Prototypes
516+ */
517+int SHA1Reset( SHA1Context *);
518+int SHA1Input( SHA1Context *,
519+ const uint8_t *,
520+ unsigned int);
521+int SHA1Result( SHA1Context *,
522+ uint8_t Message_Digest[SHA1HashSize]);
523+
524+#endif