Lec 09 - Random Number
Lec 09 - Random Number
Random Number
Random Number
Randomness
Random Number Generator (RNG)
HRNG
PRNG
Entropy
Seed (Initial Entropy)
Insecure Randomness
Random Number in Python (PRNG)
CSPRNG
Random Number in Python (CSPRNG)
1
Randomness
Randomness (entropy) plays an important role in the
cryptography.
Random number should be unpredictable to prevent the
algorithms to be compromised.
Example:
Assume a secret key is used to protect the financial assets.
This secret key should be randomly generated – nobody able to
generate or have the same key.
If the key is generated from a secure random generator, the it will be
unpredictable and the system will be secure.
Therefore "secure random" means simply "unpredictable random".
2
HRNG
Hardware random-number generator (HRNG)
It is believed to produce genuine random numbers.
It is a device that generates random numbers from a physical
process, rather than by means of an algorithm.
It may generate values based on real random physical effects
or attributes such as atmospheric, thermal conditions.
Example of HRNG:
flip a coin or toss a dice
lottery machine
PRNG
Pseudo-random number generator (PRNG)
It generate values based on software algorithms.
However, these values are deterministic and can be
reproduced, if the algorithm is known.
Hence, secure PRNG is used – Cryptography Secure Random
Number Generator(CSPRNG)
It combined entropy with PRNG and other techniques to make
the generated randomness unpredictable.
3
Entropy
Entropy is the measurement of uncertainty or disorder in
a system, and is usually measured in bits.
Good entropy comes from the surrounding environment
which is unpredictable and chaotic.
Entropy can be obtained from many hard-to-predict
events in the computer: keyboard clicks, mouse moves,
network activity, camera activity, microphone activity
and others, combined with the time at which they occur.
4
Insecure Randomness
Insecure compromised randomness can compromise
cryptography.
A story of the stolen Bitcoins due to broken random
generator in Android: https://goo.gl/PFE1kr.
Example:
The code generates a random number, but this number may be predictable.
The random library in Python (old versions) initializes the random generator seed
by the current time.
If someone know the current time when machine generating the random number,
he is able to predict the random seed and the random numbers generated.
Insecure Randomness
Another Example:
• The code prints two equal numbers,
both depending on the current time.
• It is obvious that the same time in the
initial seed causes the same
(predictable) pseudo-random
numbers to be generated in the
output
5
Random Number in Python (PRNG)
A random number generator is a code that generates a
sequence of random numbers based on some conditions
that cannot be predicted other than by random chance.
Random numbers are not truly random, rather they are
pseudo-random numbers.
The built-in Python random module implements pseudo-
random number generators for various distributions.
However, this module is not cryptographically secure.
start − Start point of the range. This would be included in the range. Optional
stop − Stop point of the range. This would be excluded from the range.
step − Steps to be added in a number to decide a random number. Optional
random() Generates a random floating-point number in the interval [0,1]
seed(x) Generates the same sequence of random numbers every time seed(x) is invoked.
sample(population, k) Selects k unique random elements from a population sequence or set.
choice(seq) Chooses a random element from a non-empty sequence seq.
choices(seq, k) Chooses k multiple random elements from a non-empty sequence seq.
shuffle(x) Shuffles list x in place (reorganize the order of the list items).
6
Random Number in Python (PRNG)
The Seed:
The seed value is a base value to generate a random number.
In Python, the seed value is provided with the random.seed()
function.
seed() is deterministic, meaning given the same seed, it will
produce the same sequence of numbers every time.
If seed value is not present:
the current system time is used as a seed value
the OS’s randomness sources is used to set the seed value
7
Random Number in Python (PRNG)
Random number with same seed
The seed value based on system time produces the different pseudo-random values.
8
Random Number in Python (PRNG)
Get a seed value by a random generator
• Random generator of Python does not store the custom seed value in memory.
i.e., It does not provide any method to get the current seed value.
• This method is time-based, it produces a different seed each time it is executed.
• The seed can be kept to produce the same result.
9
Random Number in Python (PRNG)
random.choice(), random.sample() and random.shuffle()
CSPRNG
Cryptography Secure Random Number Generator
(CSPRNG) is pseudo-random number generator (PRNG)
with properties that make them suitable for use in
cryptography.
10
Random Number in Python (CSPRNG)
Two library modules in Python provides entropy to
generate cryptographically secure random numbers
secrets module
uuid module
11
Random Number in Python (CSPRNG)
Secrets module
The random numbers generated by the random module are
pseudo-random numbers and are not cryptographically secure.
Warning on the random module official documentation page
12
Random Number in Python (CSPRNG)
Secrets module
(generate random numbers)
13
Random Number in Python (CSPRNG)
Secrets module (generate random numbers)
14
Random Number in Python (CSPRNG)
Secrets module (generate tokens)
Generating tokens
secrets.token_bytes(n) – it return a random byte string containing n number of bytes.
15
Random Number in Python (CSPRNG)
Built-in functions for secrets module
Function Description
16
Random Number in Python (CSPRNG)
UUID in Python
Python has built-in support to generate Version 1, 3, 4 and 5
UUIDs.
UUID1 generates UUID using a Host MAC address, sequence
number and the current time. This version uses the IEEE 802
MAC addresses.
UUID3 and UUID5 uses cryptographic hashing and application-
provided text strings to generate UUID. UUID 3 uses MD5
hashing, and UUID5 uses SHA-1 hashing.
UUID4 uses pseudo-random number generators to generate
UUID.
17
Random Number in Python (CSPRNG)
uuid4() function
18
Random Number in Python (CSPRNG)
uuid5() function
UUID 5 uses SHA-1 hashing
19