AWS IoT Device SDK C++ v2  1.34.0
AWS IoT Device SDK C++ v2
SymmetricCipher.h
Go to the documentation of this file.
1 #pragma once
2 
6 #include <aws/cal/symmetric_cipher.h>
7 #include <aws/crt/Exports.h>
8 #include <aws/crt/Types.h>
9 
10 struct aws_symmetric_cipher;
11 
12 namespace Aws
13 {
14  namespace Crt
15  {
16  namespace Crypto
17  {
18  static const size_t AES_256_CIPHER_BLOCK_SIZE = 16u;
19  static const size_t AES_256_KEY_SIZE_BYTES = 32u;
20 
22  {
23  Ready = AWS_SYMMETRIC_CIPHER_READY,
24  Finalized = AWS_SYMMETRIC_CIPHER_FINALIZED,
25  Error = AWS_SYMMETRIC_CIPHER_ERROR,
26  };
27 
29  {
30  public:
31  SymmetricCipher(const SymmetricCipher &) = delete;
33  SymmetricCipher(SymmetricCipher &&) noexcept = default;
34  SymmetricCipher &operator=(SymmetricCipher &&) noexcept = default;
35 
40  static SymmetricCipher CreateAES_256_CBC_Cipher(
41  const Optional<ByteCursor> &key = Optional<ByteCursor>(),
42  const Optional<ByteCursor> &iv = Optional<ByteCursor>(),
43  Allocator *allocator = ApiAllocator()) noexcept;
44 
49  static SymmetricCipher CreateAES_256_CTR_Cipher(
50  const Optional<ByteCursor> &key = Optional<ByteCursor>(),
51  const Optional<ByteCursor> &iv = Optional<ByteCursor>(),
52  Allocator *allocator = ApiAllocator()) noexcept;
53 
60  static SymmetricCipher CreateAES_256_GCM_Cipher(
61  const Optional<ByteCursor> &key = Optional<ByteCursor>(),
62  const Optional<ByteCursor> &iv = Optional<ByteCursor>(),
63  const Optional<ByteCursor> &aad = Optional<ByteCursor>(),
64  Allocator *allocator = ApiAllocator()) noexcept;
65 
70  static SymmetricCipher CreateAES_256_KeyWrap_Cipher(
71  const Optional<ByteCursor> &key = Optional<ByteCursor>(),
72  Allocator *allocator = ApiAllocator()) noexcept;
73 
77  operator bool() const noexcept;
78 
83  SymmetricCipherState GetState() const noexcept;
84 
88  inline int LastError() const noexcept { return m_lastError; }
89 
98  bool Encrypt(const ByteCursor &toEncrypt, ByteBuf &out) noexcept;
99 
108  bool FinalizeEncryption(ByteBuf &out) noexcept;
109 
116  bool Decrypt(const ByteCursor &toDecrypt, ByteBuf &out) noexcept;
117 
126  bool FinalizeDecryption(ByteBuf &out) noexcept;
127 
131  bool Reset() noexcept;
132 
137  ByteCursor GetKey() const noexcept;
138 
144  ByteCursor GetIV() const noexcept;
145 
151  ByteCursor GetTag() const noexcept;
152 
157  void SetTag(ByteCursor tag) const noexcept;
158 
159  private:
160  SymmetricCipher(aws_symmetric_cipher *cipher) noexcept;
161  ScopedResource<struct aws_symmetric_cipher> m_cipher;
162  int m_lastError;
163  };
164  } // namespace Crypto
165  } // namespace Crt
166 } // namespace Aws
Aws::Crt::ApiAllocator
AWS_CRT_CPP_API Allocator * ApiAllocator() noexcept
Definition: Allocator.cpp:24
Aws::Crt::ScopedResource
std::unique_ptr< T, std::function< void(T *)> > ScopedResource
Definition: Types.h:163
Aws::Crt::Crypto::AES_256_KEY_SIZE_BYTES
static const size_t AES_256_KEY_SIZE_BYTES
Definition: SymmetricCipher.h:19
Aws::Crt::Crypto::SymmetricCipherState
SymmetricCipherState
Definition: SymmetricCipher.h:22
Aws::Crt::Crypto::SymmetricCipher::operator=
SymmetricCipher & operator=(const SymmetricCipher &)=delete
Aws::Crt::Crypto::SymmetricCipher::SymmetricCipher
SymmetricCipher(const SymmetricCipher &)=delete
Aws::Crt::LastError
AWS_CRT_CPP_API int LastError() noexcept
Definition: Api.cpp:422
Aws::Crt::Crypto::SymmetricCipher::SymmetricCipher
SymmetricCipher(SymmetricCipher &&) noexcept=default
Aws::Crt::Optional
Definition: Optional.h:18
Types.h
Aws::Crt::Crypto::SymmetricCipher
Definition: SymmetricCipher.h:29
Aws::Crt::ByteBuf
aws_byte_buf ByteBuf
Definition: Types.h:30
AWS_CRT_CPP_API
#define AWS_CRT_CPP_API
Definition: Exports.h:37
Aws::Crt::Crypto::AES_256_CIPHER_BLOCK_SIZE
static const size_t AES_256_CIPHER_BLOCK_SIZE
Definition: SymmetricCipher.h:18
Aws
Definition: Allocator.h:11
Aws::Crt::ByteCursor
aws_byte_cursor ByteCursor
Definition: Types.h:31
Aws::Crt::Allocator
aws_allocator Allocator
Definition: Allocator.h:14
Exports.h
Aws::Crt::Crypto::SymmetricCipherState::Ready
@ Ready