diff --git a/benchmarks/crand.c b/benchmarks/crand.c index bb9d142..52b94ae 100644 --- a/benchmarks/crand.c +++ b/benchmarks/crand.c @@ -4,7 +4,7 @@ int main(void) { printf("RAND_MAX = %d\n", RAND_MAX); - for (unsigned long long i = 0; i < 1000000000; ++i) + for (uint64_t i = 0; i < 1000000000; ++i) { rand(); } diff --git a/meson.build b/meson.build index e9c9299..4c03928 100644 --- a/meson.build +++ b/meson.build @@ -1,8 +1,11 @@ project('randquik', 'c') executable( 'randquik', - 'src/randquik.c', + 'src/cli.c', + 'src/chacha20.c', c_args: ['-Wall', '-O3', '-march=native'], install: true, ) dependency('threads') + +library('randquik-chacha20', 'src/chacha20.c', build_by_default: true) diff --git a/pyproject.toml b/pyproject.toml index 31d6801..0cdd708 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,5 +1,5 @@ [build-system] -requires = ["hatchling", "hatch-vcs"] +requires = ["hatchling", "hatch-vcs", "wheel", "cffi"] build-backend = "hatchling.build" [project] @@ -9,8 +9,14 @@ description = "Extremely fast and cryptographically secure random number generat readme = "README.md" license = "" authors = [{ name = "Vasanko" }] -classifiers = [] -dependencies = [] +classifiers = [ + "Operating System :: POSIX", + "Operating System :: Unix", + "Topic :: Security :: Cryptography", + "Topic :: Security", + "Topic :: Software Development :: Libraries :: Python Modules", +] +dependencies = ["cffi>=1.0.1"] requires-python = ">=3.10" keywords = [ "random", @@ -22,10 +28,9 @@ keywords = [ ] [project.urls] -Homepage = "" [project.optional-dependencies] -dev = ["pytest"] +dev = ["pytest", "ruff"] [tool.hatchling] diff --git a/src/c-stream.h b/src/c-stream.h index 6bc6239..6c33591 100644 --- a/src/c-stream.h +++ b/src/c-stream.h @@ -2,26 +2,25 @@ #include #include -#define QUARTERSTEP(a, b, c, n) \ - a += b; \ - c ^= a; \ +#define QUARTERSTEP(a, b, c, n) \ + a += b; \ + c ^= a; \ c = (c << n) | (c >> (32 - n)) -#define QUARTERROUND(a, b, c, d) \ - QUARTERSTEP(a, b, d, 16); \ - QUARTERSTEP(c, d, b, 12); \ - QUARTERSTEP(a, b, d, 8); \ +#define QUARTERROUND(a, b, c, d) \ + QUARTERSTEP(a, b, d, 16); \ + QUARTERSTEP(c, d, b, 12); \ + QUARTERSTEP(a, b, d, 8); \ QUARTERSTEP(c, d, b, 7); -{ - // Change variables x and orig... - uint32_t const *orig = x; - while (bytes > 0) - { +static inline uint64_t +_cha_block(uint32_t* state, uint8_t* begin, uint8_t* end) { + uint64_t* counter = (uint64_t*)&state[12]; + uint8_t* c = begin; + while (c < end) { uint32_t x[16]; - memcpy(x, orig, sizeof x); - for (int i = 20; i > 0; i -= 2) - { + memcpy(x, state, sizeof x); + for (int i = 20; i > 0; i -= 2) { QUARTERROUND(x[0], x[4], x[8], x[12]) QUARTERROUND(x[1], x[5], x[9], x[13]) QUARTERROUND(x[2], x[6], x[10], x[14]) @@ -32,20 +31,18 @@ QUARTERROUND(x[3], x[4], x[9], x[14]) } for (int i = 0; i < 16; i++) - x[i] += orig[i]; + x[i] += state[i]; - uint64_t *counter = (uint64_t *)&orig[12]; ++*counter; - if (bytes < 64) - { + uint64_t bytes = end - c; + if (bytes < 64) { memcpy(c, x, bytes); - c += bytes; - bytes = 0; + c = end; break; } memcpy(c, x, 64); - bytes -= 64; c += 64; } + return end - c; } diff --git a/src/chacha20.c b/src/chacha20.c new file mode 100644 index 0000000..0e38dcc --- /dev/null +++ b/src/chacha20.c @@ -0,0 +1,62 @@ +#ifdef __GNUC__ +#pragma GCC target("sse2") +#pragma GCC target("ssse3") +#pragma GCC target("avx2") +#endif + +#include "chacha20.h" + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "c-stream.h" +#include "u4-stream.h" +#include "u8-stream.h" + +void cha_init(cha_ctx* ctx, const uint8_t* key, const uint8_t* iv) { + ctx->input[0] = 0x61707865; + ctx->input[1] = 0x3320646e; + ctx->input[2] = 0x79622d32; + ctx->input[3] = 0x6b206574; + memcpy(ctx->input + 4, key, 32); + memcpy(ctx->input + 12, iv, 16); +} + +void cha_wipe(cha_ctx* ctx) { memset(&ctx, 0, sizeof(cha_ctx)); } + +int cha_update(cha_ctx* ctx, uint8_t* out, uint64_t outlen) { + // The included header will mess with these variables + uint8_t* c = out; + uint8_t* end = out + outlen; + // TODO: Handle resume if we are not at block boundary + if (__builtin_cpu_supports("avx2")) { + c += _cha_8block(ctx->input, c, end); + assert(end - c < 512); + c += _cha_4block(ctx->input, c, end); + assert(end - c < 256); + } + c += _cha_block(ctx->input, c, end); + assert(c == end); + return 0; +} + +// ChaCha20 +int cha_generate( + uint8_t* out, uint64_t outlen, const uint8_t key[32], const uint8_t iv[16] +) { + cha_ctx ctx; + cha_init(&ctx, key, iv); + cha_update(&ctx, out, outlen); + cha_wipe(&ctx); + return 0; +} diff --git a/src/chacha20.h b/src/chacha20.h new file mode 100644 index 0000000..1eea9a5 --- /dev/null +++ b/src/chacha20.h @@ -0,0 +1,36 @@ +#include +#include + +static const uint64_t CHA_BLOCK_SIZE = 64; + +typedef struct cha_ctx { + uint32_t input[16]; +} cha_ctx; + +/// @brief Initialize cha_ctx +/// @param ctx holds ChaCha20 state +/// @param key 32 byte key +/// @param iv 16 bytes, where normally initial 4-8 bytes are zeroes and the rest +/// nonce +void cha_init(cha_ctx* ctx, const uint8_t* key, const uint8_t* iv); + +/// Dispose of sensitive data within the context +void cha_wipe(cha_ctx* ctx); + +/// @brief Incremental upgrade +/// @param ctx Gets updated +/// @param out +/// @param outlen +/// @return +int cha_update(cha_ctx* ctx, uint8_t* out, uint64_t outlen); + +/// @brief Produce a requested number of random bytes of the stream. +/// @param out +/// @param outlen +/// @param key +/// @param iv +/// @return +int cha_generate( + unsigned char* out, uint64_t outlen, const unsigned char key[32], + const unsigned char iv[16] +); diff --git a/src/cli.c b/src/cli.c new file mode 100644 index 0000000..afbc0ac --- /dev/null +++ b/src/cli.c @@ -0,0 +1,256 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "chacha20.h" + +static volatile bool quit = false; + +void signal_handler(int sig) { + quit = true; + signal(SIGINT, SIG_DFL); + signal(SIGTERM, SIG_DFL); +} + +#define BLOCK_SIZE (1 << 21) // 2 MiB seems optimal for speed + +static const unsigned char default_iv[16] = "\0\0\0\0\0\0\0\0RandQuik"; +typedef struct thread_args { + int index; + int done; + unsigned char* buf; + unsigned char key[32]; + unsigned workers; + pthread_mutex_t lock; + pthread_cond_t cond; + pthread_t thread; +} thread_args; + +void* producer_thread(void* a) { + thread_args* args = (thread_args*)a; + const uint64_t ivstep = args->workers * BLOCK_SIZE / 64; + while (!quit) { + pthread_mutex_lock(&args->lock); + while (args->done) { + pthread_cond_wait(&args->cond, &args->lock); + } + unsigned char iv[16]; + memcpy(iv, default_iv, 16); + *(uint64_t*)iv += args->index * ivstep; // Counter increment + cha_generate(args->buf, BLOCK_SIZE, args->key, default_iv); + args->done = 1; + pthread_cond_signal(&args->cond); + pthread_mutex_unlock(&args->lock); + } + return NULL; +} + +void print_status( + uint64_t bytes, uint64_t max_bytes, struct timespec start_time +) { + struct timespec end_time; + clock_gettime(CLOCK_MONOTONIC, &end_time); + double t = (end_time.tv_sec - start_time.tv_sec) + + 1e-9 * (end_time.tv_nsec - start_time.tv_nsec); + char buf[64] = {}; + double speed = bytes / t; + char const* unit = "MB"; + double m = 1e-6; + if (speed > 0.5e9) { + unit = "GB"; + m = 1e-9; + } + if (max_bytes) { + snprintf(buf, sizeof buf - 1, " of %'.0lf", m * max_bytes); + } + + fprintf( + stderr, "\r%5.0lf%s %s written, %.2lf %s/s.\e[K", m * bytes, buf, unit, + m * speed, unit + ); +} + +int fast( + FILE* f, unsigned workers, uint64_t max_bytes, unsigned char const key[32], + unsigned char const iv[16] +) { + thread_args args[workers]; + memset(args, 0, sizeof args); + for (int i = 0; i < workers; ++i) { + args[i].index = i; + args[i].buf = malloc(BLOCK_SIZE); + args[i].workers = workers; + memcpy(args[i].key, key, 32); + pthread_mutex_init(&args[i].lock, NULL); + pthread_cond_init(&args[i].cond, NULL); + pthread_create(&args[i].thread, NULL, producer_thread, &args[i]); + } + + struct timespec start_time; + clock_gettime(CLOCK_MONOTONIC, &start_time); + + int i = -1; + uint64_t bytes = 0; + while (!quit) { + i = (i + 1) % workers; + pthread_mutex_lock(&args[i].lock); + while (!args[i].done) { + pthread_cond_wait(&args[i].cond, &args[i].lock); + } + if (bytes % (1 << 30) == 0 || bytes + BLOCK_SIZE >= max_bytes) { + print_status(bytes, max_bytes, start_time); + } + uint64_t sz = BLOCK_SIZE; + if (max_bytes && bytes + sz >= max_bytes) { + fprintf(stderr, "\r\e[KMax reached\n"); + sz = max_bytes - bytes; + quit = true; + } + if (fwrite(args[i].buf, sz, 1, f) != 1) { + quit = true; + fprintf(stderr, "\r\e[KWrite failed: %s\n", strerror(errno)); + } + bytes += sz; + args[i].done = 0; + pthread_cond_signal(&args[i].cond); + pthread_mutex_unlock(&args[i].lock); + } + + print_status(bytes, max_bytes, start_time); + for (int i = 0; i < workers; ++i) { + args[i].done = 0; + pthread_cancel(args[i].thread); + pthread_join(args[i].thread, NULL); + pthread_mutex_destroy(&args[i].lock); + pthread_cond_destroy(&args[i].cond); + free(args[i].buf); + } + fprintf(stderr, "\nRandQuik wrote %lu bytes!\n\n", bytes); + return 0; +} + +bool parse_hex(char* str, unsigned char* buf, size_t len) { + for (size_t i = 0; i < len; ++i) { + int sz = 0; + if (sscanf(str, "%2hhx%n", buf + i, &sz) != 1) { + if (*str) { + fprintf(stderr, "Unable to read seed at `%s`\n\n", str); + return false; + } + return true; // Shorter than key length is OK + } + str += sz; + } + return true; +} + +void print_hex(unsigned char* buf, size_t len) { + for (size_t i = 0; i < len; ++i) { + fprintf(stderr, "%02hhx", buf[i]); + } +} + +void help(char** argv) { + fprintf( + stderr, + "Usage: %s [-t #threads] [-s hexseed] [-b #bytes] [-o outputfile]\n\n", + argv[0] + ); +} + +int main(int argc, char** argv) { + unsigned char key[32] = {}; + unsigned char iv[16] = {}; + unsigned int workers = 8; + char* output = NULL; + uint64_t max_bytes = 0; + bool seeded = false; + for (char opt; (opt = getopt(argc, argv, "bost")) != -1;) { + if (opt == 't') { + if (optind >= argc || sscanf(argv[optind++], "%u", &workers) != 1) { + fprintf( + stderr, "Expected the number of worker threads after -t\n" + ); + return 1; + } + continue; + } + if (opt == 's') { + if (optind >= argc || !parse_hex(argv[optind++], key, 32)) { + fprintf(stderr, "Expected a hex seed string after -s\n"); + return 1; + } + seeded = true; + continue; + } + if (opt == 'o') { + if (optind >= argc) { + fprintf(stderr, "Expected output filename after -s\n"); + return 1; + } + if (strcmp(argv[optind], "-") != 0) { + output = argv[optind++]; + } + continue; + } + if (opt == 'b') { + if (optind >= argc || sscanf(argv[optind++], "%lu", &max_bytes) != 1) { + fprintf( + stderr, + "Expected a maximum number of bytes to read after -b\n" + ); + return 1; + } + continue; + } + help(argv); + return 1; + } + FILE* f = stdout; + if (output) { + f = fopen(output, "wb"); + if (!f) { + fprintf(stderr, "Failed to open %s for writing.\n", output); + return 1; + } + } else if (isatty(1)) { + fprintf( + stderr, + "Won't print random on console. Pipe me to another program or " + "file instead.\n\n" + ); + help(argv); + return 1; + } + if (!seeded) { + FILE* urand = fopen("/dev/urandom", "rb"); + if (!urand || fread(key, 32, 1, urand) != 1) { + fprintf( + stderr, "Failed to seed from /dev/urandom. Use -s hexstring for " + "manual seeding.\n" + ); + fclose(urand); + return 1; + } + fclose(urand); + fprintf( + stderr, + "Random seed generated. This sequence may be repeated by:\n%s -s ", + argv[0] + ); + print_hex(key, 32); + fprintf(stderr, "\n\n"); + } + signal(SIGINT, signal_handler); + signal(SIGTERM, signal_handler); + int ret = fast(f, workers, max_bytes, key, iv); + fclose(f); + return ret; +} diff --git a/src/u4-stream.h b/src/u4-stream.h index 2eebda5..9975ce9 100644 --- a/src/u4-stream.h +++ b/src/u4-stream.h @@ -1,56 +1,61 @@ -#define VEC4_ROT(A, IMM) \ +#define VEC4_ROT(A, IMM) \ _mm_or_si128(_mm_slli_epi32(A, IMM), _mm_srli_epi32(A, (32 - IMM))) /* same, but replace 2 of the shift/shift/or "rotation" by byte shuffles (8 & * 16) (better) */ -#define VEC4_QUARTERROUND(A, B, C, D) \ - x_##A = _mm_add_epi32(x_##A, x_##B); \ - t_##A = _mm_xor_si128(x_##D, x_##A); \ - x_##D = _mm_shuffle_epi8(t_##A, rot16); \ - x_##C = _mm_add_epi32(x_##C, x_##D); \ - t_##C = _mm_xor_si128(x_##B, x_##C); \ - x_##B = VEC4_ROT(t_##C, 12); \ - x_##A = _mm_add_epi32(x_##A, x_##B); \ - t_##A = _mm_xor_si128(x_##D, x_##A); \ - x_##D = _mm_shuffle_epi8(t_##A, rot8); \ - x_##C = _mm_add_epi32(x_##C, x_##D); \ - t_##C = _mm_xor_si128(x_##B, x_##C); \ +#define VEC4_QUARTERROUND(A, B, C, D) \ + x_##A = _mm_add_epi32(x_##A, x_##B); \ + t_##A = _mm_xor_si128(x_##D, x_##A); \ + x_##D = _mm_shuffle_epi8(t_##A, rot16); \ + x_##C = _mm_add_epi32(x_##C, x_##D); \ + t_##C = _mm_xor_si128(x_##B, x_##C); \ + x_##B = VEC4_ROT(t_##C, 12); \ + x_##A = _mm_add_epi32(x_##A, x_##B); \ + t_##A = _mm_xor_si128(x_##D, x_##A); \ + x_##D = _mm_shuffle_epi8(t_##A, rot8); \ + x_##C = _mm_add_epi32(x_##C, x_##D); \ + t_##C = _mm_xor_si128(x_##B, x_##C); \ x_##B = VEC4_ROT(t_##C, 7) -#define ONEQUAD(A, B, C, D, CT) \ - { \ - /* Add original block */ \ - x_##A = _mm_add_epi32(x_##A, orig##A); \ - x_##B = _mm_add_epi32(x_##B, orig##B); \ - x_##C = _mm_add_epi32(x_##C, orig##C); \ - x_##D = _mm_add_epi32(x_##D, orig##D); \ - /* Transpose */ \ - t_##A = _mm_unpacklo_epi32(x_##A, x_##B); \ - t_##B = _mm_unpacklo_epi32(x_##C, x_##D); \ - t_##C = _mm_unpackhi_epi32(x_##A, x_##B); \ - t_##D = _mm_unpackhi_epi32(x_##C, x_##D); \ - x_##A = _mm_unpacklo_epi64(t_##A, t_##B); \ - x_##B = _mm_unpackhi_epi64(t_##A, t_##B); \ - x_##C = _mm_unpacklo_epi64(t_##C, t_##D); \ - x_##D = _mm_unpackhi_epi64(t_##C, t_##D); \ - \ - _mm_storeu_si128((__m128i *)(CT), x_##A); \ - _mm_storeu_si128((__m128i *)(CT + 64), x_##B); \ - _mm_storeu_si128((__m128i *)(CT + 128), x_##C); \ - _mm_storeu_si128((__m128i *)(CT + 192), x_##D); \ +#define ONEQUAD(A, B, C, D, CT) \ + { \ + /* Add original block */ \ + x_##A = _mm_add_epi32(x_##A, orig##A); \ + x_##B = _mm_add_epi32(x_##B, orig##B); \ + x_##C = _mm_add_epi32(x_##C, orig##C); \ + x_##D = _mm_add_epi32(x_##D, orig##D); \ + /* Transpose */ \ + t_##A = _mm_unpacklo_epi32(x_##A, x_##B); \ + t_##B = _mm_unpacklo_epi32(x_##C, x_##D); \ + t_##C = _mm_unpackhi_epi32(x_##A, x_##B); \ + t_##D = _mm_unpackhi_epi32(x_##C, x_##D); \ + x_##A = _mm_unpacklo_epi64(t_##A, t_##B); \ + x_##B = _mm_unpackhi_epi64(t_##A, t_##B); \ + x_##C = _mm_unpacklo_epi64(t_##C, t_##D); \ + x_##D = _mm_unpackhi_epi64(t_##C, t_##D); \ + \ + _mm_storeu_si128((__m128i*)(CT), x_##A); \ + _mm_storeu_si128((__m128i*)(CT + 64), x_##B); \ + _mm_storeu_si128((__m128i*)(CT + 128), x_##C); \ + _mm_storeu_si128((__m128i*)(CT + 192), x_##D); \ } -if (bytes >= 256) -{ - const __m256i vec_increment = _mm256_set_epi64x(3, 2, 1, 0); // 0, 1, 2, 3 for the increments - const __m256i interleave = _mm256_set_epi32(7, 5, 3, 1, 6, 4, 2, 0); // Indices for counters +static inline uint64_t +_cha_4block(uint32_t* state, uint8_t* begin, uint8_t* end) { + if (end - begin < 256) + return 0; + uint32_t* x = state; + const __m256i vec_increment = + _mm256_set_epi64x(3, 2, 1, 0); // 0, 1, 2, 3 for the increments + const __m256i interleave = + _mm256_set_epi32(7, 5, 3, 1, 6, 4, 2, 0); // Indices for counters /* constant for shuffling bytes (replacing multiple-of-8 rotates) */ const __m128i rot16 = - _mm_set_epi8(13, 12, 15, 14, 9, 8, 11, 10, 5, 4, 7, 6, 1, 0, 3, 2); + _mm_set_epi8(13, 12, 15, 14, 9, 8, 11, 10, 5, 4, 7, 6, 1, 0, 3, 2); const __m128i rot8 = - _mm_set_epi8(14, 13, 12, 15, 10, 9, 8, 11, 6, 5, 4, 7, 2, 1, 0, 3); + _mm_set_epi8(14, 13, 12, 15, 10, 9, 8, 11, 6, 5, 4, 7, 2, 1, 0, 3); // Load state to vectors, duplicate four times __m128i x_0 = _mm_set1_epi32(x[0]); @@ -86,10 +91,11 @@ if (bytes >= 256) __m128i orig14 = x_14; __m128i orig15 = x_15; __m128i t_0, t_1, t_2, t_3, t_4, t_5, t_6, t_7, t_8, t_9, t_10, t_11, t_12, - t_13, t_14, t_15; + t_13, t_14, t_15; - while (bytes >= 256) - { + uint8_t* c = begin; + uint64_t* counter = (uint64_t*)&x[12]; // low u32 in 12, high u32 in 13 + while (end - c >= 256) { x_0 = orig0; x_1 = orig1; x_2 = orig2; @@ -105,15 +111,15 @@ if (bytes >= 256) x_14 = orig14; x_15 = orig15; - // Calculate counter + 0..3 for adjacent blocks (x12 low and x13 high of each) - uint64_t *counter = (uint64_t *)&x[12]; - __m256i counters = _mm256_add_epi64(_mm256_set1_epi64x(*counter), vec_increment); + // Calculate counter + 0..3 for adjacent blocks (x12 low and x13 + // high of each) + __m256i counters = + _mm256_add_epi64(_mm256_set1_epi64x(*counter), vec_increment); counters = _mm256_permutevar8x32_epi32(counters, interleave); x_12 = _mm256_extracti128_si256(counters, 0); x_13 = _mm256_extracti128_si256(counters, 1); - for (int i = 0; i < 10; ++i) - { + for (int i = 0; i < 10; ++i) { // Mix columns VEC4_QUARTERROUND(0, 4, 8, 12); VEC4_QUARTERROUND(1, 5, 9, 13); @@ -132,9 +138,9 @@ if (bytes >= 256) ONEQUAD(12, 13, 14, 15, c + 48); *counter += 4; - bytes -= 256; c += 256; } + return c - begin; // Bytes written } #undef ONEQUAD