How to get ramdom number between 1 and 2048 from /dev/hwrng? I am (perhaps naively) evaluating the security considerations of using the following code to create BIP39 seed phrases (on a read-only and offline Raspberry Pi).
Apparently, using jot -r 1 1 2048 is not ideal and I would like to leverage the HRNG (/dev/hwrng) on Raspberry Pi hardware.
printf &quot;%s\n&quot; &quot;Create random 24-word BIP39 seed phrase…&quot;
seed=()
for i in $(seq 1 24); do
  seed+=($(sed &quot;$(jot -r 1 1 2048)q;d&quot; &quot;/path/to/bip39.txt&quot;))
done
echo ${seed[@]}

$ cat bip39.txt | head     
abandon
ability
able
about
above
absent
absorb
abstract
absurd
abuse

Edit: this approach would not work given BIP39 seeds use last word as a checksum (see spec).
There are different versions of jot, and they use different sources of randomness. The Debian man page states:

Random numbers are obtained through random(3).


s defaults to a seed depending upon the time of day

random is not cryptographically secure. This is not explicitly mentioned in the documentation, so it's a good guess that it isn't¹. Even if it was, The seed being the time of day is very predictable. So jot on Debian is not at all suitable to generate a key or anything else that needs to be secure.
The OpenBSD man page states:

Random numbers are obtained through arc4random(3). Historical versions of jot used s to seed the random number generator. This is no longer supported.

arc4random on OpenBSD is secure, as documented in the man page². So on OpenBSD, your script generates a passphrase securely, with $24 \log_2(n)$ bits of entropy where $n$ is the length of the word lists (assuming the word lists has no duplicates).

If you have GNU utilities, use shuf. It can be instructed to use secure randomness:

--random-source=file
Use file as a source of random data used to determine which permutation to generate. See Random sources.


By default these commands use an internal pseudo-random generator initialized by a small amount of entropy, but can be directed to use an external source with the --random-source=file option. (…) The set of available sources depends on your operating system.

On Linux, /dev/urandom is secure except on a freshly installed system that doesn't have a hardware random generator. Since your platform has a hardware random generator, you don't need to worry about the initial seeding of the system random generator.
So with GNU coreutils, your script can just be
shuf --random-source=/dev/urandom -r -n 24 &lt;bip39.txt

¹I confirm that guess.
² Historically, arc4random used RC4, which is now broken. The output is biased, which would reduce the entropy of the resulting key, but not enough to break it. In any case OpenBSD switched to a different algorithm (currently ChaCha20), so arc4random is safe. Other modern BSD variants have switched arc4random to be secure as well, but beware that older releases of FreeBSD and NetBSD were insecure..