Skip to content

Conversation

@reallxo
Copy link

@reallxo reallxo commented Dec 2, 2025

Contribution description

This pull request updates the former pull request “PSA cipher ChaCha20 multipart operation implementation” (#20788) by @Wunderbaeumchen99817 to the current state of the master branch.
The original changes have been rebased and split up into multiple commits to ensure clarity and make the review process easier.

The goal of this PR is to bring the previous contribution up to date and into a reviewable and maintainable form and request reviews from supervisors.

Testing procedure

TBD

Issues/PRs references

original pull request

Psa cipher chacha20 multipart operation implementation #20788
#20788

@github-actions github-actions bot added Area: tests Area: tests and testing framework Area: pkg Area: External package ports Area: sys Area: System labels Dec 2, 2025
@AnnsAnns AnnsAnns added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Dec 3, 2025
@AnnsAnns
Copy link
Member

AnnsAnns commented Dec 3, 2025

It sounds like nothing was changed aside from rebasing it and from the looks of it you were ready for the final review in the original PR @mguetschow, is that still correct?

@AnnsAnns AnnsAnns requested a review from mguetschow December 3, 2025 10:29
Copy link
Member

@AnnsAnns AnnsAnns left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your work on getting this merged again 😄

I did not look at the implementation itself, however, here are some nitpicks that have changed since the original PR was made. Also, please take a look at the static test output for a lot of complaints it has such as whitespaces, empty lines and long lines.

There are also a lot of todos left in multiple files, are these all still valid / are you addressing them in this PR?

@AnnsAnns AnnsAnns changed the title Rebase psa cipher chacha20 multipart operation implementation sys/psa_crypto: chacha20 multipart operation implementation [Rebase] Dec 3, 2025
@mguetschow mguetschow marked this pull request as draft December 3, 2025 11:26
@riot-ci
Copy link

riot-ci commented Dec 3, 2025

Murdock results

✔️ PASSED

c17c008 fixed broken chacha update functions

Success Failures Total Runtime
10997 0 11001 15m:07s

Artifacts

@publicvoid1337 publicvoid1337 force-pushed the rebase-psa-cipher-chacha20-multipart-operation-implementation branch from 789c8b8 to c04816e Compare December 4, 2025 18:30
@benpicco benpicco requested a review from Einhornhool December 9, 2025 12:55
@crasbe crasbe added the Type: new feature The issue requests / The PR implemements a new feature for RIOT label Dec 9, 2025
@reallxo reallxo force-pushed the rebase-psa-cipher-chacha20-multipart-operation-implementation branch from 98e208d to db565fd Compare January 3, 2026 18:13
@publicvoid1337 publicvoid1337 force-pushed the rebase-psa-cipher-chacha20-multipart-operation-implementation branch from 2e63b87 to 2d6f9c6 Compare January 13, 2026 20:38
Copy link
Contributor

@crasbe crasbe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although this PR is still in Draft stage, here are some style related comments.

Please not that I did not mark all the occurances of the documentation-blocks that have to be changed, please go through the code yourself to do the required changes.

#endif

#ifdef CPU_NRF52
#define CHECK_POINTER_DMA_ACCESS(p) ((unsigned int)p >= 0x20000000 ? (unsigned int)p < 0x40000000 : 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#define CHECK_POINTER_DMA_ACCESS(p) ((unsigned int)p >= 0x20000000 ? (unsigned int)p < 0x40000000 : 0)
# define CHECK_POINTER_DMA_ACCESS(p) ((unsigned int)p >= 0x20000000 ? (unsigned int)p < 0x40000000 : 0)

New code that uses preprocessor conditionals, the preprocessor commands inside if-else statements should be indented: https://github.com/RIOT-OS/RIOT/blob/master/CODING_CONVENTIONS.md#indentation-of-preprocessor-directives

Comment on lines -19 to -21
#ifndef PSA_CRYPTOCELL_310_CHACHA_H
#define PSA_CRYPTOCELL_310_CHACHA_H

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code is missing the #pragma once. Just removing the header guards may (will) cause havoc.

Comment on lines +73 to +75
/* 8 bytes: the cipher operation uses the original [CHACHA20] definition
of ChaCha20: the provided IV is used as the 64-bit nonce, and the 64-bit
counter value is set to zero. */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/* 8 bytes: the cipher operation uses the original [CHACHA20] definition
of ChaCha20: the provided IV is used as the 64-bit nonce, and the 64-bit
counter value is set to zero. */
/* 8 bytes: the cipher operation uses the original [CHACHA20] definition
* of ChaCha20: the provided IV is used as the 64-bit nonce, and the 64-bit
* counter value is set to zero. */

There is a specific style for multi-line comments: https://github.com/RIOT-OS/RIOT/blob/master/CODING_CONVENTIONS.md#comments

Comment on lines +79 to +80
/* 12 bytes: the provided IV is used as the nonce, and the counter value
is set to zero. */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/* 12 bytes: the provided IV is used as the nonce, and the counter value
is set to zero. */
/* 12 bytes: the provided IV is used as the nonce, and the counter value
* is set to zero. */

Comment on lines +92 to +93
/* 16 bytes: the first four bytes of the IV are used as the counter value
(encoded as little-endian), and the remaining 12 bytes are used as the nonce. */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/* 16 bytes: the first four bytes of the IV are used as the counter value
(encoded as little-endian), and the remaining 12 bytes are used as the nonce. */
/* 16 bytes: the first four bytes of the IV are used as the counter value
* (encoded as little-endian), and the remaining 12 bytes are used as the nonce. */

#endif

#if IS_USED(MODULE_PERIPH_CIPHER_CHACHA20)
#include "psa_periph_chacha20_ctx.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#include "psa_periph_chacha20_ctx.h"
# include "psa_periph_chacha20_ctx.h"

#include "crys_chacha.h"
#include "kernel_defines.h"

#if IS_USED(MODULE_PERIPH_CIPHER_CHACHA20) || DOXYGEN
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this actually required? The header is conditionally included anyway as far as I can tell.

Comment on lines +66 to +71
/* 8 bytes: the cipher operation uses the original [CHACHA20] definition
of ChaCha20: the provided IV is used as the 64-bit nonce, and the 64-bit
counter value is set to zero.
This is currently not supported, as the current implementation only handles
12-byte nonces. To change this, you would need to modify chacha20_ctx_t
and functions that are using this type. */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not really count as "short multiline comment" anymore, so please adapt it to the appropriate style: https://github.com/RIOT-OS/RIOT/blob/master/CODING_CONVENTIONS.md#comments

const uint8_t *data_in = &input[CHACHA20POLY1305_NONCE_BYTES];

chacha20_encrypt_decrypt(data_in, output, key_buffer, nonce, input_length - CHACHA20POLY1305_NONCE_BYTES);
chacha20_encrypt_decrypt(key_buffer, nonce, 0, data_in, input_length - CHACHA20POLY1305_NONCE_BYTES, output);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a line break here.

typedef cipher_t psa_cipher_aes_256_ctx_t;
#endif
#if IS_USED(MODULE_PSA_RIOT_CIPHER_CHACHA20)
#include "crypto/chacha20poly1305.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#include "crypto/chacha20poly1305.h"
# include "crypto/chacha20poly1305.h"

Copy link
Contributor

@mguetschow mguetschow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some preliminary comments. I know this is not your code originally, but I do have some questions that we should try to answer together (and maybe solve differently than in the current state).

{
DEBUG("Peripheral ChaCha20 Cipher setup");

if (key_length != CRYS_CHACHA_KEY_MAX_SIZE_IN_BYTES) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the key length really fixed, or should that rather be a <= as the macro name implies?

Comment on lines +94 to +99
periph_status = CRYS_CHACHA_Init( &ctx->ctx.post_setup,
(uint8_t*)&iv[4],
CRYS_CHACHA_Nonce96BitSize,
ctx->buffer,
unaligned_get_u32(iv),
ctx->ctx.mode);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks quite dangerous as ctx->ctx is a union and both are accessed here.

In particular, what would happen if someone called _set_iv twice?

I think the context needs an extra field to store the current state (none, setup-done, iv-set, encoding/decoding, done?)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah btw, this call misses the cryptocell_310_enable() call if Github is not betraying me.

Is this actually tested somewhere?

Comment on lines 134 to 141
if (ctx->buffer_length > 0) {
/* if yes, fill buffer up and update this block of data */
input_index = sizeof(ctx->buffer) - ctx->buffer_length;
memcpy(&ctx->buffer[ctx->buffer_length], input, input_index);
periph_status = CRYS_CHACHA_Block( &ctx->ctx.post_setup,
ctx->buffer,
64,
output);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you reworked this in your poly1305 branch, correct? Please update this branch accordingly.

/* if yes, fill buffer up and update this block of data */
input_index = sizeof(ctx->buffer) - ctx->buffer_length;
memcpy(&ctx->buffer[ctx->buffer_length], input, input_index);
periph_status = CRYS_CHACHA_Block( &ctx->ctx.post_setup,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should definitely check somewhere before that ctx has been setup and set-iv before.

Comment on lines -109 to +132
void chacha20_encrypt_decrypt(const uint8_t *input, uint8_t *output,
const uint8_t *key, const uint8_t *nonce,
size_t inputlen);
void chacha20_encrypt_decrypt( const uint8_t *key,
const uint8_t *nonce,
uint32_t counter,
const uint8_t *input,
size_t input_length,
uint8_t *output);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, so here we actually do change the public API of RIOT crypto code. We should definitely avoid this whenever possible (and here it seems it is only about reordering and renaming parameters).

Comment on lines -91 to -97
static void _xcrypt(chacha20poly1305_ctx_t *ctx, const uint8_t *key,
const uint8_t *nonce, const uint8_t *in, uint8_t *out,
size_t len, size_t counter)
{
/* Number of full 64 byte blocks */
const size_t num_blocks = len >> 6;
size_t pos = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, why can this be removed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: pkg Area: External package ports Area: sys Area: System Area: tests Area: tests and testing framework CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Type: new feature The issue requests / The PR implemements a new feature for RIOT

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants