author | Stefan Vogel <sv@exept.de> |
Wed, 11 Mar 2020 10:09:01 +0100 | |
changeset 25334 | 8f44c9352333 |
parent 23959 | 5e284faa1acc |
permissions | -rw-r--r-- |
22563 | 1 |
"{ Encoding: utf8 }" |
2 |
||
4426 | 3 |
" |
15038 | 4 |
COPYRIGHT (c) 1999-2013 by eXept Software AG |
15907
b6a05f7d463f
use int32 instead of long (for 64bit machines)
Claus Gittinger <cg@exept.de>
parents:
15623
diff
changeset
|
5 |
All Rights Reserved |
4426 | 6 |
|
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
9369
b7f43a51c4ed
Moved SHA1Stream from exept:libcrypt to stx:libbasic
Stefan Vogel <sv@exept.de>
parents:
7824
diff
changeset
|
14 |
"{ Package: 'stx:libbasic' }" |
4051 | 15 |
|
19160 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
4051 | 18 |
HashStream subclass:#SHA1Stream |
19 |
instanceVariableNames:'hashContext' |
|
20 |
classVariableNames:'HashSize ContextSize' |
|
21 |
poolDictionaries:'' |
|
12420 | 22 |
category:'System-Crypt-Hashing' |
4051 | 23 |
! |
24 |
||
25 |
!SHA1Stream primitiveDefinitions! |
|
26 |
%{ |
|
27 |
||
23953 | 28 |
#if defined(__LSBFIRST) || defined(__LSBFIRST__) |
7824 | 29 |
# ifndef LITTLE_ENDIAN |
30 |
# define LITTLE_ENDIAN /* This should be #define'd if true. */ |
|
31 |
# endif |
|
4051 | 32 |
#endif |
33 |
||
5056 | 34 |
#define SHA1HANDSOFF /* Copies data before messing with it. */ |
4051 | 35 |
|
22491 | 36 |
#ifndef _STDIO_H_INCLUDED_ |
37 |
# include <stdio.h> |
|
38 |
# define _STDIO_H_INCLUDED_ |
|
39 |
#endif |
|
40 |
||
41 |
#ifndef _STRING_H_INCLUDED_ |
|
42 |
# include <string.h> |
|
43 |
# define _STRING_H_INCLUDED_ |
|
44 |
#endif |
|
4051 | 45 |
|
46 |
typedef struct { |
|
15025
552103f2ae6e
64bit (mac) fix: long is not an int32 here
Claus Gittinger <cg@exept.de>
parents:
14791
diff
changeset
|
47 |
unsigned int32 state[5]; |
552103f2ae6e
64bit (mac) fix: long is not an int32 here
Claus Gittinger <cg@exept.de>
parents:
14791
diff
changeset
|
48 |
unsigned int32 count[2]; |
4051 | 49 |
unsigned char buffer[64]; |
50 |
} SHA1_CTX; |
|
51 |
||
23958 | 52 |
#if (defined(__GNUC__) || defined(__CLANG__)) |
23952 | 53 |
# define STATIC_INLINE static inline |
23959 | 54 |
#else |
55 |
# define STATIC_INLINE static |
|
56 |
#endif |
|
57 |
||
58 |
#if (defined(__GNUC__) || defined(__CLANG__)) |
|
23958 | 59 |
# if (defined(__SSE4_1__) && defined(__SHA__)) |
60 |
# if __SSE4_1__ && __SHA__ |
|
61 |
// # define __SSE4_1__ 1 |
|
62 |
// # define __SHA__ 1 |
|
63 |
# include <immintrin.h> |
|
23956 | 64 |
static void SHA1Transform(unsigned int32 state[5], unsigned char buffer[64]); |
65 |
// static void __attribute__ ((__target__ ("sha,sse4.1"))) SHA1Transform_x86(unsigned int32 state[5], unsigned char buffer[64]); |
|
66 |
static void SHA1Transform_x86(unsigned int32 state[5], unsigned char buffer[64]); |
|
23958 | 67 |
# define USE_SHA_INTRINSICS |
68 |
# endif |
|
69 |
# endif |
|
23956 | 70 |
#endif |
71 |
||
72 |
#ifndef USE_SHA_INTRINSICS |
|
73 |
# define SHA1Transform SHA1Transform_generic |
|
23952 | 74 |
#endif |
75 |
||
7824 | 76 |
#if USE_ANSI_C |
23952 | 77 |
|
23956 | 78 |
static void SHA1Transform_generic(unsigned int32 state[5], unsigned char buffer[64]); |
23952 | 79 |
STATIC_INLINE void SHA1Init(SHA1_CTX* context); |
7824 | 80 |
void SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned int len); |
81 |
void SHA1Final(unsigned char digest[20], SHA1_CTX* context); |
|
23952 | 82 |
|
7824 | 83 |
#else /* general: compiles everywhere */ |
23952 | 84 |
|
7824 | 85 |
# define SHA1Init __SHA1Init |
86 |
# define SHA1Update __SHA1Update |
|
87 |
# define SHA1Final __SHA2Final |
|
4051 | 88 |
|
23959 | 89 |
STATIC_INLINE void SHA1Init(); |
7824 | 90 |
void SHA1Update(); |
91 |
void SHA1Final(); |
|
23952 | 92 |
|
7824 | 93 |
#endif /* USE_ANSI_C */ |
23952 | 94 |
|
4051 | 95 |
%} |
96 |
! ! |
|
97 |
||
98 |
!SHA1Stream primitiveFunctions! |
|
23953 | 99 |
%{ |
100 |
||
101 |
/* |
|
102 |
* SHA-1 in C |
|
103 |
* By Steve Reid <steve@edmweb.com> |
|
104 |
* 100% Public Domain |
|
105 |
*/ |
|
106 |
||
107 |
#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) |
|
108 |
||
109 |
/* blk0() and blk() perform the initial expand. */ |
|
110 |
/* I got the idea of expanding during the round function from SSLeay */ |
|
23957 | 111 |
// 876543210 -> rol,24 -> 108765432 -> & -> 10xx65xx |
112 |
// 876543210 -> rol,8 -> 654321087 -> & -> xx43xx87 |
|
113 |
// oring: 10436587 |
|
23953 | 114 |
#ifdef LITTLE_ENDIAN |
23957 | 115 |
// cg: does not make any difference (actually, slightly slower, as it seems)... |
116 |
# if 0 && (defined(__i386__) || defined(__x86__) || defined(__x86_64__)) && (defined(__GNUC__) || defined(__CLANG__)) |
|
117 |
static inline u_int32_t __bswap(u_int32_t v) { |
|
118 |
register u_int32_t l = v; |
|
119 |
__asm__ __volatile__("bswap %0" : "=r" (l) : "0" (l)); |
|
120 |
return l; |
|
121 |
} |
|
122 |
# define blk0(i) \ |
|
123 |
(block->l[i] = __bswap(block->l[i])) |
|
124 |
# else |
|
125 |
# define blk0(i) \ |
|
126 |
(block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \ |
|
127 |
|(rol(block->l[i],8)&0x00FF00FF)) |
|
128 |
# endif |
|
23953 | 129 |
#else |
130 |
# define blk0(i) block->l[i] |
|
131 |
#endif |
|
132 |
||
23957 | 133 |
#define blk(i) (block->l[i&15] = \ |
134 |
rol(block->l[(i+13)&15] \ |
|
135 |
^ block->l[(i+8)&15] \ |
|
136 |
^ block->l[(i+2)&15] \ |
|
137 |
^ block->l[i&15],1)) |
|
23953 | 138 |
|
139 |
/* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */ |
|
140 |
#define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30); |
|
141 |
#define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30); |
|
142 |
#define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30); |
|
143 |
#define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30); |
|
144 |
#define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30); |
|
145 |
||
146 |
/* |
|
147 |
* Hash a single 512-bit block. This is the core of the algorithm. |
|
148 |
*/ |
|
149 |
static void |
|
150 |
#if USE_ANSI_C |
|
23956 | 151 |
SHA1Transform_generic (unsigned int32 state[5], unsigned char buffer[64]) |
23953 | 152 |
#else |
23956 | 153 |
SHA1Transform_generic (state, buffer) |
23953 | 154 |
unsigned int32 state[5]; |
155 |
unsigned char buffer[64]; |
|
156 |
#endif |
|
157 |
{ |
|
158 |
unsigned int32 a, b, c, d, e; |
|
159 |
typedef union { |
|
160 |
unsigned char c[64]; |
|
161 |
unsigned int32 /* long */ l[16]; |
|
162 |
} CHAR64LONG16; |
|
163 |
CHAR64LONG16* block; |
|
164 |
#ifdef SHA1HANDSOFF |
|
165 |
static unsigned char workspace[64]; |
|
166 |
block = (CHAR64LONG16*)workspace; |
|
167 |
memcpy(block, buffer, 64); |
|
168 |
#else |
|
169 |
block = (CHAR64LONG16*)buffer; |
|
170 |
#endif |
|
171 |
/* Copy context->state[] to working vars */ |
|
172 |
a = state[0]; |
|
173 |
b = state[1]; |
|
174 |
c = state[2]; |
|
175 |
d = state[3]; |
|
176 |
e = state[4]; |
|
177 |
/* 4 rounds of 20 operations each. Loop unrolled. */ |
|
178 |
R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3); |
|
179 |
R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7); |
|
180 |
R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11); |
|
181 |
R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15); |
|
182 |
R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19); |
|
183 |
R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23); |
|
184 |
R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27); |
|
185 |
R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31); |
|
186 |
R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35); |
|
187 |
R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39); |
|
188 |
R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43); |
|
189 |
R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47); |
|
190 |
R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51); |
|
191 |
R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55); |
|
192 |
R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59); |
|
193 |
R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63); |
|
194 |
R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67); |
|
195 |
R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71); |
|
196 |
R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75); |
|
197 |
R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79); |
|
198 |
/* Add the working vars back into context.state[] */ |
|
199 |
state[0] += a; |
|
200 |
state[1] += b; |
|
201 |
state[2] += c; |
|
202 |
state[3] += d; |
|
203 |
state[4] += e; |
|
204 |
/* Wipe variables */ |
|
205 |
a = b = c = d = e = 0; |
|
206 |
} |
|
207 |
||
23958 | 208 |
#if defined(USE_SHA_INTRINSICS) |
23956 | 209 |
|
23957 | 210 |
// |
211 |
// a specially tuned version |
|
212 |
// |
|
23956 | 213 |
static void |
214 |
SHA1Transform_x86 (unsigned int32 state[5], unsigned char buffer[64]) |
|
215 |
{ |
|
216 |
unsigned int32 a, b, c, d, e; |
|
217 |
typedef union { |
|
218 |
unsigned char c[64]; |
|
219 |
unsigned int32 /* long */ l[16]; |
|
220 |
} CHAR64LONG16; |
|
221 |
CHAR64LONG16* block; |
|
222 |
#ifdef SHA1HANDSOFF |
|
223 |
static unsigned char workspace[64]; |
|
224 |
block = (CHAR64LONG16*)workspace; |
|
225 |
memcpy(block, buffer, 64); |
|
226 |
#else |
|
227 |
block = (CHAR64LONG16*)buffer; |
|
228 |
#endif |
|
229 |
__m128i ABCD, ABCD_SAVE, E0, E0_SAVE, E1; |
|
230 |
__m128i MSG0, MSG1, MSG2, MSG3; |
|
231 |
const __m128i MASK = _mm_set_epi64x(0x0001020304050607ULL, 0x08090a0b0c0d0e0fULL); |
|
232 |
||
233 |
/* Load initial values */ |
|
234 |
ABCD = _mm_loadu_si128((const __m128i*) state); |
|
235 |
E0 = _mm_set_epi32(state[4], 0, 0, 0); |
|
236 |
ABCD = _mm_shuffle_epi32(ABCD, 0x1B); |
|
237 |
||
238 |
/* Save current state */ |
|
239 |
ABCD_SAVE = ABCD; |
|
240 |
E0_SAVE = E0; |
|
241 |
||
242 |
/* Rounds 0-3 */ |
|
243 |
MSG0 = _mm_loadu_si128((const __m128i*)(block + 0)); |
|
244 |
MSG0 = _mm_shuffle_epi8(MSG0, MASK); |
|
245 |
E0 = _mm_add_epi32(E0, MSG0); |
|
246 |
E1 = ABCD; |
|
247 |
ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 0); |
|
248 |
||
249 |
/* Rounds 4-7 */ |
|
250 |
MSG1 = _mm_loadu_si128((const __m128i*)(block + 16)); |
|
251 |
MSG1 = _mm_shuffle_epi8(MSG1, MASK); |
|
252 |
E1 = _mm_sha1nexte_epu32(E1, MSG1); |
|
253 |
E0 = ABCD; |
|
254 |
ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 0); |
|
255 |
MSG0 = _mm_sha1msg1_epu32(MSG0, MSG1); |
|
256 |
||
257 |
/* Rounds 8-11 */ |
|
258 |
MSG2 = _mm_loadu_si128((const __m128i*)(block + 32)); |
|
259 |
MSG2 = _mm_shuffle_epi8(MSG2, MASK); |
|
260 |
E0 = _mm_sha1nexte_epu32(E0, MSG2); |
|
261 |
E1 = ABCD; |
|
262 |
ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 0); |
|
263 |
MSG1 = _mm_sha1msg1_epu32(MSG1, MSG2); |
|
264 |
MSG0 = _mm_xor_si128(MSG0, MSG2); |
|
265 |
||
266 |
/* Rounds 12-15 */ |
|
267 |
MSG3 = _mm_loadu_si128((const __m128i*)(block + 48)); |
|
268 |
MSG3 = _mm_shuffle_epi8(MSG3, MASK); |
|
269 |
E1 = _mm_sha1nexte_epu32(E1, MSG3); |
|
270 |
E0 = ABCD; |
|
271 |
MSG0 = _mm_sha1msg2_epu32(MSG0, MSG3); |
|
272 |
ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 0); |
|
273 |
MSG2 = _mm_sha1msg1_epu32(MSG2, MSG3); |
|
274 |
MSG1 = _mm_xor_si128(MSG1, MSG3); |
|
275 |
||
276 |
/* Rounds 16-19 */ |
|
277 |
E0 = _mm_sha1nexte_epu32(E0, MSG0); |
|
278 |
E1 = ABCD; |
|
279 |
MSG1 = _mm_sha1msg2_epu32(MSG1, MSG0); |
|
280 |
ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 0); |
|
281 |
MSG3 = _mm_sha1msg1_epu32(MSG3, MSG0); |
|
282 |
MSG2 = _mm_xor_si128(MSG2, MSG0); |
|
283 |
||
284 |
/* Rounds 20-23 */ |
|
285 |
E1 = _mm_sha1nexte_epu32(E1, MSG1); |
|
286 |
E0 = ABCD; |
|
287 |
MSG2 = _mm_sha1msg2_epu32(MSG2, MSG1); |
|
288 |
ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 1); |
|
289 |
MSG0 = _mm_sha1msg1_epu32(MSG0, MSG1); |
|
290 |
MSG3 = _mm_xor_si128(MSG3, MSG1); |
|
291 |
||
292 |
/* Rounds 24-27 */ |
|
293 |
E0 = _mm_sha1nexte_epu32(E0, MSG2); |
|
294 |
E1 = ABCD; |
|
295 |
MSG3 = _mm_sha1msg2_epu32(MSG3, MSG2); |
|
296 |
ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 1); |
|
297 |
MSG1 = _mm_sha1msg1_epu32(MSG1, MSG2); |
|
298 |
MSG0 = _mm_xor_si128(MSG0, MSG2); |
|
299 |
||
300 |
/* Rounds 28-31 */ |
|
301 |
E1 = _mm_sha1nexte_epu32(E1, MSG3); |
|
302 |
E0 = ABCD; |
|
303 |
MSG0 = _mm_sha1msg2_epu32(MSG0, MSG3); |
|
304 |
ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 1); |
|
305 |
MSG2 = _mm_sha1msg1_epu32(MSG2, MSG3); |
|
306 |
MSG1 = _mm_xor_si128(MSG1, MSG3); |
|
307 |
||
308 |
/* Rounds 32-35 */ |
|
309 |
E0 = _mm_sha1nexte_epu32(E0, MSG0); |
|
310 |
E1 = ABCD; |
|
311 |
MSG1 = _mm_sha1msg2_epu32(MSG1, MSG0); |
|
312 |
ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 1); |
|
313 |
MSG3 = _mm_sha1msg1_epu32(MSG3, MSG0); |
|
314 |
MSG2 = _mm_xor_si128(MSG2, MSG0); |
|
315 |
||
316 |
/* Rounds 36-39 */ |
|
317 |
E1 = _mm_sha1nexte_epu32(E1, MSG1); |
|
318 |
E0 = ABCD; |
|
319 |
MSG2 = _mm_sha1msg2_epu32(MSG2, MSG1); |
|
320 |
ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 1); |
|
321 |
MSG0 = _mm_sha1msg1_epu32(MSG0, MSG1); |
|
322 |
MSG3 = _mm_xor_si128(MSG3, MSG1); |
|
323 |
||
324 |
/* Rounds 40-43 */ |
|
325 |
E0 = _mm_sha1nexte_epu32(E0, MSG2); |
|
326 |
E1 = ABCD; |
|
327 |
MSG3 = _mm_sha1msg2_epu32(MSG3, MSG2); |
|
328 |
ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 2); |
|
329 |
MSG1 = _mm_sha1msg1_epu32(MSG1, MSG2); |
|
330 |
MSG0 = _mm_xor_si128(MSG0, MSG2); |
|
331 |
||
332 |
/* Rounds 44-47 */ |
|
333 |
E1 = _mm_sha1nexte_epu32(E1, MSG3); |
|
334 |
E0 = ABCD; |
|
335 |
MSG0 = _mm_sha1msg2_epu32(MSG0, MSG3); |
|
336 |
ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 2); |
|
337 |
MSG2 = _mm_sha1msg1_epu32(MSG2, MSG3); |
|
338 |
MSG1 = _mm_xor_si128(MSG1, MSG3); |
|
339 |
||
340 |
/* Rounds 48-51 */ |
|
341 |
E0 = _mm_sha1nexte_epu32(E0, MSG0); |
|
342 |
E1 = ABCD; |
|
343 |
MSG1 = _mm_sha1msg2_epu32(MSG1, MSG0); |
|
344 |
ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 2); |
|
345 |
MSG3 = _mm_sha1msg1_epu32(MSG3, MSG0); |
|
346 |
MSG2 = _mm_xor_si128(MSG2, MSG0); |
|
347 |
||
348 |
/* Rounds 52-55 */ |
|
349 |
E1 = _mm_sha1nexte_epu32(E1, MSG1); |
|
350 |
E0 = ABCD; |
|
351 |
MSG2 = _mm_sha1msg2_epu32(MSG2, MSG1); |
|
352 |
ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 2); |
|
353 |
MSG0 = _mm_sha1msg1_epu32(MSG0, MSG1); |
|
354 |
MSG3 = _mm_xor_si128(MSG3, MSG1); |
|
355 |
||
356 |
/* Rounds 56-59 */ |
|
357 |
E0 = _mm_sha1nexte_epu32(E0, MSG2); |
|
358 |
E1 = ABCD; |
|
359 |
MSG3 = _mm_sha1msg2_epu32(MSG3, MSG2); |
|
360 |
ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 2); |
|
361 |
MSG1 = _mm_sha1msg1_epu32(MSG1, MSG2); |
|
362 |
MSG0 = _mm_xor_si128(MSG0, MSG2); |
|
363 |
||
364 |
/* Rounds 60-63 */ |
|
365 |
E1 = _mm_sha1nexte_epu32(E1, MSG3); |
|
366 |
E0 = ABCD; |
|
367 |
MSG0 = _mm_sha1msg2_epu32(MSG0, MSG3); |
|
368 |
ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 3); |
|
369 |
MSG2 = _mm_sha1msg1_epu32(MSG2, MSG3); |
|
370 |
MSG1 = _mm_xor_si128(MSG1, MSG3); |
|
371 |
||
372 |
/* Rounds 64-67 */ |
|
373 |
E0 = _mm_sha1nexte_epu32(E0, MSG0); |
|
374 |
E1 = ABCD; |
|
375 |
MSG1 = _mm_sha1msg2_epu32(MSG1, MSG0); |
|
376 |
ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 3); |
|
377 |
MSG3 = _mm_sha1msg1_epu32(MSG3, MSG0); |
|
378 |
MSG2 = _mm_xor_si128(MSG2, MSG0); |
|
379 |
||
380 |
/* Rounds 68-71 */ |
|
381 |
E1 = _mm_sha1nexte_epu32(E1, MSG1); |
|
382 |
E0 = ABCD; |
|
383 |
MSG2 = _mm_sha1msg2_epu32(MSG2, MSG1); |
|
384 |
ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 3); |
|
385 |
MSG3 = _mm_xor_si128(MSG3, MSG1); |
|
386 |
||
387 |
/* Rounds 72-75 */ |
|
388 |
E0 = _mm_sha1nexte_epu32(E0, MSG2); |
|
389 |
E1 = ABCD; |
|
390 |
MSG3 = _mm_sha1msg2_epu32(MSG3, MSG2); |
|
391 |
ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 3); |
|
392 |
||
393 |
/* Rounds 76-79 */ |
|
394 |
E1 = _mm_sha1nexte_epu32(E1, MSG3); |
|
395 |
E0 = ABCD; |
|
396 |
ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 3); |
|
397 |
||
398 |
/* Combine state */ |
|
399 |
E0 = _mm_sha1nexte_epu32(E0, E0_SAVE); |
|
400 |
ABCD = _mm_add_epi32(ABCD, ABCD_SAVE); |
|
401 |
||
402 |
/* Save state */ |
|
403 |
ABCD = _mm_shuffle_epi32(ABCD, 0x1B); |
|
404 |
_mm_storeu_si128((__m128i*) state, ABCD); |
|
405 |
state[4] = _mm_extract_epi32(E0, 3); |
|
406 |
} |
|
407 |
||
408 |
static void |
|
409 |
#if USE_ANSI_C |
|
410 |
SHA1Transform(unsigned int32 state[5], unsigned char buffer[64]) |
|
411 |
#else |
|
412 |
SHA1Transform(state, buffer) |
|
413 |
unsigned int32 state[5]; |
|
414 |
unsigned char buffer[64]; |
|
415 |
#endif |
|
416 |
{ |
|
23957 | 417 |
extern unsigned char __cpu_hasSSE4_1_and_SHA; |
418 |
||
419 |
if (__cpu_hasSSE4_1_and_SHA) { |
|
420 |
SHA1Transform_x86(state, buffer); |
|
421 |
} else { |
|
422 |
SHA1Transform_generic(state, buffer); |
|
423 |
} |
|
23956 | 424 |
} |
425 |
||
426 |
#endif |
|
427 |
||
23953 | 428 |
/* |
429 |
* SHA1Init - Initialize new context |
|
430 |
*/ |
|
431 |
STATIC_INLINE void |
|
432 |
#if USE_ANSI_C |
|
433 |
SHA1Init(SHA1_CTX* context) |
|
434 |
#else |
|
435 |
SHA1Init(context) |
|
436 |
SHA1_CTX* context; |
|
437 |
#endif |
|
438 |
{ |
|
439 |
/* SHA1 initialization constants */ |
|
440 |
context->state[0] = 0x67452301; |
|
441 |
context->state[1] = 0xEFCDAB89; |
|
442 |
context->state[2] = 0x98BADCFE; |
|
443 |
context->state[3] = 0x10325476; |
|
444 |
context->state[4] = 0xC3D2E1F0; |
|
445 |
context->count[0] = context->count[1] = 0; |
|
446 |
} |
|
447 |
||
4051 | 448 |
|
23953 | 449 |
/* |
450 |
* Run your data through this. |
|
451 |
*/ |
|
452 |
void |
|
453 |
#if USE_ANSI_C |
|
454 |
SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned int len) |
|
455 |
#else |
|
456 |
SHA1Update(context, data, len) |
|
457 |
SHA1_CTX* context; |
|
458 |
unsigned char* data; |
|
459 |
unsigned int len; |
|
460 |
#endif |
|
461 |
{ |
|
462 |
unsigned int i, j; |
|
463 |
||
464 |
j = (context->count[0] >> 3) & 63; |
|
465 |
if ((context->count[0] += len << 3) < (len << 3)) context->count[1]++; |
|
466 |
context->count[1] += (len >> 29); |
|
467 |
if ((j + len) > 63) { |
|
468 |
memcpy(&context->buffer[j], data, (i = 64-j)); |
|
469 |
SHA1Transform(context->state, context->buffer); |
|
470 |
for ( ; i + 63 < len; i += 64) { |
|
471 |
SHA1Transform(context->state, &data[i]); |
|
472 |
} |
|
473 |
j = 0; |
|
474 |
} |
|
475 |
else i = 0; |
|
476 |
memcpy(&context->buffer[j], &data[i], len - i); |
|
477 |
} |
|
478 |
||
479 |
||
480 |
/* |
|
481 |
* Add padding and return the message digest. |
|
482 |
*/ |
|
483 |
void |
|
484 |
#if USE_ANSI_C |
|
485 |
SHA1Final(unsigned char digest[20], SHA1_CTX* context) |
|
486 |
#else |
|
487 |
SHA1Final(digest, context) |
|
488 |
unsigned char digest[20]; |
|
489 |
SHA1_CTX* context; |
|
490 |
#endif |
|
491 |
{ |
|
492 |
unsigned int32 i, j; |
|
493 |
unsigned char finalcount[8]; |
|
494 |
||
495 |
for (i = 0; i < 8; i++) { |
|
496 |
finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)] |
|
497 |
>> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */ |
|
498 |
} |
|
499 |
SHA1Update(context, (unsigned char *)"\200", 1); |
|
23956 | 500 |
while ((context->count[0] & 504 /* 16r1F8 */) != 448 /* 16r1C0 */) { |
23953 | 501 |
SHA1Update(context, (unsigned char *)"\0", 1); |
502 |
} |
|
503 |
SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */ |
|
504 |
for (i = 0; i < 20; i++) { |
|
505 |
digest[i] = (unsigned char) |
|
506 |
((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255); |
|
507 |
} |
|
508 |
/* Wipe variables */ |
|
509 |
i = j = 0; |
|
510 |
memset(context->buffer, 0, 64); |
|
511 |
memset(context->state, 0, 20); |
|
512 |
memset(context->count, 0, 8); |
|
513 |
memset(&finalcount, 0, 8); |
|
514 |
#ifdef SHA1HANDSOFF /* make SHA1Transform overwrite it's own static vars */ |
|
515 |
SHA1Transform(context->state, context->buffer); |
|
516 |
#endif |
|
517 |
} |
|
518 |
||
519 |
#if 0 |
|
520 |
/*************************************************************/ |
|
521 |
||
522 |
/* |
|
523 |
* SHA1 test program |
|
524 |
*/ |
|
525 |
||
526 |
int main(int argc, char** argv) |
|
527 |
{ |
|
528 |
int i, j; |
|
529 |
SHA1_CTX context; |
|
530 |
unsigned char digest[20], buffer[16384]; |
|
531 |
FILE* file; |
|
532 |
||
533 |
if (argc > 2) { |
|
534 |
puts("Public domain SHA-1 implementation - by Steve Reid <steve@edmweb.com>"); |
|
535 |
puts("Produces the SHA-1 hash of a file, or stdin if no file is specified."); |
|
536 |
exit(0); |
|
537 |
} |
|
538 |
if (argc < 2) { |
|
539 |
file = stdin; |
|
540 |
} |
|
541 |
else { |
|
542 |
if (!(file = fopen(argv[1], "rb"))) { |
|
543 |
fputs("Unable to open file.", stderr); |
|
544 |
exit(-1); |
|
545 |
} |
|
546 |
} |
|
547 |
SHA1Init(&context); |
|
548 |
while (!feof(file)) { /* note: what if ferror(file) */ |
|
549 |
i = fread(buffer, 1, 16384, file); |
|
550 |
SHA1Update(&context, buffer, i); |
|
551 |
} |
|
552 |
SHA1Final(digest, &context); |
|
553 |
fclose(file); |
|
554 |
for (i = 0; i < 5; i++) { |
|
555 |
for (j = 0; j < 4; j++) { |
|
556 |
printf("%02X", digest[i*4+j]); |
|
557 |
} |
|
558 |
putchar(' '); |
|
559 |
} |
|
560 |
putchar('\n'); |
|
561 |
exit(0); |
|
562 |
} |
|
563 |
#endif |
|
4051 | 564 |
|
565 |
%} |
|
566 |
! ! |
|
567 |
||
568 |
!SHA1Stream class methodsFor:'documentation'! |
|
569 |
||
4426 | 570 |
copyright |
571 |
" |
|
15038 | 572 |
COPYRIGHT (c) 1999-2013 by eXept Software AG |
15907
b6a05f7d463f
use int32 instead of long (for 64bit machines)
Claus Gittinger <cg@exept.de>
parents:
15623
diff
changeset
|
573 |
All Rights Reserved |
4426 | 574 |
|
575 |
This software is furnished under a license and may be used |
|
576 |
only in accordance with the terms of that license and with the |
|
577 |
inclusion of the above copyright notice. This software may not |
|
578 |
be provided or otherwise made available to, or used by, any |
|
579 |
other person. No title to or ownership of the software is |
|
580 |
hereby transferred. |
|
581 |
" |
|
582 |
! |
|
583 |
||
4051 | 584 |
documentation |
585 |
" |
|
586 |
Generate a SHA-1 hash value as defined in |
|
587 |
NIST, FIPS PUB 180-1: Secure Hash Standard, April 1995. |
|
13933 | 588 |
|
13935 | 589 |
Notice (2005): |
23953 | 590 |
Be aware that SHA-1 is considered broken and may not be appropriate in some applications. |
591 |
Especially it should no longer be used for security stuff. |
|
4051 | 592 |
|
12265 | 593 |
performance: roughly |
23953 | 594 |
200 Mb/s on a 2012 MAC Powerbook (2.6Ghz I7) |
595 |
150 Mb/s on a 2007 MAC Powerbook (2.6Ghz Duo) |
|
596 |
120 Mb/s on a 2.5Ghz 64X2 Athlon 4800+ (64bit) |
|
597 |
47400 Kb/s on a 2Ghz Duo (old measure) |
|
598 |
9580 Kb/s on a 400Mhz PIII |
|
599 |
3970 Kb/s on a 300Mhz Sparc |
|
4916
df965a3448b9
added throughput test example
Claus Gittinger <cg@exept.de>
parents:
4798
diff
changeset
|
600 |
|
4051 | 601 |
[author:] |
23953 | 602 |
Stefan Vogel |
4051 | 603 |
|
604 |
[see also:] |
|
23953 | 605 |
MD5Stream |
606 |
SHA256Stream SHA512Stream (in libcrypt) |
|
4051 | 607 |
|
608 |
[class variables:] |
|
23953 | 609 |
HashSize size of returned hash value |
610 |
ContextSize (implementation) size of hash context |
|
4051 | 611 |
|
612 |
[instance variables:] |
|
23953 | 613 |
hashContext (implementation) |
614 |
internal buffer for computation of the hash value |
|
4051 | 615 |
" |
616 |
! |
|
617 |
||
618 |
examples |
|
619 |
" |
|
10781 | 620 |
Test Vectors (from FIPS PUB 180-1); results are: |
7824 | 621 |
|
23953 | 622 |
[exBegin] |
4051 | 623 |
|hashStream| |
624 |
||
4798 | 625 |
hashStream := SHA1Stream new. |
4051 | 626 |
hashStream nextPut:'abc'. |
627 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
628 |
hashStream nextPut:'dbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'. |
|
629 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
23953 | 630 |
[exEnd] |
19160 | 631 |
|
23953 | 632 |
[exBegin] |
19160 | 633 |
|hashValue| |
7042 | 634 |
|
19160 | 635 |
hashValue := SHA1Stream hashValueOf:'hello world'. |
636 |
self assert:(hashValue hexPrintString = '2AAE6C35C94FCFB415DBE95F408B9CE91EE846ED') |
|
23953 | 637 |
[exEnd] |
638 |
||
639 |
[exBegin] |
|
7042 | 640 |
|hashValue| |
4051 | 641 |
|
15038 | 642 |
hashValue := SHA1Stream hashValueOf:'abc'. |
7042 | 643 |
hashValue printOn:Transcript base:16. Transcript cr. |
23953 | 644 |
[exEnd] |
7042 | 645 |
|
23953 | 646 |
[exBegin] |
4051 | 647 |
|hashStream| |
648 |
||
4798 | 649 |
hashStream := SHA1Stream new. |
4051 | 650 |
hashStream nextPut:'abc' asByteArray. |
651 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
652 |
hashStream nextPut:'dbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq' asByteArray. |
|
653 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
654 |
||
23953 | 655 |
[exEnd] |
4051 | 656 |
|
23953 | 657 |
[exBegin] |
4051 | 658 |
|hashStream| |
659 |
||
4798 | 660 |
hashStream := SHA1Stream new. |
4051 | 661 |
1000000 timesRepeat:[ hashStream nextPut:$a ]. |
662 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
23953 | 663 |
[exEnd] |
4051 | 664 |
|
23953 | 665 |
[exBegin] |
4051 | 666 |
|hashStream| |
667 |
||
4798 | 668 |
hashStream := SHA1Stream new. |
4051 | 669 |
hashStream nextPut:'a'. |
670 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
23953 | 671 |
[exEnd] |
4051 | 672 |
|
23953 | 673 |
[exBegin] |
4051 | 674 |
|hashStream| |
675 |
||
4798 | 676 |
hashStream := SHA1Stream new. |
4051 | 677 |
hashStream nextPut:$a. |
678 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
23953 | 679 |
[exEnd] |
4916
df965a3448b9
added throughput test example
Claus Gittinger <cg@exept.de>
parents:
4798
diff
changeset
|
680 |
|
df965a3448b9
added throughput test example
Claus Gittinger <cg@exept.de>
parents:
4798
diff
changeset
|
681 |
timing throughput: |
23953 | 682 |
[exBegin] |
4916
df965a3448b9
added throughput test example
Claus Gittinger <cg@exept.de>
parents:
4798
diff
changeset
|
683 |
|hashStream n t| |
df965a3448b9
added throughput test example
Claus Gittinger <cg@exept.de>
parents:
4798
diff
changeset
|
684 |
|
df965a3448b9
added throughput test example
Claus Gittinger <cg@exept.de>
parents:
4798
diff
changeset
|
685 |
hashStream := SHA1Stream new. |
df965a3448b9
added throughput test example
Claus Gittinger <cg@exept.de>
parents:
4798
diff
changeset
|
686 |
n := 1000000. |
df965a3448b9
added throughput test example
Claus Gittinger <cg@exept.de>
parents:
4798
diff
changeset
|
687 |
t := Time millisecondsToRun:[ |
23953 | 688 |
n timesRepeat:[ |
689 |
hashStream nextPutAll:'12345678901234567890123456789012345678901234567890'. |
|
690 |
]. |
|
691 |
]. |
|
4916
df965a3448b9
added throughput test example
Claus Gittinger <cg@exept.de>
parents:
4798
diff
changeset
|
692 |
t := (t / 1000) asFloat. |
df965a3448b9
added throughput test example
Claus Gittinger <cg@exept.de>
parents:
4798
diff
changeset
|
693 |
Transcript show:t; show:' seconds for '; show:(50*n/1024) asFloat; showCR:' Kb'. |
df965a3448b9
added throughput test example
Claus Gittinger <cg@exept.de>
parents:
4798
diff
changeset
|
694 |
Transcript show:(n*50/1024 / t); showCR:' Kb/s' |
23953 | 695 |
[exEnd] |
4051 | 696 |
" |
19143 | 697 |
! |
698 |
||
699 |
performance |
|
700 |
" |
|
23952 | 701 |
CPU cc algo mb/sec |
19143 | 702 |
|
23952 | 703 |
MAC (2010 macbook; 2.7Ghz Duo) clang -O2 slow 128.5 |
23957 | 704 |
132 |
23956 | 705 |
MAC (2012 macbook; 2.6Ghz I7) clang -O2 190 |
23952 | 706 |
|
19143 | 707 |
|
23957 | 708 |
chunk size 10: 86.70 Mb/s 90.83 |
709 |
chunk size 50: 227.07 Mb/s 238.42 |
|
710 |
chunk size 1000: 405.82 Mb/s 414.64 |
|
711 |
chunk size 50000: 421.98 Mb/s 447.73 |
|
19143 | 712 |
|
713 |
||
714 |
timing throughput: |
|
23957 | 715 |
[exBegin] |
19143 | 716 |
|hashStream n t| |
717 |
||
718 |
hashStream := SHA1Stream new. |
|
719 |
n := 1000000. |
|
720 |
t := Time millisecondsToRun:[ |
|
23957 | 721 |
n timesRepeat:[ |
722 |
hashStream nextPutAll:'12345678901234567890123456789012345678901234567890'. |
|
723 |
]. |
|
724 |
]. |
|
19143 | 725 |
t := (t / 1000) asFloat. |
726 |
Transcript show:t; show:' seconds for '; show:(50*n/1024) asFloat; showCR:' Kb'. |
|
727 |
Transcript show:(n*50/1024 / t); showCR:' Kb/s' |
|
23957 | 728 |
[exEnd] |
19143 | 729 |
" |
4051 | 730 |
! ! |
731 |
||
732 |
!SHA1Stream class methodsFor:'initialization'! |
|
733 |
||
734 |
initialize |
|
735 |
|ctxSize| |
|
736 |
||
737 |
%{ |
|
738 |
ctxSize = __MKSMALLINT(sizeof(SHA1_CTX)); |
|
739 |
%}. |
|
740 |
ContextSize := ctxSize. |
|
741 |
HashSize := 20. |
|
742 |
||
743 |
" |
|
5056 | 744 |
self initialize |
4051 | 745 |
" |
746 |
! ! |
|
747 |
||
748 |
!SHA1Stream class methodsFor:'queries'! |
|
749 |
||
15623
1c229b466d52
Rename #blockSize to #hashBlockSize
Stefan Vogel <sv@exept.de>
parents:
15038
diff
changeset
|
750 |
hashBlockSize |
4051 | 751 |
"return the block size used internally by the compression function" |
15907
b6a05f7d463f
use int32 instead of long (for 64bit machines)
Claus Gittinger <cg@exept.de>
parents:
15623
diff
changeset
|
752 |
|
4051 | 753 |
^ 64 |
754 |
||
755 |
"Created: / 18.3.1999 / 08:37:10 / stefan" |
|
756 |
! |
|
757 |
||
758 |
hashSize |
|
759 |
"return the size of the hashvalue returned by instances of this class" |
|
760 |
||
761 |
^ HashSize |
|
762 |
||
763 |
"Modified: / 18.3.1999 / 07:54:22 / stefan" |
|
764 |
! ! |
|
765 |
||
766 |
!SHA1Stream methodsFor:'initialization'! |
|
767 |
||
768 |
initialize |
|
769 |
||
770 |
hashContext := ByteArray new:ContextSize. |
|
771 |
self reset |
|
772 |
||
773 |
"Created: / 17.3.1999 / 16:11:37 / stefan" |
|
774 |
"Modified: / 18.3.1999 / 07:56:46 / stefan" |
|
775 |
! ! |
|
776 |
||
777 |
!SHA1Stream methodsFor:'positioning'! |
|
778 |
||
779 |
reset |
|
780 |
"reset the stream in order to compute a new hash value" |
|
781 |
||
782 |
%{ |
|
14669 | 783 |
if (__isByteArray(__INST(hashContext)) && |
4051 | 784 |
__byteArraySize(__INST(hashContext)) == sizeof(SHA1_CTX) |
785 |
) { |
|
14667
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
13935
diff
changeset
|
786 |
SHA1_CTX *ctx = (SHA1_CTX *)__ByteArrayInstPtr(__INST(hashContext))->ba_element; |
4051 | 787 |
|
14667
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
13935
diff
changeset
|
788 |
SHA1Init(ctx); |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
13935
diff
changeset
|
789 |
RETURN(self); |
4051 | 790 |
} |
791 |
%}. |
|
792 |
^ self primitiveFailed |
|
793 |
||
13930 | 794 |
"Created: / 18-03-1999 / 07:59:02 / stefan" |
795 |
"Modified: / 12-01-2012 / 12:21:23 / cg" |
|
22563 | 796 |
! |
797 |
||
798 |
seedWith:fiveWordVector |
|
799 |
"seed the stream with 5*16 (for testing)" |
|
800 |
||
801 |
|seedA seedB seedC seedD seedE| |
|
802 |
||
803 |
seedA := fiveWordVector at:1. |
|
804 |
seedB := fiveWordVector at:1. |
|
805 |
seedC := fiveWordVector at:1. |
|
806 |
seedD := fiveWordVector at:1. |
|
807 |
seedE := fiveWordVector at:1. |
|
808 |
self reset. |
|
809 |
%{ |
|
810 |
if (__isByteArray(__INST(hashContext)) |
|
811 |
&& (__byteArraySize(__INST(hashContext)) == sizeof(SHA1_CTX)) |
|
812 |
&& __isSmallInteger(seedA) |
|
813 |
&& __isSmallInteger(seedB) |
|
814 |
&& __isSmallInteger(seedC) |
|
815 |
&& __isSmallInteger(seedD) |
|
816 |
&& __isSmallInteger(seedE) |
|
817 |
) { |
|
23953 | 818 |
SHA1_CTX *ctx = (SHA1_CTX *)__ByteArrayInstPtr(__INST(hashContext))->ba_element; |
22563 | 819 |
|
23953 | 820 |
ctx->state[0] = __intVal(seedA); |
821 |
ctx->state[1] = __intVal(seedB); |
|
822 |
ctx->state[2] = __intVal(seedC); |
|
823 |
ctx->state[3] = __intVal(seedD); |
|
824 |
ctx->state[4] = __intVal(seedE); |
|
825 |
RETURN(self); |
|
22563 | 826 |
} |
827 |
%}. |
|
828 |
^ self primitiveFailed |
|
4051 | 829 |
! ! |
830 |
||
831 |
!SHA1Stream methodsFor:'queries'! |
|
832 |
||
833 |
hashValue |
|
834 |
"Get the value hashed so far. |
|
835 |
The context is kept, so that more objects may be hashed after |
|
836 |
retrieving a hash value" |
|
837 |
||
838 |
||
839 |
|digest| |
|
840 |
||
841 |
digest := ByteArray new:HashSize. |
|
842 |
||
843 |
%{ |
|
14791 | 844 |
OBJ hcon; |
845 |
||
846 |
hcon = __INST(hashContext); |
|
847 |
if (__isByteArray(hcon) && |
|
848 |
__byteArraySize(hcon) == sizeof(SHA1_CTX) && |
|
14669 | 849 |
__isByteArray(digest) && |
5056 | 850 |
__byteArraySize(digest) == 20 |
4051 | 851 |
) { |
14791 | 852 |
SHA1_CTX *ctx = (SHA1_CTX *)(__ByteArrayInstPtr(hcon)->ba_element); |
5056 | 853 |
SHA1_CTX copyContext; |
14723 | 854 |
|
14791 | 855 |
memcpy(©Context, ctx, sizeof(SHA1_CTX)); |
5056 | 856 |
SHA1Final(__ByteArrayInstPtr(digest)->ba_element, ©Context); |
857 |
RETURN(digest); |
|
4051 | 858 |
} |
859 |
%}. |
|
860 |
||
861 |
^ self primitiveFailed |
|
862 |
||
863 |
"Created: / 17.3.1999 / 16:13:12 / stefan" |
|
864 |
"Modified: / 18.3.1999 / 08:00:54 / stefan" |
|
865 |
! ! |
|
866 |
||
867 |
!SHA1Stream methodsFor:'writing'! |
|
868 |
||
19400 | 869 |
nextPutByte:anInteger |
870 |
"update the hash value with anInteger <= 255." |
|
871 |
||
872 |
%{ |
|
23956 | 873 |
unsigned char value; |
874 |
OBJ _hashContext = __INST(hashContext); |
|
23957 | 875 |
|
23956 | 876 |
// fetch first; check below |
877 |
value = __intVal(anInteger); |
|
878 |
if (__isSmallInteger(anInteger) && value <= 255 |
|
879 |
&& __isByteArray(_hashContext) |
|
880 |
&& __byteArraySize(_hashContext) == sizeof(SHA1_CTX) |
|
19400 | 881 |
) { |
23957 | 882 |
SHA1_CTX *ctx = (SHA1_CTX *)__byteArrayVal(_hashContext); |
19400 | 883 |
|
23957 | 884 |
SHA1Update(ctx, &value, 1); |
885 |
RETURN(self); |
|
19400 | 886 |
} |
887 |
bad: ; |
|
888 |
%}. |
|
889 |
||
890 |
^ self primitiveFailed |
|
23956 | 891 |
|
892 |
"Modified: / 23-03-2019 / 15:19:40 / Claus Gittinger" |
|
19400 | 893 |
! |
894 |
||
7042 | 895 |
nextPutBytes:count from:anObject startingAt:start |
896 |
"update the hash value with count bytes from an object starting at index start. |
|
9369
b7f43a51c4ed
Moved SHA1Stream from exept:libcrypt to stx:libbasic
Stefan Vogel <sv@exept.de>
parents:
7824
diff
changeset
|
897 |
The object must have non-pointer indexed instvars |
7042 | 898 |
(i.e. be a ByteArray, String, Float- or DoubleArray), |
899 |
or an externalBytes object (with known size)" |
|
900 |
||
901 |
%{ |
|
14667
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
13935
diff
changeset
|
902 |
INT len, offs; |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
13935
diff
changeset
|
903 |
INT objSize; |
15989 | 904 |
int nInstBytes; |
23957 | 905 |
unsigned char *extPtr; |
23956 | 906 |
OBJ _hashContext = __INST(hashContext); |
7042 | 907 |
|
23956 | 908 |
// convert here; check later |
909 |
len = __intVal(count); |
|
910 |
offs = __intVal(start) - 1; |
|
911 |
||
912 |
if (__isByteArray(_hashContext) |
|
913 |
&& __byteArraySize(_hashContext) == sizeof(SHA1_CTX) |
|
7042 | 914 |
&& __bothSmallInteger(count, start) |
23956 | 915 |
) { |
23957 | 916 |
SHA1_CTX *ctx = (SHA1_CTX *)__byteArrayVal(_hashContext); |
7042 | 917 |
|
23957 | 918 |
if (__isByteArrayLike(anObject)) { |
919 |
extPtr = (unsigned char *)__byteArrayVal(anObject); |
|
920 |
objSize = __byteArraySize(anObject); |
|
921 |
} else if (__isStringLike(anObject)) { |
|
922 |
extPtr = (unsigned char *)__stringVal(anObject); |
|
923 |
objSize = __stringSize(anObject); |
|
924 |
} else if (__isExternalBytesLike(anObject)) { |
|
925 |
OBJ sz; |
|
23956 | 926 |
|
23957 | 927 |
nInstBytes = 0; |
928 |
extPtr = (unsigned char *)__externalBytesAddress(anObject); |
|
929 |
if (extPtr == NULL) goto bad; |
|
930 |
sz = __externalBytesSize(anObject); |
|
931 |
objSize = __intVal(sz); |
|
932 |
if (!__isSmallInteger(sz)) { |
|
933 |
objSize = 0; /* unknown */ |
|
934 |
} |
|
935 |
} else { |
|
936 |
OBJ oClass = __Class(anObject); |
|
937 |
int nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars); |
|
14723 | 938 |
|
23957 | 939 |
nInstBytes = OHDR_SIZE + __OBJS2BYTES__(nInstVars); |
940 |
switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) { |
|
941 |
case BYTEARRAY: |
|
942 |
case WORDARRAY: |
|
943 |
case LONGARRAY: |
|
944 |
case SWORDARRAY: |
|
945 |
case SLONGARRAY: |
|
946 |
case FLOATARRAY: |
|
947 |
break; |
|
948 |
case DOUBLEARRAY: |
|
15989 | 949 |
#ifdef __NEED_DOUBLE_ALIGN |
23957 | 950 |
nInstBytes = (nInstBytes-1+__DOUBLE_ALIGN) &~ (__DOUBLE_ALIGN-1); |
15989 | 951 |
#endif |
23957 | 952 |
break; |
953 |
case LONGLONGARRAY: |
|
954 |
case SLONGLONGARRAY: |
|
15989 | 955 |
#ifdef __NEED_LONGLONG_ALIGN |
23957 | 956 |
nInstBytes = (nInstBytes-1+__LONGLONG_ALIGN) &~ (__LONGLONG_ALIGN-1); |
15989 | 957 |
#endif |
23957 | 958 |
break; |
959 |
default: |
|
960 |
goto bad; |
|
961 |
} |
|
962 |
// nInstBytes is the number of bytes occupied by pointer instance variables |
|
963 |
// subtract from size and add to byte-pointer |
|
964 |
objSize = __Size(anObject) - nInstBytes; |
|
965 |
extPtr = (unsigned char *)anObject + nInstBytes; |
|
966 |
} |
|
967 |
if ((offs >= 0) && (len >= 0) && (objSize >= (len + offs))) { |
|
968 |
SHA1Update(ctx, extPtr+offs, (unsigned int)len); |
|
969 |
RETURN (count); |
|
970 |
} |
|
7042 | 971 |
} |
972 |
bad: ; |
|
973 |
%}. |
|
974 |
||
975 |
^ self primitiveFailed |
|
23956 | 976 |
|
977 |
"Modified: / 23-03-2019 / 15:28:31 / Claus Gittinger" |
|
4051 | 978 |
! ! |
979 |
||
980 |
!SHA1Stream class methodsFor:'documentation'! |
|
981 |
||
982 |
version |
|
19143 | 983 |
^ '$Header$' |
12205 | 984 |
! |
985 |
||
986 |
version_CVS |
|
19143 | 987 |
^ '$Header$' |
4051 | 988 |
! ! |
7042 | 989 |
|
14715 | 990 |
|
4051 | 991 |
SHA1Stream initialize! |