ObjFW  Diff

Differences From Artifact [ae5d03b1b4]:

To Artifact [ee4f52853e]:


11
12
13
14
15
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

#import "config.h"

#import <string.h>
#import <stdint.h>

#import "OFHashes.h"

#ifdef OF_BIG_ENDIAN
inline void
bswap(uint8_t *buf, size_t len)
{
	uint32_t t;
	while (len--) {
		t = (uint32_t)((uint32_t)buf[3] << 8 | buf[2]) << 16 |
		    ((uint32_t)buf[1] << 8 | buf[0]);
		*(uint32_t*)buf = t;
		buf += 4;
	}
}
#else
#define bswap(buf, len)
#endif

#define rol(val, bits) \
	(((val) << (bits)) | ((val) >> (32 - (bits))))

/*******
 * MD5 *
 *******/

/* The four MD5 core functions - F1 is optimized somewhat */
#define F1(x, y, z) (z ^ (x & (y ^ z)))







|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







11
12
13
14
15
16
17
18


















19
20
21
22
23
24
25

#import "config.h"

#import <string.h>
#import <stdint.h>

#import "OFHashes.h"
#import "OFMacros.h"



















/*******
 * MD5 *
 *******/

/* The four MD5 core functions - F1 is optimized somewhat */
#define F1(x, y, z) (z ^ (x & (y ^ z)))
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

		if (size < t) {
			memcpy(p, buffer, size);
			return self;
		}

		memcpy(p, buffer, t);
		bswap(in, 16);
		md5_transform(buf, (uint32_t*)in);

		buffer += t;
		size -= t;
	}

	/* Process data in 64-byte chunks */
	while (size >= 64) {
		memcpy(in, buffer, 64);
		bswap(in, 16);
		md5_transform(buf, (uint32_t*)in);

		buffer += 64;
		size -= 64;
	}

	/* Handle any remaining bytes of data. */







|









|







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

		if (size < t) {
			memcpy(p, buffer, size);
			return self;
		}

		memcpy(p, buffer, t);
		OF_BSWAP_V(in, 16);
		md5_transform(buf, (uint32_t*)in);

		buffer += t;
		size -= t;
	}

	/* Process data in 64-byte chunks */
	while (size >= 64) {
		memcpy(in, buffer, 64);
		OF_BSWAP_V(in, 16);
		md5_transform(buf, (uint32_t*)in);

		buffer += 64;
		size -= 64;
	}

	/* Handle any remaining bytes of data. */
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
256
257
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
295
296
297
298
299
300
301
302
303
	/* Bytes of padding needed to make 64 bytes */
	count = 64 - 1 - count;

	/* Pad out to 56 mod 64 */
	if (count < 8) {
		/* Two lots of padding: Pad the first block to 64 bytes */
		memset(p, 0, count);
		bswap(in, 16);
		md5_transform(buf, (uint32_t*)in);

		/* Now fill the next block with 56 bytes */
		memset(in, 0, 56);
	} else {
		/* Pad block to 56 bytes */
		memset(p, 0, count - 8);
	}
	bswap(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);
	bswap((uint8_t*)buf, 4);

	calculated = YES;

	return (uint8_t*)buf;
}
@end

#undef F1
#undef F2
#undef F3
#undef F4
#undef MD5STEP

/********
 * SHA1 *
 ********/

/* blk0() and blk() perform the initial expand. */
#ifndef OF_BIG_ENDIAN
#define blk0(i)							\
	(block->l[i] = (rol(block->l[i], 24) & 0xFF00FF00) |	\
	    (rol(block->l[i], 8) & 0x00FF00FF))
#else
#define blk0(i) block->l[i]
#endif
#define blk(i) \
	(block->l[i & 15] = rol(block->l[(i + 13) & 15] ^	\
	    block->l[(i + 8) & 15] ^ block->l[(i + 2) & 15] ^	\
	    block->l[i & 15], 1))

/* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
#define R0(v, w, x, y, z, i)						\
	z += ((w & (x ^ y)) ^ y) + blk0(i) + 0x5A827999 + rol(v, 5);	\
	w = rol(w, 30);
#define R1(v, w, x, y, z, i)						\
	z += ((w & (x ^ y)) ^ y) + blk(i) +  0x5A827999 + rol(v, 5);	\
	w = rol(w, 30);
#define R2(v, w, x, y, z, i)						\
	z += (w ^ x ^ y) + blk(i) + 0x6ED9EBA1 + rol(v, 5);		\
	w = rol(w, 30);
#define R3(v, w, x, y, z, i)						  \
	z += (((w | x) & y) | (w & x)) + blk(i) + 0x8F1BBCDC + rol(v, 5); \
	w = rol(w, 30);
#define R4(v, w, x, y, z, i)						\
	z += (w ^ x ^ y) + blk(i) + 0xCA62C1D6 + rol(v, 5);		\
	w = rol(w, 30);

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

inline void







|








|






|




















|
|




|





|
|

|
|

|
|
|
|
|

|
|







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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
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
	/* Bytes of padding needed to make 64 bytes */
	count = 64 - 1 - count;

	/* Pad out to 56 mod 64 */
	if (count < 8) {
		/* Two lots of padding: Pad the first block to 64 bytes */
		memset(p, 0, count);
		OF_BSWAP_V(in, 16);
		md5_transform(buf, (uint32_t*)in);

		/* Now fill the next block with 56 bytes */
		memset(in, 0, 56);
	} else {
		/* Pad block to 56 bytes */
		memset(p, 0, count - 8);
	}
	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);

	calculated = YES;

	return (uint8_t*)buf;
}
@end

#undef F1
#undef F2
#undef F3
#undef F4
#undef MD5STEP

/********
 * SHA1 *
 ********/

/* blk0() and blk() perform the initial expand. */
#ifndef OF_BIG_ENDIAN
#define blk0(i)							\
	(block->l[i] = (OF_ROL(block->l[i], 24) & 0xFF00FF00) |	\
	    (OF_ROL(block->l[i], 8) & 0x00FF00FF))
#else
#define blk0(i) block->l[i]
#endif
#define blk(i) \
	(block->l[i & 15] = OF_ROL(block->l[(i + 13) & 15] ^	\
	    block->l[(i + 8) & 15] ^ block->l[(i + 2) & 15] ^	\
	    block->l[i & 15], 1))

/* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
#define R0(v, w, x, y, z, i)						\
	z += ((w & (x ^ y)) ^ y) + blk0(i) + 0x5A827999 + OF_ROL(v, 5);	\
	w = OF_ROL(w, 30);
#define R1(v, w, x, y, z, i)						\
	z += ((w & (x ^ y)) ^ y) + blk(i) +  0x5A827999 + OF_ROL(v, 5);	\
	w = OF_ROL(w, 30);
#define R2(v, w, x, y, z, i)						\
	z += (w ^ x ^ y) + blk(i) + 0x6ED9EBA1 + OF_ROL(v, 5);		\
	w = OF_ROL(w, 30);
#define R3(v, w, x, y, z, i)						     \
	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];
	uint32_t l[16];
} sha1_c64l16_t;

inline void