-
Notifications
You must be signed in to change notification settings - Fork 2k
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
sys/psa_crypto: one-shot Chacha20 support #20720
Merged
mguetschow
merged 1 commit into
RIOT-OS:master
from
netd-tud:chacha20-glue-code-implementation
Oct 29, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
pkg/driver_cryptocell_310/psa_cryptocell_310/cipher_chacha20.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/* | ||
* Copyright (C) 2024 TU Dresden | ||
* | ||
* This file is subject to the terms and conditions of the GNU Lesser | ||
* General Public License v2.1. See the file LICENSE in the top level | ||
* directory for more details. | ||
*/ | ||
|
||
/** | ||
* @ingroup pkg_driver_cryptocell_310 | ||
* @{ | ||
* | ||
* @file | ||
* @brief PSA Crypto wrapper for the CryptoCell 310 ChaCha API. | ||
* | ||
* @author Lennard Melling <[email protected]> | ||
* | ||
*/ | ||
#ifndef PSA_CRYPTOCELL_310_CHACHA_H | ||
#define PSA_CRYPTOCELL_310_CHACHA_H | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#include "psa/crypto.h" | ||
#include "crys_chacha.h" | ||
#include "cryptocell_310_util.h" | ||
#include "psa_error.h" | ||
|
||
#define ENABLE_DEBUG 0 | ||
#include "debug.h" | ||
|
||
psa_status_t psa_cipher_chacha20_encrypt(uint8_t *key_buffer, | ||
size_t key_buffer_size, | ||
const uint8_t *input, | ||
size_t input_length, | ||
uint8_t *output, | ||
size_t output_size, | ||
size_t *output_length) | ||
{ | ||
DEBUG("Peripheral ChaCha20 Cipher encryption"); | ||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | ||
|
||
if (!CHECK_POINTER_DMA_ACCESS(key_buffer) || | ||
!CHECK_POINTER_DMA_ACCESS(input) || | ||
!CHECK_POINTER_DMA_ACCESS(output)) { | ||
return PSA_ERROR_DATA_INVALID; | ||
} | ||
|
||
if (output_size < (input_length + CRYS_CHACHA_NONCE_MAX_SIZE_IN_BYTES)) { | ||
return PSA_ERROR_BUFFER_TOO_SMALL; | ||
} | ||
|
||
if (key_buffer_size != CRYS_CHACHA_KEY_MAX_SIZE_IN_BYTES) { | ||
return PSA_ERROR_INVALID_ARGUMENT; | ||
} | ||
|
||
uint8_t *nonce = &output[0]; | ||
uint8_t *data_out = &output[CRYS_CHACHA_NONCE_MAX_SIZE_IN_BYTES]; | ||
status = psa_generate_random(nonce, CRYS_CHACHA_NONCE_MAX_SIZE_IN_BYTES); | ||
if (status != PSA_SUCCESS) { | ||
return status; | ||
} | ||
|
||
cryptocell_310_enable(); | ||
CRYSError_t periph_status = CRYS_CHACHA(nonce, CRYS_CHACHA_Nonce96BitSize, | ||
key_buffer, 0UL, | ||
CRYS_CHACHA_Encrypt, | ||
(uint8_t *) input, | ||
input_length, | ||
data_out); | ||
cryptocell_310_disable(); | ||
status = CRYS_to_psa_error(periph_status); | ||
if (status != PSA_SUCCESS) { | ||
return status; | ||
} | ||
|
||
*output_length = input_length + CRYS_CHACHA_NONCE_MAX_SIZE_IN_BYTES; | ||
return PSA_SUCCESS; | ||
} | ||
|
||
psa_status_t psa_cipher_chacha20_decrypt(uint8_t *key_buffer, | ||
size_t key_buffer_size, | ||
const uint8_t *input, | ||
size_t input_length, | ||
uint8_t *output, | ||
size_t output_size, | ||
size_t *output_length) | ||
{ | ||
DEBUG("Peripheral ChaCha20 Cipher decryption"); | ||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | ||
|
||
if (!CHECK_POINTER_DMA_ACCESS(key_buffer) || | ||
!CHECK_POINTER_DMA_ACCESS(input) || | ||
!CHECK_POINTER_DMA_ACCESS(output)) { | ||
return PSA_ERROR_DATA_INVALID; | ||
} | ||
|
||
if ((key_buffer_size != CRYS_CHACHA_KEY_MAX_SIZE_IN_BYTES) || | ||
(input_length < CRYS_CHACHA_NONCE_MAX_SIZE_IN_BYTES)) { | ||
return PSA_ERROR_INVALID_ARGUMENT; | ||
} | ||
|
||
if (output_size < (input_length - CRYS_CHACHA_NONCE_MAX_SIZE_IN_BYTES)) { | ||
return PSA_ERROR_BUFFER_TOO_SMALL; | ||
} | ||
|
||
const uint8_t *nonce = &input[0]; | ||
const uint8_t *data_in = &input[CRYS_CHACHA_NONCE_MAX_SIZE_IN_BYTES]; | ||
size_t data_size = input_length - CRYS_CHACHA_NONCE_MAX_SIZE_IN_BYTES; | ||
|
||
cryptocell_310_enable(); | ||
CRYSError_t periph_status = CRYS_CHACHA((uint8_t *)nonce, CRYS_CHACHA_Nonce96BitSize, | ||
key_buffer, 0UL, | ||
CRYS_CHACHA_Decrypt, | ||
(uint8_t *)data_in, | ||
data_size, | ||
output); | ||
cryptocell_310_disable(); | ||
status = CRYS_to_psa_error(periph_status); | ||
if (status != PSA_SUCCESS) { | ||
return status; | ||
} | ||
|
||
*output_length = data_size; | ||
return PSA_SUCCESS; | ||
} | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif /* PSA_CRYPTOCELL_310_CHACHA_H */ | ||
/** @} */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll have to remember doing similar checks for all the other cryptocell 310 glue code, too. I'll open a ticket.