1
/*************************************************
2
* PK Algorithm Core Source File *
3
* (C) 1999-2007 The Botan Project *
4
*************************************************/
6
#include <botan/pk_core.h>
7
#include <botan/numthry.h>
8
#include <botan/engine.h>
9
#include <botan/config.h>
16
/*************************************************
17
* Return a new blinding factor *
18
*************************************************/
19
BigInt blinding_factor(u32bit modulus_size)
21
const u32bit BLINDING_BITS =
22
global_config().option_as_u32bit("pk/blinder_size");
24
if(BLINDING_BITS == 0)
26
return random_integer(std::min(modulus_size - 1, BLINDING_BITS));
31
/*************************************************
32
* IF_Core Constructor *
33
*************************************************/
34
IF_Core::IF_Core(const BigInt& e, const BigInt& n, const BigInt& d,
35
const BigInt& p, const BigInt& q,
36
const BigInt& d1, const BigInt& d2, const BigInt& c)
38
op = Engine_Core::if_op(e, n, d, p, q, d1, d2, c);
42
BigInt k = blinding_factor(n.bits());
44
blinder = Blinder(power_mod(k, e, n), inverse_mod(k, n), n);
48
/*************************************************
49
* IF_Core Copy Constructor *
50
*************************************************/
51
IF_Core::IF_Core(const IF_Core& core)
55
op = core.op->clone();
56
blinder = core.blinder;
59
/*************************************************
60
* IF_Core Assignment Operator *
61
*************************************************/
62
IF_Core& IF_Core::operator=(const IF_Core& core)
66
op = core.op->clone();
67
blinder = core.blinder;
71
/*************************************************
72
* IF Public Operation *
73
*************************************************/
74
BigInt IF_Core::public_op(const BigInt& i) const
76
return op->public_op(i);
79
/*************************************************
80
* IF Private Operation *
81
*************************************************/
82
BigInt IF_Core::private_op(const BigInt& i) const
84
return blinder.unblind(op->private_op(blinder.blind(i)));
87
/*************************************************
88
* DSA_Core Constructor *
89
*************************************************/
90
DSA_Core::DSA_Core(const DL_Group& group, const BigInt& y, const BigInt& x)
92
op = Engine_Core::dsa_op(group, y, x);
95
/*************************************************
96
* DSA_Core Copy Constructor *
97
*************************************************/
98
DSA_Core::DSA_Core(const DSA_Core& core)
102
op = core.op->clone();
105
/*************************************************
106
* DSA_Core Assignment Operator *
107
*************************************************/
108
DSA_Core& DSA_Core::operator=(const DSA_Core& core)
112
op = core.op->clone();
116
/*************************************************
117
* DSA Verification Operation *
118
*************************************************/
119
bool DSA_Core::verify(const byte msg[], u32bit msg_length,
120
const byte sig[], u32bit sig_length) const
122
return op->verify(msg, msg_length, sig, sig_length);
125
/*************************************************
126
* DSA Signature Operation *
127
*************************************************/
128
SecureVector<byte> DSA_Core::sign(const byte in[], u32bit length,
129
const BigInt& k) const
131
return op->sign(in, length, k);
134
/*************************************************
135
* NR_Core Constructor *
136
*************************************************/
137
NR_Core::NR_Core(const DL_Group& group, const BigInt& y, const BigInt& x)
139
op = Engine_Core::nr_op(group, y, x);
142
/*************************************************
143
* NR_Core Copy Constructor *
144
*************************************************/
145
NR_Core::NR_Core(const NR_Core& core)
149
op = core.op->clone();
152
/*************************************************
153
* NR_Core Assignment Operator *
154
*************************************************/
155
NR_Core& NR_Core::operator=(const NR_Core& core)
159
op = core.op->clone();
163
/*************************************************
164
* NR Verification Operation *
165
*************************************************/
166
SecureVector<byte> NR_Core::verify(const byte in[], u32bit length) const
168
return op->verify(in, length);
171
/*************************************************
172
* NR Signature Operation *
173
*************************************************/
174
SecureVector<byte> NR_Core::sign(const byte in[], u32bit length,
175
const BigInt& k) const
177
return op->sign(in, length, k);
180
/*************************************************
181
* ELG_Core Constructor *
182
*************************************************/
183
ELG_Core::ELG_Core(const DL_Group& group, const BigInt& y, const BigInt& x)
185
op = Engine_Core::elg_op(group, y, x);
190
const BigInt& p = group.get_p();
191
p_bytes = group.get_p().bytes();
193
BigInt k = blinding_factor(p.bits());
195
blinder = Blinder(k, power_mod(k, x, p), p);
199
/*************************************************
200
* ELG_Core Copy Constructor *
201
*************************************************/
202
ELG_Core::ELG_Core(const ELG_Core& core)
206
op = core.op->clone();
207
blinder = core.blinder;
208
p_bytes = core.p_bytes;
211
/*************************************************
212
* ELG_Core Assignment Operator *
213
*************************************************/
214
ELG_Core& ELG_Core::operator=(const ELG_Core& core)
218
op = core.op->clone();
219
blinder = core.blinder;
220
p_bytes = core.p_bytes;
224
/*************************************************
225
* ElGamal Encrypt Operation *
226
*************************************************/
227
SecureVector<byte> ELG_Core::encrypt(const byte in[], u32bit length,
228
const BigInt& k) const
230
return op->encrypt(in, length, k);
233
/*************************************************
234
* ElGamal Decrypt Operation *
235
*************************************************/
236
SecureVector<byte> ELG_Core::decrypt(const byte in[], u32bit length) const
238
if(length != 2*p_bytes)
239
throw Invalid_Argument("ELG_Core::decrypt: Invalid message");
241
BigInt a(in, p_bytes);
242
BigInt b(in + p_bytes, p_bytes);
244
return BigInt::encode(blinder.unblind(op->decrypt(blinder.blind(a), b)));
247
/*************************************************
248
* DH_Core Constructor *
249
*************************************************/
250
DH_Core::DH_Core(const DL_Group& group, const BigInt& x)
252
op = Engine_Core::dh_op(group, x);
254
const BigInt& p = group.get_p();
255
BigInt k = blinding_factor(p.bits());
257
blinder = Blinder(k, power_mod(inverse_mod(k, p), x, p), p);
260
/*************************************************
261
* DH_Core Copy Constructor *
262
*************************************************/
263
DH_Core::DH_Core(const DH_Core& core)
267
op = core.op->clone();
268
blinder = core.blinder;
271
/*************************************************
272
* DH_Core Assignment Operator *
273
*************************************************/
274
DH_Core& DH_Core::operator=(const DH_Core& core)
278
op = core.op->clone();
279
blinder = core.blinder;
283
/*************************************************
285
*************************************************/
286
BigInt DH_Core::agree(const BigInt& i) const
288
return blinder.unblind(op->agree(blinder.blind(i)));