Path: blob/master/test/jdk/javax/crypto/Cipher/GCMAPI.java
41149 views
/*1* Copyright (c) 2011, 2013, 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.*;31import javax.crypto.spec.*;32import java.nio.ByteBuffer;3334/*35* At this point in time, we can't really do any testing since only the API36* is available, the underlying implementation doesn't exist yet. Test37* what we can...38*/39public class GCMAPI {4041// 16 elements42private static byte[] bytes = new byte[] {430x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,440x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f };4546private static int failed = 0;47private static Cipher c;4849public static void main(String[] args) throws Exception {50c = Cipher.getInstance("AES");51c.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(new byte[16], "AES"));5253updateAADFail((byte[]) null);54updateAADPass(bytes);5556updateAADFail(null, 2, 4);57updateAADFail(bytes, -2, 4);58updateAADFail(bytes, 2, -4);59updateAADFail(bytes, 2, 15); // one too many6061updateAADPass(bytes, 2, 14); // ok.62updateAADPass(bytes, 4, 4);63updateAADPass(bytes, 0, 0);6465ByteBuffer bb = ByteBuffer.wrap(bytes);6667updateAADFail((ByteBuffer) null);68updateAADPass(bb);6970if (failed != 0) {71throw new Exception("Test(s) failed");72}73}7475private static void updateAADPass(byte[] src) {76try {77c.updateAAD(src);78} catch (UnsupportedOperationException e) {79// swallow80} catch (IllegalStateException ise) {81// swallow82}catch (Exception e) {83e.printStackTrace();84failed++;85}86}8788private static void updateAADFail(byte[] src) {89try {90c.updateAAD(src);91new Exception("Didn't Fail as Expected").printStackTrace();92failed++;93} catch (IllegalArgumentException e) {94// swallow95}96}9798private static void updateAADPass(byte[] src, int offset, int len) {99try {100c.updateAAD(src, offset, len);101} catch (UnsupportedOperationException e) {102// swallow103} catch (IllegalStateException ise) {104// swallow105} catch (Exception e) {106e.printStackTrace();107failed++;108}109}110111private static void updateAADFail(byte[] src, int offset, int len) {112try {113c.updateAAD(src, offset, len);114new Exception("Didn't Fail as Expected").printStackTrace();115failed++;116} catch (IllegalArgumentException e) {117// swallow118}119}120121private static void updateAADPass(ByteBuffer src) {122try {123c.updateAAD(src);124} catch (UnsupportedOperationException e) {125// swallow126} catch (IllegalStateException ise) {127// swallow128}catch (Exception e) {129e.printStackTrace();130failed++;131}132}133134private static void updateAADFail(ByteBuffer src) {135try {136c.updateAAD(src);137new Exception("Didn't Fail as Expected").printStackTrace();138failed++;139} catch (IllegalArgumentException e) {140// swallow141}142}143}144145146