AWS SDK for C++
AWS SDK for C++
Loading...
Searching...
No Matches
CryptoImpl.h
Go to the documentation of this file.
1
6#pragma once
7
14#include <mutex>
15
16#if (defined(WIN32) || defined(_WIN32) || defined(__WIN32__))
17
18#define WIN32_NO_STATUS
19#include <windows.h>
20#include <bcrypt.h>
21#include <winternl.h>
22#include <winerror.h>
23#undef WIN32_NO_STATUS
24#endif // AWS_SDK_PLATFORM_WINDOWS
25
26namespace Aws
27{
28 namespace Utils
29 {
30 namespace Crypto
31 {
32 static const char* SecureRandom_BCrypt_Tag = "SecureRandom_BCrypt";
33
35 {
36 public:
39 void GetBytes(unsigned char* buffer, size_t bufferSize) override;
40 private:
41 BCRYPT_ALG_HANDLE m_algHandle;
42 };
43
44 class BCryptHashContext;
45
50 {
51 public:
56 BCryptHashImpl(LPCWSTR algorithmName, bool isHMAC);
58
66 HashResult Calculate(const ByteBuffer& toHash, const ByteBuffer& secret);
71
72 void Update(unsigned char* buffer, size_t bufferSize);
73
75
76 private:
77
78 bool IsValid() const;
79
80 HashResult HashData(const BCryptHashContext& context, PBYTE data, ULONG dataLength);
81 bool HashStream(Aws::IStream& stream);
82
83 void* m_algorithmHandle;
84 void* m_hashHandle;
85
86 DWORD m_hashBufferLength;
87 PBYTE m_hashBuffer;
88
89 DWORD m_hashObjectLength;
90 PBYTE m_hashObject;
91
92 //I'm 99% sure the algorithm handle for windows is not thread safe, but I can't
93 //prove or disprove that theory. Therefore, we have to lock to be safe.
94 std::mutex m_algorithmMutex;
95 };
96
100 class MD5BcryptImpl : public Hash
101 {
102 public:
107 virtual ~MD5BcryptImpl() {}
108
112 virtual HashResult Calculate(const Aws::String& str) override;
116 virtual HashResult Calculate(Aws::IStream& stream) override;
117
118 virtual void Update(unsigned char* buffer, size_t bufferSize) override;
119
120 virtual HashResult GetHash() override;
121
122 private:
123 BCryptHashImpl m_impl;
124 };
125
126 class Sha1BcryptImpl : public Hash
127 {
128 public:
129
131 virtual ~Sha1BcryptImpl() {}
132
133 virtual HashResult Calculate(const Aws::String& str) override;
134
135 virtual HashResult Calculate(Aws::IStream& stream) override;
136
137 virtual void Update(unsigned char* buffer, size_t bufferSize) override;
138
139 virtual HashResult GetHash() override;
140
141 private:
142 BCryptHashImpl m_impl;
143 };
144
148 class Sha256BcryptImpl : public Hash
149 {
150 public:
155 virtual ~Sha256BcryptImpl() {}
156
160 virtual HashResult Calculate(const Aws::String& str) override;
164 virtual HashResult Calculate(Aws::IStream& stream) override;
165
166 virtual void Update(unsigned char* buffer, size_t bufferSize) override;
167
168 virtual HashResult GetHash() override;
169
170 private:
171 BCryptHashImpl m_impl;
172 };
173
178 {
179 public:
180
186
190 virtual HashResult Calculate(const ByteBuffer& toSign, const ByteBuffer& secret) override;
191
192 private:
193 BCryptHashImpl m_impl;
194 };
195
200 {
201 public:
202 BCryptSymmetricCipher(const CryptoBuffer& key, size_t ivSize, bool ctrMode = false);
203
207 BCryptSymmetricCipher(const CryptoBuffer& key, const CryptoBuffer& initializationVector, const CryptoBuffer& tag = CryptoBuffer(0));
208
212 BCryptSymmetricCipher(CryptoBuffer&& key, CryptoBuffer&& initializationVector, CryptoBuffer&& tag = std::move(CryptoBuffer(0)));
213
216
223
230
232
237 CryptoBuffer EncryptBuffer(const CryptoBuffer& unEncryptedData) override;
247 CryptoBuffer DecryptBuffer(const CryptoBuffer& encryptedData) override;
253
254 void Reset() override;
255
256 protected:
257 void InitKey();
258 virtual size_t GetBlockSizeBytes() const = 0;
259 virtual size_t GetKeyLengthBits() const = 0;
260 bool CheckKeyAndIVLength(size_t expectedKeyLength, size_t expectedIVLength);
261
262 BCRYPT_ALG_HANDLE m_algHandle;
263 BCRYPT_KEY_HANDLE m_keyHandle;
264 DWORD m_flags;
266 PBCRYPT_AUTHENTICATED_CIPHER_MODE_INFO m_authInfoPtr;
267
268 static BCRYPT_KEY_HANDLE ImportKeyBlob(BCRYPT_ALG_HANDLE handle, CryptoBuffer& key);
273 private:
274 void Init();
275 void Cleanup();
276 };
277
282 {
283 public:
288
292 AES_CBC_Cipher_BCrypt(CryptoBuffer&& key, CryptoBuffer&& initializationVector);
293
297 AES_CBC_Cipher_BCrypt(const CryptoBuffer& key, const CryptoBuffer& initializationVector);
298
300
302
303 AES_CBC_Cipher_BCrypt(AES_CBC_Cipher_BCrypt&& toMove) : BCryptSymmetricCipher(std::move(toMove)), m_blockOverflow(std::move(toMove.m_blockOverflow)) {}
304
305 CryptoBuffer EncryptBuffer(const CryptoBuffer& unEncryptedData) override;
307 CryptoBuffer DecryptBuffer(const CryptoBuffer& encryptedData) override;
309
310 void Reset() override;
311
312 protected:
313 size_t GetBlockSizeBytes() const override;
314 size_t GetKeyLengthBits() const override;
315
316 private:
317 void InitCipher();
318 CryptoBuffer FillInOverflow(const CryptoBuffer& buffer);
319
320 CryptoBuffer m_blockOverflow;
321
322 static size_t BlockSizeBytes;
323 static size_t KeyLengthBits;
324 };
325
330 {
331 public:
337
341 AES_CTR_Cipher_BCrypt(CryptoBuffer&& key, CryptoBuffer&& initializationVector);
342
346 AES_CTR_Cipher_BCrypt(const CryptoBuffer& key, const CryptoBuffer& initializationVector);
347
349
351
352 AES_CTR_Cipher_BCrypt(AES_CTR_Cipher_BCrypt&& toMove) : BCryptSymmetricCipher(std::move(toMove)), m_blockOverflow(std::move(toMove.m_blockOverflow)) {}
353
354 CryptoBuffer EncryptBuffer(const CryptoBuffer& unEncryptedData) override;
356 CryptoBuffer DecryptBuffer(const CryptoBuffer& encryptedData) override;
358
359 void Reset() override;
360
361 protected:
362 size_t GetBlockSizeBytes() const override;
363 size_t GetKeyLengthBits() const override;
364
365 private:
366 void InitCipher();
367
368 static void InitBuffersToNull(Aws::Vector<ByteBuffer*>& initBuffers);
369 static void CleanupBuffers(Aws::Vector<ByteBuffer*>& cleanupBuffers);
370
371 CryptoBuffer EncryptWithCtr(const CryptoBuffer& buffer);
372
373 static size_t BlockSizeBytes;
374 static size_t KeyLengthBits;
375
376 CryptoBuffer m_blockOverflow;
377 };
378
383 {
384 public:
389
394
399 AES_GCM_Cipher_BCrypt(CryptoBuffer&& key, CryptoBuffer&& initializationVector,
400 CryptoBuffer&& tag = CryptoBuffer(0), CryptoBuffer&& aad = CryptoBuffer(0));
401
406 AES_GCM_Cipher_BCrypt(const CryptoBuffer& key, const CryptoBuffer& initializationVector,
407 const CryptoBuffer& tag = CryptoBuffer(0), const CryptoBuffer& aad = CryptoBuffer(0));
408
410
412
414 BCryptSymmetricCipher(std::move(toMove)), m_macBuffer(std::move(toMove.m_macBuffer)), m_finalBuffer(std::move(toMove.m_finalBuffer)),
415 m_authInfo(std::move(toMove.m_authInfo)) {}
416
421
422 void Reset() override;
423
424 protected:
425 size_t GetBlockSizeBytes() const override;
426 size_t GetKeyLengthBits() const override;
427 size_t GetTagLengthBytes() const;
428
429 private:
430 void InitCipher();
431
432 static size_t BlockSizeBytes;
433 static size_t IVLengthBytes;
434 static size_t KeyLengthBits;
435 static size_t TagLengthBytes;
436
437 CryptoBuffer m_macBuffer;
438 CryptoBuffer m_finalBuffer;
439 CryptoBuffer m_aad;
440 BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO m_authInfo;
441 };
442
448 {
449 public:
455
457
459
461
462 CryptoBuffer EncryptBuffer(const CryptoBuffer& unEncryptedData) override;
464 CryptoBuffer DecryptBuffer(const CryptoBuffer& encryptedData) override;
466
467 void Reset() override;
468
469 protected:
470 size_t GetBlockSizeBytes() const override;
471 size_t GetKeyLengthBits() const override;
472
473 private:
474 void InitCipher();
475
476 static size_t BlockSizeBytes;
477 static size_t KeyLengthBits;
478
479 CryptoBuffer m_operatingKeyBuffer;
480 };
481 } // namespace Crypto
482 } // namespace Utils
483} // namespace Aws
char * buffer
Definition: cJSON.h:174
CryptoBuffer EncryptBuffer(const CryptoBuffer &unEncryptedData) override
AES_CBC_Cipher_BCrypt(const CryptoBuffer &key, const CryptoBuffer &initializationVector)
size_t GetBlockSizeBytes() const override
CryptoBuffer FinalizeEncryption() override
CryptoBuffer DecryptBuffer(const CryptoBuffer &encryptedData) override
AES_CBC_Cipher_BCrypt(AES_CBC_Cipher_BCrypt &&toMove)
Definition: CryptoImpl.h:303
AES_CBC_Cipher_BCrypt & operator=(const AES_CBC_Cipher_BCrypt &)=delete
AES_CBC_Cipher_BCrypt(const AES_CBC_Cipher_BCrypt &)=delete
AES_CBC_Cipher_BCrypt(CryptoBuffer &&key, CryptoBuffer &&initializationVector)
CryptoBuffer FinalizeDecryption() override
size_t GetKeyLengthBits() const override
AES_CBC_Cipher_BCrypt(const CryptoBuffer &key)
CryptoBuffer EncryptBuffer(const CryptoBuffer &unEncryptedData) override
AES_CTR_Cipher_BCrypt(CryptoBuffer &&key, CryptoBuffer &&initializationVector)
AES_CTR_Cipher_BCrypt & operator=(const AES_CTR_Cipher_BCrypt &)=delete
AES_CTR_Cipher_BCrypt(const AES_CTR_Cipher_BCrypt &)=delete
CryptoBuffer DecryptBuffer(const CryptoBuffer &encryptedData) override
size_t GetBlockSizeBytes() const override
AES_CTR_Cipher_BCrypt(AES_CTR_Cipher_BCrypt &&toMove)
Definition: CryptoImpl.h:352
CryptoBuffer FinalizeDecryption() override
AES_CTR_Cipher_BCrypt(const CryptoBuffer &key)
size_t GetKeyLengthBits() const override
CryptoBuffer FinalizeEncryption() override
AES_CTR_Cipher_BCrypt(const CryptoBuffer &key, const CryptoBuffer &initializationVector)
AES_GCM_Cipher_BCrypt(const CryptoBuffer &key, const CryptoBuffer *aad)
size_t GetKeyLengthBits() const override
CryptoBuffer FinalizeDecryption() override
CryptoBuffer FinalizeEncryption() override
AES_GCM_Cipher_BCrypt & operator=(const AES_GCM_Cipher_BCrypt &)=delete
AES_GCM_Cipher_BCrypt(const CryptoBuffer &key, const CryptoBuffer &initializationVector, const CryptoBuffer &tag=CryptoBuffer(0), const CryptoBuffer &aad=CryptoBuffer(0))
AES_GCM_Cipher_BCrypt(AES_GCM_Cipher_BCrypt &&toMove)
Definition: CryptoImpl.h:413
AES_GCM_Cipher_BCrypt(const CryptoBuffer &key)
AES_GCM_Cipher_BCrypt(CryptoBuffer &&key, CryptoBuffer &&initializationVector, CryptoBuffer &&tag=CryptoBuffer(0), CryptoBuffer &&aad=CryptoBuffer(0))
AES_GCM_Cipher_BCrypt(const AES_GCM_Cipher_BCrypt &)=delete
CryptoBuffer EncryptBuffer(const CryptoBuffer &) override
CryptoBuffer DecryptBuffer(const CryptoBuffer &) override
size_t GetBlockSizeBytes() const override
AES_KeyWrap_Cipher_BCrypt(const AES_KeyWrap_Cipher_BCrypt &)=delete
AES_KeyWrap_Cipher_BCrypt & operator=(const AES_KeyWrap_Cipher_BCrypt &)=delete
AES_KeyWrap_Cipher_BCrypt(const CryptoBuffer &key)
CryptoBuffer DecryptBuffer(const CryptoBuffer &encryptedData) override
AES_KeyWrap_Cipher_BCrypt(AES_CTR_Cipher_BCrypt &&toMove)
Definition: CryptoImpl.h:460
CryptoBuffer EncryptBuffer(const CryptoBuffer &unEncryptedData) override
void Update(unsigned char *buffer, size_t bufferSize)
HashResult Calculate(const ByteBuffer &toHash, const ByteBuffer &secret)
BCryptHashImpl(LPCWSTR algorithmName, bool isHMAC)
HashResult Calculate(const Aws::String &str)
HashResult Calculate(Aws::IStream &stream)
BCryptSymmetricCipher(CryptoBuffer &&key, CryptoBuffer &&initializationVector, CryptoBuffer &&tag=std::move(CryptoBuffer(0)))
virtual size_t GetKeyLengthBits() const =0
virtual size_t GetBlockSizeBytes() const =0
BCryptSymmetricCipher & operator=(BCryptSymmetricCipher &&toMove)
CryptoBuffer FinalizeEncryption() override
CryptoBuffer FinalizeDecryption() override
BCryptSymmetricCipher(BCryptSymmetricCipher &&toMove)
BCryptSymmetricCipher(const CryptoBuffer &key, size_t ivSize, bool ctrMode=false)
CryptoBuffer DecryptBuffer(const CryptoBuffer &encryptedData) override
BCryptSymmetricCipher(const BCryptSymmetricCipher &)=delete
CryptoBuffer EncryptBuffer(const CryptoBuffer &unEncryptedData) override
BCryptSymmetricCipher & operator=(const BCryptSymmetricCipher &)=delete
BCryptSymmetricCipher(const CryptoBuffer &key, const CryptoBuffer &initializationVector, const CryptoBuffer &tag=CryptoBuffer(0))
PBCRYPT_AUTHENTICATED_CIPHER_MODE_INFO m_authInfoPtr
Definition: CryptoImpl.h:266
static BCRYPT_KEY_HANDLE ImportKeyBlob(BCRYPT_ALG_HANDLE handle, CryptoBuffer &key)
bool CheckKeyAndIVLength(size_t expectedKeyLength, size_t expectedIVLength)
virtual HashResult Calculate(Aws::IStream &stream) override
virtual HashResult GetHash() override
virtual HashResult Calculate(const Aws::String &str) override
virtual void Update(unsigned char *buffer, size_t bufferSize) override
void GetBytes(unsigned char *buffer, size_t bufferSize) override
virtual HashResult GetHash() override
virtual HashResult Calculate(const Aws::String &str) override
virtual HashResult Calculate(Aws::IStream &stream) override
virtual void Update(unsigned char *buffer, size_t bufferSize) override
virtual HashResult GetHash() override
virtual void Update(unsigned char *buffer, size_t bufferSize) override
virtual HashResult Calculate(Aws::IStream &stream) override
virtual HashResult Calculate(const Aws::String &str) override
virtual HashResult Calculate(const ByteBuffer &toSign, const ByteBuffer &secret) override
static const char * SecureRandom_BCrypt_Tag
Definition: CryptoImpl.h:32
std::basic_istream< char, std::char_traits< char > > IStream
Definition: AWSStreamFwd.h:20
std::basic_string< char, std::char_traits< char >, Aws::Allocator< char > > String
Definition: AWSString.h:97
std::vector< T, Aws::Allocator< T > > Vector
Definition: AWSVector.h:17