Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 11 additions & 0 deletions src/psa_aead.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <wolfssl/wolfcrypt/aes.h>
#include <wolfssl/wolfcrypt/mem_track.h>
#include "psa_aead_internal.h"
#include "psa_size.h"

static wolfpsa_aead_ctx_t* wolfpsa_aead_get_ctx(psa_aead_operation_t *operation)
{
Expand Down Expand Up @@ -475,6 +476,11 @@ static psa_status_t wolfpsa_aead_encrypt_final(wolfpsa_aead_ctx_t *ctx,
return PSA_ERROR_BUFFER_TOO_SMALL;
}

if ((wolfpsa_check_word32_length(ctx->input_length) != PSA_SUCCESS) ||
(wolfpsa_check_word32_length(ctx->aad_length) != PSA_SUCCESS)) {
return PSA_ERROR_INVALID_ARGUMENT;
}

if (PSA_ALG_AEAD_EQUAL(ctx->alg, PSA_ALG_GCM)) {
Aes aes;
ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
Expand Down Expand Up @@ -575,6 +581,11 @@ static psa_status_t wolfpsa_aead_decrypt_final(wolfpsa_aead_ctx_t *ctx,
return PSA_ERROR_INVALID_SIGNATURE;
}

if ((wolfpsa_check_word32_length(ctx->input_length) != PSA_SUCCESS) ||
(wolfpsa_check_word32_length(ctx->aad_length) != PSA_SUCCESS)) {
return PSA_ERROR_INVALID_ARGUMENT;
}

if (PSA_ALG_AEAD_EQUAL(ctx->alg, PSA_ALG_GCM)) {
Aes aes;
ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
Expand Down
26 changes: 14 additions & 12 deletions src/psa_asymmetric_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -452,23 +452,22 @@ psa_status_t psa_sign_message(psa_key_id_t key,
hash_alg = 0;
hash_length = input_length;
if (hash_length > sizeof(hash)) {
wolfpsa_forcezero_free_key_data(key_data, key_data_length);
return PSA_ERROR_INVALID_ARGUMENT;
status = PSA_ERROR_INVALID_ARGUMENT;
goto cleanup;
}
XMEMCPY(hash, input, hash_length);
}
else {
hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
if (hash_alg == 0) {
wolfpsa_forcezero_free_key_data(key_data, key_data_length);
return PSA_ERROR_NOT_SUPPORTED;
status = PSA_ERROR_NOT_SUPPORTED;
goto cleanup;
}

status = psa_hash_compute(hash_alg, input, input_length,
hash, sizeof(hash), &hash_length);
if (status != PSA_SUCCESS) {
wolfpsa_forcezero_free_key_data(key_data, key_data_length);
return status;
goto cleanup;
}
}

Expand Down Expand Up @@ -517,6 +516,8 @@ psa_status_t psa_sign_message(psa_key_id_t key,
status = PSA_ERROR_NOT_SUPPORTED;
}

cleanup:
wc_ForceZero(hash, sizeof(hash));
wolfpsa_forcezero_free_key_data(key_data, key_data_length);
return status;
}
Expand Down Expand Up @@ -550,23 +551,22 @@ psa_status_t psa_verify_message(psa_key_id_t key,
hash_alg = 0;
hash_length = input_length;
if (hash_length > sizeof(hash)) {
wolfpsa_forcezero_free_key_data(key_data, key_data_length);
return PSA_ERROR_INVALID_ARGUMENT;
status = PSA_ERROR_INVALID_ARGUMENT;
goto cleanup;
}
XMEMCPY(hash, input, hash_length);
}
else {
hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
if (hash_alg == 0) {
wolfpsa_forcezero_free_key_data(key_data, key_data_length);
return PSA_ERROR_NOT_SUPPORTED;
status = PSA_ERROR_NOT_SUPPORTED;
goto cleanup;
}

status = psa_hash_compute(hash_alg, input, input_length,
hash, sizeof(hash), &hash_length);
if (status != PSA_SUCCESS) {
wolfpsa_forcezero_free_key_data(key_data, key_data_length);
return status;
goto cleanup;
}
}

Expand Down Expand Up @@ -612,6 +612,8 @@ psa_status_t psa_verify_message(psa_key_id_t key,
status = PSA_ERROR_NOT_SUPPORTED;
}

cleanup:
wc_ForceZero(hash, sizeof(hash));
wolfpsa_forcezero_free_key_data(key_data, key_data_length);
return status;
}
Expand Down
30 changes: 26 additions & 4 deletions src/psa_chacha20_poly1305.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#if defined(WOLFSSL_PSA_ENGINE) && defined(HAVE_CHACHA) && defined(HAVE_POLY1305)

#include <psa/crypto.h>
#include "psa_size.h"
#include <wolfpsa/psa_engine.h>
#include <wolfpsa/psa_chacha20_poly1305.h>
#include <wolfssl/wolfcrypt/error-crypt.h>
Expand Down Expand Up @@ -82,6 +83,7 @@ psa_status_t psa_chacha20_poly1305_encrypt(
uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length)
{
int ret;
size_t required_size;

/* Check parameters */
if (key == NULL || key_length != CHACHA20_POLY1305_AEAD_KEYSIZE) {
Expand All @@ -105,9 +107,18 @@ psa_status_t psa_chacha20_poly1305_encrypt(
if (plaintext == NULL && plaintext_length > 0) {
return PSA_ERROR_INVALID_ARGUMENT;
}
if ((wolfpsa_check_word32_length(additional_data_length) != PSA_SUCCESS) ||
(wolfpsa_check_word32_length(plaintext_length) != PSA_SUCCESS)) {
return PSA_ERROR_INVALID_ARGUMENT;
}

if (plaintext_length > SIZE_MAX - CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE) {
return PSA_ERROR_BUFFER_TOO_SMALL;
}
required_size = plaintext_length + CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE;

/* Check output buffer size */
if (ciphertext_size < plaintext_length + CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE) {
if (ciphertext_size < required_size) {
return PSA_ERROR_BUFFER_TOO_SMALL;
}

Expand All @@ -126,7 +137,7 @@ psa_status_t psa_chacha20_poly1305_encrypt(
}

/* Set output length */
*ciphertext_length = plaintext_length + CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE;
*ciphertext_length = required_size;

return PSA_SUCCESS;
}
Expand Down Expand Up @@ -164,6 +175,11 @@ psa_status_t psa_chacha20_poly1305_decrypt(
if (ciphertext_length < CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE) {
return PSA_ERROR_INVALID_ARGUMENT;
}
if ((wolfpsa_check_word32_length(additional_data_length) != PSA_SUCCESS) ||
(wolfpsa_check_word32_length(ciphertext_length -
CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE) != PSA_SUCCESS)) {
return PSA_ERROR_INVALID_ARGUMENT;
}

/* Check output buffer size */
if (plaintext_size < ciphertext_length - CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE) {
Expand Down Expand Up @@ -201,6 +217,7 @@ psa_status_t psa_xchacha20_poly1305_encrypt(
uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length)
{
int ret;
size_t required_size;

/* Check parameters */
if (key == NULL || key_length != CHACHA20_POLY1305_AEAD_KEYSIZE) {
Expand All @@ -225,8 +242,13 @@ psa_status_t psa_xchacha20_poly1305_encrypt(
return PSA_ERROR_INVALID_ARGUMENT;
}

if (plaintext_length > SIZE_MAX - CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE) {
return PSA_ERROR_BUFFER_TOO_SMALL;
}
required_size = plaintext_length + CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE;

/* Check output buffer size */
if (ciphertext_size < plaintext_length + CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE) {
if (ciphertext_size < required_size) {
return PSA_ERROR_BUFFER_TOO_SMALL;
}

Expand All @@ -244,7 +266,7 @@ psa_status_t psa_xchacha20_poly1305_encrypt(
}

/* Set output length */
*ciphertext_length = plaintext_length + CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE;
*ciphertext_length = required_size;

return PSA_SUCCESS;
}
Expand Down
14 changes: 14 additions & 0 deletions src/psa_cipher.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#include <psa/crypto.h>
#include "psa_trace.h"
#include "psa_size.h"
#include <wolfpsa/psa_engine.h>
#include <wolfpsa/psa_key_storage.h>
#include <wolfssl/wolfcrypt/aes.h>
Expand Down Expand Up @@ -296,6 +297,11 @@ psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
ctx->block_size = wolfpsa_cipher_block_size(attributes.type);
ctx->partial_len = 0;
XMEMCPY(ctx->iv, zero_iv, sizeof(ctx->iv));
if (wolfpsa_check_word32_length(key_data_length) != PSA_SUCCESS) {
wolfpsa_forcezero_free_key_data(key_data, key_data_length);
XFREE(ctx, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return PSA_ERROR_INVALID_ARGUMENT;
}

if (ctx->is_chacha) {
ret = wc_Chacha_SetKey(&ctx->chacha, key_data,
Expand Down Expand Up @@ -432,6 +438,11 @@ psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
ctx->block_size = wolfpsa_cipher_block_size(attributes.type);
ctx->partial_len = 0;
XMEMCPY(ctx->iv, zero_iv, sizeof(ctx->iv));
if (wolfpsa_check_word32_length(key_data_length) != PSA_SUCCESS) {
wolfpsa_forcezero_free_key_data(key_data, key_data_length);
XFREE(ctx, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return PSA_ERROR_INVALID_ARGUMENT;
}

if (ctx->is_chacha) {
ret = wc_Chacha_SetKey(&ctx->chacha, key_data,
Expand Down Expand Up @@ -658,6 +669,9 @@ psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
if (output == NULL && output_size > 0) {
return PSA_ERROR_INVALID_ARGUMENT;
}
if (wolfpsa_check_word32_length(input_length) != PSA_SUCCESS) {
return PSA_ERROR_INVALID_ARGUMENT;
}

if (input_length == 0) {
return PSA_SUCCESS;
Expand Down
22 changes: 22 additions & 0 deletions src/psa_ecc.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#if defined(WOLFSSL_PSA_ENGINE) && defined(HAVE_ECC)

#include <psa/crypto.h>
#include "psa_size.h"
#include <wolfpsa/psa_engine.h>
#include <wolfssl/wolfcrypt/error-crypt.h>
#include <wolfssl/wolfcrypt/types.h>
Expand Down Expand Up @@ -83,6 +84,10 @@ psa_status_t psa_asymmetric_sign_ecc(psa_key_type_t key_type,
if (curve_id == ECC_CURVE_INVALID) {
return PSA_ERROR_NOT_SUPPORTED;
}
if ((wolfpsa_check_word32_length(key_buffer_size) != PSA_SUCCESS) ||
(wolfpsa_check_word32_length(hash_length) != PSA_SUCCESS)) {
return PSA_ERROR_INVALID_ARGUMENT;
}

/* Initialize ECC key */
ret = wc_ecc_init(&ecc);
Expand Down Expand Up @@ -146,25 +151,30 @@ psa_status_t psa_asymmetric_sign_ecc(psa_key_type_t key_type,
wc_ecc_free(&ecc);

if (ret != 0) {
wc_ForceZero(der_sig, sig_len);
XFREE(der_sig, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return wc_error_to_psa_status(ret);
}

rs = (byte*)XMALLOC(raw_sig_len, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (rs == NULL) {
wc_ForceZero(der_sig, sig_len);
XFREE(der_sig, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return PSA_ERROR_INSUFFICIENT_MEMORY;
}

r_len = (word32)key_bytes;
s_len = (word32)key_bytes;
ret = wc_ecc_sig_to_rs(der_sig, der_len, rs, &r_len, rs + key_bytes, &s_len);
wc_ForceZero(der_sig, sig_len);
XFREE(der_sig, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (ret != 0) {
wc_ForceZero(rs, raw_sig_len);
XFREE(rs, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return wc_error_to_psa_status(ret);
}
if (r_len > key_bytes || s_len > key_bytes) {
wc_ForceZero(rs, raw_sig_len);
XFREE(rs, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return PSA_ERROR_INVALID_SIGNATURE;
}
Expand All @@ -173,6 +183,7 @@ psa_status_t psa_asymmetric_sign_ecc(psa_key_type_t key_type,
XMEMCPY(signature + (key_bytes - r_len), rs, r_len);
XMEMCPY(signature + key_bytes + (key_bytes - s_len),
rs + key_bytes, s_len);
wc_ForceZero(rs, raw_sig_len);
XFREE(rs, NULL, DYNAMIC_TYPE_TMP_BUFFER);

*signature_length = raw_sig_len;
Expand Down Expand Up @@ -214,6 +225,10 @@ psa_status_t psa_asymmetric_verify_ecc(psa_key_type_t key_type,
if (curve_id == ECC_CURVE_INVALID) {
return PSA_ERROR_NOT_SUPPORTED;
}
if ((wolfpsa_check_word32_length(key_buffer_size) != PSA_SUCCESS) ||
(wolfpsa_check_word32_length(hash_length) != PSA_SUCCESS)) {
return PSA_ERROR_INVALID_ARGUMENT;
}

/* Initialize ECC key */
ret = wc_ecc_init(&ecc);
Expand Down Expand Up @@ -304,6 +319,9 @@ psa_status_t psa_asymmetric_generate_key_ecc(psa_key_type_t key_type,
if (curve_id == ECC_CURVE_INVALID) {
return PSA_ERROR_NOT_SUPPORTED;
}
if (wolfpsa_check_word32_length(public_key_size) != PSA_SUCCESS) {
return PSA_ERROR_INVALID_ARGUMENT;
}

/* Initialize ECC key */
ret = wc_ecc_init(&ecc);
Expand Down Expand Up @@ -388,6 +406,10 @@ psa_status_t psa_asymmetric_export_public_key_ecc(psa_key_type_t key_type,
if (curve_id == ECC_CURVE_INVALID) {
return PSA_ERROR_NOT_SUPPORTED;
}
if ((wolfpsa_check_word32_length(key_buffer_size) != PSA_SUCCESS) ||
(wolfpsa_check_word32_length(output_size) != PSA_SUCCESS)) {
return PSA_ERROR_INVALID_ARGUMENT;
}

/* Initialize ECC key */
ret = wc_ecc_init(&ecc);
Expand Down
29 changes: 22 additions & 7 deletions src/psa_hash_engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#include <psa/crypto.h>
#include "psa_trace.h"
#include "psa_size.h"
#include <wolfpsa/psa_engine.h>
#include <wolfssl/wolfcrypt/error-crypt.h>
#include <wolfssl/wolfcrypt/types.h>
Expand Down Expand Up @@ -433,6 +434,9 @@ psa_status_t psa_hash_update(psa_hash_operation_t *operation,
if (ctx->finalized) {
return PSA_ERROR_BAD_STATE;
}
if (wolfpsa_check_word32_length(input_length) != PSA_SUCCESS) {
return PSA_ERROR_INVALID_ARGUMENT;
}

/* Update the hash context based on algorithm */
switch (ctx->alg) {
Expand Down Expand Up @@ -640,18 +644,24 @@ psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
status = psa_hash_finish(operation, computed_hash, sizeof(computed_hash),
&computed_hash_length);
if (status != PSA_SUCCESS) {
return status;
goto cleanup;
}

if (hash_length != computed_hash_length) {
return PSA_ERROR_INVALID_SIGNATURE;
status = PSA_ERROR_INVALID_SIGNATURE;
goto cleanup;
}

if (ConstantCompare(computed_hash, hash, (int)computed_hash_length) != 0) {
return PSA_ERROR_INVALID_SIGNATURE;
status = PSA_ERROR_INVALID_SIGNATURE;
goto cleanup;
}

return PSA_SUCCESS;
status = PSA_SUCCESS;

cleanup:
wc_ForceZero(computed_hash, sizeof(computed_hash));
return status;
}

/* Abort a hash operation */
Expand Down Expand Up @@ -866,15 +876,20 @@ psa_status_t psa_hash_compare(psa_algorithm_t alg,
status = psa_hash_compute(alg, input, input_length, computed_hash,
sizeof(computed_hash), &computed_hash_length);
if (status != PSA_SUCCESS) {
return status;
goto cleanup;
}

/* Compare the computed hash with the reference hash */
if (ConstantCompare(computed_hash, hash, (int)computed_hash_length) != 0) {
return PSA_ERROR_INVALID_SIGNATURE;
status = PSA_ERROR_INVALID_SIGNATURE;
goto cleanup;
}

return PSA_SUCCESS;
status = PSA_SUCCESS;

cleanup:
wc_ForceZero(computed_hash, sizeof(computed_hash));
return status;
}

#endif /* WOLFSSL_PSA_ENGINE */
Loading
Loading