1
/* Copyright (C) 2002, 2004, 2006 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
Original Source from: http://www.faqs.org/rfcs/rfc3174.html
20
This file implements the Secure Hashing Algorithm 1 as
21
defined in FIPS PUB 180-1 published April 17, 1995.
23
The SHA-1, produces a 160-bit message digest for a given data
24
stream. It should take about 2**n steps to find a message with the
25
same digest as a given message and 2**(n/2) to find any two
26
messages with the same digest, when n is the digest size in bits.
27
Therefore, this algorithm can serve as a means of providing a
28
"fingerprint" for a message.
31
SHA-1 is defined in terms of 32-bit "words". This code uses
32
<stdint.h> (included via "sha1.h" to define 32 and 8 bit unsigned
33
integer types. If your C compiler does not support 32 bit unsigned
34
integers, this code is not appropriate.
37
SHA-1 is designed to work with messages less than 2^64 bits long.
38
Although SHA-1 allows a message digest to be generated for messages
39
of any number of bits less than 2^64, this implementation only
40
works with messages with a length that is a multiple of the size of
44
2002 by Peter Zaitsev to
45
- fit to new prototypes according to MySQL standard
47
- All checking is now done in debug only mode
51
#include "my_global.h"
56
Define the SHA1 circular left shift macro
59
#define SHA1CircularShift(bits,word) \
60
(((word) << (bits)) | ((word) >> (32-(bits))))
62
/* Local Function Prototyptes */
63
static void SHA1PadMessage(SHA1_CONTEXT*);
64
static void SHA1ProcessMessageBlock(SHA1_CONTEXT*);
68
Initialize SHA1Context
72
context [in/out] The context to reset.
75
This function will initialize the SHA1Context in preparation
76
for computing a new SHA1 message digest.
80
!= SHA_SUCCESS sha Error Code.
84
const uint32 sha_const_key[5]=
94
int mysql_sha1_reset(SHA1_CONTEXT *context)
102
context->Message_Block_Index = 0;
104
context->Intermediate_Hash[0] = sha_const_key[0];
105
context->Intermediate_Hash[1] = sha_const_key[1];
106
context->Intermediate_Hash[2] = sha_const_key[2];
107
context->Intermediate_Hash[3] = sha_const_key[3];
108
context->Intermediate_Hash[4] = sha_const_key[4];
110
context->Computed = 0;
111
context->Corrupted = 0;
118
Return the 160-bit message digest into the array provided by the caller
122
context [in/out] The context to use to calculate the SHA-1 hash.
123
Message_Digest: [out] Where the digest is returned.
126
NOTE: The first octet of hash is stored in the 0th element,
127
the last octet of hash in the 19th element.
131
!= SHA_SUCCESS sha Error Code.
134
int mysql_sha1_result(SHA1_CONTEXT *context,
135
uint8 Message_Digest[SHA1_HASH_SIZE])
140
if (!context || !Message_Digest)
143
if (context->Corrupted)
144
return context->Corrupted;
147
if (!context->Computed)
149
SHA1PadMessage(context);
150
/* message may be sensitive, clear it out */
151
bzero((char*) context->Message_Block,64);
152
context->Length = 0; /* and clear length */
153
context->Computed = 1;
156
for (i = 0; i < SHA1_HASH_SIZE; i++)
157
Message_Digest[i] = (int8)((context->Intermediate_Hash[i>>2] >> 8
158
* ( 3 - ( i & 0x03 ) )));
164
Accepts an array of octets as the next portion of the message.
168
context [in/out] The SHA context to update
169
message_array An array of characters representing the next portion
171
length The length of the message in message_array
175
!= SHA_SUCCESS sha Error Code.
178
int mysql_sha1_input(SHA1_CONTEXT *context, const uint8 *message_array,
185
/* We assume client konows what it is doing in non-debug mode */
186
if (!context || !message_array)
188
if (context->Computed)
189
return (context->Corrupted= SHA_STATE_ERROR);
190
if (context->Corrupted)
191
return context->Corrupted;
196
context->Message_Block[context->Message_Block_Index++]=
197
(*message_array & 0xFF);
198
context->Length += 8; /* Length is in bits */
202
Then we're not debugging we assume we never will get message longer
205
if (context->Length == 0)
206
return (context->Corrupted= 1); /* Message is too long */
209
if (context->Message_Block_Index == 64)
211
SHA1ProcessMessageBlock(context);
220
Process the next 512 bits of the message stored in the Message_Block array.
223
SHA1ProcessMessageBlock()
226
Many of the variable names in this code, especially the single
227
character names, were used because those were the names used in
231
/* Constants defined in SHA-1 */
232
static const uint32 K[]=
241
static void SHA1ProcessMessageBlock(SHA1_CONTEXT *context)
243
int t; /* Loop counter */
244
uint32 temp; /* Temporary word value */
245
uint32 W[80]; /* Word sequence */
246
uint32 A, B, C, D, E; /* Word buffers */
250
Initialize the first 16 words in the array W
253
for (t = 0; t < 16; t++)
256
W[t] = context->Message_Block[idx] << 24;
257
W[t] |= context->Message_Block[idx + 1] << 16;
258
W[t] |= context->Message_Block[idx + 2] << 8;
259
W[t] |= context->Message_Block[idx + 3];
263
for (t = 16; t < 80; t++)
265
W[t] = SHA1CircularShift(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]);
268
A = context->Intermediate_Hash[0];
269
B = context->Intermediate_Hash[1];
270
C = context->Intermediate_Hash[2];
271
D = context->Intermediate_Hash[3];
272
E = context->Intermediate_Hash[4];
274
for (t = 0; t < 20; t++)
276
temp= SHA1CircularShift(5,A) + ((B & C) | ((~B) & D)) + E + W[t] + K[0];
279
C = SHA1CircularShift(30,B);
284
for (t = 20; t < 40; t++)
286
temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[1];
289
C = SHA1CircularShift(30,B);
294
for (t = 40; t < 60; t++)
296
temp= (SHA1CircularShift(5,A) + ((B & C) | (B & D) | (C & D)) + E + W[t] +
300
C = SHA1CircularShift(30,B);
305
for (t = 60; t < 80; t++)
307
temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[3];
310
C = SHA1CircularShift(30,B);
315
context->Intermediate_Hash[0] += A;
316
context->Intermediate_Hash[1] += B;
317
context->Intermediate_Hash[2] += C;
318
context->Intermediate_Hash[3] += D;
319
context->Intermediate_Hash[4] += E;
321
context->Message_Block_Index = 0;
330
context: [in/out] The context to pad
333
According to the standard, the message must be padded to an even
334
512 bits. The first padding bit must be a '1'. The last 64 bits
335
represent the length of the original message. All bits in between
336
should be 0. This function will pad the message according to
337
those rules by filling the Message_Block array accordingly. It
338
will also call the ProcessMessageBlock function provided
339
appropriately. When it returns, it can be assumed that the message
340
digest has been computed.
344
static void SHA1PadMessage(SHA1_CONTEXT *context)
347
Check to see if the current message block is too small to hold
348
the initial padding bits and length. If so, we will pad the
349
block, process it, and then continue padding into a second
353
int i=context->Message_Block_Index;
357
context->Message_Block[i++] = 0x80;
358
bzero((char*) &context->Message_Block[i],
359
sizeof(context->Message_Block[0])*(64-i));
360
context->Message_Block_Index=64;
362
/* This function sets context->Message_Block_Index to zero */
363
SHA1ProcessMessageBlock(context);
365
bzero((char*) &context->Message_Block[0],
366
sizeof(context->Message_Block[0])*56);
367
context->Message_Block_Index=56;
371
context->Message_Block[i++] = 0x80;
372
bzero((char*) &context->Message_Block[i],
373
sizeof(context->Message_Block[0])*(56-i));
374
context->Message_Block_Index=56;
378
Store the message length as the last 8 octets
381
context->Message_Block[56] = (int8) (context->Length >> 56);
382
context->Message_Block[57] = (int8) (context->Length >> 48);
383
context->Message_Block[58] = (int8) (context->Length >> 40);
384
context->Message_Block[59] = (int8) (context->Length >> 32);
385
context->Message_Block[60] = (int8) (context->Length >> 24);
386
context->Message_Block[61] = (int8) (context->Length >> 16);
387
context->Message_Block[62] = (int8) (context->Length >> 8);
388
context->Message_Block[63] = (int8) (context->Length);
390
SHA1ProcessMessageBlock(context);