monotone

monotone Mtn Source Tree

Root/botan/pk_core.cpp

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

Archive Download this file

Branches

Tags

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