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