Deep Learning in Stream Cipher Cryptanalysis
Deep Learning in Stream Cipher Cryptanalysis
16209
© 2021, DESIDOC
Abstract
Conventional cryptanalysis techniques necessitate an extensive analysis of non-linear functions defining the
relationship of plain data, key, and corresponding cipher data. These functions have very high degree terms and make
cryptanalysis work extremely difficult. The advent of deep learning algorithms along with the better and efficient
computing resources has brought new opportunities to analyze cipher data in its raw form. The basic principle of
designing a cipher is to introduce randomness into it, which means the absence of any patterns in cipher data. Due
to this fact, the analysis of cipher data in its raw form becomes essential. Deep learning algorithms are different from
conventional machine learning algorithms as the former directly work on raw data without any formal requirement
of feature selection or feature extraction steps. With these facts and the assumption of the suitability of employing
deep learning algorithms for cipher data, authors introduced a deep learning based method for finding biases in
stream ciphers in the black-box analysis model. The proposed method has the objective to predict the occurrence of
an output bit/byte at a specific location in the stream cipher generated keystream. The authors validate their method
on stream cipher RC4 and its improved variant RC4A and discuss the results in detail. Further, the authors apply
the method on two more stream ciphers namely Trivium and TRIAD. The proposed method can find bias in RC4
and shows the absence of this bias in its improved variant and other two ciphers. Focusing on RC4, the authors
present a comparative analysis with some existing methods in terms of approach and observations and showed that
their process is more straightforward and less complicated than the existing ones.
Keywords: Stream cipher; Cryptanalysis; RC4; Deep learning
499
Def. SCI. J., Vol. 71, No. 4, July 2021
500
Mishra, et al.: Deep Learning based Cryptanalysis of Stream Ciphers
layers progressively process the input data further by using an biased at that specific byte position. The existence of this bias
activation function. After that, the output layer generally uses makes the stream cipher vulnerable to cryptanalytic attacks.
Softmax function for final classification. The Softmax function RC4 is a stream cipher that iteratively generates the
is given below: keystream in a byte-wise form. The generated keystream
was claimed to possess pseudo-random characteristics. The
e xi
F(Xi ) = k
where i = 0,1, 2,..., k exclusive-or (XOR) of keystream bits with the plaintext bits
∑e xi
is done for encryption. The decryption is performed on similar
i =0 lines by bit-wise XOR of ciphertext bits with the keystream bits.
Here k denotes the number of possible outputs. Stacking RC4 algorithm is composed of two phases: Key Scheduling
many intermediate layers between input and output layers Algorithm (KSA) and Pseudo Random Generation Algorithm
allows networks to learn an abstract representation of the (PRGA). The cipher description is given in Algorithm 1, and a
mapping from input data to the corresponding target output. simple layout of RC4 stream cipher is shown in Figure 3.
Some popular deep learning techniques are Convolution
Neural Networks (CNN), Recurrent Neural Networks (RNN), A lg orithm1( RC 4 Stream Cipher )
and Long Short Term Memory networks (LSTM)18. −−−−−−−−−−−−−−−−−−−
Key Scheduling Algorithm
2.2 Brief Description of Stream Ciphers
Symmetric key cryptography comprises of Block ciphers 1. procedure KSA( )
and Stream ciphers. In stream ciphers, the keystream of the 2. for i = 0 to N − 1 do
desired length is generated using an initial key (invariably 3. s[i ] ← i
known as a secret key). The generated keystream is then
4. end for
masked (generally, using exclusive-or) with the plaintext
bits to provide a ciphertext. In the early era of modern 5. j←0
cryptography, stream ciphers were widely used due to their 6. for i = 0 to N − 1 do
simplicity and speed than that of block ciphers. Stream ciphers 7. j ← j + s[i ] + key[i mod keylength]mod N
have been the preferred choice in applications where extremely
8. swap ( s[i ] and s[ j ])
high throughput is needed or only low complexity hardware
is available for usage. Keeping in mind the fundamental 9. end for
requirement in cryptography, the stream cipher design should Pseudo Random Generation Algorithm
ensure the desired cryptographic requirement of randomness 11. procedure PRGA()
in the output keystream. One of the criteria of fulfilling this is
12. while i ≥ 0 do
to ensure that the occurrence of any value in the range 0 to 255
at a byte position in stream cipher generated keystream should 13. i ← i +1
have equal probability. In other words, the occurrence of any 14. j ← j + s[i ]
value (in the given range) should have a probability 1/256. If 15. swap ( s[i ] and s[ j ])
the probability of the occurrence of any value at a byte position
16. output ( s[ s[i ] + s[ j ]])
differs from it significantly, the keystream is adjudged to be
17. end while
18. end procedure
501
Def. SCI. J., Vol. 71, No. 4, July 2021
a stream cipher can be made simple while maintaining the mentioned N pairs respectively
desired level of security and also ensuring the speed and 6. Pass the dataset to deep learning network by considering
flexibility. Since then, Trivium has been a well-tested stream Initial Key K as input and 1st byte of corresponding KS as
cipher for its security analysis and till date no cryptanalytic target
attacks superior to brute-force are reported. So, we select this 7. Train the network using training dataset to create a deep
cipher to see if our method can learn and predict any bias in learning model
Trivium generated keystream. On the other hand, TRIAD-SC 8. Validate the model on the test dataset and collect the
is relatively a new stream cipher based on which a family of result which shows the prediction probability of the model for
lightweight symmetric-key schemes are proposed under NIST 1st byte of KS
Lightweight Cipher competition23. No significant cryptanalytic 9. Repeat the process mentioned in steps from 6 to 8 for
work has been reported on TRIAD-SC as on date, therefore we experimenting with 2nd, 3rd, 4th, . . . bytes of KS
experiment over this stream cipher to explore any possibility 10. ith byte of KS is biased If Prediction Probability (for
of finding the biases in the keystream generated by this cipher. i byte of KS ) is significantly more than 1/256 (0.0039), else
th
authentication encryption mode (TRAID-AE) and a hash takes a variable-length Initial Key as input (length typically
function (TRIAD-HASH). TRIAD-AE is basically an being in the range between 5 and 256 bytes) and iteratively
encryption-then-mac construction, in which a stream cipher generates a pseudorandom Keystream in byte-wise form as
TRIAD-SC is meant for performing encryption module. TRIAD output. In our experimental setup, initially, we fixed the input
was submitted to the lightweight crypto standardisation process key size to 5 bytes and generated 4,00,000 input-output pairs.
of NIST23. TRIAD-SC takes 128-bit secret key and a 96-bit The size of State Vector/ Initial State, which gets initialised by
nonce as input along with one constant value (0xFFFFFFFE) the initial key and generates keystream by its regular updation,
and generates the keystream of desired length for encrypting is taken to be 256 for our experiments. It is re-emphasised
the plaintext. We are not providing any further details about the that the variable Initial Key is the input, and the jth byte of the
algorithm and the same can be found in21. Keystream denotes the target value in our experimental set-up.
In other words, for one training/test pair, the Initial Key is the
3. Proposed Methodology for input, and jth byte of correspondingly generated Keystream is
Cryptanalysis of Stream Cipher the target value.
In this section, we propose a method that is primarily based Initially, deep learning based prediction model was set-up
on deep learning. We adopt this method for the cryptanalysis of for predicting the first output byte of RC4 keystream. For each
stream ciphers with the target of finding biases in the generated sample in our supervised deep learning model, five bytes of
keystream. The detailed stepwise algorithm is as follows: initial key are taken as five neurons in input layer and first byte
of the keystream is taken as target in output layer. For example,
0. Let the initial parameters be represented as: if initial key is 0xEB9F72AE1C (in Hex format) and generated
a) Initial Key: K keystream is 0xBBF316E8D940AF0AD3 . . . (in Hex format),
b) Initial State: S then 0xEB, 0x9F, 0x72, 0xAE and 0x1C are five neurons in
c) Keystream: KS input layer and first byte 0xBB is the target in output layer. In
(Considering the black-box scenario, the updation of S total, 4,00,000 such input-output pairs were taken for creating
is not known to our method) the deep learning model. This data set was divided in 80%
(3,20,000 in numbers) training and 20% (80,000 in numbers)
1. Let K = k1k2 ...kn*8 ; where n denotes the length (in test instances for validation of the method. Seven dense layers,
bytes) of the Initial Key. each with 10 neurons and ReLU activation function, followed
2. With K as input, run stream cipher algorithm to by an output layer with 256 neurons with Softmax activation
generate the keystream KS function have been taken in creating the model. The model is
3. Iterate Step-1 and Step-2, N times for N different compiled with adam optimizer and categorical_crossentropy
values of K to generate " K − KS " pairs, i.e., “InitialKey − as loss function3. Loss denotes the error between actual target
Keystream” pairs and output value predicted by DL network. Adam optimizer,
4. Create a dataset consisting of these N pairs an extension to stochastic gradient descent, has found broader
5. Divide dataset into two parts, namely Training dataset adoption in recent times after increasing popularity of deep
and Test dataset having 80% and the remaining 20% of above- learning techniques in diversified domains. Categorical
502
Mishra, et al.: Deep Learning based Cryptanalysis of Stream Ciphers
crossentropy is a loss function used in multi-classs classification The broad objective of the experiment is to create deep
tasks. Multi-class classification is related to the tasks where a learning model to predict the output bytes of RC4 keystream
given sample can belong only one of many possible categories. when the initial key is known to us. For validating the proposed
This actually quantifies the difference between two probability method, we construct ten different deep learning models, one
distributions. Ours is a supervised learning problem as the each for the first 10 bytes of the keystream. Table 1 shows the
model is first trained with the help of the samples for which results of our experiments. We used 4,00,000 samples (input-
the classes are known. The process of backpropagation is used output pairs) for constructing each deep learning model for the
for fine tuning the weights for minimizing the loss. Training is prediction of output bytes as the prediction rates were stable
done in five epochs and after five epochs results are collected with this sample size. For this amount of samples, average
for training and validation data. Similar prediction models were execution time of the code/model over GPU runtime in Google
also created for predicting each of the first ten output bytes of Colab was observed to be 16 seconds. We also carried out
RC4 keystream. The deep learning arrangement employed in the experiments with fewer samples and observed that the
our work is shown in Figure 4. model was continually providing reasonable prediction rates
The similar arrangement of experiment has been used even with 30,000 samples. But below this number, the results
for cryptanalysis of RC4A stream cipher except the following (prediction rates) were not consistent.
parameter: Theoretically, if a well-designed stream cipher generates
length of Initial Key ( n ) = 10 a random keystream, then it may safely be assumed that the
The length of initial key in case of RC4A is taken large occurrence of a specific value at any output byte position in
in comparison to that for RC4 as the initial key for the former
Table 1. Prediction Probability for different output byte
stream cipher is a combination of two key components. The positions in RC4 Keystream for input secret keys of
rest of experimental set-up and the amount of data are exactly size 5 bytes
same in both cases.
For experimenting with two other stream ciphers, Trivium Byte Position Training Accuracy Testing Accuracy
and TRAID-SC, we generated 217 keystream samples for in RC4 (Prediction Probability (Prediction Probability
different respective inputs for each cipher. The objective was Keystream for Training Data) for Test Data)
to compute the prediction accuracy for each of the first 256 1 0.0040 0.0040
bits of the keystream. We also performed the experiments for
2 0.0079 0.0073
predicting the first 32 bytes of the keystream.
3 0.0040 0.0040
5. Experimental Results and 4 0.0041 0.0040
Comparative Analysis 5 0.0040 0.0039
The implementation of the code for the proposed method
has been done on Google Colab24. Colaboratory (Colab in 6 0.0040 0.0042
short) is a free cloud service provided by Google in Jupyter 7 0.0040 0.0039
notebook environment where availability of GPUs and TPUs 8 0.0039 0.0042
may be used for the development of codes for problem-solving.
9 0.0040 0.0040
It allows developing deep learning algorithms using popular
libraries such as PyTorch, TensorFlow, and Keras25. 10 0.0040 0.0040
503
Def. SCI. J., Vol. 71, No. 4, July 2021
the keystream could not be predicted with better than chance (i.e. RC4A). In case of stream ciphers Trivium and TRIAD,
probability (chance probability means the occurrence of the achieved accuracy was almost equal to chance probability.
each possible output should be equiprobable, like chances of As nothing significant was observed for two ciphers, we omit
occurrence of either head or tail in tossing a coin should be showing the results here.
with equal probability i.e., 0.5). Mantin and Shamir26 performed a comprehensive
Similarly, in our case, had RC4 stream cipher generated statistical analysis of RC4 keystream. Their result shows that
a random keystream, no method would have predicted the the probability of the second output keystream byte of RC4
occurrence of a specific value at any output byte position being zero is approximately double than expected if the initial
of keystream with significantly better than 1/256 = 0.0039 permutation is randomly chosen. Paul and Praneel19, after
probability (as a byte may take any value from 0 to 255). Our doing rigorous analysis, presented a new statistical bias in the
experimentation results show that the deep learning model has distribution of the first two output bytes of RC4 keystream
been able to predict the second output byte with a significantly generator. Pudovkina27 analytically attempted to detect a bias
good probability (i.e., 1/128 = 0.0073). In contrast, it could in the distribution of first and second output values of RC4
not learn to predict the occurrence of other output bytes with keystream considering certain uniformity assumptions. The
better than chance probability. For other output bytes of methods used by these researchers and us along with respective
RC4 keystream, the prediction probability is approximately observations are shown in Table 4. Several other researchers
1/256=0.0039. In other words, our model could learn the through statistical and mathematical means and experiments
process of mapping the input initial key to the second byte of tried to find biases in RC4 and other stream ciphers by making
the keystream with a biased probability. This finding exposes
Table 3. Prediction Probability for different output byte
the presence of bias at second byte position in RC4 keystream,
positions in RC4A Keystream for input secret keys
which can enable the adversaries in mounting a practical
of size 5 bytes
ciphertext-only attack in some applications. We carried out
similar experiments for four other different input initial key Byte Position Training Accuracy Testing Accuracy
sizes of 8, 16, 24, and 32 bytes. In all the experiments, we in RC4A (Prediction Probability (Prediction Probability
obtained similar findings that that the model could learn the Keystream for Training Data) for Test Data)
biased behavior at 2nd output byte of RC4 keystream with the 1 0.0040 0.0042
probability around 1/128. For other output bytes, the prediction 2 0.0044 0.0037
probability was approximately 1/256, which is the desired 3 0.0041 0.0046
randomness for output byte of RC4 keystream. The overall 4 0.0042 0.0040
results for our experiment with RC4 stream cipher are shown
5 0.0048 0.0033
in Table 2.
6 0.0049 0.0046
The experiments on similar lines were also carried out for
RC4A stream cipher. The experimental results for the same are 7 0.0041 0.0036
presented in Table 3. The results clearly validate the designers 8 0.0044 0.0044
claim that they have successfully eliminated the bias at second 9 0.0039 0.0037
output byte in their improved variant of RC4 stream cipher 10 0.0044 0.0041
Table 2. Prediction Probability for outputs bytes in RC4 Keystream for different input key sizes
Key Size Prediction Probability for Different Output Bytes of RC4 Keystream
in Bytes 1st Byte 2nd Byte 3rd Byte 4th Byte 5th Byte 6th Byte 7th Byte 8th Byte 9th Byte 10th Byte
5 0.0040 0.0079 0.0040 0.0041 0.0040 0.0040 0.0040 0.0039 0.0040 0.0040
8 0.0039 0.0079 0.0041 0.0037 0.0046 0.0038 0.0041 0.0035 0.0038 0.0042
16 0.0040 0.0077 0.0039 0.0042 0.0033 0.0042 0.0042 0.0038 0.0036 0.0042
24 0.0041 0.0075 0.0042 0.0041 0.0040 0.0042 0.0036 0.0044 0.0035 0.0041
32 0.0043 0.0075 0.0038 0.0038 0.0038 0.0038 0.0037 0.0039 0.0038 0.0036
Table 4. Comparative Analysis of different methods for finding weakness of RC4 in terms of approach and observations
Type Observation Source
The probability of second output word of RC4 being 0 is approximately double than the expected
Statistical analysis Ref.26
probability. Other output words have uniform distribution
The first two output words of RC4 are equal with probability that is significantly less than the
Statistical analysis Ref.19
expected probability
Exhaustive probabilistic model The distribution of first, second output values of RC4 and digraphs are not uniform Ref.27
The prediction probability of second output word of RC4 is almost double than the expected This
Deep learning method
probability. Other output words have prediction probability as expected in uniform distribution work
504
Mishra, et al.: Deep Learning based Cryptanalysis of Stream Ciphers
certain assumptions. Thus, the most of the findings have been doi: 10.1007/3-540-57332-1_36
based on manual calculation of the probability distribution in 5. Abadi, M. & Andersen, D.G. Learning to protect
keystream data, which required much in-depth analysis of the communications with adversarial neural cryptography.
cipher structure. On the other side, our proposed method does arXiv preprint arXiv:1610.06918, 2016, [Link]
not require prior assumptions and any complex mathematical abs/1610.06918 [Accessed on 31 July 2020].
or statistical analysis for finding the bias. It simply needs input- 6. Hesamifard, E.; Takabi, H. & Ghasemi, M. Cryptodl:
output pairs of the stream cipher algorithm and passes them Deep neural networks over encrypted data. arXiv preprint
to the deep learning framework. Therefore, it can evidently arXiv:1711.05189, 2017, [Link]
be concluded that the proposed deep learning based method [Accessed on 12 July 2020].
provides a straightforward approach for cryptanalysis of stream doi: 10.1145/3292006.3300044
ciphers in terms of finding the biases. 7. Picek, S.; Samiotis, I.P.; Kim, J.; Heuser, A.; Bhasin, S.
& Legay, A. On the performance of convolutional neural
6. Conclusion networks for side-channel analysis. In Proceedings of
The foremost application of the proposed work is to the International Conference on Security, Privacy, and
perceive distinguishability in different stream ciphers, which Applied Cryptography Engineering, Springer, Cham,
simply ensures mounting of distinguishing attack on stream 2018, pp. 157-176.
ciphers. In other words, the distinguishing attack means if 8. Wang, H. Side-channel analysis of aes based on deep
certain number of black boxes generate the pseudo-random learning. [Link]
keystreams from a known input key, then the black box behaving diva2:1325691/[Link], 2019 [Accessed on
as a specific stream cipher can be identified amongst all. Thus, 07 Aug 2020].
a deep learning based method has successfully been applied in 9. Gohr, A. Improving attacks on round-reduced speck32/64
finding the bias and further exploration of distinguishability in using deep learning. In Proceedings of Annual International
stream ciphers. The proposed method has been validated on Cryptology Conference, Springer, 2019, pp. 150–179.
RC4 stream cipher to ascertain its simplicity in comparison doi: 10.1007/978-3-030-26951-7_6
to earlier sophisticated mathematical and statistical methods. 10. Beaulieu, R.; Shors, D.; Smith, J.; Treatman-Clark,
The observations and analysis throughout the paper clearly S.; Weeks, B. & Wingers, L. The SIMON and SPECK
indicate that the emergence of new deep learning techniques Families of Lightweight Block Ciphers. IACR Cryptol.
will always go hand in hand with cryptographic analysis. ePrint Arch., 2013, p.404, [Link]
The prediction accuracy can be impacted by many factors, pdf [Accessed on 07 Aug 2020].
including the network design, the training data volume, and 11. Baksi, A.; Breier, J.; Dong, X. & Yi, C. Machine learning
the training time. We also carried out the experiments over assisted differential distinguishers for lightweight
three other stream ciphers RC4A, Trivium and TRIAD, but did ciphers. IACR Cryptol. ePrint Arch., 2020, p.571, https://
not observe anything significant. In future, more experiments [Link]/2020/[Link] [Accessed on 14 Oct 2020].
will be performed to gain better insights into the experimental 12. Jain, A.; Kohli, V. & Mishra, G. Deep learning based
setting. The aim will also be to analyze several other stream differential distinguisher for lightweight cipher PRESENT.
ciphers and explore better representation of cryptographic Cryptology ePrint Archive, Report 2020/846. https://
data which may become more relevant to deep learning [Link]/2020/846. [Accessed on 14 Oct 2020].
framework. 13. Yadav, T. & Kumar, M. Differential-ML distinguisher:
machine learning based generic extension for differential
References cryptanalysis, [Link]
1. Bahdanau, D.; Cho, K. & Bengio, Y. Neural machine [Accessed on 14 Oct 2020].
translation by jointly learning to align and translate. 14. Xiao, Y.; Hao, Q. & Yao, D. D. Neural cryptanalysis:
arXiv preprint arXiv:1409.0473, 2014, [Link] metrics, methodology, and applications in cps ciphers. In
abs/1409.0473 [Accessed on 01 Aug 2020]. Proceedings of the Conference on Dependable and Secure
2. Chen, C.; Seff, A.; Kornhauser, A. & Xiao, J. Deepdriving: Computing (DSC), IEEE, 2019, pp. 1-8.
Learning affordance for direct perception in autonomous doi: 10.1109/dsc47296.2019.8937659
driving. In Proceedings of the International Conference 15. Maghrebi, H.; Portigliatti, T. & Prouff, E. Breaking
on Computer Vision, IEEE, 2015, pp. 2722-2730. cryptographic implementations using deep learning
doi: 10.1109/iccv.2015.312 techniques. In Proceedings of the International Conference
3. Deng, L. & Yu, D. Deep learning: methods and on Security, Privacy, and Applied Cryptography
applications. Foundations and trends in signal processing, Engineering, Springer, Cham, 2016, pp. 3-26.
Now Publishers Inc. Hanover, MA, USA, 2014, 7(3–4), doi: 10.1007/978-3-319-49445-6_1
pp. 197-387. 16. Hochreiter, S. The vanishing gradient problem during
doi: 10.1561/9781601988157 learning recurrent neural nets and problem solutions. J. of
4. Rivest, R.L. Cryptography and machine learning. In Uncertain. Fuzziness Knowl.-based Systems, 6(02), 1998,
Proceedings of the International Conference on the pp. 107–116.
Theory and Application of Cryptology, Springer, Berlin, doi: 10.1142/s0218488598000094
Heidelberg, 1991, pp. 427-439. 17. Jarrett, K.; Kavukcuoglu, K.; Ranzato, M.A. & LeCun, Y.,
505
Def. SCI. J., Vol. 71, No. 4, July 2021
506