NingSun | 0c89b3c | 2018-02-08 08:34:03 -0800 | [diff] [blame^] | 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.h |
| 30 | |
| 31 | This is a Botan based cryptographic algorithm factory |
| 32 | *****************************************************************************/ |
| 33 | |
| 34 | #ifndef _SOFTHSM_V2_BOTANCRYPTOFACTORY_H |
| 35 | #define _SOFTHSM_V2_BOTANCRYPTOFACTORY_H |
| 36 | |
| 37 | #ifdef HAVE_PTHREAD_H |
| 38 | #include <pthread.h> |
| 39 | #endif |
| 40 | |
| 41 | #include "config.h" |
| 42 | #include "CryptoFactory.h" |
| 43 | #include "SymmetricAlgorithm.h" |
| 44 | #include "AsymmetricAlgorithm.h" |
| 45 | #include "HashAlgorithm.h" |
| 46 | #include "MacAlgorithm.h" |
| 47 | #include "RNG.h" |
| 48 | #include "MutexFactory.h" |
| 49 | #include <memory> |
| 50 | #include <map> |
| 51 | #include <botan/version.h> |
| 52 | |
| 53 | class BotanCryptoFactory : public CryptoFactory |
| 54 | { |
| 55 | public: |
| 56 | // Return the one-and-only instance |
| 57 | static BotanCryptoFactory* i(); |
| 58 | |
| 59 | // This will destroy the one-and-only instance. |
| 60 | static void reset(); |
| 61 | |
| 62 | // Create a concrete instance of a symmetric algorithm |
| 63 | SymmetricAlgorithm* getSymmetricAlgorithm(SymAlgo::Type algorithm); |
| 64 | |
| 65 | // Create a concrete instance of an asymmetric algorithm |
| 66 | AsymmetricAlgorithm* getAsymmetricAlgorithm(AsymAlgo::Type algorithm); |
| 67 | |
| 68 | // Create a concrete instance of a hash algorithm |
| 69 | HashAlgorithm* getHashAlgorithm(HashAlgo::Type algorithm); |
| 70 | |
| 71 | // Create a concrete instance of a MAC algorithm |
| 72 | MacAlgorithm* getMacAlgorithm(MacAlgo::Type algorithm); |
| 73 | |
| 74 | // Get the global RNG (may be an unique RNG per thread) |
| 75 | RNG* getRNG(RNGImpl::Type name = RNGImpl::Default); |
| 76 | |
| 77 | // Destructor |
| 78 | ~BotanCryptoFactory(); |
| 79 | |
| 80 | private: |
| 81 | // Constructor |
| 82 | BotanCryptoFactory(); |
| 83 | |
| 84 | // The one-and-only instance |
| 85 | #ifdef HAVE_CXX11 |
| 86 | static std::unique_ptr<BotanCryptoFactory> instance; |
| 87 | #else |
| 88 | static std::auto_ptr<BotanCryptoFactory> instance; |
| 89 | #endif |
| 90 | |
| 91 | // Thread specific RNG |
| 92 | #ifdef HAVE_PTHREAD_H |
| 93 | std::map<pthread_t, RNG*> rngs; |
| 94 | #elif _WIN32 |
| 95 | std::map<DWORD, RNG*> rngs; |
| 96 | #endif |
| 97 | Mutex* rngsMutex; |
| 98 | |
| 99 | #if BOTAN_VERSION_CODE < BOTAN_VERSION_CODE_FOR(1,11,14) |
| 100 | bool wasInitialized; |
| 101 | #endif |
| 102 | }; |
| 103 | |
| 104 | #endif // !_SOFTHSM_V2_BOTANCRYPTOFACTORY_H |
| 105 | |