~hikiko/nux/arb-srgba-shader

« back to all changes in this revision

Viewing changes to NuxCore/Crypto/NSHA1.cpp

  • Committer: Neil Jagdish Patel
  • Date: 2010-09-02 03:28:11 UTC
  • Revision ID: neil.patel@canonical.com-20100902032811-i2m18tfb6pkasnvt
Remove Win EOL chars

Show diffs side-by-side

added added

removed removed

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