< prev index next >

test/compiler/codegen/7184394/TestAESBase.java

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.

@@ -29,10 +29,11 @@
 import com.oracle.java.testlibrary.Utils;
 import java.security.AlgorithmParameters;
 import java.util.Random;
 import javax.crypto.Cipher;
 import javax.crypto.SecretKey;
+import javax.crypto.spec.GCMParameterSpec;
 import javax.crypto.spec.IvParameterSpec;
 import javax.crypto.spec.SecretKeySpec;
 
 abstract public class TestAESBase {
   int msgSize = Integer.getInteger("msgSize", 646);

@@ -60,10 +61,13 @@
   final Random random = Utils.getRandomInstance();
   Cipher cipher;
   Cipher dCipher;
   AlgorithmParameters algParams;
   SecretKey key;
+  GCMParameterSpec gcm_spec;
+  byte[] aad;
+  int tlen = 0;
 
   static int numThreads = 0;
   int  threadId;
   static synchronized int getThreadId() {
     int id = numThreads;

@@ -98,10 +102,13 @@
 
       if (mode.equals("CBC")) {
         int ivLen = (algorithm.equals("AES") ? 16 : algorithm.equals("DES") ? 8 : 0);
         IvParameterSpec initVector = new IvParameterSpec(new byte[ivLen]);
         cipher.init(Cipher.ENCRYPT_MODE, key, initVector);
+      } else if (mode.equals("GCM")) {
+        gcm_init();
+        cipher.init(Cipher.ENCRYPT_MODE, key, gcm_spec);
       } else {
         algParams = cipher.getParameters();
         cipher.init(Cipher.ENCRYPT_MODE, key, algParams);
       }
       algParams = cipher.getParameters();

@@ -161,16 +168,20 @@
     }
     System.out.println();
   }
 
   void compareArrays(byte b[], byte exp[]) {
-    if (b.length != exp.length) {
+    if (mode.equals("GCM") && b.length != (exp.length + tlen)) {
+      System.out.format("different length returned ( "+ b.length + " ) vs expected ( "+ exp.length + " )");
+    } else if (b.length != exp.length) {
       System.out.format("different lengths for actual and expected output arrays\n");
       showArray(b, "test: ");
       showArray(exp, "exp : ");
       System.exit(1);
     }
+    if (mode.equals("GCM"))
+      return;
     for (int i=0; i< exp.length; i++) {
       if (b[i] != exp[i]) {
         System.out.format("output error at index %d: got %02x, expected %02x\n", i, b[i] & 0xff, exp[i] & 0xff);
         showArray(b, "test: ");
         showArray(exp, "exp : ");

@@ -184,6 +195,15 @@
     System.out.println(kind + " cipher provider: " + cipher.getProvider());
     System.out.println(kind + " cipher algorithm: " + cipher.getAlgorithm());
   }
 
   abstract void childShowCipher();
+
+  void gcm_init() {
+    byte[] iv = new byte[64];
+    random.nextBytes(iv);
+    tlen = 12;
+    gcm_spec = new GCMParameterSpec(tlen * 8, iv);
+    aad = new byte[5];
+    random.nextBytes(aad);
+  }
 }
< prev index next >