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