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