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 extern void __builtin_bambu_time_start();
28 extern void __builtin_bambu_time_stop();
29 
30 /* SHA f()-functions */
31 
32 #define f1(x,y,z) ((x & y) | (~x & z))
33 #define f2(x,y,z) (x ^ y ^ z)
34 #define f3(x,y,z) ((x & y) | (x & z) | (y & z))
35 #define f4(x,y,z) (x ^ y ^ z)
36 
37 /* SHA constants */
38 
39 #define CONST1 0x5a827999L
40 #define CONST2 0x6ed9eba1L
41 #define CONST3 0x8f1bbcdcL
42 #define CONST4 0xca62c1d6L
43 
44 /* 32-bit rotate */
45 
46 #define ROT32(x,n) ((x << n) | (x >> (32 - n)))
47 
48 #define FUNC(n,i) \
49  temp = ROT32(A,5) + f##n(B,C,D) + E + W[i] + CONST##n; \
50  E = D; D = C; C = ROT32(B,30); B = A; A = temp
51 
52 void
53 local_memset (INT32 * s, int c, int n, int e)
54 {
55  INT32 uc;
56  INT32 *p;
57  int m;
58 
59  m = n / 4;
60  uc = c;
61  p = (INT32 *) s;
62  while (e-- > 0)
63  {
64  p++;
65  }
66  while (m-- > 0)
67  {
68  *p++ = uc;
69  }
70 }
71 
72 void
73 local_memcpy (INT32 * s1, const BYTE * s2, int n)
74 {
75  INT32 *p1;
76  BYTE *p2;
77  INT32 tmp;
78  int m;
79  m = n / 4;
80  p1 = (INT32 *) s1;
81  p2 = (BYTE *) s2;
82 
83  while (m-- > 0)
84  {
85  tmp = 0;
86  tmp |= 0xFF & *p2++;
87  tmp |= (0xFF & *p2++) << 8;
88  tmp |= (0xFF & *p2++) << 16;
89  tmp |= (0xFF & *p2++) << 24;
90  *p1 = tmp;
91  p1++;
92  }
93 }
94 
95 /* do SHA transformation */
96 
97 static void
99 {
100  int i;
101  INT32 temp, A, B, C, D, E, W[80];
102 
103  for (i = 0; i < 16; ++i)
104  {
105  W[i] = sha_info_data[i];
106  }
107  for (i = 16; i < 80; ++i)
108  {
109  W[i] = W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16];
110  }
111  A = sha_info_digest[0];
112  B = sha_info_digest[1];
113  C = sha_info_digest[2];
114  D = sha_info_digest[3];
115  E = sha_info_digest[4];
116 
117  for (i = 0; i < 20; ++i)
118  {
119  FUNC (1, i);
120  }
121  for (i = 20; i < 40; ++i)
122  {
123  FUNC (2, i);
124  }
125  for (i = 40; i < 60; ++i)
126  {
127  FUNC (3, i);
128  }
129  for (i = 60; i < 80; ++i)
130  {
131  FUNC (4, i);
132  }
133 
134  sha_info_digest[0] += A;
135  sha_info_digest[1] += B;
136  sha_info_digest[2] += C;
137  sha_info_digest[3] += D;
138  sha_info_digest[4] += E;
139 }
140 
141 /* initialize the SHA digest */
142 
143 void
145 {
146  sha_info_digest[0] = 0x67452301L;
147  sha_info_digest[1] = 0xefcdab89L;
148  sha_info_digest[2] = 0x98badcfeL;
149  sha_info_digest[3] = 0x10325476L;
150  sha_info_digest[4] = 0xc3d2e1f0L;
151  sha_info_count_lo = 0L;
152  sha_info_count_hi = 0L;
153 }
154 
155 /* update the SHA digest */
156 
157 void
158 sha_update (const BYTE * buffer, int count)
159 {
160  if ((sha_info_count_lo + ((INT32) count << 3)) < sha_info_count_lo)
161  {
163  }
164  sha_info_count_lo += (INT32) count << 3;
165  sha_info_count_hi += (INT32) count >> 29;
166  while (count >= SHA_BLOCKSIZE)
167  {
169  sha_transform ();
170  buffer += SHA_BLOCKSIZE;
171  count -= SHA_BLOCKSIZE;
172  }
173  local_memcpy (sha_info_data, buffer, count);
174 }
175 
176 /* finish computing the SHA digest */
177 
178 void
180 {
181  int count;
182  INT32 lo_bit_count;
183  INT32 hi_bit_count;
184 
185  lo_bit_count = sha_info_count_lo;
186  hi_bit_count = sha_info_count_hi;
187  count = (int) ((lo_bit_count >> 3) & 0x3f);
188  sha_info_data[count++] = 0x80;
189  if (count > 56)
190  {
191  local_memset (sha_info_data, 0, 64 - count, count);
192  sha_transform ();
193  local_memset (sha_info_data, 0, 56, 0);
194  }
195  else
196  {
197  local_memset (sha_info_data, 0, 56 - count, count);
198  }
199  sha_info_data[14] = hi_bit_count;
200  sha_info_data[15] = lo_bit_count;
201  sha_transform ();
202 }
203 
204 /* compute the SHA digest of a FILE stream */
205 void
206 __attribute__ ((noinline))
207 sha_stream ()
208 {
209  int i, j;
210  const BYTE *p;
212 
213  sha_init ();
214  for (j = 0; j < VSIZE; j++)
215  {
216  i = in_i[j];
217  p = &indata[j][0];
218  sha_update (p, i);
219  }
220  sha_final ();
222 }
void local_memset(INT32 *s, int c, int n, int e)
Definition: sha.c:51
#define SHA_BLOCKSIZE
Definition: sha.h:30
void __builtin_bambu_time_start()
INT32 sha_info_digest[5]
Definition: sha.h:32
#define C
Definition: generate.c:15
#define VSIZE
Definition: sha.h:44
void __builtin_bambu_time_stop()
#define A
Definition: generate.c:13
#define FUNC(n, i)
Definition: sha.c:48
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
unsigned int INT32
Definition: sha.h:28
#define D
Definition: generate.c:16
void __attribute__((noinline))
Definition: sha.c:206
#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
static void sha_transform()
Definition: sha.c:98
#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