ObjFW  Diff

Differences From Artifact [a46dde114c]:

To Artifact [d4e11eaa57]:


16
17
18
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
16
17
18
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







+


+











-
+


-
+








-
+







 */

#include "config.h"

#include <string.h>

#import "OFSHA1Hash.h"
#import "OFSecureData.h"

#import "OFHashAlreadyCalculatedException.h"
#import "OFOutOfRangeException.h"

@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, uint8_t length)
byteSwapVectorIfLE(uint32_t *vector, uint_fast8_t length)
{
#ifndef OF_BIG_ENDIAN
	for (uint8_t i = 0; i < length; i++)
	for (uint_fast8_t i = 0; i < length; i++)
		vector[i] = OF_BSWAP32(vector[i]);
#endif
}

static void
processBlock(uint32_t *state, uint32_t *buffer)
{
	uint32_t new[5];
	uint8_t i;
	uint_fast8_t i;

	new[0] = state[0];
	new[1] = state[1];
	new[2] = state[2];
	new[3] = state[3];
	new[4] = state[4];

109
110
111
112
113
114
115





116





117
118
119





120
121
122
123

124
125
126
127
128
129
130

131
132
133

134
135

136
137
138
139
140
141
142
143
144
145
146
147





148
149
150
151
152
153

154
155
156
157
158



159

160
161
162

163
164
165
166

167
168


169
170
171
172
173
174
175



176
177
178
179
180
181
182
183

184
185
186



187
188
189
190



191
192

193
194



195
196
197
198



199
200
201

202
203
204
205
206
207
208
209



210
211
212
111
112
113
114
115
116
117
118
119
120
121
122

123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138

139
140
141
142
143
144
145

146
147


148


149
150
151
152
153
154
155
156





157
158
159
160
161
162
163
164
165
166

167
168
169
170
171
172
173
174
175

176
177
178

179
180
181
182
183
184


185
186
187
188
189
190



191
192
193
194
195
196
197
198
199
200

201
202


203
204
205
206



207
208
209
210
211
212


213
214
215
216



217
218
219
220
221

222
223
224
225
226
227



228
229
230
231
232
233







+
+
+
+
+
-
+
+
+
+
+



+
+
+
+
+



-
+






-
+

-
-
+
-
-
+







-
-
-
-
-
+
+
+
+
+





-
+





+
+
+
-
+


-
+




+
-
-
+
+




-
-
-
+
+
+







-
+

-
-
+
+
+

-
-
-
+
+
+


+
-
-
+
+
+

-
-
-
+
+
+


-
+





-
-
-
+
+
+



	return [[[self alloc] init] autorelease];
}

- (instancetype)init
{
	self = [super init];

	@try {
		_iVarsData = [[OFSecureData alloc]
		    initWithCount: sizeof(*_iVars)];
		_iVars = [_iVarsData items];

	[self of_resetState];
		[self of_resetState];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (instancetype)of_init
{
	return [super init];
}

- (void)dealloc
{
	[self reset];
	[_iVarsData release];

	[super dealloc];
}

- (id)copy
{
	OFSHA1Hash *copy = [[OFSHA1Hash alloc] init];
	OFSHA1Hash *copy = [[OFSHA1Hash alloc] of_init];

	memcpy(copy->_state, _state, sizeof(_state));
	copy->_bits = _bits;
	copy->_iVarsData = [_iVarsData copy];
	memcpy(&copy->_buffer, &_buffer, sizeof(_buffer));
	copy->_bufferLength = _bufferLength;
	copy->_iVars = [copy->_iVarsData items];
	copy->_calculated = _calculated;

	return copy;
}

- (void)of_resetState
{
	_state[0] = 0x67452301;
	_state[1] = 0xEFCDAB89;
	_state[2] = 0x98BADCFE;
	_state[3] = 0x10325476;
	_state[4] = 0xC3D2E1F0;
	_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_
		  length: (size_t)length
{
	const uint8_t *buffer = buffer_;
	const unsigned char *buffer = buffer_;

	if (_calculated)
		@throw [OFHashAlreadyCalculatedException
		    exceptionWithObject: self];

	if (length > SIZE_MAX / 8)
		@throw [OFOutOfRangeException exception];

	_bits += (length * 8);
	_iVars->bits += (length * 8);

	while (length > 0) {
		size_t min = 64 - _bufferLength;
		size_t min = 64 - _iVars->bufferLength;

		if (min > length)
			min = length;

		memcpy(_iVars->buffer.bytes + _iVars->bufferLength,
		memcpy(_buffer.bytes + _bufferLength, buffer, min);
		_bufferLength += min;
		    buffer, min);
		_iVars->bufferLength += min;

		buffer += min;
		length -= min;

		if (_bufferLength == 64) {
			processBlock(_state, _buffer.words);
			_bufferLength = 0;
		if (_iVars->bufferLength == 64) {
			processBlock(_iVars->state, _iVars->buffer.words);
			_iVars->bufferLength = 0;
		}
	}
}

- (const unsigned char *)digest
{
	if (_calculated)
		return (const uint8_t *)_state;
		return (const unsigned char *)_iVars->state;

	_buffer.bytes[_bufferLength] = 0x80;
	memset(_buffer.bytes + _bufferLength + 1, 0, 64 - _bufferLength - 1);
	_iVars->buffer.bytes[_iVars->bufferLength] = 0x80;
	of_explicit_memset(_iVars->buffer.bytes + _iVars->bufferLength + 1, 0,
	    64 - _iVars->bufferLength - 1);

	if (_bufferLength >= 56) {
		processBlock(_state, _buffer.words);
		memset(_buffer.bytes, 0, 64);
	if (_iVars->bufferLength >= 56) {
		processBlock(_iVars->state, _iVars->buffer.words);
		of_explicit_memset(_iVars->buffer.bytes, 0, 64);
	}

	_iVars->buffer.words[14] =
	_buffer.words[14] = OF_BSWAP32_IF_LE((uint32_t)(_bits >> 32));
	_buffer.words[15] = OF_BSWAP32_IF_LE((uint32_t)(_bits & 0xFFFFFFFF));
	    OF_BSWAP32_IF_LE((uint32_t)(_iVars->bits >> 32));
	_iVars->buffer.words[15] =
	    OF_BSWAP32_IF_LE((uint32_t)(_iVars->bits & 0xFFFFFFFF));

	processBlock(_state, _buffer.words);
	memset(&_buffer, 0, sizeof(_buffer));
	byteSwapVectorIfLE(_state, 5);
	processBlock(_iVars->state, _iVars->buffer.words);
	of_explicit_memset(&_iVars->buffer, 0, sizeof(_iVars->buffer));
	byteSwapVectorIfLE(_iVars->state, 5);
	_calculated = true;

	return (const uint8_t *)_state;
	return (const unsigned char *)_iVars->state;
}

- (void)reset
{
	[self of_resetState];
	_bits = 0;
	memset(&_buffer, 0, sizeof(_buffer));
	_bufferLength = 0;
	_iVars->bits = 0;
	of_explicit_memset(&_iVars->buffer, 0, sizeof(_iVars->buffer));
	_iVars->bufferLength = 0;
	_calculated = false;
}
@end