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
|
/*
* Copyright (c) 2010 SURFnet bv
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*****************************************************************************
OSSLCryptoFactory.cpp
This is an OpenSSL based cryptographic algorithm factory
*****************************************************************************/
#include "config.h"
#include "MutexFactory.h"
#include "OSSLCryptoFactory.h"
#include "OSSLRNG.h"
#include "OSSLAES.h"
#include "OSSLDES.h"
#include "OSSLMD5.h"
#include "OSSLSHA1.h"
#include "OSSLSHA224.h"
#include "OSSLSHA256.h"
#include "OSSLSHA384.h"
#include "OSSLSHA512.h"
#include "OSSLCMAC.h"
#include "OSSLHMAC.h"
#include "OSSLRSA.h"
#include "OSSLDSA.h"
#include "OSSLDH.h"
#ifdef WITH_ECC
#include "OSSLECDH.h"
#include "OSSLECDSA.h"
#endif
#ifdef WITH_GOST
#include "OSSLGOSTR3411.h"
#include "OSSLGOST.h"
#endif
#ifdef WITH_EDDSA
#include "OSSLEDDSA.h"
#endif
#include <algorithm>
#include <string.h>
#include <openssl/opensslv.h>
#include <openssl/ssl.h>
#include <openssl/crypto.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#ifdef WITH_GOST
#include <openssl/objects.h>
#endif
#ifdef WITH_FIPS
// Initialise the FIPS 140-2 selftest status
bool OSSLCryptoFactory::FipsSelfTestStatus = false;
#endif
static unsigned nlocks;
static Mutex** locks;
// Mutex callback
void lock_callback(int mode, int n, const char* file, int line)
{
if ((unsigned) n >= nlocks)
{
ERROR_MSG("out of range [0..%u[ lock %d at %s:%d",
nlocks, n, file, line);
return;
}
Mutex* mtx = locks[(unsigned) n];
if (mode & CRYPTO_LOCK)
{
mtx->lock();
}
else
{
mtx->unlock();
}
}
// Constructor
OSSLCryptoFactory::OSSLCryptoFactory()
{
// Multi-thread support
nlocks = CRYPTO_num_locks();
locks = new Mutex*[nlocks];
for (unsigned i = 0; i < nlocks; i++)
{
locks[i] = MutexFactory::i()->getMutex();
}
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
setLockingCallback = false;
if (CRYPTO_get_locking_callback() == NULL)
{
CRYPTO_set_locking_callback(lock_callback);
setLockingCallback = true;
}
#endif
#ifdef WITH_FIPS
// Already in FIPS mode on reenter (avoiding selftests)
if (!FIPS_mode())
{
FipsSelfTestStatus = false;
if (!FIPS_mode_set(1))
{
ERROR_MSG("can't enter into FIPS mode");
return;
}
} else {
// Undo RAND_cleanup()
RAND_init_fips();
}
FipsSelfTestStatus = true;
#endif
// Initialise OpenSSL
OpenSSL_add_all_algorithms();
#if !( OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) )
// Make sure RDRAND is loaded first
ENGINE_load_rdrand();
#endif
// Locate the engine
rdrand_engine = ENGINE_by_id("rdrand");
// Use RDRAND if available
if (rdrand_engine != NULL)
{
// Initialize RDRAND engine
if (!ENGINE_init(rdrand_engine))
{
WARNING_MSG("ENGINE_init returned %lu\n", ERR_get_error());
}
// Set RDRAND engine as the default for RAND_ methods
else if (!ENGINE_set_default(rdrand_engine, ENGINE_METHOD_RAND))
{
WARNING_MSG("ENGINE_set_default returned %lu\n", ERR_get_error());
}
}
// Initialise the one-and-only RNG
rng = new OSSLRNG();
#ifdef WITH_GOST
// Load engines
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
ENGINE_load_builtin_engines();
#else
OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_ALL_BUILTIN |
OPENSSL_INIT_ENGINE_RDRAND |
OPENSSL_INIT_LOAD_CRYPTO_STRINGS |
OPENSSL_INIT_ADD_ALL_CIPHERS |
OPENSSL_INIT_ADD_ALL_DIGESTS |
OPENSSL_INIT_LOAD_CONFIG, NULL);
#endif
// Initialise the GOST engine
eg = ENGINE_by_id("gost");
if (eg == NULL)
{
ERROR_MSG("can't get the GOST engine");
return;
}
if (ENGINE_init(eg) <= 0)
{
ENGINE_free(eg);
eg = NULL;
ERROR_MSG("can't initialize the GOST engine");
return;
}
// better than digest_gost
EVP_GOST_34_11 = ENGINE_get_digest(eg, NID_id_GostR3411_94);
if (EVP_GOST_34_11 == NULL)
{
ERROR_MSG("can't get the GOST digest");
goto err;
}
// from the openssl.cnf
if (ENGINE_register_pkey_asn1_meths(eg) <= 0)
{
ERROR_MSG("can't register ASN.1 for the GOST engine");
goto err;
}
if (ENGINE_ctrl_cmd_string(eg,
"CRYPT_PARAMS",
"id-Gost28147-89-CryptoPro-A-ParamSet",
0) <= 0)
{
ERROR_MSG("can't set params of the GOST engine");
goto err;
}
return;
err:
ENGINE_finish(eg);
ENGINE_free(eg);
eg = NULL;
return;
#endif
}
// Destructor
OSSLCryptoFactory::~OSSLCryptoFactory()
{
#ifdef WITH_GOST
// Finish the GOST engine
if (eg != NULL)
{
ENGINE_finish(eg);
ENGINE_free(eg);
eg = NULL;
}
#endif
// Destroy the one-and-only RNG
delete rng;
// Recycle locks
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
if (setLockingCallback)
{
CRYPTO_set_locking_callback(NULL);
}
#endif
for (unsigned i = 0; i < nlocks; i++)
{
MutexFactory::i()->recycleMutex(locks[i]);
}
delete[] locks;
}
// Return the one-and-only instance
OSSLCryptoFactory* OSSLCryptoFactory::i()
{
if (!instance.get())
{
instance.reset(new OSSLCryptoFactory());
}
return instance.get();
}
// This will destroy the one-and-only instance.
void OSSLCryptoFactory::reset()
{
instance.reset();
}
#ifdef WITH_FIPS
bool OSSLCryptoFactory::getFipsSelfTestStatus() const
{
return FipsSelfTestStatus;
}
#endif
// Create a concrete instance of a symmetric algorithm
SymmetricAlgorithm* OSSLCryptoFactory::getSymmetricAlgorithm(SymAlgo::Type algorithm)
{
switch (algorithm)
{
case SymAlgo::AES:
return new OSSLAES();
case SymAlgo::DES:
case SymAlgo::DES3:
return new OSSLDES();
default:
// No algorithm implementation is available
ERROR_MSG("Unknown algorithm '%i'", algorithm);
return NULL;
}
// No algorithm implementation is available
return NULL;
}
// Create a concrete instance of an asymmetric algorithm
AsymmetricAlgorithm* OSSLCryptoFactory::getAsymmetricAlgorithm(AsymAlgo::Type algorithm)
{
switch (algorithm)
{
case AsymAlgo::RSA:
return new OSSLRSA();
case AsymAlgo::DSA:
return new OSSLDSA();
case AsymAlgo::DH:
return new OSSLDH();
#ifdef WITH_ECC
case AsymAlgo::ECDH:
return new OSSLECDH();
case AsymAlgo::ECDSA:
return new OSSLECDSA();
#endif
#ifdef WITH_GOST
case AsymAlgo::GOST:
return new OSSLGOST();
#endif
#ifdef WITH_EDDSA
case AsymAlgo::EDDSA:
return new OSSLEDDSA();
#endif
default:
// No algorithm implementation is available
ERROR_MSG("Unknown algorithm '%i'", algorithm);
return NULL;
}
// No algorithm implementation is available
return NULL;
}
// Create a concrete instance of a hash algorithm
HashAlgorithm* OSSLCryptoFactory::getHashAlgorithm(HashAlgo::Type algorithm)
{
switch (algorithm)
{
case HashAlgo::MD5:
return new OSSLMD5();
case HashAlgo::SHA1:
return new OSSLSHA1();
case HashAlgo::SHA224:
return new OSSLSHA224();
case HashAlgo::SHA256:
return new OSSLSHA256();
case HashAlgo::SHA384:
return new OSSLSHA384();
case HashAlgo::SHA512:
return new OSSLSHA512();
#ifdef WITH_GOST
case HashAlgo::GOST:
return new OSSLGOSTR3411();
#endif
default:
// No algorithm implementation is available
ERROR_MSG("Unknown algorithm '%i'", algorithm);
return NULL;
}
// No algorithm implementation is available
return NULL;
}
// Create a concrete instance of a MAC algorithm
MacAlgorithm* OSSLCryptoFactory::getMacAlgorithm(MacAlgo::Type algorithm)
{
switch (algorithm)
{
case MacAlgo::HMAC_MD5:
return new OSSLHMACMD5();
case MacAlgo::HMAC_SHA1:
return new OSSLHMACSHA1();
case MacAlgo::HMAC_SHA224:
return new OSSLHMACSHA224();
case MacAlgo::HMAC_SHA256:
return new OSSLHMACSHA256();
case MacAlgo::HMAC_SHA384:
return new OSSLHMACSHA384();
case MacAlgo::HMAC_SHA512:
return new OSSLHMACSHA512();
#ifdef WITH_GOST
case MacAlgo::HMAC_GOST:
return new OSSLHMACGOSTR3411();
#endif
case MacAlgo::CMAC_DES:
return new OSSLCMACDES();
case MacAlgo::CMAC_AES:
return new OSSLCMACAES();
default:
// No algorithm implementation is available
ERROR_MSG("Unknown algorithm '%i'", algorithm);
return NULL;
}
// No algorithm implementation is available
return NULL;
}
// Get the global RNG (may be an unique RNG per thread)
RNG* OSSLCryptoFactory::getRNG(RNGImpl::Type name /* = RNGImpl::Default */)
{
if (name == RNGImpl::Default)
{
return rng;
}
else
{
// No RNG implementation is available
ERROR_MSG("Unknown RNG '%i'", name);
return NULL;
}
}
|