Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/KeyWrap/TestGeneral.java
66649 views
/*1* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 8248268 826862126* @summary Verify general properties of the AES/KW/NoPadding,27* AES/KW/PKCS5Padding, and AES/KWP/NoPadding.28* @run main TestGeneral29*/30import java.util.Arrays;31import java.security.Key;32import java.security.InvalidAlgorithmParameterException;33import javax.crypto.*;34import javax.crypto.spec.*;3536public class TestGeneral {3738private static final byte[] DATA_128 =39Arrays.copyOf("1234567890123456789012345678901234".getBytes(), 128);40private static final SecretKey KEY =41new SecretKeySpec(DATA_128, 0, 16, "AES");42private static final int KW_IV_LEN = 8;43private static final int KWP_IV_LEN = 4;44private static final int MAX_KW_PKCS5PAD_LEN = 16; // 1-1645private static final int MAX_KWP_PAD_LEN = 7; // 0...74647public static void testEnc(Cipher c, byte[] in, int inLen, int ivLen,48int maxPadLen) throws Exception {4950System.out.println("input len: " + inLen);51c.init(Cipher.ENCRYPT_MODE, KEY, new IvParameterSpec(in, 0, ivLen));5253int estOutLen = c.getOutputSize(inLen);5455byte[] out = c.doFinal(in, 0, inLen);5657// for encryption output, the estimate should match the actual58if (estOutLen != out.length) {59System.out.println("=> estimated: " + estOutLen);60System.out.println("=> actual enc out length: " + out.length);61throw new RuntimeException("Failed enc output len check");62}6364// encryption outout should always be multiple of 8 and at least 8-byte65// longer than input66if ((out.length % 8 != 0) || (out.length - inLen < 8)) {67throw new RuntimeException("Invalid length of encrypted data: " +68out.length);69}7071c.init(Cipher.DECRYPT_MODE, KEY, new IvParameterSpec(in, 0, ivLen));72estOutLen = c.getOutputSize(out.length);7374byte[] in2 = c.doFinal(out);7576// for decryption output, the estimate should match the actual for77// AES/KW/NoPadding and slightly larger than the actual for the rest78if (estOutLen < in2.length || (estOutLen - in2.length) > maxPadLen) {79System.out.println("=> estimated: " + estOutLen);80System.out.println("=> actual dec out length: " + in2.length);81throw new RuntimeException("Failed dec output len check");82}8384if (!Arrays.equals(in, 0, inLen, in2, 0, inLen)) {85throw new RuntimeException("Failed decrypted data check");86}87}8889public static void testWrap(Cipher c, byte[] in, int inLen, int ivLen,90int maxPadLen) throws Exception {9192System.out.println("key len: " + inLen);93c.init(Cipher.WRAP_MODE, KEY, new IvParameterSpec(in, 0, ivLen));9495int estOutLen = c.getOutputSize(inLen);9697byte[] out = c.wrap(new SecretKeySpec(in, 0, inLen, "Any"));9899// for encryption output, the estimate should match the actual100if (estOutLen != out.length) {101System.out.println("=> estimated: " + estOutLen);102System.out.println("=> actual wrap out length: " + out.length);103throw new RuntimeException("Failed wrap output len check");104}105106// encryption outout should always be multiple of 8 and at least 8-byte107// longer than input108if ((out.length % 8 != 0) || (out.length - inLen < 8)) {109throw new RuntimeException("Invalid length of encrypted data: " +110out.length);111}112c.init(Cipher.UNWRAP_MODE, KEY, new IvParameterSpec(in, 0, ivLen));113estOutLen = c.getOutputSize(out.length);114115Key key2 = c.unwrap(out, "Any", Cipher.SECRET_KEY);116117if (!(key2 instanceof SecretKey)) {118throw new RuntimeException("Failed unwrap output type check");119}120121byte[] in2 = key2.getEncoded();122// for decryption output, the estimate should match the actual for123// AES/KW/NoPadding and slightly larger than the actual for the rest124if (estOutLen < in2.length || (estOutLen - in2.length) > maxPadLen) {125System.out.println("=> estimated: " + estOutLen);126System.out.println("=> actual unwrap out length: " + in2.length);127throw new RuntimeException("Failed unwrap output len check");128}129130if (inLen != in2.length ||131!Arrays.equals(in, 0, inLen, in2, 0, inLen)) {132throw new RuntimeException("Failed unwrap data check");133}134}135136public static void testIv(Cipher c) throws Exception {137// get a fresh Cipher instance so we can test iv with pre-init state138Cipher c2 = Cipher.getInstance(c.getAlgorithm(), c.getProvider());139if (c2.getIV() != null) {140throw new RuntimeException("Expects null iv");141}142if (c2.getParameters() == null) {143throw new RuntimeException("Expects non-null default parameters");144}145146c2.init(Cipher.ENCRYPT_MODE, KEY);147byte[] defIv2 = c2.getIV();148c.init(Cipher.ENCRYPT_MODE, KEY);149byte[] defIv = c.getIV();150if (!Arrays.equals(defIv, defIv2)) {151throw new RuntimeException("Failed default iv check");152}153// try init w/ an iv w/ invalid length154try {155c.init(Cipher.ENCRYPT_MODE, KEY, new IvParameterSpec(defIv, 0,156defIv.length/2));157throw new RuntimeException("Invalid iv accepted");158} catch (InvalidAlgorithmParameterException iape) {159System.out.println("Invalid IV rejected as expected");160}161Arrays.fill(defIv, (byte) 0xFF);162c.init(Cipher.ENCRYPT_MODE, KEY, new IvParameterSpec(defIv));163byte[] newIv = c.getIV();164if (!Arrays.equals(newIv, defIv)) {165throw new RuntimeException("Failed set iv check");166}167byte[] newIv2 = c.getIV();168if (newIv == newIv2) {169throw new RuntimeException("Failed getIV copy check");170}171}172173public static void main(String[] argv) throws Exception {174byte[] data = DATA_128;175176String ALGO = "AES/KW/PKCS5Padding";177System.out.println("Testing " + ALGO);178Cipher c = Cipher.getInstance(ALGO, "SunJCE");179180// test all possible pad lengths, i.e. 1 - 16181for (int i = 1; i <= MAX_KW_PKCS5PAD_LEN; i++) {182testEnc(c, data, data.length - i, KW_IV_LEN, MAX_KW_PKCS5PAD_LEN);183testWrap(c, data, data.length - i, KW_IV_LEN, MAX_KW_PKCS5PAD_LEN);184}185testIv(c);186187ALGO = "AES/KW/NoPadding";188System.out.println("Testing " + ALGO);189c = Cipher.getInstance(ALGO, "SunJCE");190testEnc(c, data, data.length, KW_IV_LEN, 0);191testEnc(c, data, data.length >> 1, KW_IV_LEN, 0);192testWrap(c, data, data.length, KW_IV_LEN, 0);193testWrap(c, data, data.length >> 1, KW_IV_LEN, 0);194testIv(c);195196ALGO = "AES/KWP/NoPadding";197System.out.println("Testing " + ALGO);198c = Cipher.getInstance(ALGO, "SunJCE");199200// test all possible pad lengths, i.e. 0 - 7201for (int i = 0; i <= MAX_KWP_PAD_LEN; i++) {202testEnc(c, data, data.length - i, KWP_IV_LEN, MAX_KWP_PAD_LEN);203testWrap(c, data, data.length - i, KWP_IV_LEN, MAX_KWP_PAD_LEN);204}205testIv(c);206207System.out.println("All Tests Passed");208}209}210211212