Coverage Report

Created: 2026-12-01 06:56

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
352
extern "C" int LLVMFuzzerTestOneInput(const unsigned char *data, size_t size) {
22
352
  int initialized = sodium_init();
23
352
  assert(initialized >= 0);
24
25
352
  if (size < crypto_box_SEEDBYTES + crypto_box_NONCEBYTES) {
26
11
    return 0;
27
11
  }
28
29
341
  setup_fake_random(data, size);
30
31
341
  unsigned char pk1[crypto_box_PUBLICKEYBYTES];
32
341
  unsigned char sk1[crypto_box_SECRETKEYBYTES];
33
341
  unsigned char pk2[crypto_box_PUBLICKEYBYTES];
34
341
  unsigned char sk2[crypto_box_SECRETKEYBYTES];
35
36
341
  const unsigned char *seed1 = data;
37
341
  const unsigned char *nonce = data + crypto_box_SEEDBYTES;
38
341
  const unsigned char *msg = nonce + crypto_box_NONCEBYTES;
39
341
  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
341
  crypto_box_seed_keypair(pk1, sk1, seed1);
43
  // Second keypair can be generated normally, but randombytes is hooked so it's also deterministic.
44
341
  crypto_box_keypair(pk2, sk2);
45
46
341
  unsigned char *ciphertext = (unsigned char *) malloc(msg_len + crypto_box_MACBYTES);
47
341
  int err = crypto_box_easy(ciphertext, msg, msg_len, nonce, pk2, sk1);
48
341
  assert(err == 0);
49
50
341
  unsigned char *decrypted = (unsigned char *) malloc(msg_len);
51
341
  err = crypto_box_open_easy(decrypted, ciphertext, msg_len + crypto_box_MACBYTES, nonce, pk1, sk2);
52
341
  assert(err == 0);
53
341
  assert(memcmp(decrypted, msg, msg_len) == 0);
54
55
341
  free(ciphertext);
56
341
  free(decrypted);
57
58
341
  return 0;
59
341
}