150N/A/*
3681N/A * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
150N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
150N/A *
150N/A * This code is free software; you can redistribute it and/or modify it
150N/A * under the terms of the GNU General Public License version 2 only, as
150N/A * published by the Free Software Foundation.
150N/A *
150N/A * This code is distributed in the hope that it will be useful, but WITHOUT
150N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
150N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
150N/A * version 2 for more details (a copy is included in the LICENSE file that
150N/A * accompanied this code).
150N/A *
150N/A * You should have received a copy of the GNU General Public License version
150N/A * 2 along with this work; if not, write to the Free Software Foundation,
150N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
150N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
150N/A */
150N/A
150N/A/**
150N/A * @test
3681N/A * @bug 6572331 6994008
150N/A * @summary basic test for RSA cipher key wrapping functionality
150N/A * @author Valerie Peng
150N/A * @library ..
150N/A */
150N/Aimport java.io.*;
150N/Aimport java.util.*;
150N/A
150N/Aimport java.security.*;
150N/A
150N/Aimport javax.crypto.*;
150N/Aimport javax.crypto.spec.SecretKeySpec;
150N/A
150N/Apublic class TestRSACipherWrap extends PKCS11Test {
150N/A
3681N/A private static final String[] RSA_ALGOS =
3681N/A { "RSA/ECB/PKCS1Padding", "RSA" };
150N/A
150N/A public void main(Provider p) throws Exception {
150N/A try {
3681N/A Cipher.getInstance(RSA_ALGOS[0], p);
150N/A } catch (GeneralSecurityException e) {
3681N/A System.out.println(RSA_ALGOS[0] + " unsupported, skipping");
150N/A return;
150N/A }
150N/A KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", p);
150N/A kpg.initialize(1024);
150N/A KeyPair kp = kpg.generateKeyPair();
150N/A
3681N/A for (String rsaAlgo: RSA_ALGOS) {
3681N/A Cipher cipherPKCS11 = Cipher.getInstance(rsaAlgo, p);
3681N/A Cipher cipherJce = Cipher.getInstance(rsaAlgo, "SunJCE");
150N/A
3681N/A String algos[] = {"AES", "RC2", "Blowfish"};
3681N/A int keySizes[] = {128, 256};
150N/A
3681N/A for (int j = 0; j < algos.length; j++) {
3681N/A String algorithm = algos[j];
3681N/A KeyGenerator keygen =
150N/A KeyGenerator.getInstance(algorithm);
150N/A
3681N/A for (int i = 0; i < keySizes.length; i++) {
3681N/A SecretKey secretKey = null;
3681N/A System.out.print("Generate " + keySizes[i] + "-bit " +
150N/A algorithm + " key using ");
3681N/A try {
3681N/A keygen.init(keySizes[i]);
3681N/A secretKey = keygen.generateKey();
3681N/A System.out.println(keygen.getProvider().getName());
3681N/A } catch (InvalidParameterException ipe) {
3681N/A secretKey = new SecretKeySpec(new byte[32], algorithm);
3681N/A System.out.println("SecretKeySpec class");
3681N/A }
3681N/A test(kp, secretKey, cipherPKCS11, cipherJce);
3681N/A test(kp, secretKey, cipherPKCS11, cipherPKCS11);
3681N/A test(kp, secretKey, cipherJce, cipherPKCS11);
150N/A }
150N/A }
150N/A }
150N/A }
150N/A
150N/A private static void test(KeyPair kp, SecretKey secretKey,
150N/A Cipher wrapCipher, Cipher unwrapCipher)
150N/A throws Exception {
150N/A String algo = secretKey.getAlgorithm();
150N/A wrapCipher.init(Cipher.WRAP_MODE, kp.getPublic());
150N/A byte[] wrappedKey = wrapCipher.wrap(secretKey);
150N/A unwrapCipher.init(Cipher.UNWRAP_MODE, kp.getPrivate());
150N/A Key unwrappedKey =
150N/A unwrapCipher.unwrap(wrappedKey, algo, Cipher.SECRET_KEY);
150N/A
150N/A System.out.println("Test " + wrapCipher.getProvider().getName() +
150N/A "/" + unwrapCipher.getProvider().getName() + ": ");
150N/A if (!Arrays.equals(secretKey.getEncoded(),
150N/A unwrappedKey.getEncoded())) {
150N/A throw new Exception("Test Failed!");
150N/A }
150N/A System.out.println("Passed");
150N/A }
150N/A
150N/A public static void main(String[] args) throws Exception {
150N/A main(new TestRSACipherWrap());
150N/A }
150N/A}