Skip to content

Commit c804c2f

Browse files
committed
test: add hkdf_add1 test
The EVP_PKEY_CTX_add1_hkdf_info implementation had a bug in the past which made it behave like a set instead of add function. When linking against external builds warn about it. The libnvme implementation works around this problem, but it's better to have this logged during the configure step, so there is chance to debug this. Signed-off-by: Daniel Wagner <[email protected]>
1 parent 59f7020 commit c804c2f

3 files changed

Lines changed: 115 additions & 0 deletions

File tree

meson.build

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,22 @@ else
252252
conf.set('fallthrough', 'do {} while (0) /* fallthrough */')
253253
endif
254254

255+
if openssl_dep.found()
256+
if openssl_dep.type_name() != 'internal'
257+
# Check for a bug in the EVP_PKEY_CTX_add1_hkdf_info implementation
258+
res = cc.run(
259+
files('test/hkdf_add1.c'),
260+
dependencies: [openssl_dep],
261+
name: 'check hkdf_add1'
262+
)
263+
if res.returncode() == 1
264+
warning('EVP_PKEY_CTX_add1_hkdf_info bahaves incorrectly')
265+
else
266+
message('EVP_PKEY_CTX_add1_hkdf_info behaves sanely')
267+
endif
268+
endif
269+
endif
270+
255271
################################################################################
256272
substs = configuration_data()
257273
substs.set('NAME', meson.project_name())

test/hkdf_add1.c

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// SPDX-License-Identifier: LGPL-2.1-or-later
2+
/**
3+
* This file is part of libnvme.
4+
* Copyright (c) 2025 SUSE LLC.
5+
*
6+
* Authors: Daniel Wagner <[email protected]>
7+
*/
8+
9+
#include <openssl/core_names.h>
10+
#include <openssl/evp.h>
11+
#include <openssl/hmac.h>
12+
#include <openssl/kdf.h>
13+
#include <openssl/params.h>
14+
#include <stdio.h>
15+
#include <string.h>
16+
17+
#define SHA256_LEN 32
18+
19+
static EVP_PKEY_CTX *setup_ctx(void)
20+
{
21+
EVP_PKEY_CTX *ctx = NULL;
22+
const char *salt = "salt";
23+
const char *key = "key";
24+
25+
ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
26+
if (!ctx)
27+
return NULL;
28+
if (EVP_PKEY_derive_init(ctx) <= 0)
29+
goto free_ctx;
30+
if (EVP_PKEY_CTX_set_hkdf_md(ctx, EVP_sha256()) <= 0)
31+
goto free_ctx;
32+
if (EVP_PKEY_CTX_set1_hkdf_salt(ctx,
33+
(unsigned char *)salt, strlen(salt)) <= 0)
34+
goto free_ctx;
35+
if (EVP_PKEY_CTX_set1_hkdf_key(ctx,
36+
(unsigned char *)key, strlen(key)) <= 0)
37+
goto free_ctx;
38+
39+
return ctx;
40+
41+
free_ctx:
42+
EVP_PKEY_CTX_free(ctx);
43+
return NULL;
44+
}
45+
46+
int main(void)
47+
{
48+
unsigned char out[SHA256_LEN], out2[SHA256_LEN];
49+
size_t len = sizeof(out);
50+
const char *a = "a";
51+
const char *b = "b";
52+
EVP_PKEY_CTX *ctx;
53+
54+
/* out = A + B */
55+
ctx = setup_ctx();
56+
if (!ctx)
57+
return 1;
58+
if (EVP_PKEY_CTX_add1_hkdf_info(ctx,
59+
(unsigned char *)a, strlen(a)) <= 0)
60+
goto free_ctx;
61+
if (EVP_PKEY_CTX_add1_hkdf_info(ctx,
62+
(unsigned char *)b, strlen(b)) <= 0)
63+
goto free_ctx;
64+
if (EVP_PKEY_derive(ctx, out, &len) <= 0)
65+
goto free_ctx;
66+
EVP_PKEY_CTX_free(ctx);
67+
68+
/* out = B */
69+
ctx = setup_ctx();
70+
if (!ctx)
71+
return 1;
72+
if (EVP_PKEY_CTX_add1_hkdf_info(ctx,
73+
(unsigned char *)b, strlen(b)) <= 0)
74+
goto free_ctx;
75+
if (EVP_PKEY_derive(ctx, out2, &len) <= 0)
76+
goto free_ctx;
77+
EVP_PKEY_CTX_free(ctx);
78+
79+
printf("EVP_PKEY_CTX_add1_hkdf_info behavior: ");
80+
if (!memcmp(out, out2, len)) {
81+
printf("set\n");
82+
return 1;
83+
}
84+
85+
printf("add\n");
86+
return 0;
87+
88+
free_ctx:
89+
EVP_PKEY_CTX_free(ctx);
90+
return 1;
91+
}

test/meson.build

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,11 @@ if json_c_dep.found()
131131
subdir('sysfs')
132132
subdir('config')
133133
endif
134+
135+
if openssl_dep.found()
136+
hkdf_add1 = executable(
137+
'hkdf_add1',
138+
['hkdf_add1.c'],
139+
dependencies: openssl_dep,
140+
)
141+
endif

0 commit comments

Comments
 (0)