Skip to content

Commit

Permalink
sys/psa_crypto: wipe temporary private key copies from stack
Browse files Browse the repository at this point in the history
  • Loading branch information
mguetschow committed Sep 28, 2023
1 parent c1fd585 commit 3627baa
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
10 changes: 7 additions & 3 deletions pkg/driver_cryptocell_310/psa_cryptocell_310/ecc_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "psa_error.h"
#include "psa_cryptocell_310_ecc_common.h"
#include "cryptocell_310_util.h"
#include "string_utils.h"

#define ENABLE_DEBUG 0
#include "debug.h"
Expand Down Expand Up @@ -67,6 +68,7 @@ psa_status_t cryptocell_310_common_ecc_generate_key_pair(uint8_t *priv_key_buffe
return CRYS_to_psa_error(ret);
}

explicit_bzero(&priv_key, sizeof(priv_key));
return PSA_SUCCESS;
}

Expand All @@ -89,7 +91,7 @@ psa_status_t cryptocell_310_common_ecc_sign(const uint8_t *priv_key,
ret = CRYS_ECPKI_BuildPrivKey(pDomain, priv_key, priv_key_size, &user_priv_key);
if (ret != CRYS_OK) {
DEBUG("CRYS_ECPKI_BuildPrivKey failed with %s\n", cryptocell310_status_to_humanly_readable(ret));

Check warning on line 93 in pkg/driver_cryptocell_310/psa_cryptocell_310/ecc_common.c

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters
return CRYS_to_psa_error(ret);
goto done;
}

cryptocell_310_enable();
Expand All @@ -99,10 +101,12 @@ psa_status_t cryptocell_310_common_ecc_sign(const uint8_t *priv_key,
cryptocell_310_disable();
if (ret != CRYS_OK) {
DEBUG("CRYS_ECDSA_Sign failed with %s\n", cryptocell310_status_to_humanly_readable(ret));
return CRYS_to_psa_error(ret);
goto done;
}

return PSA_SUCCESS;
done:
explicit_bzero(&user_priv_key, sizeof(user_priv_key));
return CRYS_to_psa_error(ret);
}

psa_status_t cryptocell_310_common_ecc_verify(const uint8_t *pub_key,
Expand Down
13 changes: 9 additions & 4 deletions pkg/driver_cryptocell_310/psa_cryptocell_310/ecc_ed25519.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "crys_ec_edw_api.h"
#include "psa_error.h"
#include "cryptocell_310_util.h"
#include "string_utils.h"

#define ENABLE_DEBUG 0
#include "debug.h"
Expand Down Expand Up @@ -49,13 +50,15 @@ psa_status_t psa_generate_ecc_ed25519_key_pair( uint8_t *priv_key_buffer,
cryptocell_310_disable();
if (ret != CRYS_OK) {
DEBUG("CRYS_ECEDW_KeyPair failed with %s\n", cryptocell310_status_to_humanly_readable(ret));
return CRYS_to_psa_error(ret);
goto done;
}

memcpy(priv_key_buffer, secret_key, CRYS_ECEDW_ORD_SIZE_IN_BYTES);
memcpy(pub_key_buffer, &secret_key[CRYS_ECEDW_ORD_SIZE_IN_BYTES], CRYS_ECEDW_MOD_SIZE_IN_BYTES);

return PSA_SUCCESS;
done:
explicit_bzero(&secret_key, sizeof(secret_key));
return CRYS_to_psa_error(ret);
}

psa_status_t psa_ecc_ed25519_sign_message(const uint8_t *priv_key_buffer,
Expand Down Expand Up @@ -91,10 +94,12 @@ psa_status_t psa_ecc_ed25519_sign_message(const uint8_t *priv_key_buffer,
cryptocell_310_disable();
if (ret != CRYS_OK) {
DEBUG("CRYS_ECEDW_Sign failed with %s\n", cryptocell310_status_to_humanly_readable(ret));
return CRYS_to_psa_error(ret);
goto done;
}

return PSA_SUCCESS;
done:
explicit_bzero(&secret_key, sizeof(secret_key));
return CRYS_to_psa_error(ret);

(void)signature_size;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
psa_status_t CRYS_to_psa_error(CRYSError_t error)
{
switch (error) {
case CRYS_OK:
return PSA_SUCCESS;
case CRYS_HASH_ILLEGAL_OPERATION_MODE_ERROR:
case CRYS_HASH_IS_NOT_SUPPORTED:
return PSA_ERROR_NOT_SUPPORTED;
Expand Down

0 comments on commit 3627baa

Please sign in to comment.