aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/math/ec_gfp/curve_nistp.h
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2015-08-08 12:41:26 -0400
committerJack Lloyd <[email protected]>2015-08-08 12:41:26 -0400
commit549aaeccca01671ca94b422fe589c772349983ff (patch)
tree27bb784b27c7ac85717b9a3ff7cda4d8ea6c4523 /src/lib/math/ec_gfp/curve_nistp.h
parent63c1958b841d26184c526b54c531b0188c34ab0a (diff)
Expose the NIST prime values and reduction operations as plain functions.
Previously they were hidden away as private functions on the CurveGFp types. This allows directly testing the reduction functions against other computational methods.
Diffstat (limited to 'src/lib/math/ec_gfp/curve_nistp.h')
-rw-r--r--src/lib/math/ec_gfp/curve_nistp.h152
1 files changed, 23 insertions, 129 deletions
diff --git a/src/lib/math/ec_gfp/curve_nistp.h b/src/lib/math/ec_gfp/curve_nistp.h
index 0bf707f58..e7af69964 100644
--- a/src/lib/math/ec_gfp/curve_nistp.h
+++ b/src/lib/math/ec_gfp/curve_nistp.h
@@ -1,152 +1,46 @@
/*
-* NIST elliptic curves over GF(p)
-* (C) 2014 Jack Lloyd
+* Arithmetic operations specialized for NIST ECC primes
+* (C) 2014,2015 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
-#ifndef BOTAN_GFP_CURVE_NIST_H__
-#define BOTAN_GFP_CURVE_NIST_H__
+#ifndef BOTAN_NIST_PRIMES_H__
+#define BOTAN_NIST_PRIMES_H__
-#include <botan/curve_gfp.h>
-#include <memory>
+#include <botan/bigint.h>
namespace Botan {
-class CurveGFp_NIST : public CurveGFp_Repr
- {
- public:
- CurveGFp_NIST(size_t p_bits, const BigInt& a, const BigInt& b) :
- m_a(a), m_b(b), m_p_words((p_bits + BOTAN_MP_WORD_BITS - 1) / BOTAN_MP_WORD_BITS)
- {
- }
-
- size_t get_p_words() const override { return m_p_words; }
-
- const BigInt& get_a() const override { return m_a; }
-
- const BigInt& get_b() const override { return m_b; }
-
- const BigInt& get_a_rep() const override { return m_a; }
-
- const BigInt& get_b_rep() const override { return m_b; }
-
- void to_curve_rep(BigInt& x, secure_vector<word>& ws) const override
- { redc(x, ws); }
-
- void from_curve_rep(BigInt& x, secure_vector<word>& ws) const override
- { redc(x, ws); }
-
- void curve_mul(BigInt& z, const BigInt& x, const BigInt& y,
- secure_vector<word>& ws) const override;
-
- void curve_sqr(BigInt& z, const BigInt& x,
- secure_vector<word>& ws) const override;
- private:
- virtual void redc(BigInt& x, secure_vector<word>& ws) const = 0;
-
- virtual size_t max_redc_subtractions() const = 0;
-
- // Curve parameters
- BigInt m_a, m_b;
- size_t m_p_words; // cache of m_p.sig_words()
- };
-
-#if (BOTAN_MP_WORD_BITS == 32) || (BOTAN_MP_WORD_BITS == 64)
-
-#define BOTAN_HAS_CURVEGFP_NISTP_M32
-
-/**
-* The NIST P-192 curve
-*/
-class CurveGFp_P192 : public CurveGFp_NIST
- {
- public:
- CurveGFp_P192(const BigInt& a, const BigInt& b) : CurveGFp_NIST(192, a, b) {}
-
- static const BigInt& prime();
-
- const BigInt& get_p() const override { return CurveGFp_P192::prime(); }
-
- private:
- void redc(BigInt& x, secure_vector<word>& ws) const override;
-
- size_t max_redc_subtractions() const override { return 3; }
- };
-
/**
-* The NIST P-224 curve
-*/
-class CurveGFp_P224 : public CurveGFp_NIST
- {
- public:
- CurveGFp_P224(const BigInt& a, const BigInt& b) : CurveGFp_NIST(224, a, b) {}
-
- static const BigInt& prime();
-
- const BigInt& get_p() const override { return CurveGFp_P224::prime(); }
- private:
- void redc(BigInt& x, secure_vector<word>& ws) const override;
-
- size_t max_redc_subtractions() const override { return 3; }
- };
-
-/**
-* The NIST P-256 curve
+* NIST Prime reduction functions.
+*
+* Reduces the value in place
+*
+* ws is a workspace function which is used as a temporary,
+* and will be resized as needed.
*/
-class CurveGFp_P256 : public CurveGFp_NIST
- {
- public:
- CurveGFp_P256(const BigInt& a, const BigInt& b) : CurveGFp_NIST(256, a, b) {}
-
- static const BigInt& prime();
-
- const BigInt& get_p() const override { return CurveGFp_P256::prime(); }
-
- private:
- void redc(BigInt& x, secure_vector<word>& ws) const override;
+BOTAN_DLL const BigInt& prime_p521();
+BOTAN_DLL void redc_p521(BigInt& x, secure_vector<word>& ws);
- size_t max_redc_subtractions() const override { return 10; }
- };
+#if (BOTAN_MP_WORD_BITS == 32) || (BOTAN_MP_WORD_BITS == 64)
-/**
-* The NIST P-384 curve
-*/
-class CurveGFp_P384 : public CurveGFp_NIST
- {
- public:
- CurveGFp_P384(const BigInt& a, const BigInt& b) : CurveGFp_NIST(384, a, b) {}
+#define BOTAN_HAS_NIST_PRIME_REDUCERS_W32
- static const BigInt& prime();
+BOTAN_DLL const BigInt& prime_p384();
+BOTAN_DLL void redc_p384(BigInt& x, secure_vector<word>& ws);
- const BigInt& get_p() const override { return CurveGFp_P384::prime(); }
+BOTAN_DLL const BigInt& prime_p256();
+BOTAN_DLL void redc_p256(BigInt& x, secure_vector<word>& ws);
- private:
- void redc(BigInt& x, secure_vector<word>& ws) const override;
+BOTAN_DLL const BigInt& prime_p224();
+BOTAN_DLL void redc_p224(BigInt& x, secure_vector<word>& ws);
- size_t max_redc_subtractions() const override { return 4; }
- };
+BOTAN_DLL const BigInt& prime_p192();
+BOTAN_DLL void redc_p192(BigInt& x, secure_vector<word>& ws);
#endif
-/**
-* The NIST P-521 curve
-*/
-class CurveGFp_P521 : public CurveGFp_NIST
- {
- public:
- CurveGFp_P521(const BigInt& a, const BigInt& b) : CurveGFp_NIST(521, a, b) {}
-
- static const BigInt& prime();
-
- const BigInt& get_p() const override { return CurveGFp_P521::prime(); }
-
- private:
- void redc(BigInt& x, secure_vector<word>& ws) const override;
-
- size_t max_redc_subtractions() const override { return 1; }
- };
-
}
#endif