2 * Copyright(C) 2006 Cameron Rich
4 * This library is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 2.1 of the License, or
7 * (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * SHA1 implementation - as defined in FIPS PUB 180-1 published April 17, 1995.
21 * This code was originally taken from RFC3174
28 * Define the SHA1 circular left shift macro
30 #define SHA1CircularShift(bits,word) \
31 (((word) << (bits)) | ((word) >> (32-(bits))))
33 /* ----- static functions ----- */
34 static void SHA1PadMessage(SHA1_CTX
*ctx
);
35 static void SHA1ProcessMessageBlock(SHA1_CTX
*ctx
);
38 * Initialize the SHA1 context
40 void SHA1Init(SHA1_CTX
*ctx
)
44 ctx
->Message_Block_Index
= 0;
45 ctx
->Intermediate_Hash
[0] = 0x67452301;
46 ctx
->Intermediate_Hash
[1] = 0xEFCDAB89;
47 ctx
->Intermediate_Hash
[2] = 0x98BADCFE;
48 ctx
->Intermediate_Hash
[3] = 0x10325476;
49 ctx
->Intermediate_Hash
[4] = 0xC3D2E1F0;
53 * Accepts an array of octets as the next portion of the message.
55 void SHA1Update(SHA1_CTX
*ctx
, const uint8_t *msg
, int len
)
59 ctx
->Message_Block
[ctx
->Message_Block_Index
++] = (*msg
& 0xFF);
62 if (ctx
->Length_Low
== 0)
67 if (ctx
->Message_Block_Index
== 64)
69 SHA1ProcessMessageBlock(ctx
);
77 * Return the 160-bit message digest into the user's array
79 void SHA1Final(SHA1_CTX
*ctx
, uint8_t *digest
)
84 memset(ctx
->Message_Block
, 0, 64);
85 ctx
->Length_Low
= 0; /* and clear length */
88 for (i
= 0; i
< SHA1_SIZE
; i
++)
90 digest
[i
] = ctx
->Intermediate_Hash
[i
>>2] >> 8 * ( 3 - ( i
& 0x03 ) );
95 * Process the next 512 bits of the message stored in the array.
97 static void SHA1ProcessMessageBlock(SHA1_CTX
*ctx
)
99 const uint32_t K
[] = { /* Constants defined in SHA-1 */
105 int t
; /* Loop counter */
106 uint32_t temp
; /* Temporary word value */
107 uint32_t W
[80]; /* Word sequence */
108 uint32_t A
, B
, C
, D
, E
; /* Word buffers */
111 * Initialize the first 16 words in the array W
113 for (t
= 0; t
< 16; t
++)
115 W
[t
] = ctx
->Message_Block
[t
* 4] << 24;
116 W
[t
] |= ctx
->Message_Block
[t
* 4 + 1] << 16;
117 W
[t
] |= ctx
->Message_Block
[t
* 4 + 2] << 8;
118 W
[t
] |= ctx
->Message_Block
[t
* 4 + 3];
121 for (t
= 16; t
< 80; t
++)
123 W
[t
] = SHA1CircularShift(1,W
[t
-3] ^ W
[t
-8] ^ W
[t
-14] ^ W
[t
-16]);
126 A
= ctx
->Intermediate_Hash
[0];
127 B
= ctx
->Intermediate_Hash
[1];
128 C
= ctx
->Intermediate_Hash
[2];
129 D
= ctx
->Intermediate_Hash
[3];
130 E
= ctx
->Intermediate_Hash
[4];
132 for (t
= 0; t
< 20; t
++)
134 temp
= SHA1CircularShift(5,A
) +
135 ((B
& C
) | ((~B
) & D
)) + E
+ W
[t
] + K
[0];
138 C
= SHA1CircularShift(30,B
);
144 for (t
= 20; t
< 40; t
++)
146 temp
= SHA1CircularShift(5,A
) + (B
^ C
^ D
) + E
+ W
[t
] + K
[1];
149 C
= SHA1CircularShift(30,B
);
154 for (t
= 40; t
< 60; t
++)
156 temp
= SHA1CircularShift(5,A
) +
157 ((B
& C
) | (B
& D
) | (C
& D
)) + E
+ W
[t
] + K
[2];
160 C
= SHA1CircularShift(30,B
);
165 for (t
= 60; t
< 80; t
++)
167 temp
= SHA1CircularShift(5,A
) + (B
^ C
^ D
) + E
+ W
[t
] + K
[3];
170 C
= SHA1CircularShift(30,B
);
175 ctx
->Intermediate_Hash
[0] += A
;
176 ctx
->Intermediate_Hash
[1] += B
;
177 ctx
->Intermediate_Hash
[2] += C
;
178 ctx
->Intermediate_Hash
[3] += D
;
179 ctx
->Intermediate_Hash
[4] += E
;
180 ctx
->Message_Block_Index
= 0;
184 * According to the standard, the message must be padded to an even
185 * 512 bits. The first padding bit must be a '1'. The last 64
186 * bits represent the length of the original message. All bits in
187 * between should be 0. This function will pad the message
188 * according to those rules by filling the Message_Block array
189 * accordingly. It will also call the ProcessMessageBlock function
190 * provided appropriately. When it returns, it can be assumed that
191 * the message digest has been computed.
193 * @param ctx [in, out] The SHA1 context
195 static void SHA1PadMessage(SHA1_CTX
*ctx
)
198 * Check to see if the current message block is too small to hold
199 * the initial padding bits and length. If so, we will pad the
200 * block, process it, and then continue padding into a second
203 if (ctx
->Message_Block_Index
> 55)
205 ctx
->Message_Block
[ctx
->Message_Block_Index
++] = 0x80;
206 while(ctx
->Message_Block_Index
< 64)
208 ctx
->Message_Block
[ctx
->Message_Block_Index
++] = 0;
211 SHA1ProcessMessageBlock(ctx
);
213 while (ctx
->Message_Block_Index
< 56)
215 ctx
->Message_Block
[ctx
->Message_Block_Index
++] = 0;
220 ctx
->Message_Block
[ctx
->Message_Block_Index
++] = 0x80;
221 while(ctx
->Message_Block_Index
< 56)
224 ctx
->Message_Block
[ctx
->Message_Block_Index
++] = 0;
229 * Store the message length as the last 8 octets
231 ctx
->Message_Block
[56] = ctx
->Length_High
>> 24;
232 ctx
->Message_Block
[57] = ctx
->Length_High
>> 16;
233 ctx
->Message_Block
[58] = ctx
->Length_High
>> 8;
234 ctx
->Message_Block
[59] = ctx
->Length_High
;
235 ctx
->Message_Block
[60] = ctx
->Length_Low
>> 24;
236 ctx
->Message_Block
[61] = ctx
->Length_Low
>> 16;
237 ctx
->Message_Block
[62] = ctx
->Length_Low
>> 8;
238 ctx
->Message_Block
[63] = ctx
->Length_Low
;
239 SHA1ProcessMessageBlock(ctx
);