ObjFW  Diff

Differences From Artifact [78e42ea05b]:

To Artifact [fb812c0e73]:


133
134
135
136
137
138
139
140

141
142
143
144
145
146
147
133
134
135
136
137
138
139

140
141
142
143
144
145
146
147







-
+








		calculated = NO;
	}

	return self;
}

- updateWithBuffer: (const uint8_t*)buffer
- updateWithBuffer: (const char*)buffer
	    ofSize: (size_t)size
{
	uint32_t t;

	if (calculated)
		return self;
	if (size == 0)
155
156
157
158
159
160
161
162

163
164
165
166
167
168
169
155
156
157
158
159
160
161

162
163
164
165
166
167
168
169







-
+







	bits[1] += size >> 29;

	/* Bytes already in shsInfo->data */
	t = (t >> 3) & 0x3F;

	/* Handle any leading odd-sized chunks */
	if (t) {
		uint8_t *p = (uint8_t*)in + t;
		char *p = in + t;

		t = 64 - t;

		if (size < t) {
			memcpy(p, buffer, size);
			return self;
		}
188
189
190
191
192
193
194
195

196
197

198
199
200
201

202
203
204
205
206
207
208
188
189
190
191
192
193
194

195
196

197
198
199
200

201
202
203
204
205
206
207
208







-
+

-
+



-
+








	/* Handle any remaining bytes of data. */
	memcpy(in, buffer, size);

	return self;
}

- (uint8_t*)digest
- (char*)digest
{
	uint8_t	*p;
	char	*p;
	size_t	count;

	if (calculated)
		return (uint8_t*)buf;
		return (char*)buf;

	/* Compute number of bytes mod 64 */
	count = (bits[0] >> 3) & 0x3F;

	/*
	 * Set the first char of padding to 0x80. This is safe since there is
	 * always at least one byte free
229
230
231
232
233
234
235
236

237
238
239
240

241
242
243
244
245
246
247
229
230
231
232
233
234
235

236
237
238
239

240
241
242
243
244
245
246
247







-
+



-
+







	OF_BSWAP_V(in, 14);

	/* Append length in bits and transform */
	((uint32_t*)in)[14] = bits[0];
	((uint32_t*)in)[15] = bits[1];

	md5_transform(buf, (uint32_t*)in);
	OF_BSWAP_V((uint8_t*)buf, 4);
	OF_BSWAP_V((char*)buf, 4);

	calculated = YES;

	return (uint8_t*)buf;
	return (char*)buf;
}
@end

#undef F1
#undef F2
#undef F3
#undef F4
278
279
280
281
282
283
284
285

286
287
288
289
290

291
292
293

294
295
296
297
298
299
300
278
279
280
281
282
283
284

285
286
287
288
289

290
291
292

293
294
295
296
297
298
299
300







-
+




-
+


-
+







	z += (((w | x) & y) | (w & x)) + blk(i) + 0x8F1BBCDC + OF_ROL(v, 5); \
	w = OF_ROL(w, 30);
#define R4(v, w, x, y, z, i)						\
	z += (w ^ x ^ y) + blk(i) + 0xCA62C1D6 + OF_ROL(v, 5);		\
	w = OF_ROL(w, 30);

typedef union {
	uint8_t	 c[64];
	char	 c[64];
	uint32_t l[16];
} sha1_c64l16_t;

static inline void
sha1_transform(uint32_t state[5], const uint8_t buffer[64])
sha1_transform(uint32_t state[5], const char buffer[64])
{
	uint32_t      a, b, c, d, e;
	uint8_t	      workspace[64];
	char	      workspace[64];
	sha1_c64l16_t *block;

	block = (sha1_c64l16_t*)workspace;
	memcpy(block, buffer, 64);

	/* Copy state[] to working vars */
	a = state[0];
330
331
332
333
334
335
336
337
338


339
340
341
342
343
344
345
330
331
332
333
334
335
336


337
338
339
340
341
342
343
344
345







-
-
+
+







	state[1] += b;
	state[2] += c;
	state[3] += d;
	state[4] += e;
}

static inline void
sha1_update(uint32_t *state, uint64_t *count, uint8_t *buffer,
    const uint8_t *buf, size_t size)
sha1_update(uint32_t *state, uint64_t *count, char *buffer,
    const char *buf, size_t size)
{
	size_t i, j;

	j = (size_t)((*count >> 3) & 63);
	*count += (size << 3);

	if ((j + size) > 63) {
373
374
375
376
377
378
379
380

381
382
383
384
385
386
387
388
389
390
391
392
393

394
395
396

397
398
399
400
401
402
403
404


405
406
407

408
409
410
411
412

413
414
415
416
417
418
419
420
421
422
423
424
425
373
374
375
376
377
378
379

380
381
382
383
384
385
386
387
388
389
390
391
392

393
394
395

396
397
398
399
400
401
402


403
404
405
406

407
408
409
410
411

412
413
414
415
416
417
418
419
420
421
422
423
424
425







-
+












-
+


-
+






-
-
+
+


-
+




-
+













		state[3] = 0x10325476;
		state[4] = 0xC3D2E1F0;
	}

	return self;
}

- updateWithBuffer: (const uint8_t*)buf
- updateWithBuffer: (const char*)buf
	    ofSize: (size_t)size
{
	if (calculated)
		return self;
	if (size == 0)
		return self;

	sha1_update(state, &count, buffer, buf, size);

	return self;
}

- (uint8_t*)digest
- (char*)digest
{
	size_t i;
	uint8_t finalcount[8];
	char   finalcount[8];

	if (calculated)
		return digest;

	for (i = 0; i < 8; i++)
		/* Endian independent */
		finalcount[i] = (uint8_t)((count >> ((7 - (i & 7)) * 8)) & 255);
	sha1_update(state, &count, buffer, (const uint8_t*)"\200", 1);
		finalcount[i] = (char)((count >> ((7 - (i & 7)) * 8)) & 255);
	sha1_update(state, &count, buffer, "\200", 1);

	while ((count & 504) != 448)
		sha1_update(state, &count, buffer, (const uint8_t*)"\0", 1);
		sha1_update(state, &count, buffer, "\0", 1);
	/* Should cause a sha1_transform() */
	sha1_update(state, &count, buffer, finalcount, 8);

	for (i = 0; i < SHA1_DIGEST_SIZE; i++)
		digest[i] = (uint8_t)((state[i >> 2] >>
		digest[i] = (char)((state[i >> 2] >>
		    ((3 - (i & 3)) * 8)) & 255);

	return digest;
}
@end

#undef blk0
#undef blk
#undef R0
#undef R1
#undef R2
#undef R3
#undef R4