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