Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Oct 4, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
24 changes: 24 additions & 0 deletions src/crypto/CHIPCryptoPALOpenSSL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,18 @@ CHIP_ERROR AES_CCM_encrypt(const uint8_t * plaintext, size_t plaintext_length, c
int result = 1;
const EVP_CIPHER * type = nullptr;

const uint8_t * empty_plaintext = Uint8::from_const_char("");
pan-apple marked this conversation as resolved.
Show resolved Hide resolved

if (plaintext_length != 0)
{
VerifyOrExit(plaintext != nullptr, error = CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrExit(ciphertext != nullptr, error = CHIP_ERROR_INVALID_ARGUMENT);
pan-apple marked this conversation as resolved.
Show resolved Hide resolved
}
else if (plaintext == nullptr)
{
plaintext = empty_plaintext;
}

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,18 @@ CHIP_ERROR AES_CCM_decrypt(const uint8_t * ciphertext, size_t ciphertext_length,
int result = 1;
const EVP_CIPHER * type = nullptr;

const uint8_t * empty_ciphertext = Uint8::from_const_char("");

if (ciphertext_length != 0)
{
VerifyOrExit(ciphertext != nullptr, error = CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrExit(plaintext != nullptr, error = CHIP_ERROR_INVALID_ARGUMENT);
pan-apple marked this conversation as resolved.
Show resolved Hide resolved
}
else if (ciphertext == nullptr)
{
ciphertext = empty_ciphertext;
}

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
12 changes: 12 additions & 0 deletions src/crypto/CHIPCryptoPALmbedTLS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ CHIP_ERROR AES_CCM_encrypt(const uint8_t * plaintext, size_t plaintext_length, c
mbedtls_ccm_context context;
mbedtls_ccm_init(&context);

if (plaintext_length != 0)
{
VerifyOrExit(plaintext != nullptr, error = CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrExit(ciphertext != nullptr, error = CHIP_ERROR_INVALID_ARGUMENT);
}
pan-apple marked this conversation as resolved.
Show resolved Hide resolved

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 +157,12 @@ CHIP_ERROR AES_CCM_decrypt(const uint8_t * ciphertext, size_t ciphertext_len, co
mbedtls_ccm_context context;
mbedtls_ccm_init(&context);

if (ciphertext_len != 0)
{
VerifyOrExit(ciphertext != nullptr, error = CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrExit(plaintext != nullptr, 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