19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
-
-
+
+
-
+
-
+
-
+
-
+
|
#import "OFSHA1Hash.h"
#import "OFSecureData.h"
#import "OFHashAlreadyCalculatedException.h"
#import "OFOutOfRangeException.h"
#define DIGEST_SIZE 20
#define BLOCK_SIZE 64
static const size_t digestSize = 20;
static const size_t blockSize = 64;
OF_DIRECT_MEMBERS
@interface OFSHA1Hash ()
- (void)of_resetState;
@end
#define F(a, b, c, d) ((d) ^ ((b) & ((c) ^ (d))))
#define G(a, b, c, d) ((b) ^ (c) ^ (d))
#define H(a, b, c, d) (((b) & (c)) | ((d) & ((b) | (c))))
#define I(a, b, c, d) ((b) ^ (c) ^ (d))
static OF_INLINE void
byteSwapVectorIfLE(uint32_t *vector, uint_fast8_t length)
{
#ifndef OF_BIG_ENDIAN
for (uint_fast8_t i = 0; i < length; i++)
vector[i] = OF_BSWAP32(vector[i]);
vector[i] = OFByteSwap32(vector[i]);
#endif
}
static void
processBlock(uint32_t *state, uint32_t *buffer)
{
uint32_t new[5];
uint_fast8_t i;
new[0] = state[0];
new[1] = state[1];
new[2] = state[2];
new[3] = state[3];
new[4] = state[4];
byteSwapVectorIfLE(buffer, 16);
for (i = 16; i < 80; i++) {
uint32_t tmp = buffer[i - 3] ^ buffer[i - 8] ^
buffer[i - 14] ^ buffer[i - 16];
buffer[i] = OF_ROL(tmp, 1);
buffer[i] = OFRotateLeft(tmp, 1);
}
#define LOOP_BODY(f, k) \
{ \
uint32_t tmp = OF_ROL(new[0], 5) + \
uint32_t tmp = OFRotateLeft(new[0], 5) + \
f(new[0], new[1], new[2], new[3]) + \
new[4] + k + buffer[i]; \
new[4] = new[3]; \
new[3] = new[2]; \
new[2] = OF_ROL(new[1], 30); \
new[2] = OFRotateLeft(new[1], 30); \
new[1] = new[0]; \
new[0] = tmp; \
}
for (i = 0; i < 20; i++)
LOOP_BODY(F, 0x5A827999)
for (; i < 40; i++)
|
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
-
+
-
+
-
+
|
@implementation OFSHA1Hash
@synthesize calculated = _calculated;
@synthesize allowsSwappableMemory = _allowsSwappableMemory;
+ (size_t)digestSize
{
return DIGEST_SIZE;
return digestSize;
}
+ (size_t)blockSize
{
return BLOCK_SIZE;
return blockSize;
}
+ (instancetype)cryptoHashWithAllowsSwappableMemory: (bool)allowsSwappableMemory
+ (instancetype)hashWithAllowsSwappableMemory: (bool)allowsSwappableMemory
{
return [[[self alloc] initWithAllowsSwappableMemory:
allowsSwappableMemory] autorelease];
}
- (instancetype)initWithAllowsSwappableMemory: (bool)allowsSwappableMemory
{
|
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
-
+
-
+
|
[_iVarsData release];
[super dealloc];
}
- (size_t)digestSize
{
return DIGEST_SIZE;
return digestSize;
}
- (size_t)blockSize
{
return BLOCK_SIZE;
return blockSize;
}
- (id)copy
{
OFSHA1Hash *copy = [[OFSHA1Hash alloc] of_init];
copy->_iVarsData = [_iVarsData copy];
|
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
-
+
-
|
_iVars->state[0] = 0x67452301;
_iVars->state[1] = 0xEFCDAB89;
_iVars->state[2] = 0x98BADCFE;
_iVars->state[3] = 0x10325476;
_iVars->state[4] = 0xC3D2E1F0;
}
- (void)updateWithBuffer: (const void *)buffer_
- (void)updateWithBuffer: (const void *)buffer_ length: (size_t)length
length: (size_t)length
{
const unsigned char *buffer = buffer_;
if (_calculated)
@throw [OFHashAlreadyCalculatedException
exceptionWithObject: self];
|
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
-
+
-
+
-
+
-
+
-
+
-
+
|
- (const unsigned char *)digest
{
if (_calculated)
return (const unsigned char *)_iVars->state;
_iVars->buffer.bytes[_iVars->bufferLength] = 0x80;
of_explicit_memset(_iVars->buffer.bytes + _iVars->bufferLength + 1, 0,
OFZeroMemory(_iVars->buffer.bytes + _iVars->bufferLength + 1,
64 - _iVars->bufferLength - 1);
if (_iVars->bufferLength >= 56) {
processBlock(_iVars->state, _iVars->buffer.words);
of_explicit_memset(_iVars->buffer.bytes, 0, 64);
OFZeroMemory(_iVars->buffer.bytes, 64);
}
_iVars->buffer.words[14] =
OF_BSWAP32_IF_LE((uint32_t)(_iVars->bits >> 32));
OFToBigEndian32((uint32_t)(_iVars->bits >> 32));
_iVars->buffer.words[15] =
OF_BSWAP32_IF_LE((uint32_t)(_iVars->bits & 0xFFFFFFFF));
OFToBigEndian32((uint32_t)(_iVars->bits & 0xFFFFFFFF));
processBlock(_iVars->state, _iVars->buffer.words);
of_explicit_memset(&_iVars->buffer, 0, sizeof(_iVars->buffer));
OFZeroMemory(&_iVars->buffer, sizeof(_iVars->buffer));
byteSwapVectorIfLE(_iVars->state, 5);
_calculated = true;
return (const unsigned char *)_iVars->state;
}
- (void)reset
{
[self of_resetState];
_iVars->bits = 0;
of_explicit_memset(&_iVars->buffer, 0, sizeof(_iVars->buffer));
OFZeroMemory(&_iVars->buffer, sizeof(_iVars->buffer));
_iVars->bufferLength = 0;
_calculated = false;
}
@end
|