< prev index next >

test/compiler/7184394/TestAESBase.java

Print this page
rev 9055 : 8073108: Use x86 and SPARC CPU instructions for GHASH acceleration
Reviewed-by: kvn, jrose


  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 /**
  26  * @author Tom Deneau
  27  */
  28 
  29 import javax.crypto.Cipher;
  30 import javax.crypto.KeyGenerator;
  31 import javax.crypto.SecretKey;

  32 import javax.crypto.spec.IvParameterSpec;
  33 import javax.crypto.spec.SecretKeySpec;
  34 import java.security.AlgorithmParameters;
  35 
  36 import java.util.Random;
  37 import java.util.Arrays;
  38 
  39 abstract public class TestAESBase {
  40   int msgSize = Integer.getInteger("msgSize", 646);
  41   boolean checkOutput = Boolean.getBoolean("checkOutput");
  42   boolean noReinit = Boolean.getBoolean("noReinit");
  43   boolean testingMisalignment;
  44   private static final int ALIGN = 8;
  45   int encInputOffset = Integer.getInteger("encInputOffset", 0) % ALIGN;
  46   int encOutputOffset = Integer.getInteger("encOutputOffset", 0) % ALIGN;
  47   int decOutputOffset = Integer.getInteger("decOutputOffset", 0) % ALIGN;
  48   int lastChunkSize = Integer.getInteger("lastChunkSize", 32);
  49   int keySize = Integer.getInteger("keySize", 128);
  50   int inputLength;
  51   int encodeLength;
  52   int decodeLength;
  53   int decodeMsgSize;
  54   String algorithm = System.getProperty("algorithm", "AES");
  55   String mode = System.getProperty("mode", "CBC");
  56   String paddingStr = System.getProperty("paddingStr", "PKCS5Padding");
  57   byte[] input;
  58   byte[] encode;
  59   byte[] expectedEncode;
  60   byte[] decode;
  61   byte[] expectedDecode;
  62   Random random = new Random(0);
  63   Cipher cipher;
  64   Cipher dCipher;
  65   AlgorithmParameters algParams;
  66   SecretKey key;




  67 
  68   static int numThreads = 0;
  69   int  threadId;
  70   static synchronized int getThreadId() {
  71     int id = numThreads;
  72     numThreads++;
  73     return id;
  74   }
  75 
  76   abstract public void run();
  77 
  78   public void prepare() {
  79     try {
  80     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 );
  81 
  82       if (encInputOffset % ALIGN != 0 || encOutputOffset % ALIGN != 0 || decOutputOffset % ALIGN !=0 )
  83         testingMisalignment = true;
  84 
  85       int keyLenBytes = (keySize == 0 ? 16 : keySize/8);
  86       byte keyBytes[] = new byte[keyLenBytes];
  87       if (keySize == 128)
  88         keyBytes = new byte[] {-8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7};
  89       else
  90         random.nextBytes(keyBytes);
  91 
  92       key = new SecretKeySpec(keyBytes, algorithm);
  93       if (threadId == 0) {
  94         System.out.println("Algorithm: " + key.getAlgorithm() + "("
  95                            + key.getEncoded().length * 8 + "bit)");
  96       }
  97 
  98       cipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE");
  99       dCipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE");
 100 
 101       if (mode.equals("CBC")) {
 102         int ivLen = (algorithm.equals("AES") ? 16 : algorithm.equals("DES") ? 8 : 0);
 103         IvParameterSpec initVector = new IvParameterSpec(new byte[ivLen]);
 104         cipher.init(Cipher.ENCRYPT_MODE, key, initVector);






 105       } else {
 106         algParams = cipher.getParameters();
 107         cipher.init(Cipher.ENCRYPT_MODE, key, algParams);
 108       }
 109       algParams = cipher.getParameters();
 110       dCipher.init(Cipher.DECRYPT_MODE, key, algParams);
 111       if (threadId == 0) {
 112         childShowCipher();
 113       }
 114 
 115       inputLength = msgSize + encInputOffset;
 116       if (testingMisalignment) {
 117         encodeLength = cipher.getOutputSize(msgSize - lastChunkSize) + encOutputOffset;
 118         encodeLength += cipher.getOutputSize(lastChunkSize);
 119         decodeLength = dCipher.getOutputSize(encodeLength - lastChunkSize) + decOutputOffset;
 120         decodeLength += dCipher.getOutputSize(lastChunkSize);
 121       } else {
 122         encodeLength = cipher.getOutputSize(msgSize) + encOutputOffset;
 123         decodeLength = dCipher.getOutputSize(encodeLength) + decOutputOffset;
 124       }


 171       showArray(exp, "exp : ");
 172       System.exit(1);
 173     }
 174     for (int i=0; i< exp.length; i++) {
 175       if (b[i] != exp[i]) {
 176         System.out.format("output error at index %d: got %02x, expected %02x\n", i, b[i] & 0xff, exp[i] & 0xff);
 177         showArray(b, "test: ");
 178         showArray(exp, "exp : ");
 179         System.exit(1);
 180       }
 181     }
 182   }
 183 
 184 
 185   void showCipher(Cipher c, String kind) {
 186     System.out.println(kind + " cipher provider: " + cipher.getProvider());
 187     System.out.println(kind + " cipher algorithm: " + cipher.getAlgorithm());
 188   }
 189 
 190   abstract void childShowCipher();








 191 }


  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 /**
  26  * @author Tom Deneau
  27  */
  28 
  29 import javax.crypto.Cipher;
  30 import javax.crypto.KeyGenerator;
  31 import javax.crypto.SecretKey;
  32 import javax.crypto.spec.GCMParameterSpec;
  33 import javax.crypto.spec.IvParameterSpec;
  34 import javax.crypto.spec.SecretKeySpec;
  35 import java.security.AlgorithmParameters;
  36 
  37 import java.util.Random;
  38 import java.util.Arrays;
  39 
  40 abstract public class TestAESBase {
  41   int msgSize = Integer.getInteger("msgSize", 646);
  42   boolean checkOutput = Boolean.getBoolean("checkOutput");
  43   boolean noReinit = Boolean.getBoolean("noReinit");
  44   boolean testingMisalignment;
  45   private static final int ALIGN = 8;
  46   int encInputOffset = Integer.getInteger("encInputOffset", 0) % ALIGN;
  47   int encOutputOffset = Integer.getInteger("encOutputOffset", 0) % ALIGN;
  48   int decOutputOffset = Integer.getInteger("decOutputOffset", 0) % ALIGN;
  49   int lastChunkSize = Integer.getInteger("lastChunkSize", 32);
  50   int keySize = Integer.getInteger("keySize", 128);
  51   int inputLength;
  52   int encodeLength;
  53   int decodeLength;
  54   int decodeMsgSize;
  55   String algorithm = System.getProperty("algorithm", "AES");
  56   String mode = System.getProperty("mode", "CBC");
  57   String paddingStr = System.getProperty("paddingStr", "PKCS5Padding");
  58   byte[] input;
  59   byte[] encode;
  60   byte[] expectedEncode;
  61   byte[] decode;
  62   byte[] expectedDecode;
  63   Random random = new Random(0);
  64   Cipher cipher;
  65   Cipher dCipher;
  66   AlgorithmParameters algParams;
  67   SecretKey key;
  68   GCMParameterSpec gcm_spec;
  69   byte[] aad;
  70   int tlen = 12;
  71   byte[] iv;
  72 
  73   static int numThreads = 0;
  74   int  threadId;
  75   static synchronized int getThreadId() {
  76     int id = numThreads;
  77     numThreads++;
  78     return id;
  79   }
  80 
  81   abstract public void run();
  82 
  83   public void prepare() {
  84     try {
  85     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 );
  86 
  87       if (encInputOffset % ALIGN != 0 || encOutputOffset % ALIGN != 0 || decOutputOffset % ALIGN !=0 )
  88         testingMisalignment = true;
  89 
  90       int keyLenBytes = (keySize == 0 ? 16 : keySize/8);
  91       byte keyBytes[] = new byte[keyLenBytes];
  92       if (keySize == 128)
  93         keyBytes = new byte[] {-8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7};
  94       else
  95         random.nextBytes(keyBytes);
  96 
  97       key = new SecretKeySpec(keyBytes, algorithm);
  98       if (threadId == 0) {
  99         System.out.println("Algorithm: " + key.getAlgorithm() + "("
 100                            + key.getEncoded().length * 8 + "bit)");
 101       }
 102 
 103       cipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE");
 104       dCipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE");
 105 
 106       if (mode.equals("CBC")) {
 107         int ivLen = (algorithm.equals("AES") ? 16 : algorithm.equals("DES") ? 8 : 0);
 108         IvParameterSpec initVector = new IvParameterSpec(new byte[ivLen]);
 109         cipher.init(Cipher.ENCRYPT_MODE, key, initVector);
 110       } else if (mode.equals("GCM")) {
 111           iv = new byte[64];
 112           random.nextBytes(iv);
 113           aad = new byte[5];
 114           random.nextBytes(aad);
 115           gcm_init();
 116       } else {
 117         algParams = cipher.getParameters();
 118         cipher.init(Cipher.ENCRYPT_MODE, key, algParams);
 119       }
 120       algParams = cipher.getParameters();
 121       dCipher.init(Cipher.DECRYPT_MODE, key, algParams);
 122       if (threadId == 0) {
 123         childShowCipher();
 124       }
 125 
 126       inputLength = msgSize + encInputOffset;
 127       if (testingMisalignment) {
 128         encodeLength = cipher.getOutputSize(msgSize - lastChunkSize) + encOutputOffset;
 129         encodeLength += cipher.getOutputSize(lastChunkSize);
 130         decodeLength = dCipher.getOutputSize(encodeLength - lastChunkSize) + decOutputOffset;
 131         decodeLength += dCipher.getOutputSize(lastChunkSize);
 132       } else {
 133         encodeLength = cipher.getOutputSize(msgSize) + encOutputOffset;
 134         decodeLength = dCipher.getOutputSize(encodeLength) + decOutputOffset;
 135       }


 182       showArray(exp, "exp : ");
 183       System.exit(1);
 184     }
 185     for (int i=0; i< exp.length; i++) {
 186       if (b[i] != exp[i]) {
 187         System.out.format("output error at index %d: got %02x, expected %02x\n", i, b[i] & 0xff, exp[i] & 0xff);
 188         showArray(b, "test: ");
 189         showArray(exp, "exp : ");
 190         System.exit(1);
 191       }
 192     }
 193   }
 194 
 195 
 196   void showCipher(Cipher c, String kind) {
 197     System.out.println(kind + " cipher provider: " + cipher.getProvider());
 198     System.out.println(kind + " cipher algorithm: " + cipher.getAlgorithm());
 199   }
 200 
 201   abstract void childShowCipher();
 202 
 203   void gcm_init() throws Exception {
 204     tlen = 12;
 205     gcm_spec = new GCMParameterSpec(tlen * 8, iv);
 206     cipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE");
 207     cipher.init(Cipher.ENCRYPT_MODE, key, gcm_spec);
 208     cipher.update(aad);
 209   }
 210 }
< prev index next >