bn_div.cc
Go to the documentation of this file.
1 /* Copyright (c) 2017, Google Inc.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14 
15 #include <openssl/bn.h>
16 #include <openssl/bytestring.h>
17 #include <openssl/span.h>
18 
19 #define CHECK(expr) \
20  do { \
21  if (!(expr)) { \
22  printf("%s failed\n", #expr); \
23  abort(); \
24  } \
25  } while (false)
26 
27 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len) {
28  CBS cbs, child0, child1;
29  uint8_t sign0, sign1;
30  CBS_init(&cbs, buf, len);
31  if (!CBS_get_u16_length_prefixed(&cbs, &child0) ||
32  !CBS_get_u8(&child0, &sign0) ||
33  CBS_len(&child0) == 0 ||
34  !CBS_get_u16_length_prefixed(&cbs, &child1) ||
35  !CBS_get_u8(&child1, &sign1) ||
36  CBS_len(&child1) == 0) {
37  return 0;
38  }
39 
40  bssl::UniquePtr<BIGNUM> numerator(
41  BN_bin2bn(CBS_data(&child0), CBS_len(&child0), nullptr));
42  BN_set_negative(numerator.get(), sign0 % 2);
43  bssl::UniquePtr<BIGNUM> divisor(
44  BN_bin2bn(CBS_data(&child1), CBS_len(&child1), nullptr));
45  BN_set_negative(divisor.get(), sign1 % 2);
46 
47  if (BN_is_zero(divisor.get())) {
48  return 0;
49  }
50 
51  bssl::UniquePtr<BN_CTX> ctx(BN_CTX_new());
52  bssl::UniquePtr<BIGNUM> result(BN_new());
53  bssl::UniquePtr<BIGNUM> remainder(BN_new());
54  CHECK(ctx);
55  CHECK(result);
56  CHECK(remainder);
57 
58 
59  CHECK(BN_div(result.get(), remainder.get(), numerator.get(), divisor.get(),
60  ctx.get()));
61  CHECK(BN_ucmp(remainder.get(), divisor.get()) < 0);
62 
63  // Check that result*divisor+remainder = numerator.
64  CHECK(BN_mul(result.get(), result.get(), divisor.get(), ctx.get()));
65  CHECK(BN_add(result.get(), result.get(), remainder.get()));
66  CHECK(BN_cmp(result.get(), numerator.get()) == 0);
67 
68  return 0;
69 }
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
bn.h
cbs_st
Definition: bytestring.h:39
ctx
Definition: benchmark-async.c:30
CBS_data
#define CBS_data
Definition: boringssl_prefix_symbols.h:1057
BN_bin2bn
#define BN_bin2bn
Definition: boringssl_prefix_symbols.h:900
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
CBS_len
#define CBS_len
Definition: boringssl_prefix_symbols.h:1089
ctx
static struct test_ctx ctx
Definition: test-ipc-send-recv.c:65
cbs
const CBS * cbs
Definition: third_party/boringssl-with-bazel/src/crypto/trust_token/internal.h:107
CBS_init
#define CBS_init
Definition: boringssl_prefix_symbols.h:1085
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
bytestring.h
CBS_get_u8
#define CBS_get_u8
Definition: boringssl_prefix_symbols.h:1082
CHECK
#define CHECK(expr)
Definition: bn_div.cc:19
BN_mul
#define BN_mul
Definition: boringssl_prefix_symbols.h:969
BN_ucmp
#define BN_ucmp
Definition: boringssl_prefix_symbols.h:1001
BN_is_zero
#define BN_is_zero
Definition: boringssl_prefix_symbols.h:940
BN_CTX_new
#define BN_CTX_new
Definition: boringssl_prefix_symbols.h:885
CBS_get_u16_length_prefixed
#define CBS_get_u16_length_prefixed
Definition: boringssl_prefix_symbols.h:1074
BN_cmp
#define BN_cmp
Definition: boringssl_prefix_symbols.h:912
LLVMFuzzerTestOneInput
int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len)
Definition: bn_div.cc:27
BN_add
#define BN_add
Definition: boringssl_prefix_symbols.h:897
span.h
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
BN_div
#define BN_div
Definition: boringssl_prefix_symbols.h:917
BN_set_negative
#define BN_set_negative
Definition: boringssl_prefix_symbols.h:990
BN_new
#define BN_new
Definition: boringssl_prefix_symbols.h:971


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:49