1 /* |
1 /* |
2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
38 |
38 |
39 abstract public class TestAESBase { |
39 abstract public class TestAESBase { |
40 int msgSize = Integer.getInteger("msgSize", 646); |
40 int msgSize = Integer.getInteger("msgSize", 646); |
41 boolean checkOutput = Boolean.getBoolean("checkOutput"); |
41 boolean checkOutput = Boolean.getBoolean("checkOutput"); |
42 boolean noReinit = Boolean.getBoolean("noReinit"); |
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); |
43 int keySize = Integer.getInteger("keySize", 128); |
49 int keySize = Integer.getInteger("keySize", 128); |
|
50 int inputLength; |
|
51 int encodeLength; |
|
52 int decodeLength; |
|
53 int decodeMsgSize; |
44 String algorithm = System.getProperty("algorithm", "AES"); |
54 String algorithm = System.getProperty("algorithm", "AES"); |
45 String mode = System.getProperty("mode", "CBC"); |
55 String mode = System.getProperty("mode", "CBC"); |
|
56 String paddingStr = System.getProperty("paddingStr", "PKCS5Padding"); |
46 byte[] input; |
57 byte[] input; |
47 byte[] encode; |
58 byte[] encode; |
48 byte[] expectedEncode; |
59 byte[] expectedEncode; |
49 byte[] decode; |
60 byte[] decode; |
50 byte[] expectedDecode; |
61 byte[] expectedDecode; |
51 Random random = new Random(0); |
62 Random random = new Random(0); |
52 Cipher cipher; |
63 Cipher cipher; |
53 Cipher dCipher; |
64 Cipher dCipher; |
54 String paddingStr = "PKCS5Padding"; |
|
55 AlgorithmParameters algParams; |
65 AlgorithmParameters algParams; |
56 SecretKey key; |
66 SecretKey key; |
57 |
67 |
58 static int numThreads = 0; |
68 static int numThreads = 0; |
59 int threadId; |
69 int threadId; |
65 |
75 |
66 abstract public void run(); |
76 abstract public void run(); |
67 |
77 |
68 public void prepare() { |
78 public void prepare() { |
69 try { |
79 try { |
70 System.out.println("\nalgorithm=" + algorithm + ", mode=" + mode + ", msgSize=" + msgSize + ", keySize=" + keySize + ", noReinit=" + noReinit + ", checkOutput=" + checkOutput); |
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; |
71 |
84 |
72 int keyLenBytes = (keySize == 0 ? 16 : keySize/8); |
85 int keyLenBytes = (keySize == 0 ? 16 : keySize/8); |
73 byte keyBytes[] = new byte[keyLenBytes]; |
86 byte keyBytes[] = new byte[keyLenBytes]; |
74 if (keySize == 128) |
87 if (keySize == 128) |
75 keyBytes = new byte[] {-8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7}; |
88 keyBytes = new byte[] {-8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7}; |
78 |
91 |
79 key = new SecretKeySpec(keyBytes, algorithm); |
92 key = new SecretKeySpec(keyBytes, algorithm); |
80 if (threadId == 0) { |
93 if (threadId == 0) { |
81 System.out.println("Algorithm: " + key.getAlgorithm() + "(" |
94 System.out.println("Algorithm: " + key.getAlgorithm() + "(" |
82 + key.getEncoded().length * 8 + "bit)"); |
95 + key.getEncoded().length * 8 + "bit)"); |
83 } |
|
84 input = new byte[msgSize]; |
|
85 for (int i=0; i<input.length; i++) { |
|
86 input[i] = (byte) (i & 0xff); |
|
87 } |
96 } |
88 |
97 |
89 cipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE"); |
98 cipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE"); |
90 dCipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE"); |
99 dCipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE"); |
91 |
100 |
101 dCipher.init(Cipher.DECRYPT_MODE, key, algParams); |
110 dCipher.init(Cipher.DECRYPT_MODE, key, algParams); |
102 if (threadId == 0) { |
111 if (threadId == 0) { |
103 childShowCipher(); |
112 childShowCipher(); |
104 } |
113 } |
105 |
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 } |
|
125 |
|
126 input = new byte[inputLength]; |
|
127 for (int i=encInputOffset, j=0; i<inputLength; i++, j++) { |
|
128 input[i] = (byte) (j & 0xff); |
|
129 } |
|
130 |
106 // do one encode and decode in preparation |
131 // do one encode and decode in preparation |
107 // this will also create the encode buffer and decode buffer |
132 encode = new byte[encodeLength]; |
108 encode = cipher.doFinal(input); |
133 decode = new byte[decodeLength]; |
109 decode = dCipher.doFinal(encode); |
134 if (testingMisalignment) { |
|
135 decodeMsgSize = cipher.update(input, encInputOffset, (msgSize - lastChunkSize), encode, encOutputOffset); |
|
136 decodeMsgSize += cipher.doFinal(input, (encInputOffset + msgSize - lastChunkSize), lastChunkSize, encode, (encOutputOffset + decodeMsgSize)); |
|
137 |
|
138 int tempSize = dCipher.update(encode, encOutputOffset, (decodeMsgSize - lastChunkSize), decode, decOutputOffset); |
|
139 dCipher.doFinal(encode, (encOutputOffset + decodeMsgSize - lastChunkSize), lastChunkSize, decode, (decOutputOffset + tempSize)); |
|
140 } else { |
|
141 decodeMsgSize = cipher.doFinal(input, encInputOffset, msgSize, encode, encOutputOffset); |
|
142 dCipher.doFinal(encode, encOutputOffset, decodeMsgSize, decode, decOutputOffset); |
|
143 } |
110 if (checkOutput) { |
144 if (checkOutput) { |
111 expectedEncode = (byte[]) encode.clone(); |
145 expectedEncode = (byte[]) encode.clone(); |
112 expectedDecode = (byte[]) decode.clone(); |
146 expectedDecode = (byte[]) decode.clone(); |
113 showArray(key.getEncoded() , "key: "); |
147 showArray(key.getEncoded() , "key: "); |
114 showArray(input, "input: "); |
148 showArray(input, "input: "); |