Fix some typo and comments
[openssl.git] / test / pkey_meth_kdf_test.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 /* 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
19 static int test_kdf_hkdf(void)
20 {
21     EVP_PKEY_CTX *pctx;
22     unsigned char out[10];
23     size_t outlen = sizeof(out);
24     pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
25
26     if (EVP_PKEY_derive_init(pctx) <= 0) {
27         TEST_error("EVP_PKEY_derive_init");
28         return 0;
29     }
30     if (EVP_PKEY_CTX_set_hkdf_md(pctx, EVP_sha256()) <= 0) {
31         TEST_error("EVP_PKEY_CTX_set_hkdf_md");
32         return 0;
33     }
34     if (EVP_PKEY_CTX_set1_hkdf_salt(pctx, "salt", 4) <= 0) {
35         TEST_error("EVP_PKEY_CTX_set1_hkdf_salt");
36         return 0;
37     }
38     if (EVP_PKEY_CTX_set1_hkdf_key(pctx, "secret", 6) <= 0) {
39         TEST_error("EVP_PKEY_CTX_set1_hkdf_key");
40         return 0;
41     }
42     if (EVP_PKEY_CTX_add1_hkdf_info(pctx, "label", 5) <= 0) {
43         TEST_error("EVP_PKEY_CTX_set1_hkdf_info");
44         return 0;
45     }
46     if (EVP_PKEY_derive(pctx, out, &outlen) <= 0) {
47         TEST_error("EVP_PKEY_derive");
48         return 0;
49     }
50
51     {
52         const unsigned char expected[sizeof(out)] = {
53             0x2a, 0xc4, 0x36, 0x9f, 0x52, 0x59, 0x96, 0xf8, 0xde, 0x13
54         };
55         if (!TEST_mem_eq(out, sizeof(out), expected, sizeof(expected))) {
56             return 0;
57         }
58     }
59     EVP_PKEY_CTX_free(pctx);
60     return 1;
61 }
62
63 #ifndef OPENSSL_NO_SCRYPT
64 static int test_kdf_scrypt(void)
65 {
66     EVP_PKEY_CTX *pctx;
67     unsigned char out[64];
68     size_t outlen = sizeof(out);
69     pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_SCRYPT, NULL);
70
71     if (EVP_PKEY_derive_init(pctx) <= 0) {
72         TEST_error("EVP_PKEY_derive_init");
73         return 0;
74     }
75     if (EVP_PKEY_CTX_set1_pbe_pass(pctx, "password", 8) <= 0) {
76         TEST_error("EVP_PKEY_CTX_set1_pbe_pass");
77         return 0;
78     }
79     if (EVP_PKEY_CTX_set1_scrypt_salt(pctx, "NaCl", 4) <= 0) {
80         TEST_error("EVP_PKEY_CTX_set1_scrypt_salt");
81         return 0;
82     }
83     if (EVP_PKEY_CTX_set_scrypt_N(pctx, 1024) <= 0) {
84         TEST_error("EVP_PKEY_CTX_set_scrypt_N");
85         return 0;
86     }
87     if (EVP_PKEY_CTX_set_scrypt_r(pctx, 8) <= 0) {
88         TEST_error("EVP_PKEY_CTX_set_scrypt_r");
89         return 0;
90     }
91     if (EVP_PKEY_CTX_set_scrypt_p(pctx, 16) <= 0) {
92         TEST_error("EVP_PKEY_CTX_set_scrypt_p");
93         return 0;
94     }
95     if (EVP_PKEY_CTX_set_scrypt_maxmem_bytes(pctx, 16) <= 0) {
96         TEST_error("EVP_PKEY_CTX_set_maxmem_bytes");
97         return 0;
98     }
99     if (EVP_PKEY_derive(pctx, out, &outlen) > 0) {
100         TEST_error("EVP_PKEY_derive should have failed");
101         return 0;
102     }
103     if (EVP_PKEY_CTX_set_scrypt_maxmem_bytes(pctx, 10 * 1024 * 1024) <= 0) {
104         TEST_error("EVP_PKEY_CTX_set_maxmem_bytes");
105         return 0;
106     }
107     if (EVP_PKEY_derive(pctx, out, &outlen) <= 0) {
108         TEST_error("EVP_PKEY_derive");
109         return 0;
110     }
111
112     {
113         const unsigned char expected[sizeof(out)] = {
114             0xfd, 0xba, 0xbe, 0x1c, 0x9d, 0x34, 0x72, 0x00,
115             0x78, 0x56, 0xe7, 0x19, 0x0d, 0x01, 0xe9, 0xfe,
116             0x7c, 0x6a, 0xd7, 0xcb, 0xc8, 0x23, 0x78, 0x30,
117             0xe7, 0x73, 0x76, 0x63, 0x4b, 0x37, 0x31, 0x62,
118             0x2e, 0xaf, 0x30, 0xd9, 0x2e, 0x22, 0xa3, 0x88,
119             0x6f, 0xf1, 0x09, 0x27, 0x9d, 0x98, 0x30, 0xda,
120             0xc7, 0x27, 0xaf, 0xb9, 0x4a, 0x83, 0xee, 0x6d,
121             0x83, 0x60, 0xcb, 0xdf, 0xa2, 0xcc, 0x06, 0x40
122         };
123         if (!TEST_mem_eq(out, sizeof(out), expected, sizeof(expected))) {
124             return 0;
125         }
126     }
127     EVP_PKEY_CTX_free(pctx);
128     return 1;
129 }
130 #endif
131
132 int setup_tests()
133 {
134     ADD_TEST(test_kdf_hkdf);
135 #ifndef OPENSSL_NO_SCRYPT
136     ADD_TEST(test_kdf_scrypt);
137 #endif
138     return 1;
139 }