]>
git.ipfire.org Git - thirdparty/openssl.git/blob - fuzz/bignum.c
2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL licenses, (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 * https://www.openssl.org/source/license.html
8 * or in the file LICENSE in the source distribution.
12 * Confirm that a^b mod c agrees when calculated cleverly vs naively, for
17 #include <openssl/bn.h>
20 int FuzzerTestOneInput(const uint8_t *buf
, size_t len
) {
23 static BN_MONT_CTX
*mont
;
37 mont
= BN_MONT_CTX_new();
39 // Divide the input into three parts, using the values of the first two
40 // bytes to choose lengths, which generate b1, b2 and b3. Use three bits
41 // of the third byte to choose signs for the three numbers.
42 size_t l1
= 0, l2
= 0, l3
= 0;
43 int s1
= 0, s2
= 0, s3
= 0;
46 l1
= (buf
[0] * len
) / 255;
48 l2
= (buf
[0] * (len
- l1
)) / 255;
57 OPENSSL_assert(BN_bin2bn(buf
, l1
, b1
) == b1
);
58 BN_set_negative(b1
, s1
);
59 OPENSSL_assert(BN_bin2bn(buf
+ l1
, l2
, b2
) == b2
);
60 BN_set_negative(b2
, s2
);
61 OPENSSL_assert(BN_bin2bn(buf
+ l1
+ l2
, l3
, b3
) == b3
);
62 BN_set_negative(b3
, s3
);
70 OPENSSL_assert(BN_mod_exp(b4
, b1
, b2
, b3
, ctx
));
71 OPENSSL_assert(BN_mod_exp_simple(b5
, b1
, b2
, b3
, ctx
));
73 success
= BN_cmp(b4
, b5
) == 0;
75 BN_print_fp(stdout
, b1
);
77 BN_print_fp(stdout
, b2
);
79 BN_print_fp(stdout
, b3
);
81 BN_print_fp(stdout
, b4
);
83 BN_print_fp(stdout
, b5
);
88 OPENSSL_assert(success
);