]> code.citadel.org Git - citadel.git/blob - citadel/sha1.c
ad2713b260df3f2ce09805ea2c8b595514ae77ab
[citadel.git] / citadel / sha1.c
1 /*
2  *  sha1.c
3  *
4  *  Description:
5  *      This file implements the Secure Hashing Algorithm 1 as
6  *      defined in FIPS PUB 180-1 published April 17, 1995.
7  *
8  *      The SHA-1, produces a 160-bit message digest for a given
9  *      data stream.  It should take about 2**n steps to find a
10  *      message with the same digest as a given message and
11  *      2**(n/2) to find any two messages with the same digest,
12  *      when n is the digest size in bits.  Therefore, this
13  *      algorithm can serve as a means of providing a
14  *      "fingerprint" for a message.
15  *
16  *  Portability Issues:
17  *      SHA-1 is defined in terms of 32-bit "words".  This code
18  *      uses <stdint.h> (included via "sha1.h" to define 32 and 8
19  *      bit unsigned integer types.  If your C compiler does not
20  *      support 32 bit unsigned integers, this code is not
21  *      appropriate.
22  *
23  *  Caveats:
24  *      SHA-1 is designed to work with messages less than 2^64 bits
25  *      long.  Although SHA-1 allows a message digest to be generated
26  *      for messages of any number of bits less than 2^64, this
27  *      implementation only works with messages with a length that is
28  *      a multiple of the size of an 8-bit character.
29  *
30  */
31
32
33
34 #include "sha1.h"
35
36 /*
37  *  Define the SHA1 circular left shift macro
38  */
39 #define SHA1CircularShift(bits,word) \
40                 (((word) << (bits)) | ((word) >> (32-(bits))))
41
42 /* Local Function Prototyptes */
43 void SHA1PadMessage(SHA1Context *);
44 void SHA1ProcessMessageBlock(SHA1Context *);
45
46 /*
47  *  SHA1Reset
48  *
49  *  Description:
50  *      This function will initialize the SHA1Context in preparation
51  *      for computing a new SHA1 message digest.
52  *
53  *  Parameters:
54  *      context: [in/out]
55  *          The context to reset.
56  *
57  *  Returns:
58  *      sha Error Code.
59  *
60  */
61 int SHA1Reset(SHA1Context * context)
62 {
63         if (!context) {
64                 return shaNull;
65         }
66
67         context->Length_Low = 0;
68         context->Length_High = 0;
69         context->Message_Block_Index = 0;
70
71         context->Intermediate_Hash[0] = 0x67452301;
72         context->Intermediate_Hash[1] = 0xEFCDAB89;
73         context->Intermediate_Hash[2] = 0x98BADCFE;
74         context->Intermediate_Hash[3] = 0x10325476;
75         context->Intermediate_Hash[4] = 0xC3D2E1F0;
76
77         context->Computed = 0;
78         context->Corrupted = 0;
79
80         return shaSuccess;
81 }
82
83 /*
84  *  SHA1Result
85  *
86  *  Description:
87  *      This function will return the 160-bit message digest into the
88  *      Message_Digest array  provided by the caller.
89  *      NOTE: The first octet of hash is stored in the 0th element,
90  *            the last octet of hash in the 19th element.
91  *
92  *  Parameters:
93  *      context: [in/out]
94  *          The context to use to calculate the SHA-1 hash.
95  *      Message_Digest: [out]
96  *          Where the digest is returned.
97  *
98  *  Returns:
99  *      sha Error Code.
100  *
101  */
102 int SHA1Result(SHA1Context * context, uint8_t Message_Digest[SHA1HashSize])
103 {
104         int i;
105
106         if (!context || !Message_Digest) {
107                 return shaNull;
108         }
109
110         if (context->Corrupted) {
111                 return context->Corrupted;
112         }
113
114         if (!context->Computed) {
115                 SHA1PadMessage(context);
116                 for (i = 0; i < 64; ++i) {
117                         /* message may be sensitive, clear it out */
118                         context->Message_Block[i] = 0;
119                 }
120                 context->Length_Low = 0;        /* and clear length */
121                 context->Length_High = 0;
122                 context->Computed = 1;
123         }
124
125         for (i = 0; i < SHA1HashSize; ++i) {
126                 Message_Digest[i] = context->Intermediate_Hash[i >> 2]
127                     >> 8 * (3 - (i & 0x03));
128         }
129
130         return shaSuccess;
131 }
132
133 /*
134  *  SHA1Input
135  *
136  *  Description:
137  *      This function accepts an array of octets as the next portion
138  *      of the message.
139  *
140  *  Parameters:
141  *      context: [in/out]
142  *          The SHA context to update
143  *      message_array: [in]
144  *          An array of characters representing the next portion of
145  *          the message.
146  *      length: [in]
147  *          The length of the message in message_array
148  *
149  *  Returns:
150  *      sha Error Code.
151  *
152  */
153 int SHA1Input(SHA1Context * context,
154               const uint8_t * message_array, unsigned length)
155 {
156         if (!length) {
157                 return shaSuccess;
158         }
159
160         if (!context || !message_array) {
161                 return shaNull;
162         }
163
164         if (context->Computed) {
165                 context->Corrupted = shaStateError;
166                 return shaStateError;
167         }
168
169         if (context->Corrupted) {
170                 return context->Corrupted;
171         }
172         while (length-- && !context->Corrupted) {
173                 context->Message_Block[context->Message_Block_Index++] =
174                     (*message_array & 0xFF);
175
176                 context->Length_Low += 8;
177                 if (context->Length_Low == 0) {
178                         context->Length_High++;
179                         if (context->Length_High == 0) {
180                                 /* Message is too long */
181                                 context->Corrupted = 1;
182                         }
183                 }
184
185                 if (context->Message_Block_Index == 64) {
186                         SHA1ProcessMessageBlock(context);
187                 }
188
189                 message_array++;
190         }
191
192         return shaSuccess;
193 }
194
195 /*
196  *  SHA1ProcessMessageBlock
197  *
198  *  Description:
199  *      This function will process the next 512 bits of the message
200  *      stored in the Message_Block array.
201  *
202  *  Parameters:
203  *      None.
204  *
205  *  Returns:
206  *      Nothing.
207  *
208  *  Comments:
209  *      Many of the variable names in this code, especially the
210  *      single character names, were used because those were the
211  *      names used in the publication.
212  *
213  *
214  */
215 void SHA1ProcessMessageBlock(SHA1Context * context)
216 {
217         const uint32_t K[] = {  /* Constants defined in SHA-1   */
218                 0x5A827999,
219                 0x6ED9EBA1,
220                 0x8F1BBCDC,
221                 0xCA62C1D6
222         };
223         int t;                  /* Loop counter                */
224         uint32_t temp;          /* Temporary word value        */
225         uint32_t W[80];         /* Word sequence               */
226         uint32_t A, B, C, D, E; /* Word buffers                */
227
228         /*
229          *  Initialize the first 16 words in the array W
230          */
231         for (t = 0; t < 16; t++) {
232                 W[t] = context->Message_Block[t * 4] << 24;
233                 W[t] |= context->Message_Block[t * 4 + 1] << 16;
234                 W[t] |= context->Message_Block[t * 4 + 2] << 8;
235                 W[t] |= context->Message_Block[t * 4 + 3];
236         }
237
238         for (t = 16; t < 80; t++) {
239                 W[t] =
240                     SHA1CircularShift(1,
241                                       W[t - 3] ^ W[t - 8] ^ W[t -
242                                                               14] ^ W[t -
243                                                                       16]);
244         }
245
246         A = context->Intermediate_Hash[0];
247         B = context->Intermediate_Hash[1];
248         C = context->Intermediate_Hash[2];
249         D = context->Intermediate_Hash[3];
250         E = context->Intermediate_Hash[4];
251
252         for (t = 0; t < 20; t++) {
253                 temp = SHA1CircularShift(5, A) +
254                     ((B & C) | ((~B) & D)) + E + W[t] + K[0];
255                 E = D;
256                 D = C;
257                 C = SHA1CircularShift(30, B);
258                 B = A;
259                 A = temp;
260         }
261
262         for (t = 20; t < 40; t++) {
263                 temp =
264                     SHA1CircularShift(5,
265                                       A) + (B ^ C ^ D) + E + W[t] + K[1];
266                 E = D;
267                 D = C;
268                 C = SHA1CircularShift(30, B);
269                 B = A;
270                 A = temp;
271         }
272
273         for (t = 40; t < 60; t++) {
274                 temp = SHA1CircularShift(5, A) +
275                     ((B & C) | (B & D) | (C & D)) + E + W[t] + K[2];
276                 E = D;
277                 D = C;
278                 C = SHA1CircularShift(30, B);
279                 B = A;
280                 A = temp;
281         }
282
283         for (t = 60; t < 80; t++) {
284                 temp =
285                     SHA1CircularShift(5,
286                                       A) + (B ^ C ^ D) + E + W[t] + K[3];
287                 E = D;
288                 D = C;
289                 C = SHA1CircularShift(30, B);
290                 B = A;
291                 A = temp;
292         }
293
294         context->Intermediate_Hash[0] += A;
295         context->Intermediate_Hash[1] += B;
296         context->Intermediate_Hash[2] += C;
297         context->Intermediate_Hash[3] += D;
298         context->Intermediate_Hash[4] += E;
299
300         context->Message_Block_Index = 0;
301 }
302
303
304 /*
305  *  SHA1PadMessage
306  *
307  *  Description:
308  *      According to the standard, the message must be padded to an even
309  *      512 bits.  The first padding bit must be a '1'.  The last 64
310  *      bits represent the length of the original message.  All bits in
311  *      between should be 0.  This function will pad the message
312  *      according to those rules by filling the Message_Block array
313  *      accordingly.  It will also call the ProcessMessageBlock function
314  *      provided appropriately.  When it returns, it can be assumed that
315  *      the message digest has been computed.
316  *
317  *  Parameters:
318  *      context: [in/out]
319  *          The context to pad
320  *      ProcessMessageBlock: [in]
321  *          The appropriate SHA*ProcessMessageBlock function
322  *  Returns:
323  *      Nothing.
324  *
325  */
326
327 void SHA1PadMessage(SHA1Context * context)
328 {
329         /*
330          *  Check to see if the current message block is too small to hold
331          *  the initial padding bits and length.  If so, we will pad the
332          *  block, process it, and then continue padding into a second
333          *  block.
334          */
335         if (context->Message_Block_Index > 55) {
336                 context->Message_Block[context->Message_Block_Index++] =
337                     0x80;
338                 while (context->Message_Block_Index < 64) {
339                         context->Message_Block[context->
340                                                Message_Block_Index++] = 0;
341                 }
342
343                 SHA1ProcessMessageBlock(context);
344
345                 while (context->Message_Block_Index < 56) {
346                         context->Message_Block[context->
347                                                Message_Block_Index++] = 0;
348                 }
349         } else {
350                 context->Message_Block[context->Message_Block_Index++] =
351                     0x80;
352                 while (context->Message_Block_Index < 56) {
353                         context->Message_Block[context->
354                                                Message_Block_Index++] = 0;
355                 }
356         }
357
358         /*
359          *  Store the message length as the last 8 octets
360          */
361         context->Message_Block[56] = context->Length_High >> 24;
362         context->Message_Block[57] = context->Length_High >> 16;
363         context->Message_Block[58] = context->Length_High >> 8;
364         context->Message_Block[59] = context->Length_High;
365         context->Message_Block[60] = context->Length_Low >> 24;
366         context->Message_Block[61] = context->Length_Low >> 16;
367         context->Message_Block[62] = context->Length_Low >> 8;
368         context->Message_Block[63] = context->Length_Low;
369
370         SHA1ProcessMessageBlock(context);
371 }