Skip to content

Commit

Permalink
Merge branch 'master' of github.com:randombit/botan
Browse files Browse the repository at this point in the history
  • Loading branch information
fwsGonzo committed Apr 11, 2018
2 parents f25d5d1 + c6a90da commit 4844c15
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/lib/entropy/dev_random/dev_random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <sys/select.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>

namespace Botan {
Expand Down
10 changes: 9 additions & 1 deletion src/lib/pubkey/ec_group/ec_group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class EC_Group_Data final
m_oid(oid),
m_p_bits(p.bits()),
m_order_bits(order.bits()),
m_a_is_minus_3(a == p - 3)
m_a_is_minus_3(a == p - 3),
m_a_is_zero(a.is_zero())
{
}

Expand Down Expand Up @@ -79,6 +80,7 @@ class EC_Group_Data final
const PointGFp& base_point() const { return m_base_point; }

bool a_is_minus_3() const { return m_a_is_minus_3; }
bool a_is_zero() const { return m_a_is_zero; }

BigInt mod_order(const BigInt& x) const { return m_mod_order.reduce(x); }

Expand Down Expand Up @@ -108,6 +110,7 @@ class EC_Group_Data final
size_t m_p_bits;
size_t m_order_bits;
bool m_a_is_minus_3;
bool m_a_is_zero;
};

class EC_Group_Data_Map final
Expand Down Expand Up @@ -399,6 +402,11 @@ bool EC_Group::a_is_minus_3() const
return data().a_is_minus_3();
}

bool EC_Group::a_is_zero() const
{
return data().a_is_zero();
}

size_t EC_Group::get_p_bits() const
{
return data().p_bits();
Expand Down
5 changes: 5 additions & 0 deletions src/lib/pubkey/ec_group/ec_group.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ class BOTAN_PUBLIC_API(2,0) EC_Group final
*/
bool a_is_minus_3() const;

/**
* Return if a == 0 mod p
*/
bool a_is_zero() const;

/**
* Return the size of p in bits (same as get_p().bits())
*/
Expand Down
9 changes: 8 additions & 1 deletion src/tests/test_filters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,14 @@ class Filter_Tests final : public Test
Test::Result result("DataSinkFlush");

#if defined(BOTAN_HAS_CODEC_FILTERS) && defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
const std::string tmp_name("botan_test_data_src_sink_flush.tmp");

const std::string tmp_name = Test::temp_file_name("botan_test_data_src_sink_flush.tmp");
if(tmp_name.empty())
{
result.test_failure("Failed to create temporary file");
return result;
}

std::ofstream outfile(tmp_name);

Botan::Pipe pipe(new Botan::Hex_Decoder, new Botan::DataSink_Stream(outfile));
Expand Down
32 changes: 32 additions & 0 deletions src/tests/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
#include <botan/point_gfp.h>
#endif

#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
#include <stdlib.h>
#include <unistd.h>
#endif

namespace Botan_Tests {

Test::Registration::Registration(const std::string& name, Test* test)
Expand Down Expand Up @@ -495,6 +500,33 @@ Test* Test::get_test(const std::string& test_name)
return nullptr;
}

//static
std::string Test::temp_file_name(const std::string& basename)
{
// TODO add a --tmp-dir option to the tests to specify where these files go

#if defined(BOTAN_TARGET_OS_HAS_POSIX1)

// POSIX only calls for 6 'X' chars but OpenBSD allows arbitrary amount
std::string mkstemp_basename = "/tmp/" + basename + ".XXXXXXXXXX";

int fd = ::mkstemp(&mkstemp_basename[0]);

// error
if(fd < 0)
{
return "";
}

::close(fd);

return mkstemp_basename;
#else
// For now just create the temp in the current working directory
return basename;
#endif
}

std::string Test::read_data_file(const std::string& path)
{
const std::string fsname = Test::data_file(path);
Expand Down
2 changes: 2 additions & 0 deletions src/tests/tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,8 @@ class Test
static const std::string& data_dir() { return m_opts.data_dir(); }
static const std::string& pkcs11_lib() { return m_opts.pkcs11_lib(); }

static std::string temp_file_name(const std::string& basename);

static std::vector<std::string> provider_filter(const std::vector<std::string>& providers);

static std::string read_data_file(const std::string& path);
Expand Down
5 changes: 5 additions & 0 deletions src/tests/unit_ecc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,11 @@ class EC_Group_Tests : public Test
else
result.test_ne("Group " + group_name + " A does not equal -3", group.get_a(), group.get_p() - 3);

if(group.a_is_zero())
result.test_eq("Group A is zero", group.get_a(), BigInt(0));
else
result.test_ne("Group " + group_name + " A does not equal zero", group.get_a(), BigInt(0));

// get a valid point
Botan::PointGFp p = group.get_base_point() * Test::rng().next_nonzero_byte();

Expand Down

0 comments on commit 4844c15

Please sign in to comment.