xref: /freebsd/crypto/openssh/regress/misc/fuzz-harness/sntrup761_enc_fuzz.cc (revision ee3960cba1068e12fb032a68c46d74841d9edab3)
1 // Basic fuzz test for encapsulate operation.
2 
3 #include <stddef.h>
4 #include <stdio.h>
5 #include <stdint.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <unistd.h>
9 #include <err.h>
10 
11 extern "C" {
12 
13 #include "crypto_api.h"
14 #include "hash.c"
15 
16 #undef randombytes
17 #define USE_SNTRUP761X25519 1
18 #ifdef SNTRUP761_NO_ASM
19 # undef __GNUC__
20 #endif
21 void randombytes(unsigned char *ptr, size_t l);
22 volatile crypto_int16 crypto_int16_optblocker = 0;
23 volatile crypto_int32 crypto_int32_optblocker = 0;
24 volatile crypto_int64 crypto_int64_optblocker = 0;
25 #include "sntrup761.c"
26 
27 static int real_random;
28 
29 void
30 randombytes(unsigned char *ptr, size_t l)
31 {
32 	if (real_random)
33 		arc4random_buf(ptr, l);
34 	else
35 		memset(ptr, 0, l);
36 }
37 
38 int LLVMFuzzerTestOneInput(const uint8_t* input, size_t len)
39 {
40 	unsigned char pk[crypto_kem_sntrup761_PUBLICKEYBYTES];
41 	unsigned char ciphertext[crypto_kem_sntrup761_CIPHERTEXTBYTES];
42 	unsigned char secret[crypto_kem_sntrup761_BYTES];
43 
44 	memset(&pk, 0, sizeof(pk));
45 	if (len > sizeof(pk)) {
46 		len = sizeof(pk);
47 	}
48 	memcpy(pk, input, len);
49 
50 	real_random = 0;
51 	(void)crypto_kem_sntrup761_enc(ciphertext, secret, pk);
52 	real_random = 1;
53 	(void)crypto_kem_sntrup761_enc(ciphertext, secret, pk);
54 	return 0;
55 }
56 
57 } // extern
58