19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
-
-
+
+
|
#import "OFRIPEMD160Hash.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 OFRIPEMD160Hash ()
- (void)of_resetState;
@end
#define F(a, b, c) ((a) ^ (b) ^ (c))
|
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
};
static OF_INLINE void
byteSwapVectorIfBE(uint32_t *vector, uint_fast8_t length)
{
#ifdef 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], new2[5];
uint_fast8_t i = 0;
new[0] = new2[0] = state[0];
new[1] = new2[1] = state[1];
new[2] = new2[2] = state[2];
new[3] = new2[3] = state[3];
new[4] = new2[4] = state[4];
byteSwapVectorIfBE(buffer, 16);
#define LOOP_BODY(f, g, k, k2) \
{ \
uint32_t tmp; \
\
tmp = new[0] + f(new[1], new[2], new[3]) + \
buffer[wordOrder[i]] + k; \
tmp = OF_ROL(tmp, rotateBits[i]) + new[4]; \
\
new[0] = new[4]; \
new[4] = new[3]; \
new[3] = OF_ROL(new[2], 10); \
new[2] = new[1]; \
new[1] = tmp; \
\
tmp = new2[0] + g(new2[1], new2[2], new2[3]) + \
buffer[wordOrder2[i]] + k2; \
tmp = OF_ROL(tmp, rotateBits2[i]) + new2[4]; \
\
new2[0] = new2[4]; \
new2[4] = new2[3]; \
new2[3] = OF_ROL(new2[2], 10); \
new2[2] = new2[1]; \
new2[1] = tmp; \
#define LOOP_BODY(f, g, k, k2) \
{ \
uint32_t tmp; \
\
tmp = new[0] + f(new[1], new[2], new[3]) + \
buffer[wordOrder[i]] + k; \
tmp = OFRotateLeft(tmp, rotateBits[i]) + new[4]; \
\
new[0] = new[4]; \
new[4] = new[3]; \
new[3] = OFRotateLeft(new[2], 10); \
new[2] = new[1]; \
new[1] = tmp; \
\
tmp = new2[0] + g(new2[1], new2[2], new2[3]) + \
buffer[wordOrder2[i]] + k2; \
tmp = OFRotateLeft(tmp, rotateBits2[i]) + new2[4]; \
\
new2[0] = new2[4]; \
new2[4] = new2[3]; \
new2[3] = OFRotateLeft(new2[2], 10); \
new2[2] = new2[1]; \
new2[1] = tmp; \
}
for (; i < 16; i++)
LOOP_BODY(F, J, 0x00000000, 0x50A28BE6)
for (; i < 32; i++)
LOOP_BODY(G, I, 0x5A827999, 0x5C4DD124)
for (; i < 48; i++)
|
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
-
+
-
+
-
+
|
@implementation OFRIPEMD160Hash
@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
{
|
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
-
+
-
+
|
[_iVarsData release];
[super dealloc];
}
- (size_t)digestSize
{
return DIGEST_SIZE;
return digestSize;
}
- (size_t)blockSize
{
return BLOCK_SIZE;
return blockSize;
}
- (id)copy
{
OFRIPEMD160Hash *copy = [[OFRIPEMD160Hash alloc] of_init];
copy->_iVarsData = [_iVarsData copy];
|
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
-
+
-
|
_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];
|
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
-
+
-
+
-
+
-
+
-
+
-
+
|
- (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_BE((uint32_t)(_iVars->bits & 0xFFFFFFFF));
OFToLittleEndian32((uint32_t)(_iVars->bits & 0xFFFFFFFF));
_iVars->buffer.words[15] =
OF_BSWAP32_IF_BE((uint32_t)(_iVars->bits >> 32));
OFToLittleEndian32((uint32_t)(_iVars->bits >> 32));
processBlock(_iVars->state, _iVars->buffer.words);
of_explicit_memset(&_iVars->buffer, 0, sizeof(_iVars->buffer));
OFZeroMemory(&_iVars->buffer, sizeof(_iVars->buffer));
byteSwapVectorIfBE(_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
|