xref: /freebsd/crypto/openssl/demos/kdf/pbkdf2.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert  * Copyright 2021 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert  *
4*e0c4386eSCy Schubert  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*e0c4386eSCy Schubert  * this file except in compliance with the License.  You can obtain a copy
6*e0c4386eSCy Schubert  * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert  * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert  */
9*e0c4386eSCy Schubert 
10*e0c4386eSCy Schubert #include <stdio.h>
11*e0c4386eSCy Schubert #include <openssl/core_names.h>
12*e0c4386eSCy Schubert #include <openssl/crypto.h>
13*e0c4386eSCy Schubert #include <openssl/kdf.h>
14*e0c4386eSCy Schubert #include <openssl/obj_mac.h>
15*e0c4386eSCy Schubert #include <openssl/params.h>
16*e0c4386eSCy Schubert 
17*e0c4386eSCy Schubert /*
18*e0c4386eSCy Schubert  * test vector from
19*e0c4386eSCy Schubert  * https://datatracker.ietf.org/doc/html/rfc7914
20*e0c4386eSCy Schubert  */
21*e0c4386eSCy Schubert 
22*e0c4386eSCy Schubert /*
23*e0c4386eSCy Schubert  * Hard coding a password into an application is very bad.
24*e0c4386eSCy Schubert  * It is done here solely for educational purposes.
25*e0c4386eSCy Schubert  */
26*e0c4386eSCy Schubert static unsigned char password[] = {
27*e0c4386eSCy Schubert     'P', 'a', 's', 's', 'w', 'o', 'r', 'd'
28*e0c4386eSCy Schubert };
29*e0c4386eSCy Schubert 
30*e0c4386eSCy Schubert /*
31*e0c4386eSCy Schubert  * The salt is better not being hard coded too.  Each password should have a
32*e0c4386eSCy Schubert  * different salt if possible.  The salt is not considered secret information
33*e0c4386eSCy Schubert  * and is safe to store with an encrypted password.
34*e0c4386eSCy Schubert  */
35*e0c4386eSCy Schubert static unsigned char pbkdf2_salt[] = {
36*e0c4386eSCy Schubert     'N', 'a', 'C', 'l'
37*e0c4386eSCy Schubert };
38*e0c4386eSCy Schubert 
39*e0c4386eSCy Schubert /*
40*e0c4386eSCy Schubert  * The iteration parameter can be variable or hard coded.  The disadvantage with
41*e0c4386eSCy Schubert  * hard coding them is that they cannot easily be adjusted for future
42*e0c4386eSCy Schubert  * technological improvements appear.
43*e0c4386eSCy Schubert  */
44*e0c4386eSCy Schubert static unsigned int pbkdf2_iterations = 80000;
45*e0c4386eSCy Schubert 
46*e0c4386eSCy Schubert static const unsigned char expected_output[] = {
47*e0c4386eSCy Schubert 
48*e0c4386eSCy Schubert     0x4d, 0xdc, 0xd8, 0xf6, 0x0b, 0x98, 0xbe, 0x21,
49*e0c4386eSCy Schubert     0x83, 0x0c, 0xee, 0x5e, 0xf2, 0x27, 0x01, 0xf9,
50*e0c4386eSCy Schubert     0x64, 0x1a, 0x44, 0x18, 0xd0, 0x4c, 0x04, 0x14,
51*e0c4386eSCy Schubert     0xae, 0xff, 0x08, 0x87, 0x6b, 0x34, 0xab, 0x56,
52*e0c4386eSCy Schubert     0xa1, 0xd4, 0x25, 0xa1, 0x22, 0x58, 0x33, 0x54,
53*e0c4386eSCy Schubert     0x9a, 0xdb, 0x84, 0x1b, 0x51, 0xc9, 0xb3, 0x17,
54*e0c4386eSCy Schubert     0x6a, 0x27, 0x2b, 0xde, 0xbb, 0xa1, 0xd0, 0x78,
55*e0c4386eSCy Schubert     0x47, 0x8f, 0x62, 0xb3, 0x97, 0xf3, 0x3c, 0x8d
56*e0c4386eSCy Schubert };
57*e0c4386eSCy Schubert 
main(int argc,char ** argv)58*e0c4386eSCy Schubert int main(int argc, char **argv)
59*e0c4386eSCy Schubert {
60*e0c4386eSCy Schubert     int rv = 1;
61*e0c4386eSCy Schubert     EVP_KDF *kdf = NULL;
62*e0c4386eSCy Schubert     EVP_KDF_CTX *kctx = NULL;
63*e0c4386eSCy Schubert     unsigned char out[64];
64*e0c4386eSCy Schubert     OSSL_PARAM params[5], *p = params;
65*e0c4386eSCy Schubert     OSSL_LIB_CTX *library_context = NULL;
66*e0c4386eSCy Schubert 
67*e0c4386eSCy Schubert     library_context = OSSL_LIB_CTX_new();
68*e0c4386eSCy Schubert     if (library_context == NULL) {
69*e0c4386eSCy Schubert         fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");
70*e0c4386eSCy Schubert         goto end;
71*e0c4386eSCy Schubert     }
72*e0c4386eSCy Schubert 
73*e0c4386eSCy Schubert     /* Fetch the key derivation function implementation */
74*e0c4386eSCy Schubert     kdf = EVP_KDF_fetch(library_context, "PBKDF2", NULL);
75*e0c4386eSCy Schubert     if (kdf == NULL) {
76*e0c4386eSCy Schubert         fprintf(stderr, "EVP_KDF_fetch() returned NULL\n");
77*e0c4386eSCy Schubert         goto end;
78*e0c4386eSCy Schubert     }
79*e0c4386eSCy Schubert 
80*e0c4386eSCy Schubert     /* Create a context for the key derivation operation */
81*e0c4386eSCy Schubert     kctx = EVP_KDF_CTX_new(kdf);
82*e0c4386eSCy Schubert     if (kctx == NULL) {
83*e0c4386eSCy Schubert         fprintf(stderr, "EVP_KDF_CTX_new() returned NULL\n");
84*e0c4386eSCy Schubert         goto end;
85*e0c4386eSCy Schubert     }
86*e0c4386eSCy Schubert 
87*e0c4386eSCy Schubert     /* Set password */
88*e0c4386eSCy Schubert     *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD, password,
89*e0c4386eSCy Schubert                                              sizeof(password));
90*e0c4386eSCy Schubert     /* Set salt */
91*e0c4386eSCy Schubert     *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT, pbkdf2_salt,
92*e0c4386eSCy Schubert                                              sizeof(pbkdf2_salt));
93*e0c4386eSCy Schubert     /* Set iteration count (default 2048) */
94*e0c4386eSCy Schubert     *p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_ITER, &pbkdf2_iterations);
95*e0c4386eSCy Schubert     /* Set the underlying hash function used to derive the key */
96*e0c4386eSCy Schubert     *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
97*e0c4386eSCy Schubert                                             "SHA256", 0);
98*e0c4386eSCy Schubert     *p = OSSL_PARAM_construct_end();
99*e0c4386eSCy Schubert 
100*e0c4386eSCy Schubert     /* Derive the key */
101*e0c4386eSCy Schubert     if (EVP_KDF_derive(kctx, out, sizeof(out), params) != 1) {
102*e0c4386eSCy Schubert         fprintf(stderr, "EVP_KDF_derive() failed\n");
103*e0c4386eSCy Schubert         goto end;
104*e0c4386eSCy Schubert     }
105*e0c4386eSCy Schubert 
106*e0c4386eSCy Schubert     if (CRYPTO_memcmp(expected_output, out, sizeof(expected_output)) != 0) {
107*e0c4386eSCy Schubert         fprintf(stderr, "Generated key does not match expected value\n");
108*e0c4386eSCy Schubert         goto end;
109*e0c4386eSCy Schubert     }
110*e0c4386eSCy Schubert 
111*e0c4386eSCy Schubert     rv = 0;
112*e0c4386eSCy Schubert end:
113*e0c4386eSCy Schubert     EVP_KDF_CTX_free(kctx);
114*e0c4386eSCy Schubert     EVP_KDF_free(kdf);
115*e0c4386eSCy Schubert     OSSL_LIB_CTX_free(library_context);
116*e0c4386eSCy Schubert     return rv;
117*e0c4386eSCy Schubert }
118