Coverage Report

Created: 2018-08-29 13:53

/src/openssl/crypto/asn1/p5_scrypt.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2015-2018 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 <stdio.h>
11
#include "internal/cryptlib.h"
12
#include <openssl/asn1t.h>
13
#include <openssl/err.h>
14
#include <openssl/evp.h>
15
#include <openssl/x509.h>
16
#include <openssl/rand.h>
17
18
#ifndef OPENSSL_NO_SCRYPT
19
/* PKCS#5 scrypt password based encryption structures */
20
21
ASN1_SEQUENCE(SCRYPT_PARAMS) = {
22
        ASN1_SIMPLE(SCRYPT_PARAMS, salt, ASN1_OCTET_STRING),
23
        ASN1_SIMPLE(SCRYPT_PARAMS, costParameter, ASN1_INTEGER),
24
        ASN1_SIMPLE(SCRYPT_PARAMS, blockSize, ASN1_INTEGER),
25
        ASN1_SIMPLE(SCRYPT_PARAMS, parallelizationParameter, ASN1_INTEGER),
26
        ASN1_OPT(SCRYPT_PARAMS, keyLength, ASN1_INTEGER),
27
} ASN1_SEQUENCE_END(SCRYPT_PARAMS)
28
29
IMPLEMENT_ASN1_FUNCTIONS(SCRYPT_PARAMS)
30
31
static X509_ALGOR *pkcs5_scrypt_set(const unsigned char *salt, size_t saltlen,
32
                                    size_t keylen, uint64_t N, uint64_t r,
33
                                    uint64_t p);
34
35
/*
36
 * Return an algorithm identifier for a PKCS#5 v2.0 PBE algorithm using scrypt
37
 */
38
39
X509_ALGOR *PKCS5_pbe2_set_scrypt(const EVP_CIPHER *cipher,
40
                                  const unsigned char *salt, int saltlen,
41
                                  unsigned char *aiv, uint64_t N, uint64_t r,
42
                                  uint64_t p)
43
0
{
44
0
    X509_ALGOR *scheme = NULL, *ret = NULL;
45
0
    int alg_nid;
46
0
    size_t keylen = 0;
47
0
    EVP_CIPHER_CTX *ctx = NULL;
48
0
    unsigned char iv[EVP_MAX_IV_LENGTH];
49
0
    PBE2PARAM *pbe2 = NULL;
50
0
51
0
    if (!cipher) {
52
0
        ASN1err(ASN1_F_PKCS5_PBE2_SET_SCRYPT, ERR_R_PASSED_NULL_PARAMETER);
53
0
        goto err;
54
0
    }
55
0
56
0
    if (EVP_PBE_scrypt(NULL, 0, NULL, 0, N, r, p, 0, NULL, 0) == 0) {
57
0
        ASN1err(ASN1_F_PKCS5_PBE2_SET_SCRYPT,
58
0
                ASN1_R_INVALID_SCRYPT_PARAMETERS);
59
0
        goto err;
60
0
    }
61
0
62
0
    alg_nid = EVP_CIPHER_type(cipher);
63
0
    if (alg_nid == NID_undef) {
64
0
        ASN1err(ASN1_F_PKCS5_PBE2_SET_SCRYPT,
65
0
                ASN1_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER);
66
0
        goto err;
67
0
    }
68
0
69
0
    pbe2 = PBE2PARAM_new();
70
0
    if (pbe2 == NULL)
71
0
        goto merr;
72
0
73
0
    /* Setup the AlgorithmIdentifier for the encryption scheme */
74
0
    scheme = pbe2->encryption;
75
0
76
0
    scheme->algorithm = OBJ_nid2obj(alg_nid);
77
0
    scheme->parameter = ASN1_TYPE_new();
78
0
    if (scheme->parameter == NULL)
79
0
        goto merr;
80
0
81
0
    /* Create random IV */
82
0
    if (EVP_CIPHER_iv_length(cipher)) {
83
0
        if (aiv)
84
0
            memcpy(iv, aiv, EVP_CIPHER_iv_length(cipher));
85
0
        else if (RAND_bytes(iv, EVP_CIPHER_iv_length(cipher)) <= 0)
86
0
            goto err;
87
0
    }
88
0
89
0
    ctx = EVP_CIPHER_CTX_new();
90
0
    if (ctx == NULL)
91
0
        goto merr;
92
0
93
0
    /* Dummy cipherinit to just setup the IV */
94
0
    if (EVP_CipherInit_ex(ctx, cipher, NULL, NULL, iv, 0) == 0)
95
0
        goto err;
96
0
    if (EVP_CIPHER_param_to_asn1(ctx, scheme->parameter) <= 0) {
97
0
        ASN1err(ASN1_F_PKCS5_PBE2_SET_SCRYPT,
98
0
                ASN1_R_ERROR_SETTING_CIPHER_PARAMS);
99
0
        goto err;
100
0
    }
101
0
    EVP_CIPHER_CTX_free(ctx);
102
0
    ctx = NULL;
103
0
104
0
    /* If its RC2 then we'd better setup the key length */
105
0
106
0
    if (alg_nid == NID_rc2_cbc)
107
0
        keylen = EVP_CIPHER_key_length(cipher);
108
0
109
0
    /* Setup keyfunc */
110
0
111
0
    X509_ALGOR_free(pbe2->keyfunc);
112
0
113
0
    pbe2->keyfunc = pkcs5_scrypt_set(salt, saltlen, keylen, N, r, p);
114
0
115
0
    if (pbe2->keyfunc == NULL)
116
0
        goto merr;
117
0
118
0
    /* Now set up top level AlgorithmIdentifier */
119
0
120
0
    ret = X509_ALGOR_new();
121
0
    if (ret == NULL)
122
0
        goto merr;
123
0
124
0
    ret->algorithm = OBJ_nid2obj(NID_pbes2);
125
0
126
0
    /* Encode PBE2PARAM into parameter */
127
0
128
0
    if (ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(PBE2PARAM), pbe2,
129
0
                                &ret->parameter) == NULL)
