ObjFW  Check-in [c55c5dff51]

Overview
Comment:OFHMAC: Allow resetting while keeping the key
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: c55c5dff513e04dc8f896cfed3af470ef192c73a9b1ef94ad2926a057a6fa2f8
User & Date: js on 2016-07-24 20:51:18
Other Links: manifest | tags
Context
2016-07-25
22:30
Implement PBKDF2 check-in: 49d1987eaf user: js tags: trunk
2016-07-24
20:51
OFHMAC: Allow resetting while keeping the key check-in: c55c5dff51 user: js tags: trunk
20:05
OF*Hash: Call [self reset] in -[dealloc] check-in: 096dd743b5 user: js tags: trunk
Changes

Modified src/OFHMAC.h from [682e9b5139] to [fe33092934].

22
23
24
25
26
27
28

29

30
31



32
33
34
35
36
37
38
/*!
 * @class OFHMAC OFHMAC.h ObjFW/OFHMAC.h
 *
 * @brief A class which provides methods to calculate an HMAC.
 */
@interface OFHMAC: OFObject
{

	id <OFCryptoHash> _outerHash, _innerHash;

	bool _keySet, _calculated;
}




/*!
 * @brief Returns a new OFHMAC with the specified hashing algorithm.
 *
 * @param class The class of the hashing algorithm
 * @return A new, autoreleased OFHMAC
 */







>

>
|

>
>
>







22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*!
 * @class OFHMAC OFHMAC.h ObjFW/OFHMAC.h
 *
 * @brief A class which provides methods to calculate an HMAC.
 */
@interface OFHMAC: OFObject
{
	Class <OFCryptoHash> _hashClass;
	id <OFCryptoHash> _outerHash, _innerHash;
	id <OFCryptoHash> _outerHashCopy, _innerHashCopy;
	bool _calculated;
}

/*! The class for the cryptographic hash used by the HMAC. */
@property (assign, readonly) Class <OFCryptoHash> hashClass;

/*!
 * @brief Returns a new OFHMAC with the specified hashing algorithm.
 *
 * @param class The class of the hashing algorithm
 * @return A new, autoreleased OFHMAC
 */
46
47
48
49
50
51
52






53
54
55
56
57
58
59
 * @return An initialized OFHMAC
 */
- initWithHashClass: (Class <OFCryptoHash>)class;

/*!
 * @brief Sets the key for the HMAC.
 *






 * @param key The key for the HMAC
 * @param length The length of the key for the HMAC
 */
- (void)setKey: (const void*)key
	length: (size_t)length;

/*!







>
>
>
>
>
>







51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
 * @return An initialized OFHMAC
 */
- initWithHashClass: (Class <OFCryptoHash>)class;

/*!
 * @brief Sets the key for the HMAC.
 *
 * @note This resets the HMAC!
 *
 * @warning This invalidates any pointer previously returned by @ref digest. If
 *	    you are still interested in the previous digest, you need to memcpy
 *	    it yourself before calling @ref setKey:length:!
 *
 * @param key The key for the HMAC
 * @param length The length of the key for the HMAC
 */
- (void)setKey: (const void*)key
	length: (size_t)length;

/*!
79
80
81
82
83
84
85
86




87
88
89
90
91
92
93
94
95
 * @brief Returns the size of the digest.
 *
 * @return The size of the digest.
 */
- (size_t)digestSize;

/*!
 * @brief Resets all state so that a new HMAC can be calculated.




 *
 * @warning This invalidates any pointer previously returned by @ref digest. If
 *	    you are still interested in the previous digest, you need to memcpy
 *	    it yourself before calling @ref reset!
 */
- (void)reset;
@end

OF_ASSUME_NONNULL_END







|
>
>
>
>









90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
 * @brief Returns the size of the digest.
 *
 * @return The size of the digest.
 */
- (size_t)digestSize;

/*!
 * @brief Resets the HMAC so that it can be calculated for a new message.
 *
 * @note This does not reset the key so that a new HMAC with the same key can
 *	 be calculated efficiently. If you want to reset both, use
 *	 @ref setKey:length:.
 *
 * @warning This invalidates any pointer previously returned by @ref digest. If
 *	    you are still interested in the previous digest, you need to memcpy
 *	    it yourself before calling @ref reset!
 */
- (void)reset;
@end

OF_ASSUME_NONNULL_END

Modified src/OFHMAC.m from [9c331f3be4] to [8de8a11875].

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

58
59
60






61
62
63
64
65
66
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
122
123
124
125
126
127
128
129
130
131

132


133
134
135
136

#import "OFHMAC.h"

#import "OFHashAlreadyCalculatedException.h"
#import "OFInvalidArgumentException.h"

@implementation OFHMAC


+ (instancetype)HMACWithHashClass: (Class <OFCryptoHash>)class
{
	return [[[self alloc] initWithHashClass: class] autorelease];
}

