AWS SDK for C++  0.14.3
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  void CheckInitEncryptor();
197  void CheckInitDecryptor();
198 
199  private:
200  void Init();
201  void Cleanup();
202 
203  bool m_encDecInitialized;
204  bool m_encryptionMode;
205  bool m_decryptionMode;
206  };
207 
212  {
213  public:
218 
222  AES_CBC_Cipher_OpenSSL(CryptoBuffer&& key, CryptoBuffer&& initializationVector);
223 
227  AES_CBC_Cipher_OpenSSL(const CryptoBuffer& key, const CryptoBuffer& initializationVector);
228 
229  AES_CBC_Cipher_OpenSSL(const AES_CBC_Cipher_OpenSSL& other) = delete;
230 
231  AES_CBC_Cipher_OpenSSL& operator=(const AES_CBC_Cipher_OpenSSL& other) = delete;
232 
234 
235  protected:
236  void InitEncryptor_Internal() override;
237 
238  void InitDecryptor_Internal() override;
239 
240  size_t GetBlockSizeBytes() const override;
241 
242  size_t GetKeyLengthBits() const override;
243 
244  private:
245  static size_t BlockSizeBytes;
246  static size_t KeyLengthBits;
247  };
248 
253  {
254  public:
260 
264  AES_CTR_Cipher_OpenSSL(CryptoBuffer&& key, CryptoBuffer&& initializationVector);
265 
269  AES_CTR_Cipher_OpenSSL(const CryptoBuffer& key, const CryptoBuffer& initializationVector);
270 
271  AES_CTR_Cipher_OpenSSL(const AES_CTR_Cipher_OpenSSL& other) = delete;
272 
273  AES_CTR_Cipher_OpenSSL& operator=(const AES_CTR_Cipher_OpenSSL& other) = delete;
274 
276 
277  protected:
278  void InitEncryptor_Internal() override;
279 
280  void InitDecryptor_Internal() override;
281 
282  size_t GetBlockSizeBytes() const override;
283 
284  size_t GetKeyLengthBits() const override;
285 
286  private:
287  static size_t BlockSizeBytes;
288  static size_t KeyLengthBits;
289  };
290 
295  {
296  public:
301 
306  AES_GCM_Cipher_OpenSSL(CryptoBuffer&& key, CryptoBuffer&& initializationVector,
307  CryptoBuffer&& tag = CryptoBuffer(0));
308 
313  AES_GCM_Cipher_OpenSSL(const CryptoBuffer& key, const CryptoBuffer& initializationVector,
314  const CryptoBuffer& tag = CryptoBuffer(0));
315 
316  AES_GCM_Cipher_OpenSSL(const AES_GCM_Cipher_OpenSSL& other) = delete;
317 
318  AES_GCM_Cipher_OpenSSL& operator=(const AES_GCM_Cipher_OpenSSL& other) = delete;
319 
321 
327  CryptoBuffer FinalizeEncryption() override;
328 
329  protected:
330  void InitEncryptor_Internal() override;
331 
332  void InitDecryptor_Internal() override;
333 
334  size_t GetBlockSizeBytes() const override;
335 
336  size_t GetKeyLengthBits() const override;
337 
338  size_t GetTagLengthBytes() const;
339 
340  private:
341  static size_t BlockSizeBytes;
342  static size_t IVLengthBytes;
343  static size_t KeyLengthBits;
344  static size_t TagLengthBytes;
345  };
346 
352  {
353  public:
354 
359 
361 
362  AES_KeyWrap_Cipher_OpenSSL& operator=(const AES_KeyWrap_Cipher_OpenSSL&) = delete;
363 
365 
366  CryptoBuffer EncryptBuffer(const CryptoBuffer&) override;
367  CryptoBuffer FinalizeEncryption() override;
368 
369  CryptoBuffer DecryptBuffer(const CryptoBuffer&) override;
370  CryptoBuffer FinalizeDecryption() override;
371 
372  protected:
373  void InitEncryptor_Internal() override;
374 
375  void InitDecryptor_Internal() override;
376 
377  inline size_t GetBlockSizeBytes() const override { return BlockSizeBytes; }
378 
379  inline size_t GetKeyLengthBits() const override { return KeyLengthBits; }
380 
381  private:
382  static size_t BlockSizeBytes;
383  static size_t KeyLengthBits;
384 
385  CryptoBuffer m_workingKeyBuffer;
386  };
387 
388  } // namespace Crypto
389  } // namespace Utils
390 } // 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).