ObjFW  Check-in [6a0ac6a3f1]

Overview
Comment:Fix a bug in OFHashing (SHA1 didn't cache correctly in -[digit]).
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 6a0ac6a3f122f245bad8714d64899030827ea611b7b509b58c80676a7a957063
User & Date: js on 2009-08-28 17:57:07
Other Links: manifest | tags
Context
2009-08-30
20:06
Add some PowerPC assembly optimizations. check-in: 37e9b7c11c user: js tags: trunk
2009-08-28
17:57
Fix a bug in OFHashing (SHA1 didn't cache correctly in -[digit]). check-in: 6a0ac6a3f1 user: js tags: trunk
02:12
Add assembly implementation for OF_BSWAP{16,32,64} for x86 and AMD64. check-in: 42aaecc3cd user: js tags: trunk
Changes

Modified src/OFHashes.h from [d2aa9374f1] to [5648aee5dd].

8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import "OFObject.h"
#import "OFString.h"

#define MD5_DIGEST_SIZE	 16
#define SHA1_DIGEST_SIZE 20

extern int _OFHashing_reference;

/**
 * The OFMD5Hash class provides functions to create an MD5 hash.
 */
@interface OFMD5Hash: OFObject







|
|







8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import "OFObject.h"
#import "OFString.h"

#define OF_MD5_DIGEST_SIZE  16
#define OF_SHA1_DIGEST_SIZE 20

extern int _OFHashing_reference;

/**
 * The OFMD5Hash class provides functions to create an MD5 hash.
 */
@interface OFMD5Hash: OFObject
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
 * \param buf The buffer which should be included into calculation.
 * \param size The size of the buffer
 */
- updateWithBuffer: (const char*)buf
	    ofSize: (size_t)size;

/**
 * \return A buffer containing the hash (MD5_DIGEST_SIZE = 16 bytes).
 *	   The buffer is part of object's memory pool.
 */
- (uint8_t*)digest;
@end

/**
 * The OFSHA1Hash class provides functions to create an SHA1 hash.
 */
@interface OFSHA1Hash: OFObject
{
	uint32_t state[5];
	uint64_t count;
	char	 buffer[64];
	uint8_t	 digest[SHA1_DIGEST_SIZE];

	BOOL	 calculated;
}

/**
 * \return A new autoreleased SHA1 Hash
 */







|













|







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
 * \param buf The buffer which should be included into calculation.
 * \param size The size of the buffer
 */
- updateWithBuffer: (const char*)buf
	    ofSize: (size_t)size;

/**
 * \return A buffer containing the hash (OF_MD5_DIGEST_SIZE = 16 bytes).
 *	   The buffer is part of object's memory pool.
 */
- (uint8_t*)digest;
@end

/**
 * The OFSHA1Hash class provides functions to create an SHA1 hash.
 */
@interface OFSHA1Hash: OFObject
{
	uint32_t state[5];
	uint64_t count;
	char	 buffer[64];
	uint8_t	 digest[OF_SHA1_DIGEST_SIZE];

	BOOL	 calculated;
}

/**
 * \return A new autoreleased SHA1 Hash
 */
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
 * \param buf The buffer which should be included into calculation.
 * \param size The size of the buffer
 */
- updateWithBuffer: (const char*)buf
	    ofSize: (size_t)size;

/**
 * \return A buffer containing the hash (SHA1_DIGEST_SIZE = 20 bytes).
 *	   The buffer is part of object's memory pool.
 */
- (uint8_t*)digest;
@end

/**
 * The OFString (OFHashing) category provides methods to calculate hashes for







|







78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
 * \param buf The buffer which should be included into calculation.
 * \param size The size of the buffer
 */
- updateWithBuffer: (const char*)buf
	    ofSize: (size_t)size;

/**
 * \return A buffer containing the hash (OF_SHA1_DIGEST_SIZE = 20 bytes).
 *	   The buffer is part of object's memory pool.
 */
- (uint8_t*)digest;
@end

/**
 * The OFString (OFHashing) category provides methods to calculate hashes for

Modified src/OFHashes.m from [a7bb9e72ee] to [11feb3bb82].

16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#import "OFHashes.h"
#import "OFAutoreleasePool.h"
#import "OFExceptions.h"
#import "OFMacros.h"

int _OFHashing_reference;

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

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








|
|
|







16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#import "OFHashes.h"
#import "OFAutoreleasePool.h"
#import "OFExceptions.h"
#import "OFMacros.h"

int _OFHashing_reference;

/*
 * MD5
 */

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

244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260

#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







|
|
|







244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260

#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
406
407
408
409
410
411
412
413
414
415


416
417
418
419
420
421
422
	sha1_update(state, &count, buffer, "\200", 1);

	while ((count & 504) != 448)
		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] = (char)((state[i >> 2] >>
		    ((3 - (i & 3)) * 8)) & 255);



	return digest;
}
@end

