1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2017-2020 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci/* Tests of the EVP_PKEY_CTX_set_* macro family */
11e1051a39Sopenharmony_ci
12e1051a39Sopenharmony_ci#include <stdio.h>
13e1051a39Sopenharmony_ci#include <string.h>
14e1051a39Sopenharmony_ci
15e1051a39Sopenharmony_ci#include <openssl/evp.h>
16e1051a39Sopenharmony_ci#include <openssl/kdf.h>
17e1051a39Sopenharmony_ci#include "testutil.h"
18e1051a39Sopenharmony_ci
19e1051a39Sopenharmony_cistatic int test_kdf_tls1_prf(void)
20e1051a39Sopenharmony_ci{
21e1051a39Sopenharmony_ci    int ret = 0;
22e1051a39Sopenharmony_ci    EVP_PKEY_CTX *pctx;
23e1051a39Sopenharmony_ci    unsigned char out[16];
24e1051a39Sopenharmony_ci    size_t outlen = sizeof(out);
25e1051a39Sopenharmony_ci
26e1051a39Sopenharmony_ci    if ((pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_TLS1_PRF, NULL)) == NULL) {
27e1051a39Sopenharmony_ci        TEST_error("EVP_PKEY_TLS1_PRF");
28e1051a39Sopenharmony_ci        goto err;
29e1051a39Sopenharmony_ci    }
30e1051a39Sopenharmony_ci    if (EVP_PKEY_derive_init(pctx) <= 0) {
31e1051a39Sopenharmony_ci        TEST_error("EVP_PKEY_derive_init");
32e1051a39Sopenharmony_ci        goto err;
33e1051a39Sopenharmony_ci    }
34e1051a39Sopenharmony_ci    if (EVP_PKEY_CTX_set_tls1_prf_md(pctx, EVP_sha256()) <= 0) {
35e1051a39Sopenharmony_ci        TEST_error("EVP_PKEY_CTX_set_tls1_prf_md");
36e1051a39Sopenharmony_ci        goto err;
37e1051a39Sopenharmony_ci    }
38e1051a39Sopenharmony_ci    if (EVP_PKEY_CTX_set1_tls1_prf_secret(pctx,
39e1051a39Sopenharmony_ci                                          (unsigned char *)"secret", 6) <= 0) {
40e1051a39Sopenharmony_ci        TEST_error("EVP_PKEY_CTX_set1_tls1_prf_secret");
41e1051a39Sopenharmony_ci        goto err;
42e1051a39Sopenharmony_ci    }
43e1051a39Sopenharmony_ci    if (EVP_PKEY_CTX_add1_tls1_prf_seed(pctx,
44e1051a39Sopenharmony_ci                                        (unsigned char *)"seed", 4) <= 0) {
45e1051a39Sopenharmony_ci        TEST_error("EVP_PKEY_CTX_add1_tls1_prf_seed");
46e1051a39Sopenharmony_ci        goto err;
47e1051a39Sopenharmony_ci    }
48e1051a39Sopenharmony_ci    if (EVP_PKEY_derive(pctx, out, &outlen) <= 0) {
49e1051a39Sopenharmony_ci        TEST_error("EVP_PKEY_derive");
50e1051a39Sopenharmony_ci        goto err;
51e1051a39Sopenharmony_ci    }
52e1051a39Sopenharmony_ci
53e1051a39Sopenharmony_ci    {
54e1051a39Sopenharmony_ci        const unsigned char expected[sizeof(out)] = {
55e1051a39Sopenharmony_ci            0x8e, 0x4d, 0x93, 0x25, 0x30, 0xd7, 0x65, 0xa0,
56e1051a39Sopenharmony_ci            0xaa, 0xe9, 0x74, 0xc3, 0x04, 0x73, 0x5e, 0xcc
57e1051a39Sopenharmony_ci        };
58e1051a39Sopenharmony_ci        if (!TEST_mem_eq(out, sizeof(out), expected, sizeof(expected))) {
59e1051a39Sopenharmony_ci            goto err;
60e1051a39Sopenharmony_ci        }
61e1051a39Sopenharmony_ci    }
62e1051a39Sopenharmony_ci    ret = 1;
63e1051a39Sopenharmony_cierr:
64e1051a39Sopenharmony_ci    EVP_PKEY_CTX_free(pctx);
65e1051a39Sopenharmony_ci    return ret;
66e1051a39Sopenharmony_ci}
67e1051a39Sopenharmony_ci
68e1051a39Sopenharmony_cistatic int test_kdf_hkdf(void)
69e1051a39Sopenharmony_ci{
70e1051a39Sopenharmony_ci    int ret = 0;
71e1051a39Sopenharmony_ci    EVP_PKEY_CTX *pctx;
72e1051a39Sopenharmony_ci    unsigned char out[10];
73e1051a39Sopenharmony_ci    size_t outlen = sizeof(out);
74e1051a39Sopenharmony_ci
75e1051a39Sopenharmony_ci    if ((pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL)) == NULL) {
76e1051a39Sopenharmony_ci        TEST_error("EVP_PKEY_HKDF");
77e1051a39Sopenharmony_ci        goto err;
78e1051a39Sopenharmony_ci    }
79e1051a39Sopenharmony_ci    if (EVP_PKEY_derive_init(pctx) <= 0) {
80e1051a39Sopenharmony_ci        TEST_error("EVP_PKEY_derive_init");
81e1051a39Sopenharmony_ci        goto err;
82e1051a39Sopenharmony_ci    }
83e1051a39Sopenharmony_ci    if (EVP_PKEY_CTX_set_hkdf_md(pctx, EVP_sha256()) <= 0) {
84e1051a39Sopenharmony_ci        TEST_error("EVP_PKEY_CTX_set_hkdf_md");
85e1051a39Sopenharmony_ci        goto err;
86e1051a39Sopenharmony_ci    }
87e1051a39Sopenharmony_ci    if (EVP_PKEY_CTX_set1_hkdf_salt(pctx, (const unsigned char *)"salt", 4)
88e1051a39Sopenharmony_ci            <= 0) {
89e1051a39Sopenharmony_ci        TEST_error("EVP_PKEY_CTX_set1_hkdf_salt");
90e1051a39Sopenharmony_ci        goto err;
91e1051a39Sopenharmony_ci    }
92e1051a39Sopenharmony_ci    if (EVP_PKEY_CTX_set1_hkdf_key(pctx, (const unsigned char *)"secret", 6)
93e1051a39Sopenharmony_ci            <= 0) {
94e1051a39Sopenharmony_ci        TEST_error("EVP_PKEY_CTX_set1_hkdf_key");
95e1051a39Sopenharmony_ci        goto err;
96e1051a39Sopenharmony_ci    }
97e1051a39Sopenharmony_ci    if (EVP_PKEY_CTX_add1_hkdf_info(pctx, (const unsigned char *)"label", 5)
98e1051a39Sopenharmony_ci            <= 0) {
99e1051a39Sopenharmony_ci        TEST_error("EVP_PKEY_CTX_set1_hkdf_info");
100e1051a39Sopenharmony_ci        goto err;
101e1051a39Sopenharmony_ci    }
102e1051a39Sopenharmony_ci    if (EVP_PKEY_derive(pctx, out, &outlen) <= 0) {
103e1051a39Sopenharmony_ci        TEST_error("EVP_PKEY_derive");
104e1051a39Sopenharmony_ci        goto err;
105e1051a39Sopenharmony_ci    }
106e1051a39Sopenharmony_ci
107e1051a39Sopenharmony_ci    {
108e1051a39Sopenharmony_ci        const unsigned char expected[sizeof(out)] = {
109e1051a39Sopenharmony_ci            0x2a, 0xc4, 0x36, 0x9f, 0x52, 0x59, 0x96, 0xf8, 0xde, 0x13
110e1051a39Sopenharmony_ci        };
111e1051a39Sopenharmony_ci        if (!TEST_mem_eq(out, sizeof(out), expected, sizeof(expected))) {
112e1051a39Sopenharmony_ci            goto err;
113e1051a39Sopenharmony_ci        }
114e1051a39Sopenharmony_ci    }
115e1051a39Sopenharmony_ci    ret = 1;
116e1051a39Sopenharmony_cierr:
117e1051a39Sopenharmony_ci    EVP_PKEY_CTX_free(pctx);
118e1051a39Sopenharmony_ci    return ret;
119e1051a39Sopenharmony_ci}
120e1051a39Sopenharmony_ci
121e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_SCRYPT
122e1051a39Sopenharmony_cistatic int test_kdf_scrypt(void)
123e1051a39Sopenharmony_ci{
124e1051a39Sopenharmony_ci    int ret = 0;
125e1051a39Sopenharmony_ci    EVP_PKEY_CTX *pctx;
126e1051a39Sopenharmony_ci    unsigned char out[64];
127e1051a39Sopenharmony_ci    size_t outlen = sizeof(out);
128e1051a39Sopenharmony_ci
129e1051a39Sopenharmony_ci    if ((pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_SCRYPT, NULL)) == NULL) {
130e1051a39Sopenharmony_ci        TEST_error("EVP_PKEY_SCRYPT");
131e1051a39Sopenharmony_ci        goto err;
132e1051a39Sopenharmony_ci    }
133e1051a39Sopenharmony_ci    if (EVP_PKEY_derive_init(pctx) <= 0) {
134e1051a39Sopenharmony_ci        TEST_error("EVP_PKEY_derive_init");
135e1051a39Sopenharmony_ci        goto err;
136e1051a39Sopenharmony_ci    }
137e1051a39Sopenharmony_ci    if (EVP_PKEY_CTX_set1_pbe_pass(pctx, "password", 8) <= 0) {
138e1051a39Sopenharmony_ci        TEST_error("EVP_PKEY_CTX_set1_pbe_pass");
139e1051a39Sopenharmony_ci        goto err;
140e1051a39Sopenharmony_ci    }
141e1051a39Sopenharmony_ci    if (EVP_PKEY_CTX_set1_scrypt_salt(pctx, (unsigned char *)"NaCl", 4) <= 0) {
142e1051a39Sopenharmony_ci        TEST_error("EVP_PKEY_CTX_set1_scrypt_salt");
143e1051a39Sopenharmony_ci        goto err;
144e1051a39Sopenharmony_ci    }
145e1051a39Sopenharmony_ci    if (EVP_PKEY_CTX_set_scrypt_N(pctx, 1024) <= 0) {
146e1051a39Sopenharmony_ci        TEST_error("EVP_PKEY_CTX_set_scrypt_N");
147e1051a39Sopenharmony_ci        goto err;
148e1051a39Sopenharmony_ci    }
149e1051a39Sopenharmony_ci    if (EVP_PKEY_CTX_set_scrypt_r(pctx, 8) <= 0) {
150e1051a39Sopenharmony_ci        TEST_error("EVP_PKEY_CTX_set_scrypt_r");
151e1051a39Sopenharmony_ci        goto err;
152e1051a39Sopenharmony_ci    }
153e1051a39Sopenharmony_ci    if (EVP_PKEY_CTX_set_scrypt_p(pctx, 16) <= 0) {
154e1051a39Sopenharmony_ci        TEST_error("EVP_PKEY_CTX_set_scrypt_p");
155e1051a39Sopenharmony_ci        goto err;
156e1051a39Sopenharmony_ci    }
157e1051a39Sopenharmony_ci    if (EVP_PKEY_CTX_set_scrypt_maxmem_bytes(pctx, 16) <= 0) {
158e1051a39Sopenharmony_ci        TEST_error("EVP_PKEY_CTX_set_maxmem_bytes");
159e1051a39Sopenharmony_ci        goto err;
160e1051a39Sopenharmony_ci    }
161e1051a39Sopenharmony_ci    if (EVP_PKEY_derive(pctx, out, &outlen) > 0) {
162e1051a39Sopenharmony_ci        TEST_error("EVP_PKEY_derive should have failed");
163e1051a39Sopenharmony_ci        goto err;
164e1051a39Sopenharmony_ci    }
165e1051a39Sopenharmony_ci    if (EVP_PKEY_CTX_set_scrypt_maxmem_bytes(pctx, 10 * 1024 * 1024) <= 0) {
166e1051a39Sopenharmony_ci        TEST_error("EVP_PKEY_CTX_set_maxmem_bytes");
167e1051a39Sopenharmony_ci        goto err;
168e1051a39Sopenharmony_ci    }
169e1051a39Sopenharmony_ci    if (EVP_PKEY_derive(pctx, out, &outlen) <= 0) {
170e1051a39Sopenharmony_ci        TEST_error("EVP_PKEY_derive");
171e1051a39Sopenharmony_ci        goto err;
172e1051a39Sopenharmony_ci    }
173e1051a39Sopenharmony_ci
174e1051a39Sopenharmony_ci    {
175e1051a39Sopenharmony_ci        const unsigned char expected[sizeof(out)] = {
176e1051a39Sopenharmony_ci            0xfd, 0xba, 0xbe, 0x1c, 0x9d, 0x34, 0x72, 0x00,
177e1051a39Sopenharmony_ci            0x78, 0x56, 0xe7, 0x19, 0x0d, 0x01, 0xe9, 0xfe,
178e1051a39Sopenharmony_ci            0x7c, 0x6a, 0xd7, 0xcb, 0xc8, 0x23, 0x78, 0x30,
179e1051a39Sopenharmony_ci            0xe7, 0x73, 0x76, 0x63, 0x4b, 0x37, 0x31, 0x62,
180e1051a39Sopenharmony_ci            0x2e, 0xaf, 0x30, 0xd9, 0x2e, 0x22, 0xa3, 0x88,
181e1051a39Sopenharmony_ci            0x6f, 0xf1, 0x09, 0x27, 0x9d, 0x98, 0x30, 0xda,
182e1051a39Sopenharmony_ci            0xc7, 0x27, 0xaf, 0xb9, 0x4a, 0x83, 0xee, 0x6d,
183e1051a39Sopenharmony_ci            0x83, 0x60, 0xcb, 0xdf, 0xa2, 0xcc, 0x06, 0x40
184e1051a39Sopenharmony_ci        };
185e1051a39Sopenharmony_ci        if (!TEST_mem_eq(out, sizeof(out), expected, sizeof(expected))) {
186e1051a39Sopenharmony_ci            goto err;
187e1051a39Sopenharmony_ci        }
188e1051a39Sopenharmony_ci    }
189e1051a39Sopenharmony_ci    ret = 1;
190e1051a39Sopenharmony_cierr:
191e1051a39Sopenharmony_ci    EVP_PKEY_CTX_free(pctx);
192e1051a39Sopenharmony_ci    return ret;
193e1051a39Sopenharmony_ci}
194e1051a39Sopenharmony_ci#endif
195e1051a39Sopenharmony_ci
196e1051a39Sopenharmony_ciint setup_tests(void)
197e1051a39Sopenharmony_ci{
198e1051a39Sopenharmony_ci    ADD_TEST(test_kdf_tls1_prf);
199e1051a39Sopenharmony_ci    ADD_TEST(test_kdf_hkdf);
200e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_SCRYPT
201e1051a39Sopenharmony_ci    ADD_TEST(test_kdf_scrypt);
202e1051a39Sopenharmony_ci#endif
203e1051a39Sopenharmony_ci    return 1;
204e1051a39Sopenharmony_ci}
205