@@ -22,11 +22,11 @@ #import "OFSecureData.h" #import "OFHashAlreadyCalculatedException.h" #import "OFOutOfRangeException.h" -#define BLOCK_SIZE 64 +static const size_t blockSize = 64; @interface OFSHA224Or256Hash () - (void)of_resetState; @end @@ -52,11 +52,11 @@ static OF_INLINE void byteSwapVectorIfLE(uint32_t *vector, uint_fast8_t length) { #ifndef OF_BIG_ENDIAN for (uint_fast8_t i = 0; i < length; i++) - vector[i] = OF_BSWAP32(vector[i]); + vector[i] = OFByteSwap32(vector[i]); #endif } static void processBlock(uint32_t *state, uint32_t *buffer) @@ -77,24 +77,24 @@ for (i = 16; i < 64; i++) { uint32_t tmp; tmp = buffer[i - 2]; - buffer[i] = (OF_ROR(tmp, 17) ^ OF_ROR(tmp, 19) ^ (tmp >> 10)) + - buffer[i - 7]; + buffer[i] = (OFRotateRight(tmp, 17) ^ OFRotateRight(tmp, 19) ^ + (tmp >> 10)) + buffer[i - 7]; tmp = buffer[i - 15]; - buffer[i] += (OF_ROR(tmp, 7) ^ OF_ROR(tmp, 18) ^ (tmp >> 3)) + - buffer[i - 16]; + buffer[i] += (OFRotateRight(tmp, 7) ^ OFRotateRight(tmp, 18) ^ + (tmp >> 3)) + buffer[i - 16]; } for (i = 0; i < 64; i++) { - uint32_t tmp1 = new[7] + (OF_ROR(new[4], 6) ^ - OF_ROR(new[4], 11) ^ OF_ROR(new[4], 25)) + + uint32_t tmp1 = new[7] + (OFRotateRight(new[4], 6) ^ + OFRotateRight(new[4], 11) ^ OFRotateRight(new[4], 25)) + ((new[4] & (new[5] ^ new[6])) ^ new[6]) + table[i] + buffer[i]; - uint32_t tmp2 = (OF_ROR(new[0], 2) ^ OF_ROR(new[0], 13) ^ - OF_ROR(new[0], 22)) + + uint32_t tmp2 = (OFRotateRight(new[0], 2) ^ + OFRotateRight(new[0], 13) ^ OFRotateRight(new[0], 22)) + ((new[0] & (new[1] | new[2])) | (new[1] & new[2])); new[7] = new[6]; new[6] = new[5]; new[5] = new[4]; @@ -124,14 +124,14 @@ OF_UNRECOGNIZED_SELECTOR } + (size_t)blockSize { - return BLOCK_SIZE; + return blockSize; } -+ (instancetype)cryptoHashWithAllowsSwappableMemory: (bool)allowsSwappableMemory ++ (instancetype)hashWithAllowsSwappableMemory: (bool)allowsSwappableMemory { return [[[self alloc] initWithAllowsSwappableMemory: allowsSwappableMemory] autorelease]; } @@ -182,11 +182,11 @@ OF_UNRECOGNIZED_SELECTOR } - (size_t)blockSize { - return BLOCK_SIZE; + return blockSize; } - (id)copy { OFSHA224Or256Hash *copy = [[[self class] alloc] of_init]; @@ -197,12 +197,11 @@ copy->_calculated = _calculated; return copy; } -- (void)updateWithBuffer: (const void *)buffer_ - length: (size_t)length +- (void)updateWithBuffer: (const void *)buffer_ length: (size_t)length { const unsigned char *buffer = buffer_; if (_calculated) @throw [OFHashAlreadyCalculatedException @@ -237,25 +236,25 @@ { if (_calculated) return (const unsigned char *)_iVars->state; _iVars->buffer.bytes[_iVars->bufferLength] = 0x80; - of_explicit_memset(_iVars->buffer.bytes + _iVars->bufferLength + 1, 0, + OFZeroMemory(_iVars->buffer.bytes + _iVars->bufferLength + 1, 64 - _iVars->bufferLength - 1); if (_iVars->bufferLength >= 56) { processBlock(_iVars->state, _iVars->buffer.words); - of_explicit_memset(_iVars->buffer.bytes, 0, 64); + OFZeroMemory(_iVars->buffer.bytes, 64); } _iVars->buffer.words[14] = - OF_BSWAP32_IF_LE((uint32_t)(_iVars->bits >> 32)); + OFToBigEndian32((uint32_t)(_iVars->bits >> 32)); _iVars->buffer.words[15] = - OF_BSWAP32_IF_LE((uint32_t)(_iVars->bits & 0xFFFFFFFF)); + OFToBigEndian32((uint32_t)(_iVars->bits & 0xFFFFFFFF)); processBlock(_iVars->state, _iVars->buffer.words); - of_explicit_memset(&_iVars->buffer, 0, sizeof(_iVars->buffer)); + OFZeroMemory(&_iVars->buffer, sizeof(_iVars->buffer)); byteSwapVectorIfLE(_iVars->state, 8); _calculated = true; return (const unsigned char *)_iVars->state; } @@ -262,15 +261,15 @@ - (void)reset { [self of_resetState]; _iVars->bits = 0; - of_explicit_memset(&_iVars->buffer, 0, sizeof(_iVars->buffer)); + OFZeroMemory(&_iVars->buffer, sizeof(_iVars->buffer)); _iVars->bufferLength = 0; _calculated = false; } - (void)of_resetState { OF_UNRECOGNIZED_SELECTOR } @end