4007N/A/*
4007N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
4007N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4007N/A *
4007N/A * This code is free software; you can redistribute it and/or modify it
4007N/A * under the terms of the GNU General Public License version 2 only, as
4007N/A * published by the Free Software Foundation.
4007N/A *
4007N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4007N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4007N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4007N/A * version 2 for more details (a copy is included in the LICENSE file that
4007N/A * accompanied this code).
4007N/A *
4007N/A * You should have received a copy of the GNU General Public License version
4007N/A * 2 along with this work; if not, write to the Free Software Foundation,
4007N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4007N/A *
4007N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4007N/A * or visit www.oracle.com if you need additional information or have any
4007N/A * questions.
4007N/A */
4007N/A
4007N/A/**
4007N/A * @test
4007N/A * @bug 7031343
4007N/A * @summary Provide API changes to support GCM AEAD ciphers
4007N/A * @author Brad Wetmore
4007N/A */
4007N/A
4007N/Aimport javax.crypto.*;
4007N/Aimport javax.crypto.spec.*;
4007N/Aimport java.nio.ByteBuffer;
4007N/A
4007N/A/*
4007N/A * At this point in time, we can't really do any testing since only the API
4007N/A * is available, the underlying implementation doesn't exist yet. Test
4007N/A * what we can...
4007N/A */
4007N/Apublic class GCMAPI {
4007N/A
4007N/A // 16 elements
4007N/A private static byte[] bytes = new byte[] {
4007N/A 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
4007N/A 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f };
4007N/A
4007N/A private static int failed = 0;
4007N/A private static Cipher c;
4007N/A
4007N/A public static void main(String[] args) throws Exception {
4007N/A c = Cipher.getInstance("AES");
4007N/A c.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(new byte[16], "AES"));
4007N/A
4007N/A updateAADFail((byte[]) null);
4007N/A updateAADPass(bytes);
4007N/A
4007N/A updateAADFail(null, 2, 4);
4007N/A updateAADFail(bytes, -2, 4);
4007N/A updateAADFail(bytes, 2, -4);
4007N/A updateAADFail(bytes, 2, 15); // one too many
4007N/A
4007N/A updateAADPass(bytes, 2, 14); // ok.
4007N/A updateAADPass(bytes, 4, 4);
4007N/A updateAADPass(bytes, 0, 0);
4007N/A
4007N/A ByteBuffer bb = ByteBuffer.wrap(bytes);
4007N/A
4007N/A updateAADFail((ByteBuffer) null);
4007N/A updateAADPass(bb);
4007N/A
4007N/A if (failed != 0) {
4007N/A throw new Exception("Test(s) failed");
4007N/A }
4007N/A }
4007N/A
4007N/A private static void updateAADPass(byte[] src) {
4007N/A try {
4007N/A c.updateAAD(src);
4007N/A } catch (UnsupportedOperationException e) {
4007N/A // swallow
4007N/A }catch (Exception e) {
4007N/A e.printStackTrace();
4007N/A failed++;
4007N/A }
4007N/A }
4007N/A
4007N/A private static void updateAADFail(byte[] src) {
4007N/A try {
4007N/A c.updateAAD(src);
4007N/A new Exception("Didn't Fail as Expected").printStackTrace();
4007N/A failed++;
4007N/A } catch (IllegalArgumentException e) {
4007N/A // swallow
4007N/A }
4007N/A }
4007N/A
4007N/A private static void updateAADPass(byte[] src, int offset, int len) {
4007N/A try {
4007N/A c.updateAAD(src, offset, len);
4007N/A } catch (UnsupportedOperationException e) {
4007N/A // swallow
4007N/A } catch (Exception e) {
4007N/A e.printStackTrace();
4007N/A failed++;
4007N/A }
4007N/A }
4007N/A
4007N/A private static void updateAADFail(byte[] src, int offset, int len) {
4007N/A try {
4007N/A c.updateAAD(src, offset, len);
4007N/A new Exception("Didn't Fail as Expected").printStackTrace();
4007N/A failed++;
4007N/A } catch (IllegalArgumentException e) {
4007N/A // swallow
4007N/A }
4007N/A }
4007N/A
4007N/A private static void updateAADPass(ByteBuffer src) {
4007N/A try {
4007N/A c.updateAAD(src);
4007N/A } catch (UnsupportedOperationException e) {
4007N/A // swallow
4007N/A }catch (Exception e) {
4007N/A e.printStackTrace();
4007N/A failed++;
4007N/A }
4007N/A }
4007N/A
4007N/A private static void updateAADFail(ByteBuffer src) {
4007N/A try {
4007N/A c.updateAAD(src);
4007N/A new Exception("Didn't Fail as Expected").printStackTrace();
4007N/A failed++;
4007N/A } catch (IllegalArgumentException e) {
4007N/A // swallow
4007N/A }
4007N/A }
4007N/A}