rc4ok
Lightweight High-Performance Cryptographically Strong Random Number Generator based on Improved RC4
Overview
RC4OK is a light-weight, high-performance, cryptographically secure random number generator, which is based on an improved version of the RC4 stream cipher, which was proposed in https://ia.cr/2023/1486. It's suitable for use in IOT devices, which might lack presence of an operating system managed pseudo random number generator.
RC4OK pseudo-random number generator can be initialized with a non-empty seed string and it should produce arbitrary long pseudo-random bytes. True random events such as external peripheral interrupts can be used as entropy source and they can be added to the RC4OK PRNG state, though not yet in a thread-safe manner, in this implementation.
Prerequisites
Rust stable toolchain; see https://rustup.rs for installation guide.
# When developing this library, I was using
)
I advise you to also use cargo-criterion for running benchmark executable. Read more about it @ https://crates.io/crates/cargo-criterion. You can just issue following command for installing it system-wide.
Testing
For ensuring functional correctness and conformance of this RC4OK PRNG implementation, I generate Known Answer Tests using the official implementation by the RC4OK authors, living @ https://github.com/emercoin/rc4ok.
Note Those (reproducible) steps for generating KAT files are described in the gist https://gist.github.com/itzmeanjan/5d1379b4d324e888a2683d2820b57e23.
Issue following command to run all test cases.
Benchmarking
Issue following command for benchmarking RC4OK PRNG, with variable length input and output.
Warning When benchmarking make sure you've disabled CPU frequency scaling, otherwise numbers you see can be pretty misleading. I found https://github.com/google/benchmark/blob/b40db869/docs/reducing_variance.md helpful.
# In case you didn't install `cargo-criterion`, you've to run benchmark with
# RUSTFLAGS="-C opt-level=3 -C target-cpu=native" cargo bench rc4ok
RUSTFLAGS="-C opt-level=3 -C target-cpu=native"
On 12th Gen Intel(R) Core(TM) i7-1260P
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
Usage
Using RC4OK PRNG is fairly easy.
- Add
rc4okto the [dependencies] section of the Cargo.toml file of your project.
[]
= { = "https://github.com/itzmeanjan/rc4ok" }
# or
= "0.1.0"
- Initialize RC4OK pseudo-random number generator with a non-empty key i.e. seed.
use rc4ok;
- Request arbitrary many pseudo-random bytes from PRNG object.
// Generate pseudo-random bytes
rc4ok_prng.generate;
- You can add some entropy into the RC4OK PRNG state from time to time.
Warning RC4OK state is not yet thread-safe so you can't spawn a thread to harvest entropy and add that to the state of RC4OK PRNG from time to time.
let mut entropy = 0u16; // harvest 16 -bit entropy
rc4ok_prng.add_entropy; // Add entropy
- Finally you can reset the state of an existing RC4OK PRNG and reinit it with a new non-empty seed.
let another_seed = vec!; // Populate another seed
rc4ok_prng.reset; // Re-seed PRNG
I'm maintaining a program (see src/main.rs) which can be invoked as a binary and requested for producing arbitrary many psuedo-random bytes given a non-empty seed string.
Note
rc4okbinary executable writes requested-many or arbitrary-many pseudo-random bytes directly onto STDOUT device, hence you may want to pipe ( read more @ https://en.wikipedia.org/wiki/Pipeline_(Unix) ) the output to a file or another program.
# --- --- --- --- ---
# Encode output of rc4ok binary executable as hex string onto STDOUT.
# You may find https://stackoverflow.com/questions/6292645/convert-binary-data-to-hexadecimal-in-a-shell-script helpful.
| | && | | &&
# Compute SHA256 digest over output of rc4ok binary executable.
| | | |