1*e7be843bSPierre Pronchery /*
2*e7be843bSPierre Pronchery * Copyright 2023 The OpenSSL Project Authors. All Rights Reserved.
3*e7be843bSPierre Pronchery *
4*e7be843bSPierre Pronchery * Licensed under the Apache License 2.0 (the "License"). You may not use
5*e7be843bSPierre Pronchery * this file except in compliance with the License. You can obtain a copy
6*e7be843bSPierre Pronchery * in the file LICENSE in the source distribution or at
7*e7be843bSPierre Pronchery * https://www.openssl.org/source/license.html
8*e7be843bSPierre Pronchery */
9*e7be843bSPierre Pronchery
10*e7be843bSPierre Pronchery #include <stdio.h>
11*e7be843bSPierre Pronchery #include <openssl/core_names.h>
12*e7be843bSPierre Pronchery #include <openssl/crypto.h>
13*e7be843bSPierre Pronchery #include <openssl/kdf.h>
14*e7be843bSPierre Pronchery #include <openssl/params.h>
15*e7be843bSPierre Pronchery #include <openssl/thread.h>
16*e7be843bSPierre Pronchery
17*e7be843bSPierre Pronchery /*
18*e7be843bSPierre Pronchery * Example showing how to use Argon2 KDF.
19*e7be843bSPierre Pronchery * See man EVP_KDF-ARGON2 for more information.
20*e7be843bSPierre Pronchery *
21*e7be843bSPierre Pronchery * test vector from
22*e7be843bSPierre Pronchery * https://datatracker.ietf.org/doc/html/rfc9106
23*e7be843bSPierre Pronchery */
24*e7be843bSPierre Pronchery
25*e7be843bSPierre Pronchery /*
26*e7be843bSPierre Pronchery * Hard coding a password into an application is very bad.
27*e7be843bSPierre Pronchery * It is done here solely for educational purposes.
28*e7be843bSPierre Pronchery */
29*e7be843bSPierre Pronchery static unsigned char password[] = {
30*e7be843bSPierre Pronchery 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
31*e7be843bSPierre Pronchery 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
32*e7be843bSPierre Pronchery 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
33*e7be843bSPierre Pronchery 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01
34*e7be843bSPierre Pronchery };
35*e7be843bSPierre Pronchery
36*e7be843bSPierre Pronchery /*
37*e7be843bSPierre Pronchery * The salt is better not being hard coded too. Each password should have a
38*e7be843bSPierre Pronchery * different salt if possible. The salt is not considered secret information
39*e7be843bSPierre Pronchery * and is safe to store with an encrypted password.
40*e7be843bSPierre Pronchery */
41*e7be843bSPierre Pronchery static unsigned char salt[] = {
42*e7be843bSPierre Pronchery 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
43*e7be843bSPierre Pronchery 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02
44*e7be843bSPierre Pronchery };
45*e7be843bSPierre Pronchery
46*e7be843bSPierre Pronchery /*
47*e7be843bSPierre Pronchery * Optional secret for KDF
48*e7be843bSPierre Pronchery */
49*e7be843bSPierre Pronchery static unsigned char secret[] = {
50*e7be843bSPierre Pronchery 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
51*e7be843bSPierre Pronchery };
52*e7be843bSPierre Pronchery
53*e7be843bSPierre Pronchery /*
54*e7be843bSPierre Pronchery * Optional additional data
55*e7be843bSPierre Pronchery */
56*e7be843bSPierre Pronchery static unsigned char ad[] = {
57*e7be843bSPierre Pronchery 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
58*e7be843bSPierre Pronchery 0x04, 0x04, 0x04, 0x04
59*e7be843bSPierre Pronchery };
60*e7be843bSPierre Pronchery
61*e7be843bSPierre Pronchery /*
62*e7be843bSPierre Pronchery * Argon2 cost parameters
63*e7be843bSPierre Pronchery */
64*e7be843bSPierre Pronchery static uint32_t memory_cost = 32;
65*e7be843bSPierre Pronchery static uint32_t iteration_cost = 3;
66*e7be843bSPierre Pronchery static uint32_t parallel_cost = 4;
67*e7be843bSPierre Pronchery
68*e7be843bSPierre Pronchery static const unsigned char expected_output[] = {
69*e7be843bSPierre Pronchery 0x0d, 0x64, 0x0d, 0xf5, 0x8d, 0x78, 0x76, 0x6c,
70*e7be843bSPierre Pronchery 0x08, 0xc0, 0x37, 0xa3, 0x4a, 0x8b, 0x53, 0xc9,
71*e7be843bSPierre Pronchery 0xd0, 0x1e, 0xf0, 0x45, 0x2d, 0x75, 0xb6, 0x5e,
72*e7be843bSPierre Pronchery 0xb5, 0x25, 0x20, 0xe9, 0x6b, 0x01, 0xe6, 0x59
73*e7be843bSPierre Pronchery };
74*e7be843bSPierre Pronchery
main(int argc,char ** argv)75*e7be843bSPierre Pronchery int main(int argc, char **argv)
76*e7be843bSPierre Pronchery {
77*e7be843bSPierre Pronchery int rv = EXIT_FAILURE;
78*e7be843bSPierre Pronchery EVP_KDF *kdf = NULL;
79*e7be843bSPierre Pronchery EVP_KDF_CTX *kctx = NULL;
80*e7be843bSPierre Pronchery unsigned char out[32];
81*e7be843bSPierre Pronchery OSSL_PARAM params[9], *p = params;
82*e7be843bSPierre Pronchery OSSL_LIB_CTX *library_context = NULL;
83*e7be843bSPierre Pronchery unsigned int threads;
84*e7be843bSPierre Pronchery
85*e7be843bSPierre Pronchery library_context = OSSL_LIB_CTX_new();
86*e7be843bSPierre Pronchery if (library_context == NULL) {
87*e7be843bSPierre Pronchery fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");
88*e7be843bSPierre Pronchery goto end;
89*e7be843bSPierre Pronchery }
90*e7be843bSPierre Pronchery
91*e7be843bSPierre Pronchery /* Fetch the key derivation function implementation */
92*e7be843bSPierre Pronchery kdf = EVP_KDF_fetch(library_context, "argon2id", NULL);
93*e7be843bSPierre Pronchery if (kdf == NULL) {
94*e7be843bSPierre Pronchery fprintf(stderr, "EVP_KDF_fetch() returned NULL\n");
95*e7be843bSPierre Pronchery goto end;
96*e7be843bSPierre Pronchery }
97*e7be843bSPierre Pronchery
98*e7be843bSPierre Pronchery /* Create a context for the key derivation operation */
99*e7be843bSPierre Pronchery kctx = EVP_KDF_CTX_new(kdf);
100*e7be843bSPierre Pronchery if (kctx == NULL) {
101*e7be843bSPierre Pronchery fprintf(stderr, "EVP_KDF_CTX_new() returned NULL\n");
102*e7be843bSPierre Pronchery goto end;
103*e7be843bSPierre Pronchery }
104*e7be843bSPierre Pronchery
105*e7be843bSPierre Pronchery /*
106*e7be843bSPierre Pronchery * Thread support can be turned off; use serialization if we cannot
107*e7be843bSPierre Pronchery * set requested number of threads.
108*e7be843bSPierre Pronchery */
109*e7be843bSPierre Pronchery threads = parallel_cost;
110*e7be843bSPierre Pronchery if (OSSL_set_max_threads(library_context, parallel_cost) != 1) {
111*e7be843bSPierre Pronchery uint64_t max_threads = OSSL_get_max_threads(library_context);
112*e7be843bSPierre Pronchery
113*e7be843bSPierre Pronchery if (max_threads == 0)
114*e7be843bSPierre Pronchery threads = 1;
115*e7be843bSPierre Pronchery else if (max_threads < parallel_cost)
116*e7be843bSPierre Pronchery threads = max_threads;
117*e7be843bSPierre Pronchery }
118*e7be843bSPierre Pronchery
119*e7be843bSPierre Pronchery /* Set password */
120*e7be843bSPierre Pronchery *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD, password, sizeof(password));
121*e7be843bSPierre Pronchery /* Set salt */
122*e7be843bSPierre Pronchery *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT, salt, sizeof(salt));
123*e7be843bSPierre Pronchery /* Set optional additional data */
124*e7be843bSPierre Pronchery *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_ARGON2_AD, ad, sizeof(ad));
125*e7be843bSPierre Pronchery /* Set optional secret */
126*e7be843bSPierre Pronchery *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SECRET, secret, sizeof(secret));
127*e7be843bSPierre Pronchery /* Set iteration count */
128*e7be843bSPierre Pronchery *p++ = OSSL_PARAM_construct_uint32(OSSL_KDF_PARAM_ITER, &iteration_cost);
129*e7be843bSPierre Pronchery /* Set threads performing derivation (can be decreased) */
130*e7be843bSPierre Pronchery *p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_THREADS, &threads);
131*e7be843bSPierre Pronchery /* Set parallel cost */
132*e7be843bSPierre Pronchery *p++ = OSSL_PARAM_construct_uint32(OSSL_KDF_PARAM_ARGON2_LANES, ¶llel_cost);
133*e7be843bSPierre Pronchery /* Set memory requirement */
134*e7be843bSPierre Pronchery *p++ = OSSL_PARAM_construct_uint32(OSSL_KDF_PARAM_ARGON2_MEMCOST, &memory_cost);
135*e7be843bSPierre Pronchery *p = OSSL_PARAM_construct_end();
136*e7be843bSPierre Pronchery
137*e7be843bSPierre Pronchery /* Derive the key */
138*e7be843bSPierre Pronchery if (EVP_KDF_derive(kctx, out, sizeof(out), params) != 1) {
139*e7be843bSPierre Pronchery fprintf(stderr, "EVP_KDF_derive() failed\n");
140*e7be843bSPierre Pronchery goto end;
141*e7be843bSPierre Pronchery }
142*e7be843bSPierre Pronchery
143*e7be843bSPierre Pronchery if (CRYPTO_memcmp(expected_output, out, sizeof(expected_output)) != 0) {
144*e7be843bSPierre Pronchery fprintf(stderr, "Generated key does not match expected value\n");
145*e7be843bSPierre Pronchery goto end;
146*e7be843bSPierre Pronchery }
147*e7be843bSPierre Pronchery
148*e7be843bSPierre Pronchery printf("Success\n");
149*e7be843bSPierre Pronchery
150*e7be843bSPierre Pronchery rv = EXIT_SUCCESS;
151*e7be843bSPierre Pronchery end:
152*e7be843bSPierre Pronchery EVP_KDF_CTX_free(kctx);
153*e7be843bSPierre Pronchery EVP_KDF_free(kdf);
154*e7be843bSPierre Pronchery OSSL_LIB_CTX_free(library_context);
155*e7be843bSPierre Pronchery return rv;
156*e7be843bSPierre Pronchery }
157