1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
|
/* crypto_wrapper.hpp
*
* Copyright (C) 2003 Sawtooth Consulting Ltd.
*
* This file is part of yaSSL.
*
* yaSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* There are special exceptions to the terms and conditions of the GPL as it
* is applied to yaSSL. View the full text of the exception in the file
* FLOSS-EXCEPTIONS in the directory of this software distribution.
*
* yaSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
/* The crypto wrapper header is used to define policies for the cipher
* components used by SSL. There are 3 policies to consider:
*
* 1) MAC, the Message Authentication Code used for each Message
* 2) Bulk Cipher, the Cipher used to encrypt/decrypt each Message
* 3) Atuhentication, the Digitial Signing/Verifiaction scheme used
*
* This header doesn't rely on a specific crypto libraries internals,
* only the implementation should.
*/
#ifndef yaSSL_CRYPTO_WRAPPER_HPP
#define yaSSL_CRYPTO_WRAPPER_HPP
#include "yassl_types.hpp"
#include <stdio.h> // FILE
namespace yaSSL {
// Digest policy should implement a get_digest, update, and get sizes for pad
// and digest
struct Digest : public virtual_base {
virtual void get_digest(byte*) = 0;
virtual void get_digest(byte*, const byte*, unsigned int) = 0;
virtual void update(const byte*, unsigned int) = 0;
virtual uint get_digestSize() const = 0;
virtual uint get_padSize() const = 0;
virtual ~Digest() {}
};
// For use with NULL Digests
struct NO_MAC : public Digest {
void get_digest(byte*);
void get_digest(byte*, const byte*, unsigned int);
void update(const byte*, unsigned int);
uint get_digestSize() const;
uint get_padSize() const;
};
// MD5 Digest
class MD5 : public Digest {
public:
void get_digest(byte*);
void get_digest(byte*, const byte*, unsigned int);
void update(const byte*, unsigned int);
uint get_digestSize() const;
uint get_padSize() const;
MD5();
~MD5();
MD5(const MD5&);
MD5& operator=(const MD5&);
private:
struct MD5Impl;
MD5Impl* pimpl_;
};
// SHA-1 Digest
class SHA : public Digest {
public:
void get_digest(byte*);
void get_digest(byte*, const byte*, unsigned int);
void update(const byte*, unsigned int);
uint get_digestSize() const;
uint get_padSize() const;
SHA();
~SHA();
SHA(const SHA&);
SHA& operator=(const SHA&);
private:
struct SHAImpl;
SHAImpl* pimpl_;
};
// RIPEMD-160 Digest
class RMD : public Digest {
public:
void get_digest(byte*);
void get_digest(byte*, const byte*, unsigned int);
void update(const byte*, unsigned int);
uint get_digestSize() const;
uint get_padSize() const;
RMD();
~RMD();
RMD(const RMD&);
RMD& operator=(const RMD&);
private:
struct RMDImpl;
RMDImpl* pimpl_;
};
// HMAC_MD5
class HMAC_MD5 : public Digest {
public:
void get_digest(byte*);
void get_digest(byte*, const byte*, unsigned int);
void update(const byte*, unsigned int);
uint get_digestSize() const;
uint get_padSize() const;
HMAC_MD5(const byte*, unsigned int);
~HMAC_MD5();
private:
struct HMAC_MD5Impl;
HMAC_MD5Impl* pimpl_;
HMAC_MD5(const HMAC_MD5&);
HMAC_MD5& operator=(const HMAC_MD5&);
};
// HMAC_SHA-1
class HMAC_SHA : public Digest {
public:
void get_digest(byte*);
void get_digest(byte*, const byte*, unsigned int);
void update(const byte*, unsigned int);
uint get_digestSize() const;
uint get_padSize() const;
HMAC_SHA(const byte*, unsigned int);
~HMAC_SHA();
private:
struct HMAC_SHAImpl;
HMAC_SHAImpl* pimpl_;
HMAC_SHA(const HMAC_SHA&);
HMAC_SHA& operator=(const HMAC_SHA&);
};
// HMAC_RMD
class HMAC_RMD : public Digest {
public:
void get_digest(byte*);
void get_digest(byte*, const byte*, unsigned int);
void update(const byte*, unsigned int);
uint get_digestSize() const;
uint get_padSize() const;
HMAC_RMD(const byte*, unsigned int);
~HMAC_RMD();
private:
struct HMAC_RMDImpl;
HMAC_RMDImpl* pimpl_;
HMAC_RMD(const HMAC_RMD&);
HMAC_RMD& operator=(const HMAC_RMD&);
};
// BulkCipher policy should implement encrypt, decrypt, get block size,
// and set keys for encrypt and decrypt
struct BulkCipher : public virtual_base {
virtual void encrypt(byte*, const byte*, unsigned int) = 0;
virtual void decrypt(byte*, const byte*, unsigned int) = 0;
virtual void set_encryptKey(const byte*, const byte* = 0) = 0;
virtual void set_decryptKey(const byte*, const byte* = 0) = 0;
virtual uint get_blockSize() const = 0;
virtual int get_keySize() const = 0;
virtual int get_ivSize() const = 0;
virtual ~BulkCipher() {}
};
// For use with NULL Ciphers
struct NO_Cipher : public BulkCipher {
void encrypt(byte*, const byte*, unsigned int) {}
void decrypt(byte*, const byte*, unsigned int) {}
void set_encryptKey(const byte*, const byte*) {}
void set_decryptKey(const byte*, const byte*) {}
uint get_blockSize() const { return 0; }
int get_keySize() const { return 0; }
int get_ivSize() const { return 0; }
};
// SSLv3 and TLSv1 always use DES in CBC mode so IV is required
class DES : public BulkCipher {
public:
void encrypt(byte*, const byte*, unsigned int);
void decrypt(byte*, const byte*, unsigned int);
void set_encryptKey(const byte*, const byte*);
void set_decryptKey(const byte*, const byte*);
uint get_blockSize() const { return DES_BLOCK; }
int get_keySize() const { return DES_KEY_SZ; }
int get_ivSize() const { return DES_IV_SZ; }
DES();
~DES();
private:
struct DESImpl;
DESImpl* pimpl_;
DES(const DES&); // hide copy
DES& operator=(const DES&); // & assign
};
// 3DES Encrypt-Decrypt-Encrypt in CBC mode
class DES_EDE : public BulkCipher {
public:
void encrypt(byte*, const byte*, unsigned int);
void decrypt(byte*, const byte*, unsigned int);
void set_encryptKey(const byte*, const byte*);
void set_decryptKey(const byte*, const byte*);
uint get_blockSize() const { return DES_BLOCK; }
int get_keySize() const { return DES_EDE_KEY_SZ; }
int get_ivSize() const { return DES_IV_SZ; }
DES_EDE();
~DES_EDE();
private:
struct DES_EDEImpl;
DES_EDEImpl* pimpl_;
DES_EDE(const DES_EDE&); // hide copy
DES_EDE& operator=(const DES_EDE&); // & assign
};
// Alledged RC4
class RC4 : public BulkCipher {
public:
void encrypt(byte*, const byte*, unsigned int);
void decrypt(byte*, const byte*, unsigned int);
void set_encryptKey(const byte*, const byte*);
void set_decryptKey(const byte*, const byte*);
uint get_blockSize() const { return 0; }
int get_keySize() const { return RC4_KEY_SZ; }
int get_ivSize() const { return 0; }
RC4();
~RC4();
private:
struct RC4Impl;
RC4Impl* pimpl_;
RC4(const RC4&); // hide copy
RC4& operator=(const RC4&); // & assign
};
// AES
class AES : public BulkCipher {
public:
void encrypt(byte*, const byte*, unsigned int);
void decrypt(byte*, const byte*, unsigned int);
void set_encryptKey(const byte*, const byte*);
void set_decryptKey(const byte*, const byte*);
uint get_blockSize() const { return AES_BLOCK_SZ; }
int get_keySize() const;
int get_ivSize() const { return AES_IV_SZ; }
explicit AES(unsigned int = AES_128_KEY_SZ);
~AES();
private:
struct AESImpl;
AESImpl* pimpl_;
AES(const AES&); // hide copy
AES& operator=(const AES&); // & assign
};
// Random number generator
class RandomPool {
public:
void Fill(opaque* dst, uint sz) const;
RandomPool();
~RandomPool();
int GetError() const;
friend class RSA;
friend class DSS;
friend class DiffieHellman;
private:
struct RandomImpl;
RandomImpl* pimpl_;
RandomPool(const RandomPool&); // hide copy
RandomPool& operator=(const RandomPool&); // & assign
};
// Authentication policy should implement sign, and verify
struct Auth : public virtual_base {
virtual void sign(byte*, const byte*, unsigned int, const RandomPool&) = 0;
virtual bool verify(const byte*, unsigned int, const byte*,
unsigned int) = 0;
virtual uint get_signatureLength() const = 0;
virtual ~Auth() {}
};
// For use with NULL Authentication schemes
struct NO_Auth : public Auth {
void sign(byte*, const byte*, unsigned int, const RandomPool&) {}
bool verify(const byte*, unsigned int, const byte*, unsigned int)
{ return true; }
};
// Digitial Signature Standard scheme
class DSS : public Auth {
public:
void sign(byte*, const byte*, unsigned int, const RandomPool&);
bool verify(const byte*, unsigned int, const byte*, unsigned int);
uint get_signatureLength() const;
DSS(const byte*, unsigned int, bool publicKey = true);
~DSS();
private:
struct DSSImpl;
DSSImpl* pimpl_;
DSS(const DSS&);
DSS& operator=(const DSS&);
};
// RSA Authentication and exchange
class RSA : public Auth {
public:
void sign(byte*, const byte*, unsigned int, const RandomPool&);
bool verify(const byte*, unsigned int, const byte*, unsigned int);
void encrypt(byte*, const byte*, unsigned int, const RandomPool&);
void decrypt(byte*, const byte*, unsigned int, const RandomPool&);
uint get_signatureLength() const;
uint get_cipherLength() const;
RSA(const byte*, unsigned int, bool publicKey = true);
~RSA();
private:
struct RSAImpl;
RSAImpl* pimpl_;
RSA(const RSA&); // hide copy
RSA& operator=(const RSA&); // & assing
};
class Integer;
// Diffie-Hellman agreement
// hide for now TODO: figure out a way to give access to C clients p and g args
class DiffieHellman {
public:
DiffieHellman(const byte*, unsigned int, const byte*, unsigned int,
const byte*, unsigned int, const RandomPool& random);
//DiffieHellman(const char*, const RandomPool&);
DiffieHellman(const Integer&, const Integer&, const RandomPool&);
~DiffieHellman();
DiffieHellman(const DiffieHellman&);
DiffieHellman& operator=(const DiffieHellman&);
uint get_agreedKeyLength() const;
const byte* get_agreedKey() const;
const byte* get_publicKey() const;
void makeAgreement(const byte*, unsigned int);
void set_sizes(int&, int&, int&) const;
void get_parms(byte*, byte*, byte*) const;
private:
struct DHImpl;
DHImpl* pimpl_;
};
// Lagrge Integer
class Integer {
public:
Integer();
~Integer();
Integer(const Integer&);
Integer& operator=(const Integer&);
void assign(const byte*, unsigned int);
friend class DiffieHellman;
private:
struct IntegerImpl;
IntegerImpl* pimpl_;
};
class x509;
x509* PemToDer(FILE*, CertType);
} // naemspace
#endif // yaSSL_CRYPTO_WRAPPER_HPP
|