1 //
2 // CipherKeyImpl.h
3 //
4 // Library: Crypto
5 // Package: Cipher
6 // Module:  CipherKeyImpl
7 //
8 // Definition of the CipherKeyImpl class.
9 //
10 // Copyright (c) 2008, Applied Informatics Software Engineering GmbH.
11 // and Contributors.
12 //
13 // SPDX-License-Identifier:	BSL-1.0
14 //
15 
16 
17 #ifndef Crypto_CipherKeyImpl_INCLUDED
18 #define Crypto_CipherKeyImpl_INCLUDED
19 
20 
21 #include "Poco/Crypto/Crypto.h"
22 #include "Poco/Crypto/OpenSSLInitializer.h"
23 #include "Poco/RefCountedObject.h"
24 #include "Poco/AutoPtr.h"
25 #include <vector>
26 
27 
28 struct evp_cipher_st;
29 typedef struct evp_cipher_st EVP_CIPHER;
30 
31 
32 namespace Poco {
33 namespace Crypto {
34 
35 
36 class CipherKeyImpl: public RefCountedObject
37 	/// An implementation of the CipherKey class for OpenSSL's crypto library.
38 {
39 public:
40 	using Ptr = Poco::AutoPtr<CipherKeyImpl>;
41 	using ByteVec = std::vector<unsigned char>;
42 
43 	enum Mode
44 		/// Cipher mode of operation. This mode determines how multiple blocks
45 		/// are connected; this is essential to improve security.
46 	{
47 		MODE_STREAM_CIPHER,	/// Stream cipher
48 		MODE_ECB,			/// Electronic codebook (plain concatenation)
49 		MODE_CBC,			/// Cipher block chaining (default)
50 		MODE_CFB,			/// Cipher feedback
51 		MODE_OFB,			/// Output feedback
52 		MODE_CTR,           /// Counter mode
53 		MODE_GCM,           /// Galois/Counter mode
54 		MODE_CCM            /// Counter with CBC-MAC
55 	};
56 
57 	CipherKeyImpl(const std::string& name,
58 		const std::string& passphrase,
59 		const std::string& salt,
60 		int iterationCount,
61 		const std::string& digest);
62 		/// Creates a new CipherKeyImpl object, using
63 		/// the given cipher name, passphrase, salt value
64 		/// and iteration count.
65 
66 	CipherKeyImpl(const std::string& name,
67 		const ByteVec& key,
68 		const ByteVec& iv);
69 		/// Creates a new CipherKeyImpl object, using the
70 		/// given cipher name, key and initialization vector.
71 
72 	CipherKeyImpl(const std::string& name);
73 		/// Creates a new CipherKeyImpl object. Autoinitializes key
74 		/// and initialization vector.
75 
76 	virtual ~CipherKeyImpl();
77 		/// Destroys the CipherKeyImpl.
78 
79 	const std::string& name() const;
80 		/// Returns the name of the Cipher.
81 
82 	int keySize() const;
83 		/// Returns the key size of the Cipher.
84 
85 	int blockSize() const;
86 		/// Returns the block size of the Cipher.
87 
88 	int ivSize() const;
89 		/// Returns the IV size of the Cipher.
90 
91 	Mode mode() const;
92 		/// Returns the Cipher's mode of operation.
93 
94 	const ByteVec& getKey() const;
95 		/// Returns the key for the Cipher.
96 
97 	void setKey(const ByteVec& key);
98 		/// Sets the key for the Cipher.
99 
100 	const ByteVec& getIV() const;
101 		/// Returns the initialization vector (IV) for the Cipher.
102 
103 	void setIV(const ByteVec& iv);
104 		/// Sets the initialization vector (IV) for the Cipher.
105 
106 	const EVP_CIPHER* cipher();
107 		/// Returns the cipher object
108 
109 private:
110 	void generateKey(const std::string& passphrase,
111 		const std::string& salt,
112 		int iterationCount);
113 	 	/// Generates key and IV from a password and optional salt string.
114 
115 	void generateKey();
116 		/// Generates key and IV from random data.
117 
118 	void getRandomBytes(ByteVec& vec, std::size_t count);
119 		/// Stores random bytes in vec.
120 
121 private:
122 	const EVP_CIPHER*  _pCipher;
123 	const EVP_MD*      _pDigest;
124 	std::string	       _name;
125 	ByteVec		       _key;
126 	ByteVec		       _iv;
127 	OpenSSLInitializer _openSSLInitializer;
128 };
129 
130 
131 //
132 // Inlines
133 //
name()134 inline const std::string& CipherKeyImpl::name() const
135 {
136 	return _name;
137 }
138 
139 
getKey()140 inline const CipherKeyImpl::ByteVec& CipherKeyImpl::getKey() const
141 {
142 	return _key;
143 }
144 
145 
setKey(const ByteVec & key)146 inline void CipherKeyImpl::setKey(const ByteVec& key)
147 {
148 	poco_assert(key.size() == static_cast<ByteVec::size_type>(keySize()));
149 	_key = key;
150 }
151 
152 
getIV()153 inline const CipherKeyImpl::ByteVec& CipherKeyImpl::getIV() const
154 {
155 	return _iv;
156 }
157 
158 
cipher()159 inline const EVP_CIPHER* CipherKeyImpl::cipher()
160 {
161 	return _pCipher;
162 }
163 
164 
165 } } // namespace Poco::Crypto
166 
167 
168 #endif // Crypto_CipherKeyImpl_INCLUDED
169