Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/KeyWrap/TestGeneral.java
41161 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 824826826* @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.util.Random;32import java.security.Key;33import java.security.InvalidAlgorithmParameterException;34import javax.crypto.*;35import javax.crypto.spec.*;3637public class TestGeneral {3839private static final SecretKey KEY = new SecretKeySpec(new byte[16], "AES");;40private static final int KW_IV_LEN = 8;41private static final int KWP_IV_LEN = 4;42private static final int MAX_KW_PKCS5PAD_LEN = 16; // 1-1643private static final int MAX_KWP_PAD_LEN = 7; // 0...74445public static void testEnc(Cipher c, byte[] in, int inLen, int ivLen,46int maxPadLen) throws Exception {4748System.out.println("input len: " + inLen);49c.init(Cipher.ENCRYPT_MODE, KEY, new IvParameterSpec(in, 0, ivLen));5051int estOutLen = c.getOutputSize(inLen);5253byte[] out = c.doFinal(in, 0, inLen);5455// for encryption output, the estimate should match the actual56if (estOutLen != out.length) {57System.out.println("=> estimated: " + estOutLen);58System.out.println("=> actual enc out length: " + out.length);59throw new RuntimeException("Failed enc output len check");60}6162// encryption outout should always be multiple of 8 and at least 8-byte63// longer than input64if ((out.length % 8 != 0) || (out.length - inLen < 8)) {65throw new RuntimeException("Invalid length of encrypted data: " +66out.length);67}6869c.init(Cipher.DECRYPT_MODE, KEY, new IvParameterSpec(in, 0, ivLen));70estOutLen = c.getOutputSize(out.length);7172byte[] in2 = c.doFinal(out);7374// for decryption output, the estimate should match the actual for75// AES/KW/NoPadding and slightly larger than the actual for the rest76if (estOutLen < in2.length || (estOutLen - in2.length) > maxPadLen) {77System.out.println("=> estimated: " + estOutLen);78System.out.println("=> actual dec out length: " + in2.length);79throw new RuntimeException("Failed dec output len check");80}8182if (!Arrays.equals(in, 0, inLen, in2, 0, inLen)) {83throw new RuntimeException("Failed decrypted data check");84}85}8687public static void testWrap(Cipher c, byte[] in, int inLen, int ivLen,88int maxPadLen) throws Exception {8990System.out.println("key len: " + inLen);91c.init(Cipher.WRAP_MODE, KEY, new IvParameterSpec(in, 0, ivLen));9293int estOutLen = c.getOutputSize(inLen);9495byte[] out = c.wrap(new SecretKeySpec(in, 0, inLen, "Any"));9697// for encryption output, the estimate should match the actual98if (estOutLen != out.length) {99System.out.println("=> estimated: " + estOutLen);100System.out.println("=> actual wrap out length: " + out.length);101throw new RuntimeException("Failed wrap output len check");102}103104// encryption outout should always be multiple of 8 and at least 8-byte105// longer than input106if ((out.length % 8 != 0) || (out.length - inLen < 8)) {107throw new RuntimeException("Invalid length of encrypted data: " +108out.length);109}110c.init(Cipher.UNWRAP_MODE, KEY, new IvParameterSpec(in, 0, ivLen));111estOutLen = c.getOutputSize(out.length);112113Key key2 = c.unwrap(out, "Any", Cipher.SECRET_KEY);114115if (!(key2 instanceof SecretKey)) {116throw new RuntimeException("Failed unwrap output type check");117}118119byte[] in2 = key2.getEncoded();120// for decryption output, the estimate should match the actual for121// AES/KW/NoPadding and slightly larger than the actual for the rest122if (estOutLen < in2.length || (estOutLen - in2.length) > maxPadLen) {123System.out.println("=> estimated: " + estOutLen);124System.out.println("=> actual unwrap out length: " + in2.length);125throw new RuntimeException("Failed unwrap output len check");126}127128if (inLen != in2.length ||129!Arrays.equals(in, 0, inLen, in2, 0, inLen)) {130throw new RuntimeException("Failed unwrap data check");131}132}133134public static void testIv(Cipher c) throws Exception {135c.init(Cipher.ENCRYPT_MODE, KEY);136byte[] defIv = c.getIV();137// try init w/ an iv w/ different length138try {139c.init(Cipher.ENCRYPT_MODE, KEY, new IvParameterSpec(defIv, 0,140defIv.length/2));141} catch (InvalidAlgorithmParameterException iape) {142System.out.println("Invalid IV rejected as expected");143}144Arrays.fill(defIv, (byte) 0xFF);145c.init(Cipher.ENCRYPT_MODE, KEY, new IvParameterSpec(defIv));146byte[] newIv = c.getIV();147if (!Arrays.equals(newIv, defIv)) {148throw new RuntimeException("Failed iv check");149}150}151152public static void main(String[] argv) throws Exception {153// test all possible pad lengths, i.e. 1 - 16154byte[] data = new byte[128];155new Random().nextBytes(data);156157String ALGO = "AES/KW/PKCS5Padding";158System.out.println("Testing " + ALGO);159Cipher c = Cipher.getInstance(ALGO, "SunJCE");160for (int i = 0; i < MAX_KW_PKCS5PAD_LEN; i++) {161testEnc(c, data, data.length - i, KW_IV_LEN, MAX_KW_PKCS5PAD_LEN);162testWrap(c, data, data.length - i, KW_IV_LEN, MAX_KW_PKCS5PAD_LEN);163}164testIv(c);165166ALGO = "AES/KW/NoPadding";167System.out.println("Testing " + ALGO);168c = Cipher.getInstance(ALGO, "SunJCE");169testEnc(c, data, data.length, KW_IV_LEN, 0);170testEnc(c, data, data.length >> 1, KW_IV_LEN, 0);171testWrap(c, data, data.length, KW_IV_LEN, 0);172testWrap(c, data, data.length >> 1, KW_IV_LEN, 0);173testIv(c);174175ALGO = "AES/KWP/NoPadding";176System.out.println("Testing " + ALGO);177c = Cipher.getInstance(ALGO, "SunJCE");178for (int i = 0; i < MAX_KWP_PAD_LEN; i++) {179testEnc(c, data, data.length - i, KWP_IV_LEN, MAX_KWP_PAD_LEN);180testWrap(c, data, data.length - i, KWP_IV_LEN, MAX_KWP_PAD_LEN);181}182testIv(c);183184System.out.println("All Tests Passed");185}186}187188189