4597N/A/*
4597N/A * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
4597N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4597N/A *
4597N/A * This code is free software; you can redistribute it and/or modify it
4597N/A * under the terms of the GNU General Public License version 2 only, as
4597N/A * published by the Free Software Foundation.
4597N/A *
4597N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4597N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4597N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4597N/A * version 2 for more details (a copy is included in the LICENSE file that
4597N/A * accompanied this code).
4597N/A *
4597N/A * You should have received a copy of the GNU General Public License version
4597N/A * 2 along with this work; if not, write to the Free Software Foundation,
4597N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4597N/A *
4597N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4597N/A * or visit www.oracle.com if you need additional information or have any
4597N/A * questions.
4597N/A */
4597N/A
4597N/A/*
4597N/A * @test
4597N/A * @bug 7088989
4597N/A * @summary Ensure the AES ciphers of OracleUcrypto provider works correctly
4597N/A */
4597N/Aimport java.io.*;
4597N/Aimport java.security.*;
4597N/Aimport java.security.spec.*;
4597N/Aimport java.util.*;
4597N/Aimport javax.crypto.*;
4597N/Aimport javax.crypto.spec.*;
4597N/A
4597N/Apublic class TestAES extends UcryptoTest {
4597N/A
4597N/A private static final String[] PADDEDCIPHER_ALGOS = {
4597N/A "AES/ECB/PKCS5Padding",
4597N/A "AES/CBC/PKCS5Padding",
4597N/A "AES/CFB128/PKCS5Padding"
4597N/A };
4597N/A
4597N/A private static final String[] CIPHER_ALGOS = {
4597N/A "AES/ECB/NoPadding",
4597N/A "AES/CBC/NoPadding",
4597N/A "AES/CFB128/NoPadding",
4597N/A "AES/CTR/NoPadding",
4597N/A };
4597N/A
4597N/A private static final SecretKey CIPHER_KEY =
4597N/A new SecretKeySpec(new byte[16], "AES");
4597N/A
4597N/A public static void main(String[] args) throws Exception {
4597N/A main(new TestAES(), null);
4597N/A }
4597N/A
4597N/A public void doTest(Provider prov) throws Exception {
4597N/A // Provider for testing Interoperability
4597N/A Provider sunJCEProv = Security.getProvider("SunJCE");
4597N/A
4597N/A testCipherInterop(CIPHER_ALGOS, CIPHER_KEY, prov, sunJCEProv);
4597N/A testCipherInterop(PADDEDCIPHER_ALGOS, CIPHER_KEY, prov, sunJCEProv);
4597N/A
4597N/A testCipherOffset(CIPHER_ALGOS, CIPHER_KEY, prov);
4597N/A testCipherOffset(PADDEDCIPHER_ALGOS, CIPHER_KEY, prov);
4597N/A
4597N/A testCipherKeyWrapping(PADDEDCIPHER_ALGOS, CIPHER_KEY, prov, sunJCEProv);
4597N/A testCipherGCM(CIPHER_KEY, prov);
4597N/A }
4597N/A
4597N/A private static void testCipherInterop(String[] algos, SecretKey key,
4597N/A Provider p,
4597N/A Provider interopP) {
4597N/A boolean testPassed = true;
4597N/A byte[] in = new byte[32];
4597N/A (new SecureRandom()).nextBytes(in);
4597N/A
4597N/A for (String algo : algos) {
4597N/A try {
4597N/A // check ENC
4597N/A Cipher c;
4597N/A try {
4597N/A c = Cipher.getInstance(algo, p);
4597N/A } catch (NoSuchAlgorithmException nsae) {
4597N/A System.out.println("Skipping Unsupported CIP algo: " + algo);
4597N/A continue;
4597N/A }
4597N/A c.init(Cipher.ENCRYPT_MODE, key, (AlgorithmParameters)null, null);
4597N/A byte[] eout = c.doFinal(in, 0, in.length);
4597N/A
4597N/A AlgorithmParameters params = c.getParameters();
4597N/A Cipher c2 = Cipher.getInstance(algo, interopP);
4597N/A c2.init(Cipher.ENCRYPT_MODE, key, params, null);
4597N/A byte[] eout2 = c2.doFinal(in, 0, in.length);
4597N/A
4597N/A if (!Arrays.equals(eout, eout2)) {
4597N/A System.out.println(algo + ": DIFF FAILED");
4597N/A testPassed = false;
4597N/A } else {
4597N/A System.out.println(algo + ": ENC Passed");
4597N/A }
4597N/A
4597N/A // check DEC
4597N/A c.init(Cipher.DECRYPT_MODE, key, params, null);
4597N/A byte[] dout = c.doFinal(eout);
4597N/A c2.init(Cipher.DECRYPT_MODE, key, params, null);
4597N/A byte[] dout2 = c2.doFinal(eout2);
4597N/A
4597N/A if (!Arrays.equals(dout, dout2)) {
4597N/A System.out.println(algo + ": DIFF FAILED");
4597N/A testPassed = false;
4597N/A } else {
4597N/A System.out.println(algo + ": DEC Passed");
4597N/A }
4597N/A } catch(Exception ex) {
4597N/A System.out.println("Unexpected Exception: " + algo);
4597N/A ex.printStackTrace();
4597N/A testPassed = false;
4597N/A }
4597N/A }
4597N/A
4597N/A if (!testPassed) {
4597N/A throw new RuntimeException("One or more CIPHER test failed!");
4597N/A } else {
4597N/A System.out.println("CIPHER Interop Tests Passed");
4597N/A }
4597N/A }
4597N/A
4597N/A private static void testCipherOffset(String[] algos, SecretKey key,
4597N/A Provider p) {
4597N/A boolean testPassed = true;
4597N/A byte[] in = new byte[16];
4597N/A (new SecureRandom()).nextBytes(in);
4597N/A int blockSize = 16;
4597N/A
4597N/A for (int j = 1; j < (in.length - 1); j++) {
4597N/A System.out.println("Input offset size: " + j);
4597N/A for (int i = 0; i < algos.length; i++) {
4597N/A try {
4597N/A // check ENC
4597N/A Cipher c;
4597N/A try {
4597N/A c = Cipher.getInstance(algos[i], p);
4597N/A } catch (NoSuchAlgorithmException nsae) {
4597N/A System.out.println("Skip Unsupported CIP algo: " + algos[i]);
4597N/A continue;
4597N/A }
4597N/A c.init(Cipher.ENCRYPT_MODE, key, (AlgorithmParameters)null, null);
4597N/A byte[] eout = new byte[c.getOutputSize(in.length)];
4597N/A int firstPartLen = in.length - j - 1;
4597N/A //System.out.print("1st UPDATE: " + firstPartLen);
4597N/A int k = c.update(in, 0, firstPartLen, eout, 0);
4597N/A k += c.update(in, firstPartLen, 1, eout, k);
4597N/A k += c.doFinal(in, firstPartLen+1, j, eout, k);
4597N/A
4597N/A AlgorithmParameters params = c.getParameters();
4597N/A
4597N/A Cipher c2 = Cipher.getInstance(algos[i], p);
4597N/A c2.init(Cipher.ENCRYPT_MODE, key, params, null);
4597N/A byte[] eout2 = new byte[c2.getOutputSize(in.length)];
4597N/A int k2 = c2.update(in, 0, j, eout2, 0);
4597N/A k2 += c2.update(in, j, 1, eout2, k2);
4597N/A k2 += c2.doFinal(in, j+1, firstPartLen, eout2, k2);
4597N/A
4597N/A if (!checkArrays(eout, k, eout2, k2)) testPassed = false;
4597N/A
4597N/A // check DEC
4597N/A c.init(Cipher.DECRYPT_MODE, key, params, null);
4597N/A byte[] dout = new byte[c.getOutputSize(eout.length)];
4597N/A k = c.update(eout, 0, firstPartLen, dout, 0);
4597N/A k += c.update(eout, firstPartLen, 1, dout, k);
4597N/A k += c.doFinal(eout, firstPartLen+1, eout.length - firstPartLen - 1, dout, k);
4597N/A if (!checkArrays(in, in.length, dout, k)) testPassed = false;
4597N/A } catch(Exception ex) {
4597N/A System.out.println("Unexpected Exception: " + algos[i]);
4597N/A ex.printStackTrace();
4597N/A testPassed = false;
4597N/A }
4597N/A }
4597N/A }
4597N/A if (!testPassed) {
4597N/A throw new RuntimeException("One or more CIPHER test failed!");
4597N/A } else {
4597N/A System.out.println("CIPHER Offset Tests Passed");
4597N/A }
4597N/A }
4597N/A
4597N/A private static void testCipherKeyWrapping(String[] algos, SecretKey key,
4597N/A Provider p, Provider interopP)
4597N/A throws NoSuchAlgorithmException {
4597N/A boolean testPassed = true;
4597N/A
4597N/A // Test SecretKey, PrivateKey and PublicKey
4597N/A Key[] tbwKeys = new Key[3];
4597N/A int[] tbwKeyTypes = { Cipher.SECRET_KEY, Cipher.PRIVATE_KEY, Cipher.PUBLIC_KEY };
4597N/A tbwKeys[0] = new SecretKeySpec(new byte[20], "Blowfish");
4597N/A KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
4597N/A kpg.initialize(1024);
4597N/A KeyPair kp = kpg.generateKeyPair();
4597N/A tbwKeys[1] = kp.getPrivate();
4597N/A tbwKeys[2] = kp.getPublic();
4597N/A
4597N/A for (int i = 0; i < algos.length; i++) {
4597N/A try {
4597N/A System.out.println(algos[i] + " - Native WRAP/Java UNWRAP");
4597N/A
4597N/A Cipher c1;
4597N/A try {
4597N/A c1 = Cipher.getInstance(algos[i], p);
4597N/A } catch (NoSuchAlgorithmException nsae) {
4597N/A System.out.println("Skipping Unsupported CIP algo: " + algos[i]);
4597N/A continue;
4597N/A }
4597N/A c1.init(Cipher.WRAP_MODE, key, (AlgorithmParameters)null, null);
4597N/A AlgorithmParameters params = c1.getParameters();
4597N/A Cipher c2 = Cipher.getInstance(algos[i], interopP);
4597N/A c2.init(Cipher.UNWRAP_MODE, key, params, null);
4597N/A
4597N/A for (int j = 0; j < tbwKeys.length ; j++) {
4597N/A byte[] wrappedKey = c1.wrap(tbwKeys[j]);
4597N/A Key recovered = c2.unwrap(wrappedKey,
4597N/A tbwKeys[j].getAlgorithm(), tbwKeyTypes[j]);
4597N/A if (!checkKeys(tbwKeys[j], recovered)) testPassed = false;
4597N/A }
4597N/A
4597N/A System.out.println(algos[i] + " - Java WRAP/Native UNWRAP");
4597N/A c1 = Cipher.getInstance(algos[i], interopP);
4597N/A c1.init(Cipher.WRAP_MODE, key, (AlgorithmParameters)null, null);
4597N/A params = c1.getParameters();
4597N/A c2 = Cipher.getInstance(algos[i], p);
4597N/A c2.init(Cipher.UNWRAP_MODE, key, params, null);
4597N/A
4597N/A for (int j = 0; j < tbwKeys.length ; j++) {
4597N/A byte[] wrappedKey = c1.wrap(tbwKeys[j]);
4597N/A Key recovered = c2.unwrap(wrappedKey,
4597N/A tbwKeys[j].getAlgorithm(), tbwKeyTypes[j]);
4597N/A if (!checkKeys(tbwKeys[j], recovered)) testPassed = false;
4597N/A }
4597N/A
4597N/A } catch(Exception ex) {
4597N/A System.out.println("Unexpected Exception: " + algos[i]);
4597N/A ex.printStackTrace();
4597N/A testPassed = false;
4597N/A }
4597N/A }
4597N/A if (!testPassed) {
4597N/A throw new RuntimeException("One or more CIPHER test failed!");
4597N/A } else {
4597N/A System.out.println("CIPHER KeyWrapping Tests Passed");
4597N/A }
4597N/A }
4597N/A
4597N/A
4597N/A private static void testCipherGCM(SecretKey key,
4597N/A Provider p) {
4597N/A boolean testPassed = true;
4597N/A byte[] in = new byte[16];
4597N/A (new SecureRandom()).nextBytes(in);
4597N/A
4597N/A byte[] iv = new byte[16];
4597N/A (new SecureRandom()).nextBytes(iv);
4597N/A
4597N/A
4597N/A String algo = "AES/GCM/NoPadding";
4597N/A int tagLen[] = { 128, 120, 112, 104, 96, 64, 32 };
4597N/A
4597N/A try {
4597N/A Cipher c;
4597N/A try {
4597N/A c = Cipher.getInstance(algo, p);
4597N/A } catch (NoSuchAlgorithmException nsae) {
4597N/A System.out.println("Skipping Unsupported CIP algo: " + algo);
4597N/A return;
4597N/A }
4597N/A for (int i = 0; i < tagLen.length; i++) {
4597N/A AlgorithmParameterSpec paramSpec = new GCMParameterSpec(tagLen[i], iv);
4597N/A // check ENC
4597N/A c.init(Cipher.ENCRYPT_MODE, key, paramSpec, null);
4597N/A c.updateAAD(iv);
4597N/A byte[] eout = c.doFinal(in, 0, in.length);
4597N/A
4597N/A AlgorithmParameters param = c.getParameters();
4597N/A // check DEC
4597N/A c.init(Cipher.DECRYPT_MODE, key, param, null);
4597N/A c.updateAAD(iv);
4597N/A byte[] dout = c.doFinal(eout, 0, eout.length);
4597N/A
4597N/A if (!Arrays.equals(dout, in)) {
4597N/A System.out.println(algo + ": PT and RT DIFF FAILED");
4597N/A testPassed = false;
4597N/A } else {
4597N/A System.out.println(algo + ": tagLen " + tagLen[i] + " done");
4597N/A }
4597N/A }
4597N/A } catch(Exception ex) {
4597N/A System.out.println("Unexpected Exception: " + algo);
4597N/A ex.printStackTrace();
4597N/A testPassed = false;
4597N/A }
4597N/A if (!testPassed) {
4597N/A throw new RuntimeException("One or more CIPHER test failed!");
4597N/A } else {
4597N/A System.out.println("CIPHER GCM Tests Passed");
4597N/A }
4597N/A }
4597N/A
4597N/A private static boolean checkArrays(byte[] a1, int a1Len, byte[] a2, int a2Len) {
4597N/A boolean equal = true;
4597N/A if (a1Len != a2Len) {
4597N/A System.out.println("DIFFERENT OUT LENGTH");
4597N/A equal = false;
4597N/A } else {
4597N/A for (int p = 0; p < a1Len; p++) {
4597N/A if (a1[p] != a2[p]) {
4597N/A System.out.println("DIFF FAILED");
4597N/A equal = false;
4597N/A break;
4597N/A }
4597N/A }
4597N/A }
4597N/A return equal;
4597N/A }
4597N/A
4597N/A private static boolean checkKeys(Key k1, Key k2) {
4597N/A boolean equal = true;
4597N/A if (!k1.getAlgorithm().equalsIgnoreCase(k2.getAlgorithm())) {
4597N/A System.out.println("DIFFERENT Key Algorithm");
4597N/A equal = false;
4597N/A } else if (!k1.getFormat().equalsIgnoreCase(k2.getFormat())) {
4597N/A System.out.println("DIFFERENT Key Format");
4597N/A equal = false;
4597N/A } else if (!Arrays.equals(k1.getEncoded(), k2.getEncoded())) {
4597N/A System.out.println("DIFFERENT Key Encoding");
4597N/A equal = false;
4597N/A }
4597N/A return equal;
4597N/A }
4597N/A}