AWS SDK for C++  0.12.9
AWS SDK for C++
CryptoImpl.h
Go to the documentation of this file.
1 /*
2 * Copyright 2010-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15 #pragma once
16 
22 #include <openssl/ossl_typ.h>
23 #include <openssl/evp.h>
24 #include <openssl/rand.h>
25 #include <atomic>
26 #include <mutex>
27 
28 namespace Aws
29 {
30  namespace Utils
31  {
32  namespace Crypto
33  {
34  namespace OpenSSL
35  {
37 
38  void init_static_state();
39 
40  void cleanup_static_state();
41 
42  void locking_fn(int mode, int n, const char* file, int line);
43 
44  unsigned long id_fn();
45  }
46 
54  {
55  public:
57  { }
58 
59  ~SecureRandomBytes_OpenSSLImpl() = default;
60 
64  void GetBytes(unsigned char* buffer, size_t bufferSize) override;
65  };
66 
67  class MD5OpenSSLImpl : public Hash
68  {
69  public:
70 
72  { }
73 
74  virtual ~MD5OpenSSLImpl() = default;
75 
76  virtual HashResult Calculate(const Aws::String& str) override;
77 
78  virtual HashResult Calculate(Aws::IStream& stream) override;
79 
80  };
81 
82  class Sha256OpenSSLImpl : public Hash
83  {
84  public:
86  { }
87 
88  virtual ~Sha256OpenSSLImpl() = default;
89 
90  virtual HashResult Calculate(const Aws::String& str) override;
91 
92  virtual HashResult Calculate(Aws::IStream& stream) override;
93  };
94 
95  class Sha256HMACOpenSSLImpl : public HMAC
96  {
97  public:
98 
100  { }
101 
102  virtual ~Sha256HMACOpenSSLImpl() = default;
103 
104  virtual HashResult Calculate(const ByteBuffer& toSign, const ByteBuffer& secret) override;
105  };
106 
111  {
112  public:
116  OpenSSLCipher(const CryptoBuffer& key, size_t ivSize, bool ctrMode = false);
117 
122  OpenSSLCipher(CryptoBuffer&& key, CryptoBuffer&& initializationVector,
123  CryptoBuffer&& tag = CryptoBuffer(0));
124 
129  OpenSSLCipher(const CryptoBuffer& key, const CryptoBuffer& initializationVector,
130  const CryptoBuffer& tag = CryptoBuffer(0));
131 
132  OpenSSLCipher(const OpenSSLCipher& other) = delete;
133 
134  OpenSSLCipher& operator=(const OpenSSLCipher& other) = delete;
135 
141  OpenSSLCipher(OpenSSLCipher&& toMove);
142 
148  OpenSSLCipher& operator=(OpenSSLCipher&& toMove) = default;
149 
150 
151  virtual ~OpenSSLCipher();
152 
158  CryptoBuffer EncryptBuffer(const CryptoBuffer& unEncryptedData) override;
159 
163  CryptoBuffer FinalizeEncryption() override;
164 
170  CryptoBuffer DecryptBuffer(const CryptoBuffer& encryptedData) override;
171 
175  CryptoBuffer FinalizeDecryption() override;
176 
177  void Reset() override;
178 
179  protected:
183  virtual void InitEncryptor_Internal() = 0;
184 
188  virtual void InitDecryptor_Internal() = 0;
189 
190  virtual size_t GetBlockSizeBytes() const = 0;
191 
192  virtual size_t GetKeyLengthBits() const = 0;
193 
194  EVP_CIPHER_CTX m_ctx;
195 
196  private:
197  void Init();
198  void CheckInitEncryptor();
199  void CheckInitDecryptor();
200  void Cleanup();
201 
202  bool m_encDecInitialized;
203  bool m_encryptionMode;
204  bool m_decryptionMode;
205  };
206 
211  {
212  public:
217 
221  AES_CBC_Cipher_OpenSSL(CryptoBuffer&& key, CryptoBuffer&& initializationVector);
222 
226  AES_CBC_Cipher_OpenSSL(const CryptoBuffer& key, const CryptoBuffer& initializationVector);
227 
228  AES_CBC_Cipher_OpenSSL(const AES_CBC_Cipher_OpenSSL& other) = delete;
229 
230  AES_CBC_Cipher_OpenSSL& operator=(const AES_CBC_Cipher_OpenSSL& other) = delete;
231 
233 
234  protected:
235  void InitEncryptor_Internal() override;
236 
237  void InitDecryptor_Internal() override;
238 
239  size_t GetBlockSizeBytes() const override;
240 
241  size_t GetKeyLengthBits() const override;
242 
243  private:
244  static size_t BlockSizeBytes;
245  static size_t KeyLengthBits;
246  };
247 
252  {
253  public:
259 
263  AES_CTR_Cipher_OpenSSL(CryptoBuffer&& key, CryptoBuffer&& initializationVector);
264 
268  AES_CTR_Cipher_OpenSSL(const CryptoBuffer& key, const CryptoBuffer& initializationVector);
269 
270  AES_CTR_Cipher_OpenSSL(const AES_CTR_Cipher_OpenSSL& other) = delete;
271 
272  AES_CTR_Cipher_OpenSSL& operator=(const AES_CTR_Cipher_OpenSSL& other) = delete;
273 
275 
276  protected:
277  void InitEncryptor_Internal() override;
278 
279  void InitDecryptor_Internal() override;
280 
281  size_t GetBlockSizeBytes() const override;
282 
283  size_t GetKeyLengthBits() const override;
284 
285  private:
286  static size_t BlockSizeBytes;
287  static size_t KeyLengthBits;
288  };
289 
294  {
295  public:
300 
305  AES_GCM_Cipher_OpenSSL(CryptoBuffer&& key, CryptoBuffer&& initializationVector,
306  CryptoBuffer&& tag = CryptoBuffer(0));
307 
312  AES_GCM_Cipher_OpenSSL(const CryptoBuffer& key, const CryptoBuffer& initializationVector,
313  const CryptoBuffer& tag = CryptoBuffer(0));
314 
315  AES_GCM_Cipher_OpenSSL(const AES_GCM_Cipher_OpenSSL& other) = delete;
316 
317  AES_GCM_Cipher_OpenSSL& operator=(const AES_GCM_Cipher_OpenSSL& other) = delete;
318 
320 
326  CryptoBuffer FinalizeEncryption() override;
327 
328  protected:
329  void InitEncryptor_Internal() override;
330 
331  void InitDecryptor_Internal() override;
332 
333  size_t GetBlockSizeBytes() const override;
334 
335  size_t GetKeyLengthBits() const override;
336 
337  size_t GetTagLengthBytes() const;
338 
339  private:
340  static size_t BlockSizeBytes;
341  static size_t IVLengthBytes;
342  static size_t KeyLengthBits;
343  static size_t TagLengthBytes;
344  };
345 
346  } // namespace Crypto
347  } // namespace Utils
348 } // namespace Aws
void locking_fn(int mode, int n, const char *file, int line)
std::basic_istream< char, std::char_traits< char > > IStream
Definition: AWSStreamFwd.h:30
std::basic_string< char, std::char_traits< char >, Aws::Allocator< char > > String
Definition: AWSString.h:97
JSON (JavaScript Object Notation).