Differences From Artifact [42e8413fd6]:
- File
src/scrypt.m
— part of check-in
[b9641347e3]
at
2020-06-21 21:30:23
on branch trunk
— Move parameters for of_pbkdf2() to a struct
This should make it more readable for such a large number of parameters. (user: js, size: 6525) [annotate] [blame] [check-ins using]
To Artifact [163ddcc8f1]:
- File
src/scrypt.m
— part of check-in
[63f5276b33]
at
2020-06-21 22:08:00
on branch trunk
— Move parameters for of_scrypt() to a struct
This should make it more readable for such a large number of parameters. (user: js, size: 6818) [annotate] [blame] [check-ins using] [more...]
︙ | ︙ | |||
81 82 83 84 85 86 87 | 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() */ #define OVERFLOW_CHECK_1 \ | | | | 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | 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() */ #define OVERFLOW_CHECK_1 \ if (param.blockSize > SIZE_MAX / 2 || \ 2 * param.blockSize - 1 > SIZE_MAX / 16) \ @throw [OFOutOfRangeException exception]; memcpy(tmp, input + (2 * blockSize - 1) * 16, 64); for (size_t i = 0; i < 2 * blockSize; i++) { for (size_t j = 0; j < 16; j++) tmp[j] ^= input[i * 16 + j]; |
︙ | ︙ | |||
108 109 110 111 112 113 114 | } void of_scrypt_romix(uint32_t *buffer, size_t blockSize, size_t costFactor, uint32_t *tmp) { /* Check defined here and executed in of_scrypt() */ | | | | 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | } void of_scrypt_romix(uint32_t *buffer, size_t blockSize, size_t costFactor, uint32_t *tmp) { /* Check defined here and executed in of_scrypt() */ #define OVERFLOW_CHECK_2 \ if (param.blockSize > SIZE_MAX / 128 / param.costFactor) \ @throw [OFOutOfRangeException exception]; uint32_t *tmp2 = tmp + 32 * blockSize; memcpy(tmp, buffer, 128 * blockSize); for (size_t i = 0; i < costFactor; i++) { |
︙ | ︙ | |||
135 136 137 138 139 140 141 | of_scrypt_block_mix(buffer, tmp, blockSize); if (i < costFactor - 1) memcpy(tmp, buffer, 128 * blockSize); } } | | | < < | > | | | | | | | | | | | | | | | | | | | > | | | | | | | > | | | | | | | 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | of_scrypt_block_mix(buffer, tmp, blockSize); if (i < costFactor - 1) memcpy(tmp, buffer, 128 * blockSize); } } void of_scrypt(of_scrypt_parameters_t param) { OFSecureData *tmp = nil, *buffer = nil; OFHMAC *HMAC = nil; if (param.blockSize == 0 || param.costFactor <= 1 || (param.costFactor & (param.costFactor - 1)) != 0 || param.parallelization == 0) @throw [OFInvalidArgumentException exception]; /* * These are defined by the functions above. They are defined there so * that the check is next to the code and easy to verify, but actually * checked here for performance. */ OVERFLOW_CHECK_1 OVERFLOW_CHECK_2 @try { uint32_t *tmpItems, *bufferItems; if (param.costFactor > SIZE_MAX - 1 || (param.costFactor + 1) > SIZE_MAX / 128) @throw [OFOutOfRangeException exception]; tmp = [[OFSecureData alloc] initWithItemSize: param.blockSize count: (param.costFactor + 1) * 128 allowsSwappableMemory: param.allowsSwappableMemory]; tmpItems = tmp.mutableItems; if (param.parallelization > SIZE_MAX / 128) @throw [OFOutOfRangeException exception]; buffer = [[OFSecureData alloc] initWithItemSize: param.blockSize count: param.parallelization * 128 allowsSwappableMemory: param.allowsSwappableMemory]; bufferItems = buffer.mutableItems; HMAC = [[OFHMAC alloc] initWithHashClass: [OFSHA256Hash class] allowsSwappableMemory: param.allowsSwappableMemory]; of_pbkdf2((of_pbkdf2_parameters_t){ .HMAC = HMAC, .iterations = 1, .salt = param.salt, .saltLength = param.saltLength, .password = param.password, .passwordLength = param.passwordLength, .key = (unsigned char *)bufferItems, .keyLength = param.parallelization * 128 * param.blockSize, .allowsSwappableMemory = param.allowsSwappableMemory }); for (size_t i = 0; i < param.parallelization; i++) of_scrypt_romix(bufferItems + i * 32 * param.blockSize, param.blockSize, param.costFactor, tmpItems); of_pbkdf2((of_pbkdf2_parameters_t){ .HMAC = HMAC, .iterations = 1, .salt = (unsigned char *)bufferItems, .saltLength = param.parallelization * 128 * param.blockSize, .password = param.password, .passwordLength = param.passwordLength, .key = param.key, .keyLength = param.keyLength, .allowsSwappableMemory = param.allowsSwappableMemory }); } @finally { [tmp release]; [buffer release]; [HMAC release]; } } |