aboutsummaryrefslogtreecommitdiffstats
path: root/src/math
diff options
context:
space:
mode:
Diffstat (limited to 'src/math')
-rw-r--r--src/math/numbertheory/blinding.cpp49
-rw-r--r--src/math/numbertheory/blinding.h34
-rw-r--r--src/math/numbertheory/info.txt2
-rw-r--r--src/math/numbertheory/make_prm.cpp3
-rw-r--r--src/math/numbertheory/point_gfp.cpp29
-rw-r--r--src/math/numbertheory/point_gfp.h7
-rw-r--r--src/math/numbertheory/pow_mod.cpp53
-rw-r--r--src/math/numbertheory/pow_mod.h7
-rw-r--r--src/math/numbertheory/powm_fw.cpp40
-rw-r--r--src/math/numbertheory/powm_mnt.cpp34
-rw-r--r--src/math/numbertheory/reducer.h2
11 files changed, 86 insertions, 174 deletions
diff --git a/src/math/numbertheory/blinding.cpp b/src/math/numbertheory/blinding.cpp
deleted file mode 100644
index c6a3fd1bd..000000000
--- a/src/math/numbertheory/blinding.cpp
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
-* Blinder
-* (C) 1999-2007 Jack Lloyd
-*
-* Distributed under the terms of the Botan license
-*/
-
-#include <botan/blinding.h>
-#include <botan/numthry.h>
-
-namespace Botan {
-
-/*
-* Blinder Constructor
-*/
-Blinder::Blinder(const BigInt& e, const BigInt& d, const BigInt& n)
- {
- if(e < 1 || d < 1 || n < 1)
- throw Invalid_Argument("Blinder: Arguments too small");
-
- reducer = Modular_Reducer(n);
- this->e = e;
- this->d = d;
- }
-
-/*
-* Blind a number
-*/
-BigInt Blinder::blind(const BigInt& i) const
- {
- if(!reducer.initialized())
- return i;
-
- e = reducer.square(e);
- d = reducer.square(d);
- return reducer.multiply(i, e);
- }
-
-/*
-* Unblind a number
-*/
-BigInt Blinder::unblind(const BigInt& i) const
- {
- if(!reducer.initialized())
- return i;
- return reducer.multiply(i, d);
- }
-
-}
diff --git a/src/math/numbertheory/blinding.h b/src/math/numbertheory/blinding.h
deleted file mode 100644
index 5f7f9e6b7..000000000
--- a/src/math/numbertheory/blinding.h
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
-* Blinder
-* (C) 1999-2007 Jack Lloyd
-*
-* Distributed under the terms of the Botan license
-*/
-
-#ifndef BOTAN_BLINDER_H__
-#define BOTAN_BLINDER_H__
-
-#include <botan/bigint.h>
-#include <botan/reducer.h>
-
-namespace Botan {
-
-/*
-* Blinding Function Object
-*/
-class BOTAN_DLL Blinder
- {
- public:
- BigInt blind(const BigInt&) const;
- BigInt unblind(const BigInt&) const;
-
- Blinder() {}
- Blinder(const BigInt&, const BigInt&, const BigInt&);
- private:
- Modular_Reducer reducer;
- mutable BigInt e, d;
- };
-
-}
-
-#endif
diff --git a/src/math/numbertheory/info.txt b/src/math/numbertheory/info.txt
index 58851e055..18349ef78 100644
--- a/src/math/numbertheory/info.txt
+++ b/src/math/numbertheory/info.txt
@@ -3,7 +3,6 @@ load_on auto
define BIGINT_MATH
<header:public>
-blinding.h
curve_gfp.h
numthry.h
point_gfp.h
@@ -16,7 +15,6 @@ def_powm.h
</header:internal>
<source>
-blinding.cpp
dsa_gen.cpp
jacobi.cpp
make_prm.cpp
diff --git a/src/math/numbertheory/make_prm.cpp b/src/math/numbertheory/make_prm.cpp
index 3eb01cd42..23b8cf549 100644
--- a/src/math/numbertheory/make_prm.cpp
+++ b/src/math/numbertheory/make_prm.cpp
@@ -38,6 +38,9 @@ BigInt random_prime(RandomNumberGenerator& rng,
while(true)
{
BigInt p(rng, bits);
+
+ // Force lowest and two top bits on
+ p.set_bit(bits - 1);
p.set_bit(bits - 2);
p.set_bit(0);
diff --git a/src/math/numbertheory/point_gfp.cpp b/src/math/numbertheory/point_gfp.cpp
index bed08eb39..db422c8aa 100644
--- a/src/math/numbertheory/point_gfp.cpp
+++ b/src/math/numbertheory/point_gfp.cpp
@@ -202,7 +202,7 @@ void PointGFp::mult2()
BigInt PointGFp::get_affine_x() const
{
if(is_zero())
- throw Illegal_Transformation("cannot convert to affine");
+ throw Illegal_Transformation("Cannot convert zero point to affine");
const Modular_Reducer& mod_p = curve.mod_p();
@@ -213,7 +213,7 @@ BigInt PointGFp::get_affine_x() const
BigInt PointGFp::get_affine_y() const
{
if(is_zero())
- throw Illegal_Transformation("cannot convert to affine");
+ throw Illegal_Transformation("Cannot convert zero point to affine");
const Modular_Reducer& mod_p = curve.mod_p();
@@ -326,38 +326,39 @@ SecureVector<byte> EC2OSP(const PointGFp& point, byte format)
throw Invalid_Argument("illegal point encoding format specification");
}
-PointGFp OS2ECP(const MemoryRegion<byte>& os, const CurveGFp& curve)
+PointGFp OS2ECP(const byte data[], u32bit data_len,
+ const CurveGFp& curve)
{
- if(os.size() == 1 && os[0] == 0)
+ if(data_len <= 1)
return PointGFp(curve); // return zero
- const byte pc = os[0];
+ const byte pc = data[0];
BigInt x, y;
if(pc == 2 || pc == 3)
{
//compressed form
- x = BigInt::decode(&os[1], os.size() - 1);
+ x = BigInt::decode(&data[1], data_len - 1);
bool yMod2 = ((pc & 0x01) == 1);
y = decompress_point(yMod2, x, curve);
}
else if(pc == 4)
{
- // uncompressed form
- u32bit l = (os.size() - 1) / 2;
+ const u32bit l = (data_len - 1) / 2;
- x = BigInt::decode(&os[1], l);
- y = BigInt::decode(&os[l+1], l);
+ // uncompressed form
+ x = BigInt::decode(&data[1], l);
+ y = BigInt::decode(&data[l+1], l);
}
else if(pc == 6 || pc == 7)
{
- // hybrid form
- u32bit l = (os.size() - 1) / 2;
+ const u32bit l = (data_len - 1) / 2;
- x = BigInt::decode(&os[1], l);
- y = BigInt::decode(&os[l+1], l);
+ // hybrid form
+ x = BigInt::decode(&data[1], l);
+ y = BigInt::decode(&data[l+1], l);
bool yMod2 = ((pc & 0x01) == 1);
diff --git a/src/math/numbertheory/point_gfp.h b/src/math/numbertheory/point_gfp.h
index 3bb763d44..2f31421fc 100644
--- a/src/math/numbertheory/point_gfp.h
+++ b/src/math/numbertheory/point_gfp.h
@@ -219,7 +219,12 @@ inline PointGFp operator*(const PointGFp& point, const BigInt& scalar)
// encoding and decoding
SecureVector<byte> BOTAN_DLL EC2OSP(const PointGFp& point, byte format);
-PointGFp BOTAN_DLL OS2ECP(const MemoryRegion<byte>& os, const CurveGFp& curve);
+
+PointGFp BOTAN_DLL OS2ECP(const byte data[], u32bit data_len,
+ const CurveGFp& curve);
+
+inline PointGFp OS2ECP(const MemoryRegion<byte>& data, const CurveGFp& curve)
+ { return OS2ECP(&data[0], data.size(), curve); }
}
diff --git a/src/math/numbertheory/pow_mod.cpp b/src/math/numbertheory/pow_mod.cpp
index 8d6bac699..5ab5638ea 100644
--- a/src/math/numbertheory/pow_mod.cpp
+++ b/src/math/numbertheory/pow_mod.cpp
@@ -6,7 +6,8 @@
*/
#include <botan/pow_mod.h>
-#include <botan/internal/pk_engine.h>
+#include <botan/libstate.h>
+#include <botan/engine.h>
namespace Botan {
@@ -55,7 +56,23 @@ Power_Mod::~Power_Mod()
void Power_Mod::set_modulus(const BigInt& n, Usage_Hints hints) const
{
delete core;
- core = ((n == 0) ? 0 : Engine_Core::mod_exp(n, hints));
+ core = 0;
+
+ if(n != 0)
+ {
+ Algorithm_Factory::Engine_Iterator i(global_state().algorithm_factory());
+
+ while(const Engine* engine = i.next())
+ {
+ core = engine->mod_exp(n, hints);
+
+ if(core)
+ break;
+ }
+
+ if(!core)
+ throw Lookup_Error("Power_Mod: Unable to find a working engine");
+ }
}
/*
@@ -94,6 +111,38 @@ BigInt Power_Mod::execute() const
return core->execute();
}
+/*
+* Try to choose a good window size
+*/
+u32bit Power_Mod::window_bits(u32bit exp_bits, u32bit,
+ Power_Mod::Usage_Hints hints)
+ {
+ static const u32bit wsize[][2] = {
+ { 2048, 7 }, { 1024, 6 }, { 256, 5 }, { 128, 4 }, { 64, 3 }, { 0, 0 }
+ };
+
+ u32bit window_bits = 1;
+
+ if(exp_bits)
+ {
+ for(u32bit j = 0; wsize[j][0]; ++j)
+ {
+ if(exp_bits >= wsize[j][0])
+ {
+ window_bits += wsize[j][1];
+ break;
+ }
+ }
+ }
+
+ if(hints & Power_Mod::BASE_IS_FIXED)
+ window_bits += 2;
+ if(hints & Power_Mod::EXP_IS_LARGE)
+ ++window_bits;
+
+ return window_bits;
+ }
+
namespace {
/*
diff --git a/src/math/numbertheory/pow_mod.h b/src/math/numbertheory/pow_mod.h
index 6952dcd1b..7b92f0ec4 100644
--- a/src/math/numbertheory/pow_mod.h
+++ b/src/math/numbertheory/pow_mod.h
@@ -31,6 +31,7 @@ class BOTAN_DLL Modular_Exponentiator
class BOTAN_DLL Power_Mod
{
public:
+
enum Usage_Hints {
NO_HINTS = 0x0000,
@@ -44,6 +45,12 @@ class BOTAN_DLL Power_Mod
EXP_IS_LARGE = 0x0400
};
+ /*
+ * Try to choose a good window size
+ */
+ static u32bit window_bits(u32bit exp_bits, u32bit base_bits,
+ Power_Mod::Usage_Hints hints);
+
void set_modulus(const BigInt&, Usage_Hints = NO_HINTS) const;
void set_base(const BigInt&) const;
void set_exponent(const BigInt&) const;
diff --git a/src/math/numbertheory/powm_fw.cpp b/src/math/numbertheory/powm_fw.cpp
index 1cfcdcd66..68dabc332 100644
--- a/src/math/numbertheory/powm_fw.cpp
+++ b/src/math/numbertheory/powm_fw.cpp
@@ -11,44 +11,6 @@
namespace Botan {
-namespace {
-
-/*
-* Try to choose a good window size
-*/
-u32bit fw_powm_window_bits(u32bit exp_bits, u32bit,
- Power_Mod::Usage_Hints hints)
- {
- static const u32bit wsize[][2] = {
- { 2048, 7 }, { 1024, 6 }, { 256, 5 }, { 128, 4 }, { 64, 3 }, { 0, 0 }
- };
-
- u32bit window_bits = 3;
-
- if(exp_bits)
- {
- for(u32bit j = 0; wsize[j][0]; ++j)
- {
- if(exp_bits >= wsize[j][0])
- {
- window_bits += wsize[j][1];
- break;
- }
- }
- }
-
- if(hints & Power_Mod::EXP_IS_FIXED)
- window_bits += 2;
- if(hints & Power_Mod::EXP_IS_LARGE)
- window_bits += 2;
- if(hints & Power_Mod::BASE_IS_FIXED)
- ++window_bits;
-
- return window_bits;
- }
-
-}
-
/*
* Set the exponent
*/
@@ -62,7 +24,7 @@ void Fixed_Window_Exponentiator::set_exponent(const BigInt& e)
*/
void Fixed_Window_Exponentiator::set_base(const BigInt& base)
{
- window_bits = fw_powm_window_bits(exp.bits(), base.bits(), hints);
+ window_bits = Power_Mod::window_bits(exp.bits(), base.bits(), hints);
g.resize((1 << window_bits) - 1);
g[0] = base;
diff --git a/src/math/numbertheory/powm_mnt.cpp b/src/math/numbertheory/powm_mnt.cpp
index 2d18ccdef..cce142020 100644
--- a/src/math/numbertheory/powm_mnt.cpp
+++ b/src/math/numbertheory/powm_mnt.cpp
@@ -14,38 +14,6 @@ namespace Botan {
namespace {
/*
-* Try to choose a good window size
-*/
-u32bit montgomery_powm_window_bits(u32bit exp_bits, u32bit,
- Power_Mod::Usage_Hints hints)
- {
- static const u32bit wsize[][2] = {
- { 2048, 4 }, { 1024, 3 }, { 256, 2 }, { 128, 1 }, { 0, 0 }
- };
-
- u32bit window_bits = 1;
-
- if(exp_bits)
- {
- for(u32bit j = 0; wsize[j][0]; ++j)
- {
- if(exp_bits >= wsize[j][0])
- {
- window_bits += wsize[j][1];
- break;
- }
- }
- }
-
- if(hints & Power_Mod::BASE_IS_FIXED)
- window_bits += 2;
- if(hints & Power_Mod::EXP_IS_LARGE)
- ++window_bits;
-
- return window_bits;
- }
-
-/*
* Montgomery Reduction
*/
inline void montgomery_reduce(BigInt& out, MemoryRegion<word>& z_buf,
@@ -76,7 +44,7 @@ void Montgomery_Exponentiator::set_exponent(const BigInt& exp)
*/
void Montgomery_Exponentiator::set_base(const BigInt& base)
{
- window_bits = montgomery_powm_window_bits(exp.bits(), base.bits(), hints);
+ window_bits = Power_Mod::window_bits(exp.bits(), base.bits(), hints);
g.resize((1 << window_bits) - 1);
diff --git a/src/math/numbertheory/reducer.h b/src/math/numbertheory/reducer.h
index 80c0f27e1..c121f1499 100644
--- a/src/math/numbertheory/reducer.h
+++ b/src/math/numbertheory/reducer.h
@@ -18,6 +18,8 @@ namespace Botan {
class BOTAN_DLL Modular_Reducer
{
public:
+ const BigInt& get_modulus() const { return modulus; }
+
BigInt reduce(const BigInt& x) const;
/**