Skip to content

Commit 7e07380

Browse files
committed
Complete the code part of the fuzzer test
1 parent 7cc440f commit 7e07380

File tree

4 files changed

+61
-47
lines changed

4 files changed

+61
-47
lines changed

c/ref/sign.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,6 @@ int crypto_sign_verify(crypto_context *cctx, const uint8_t *sig, size_t siglen,
274274

275275
/* Check if the root node equals the root node in the public key. */
276276
if (memcmp(root, pub_root, SPX_N)) {
277-
printf("R2\n");
278277
return -1;
279278
}
280279

tests/sphincsplus/fuzzer/Makefile

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ CC=clang
44
LLVM_PROFDATA=llvm-profdata
55
LLVM_COV=llvm-cov
66

7-
PARAMS = sphincs-shake-256f
8-
THASH = robust
7+
NPROC?=30
98

109
SOURCES_DIR = ref
1110

1211
SOURCES = \
12+
../../../c/$(SOURCES_DIR)/params.c \
1313
../../../c/$(SOURCES_DIR)/address.c \
1414
../../../c/$(SOURCES_DIR)/merkle.c \
1515
../../../c/$(SOURCES_DIR)/wots.c \
@@ -36,35 +36,37 @@ HEADERS = \
3636
../../../c/$(SOURCES_DIR)/randombytes.h \
3737
../../../c/ckb-sphincsplus.h
3838

39-
ifneq (,$(findstring shake,$(PARAMS)))
40-
SOURCES += \
41-
../../../c/$(SOURCES_DIR)/fips202.c \
42-
../../../c/$(SOURCES_DIR)/hash_shake.c \
43-
../../../c/$(SOURCES_DIR)/thash_shake_$(THASH).c
44-
HEADERS += \
45-
../../../c/$(SOURCES_DIR)/fips202.h
46-
endif
47-
ifneq (,$(findstring haraka,$(PARAMS)))
48-
SOURCES += \
49-
../../../c/$(SOURCES_DIR)/haraka.c \
50-
../../../c/$(SOURCES_DIR)/hash_haraka.c \
51-
../../../c/$(SOURCES_DIR)/thash_haraka_$(THASH).c
52-
HEADERS += \
53-
../../../c/$(SOURCES_DIR)/haraka.h
54-
endif
55-
ifneq (,$(findstring sha2,$(PARAMS)))
56-
SOURCES += \
57-
../../../c/$(SOURCES_DIR)/sha2.c \
58-
../../../c/$(SOURCES_DIR)/hash_sha2.c \
59-
../../../c/$(SOURCES_DIR)/thash_sha2_$(THASH).c
60-
HEADERS += \
61-
../../../c/$(SOURCES_DIR)/sha2.h
62-
endif
39+
# shake
40+
SOURCES += \
41+
../../../c/$(SOURCES_DIR)/fips202.c \
42+
../../../c/$(SOURCES_DIR)/hash_shake.c \
43+
../../../c/$(SOURCES_DIR)/thash_shake_robust.c\
44+
../../../c/$(SOURCES_DIR)/thash_shake_simple.c
45+
HEADERS += \
46+
../../../c/$(SOURCES_DIR)/fips202.h
47+
48+
# sha2
49+
SOURCES += \
50+
../../../c/$(SOURCES_DIR)/sha2.c \
51+
../../../c/$(SOURCES_DIR)/hash_sha2.c \
52+
../../../c/$(SOURCES_DIR)/thash_sha2_robust.c \
53+
../../../c/$(SOURCES_DIR)/thash_sha2_simple.c
54+
HEADERS += \
55+
../../../c/$(SOURCES_DIR)/sha2.h
6356

64-
FUZZER_FLAGS=-g -O1 -fsanitize=fuzzer,address,undefined -fsanitize-recover=address -DPARAMS=$(PARAMS)
57+
# haraka
58+
SOURCES += \
59+
../../../c/$(SOURCES_DIR)/haraka.c \
60+
../../../c/$(SOURCES_DIR)/hash_haraka.c \
61+
../../../c/$(SOURCES_DIR)/thash_haraka_robust.c \
62+
../../../c/$(SOURCES_DIR)/thash_haraka_simple.c
63+
HEADERS += \
64+
../../../c/$(SOURCES_DIR)/haraka.h
65+
66+
FUZZER_FLAGS=-g -O1 -fsanitize=fuzzer,address,undefined -fsanitize-recover=address
6567
FUZZER_FLAGS := $(FUZZER_FLAGS) -I ../../../c -I ../../../c/ref
6668

67-
COVERAGE_FLAGS=-fprofile-instr-generate -fcoverage-mapping -DPARAMS=$(PARAMS)
69+
COVERAGE_FLAGS=-fprofile-instr-generate -fcoverage-mapping
6870
COVERAGE_FLAGS := $(COVERAGE_FLAGS) -I ../../../c -I ../../../c/ref
6971

7072
ifeq ($(OS),MacOS)
@@ -80,5 +82,13 @@ build/fuzzer: sphincs_plus_fuzzer.c $(SOURCES) $(HEADERS)
8082
build/cover: sphincs_plus_cover.c sphincs_plus_fuzzer.c $(SOURCES) $(HEADERS)
8183
$(CC) $(COVERAGE_FLAGS) -o $@ $(SOURCES) $< sphincs_plus_fuzzer.c
8284

85+
start-fuzzer: build/fuzzer
86+
mkdir -p build/corpus
87+
cd build && ./fuzzer -max_len=8000000 -jobs=$(NPROC) corpus
88+
8389
clean:
84-
rm -rf build/*
90+
rm -rf build/*
91+
92+
clean-fuzzer:
93+
rm -rf build/corpus
94+
rm -rf build/fuzz-*.log

tests/sphincsplus/fuzzer/run.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ else
1414
fi
1515

1616
cd build
17-
./fuzzer
17+
mkdir -p corpus
18+
./fuzzer -max_len=80000 -workers=2 -jobs=2 corpus

tests/sphincsplus/fuzzer/sphincs_plus_fuzzer.c

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,56 +4,60 @@
44

55
#include "ckb-sphincsplus.h"
66

7-
typedef enum {
8-
SphincsFuzz_Success = 0,
9-
SphincsFuzz_Init,
10-
} SphincsFuzzError;
11-
127
size_t fill_buf(uint8_t *buf, size_t buf_size, uint8_t *data, size_t size) {
138
if (size == 0) {
149
memset(buf, 0, buf_size);
10+
return 0;
1511
} else if (size >= buf_size) {
1612
memcpy(buf, data, buf_size);
13+
return buf_size;
1714
} else {
1815
memcpy(buf, data, size);
1916
buf += size;
2017
memset(buf, 0, buf_size - size);
18+
return size;
2119
}
2220
}
2321

2422
int LLVMFuzzerTestOneInput(uint8_t *data, size_t size) {
2523
uint32_t hash_type = 0;
2624
if (size > 0) {
27-
hash_type = data[0];
25+
hash_type = data[0] % 37;
2826
}
2927
data += 1;
3028
size -= 1;
3129

32-
int err = sphincs_plus_init(hash_type);
30+
crypto_context cctx = {0};
31+
int err = sphincs_plus_init_context(hash_type, &cctx);
3332
if (err != 0) {
34-
return SphincsFuzz_Success;
33+
return 0;
3534
}
3635

3736
uint8_t message[SPX_MLEN];
38-
uint8_t pubkey[sphincs_plus_get_pk_size()];
39-
uint8_t sign[sphincs_plus_get_sign_size()];
37+
38+
uint32_t pubkey_size = sphincs_plus_get_pk_size(&cctx);
39+
uint8_t pubkey[pubkey_size];
40+
41+
uint32_t sign_size = sphincs_plus_get_sign_size(&cctx);
42+
uint8_t sign[sign_size];
4043

4144
size_t offset = fill_buf(message, SPX_MLEN, data, size);
4245
data += offset;
4346
size -= offset;
4447

45-
offset = fill_buf(pubkey, sphincs_plus_get_pk_size(), data, size);
48+
offset = fill_buf(pubkey, pubkey_size, data, size);
4649
data += offset;
4750
size -= offset;
4851

49-
offset = fill_buf(sign, sphincs_plus_get_sign_size(), data, size);
52+
offset = fill_buf(sign, sign_size, data, size);
5053
data += offset;
5154
size -= offset;
5255

53-
err = sphincs_plus_verify(sign, message, pubkey);
54-
if (err == 0) {
55-
return SphincsFuzz_Success;
56+
err = sphincs_plus_verify(&cctx, sign, sign_size, message, SPX_MLEN, pubkey,
57+
pubkey_size);
58+
if (err != 0) {
59+
return 0;
5660
};
5761

58-
return SphincsFuzz_Success;
62+
return 0;
5963
}

0 commit comments

Comments
 (0)