AWS SDK for C++

AWS SDK for C++ Version 1.11.440

Loading...
Searching...
No Matches
Cipher.h
1
6#pragma once
7
8#include <aws/core/Core_EXPORTS.h>
9#include <aws/core/utils/Array.h>
10#include <aws/core/utils/memory/stl/AWSStreamFwd.h>
11
12namespace 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
27 class AWS_CORE_API SymmetricCipher
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 const CryptoBuffer& GetIV() const { return m_initializationVector; }
132
137 const CryptoBuffer& GetTag() const { return m_tag; }
138
139 bool Fail() const { return m_failure; }
140 virtual 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
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}
virtual std::shared_ptr< SymmetricCipher > CreateImplementation(const CryptoBuffer &key) const =0
virtual std::shared_ptr< SymmetricCipher > CreateImplementation(const CryptoBuffer &key, const CryptoBuffer &iv, const CryptoBuffer &tag=CryptoBuffer(0), const CryptoBuffer &aad=CryptoBuffer(0)) const =0
virtual std::shared_ptr< SymmetricCipher > CreateImplementation(const CryptoBuffer &key, const CryptoBuffer *) const
Definition Cipher.h:179
virtual std::shared_ptr< SymmetricCipher > CreateImplementation(CryptoBuffer &&key, CryptoBuffer &&iv, CryptoBuffer &&tag=CryptoBuffer(0), CryptoBuffer &&aad=CryptoBuffer(0)) const =0
SymmetricCipher(const SymmetricCipher &other)=delete
virtual CryptoBuffer EncryptBuffer(const CryptoBuffer &unEncryptedData)=0
SymmetricCipher(const CryptoBuffer &key, const CryptoBuffer &initializationVector, const CryptoBuffer &tag=CryptoBuffer(0))
Definition Cipher.h:44
SymmetricCipher(CryptoBuffer &&key, CryptoBuffer &&initializationVector, CryptoBuffer &&tag=CryptoBuffer(0))
Definition Cipher.h:55
virtual bool Good() const
Definition Cipher.h:140
SymmetricCipher(const CryptoBuffer &key, size_t ivSize, bool ivGenerationInCtrMode=false)
Definition Cipher.h:34
virtual CryptoBuffer FinalizeEncryption()=0
virtual CryptoBuffer DecryptBuffer(const CryptoBuffer &encryptedData)=0
const CryptoBuffer & GetTag() const
Definition Cipher.h:137
virtual CryptoBuffer FinalizeDecryption()=0
static CryptoBuffer GenerateKey(size_t keyLengthBytes=SYMMETRIC_KEY_LENGTH)
SymmetricCipher & operator=(const SymmetricCipher &other)=delete
SymmetricCipher(SymmetricCipher &&toMove)
Definition Cipher.h:71
static CryptoBuffer GenerateIV(size_t ivLengthBytes, bool ctrMode=false)
SymmetricCipher & operator=(SymmetricCipher &&toMove)
Definition Cipher.h:84
const CryptoBuffer & GetIV() const
Definition Cipher.h:131
static const size_t SYMMETRIC_KEY_LENGTH
Definition Cipher.h:18
static const size_t MIN_IV_LENGTH
Definition Cipher.h:19
AWS_CORE_API CryptoBuffer IncrementCTRCounter(const CryptoBuffer &counter, uint32_t numberOfBlocks)