#undef blk0
#undef blk







|


>
>







406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
	sha1_update(state, &count, buffer, "\200", 1);

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

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

	calculated = YES;

	return digest;
}
@end

#undef blk0
#undef blk

Modified tests/OFHashes/OFHashes.m from [326e3de950] to [b3e57d0d68].

13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

#include <stdio.h>
#include <string.h>

#import "OFHashes.h"
#import "OFFile.h"

const uint8_t testfile_md5[MD5_DIGEST_SIZE] =
    "\x00\x8B\x9D\x1B\x58\xDF\xF8\xFE\xEE\xF3\xAE\x8D\xBB\x68\x2D\x38";
const uint8_t testfile_sha1[SHA1_DIGEST_SIZE] =
    "\xC9\x9A\xB8\x7E\x1E\xC8\xEC\x65\xD5\xEB\xE4\x2E\x0D\xA6\x80\x96\xF5"
    "\x94\xE7\x17";

int
main()
{
	char   buf[64];







|

|







13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

#include <stdio.h>
#include <string.h>

#import "OFHashes.h"
#import "OFFile.h"

const uint8_t testfile_md5[OF_MD5_DIGEST_SIZE] =
    "\x00\x8B\x9D\x1B\x58\xDF\xF8\xFE\xEE\xF3\xAE\x8D\xBB\x68\x2D\x38";
const uint8_t testfile_sha1[OF_SHA1_DIGEST_SIZE] =
    "\xC9\x9A\xB8\x7E\x1E\xC8\xEC\x65\xD5\xEB\xE4\x2E\x0D\xA6\x80\x96\xF5"
    "\x94\xE7\x17";

int
main()
{
	char   buf[64];
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
		[md5 updateWithBuffer: buf
			       ofSize: len];
		[sha1 updateWithBuffer: buf
				ofSize: len];
	}
	[f close];

	if (!memcmp([md5 digest], testfile_md5, MD5_DIGEST_SIZE)) {
		fputs("\r\033[1;33mTests successful: 1/2\033[0m", stdout);
		fflush(stdout);
	} else {
		puts("\r\033[K\033[1;31mTest 1/2 failed!\033[0m");
		return 1;
	}

	if (!memcmp([sha1 digest], testfile_sha1, SHA1_DIGEST_SIZE))
		puts("\r\033[1;32mTests successful: 2/2\033[0m");
	else {
		puts("\r\033[K\033[1;31mTest 2/2 failed!\033[0m");
		return 1;
	}

	return 0;
}







|







|








40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
		[md5 updateWithBuffer: buf
			       ofSize: len];
		[sha1 updateWithBuffer: buf
				ofSize: len];
	}
	[f close];

	if (!memcmp([md5 digest], testfile_md5, OF_MD5_DIGEST_SIZE)) {
		fputs("\r\033[1;33mTests successful: 1/2\033[0m", stdout);
		fflush(stdout);
	} else {
		puts("\r\033[K\033[1;31mTest 1/2 failed!\033[0m");
		return 1;
	}

	if (!memcmp([sha1 digest], testfile_sha1, OF_SHA1_DIGEST_SIZE))
		puts("\r\033[1;32mTests successful: 2/2\033[0m");
	else {
		puts("\r\033[K\033[1;31mTest 2/2 failed!\033[0m");
		return 1;
	}

	return 0;
}