AWS SDK for C++  1.9.108
AWS SDK for C++
Cipher.h
Go to the documentation of this file.
1 
6 #pragma once
7 
9 #include <aws/core/utils/Array.h>
11 
12 namespace Aws
13 {
14  namespace Utils
15  {
16  namespace Crypto
17  {
18  static const size_t SYMMETRIC_KEY_LENGTH = 32;
19  static const size_t MIN_IV_LENGTH = 12;
20 
21  AWS_CORE_API CryptoBuffer IncrementCTRCounter(const CryptoBuffer& counter, uint32_t numberOfBlocks);
22 
28  {
29  public:
34  SymmetricCipher(const CryptoBuffer& key, size_t ivSize, bool ivGenerationInCtrMode = false) :
35  m_key(key),
36  m_initializationVector(ivSize > 0 ? GenerateIV(ivSize, ivGenerationInCtrMode) : 0),
37  m_failure(false)
38  {
39  }
40 
44  SymmetricCipher(const CryptoBuffer& key, const CryptoBuffer& initializationVector, const CryptoBuffer& tag = CryptoBuffer(0)) :
45  m_key(key),
46  m_initializationVector(initializationVector),
47  m_tag(tag),
48  m_failure(false)
49  {
50  }
51 
55  SymmetricCipher(CryptoBuffer&& key, CryptoBuffer&& initializationVector, CryptoBuffer&& tag = CryptoBuffer(0)) :
56  m_key(std::move(key)),
57  m_initializationVector(std::move(initializationVector)),
58  m_tag(std::move(tag)),
59  m_failure(false)
60  {
61  }
62 
63  SymmetricCipher(const SymmetricCipher& other) = delete;
64  SymmetricCipher& operator=(const SymmetricCipher& other) = delete;
65 
72  m_key(std::move(toMove.m_key)),
73  m_initializationVector(std::move(toMove.m_initializationVector)),
74  m_tag(std::move(toMove.m_tag)),
75  m_failure(toMove.m_failure)
76  {
77  }
78 
85  {
86  m_key = std::move(toMove.m_key);
87  m_initializationVector = std::move(toMove.m_initializationVector);
88  m_tag = std::move(toMove.m_tag);
89  m_failure = toMove.m_failure;
90 
91  return *this;
92  }
93 
94  virtual ~SymmetricCipher() = default;
95 
100  virtual operator bool() const { return Good(); }
101 
107  virtual CryptoBuffer EncryptBuffer( const CryptoBuffer& unEncryptedData) = 0;
108 
113 
119  virtual CryptoBuffer DecryptBuffer(const CryptoBuffer& encryptedData) = 0;
120 
125 
126  virtual void Reset() = 0;
127 
131  inline const CryptoBuffer& GetIV() const { return m_initializationVector; }
132 
137  inline const CryptoBuffer& GetTag() const { return m_tag; }
138 
139  inline bool Fail() const { return m_failure; }
140  inline bool Good() const { return !Fail(); }
141 
147  static CryptoBuffer GenerateIV(size_t ivLengthBytes, bool ctrMode = false);
148 
152  static CryptoBuffer GenerateKey(size_t keyLengthBytes = SYMMETRIC_KEY_LENGTH);
153 
154  protected:
155 
156  SymmetricCipher() : m_failure(false) {}
157 
161  bool m_failure;
162  };
163 
168  {
169  public:
171 
175  virtual std::shared_ptr<SymmetricCipher> CreateImplementation(const CryptoBuffer& key) const = 0;
179  virtual std::shared_ptr<SymmetricCipher> CreateImplementation(const CryptoBuffer& key, const CryptoBuffer*) const
180  {
181  return CreateImplementation(key);
182  }
186  virtual std::shared_ptr<SymmetricCipher> CreateImplementation(const CryptoBuffer& key, const CryptoBuffer& iv, const CryptoBuffer& tag = CryptoBuffer(0), const CryptoBuffer& aad = CryptoBuffer(0)) const = 0;
190  virtual std::shared_ptr<SymmetricCipher> CreateImplementation(CryptoBuffer&& key, CryptoBuffer&& iv, CryptoBuffer&& tag = CryptoBuffer(0), CryptoBuffer&& aad = CryptoBuffer(0)) const = 0;
191 
196  virtual void InitStaticState() {}
197 
202  virtual void CleanupStaticState() {}
203  };
204  }
205  }
206 }
Aws::Utils::Crypto::SymmetricCipher::SymmetricCipher
SymmetricCipher()
Definition: Cipher.h:156
Aws::Utils::Crypto::SymmetricCipherFactory::CreateImplementation
virtual std::shared_ptr< SymmetricCipher > CreateImplementation(CryptoBuffer &&key, CryptoBuffer &&iv, CryptoBuffer &&tag=CryptoBuffer(0), CryptoBuffer &&aad=CryptoBuffer(0)) const =0
Aws::Utils::Crypto::SymmetricCipher::EncryptBuffer
virtual CryptoBuffer EncryptBuffer(const CryptoBuffer &unEncryptedData)=0
AWS_CORE_API
#define AWS_CORE_API
Definition: Core_EXPORTS.h:25
Aws::Utils::Crypto::SymmetricCipherFactory::InitStaticState
virtual void InitStaticState()
Definition: Cipher.h:196
Aws::Utils::Crypto::SymmetricCipher::SymmetricCipher
SymmetricCipher(const CryptoBuffer &key, const CryptoBuffer &initializationVector, const CryptoBuffer &tag=CryptoBuffer(0))
Definition: Cipher.h:44
Aws::Utils::Crypto::SymmetricCipher::m_tag
CryptoBuffer m_tag
Definition: Cipher.h:160
Aws::Utils::Crypto::SymmetricCipher::GetTag
const CryptoBuffer & GetTag() const
Definition: Cipher.h:137
Aws::Utils::Crypto::SymmetricCipher::Good
bool Good() const
Definition: Cipher.h:140
Aws::Utils::Crypto::SYMMETRIC_KEY_LENGTH
static const size_t SYMMETRIC_KEY_LENGTH
Definition: Cipher.h:18
Aws::Utils::Crypto::SymmetricCipher::SymmetricCipher
SymmetricCipher(SymmetricCipher &&toMove)
Definition: Cipher.h:71
Aws::Utils::Crypto::SymmetricCipher::FinalizeDecryption
virtual CryptoBuffer FinalizeDecryption()=0
Aws::Utils::Crypto::SymmetricCipherFactory::CreateImplementation
virtual std::shared_ptr< SymmetricCipher > CreateImplementation(const CryptoBuffer &key, const CryptoBuffer *) const
Definition: Cipher.h:179
AWSStreamFwd.h
Aws::Utils::Crypto::SymmetricCipher::~SymmetricCipher
virtual ~SymmetricCipher()=default
Aws::Utils::Crypto::SymmetricCipherFactory::CreateImplementation
virtual std::shared_ptr< SymmetricCipher > CreateImplementation(const CryptoBuffer &key) const =0
Aws::Utils::Crypto::SymmetricCipher::m_initializationVector
CryptoBuffer m_initializationVector
Definition: Cipher.h:159
Aws::Utils::Crypto::SymmetricCipher::Reset
virtual void Reset()=0
Aws::Utils::Crypto::SymmetricCipherFactory::CreateImplementation
virtual std::shared_ptr< SymmetricCipher > CreateImplementation(const CryptoBuffer &key, const CryptoBuffer &iv, const CryptoBuffer &tag=CryptoBuffer(0), const CryptoBuffer &aad=CryptoBuffer(0)) const =0
Aws::Utils::Crypto::SymmetricCipher
Definition: Cipher.h:28
Aws::Utils::Crypto::SymmetricCipherFactory
Definition: Cipher.h:168
Aws::Utils::Crypto::SymmetricCipher::SymmetricCipher
SymmetricCipher(const SymmetricCipher &other)=delete
Aws::Utils::Crypto::SymmetricCipherFactory::CleanupStaticState
virtual void CleanupStaticState()
Definition: Cipher.h:202
Aws::Utils::Crypto::MIN_IV_LENGTH
static const size_t MIN_IV_LENGTH
Definition: Cipher.h:19
Aws::Utils::Crypto::SymmetricCipher::Fail
bool Fail() const
Definition: Cipher.h:139
Aws::Utils::Crypto::SymmetricCipher::FinalizeEncryption
virtual CryptoBuffer FinalizeEncryption()=0
Aws::Utils::Crypto::SymmetricCipher::operator=
SymmetricCipher & operator=(const SymmetricCipher &other)=delete
Aws::Utils::Crypto::SymmetricCipherFactory::~SymmetricCipherFactory
virtual ~SymmetricCipherFactory()
Definition: Cipher.h:170
Aws::Utils::Crypto::SymmetricCipher::GenerateKey
static CryptoBuffer GenerateKey(size_t keyLengthBytes=SYMMETRIC_KEY_LENGTH)
Aws::Utils::Crypto::SymmetricCipher::SymmetricCipher
SymmetricCipher(CryptoBuffer &&key, CryptoBuffer &&initializationVector, CryptoBuffer &&tag=CryptoBuffer(0))
Definition: Cipher.h:55
Core_EXPORTS.h
Aws::Utils::CryptoBuffer
Definition: Array.h:229
Aws::Utils::Crypto::SymmetricCipher::m_key
CryptoBuffer m_key
Definition: Cipher.h:158
Aws::Utils::Crypto::SymmetricCipher::SymmetricCipher
SymmetricCipher(const CryptoBuffer &key, size_t ivSize, bool ivGenerationInCtrMode=false)
Definition: Cipher.h:34
Aws
Definition: AccessManagementClient.h:15
Aws::Utils::Crypto::SymmetricCipher::DecryptBuffer
virtual CryptoBuffer DecryptBuffer(const CryptoBuffer &encryptedData)=0
Array.h
Aws::Utils::Crypto::SymmetricCipher::GenerateIV
static CryptoBuffer GenerateIV(size_t ivLengthBytes, bool ctrMode=false)
Aws::Utils::Crypto::SymmetricCipher::GetIV
const CryptoBuffer & GetIV() const
Definition: Cipher.h:131
Aws::Utils::Crypto::SymmetricCipher::operator=
SymmetricCipher & operator=(SymmetricCipher &&toMove)
Definition: Cipher.h:84
Aws::Utils::Crypto::SymmetricCipher::m_failure
bool m_failure
Definition: Cipher.h:161
Aws::Utils::Crypto::IncrementCTRCounter
AWS_CORE_API CryptoBuffer IncrementCTRCounter(const CryptoBuffer &counter, uint32_t numberOfBlocks)