Coverage Report

Created: 2026-06-07 06:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/crypto_generichash_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 <stddef.h>
16
#include <stdint.h>
17
#include <string.h>
18
19
#include <sodium.h>
20
21
220
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
22
220
  if (sodium_init() == -1) {
23
0
    return 0;
24
0
  }
25
26
  // We need some data for outlen, keylen, and the message itself
27
220
  if (size < 2) {
28
2
    return 0;
29
2
  }
30
31
218
  size_t outlen = (data[0] % (crypto_generichash_BYTES_MAX - crypto_generichash_BYTES_MIN + 1)) + crypto_generichash_BYTES_MIN;
32
218
  size_t keylen = (data[1] % (crypto_generichash_KEYBYTES_MAX - crypto_generichash_KEYBYTES_MIN + 1));
33
  
34
218
  if (keylen > 0 && size < 2 + keylen) {
35
25
      keylen = 0;
36
25
  }
37
38
218
  const uint8_t *key = keylen > 0 ? data + 2 : NULL;
39
218
  const uint8_t *msg = data + 2 + keylen;
40
218
  size_t msglen = size - (2 + keylen);
41
42
218
  unsigned char out[crypto_generichash_BYTES_MAX];
43
44
  // Single-part API
45
218
  crypto_generichash(out, outlen, msg, msglen, key, keylen);
46
47
  // Multi-part API
48
218
  crypto_generichash_state state;
49
218
  if (crypto_generichash_init(&state, key, keylen, outlen) == 0) {
50
      // Split msg into two parts if possible
51
218
      if (msglen > 0) {
52
192
          size_t part1 = msglen / 2;
53
192
          crypto_generichash_update(&state, msg, part1);
54
192
          crypto_generichash_update(&state, msg + part1, msglen - part1);
55
192
      } else {
56
26
          crypto_generichash_update(&state, msg, 0);
57
26
      }
58
218
      crypto_generichash_final(&state, out, outlen);
59
218
  }
60
61
218
  return 0;
62
220
}