Created
October 29, 2023 13:12
-
-
Save itzmeanjan/5d1379b4d324e888a2683d2820b57e23 to your computer and use it in GitHub Desktop.
Steps to Generate Known Answer Tests for RC4OK Pseudo Random Number Generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
gcc -Wall -Wextra -pedantic -O3 -march=native gen_kat.c rc4ok.c ./a.out | tee rc4ok.kat
rc4ok.kat
.