This commit is contained in:
2025-09-05 11:59:29 +02:00
parent 6203420266
commit 24bc8bee8f
3 changed files with 127 additions and 39 deletions

26
LICENSE
View File

@@ -1,7 +1,6 @@
This software is available under 2 licenses -- choose whichever you prefer.
------------------------------------------------------------------------------
ALTERNATIVE A - MIT License
Copyright (c) 2017 Sean Barrett
=== MIT License ===
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
@@ -17,22 +16,3 @@ 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.
------------------------------------------------------------------------------
ALTERNATIVE B - Public Domain (www.unlicense.org)
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
software, either in source code form or as a compiled binary, for any purpose,
commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this
software dedicate any and all copyright interest in the software to the public
domain. We make this dedication for the benefit of the public at large and to
the detriment of our heirs and successors. We intend this dedication to be an
overt act of relinquishment in perpetuity of all present and future rights to
this software under copyright law.
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 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.

View File

@@ -3,19 +3,43 @@ MIT / Public Domain (see ./LICENSE)
Most libraries are not actually slow, but that is just the naming scheme.
## Features of all libraries
- C89 (or later) compatible
- intuitive to use
- don't pollude namespace
- configurable with pre-processor macros
## Libraries
- `./chacha20.h`
- `./slowarr.h`: C templated dynamic array
- `./slowgraph.h`: WIP graph library (this is the only library that is actually slow)
## CLI Tools
CLI utils built with slow-libs.
- `./slowgraph/sgraph-adj.c`: dgtxt graphs <-> adjacency matrix
## Cryptography Security Considerations
All cryptography libraries list possible side channel attacks in their documentation
| Library | Zeroing memory | Timing attacks | Notes |
| ------------ | -------------- | -------------- | ----- |
| `chacha20.h` | manual [^1] | impossible | [^2] |
[^1]: the compiler might optimize the zeroing away
[^2]: does not prevent against length-extension, by algorithm design. See Cryptography 101.
## Cryptography 101
- TODO: explain asymetric
- TODO: compare symetrics
- TODO: compare asymetrics
- TODO: compare hashes
- TODO: XOF
- TODO: rng
- TODO: length extension
- TODO: password hashing

View File

@@ -1,16 +1,100 @@
/* define SLOWGRAPH_IMPL */
/*
* Copyright (c) 2025 Alexander Nutz
* MIT licensed, see below documentation
*
*
* ======== ChaCha20 stream chiper ========
*
* Security considerations:
* - manually zeroize memory (depending on your application)
* - length extension attack:
* ChaCha20 is only a stream chiper, and, like AES,
* does NOT prevent against length extension attacks.
* Consider using ChaCha20-Poly1305 instead.
*
*
* Configuration options:
* - SLOWCRYPT_CHACHA20_IMPL
* - SLOWCRYPT_CHACHA20_FUNC
* will be used in front of every function definition / declaration
* - SLOWCRYPT_CHACHA20_UINT32
* if this is not set, will include <stdint.h>, and use `uint32_t`
*
*
* Compatibility:
* requires only a C89 compiler.
*
*
* Usage example 1: en-/de- crypt blocks of data
* slowcrypt_chacha20 state[2];
* uint32_t ctr = 1;
* char buf[64];
*
* iterate over blocks {
* copy block into buf (can pad with zeros)
* slowcrypt_chacha20_block(state, key, ctr, nonce, buf);
* ctr += 1;
* }
*
* # optionally zeroize memory
* slowcrypt_chacha20_deinit(&state[0]);
* slowcrypt_chacha20_deinit(&state[1]);
* bzero(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);
* slowcrypt_chacha20_run(state, &state[1], 20);
* slowcrypt_chacha20_serialize(buf, state);
* yield buf
* ctr += 1;
* }
*
* # optionally zeroize memory
* slowcrypt_chacha20_deinit(&state[0]);
* slowcrypt_chacha20_deinit(&state[1]);
* bzero(buf, 64);
*
*/
/*
* 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.
*/
#ifndef SLOWCRYPT_CHACHA20_H
#define SLOWCRYPT_CHACHA20_H
#ifndef SLOWCRYPT_CHACHA20_UINT32
#include <stdint.h>
#define SLOWCRYPT_CHACHA20_UINT32 uint32_t
#endif
#ifndef SLOWCRYPT_CHACHA20_FUNC
#define SLOWCRYPT_CHACHA20_FUNC /**/
#endif
typedef struct {
uint32_t state[16];
SLOWCRYPT_CHACHA20_UINT32 state[16];
} slowcrypt_chacha20;
/*
@@ -20,7 +104,7 @@ typedef struct {
*/
SLOWCRYPT_CHACHA20_FUNC void
slowcrypt_chacha20_block(slowcrypt_chacha20 state[2], char const key[32],
uint32_t block_ctr, char const nonce[12],
SLOWCRYPT_CHACHA20_UINT32 block_ctr, char const nonce[12],
char data[64]);
/* call this to zero out memory */
@@ -29,7 +113,7 @@ slowcrypt_chacha20_deinit(slowcrypt_chacha20 *state);
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_init(slowcrypt_chacha20 *state,
char const key[32],
uint32_t block_ctr,
SLOWCRYPT_CHACHA20_UINT32 block_ctr,
char const nonce[12]);
SLOWCRYPT_CHACHA20_FUNC void
@@ -42,15 +126,15 @@ SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_run(slowcrypt_chacha20 *state,
slowcrypt_chacha20 *swap,
int num_rounds);
SLOWCRYPT_CHACHA20_FUNC uint32_t 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,
uint32_t val);
SLOWCRYPT_CHACHA20_UINT32 val);
#define SLOWCRYPT_CHACHA20_LAST32(n, bits) (((uint32_t)(n)) >> (32 - (bits)))
#define SLOWCRYPT_CHACHA20_LAST32(n, bits) (((SLOWCRYPT_CHACHA20_UINT32)(n)) >> (32 - (bits)))
#define SLOWCRYPT_CHACHA20_ROL32(n, by) \
((((uint32_t)(n)) << (by)) | SLOWCRYPT_CHACHA20_LAST32((n), (by)))
((((SLOWCRYPT_CHACHA20_UINT32)(n)) << (by)) | SLOWCRYPT_CHACHA20_LAST32((n), (by)))
#define SLOWCRYPT_CHACHA20_QROUND(state, a, b, c, d) \
do { \
@@ -80,16 +164,16 @@ slowcrypt_chacha20_deinit(slowcrypt_chacha20 *state) {
*(volatile int *)&state->state[i] = 0;
}
SLOWCRYPT_CHACHA20_FUNC uint32_t slowcrypt_chacha20_read_ul32(char const *buf) {
uint32_t o = (uint32_t)((uint8_t const *)buf)[0];
o |= (uint32_t)((uint8_t const *)buf)[1] << 8;
o |= (uint32_t)((uint8_t const *)buf)[2] << 16;
o |= (uint32_t)((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,
uint32_t val) {
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);
@@ -98,7 +182,7 @@ SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_write_ul32(char *buf,
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_init(slowcrypt_chacha20 *state,
char const key[32],
uint32_t block_ctr,
SLOWCRYPT_CHACHA20_UINT32 block_ctr,
char const nonce[12]) {
int i;
state->state[0] = 0x61707865;
@@ -162,7 +246,7 @@ SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_run(slowcrypt_chacha20 *state,
SLOWCRYPT_CHACHA20_FUNC void
slowcrypt_chacha20_block(slowcrypt_chacha20 state[2], char const key[32],
uint32_t block_ctr, char const nonce[12],
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);