Skip to content

Instantly share code, notes, and snippets.

@itzmeanjan
Created October 29, 2023 13:12
Show Gist options
  • Save itzmeanjan/5d1379b4d324e888a2683d2820b57e23 to your computer and use it in GitHub Desktop.
Save itzmeanjan/5d1379b4d324e888a2683d2820b57e23 to your computer and use it in GitHub Desktop.
Steps to Generate Known Answer Tests for RC4OK Pseudo Random Number Generator
diff --git a/demo_rc4ok.c b/demo_rc4ok.c
index 72abbb1..968c191 100644
--- a/demo_rc4ok.c
+++ b/demo_rc4ok.c
@@ -3,7 +3,7 @@
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
-#include <rc4ok.h>
+#include "rc4ok.h"
/*-----------------------------------------------------------------------------*/
// Test key and output reference vector
diff --git a/gen_kat.c b/gen_kat.c
new file mode 100644
index 0000000..e82b981
--- /dev/null
+++ b/gen_kat.c
@@ -0,0 +1,83 @@
+#include "rc4ok.h"
+#include <math.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+static inline void
+to_hex(const uint8_t* const bytes, const size_t len)
+{
+ for (size_t i = 0; i < len; i++) {
+ printf("%.2x", bytes[i]);
+ }
+ printf("\n");
+}
+
+static inline size_t
+min(const size_t a, const size_t b)
+{
+ return a < b ? a : b;
+}
+
+// Ensure srand() is invoked properly, read
+// https://en.cppreference.com/w/c/numeric/random/srand carefully !
+static inline void
+gen_random(uint8_t* const bytes, const size_t len)
+{
+ const size_t bytes_per_word = ((size_t)round(log2((double)RAND_MAX))) / 8;
+
+ size_t off = 0;
+ while (off < len) {
+ const int res = rand();
+
+ const size_t elen = min(bytes_per_word, len - off);
+ memcpy(bytes + off, (uint8_t*)&res, elen);
+
+ off += elen;
+ }
+}
+
+int
+main(void)
+{
+ // Seeding for sake of reproducibility !
+ srand(0);
+
+ const size_t MIN_KEY_LEN = 8;
+ const size_t KEY_STEP_LEN = 1;
+ const size_t MAX_KEY_LEN = 4096;
+ const size_t FIXED_OUT_LEN = 1024;
+
+ uint8_t* key_bytes = (uint8_t*)malloc(MAX_KEY_LEN);
+ uint8_t* pr_bytes = (uint8_t*)malloc(FIXED_OUT_LEN);
+
+ memset(key_bytes, 0x00, MAX_KEY_LEN);
+ memset(pr_bytes, 0x00, FIXED_OUT_LEN);
+
+ size_t klen = MIN_KEY_LEN;
+ size_t off = 0;
+ while (klen <= MAX_KEY_LEN) {
+ gen_random(key_bytes + off, klen - off);
+
+ rc4ok ctx = { 0 };
+ rc4ok_ksa(&ctx, key_bytes, klen);
+ rc4ok_prng(&ctx, pr_bytes, FIXED_OUT_LEN);
+
+ printf("Key = ");
+ to_hex(key_bytes, klen);
+ printf("PRBytes = ");
+ to_hex(pr_bytes, FIXED_OUT_LEN);
+ printf("\n");
+
+ off = klen;
+ klen += KEY_STEP_LEN;
+ }
+
+ free(key_bytes);
+ free(pr_bytes);
+
+ return 0;
+}
diff --git a/rc4ok.c b/rc4ok.c
index 728295d..68d9860 100644
--- a/rc4ok.c
+++ b/rc4ok.c
@@ -1,4 +1,4 @@
-#include <rc4ok.h>
+#include "rc4ok.h"
/*-----------------------------------------------------------------------------*/
// Key Scheduling Algorithm
@@ -60,4 +60,3 @@ void rc4ok_addentropy(rc4ok *ctx, uint16_t x) {
#endif
*pj16 = ((*pj16 << 1) | (*pj16 >> 15)) + x;
} // rc4ok_addentropy
-
@itzmeanjan
Copy link
Author

Steps to Generate Known Answer Tests for RC4OK Pseudo Random Number Generator

RC4OK is a lightweight high-performance cryptographically strong random number generator based on improved RC4 stream cipher, which is proposed in paper https://ia.cr/2023/1486. Reference implementation of RC4OK lives @ https://github.com/emercoin/rc4ok. I'm maintaining these steps for generating Known Answer Tests for RC4OK because I needed some when I was working on https://github.com/itzmeanjan/rc4ok - a Rust library implementation of RC4OK PRNG.

  • Clone reference implementation of RC4OK.
git clone https://github.com/emercoin/rc4ok.git
  • Checkout RC4OK reference implementation to a specific git commit.
pushd rc4ok
git checkout 09f0724f4a2a2800a2d90639db376412ba22975e
popd
  • Clone the git patch file and apply it onto RC4OK git tree.
git clone https://gist.github.com/5d1379b4d324e888a2683d2820b57e23.git
pushd 5d1379b4d324e888a2683d2820b57e23
cp diff.patch ../rc4ok/
popd

pushd rc4ok
git apply diff.patch
git status # Optional
popd
  • Build and execute KAT generator program.
gcc -Wall -Wextra -pedantic -O3 -march=native gen_kat.c rc4ok.c
./a.out | tee rc4ok.kat
  • Generated KAT file is rc4ok.kat.
$ du -h rc4ok.kat
25M     rc4ok.kat

$ sha256sum rc4ok.kat
4469a0a076171b2daf57760330faa943da8c4b7c1db9bf6e74c9a989da573d53  rc4ok.kat

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