An Analysis of Primality Testing and Its Use in Cryptographic Applications
Total Page:16
File Type:pdf, Size:1020Kb
An Analysis of Primality Testing and Its Use in Cryptographic Applications Jake Massimo Thesis submitted to the University of London for the degree of Doctor of Philosophy Information Security Group Department of Information Security Royal Holloway, University of London 2020 Declaration These doctoral studies were conducted under the supervision of Prof. Kenneth G. Paterson. The work presented in this thesis is the result of original research carried out by myself, in collaboration with others, whilst enrolled in the Department of Mathe- matics as a candidate for the degree of Doctor of Philosophy. This work has not been submitted for any other degree or award in any other university or educational establishment. Jake Massimo April, 2020 2 Abstract Due to their fundamental utility within cryptography, prime numbers must be easy to both recognise and generate. For this, we depend upon primality testing. Both used as a tool to validate prime parameters, or as part of the algorithm used to generate random prime numbers, primality tests are found near universally within a cryptographer's tool-kit. In this thesis, we study in depth primality tests and their use in cryptographic applications. We first provide a systematic analysis of the implementation landscape of primality testing within cryptographic libraries and mathematical software. We then demon- strate how these tests perform under adversarial conditions, where the numbers being tested are not generated randomly, but instead by a possibly malicious party. We show that many of the libraries studied provide primality tests that are not pre- pared for testing on adversarial input, and therefore can declare composite numbers as being prime with a high probability. We also demonstrate that for a number of libraries, including Apple's CommonCrypto, we are able to construct composites that always pass the supplied primality tests. We then explore the implications of these security failures in applications, focus- ing on the construction of malicious Diffie-Hellman parameters. These malicious parameter sets target the public key parameter validation functions found in these same cryptographic libraries { particularly within the ones that offer TLS imple- mentations. Using the analysis performed on these library's primality tests, we are able to construct malicious parameter sets for both finite-field and elliptic curve Diffie-Hellman that pass validation testing with some probability, but are designed such that the Discrete Logarithm Problem (DLP) is relatively easy to solve. We give an application of these malicious parameter sets to OpenSSL and password authenticated key exchange (PAKE) protocols. Finally, we address the shortcomings uncovered in primality testing under adver- sarial conditions by the introduction of a performant primality test that provides strong security guarantees across all use cases, while providing the simplest possible API. We examine different options for the core of our test, describing four different candidate primality tests and analysing them theoretically and experimentally. We then evaluate the performance of the chosen test in the use case of prime generation and discuss how our proposed test was fully adopted by the developers of OpenSSL through a new API and primality test scheduled for release in OpenSSL 3.0 (2020). 3 Acknowledgements I would like to first thank my supervisor Kenny Paterson for his patience, motivation, and guidance throughout the course of my Ph.D. Kenny has provided me so much insight and development into all aspects of my academic work, and I thank him for always making time for this. I would also like to thank Martin Albrecht for his support as a mentor and collaborator, as well as Liz Quaglia. I would like to thank Juraj Somorovsky and Steven Galbraith for giving me the opportunity to work with them as collaborators and to learn from their expertise. It was inspirational to see the level at which they work. I am grateful to the Engineering and Physical Sciences Research Council (EPSRC) for funding my work as part of the Centre for Doctoral Training (CDT) at Royal Holloway, University of London. I also give thanks to my fellow CDT students, and in particular Joanne Woodage, for truly being a role model and a friend to me { I learned from you how to deal with all the things that can't be learned from supervision and was constantly motivated by your outstanding level of work. I would like also to thank the CDT leadership and support staff at Royal Holloway, particularly Carlos Cid and Claire Hudson. I would also like to thank Matthew Campagna and Shay Gueron for hosting me at Amazon Web Services as an intern. Your team gave me an incredible opportunity to learn and work with so many other great researchers and engineers. Lastly, I give thanks to my Mum, Dad, Sister, and Anna for your love, support, and for watching my presentations even when you don't understand them. 4 Publications This thesis is based on the following three publications: 1. Martin R. Albrecht, Jake Massimo, Kenneth G. Paterson, and Juraj So- morovsky. Prime and Prejudice: Primality Testing Under Adversarial Condi- tions. In Proceedings of the 2018 ACM SIGSAC Conference on Computer and Communications Security, CCS 2018, Toronto, Canada, October 15-19, 2018, pp. 281-298, ACM. [5] This work originated with discussions between Martin and Kenny. I then joined the project as work began to flesh out these ideas. The main theoretical parts of the work arose from discussions between Martin, Kenny and me. The practical analysis of cryptographic libraries and mathematical software was in the main performed by me (and supported by all other authors), with the exception of the Bouncy Castle and Botan libraries, which were jointly worked on with Juraj Somorovsky. 2. Steven Galbraith, Jake Massimo, and Kenneth G. Paterson. Safety in Num- bers: On the Need for Robust Diffie-Hellman Parameter Validation. In IACR International Workshop on Public Key Cryptography, PKC 2019, Beijing, China, April 14-17, 2019, pp. 379-407, Springer. [57] This work started as a continuation of the previous work between Kenny and me. All sections were therefore authored jointly between Kenny and me, with the exception of the theoretical work on the elliptic curve setting which was due to Steven. Steven then contributed to the authorship as a whole during the submission process. 3. Jake Massimo and Kenneth G. Paterson. A Performant, Misuse-Resistant API for Primality Testing. To appear in Proceedings of the 2020 ACM SIGSAC Conference on Computer and Communications Security, CCS 2020, Orlando, USA, November 9-13, 2020, ACM. [100] This work was contributed to equally by Kenny and me. 5 Contents 1 Introduction 9 1.1 Motivation . .9 1.2 Contributions . 14 1.2.1 Thesis Structure . 14 1.2.2 OpenSSL Timeline . 16 2 Background and Preliminaries 19 2.1 Mathematical Background . 19 2.1.1 The Prime Number Theorem . 19 ∗ 2.1.2 The Group Zn .......................... 20 2.1.3 Cyclic Groups and Primitive Roots . 20 2.1.4 Lagrange's Theorem . 21 2.1.5 Finding Primitive Elements and Elements of Order q ..... 22 2.2 The Diffie-Hellman and Discrete Log Problems . 23 2.2.1 The Discrete Log Problem . 23 2.2.2 Prime Parameters and Safe Primes . 24 2.2.3 Security Level and Generic Attacks . 25 2.2.4 Pollard Rho . 25 2.2.5 Pohlig-Hellman . 26 2.2.6 Finite Field Diffie-Hellman Key Exchange . 27 2.3 Elliptic Curve Cryptography . 28 2.3.1 Elliptic Curves . 29 2.3.2 The Elliptic Curve Discrete Log Problem . 30 2.3.3 Elliptic Curve Diffie-Hellman . 30 2.4 Primality Testing . 31 2.4.1 Fermat Test . 31 2.4.2 Miller-Rabin Test . 32 2.4.3 Lucas Test . 33 2.4.4 Baillie-PSW . 35 2.4.5 Supplementary and Preliminary Tests . 36 2.4.6 Standards and Technical Guidelines . 37 2.5 Prime Generation . 38 2.5.1 Bias in Prime Generation . 39 3 Prime and Prejudice: Primality Testing Under Adversarial Condi- tions 41 3.1 Introduction and Motivation . 42 3.1.1 Overview of Primality Testing . 44 3.1.2 Contributions & Outline . 45 3.2 Constructing Pseudoprimes . 47 3.2.1 Miller-Rabin Pseudoprimes . 47 3.2.2 Lucas Pseudoprimes . 54 6 CONTENTS 3.3 Cryptographic Libraries . 57 3.3.1 OpenSSL . 57 3.3.2 GNU GMP . 60 3.3.3 Mini-GMP . 65 3.3.4 NSS . 67 3.3.5 Apple corecrypto and CommonCrypto . 69 3.3.6 Cryptlib . 70 3.3.7 JavaScript Big Number (JSBN) . 71 3.3.8 LibTomMath . 73 3.3.9 LibTomCrypt . 74 3.3.10 WolfSSL . 74 3.3.11 Libgcrypt . 75 3.3.12 Java . 76 3.3.13 Bouncy Castle . 78 3.3.14 Botan . 79 3.3.15 Crypto++ . 79 3.3.16 Golang . 80 3.3.17 Example Pseudoprimes for Apple corecrypto and Common- Crypto, LibTomMath, LibTomCrypt and WolfSSL . 81 3.4 Mathematical Software . 83 3.4.1 Magma . 84 3.4.2 Maple . 84 3.4.3 MATLAB . 86 3.4.4 Maxima . 86 3.4.5 SageMath . 87 3.4.6 SymPy . 88 3.4.7 Wolfram Mathematica . 90 3.5 Application to Diffie-Hellman . 90 3.6 Disclosure and Mitigations . 94 3.7 Conclusion and Recommendations . 96 4 Great Exponentiations: On the Need for Robust Diffie-Hellman Parameter Validation 98 4.1 Introduction and Motivation . 99 4.1.1 Contributions & Outline . 101 4.1.2 Related Work . 104 4.2 Miller-Rabin Primality Testing and Pseudoprimes . 105 4.2.1 On the Relationship Between S(n) and m, the Number of Prime Factors of n ........................ 107 4.3 Generating Large Carmichael Numbers . 110 4.3.1 The Erd¨osMethod . 110 4.3.2 The Selection of L in the Erd¨osMethod .