1 /* 2 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * @test 26 * @bug 4898468 6994008 27 * @summary basic test for RSA cipher 28 * @author Andreas Sterbenz 29 * @library .. 30 */ 31 32 import java.io.*; 33 import java.util.*; 34 35 import java.security.*; 36 37 import javax.crypto.*; 38 39 public class TestRSACipher extends PKCS11Test { 40 41 private static final String[] RSA_ALGOS = 42 { "RSA/ECB/PKCS1Padding", "RSA" }; 43 44 public void main(Provider p) throws Exception { 45 try { 46 Cipher.getInstance(RSA_ALGOS[0], p); 47 } catch (GeneralSecurityException e) { 48 System.out.println("Not supported by provider, skipping"); 49 return; 50 } 51 KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", p); 52 kpg.initialize(1024); 53 KeyPair kp = kpg.generateKeyPair(); 54 PublicKey publicKey = kp.getPublic(); 55 PrivateKey privateKey = kp.getPrivate(); 56 Random random = new Random(); 57 byte[] b, e, d; 58 b = new byte[16]; 59 random.nextBytes(b); 60 61 for (String rsaAlgo: RSA_ALGOS) { 62 Cipher c1 = Cipher.getInstance(rsaAlgo, p); 63 Cipher c2 = Cipher.getInstance(rsaAlgo, "SunJCE"); 64 65 c1.init(Cipher.ENCRYPT_MODE, publicKey); 66 e = c1.doFinal(b); 67 c1.init(Cipher.DECRYPT_MODE, privateKey); 68 d = c1.doFinal(e); 69 match(b, d); 70 c2.init(Cipher.DECRYPT_MODE, privateKey); 71 d = c2.doFinal(e); 72 match(b, d); 73 74 // invalid data 75 c1.init(Cipher.DECRYPT_MODE, publicKey); 76 try { 77 d = c1.doFinal(e); 78 throw new Exception("completed call"); 79 } catch (BadPaddingException ee) { 80 ee.printStackTrace(); 81 } 82 83 c1.init(Cipher.ENCRYPT_MODE, privateKey); 84 e = c1.doFinal(b); 85 c1.init(Cipher.DECRYPT_MODE, publicKey); 86 d = c1.doFinal(e); 87 match(b, d); 88 c2.init(Cipher.DECRYPT_MODE, publicKey); 89 d = c2.doFinal(e); 90 match(b, d); 91 92 // reinit tests 93 c1.init(Cipher.ENCRYPT_MODE, privateKey); 94 c1.init(Cipher.ENCRYPT_MODE, privateKey); 95 e = c1.doFinal(b); 96 e = c1.doFinal(b); 97 c1.update(b); 98 c1.update(b); 99 c1.init(Cipher.ENCRYPT_MODE, privateKey); 100 e = c1.doFinal(); 101 e = c1.doFinal(); 102 c1.update(b); 103 e = c1.doFinal(); 104 105 c1.update(new byte[256]); 106 try { 107 e = c1.doFinal(); 108 throw new Exception("completed call"); 109 } catch (IllegalBlockSizeException ee) { 110 System.out.println(ee); 111 } 112 } 113 } 114 115 private static void match(byte[] b1, byte[] b2) throws Exception { 116 if (Arrays.equals(b1, b2) == false) { 117 System.out.println(toString(b1)); 118 System.out.println(toString(b2)); 119 throw new Exception("mismatch"); 120 } 121 } 122 123 public static void main(String[] args) throws Exception { 124 main(new TestRSACipher()); 125 } 126 127 }