xref: /freebsd/crypto/openssl/demos/kdf/hkdf.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/rfc5869
20*e0c4386eSCy Schubert  */
21*e0c4386eSCy Schubert 
22*e0c4386eSCy Schubert static unsigned char hkdf_salt[] = {
23*e0c4386eSCy Schubert     0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
24*e0c4386eSCy Schubert     0x0c
25*e0c4386eSCy Schubert };
26*e0c4386eSCy Schubert 
27*e0c4386eSCy Schubert static unsigned char hkdf_ikm[] = {
28*e0c4386eSCy Schubert     0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
29*e0c4386eSCy Schubert     0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b
30*e0c4386eSCy Schubert };
31*e0c4386eSCy Schubert 
32*e0c4386eSCy Schubert static unsigned char hkdf_info[] = {
33*e0c4386eSCy Schubert     0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9
34*e0c4386eSCy Schubert };
35*e0c4386eSCy Schubert 
36*e0c4386eSCy Schubert /* Expected output keying material */
37*e0c4386eSCy Schubert static unsigned char hkdf_okm[] = {
38*e0c4386eSCy Schubert     0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a, 0x90, 0x43, 0x4f, 0x64,
39*e0c4386eSCy Schubert     0xd0, 0x36, 0x2f, 0x2a, 0x2d, 0x2d, 0x0a, 0x90, 0xcf, 0x1a, 0x5a, 0x4c,
40*e0c4386eSCy Schubert     0x5d, 0xb0, 0x2d, 0x56, 0xec, 0xc4, 0xc5, 0xbf, 0x34, 0x00, 0x72, 0x08,
41*e0c4386eSCy Schubert     0xd5, 0xb8, 0x87, 0x18, 0x58, 0x65
42*e0c4386eSCy Schubert };
43*e0c4386eSCy Schubert 
main(int argc,char ** argv)44*e0c4386eSCy Schubert int main(int argc, char **argv)
45*e0c4386eSCy Schubert {
46*e0c4386eSCy Schubert     int rv = 1;
47*e0c4386eSCy Schubert     EVP_KDF *kdf = NULL;
48*e0c4386eSCy Schubert     EVP_KDF_CTX *kctx = NULL;
49*e0c4386eSCy Schubert     unsigned char out[42];
50*e0c4386eSCy Schubert     OSSL_PARAM params[5], *p = params;
51*e0c4386eSCy Schubert     OSSL_LIB_CTX *library_context = NULL;
52*e0c4386eSCy Schubert 
53*e0c4386eSCy Schubert     library_context = OSSL_LIB_CTX_new();
54*e0c4386eSCy Schubert     if (library_context == NULL) {
55*e0c4386eSCy Schubert         fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");
56*e0c4386eSCy Schubert         goto end;
57*e0c4386eSCy Schubert     }
58*e0c4386eSCy Schubert 
59*e0c4386eSCy Schubert     /* Fetch the key derivation function implementation */
60*e0c4386eSCy Schubert     kdf = EVP_KDF_fetch(library_context, "HKDF", NULL);
61*e0c4386eSCy Schubert     if (kdf == NULL) {
62*e0c4386eSCy Schubert         fprintf(stderr, "EVP_KDF_fetch() returned NULL\n");
63*e0c4386eSCy Schubert         goto end;
64*e0c4386eSCy Schubert     }
65*e0c4386eSCy Schubert 
66*e0c4386eSCy Schubert     /* Create a context for the key derivation operation */
67*e0c4386eSCy Schubert     kctx = EVP_KDF_CTX_new(kdf);
68*e0c4386eSCy Schubert     if (kctx == NULL) {
69*e0c4386eSCy Schubert         fprintf(stderr, "EVP_KDF_CTX_new() returned NULL\n");
70*e0c4386eSCy Schubert         goto end;
71*e0c4386eSCy Schubert     }
72*e0c4386eSCy Schubert 
73*e0c4386eSCy Schubert     /* Set the underlying hash function used to derive the key */
74*e0c4386eSCy Schubert     *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
75*e0c4386eSCy Schubert                                             "SHA256", 0);
76*e0c4386eSCy Schubert     /* Set input keying material */
77*e0c4386eSCy Schubert     *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, hkdf_ikm,
78*e0c4386eSCy Schubert                                              sizeof(hkdf_ikm));
79*e0c4386eSCy Schubert     /* Set application specific information */
80*e0c4386eSCy Schubert     *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO, hkdf_info,
81*e0c4386eSCy Schubert                                              sizeof(hkdf_info));
82*e0c4386eSCy Schubert     /* Set salt */
83*e0c4386eSCy Schubert     *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT, hkdf_salt,
84*e0c4386eSCy Schubert                                              sizeof(hkdf_salt));
85*e0c4386eSCy Schubert     *p = OSSL_PARAM_construct_end();
86*e0c4386eSCy Schubert 
87*e0c4386eSCy Schubert     /* Derive the key */
88*e0c4386eSCy Schubert     if (EVP_KDF_derive(kctx, out, sizeof(out), params) != 1) {
89*e0c4386eSCy Schubert         fprintf(stderr, "EVP_KDF_derive() failed\n");
90*e0c4386eSCy Schubert         goto end;
91*e0c4386eSCy Schubert     }
92*e0c4386eSCy Schubert 
93*e0c4386eSCy Schubert     if (CRYPTO_memcmp(hkdf_okm, out, sizeof(hkdf_okm)) != 0) {
94*e0c4386eSCy Schubert         fprintf(stderr, "Generated key does not match expected value\n");
95*e0c4386eSCy Schubert         goto end;
96*e0c4386eSCy Schubert     }
97*e0c4386eSCy Schubert 
98*e0c4386eSCy Schubert     rv = 0;
99*e0c4386eSCy Schubert end:
100*e0c4386eSCy Schubert     EVP_KDF_CTX_free(kctx);
101*e0c4386eSCy Schubert     EVP_KDF_free(kdf);
102*e0c4386eSCy Schubert     OSSL_LIB_CTX_free(library_context);
103*e0c4386eSCy Schubert     return rv;
104*e0c4386eSCy Schubert }
105