3 * \brief Based on the RFC 6234
5 * Copyright (c) 2011 IETF Trust and the persons identified as
6 * authors of the code. All rights reserved.
8 * Redistribution and use in source and binary forms, with or
9 * without modification, are permitted provided that the following
12 * - Redistributions of source code must retain the above
13 * copyright notice, this list of conditions and
14 * the following disclaimer.
16 * - Redistributions in binary form must reproduce the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer in the documentation and/or other materials provided
19 * with the distribution.
21 * - Neither the name of Internet Society, IETF or IETF Trust, nor
22 * the names of specific contributors, may be used to endorse or
23 * promote products derived from this software without specific
24 * prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
27 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
28 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
29 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
31 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
33 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
37 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
38 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 * This file implements the Secure Hash Algorithm SHA-1
42 * as defined in the U.S. National Institute of Standards
43 * and Technology Federal Information Processing Standards
44 * Publication (FIPS PUB) 180-3 published in October 2008
45 * and formerly defined in its predecessors, FIPS PUB 180-1
48 * A combined document showing all algorithms is available at
49 * http://csrc.nist.gov/publications/fips/
50 * fips180-3/fips180-3_final.pdf
52 * The SHA-1 algorithm produces a 160-bit message digest for a
53 * given data stream that can serve as a means of providing a
54 * "fingerprint" for a message.
57 * SHA-1 is defined in terms of 32-bit "words". This code
58 * uses <stdint.h> (included via "sha.h") to define 32- and
59 * 8-bit unsigned integer types. If your C compiler does
60 * not support 32-bit unsigned integers, this code is not
64 * SHA-1 is designed to work with messages less than 2^64 bits
65 * long. This implementation uses SHA1Input() to hash the bits
66 * that are a multiple of the size of an 8-bit octet, and then
67 * optionally uses SHA1FinalBits() to hash the final few bits of
71 #include <asterisk/sha1.h>
73 /*! Define the SHA1 circular left shift macro */
74 #define SHA1_ROTL(bits,word) \
75 (((word) << (bits)) | ((word) >> (32-(bits))))
78 * Add "length" to the length.
79 * Set Corrupted when overflow has occurred.
81 static uint32_t addTemp;
82 #define SHA1AddLength(context, length) \
83 (addTemp = (context)->Length_Low, \
84 (context)->Corrupted = \
85 (((context)->Length_Low += (length)) < addTemp) && \
86 (++(context)->Length_High == 0) ? shaInputTooLong \
87 : (context)->Corrupted )
89 /* Local Function Prototypes */
90 static void SHA1ProcessMessageBlock(SHA1Context * context);
91 static void SHA1Finalize(SHA1Context * context, uint8_t Pad_Byte);
92 static void SHA1PadMessage(SHA1Context * context, uint8_t Pad_Byte);
96 * \param context the context to be reset.
97 * This function will initialize the SHA1Context in preparation
98 * for computing a new SHA1 message digest.
99 * \return sha Error Code.
101 int SHA1Reset(SHA1Context *context)
107 context->Length_High = context->Length_Low = 0;
108 context->Message_Block_Index = 0;
110 /* Initial Hash Values: FIPS 180-3 section 5.3.1 */
111 context->Intermediate_Hash[0] = 0x67452301;
112 context->Intermediate_Hash[1] = 0xEFCDAB89;
113 context->Intermediate_Hash[2] = 0x98BADCFE;
114 context->Intermediate_Hash[3] = 0x10325476;
115 context->Intermediate_Hash[4] = 0xC3D2E1F0;
117 context->Computed = 0;
118 context->Corrupted = shaSuccess;
125 * \param context [in/out] The SHA context to update
126 * \param message_array [in] An array of characters representing the next portion of
128 * \param length [in] The length of the message in message_array.
129 * This function accepts an array of octets as the next portion
131 * \return sha Error Code.
133 int SHA1Input(SHA1Context *context,
134 const uint8_t *message_array, unsigned length)
142 if (!message_array) {
146 if (context->Computed) {
147 context->Corrupted = shaStateError;
148 return shaStateError;
151 if (context->Corrupted) {
152 return context->Corrupted;
156 context->Message_Block[context->Message_Block_Index++] =
159 if ((SHA1AddLength(context, 8) == shaSuccess) &&
160 (context->Message_Block_Index == SHA1_Message_Block_Size))
161 SHA1ProcessMessageBlock(context);
166 return context->Corrupted;
170 * \brief SHA1FinalBits Add in any final bits of the message.
172 * \param context [in/out] The SHA context to update.
173 * \param message_bits [in] The final bits of the message, in the upper portion of the
174 * byte. (Use 0b###00000 instead of 0b00000### to input the
176 * \param length [in] * The number of bits in message_bits, between 1 and 7.
177 * \returns sha Error Code.
179 int SHA1FinalBits(SHA1Context * context, uint8_t message_bits,
182 static uint8_t masks[8] = {
183 /* 0 0b00000000 */ 0x00, /* 1 0b10000000 */ 0x80,
184 /* 2 0b11000000 */ 0xC0, /* 3 0b11100000 */ 0xE0,
185 /* 4 0b11110000 */ 0xF0, /* 5 0b11111000 */ 0xF8,
186 /* 6 0b11111100 */ 0xFC, /* 7 0b11111110 */ 0xFE
189 static uint8_t markbit[8] = {
190 /* 0 0b10000000 */ 0x80, /* 1 0b01000000 */ 0x40,
191 /* 2 0b00100000 */ 0x20, /* 3 0b00010000 */ 0x10,
192 /* 4 0b00001000 */ 0x08, /* 5 0b00000100 */ 0x04,
193 /* 6 0b00000010 */ 0x02, /* 7 0b00000001 */ 0x01
200 if (context->Corrupted)
201 return context->Corrupted;
202 if (context->Computed)
203 return context->Corrupted = shaStateError;
205 return context->Corrupted = shaBadParam;
207 SHA1AddLength(context, length);
208 SHA1Finalize(context,
209 (uint8_t) ((message_bits & masks[length]) |
212 return context->Corrupted;
216 * \brief SHA1Result Returns the resulting 160-bit digest
217 * \param context [in/out] The SHA context to update.
218 * \param Message_Digest [out] Where the digest is returned.
220 * This function will return the 160-bit message digest
221 * into the Message_Digest array provided by the caller.
222 * \note The first octet of hash is stored in the element with index 0,
223 * the last octet of hash in the element with index 19.
224 * \returns sha Error Code.
226 int SHA1Result(SHA1Context * context, uint8_t Message_Digest[SHA1HashSize])
233 if (!Message_Digest) {
236 if (context->Corrupted) {
237 return context->Corrupted;
240 if (!context->Computed) {
241 SHA1Finalize(context, 0x80);
244 for (i = 0; i < SHA1HashSize; ++i) {
245 Message_Digest[i] = (uint8_t) (context->Intermediate_Hash[i >> 2]
246 >> (8 * (3 - (i & 0x03))));
253 * \brief Process the next 512 bits of the message stored in the Message_Block array.
254 * \param context [in/out] The SHA context to update
255 * \note Many of the variable names in this code, especially the
256 * single character names, were used because those were the
257 * names used in the publication.
260 static void SHA1ProcessMessageBlock(SHA1Context *context)
262 /* Constants defined in FIPS 180-3, section 4.2.1 */
263 const uint32_t K[4] = {
264 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6
266 int t; /* Loop counter */
267 uint32_t temp; /* Temporary word value */
268 uint32_t W[80]; /* Word sequence */
269 uint32_t A, B, C, D, E; /* Word buffers */
272 * Initialize the first 16 words in the array W
274 for (t = 0; t < 16; t++) {
275 W[t] = ((uint32_t) context->Message_Block[t * 4]) << 24;
276 W[t] |= ((uint32_t) context->Message_Block[t * 4 + 1]) << 16;
277 W[t] |= ((uint32_t) context->Message_Block[t * 4 + 2]) << 8;
278 W[t] |= ((uint32_t) context->Message_Block[t * 4 + 3]);
281 for (t = 16; t < 80; t++) {
282 W[t] = SHA1_ROTL(1, W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16]);
285 A = context->Intermediate_Hash[0];
286 B = context->Intermediate_Hash[1];
287 C = context->Intermediate_Hash[2];
288 D = context->Intermediate_Hash[3];
289 E = context->Intermediate_Hash[4];
291 for (t = 0; t < 20; t++) {
292 temp = SHA1_ROTL(5, A) + SHA_Ch(B, C, D) + E + W[t] + K[0];
295 C = SHA1_ROTL(30, B);
300 for (t = 20; t < 40; t++) {
301 temp = SHA1_ROTL(5, A) + SHA_Parity(B, C, D) + E + W[t] + K[1];
304 C = SHA1_ROTL(30, B);
309 for (t = 40; t < 60; t++) {
310 temp = SHA1_ROTL(5, A) + SHA_Maj(B, C, D) + E + W[t] + K[2];
313 C = SHA1_ROTL(30, B);
318 for (t = 60; t < 80; t++) {
319 temp = SHA1_ROTL(5, A) + SHA_Parity(B, C, D) + E + W[t] + K[3];
322 C = SHA1_ROTL(30, B);
327 context->Intermediate_Hash[0] += A;
328 context->Intermediate_Hash[1] += B;
329 context->Intermediate_Hash[2] += C;
330 context->Intermediate_Hash[3] += D;
331 context->Intermediate_Hash[4] += E;
333 context->Message_Block_Index = 0;
337 * \brief This helper function finishes off the digest calculations.
338 * \param context [in/out] The context to pad.
339 * \param Pad_byte [in] The last byte to add to the message block
340 * before the 0-padding and length. This will contain the last
341 * bits of the message followed by another single bit. If the
342 * message was an exact multiple of 8-bits long, Pad_Byte will
344 * \returns sha Error Code.
346 static void SHA1Finalize(SHA1Context * context, uint8_t Pad_Byte)
349 SHA1PadMessage(context, Pad_Byte);
350 /* message may be sensitive, clear it out */
351 for (i = 0; i < SHA1_Message_Block_Size; ++i) {
352 context->Message_Block[i] = 0;
354 context->Length_High = 0; /* and clear length */
355 context->Length_Low = 0;
356 context->Computed = 1;
360 * \brief Pad message to be 512 bits.
361 * \param context [in/out] The context to pad.
362 * \param Pad_byte [in] Last padding byte.
364 * According to the standard, the message must be padded to the next
365 * even multiple of 512 bits. The first padding bit must be a '1'.
366 * The last 64 bits represent the length of the original message.
367 * All bits in between should be 0. This helper function will pad
368 * the message according to those rules by filling the Message_Block
369 * array accordingly. When it returns, it can be assumed that the
370 * message digest has been computed.
374 static void SHA1PadMessage(SHA1Context * context, uint8_t Pad_Byte)
377 * Check to see if the current message block is too small to hold
378 * the initial padding bits and length. If so, we will pad the
379 * block, process it, and then continue padding into a second
382 if (context->Message_Block_Index >= (SHA1_Message_Block_Size - 8)) {
383 context->Message_Block[context->Message_Block_Index++] = Pad_Byte;
384 while (context->Message_Block_Index < SHA1_Message_Block_Size) {
385 context->Message_Block[context->Message_Block_Index++] = 0;
388 SHA1ProcessMessageBlock(context);
390 context->Message_Block[context->Message_Block_Index++] = Pad_Byte;
392 while (context->Message_Block_Index < (SHA1_Message_Block_Size - 8)) {
393 context->Message_Block[context->Message_Block_Index++] = 0;
397 * Store the message length as the last 8 octets
399 context->Message_Block[56] = (uint8_t) (context->Length_High >> 24);
400 context->Message_Block[57] = (uint8_t) (context->Length_High >> 16);
401 context->Message_Block[58] = (uint8_t) (context->Length_High >> 8);
402 context->Message_Block[59] = (uint8_t) (context->Length_High);
403 context->Message_Block[60] = (uint8_t) (context->Length_Low >> 24);
404 context->Message_Block[61] = (uint8_t) (context->Length_Low >> 16);
405 context->Message_Block[62] = (uint8_t) (context->Length_Low >> 8);
406 context->Message_Block[63] = (uint8_t) (context->Length_Low);
408 SHA1ProcessMessageBlock(context);