Skip to content

Commit 5731fba

Browse files
hodgesdsKernel Patches Daemon
authored andcommitted
selftests/bpf: Add tests for ECDSA signature verification kfuncs
Add selftests to validate the ECDSA signature verification kfuncs introduced in the BPF crypto subsystem. The tests verify both valid signature acceptance and invalid signature rejection using the context-based ECDSA API. The tests use RFC 6979 test vectors for NIST P-256 (secp256r1) with well-known valid signatures. The algorithm "p1363(ecdsa-nist-p256)" is used to handle standard r||s signature format. Signed-off-by: Daniel Hodges <[email protected]>
1 parent 3b29669 commit 5731fba

File tree

3 files changed

+236
-0
lines changed

3 files changed

+236
-0
lines changed

tools/testing/selftests/bpf/config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ CONFIG_CRYPTO_HMAC=y
1515
CONFIG_CRYPTO_HASH2=y
1616
CONFIG_CRYPTO_SHA256=y
1717
CONFIG_CRYPTO_SHA512=y
18+
CONFIG_CRYPTO_ECDSA=y
1819
CONFIG_CRYPTO_USER_API=y
1920
CONFIG_CRYPTO_USER_API_HASH=y
2021
CONFIG_CRYPTO_USER_API_SKCIPHER=y
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
3+
4+
#include <test_progs.h>
5+
#include "ecdsa_verify.skel.h"
6+
7+
static void test_ecdsa_verify_valid_signature(void)
8+
{
9+
struct ecdsa_verify *skel;
10+
int err, prog_fd;
11+
12+
LIBBPF_OPTS(bpf_test_run_opts, topts);
13+
14+
skel = ecdsa_verify__open_and_load();
15+
if (!ASSERT_OK_PTR(skel, "ecdsa_verify__open_and_load"))
16+
return;
17+
18+
prog_fd = bpf_program__fd(skel->progs.test_ecdsa_verify_valid);
19+
err = bpf_prog_test_run_opts(prog_fd, &topts);
20+
ASSERT_OK(err, "test_ecdsa_verify_valid");
21+
ASSERT_EQ(skel->data->ctx_create_status, 0, "ctx_create_status");
22+
ASSERT_EQ(skel->data->verify_result, 0, "verify_valid_signature");
23+
24+
ecdsa_verify__destroy(skel);
25+
}
26+
27+
static void test_ecdsa_verify_invalid_signature(void)
28+
{
29+
struct ecdsa_verify *skel;
30+
int err, prog_fd;
31+
32+
LIBBPF_OPTS(bpf_test_run_opts, topts);
33+
34+
skel = ecdsa_verify__open_and_load();
35+
if (!ASSERT_OK_PTR(skel, "ecdsa_verify__open_and_load"))
36+
return;
37+
38+
prog_fd = bpf_program__fd(skel->progs.test_ecdsa_verify_invalid);
39+
err = bpf_prog_test_run_opts(prog_fd, &topts);
40+
ASSERT_OK(err, "test_ecdsa_verify_invalid");
41+
ASSERT_NEQ(skel->data->verify_invalid_result, 0, "verify_invalid_signature_rejected");
42+
43+
ecdsa_verify__destroy(skel);
44+
}
45+
46+
static void test_ecdsa_size_queries(void)
47+
{
48+
struct ecdsa_verify *skel;
49+
int err, prog_fd;
50+
51+
LIBBPF_OPTS(bpf_test_run_opts, topts);
52+
53+
skel = ecdsa_verify__open_and_load();
54+
if (!ASSERT_OK_PTR(skel, "ecdsa_verify__open_and_load"))
55+
return;
56+
57+
prog_fd = bpf_program__fd(skel->progs.test_ecdsa_size_queries);
58+
err = bpf_prog_test_run_opts(prog_fd, &topts);
59+
ASSERT_OK(err, "test_ecdsa_size_queries");
60+
ASSERT_EQ(skel->data->keysize_result, 256, "keysize_p256");
61+
ASSERT_EQ(skel->data->digestsize_result, 64, "digestsize_p256");
62+
ASSERT_EQ(skel->data->maxsize_result, 64, "maxsize_p256");
63+
64+
ecdsa_verify__destroy(skel);
65+
}
66+
67+
void test_ecdsa_verify(void)
68+
{
69+
if (test__start_subtest("verify_valid_signature"))
70+
test_ecdsa_verify_valid_signature();
71+
if (test__start_subtest("verify_invalid_signature"))
72+
test_ecdsa_verify_invalid_signature();
73+
if (test__start_subtest("size_queries"))
74+
test_ecdsa_size_queries();
75+
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
3+
4+
#include "vmlinux.h"
5+
#include <bpf/bpf_helpers.h>
6+
#include "bpf_misc.h"
7+
8+
struct bpf_ecdsa_ctx;
9+
extern struct bpf_ecdsa_ctx *
10+
bpf_ecdsa_ctx_create(const struct bpf_dynptr *algo_name,
11+
const struct bpf_dynptr *public_key, int *err) __ksym;
12+
extern int bpf_ecdsa_verify(struct bpf_ecdsa_ctx *ctx,
13+
const struct bpf_dynptr *message,
14+
const struct bpf_dynptr *signature) __ksym;
15+
extern int bpf_ecdsa_keysize(struct bpf_ecdsa_ctx *ctx) __ksym;
16+
extern int bpf_ecdsa_digestsize(struct bpf_ecdsa_ctx *ctx) __ksym;
17+
extern int bpf_ecdsa_maxsize(struct bpf_ecdsa_ctx *ctx) __ksym;
18+
extern void bpf_ecdsa_ctx_release(struct bpf_ecdsa_ctx *ctx) __ksym;
19+
20+
/* NIST P-256 test vector
21+
* This is a known valid ECDSA signature for testing purposes
22+
*/
23+
24+
/* Algorithm name for P-256 with p1363 format (standard r||s signature) */
25+
char algo_p256[] = "p1363(ecdsa-nist-p256)";
26+
27+
/* Public key in uncompressed format: 0x04 || x || y (65 bytes) */
28+
unsigned char pubkey_p256[65] = {
29+
0x04, /* Uncompressed point indicator */
30+
/* X coordinate (32 bytes) */
31+
0x60, 0xfe, 0xd4, 0xba, 0x25, 0x5a, 0x9d, 0x31,
32+
0xc9, 0x61, 0xeb, 0x74, 0xc6, 0x35, 0x6d, 0x68,
33+
0xc0, 0x49, 0xb8, 0x92, 0x3b, 0x61, 0xfa, 0x6c,
34+
0xe6, 0x69, 0x62, 0x2e, 0x60, 0xf2, 0x9f, 0xb6,
35+
/* Y coordinate (32 bytes) */
36+
0x79, 0x03, 0xfe, 0x10, 0x08, 0xb8, 0xbc, 0x99,
37+
0xa4, 0x1a, 0xe9, 0xe9, 0x56, 0x28, 0xbc, 0x64,
38+
0xf2, 0xf1, 0xb2, 0x0c, 0x2d, 0x7e, 0x9f, 0x51,
39+
0x77, 0xa3, 0xc2, 0x94, 0xd4, 0x46, 0x22, 0x99
40+
};
41+
42+
/* Message hash (32 bytes) - SHA-256 of "sample" */
43+
unsigned char message_hash[32] = {
44+
0xaf, 0x2b, 0xdb, 0xe1, 0xaa, 0x9b, 0x6e, 0xc1,
45+
0xe2, 0xad, 0xe1, 0xd6, 0x94, 0xf4, 0x1f, 0xc7,
46+
0x1a, 0x83, 0x1d, 0x02, 0x68, 0xe9, 0x89, 0x15,
47+
0x62, 0x11, 0x3d, 0x8a, 0x62, 0xad, 0xd1, 0xbf
48+
};
49+
50+
/* Valid signature r || s (64 bytes) */
51+
unsigned char valid_signature[64] = {
52+
/* r component (32 bytes) */
53+
0xef, 0xd4, 0x8b, 0x2a, 0xac, 0xb6, 0xa8, 0xfd,
54+
0x11, 0x40, 0xdd, 0x9c, 0xd4, 0x5e, 0x81, 0xd6,
55+
0x9d, 0x2c, 0x87, 0x7b, 0x56, 0xaa, 0xf9, 0x91,
56+
0xc3, 0x4d, 0x0e, 0xa8, 0x4e, 0xaf, 0x37, 0x16,
57+
/* s component (32 bytes) */
58+
0xf7, 0xcb, 0x1c, 0x94, 0x2d, 0x65, 0x7c, 0x41,
59+
0xd4, 0x36, 0xc7, 0xa1, 0xb6, 0xe2, 0x9f, 0x65,
60+
0xf3, 0xe9, 0x00, 0xdb, 0xb9, 0xaf, 0xf4, 0x06,
61+
0x4d, 0xc4, 0xab, 0x2f, 0x84, 0x3a, 0xcd, 0xa8
62+
};
63+
64+
/* Invalid signature (modified r component) for negative test */
65+
unsigned char invalid_signature[64] = {
66+
/* r component (32 bytes) - first byte modified */
67+
0xff, 0xd4, 0x8b, 0x2a, 0xac, 0xb6, 0xa8, 0xfd,
68+
0x11, 0x40, 0xdd, 0x9c, 0xd4, 0x5e, 0x81, 0xd6,
69+
0x9d, 0x2c, 0x87, 0x7b, 0x56, 0xaa, 0xf9, 0x91,
70+
0xc3, 0x4d, 0x0e, 0xa8, 0x4e, 0xaf, 0x37, 0x16,
71+
/* s component (32 bytes) */
72+
0xf7, 0xcb, 0x1c, 0x94, 0x2d, 0x65, 0x7c, 0x41,
73+
0xd4, 0x36, 0xc7, 0xa1, 0xb6, 0xe2, 0x9f, 0x65,
74+
0xf3, 0xe9, 0x00, 0xdb, 0xb9, 0xaf, 0xf4, 0x06,
75+
0x4d, 0xc4, 0xab, 0x2f, 0x84, 0x3a, 0xcd, 0xa8
76+
};
77+
78+
/* Test results */
79+
int verify_result = -1;
80+
int verify_invalid_result = -1;
81+
int ctx_create_status = -1;
82+
int keysize_result = -1;
83+
int digestsize_result = -1;
84+
int maxsize_result = -1;
85+
86+
SEC("syscall")
87+
int test_ecdsa_verify_valid(void *ctx)
88+
{
89+
struct bpf_ecdsa_ctx *ecdsa_ctx;
90+
struct bpf_dynptr algo_ptr, key_ptr, msg_ptr, sig_ptr;
91+
int err = 0;
92+
93+
bpf_dynptr_from_mem(algo_p256, sizeof(algo_p256) - 1, 0, &algo_ptr);
94+
bpf_dynptr_from_mem(pubkey_p256, sizeof(pubkey_p256), 0, &key_ptr);
95+
96+
ecdsa_ctx = bpf_ecdsa_ctx_create(&algo_ptr, &key_ptr, &err);
97+
if (!ecdsa_ctx) {
98+
ctx_create_status = err;
99+
return 0;
100+
}
101+
ctx_create_status = 0;
102+
103+
bpf_dynptr_from_mem(message_hash, sizeof(message_hash), 0, &msg_ptr);
104+
bpf_dynptr_from_mem(valid_signature, sizeof(valid_signature), 0, &sig_ptr);
105+
106+
verify_result = bpf_ecdsa_verify(ecdsa_ctx, &msg_ptr, &sig_ptr);
107+
108+
bpf_ecdsa_ctx_release(ecdsa_ctx);
109+
110+
return 0;
111+
}
112+
113+
SEC("syscall")
114+
int test_ecdsa_verify_invalid(void *ctx)
115+
{
116+
struct bpf_ecdsa_ctx *ecdsa_ctx;
117+
struct bpf_dynptr algo_ptr, key_ptr, msg_ptr, sig_ptr;
118+
int err = 0;
119+
120+
bpf_dynptr_from_mem(algo_p256, sizeof(algo_p256) - 1, 0, &algo_ptr);
121+
bpf_dynptr_from_mem(pubkey_p256, sizeof(pubkey_p256), 0, &key_ptr);
122+
123+
ecdsa_ctx = bpf_ecdsa_ctx_create(&algo_ptr, &key_ptr, &err);
124+
if (!ecdsa_ctx)
125+
return 0;
126+
127+
bpf_dynptr_from_mem(message_hash, sizeof(message_hash), 0, &msg_ptr);
128+
bpf_dynptr_from_mem(invalid_signature, sizeof(invalid_signature), 0, &sig_ptr);
129+
130+
verify_invalid_result = bpf_ecdsa_verify(ecdsa_ctx, &msg_ptr, &sig_ptr);
131+
132+
bpf_ecdsa_ctx_release(ecdsa_ctx);
133+
134+
return 0;
135+
}
136+
137+
SEC("syscall")
138+
int test_ecdsa_size_queries(void *ctx)
139+
{
140+
struct bpf_ecdsa_ctx *ecdsa_ctx;
141+
struct bpf_dynptr algo_ptr, key_ptr;
142+
int err = 0;
143+
144+
bpf_dynptr_from_mem(algo_p256, sizeof(algo_p256) - 1, 0, &algo_ptr);
145+
bpf_dynptr_from_mem(pubkey_p256, sizeof(pubkey_p256), 0, &key_ptr);
146+
147+
ecdsa_ctx = bpf_ecdsa_ctx_create(&algo_ptr, &key_ptr, &err);
148+
if (!ecdsa_ctx)
149+
return 0;
150+
151+
keysize_result = bpf_ecdsa_keysize(ecdsa_ctx);
152+
digestsize_result = bpf_ecdsa_digestsize(ecdsa_ctx);
153+
maxsize_result = bpf_ecdsa_maxsize(ecdsa_ctx);
154+
155+
bpf_ecdsa_ctx_release(ecdsa_ctx);
156+
157+
return 0;
158+
}
159+
160+
char __license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)