@@ -21,69 +21,69 @@ #import "OFInvalidArgumentException.h" #import "OFOutOfMemoryException.h" #import "OFOutOfRangeException.h" -#import "scrypt.h" -#import "pbkdf2.h" - -void -of_salsa20_8_core(uint32_t buffer[16]) -{ - uint32_t tmp[16]; - - for (uint_fast8_t i = 0; i < 16; i++) - tmp[i] = OF_BSWAP32_IF_BE(buffer[i]); - - for (uint_fast8_t i = 0; i < 8; i += 2) { - tmp[ 4] ^= OF_ROL(tmp[ 0] + tmp[12], 7); - tmp[ 8] ^= OF_ROL(tmp[ 4] + tmp[ 0], 9); - tmp[12] ^= OF_ROL(tmp[ 8] + tmp[ 4], 13); - tmp[ 0] ^= OF_ROL(tmp[12] + tmp[ 8], 18); - tmp[ 9] ^= OF_ROL(tmp[ 5] + tmp[ 1], 7); - tmp[13] ^= OF_ROL(tmp[ 9] + tmp[ 5], 9); - tmp[ 1] ^= OF_ROL(tmp[13] + tmp[ 9], 13); - tmp[ 5] ^= OF_ROL(tmp[ 1] + tmp[13], 18); - tmp[14] ^= OF_ROL(tmp[10] + tmp[ 6], 7); - tmp[ 2] ^= OF_ROL(tmp[14] + tmp[10], 9); - tmp[ 6] ^= OF_ROL(tmp[ 2] + tmp[14], 13); - tmp[10] ^= OF_ROL(tmp[ 6] + tmp[ 2], 18); - tmp[ 3] ^= OF_ROL(tmp[15] + tmp[11], 7); - tmp[ 7] ^= OF_ROL(tmp[ 3] + tmp[15], 9); - tmp[11] ^= OF_ROL(tmp[ 7] + tmp[ 3], 13); - tmp[15] ^= OF_ROL(tmp[11] + tmp[ 7], 18); - tmp[ 1] ^= OF_ROL(tmp[ 0] + tmp[ 3], 7); - tmp[ 2] ^= OF_ROL(tmp[ 1] + tmp[ 0], 9); - tmp[ 3] ^= OF_ROL(tmp[ 2] + tmp[ 1], 13); - tmp[ 0] ^= OF_ROL(tmp[ 3] + tmp[ 2], 18); - tmp[ 6] ^= OF_ROL(tmp[ 5] + tmp[ 4], 7); - tmp[ 7] ^= OF_ROL(tmp[ 6] + tmp[ 5], 9); - tmp[ 4] ^= OF_ROL(tmp[ 7] + tmp[ 6], 13); - tmp[ 5] ^= OF_ROL(tmp[ 4] + tmp[ 7], 18); - tmp[11] ^= OF_ROL(tmp[10] + tmp[ 9], 7); - tmp[ 8] ^= OF_ROL(tmp[11] + tmp[10], 9); - tmp[ 9] ^= OF_ROL(tmp[ 8] + tmp[11], 13); - tmp[10] ^= OF_ROL(tmp[ 9] + tmp[ 8], 18); - tmp[12] ^= OF_ROL(tmp[15] + tmp[14], 7); - tmp[13] ^= OF_ROL(tmp[12] + tmp[15], 9); - tmp[14] ^= OF_ROL(tmp[13] + tmp[12], 13); - tmp[15] ^= OF_ROL(tmp[14] + tmp[13], 18); - } - - for (uint_fast8_t i = 0; i < 16; i++) - buffer[i] = OF_BSWAP32_IF_BE(OF_BSWAP32_IF_BE(buffer[i]) + - tmp[i]); - - of_explicit_memset(tmp, 0, sizeof(tmp)); -} - -void -of_scrypt_block_mix(uint32_t *output, const uint32_t *input, size_t blockSize) -{ - uint32_t tmp[16]; - - /* Check defined here and executed in of_scrypt() */ +#import "OFScrypt.h" +#import "OFPBKDF2.h" + +void +OFSalsa20_8Core(uint32_t buffer[16]) +{ + uint32_t tmp[16]; + + for (uint_fast8_t i = 0; i < 16; i++) + tmp[i] = OFToLittleEndian32(buffer[i]); + + for (uint_fast8_t i = 0; i < 8; i += 2) { + tmp[ 4] ^= OFRotateLeft(tmp[ 0] + tmp[12], 7); + tmp[ 8] ^= OFRotateLeft(tmp[ 4] + tmp[ 0], 9); + tmp[12] ^= OFRotateLeft(tmp[ 8] + tmp[ 4], 13); + tmp[ 0] ^= OFRotateLeft(tmp[12] + tmp[ 8], 18); + tmp[ 9] ^= OFRotateLeft(tmp[ 5] + tmp[ 1], 7); + tmp[13] ^= OFRotateLeft(tmp[ 9] + tmp[ 5], 9); + tmp[ 1] ^= OFRotateLeft(tmp[13] + tmp[ 9], 13); + tmp[ 5] ^= OFRotateLeft(tmp[ 1] + tmp[13], 18); + tmp[14] ^= OFRotateLeft(tmp[10] + tmp[ 6], 7); + tmp[ 2] ^= OFRotateLeft(tmp[14] + tmp[10], 9); + tmp[ 6] ^= OFRotateLeft(tmp[ 2] + tmp[14], 13); + tmp[10] ^= OFRotateLeft(tmp[ 6] + tmp[ 2], 18); + tmp[ 3] ^= OFRotateLeft(tmp[15] + tmp[11], 7); + tmp[ 7] ^= OFRotateLeft(tmp[ 3] + tmp[15], 9); + tmp[11] ^= OFRotateLeft(tmp[ 7] + tmp[ 3], 13); + tmp[15] ^= OFRotateLeft(tmp[11] + tmp[ 7], 18); + tmp[ 1] ^= OFRotateLeft(tmp[ 0] + tmp[ 3], 7); + tmp[ 2] ^= OFRotateLeft(tmp[ 1] + tmp[ 0], 9); + tmp[ 3] ^= OFRotateLeft(tmp[ 2] + tmp[ 1], 13); + tmp[ 0] ^= OFRotateLeft(tmp[ 3] + tmp[ 2], 18); + tmp[ 6] ^= OFRotateLeft(tmp[ 5] + tmp[ 4], 7); + tmp[ 7] ^= OFRotateLeft(tmp[ 6] + tmp[ 5], 9); + tmp[ 4] ^= OFRotateLeft(tmp[ 7] + tmp[ 6], 13); + tmp[ 5] ^= OFRotateLeft(tmp[ 4] + tmp[ 7], 18); + tmp[11] ^= OFRotateLeft(tmp[10] + tmp[ 9], 7); + tmp[ 8] ^= OFRotateLeft(tmp[11] + tmp[10], 9); + tmp[ 9] ^= OFRotateLeft(tmp[ 8] + tmp[11], 13); + tmp[10] ^= OFRotateLeft(tmp[ 9] + tmp[ 8], 18); + tmp[12] ^= OFRotateLeft(tmp[15] + tmp[14], 7); + tmp[13] ^= OFRotateLeft(tmp[12] + tmp[15], 9); + tmp[14] ^= OFRotateLeft(tmp[13] + tmp[12], 13); + tmp[15] ^= OFRotateLeft(tmp[14] + tmp[13], 18); + } + + for (uint_fast8_t i = 0; i < 16; i++) + buffer[i] = OFToLittleEndian32(OFFromLittleEndian32(buffer[i]) + + tmp[i]); + + OFZeroMemory(tmp, sizeof(tmp)); +} + +void +OFScryptBlockMix(uint32_t *output, const uint32_t *input, size_t blockSize) +{ + uint32_t tmp[16]; + + /* Check defined here and executed in OFScrypt() */ #define OVERFLOW_CHECK_1 \ if (param.blockSize > SIZE_MAX / 2 || \ 2 * param.blockSize - 1 > SIZE_MAX / 16) \ @throw [OFOutOfRangeException exception]; @@ -91,27 +91,27 @@ for (size_t i = 0; i < 2 * blockSize; i++) { for (size_t j = 0; j < 16; j++) tmp[j] ^= input[i * 16 + j]; - of_salsa20_8_core(tmp); + OFSalsa20_8Core(tmp); /* * Even indices are stored in the first half and odd ones in * the second. */ memcpy(output + ((i / 2) + (i & 1) * blockSize) * 16, tmp, 64); } - of_explicit_memset(tmp, 0, sizeof(tmp)); + OFZeroMemory(tmp, sizeof(tmp)); } void -of_scrypt_romix(uint32_t *buffer, size_t blockSize, size_t costFactor, +OFScryptROMix(uint32_t *buffer, size_t blockSize, size_t costFactor, uint32_t *tmp) { - /* Check defined here and executed in of_scrypt() */ + /* Check defined here and executed in OFScrypt() */ #define OVERFLOW_CHECK_2 \ if (param.blockSize > SIZE_MAX / 128 / param.costFactor) \ @throw [OFOutOfRangeException exception]; uint32_t *tmp2 = tmp + 32 * blockSize; @@ -118,29 +118,29 @@ memcpy(tmp, buffer, 128 * blockSize); for (size_t i = 0; i < costFactor; i++) { memcpy(tmp2 + i * 32 * blockSize, tmp, 128 * blockSize); - of_scrypt_block_mix(tmp, tmp2 + i * 32 * blockSize, blockSize); + OFScryptBlockMix(tmp, tmp2 + i * 32 * blockSize, blockSize); } for (size_t i = 0; i < costFactor; i++) { - uint32_t j = OF_BSWAP32_IF_BE(tmp[(2 * blockSize - 1) * 16]) & - (costFactor - 1); + uint32_t j = OFFromLittleEndian32( + tmp[(2 * blockSize - 1) * 16]) & (costFactor - 1); for (size_t k = 0; k < 32 * blockSize; k++) tmp[k] ^= tmp2[j * 32 * blockSize + k]; - of_scrypt_block_mix(buffer, tmp, blockSize); + OFScryptBlockMix(buffer, tmp, blockSize); if (i < costFactor - 1) memcpy(tmp, buffer, 128 * blockSize); } } void -of_scrypt(of_scrypt_parameters_t param) +OFScrypt(OFScryptParameters param) { OFSecureData *tmp = nil, *buffer = nil; OFHMAC *HMAC = nil; if (param.blockSize == 0 || param.costFactor <= 1 || @@ -180,11 +180,11 @@ HMAC = [[OFHMAC alloc] initWithHashClass: [OFSHA256Hash class] allowsSwappableMemory: param.allowsSwappableMemory]; - of_pbkdf2((of_pbkdf2_parameters_t){ + OFPBKDF2((OFPBKDF2Parameters){ .HMAC = HMAC, .iterations = 1, .salt = param.salt, .saltLength = param.saltLength, .password = param.password, @@ -194,14 +194,14 @@ param.blockSize, .allowsSwappableMemory = param.allowsSwappableMemory }); for (size_t i = 0; i < param.parallelization; i++) - of_scrypt_romix(bufferItems + i * 32 * param.blockSize, + OFScryptROMix(bufferItems + i * 32 * param.blockSize, param.blockSize, param.costFactor, tmpItems); - of_pbkdf2((of_pbkdf2_parameters_t){ + OFPBKDF2((OFPBKDF2Parameters){ .HMAC = HMAC, .iterations = 1, .salt = (unsigned char *)bufferItems, .saltLength = param.parallelization * 128 * param.blockSize,