Skip to content

Commit 60af4a9

Browse files
Adapt existing sig fuzz harness including more algorithms (#1955)
Signed-off-by: Nathaniel Brough <[email protected]>
1 parent 3c8bde1 commit 60af4a9

File tree

4 files changed

+31
-26
lines changed

4 files changed

+31
-26
lines changed

.github/workflows/basic.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ jobs:
113113
cmake -LA -N .. && \
114114
! (grep -i "uninitialized variable" config.log)
115115
- name: Build code
116-
run: ninja
116+
run: ninja fuzz_test_sig
117117
working-directory: build
118118

119119
- name: Short fuzz check (30s)
120-
run: ./tests/fuzz_test_dilithium2 -max_total_time=30
120+
run: ./tests/fuzz_test_sig -max_total_time=30
121121
working-directory: build

docs/FUZZING.md

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,11 @@ errors, helping developers identify and fix bugs and security loopholes.
1515
- [ ] ml_kem
1616
- [ ] ntruprime
1717
- [ ] sig
18-
- [ ] dilithium
19-
- [x] dilithium2
20-
- [ ] dilithium3
21-
- [ ] dilithium5
22-
- [ ] falcon
23-
- [ ] mayo
24-
- [ ] ml_dsa
25-
- [ ] sphincs
18+
- [x] dilithium
19+
- [x] falcon
20+
- [x] mayo
21+
- [x] ml_dsa
22+
- [x] sphincs
2623
- [ ] sig_stfl
2724
- [ ] lms
2825
- [ ] sig_stfl

tests/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ add_executable(example_sig example_sig.c)
9595
target_link_libraries(example_sig PRIVATE ${TEST_DEPS})
9696

9797
if(OQS_BUILD_FUZZ_TESTS AND '${CMAKE_C_COMPILER_ID}' STREQUAL 'Clang')
98-
add_executable(fuzz_test_dilithium2 fuzz_test_dilithium2.c)
99-
target_link_libraries(fuzz_test_dilithium2 PRIVATE ${TEST_DEPS})
100-
set_target_properties(fuzz_test_dilithium2 PROPERTIES
98+
add_executable(fuzz_test_sig fuzz_test_sig.c)
99+
target_link_libraries(fuzz_test_sig PRIVATE ${TEST_DEPS})
100+
set_target_properties(fuzz_test_sig PROPERTIES
101101
COMPILE_FLAGS "${FUZZING_COMPILE_FLAGS}"
102102
LINK_FLAGS "${FUZZING_LINK_FLAGS}"
103103
)
Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* SPDX-License-Identifier: MIT
77
*/
88

9+
#include "oqs/sig.h"
910
#include <stdbool.h>
1011
#include <stdio.h>
1112
#include <stdlib.h>
@@ -18,20 +19,33 @@ void cleanup_heap(uint8_t *public_key, uint8_t *secret_key,
1819
uint8_t *signature,
1920
OQS_SIG *sig);
2021

21-
static OQS_STATUS fuzz_dilithium_2(const uint8_t *message, size_t message_len) {
22-
23-
#ifdef OQS_ENABLE_SIG_dilithium_2
24-
22+
static OQS_STATUS fuzz_sig(const uint8_t *data, size_t data_len) {
2523
OQS_SIG *sig = NULL;
2624
uint8_t *public_key = NULL;
2725
uint8_t *secret_key = NULL;
2826
uint8_t *signature = NULL;
2927
size_t signature_len;
3028
OQS_STATUS rc;
3129

32-
sig = OQS_SIG_new(OQS_SIG_alg_dilithium_2);
30+
// Select algorithm based on fuzzed data.
31+
size_t algorithm_index = 0;
32+
const uint8_t *message = NULL;
33+
size_t message_len = 0;
34+
if (data_len > sizeof(size_t)) {
35+
memcpy(&algorithm_index, data, sizeof(size_t));
36+
message = data + sizeof(size_t);
37+
message_len = data_len - sizeof(size_t);
38+
39+
algorithm_index %= OQS_SIG_algs_length;
40+
} else {
41+
message = data;
42+
message_len = data_len;
43+
}
44+
const char *algorithm = OQS_SIG_alg_identifier(algorithm_index);
45+
46+
sig = OQS_SIG_new(algorithm);
3347
if (sig == NULL) {
34-
printf("[fuzz_test_dilithium_2] OQS_SIG_alg_dilithium_2 was not enabled at compile-time.\n");
48+
printf("%s was not enabled at compile-time.\n", algorithm);
3549
return OQS_ERROR;
3650
}
3751

@@ -65,12 +79,6 @@ static OQS_STATUS fuzz_dilithium_2(const uint8_t *message, size_t message_len) {
6579

6680
cleanup_heap(public_key, secret_key, signature, sig);
6781
return OQS_SUCCESS; // success
68-
#else
69-
70-
printf("[fuzz_test_dilithium_2] OQS_SIG_dilithium_2 was not enabled at compile-time.\n");
71-
return OQS_SUCCESS;
72-
73-
#endif
7482
}
7583

7684
void cleanup_heap(uint8_t *public_key, uint8_t *secret_key,
@@ -86,7 +94,7 @@ void cleanup_heap(uint8_t *public_key, uint8_t *secret_key,
8694

8795
int LLVMFuzzerTestOneInput(const char *data, size_t size) {
8896
OQS_init();
89-
if (OQS_ERROR == fuzz_dilithium_2((const uint8_t *)data, size)) {
97+
if (OQS_ERROR == fuzz_sig((const uint8_t *)data, size)) {
9098
// If we get an error prune testcase from corpus.
9199
return -1;
92100
}

0 commit comments

Comments
 (0)