1*3d322d52SAndroid Build Coastguard Worker# RSA 2*3d322d52SAndroid Build Coastguard Worker 3*3d322d52SAndroid Build Coastguard Worker[TOC] 4*3d322d52SAndroid Build Coastguard Worker 5*3d322d52SAndroid Build Coastguard Worker## RSA key generation 6*3d322d52SAndroid Build Coastguard Worker 7*3d322d52SAndroid Build Coastguard Worker**Default size:** If a library supports a key default size for RSA keys then 8*3d322d52SAndroid Build Coastguard Workerthis key size should be at least 2048 bits. This limit is based on the minimum 9*3d322d52SAndroid Build Coastguard Workerrecommendation of [NIST SP 800-57] part1 revision 4, Table 2, page 53. NIST 10*3d322d52SAndroid Build Coastguard Workerrecommends a minimal security strength of 112 bits for keys used until 2030. 112 11*3d322d52SAndroid Build Coastguard Workerbit security strength translates to a minimal key size of 2048 bits. Other 12*3d322d52SAndroid Build Coastguard Workerorganizations recommend somewhat different sizes: [Enisa], Section 3.6 also 13*3d322d52SAndroid Build Coastguard Workersuggests that 2048-bit RSA keys provide a security strength of about 112 bits, 14*3d322d52SAndroid Build Coastguard Workerbut recommends a security strength of 128 bits for near term systems, hence 3072 15*3d322d52SAndroid Build Coastguard Workerbit RSA keys. [ECRYPT II], Section 13.3 suggests at least 2432 bits for new 16*3d322d52SAndroid Build Coastguard Workerkeys. 17*3d322d52SAndroid Build Coastguard Worker 18*3d322d52SAndroid Build Coastguard WorkerAll the references above clearly state that keys smaller than 2048 bits should 19*3d322d52SAndroid Build Coastguard Workeronly be used in legacy cases. Therefore, it seems wrong to use a default key 20*3d322d52SAndroid Build Coastguard Workersize smaller than 2048 bits. If a user really wants a small RSA key then such a 21*3d322d52SAndroid Build Coastguard Workerchoice should be made by explicitly providing the desired key length during the 22*3d322d52SAndroid Build Coastguard Workerinitalization of a key pair generator. 23*3d322d52SAndroid Build Coastguard Worker 24*3d322d52SAndroid Build Coastguard WorkerAccording to https://docs.oracle.com/javase/7/docs/api/javax/crypto/Cipher.html 25*3d322d52SAndroid Build Coastguard Workerevery implementation of the Java platform is required to implement RSA with both 26*3d322d52SAndroid Build Coastguard Worker1024 and 2048 bit key sizes. Hence a 2048 bit default should not lead to 27*3d322d52SAndroid Build Coastguard Workercompatibility problems. 28*3d322d52SAndroid Build Coastguard Worker 29*3d322d52SAndroid Build Coastguard Worker**Cryptographically strong random numbers:** 30*3d322d52SAndroid Build Coastguard WorkerSo far the tests check that java.util.Random is not used. This needs to be 31*3d322d52SAndroid Build Coastguard Workerextended. 32*3d322d52SAndroid Build Coastguard Worker 33*3d322d52SAndroid Build Coastguard Worker**Other bugs:** 34*3d322d52SAndroid Build Coastguard WorkerThe public exponent e should be larger than 1 [CVE-1999-1444] 35*3d322d52SAndroid Build Coastguard Worker 36*3d322d52SAndroid Build Coastguard Worker## RSA PKCS #1 v1.5 encryption 37*3d322d52SAndroid Build Coastguard Worker 38*3d322d52SAndroid Build Coastguard WorkerPKCS #1 v1.5 padding is susceptible to adaptive chosen ciphertext attacks and 39*3d322d52SAndroid Build Coastguard Workerhence should be avoided [B98]. The difficulty of exploiting protocols using 40*3d322d52SAndroid Build Coastguard WorkerPKCS #1 v1.5 encryption often depends on the amount of information leaked after 41*3d322d52SAndroid Build Coastguard Workerdecrypting corrupt ciphertexts. Implementations frequently leak information 42*3d322d52SAndroid Build Coastguard Workerabout the decrypted plaintext in form of error messages. The content of the 43*3d322d52SAndroid Build Coastguard Workererror messages are extremely helpful to potential attackers. Bardou et al. 44*3d322d52SAndroid Build Coastguard Worker[BFKLSST12] analyze the difficult of attacks based on different types of 45*3d322d52SAndroid Build Coastguard Workerinformation leakage. Smart even describes an attack that only needs about 40 46*3d322d52SAndroid Build Coastguard Workerchosen ciphertexts [S10], though in this case the encryption did not use PKCS #1 47*3d322d52SAndroid Build Coastguard Workerpadding. 48*3d322d52SAndroid Build Coastguard Worker 49*3d322d52SAndroid Build Coastguard Worker**Bugs** 50*3d322d52SAndroid Build Coastguard Worker 51*3d322d52SAndroid Build Coastguard Worker* Bouncycastle throws detailed exceptions: 52*3d322d52SAndroid Build Coastguard Worker InvalidCipherTextException("unknown block type") or 53*3d322d52SAndroid Build Coastguard Worker InvalidCipherTextException("block padding incorrect"). 54*3d322d52SAndroid Build Coastguard Worker 55*3d322d52SAndroid Build Coastguard Worker<!-- the SUN provider used to include that block type --> 56*3d322d52SAndroid Build Coastguard Worker 57*3d322d52SAndroid Build Coastguard Worker**Tests** To test whether an implementation leaks more information than 58*3d322d52SAndroid Build Coastguard Workernecessary a test decrypts some random ciphertexts and catches the exceptions. If 59*3d322d52SAndroid Build Coastguard Workerthe exceptions are distinguishable then the test assumes that unnecessary 60*3d322d52SAndroid Build Coastguard Workerinformation about the padding is leaked. 61*3d322d52SAndroid Build Coastguard Worker 62*3d322d52SAndroid Build Coastguard WorkerDue to the nature of unit tests not every attack can be detected this way. Some 63*3d322d52SAndroid Build Coastguard Workerattacks require a large number of ciphertexts to be detected if random 64*3d322d52SAndroid Build Coastguard Workerciphertexts are used. For example Klima et al. [KPR03] describe an 65*3d322d52SAndroid Build Coastguard Workerimplementation flaw that could not be detected with our test. 66*3d322d52SAndroid Build Coastguard Worker 67*3d322d52SAndroid Build Coastguard WorkerTiming leakages because of differences in parsing the padding can leak 68*3d322d52SAndroid Build Coastguard Workerinformation (e.g. CVE-2015-7827). Such differences are too small to be reliably 69*3d322d52SAndroid Build Coastguard Workerdetectable in unit tests. 70*3d322d52SAndroid Build Coastguard Worker 71*3d322d52SAndroid Build Coastguard Worker## RSA OAEP 72*3d322d52SAndroid Build Coastguard Worker 73*3d322d52SAndroid Build Coastguard WorkerManger describes an chosen ciphertext attack against RSA in [M01]. There are 74*3d322d52SAndroid Build Coastguard Workerimplementations that were susceptible to Mangers attack, e.g. [CVE-2012-5081]. 75*3d322d52SAndroid Build Coastguard Worker 76*3d322d52SAndroid Build Coastguard Worker## RSA PKCS1 signatures 77*3d322d52SAndroid Build Coastguard Worker**Potential problems:** 78*3d322d52SAndroid Build Coastguard Worker 79*3d322d52SAndroid Build Coastguard Worker* Some libraries parse PKCS#1 padding during signature verification 80*3d322d52SAndroid Build Coastguard Worker incorrectly. 81*3d322d52SAndroid Build Coastguard Worker* Some libraries determine the hash function from the signature (rather than 82*3d322d52SAndroid Build Coastguard Worker encoding this in the key) Effect: 83*3d322d52SAndroid Build Coastguard Worker* If the verification is buggy then an attacker might be able to generate 84*3d322d52SAndroid Build Coastguard Worker signatures for keys with a small (i.e. e=3) public exponent. 85*3d322d52SAndroid Build Coastguard Worker* If the hash algorithm is not determined by in an authentic manner then 86*3d322d52SAndroid Build Coastguard Worker preimage attacks against weak hashes are possible, even if the hashes are 87*3d322d52SAndroid Build Coastguard Worker not used by the signer. 88*3d322d52SAndroid Build Coastguard Worker 89*3d322d52SAndroid Build Coastguard Worker**Countermeasures:** A good way to implement RSA signature verification is 90*3d322d52SAndroid Build Coastguard Workerdescribed in the standard PKCS#1 v.2.2 Section 8.2.2. This standard proposes to 91*3d322d52SAndroid Build Coastguard Workerreconstruct the padding during verification and compare the padded hash to the 92*3d322d52SAndroid Build Coastguard Workervalue $$s^e \bmod n$$ obtained from applying a public key exponentiation to the 93*3d322d52SAndroid Build Coastguard Workersignature s. Since this is a recurring bug it makes also a lot of sense to avoid 94*3d322d52SAndroid Build Coastguard Workersmall public exponents and prefer for example e=65537 . 95*3d322d52SAndroid Build Coastguard Worker 96*3d322d52SAndroid Build Coastguard Worker**List of broken implementations** 97*3d322d52SAndroid Build Coastguard WorkerThis is a large list. 98*3d322d52SAndroid Build Coastguard Worker 99*3d322d52SAndroid Build Coastguard Worker## References 100*3d322d52SAndroid Build Coastguard Worker 101*3d322d52SAndroid Build Coastguard Worker\[B98]: D. Bleichenbacher, "Chosen ciphertext attacks against protocols based on 102*3d322d52SAndroid Build Coastguard Workerthe RSA encryption standard PKCS# 1" Crypto 98 103*3d322d52SAndroid Build Coastguard Worker 104*3d322d52SAndroid Build Coastguard Worker\[M01]: J. Manger, "A chosen ciphertext attack on RSA optimal asymmetric 105*3d322d52SAndroid Build Coastguard Workerencryption padding (OAEP) as standardized in PKCS# 1 v2.0", Crypto 2001 This 106*3d322d52SAndroid Build Coastguard Workerpaper shows that OAEP is susceptible to a chosen ciphertext attack if error 107*3d322d52SAndroid Build Coastguard Workermessages distinguish between different failure condidtions. [S10]: N. Smart, 108*3d322d52SAndroid Build Coastguard Worker"Errors matter: Breaking RSA-based PIN encryption with thirty ciphertext 109*3d322d52SAndroid Build Coastguard Workervalidity queries" RSA conference, 2010 This paper shows that padding oracle 110*3d322d52SAndroid Build Coastguard Workerattacks can be successful with even a small number of queries. 111*3d322d52SAndroid Build Coastguard Worker 112*3d322d52SAndroid Build Coastguard Worker\[KPR03]: V. Klima, O. Pokorny, and T. Rosa, "Attacking RSA-based Sessions in 113*3d322d52SAndroid Build Coastguard WorkerSSL/TLS" https://eprint.iacr.org/2003/052/ 114*3d322d52SAndroid Build Coastguard Worker 115*3d322d52SAndroid Build Coastguard Worker\[BFKLSST12]: "Efficient padding oracle attacks on cryptographic hardware" R. 116*3d322d52SAndroid Build Coastguard WorkerBardou, R. Focardi, Y. Kawamoto, L. Simionato, G. Steel, J.K. Tsay, Crypto 2012 117*3d322d52SAndroid Build Coastguard Worker 118*3d322d52SAndroid Build Coastguard Worker\[NIST SP 800-57]: 119*3d322d52SAndroid Build Coastguard Workerhttp://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-57pt1r4.pdf 120*3d322d52SAndroid Build Coastguard Worker 121*3d322d52SAndroid Build Coastguard Worker\[Enisa]: "Algorithms, key size and parameters report – 2014" 122*3d322d52SAndroid Build Coastguard Workerhttps://www.enisa.europa.eu/publications/algorithms-key-size-and-parameters-report-2014 123*3d322d52SAndroid Build Coastguard Worker 124*3d322d52SAndroid Build Coastguard Worker\[ECRYPT II]: Yearly Report on Algorithms and Keysizes (2011-2012), 125*3d322d52SAndroid Build Coastguard Workerhttp://www.ecrypt.eu.org/ecrypt2/documents/D.SPA.20.pdf 126*3d322d52SAndroid Build Coastguard Worker 127*3d322d52SAndroid Build Coastguard Worker\[CVE-1999-1444]: Alibaba 2.0 generated RSA key pairs with an exponent 1 128*3d322d52SAndroid Build Coastguard Worker 129*3d322d52SAndroid Build Coastguard Worker\[CVE-2012-5081]: Java JSSE provider leaked information through exceptions and 130*3d322d52SAndroid Build Coastguard Workertiming. Both the PKCS #1 padding and the OAEP padding were broken: 131*3d322d52SAndroid Build Coastguard Workerhttp://www-brs.ub.ruhr-uni-bochum.de/netahtml/HSS/Diss/MeyerChristopher/diss.pdf 132