| 1 |
/* |
| 2 |
* Copyright (c) 2010 SURFnet bv |
| 3 |
* Copyright (c) 2010 .SE (The Internet Infrastructure Foundation) |
| 4 |
* All rights reserved. |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions |
| 8 |
* are met: |
| 9 |
* 1. Redistributions of source code must retain the above copyright |
| 10 |
* notice, this list of conditions and the following disclaimer. |
| 11 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 12 |
* notice, this list of conditions and the following disclaimer in the |
| 13 |
* documentation and/or other materials provided with the distribution. |
| 14 |
* |
| 15 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 16 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 17 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 18 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 19 |
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 20 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 21 |
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 22 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
| 23 |
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 25 |
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 |
*/ |
| 27 |
|
| 28 |
/***************************************************************************** |
| 29 |
BotanCryptoFactory.cpp |
| 30 |
|
| 31 |
This is a Botan based cryptographic algorithm factory |
| 32 |
*****************************************************************************/ |
| 33 |
#include "config.h" |
| 34 |
#include "BotanCryptoFactory.h" |
| 35 |
#include "BotanAES.h" |
| 36 |
#include "BotanDES.h" |
| 37 |
#include "BotanDSA.h" |
| 38 |
#include "BotanDH.h" |
| 39 |
#ifdef WITH_ECC |
| 40 |
#include "BotanECDH.h" |
| 41 |
#include "BotanECDSA.h" |
| 42 |
#endif |
| 43 |
#include "BotanMD5.h" |
| 44 |
#include "BotanRNG.h" |
| 45 |
#include "BotanRSA.h" |
| 46 |
#include "BotanSHA1.h" |
| 47 |
#include "BotanSHA224.h" |
| 48 |
#include "BotanSHA256.h" |
| 49 |
#include "BotanSHA384.h" |
| 50 |
#include "BotanSHA512.h" |
| 51 |
#ifdef WITH_GOST |
| 52 |
#include "BotanGOST.h" |
| 53 |
#include "BotanGOSTR3411.h" |
| 54 |
#endif |
| 55 |
#include "BotanMAC.h" |
| 56 |
#ifdef WITH_EDDSA |
| 57 |
#include "BotanEDDSA.h" |
| 58 |
#endif |
| 59 |
|
| 60 |
// Constructor |
| 61 |
BotanCryptoFactory::BotanCryptoFactory() |
| 62 |
{ |
| 63 |
// Create mutex |
| 64 |
rngsMutex = MutexFactory::i()->getMutex(); |
| 65 |
} |
| 66 |
|
| 67 |
// Destructor |
| 68 |
BotanCryptoFactory::~BotanCryptoFactory() |
| 69 |
{ |
| 70 |
// Delete the RNGs |
| 71 |
#ifdef HAVE_PTHREAD_H |
| 72 |
std::map<pthread_t,RNG*>::iterator it; |
| 73 |
for (it=rngs.begin(); it != rngs.end(); it++) |
| 74 |
{ |
| 75 |
delete (BotanRNG*)it->second; |
| 76 |
} |
| 77 |
#elif _WIN32 |
| 78 |
std::map<DWORD,RNG*>::iterator it; |
| 79 |
for (it=rngs.begin(); it != rngs.end(); it++) |
| 80 |
{ |
| 81 |
delete (BotanRNG*)it->second; |
| 82 |
} |
| 83 |
#endif |
| 84 |
|
| 85 |
// Delete the mutex |
| 86 |
MutexFactory::i()->recycleMutex(rngsMutex); |
| 87 |
} |
| 88 |
|
| 89 |
// Return the one-and-only instance |
| 90 |
BotanCryptoFactory* BotanCryptoFactory::i() |
| 91 |
{ |
| 92 |
if (!instance.get()) |
| 93 |
{ |
| 94 |
instance.reset(new BotanCryptoFactory()); |
| 95 |
} |
| 96 |
|
| 97 |
return instance.get(); |
| 98 |
} |
| 99 |
|
| 100 |
// This will destroy the one-and-only instance. |
| 101 |
void BotanCryptoFactory::reset() |
| 102 |
{ |
| 103 |
instance.reset(); |
| 104 |
} |
| 105 |
|
| 106 |
// Create a concrete instance of a symmetric algorithm |
| 107 |
SymmetricAlgorithm* BotanCryptoFactory::getSymmetricAlgorithm(SymAlgo::Type algorithm) |
| 108 |
{ |
| 109 |
switch (algorithm) |
| 110 |
{ |
| 111 |
case SymAlgo::AES: |
| 112 |
return new BotanAES(); |
| 113 |
case SymAlgo::DES: |
| 114 |
case SymAlgo::DES3: |
| 115 |
return new BotanDES(); |
| 116 |
default: |
| 117 |
// No algorithm implementation is available |
| 118 |
ERROR_MSG("Unknown algorithm '%i'", algorithm); |
| 119 |
|
| 120 |
return NULL; |
| 121 |
} |
| 122 |
|
| 123 |
// No algorithm implementation is available |
| 124 |
return NULL; |
| 125 |
} |
| 126 |
|
| 127 |
// Create a concrete instance of an asymmetric algorithm |
| 128 |
AsymmetricAlgorithm* BotanCryptoFactory::getAsymmetricAlgorithm(AsymAlgo::Type algorithm) |
| 129 |
{ |
| 130 |
switch (algorithm) |
| 131 |
{ |
| 132 |
case AsymAlgo::RSA: |
| 133 |
return new BotanRSA(); |
| 134 |
case AsymAlgo::DSA: |
| 135 |
return new BotanDSA(); |
| 136 |
case AsymAlgo::DH: |
| 137 |
return new BotanDH(); |
| 138 |
#ifdef WITH_ECC |
| 139 |
case AsymAlgo::ECDH: |
| 140 |
return new BotanECDH(); |
| 141 |
case AsymAlgo::ECDSA: |
| 142 |
return new BotanECDSA(); |
| 143 |
#endif |
| 144 |
#ifdef WITH_GOST |
| 145 |
case AsymAlgo::GOST: |
| 146 |
return new BotanGOST(); |
| 147 |
#endif |
| 148 |
#ifdef WITH_EDDSA |
| 149 |
case AsymAlgo::EDDSA: |
| 150 |
return new BotanEDDSA(); |
| 151 |
#endif |
| 152 |
default: |
| 153 |
// No algorithm implementation is available |
| 154 |
ERROR_MSG("Unknown algorithm '%i'", algorithm); |
| 155 |
|
| 156 |
return NULL; |
| 157 |
} |
| 158 |
|
| 159 |
// No algorithm implementation is available |
| 160 |
return NULL; |
| 161 |
} |
| 162 |
|
| 163 |
// Create a concrete instance of a hash algorithm |
| 164 |
HashAlgorithm* BotanCryptoFactory::getHashAlgorithm(HashAlgo::Type algorithm) |
| 165 |
{ |
| 166 |
switch (algorithm) |
| 167 |
{ |
| 168 |
case HashAlgo::MD5: |
| 169 |
return new BotanMD5(); |
| 170 |
case HashAlgo::SHA1: |
| 171 |
return new BotanSHA1(); |
| 172 |
case HashAlgo::SHA224: |
| 173 |
return new BotanSHA224(); |
| 174 |
case HashAlgo::SHA256: |
| 175 |
return new BotanSHA256(); |
| 176 |
case HashAlgo::SHA384: |
| 177 |
return new BotanSHA384(); |
| 178 |
case HashAlgo::SHA512: |
| 179 |
return new BotanSHA512(); |
| 180 |
#ifdef WITH_GOST |
| 181 |
case HashAlgo::GOST: |
| 182 |
return new BotanGOSTR3411(); |
| 183 |
#endif |
| 184 |
default: |
| 185 |
// No algorithm implementation is available |
| 186 |
ERROR_MSG("Unknown algorithm '%i'", algorithm); |
| 187 |
|
| 188 |
return NULL; |
| 189 |
} |
| 190 |
|
| 191 |
// No algorithm implementation is available |
| 192 |
return NULL; |
| 193 |
} |
| 194 |
|
| 195 |
// Create a concrete instance of a MAC algorithm |
| 196 |
MacAlgorithm* BotanCryptoFactory::getMacAlgorithm(MacAlgo::Type algorithm) |
| 197 |
{ |
| 198 |
switch (algorithm) |
| 199 |
{ |
| 200 |
case MacAlgo::HMAC_MD5: |
| 201 |
return new BotanHMACMD5(); |
| 202 |
case MacAlgo::HMAC_SHA1: |
| 203 |
return new BotanHMACSHA1(); |
| 204 |
case MacAlgo::HMAC_SHA224: |
| 205 |
return new BotanHMACSHA224(); |
| 206 |
case MacAlgo::HMAC_SHA256: |
| 207 |
return new BotanHMACSHA256(); |
| 208 |
case MacAlgo::HMAC_SHA384: |
| 209 |
return new BotanHMACSHA384(); |
| 210 |
case MacAlgo::HMAC_SHA512: |
| 211 |
return new BotanHMACSHA512(); |
| 212 |
#ifdef WITH_GOST |
| 213 |
case MacAlgo::HMAC_GOST: |
| 214 |
return new BotanHMACGOSTR3411(); |
| 215 |
#endif |
| 216 |
case MacAlgo::CMAC_DES: |
| 217 |
return new BotanCMACDES(); |
| 218 |
case MacAlgo::CMAC_AES: |
| 219 |
return new BotanCMACAES(); |
| 220 |
default: |
| 221 |
// No algorithm implementation is available |
| 222 |
ERROR_MSG("Unknown algorithm '%i'", algorithm); |
| 223 |
|
| 224 |
return NULL; |
| 225 |
} |
| 226 |
|
| 227 |
// No algorithm implementation is available |
| 228 |
return NULL; |
| 229 |
} |
| 230 |
|
| 231 |
// Get the global RNG (may be an unique RNG per thread) |
| 232 |
RNG* BotanCryptoFactory::getRNG(RNGImpl::Type name /* = RNGImpl::Default */) |
| 233 |
{ |
| 234 |
if (name == RNGImpl::Default) |
| 235 |
{ |
| 236 |
RNG *threadRNG = NULL; |
| 237 |
|
| 238 |
// Lock access to the map |
| 239 |
MutexLocker lock(rngsMutex); |
| 240 |
|
| 241 |
#ifdef HAVE_PTHREAD_H |
| 242 |
// Get thread ID |
| 243 |
pthread_t threadID = pthread_self(); |
| 244 |
|
| 245 |
// Find the RNG |
| 246 |
std::map<pthread_t,RNG*>::iterator findIt; |
| 247 |
findIt=rngs.find(threadID); |
| 248 |
if (findIt != rngs.end()) |
| 249 |
{ |
| 250 |
return findIt->second; |
| 251 |
} |
| 252 |
|
| 253 |
threadRNG = new BotanRNG(); |
| 254 |
rngs[threadID] = threadRNG; |
| 255 |
#elif _WIN32 |
| 256 |
// Get thread ID |
| 257 |
DWORD threadID = GetCurrentThreadId(); |
| 258 |
|
| 259 |
// Find the RNG |
| 260 |
std::map<DWORD,RNG*>::iterator findIt; |
| 261 |
findIt=rngs.find(threadID); |
| 262 |
if (findIt != rngs.end()) |
| 263 |
{ |
| 264 |
return findIt->second; |
| 265 |
} |
| 266 |
|
| 267 |
threadRNG = new BotanRNG(); |
| 268 |
rngs[threadID] = threadRNG; |
| 269 |
#else |
| 270 |
#error "There are no thread-specific data implementations for your operating system yet" |
| 271 |
#endif |
| 272 |
return threadRNG; |
| 273 |
} |
| 274 |
else |
| 275 |
{ |
| 276 |
// No RNG implementation is available |
| 277 |
ERROR_MSG("Unknown RNG '%i'", name); |
| 278 |
|
| 279 |
return NULL; |
| 280 |
} |
| 281 |
} |