test/compiler/7184394/TestAESBase.java

changeset 9806
758c07667682
parent 6876
710a3c8b516e
parent 9789
e55d4d896e30
     1.1 --- a/test/compiler/7184394/TestAESBase.java	Tue Feb 04 17:38:01 2020 +0800
     1.2 +++ b/test/compiler/7184394/TestAESBase.java	Tue Feb 04 18:13:14 2020 +0800
     1.3 @@ -29,6 +29,7 @@
     1.4  import javax.crypto.Cipher;
     1.5  import javax.crypto.KeyGenerator;
     1.6  import javax.crypto.SecretKey;
     1.7 +import javax.crypto.spec.GCMParameterSpec;
     1.8  import javax.crypto.spec.IvParameterSpec;
     1.9  import javax.crypto.spec.SecretKeySpec;
    1.10  import java.security.AlgorithmParameters;
    1.11 @@ -62,8 +63,12 @@
    1.12    Random random = new Random(0);
    1.13    Cipher cipher;
    1.14    Cipher dCipher;
    1.15 -  AlgorithmParameters algParams;
    1.16 +  AlgorithmParameters algParams = null;
    1.17    SecretKey key;
    1.18 +  GCMParameterSpec gcm_spec;
    1.19 +  byte[] aad = { 0x11, 0x22, 0x33, 0x44, 0x55 };
    1.20 +  int tlen = 12;
    1.21 +  byte[] iv = new byte[16];
    1.22  
    1.23    static int numThreads = 0;
    1.24    int  threadId;
    1.25 @@ -77,7 +82,10 @@
    1.26  
    1.27    public void prepare() {
    1.28      try {
    1.29 -    System.out.println("\nalgorithm=" + algorithm + ", mode=" + mode + ", paddingStr=" + paddingStr + ", msgSize=" + msgSize + ", keySize=" + keySize + ", noReinit=" + noReinit + ", checkOutput=" + checkOutput + ", encInputOffset=" + encInputOffset + ", encOutputOffset=" + encOutputOffset + ", decOutputOffset=" + decOutputOffset + ", lastChunkSize=" +lastChunkSize );
    1.30 +      System.out.println("\nalgorithm=" + algorithm + ", mode=" + mode + ", paddingStr=" + paddingStr +
    1.31 +              ", msgSize=" + msgSize + ", keySize=" + keySize + ", noReinit=" + noReinit +
    1.32 +              ", checkOutput=" + checkOutput + ", encInputOffset=" + encInputOffset + ", encOutputOffset=" +
    1.33 +              encOutputOffset + ", decOutputOffset=" + decOutputOffset + ", lastChunkSize=" +lastChunkSize );
    1.34  
    1.35        if (encInputOffset % ALIGN != 0 || encOutputOffset % ALIGN != 0 || decOutputOffset % ALIGN !=0 )
    1.36          testingMisalignment = true;
    1.37 @@ -98,16 +106,24 @@
    1.38        cipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE");
    1.39        dCipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE");
    1.40  
    1.41 +      // CBC init
    1.42        if (mode.equals("CBC")) {
    1.43 -        int ivLen = (algorithm.equals("AES") ? 16 : algorithm.equals("DES") ? 8 : 0);
    1.44 -        IvParameterSpec initVector = new IvParameterSpec(new byte[ivLen]);
    1.45 +        IvParameterSpec initVector = new IvParameterSpec(iv);
    1.46          cipher.init(Cipher.ENCRYPT_MODE, key, initVector);
    1.47 +        algParams = cipher.getParameters();
    1.48 +        dCipher.init(Cipher.DECRYPT_MODE, key, initVector);
    1.49 +
    1.50 +      // GCM init
    1.51 +      } else if (mode.equals("GCM")) {
    1.52 +        gcm_init(true);
    1.53 +        gcm_init(false);
    1.54 +
    1.55 +      // ECB init
    1.56        } else {
    1.57 -        algParams = cipher.getParameters();
    1.58          cipher.init(Cipher.ENCRYPT_MODE, key, algParams);
    1.59 +        dCipher.init(Cipher.DECRYPT_MODE, key, algParams);
    1.60        }
    1.61 -      algParams = cipher.getParameters();
    1.62 -      dCipher.init(Cipher.DECRYPT_MODE, key, algParams);
    1.63 +
    1.64        if (threadId == 0) {
    1.65          childShowCipher();
    1.66        }
    1.67 @@ -188,4 +204,19 @@
    1.68    }
    1.69  
    1.70    abstract void childShowCipher();
    1.71 +
    1.72 +  void gcm_init(boolean encrypt) throws Exception {
    1.73 +    gcm_spec = new GCMParameterSpec(tlen * 8, iv);
    1.74 +    if (encrypt) {
    1.75 +      // Get a new instance everytime because of reuse IV restrictions
    1.76 +      cipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE");
    1.77 +      cipher.init(Cipher.ENCRYPT_MODE, key, gcm_spec);
    1.78 +      cipher.updateAAD(aad);
    1.79 +    } else {
    1.80 +      dCipher.init(Cipher.DECRYPT_MODE, key, gcm_spec);
    1.81 +      dCipher.updateAAD(aad);
    1.82 +
    1.83 +
    1.84 +    }
    1.85 +  }
    1.86  }

mercurial