monotone

monotone Mtn Source Tree

Root/botan/def_ops.cpp

1/*************************************************
2* Default Engine PK Operations Source File *
3* (C) 1999-2006 The Botan Project *
4*************************************************/
5
6#include <botan/eng_def.h>
7#include <botan/pow_mod.h>
8#include <botan/numthry.h>
9#include <botan/reducer.h>
10
11namespace Botan {
12
13namespace {
14
15/*************************************************
16* Default IF Operation *
17*************************************************/
18class Default_IF_Op : public IF_Operation
19 {
20 public:
21 BigInt public_op(const BigInt& i) const
22 { return powermod_e_n(i); }
23 BigInt private_op(const BigInt&) const;
24
25 IF_Operation* clone() const { return new Default_IF_Op(*this); }
26
27 Default_IF_Op(const BigInt&, const BigInt&, const BigInt&,
28 const BigInt&, const BigInt&, const BigInt&,
29 const BigInt&, const BigInt&);
30 private:
31 Fixed_Exponent_Power_Mod powermod_e_n, powermod_d1_p, powermod_d2_q;
32 Modular_Reducer reducer;
33 BigInt c, q;
34 };
35
36/*************************************************
37* Default_IF_Op Constructor *
38*************************************************/
39Default_IF_Op::Default_IF_Op(const BigInt& e, const BigInt& n, const BigInt&,
40 const BigInt& p, const BigInt& q,
41 const BigInt& d1, const BigInt& d2,
42 const BigInt& c)
43 {
44 powermod_e_n = Fixed_Exponent_Power_Mod(e, n);
45
46 if(d1 != 0 && d2 != 0 && p != 0 && q != 0)
47 {
48 powermod_d1_p = Fixed_Exponent_Power_Mod(d1, p);
49 powermod_d2_q = Fixed_Exponent_Power_Mod(d2, q);
50 reducer = Modular_Reducer(p);
51 this->c = c;
52 this->q = q;
53 }
54 }
55
56/*************************************************
57* Default IF Private Operation *
58*************************************************/
59BigInt Default_IF_Op::private_op(const BigInt& i) const
60 {
61 if(q == 0)
62 throw Internal_Error("Default_IF_Op::private_op: No private key");
63
64 BigInt j1 = powermod_d1_p(i);
65 BigInt j2 = powermod_d2_q(i);
66 j1 = reducer.reduce(sub_mul(j1, j2, c));
67 return mul_add(j1, q, j2);
68 }
69
70/*************************************************
71* Default DSA Operation *
72*************************************************/
73class Default_DSA_Op : public DSA_Operation
74 {
75 public:
76 bool verify(const byte[], u32bit, const byte[], u32bit) const;
77 SecureVector<byte> sign(const byte[], u32bit, const BigInt&) const;
78
79 DSA_Operation* clone() const { return new Default_DSA_Op(*this); }
80
81 Default_DSA_Op(const DL_Group&, const BigInt&, const BigInt&);
82 private:
83 const BigInt x, y;
84 const DL_Group group;
85 Fixed_Base_Power_Mod powermod_g_p, powermod_y_p;
86 Modular_Reducer mod_p, mod_q;
87 };
88
89/*************************************************
90* Default_DSA_Op Constructor *
91*************************************************/
92Default_DSA_Op::Default_DSA_Op(const DL_Group& grp, const BigInt& y1,
93 const BigInt& x1) : x(x1), y(y1), group(grp)
94 {
95 powermod_g_p = Fixed_Base_Power_Mod(group.get_g(), group.get_p());
96 powermod_y_p = Fixed_Base_Power_Mod(y, group.get_p());
97 mod_p = Modular_Reducer(group.get_p());
98 mod_q = Modular_Reducer(group.get_q());
99 }
100
101/*************************************************
102* Default DSA Verify Operation *
103*************************************************/
104bool Default_DSA_Op::verify(const byte msg[], u32bit msg_len,
105 const byte sig[], u32bit sig_len) const
106 {
107 const BigInt& q = group.get_q();
108
109 if(sig_len != 2*q.bytes() || msg_len > q.bytes())
110 return false;
111
112 BigInt r(sig, q.bytes());
113 BigInt s(sig + q.bytes(), q.bytes());
114 BigInt i(msg, msg_len);
115
116 if(r <= 0 || r >= q || s <= 0 || s >= q)
117 return false;
118
119 s = inverse_mod(s, q);
120 s = mod_p.multiply(powermod_g_p(mod_q.multiply(s, i)),
121 powermod_y_p(mod_q.multiply(s, r)));
122
123 return (mod_q.reduce(s) == r);
124 }
125
126/*************************************************
127* Default DSA Sign Operation *
128*************************************************/
129SecureVector<byte> Default_DSA_Op::sign(const byte in[], u32bit length,
130 const BigInt& k) const
131 {
132 if(x == 0)
133 throw Internal_Error("Default_DSA_Op::sign: No private key");
134
135 const BigInt& q = group.get_q();
136 BigInt i(in, length);
137
138 BigInt r = mod_q.reduce(powermod_g_p(k));
139 BigInt s = mod_q.multiply(inverse_mod(k, q), mul_add(x, r, i));
140
141 if(r.is_zero() || s.is_zero())
142 throw Internal_Error("Default_DSA_Op::sign: r or s was zero");
143
144 SecureVector<byte> output(2*q.bytes());
145 r.binary_encode(output + (output.size() / 2 - r.bytes()));
146 s.binary_encode(output + (output.size() - s.bytes()));
147 return output;
148 }
149
150/*************************************************
151* Default NR Operation *
152*************************************************/
153class Default_NR_Op : public NR_Operation
154 {
155 public:
156 SecureVector<byte> verify(const byte[], u32bit) const;
157 SecureVector<byte> sign(const byte[], u32bit, const BigInt&) const;
158
159 NR_Operation* clone() const { return new Default_NR_Op(*this); }
160
161 Default_NR_Op(const DL_Group&, const BigInt&, const BigInt&);
162 private:
163 const BigInt x, y;
164 const DL_Group group;
165 Fixed_Base_Power_Mod powermod_g_p, powermod_y_p;
166 Modular_Reducer mod_p, mod_q;
167 };
168
169/*************************************************
170* Default_NR_Op Constructor *
171*************************************************/
172Default_NR_Op::Default_NR_Op(const DL_Group& grp, const BigInt& y1,
173 const BigInt& x1) : x(x1), y(y1), group(grp)
174 {
175 powermod_g_p = Fixed_Base_Power_Mod(group.get_g(), group.get_p());
176 powermod_y_p = Fixed_Base_Power_Mod(y, group.get_p());
177 mod_p = Modular_Reducer(group.get_p());
178 mod_q = Modular_Reducer(group.get_q());
179 }
180
181/*************************************************
182* Default NR Verify Operation *
183*************************************************/
184SecureVector<byte> Default_NR_Op::verify(const byte in[], u32bit length) const
185 {
186 const BigInt& q = group.get_q();
187
188 if(length != 2*q.bytes())
189 return false;
190
191 BigInt c(in, q.bytes());
192 BigInt d(in + q.bytes(), q.bytes());
193
194 if(c.is_zero() || c >= q || d >= q)
195 throw Invalid_Argument("Default_NR_Op::verify: Invalid signature");
196
197 BigInt i = mod_p.multiply(powermod_g_p(d), powermod_y_p(c));
198 return BigInt::encode(mod_q.reduce(c - i));
199 }
200
201/*************************************************
202* Default NR Sign Operation *
203*************************************************/
204SecureVector<byte> Default_NR_Op::sign(const byte in[], u32bit length,
205 const BigInt& k) const
206 {
207 if(x == 0)
208 throw Internal_Error("Default_NR_Op::sign: No private key");
209
210 const BigInt& q = group.get_q();
211
212 BigInt f(in, length);
213
214 if(f >= q)
215 throw Invalid_Argument("Default_NR_Op::sign: Input is out of range");
216
217 BigInt c = mod_q.reduce(powermod_g_p(k) + f);
218 if(c.is_zero())
219 throw Internal_Error("Default_NR_Op::sign: c was zero");
220 BigInt d = mod_q.reduce(k - x * c);
221
222 SecureVector<byte> output(2*q.bytes());
223 c.binary_encode(output + (output.size() / 2 - c.bytes()));
224 d.binary_encode(output + (output.size() - d.bytes()));
225 return output;
226 }
227
228/*************************************************
229* Default ElGamal Operation *
230*************************************************/
231class Default_ELG_Op : public ELG_Operation
232 {
233 public:
234 SecureVector<byte> encrypt(const byte[], u32bit, const BigInt&) const;
235 BigInt decrypt(const BigInt&, const BigInt&) const;
236
237 ELG_Operation* clone() const { return new Default_ELG_Op(*this); }
238
239 Default_ELG_Op(const DL_Group&, const BigInt&, const BigInt&);
240 private:
241 const BigInt p;
242 Fixed_Base_Power_Mod powermod_g_p, powermod_y_p;
243 Fixed_Exponent_Power_Mod powermod_x_p;
244 Modular_Reducer mod_p;
245 };
246
247/*************************************************
248* Default_ELG_Op Constructor *
249*************************************************/
250Default_ELG_Op::Default_ELG_Op(const DL_Group& group, const BigInt& y,
251 const BigInt& x) : p(group.get_p())
252 {
253 powermod_g_p = Fixed_Base_Power_Mod(group.get_g(), p);
254 powermod_y_p = Fixed_Base_Power_Mod(y, p);
255 mod_p = Modular_Reducer(p);
256
257 if(x != 0)
258 powermod_x_p = Fixed_Exponent_Power_Mod(x, p);
259 }
260
261/*************************************************
262* Default ElGamal Encrypt Operation *
263*************************************************/
264SecureVector<byte> Default_ELG_Op::encrypt(const byte in[], u32bit length,
265 const BigInt& k) const
266 {
267 BigInt m(in, length);
268 if(m >= p)
269 throw Invalid_Argument("Default_ELG_Op::encrypt: Input is too large");
270
271 BigInt a = powermod_g_p(k);
272 BigInt b = mod_p.multiply(m, powermod_y_p(k));
273
274 SecureVector<byte> output(2*p.bytes());
275 a.binary_encode(output + (p.bytes() - a.bytes()));
276 b.binary_encode(output + output.size() / 2 + (p.bytes() - b.bytes()));
277 return output;
278 }
279
280/*************************************************
281* Default ElGamal Decrypt Operation *
282*************************************************/
283BigInt Default_ELG_Op::decrypt(const BigInt& a, const BigInt& b) const
284 {
285 if(a >= p || b >= p)
286 throw Invalid_Argument("Default_ELG_Op: Invalid message");
287
288 return mod_p.multiply(b, inverse_mod(powermod_x_p(a), p));
289 }
290
291/*************************************************
292* Default DH Operation *
293*************************************************/
294class Default_DH_Op : public DH_Operation
295 {
296 public:
297 BigInt agree(const BigInt& i) const { return powermod_x_p(i); }
298 DH_Operation* clone() const { return new Default_DH_Op(*this); }
299
300 Default_DH_Op(const DL_Group& group, const BigInt& x) :
301 powermod_x_p(x, group.get_p()) {}
302 private:
303 const Fixed_Exponent_Power_Mod powermod_x_p;
304 };
305
306}
307
308/*************************************************
309* Acquire an IF op *
310*************************************************/
311IF_Operation* Default_Engine::if_op(const BigInt& e, const BigInt& n,
312 const BigInt& d, const BigInt& p,
313 const BigInt& q, const BigInt& d1,
314 const BigInt& d2, const BigInt& c) const
315 {
316 return new Default_IF_Op(e, n, d, p, q, d1, d2, c);
317 }
318
319/*************************************************
320* Acquire a DSA op *
321*************************************************/
322DSA_Operation* Default_Engine::dsa_op(const DL_Group& group, const BigInt& y,
323 const BigInt& x) const
324 {
325 return new Default_DSA_Op(group, y, x);
326 }
327
328/*************************************************
329* Acquire a NR op *
330*************************************************/
331NR_Operation* Default_Engine::nr_op(const DL_Group& group, const BigInt& y,
332 const BigInt& x) const
333 {
334 return new Default_NR_Op(group, y, x);
335 }
336
337/*************************************************
338* Acquire an ElGamal op *
339*************************************************/
340ELG_Operation* Default_Engine::elg_op(const DL_Group& group, const BigInt& y,
341 const BigInt& x) const
342 {
343 return new Default_ELG_Op(group, y, x);
344 }
345
346/*************************************************
347* Acquire a DH op *
348*************************************************/
349DH_Operation* Default_Engine::dh_op(const DL_Group& group,
350 const BigInt& x) const
351 {
352 return new Default_DH_Op(group, x);
353 }
354
355}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status