OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rc203soc/] [sw/] [uClinux/] [drivers/] [net/] [sha1dgst.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1626 jcastillo
/* crypto/sha/sha1dgst.c */
2
/* Copyright (C) 1995-1997 Eric Young (eay@mincom.oz.au)
3
 * All rights reserved.
4
 *
5
 * This package is an SSL implementation written
6
 * by Eric Young (eay@mincom.oz.au).
7
 * The implementation was written so as to conform with Netscapes SSL.
8
 *
9
 * Added Microsoft's Get_Key() per mppe draft by mag <mag@bunuel.tii.matav.hu>
10
 *
11
 * This library is free for commercial and non-commercial use as long as
12
 * the following conditions are aheared to.  The following conditions
13
 * apply to all code found in this distribution, be it the RC4, RSA,
14
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
15
 * included with this distribution is covered by the same copyright terms
16
 * except that the holder is Tim Hudson (tjh@mincom.oz.au).
17
 *
18
 * Copyright remains Eric Young's, and as such any Copyright notices in
19
 * the code are not to be removed.
20
 * If this package is used in a product, Eric Young should be given attribution
21
 * as the author of the parts of the library used.
22
 * This can be in the form of a textual message at program startup or
23
 * in documentation (online or textual) provided with the package.
24
 *
25
 * Redistribution and use in source and binary forms, with or without
26
 * modification, are permitted provided that the following conditions
27
 * are met:
28
 * 1. Redistributions of source code must retain the copyright
29
 *    notice, this list of conditions and the following disclaimer.
30
 * 2. Redistributions in binary form must reproduce the above copyright
31
 *    notice, this list of conditions and the following disclaimer in the
32
 *    documentation and/or other materials provided with the distribution.
33
 * 3. All advertising materials mentioning features or use of this software
34
 *    must display the following acknowledgement:
35
 *    "This product includes cryptographic software written by
36
 *     Eric Young (eay@mincom.oz.au)"
37
 *    The word 'cryptographic' can be left out if the rouines from the library
38
 *    being used are not cryptographic related :-).
39
 * 4. If you include any Windows specific code (or a derivative thereof) from
40
 *    the apps directory (application code) you must include an acknowledgement:
41
 *    "This product includes software written by Tim Hudson (tjh@mincom.oz.au)"
42
 *
43
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
44
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
47
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53
 * SUCH DAMAGE.
54
 *
55
 * The licence and distribution terms for any publically available version or
56
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
57
 * copied and put under another distribution licence
58
 * [including the GNU Public Licence.]
59
 */
60
 
61
#undef  SHA_0
62
#define SHA_1
63
#include "sha.h"
64
#include "sha_locl.h"
65
 
66
char *SHA1_version="SHA1 part of SSLeay 0.6.6 14-Jan-1997";
67
 
68
/* Implemented from SHA-1 document - The Secure Hash Algorithm
69
 */
70
 
71
#define INIT_DATA_h0 (unsigned long)0x67452301L
72
#define INIT_DATA_h1 (unsigned long)0xefcdab89L
73
#define INIT_DATA_h2 (unsigned long)0x98badcfeL
74
#define INIT_DATA_h3 (unsigned long)0x10325476L
75
#define INIT_DATA_h4 (unsigned long)0xc3d2e1f0L
76
 
77
#define K_00_19 0x5a827999L
78
#define K_20_39 0x6ed9eba1L
79
#define K_40_59 0x8f1bbcdcL
80
#define K_60_79 0xca62c1d6L
81
 
82
#ifndef NOPROTO
83
static void sha1_block(SHA_CTX *c, register unsigned long *p);
84
#else
85
static void sha1_block();
86
#endif
87
 
88
void SHA1_Init(c)
89
SHA_CTX *c;
90
        {
91
        c->h0=INIT_DATA_h0;
92
        c->h1=INIT_DATA_h1;
93
        c->h2=INIT_DATA_h2;
94
        c->h3=INIT_DATA_h3;
95
        c->h4=INIT_DATA_h4;
96
        c->Nl=0;
97
        c->Nh=0;
98
        c->num=0;
99
        }
100
 
101
void SHA1_Update(c, data, len)
102
SHA_CTX *c;
103
register unsigned char *data;
104
unsigned long len;
105
        {
106
        register ULONG *p;
107
        int ew,ec,sw,sc;
108
        ULONG l;
109
 
110
        if (len == 0) return;
111
 
112
        l=(c->Nl+(len<<3))&0xffffffff;
113
        if (l < c->Nl) /* overflow */
114
                c->Nh++;
115
        c->Nh+=(len>>29);
116
        c->Nl=l;
117
 
118
        if (c->num != 0)
119
                {
120
                p=c->data;
121
                sw=c->num>>2;
122
                sc=c->num&0x03;
123
 
124
                if ((c->num+len) >= SHA_CBLOCK)
125
                        {
126
                        l= p[sw];
127
                        p_c2nl(data,l,sc);
128
                        p[sw++]=l;
129
                        for (; sw<SHA_LBLOCK; sw++)
130
                                {
131
                                c2nl(data,l);
132
                                p[sw]=l;
133
                                }
134
                        len-=(SHA_CBLOCK-c->num);
135
 
136
                        sha1_block(c,p);
137
                        c->num=0;
138
                        /* drop through and do the rest */
139
                        }
140
                else
141
                        {
142
                        int ew,ec;
143
 
144
                        c->num+=(int)len;
145
                        if ((sc+len) < 4) /* ugly, add char's to a word */
146
                                {
147
                                l= p[sw];
148
                                p_c2nl_p(data,l,sc,len);
149
                                p[sw]=l;
150
                                }
151
                        else
152
                                {
153
                                ew=(c->num>>2);
154
                                ec=(c->num&0x03);
155
                                l= p[sw];
156
                                p_c2nl(data,l,sc);
157
                                p[sw++]=l;
158
                                for (; sw < ew; sw++)
159
                                        { c2nl(data,l); p[sw]=l; }
160
                                if (ec)
161
                                        {
162
                                        c2nl_p(data,l,ec);
163
                                        p[sw]=l;
164
                                        }
165
                                }
166
                        return;
167
                        }
168
                }
169
        /* we now can process the input data in blocks of SHA_CBLOCK
170
         * chars and save the leftovers to c->data. */
171
        p=c->data;
172
        while (len >= SHA_CBLOCK)
173
                {
174
#if defined(B_ENDIAN) || defined(L_ENDIAN)
175
                memcpy(p,data,SHA_CBLOCK);
176
                data+=SHA_CBLOCK;
177
#ifdef L_ENDIAN
178
                for (sw=(SHA_LBLOCK/4); sw; sw--)
179
                        {
180
                        Endian_Reverse32(p[0]);
181
                        Endian_Reverse32(p[1]);
182
                        Endian_Reverse32(p[2]);
183
                        Endian_Reverse32(p[3]);
184
                        p+=4;
185
                        }
186
#endif
187
#else
188
                for (sw=(SHA_BLOCK/4); sw; sw--)
189
                        {
190
                        c2nl(data,l); *(p++)=l;
191
                        c2nl(data,l); *(p++)=l;
192
                        c2nl(data,l); *(p++)=l;
193
                        c2nl(data,l); *(p++)=l;
194
                        }
195
#endif
196
                p=c->data;
197
                sha1_block(c,p);
198
                len-=SHA_CBLOCK;
199
                }
200
        ec=(int)len;
201
        c->num=ec;
202
        ew=(ec>>2);
203
        ec&=0x03;
204
 
205
        for (sw=0; sw < ew; sw++)
206
                { c2nl(data,l); p[sw]=l; }
207
        c2nl_p(data,l,ec);
208
        p[sw]=l;
209
        }
210
 
211
static void sha1_block(c, X)
212
SHA_CTX *c;
213
register unsigned long *X;
214
        {
215
        register ULONG A,B,C,D,E,T;
216
 
217
        A=c->h0;
218
        B=c->h1;
219
        C=c->h2;
220
        D=c->h3;
221
        E=c->h4;
222
 
223
        BODY_00_15( 0,A,B,C,D,E,T);
224
        BODY_00_15( 1,T,A,B,C,D,E);
225
        BODY_00_15( 2,E,T,A,B,C,D);
226
        BODY_00_15( 3,D,E,T,A,B,C);
227
        BODY_00_15( 4,C,D,E,T,A,B);
228
        BODY_00_15( 5,B,C,D,E,T,A);
229
        BODY_00_15( 6,A,B,C,D,E,T);
230
        BODY_00_15( 7,T,A,B,C,D,E);
231
        BODY_00_15( 8,E,T,A,B,C,D);
232
        BODY_00_15( 9,D,E,T,A,B,C);
233
        BODY_00_15(10,C,D,E,T,A,B);
234
        BODY_00_15(11,B,C,D,E,T,A);
235
        BODY_00_15(12,A,B,C,D,E,T);
236
        BODY_00_15(13,T,A,B,C,D,E);
237
        BODY_00_15(14,E,T,A,B,C,D);
238
        BODY_00_15(15,D,E,T,A,B,C);
239
        BODY_16_19(16,C,D,E,T,A,B);
240
        BODY_16_19(17,B,C,D,E,T,A);
241
        BODY_16_19(18,A,B,C,D,E,T);
242
        BODY_16_19(19,T,A,B,C,D,E);
243
 
244
        BODY_20_39(20,E,T,A,B,C,D);
245
        BODY_20_39(21,D,E,T,A,B,C);
246
        BODY_20_39(22,C,D,E,T,A,B);
247
        BODY_20_39(23,B,C,D,E,T,A);
248
        BODY_20_39(24,A,B,C,D,E,T);
249
        BODY_20_39(25,T,A,B,C,D,E);
250
        BODY_20_39(26,E,T,A,B,C,D);
251
        BODY_20_39(27,D,E,T,A,B,C);
252
        BODY_20_39(28,C,D,E,T,A,B);
253
        BODY_20_39(29,B,C,D,E,T,A);
254
        BODY_20_39(30,A,B,C,D,E,T);
255
        BODY_20_39(31,T,A,B,C,D,E);
256
        BODY_20_39(32,E,T,A,B,C,D);
257
        BODY_20_39(33,D,E,T,A,B,C);
258
        BODY_20_39(34,C,D,E,T,A,B);
259
        BODY_20_39(35,B,C,D,E,T,A);
260
        BODY_20_39(36,A,B,C,D,E,T);
261
        BODY_20_39(37,T,A,B,C,D,E);
262
        BODY_20_39(38,E,T,A,B,C,D);
263
        BODY_20_39(39,D,E,T,A,B,C);
264
 
265
        BODY_40_59(40,C,D,E,T,A,B);
266
        BODY_40_59(41,B,C,D,E,T,A);
267
        BODY_40_59(42,A,B,C,D,E,T);
268
        BODY_40_59(43,T,A,B,C,D,E);
269
        BODY_40_59(44,E,T,A,B,C,D);
270
        BODY_40_59(45,D,E,T,A,B,C);
271
        BODY_40_59(46,C,D,E,T,A,B);
272
        BODY_40_59(47,B,C,D,E,T,A);
273
        BODY_40_59(48,A,B,C,D,E,T);
274
        BODY_40_59(49,T,A,B,C,D,E);
275
        BODY_40_59(50,E,T,A,B,C,D);
276
        BODY_40_59(51,D,E,T,A,B,C);
277
        BODY_40_59(52,C,D,E,T,A,B);
278
        BODY_40_59(53,B,C,D,E,T,A);
279
        BODY_40_59(54,A,B,C,D,E,T);
280
        BODY_40_59(55,T,A,B,C,D,E);
281
        BODY_40_59(56,E,T,A,B,C,D);
282
        BODY_40_59(57,D,E,T,A,B,C);
283
        BODY_40_59(58,C,D,E,T,A,B);
284
        BODY_40_59(59,B,C,D,E,T,A);
285
 
286
        BODY_60_79(60,A,B,C,D,E,T);
287
        BODY_60_79(61,T,A,B,C,D,E);
288
        BODY_60_79(62,E,T,A,B,C,D);
289
        BODY_60_79(63,D,E,T,A,B,C);
290
        BODY_60_79(64,C,D,E,T,A,B);
291
        BODY_60_79(65,B,C,D,E,T,A);
292
        BODY_60_79(66,A,B,C,D,E,T);
293
        BODY_60_79(67,T,A,B,C,D,E);
294
        BODY_60_79(68,E,T,A,B,C,D);
295
        BODY_60_79(69,D,E,T,A,B,C);
296
        BODY_60_79(70,C,D,E,T,A,B);
297
        BODY_60_79(71,B,C,D,E,T,A);
298
        BODY_60_79(72,A,B,C,D,E,T);
299
        BODY_60_79(73,T,A,B,C,D,E);
300
        BODY_60_79(74,E,T,A,B,C,D);
301
        BODY_60_79(75,D,E,T,A,B,C);
302
        BODY_60_79(76,C,D,E,T,A,B);
303
        BODY_60_79(77,B,C,D,E,T,A);
304
        BODY_60_79(78,A,B,C,D,E,T);
305
        BODY_60_79(79,T,A,B,C,D,E);
306
 
307
        c->h0=(c->h0+E)&0xffffffff;
308
        c->h1=(c->h1+T)&0xffffffff;
309
        c->h2=(c->h2+A)&0xffffffff;
310
        c->h3=(c->h3+B)&0xffffffff;
311
        c->h4=(c->h4+C)&0xffffffff;
312
        }
313
 
314
void SHA1_Final(md, c)
315
unsigned char *md;
316
SHA_CTX *c;
317
        {
318
        register int i,j;
319
        register ULONG l;
320
        register ULONG *p;
321
        static unsigned char end[4]={0x80,0x00,0x00,0x00};
322
        unsigned char *cp=end;
323
 
324
        /* c->num should definitly have room for at least one more byte. */
325
        p=c->data;
326
        j=c->num;
327
        i=j>>2;
328
#ifdef PURIFY
329
        if ((j&0x03) == 0) p[i]=0;
330
#endif
331
        l=p[i];
332
        p_c2nl(cp,l,j&0x03);
333
        p[i]=l;
334
        i++;
335
        /* i is the next 'undefined word' */
336
        if (c->num >= SHA_LAST_BLOCK)
337
                {
338
                for (; i<SHA_LBLOCK; i++)
339
                        p[i]=0;
340
                sha1_block(c,p);
341
                i=0;
342
                }
343
        for (; i<(SHA_LBLOCK-2); i++)
344
                p[i]=0;
345
        p[SHA_LBLOCK-2]=c->Nh;
346
        p[SHA_LBLOCK-1]=c->Nl;
347
        sha1_block(c,p);
348
        cp=md;
349
        l=c->h0; nl2c(l,cp);
350
        l=c->h1; nl2c(l,cp);
351
        l=c->h2; nl2c(l,cp);
352
        l=c->h3; nl2c(l,cp);
353
        l=c->h4; nl2c(l,cp);
354
 
355
        /* clear stuff, sha1_block may be leaving some stuff on the stack
356
         * but I'm not worried :-) */
357
        c->num=0;
358
/*      memset((char *)&c,0,sizeof(c));*/
359
        }
360
 
361
#ifdef undef
362
int printit(l)
363
unsigned long *l;
364
        {
365
        int i,ii;
366
 
367
        for (i=0; i<2; i++)
368
                {
369
                for (ii=0; ii<8; ii++)
370
                        {
371
                        fprintf(stderr,"%08lx ",l[i*8+ii]);
372
                        }
373
                fprintf(stderr,"\n");
374
                }
375
        }
376
#endif
377
   /*
378
    * Pads used in key derivation
379
    */
380
static unsigned char  SHAPad1[40] =
381
        {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
382
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
383
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
384
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
385
static unsigned char  SHAPad2[40] =
386
        {0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2,
387
         0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2,
388
         0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2,
389
         0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2};
390
 
391
   /*
392
    * SHAInit(), SHAUpdate() and SHAFinal() functions are an
393
    * implementation of Secure Hash Algorithm (SHA-1) [7]. These are
394
    * available in public domain or can be licensed from
395
    * RSA Data Security, Inc.
396
    *
397
    * 1) H is 8 bytes long for 40 bit session keys.
398
    * 2) H is 16 bytes long for 128 bit session keys.
399
    * 3) H' is same as H when this routine is called for the first time
400
    *    for the session.
401
    * 4) The generated key is returned in H'. This is the "current" key.
402
    */
403
void
404
GetNewKeyFromSHA(StartKey, SessionKey, SessionKeyLength, InterimKey)
405
    unsigned char *StartKey;
406
    unsigned char *SessionKey;
407
    unsigned long SessionKeyLength;
408
    unsigned char *InterimKey;
409
{
410
    SHA_CTX Context;
411
    unsigned char Digest[SHA_DIGEST_LENGTH];
412
 
413
    SHA1_Init(&Context);
414
    SHA1_Update(&Context, StartKey, SessionKeyLength);
415
    SHA1_Update(&Context, SHAPad1, 40);
416
    SHA1_Update(&Context, SessionKey, SessionKeyLength);
417
    SHA1_Update(&Context, SHAPad2, 40);
418
    SHA1_Final(Digest,&Context);
419
    memcpy(InterimKey, Digest, SessionKeyLength);
420
}
421
/*==FILEVERSION 9707284==*/

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.