9 #include <botan/curve_gfp.h>
10 #include <botan/curve_nistp.h>
11 #include <botan/internal/mp_core.h>
12 #include <botan/internal/mp_asmi.h>
18 class CurveGFp_Montgomery final :
public CurveGFp_Repr
21 CurveGFp_Montgomery(
const BigInt& p,
const BigInt& a,
const BigInt& b) :
33 const BigInt& get_a()
const override {
return m_a; }
35 const BigInt& get_b()
const override {
return m_b; }
37 const BigInt& get_p()
const override {
return m_p; }
39 const BigInt& get_a_rep()
const override {
return m_a_r; }
41 const BigInt& get_b_rep()
const override {
return m_b_r; }
43 size_t get_p_words()
const override {
return m_p_words; }
45 void to_curve_rep(BigInt& x, secure_vector<word>& ws)
const override;
47 void from_curve_rep(BigInt& x, secure_vector<word>& ws)
const override;
49 void curve_mul(BigInt& z,
const BigInt& x,
const BigInt& y,
50 secure_vector<word>& ws)
const override;
52 void curve_sqr(BigInt& z,
const BigInt& x,
53 secure_vector<word>& ws)
const override;
63 void CurveGFp_Montgomery::to_curve_rep(BigInt& x, secure_vector<word>& ws)
const
66 curve_mul(x, tx,
m_r2, ws);
69 void CurveGFp_Montgomery::from_curve_rep(BigInt& x, secure_vector<word>& ws)
const
72 curve_mul(x, tx, 1, ws);
75 void CurveGFp_Montgomery::curve_mul(BigInt& z,
const BigInt& x,
const BigInt& y,
76 secure_vector<word>& ws)
const
78 if(x.is_zero() || y.is_zero())
84 const size_t output_size = 2*
m_p_words + 1;
87 z.grow_to(output_size);
94 void CurveGFp_Montgomery::curve_sqr(BigInt& z,
const BigInt& x,
95 secure_vector<word>& ws)
const
103 const size_t x_sw = x.sig_words();
106 const size_t output_size = 2*
m_p_words + 1;
110 z.grow_to(output_size);
117 class CurveGFp_NIST :
public CurveGFp_Repr
120 CurveGFp_NIST(
size_t p_bits,
const BigInt& a,
const BigInt& b) :
121 m_a(a),
m_b(b),
m_p_words((p_bits + BOTAN_MP_WORD_BITS - 1) / BOTAN_MP_WORD_BITS)
125 const BigInt& get_a()
const override {
return m_a; }
127 const BigInt& get_b()
const override {
return m_b; }
129 size_t get_p_words()
const override {
return m_p_words; }
131 const BigInt& get_a_rep()
const override {
return m_a; }
133 const BigInt& get_b_rep()
const override {
return m_b; }
135 void to_curve_rep(BigInt& x, secure_vector<word>& ws)
const override
138 void from_curve_rep(BigInt& x, secure_vector<word>& ws)
const override
141 void curve_mul(BigInt& z,
const BigInt& x,
const BigInt& y,
142 secure_vector<word>& ws)
const override;
144 void curve_sqr(BigInt& z,
const BigInt& x,
145 secure_vector<word>& ws)
const override;
147 virtual void redc(BigInt& x, secure_vector<word>& ws)
const = 0;
154 void CurveGFp_NIST::curve_mul(BigInt& z,
const BigInt& x,
const BigInt& y,
155 secure_vector<word>& ws)
const
157 if(x.is_zero() || y.is_zero())
163 const size_t p_words = get_p_words();
164 const size_t output_size = 2*p_words + 1;
165 ws.resize(2*(p_words+2));
167 z.grow_to(output_size);
175 void CurveGFp_NIST::curve_sqr(BigInt& z,
const BigInt& x,
176 secure_vector<word>& ws)
const
184 const size_t p_words = get_p_words();
185 const size_t output_size = 2*p_words + 1;
187 ws.resize(2*(p_words+2));
189 z.grow_to(output_size);
192 bigint_sqr(z.mutable_data(), output_size, ws.data(),
193 x.data(), x.size(), x.sig_words());
198 #if defined(BOTAN_HAS_NIST_PRIME_REDUCERS_W32)
203 class CurveGFp_P192 final :
public CurveGFp_NIST
206 CurveGFp_P192(
const BigInt& a,
const BigInt& b) : CurveGFp_NIST(192, a, b) {}
207 const BigInt& get_p()
const override {
return prime_p192(); }
209 void redc(BigInt& x, secure_vector<word>& ws)
const override { redc_p192(x, ws); }
215 class CurveGFp_P224 final :
public CurveGFp_NIST
218 CurveGFp_P224(
const BigInt& a,
const BigInt& b) : CurveGFp_NIST(224, a, b) {}
219 const BigInt& get_p()
const override {
return prime_p224(); }
221 void redc(BigInt& x, secure_vector<word>& ws)
const override { redc_p224(x, ws); }
227 class CurveGFp_P256 final :
public CurveGFp_NIST
230 CurveGFp_P256(
const BigInt& a,
const BigInt& b) : CurveGFp_NIST(256, a, b) {}
231 const BigInt& get_p()
const override {
return prime_p256(); }
233 void redc(BigInt& x, secure_vector<word>& ws)
const override { redc_p256(x, ws); }
239 class CurveGFp_P384 final :
public CurveGFp_NIST
242 CurveGFp_P384(
const BigInt& a,
const BigInt& b) : CurveGFp_NIST(384, a, b) {}
243 const BigInt& get_p()
const override {
return prime_p384(); }
245 void redc(BigInt& x, secure_vector<word>& ws)
const override { redc_p384(x, ws); }
253 class CurveGFp_P521 final :
public CurveGFp_NIST
256 CurveGFp_P521(
const BigInt& a,
const BigInt& b) : CurveGFp_NIST(521, a, b) {}
257 const BigInt& get_p()
const override {
return prime_p521(); }
259 void redc(BigInt& x, secure_vector<word>& ws)
const override {
redc_p521(x, ws); }
264 std::shared_ptr<CurveGFp_Repr>
265 CurveGFp::choose_repr(
const BigInt& p,
const BigInt& a,
const BigInt& b)
267 #if defined(BOTAN_HAS_NIST_PRIME_REDUCERS_W32)
268 if(p == prime_p192())
269 return std::shared_ptr<CurveGFp_Repr>(
new CurveGFp_P192(a, b));
270 if(p == prime_p224())
271 return std::shared_ptr<CurveGFp_Repr>(
new CurveGFp_P224(a, b));
272 if(p == prime_p256())
273 return std::shared_ptr<CurveGFp_Repr>(
new CurveGFp_P256(a, b));
274 if(p == prime_p384())
275 return std::shared_ptr<CurveGFp_Repr>(
new CurveGFp_P384(a, b));
279 return std::shared_ptr<CurveGFp_Repr>(
new CurveGFp_P521(a, b));
281 return std::shared_ptr<CurveGFp_Repr>(
new CurveGFp_Montgomery(p, a, b));
#define BOTAN_ASSERT(expr, assertion_made)
void bigint_monty_sqr(BigInt &z, const BigInt &x, const word p[], size_t p_size, word p_dash, word workspace[])
void bigint_sqr(word z[], size_t z_size, word workspace[], const word x[], size_t x_size, size_t x_sw)
void bigint_monty_mul(BigInt &z, const BigInt &x, const BigInt &y, const word p[], size_t p_size, word p_dash, word workspace[])
void redc_p521(BigInt &x, secure_vector< word > &ws)
const word * data() const
static BigInt power_of_2(size_t n)
const BigInt & prime_p521()
word monty_inverse(word input)
void bigint_mul(BigInt &z, const BigInt &x, const BigInt &y, word workspace[])