- initWithHashClass: (id <OFCryptoHash>)class
{
	self = [super init];

	@try {
		void *pool = objc_autoreleasePoolPush();

		_outerHash = [[class cryptoHash] retain];
		_innerHash = [[class cryptoHash] retain];

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

	return self;
}

- (void)dealloc
{
	[_outerHash release];
	[_innerHash release];



	[super dealloc];
}

- (void)setKey: (const void*)key
	length: (size_t)length
{

	size_t blockSize = [[_outerHash class] blockSize];
	uint8_t outerKeyPad[blockSize], innerKeyPad[blockSize];







	if (length > blockSize) {
		void *pool = objc_autoreleasePoolPush();
		id <OFCryptoHash> hash = [[_outerHash class] cryptoHash];

		[hash updateWithBuffer: key
				length: length];

		length = [[hash class] digestSize];
		if OF_UNLIKELY (length > blockSize)
			length = blockSize;

		memcpy(outerKeyPad, [hash digest], length);
		memcpy(innerKeyPad, [hash digest], length);

		objc_autoreleasePoolPop(pool);
	} else {
		memcpy(outerKeyPad, key, length);
		memcpy(innerKeyPad, key, length);
	}

	memset(outerKeyPad + length, 0, blockSize - length);
	memset(innerKeyPad + length, 0, blockSize - length);

	for (size_t i = 0; i < blockSize; i++) {
		outerKeyPad[i] ^= 0x5C;
		innerKeyPad[i] ^= 0x36;
	}




	[_outerHash updateWithBuffer: outerKeyPad
			      length: blockSize];
	[_innerHash updateWithBuffer: innerKeyPad
			      length: blockSize];


	_keySet = true;




}

- (void)updateWithBuffer: (const void*)buffer
		  length: (size_t)length
{
	if (!_keySet)
		@throw [OFInvalidArgumentException exception];

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

	[_innerHash updateWithBuffer: buffer
			      length: length];
}

- (const unsigned char*)digest
{



	if (_calculated)
		return [_outerHash digest];

	[_outerHash updateWithBuffer: [_innerHash digest]
			      length: [[_innerHash class] digestSize]];
	_calculated = true;

	return [_outerHash digest];
}

- (size_t)digestSize
{
	return [[_outerHash class] digestSize];
}

- (void)reset
{
	[_outerHash reset];
	[_innerHash reset];




	_keySet = false;
	_calculated = false;
}
@end







>
>





|



<
<
|
<
<
<
<
<
<
<
<








>
>







>
|


>
>
>
>
>
>

<
|




|





<
<













>
>
>





>
|
>
>
>
>





|












>
>
>




|







|




|
|
>

>
>
|



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
58
59
60
61
62

63
64
65
66
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
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

#import "OFHMAC.h"

#import "OFHashAlreadyCalculatedException.h"
#import "OFInvalidArgumentException.h"

@implementation OFHMAC
@synthesize hashClass = _hashClass;

+ (instancetype)HMACWithHashClass: (Class <OFCryptoHash>)class
{
	return [[[self alloc] initWithHashClass: class] autorelease];
}

- initWithHashClass: (Class <OFCryptoHash>)class
{
	self = [super init];



	_hashClass = class;









	return self;
}

- (void)dealloc
{
	[_outerHash release];
	[_innerHash release];
	[_outerHashCopy release];
	[_innerHashCopy release];

	[super dealloc];
}

- (void)setKey: (const void*)key
	length: (size_t)length
{
	void *pool = objc_autoreleasePoolPush();
	size_t blockSize = [_hashClass blockSize];
	uint8_t outerKeyPad[blockSize], innerKeyPad[blockSize];

	[_outerHash release];
	[_innerHash release];
	[_outerHashCopy release];
	[_innerHashCopy release];
	_outerHash = _innerHash = _outerHashCopy = _innerHashCopy = nil;

	if (length > blockSize) {

		id <OFCryptoHash> hash = [_hashClass cryptoHash];

		[hash updateWithBuffer: key
				length: length];

		length = [_hashClass digestSize];
		if OF_UNLIKELY (length > blockSize)
			length = blockSize;

		memcpy(outerKeyPad, [hash digest], length);
		memcpy(innerKeyPad, [hash digest], length);


	} else {
		memcpy(outerKeyPad, key, length);
		memcpy(innerKeyPad, key, length);
	}

	memset(outerKeyPad + length, 0, blockSize - length);
	memset(innerKeyPad + length, 0, blockSize - length);

	for (size_t i = 0; i < blockSize; i++) {
		outerKeyPad[i] ^= 0x5C;
		innerKeyPad[i] ^= 0x36;
	}

	_outerHash = [[_hashClass cryptoHash] retain];
	_innerHash = [[_hashClass cryptoHash] retain];

	[_outerHash updateWithBuffer: outerKeyPad
			      length: blockSize];
	[_innerHash updateWithBuffer: innerKeyPad
			      length: blockSize];

	objc_autoreleasePoolPop(pool);

	_outerHashCopy = [_outerHash copy];
	_innerHashCopy = [_innerHash copy];

	_calculated = false;
}

- (void)updateWithBuffer: (const void*)buffer
		  length: (size_t)length
{
	if (_innerHash == nil)
		@throw [OFInvalidArgumentException exception];

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

	[_innerHash updateWithBuffer: buffer
			      length: length];
}

- (const unsigned char*)digest
{
	if (_outerHash == nil || _innerHash == nil)
		@throw [OFInvalidArgumentException exception];

	if (_calculated)
		return [_outerHash digest];

	[_outerHash updateWithBuffer: [_innerHash digest]
			      length: [_hashClass digestSize]];
	_calculated = true;

	return [_outerHash digest];
}

- (size_t)digestSize
{
	return [_hashClass digestSize];
}

- (void)reset
{
	[_outerHash release];
	[_innerHash release];
	_outerHash = _innerHash = nil;

	_outerHash = [_outerHashCopy copy];
	_innerHash = [_innerHashCopy copy];

	_calculated = false;
}
@end