Skip to content

Commit

Permalink
Enable use of nullptr for 0 length plaintext/ciphertext in crypto (#1…
Browse files Browse the repository at this point in the history
…0198)

* Enable use of nullptr for 0 length plaintext/ciphertext in crypto

* address review comments
  • Loading branch information
pan-apple authored and pull[bot] committed Nov 4, 2021
1 parent 60ff8e0 commit 2f7868b
Show file tree
Hide file tree
Showing 5 changed files with 332 additions and 77 deletions.
8 changes: 6 additions & 2 deletions src/crypto/CHIPCryptoPAL.h
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,9 @@ CHIP_ERROR ConvertIntegerRawToDerWithoutTag(const ByteSpan & raw_integer, Mutabl
* @brief A function that implements AES-CCM encryption
*
* This implements the CHIP_Crypto_AEAD_GenerateEncrypt() cryptographic primitive
* from the specification.
* from the specification. For an empty plaintext, the user of the API can provide
* an empty string, or a nullptr, and provide plaintext_length as 0. The output buffer,
* ciphertext can also be an empty string, or a nullptr for this case.
*
* @param plaintext Plaintext to encrypt
* @param plaintext_length Length of plain_text
Expand All @@ -500,7 +502,9 @@ CHIP_ERROR AES_CCM_encrypt(const uint8_t * plaintext, size_t plaintext_length, c
* @brief A function that implements AES-CCM decryption
*
* This implements the CHIP_Crypto_AEAD_DecryptVerify() cryptographic primitive
* from the specification.
* from the specification. For an empty ciphertext, the user of the API can provide
* an empty string, or a nullptr, and provide ciphertext_length as 0. The output buffer,
* plaintext can also be an empty string, or a nullptr for this case.
*
* @param ciphertext Ciphertext to decrypt
* @param ciphertext_length Length of ciphertext
Expand Down
22 changes: 22 additions & 0 deletions src/crypto/CHIPCryptoPALOpenSSL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ namespace Crypto {

typedef struct stack_st_X509 X509_LIST;

constexpr char kEmptyCryptoText[] = "";

enum class DigestType
{
SHA256
Expand Down Expand Up @@ -132,6 +134,16 @@ CHIP_ERROR AES_CCM_encrypt(const uint8_t * plaintext, size_t plaintext_length, c
int result = 1;
const EVP_CIPHER * type = nullptr;

if (plaintext_length != 0)
{
VerifyOrExit(plaintext != nullptr, error = CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrExit(ciphertext != nullptr, error = CHIP_ERROR_INVALID_ARGUMENT);
}
else if (plaintext == nullptr)
{
plaintext = Uint8::from_const_char(kEmptyCryptoText);
}

VerifyOrExit(key != nullptr, error = CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrExit(_isValidKeyLength(key_length), error = CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrExit(iv != nullptr, error = CHIP_ERROR_INVALID_ARGUMENT);
Expand Down Expand Up @@ -214,6 +226,16 @@ CHIP_ERROR AES_CCM_decrypt(const uint8_t * ciphertext, size_t ciphertext_length,
int result = 1;
const EVP_CIPHER * type = nullptr;

if (ciphertext_length != 0)
{
VerifyOrExit(ciphertext != nullptr, error = CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrExit(plaintext != nullptr, error = CHIP_ERROR_INVALID_ARGUMENT);
}
else if (ciphertext == nullptr)
{
ciphertext = Uint8::from_const_char(kEmptyCryptoText);
}

VerifyOrExit(tag != nullptr, error = CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrExit(_isValidTagLength(tag_length), error = CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrExit(key != nullptr, error = CHIP_ERROR_INVALID_ARGUMENT);
Expand Down
4 changes: 4 additions & 0 deletions src/crypto/CHIPCryptoPALmbedTLS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ CHIP_ERROR AES_CCM_encrypt(const uint8_t * plaintext, size_t plaintext_length, c
mbedtls_ccm_context context;
mbedtls_ccm_init(&context);

VerifyOrExit(plaintext != nullptr || plaintext_length == 0, error = CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrExit(ciphertext != nullptr || plaintext_length == 0, error = CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrExit(key != nullptr, error = CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrExit(_isValidKeyLength(key_length), error = CHIP_ERROR_UNSUPPORTED_ENCRYPTION_TYPE);
VerifyOrExit(iv != nullptr, error = CHIP_ERROR_INVALID_ARGUMENT);
Expand Down Expand Up @@ -151,6 +153,8 @@ CHIP_ERROR AES_CCM_decrypt(const uint8_t * ciphertext, size_t ciphertext_len, co
mbedtls_ccm_context context;
mbedtls_ccm_init(&context);

VerifyOrExit(plaintext != nullptr || ciphertext_len == 0, error = CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrExit(ciphertext != nullptr || ciphertext_len == 0, error = CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrExit(tag != nullptr, error = CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrExit(_isValidTagLength(tag_length), error = CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrExit(key != nullptr, error = CHIP_ERROR_INVALID_ARGUMENT);
Expand Down
Loading

0 comments on commit 2f7868b

Please sign in to comment.