Path: blob/master/test/jdk/sun/security/pkcs11/Cipher/TestGCMKeyAndIvCheck.java
41152 views
/*1* Copyright (c) 2018, 2019, 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 8080462 822924326* @library /test/lib ..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}83System.out.println("Testing against " + p.getName());84SecretKey key = new SecretKeySpec(new byte[16], "AES");85// First try parameter-less init.86c.init(Cipher.ENCRYPT_MODE, key);87c.updateAAD(AAD);88byte[] ctPlusTag = c.doFinal(PT);8990// subsequent encryption should fail unless re-init w/ different key+iv91checkISE(c);9293// Validate the retrieved parameters against the IV and tag length.94AlgorithmParameters params = c.getParameters();95if (params == null) {96throw new Exception("getParameters() should not return null");97}98byte[] iv = null;99int tagLength = 0; // in bits100if (mode.equalsIgnoreCase("GCM")) {101GCMParameterSpec spec = params.getParameterSpec(GCMParameterSpec.class);102tagLength = spec.getTLen();103iv = spec.getIV();104} else {105throw new RuntimeException("Error: Unsupported mode: " + mode);106}107if (tagLength != (ctPlusTag.length - PT.length)*8) {108throw new Exception("Parameters contains incorrect TLen value");109}110if (!Arrays.equals(iv, c.getIV())) {111throw new Exception("Parameters contains incorrect IV value");112}113114c.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// expected127System.out.println("Expected IAPE thrown");128}129130// Now try to encrypt again using parameter-less init; should work131c.init(Cipher.ENCRYPT_MODE, key);132c.doFinal(PT);133134// make sure a different iv is used135byte[] ivNew = c.getIV();136if (Arrays.equals(iv, ivNew)) {137throw new Exception("IV should be different now");138}139140// Now try to encrypt again using a different parameter; should work141AlgorithmParameterSpec spec2 = new GCMParameterSpec(128,142"Solaris PKCS11 lib does not allow all-zero IV".getBytes());143c.init(Cipher.ENCRYPT_MODE, key, spec2);144c.updateAAD(AAD);145c.doFinal(PT);146// subsequent encryption should fail unless re-init w/ different key+iv147checkISE(c);148149// Now try decryption twice in a row; no re-init required and150// same parameters is used.151c.init(Cipher.DECRYPT_MODE, key, params);152c.updateAAD(AAD);153recovered = c.doFinal(ctPlusTag);154155c.updateAAD(AAD);156recovered = c.doFinal(ctPlusTag);157if (!Arrays.equals(recovered, PT)) {158throw new Exception("Decryption result mismatch");159}160161// Now try decryption again and re-init using the same parameters162c.init(Cipher.DECRYPT_MODE, key, params);163c.updateAAD(AAD);164recovered = c.doFinal(ctPlusTag);165166// init to decrypt w/o parameters; should fail with IKE as167// javadoc specified168try {169c.init(Cipher.DECRYPT_MODE, key);170throw new Exception("Should throw IKE for dec w/o params");171} catch (InvalidKeyException ike) {172// expected173}174175// Lastly, try encryption AND decryption w/ wrong type of parameters,176// e.g. IvParameterSpec177try {178c.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));179throw new Exception("Should throw IAPE");180} catch (InvalidAlgorithmParameterException iape) {181// expected182}183try {184c.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));185throw new Exception("Should throw IAPE");186} catch (InvalidAlgorithmParameterException iape) {187// expected188}189190System.out.println("Test Passed!");191}192193@Override194public void main(Provider p) throws Exception {195test("GCM", p);196}197}198199200201