130
0
        goto merr;
131
0
132
0
    PBE2PARAM_free(pbe2);
133
0
    pbe2 = NULL;
134
0
135
0
    return ret;
136
0
137
0
 merr:
138
0
    ASN1err(ASN1_F_PKCS5_PBE2_SET_SCRYPT, ERR_R_MALLOC_FAILURE);
139
0
140
0
 err:
141
0
    PBE2PARAM_free(pbe2);
142
0
    X509_ALGOR_free(ret);
143
0
    EVP_CIPHER_CTX_free(ctx);
144
0
145
0
    return NULL;
146
0
}
147
148
static X509_ALGOR *pkcs5_scrypt_set(const unsigned char *salt, size_t saltlen,
149
                                    size_t keylen, uint64_t N, uint64_t r,
150
                                    uint64_t p)
151
0
{
152
0
    X509_ALGOR *keyfunc = NULL;
153
0
    SCRYPT_PARAMS *sparam = SCRYPT_PARAMS_new();
154
0
155
0
    if (sparam == NULL)
156
0
        goto merr;
157
0
158
0
    if (!saltlen)
159
0
        saltlen = PKCS5_SALT_LEN;
160
0
161
0
    /* This will either copy salt or grow the buffer */
162
0
    if (ASN1_STRING_set(sparam->salt, salt, saltlen) == 0)
163
0
        goto merr;
164
0
165
0
    if (salt == NULL && RAND_bytes(sparam->salt->data, saltlen) <= 0)
166
0
        goto err;
167
0
168
0
    if (ASN1_INTEGER_set_uint64(sparam->costParameter, N) == 0)
169
0
        goto merr;
170
0
171
0
    if (ASN1_INTEGER_set_uint64(sparam->blockSize, r) == 0)
172
0
        goto merr;
173
0
174
0
    if (ASN1_INTEGER_set_uint64(sparam->parallelizationParameter, p) == 0)
175
0
        goto merr;
176
0
177
0
    /* If have a key len set it up */
178
0
179
0
    if (keylen > 0) {
180
0
        sparam->keyLength = ASN1_INTEGER_new();
181
0
        if (sparam->keyLength == NULL)
182
0
            goto merr;
183
0
        if (ASN1_INTEGER_set_int64(sparam->keyLength, keylen) == 0)
184
0
            goto merr;
185
0
    }
186
0
187
0
    /* Finally setup the keyfunc structure */
188
0
189
0
    keyfunc = X509_ALGOR_new();
190
0
    if (keyfunc == NULL)
191
0
        goto merr;
192
0
193
0
    keyfunc->algorithm = OBJ_nid2obj(NID_id_scrypt);
194
0
195
0
    /* Encode SCRYPT_PARAMS into parameter of pbe2 */
196
0
197
0
    if (ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(SCRYPT_PARAMS), sparam,
198
0
                                &keyfunc->parameter) == NULL)
