Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/RSA/TestOAEP.java
41161 views
/*1* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4894151 814629326* @summary encryption/decryption test for OAEP27* @author Andreas Sterbenz28* @key randomness29*/3031import java.util.*;3233import java.security.*;3435import javax.crypto.*;3637public class TestOAEP {3839private static Provider cp;4041private static PrivateKey privateKey;4243private static PublicKey publicKey;4445private static Random random = new Random();4647public static void main(String[] args) throws Exception {48long start = System.currentTimeMillis();49cp = Security.getProvider("SunJCE");50System.out.println("Testing provider " + cp.getName() + "...");51Provider kfp = Security.getProvider("SunRsaSign");52KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", kfp);53kpg.initialize(768);54KeyPair kp = kpg.generateKeyPair();55privateKey = kp.getPrivate();56publicKey = kp.getPublic();5758Cipher.getInstance("RSA/ECB/OAEPwithMD5andMGF1Padding");59Cipher.getInstance("RSA/ECB/OAEPwithSHA1andMGF1Padding");60Cipher.getInstance("RSA/ECB/OAEPwithSHA-1andMGF1Padding");61Cipher.getInstance("RSA/ECB/OAEPwithSHA-224andMGF1Padding");62Cipher.getInstance("RSA/ECB/OAEPwithSHA-256andMGF1Padding");63Cipher.getInstance("RSA/ECB/OAEPwithSHA-384andMGF1Padding");64Cipher.getInstance("RSA/ECB/OAEPwithSHA-512andMGF1Padding");65Cipher.getInstance("RSA/ECB/OAEPwithSHA-512/224andMGF1Padding");66Cipher.getInstance("RSA/ECB/OAEPwithSHA-512/256andMGF1Padding");6768// basic test using MD569testEncryptDecrypt("MD5", 0);70testEncryptDecrypt("MD5", 16);71testEncryptDecrypt("MD5", 62);72try {73testEncryptDecrypt("MD5", 63);74throw new Exception("Unexpectedly completed call");75} catch (IllegalBlockSizeException e) {76// ok77System.out.println(e);78}7980// basic test using SHA-181testEncryptDecrypt("SHA1", 0);82testEncryptDecrypt("SHA1", 16);83testEncryptDecrypt("SHA1", 54);84try {85testEncryptDecrypt("SHA1", 55);86throw new Exception("Unexpectedly completed call");87} catch (IllegalBlockSizeException e) {88// ok89System.out.println(e);90}91// tests alias works92testEncryptDecrypt("SHA-1", 16);9394String[] HASH_ALG_224 = { "SHA-224", "SHA-512/224" };95for (String ha : HASH_ALG_224) {96testEncryptDecrypt(ha, 0);97testEncryptDecrypt(ha, 16);98testEncryptDecrypt(ha, 38);99try {100testEncryptDecrypt(ha, 39);101throw new Exception("Unexpectedly completed call");102} catch (IllegalBlockSizeException e) {103// ok104System.out.println(e);105}106}107108String[] HASH_ALG_256 = { "SHA-256", "SHA-512/256" };109for (String ha : HASH_ALG_256) {110testEncryptDecrypt(ha, 0);111testEncryptDecrypt(ha, 16);112testEncryptDecrypt(ha, 30);113try {114testEncryptDecrypt(ha, 31);115throw new Exception("Unexpectedly completed call");116} catch (IllegalBlockSizeException e) {117// ok118System.out.println(e);119}120}121122// 768 bit key too short for OAEP with 64 byte digest123try {124testEncryptDecrypt("SHA-512", 1);125throw new Exception("Unexpectedly completed call");126} catch (InvalidKeyException e) {127// ok128System.out.println(e);129}130131Cipher c;132byte[] enc;133byte[] data = new byte[16];134random.nextBytes(data);135136try {137c = Cipher.getInstance("RSA/ECB/OAEPwithFOOandMGF1Padding", cp);138throw new Exception("Unexpectedly completed call");139} catch (NoSuchPaddingException e) {140// ok141System.out.println(e);142}143144c = Cipher.getInstance("RSA/ECB/OAEPwithMD5andMGF1Padding", cp);145// cannot "sign" using OAEP146try {147c.init(Cipher.ENCRYPT_MODE, privateKey);148throw new Exception("Unexpectedly completed call");149} catch (InvalidKeyException e) {150// ok151System.out.println(e);152}153154// cannot "verify" using OAEP155try {156c.init(Cipher.DECRYPT_MODE, publicKey);157throw new Exception("Unexpectedly completed call");158} catch (InvalidKeyException e) {159// ok160System.out.println(e);161}162163// decryption failure164c.init(Cipher.DECRYPT_MODE, privateKey);165try {166c.doFinal(data);167throw new Exception("Unexpectedly completed call");168} catch (BadPaddingException e) {169// ok170System.out.println(e);171}172173// wrong hash length174c.init(Cipher.ENCRYPT_MODE, publicKey);175enc = c.doFinal(data);176c = Cipher.getInstance("RSA/ECB/OAEPwithSHA1andMGF1Padding", cp);177c.init(Cipher.DECRYPT_MODE, privateKey);178try {179c.doFinal(enc);180throw new Exception("Unexpectedly completed call");181} catch (BadPaddingException e) {182// ok183System.out.println(e);184}185186/* MD2 not supported with OAEP187// wrong hash value188c = Cipher.getInstance("RSA/ECB/OAEPwithMD2andMGF1Padding", cp);189c.init(Cipher.DECRYPT_MODE, privateKey);190try {191c.doFinal(enc);192throw new Exception("Unexpectedly completed call");193} catch (BadPaddingException e) {194// ok195System.out.println(e);196}197*/198199// wrong padding type200c = Cipher.getInstance("RSA/ECB/PKCS1Padding", cp);201c.init(Cipher.ENCRYPT_MODE, publicKey);202enc = c.doFinal(data);203c = Cipher.getInstance("RSA/ECB/OAEPwithSHA1andMGF1Padding", cp);204c.init(Cipher.DECRYPT_MODE, privateKey);205try {206c.doFinal(enc);207throw new Exception("Unexpectedly completed call");208} catch (BadPaddingException e) {209// ok210System.out.println(e);211}212213long stop = System.currentTimeMillis();214System.out.println("Done (" + (stop - start) + " ms).");215}216217// NOTE: OAEP can process up to (modLen - 2*digestLen - 2) bytes of data218private static void testEncryptDecrypt(String hashAlg, int dataLength) throws Exception {219System.out.println("Testing OAEP with hash " + hashAlg + ", " + dataLength + " bytes");220Cipher c = Cipher.getInstance("RSA/ECB/OAEPwith" + hashAlg + "andMGF1Padding", cp);221c.init(Cipher.ENCRYPT_MODE, publicKey);222byte[] data = new byte[dataLength];223byte[] enc = c.doFinal(data);224c.init(Cipher.DECRYPT_MODE, privateKey);225byte[] dec = c.doFinal(enc);226if (Arrays.equals(data, dec) == false) {227throw new Exception("Data does not match");228}229}230}231232233