Trying to write a one-time pad implementation, most of the time I end up with key sizes shorter than the plaintext.
Already tried multiple APIs such as read()
and fread()
from /dev/(u)random
, linux getrandom()
and even openssl RAND_bytes()
. Whenever I check the length returned from those functions, it gives me the right amount of bytes, but the string itself (and therefore the keyfile) are somehow shorter.
Also tried with different datatypes, such as int
, uint8_t
and wchar_t
, and loops, but to no avail.
char *genkey(const size_t length)
{
char *key;
key = calloc(length, sizeof *key);
if (!key) ERROR("could not allocate memory\n");
getrandom(key, (length * sizeof *key), 0);
return key;
}
CodePudding user response:
At least one of the random bytes is 0 — so if you measure it as a string, it will typically be shorter as a string than the length you requested.
You are getting a byte array, not a string, from your random number generators.