1 /*
2  * Copyright (c) 2004 Bob Deblier
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18 
19 #define BEECRYPT_DLL_EXPORT
20 
21 #if HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24 
25 #include "beecrypt/beecrypt.h"
26 #include "beecrypt/mpnumber.h"
27 #include "beecrypt/mpbarrett.h"
28 #include "beecrypt/dldp.h"
29 #include "beecrypt/dlkp.h"
30 #include "beecrypt/dlpk.h"
31 #include "beecrypt/rsakp.h"
32 #include "beecrypt/rsapk.h"
33 
34 #include <iomanip>
35 
36 const mpnumber mpnumber::ZERO;
37 const mpnumber mpnumber::ONE(1);
38 
mpnumber()39 mpnumber::mpnumber()
40 {
41 	mpnzero(this);
42 }
43 
mpnumber(unsigned int value)44 mpnumber::mpnumber(unsigned int value)
45 {
46 	mpnsize(this, 1);
47 	mpnsetw(this, value);
48 }
49 
mpnumber(size_t size,const mpw * data)50 mpnumber::mpnumber(size_t size, const mpw* data)
51 {
52 	mpninit(this, size, data);
53 }
54 
mpnumber(const mpnumber & copy)55 mpnumber::mpnumber(const mpnumber& copy)
56 {
57 	mpnzero(this);
58 	mpncopy(this, &copy);
59 }
60 
~mpnumber()61 mpnumber::~mpnumber()
62 {
63 	mpnfree(this);
64 }
65 
operator =(const mpnumber & copy)66 const mpnumber& mpnumber::operator=(const mpnumber& copy)
67 {
68 	mpncopy(this, &copy);
69 	return *this;
70 }
71 
wipe()72 void mpnumber::wipe()
73 {
74 	mpnwipe(this);
75 }
76 
bitlength() const77 size_t mpnumber::bitlength() const
78 {
79 	return mpbits(size, data);
80 }
81 
operator <<(std::ostream & stream,const mpnumber & n)82 std::ostream& operator<<(std::ostream& stream, const mpnumber& n)
83 {
84 	if (n.size)
85 	{
86 		stream << std::hex << std::setfill('0') << n.data[0];
87 		for (size_t i = 1; i < n.size; i++)
88 			stream << std::setw(MP_WNIBBLES) << n.data[i];
89 	}
90 
91 	return stream;
92 }
93 
mpbarrett()94 mpbarrett::mpbarrett()
95 {
96 	mpbzero(this);
97 }
98 
mpbarrett(const mpbarrett & copy)99 mpbarrett::mpbarrett(const mpbarrett& copy)
100 {
101 	mpbzero(this);
102 	mpbcopy(this, &copy);
103 }
104 
~mpbarrett()105 mpbarrett::~mpbarrett()
106 {
107 	mpbfree(this);
108 }
109 
operator =(const mpbarrett & copy)110 const mpbarrett& mpbarrett::operator=(const mpbarrett& copy)
111 {
112 	mpbcopy(this, &copy);
113 	return *this;
114 }
115 
wipe()116 void mpbarrett::wipe()
117 {
118 	mpbwipe(this);
119 }
120 
bitlength() const121 size_t mpbarrett::bitlength() const
122 {
123 	return mpbits(size, modl);
124 }
125 
operator <<(std::ostream & stream,const mpbarrett & b)126 std::ostream& operator<<(std::ostream& stream, const mpbarrett& b)
127 {
128 	stream << std::hex << std::setfill('0');
129 
130 	for (size_t i = 0; i < b.size; i++)
131 		stream << std::setw(MP_WNIBBLES) << b.modl[i];
132 
133 	return stream;
134 }
135 
dldp_p()136 dldp_p::dldp_p()
137 {
138 	dldp_pInit(this);
139 }
140 
dldp_p(const dldp_p & copy)141 dldp_p::dldp_p(const dldp_p& copy)
142 {
143 	dldp_pInit(this);
144 	dldp_pCopy(this, &copy);
145 }
146 
~dldp_p()147 dldp_p::~dldp_p()
148 {
149 	dldp_pFree(this);
150 }
151 
dlkp_p()152 dlkp_p::dlkp_p()
153 {
154 	dlkp_pInit(this);
155 }
156 
dlkp_p(const dlkp_p & copy)157 dlkp_p::dlkp_p(const dlkp_p& copy)
158 {
159 	dlkp_pInit(this);
160 	dlkp_pCopy(this, &copy);
161 }
162 
~dlkp_p()163 dlkp_p::~dlkp_p()
164 {
165 	dlkp_pFree(this);
166 }
167 
dlpk_p()168 dlpk_p::dlpk_p()
169 {
170 	dlpk_pInit(this);
171 }
172 
dlpk_p(const dlpk_p & copy)173 dlpk_p::dlpk_p(const dlpk_p& copy)
174 {
175 	dlpk_pInit(this);
176 	dlpk_pCopy(this, &copy);
177 }
178 
~dlpk_p()179 dlpk_p::~dlpk_p()
180 {
181 	dlpk_pFree(this);
182 }
183 
rsakp()184 rsakp::rsakp()
185 {
186 	rsakpInit(this);
187 }
188 
rsakp(const rsakp & copy)189 rsakp::rsakp(const rsakp& copy)
190 {
191 	rsakpInit(this);
192 	rsakpCopy(this, &copy);
193 }
194 
~rsakp()195 rsakp::~rsakp()
196 {
197 	rsakpFree(this);
198 }
199 
rsapk()200 rsapk::rsapk()
201 {
202 	rsapkInit(this);
203 }
204 
rsapk(const rsapk & copy)205 rsapk::rsapk(const rsapk& copy)
206 {
207 	rsapkInit(this);
208 	rsapkCopy(this, &copy);
209 }
210 
~rsapk()211 rsapk::~rsapk()
212 {
213 	rsapkFree(this);
214 }
215 
blockCipherContext()216 blockCipherContext::blockCipherContext()
217 {
218 	blockCipherContextInit(this, blockCipherDefault());
219 }
220 
blockCipherContext(const blockCipher * b)221 blockCipherContext::blockCipherContext(const blockCipher* b)
222 {
223 	blockCipherContextInit(this, b);
224 }
225 
~blockCipherContext()226 blockCipherContext::~blockCipherContext()
227 {
228 	blockCipherContextFree(this);
229 }
230 
hashFunctionContext()231 hashFunctionContext::hashFunctionContext()
232 {
233 	hashFunctionContextInit(this, hashFunctionDefault());
234 }
235 
hashFunctionContext(const hashFunction * h)236 hashFunctionContext::hashFunctionContext(const hashFunction* h)
237 {
238 	hashFunctionContextInit(this, h);
239 }
240 
~hashFunctionContext()241 hashFunctionContext::~hashFunctionContext()
242 {
243 	hashFunctionContextFree(this);
244 }
245 
keyedHashFunctionContext()246 keyedHashFunctionContext::keyedHashFunctionContext()
247 {
248 	keyedHashFunctionContextInit(this, keyedHashFunctionDefault());
249 }
250 
keyedHashFunctionContext(const keyedHashFunction * k)251 keyedHashFunctionContext::keyedHashFunctionContext(const keyedHashFunction* k)
252 {
253 	keyedHashFunctionContextInit(this, k);
254 }
255 
~keyedHashFunctionContext()256 keyedHashFunctionContext::~keyedHashFunctionContext()
257 {
258 	keyedHashFunctionContextFree(this);
259 }
260 
randomGeneratorContext()261 randomGeneratorContext::randomGeneratorContext()
262 {
263 	randomGeneratorContextInit(this, randomGeneratorDefault());
264 }
265 
randomGeneratorContext(const randomGenerator * rng)266 randomGeneratorContext::randomGeneratorContext(const randomGenerator* rng)
267 {
268 	randomGeneratorContextInit(this, rng);
269 }
270 
~randomGeneratorContext()271 randomGeneratorContext::~randomGeneratorContext()
272 {
273 	randomGeneratorContextFree(this);
274 }
275