aboutsummaryrefslogtreecommitdiffhomepage
path: root/projects/pycryptodome/pcd_hash_fuzzer.cc
blob: 681f83445dcbc02fbedfda3ec1adb4717fdab28f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "common.h"
#include <fuzzer/FuzzedDataProvider.h>

#ifndef HASHTYPE
#error Macro HASHTYPE must be defined.
#endif

#ifndef FNAME
#error Macro FNAME must be defined.
#endif

#define CONCAT_TYPE(x) _PASTE2(HASHTYPE, x)

#define init CONCAT_TYPE(_init)
#define update CONCAT_TYPE(_update)
#define digest CONCAT_TYPE(_digest)
#define destroy CONCAT_TYPE(_destroy)

#define STR(x) #x
#define INCLUDE(x) STR(x)

#include INCLUDE(FNAME)

#ifndef DIGEST_SIZE
#error Macro DIGEST_SIZE must be defined.
#endif

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {

  if (!size)
    return 0;

  FuzzedDataProvider stream(data, size);
  hash_state *hs;
  if (init(&hs))
    return 0;

  while (stream.remaining_bytes()) {
    size_t num_bytes = stream.ConsumeIntegral<size_t>();
    std::vector<uint8_t> buffer = stream.ConsumeBytes<uint8_t>(num_bytes);

    if (update(hs, buffer.data(), buffer.size()))
      goto error;
  }

  uint8_t result[DIGEST_SIZE];

#ifndef DIGEST_THIRD_PARAM
  digest(hs, result);
#else
  digest(hs, result, DIGEST_SIZE);
#endif

error:
  destroy(hs);
  return 0;
}