Making it more secure: RNG required

I imagined permutating the key to make it more secure and inevitably entered the slippery realm of deterministic pseudo-random number generator. Writing one from scratch is quite a challenge without prior experience or exposition to the field.

At first, I tried to start with a seed number and perform up to 32 different operations on the seed depending on the condition of the current sequence being divisible by some prime numbers. This approach yielded poor "randomness", especially in cases where the seed had poor bit distribution.

The prime derivation

To ensure the quality distribution of ones and zeros in the seed, I figured I could look at each bit and multiply a different prime number depending on whether the bit is set. Example:

result = 1

for x in bits.length
  left_bit_rotation (result, x)
  if bits[x] == 0
    result = result * prime_list[ x * 2 + 0 ]
  else
    result = result * prime_list[ x * 2 + 1 ]

return result

You can read the complete analysis of the method by reading this paper :
Using prime numbers to create high entropy derivative numbers

The quest for performance

To improve the speed and quality of randomness, I modified the algorithm to require much less condition verification for every iteration. After using Dieharder to measure "randomness" and tweaking the algorithm, here is my solution:

output = a ^ b ^ c

s = s + 1

if (s % 1024) == 0
  swap(a, c)

a = left_bit_rotation(a, 11)

if (s % 64) == 0
  b = left_bit_rotation( b, 13 )

c = output + left_bit_rotation( c, 17 )

return output

How to use it...

How to use the RNG to improve security? At first, I imagined initializing the RNG with the master key parameters and using it to alter the key after each byte has been processed. Since I am building a lookup table (LUT) to speed things up, this would invalidate it and rebuilding it constantly would negate the speed gain.

A solution would be to use it on larger chunks... Let's say I use the RNG to pull numbers from it for every block, it helps speed things up a bit but it still behaves like a stream cipher. Let's go all-in and make it a real block cipher.

Continue reading: Playing around the block...