Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/AEAD/SealedObjectTest.java
41161 views
/*1* Copyright (c) 2007, 2015, 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*/2223import java.security.AlgorithmParameters;24import java.util.Arrays;25import javax.crypto.SecretKey;26import javax.crypto.Cipher;27import javax.crypto.KeyGenerator;28import javax.crypto.SealedObject;2930/*31* @test32* @bug 804859633* @summary Check if the seal/unseal feature works properly in AEAD/GCM mode.34*/35public class SealedObjectTest {3637private static final String AES = "AES";38private static final String TRANSFORMATION = "AES/GCM/NoPadding";39private static final String PROVIDER = "SunJCE";40private static final int KEY_LENGTH = 128;4142public static void main(String[] args) throws Exception {43doTest();44}4546/*47* Run the test:48* - init a cipher with AES/GCM/NoPadding transformation49* - seal an object50* - check if we can't seal it again with the same key/IV51* - unseal the object using different methods of SealedObject class52* - check if the original and sealed objects are equal53*/54static void doTest() throws Exception {55// init a secret Key56KeyGenerator kg = KeyGenerator.getInstance(AES, PROVIDER);57kg.init(KEY_LENGTH);58SecretKey key = kg.generateKey();5960// initialization61Cipher cipher = Cipher.getInstance(TRANSFORMATION, PROVIDER);62cipher.init(Cipher.ENCRYPT_MODE, key);63AlgorithmParameters params = cipher.getParameters();6465// seal an object66SealedObject so = new SealedObject(key, cipher);67try {68// check if we can't seal it again with the same key/IV69so = new SealedObject(key, cipher);70throw new RuntimeException(71"FAILED: expected IllegalStateException hasn't "72+ "been thrown");73} catch (IllegalStateException ise) {74System.out.println("Expected exception when seal it again with"75+ " the same key/IV: " + ise);76}7778// unseal the object using getObject(Cipher) and compare79cipher.init(Cipher.DECRYPT_MODE, key, params);80SecretKey unsealedKey = (SecretKey) so.getObject(cipher);81assertKeysSame(unsealedKey, key, "SealedObject.getObject(Cipher)");8283// unseal the object using getObject(Key) and compare84unsealedKey = (SecretKey) so.getObject(key);85assertKeysSame(unsealedKey, key, "SealedObject.getObject(Key)");8687// unseal the object using getObject(Key, String) and compare88unsealedKey = (SecretKey) so.getObject(key, PROVIDER);8990assertKeysSame(unsealedKey, key,91"SealedObject.getObject(Key, String)");92}9394/**95* Compare two SecretKey objects.96*97* @param key1 first key98* @param key2 second key99* @param meth method that was used for unsealing the SecretKey object100* @return true if key1 and key2 are the same, false otherwise.101*/102static void assertKeysSame(SecretKey key1, SecretKey key2, String meth) {103if (!Arrays.equals(key1.getEncoded(), key2.getEncoded())) {104throw new RuntimeException(105"FAILED: original and unsealed objects aren't the same for "106+ meth);107}108}109}110111112