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  * @key randomness
  31  */
  32 
  33 import java.io.*;
  34 import java.util.*;
  35 
  36 import java.security.*;
  37 
  38 import javax.crypto.*;
  39 
  40 public class TestRSACipher extends PKCS11Test {
  41 
  42     private static final String[] RSA_ALGOS =
  43         { "RSA/ECB/PKCS1Padding", "RSA" };
  44 
  45     public void main(Provider p) throws Exception {
  46         try {
  47             Cipher.getInstance(RSA_ALGOS[0], p);
  48         } catch (GeneralSecurityException e) {
  49             System.out.println("Not supported by provider, skipping");
  50             return;
  51         }
  52         KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", p);
  53         kpg.initialize(1024);
  54         KeyPair kp = kpg.generateKeyPair();
  55         PublicKey publicKey = kp.getPublic();
  56         PrivateKey privateKey = kp.getPrivate();
  57         Random random = new Random();
  58         byte[] b, e, d;
  59         b = new byte[16];
  60         random.nextBytes(b);
  61 
  62         for (String rsaAlgo: RSA_ALGOS) {
  63             Cipher c1 = Cipher.getInstance(rsaAlgo, p);
  64             Cipher c2 = Cipher.getInstance(rsaAlgo, "SunJCE");
  65 
  66             c1.init(Cipher.ENCRYPT_MODE, publicKey);
  67             e = c1.doFinal(b);
  68             c1.init(Cipher.DECRYPT_MODE, privateKey);
  69             d = c1.doFinal(e);
  70             match(b, d);
  71             c2.init(Cipher.DECRYPT_MODE, privateKey);
  72             d = c2.doFinal(e);
  73             match(b, d);
  74 
  75             // invalid data
  76             c1.init(Cipher.DECRYPT_MODE, publicKey);
  77             try {
  78                 d = c1.doFinal(e);
  79                 throw new Exception("completed call");
  80             } catch (BadPaddingException ee) {
  81                 ee.printStackTrace();
  82             }
  83 
  84             c1.init(Cipher.ENCRYPT_MODE, privateKey);
  85             e = c1.doFinal(b);
  86             c1.init(Cipher.DECRYPT_MODE, publicKey);
  87             d = c1.doFinal(e);
  88             match(b, d);
  89             c2.init(Cipher.DECRYPT_MODE, publicKey);
  90             d = c2.doFinal(e);
  91             match(b, d);
  92 
  93             // reinit tests
  94             c1.init(Cipher.ENCRYPT_MODE, privateKey);
  95             c1.init(Cipher.ENCRYPT_MODE, privateKey);
  96             e = c1.doFinal(b);
  97             e = c1.doFinal(b);
  98             c1.update(b);
  99             c1.update(b);
 100             c1.init(Cipher.ENCRYPT_MODE, privateKey);
 101             e = c1.doFinal();
 102             e = c1.doFinal();
 103             c1.update(b);
 104             e = c1.doFinal();
 105 
 106             c1.update(new byte[256]);
 107             try {
 108                 e = c1.doFinal();
 109                 throw new Exception("completed call");
 110             } catch (IllegalBlockSizeException ee) {
 111                 System.out.println(ee);
 112             }
 113         }
 114     }
 115 
 116     private static void match(byte[] b1, byte[] b2) throws Exception {
 117         if (Arrays.equals(b1, b2) == false) {
 118             System.out.println(toString(b1));
 119             System.out.println(toString(b2));
 120             throw new Exception("mismatch");
 121         }
 122     }
 123 
 124     public static void main(String[] args) throws Exception {
 125         main(new TestRSACipher());
 126     }
 127 
 128 }