Path: blob/master/test/jdk/javax/crypto/spec/GCMParameterSpec/GCMParameterSpecTest.java
41154 views
/*1* Copyright (c) 2011, 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 703134326* @summary Provide API changes to support GCM AEAD ciphers27* @author Brad Wetmore28*/2930import javax.crypto.AEADBadTagException;31import javax.crypto.spec.GCMParameterSpec;32import java.util.Arrays;3334public class GCMParameterSpecTest {3536// 16 elements37private static byte[] bytes = new byte[] {380x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,390x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f };4041private static int failed = 0;4243public static void main(String[] args) throws Exception {44newGCMParameterSpecFail(-1, bytes);45newGCMParameterSpecFail(128, null);46newGCMParameterSpecPass(128, bytes);4748newGCMParameterSpecFail(-1, bytes, 2, 4);49newGCMParameterSpecFail(128, null, 2, 4);50newGCMParameterSpecFail(128, bytes, -2, 4);51newGCMParameterSpecFail(128, bytes, 2, -4);52newGCMParameterSpecFail(128, bytes, 2, 15); // one too many5354newGCMParameterSpecPass(128, bytes, 2, 14); // ok.55newGCMParameterSpecPass(96, bytes, 4, 4);56newGCMParameterSpecPass(96, bytes, 0, 0);5758// Might as well check the Exception constructors.59try {60new AEADBadTagException();61new AEADBadTagException("Bad Tag Seen");62} catch (Exception e) {63e.printStackTrace();64failed++;65}6667if (failed != 0) {68throw new Exception("Test(s) failed");69}70}7172private static void newGCMParameterSpecPass(73int tLen, byte[] src) {74try {75GCMParameterSpec gcmps = new GCMParameterSpec(tLen, src);76if (gcmps.getTLen() != tLen) {77throw new Exception("tLen's not equal");78}79if (!Arrays.equals(gcmps.getIV(), src)) {80throw new Exception("IV's not equal");81}82} catch (Exception e) {83e.printStackTrace();84failed++;85}86}8788private static void newGCMParameterSpecFail(89int tLen, byte[] src) {90try {91new GCMParameterSpec(tLen, src);92new Exception("Didn't Fail as Expected").printStackTrace();93failed++;94} catch (IllegalArgumentException e) {95// swallow96}97}9899private static void newGCMParameterSpecPass(100int tLen, byte[] src, int offset, int len) {101try {102GCMParameterSpec gcmps =103new GCMParameterSpec(tLen, src, offset, len);104if (gcmps.getTLen() != tLen) {105throw new Exception("tLen's not equal");106}107if (!Arrays.equals(gcmps.getIV(),108Arrays.copyOfRange(src, offset, offset + len))) {109System.out.println(offset + " " + len);110System.out.println(Arrays.copyOfRange(src, offset, len)[0]);111throw new Exception("IV's not equal");112}113} catch (Exception e) {114e.printStackTrace();115failed++;116}117}118119private static void newGCMParameterSpecFail(120int tLen, byte[] src, int offset, int len) {121try {122new GCMParameterSpec(tLen, src, offset, len);123new Exception("Didn't Fail as Expected").printStackTrace();124failed++;125} catch (IllegalArgumentException e) {126// swallow127}128}129}130131132