Coverage Report

Created: 2026-06-15 07:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boringssl/fuzz/bn_div.cc
Line
Count
Source
1
// Copyright 2017 The BoringSSL Authors
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
//     https://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 <openssl/bn.h>
16
#include <openssl/bytestring.h>
17
#include <openssl/span.h>
18
19
#define CHECK(expr)                 \
20
11.2k
  do {                              \
21
11.2k
    if (!(expr)) {                  \
22
0
      printf("%s failed\n", #expr); \
23
0
      abort();                      \
24
0
    }                               \
25
11.2k
  } while (false)
26
27
1.50k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len) {
28
1.50k
  CBS cbs, child0, child1;
29
1.50k
  uint8_t sign0, sign1;
30
1.50k
  CBS_init(&cbs, buf, len);
31
1.50k
  if (!CBS_get_u16_length_prefixed(&cbs, &child0) ||
32
1.48k
      !CBS_get_u8(&child0, &sign0) ||
33
1.46k
      CBS_len(&child0) == 0 ||
34
1.46k
      !CBS_get_u16_length_prefixed(&cbs, &child1) ||
35
1.42k
      !CBS_get_u8(&child1, &sign1) ||
36
1.41k
      CBS_len(&child1) == 0) {
37
88
    return 0;
38
88
  }
39
40
1.41k
  bssl::UniquePtr<BIGNUM> numerator(
41
1.41k
      BN_bin2bn(CBS_data(&child0), CBS_len(&child0), nullptr));
42
1.41k
  BN_set_negative(numerator.get(), sign0 % 2);
43
1.41k
  bssl::UniquePtr<BIGNUM> divisor(
44
1.41k
      BN_bin2bn(CBS_data(&child1), CBS_len(&child1), nullptr));
45
1.41k
  BN_set_negative(divisor.get(), sign1 % 2);
46
47
1.41k
  if (BN_is_zero(divisor.get())) {
48
4
    return 0;
49
4
  }
50
51
1.41k
  bssl::UniquePtr<BN_CTX> ctx(BN_CTX_new());
52
1.41k
  bssl::UniquePtr<BIGNUM> result(BN_new());
53
1.41k
  bssl::UniquePtr<BIGNUM> remainder(BN_new());
54
1.41k
  CHECK(ctx);
55
1.41k
  CHECK(result);
56
1.41k
  CHECK(remainder);
57
58
59
1.41k
  CHECK(BN_div(result.get(), remainder.get(), numerator.get(), divisor.get(),
60
1.41k
               ctx.get()));
61
1.41k
  CHECK(BN_ucmp(remainder.get(), divisor.get()) < 0);
62
63
  // Check that result*divisor+remainder = numerator.
64
1.41k
  CHECK(BN_mul(result.get(), result.get(), divisor.get(), ctx.get()));
65
1.41k
  CHECK(BN_add(result.get(), result.get(), remainder.get()));
66
1.41k
  CHECK(BN_cmp(result.get(), numerator.get()) == 0);
67
68
1.41k
  return 0;
69
1.41k
}