1 // Basic fuzz test for depcapsulate 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 void privkeys(unsigned char *zero_sk, unsigned char *rnd_sk) 39 { 40 unsigned char pk[crypto_kem_sntrup761_PUBLICKEYBYTES]; 41 42 real_random = 0; 43 if (crypto_kem_sntrup761_keypair(pk, zero_sk) != 0) 44 errx(1, "crypto_kem_sntrup761_keypair failed"); 45 real_random = 1; 46 if (crypto_kem_sntrup761_keypair(pk, rnd_sk) != 0) 47 errx(1, "crypto_kem_sntrup761_keypair failed"); 48 } 49 50 int LLVMFuzzerTestOneInput(const uint8_t* input, size_t len) 51 { 52 static bool once; 53 static unsigned char zero_sk[crypto_kem_sntrup761_SECRETKEYBYTES]; 54 static unsigned char rnd_sk[crypto_kem_sntrup761_SECRETKEYBYTES]; 55 unsigned char ciphertext[crypto_kem_sntrup761_CIPHERTEXTBYTES]; 56 unsigned char secret[crypto_kem_sntrup761_BYTES]; 57 58 if (!once) { 59 privkeys(zero_sk, rnd_sk); 60 once = true; 61 } 62 63 memset(&ciphertext, 0, sizeof(ciphertext)); 64 if (len > sizeof(ciphertext)) { 65 len = sizeof(ciphertext); 66 } 67 memcpy(ciphertext, input, len); 68 69 (void)crypto_kem_sntrup761_dec(secret, ciphertext, zero_sk); 70 (void)crypto_kem_sntrup761_dec(secret, ciphertext, rnd_sk); 71 return 0; 72 } 73 74 } // extern 75