Project
Loading...
Searching...
No Matches
SHA1.h
Go to the documentation of this file.
1/*
2SHA-1 in C
3By Steve Reid <steve@edmweb.com>
4100% Public Domain
5Test Vectors (from FIPS PUB 180-1)
6"abc"
7 A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
8"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
9 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
10A million repetitions of "a"
11 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
12*/
13
14/* #define LITTLE_ENDIAN * This should be #define'd already, if true. */
15/* #define SHA1HANDSOFF * Copies data before messing with it. */
16
17#ifndef O2_FRAMEWORK_SHA1_H_
18#define O2_FRAMEWORK_SHA1_H_
19
20#define SHA1HANDSOFF
21
22#include <cstdio>
23#include <cstring>
24
25/* for uint32_t */
26#include <cstdint>
27
28#define SHA_rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
29
30/* blk0() and blk() perform the initial expand. */
31/* I got the idea of expanding during the round function from SSLeay */
32#if BYTE_ORDER == LITTLE_ENDIAN
33#define SHA_blk0(i) (block->l[i] = (SHA_rol(block->l[i], 24) & 0xFF00FF00) | (SHA_rol(block->l[i], 8) & 0x00FF00FF))
34#elif BYTE_ORDER == BIG_ENDIAN
35#define SHA_blk0(i) block->l[i]
36#else
37#error "Endianness not defined!"
38#endif
39#define SHA_blk(i) (block->l[i & 15] = SHA_rol(block->l[(i + 13) & 15] ^ block->l[(i + 8) & 15] ^ block->l[(i + 2) & 15] ^ block->l[i & 15], 1))
40
41/* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
42#define SHA_R0(v, w, x, y, z, i) \
43 z += ((w & (x ^ y)) ^ y) + SHA_blk0(i) + 0x5A827999 + SHA_rol(v, 5); \
44 w = SHA_rol(w, 30);
45#define SHA_R1(v, w, x, y, z, i) \
46 z += ((w & (x ^ y)) ^ y) + SHA_blk(i) + 0x5A827999 + SHA_rol(v, 5); \
47 w = SHA_rol(w, 30);
48#define SHA_R2(v, w, x, y, z, i) \
49 z += (w ^ x ^ y) + SHA_blk(i) + 0x6ED9EBA1 + SHA_rol(v, 5); \
50 w = SHA_rol(w, 30);
51#define SHA_R3(v, w, x, y, z, i) \
52 z += (((w | x) & y) | (w & x)) + SHA_blk(i) + 0x8F1BBCDC + SHA_rol(v, 5); \
53 w = SHA_rol(w, 30);
54#define SHA_R4(v, w, x, y, z, i) \
55 z += (w ^ x ^ y) + SHA_blk(i) + 0xCA62C1D6 + SHA_rol(v, 5); \
56 w = SHA_rol(w, 30);
57
59{
60
61typedef struct
62{
65 unsigned char buffer[64];
66} SHA1_CTX;
67
68/* Hash a single 512-bit block. This is the core of the algorithm. */
69static void SHA1Transform(
70 uint32_t state[5],
71 const unsigned char buffer[64])
72{
73 uint32_t a, b, c, d, e;
74
75 typedef union {
76 unsigned char c[64];
77 uint32_t l[16];
78 } CHAR64LONG16;
79
80#ifdef SHA1HANDSOFF
81 CHAR64LONG16 block[1]; /* use array to appear as a pointer */
82
83 memcpy(block, buffer, 64);
84#else
85 /* The following had better never be used because it causes the
86 * pointer-to-const buffer to be cast into a pointer to non-const.
87 * And the result is written through. I threw a "const" in, hoping
88 * this will cause a diagnostic.
89 */
90 CHAR64LONG16* block = (const CHAR64LONG16*)buffer;
91#endif
92 /* Copy context->state[] to working vars */
93 a = state[0];
94 b = state[1];
95 c = state[2];
96 d = state[3];
97 e = state[4];
98 /* 4 rounds of 20 operations each. Loop unrolled. */
99 SHA_R0(a, b, c, d, e, 0);
100 SHA_R0(e, a, b, c, d, 1);
101 SHA_R0(d, e, a, b, c, 2);
102 SHA_R0(c, d, e, a, b, 3);
103 SHA_R0(b, c, d, e, a, 4);
104 SHA_R0(a, b, c, d, e, 5);
105 SHA_R0(e, a, b, c, d, 6);
106 SHA_R0(d, e, a, b, c, 7);
107 SHA_R0(c, d, e, a, b, 8);
108 SHA_R0(b, c, d, e, a, 9);
109 SHA_R0(a, b, c, d, e, 10);
110 SHA_R0(e, a, b, c, d, 11);
111 SHA_R0(d, e, a, b, c, 12);
112 SHA_R0(c, d, e, a, b, 13);
113 SHA_R0(b, c, d, e, a, 14);
114 SHA_R0(a, b, c, d, e, 15);
115 SHA_R1(e, a, b, c, d, 16);
116 SHA_R1(d, e, a, b, c, 17);
117 SHA_R1(c, d, e, a, b, 18);
118 SHA_R1(b, c, d, e, a, 19);
119 SHA_R2(a, b, c, d, e, 20);
120 SHA_R2(e, a, b, c, d, 21);
121 SHA_R2(d, e, a, b, c, 22);
122 SHA_R2(c, d, e, a, b, 23);
123 SHA_R2(b, c, d, e, a, 24);
124 SHA_R2(a, b, c, d, e, 25);
125 SHA_R2(e, a, b, c, d, 26);
126 SHA_R2(d, e, a, b, c, 27);
127 SHA_R2(c, d, e, a, b, 28);
128 SHA_R2(b, c, d, e, a, 29);
129 SHA_R2(a, b, c, d, e, 30);
130 SHA_R2(e, a, b, c, d, 31);
131 SHA_R2(d, e, a, b, c, 32);
132 SHA_R2(c, d, e, a, b, 33);
133 SHA_R2(b, c, d, e, a, 34);
134 SHA_R2(a, b, c, d, e, 35);
135 SHA_R2(e, a, b, c, d, 36);
136 SHA_R2(d, e, a, b, c, 37);
137 SHA_R2(c, d, e, a, b, 38);
138 SHA_R2(b, c, d, e, a, 39);
139 SHA_R3(a, b, c, d, e, 40);
140 SHA_R3(e, a, b, c, d, 41);
141 SHA_R3(d, e, a, b, c, 42);
142 SHA_R3(c, d, e, a, b, 43);
143 SHA_R3(b, c, d, e, a, 44);
144 SHA_R3(a, b, c, d, e, 45);
145 SHA_R3(e, a, b, c, d, 46);
146 SHA_R3(d, e, a, b, c, 47);
147 SHA_R3(c, d, e, a, b, 48);
148 SHA_R3(b, c, d, e, a, 49);
149 SHA_R3(a, b, c, d, e, 50);
150 SHA_R3(e, a, b, c, d, 51);
151 SHA_R3(d, e, a, b, c, 52);
152 SHA_R3(c, d, e, a, b, 53);
153 SHA_R3(b, c, d, e, a, 54);
154 SHA_R3(a, b, c, d, e, 55);
155 SHA_R3(e, a, b, c, d, 56);
156 SHA_R3(d, e, a, b, c, 57);
157 SHA_R3(c, d, e, a, b, 58);
158 SHA_R3(b, c, d, e, a, 59);
159 SHA_R4(a, b, c, d, e, 60);
160 SHA_R4(e, a, b, c, d, 61);
161 SHA_R4(d, e, a, b, c, 62);
162 SHA_R4(c, d, e, a, b, 63);
163 SHA_R4(b, c, d, e, a, 64);
164 SHA_R4(a, b, c, d, e, 65);
165 SHA_R4(e, a, b, c, d, 66);
166 SHA_R4(d, e, a, b, c, 67);
167 SHA_R4(c, d, e, a, b, 68);
168 SHA_R4(b, c, d, e, a, 69);
169 SHA_R4(a, b, c, d, e, 70);
170 SHA_R4(e, a, b, c, d, 71);
171 SHA_R4(d, e, a, b, c, 72);
172 SHA_R4(c, d, e, a, b, 73);
173 SHA_R4(b, c, d, e, a, 74);
174 SHA_R4(a, b, c, d, e, 75);
175 SHA_R4(e, a, b, c, d, 76);
176 SHA_R4(d, e, a, b, c, 77);
177 SHA_R4(c, d, e, a, b, 78);
178 SHA_R4(b, c, d, e, a, 79);
179 /* Add the working vars back into context.state[] */
180 state[0] += a;
181 state[1] += b;
182 state[2] += c;
183 state[3] += d;
184 state[4] += e;
185 /* Wipe variables */
186 a = b = c = d = e = 0;
187#ifdef SHA1HANDSOFF
188 memset(block, '\0', sizeof(block));
189#endif
190}
191
192/* SHA1Init - Initialize new context */
193
194static void SHA1Init(
195 SHA1_CTX* context)
196{
197 /* SHA1 initialization constants */
198 context->state[0] = 0x67452301;
199 context->state[1] = 0xEFCDAB89;
200 context->state[2] = 0x98BADCFE;
201 context->state[3] = 0x10325476;
202 context->state[4] = 0xC3D2E1F0;
203 context->count[0] = context->count[1] = 0;
204}
205
206/* Run your data through this. */
207
208static void SHA1Update(
209 SHA1_CTX* context,
210 const unsigned char* data,
212{
213 uint32_t i;
214
215 uint32_t j;
216
217 j = context->count[0];
218 if ((context->count[0] += len << 3) < j) {
219 context->count[1]++;
220 }
221 context->count[1] += (len >> 29);
222 j = (j >> 3) & 63;
223 if ((j + len) > 63) {
224 memcpy(&context->buffer[j], data, (i = 64 - j));
225 SHA1Transform(context->state, context->buffer);
226 for (; i + 63 < len; i += 64) {
227 SHA1Transform(context->state, &data[i]);
228 }
229 j = 0;
230 } else {
231 i = 0;
232 }
233 memcpy(&context->buffer[j], &data[i], len - i);
234}
235
236/* Add padding and return the message digest. */
237
238static void SHA1Final(
239 unsigned char digest[20],
240 SHA1_CTX* context)
241{
242 unsigned i;
243
244 unsigned char finalcount[8];
245
246 unsigned char c;
247
248 for (i = 0; i < 8; i++) {
249 finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)] >> ((3 - (i & 3)) * 8)) & 255); /* Endian independent */
250 }
251 c = 0200;
252 SHA1Update(context, &c, 1);
253 while ((context->count[0] & 504) != 448) {
254 c = 0000;
255 SHA1Update(context, &c, 1);
256 }
257 SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */
258 for (i = 0; i < 20; i++) {
259 digest[i] = (unsigned char)((context->state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255);
260 }
261 /* Wipe variables */
262 memset(context, '\0', sizeof(*context));
263 memset(&finalcount, '\0', sizeof(finalcount));
264}
265
266static void SHA1(
267 char* hash_out,
268 const char* str,
269 unsigned int len)
270{
271 SHA1_CTX ctx;
272 unsigned int ii;
273
274 SHA1Init(&ctx);
275 for (ii = 0; ii < len; ii += 1) {
276 SHA1Update(&ctx, (const unsigned char*)str + ii, 1);
277 }
278 SHA1Final((unsigned char*)hash_out, &ctx);
279 hash_out[20] = '\0';
280}
281
282} // namespace o2::framework::internal
283
284#undef SHA1HANDSOFF
285#undef SHA_rol
286#undef SHA_blk0
287#undef SHA_blk
288#undef SHA_R0
289#undef SHA_R1
290#undef SHA_R2
291#undef SHA_R3
292#undef SHA_R4
293
294#endif // O2_FRAMEWORK_SHA1_H_
unsigned int uint32_t
int32_t i
Timeframe settings Average distance in cm of events merged into Randomize distance around average distance of merged events Simulate correct bunch interactions instead of placing only the average number of events A value[n] sets TFlen for[n] collisions in Number of bunches per trainsort Number of bunch trains Instantaneous interaction rate Do not simulate border Total number of collisions to be placed in the interior of all time Do not select random but walk over array of events in stride steps h Event generator settings h configStandalone Use CPU for GPU Force usage of the specified GPU device no CPU fallback Continue processing after an error First event to Number of events to Number of iterations to Number of iterations to Number of initial iterations excluded from const char e
Timeframe settings Average distance in cm of events merged into Randomize distance around average distance of merged events Simulate correct bunch interactions instead of placing only the average number of events A value[n] sets TFlen for[n] collisions in Number of bunches per trainsort Number of bunch trains Instantaneous interaction rate Do not simulate border Total number of collisions to be placed in the interior of all time Do not select random but walk over array of events in stride steps h Event generator settings h configStandalone Use CPU for GPU Force usage of the specified GPU device no CPU fallback Continue processing after an error First event to Number of events to Number of iterations to Number of iterations to Number of initial iterations excluded from const char Directory with events to Events directory is and not inside events d
SurfaceTrackState state
uint32_t j
Definition RawData.h:0
uint32_t c
Definition RawData.h:2
#define SHA_R3(v, w, x, y, z, i)
Definition SHA1.h:51
#define SHA_R2(v, w, x, y, z, i)
Definition SHA1.h:48
#define SHA_R0(v, w, x, y, z, i)
Definition SHA1.h:42
#define SHA_R1(v, w, x, y, z, i)
Definition SHA1.h:45
#define SHA_R4(v, w, x, y, z, i)
Definition SHA1.h:54
GLint GLsizei count
Definition glcorearb.h:399
GLboolean GLboolean GLboolean b
Definition glcorearb.h:1233
GLenum GLenum GLsizei len
Definition glcorearb.h:4232
GLboolean GLboolean GLboolean GLboolean a
Definition glcorearb.h:1233
const track::TrackFitContext< NLayers > const TrackFollowContext< NLayers > & ctx
const std::string str