source: trunk/Softwares/MiBench/src/c/sha-sha.c @ 135

Last change on this file since 135 was 117, checked in by rosiere, 15 years ago

1) Platforms : add new organization for test
2) Load_Store_Unit : add array to count nb_check in store_queue
3) Issue_queue and Core_Glue : rewrite the issue network
4) Special_Register_Unit : add reset value to register CID
5) Softwares : add multicontext test
6) Softwares : add SPECINT
7) Softwares : add MiBench?
7) Read_queue : inhib access for r0
8) Change Core_Glue (network) - dont yet support priority and load balancing scheme

  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 5.5 KB
Line 
1/* NIST Secure Hash Algorithm */
2/* heavily modified by Uwe Hollerbach uh@alumni.caltech edu */
3/* from Peter C. Gutmann's implementation as found in */
4/* Applied Cryptography by Bruce Schneier */
5
6/* NIST's proposed modification to SHA of 7/11/94 may be */
7/* activated by defining USE_MODIFIED_SHA */
8
9#include <stdlib.h>
10#include <stdio.h>
11#include <string.h>
12#include "sha-sha.h"
13
14/* SHA f()-functions */
15
16#define f1(x,y,z)       ((x & y) | (~x & z))
17#define f2(x,y,z)       (x ^ y ^ z)
18#define f3(x,y,z)       ((x & y) | (x & z) | (y & z))
19#define f4(x,y,z)       (x ^ y ^ z)
20
21/* SHA constants */
22
23#define CONST1          0x5a827999L
24#define CONST2          0x6ed9eba1L
25#define CONST3          0x8f1bbcdcL
26#define CONST4          0xca62c1d6L
27
28/* 32-bit rotate */
29
30#define ROT32(x,n)      ((x << n) | (x >> (32 - n)))
31
32#define FUNC(n,i)                                               \
33    temp = ROT32(A,5) + f##n(B,C,D) + E + W[i] + CONST##n;      \
34    E = D; D = C; C = ROT32(B,30); B = A; A = temp
35
36/* do SHA transformation */
37
38static void sha_transform(SHA_INFO *sha_info)
39{
40    int i;
41    LONG temp, A, B, C, D, E, W[80];
42
43    for (i = 0; i < 16; ++i) {
44        W[i] = sha_info->data[i];
45    }
46    for (i = 16; i < 80; ++i) {
47        W[i] = W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16];
48#ifdef USE_MODIFIED_SHA
49        W[i] = ROT32(W[i], 1);
50#endif /* USE_MODIFIED_SHA */
51    }
52    A = sha_info->digest[0];
53    B = sha_info->digest[1];
54    C = sha_info->digest[2];
55    D = sha_info->digest[3];
56    E = sha_info->digest[4];
57#ifdef UNROLL_LOOPS
58    FUNC(1, 0);  FUNC(1, 1);  FUNC(1, 2);  FUNC(1, 3);  FUNC(1, 4);
59    FUNC(1, 5);  FUNC(1, 6);  FUNC(1, 7);  FUNC(1, 8);  FUNC(1, 9);
60    FUNC(1,10);  FUNC(1,11);  FUNC(1,12);  FUNC(1,13);  FUNC(1,14);
61    FUNC(1,15);  FUNC(1,16);  FUNC(1,17);  FUNC(1,18);  FUNC(1,19);
62
63    FUNC(2,20);  FUNC(2,21);  FUNC(2,22);  FUNC(2,23);  FUNC(2,24);
64    FUNC(2,25);  FUNC(2,26);  FUNC(2,27);  FUNC(2,28);  FUNC(2,29);
65    FUNC(2,30);  FUNC(2,31);  FUNC(2,32);  FUNC(2,33);  FUNC(2,34);
66    FUNC(2,35);  FUNC(2,36);  FUNC(2,37);  FUNC(2,38);  FUNC(2,39);
67
68    FUNC(3,40);  FUNC(3,41);  FUNC(3,42);  FUNC(3,43);  FUNC(3,44);
69    FUNC(3,45);  FUNC(3,46);  FUNC(3,47);  FUNC(3,48);  FUNC(3,49);
70    FUNC(3,50);  FUNC(3,51);  FUNC(3,52);  FUNC(3,53);  FUNC(3,54);
71    FUNC(3,55);  FUNC(3,56);  FUNC(3,57);  FUNC(3,58);  FUNC(3,59);
72
73    FUNC(4,60);  FUNC(4,61);  FUNC(4,62);  FUNC(4,63);  FUNC(4,64);
74    FUNC(4,65);  FUNC(4,66);  FUNC(4,67);  FUNC(4,68);  FUNC(4,69);
75    FUNC(4,70);  FUNC(4,71);  FUNC(4,72);  FUNC(4,73);  FUNC(4,74);
76    FUNC(4,75);  FUNC(4,76);  FUNC(4,77);  FUNC(4,78);  FUNC(4,79);
77#else /* !UNROLL_LOOPS */
78    for (i = 0; i < 20; ++i) {
79        FUNC(1,i);
80    }
81    for (i = 20; i < 40; ++i) {
82        FUNC(2,i);
83    }
84    for (i = 40; i < 60; ++i) {
85        FUNC(3,i);
86    }
87    for (i = 60; i < 80; ++i) {
88        FUNC(4,i);
89    }
90#endif /* !UNROLL_LOOPS */
91    sha_info->digest[0] += A;
92    sha_info->digest[1] += B;
93    sha_info->digest[2] += C;
94    sha_info->digest[3] += D;
95    sha_info->digest[4] += E;
96}
97
98#ifdef LITTLE_ENDIAN
99
100/* change endianness of data */
101
102static void byte_reverse(LONG *buffer, int count)
103{
104    int i;
105    BYTE ct[4], *cp;
106
107    count /= sizeof(LONG);
108    cp = (BYTE *) buffer;
109    for (i = 0; i < count; ++i) {
110        ct[0] = cp[0];
111        ct[1] = cp[1];
112        ct[2] = cp[2];
113        ct[3] = cp[3];
114        cp[0] = ct[3];
115        cp[1] = ct[2];
116        cp[2] = ct[1];
117        cp[3] = ct[0];
118        cp += sizeof(LONG);
119    }
120}
121
122#endif /* LITTLE_ENDIAN */
123
124/* initialize the SHA digest */
125
126void sha_init(SHA_INFO *sha_info)
127{
128    sha_info->digest[0] = 0x67452301L;
129    sha_info->digest[1] = 0xefcdab89L;
130    sha_info->digest[2] = 0x98badcfeL;
131    sha_info->digest[3] = 0x10325476L;
132    sha_info->digest[4] = 0xc3d2e1f0L;
133    sha_info->count_lo = 0L;
134    sha_info->count_hi = 0L;
135}
136
137/* update the SHA digest */
138
139void sha_update(SHA_INFO *sha_info, BYTE *buffer, int count)
140{
141    if ((sha_info->count_lo + ((LONG) count << 3)) < sha_info->count_lo) {
142        ++sha_info->count_hi;
143    }
144    sha_info->count_lo += (LONG) count << 3;
145    sha_info->count_hi += (LONG) count >> 29;
146    while (count >= SHA_BLOCKSIZE) {
147        memcpy(sha_info->data, buffer, SHA_BLOCKSIZE);
148#ifdef LITTLE_ENDIAN
149        byte_reverse(sha_info->data, SHA_BLOCKSIZE);
150#endif /* LITTLE_ENDIAN */
151        sha_transform(sha_info);
152        buffer += SHA_BLOCKSIZE;
153        count -= SHA_BLOCKSIZE;
154    }
155    memcpy(sha_info->data, buffer, count);
156}
157
158/* finish computing the SHA digest */
159
160void sha_final(SHA_INFO *sha_info)
161{
162    int count;
163    LONG lo_bit_count, hi_bit_count;
164
165    lo_bit_count = sha_info->count_lo;
166    hi_bit_count = sha_info->count_hi;
167    count = (int) ((lo_bit_count >> 3) & 0x3f);
168    ((BYTE *) sha_info->data)[count++] = 0x80;
169    if (count > 56) {
170        memset((BYTE *) &sha_info->data + count, 0, 64 - count);
171#ifdef LITTLE_ENDIAN
172        byte_reverse(sha_info->data, SHA_BLOCKSIZE);
173#endif /* LITTLE_ENDIAN */
174        sha_transform(sha_info);
175        memset(&sha_info->data, 0, 56);
176    } else {
177        memset((BYTE *) &sha_info->data + count, 0, 56 - count);
178    }
179#ifdef LITTLE_ENDIAN
180    byte_reverse(sha_info->data, SHA_BLOCKSIZE);
181#endif /* LITTLE_ENDIAN */
182    sha_info->data[14] = hi_bit_count;
183    sha_info->data[15] = lo_bit_count;
184    sha_transform(sha_info);
185}
186
187/* compute the SHA digest of a FILE stream */
188
189#define BLOCK_SIZE      8192
190
191void sha_stream(SHA_INFO *sha_info, FILE *fin)
192{
193    int i;
194    BYTE data[BLOCK_SIZE];
195
196    sha_init(sha_info);
197    while ((i = fread(data, 1, BLOCK_SIZE, fin)) > 0) {
198        sha_update(sha_info, data, i);
199    }
200    sha_final(sha_info);
201}
202
203/* print a SHA digest */
204
205void sha_print(SHA_INFO *sha_info)
206{
207    printf("%08lx %08lx %08lx %08lx %08lx\n",
208        sha_info->digest[0], sha_info->digest[1], sha_info->digest[2],
209        sha_info->digest[3], sha_info->digest[4]);
210}
Note: See TracBrowser for help on using the repository browser.