More updates following review feedback
[openssl.git] / crypto / kdf / scrypt.c
1 /*
2  * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdlib.h>
11 #include <string.h>
12 #include <openssl/hmac.h>
13 #include <openssl/kdf.h>
14 #include <openssl/evp.h>
15 #include "internal/cryptlib.h"
16 #include "internal/evp_int.h"
17
18 #ifndef OPENSSL_NO_SCRYPT
19
20 static int atou64(const char *nptr, uint64_t *result);
21
22 typedef struct {
23     unsigned char *pass;
24     size_t pass_len;
25     unsigned char *salt;
26     size_t salt_len;
27     uint64_t N, r, p;
28     uint64_t maxmem_bytes;
29 } SCRYPT_PKEY_CTX;
30
31 /* Custom uint64_t parser since we do not have strtoull */
32 static int atou64(const char *nptr, uint64_t *result)
33 {
34     uint64_t value = 0;
35
36     while (*nptr) {
37         unsigned int digit;
38         uint64_t new_value;
39
40         if ((*nptr < '0') || (*nptr > '9')) {
41             return 0;
42         }
43         digit = (unsigned int)(*nptr - '0');
44         new_value = (value * 10) + digit;
45         if ((new_value < digit) || ((new_value - digit) / 10 != value)) {
46             /* Overflow */
47             return 0;
48         }
49         value = new_value;
50         nptr++;
51     }
52     *result = value;
53     return 1;
54 }
55
56 static int pkey_scrypt_init(EVP_PKEY_CTX *ctx)
57 {
58     SCRYPT_PKEY_CTX *kctx;
59
60     kctx = OPENSSL_zalloc(sizeof(*kctx));
61     if (kctx == NULL)
62         return 0;
63
64     /* Default values are the most conservative recommendation given in the
65      * original paper of C. Percival. Derivation uses roughly 1 GiB of memory
66      * for this parameter choice (approx. 128 * r * (N + p) bytes).
67      */
68     kctx->N = 1 << 20;
69     kctx->r = 8;
70     kctx->p = 1;
71     kctx->maxmem_bytes = 1025 * 1024 * 1024;
72
73     ctx->data = kctx;
74
75     return 1;
76 }
77
78 static void pkey_scrypt_cleanup(EVP_PKEY_CTX *ctx)
79 {
80     SCRYPT_PKEY_CTX *kctx = ctx->data;
81
82     OPENSSL_clear_free(kctx->salt, kctx->salt_len);
83     OPENSSL_clear_free(kctx->pass, kctx->pass_len);
84     OPENSSL_free(kctx);
85 }
86
87 static int pkey_scrypt_set_membuf(unsigned char **buffer, size_t *buflen,
88                                   const unsigned char *new_buffer,
89                                   const int new_buflen)
90 {
91     if (new_buffer == NULL)
92         return 1;
93
94     if (new_buflen < 0)
95         return 0;
96
97     if (*buffer != NULL)
98         OPENSSL_clear_free(*buffer, *buflen);
99
100     if (new_buflen > 0) {
101         *buffer = OPENSSL_memdup(new_buffer, new_buflen);
102     } else {
103         *buffer = OPENSSL_malloc(1);
104     }
105     if (*buffer == NULL)
106         return 0;
107
108     *buflen = new_buflen;
109     return 1;
110 }
111
112 static int is_power_of_two(uint64_t value)
113 {
114     return (value != 0) && ((value & (value - 1)) == 0);
115 }
116
117 static int pkey_scrypt_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
118 {
119     SCRYPT_PKEY_CTX *kctx = ctx->data;
120     uint64_t u64_value;
121
122     switch (type) {
123     case EVP_PKEY_CTRL_PASS:
124         return pkey_scrypt_set_membuf(&kctx->pass, &kctx->pass_len, p2, p1);
125
126     case EVP_PKEY_CTRL_SCRYPT_SALT:
127         return pkey_scrypt_set_membuf(&kctx->salt, &kctx->salt_len, p2, p1);
128
129     case EVP_PKEY_CTRL_SCRYPT_N:
130         u64_value = *((uint64_t *)p2);
131         if ((u64_value <= 1) || !is_power_of_two(u64_value))
132             return 0;
133         kctx->N = u64_value;
134         return 1;
135
136     case EVP_PKEY_CTRL_SCRYPT_R:
137         u64_value = *((uint64_t *)p2);
138         if (u64_value < 1)
139             return 0;
140         kctx->r = u64_value;
141         return 1;
142
143     case EVP_PKEY_CTRL_SCRYPT_P:
144         u64_value = *((uint64_t *)p2);
145         if (u64_value < 1)
146             return 0;
147         kctx->p = u64_value;
148         return 1;
149
150     case EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES:
151         u64_value = *((uint64_t *)p2);
152         if (u64_value < 1)
153             return 0;
154         kctx->maxmem_bytes = u64_value;
155         return 1;
156
157     default:
158         return -2;
159
160     }
161 }
162
163 static int pkey_scrypt_ctrl_uint64(EVP_PKEY_CTX *ctx, int type,
164                                    const char *value)
165 {
166     uint64_t int_value;
167
168     if (!atou64(value, &int_value)) {
169         KDFerr(KDF_F_PKEY_SCRYPT_CTRL_UINT64, KDF_R_VALUE_ERROR);
170         return 0;
171     }
172     return pkey_scrypt_ctrl(ctx, type, 0, &int_value);
173 }
174
175 static int pkey_scrypt_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
176                                 const char *value)
177 {
178     if (value == NULL) {
179         KDFerr(KDF_F_PKEY_SCRYPT_CTRL_STR, KDF_R_VALUE_MISSING);
180         return 0;
181     }
182
183     if (strcmp(type, "pass") == 0)
184         return EVP_PKEY_CTX_str2ctrl(ctx, EVP_PKEY_CTRL_PASS, value);
185
186     if (strcmp(type, "hexpass") == 0)
187         return EVP_PKEY_CTX_hex2ctrl(ctx, EVP_PKEY_CTRL_PASS, value);
188
189     if (strcmp(type, "salt") == 0)
190         return EVP_PKEY_CTX_str2ctrl(ctx, EVP_PKEY_CTRL_SCRYPT_SALT, value);
191
192     if (strcmp(type, "hexsalt") == 0)
193         return EVP_PKEY_CTX_hex2ctrl(ctx, EVP_PKEY_CTRL_SCRYPT_SALT, value);
194
195     if (strcmp(type, "N") == 0)
196         return pkey_scrypt_ctrl_uint64(ctx, EVP_PKEY_CTRL_SCRYPT_N, value);
197
198     if (strcmp(type, "r") == 0)
199         return pkey_scrypt_ctrl_uint64(ctx, EVP_PKEY_CTRL_SCRYPT_R, value);
200
201     if (strcmp(type, "p") == 0)
202         return pkey_scrypt_ctrl_uint64(ctx, EVP_PKEY_CTRL_SCRYPT_P, value);
203
204     if (strcmp(type, "maxmem_bytes") == 0)
205         return pkey_scrypt_ctrl_uint64(ctx, EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES,
206                                        value);
207
208     KDFerr(KDF_F_PKEY_SCRYPT_CTRL_STR, KDF_R_UNKNOWN_PARAMETER_TYPE);
209     return -2;
210 }
211
212 static int pkey_scrypt_derive(EVP_PKEY_CTX *ctx, unsigned char *key,
213                               size_t *keylen)
214 {
215     SCRYPT_PKEY_CTX *kctx = ctx->data;
216
217     if (kctx->pass == NULL) {
218         KDFerr(KDF_F_PKEY_SCRYPT_DERIVE, KDF_R_MISSING_PASS);
219         return 0;
220     }
221
222     if (kctx->salt == NULL) {
223         KDFerr(KDF_F_PKEY_SCRYPT_DERIVE, KDF_R_MISSING_SALT);
224         return 0;
225     }
226
227     return EVP_PBE_scrypt((char *)kctx->pass, kctx->pass_len, kctx->salt,
228                           kctx->salt_len, kctx->N, kctx->r, kctx->p,
229                           kctx->maxmem_bytes, key, *keylen);
230 }
231
232 const EVP_PKEY_METHOD scrypt_pkey_meth = {
233     EVP_PKEY_SCRYPT,
234     0,
235     pkey_scrypt_init,
236     0,
237     pkey_scrypt_cleanup,
238
239     0, 0,
240     0, 0,
241
242     0,
243     0,
244
245     0,
246     0,
247
248     0, 0,
249
250     0, 0, 0, 0,
251
252     0, 0,
253
254     0, 0,
255
256     0,
257     pkey_scrypt_derive,
258     pkey_scrypt_ctrl,
259     pkey_scrypt_ctrl_str
260 };
261
262 #endif