Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/Cipher/TestGCMKeyAndIvCheck.java
38855 views
/*1* Copyright (c) 2018, 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 808046226* @library ..27* @modules jdk.crypto.cryptoki28* @run main TestGCMKeyAndIvCheck29* @summary Ensure that same key+iv can't be repeated used for encryption.30*/313233import java.security.*;34import java.security.spec.AlgorithmParameterSpec;35import javax.crypto.*;36import javax.crypto.spec.*;37import java.math.*;3839import java.util.*;4041public class TestGCMKeyAndIvCheck extends PKCS11Test {4243private static final byte[] AAD = new byte[5];44private static final byte[] PT = new byte[18];4546public static void main(String[] args) throws Exception {47main(new TestGCMKeyAndIvCheck(), args);48}4950private static void checkISE(Cipher c) throws Exception {51// Subsequent encryptions should fail52try {53c.updateAAD(AAD);54throw new Exception("Should throw ISE for updateAAD()");55} catch (IllegalStateException ise) {56// expected57}5859try {60c.update(PT);61throw new Exception("Should throw ISE for update()");62} catch (IllegalStateException ise) {63// expected64}65try {66c.doFinal(PT);67throw new Exception("Should throw ISE for doFinal()");68} catch (IllegalStateException ise) {69// expected70}71}7273public void test(String mode, Provider p) throws Exception {74Cipher c;75try {76String transformation = "AES/" + mode + "/NoPadding";77c = Cipher.getInstance(transformation, p);78} catch (GeneralSecurityException e) {79System.out.println("Skip testing " + p.getName() +80", no support for " + mode);81return;82}83SecretKey key = new SecretKeySpec(new byte[16], "AES");84// First try parameter-less init.85c.init(Cipher.ENCRYPT_MODE, key);86c.updateAAD(AAD);87byte[] ctPlusTag = c.doFinal(PT);8889// subsequent encryption should fail unless re-init w/ different key+iv90checkISE(c);9192// Validate the retrieved parameters against the IV and tag length.93AlgorithmParameters params = c.getParameters();94if (params == null) {95throw new Exception("getParameters() should not return null");96}97byte[] iv = null;98int tagLength = 0; // in bits99if (mode.equalsIgnoreCase("GCM")) {100GCMParameterSpec spec = params.getParameterSpec(GCMParameterSpec.class);101tagLength = spec.getTLen();102iv = spec.getIV();103} else {104throw new RuntimeException("Error: Unsupported mode: " + mode);105}106if (tagLength != (ctPlusTag.length - PT.length)*8) {107throw new Exception("Parameters contains incorrect TLen value");108}109if (!Arrays.equals(iv, c.getIV())) {110throw new Exception("Parameters contains incorrect IV value");111}112113// Should be ok to use the same key+iv for decryption114c.init(Cipher.DECRYPT_MODE, key, params);115c.updateAAD(AAD);116byte[] recovered = c.doFinal(ctPlusTag);117if (!Arrays.equals(recovered, PT)) {118throw new Exception("decryption result mismatch");119}120121// Now try to encrypt again using the same key+iv; should fail also122try {123c.init(Cipher.ENCRYPT_MODE, key, params);124throw new Exception("Should throw exception when same key+iv is used");125} catch (InvalidAlgorithmParameterException iape) {126// expected127}128129// Now try to encrypt again using parameter-less init; should work130c.init(Cipher.ENCRYPT_MODE, key);131c.doFinal(PT);132133// make sure a different iv is used134byte[] ivNew = c.getIV();135if (Arrays.equals(iv, ivNew)) {136throw new Exception("IV should be different now");137}138139// Now try to encrypt again using a different parameter; should work140AlgorithmParameterSpec spec2 = new GCMParameterSpec(128, new byte[30]);141c.init(Cipher.ENCRYPT_MODE, key, spec2);142c.updateAAD(AAD);143c.doFinal(PT);144// subsequent encryption should fail unless re-init w/ different key+iv145checkISE(c);146147// Now try decryption twice in a row; no re-init required and148// same parameters is used.149c.init(Cipher.DECRYPT_MODE, key, params);150c.updateAAD(AAD);151recovered = c.doFinal(ctPlusTag);152153c.updateAAD(AAD);154recovered = c.doFinal(ctPlusTag);155if (!Arrays.equals(recovered, PT)) {156throw new Exception("decryption result mismatch");157}158159// Now try decryption again and re-init using the same parameters160c.init(Cipher.DECRYPT_MODE, key, params);161c.updateAAD(AAD);162recovered = c.doFinal(ctPlusTag);163164// init to decrypt w/o parameters; should fail with IKE as165// javadoc specified166try {167c.init(Cipher.DECRYPT_MODE, key);168throw new Exception("Should throw IKE for dec w/o params");169} catch (InvalidKeyException ike) {170// expected171}172173// Lastly, try encryption AND decryption w/ wrong type of parameters,174// e.g. IvParameterSpec175try {176c.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));177throw new Exception("Should throw IAPE");178} catch (InvalidAlgorithmParameterException iape) {179// expected180}181try {182c.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));183throw new Exception("Should throw IAPE");184} catch (InvalidAlgorithmParameterException iape) {185// expected186}187188System.out.println("Test Passed!");189}190191@Override192public void main(Provider p) throws Exception {193test("GCM", p);194}195}196197198199