|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
|
|
Packit Service |
084de1 |
*
|
|
Packit Service |
084de1 |
* Licensed under the OpenSSL licenses, (the "License");
|
|
Packit Service |
084de1 |
* you may not use this file except in compliance with the License.
|
|
Packit Service |
084de1 |
* You may obtain a copy of the License at
|
|
Packit Service |
084de1 |
* https://www.openssl.org/source/license.html
|
|
Packit Service |
084de1 |
* or in the file LICENSE in the source distribution.
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* Confirm that a^b mod c agrees when calculated cleverly vs naively, for
|
|
Packit Service |
084de1 |
* random a, b and c.
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
#include <stdio.h>
|
|
Packit Service |
084de1 |
#include <openssl/bn.h>
|
|
Packit Service |
084de1 |
#include <openssl/err.h>
|
|
Packit Service |
084de1 |
#include "fuzzer.h"
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
int FuzzerInitialize(int *argc, char ***argv)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
|
|
Packit Service |
084de1 |
ERR_get_state();
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
int FuzzerTestOneInput(const uint8_t *buf, size_t len)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
int success = 0;
|
|
Packit Service |
084de1 |
size_t l1 = 0, l2 = 0, l3 = 0;
|
|
Packit Service |
084de1 |
int s1 = 0, s3 = 0;
|
|
Packit Service |
084de1 |
BN_CTX *ctx;
|
|
Packit Service |
084de1 |
BIGNUM *b1;
|
|
Packit Service |
084de1 |
BIGNUM *b2;
|
|
Packit Service |
084de1 |
BIGNUM *b3;
|
|
Packit Service |
084de1 |
BIGNUM *b4;
|
|
Packit Service |
084de1 |
BIGNUM *b5;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
b1 = BN_new();
|
|
Packit Service |
084de1 |
b2 = BN_new();
|
|
Packit Service |
084de1 |
b3 = BN_new();
|
|
Packit Service |
084de1 |
b4 = BN_new();
|
|
Packit Service |
084de1 |
b5 = BN_new();
|
|
Packit Service |
084de1 |
ctx = BN_CTX_new();
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* Divide the input into three parts, using the values of the first two
|
|
Packit Service |
084de1 |
* bytes to choose lengths, which generate b1, b2 and b3. Use three bits
|
|
Packit Service |
084de1 |
* of the third byte to choose signs for the three numbers.
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
if (len > 2) {
|
|
Packit Service |
084de1 |
len -= 3;
|
|
Packit Service |
084de1 |
l1 = (buf[0] * len) / 255;
|
|
Packit Service |
084de1 |
++buf;
|
|
Packit Service |
084de1 |
l2 = (buf[0] * (len - l1)) / 255;
|
|
Packit Service |
084de1 |
++buf;
|
|
Packit Service |
084de1 |
l3 = len - l1 - l2;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
s1 = buf[0] & 1;
|
|
Packit Service |
084de1 |
s3 = buf[0] & 4;
|
|
Packit Service |
084de1 |
++buf;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
OPENSSL_assert(BN_bin2bn(buf, l1, b1) == b1);
|
|
Packit Service |
084de1 |
BN_set_negative(b1, s1);
|
|
Packit Service |
084de1 |
OPENSSL_assert(BN_bin2bn(buf + l1, l2, b2) == b2);
|
|
Packit Service |
084de1 |
OPENSSL_assert(BN_bin2bn(buf + l1 + l2, l3, b3) == b3);
|
|
Packit Service |
084de1 |
BN_set_negative(b3, s3);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* mod 0 is undefined */
|
|
Packit Service |
084de1 |
if (BN_is_zero(b3)) {
|
|
Packit Service |
084de1 |
success = 1;
|
|
Packit Service |
084de1 |
goto done;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
OPENSSL_assert(BN_mod_exp(b4, b1, b2, b3, ctx));
|
|
Packit Service |
084de1 |
OPENSSL_assert(BN_mod_exp_simple(b5, b1, b2, b3, ctx));
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
success = BN_cmp(b4, b5) == 0;
|
|
Packit Service |
084de1 |
if (!success) {
|
|
Packit Service |
084de1 |
BN_print_fp(stdout, b1);
|
|
Packit Service |
084de1 |
putchar('\n');
|
|
Packit Service |
084de1 |
BN_print_fp(stdout, b2);
|
|
Packit Service |
084de1 |
putchar('\n');
|
|
Packit Service |
084de1 |
BN_print_fp(stdout, b3);
|
|
Packit Service |
084de1 |
putchar('\n');
|
|
Packit Service |
084de1 |
BN_print_fp(stdout, b4);
|
|
Packit Service |
084de1 |
putchar('\n');
|
|
Packit Service |
084de1 |
BN_print_fp(stdout, b5);
|
|
Packit Service |
084de1 |
putchar('\n');
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
done:
|
|
Packit Service |
084de1 |
OPENSSL_assert(success);
|
|
Packit Service |
084de1 |
BN_free(b1);
|
|
Packit Service |
084de1 |
BN_free(b2);
|
|
Packit Service |
084de1 |
BN_free(b3);
|
|
Packit Service |
084de1 |
BN_free(b4);
|
|
Packit Service |
084de1 |
BN_free(b5);
|
|
Packit Service |
084de1 |
BN_CTX_free(ctx);
|
|
Packit Service |
084de1 |
ERR_clear_error();
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
void FuzzerCleanup(void)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
}
|