| 1 |
// Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#include <openssl/evp.h> |
| 5 |
|
| 6 |
#include <assert.h> |
| 7 |
|
| 8 |
#include <openssl/err.h> |
| 9 |
#include <openssl/mem.h> |
| 10 |
#include <openssl/type_check.h> |
| 11 |
|
| 12 |
#include "../internal.h" |
| 13 |
|
| 14 |
|
| 15 |
// This file implements scrypt, described in RFC 7914. |
| 16 |
// |
| 17 |
// Note scrypt refers to both "blocks" and a "block size" parameter, r. These |
| 18 |
// are two different notions of blocks. A Salsa20 block is 64 bytes long, |
| 19 |
// represented in this implementation by 16 |uint32_t|s. |r| determines the |
| 20 |
// number of 64-byte Salsa20 blocks in a scryptBlockMix block, which is 2 * |r| |
| 21 |
// Salsa20 blocks. This implementation refers to them as Salsa20 blocks and |
| 22 |
// scrypt blocks, respectively. |
| 23 |
|
| 24 |
// A block_t is a Salsa20 block. |
| 25 |
#define SCRYPT_BLOCK_WORD_CNT 16 |
| 26 |
typedef struct { uint32_t words[SCRYPT_BLOCK_WORD_CNT]; } block_t; |
| 27 |
|
| 28 |
OPENSSL_STATIC_ASSERT(sizeof(block_t) == 64, block_t_has_padding) |
| 29 |
|
| 30 |
// salsa208_word_specification implements the Salsa20/8 core function, also |
| 31 |
// described in RFC 7914, section 3. It modifies the block at |inout| |
| 32 |
// in-place. |
| 33 |
static void salsa208_word_specification(block_t *inout) { |
| 34 |
block_t x; |
| 35 |
OPENSSL_memcpy(&x, inout, sizeof(x)); |
| 36 |
|
| 37 |
for (int i = 8; i > 0; i -= 2) { |
| 38 |
x.words[4] ^= CRYPTO_rotl_u32(x.words[0] + x.words[12], 7); |
| 39 |
x.words[8] ^= CRYPTO_rotl_u32(x.words[4] + x.words[0], 9); |
| 40 |
x.words[12] ^= CRYPTO_rotl_u32(x.words[8] + x.words[4], 13); |
| 41 |
x.words[0] ^= CRYPTO_rotl_u32(x.words[12] + x.words[8], 18); |
| 42 |
x.words[9] ^= CRYPTO_rotl_u32(x.words[5] + x.words[1], 7); |
| 43 |
x.words[13] ^= CRYPTO_rotl_u32(x.words[9] + x.words[5], 9); |
| 44 |
x.words[1] ^= CRYPTO_rotl_u32(x.words[13] + x.words[9], 13); |
| 45 |
x.words[5] ^= CRYPTO_rotl_u32(x.words[1] + x.words[13], 18); |
| 46 |
x.words[14] ^= CRYPTO_rotl_u32(x.words[10] + x.words[6], 7); |
| 47 |
x.words[2] ^= CRYPTO_rotl_u32(x.words[14] + x.words[10], 9); |
| 48 |
x.words[6] ^= CRYPTO_rotl_u32(x.words[2] + x.words[14], 13); |
| 49 |
x.words[10] ^= CRYPTO_rotl_u32(x.words[6] + x.words[2], 18); |
| 50 |
x.words[3] ^= CRYPTO_rotl_u32(x.words[15] + x.words[11], 7); |
| 51 |
x.words[7] ^= CRYPTO_rotl_u32(x.words[3] + x.words[15], 9); |
| 52 |
x.words[11] ^= CRYPTO_rotl_u32(x.words[7] + x.words[3], 13); |
| 53 |
x.words[15] ^= CRYPTO_rotl_u32(x.words[11] + x.words[7], 18); |
| 54 |
x.words[1] ^= CRYPTO_rotl_u32(x.words[0] + x.words[3], 7); |
| 55 |
x.words[2] ^= CRYPTO_rotl_u32(x.words[1] + x.words[0], 9); |
| 56 |
x.words[3] ^= CRYPTO_rotl_u32(x.words[2] + x.words[1], 13); |
| 57 |
x.words[0] ^= CRYPTO_rotl_u32(x.words[3] + x.words[2], 18); |
| 58 |
x.words[6] ^= CRYPTO_rotl_u32(x.words[5] + x.words[4], 7); |
| 59 |
x.words[7] ^= CRYPTO_rotl_u32(x.words[6] + x.words[5], 9); |
| 60 |
x.words[4] ^= CRYPTO_rotl_u32(x.words[7] + x.words[6], 13); |
| 61 |
x.words[5] ^= CRYPTO_rotl_u32(x.words[4] + x.words[7], 18); |
| 62 |
x.words[11] ^= CRYPTO_rotl_u32(x.words[10] + x.words[9], 7); |
| 63 |
x.words[8] ^= CRYPTO_rotl_u32(x.words[11] + x.words[10], 9); |
| 64 |
x.words[9] ^= CRYPTO_rotl_u32(x.words[8] + x.words[11], 13); |
| 65 |
x.words[10] ^= CRYPTO_rotl_u32(x.words[9] + x.words[8], 18); |
| 66 |
x.words[12] ^= CRYPTO_rotl_u32(x.words[15] + x.words[14], 7); |
| 67 |
x.words[13] ^= CRYPTO_rotl_u32(x.words[12] + x.words[15], 9); |
| 68 |
x.words[14] ^= CRYPTO_rotl_u32(x.words[13] + x.words[12], 13); |
| 69 |
x.words[15] ^= CRYPTO_rotl_u32(x.words[14] + x.words[13], 18); |
| 70 |
} |
| 71 |
|
| 72 |
for (int i = 0; i < 16; ++i) { |
| 73 |
inout->words[i] += x.words[i]; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
// xor_block sets |*out| to be |*a| XOR |*b|. |
| 78 |
static void xor_block(block_t *out, const block_t *a, const block_t *b) { |
| 79 |
for (size_t i = 0; i < 16; i++) { |
| 80 |
out->words[i] = a->words[i] ^ b->words[i]; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
// scryptBlockMix implements the function described in RFC 7914, section 4. B' |
| 85 |
// is written to |out|. |out| and |B| may not alias and must be each one scrypt |
| 86 |
// block (2 * |r| Salsa20 blocks) long. |
| 87 |
static void scryptBlockMix(block_t *out, const block_t *B, uint64_t r) { |
| 88 |
assert(out != B); |
| 89 |
|
| 90 |
block_t X; |
| 91 |
OPENSSL_memcpy(&X, &B[r * 2 - 1], sizeof(X)); |
| 92 |
for (uint64_t i = 0; i < r * 2; i++) { |
| 93 |
xor_block(&X, &X, &B[i]); |
| 94 |
salsa208_word_specification(&X); |
| 95 |
|
| 96 |
// This implements the permutation in step 3. |
| 97 |
OPENSSL_memcpy(&out[i / 2 + (i & 1) * r], &X, sizeof(X)); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
// scryptROMix implements the function described in RFC 7914, section 5. |B| is |
| 102 |
// an scrypt block (2 * |r| Salsa20 blocks) and is modified in-place. |T| and |
| 103 |
// |V| are scratch space allocated by the caller. |T| must have space for one |
| 104 |
// scrypt block (2 * |r| Salsa20 blocks). |V| must have space for |N| scrypt |
| 105 |
// blocks (2 * |r| * |N| Salsa20 blocks). |
| 106 |
static void scryptROMix(block_t *B, uint64_t r, uint64_t N, block_t *T, |
| 107 |
block_t *V) { |
| 108 |
// Steps 1 and 2. |
| 109 |
#ifdef OPENSSL_BIG_ENDIAN |
| 110 |
for(size_t i = 0; i < (2 * r * SCRYPT_BLOCK_WORD_CNT); i++) { |
| 111 |
CRYPTO_store_u32_le(&V->words[i], B->words[i]); |
| 112 |
} |
| 113 |
#else |
| 114 |
OPENSSL_memcpy(V, B, 2 * r * sizeof(block_t)); |
| 115 |
#endif |
| 116 |
for (uint64_t i = 1; i < N; i++) { |
| 117 |
scryptBlockMix(&V[2 * r * i /* scrypt block i */], |
| 118 |
&V[2 * r * (i - 1) /* scrypt block i-1 */], r); |
| 119 |
} |
| 120 |
scryptBlockMix(B, &V[2 * r * (N - 1) /* scrypt block N-1 */], r); |
| 121 |
|
| 122 |
// Step 3. |
| 123 |
for (uint64_t i = 0; i < N; i++) { |
| 124 |
// Note this assumes |N| <= 2^32 and is a power of 2. |
| 125 |
uint32_t j = B[2 * r - 1].words[0] & (N - 1); |
| 126 |
for (size_t k = 0; k < 2 * r; k++) { |
| 127 |
xor_block(&T[k], &B[k], &V[2 * r * j + k]); |
| 128 |
} |
| 129 |
scryptBlockMix(B, T, r); |
| 130 |
} |
| 131 |
#ifdef OPENSSL_BIG_ENDIAN |
| 132 |
for(size_t i = 0; i < (2 * r * SCRYPT_BLOCK_WORD_CNT); i++) { |
| 133 |
CRYPTO_store_u32_le(&B->words[i], B->words[i]); |
| 134 |
} |
| 135 |
#endif |
| 136 |
} |
| 137 |
|
| 138 |
// SCRYPT_PR_MAX is the maximum value of p * r. This is equivalent to the |
| 139 |
// bounds on p in section 6: |
| 140 |
// |
| 141 |
// p <= ((2^32-1) * hLen) / MFLen iff |
| 142 |
// p <= ((2^32-1) * 32) / (128 * r) iff |
| 143 |
// p * r <= (2^30-1) |
| 144 |
#define SCRYPT_PR_MAX ((1 << 30) - 1) |
| 145 |
|
| 146 |
// SCRYPT_MAX_MEM is the default maximum memory that may be allocated by |
| 147 |
// |EVP_PBE_scrypt|. |
| 148 |
#define SCRYPT_MAX_MEM (1024 * 1024 * 32) |
| 149 |
|
| 150 |
int EVP_PBE_scrypt(const char *password, size_t password_len, |
| 151 |
const uint8_t *salt, size_t salt_len, uint64_t N, uint64_t r, |
| 152 |
uint64_t p, size_t max_mem, uint8_t *out_key, |
| 153 |
size_t key_len) { |
| 154 |
if (r == 0 || p == 0 || p > SCRYPT_PR_MAX / r || |
| 155 |
// |N| must be a power of two. |
| 156 |
N < 2 || (N & (N - 1)) || |
| 157 |
// We only support |N| <= 2^32 in |scryptROMix|. |
| 158 |
N > UINT64_C(1) << 32 || |
| 159 |
// Check that |N| < 2^(128×r / 8). |
| 160 |
(16 * r <= 63 && N >= UINT64_C(1) << (16 * r))) { |
| 161 |
OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PARAMETERS); |
| 162 |
return 0; |
| 163 |
} |
| 164 |
|
| 165 |
// Determine the amount of memory needed. B, T, and V are |p|, 1, and |N| |
| 166 |
// scrypt blocks, respectively. Each scrypt block is 2*|r| |block_t|s. |
| 167 |
if (max_mem == 0) { |
| 168 |
max_mem = SCRYPT_MAX_MEM; |
| 169 |
} |
| 170 |
|
| 171 |
size_t max_scrypt_blocks = max_mem / (2 * r * sizeof(block_t)); |
| 172 |
if (max_scrypt_blocks < p + 1 || |
| 173 |
max_scrypt_blocks - p - 1 < N) { |
| 174 |
OPENSSL_PUT_ERROR(EVP, EVP_R_MEMORY_LIMIT_EXCEEDED); |
| 175 |
return 0; |
| 176 |
} |
| 177 |
|
| 178 |
// Allocate and divide up the scratch space. |max_mem| fits in a size_t, which |
| 179 |
// is no bigger than uint64_t, so none of these operations may overflow. |
| 180 |
OPENSSL_STATIC_ASSERT(UINT64_MAX >= SIZE_MAX, size_t_exceeds_uint64_t) |
| 181 |
size_t B_blocks = p * 2 * r; |
| 182 |
size_t B_bytes = B_blocks * sizeof(block_t); |
| 183 |
size_t T_blocks = 2 * r; |
| 184 |
size_t V_blocks = N * 2 * r; |
| 185 |
block_t *B = OPENSSL_calloc((B_blocks + T_blocks + V_blocks), sizeof(block_t)); |
| 186 |
if (B == NULL) { |
| 187 |
return 0; |
| 188 |
} |
| 189 |
|
| 190 |
int ret = 0; |
| 191 |
block_t *T = B + B_blocks; |
| 192 |
block_t *V = T + T_blocks; |
| 193 |
|
| 194 |
// NOTE: PKCS5_PBKDF2_HMAC can only fail due to allocation failure |
| 195 |
// or |iterations| of 0 (we pass 1 here). This is consistent with |
| 196 |
// the documented failure conditions of EVP_PBE_scrypt. |
| 197 |
if (!PKCS5_PBKDF2_HMAC(password, password_len, salt, salt_len, 1, |
| 198 |
EVP_sha256(), B_bytes, (uint8_t *)B)) { |
| 199 |
goto err; |
| 200 |
} |
| 201 |
|
| 202 |
for (uint64_t i = 0; i < p; i++) { |
| 203 |
scryptROMix(B + 2 * r * i, r, N, T, V); |
| 204 |
} |
| 205 |
|
| 206 |
if (!PKCS5_PBKDF2_HMAC(password, password_len, (const uint8_t *)B, B_bytes, 1, |
| 207 |
EVP_sha256(), key_len, out_key)) { |
| 208 |
goto err; |
| 209 |
} |
| 210 |
|
| 211 |
ret = 1; |
| 212 |
|
| 213 |
err: |
| 214 |
OPENSSL_free(B); |
| 215 |
return ret; |
| 216 |
} |