199
0
        goto merr;
200
0
201
0
    SCRYPT_PARAMS_free(sparam);
202
0
    return keyfunc;
203
0
204
0
 merr:
205
0
    ASN1err(ASN1_F_PKCS5_SCRYPT_SET, ERR_R_MALLOC_FAILURE);
206
0
 err:
207
0
    SCRYPT_PARAMS_free(sparam);
208
0
    X509_ALGOR_free(keyfunc);
209
0
    return NULL;
210
0
}
211
212
int PKCS5_v2_scrypt_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass,
213
                             int passlen, ASN1_TYPE *param,
214
                             const EVP_CIPHER *c, const EVP_MD *md, int en_de)
215
0
{
216
0
    unsigned char *salt, key[EVP_MAX_KEY_LENGTH];
217
0
    uint64_t p, r, N;
218
0
    size_t saltlen;
219
0
    size_t keylen = 0;
220
0
    int rv = 0;
221
0
    SCRYPT_PARAMS *sparam = NULL;
222
0
223
0
    if (EVP_CIPHER_CTX_cipher(ctx) == NULL) {
224
0
        EVPerr(EVP_F_PKCS5_V2_SCRYPT_KEYIVGEN, EVP_R_NO_CIPHER_SET);
225
0
        goto err;
226
0
    }
227
0
228
0
    /* Decode parameter */
229
0
230
0
    sparam = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(SCRYPT_PARAMS), param);
231
0
232
0
    if (sparam == NULL) {
233
0
        EVPerr(EVP_F_PKCS5_V2_SCRYPT_KEYIVGEN, EVP_R_DECODE_ERROR);
234
0
        goto err;
235
0
    }
236
0
237
0
    keylen = EVP_CIPHER_CTX_key_length(ctx);
238
0
239
0
    /* Now check the parameters of sparam */
240
0
241
0
    if (sparam->keyLength) {
242
0
        uint64_t spkeylen;
243
0
        if ((ASN1_INTEGER_get_uint64(&spkeylen, sparam->keyLength) == 0)
244
0
            || (spkeylen != keylen)) {
245
0
            EVPerr(EVP_F_PKCS5_V2_SCRYPT_KEYIVGEN,
246
0
                   EVP_R_UNSUPPORTED_KEYLENGTH);
247
0
            goto err;
248
0
        }
249
0
    }
250
0
    /* Check all parameters fit in uint64_t and are acceptable to scrypt */
251
0
    if (ASN1_INTEGER_get_uint64(&N, sparam->costParameter) == 0
252
0
        || ASN1_INTEGER_get_uint64(&r, sparam->blockSize) == 0
253
0
        || ASN1_INTEGER_get_uint64(&p, sparam->parallelizationParameter) == 0
254
0
        || EVP_PBE_scrypt(NULL, 0, NULL, 0, N, r, p, 0, NULL, 0) == 0) {
255
0
        EVPerr(EVP_F_PKCS5_V2_SCRYPT_KEYIVGEN,
256
0
               EVP_R_ILLEGAL_SCRYPT_PARAMETERS);
257
0
        goto err;
258
0
    }
259
0
260
0
    /* it seems that its all OK */
261
0
262
0
    salt = sparam->salt->data;
263
0
    saltlen = sparam->salt->length;
264
0
    if (EVP_PBE_scrypt(pass, passlen, salt, saltlen, N, r, p, 0, key, keylen)
265
0
        == 0)
266
0
        goto err;
267
0
    rv = EVP_CipherInit_ex(ctx, NULL, NULL, key, NULL, en_de);
268
0
 err:
269
0
    if (keylen)
270
0
        OPENSSL_cleanse(key, keylen);
271
0
    SCRYPT_PARAMS_free(sparam);
272
0
    return rv;
273
0
}
274
#endif /* OPENSSL_NO_SCRYPT */