1 /* 2 * Copyright (c) 2010 SURFnet bv 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 20 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 22 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 24 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 /***************************************************************************** 28 SymmetricAlgorithm.h 29 30 Base class for symmetric algorithm classes 31 *****************************************************************************/ 32 33 #ifndef _SOFTHSM_V2_SYMMETRICALGORITHM_H 34 #define _SOFTHSM_V2_SYMMETRICALGORITHM_H 35 36 #include <string> 37 #include "config.h" 38 #include "SymmetricKey.h" 39 #include "RNG.h" 40 41 struct SymAlgo 42 { 43 enum Type 44 { 45 Unknown, 46 AES, 47 DES, 48 DES3 49 }; 50 }; 51 52 struct SymMode 53 { 54 enum Type 55 { 56 Unknown, 57 CBC, 58 CFB, 59 CTR, 60 ECB, 61 GCM, 62 OFB 63 }; 64 }; 65 66 struct SymWrap 67 { 68 enum Type 69 { 70 Unknown, 71 AES_KEYWRAP, 72 AES_KEYWRAP_PAD 73 }; 74 }; 75 76 class SymmetricAlgorithm 77 { 78 public: 79 // Base constructors 80 SymmetricAlgorithm(); 81 82 // Destructor ~SymmetricAlgorithm()83 virtual ~SymmetricAlgorithm() { } 84 85 // Encryption functions 86 virtual bool encryptInit(const SymmetricKey* key, const SymMode::Type mode = SymMode::CBC, const ByteString& IV = ByteString(), bool padding = true, size_t counterBits = 0, const ByteString& aad = ByteString(), size_t tagBytes = 0); 87 virtual bool encryptUpdate(const ByteString& data, ByteString& encryptedData); 88 virtual bool encryptFinal(ByteString& encryptedData); 89 90 // Decryption functions 91 virtual bool decryptInit(const SymmetricKey* key, const SymMode::Type mode = SymMode::CBC, const ByteString& IV = ByteString(), bool padding = true, size_t counterBits = 0, const ByteString& aad = ByteString(), size_t tagBytes = 0); 92 virtual bool decryptUpdate(const ByteString& encryptedData, ByteString& data); 93 virtual bool decryptFinal(ByteString& data); 94 95 // Wrap/Unwrap keys 96 virtual bool wrapKey(const SymmetricKey* key, const SymWrap::Type mode, const ByteString& in, ByteString& out) = 0; 97 98 virtual bool unwrapKey(const SymmetricKey* key, const SymWrap::Type mode, const ByteString& in, ByteString& out) = 0; 99 100 // Key factory 101 virtual void recycleKey(SymmetricKey* toRecycle); 102 virtual bool generateKey(SymmetricKey& key, RNG* rng = NULL); 103 virtual bool reconstructKey(SymmetricKey& key, const ByteString& serialisedData); 104 105 // Return cipher information 106 virtual size_t getBlockSize() const = 0; 107 virtual SymMode::Type getCipherMode(); 108 virtual bool getPaddingMode(); 109 virtual unsigned long getBufferSize(); 110 virtual size_t getTagBytes(); 111 virtual bool isStreamCipher(); 112 virtual bool isBlockCipher(); 113 virtual bool checkMaximumBytes(unsigned long bytes) = 0; 114 115 protected: 116 // The current key 117 const SymmetricKey* currentKey; 118 119 // The current cipher mode 120 SymMode::Type currentCipherMode; 121 122 // The current padding 123 bool currentPaddingMode; 124 125 // The current counter bits 126 size_t currentCounterBits; 127 128 // The current tag bytes 129 size_t currentTagBytes; 130 131 // The current operation 132 enum 133 { 134 NONE, 135 ENCRYPT, 136 DECRYPT 137 } 138 currentOperation; 139 140 // The current number of bytes in buffer 141 unsigned long currentBufferSize; 142 143 // The current AEAD buffer 144 ByteString currentAEADBuffer; 145 }; 146 147 #endif // !_SOFTHSM_V2_SYMMETRICALGORITHM_H 148 149