Skip to content

Commit

Permalink
Bug 1334106 - split mpi target, r=ttaubert
Browse files Browse the repository at this point in the history
Differential Revision: https://nss-review.dev.mozaws.net/D189

--HG--
extra : rebase_source : 30bd247a2eb5023e3ce5438b3f32d73ac3752065
extra : histedit_source : 3e3642f16fe9526943b563560468e4944bf37ff6
  • Loading branch information
franziskuskiefer committed Feb 8, 2017
1 parent 4fc4896 commit 45ad819
Show file tree
Hide file tree
Showing 14 changed files with 667 additions and 195 deletions.
157 changes: 139 additions & 18 deletions fuzz/fuzz.gyp
Expand Up @@ -65,6 +65,33 @@
}]
],
},
{
'target_name': 'nssfuzz-mpi-base',
'type': 'none',
'dependencies': [
'<(DEPTH)/exports.gyp:nss_exports',
'fuzz_base',
],
'direct_dependent_settings': {
'include_dirs': [
'<(DEPTH)/lib/freebl/mpi',
],
'sources': [
'mpi_helper.cc',
],
'conditions': [
[ 'fuzz_oss==1', {
'libraries': [
'/usr/lib/x86_64-linux-gnu/libcrypto.a',
],
}, {
'libraries': [
'-lcrypto',
],
}],
],
},
},
{
'target_name': 'nssfuzz-pkcs8',
'type': 'executable',
Expand Down Expand Up @@ -101,39 +128,124 @@
],
},
{
'target_name': 'nssfuzz-mpi',
'target_name': 'nssfuzz-certDN',
'type': 'executable',
'sources': [
'mpi_target.cc',
'certDN_target.cc',
],
'dependencies': [
'<(DEPTH)/exports.gyp:nss_exports',
'fuzz_base',
],
'conditions': [
[ 'fuzz_oss==1', {
'libraries': [
'/usr/lib/x86_64-linux-gnu/libcrypto.a',
],
}, {
'libraries': [
'-lcrypto',
],
}],
},
{
'target_name': 'nssfuzz-mpi-add',
'type': 'executable',
'sources': [
'mpi_add_target.cc',
],
'include_dirs': [
'<(DEPTH)/lib/freebl/mpi',
'dependencies': [
'<(DEPTH)/exports.gyp:nss_exports',
'nssfuzz-mpi-base',
],
},
{
'target_name': 'nssfuzz-certDN',
'target_name': 'nssfuzz-mpi-sub',
'type': 'executable',
'sources': [
'certDN_target.cc',
'mpi_sub_target.cc',
],
'dependencies': [
'<(DEPTH)/exports.gyp:nss_exports',
'fuzz_base',
'nssfuzz-mpi-base',
],
},
{
'target_name': 'nssfuzz-mpi-sqr',
'type': 'executable',
'sources': [
'mpi_sqr_target.cc',
],
'dependencies': [
'<(DEPTH)/exports.gyp:nss_exports',
'nssfuzz-mpi-base',
],
},
{
'target_name': 'nssfuzz-mpi-div',
'type': 'executable',
'sources': [
'mpi_div_target.cc',
],
'dependencies': [
'<(DEPTH)/exports.gyp:nss_exports',
'nssfuzz-mpi-base',
],
},
{
'target_name': 'nssfuzz-mpi-mod',
'type': 'executable',
'sources': [
'mpi_mod_target.cc',
],
'dependencies': [
'<(DEPTH)/exports.gyp:nss_exports',
'nssfuzz-mpi-base',
],
},
{
'target_name': 'nssfuzz-mpi-sqrmod',
'type': 'executable',
'sources': [
'mpi_sqrmod_target.cc',
],
'dependencies': [
'<(DEPTH)/exports.gyp:nss_exports',
'nssfuzz-mpi-base',
],
},
{
'target_name': 'nssfuzz-mpi-addmod',
'type': 'executable',
'sources': [
'mpi_addmod_target.cc',
],
'dependencies': [
'<(DEPTH)/exports.gyp:nss_exports',
'nssfuzz-mpi-base',
],
},
{
'target_name': 'nssfuzz-mpi-submod',
'type': 'executable',
'sources': [
'mpi_submod_target.cc',
],
'dependencies': [
'<(DEPTH)/exports.gyp:nss_exports',
'nssfuzz-mpi-base',
],
},
{
'target_name': 'nssfuzz-mpi-mulmod',
'type': 'executable',
'sources': [
'mpi_mulmod_target.cc',
],
'dependencies': [
'<(DEPTH)/exports.gyp:nss_exports',
'nssfuzz-mpi-base',
],
},
{
'target_name': 'nssfuzz-mpi-expmod',
'type': 'executable',
'sources': [
'mpi_expmod_target.cc',
],
'dependencies': [
'<(DEPTH)/exports.gyp:nss_exports',
'nssfuzz-mpi-base',
],
},
{
Expand All @@ -148,7 +260,16 @@
'conditions': [
['OS=="linux"', {
'dependencies': [
'nssfuzz-mpi',
'nssfuzz-mpi-add',
'nssfuzz-mpi-addmod',
'nssfuzz-mpi-div',
'nssfuzz-mpi-expmod',
'nssfuzz-mpi-mod',
'nssfuzz-mpi-mulmod',
'nssfuzz-mpi-sqr',
'nssfuzz-mpi-sqrmod',
'nssfuzz-mpi-sub',
'nssfuzz-mpi-submod',
],
}],
],
Expand Down
42 changes: 42 additions & 0 deletions fuzz/mpi_add_target.cc
@@ -0,0 +1,42 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/*
* This target fuzzes NSS mpi against openssl bignum.
* It therefore requires openssl to be installed.
*/

#include "mpi_helper.h"

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
// We require at least size 3 to get two integers from Data.
if (size < 3) {
return 0;
}
INIT_NUMBERS

// Compare with OpenSSL addition
assert(mp_add(&a, &b, &c) == MP_OKAY);
(void)BN_add(C, A, B);
check_equal(C, &c, max_size);

// Check a + b == a - -b
mp_neg(&b, &b);
assert(mp_sub(&a, &b, &r) == MP_OKAY);
bool eq = mp_cmp(&r, &c) == 0;
if (!eq) {
char rC[max_size], cC[max_size], aC[max_size], bC[max_size];
mp_tohex(&r, rC);
mp_tohex(&c, cC);
mp_tohex(&a, aC);
mp_tohex(&b, bC);
std::cout << "a = " << std::hex << aC << std::endl;
std::cout << "-b = " << std::hex << bC << std::endl;
std::cout << "a + b = " << std::hex << cC << std::endl;
std::cout << "a - -b = " << std::hex << rC << std::endl;
}
assert(eq);

CLEANUP_AND_RETURN
}
27 changes: 27 additions & 0 deletions fuzz/mpi_addmod_target.cc
@@ -0,0 +1,27 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/*
* This target fuzzes NSS mpi against openssl bignum.
* It therefore requires openssl to be installed.
*/

#include "mpi_helper.h"

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
// We require at least size 3 to get two integers from Data.
if (size < 3) {
return 0;
}
INIT_NUMBERS

auto modulus = get_modulus(data, size, ctx);
// Compare with OpenSSL add mod
m1 = &std::get<1>(modulus);
assert(mp_addmod(&a, &b, m1, &c) == MP_OKAY);
(void)BN_mod_add(C, A, B, std::get<0>(modulus), ctx);
check_equal(C, &c, max_size);

CLEANUP_AND_RETURN
}
36 changes: 36 additions & 0 deletions fuzz/mpi_div_target.cc
@@ -0,0 +1,36 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/*
* This target fuzzes NSS mpi against openssl bignum.
* It therefore requires openssl to be installed.
*/

#include "mpi_helper.h"

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
// We require at least size 3 to get two integers from Data.
if (size < 3) {
return 0;
}
INIT_NUMBERS

// We can't divide by 0.
if (mp_cmp_z(&b) == 0) {
CLEANUP_AND_RETURN
}

// Compare with OpenSSL division
assert(mp_div(&a, &b, &c, &r) == MP_OKAY);
BN_div(C, R, A, B, ctx);
check_equal(C, &c, max_size);
check_equal(R, &r, max_size);

// Check c * b + r == a
assert(mp_mul(&c, &b, &c) == MP_OKAY);
assert(mp_add(&c, &r, &c) == MP_OKAY);
assert(mp_cmp(&c, &a) == 0);

CLEANUP_AND_RETURN
}
27 changes: 27 additions & 0 deletions fuzz/mpi_expmod_target.cc
@@ -0,0 +1,27 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/*
* This target fuzzes NSS mpi against openssl bignum.
* It therefore requires openssl to be installed.
*/

#include "mpi_helper.h"

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
// We require at least size 3 to get two integers from Data.
if (size < 3) {
return 0;
}
INIT_NUMBERS

auto modulus = get_modulus(data, size, ctx);
// Compare with OpenSSL exp mod
m1 = &std::get<1>(modulus);
assert(mp_exptmod(&a, &b, m1, &c) == MP_OKAY);
(void)BN_mod_exp(C, A, B, std::get<0>(modulus), ctx);
check_equal(C, &c, 2 * max_size);

CLEANUP_AND_RETURN
}

0 comments on commit 45ad819

Please sign in to comment.