Botan  1.11.15
src/lib/math/ec_gfp/curve_nistp.h
Go to the documentation of this file.
00001 /*
00002 * NIST elliptic curves over GF(p)
00003 * (C) 2014 Jack Lloyd
00004 *
00005 * Botan is released under the Simplified BSD License (see license.txt)
00006 */
00007 
00008 #ifndef BOTAN_GFP_CURVE_NIST_H__
00009 #define BOTAN_GFP_CURVE_NIST_H__
00010 
00011 #include <botan/curve_gfp.h>
00012 #include <memory>
00013 
00014 namespace Botan {
00015 
00016 class CurveGFp_NIST : public CurveGFp_Repr
00017    {
00018    public:
00019       CurveGFp_NIST(size_t p_bits, const BigInt& a, const BigInt& b) :
00020          m_a(a), m_b(b), m_p_words((p_bits + BOTAN_MP_WORD_BITS - 1) / BOTAN_MP_WORD_BITS)
00021          {
00022          }
00023 
00024       size_t get_p_words() const override { return m_p_words; }
00025 
00026       const BigInt& get_a() const override { return m_a; }
00027 
00028       const BigInt& get_b() const override { return m_b; }
00029 
00030       const BigInt& get_a_rep() const override { return m_a; }
00031 
00032       const BigInt& get_b_rep() const override { return m_b; }
00033 
00034       void to_curve_rep(BigInt& x, secure_vector<word>& ws) const override
00035          { redc(x, ws); }
00036 
00037       void from_curve_rep(BigInt& x, secure_vector<word>& ws) const override
00038          { redc(x, ws); }
00039 
00040       void curve_mul(BigInt& z, const BigInt& x, const BigInt& y,
00041                      secure_vector<word>& ws) const override;
00042 
00043       void curve_sqr(BigInt& z, const BigInt& x,
00044                      secure_vector<word>& ws) const override;
00045    private:
00046       virtual void redc(BigInt& x, secure_vector<word>& ws) const = 0;
00047 
00048       virtual size_t max_redc_subtractions() const = 0;
00049 
00050       // Curve parameters
00051       BigInt m_a, m_b;
00052       size_t m_p_words; // cache of m_p.sig_words()
00053    };
00054 
00055 #if (BOTAN_MP_WORD_BITS == 32) || (BOTAN_MP_WORD_BITS == 64)
00056 
00057 #define BOTAN_HAS_CURVEGFP_NISTP_M32
00058 
00059 /**
00060 * The NIST P-192 curve
00061 */
00062 class CurveGFp_P192 : public CurveGFp_NIST
00063    {
00064    public:
00065       CurveGFp_P192(const BigInt& a, const BigInt& b) : CurveGFp_NIST(192, a, b) {}
00066 
00067       static const BigInt& prime();
00068 
00069       const BigInt& get_p() const override { return CurveGFp_P192::prime(); }
00070 
00071    private:
00072       void redc(BigInt& x, secure_vector<word>& ws) const override;
00073 
00074       size_t max_redc_subtractions() const override { return 3; }
00075    };
00076 
00077 /**
00078 * The NIST P-224 curve
00079 */
00080 class CurveGFp_P224 : public CurveGFp_NIST
00081    {
00082    public:
00083       CurveGFp_P224(const BigInt& a, const BigInt& b) : CurveGFp_NIST(224, a, b) {}
00084 
00085       static const BigInt& prime();
00086 
00087       const BigInt& get_p() const override { return CurveGFp_P224::prime(); }
00088    private:
00089       void redc(BigInt& x, secure_vector<word>& ws) const override;
00090 
00091       size_t max_redc_subtractions() const override { return 3; }
00092    };
00093 
00094 /**
00095 * The NIST P-256 curve
00096 */
00097 class CurveGFp_P256 : public CurveGFp_NIST
00098    {
00099    public:
00100       CurveGFp_P256(const BigInt& a, const BigInt& b) : CurveGFp_NIST(256, a, b) {}
00101 
00102       static const BigInt& prime();
00103 
00104       const BigInt& get_p() const override { return CurveGFp_P256::prime(); }
00105 
00106    private:
00107       void redc(BigInt& x, secure_vector<word>& ws) const override;
00108 
00109       size_t max_redc_subtractions() const override { return 10; }
00110    };
00111 
00112 /**
00113 * The NIST P-384 curve
00114 */
00115 class CurveGFp_P384 : public CurveGFp_NIST
00116    {
00117    public:
00118       CurveGFp_P384(const BigInt& a, const BigInt& b) : CurveGFp_NIST(384, a, b) {}
00119 
00120       static const BigInt& prime();
00121 
00122       const BigInt& get_p() const override { return CurveGFp_P384::prime(); }
00123 
00124    private:
00125       void redc(BigInt& x, secure_vector<word>& ws) const override;
00126 
00127       size_t max_redc_subtractions() const override { return 4; }
00128    };
00129 
00130 #endif
00131 
00132 /**
00133 * The NIST P-521 curve
00134 */
00135 class CurveGFp_P521 : public CurveGFp_NIST
00136    {
00137    public:
00138       CurveGFp_P521(const BigInt& a, const BigInt& b) : CurveGFp_NIST(521, a, b) {}
00139 
00140       static const BigInt& prime();
00141 
00142       const BigInt& get_p() const override { return CurveGFp_P521::prime(); }
00143 
00144    private:
00145       void redc(BigInt& x, secure_vector<word>& ws) const override;
00146 
00147       size_t max_redc_subtractions() const override { return 1; }
00148    };
00149 
00150 }
00151 
00152 #endif