author | mbalao |
Tue, 12 Nov 2019 00:30:55 -0300 | |
changeset 59158 | 438337c846fb |
parent 51052 | 080776992b29 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
51052
080776992b29
8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package com.sun.crypto.provider; |
|
27 |
||
28 |
import java.security.InvalidKeyException; |
|
28241
b7b95c178b76
8049312: AES/CICO test failed with on several modes
valeriep
parents:
25859
diff
changeset
|
29 |
import java.security.ProviderException; |
31671
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
28241
diff
changeset
|
30 |
import java.util.Objects; |
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
28241
diff
changeset
|
31 |
|
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
28241
diff
changeset
|
32 |
import jdk.internal.HotSpotIntrinsicCandidate; |
51052
080776992b29
8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
47216
diff
changeset
|
33 |
import sun.security.util.ArrayUtil; |
28241
b7b95c178b76
8049312: AES/CICO test failed with on several modes
valeriep
parents:
25859
diff
changeset
|
34 |
|
2 | 35 |
|
36 |
/** |
|
37 |
* This class represents ciphers in cipher block chaining (CBC) mode. |
|
38 |
* |
|
39 |
* <p>This mode is implemented independently of a particular cipher. |
|
40 |
* Ciphers to which this mode should apply (e.g., DES) must be |
|
41 |
* <i>plugged-in</i> using the constructor. |
|
42 |
* |
|
43 |
* <p>NOTE: This class does not deal with buffering or padding. |
|
44 |
* |
|
45 |
* @author Gigi Ankeny |
|
46 |
*/ |
|
47 |
||
48 |
class CipherBlockChaining extends FeedbackCipher { |
|
49 |
||
50 |
/* |
|
51 |
* random bytes that are initialized with iv |
|
52 |
*/ |
|
53 |
protected byte[] r; |
|
54 |
||
55 |
/* |
|
56 |
* output buffer |
|
57 |
*/ |
|
58 |
private byte[] k; |
|
59 |
||
60 |
// variables for save/restore calls |
|
61 |
private byte[] rSave = null; |
|
62 |
||
63 |
CipherBlockChaining(SymmetricCipher embeddedCipher) { |
|
64 |
super(embeddedCipher); |
|
65 |
k = new byte[blockSize]; |
|
66 |
r = new byte[blockSize]; |
|
67 |
} |
|
68 |
||
69 |
/** |
|
70 |
* Gets the name of this feedback mode. |
|
71 |
* |
|
72 |
* @return the string <code>CBC</code> |
|
73 |
*/ |
|
74 |
String getFeedback() { |
|
75 |
return "CBC"; |
|
76 |
} |
|
77 |
||
78 |
/** |
|
79 |
* Initializes the cipher in the specified mode with the given key |
|
80 |
* and iv. |
|
81 |
* |
|
82 |
* @param decrypting flag indicating encryption or decryption |
|
83 |
* @param algorithm the algorithm name |
|
84 |
* @param key the key |
|
85 |
* @param iv the iv |
|
86 |
* |
|
87 |
* @exception InvalidKeyException if the given key is inappropriate for |
|
88 |
* initializing this cipher |
|
89 |
*/ |
|
90 |
void init(boolean decrypting, String algorithm, byte[] key, byte[] iv) |
|
91 |
throws InvalidKeyException { |
|
92 |
if ((key == null) || (iv == null) || (iv.length != blockSize)) { |
|
93 |
throw new InvalidKeyException("Internal error"); |
|
94 |
} |
|
95 |
this.iv = iv; |
|
96 |
reset(); |
|
97 |
embeddedCipher.init(decrypting, algorithm, key); |
|
98 |
} |
|
99 |
||
100 |
/** |
|
101 |
* Resets the iv to its original value. |
|
102 |
* This is used when doFinal is called in the Cipher class, so that the |
|
103 |
* cipher can be reused (with its original iv). |
|
104 |
*/ |
|
105 |
void reset() { |
|
106 |
System.arraycopy(iv, 0, r, 0, blockSize); |
|
107 |
} |
|
108 |
||
109 |
/** |
|
110 |
* Save the current content of this cipher. |
|
111 |
*/ |
|
112 |
void save() { |
|
113 |
if (rSave == null) { |
|
114 |
rSave = new byte[blockSize]; |
|
115 |
} |
|
116 |
System.arraycopy(r, 0, rSave, 0, blockSize); |
|
117 |
} |
|
118 |
||
119 |
/** |
|
120 |
* Restores the content of this cipher to the previous saved one. |
|
121 |
*/ |
|
122 |
void restore() { |
|
123 |
System.arraycopy(rSave, 0, r, 0, blockSize); |
|
124 |
} |
|
125 |
||
126 |
/** |
|
127 |
* Performs encryption operation. |
|
128 |
* |
|
129 |
* <p>The input plain text <code>plain</code>, starting at |
|
130 |
* <code>plainOffset</code> and ending at |
|
28241
b7b95c178b76
8049312: AES/CICO test failed with on several modes
valeriep
parents:
25859
diff
changeset
|
131 |
* <code>(plainOffset + plainLen - 1)</code>, is encrypted. |
2 | 132 |
* The result is stored in <code>cipher</code>, starting at |
133 |
* <code>cipherOffset</code>. |
|
134 |
* |
|
135 |
* @param plain the buffer with the input data to be encrypted |
|
136 |
* @param plainOffset the offset in <code>plain</code> |
|
137 |
* @param plainLen the length of the input data |
|
138 |
* @param cipher the buffer for the result |
|
139 |
* @param cipherOffset the offset in <code>cipher</code> |
|
28241
b7b95c178b76
8049312: AES/CICO test failed with on several modes
valeriep
parents:
25859
diff
changeset
|
140 |
* @exception ProviderException if <code>len</code> is not |
b7b95c178b76
8049312: AES/CICO test failed with on several modes
valeriep
parents:
25859
diff
changeset
|
141 |
* a multiple of the block size |
20752 | 142 |
* @return the length of the encrypted data |
2 | 143 |
*/ |
20752 | 144 |
int encrypt(byte[] plain, int plainOffset, int plainLen, |
31671
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
28241
diff
changeset
|
145 |
byte[] cipher, int cipherOffset) { |
42679
25fec8839946
8159035: com/sun/crypto/provider/Cipher/CTS/CTSMode.java test crashed due to unhandled case of cipher length value as 0
rraghavan
parents:
31671
diff
changeset
|
146 |
if (plainLen <= 0) { |
25fec8839946
8159035: com/sun/crypto/provider/Cipher/CTS/CTSMode.java test crashed due to unhandled case of cipher length value as 0
rraghavan
parents:
31671
diff
changeset
|
147 |
return plainLen; |
25fec8839946
8159035: com/sun/crypto/provider/Cipher/CTS/CTSMode.java test crashed due to unhandled case of cipher length value as 0
rraghavan
parents:
31671
diff
changeset
|
148 |
} |
51052
080776992b29
8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
47216
diff
changeset
|
149 |
ArrayUtil.blockSizeCheck(plainLen, blockSize); |
080776992b29
8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
47216
diff
changeset
|
150 |
ArrayUtil.nullAndBoundsCheck(plain, plainOffset, plainLen); |
080776992b29
8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
47216
diff
changeset
|
151 |
ArrayUtil.nullAndBoundsCheck(cipher, cipherOffset, plainLen); |
31671
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
28241
diff
changeset
|
152 |
return implEncrypt(plain, plainOffset, plainLen, |
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
28241
diff
changeset
|
153 |
cipher, cipherOffset); |
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
28241
diff
changeset
|
154 |
} |
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
28241
diff
changeset
|
155 |
|
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
28241
diff
changeset
|
156 |
@HotSpotIntrinsicCandidate |
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
28241
diff
changeset
|
157 |
private int implEncrypt(byte[] plain, int plainOffset, int plainLen, |
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
28241
diff
changeset
|
158 |
byte[] cipher, int cipherOffset) |
2 | 159 |
{ |
160 |
int endIndex = plainOffset + plainLen; |
|
161 |
||
162 |
for (; plainOffset < endIndex; |
|
31671
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
28241
diff
changeset
|
163 |
plainOffset += blockSize, cipherOffset += blockSize) { |
28241
b7b95c178b76
8049312: AES/CICO test failed with on several modes
valeriep
parents:
25859
diff
changeset
|
164 |
for (int i = 0; i < blockSize; i++) { |
b7b95c178b76
8049312: AES/CICO test failed with on several modes
valeriep
parents:
25859
diff
changeset
|
165 |
k[i] = (byte)(plain[i + plainOffset] ^ r[i]); |
2 | 166 |
} |
167 |
embeddedCipher.encryptBlock(k, 0, cipher, cipherOffset); |
|
168 |
System.arraycopy(cipher, cipherOffset, r, 0, blockSize); |
|
169 |
} |
|
20752 | 170 |
return plainLen; |
2 | 171 |
} |
172 |
||
173 |
/** |
|
174 |
* Performs decryption operation. |
|
175 |
* |
|
176 |
* <p>The input cipher text <code>cipher</code>, starting at |
|
177 |
* <code>cipherOffset</code> and ending at |
|
28241
b7b95c178b76
8049312: AES/CICO test failed with on several modes
valeriep
parents:
25859
diff
changeset
|
178 |
* <code>(cipherOffset + cipherLen - 1)</code>, is decrypted. |
2 | 179 |
* The result is stored in <code>plain</code>, starting at |
180 |
* <code>plainOffset</code>. |
|
181 |
* |
|
182 |
* <p>It is also the application's responsibility to make sure that |
|
183 |
* <code>init</code> has been called before this method is called. |
|
184 |
* (This check is omitted here, to avoid double checking.) |
|
185 |
* |
|
186 |
* @param cipher the buffer with the input data to be decrypted |
|
187 |
* @param cipherOffset the offset in <code>cipherOffset</code> |
|
188 |
* @param cipherLen the length of the input data |
|
189 |
* @param plain the buffer for the result |
|
190 |
* @param plainOffset the offset in <code>plain</code> |
|
28241
b7b95c178b76
8049312: AES/CICO test failed with on several modes
valeriep
parents:
25859
diff
changeset
|
191 |
* @exception ProviderException if <code>len</code> is not |
b7b95c178b76
8049312: AES/CICO test failed with on several modes
valeriep
parents:
25859
diff
changeset
|
192 |
* a multiple of the block size |
20752 | 193 |
* @return the length of the decrypted data |
2 | 194 |
*/ |
20752 | 195 |
int decrypt(byte[] cipher, int cipherOffset, int cipherLen, |
31671
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
28241
diff
changeset
|
196 |
byte[] plain, int plainOffset) { |
42679
25fec8839946
8159035: com/sun/crypto/provider/Cipher/CTS/CTSMode.java test crashed due to unhandled case of cipher length value as 0
rraghavan
parents:
31671
diff
changeset
|
197 |
if (cipherLen <= 0) { |
25fec8839946
8159035: com/sun/crypto/provider/Cipher/CTS/CTSMode.java test crashed due to unhandled case of cipher length value as 0
rraghavan
parents:
31671
diff
changeset
|
198 |
return cipherLen; |
25fec8839946
8159035: com/sun/crypto/provider/Cipher/CTS/CTSMode.java test crashed due to unhandled case of cipher length value as 0
rraghavan
parents:
31671
diff
changeset
|
199 |
} |
51052
080776992b29
8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
47216
diff
changeset
|
200 |
ArrayUtil.blockSizeCheck(cipherLen, blockSize); |
080776992b29
8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
47216
diff
changeset
|
201 |
ArrayUtil.nullAndBoundsCheck(cipher, cipherOffset, cipherLen); |
080776992b29
8179098: Crypto AES/ECB encryption/decryption performance regression (introduced in jdk9b73)
valeriep
parents:
47216
diff
changeset
|
202 |
ArrayUtil.nullAndBoundsCheck(plain, plainOffset, cipherLen); |
31671
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
28241
diff
changeset
|
203 |
return implDecrypt(cipher, cipherOffset, cipherLen, plain, plainOffset); |
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
28241
diff
changeset
|
204 |
} |
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
28241
diff
changeset
|
205 |
|
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
28241
diff
changeset
|
206 |
@HotSpotIntrinsicCandidate |
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
28241
diff
changeset
|
207 |
private int implDecrypt(byte[] cipher, int cipherOffset, int cipherLen, |
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
28241
diff
changeset
|
208 |
byte[] plain, int plainOffset) |
2 | 209 |
{ |
210 |
int endIndex = cipherOffset + cipherLen; |
|
211 |
||
212 |
for (; cipherOffset < endIndex; |
|
213 |
cipherOffset += blockSize, plainOffset += blockSize) { |
|
214 |
embeddedCipher.decryptBlock(cipher, cipherOffset, k, 0); |
|
28241
b7b95c178b76
8049312: AES/CICO test failed with on several modes
valeriep
parents:
25859
diff
changeset
|
215 |
for (int i = 0; i < blockSize; i++) { |
b7b95c178b76
8049312: AES/CICO test failed with on several modes
valeriep
parents:
25859
diff
changeset
|
216 |
plain[i + plainOffset] = (byte)(k[i] ^ r[i]); |
2 | 217 |
} |
21837
0c41fa97176a
8026943: SQE test jce/Global/Cipher/SameBuffer failed
valeriep
parents:
20752
diff
changeset
|
218 |
System.arraycopy(cipher, cipherOffset, r, 0, blockSize); |
2 | 219 |
} |
20752 | 220 |
return cipherLen; |
2 | 221 |
} |
222 |
} |