Skip to content

Commit

Permalink
Create CRT API for accessing symmetric cipher state
Browse files Browse the repository at this point in the history
Create CRT API for creating dynamically sized byte buf
  • Loading branch information
sbiscigl committed May 17, 2024
1 parent cac7ca5 commit 6950b76
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
1 change: 1 addition & 0 deletions include/aws/crt/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ namespace Aws
AWS_CRT_CPP_API ByteBuf ByteBufFromEmptyArray(const uint8_t *array, size_t len) noexcept;
AWS_CRT_CPP_API ByteBuf ByteBufFromArray(const uint8_t *array, size_t capacity) noexcept;
AWS_CRT_CPP_API ByteBuf ByteBufNewCopy(Allocator *alloc, const uint8_t *array, size_t len);
AWS_CRT_CPP_API ByteBuf ByteBufInit(Allocator *alloc, size_t len);
AWS_CRT_CPP_API void ByteBufDelete(ByteBuf &);

AWS_CRT_CPP_API ByteCursor ByteCursorFromCString(const char *str) noexcept;
Expand Down
14 changes: 14 additions & 0 deletions include/aws/crt/crypto/SymmetricCipher.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include <aws/cal/symmetric_cipher.h>
#include <aws/crt/Exports.h>
#include <aws/crt/Types.h>

Expand All @@ -17,6 +18,13 @@ namespace Aws
static const size_t AES_256_CIPHER_BLOCK_SIZE = 16u;
static const size_t AES_256_KEY_SIZE_BYTES = 32u;

enum class SymmetricCipherState
{
READY,
FINALIZED,
ERROR,
};

class AWS_CRT_CPP_API SymmetricCipher final
{
public:
Expand Down Expand Up @@ -71,6 +79,12 @@ namespace Aws
*/
operator bool() const noexcept;

/**
* Returns current stat of the cipher instance. ready to be used, finalized, or in a error state.
* If the cipher is in a finalized or error state it may not be used anymore
**/
SymmetricCipherState GetState() const noexcept;

/**
* Returns the value of the last aws error encountered by operations on this instance.
*/
Expand Down
7 changes: 7 additions & 0 deletions source/Types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ namespace Aws
return retVal;
}

ByteBuf ByteBufInit(Allocator *alloc, size_t len)
{
ByteBuf buff;
aws_byte_buf_init(&buff, alloc, len);
return buff;
}

void ByteBufDelete(ByteBuf &buf) { aws_byte_buf_clean_up(&buf); }

ByteCursor ByteCursorFromCString(const char *str) noexcept { return aws_byte_cursor_from_c_str(str); }
Expand Down
19 changes: 19 additions & 0 deletions source/crypto/SymmetricCipher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <aws/crt/crypto/SymmetricCipher.h>

#include <aws/cal/symmetric_cipher.h>
#include <sys/stat.h>

namespace Aws
{
Expand All @@ -27,6 +28,24 @@ namespace Aws
return m_cipher != nullptr ? aws_symmetric_cipher_is_good(m_cipher.get()) : false;
}

SymmetricCipherState SymmetricCipher::GetState() const noexcept
{
if (m_cipher == nullptr)
{
return SymmetricCipherState::ERROR;
}
const auto state = aws_symmetric_cipher_get_state(m_cipher.get());
switch (state)
{
case AWS_SYMMETRIC_CIPHER_READY:
return SymmetricCipherState::READY;
case AWS_SYMMETRIC_CIPHER_FINALIZED:
return SymmetricCipherState::FINALIZED;
default:
return SymmetricCipherState::ERROR;
}
}

bool SymmetricCipher::Encrypt(const ByteCursor &toEncrypt, ByteBuf &out) noexcept
{
if (!*this)
Expand Down

0 comments on commit 6950b76

Please sign in to comment.