]>
Commit | Line | Data |
---|---|---|
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 |
19 | static 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 JB |
37 | } |
38 | if (EVP_PKEY_CTX_set1_tls1_prf_secret(pctx, "secret", 6) <= 0) { | |
39 | TEST_error("EVP_PKEY_CTX_set1_tls1_prf_secret"); | |
5a285add | 40 | goto err; |
b73b4d73 JB |
41 | } |
42 | if (EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, "seed", 4) <= 0) { | |
43 | TEST_error("EVP_PKEY_CTX_add1_tls1_prf_seed"); | |
5a285add | 44 | goto err; |
b73b4d73 JB |
45 | } |
46 | if (EVP_PKEY_derive(pctx, out, &outlen) <= 0) { | |
47 | TEST_error("EVP_PKEY_derive"); | |
5a285add | 48 | goto err; |
b73b4d73 JB |
49 | } |
50 | ||
51 | { | |
52 | const unsigned char expected[sizeof(out)] = { | |
53 | 0x8e, 0x4d, 0x93, 0x25, 0x30, 0xd7, 0x65, 0xa0, | |
54 | 0xaa, 0xe9, 0x74, 0xc3, 0x04, 0x73, 0x5e, 0xcc | |
55 | }; | |
56 | if (!TEST_mem_eq(out, sizeof(out), expected, sizeof(expected))) { | |
5a285add | 57 | goto err; |
b73b4d73 JB |
58 | } |
59 | } | |
5a285add DM |
60 | ret = 1; |
61 | err: | |
b73b4d73 | 62 | EVP_PKEY_CTX_free(pctx); |
5a285add | 63 | return ret; |
b73b4d73 JB |
64 | } |
65 | ||
6aa907a6 JB |
66 | static int test_kdf_hkdf(void) |
67 | { | |
5a285add | 68 | int ret = 0; |
6aa907a6 JB |
69 | EVP_PKEY_CTX *pctx; |
70 | unsigned char out[10]; | |
71 | size_t outlen = sizeof(out); | |
6aa907a6 | 72 | |
5a285add DM |
73 | if ((pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL)) == NULL) { |
74 | TEST_error("EVP_PKEY_HKDF"); | |
75 | goto err; | |
76 | } | |
6aa907a6 JB |
77 | if (EVP_PKEY_derive_init(pctx) <= 0) { |
78 | TEST_error("EVP_PKEY_derive_init"); | |
5a285add | 79 | goto err; |
6aa907a6 JB |
80 | } |
81 | if (EVP_PKEY_CTX_set_hkdf_md(pctx, EVP_sha256()) <= 0) { | |
82 | TEST_error("EVP_PKEY_CTX_set_hkdf_md"); | |
5a285add | 83 | goto err; |
6aa907a6 JB |
84 | } |
85 | if (EVP_PKEY_CTX_set1_hkdf_salt(pctx, "salt", 4) <= 0) { | |
86 | TEST_error("EVP_PKEY_CTX_set1_hkdf_salt"); | |
5a285add | 87 | goto err; |
6aa907a6 JB |
88 | } |
89 | if (EVP_PKEY_CTX_set1_hkdf_key(pctx, "secret", 6) <= 0) { | |
90 | TEST_error("EVP_PKEY_CTX_set1_hkdf_key"); | |
5a285add | 91 | goto err; |
6aa907a6 JB |
92 | } |
93 | if (EVP_PKEY_CTX_add1_hkdf_info(pctx, "label", 5) <= 0) { | |
94 | TEST_error("EVP_PKEY_CTX_set1_hkdf_info"); | |
5a285add | 95 | goto err; |
6aa907a6 JB |
96 | } |
97 | if (EVP_PKEY_derive(pctx, out, &outlen) <= 0) { | |
98 | TEST_error("EVP_PKEY_derive"); | |
5a285add | 99 | goto err; |
6aa907a6 JB |
100 | } |
101 | ||
102 | { | |
103 | const unsigned char expected[sizeof(out)] = { | |
104 | 0x2a, 0xc4, 0x36, 0x9f, 0x52, 0x59, 0x96, 0xf8, 0xde, 0x13 | |
105 | }; | |
106 | if (!TEST_mem_eq(out, sizeof(out), expected, sizeof(expected))) { | |
5a285add | 107 | goto err; |
6aa907a6 JB |
108 | } |
109 | } | |
5a285add DM |
110 | ret = 1; |
111 | err: | |
6aa907a6 | 112 | EVP_PKEY_CTX_free(pctx); |
5a285add | 113 | return ret; |
6aa907a6 JB |
114 | } |
115 | ||
402f26e6 | 116 | #ifndef OPENSSL_NO_SCRYPT |
6aa907a6 JB |
117 | static int test_kdf_scrypt(void) |
118 | { | |
5a285add | 119 | int ret = 0; |
6aa907a6 JB |
120 | EVP_PKEY_CTX *pctx; |
121 | unsigned char out[64]; | |
122 | size_t outlen = sizeof(out); | |
6aa907a6 | 123 | |
5a285add DM |
124 | if ((pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_SCRYPT, NULL)) == NULL) { |
125 | TEST_error("EVP_PKEY_SCRYPT"); | |
126 | goto err; | |
127 | } | |
6aa907a6 JB |
128 | if (EVP_PKEY_derive_init(pctx) <= 0) { |
129 | TEST_error("EVP_PKEY_derive_init"); | |
5a285add | 130 | goto err; |
6aa907a6 JB |
131 | } |
132 | if (EVP_PKEY_CTX_set1_pbe_pass(pctx, "password", 8) <= 0) { | |
133 | TEST_error("EVP_PKEY_CTX_set1_pbe_pass"); | |
5a285add | 134 | goto err; |
6aa907a6 JB |
135 | } |
136 | if (EVP_PKEY_CTX_set1_scrypt_salt(pctx, "NaCl", 4) <= 0) { | |
137 | TEST_error("EVP_PKEY_CTX_set1_scrypt_salt"); | |
5a285add | 138 | goto err; |
6aa907a6 JB |
139 | } |
140 | if (EVP_PKEY_CTX_set_scrypt_N(pctx, 1024) <= 0) { | |
141 | TEST_error("EVP_PKEY_CTX_set_scrypt_N"); | |
5a285add | 142 | goto err; |
6aa907a6 JB |
143 | } |
144 | if (EVP_PKEY_CTX_set_scrypt_r(pctx, 8) <= 0) { | |
145 | TEST_error("EVP_PKEY_CTX_set_scrypt_r"); | |
5a285add | 146 | goto err; |
6aa907a6 JB |
147 | } |
148 | if (EVP_PKEY_CTX_set_scrypt_p(pctx, 16) <= 0) { | |
149 | TEST_error("EVP_PKEY_CTX_set_scrypt_p"); | |
5a285add | 150 | goto err; |
6aa907a6 JB |
151 | } |
152 | if (EVP_PKEY_CTX_set_scrypt_maxmem_bytes(pctx, 16) <= 0) { | |
153 | TEST_error("EVP_PKEY_CTX_set_maxmem_bytes"); | |
5a285add | 154 | goto err; |
6aa907a6 JB |
155 | } |
156 | if (EVP_PKEY_derive(pctx, out, &outlen) > 0) { | |
157 | TEST_error("EVP_PKEY_derive should have failed"); | |
5a285add | 158 | goto err; |
6aa907a6 JB |
159 | } |
160 | if (EVP_PKEY_CTX_set_scrypt_maxmem_bytes(pctx, 10 * 1024 * 1024) <= 0) { | |
161 | TEST_error("EVP_PKEY_CTX_set_maxmem_bytes"); | |
5a285add | 162 | goto err; |
6aa907a6 JB |
163 | } |
164 | if (EVP_PKEY_derive(pctx, out, &outlen) <= 0) { | |
165 | TEST_error("EVP_PKEY_derive"); | |
5a285add | 166 | goto err; |
6aa907a6 JB |
167 | } |
168 | ||
169 | { | |
170 | const unsigned char expected[sizeof(out)] = { | |
171 | 0xfd, 0xba, 0xbe, 0x1c, 0x9d, 0x34, 0x72, 0x00, | |
172 | 0x78, 0x56, 0xe7, 0x19, 0x0d, 0x01, 0xe9, 0xfe, | |
173 | 0x7c, 0x6a, 0xd7, 0xcb, 0xc8, 0x23, 0x78, 0x30, | |
174 | 0xe7, 0x73, 0x76, 0x63, 0x4b, 0x37, 0x31, 0x62, | |
175 | 0x2e, 0xaf, 0x30, 0xd9, 0x2e, 0x22, 0xa3, 0x88, | |
176 | 0x6f, 0xf1, 0x09, 0x27, 0x9d, 0x98, 0x30, 0xda, | |
177 | 0xc7, 0x27, 0xaf, 0xb9, 0x4a, 0x83, 0xee, 0x6d, | |
178 | 0x83, 0x60, 0xcb, 0xdf, 0xa2, 0xcc, 0x06, 0x40 | |
179 | }; | |
180 | if (!TEST_mem_eq(out, sizeof(out), expected, sizeof(expected))) { | |
5a285add | 181 | goto err; |
6aa907a6 JB |
182 | } |
183 | } | |
5a285add DM |
184 | ret = 1; |
185 | err: | |
6aa907a6 | 186 | EVP_PKEY_CTX_free(pctx); |
5a285add | 187 | return ret; |
6aa907a6 | 188 | } |
402f26e6 | 189 | #endif |
6aa907a6 | 190 | |
3cb7c5cf | 191 | int setup_tests(void) |
6aa907a6 | 192 | { |
b73b4d73 | 193 | ADD_TEST(test_kdf_tls1_prf); |
6aa907a6 | 194 | ADD_TEST(test_kdf_hkdf); |
402f26e6 | 195 | #ifndef OPENSSL_NO_SCRYPT |
6aa907a6 | 196 | ADD_TEST(test_kdf_scrypt); |
402f26e6 | 197 | #endif |
6aa907a6 JB |
198 | return 1; |
199 | } |