PandA-2024.02
sha.c
Go to the documentation of this file.
1 /*
2 +--------------------------------------------------------------------------+
3 | CHStone : a suite of benchmark programs for C-based High-Level Synthesis |
4 | ======================================================================== |
5 | |
6 | * Collected and Modified : Y. Hara, H. Tomiyama, S. Honda, |
7 | H. Takada and K. Ishii |
8 | Nagoya University, Japan |
9 | |
10 | * Remark : |
11 | 1. This source code is modified to unify the formats of the benchmark |
12 | programs in CHStone. |
13 | 2. Test vectors are added for CHStone. |
14 | 3. If "main_result" is 0 at the end of the program, the program is |
15 | correctly executed. |
16 | 4. Please follow the copyright of each benchmark program. |
17 +--------------------------------------------------------------------------+
18 */
19 /* NIST Secure Hash Algorithm */
20 /* heavily modified by Uwe Hollerbach uh@alumni.caltech edu */
21 /* from Peter C. Gutmann's implementation as found in */
22 /* Applied Cryptography by Bruce Schneier */
23 
24 /* NIST's proposed modification to SHA of 7/11/94 may be */
25 /* activated by defining USE_MODIFIED_SHA */
26 
27 
28 /* SHA f()-functions */
29 
30 #define f1(x,y,z) ((x & y) | (~x & z))
31 #define f2(x,y,z) (x ^ y ^ z)
32 #define f3(x,y,z) ((x & y) | (x & z) | (y & z))
33 #define f4(x,y,z) (x ^ y ^ z)
34 
35 /* SHA constants */
36 
37 #define CONST1 0x5a827999L
38 #define CONST2 0x6ed9eba1L
39 #define CONST3 0x8f1bbcdcL
40 #define CONST4 0xca62c1d6L
41 
42 /* 32-bit rotate */
43 
44 #define ROT32(x,n) ((x << n) | (x >> (32 - n)))
45 
46 #define FUNC(n,i) \
47  temp = ROT32(A,5) + f##n(B,C,D) + E + W[i] + CONST##n; \
48  E = D; D = C; C = ROT32(B,30); B = A; A = temp
49 
50 void
51 local_memset (INT32 * s, int c, int n, int e)
52 {
53  INT32 uc;
54  INT32 *p;
55  int m;
56 
57  m = n / 4;
58  uc = c;
59  p = (INT32 *) s;
60  while (e-- > 0)
61  {
62  p++;
63  }
64  while (m-- > 0)
65  {
66  *p++ = uc;
67  }
68 }
69 
70 void
71 local_memcpy (INT32 * s1, const BYTE * s2, int n)
72 {
73  INT32 *p1;
74  BYTE *p2;
75  INT32 tmp;
76  int m;
77  m = n / 4;
78  p1 = (INT32 *) s1;
79  p2 = (BYTE *) s2;
80 
81  while (m-- > 0)
82  {
83  tmp = 0;
84  tmp |= 0xFF & *p2++;
85  tmp |= (0xFF & *p2++) << 8;
86  tmp |= (0xFF & *p2++) << 16;
87  tmp |= (0xFF & *p2++) << 24;
88  *p1 = tmp;
89  p1++;
90  }
91 }
92 
93 /* do SHA transformation */
94 
95 static void
97 {
98  int i;
99  INT32 temp, A, B, C, D, E, W[80];
100 
101  for (i = 0; i < 16; ++i)
102  {
103  W[i] = sha_info_data[i];
104  }
105  for (i = 16; i < 80; ++i)
106  {
107  W[i] = W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16];
108  }
109  A = sha_info_digest[0];
110  B = sha_info_digest[1];
111  C = sha_info_digest[2];
112  D = sha_info_digest[3];
113  E = sha_info_digest[4];
114 
115  for (i = 0; i < 20; ++i)
116  {
117  FUNC (1, i);
118  }
119  for (i = 20; i < 40; ++i)
120  {
121  FUNC (2, i);
122  }
123  for (i = 40; i < 60; ++i)
124  {
125  FUNC (3, i);
126  }
127  for (i = 60; i < 80; ++i)
128  {
129  FUNC (4, i);
130  }
131 
132  sha_info_digest[0] += A;
133  sha_info_digest[1] += B;
134  sha_info_digest[2] += C;
135  sha_info_digest[3] += D;
136  sha_info_digest[4] += E;
137 }
138 
139 /* initialize the SHA digest */
140 
141 void
143 {
144  sha_info_digest[0] = 0x67452301L;
145  sha_info_digest[1] = 0xefcdab89L;
146  sha_info_digest[2] = 0x98badcfeL;
147  sha_info_digest[3] = 0x10325476L;
148  sha_info_digest[4] = 0xc3d2e1f0L;
149  sha_info_count_lo = 0L;
150  sha_info_count_hi = 0L;
151 }
152 
153 /* update the SHA digest */
154 
155 void
156 sha_update (const BYTE * buffer, int count)
157 {
158  if ((sha_info_count_lo + ((INT32) count << 3)) < sha_info_count_lo)
159  {
161  }
162  sha_info_count_lo += (INT32) count << 3;
163  sha_info_count_hi += (INT32) count >> 29;
164  while (count >= SHA_BLOCKSIZE)
165  {
167  sha_transform ();
168  buffer += SHA_BLOCKSIZE;
169  count -= SHA_BLOCKSIZE;
170  }
171  local_memcpy (sha_info_data, buffer, count);
172 }
173 
174 /* finish computing the SHA digest */
175 
176 void
178 {
179  int count;
180  INT32 lo_bit_count;
181  INT32 hi_bit_count;
182 
183  lo_bit_count = sha_info_count_lo;
184  hi_bit_count = sha_info_count_hi;
185  count = (int) ((lo_bit_count >> 3) & 0x3f);
186  sha_info_data[count++] = 0x80;
187  if (count > 56)
188  {
189  local_memset (sha_info_data, 0, 64 - count, count);
190  sha_transform ();
191  local_memset (sha_info_data, 0, 56, 0);
192  }
193  else
194  {
195  local_memset (sha_info_data, 0, 56 - count, count);
196  }
197  sha_info_data[14] = hi_bit_count;
198  sha_info_data[15] = lo_bit_count;
199  sha_transform ();
200 }
201 
202 /* compute the SHA digest of a FILE stream */
203 void
205 {
206  int i, j;
207  const BYTE *p;
208 
209  sha_init ();
210  for (j = 0; j < VSIZE; j++)
211  {
212  i = in_i[j];
213  p = &indata[j][0];
214  sha_update (p, i);
215  }
216  sha_final ();
217 }
void local_memset(INT32 *s, int c, int n, int e)
Definition: sha.c:51
#define SHA_BLOCKSIZE
Definition: sha.h:30
INT32 sha_info_digest[5]
Definition: sha.h:32
#define C
Definition: generate.c:15
#define VSIZE
Definition: sha.h:44
#define A
Definition: generate.c:13
void sha_final()
Definition: sha.c:177
INT32 sha_info_count_lo
Definition: sha.h:33
const BYTE indata[VSIZE][BLOCK_SIZE]
Definition: sha.h:52
#define FUNC(n, i)
Definition: sha.c:46
unsigned int INT32
Definition: sha.h:28
#define D
Definition: generate.c:16
static void sha_transform()
Definition: sha.c:96
#define W
Definition: deriche.h:36
#define L
Definition: spmv.h:13
void sha_stream()
Definition: sha.c:204
const int in_i[VSIZE]
Definition: sha.h:1136
INT32 sha_info_count_hi
Definition: sha.h:33
void sha_update(const BYTE *buffer, int count)
Definition: sha.c:156
INT32 sha_info_data[16]
Definition: sha.h:34
void sha_init()
Definition: sha.c:142
void local_memcpy(INT32 *s1, const BYTE *s2, int n)
Definition: sha.c:71
#define B
Definition: generate.c:14
unsigned char BYTE
Definition: sha.h:27

Generated on Mon Feb 12 2024 13:02:49 for PandA-2024.02 by doxygen 1.8.13