| 1 | // -------------------------------------------------------------------------- |
|---|
| 2 | // |
|---|
| 3 | // File |
|---|
| 4 | // Name: CipherContext.h |
|---|
| 5 | // Purpose: Context for symmetric encryption / descryption |
|---|
| 6 | // Created: 1/12/03 |
|---|
| 7 | // |
|---|
| 8 | // -------------------------------------------------------------------------- |
|---|
| 9 | |
|---|
| 10 | #ifndef CIPHERCONTEXT__H |
|---|
| 11 | #define CIPHERCONTEXT__H |
|---|
| 12 | |
|---|
| 13 | #ifdef BOX_LIB_CRYPTO_OPENSSL_HEADERS_INCLUDED_FALSE |
|---|
| 14 | always include CipherContext.h first in any .cpp file |
|---|
| 15 | #endif |
|---|
| 16 | #define BOX_LIB_CRYPTO_OPENSSL_HEADERS_INCLUDED_TRUE |
|---|
| 17 | #include <openssl/evp.h> |
|---|
| 18 | class CipherDescription; |
|---|
| 19 | |
|---|
| 20 | #define CIPHERCONTEXT_MAX_GENERATED_IV_LENGTH 32 |
|---|
| 21 | |
|---|
| 22 | // -------------------------------------------------------------------------- |
|---|
| 23 | // |
|---|
| 24 | // Class |
|---|
| 25 | // Name: CipherContext |
|---|
| 26 | // Purpose: Context for symmetric encryption / descryption |
|---|
| 27 | // Created: 1/12/03 |
|---|
| 28 | // |
|---|
| 29 | // -------------------------------------------------------------------------- |
|---|
| 30 | class CipherContext |
|---|
| 31 | { |
|---|
| 32 | public: |
|---|
| 33 | CipherContext(); |
|---|
| 34 | ~CipherContext(); |
|---|
| 35 | private: |
|---|
| 36 | CipherContext(const CipherContext &); // no copying |
|---|
| 37 | CipherContext &operator=(const CipherContext &); // no assignment |
|---|
| 38 | protected: |
|---|
| 39 | std::string LogError(const std::string& operation); |
|---|
| 40 | public: |
|---|
| 41 | |
|---|
| 42 | typedef enum |
|---|
| 43 | { |
|---|
| 44 | None = 0, |
|---|
| 45 | Decrypt, |
|---|
| 46 | Encrypt |
|---|
| 47 | } CipherFunction; |
|---|
| 48 | |
|---|
| 49 | void Init(CipherContext::CipherFunction Function, const CipherDescription &rDescription); |
|---|
| 50 | void Reset(); |
|---|
| 51 | |
|---|
| 52 | void Begin(); |
|---|
| 53 | int Transform(void *pOutBuffer, int OutLength, const void *pInBuffer, int InLength); |
|---|
| 54 | int Final(void *pOutBuffer, int OutLength); |
|---|
| 55 | int InSizeForOutBufferSize(int OutLength); |
|---|
| 56 | int MaxOutSizeForInBufferSize(int InLength); |
|---|
| 57 | |
|---|
| 58 | int TransformBlock(void *pOutBuffer, int OutLength, const void *pInBuffer, int InLength); |
|---|
| 59 | |
|---|
| 60 | bool IsInitialised() {return mInitialised;} |
|---|
| 61 | |
|---|
| 62 | int GetIVLength(); |
|---|
| 63 | void SetIV(const void *pIV); |
|---|
| 64 | const void *SetRandomIV(int &rLengthOut); |
|---|
| 65 | |
|---|
| 66 | void UsePadding(bool Padding = true); |
|---|
| 67 | const char* GetFunction() const |
|---|
| 68 | { |
|---|
| 69 | return (mFunction == Encrypt) ? "encrypt" : "decrypt"; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | #ifdef HAVE_OLD_SSL |
|---|
| 73 | void OldOpenSSLFinal(unsigned char *Buffer, int &rOutLengthOut); |
|---|
| 74 | #endif |
|---|
| 75 | |
|---|
| 76 | private: |
|---|
| 77 | EVP_CIPHER_CTX ctx; |
|---|
| 78 | bool mInitialised; |
|---|
| 79 | bool mWithinTransform; |
|---|
| 80 | bool mPaddingOn; |
|---|
| 81 | uint8_t mGeneratedIV[CIPHERCONTEXT_MAX_GENERATED_IV_LENGTH]; |
|---|
| 82 | CipherFunction mFunction; |
|---|
| 83 | std::string mCipherName; |
|---|
| 84 | #ifdef HAVE_OLD_SSL |
|---|
| 85 | CipherDescription *mpDescription; |
|---|
| 86 | #endif |
|---|
| 87 | }; |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | #endif // CIPHERCONTEXT__H |
|---|
| 91 | |
|---|