Coverage Report

Created: 2026-06-07 06:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/crypto_box_fuzzer.cc
Line
Count
Source
1
// Copyright 2026 Google LLC
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#include <assert.h>
16
#include <stdlib.h>
17
#include <sodium.h>
18
19
#include "fake_random.h"
20
21
0
extern "C" int LLVMFuzzerTestOneInput(const unsigned char *data, size_t size) {
22
0
  int initialized = sodium_init();
23
0
  assert(initialized >= 0);
24
25
0
  if (size < crypto_box_SEEDBYTES + crypto_box_NONCEBYTES) {
26
0
    return 0;
27
0
  }
28
29
0
  setup_fake_random(data, size);
30
31
0
  unsigned char pk1[crypto_box_PUBLICKEYBYTES];
32
0
  unsigned char sk1[crypto_box_SECRETKEYBYTES];
33
0
  unsigned char pk2[crypto_box_PUBLICKEYBYTES];
34
0
  unsigned char sk2[crypto_box_SECRETKEYBYTES];
35
36
0
  const unsigned char *seed1 = data;
37
0
  const unsigned char *nonce = data + crypto_box_SEEDBYTES;
38
0
  const unsigned char *msg = nonce + crypto_box_NONCEBYTES;
39
0
  size_t msg_len = size - (crypto_box_SEEDBYTES + crypto_box_NONCEBYTES);
40
41
  // Generate keypairs. Using seed for the first one to be more deterministic from input.
42
0
  crypto_box_seed_keypair(pk1, sk1, seed1);
43
  // Second keypair can be generated normally, but randombytes is hooked so it's also deterministic.
44
0
  crypto_box_keypair(pk2, sk2);
45
46
0
  unsigned char *ciphertext = (unsigned char *) malloc(msg_len + crypto_box_MACBYTES);
47
0
  int err = crypto_box_easy(ciphertext, msg, msg_len, nonce, pk2, sk1);
48
0
  assert(err == 0);
49
50
0
  unsigned char *decrypted = (unsigned char *) malloc(msg_len);
51
0
  err = crypto_box_open_easy(decrypted, ciphertext, msg_len + crypto_box_MACBYTES, nonce, pk1, sk2);
52
0
  assert(err == 0);
53
0
  assert(memcmp(decrypted, msg, msg_len) == 0);
54
55
0
  free(ciphertext);
56
0
  free(decrypted);
57
58
0
  return 0;
59
0
}