monotone

monotone Mtn Source Tree

Root/botan/pk_core.cpp

1/*************************************************
2* PK Algorithm Core Source File *
3* (C) 1999-2007 The Botan Project *
4*************************************************/
5
6#include <botan/pk_core.h>
7#include <botan/numthry.h>
8#include <botan/engine.h>
9#include <botan/config.h>
10#include <algorithm>
11
12namespace Botan {
13
14namespace {
15
16/*************************************************
17* Return a new blinding factor *
18*************************************************/
19BigInt blinding_factor(u32bit modulus_size)
20 {
21 const u32bit BLINDING_BITS =
22 global_config().option_as_u32bit("pk/blinder_size");
23
24 if(BLINDING_BITS == 0)
25 return 0;
26 return random_integer(std::min(modulus_size - 1, BLINDING_BITS));
27 }
28
29}
30
31/*************************************************
32* IF_Core Constructor *
33*************************************************/
34IF_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)
37 {
38 op = Engine_Core::if_op(e, n, d, p, q, d1, d2, c);
39
40 if(d != 0)
41 {
42 BigInt k = blinding_factor(n.bits());
43 if(k != 0)
44 blinder = Blinder(power_mod(k, e, n), inverse_mod(k, n), n);
45 }
46 }
47
48/*************************************************
49* IF_Core Copy Constructor *
50*************************************************/
51IF_Core::IF_Core(const IF_Core& core)
52 {
53 op = 0;
54 if(core.op)
55 op = core.op->clone();
56 blinder = core.blinder;
57 }
58
59/*************************************************
60* IF_Core Assignment Operator *
61*************************************************/
62IF_Core& IF_Core::operator=(const IF_Core& core)
63 {
64 delete op;
65 if(core.op)
66 op = core.op->clone();
67 blinder = core.blinder;
68 return (*this);
69 }
70
71/*************************************************
72* IF Public Operation *
73*************************************************/
74BigInt IF_Core::public_op(const BigInt& i) const
75 {
76 return op->public_op(i);
77 }
78
79/*************************************************
80* IF Private Operation *
81*************************************************/
82BigInt IF_Core::private_op(const BigInt& i) const
83 {
84 return blinder.unblind(op->private_op(blinder.blind(i)));
85 }
86
87/*************************************************
88* DSA_Core Constructor *
89*************************************************/
90DSA_Core::DSA_Core(const DL_Group& group, const BigInt& y, const BigInt& x)
91 {
92 op = Engine_Core::dsa_op(group, y, x);
93 }
94
95/*************************************************
96* DSA_Core Copy Constructor *
97*************************************************/
98DSA_Core::DSA_Core(const DSA_Core& core)
99 {
100 op = 0;
101 if(core.op)
102 op = core.op->clone();
103 }
104
105/*************************************************
106* DSA_Core Assignment Operator *
107*************************************************/
108DSA_Core& DSA_Core::operator=(const DSA_Core& core)
109 {
110 delete op;
111 if(core.op)
112 op = core.op->clone();
113 return (*this);
114 }
115
116/*************************************************
117* DSA Verification Operation *
118*************************************************/
119bool DSA_Core::verify(const byte msg[], u32bit msg_length,
120 const byte sig[], u32bit sig_length) const
121 {
122 return op->verify(msg, msg_length, sig, sig_length);
123 }
124
125/*************************************************
126* DSA Signature Operation *
127*************************************************/
128SecureVector<byte> DSA_Core::sign(const byte in[], u32bit length,
129 const BigInt& k) const
130 {
131 return op->sign(in, length, k);
132 }
133
134/*************************************************
135* NR_Core Constructor *
136*************************************************/
137NR_Core::NR_Core(const DL_Group& group, const BigInt& y, const BigInt& x)
138 {
139 op = Engine_Core::nr_op(group, y, x);
140 }
141
142/*************************************************
143* NR_Core Copy Constructor *
144*************************************************/
145NR_Core::NR_Core(const NR_Core& core)
146 {
147 op = 0;
148 if(core.op)
149 op = core.op->clone();
150 }
151
152/*************************************************
153* NR_Core Assignment Operator *
154*************************************************/
155NR_Core& NR_Core::operator=(const NR_Core& core)
156 {
157 delete op;
158 if(core.op)
159 op = core.op->clone();
160 return (*this);
161 }
162
163/*************************************************
164* NR Verification Operation *
165*************************************************/
166SecureVector<byte> NR_Core::verify(const byte in[], u32bit length) const
167 {
168 return op->verify(in, length);
169 }
170
171/*************************************************
172* NR Signature Operation *
173*************************************************/
174SecureVector<byte> NR_Core::sign(const byte in[], u32bit length,
175 const BigInt& k) const
176 {
177 return op->sign(in, length, k);
178 }
179
180/*************************************************
181* ELG_Core Constructor *
182*************************************************/
183ELG_Core::ELG_Core(const DL_Group& group, const BigInt& y, const BigInt& x)
184 {
185 op = Engine_Core::elg_op(group, y, x);
186
187 p_bytes = 0;
188 if(x != 0)
189 {
190 const BigInt& p = group.get_p();
191 p_bytes = group.get_p().bytes();
192
193 BigInt k = blinding_factor(p.bits());
194 if(k != 0)
195 blinder = Blinder(k, power_mod(k, x, p), p);
196 }
197 }
198
199/*************************************************
200* ELG_Core Copy Constructor *
201*************************************************/
202ELG_Core::ELG_Core(const ELG_Core& core)
203 {
204 op = 0;
205 if(core.op)
206 op = core.op->clone();
207 blinder = core.blinder;
208 p_bytes = core.p_bytes;
209 }
210
211/*************************************************
212* ELG_Core Assignment Operator *
213*************************************************/
214ELG_Core& ELG_Core::operator=(const ELG_Core& core)
215 {
216 delete op;
217 if(core.op)
218 op = core.op->clone();
219 blinder = core.blinder;
220 p_bytes = core.p_bytes;
221 return (*this);
222 }
223
224/*************************************************
225* ElGamal Encrypt Operation *
226*************************************************/
227SecureVector<byte> ELG_Core::encrypt(const byte in[], u32bit length,
228 const BigInt& k) const
229 {
230 return op->encrypt(in, length, k);
231 }
232
233/*************************************************
234* ElGamal Decrypt Operation *
235*************************************************/
236SecureVector<byte> ELG_Core::decrypt(const byte in[], u32bit length) const
237 {
238 if(length != 2*p_bytes)
239 throw Invalid_Argument("ELG_Core::decrypt: Invalid message");
240
241 BigInt a(in, p_bytes);
242 BigInt b(in + p_bytes, p_bytes);
243
244 return BigInt::encode(blinder.unblind(op->decrypt(blinder.blind(a), b)));
245 }
246
247/*************************************************
248* DH_Core Constructor *
249*************************************************/
250DH_Core::DH_Core(const DL_Group& group, const BigInt& x)
251 {
252 op = Engine_Core::dh_op(group, x);
253
254 const BigInt& p = group.get_p();
255 BigInt k = blinding_factor(p.bits());
256 if(k != 0)
257 blinder = Blinder(k, power_mod(inverse_mod(k, p), x, p), p);
258 }
259
260/*************************************************
261* DH_Core Copy Constructor *
262*************************************************/
263DH_Core::DH_Core(const DH_Core& core)
264 {
265 op = 0;
266 if(core.op)
267 op = core.op->clone();
268 blinder = core.blinder;
269 }
270
271/*************************************************
272* DH_Core Assignment Operator *
273*************************************************/
274DH_Core& DH_Core::operator=(const DH_Core& core)
275 {
276 delete op;
277 if(core.op)
278 op = core.op->clone();
279 blinder = core.blinder;
280 return (*this);
281 }
282
283/*************************************************
284* DH Operation *
285*************************************************/
286BigInt DH_Core::agree(const BigInt& i) const
287 {
288 return blinder.unblind(op->agree(blinder.blind(i)));
289 }
290
291}

Archive Download this file

Branches

Tags

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