change fmt style

This commit is contained in:
2025-09-05 12:13:53 +02:00
parent bd09bae088
commit 7e08f3e736
11 changed files with 591 additions and 226 deletions

View File

@@ -2,15 +2,16 @@
* Copyright (c) 2025 Alexander Nutz
* MIT licensed, see below documentation
*
* Latest version can be found at: https://gitea.vxcc.dev/alexander.nutz/slow-libs
* Latest version can be found at:
* https://gitea.vxcc.dev/alexander.nutz/slow-libs
*
*
* ======== ChaCha20 stream chiper ========
* ======== ChaCha20 stream cihper ========
*
* Security considerations:
* - manually zeroize memory (depending on your application)
* - length extension attack:
* ChaCha20 is only a stream chiper, and, like AES,
* ChaCha20 is only a stream cihper, and, like AES,
* does NOT prevent against length extension attacks.
* Consider using ChaCha20-Poly1305 instead.
*
@@ -44,10 +45,8 @@
* bzero(buf, 64);
*
*
* Usage example 2: CSPRNG (cryptographically secure pseudo random number generator)
* slowcrypt_chacha20 state[2];
* uint32_t ctr = 1;
* char buf[64];
* Usage example 2: CSPRNG (cryptographically secure pseudo random number
* generator) slowcrypt_chacha20 state[2]; uint32_t ctr = 1; char buf[64];
*
* while need random numbers {
* slowcrypt_chacha20_init(state, key, block_ctr, nonce);
@@ -66,21 +65,20 @@
/*
* Copyright (c) 2025 Alexander Nutz
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions: The above copyright
* notice and this permission notice shall be included in all copies or
* substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS",
* WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef SLOWCRYPT_CHACHA20_H
@@ -95,7 +93,8 @@
#define SLOWCRYPT_CHACHA20_FUNC /**/
#endif
typedef struct {
typedef struct
{
SLOWCRYPT_CHACHA20_UINT32 state[16];
} slowcrypt_chacha20;
@@ -104,110 +103,134 @@ typedef struct {
*
* does NOT zeroize states! zeroize manually when done.
*/
SLOWCRYPT_CHACHA20_FUNC void
slowcrypt_chacha20_block(slowcrypt_chacha20 state[2], char const key[32],
SLOWCRYPT_CHACHA20_UINT32 block_ctr, char const nonce[12],
char data[64]);
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_block(
slowcrypt_chacha20 state[2],
char const key[32],
SLOWCRYPT_CHACHA20_UINT32 block_ctr,
char const nonce[12],
char data[64]);
/* call this to zero out memory */
SLOWCRYPT_CHACHA20_FUNC void
slowcrypt_chacha20_deinit(slowcrypt_chacha20 *state);
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_deinit(
slowcrypt_chacha20* state);
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_init(slowcrypt_chacha20 *state,
char const key[32],
SLOWCRYPT_CHACHA20_UINT32 block_ctr,
char const nonce[12]);
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_init(
slowcrypt_chacha20* state,
char const key[32],
SLOWCRYPT_CHACHA20_UINT32 block_ctr,
char const nonce[12]);
SLOWCRYPT_CHACHA20_FUNC void
slowcrypt_chacha20_serialize(char buf[64], slowcrypt_chacha20 const *state);
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_serialize(
char buf[64],
slowcrypt_chacha20 const* state);
SLOWCRYPT_CHACHA20_FUNC void
slowcrypt_chacha20_serialize_xor(char buf[64], slowcrypt_chacha20 const *state);
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_serialize_xor(
char buf[64],
slowcrypt_chacha20 const* state);
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_run(slowcrypt_chacha20 *state,
slowcrypt_chacha20 *swap,
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_run(slowcrypt_chacha20* state,
slowcrypt_chacha20* swap,
int num_rounds);
SLOWCRYPT_CHACHA20_FUNC SLOWCRYPT_CHACHA20_UINT32 slowcrypt_chacha20_read_ul32(char const *buf);
SLOWCRYPT_CHACHA20_FUNC SLOWCRYPT_CHACHA20_UINT32
slowcrypt_chacha20_read_ul32(char const* buf);
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_write_ul32(char *buf,
SLOWCRYPT_CHACHA20_UINT32 val);
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_write_ul32(
char* buf,
SLOWCRYPT_CHACHA20_UINT32 val);
#define SLOWCRYPT_CHACHA20_LAST32(n, bits) (((SLOWCRYPT_CHACHA20_UINT32)(n)) >> (32 - (bits)))
#define SLOWCRYPT_CHACHA20_LAST32(n, bits) \
(((SLOWCRYPT_CHACHA20_UINT32)(n)) >> (32 - (bits)))
#define SLOWCRYPT_CHACHA20_ROL32(n, by) \
((((SLOWCRYPT_CHACHA20_UINT32)(n)) << (by)) | SLOWCRYPT_CHACHA20_LAST32((n), (by)))
#define SLOWCRYPT_CHACHA20_ROL32(n, by) \
((((SLOWCRYPT_CHACHA20_UINT32)(n)) << (by)) | \
SLOWCRYPT_CHACHA20_LAST32((n), (by)))
#define SLOWCRYPT_CHACHA20_QROUND(state, a, b, c, d) \
do { \
state[a] += state[b]; \
state[d] ^= state[a]; \
state[d] = SLOWCRYPT_CHACHA20_ROL32(state[d], 16); \
\
state[c] += state[d]; \
state[b] ^= state[c]; \
state[b] = SLOWCRYPT_CHACHA20_ROL32(state[b], 12); \
\
state[a] += state[b]; \
state[d] ^= state[a]; \
state[d] = SLOWCRYPT_CHACHA20_ROL32(state[d], 8); \
\
state[c] += state[d]; \
state[b] ^= state[c]; \
state[b] = SLOWCRYPT_CHACHA20_ROL32(state[b], 7); \
#define SLOWCRYPT_CHACHA20_QROUND(state, a, b, c, d) \
do { \
state[a] += state[b]; \
state[d] ^= state[a]; \
state[d] = SLOWCRYPT_CHACHA20_ROL32(state[d], 16); \
\
state[c] += state[d]; \
state[b] ^= state[c]; \
state[b] = SLOWCRYPT_CHACHA20_ROL32(state[b], 12); \
\
state[a] += state[b]; \
state[d] ^= state[a]; \
state[d] = SLOWCRYPT_CHACHA20_ROL32(state[d], 8); \
\
state[c] += state[d]; \
state[b] ^= state[c]; \
state[b] = SLOWCRYPT_CHACHA20_ROL32(state[b], 7); \
} while (0)
#ifdef SLOWCRYPT_CHACHA20_IMPL
SLOWCRYPT_CHACHA20_FUNC void
slowcrypt_chacha20_deinit(slowcrypt_chacha20 *state) {
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_deinit(
slowcrypt_chacha20* state)
{
int i;
for (i = 0; i < 16; i++)
*(volatile int *)&state->state[i] = 0;
*(volatile int*)&state->state[i] = 0;
}
SLOWCRYPT_CHACHA20_FUNC SLOWCRYPT_CHACHA20_UINT32 slowcrypt_chacha20_read_ul32(char const *buf) {
SLOWCRYPT_CHACHA20_UINT32 o = (SLOWCRYPT_CHACHA20_UINT32)((uint8_t const *)buf)[0];
o |= (SLOWCRYPT_CHACHA20_UINT32)((uint8_t const *)buf)[1] << 8;
o |= (SLOWCRYPT_CHACHA20_UINT32)((uint8_t const *)buf)[2] << 16;
o |= (SLOWCRYPT_CHACHA20_UINT32)((uint8_t const *)buf)[3] << 24;
SLOWCRYPT_CHACHA20_FUNC SLOWCRYPT_CHACHA20_UINT32
slowcrypt_chacha20_read_ul32(char const* buf)
{
SLOWCRYPT_CHACHA20_UINT32 o =
(SLOWCRYPT_CHACHA20_UINT32)((uint8_t const*)buf)[0];
o |= (SLOWCRYPT_CHACHA20_UINT32)((uint8_t const*)buf)[1] << 8;
o |= (SLOWCRYPT_CHACHA20_UINT32)((uint8_t const*)buf)[2] << 16;
o |= (SLOWCRYPT_CHACHA20_UINT32)((uint8_t const*)buf)[3] << 24;
return o;
}
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_write_ul32(char *buf,
SLOWCRYPT_CHACHA20_UINT32 val) {
((uint8_t *)buf)[0] = (uint8_t)(val & 0xFF);
((uint8_t *)buf)[1] = (uint8_t)((val >> 8) & 0xFF);
((uint8_t *)buf)[2] = (uint8_t)((val >> 16) & 0xFF);
((uint8_t *)buf)[3] = (uint8_t)((val >> 24) & 0xFF);
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_write_ul32(
char* buf,
SLOWCRYPT_CHACHA20_UINT32 val)
{
((uint8_t*)buf)[0] = (uint8_t)(val & 0xFF);
((uint8_t*)buf)[1] = (uint8_t)((val >> 8) & 0xFF);
((uint8_t*)buf)[2] = (uint8_t)((val >> 16) & 0xFF);
((uint8_t*)buf)[3] = (uint8_t)((val >> 24) & 0xFF);
}
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_init(slowcrypt_chacha20 *state,
char const key[32],
SLOWCRYPT_CHACHA20_UINT32 block_ctr,
char const nonce[12]) {
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_init(
slowcrypt_chacha20* state,
char const key[32],
SLOWCRYPT_CHACHA20_UINT32 block_ctr,
char const nonce[12])
{
int i;
state->state[0] = 0x61707865;
state->state[1] = 0x3320646e;
state->state[2] = 0x79622d32;
state->state[3] = 0x6b206574;
for (i = 0; i < 8; i++)
state->state[4 + i] = slowcrypt_chacha20_read_ul32(&key[i * 4]);
state->state[12] = block_ctr;
for (i = 0; i < 3; i++)
state->state[13 + i] = slowcrypt_chacha20_read_ul32(&nonce[i * 4]);
}
SLOWCRYPT_CHACHA20_FUNC void
slowcrypt_chacha20_serialize(char buf[64], slowcrypt_chacha20 const *state) {
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_serialize(
char buf[64],
slowcrypt_chacha20 const* state)
{
int i;
for (i = 0; i < 16; i++)
slowcrypt_chacha20_write_ul32(&buf[i * 4], state->state[i]);
}
SLOWCRYPT_CHACHA20_FUNC void
slowcrypt_chacha20_serialize_xor(char buf[64],
slowcrypt_chacha20 const *state) {
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_serialize_xor(
char buf[64],
slowcrypt_chacha20 const* state)
{
char swp[4];
int i, j;
@@ -218,9 +241,10 @@ slowcrypt_chacha20_serialize_xor(char buf[64],
}
}
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_run(slowcrypt_chacha20 *state,
slowcrypt_chacha20 *swap,
int num_rounds) {
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_run(slowcrypt_chacha20* state,
slowcrypt_chacha20* swap,
int num_rounds)
{
int i;
for (i = 0; i < 16; i++)
@@ -246,10 +270,13 @@ SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_run(slowcrypt_chacha20 *state,
state->state[i] += swap->state[i];
}
SLOWCRYPT_CHACHA20_FUNC void
slowcrypt_chacha20_block(slowcrypt_chacha20 state[2], char const key[32],
SLOWCRYPT_CHACHA20_UINT32 block_ctr, char const nonce[12],
char data[64]) {
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_block(
slowcrypt_chacha20 state[2],
char const key[32],
SLOWCRYPT_CHACHA20_UINT32 block_ctr,
char const nonce[12],
char data[64])
{
slowcrypt_chacha20_init(state, key, block_ctr, nonce);
slowcrypt_chacha20_run(state, &state[1], 20);
slowcrypt_chacha20_serialize_xor(data, state);