J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /** |
| 25 | * @test |
| 26 | * @bug 4898468 |
| 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 | public void main(Provider p) throws Exception { |
| 42 | try { |
| 43 | Cipher.getInstance("RSA/ECB/PKCS1Padding", p); |
| 44 | } catch (GeneralSecurityException e) { |
| 45 | System.out.println("Not supported by provider, skipping"); |
| 46 | return; |
| 47 | } |
| 48 | KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", p); |
| 49 | kpg.initialize(1024); |
| 50 | KeyPair kp = kpg.generateKeyPair(); |
| 51 | PublicKey publicKey = kp.getPublic(); |
| 52 | PrivateKey privateKey = kp.getPrivate(); |
| 53 | Random random = new Random(); |
| 54 | byte[] b, e, d; |
| 55 | b = new byte[16]; |
| 56 | random.nextBytes(b); |
| 57 | |
| 58 | Cipher c1 = Cipher.getInstance("RSA/ECB/PKCS1Padding", p); |
| 59 | Cipher c2 = Cipher.getInstance("RSA/ECB/PKCS1Padding", "SunJCE"); |
| 60 | |
| 61 | c1.init(Cipher.ENCRYPT_MODE, publicKey); |
| 62 | e = c1.doFinal(b); |
| 63 | c1.init(Cipher.DECRYPT_MODE, privateKey); |
| 64 | d = c1.doFinal(e); |
| 65 | match(b, d); |
| 66 | c2.init(Cipher.DECRYPT_MODE, privateKey); |
| 67 | d = c2.doFinal(e); |
| 68 | match(b, d); |
| 69 | |
| 70 | // invalid data |
| 71 | c1.init(Cipher.DECRYPT_MODE, publicKey); |
| 72 | try { |
| 73 | d = c1.doFinal(e); |
| 74 | throw new Exception("completed call"); |
| 75 | } catch (BadPaddingException ee) { |
| 76 | ee.printStackTrace(); |
| 77 | } |
| 78 | |
| 79 | c1.init(Cipher.ENCRYPT_MODE, privateKey); |
| 80 | e = c1.doFinal(b); |
| 81 | c1.init(Cipher.DECRYPT_MODE, publicKey); |
| 82 | d = c1.doFinal(e); |
| 83 | match(b, d); |
| 84 | c2.init(Cipher.DECRYPT_MODE, publicKey); |
| 85 | d = c2.doFinal(e); |
| 86 | match(b, d); |
| 87 | |
| 88 | // reinit tests |
| 89 | c1.init(Cipher.ENCRYPT_MODE, privateKey); |
| 90 | c1.init(Cipher.ENCRYPT_MODE, privateKey); |
| 91 | e = c1.doFinal(b); |
| 92 | e = c1.doFinal(b); |
| 93 | c1.update(b); |
| 94 | c1.update(b); |
| 95 | c1.init(Cipher.ENCRYPT_MODE, privateKey); |
| 96 | e = c1.doFinal(); |
| 97 | e = c1.doFinal(); |
| 98 | c1.update(b); |
| 99 | e = c1.doFinal(); |
| 100 | |
| 101 | c1.update(new byte[256]); |
| 102 | try { |
| 103 | e = c1.doFinal(); |
| 104 | throw new Exception("completed call"); |
| 105 | } catch (IllegalBlockSizeException ee) { |
| 106 | System.out.println(ee); |
| 107 | } |
| 108 | |
| 109 | } |
| 110 | |
| 111 | private static void match(byte[] b1, byte[] b2) throws Exception { |
| 112 | if (Arrays.equals(b1, b2) == false) { |
| 113 | System.out.println(toString(b1)); |
| 114 | System.out.println(toString(b2)); |
| 115 | throw new Exception("mismatch"); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | public static void main(String[] args) throws Exception { |
| 120 | main(new TestRSACipher()); |
| 121 | } |
| 122 | |
| 123 | } |