author | coffeys |
Thu, 23 Aug 2018 11:37:14 +0100 | |
changeset 51504 | c9a3e3cac9c7 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
36248
29aaf8a0d7a9
8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
25859
diff
changeset
|
2 |
* Copyright (c) 2003, 2016, 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.math.BigInteger; |
|
29 |
import java.security.*; |
|
30 |
import java.security.spec.*; |
|
31 |
import java.util.Arrays; |
|
32 |
import javax.crypto.*; |
|
33 |
import javax.crypto.spec.*; |
|
34 |
||
35 |
/** |
|
36 |
* This class implements password-base encryption algorithm with |
|
14405
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
37 |
* SHA1 digest and the following Ciphers (in CBC mode, where applicable): |
2 | 38 |
* - DESede cipher and |
14405
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
39 |
* - RC2 Cipher with 40-bit or 128-bit effective key length and |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
40 |
* - RC4 Cipher with 40-bit or 128-bit effective key length |
2 | 41 |
* as defined by PKCS #12 version 1.0 standard. |
42 |
* |
|
43 |
* @author Valerie Peng |
|
44 |
* @see javax.crypto.CipherSpi |
|
45 |
*/ |
|
46 |
final class PKCS12PBECipherCore { |
|
14405
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
47 |
|
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
48 |
// TBD: replace CipherCore with a CipherSpi object to simplify maintenance |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
49 |
|
2 | 50 |
private CipherCore cipher; |
51 |
private int blockSize; |
|
52 |
private int keySize; |
|
36248
29aaf8a0d7a9
8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
25859
diff
changeset
|
53 |
private int keyLength; // in bits |
2 | 54 |
private String algo = null; |
14405
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
55 |
private String pbeAlgo = null; |
2 | 56 |
private byte[] salt = null; |
57 |
private int iCount = 0; |
|
58 |
||
59 |
private static final int DEFAULT_SALT_LENGTH = 20; |
|
60 |
private static final int DEFAULT_COUNT = 1024; |
|
61 |
||
62 |
static final int CIPHER_KEY = 1; |
|
63 |
static final int CIPHER_IV = 2; |
|
64 |
static final int MAC_KEY = 3; |
|
65 |
||
14405
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
66 |
// Uses default hash algorithm (SHA-1) |
2 | 67 |
static byte[] derive(char[] chars, byte[] salt, |
68 |
int ic, int n, int type) { |
|
14405
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
69 |
return derive(chars, salt, ic, n, type, "SHA-1", 64); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
70 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
71 |
|
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
72 |
// Uses supplied hash algorithm |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
73 |
static byte[] derive(char[] chars, byte[] salt, int ic, int n, int type, |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
74 |
String hashAlgo, int blockLength) { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
75 |
|
11835
c9e7cfc908b3
6879539: enable empty password support for pkcs12 keystore
weijun
parents:
7043
diff
changeset
|
76 |
// Add in trailing NULL terminator. Special case: |
c9e7cfc908b3
6879539: enable empty password support for pkcs12 keystore
weijun
parents:
7043
diff
changeset
|
77 |
// no terminator if password is "\0". |
2 | 78 |
int length = chars.length*2; |
11835
c9e7cfc908b3
6879539: enable empty password support for pkcs12 keystore
weijun
parents:
7043
diff
changeset
|
79 |
if (length == 2 && chars[0] == 0) { |
c9e7cfc908b3
6879539: enable empty password support for pkcs12 keystore
weijun
parents:
7043
diff
changeset
|
80 |
chars = new char[0]; |
c9e7cfc908b3
6879539: enable empty password support for pkcs12 keystore
weijun
parents:
7043
diff
changeset
|
81 |
length = 0; |
c9e7cfc908b3
6879539: enable empty password support for pkcs12 keystore
weijun
parents:
7043
diff
changeset
|
82 |
} else { |
2 | 83 |
length += 2; |
84 |
} |
|
11835
c9e7cfc908b3
6879539: enable empty password support for pkcs12 keystore
weijun
parents:
7043
diff
changeset
|
85 |
|
2 | 86 |
byte[] passwd = new byte[length]; |
87 |
for (int i = 0, j = 0; i < chars.length; i++, j+=2) { |
|
88 |
passwd[j] = (byte) ((chars[i] >>> 8) & 0xFF); |
|
89 |
passwd[j+1] = (byte) (chars[i] & 0xFF); |
|
90 |
} |
|
91 |
byte[] key = new byte[n]; |
|
92 |
||
14405
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
93 |
try { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
94 |
MessageDigest sha = MessageDigest.getInstance(hashAlgo); |
2 | 95 |
|
14405
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
96 |
int v = blockLength; |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
97 |
int u = sha.getDigestLength(); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
98 |
int c = roundup(n, u) / u; |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
99 |
byte[] D = new byte[v]; |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
100 |
int s = roundup(salt.length, v); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
101 |
int p = roundup(passwd.length, v); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
102 |
byte[] I = new byte[s + p]; |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
103 |
|
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
104 |
Arrays.fill(D, (byte)type); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
105 |
concat(salt, I, 0, s); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
106 |
concat(passwd, I, s, p); |
51504
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
107 |
Arrays.fill(passwd, (byte) 0x00); |
14405
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
108 |
|
2 | 109 |
byte[] Ai; |
110 |
byte[] B = new byte[v]; |
|
111 |
byte[] tmp = new byte[v]; |
|
112 |
||
113 |
int i = 0; |
|
114 |
for (; ; i++, n -= u) { |
|
115 |
sha.update(D); |
|
116 |
sha.update(I); |
|
117 |
Ai = sha.digest(); |
|
118 |
for (int r = 1; r < ic; r++) |
|
119 |
Ai = sha.digest(Ai); |
|
120 |
System.arraycopy(Ai, 0, key, u * i, Math.min(n, u)); |
|
121 |
if (i + 1 == c) |
|
122 |
break; |
|
123 |
concat(Ai, B, 0, B.length); |
|
124 |
BigInteger B1; |
|
125 |
B1 = new BigInteger(1, B).add(BigInteger.ONE); |
|
126 |
||
127 |
for (int j = 0; j < I.length; j += v) { |
|
128 |
BigInteger Ij; |
|
129 |
int trunc; |
|
130 |
||
131 |
if (tmp.length != v) |
|
132 |
tmp = new byte[v]; |
|
133 |
System.arraycopy(I, j, tmp, 0, v); |
|
134 |
Ij = new BigInteger(1, tmp); |
|
135 |
Ij = Ij.add(B1); |
|
136 |
tmp = Ij.toByteArray(); |
|
137 |
trunc = tmp.length - v; |
|
138 |
if (trunc >= 0) { |
|
139 |
System.arraycopy(tmp, trunc, I, j, v); |
|
140 |
} else if (trunc < 0) { |
|
141 |
Arrays.fill(I, j, j + (-trunc), (byte)0); |
|
142 |
System.arraycopy(tmp, 0, I, j + (-trunc), tmp.length); |
|
143 |
} |
|
144 |
} |
|
145 |
} |
|
146 |
} catch (Exception e) { |
|
147 |
throw new RuntimeException("internal error: " + e); |
|
148 |
} |
|
149 |
return key; |
|
150 |
} |
|
151 |
||
152 |
private static int roundup(int x, int y) { |
|
153 |
return ((x + (y - 1)) / y) * y; |
|
154 |
} |
|
155 |
||
156 |
private static void concat(byte[] src, byte[] dst, int start, int len) { |
|
11835
c9e7cfc908b3
6879539: enable empty password support for pkcs12 keystore
weijun
parents:
7043
diff
changeset
|
157 |
if (src.length == 0) { |
c9e7cfc908b3
6879539: enable empty password support for pkcs12 keystore
weijun
parents:
7043
diff
changeset
|
158 |
return; |
c9e7cfc908b3
6879539: enable empty password support for pkcs12 keystore
weijun
parents:
7043
diff
changeset
|
159 |
} |
2 | 160 |
int loop = len / src.length; |
161 |
int off, i; |
|
162 |
for (i = 0, off = 0; i < loop; i++, off += src.length) |
|
163 |
System.arraycopy(src, 0, dst, off + start, src.length); |
|
164 |
System.arraycopy(src, 0, dst, off + start, len - off); |
|
165 |
} |
|
166 |
||
167 |
PKCS12PBECipherCore(String symmCipherAlg, int defKeySize) |
|
168 |
throws NoSuchAlgorithmException { |
|
14405
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
169 |
|
2 | 170 |
algo = symmCipherAlg; |
36248
29aaf8a0d7a9
8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
25859
diff
changeset
|
171 |
keyLength = defKeySize * 8; |
14405
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
172 |
if (algo.equals("RC4")) { |
36248
29aaf8a0d7a9
8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
25859
diff
changeset
|
173 |
pbeAlgo = "PBEWithSHA1AndRC4_" + keyLength; |
2 | 174 |
} else { |
14405
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
175 |
SymmetricCipher symmCipher = null; |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
176 |
if (algo.equals("DESede")) { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
177 |
symmCipher = new DESedeCrypt(); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
178 |
pbeAlgo = "PBEWithSHA1AndDESede"; |
36248
29aaf8a0d7a9
8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
25859
diff
changeset
|
179 |
keyLength = 112; // effective key length |
14405
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
180 |
} else if (algo.equals("RC2")) { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
181 |
symmCipher = new RC2Crypt(); |
36248
29aaf8a0d7a9
8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
25859
diff
changeset
|
182 |
pbeAlgo = "PBEWithSHA1AndRC2_" + keyLength; |
14405
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
183 |
} else { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
184 |
throw new NoSuchAlgorithmException("No Cipher implementation " + |
2 | 185 |
"for PBEWithSHA1And" + algo); |
14405
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
186 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
187 |
blockSize = symmCipher.getBlockSize(); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
188 |
cipher = new CipherCore(symmCipher, blockSize); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
189 |
cipher.setMode("CBC"); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
190 |
try { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
191 |
cipher.setPadding("PKCS5Padding"); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
192 |
} catch (NoSuchPaddingException nspe) { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
193 |
// should not happen |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
194 |
} |
2 | 195 |
} |
196 |
keySize = defKeySize; |
|
197 |
} |
|
198 |
||
199 |
void implSetMode(String mode) throws NoSuchAlgorithmException { |
|
200 |
if ((mode != null) && (!mode.equalsIgnoreCase("CBC"))) { |
|
201 |
throw new NoSuchAlgorithmException("Invalid cipher mode: " |
|
202 |
+ mode); |
|
203 |
} |
|
204 |
} |
|
205 |
||
206 |
void implSetPadding(String padding) throws NoSuchPaddingException { |
|
207 |
if ((padding != null) && |
|
208 |
(!padding.equalsIgnoreCase("PKCS5Padding"))) { |
|
209 |
throw new NoSuchPaddingException("Invalid padding scheme: " + |
|
210 |
padding); |
|
211 |
} |
|
212 |
} |
|
213 |
||
214 |
int implGetBlockSize() { |
|
215 |
return blockSize; |
|
216 |
} |
|
217 |
||
218 |
int implGetOutputSize(int inLen) { |
|
219 |
return cipher.getOutputSize(inLen); |
|
220 |
} |
|
221 |
||
222 |
byte[] implGetIV() { |
|
223 |
return cipher.getIV(); |
|
224 |
} |
|
225 |
||
226 |
AlgorithmParameters implGetParameters() { |
|
227 |
AlgorithmParameters params = null; |
|
228 |
if (salt == null) { |
|
229 |
// Cipher is not initialized with parameters; |
|
230 |
// follow the recommendation in PKCS12 v1.0 |
|
231 |
// section B.4 to generate salt and iCount. |
|
232 |
salt = new byte[DEFAULT_SALT_LENGTH]; |
|
15010 | 233 |
SunJCE.getRandom().nextBytes(salt); |
2 | 234 |
iCount = DEFAULT_COUNT; |
235 |
} |
|
236 |
PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, iCount); |
|
237 |
try { |
|
16909
78a1749a43e2
7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents:
15010
diff
changeset
|
238 |
params = AlgorithmParameters.getInstance(pbeAlgo, |
78a1749a43e2
7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents:
15010
diff
changeset
|
239 |
SunJCE.getInstance()); |
78a1749a43e2
7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents:
15010
diff
changeset
|
240 |
params.init(pbeSpec); |
78a1749a43e2
7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents:
15010
diff
changeset
|
241 |
} catch (NoSuchAlgorithmException nsae) { |
2 | 242 |
// should never happen |
7043 | 243 |
throw new RuntimeException( |
244 |
"SunJCE provider is not configured properly"); |
|
2 | 245 |
} catch (InvalidParameterSpecException ipse) { |
246 |
// should never happen |
|
247 |
throw new RuntimeException("PBEParameterSpec not supported"); |
|
248 |
} |
|
249 |
return params; |
|
250 |
} |
|
251 |
||
252 |
void implInit(int opmode, Key key, AlgorithmParameterSpec params, |
|
253 |
SecureRandom random) throws InvalidKeyException, |
|
254 |
InvalidAlgorithmParameterException { |
|
14405
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
255 |
implInit(opmode, key, params, random, null); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
256 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
257 |
|
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
258 |
void implInit(int opmode, Key key, AlgorithmParameterSpec params, |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
259 |
SecureRandom random, CipherSpi cipherImpl) |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
260 |
throws InvalidKeyException, |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
261 |
InvalidAlgorithmParameterException { |
2 | 262 |
char[] passwdChars = null; |
263 |
salt = null; |
|
264 |
iCount = 0; |
|
265 |
if (key instanceof javax.crypto.interfaces.PBEKey) { |
|
266 |
javax.crypto.interfaces.PBEKey pbeKey = |
|
267 |
(javax.crypto.interfaces.PBEKey) key; |
|
268 |
passwdChars = pbeKey.getPassword(); |
|
269 |
salt = pbeKey.getSalt(); // maybe null if unspecified |
|
270 |
iCount = pbeKey.getIterationCount(); // maybe 0 if unspecified |
|
271 |
} else if (key instanceof SecretKey) { |
|
51504
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
272 |
byte[] passwdBytes; |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
273 |
if (!(key.getAlgorithm().regionMatches(true, 0, "PBE", 0, 3)) || |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
274 |
(passwdBytes = key.getEncoded()) == null) { |
2 | 275 |
throw new InvalidKeyException("Missing password"); |
276 |
} |
|
277 |
passwdChars = new char[passwdBytes.length]; |
|
278 |
for (int i=0; i<passwdChars.length; i++) { |
|
279 |
passwdChars[i] = (char) (passwdBytes[i] & 0x7f); |
|
280 |
} |
|
51504
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
281 |
Arrays.fill(passwdBytes, (byte)0x00); |
2 | 282 |
} else { |
283 |
throw new InvalidKeyException("SecretKey of PBE type required"); |
|
284 |
} |
|
285 |
||
51504
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
286 |
try { |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
287 |
if (((opmode == Cipher.DECRYPT_MODE) || |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
288 |
(opmode == Cipher.UNWRAP_MODE)) && |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
289 |
((params == null) && ((salt == null) || (iCount == 0)))) { |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
290 |
throw new InvalidAlgorithmParameterException |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
291 |
("Parameters missing"); |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
292 |
} |
2 | 293 |
|
51504
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
294 |
if (params == null) { |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
295 |
// generate default for salt and iteration count if necessary |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
296 |
if (salt == null) { |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
297 |
salt = new byte[DEFAULT_SALT_LENGTH]; |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
298 |
if (random != null) { |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
299 |
random.nextBytes(salt); |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
300 |
} else { |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
301 |
SunJCE.getRandom().nextBytes(salt); |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
302 |
} |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
303 |
} |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
304 |
if (iCount == 0) iCount = DEFAULT_COUNT; |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
305 |
} else if (!(params instanceof PBEParameterSpec)) { |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
306 |
throw new InvalidAlgorithmParameterException |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
307 |
("PBEParameterSpec type required"); |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
308 |
} else { |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
309 |
PBEParameterSpec pbeParams = (PBEParameterSpec) params; |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
310 |
// make sure the parameter values are consistent |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
311 |
if (salt != null) { |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
312 |
if (!Arrays.equals(salt, pbeParams.getSalt())) { |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
313 |
throw new InvalidAlgorithmParameterException |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
314 |
("Inconsistent value of salt between key and params"); |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
315 |
} |
2 | 316 |
} else { |
51504
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
317 |
salt = pbeParams.getSalt(); |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
318 |
} |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
319 |
if (iCount != 0) { |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
320 |
if (iCount != pbeParams.getIterationCount()) { |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
321 |
throw new InvalidAlgorithmParameterException |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
322 |
("Different iteration count between key and params"); |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
323 |
} |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
324 |
} else { |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
325 |
iCount = pbeParams.getIterationCount(); |
2 | 326 |
} |
327 |
} |
|
51504
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
328 |
// salt is recommended to be ideally as long as the output |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
329 |
// of the hash function. However, it may be too strict to |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
330 |
// force this; so instead, we'll just require the minimum |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
331 |
// salt length to be 8-byte which is what PKCS#5 recommends |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
332 |
// and openssl does. |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
333 |
if (salt.length < 8) { |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
334 |
throw new InvalidAlgorithmParameterException |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
335 |
("Salt must be at least 8 bytes long"); |
2 | 336 |
} |
51504
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
337 |
if (iCount <= 0) { |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
338 |
throw new InvalidAlgorithmParameterException |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
339 |
("IterationCount must be a positive number"); |
2 | 340 |
} |
51504
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
341 |
byte[] derivedKey = derive(passwdChars, salt, iCount, |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
342 |
keySize, CIPHER_KEY); |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
343 |
SecretKey cipherKey = new SecretKeySpec(derivedKey, algo); |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
344 |
|
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
345 |
if (cipherImpl != null && cipherImpl instanceof ARCFOURCipher) { |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
346 |
((ARCFOURCipher)cipherImpl).engineInit(opmode, cipherKey, random); |
14405
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
347 |
|
51504
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
348 |
} else { |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
349 |
byte[] derivedIv = derive(passwdChars, salt, iCount, 8, |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
350 |
CIPHER_IV); |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
351 |
IvParameterSpec ivSpec = new IvParameterSpec(derivedIv, 0, 8); |
14405
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
352 |
|
51504
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
353 |
// initialize the underlying cipher |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
354 |
cipher.init(opmode, cipherKey, ivSpec, random); |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
355 |
} |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
356 |
} finally { |
c9a3e3cac9c7
8209129: Further improvements to cipher buffer management
coffeys
parents:
47216
diff
changeset
|
357 |
Arrays.fill(passwdChars, '\0'); |
14405
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
358 |
} |
2 | 359 |
} |
360 |
||
361 |
void implInit(int opmode, Key key, AlgorithmParameters params, |
|
362 |
SecureRandom random) |
|
363 |
throws InvalidKeyException, InvalidAlgorithmParameterException { |
|
14405
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
364 |
implInit(opmode, key, params, random, null); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
365 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
366 |
|
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
367 |
void implInit(int opmode, Key key, AlgorithmParameters params, |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
368 |
SecureRandom random, CipherSpi cipherImpl) |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
369 |
throws InvalidKeyException, InvalidAlgorithmParameterException { |
2 | 370 |
AlgorithmParameterSpec paramSpec = null; |
371 |
if (params != null) { |
|
372 |
try { |
|
373 |
paramSpec = params.getParameterSpec(PBEParameterSpec.class); |
|
374 |
} catch (InvalidParameterSpecException ipse) { |
|
7043 | 375 |
throw new InvalidAlgorithmParameterException( |
376 |
"requires PBE parameters"); |
|
2 | 377 |
} |
378 |
} |
|
14405
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
379 |
implInit(opmode, key, paramSpec, random, cipherImpl); |
2 | 380 |
} |
381 |
||
382 |
void implInit(int opmode, Key key, SecureRandom random) |
|
383 |
throws InvalidKeyException { |
|
14405
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
384 |
implInit(opmode, key, random, null); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
385 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
386 |
|
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
387 |
void implInit(int opmode, Key key, SecureRandom random, |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
388 |
CipherSpi cipherImpl) throws InvalidKeyException { |
2 | 389 |
try { |
14405
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
390 |
implInit(opmode, key, (AlgorithmParameterSpec) null, random, |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
391 |
cipherImpl); |
2 | 392 |
} catch (InvalidAlgorithmParameterException iape) { |
393 |
throw new InvalidKeyException("requires PBE parameters"); |
|
394 |
} |
|
395 |
} |
|
396 |
||
397 |
byte[] implUpdate(byte[] in, int inOff, int inLen) { |
|
398 |
return cipher.update(in, inOff, inLen); |
|
399 |
} |
|
400 |
||
401 |
int implUpdate(byte[] in, int inOff, int inLen, byte[] out, int outOff) |
|
402 |
throws ShortBufferException { |
|
403 |
return cipher.update(in, inOff, inLen, out, outOff); |
|
404 |
} |
|
405 |
||
406 |
byte[] implDoFinal(byte[] in, int inOff, int inLen) |
|
407 |
throws IllegalBlockSizeException, BadPaddingException { |
|
408 |
return cipher.doFinal(in, inOff, inLen); |
|
409 |
} |
|
410 |
||
411 |
int implDoFinal(byte[] in, int inOff, int inLen, byte[] out, int outOff) |
|
412 |
throws ShortBufferException, IllegalBlockSizeException, |
|
413 |
BadPaddingException { |
|
414 |
return cipher.doFinal(in, inOff, inLen, out, outOff); |
|
415 |
} |
|
416 |
||
417 |
int implGetKeySize(Key key) throws InvalidKeyException { |
|
36248
29aaf8a0d7a9
8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
25859
diff
changeset
|
418 |
return keyLength; |
2 | 419 |
} |
420 |
||
421 |
byte[] implWrap(Key key) throws IllegalBlockSizeException, |
|
422 |
InvalidKeyException { |
|
423 |
return cipher.wrap(key); |
|
424 |
} |
|
425 |
||
426 |
Key implUnwrap(byte[] wrappedKey, String wrappedKeyAlgorithm, |
|
427 |
int wrappedKeyType) |
|
428 |
throws InvalidKeyException, NoSuchAlgorithmException { |
|
429 |
return cipher.unwrap(wrappedKey, wrappedKeyAlgorithm, |
|
430 |
wrappedKeyType); |
|
431 |
} |
|
432 |
||
433 |
public static final class PBEWithSHA1AndDESede extends CipherSpi { |
|
434 |
private final PKCS12PBECipherCore core; |
|
435 |
public PBEWithSHA1AndDESede() throws NoSuchAlgorithmException { |
|
436 |
core = new PKCS12PBECipherCore("DESede", 24); |
|
437 |
} |
|
438 |
protected byte[] engineDoFinal(byte[] in, int inOff, int inLen) |
|
439 |
throws IllegalBlockSizeException, BadPaddingException { |
|
440 |
return core.implDoFinal(in, inOff, inLen); |
|
441 |
} |
|
442 |
protected int engineDoFinal(byte[] in, int inOff, int inLen, |
|
443 |
byte[] out, int outOff) |
|
444 |
throws ShortBufferException, IllegalBlockSizeException, |
|
445 |
BadPaddingException { |
|
446 |
return core.implDoFinal(in, inOff, inLen, out, outOff); |
|
447 |
} |
|
448 |
protected int engineGetBlockSize() { |
|
449 |
return core.implGetBlockSize(); |
|
450 |
} |
|
451 |
protected byte[] engineGetIV() { |
|
452 |
return core.implGetIV(); |
|
453 |
} |
|
454 |
protected int engineGetKeySize(Key key) throws InvalidKeyException { |
|
455 |
return core.implGetKeySize(key); |
|
456 |
} |
|
457 |
protected int engineGetOutputSize(int inLen) { |
|
458 |
return core.implGetOutputSize(inLen); |
|
459 |
} |
|
460 |
protected AlgorithmParameters engineGetParameters() { |
|
461 |
return core.implGetParameters(); |
|
462 |
} |
|
463 |
protected void engineInit(int opmode, Key key, |
|
464 |
AlgorithmParameterSpec params, |
|
465 |
SecureRandom random) |
|
466 |
throws InvalidKeyException, InvalidAlgorithmParameterException { |
|
467 |
core.implInit(opmode, key, params, random); |
|
468 |
} |
|
469 |
protected void engineInit(int opmode, Key key, |
|
470 |
AlgorithmParameters params, |
|
471 |
SecureRandom random) |
|
472 |
throws InvalidKeyException, InvalidAlgorithmParameterException { |
|
473 |
core.implInit(opmode, key, params, random); |
|
474 |
} |
|
475 |
protected void engineInit(int opmode, Key key, SecureRandom random) |
|
476 |
throws InvalidKeyException { |
|
477 |
core.implInit(opmode, key, random); |
|
478 |
} |
|
479 |
protected void engineSetMode(String mode) |
|
480 |
throws NoSuchAlgorithmException { |
|
481 |
core.implSetMode(mode); |
|
482 |
} |
|
483 |
protected void engineSetPadding(String paddingScheme) |
|
484 |
throws NoSuchPaddingException { |
|
485 |
core.implSetPadding(paddingScheme); |
|
486 |
} |
|
487 |
protected Key engineUnwrap(byte[] wrappedKey, |
|
488 |
String wrappedKeyAlgorithm, |
|
489 |
int wrappedKeyType) |
|
490 |
throws InvalidKeyException, NoSuchAlgorithmException { |
|
491 |
return core.implUnwrap(wrappedKey, wrappedKeyAlgorithm, |
|
492 |
wrappedKeyType); |
|
493 |
} |
|
494 |
protected byte[] engineUpdate(byte[] in, int inOff, int inLen) { |
|
495 |
return core.implUpdate(in, inOff, inLen); |
|
496 |
} |
|
497 |
protected int engineUpdate(byte[] in, int inOff, int inLen, |
|
498 |
byte[] out, int outOff) |
|
499 |
throws ShortBufferException { |
|
500 |
return core.implUpdate(in, inOff, inLen, out, outOff); |
|
501 |
} |
|
502 |
protected byte[] engineWrap(Key key) |
|
503 |
throws IllegalBlockSizeException, InvalidKeyException { |
|
504 |
return core.implWrap(key); |
|
505 |
} |
|
506 |
} |
|
507 |
||
508 |
public static final class PBEWithSHA1AndRC2_40 extends CipherSpi { |
|
509 |
private final PKCS12PBECipherCore core; |
|
510 |
public PBEWithSHA1AndRC2_40() throws NoSuchAlgorithmException { |
|
511 |
core = new PKCS12PBECipherCore("RC2", 5); |
|
512 |
} |
|
513 |
protected byte[] engineDoFinal(byte[] in, int inOff, int inLen) |
|
514 |
throws IllegalBlockSizeException, BadPaddingException { |
|
515 |
return core.implDoFinal(in, inOff, inLen); |
|
516 |
} |
|
517 |
protected int engineDoFinal(byte[] in, int inOff, int inLen, |
|
518 |
byte[] out, int outOff) |
|
519 |
throws ShortBufferException, IllegalBlockSizeException, |
|
520 |
BadPaddingException { |
|
521 |
return core.implDoFinal(in, inOff, inLen, out, outOff); |
|
522 |
} |
|
523 |
protected int engineGetBlockSize() { |
|
524 |
return core.implGetBlockSize(); |
|
525 |
} |
|
526 |
protected byte[] engineGetIV() { |
|
527 |
return core.implGetIV(); |
|
528 |
} |
|
529 |
protected int engineGetKeySize(Key key) throws InvalidKeyException { |
|
530 |
return core.implGetKeySize(key); |
|
531 |
} |
|
532 |
protected int engineGetOutputSize(int inLen) { |
|
533 |
return core.implGetOutputSize(inLen); |
|
534 |
} |
|
535 |
protected AlgorithmParameters engineGetParameters() { |
|
536 |
return core.implGetParameters(); |
|
537 |
} |
|
538 |
protected void engineInit(int opmode, Key key, |
|
539 |
AlgorithmParameterSpec params, |
|
540 |
SecureRandom random) |
|
541 |
throws InvalidKeyException, InvalidAlgorithmParameterException { |
|
542 |
core.implInit(opmode, key, params, random); |
|
543 |
} |
|
544 |
protected void engineInit(int opmode, Key key, |
|
545 |
AlgorithmParameters params, |
|
546 |
SecureRandom random) |
|
547 |
throws InvalidKeyException, InvalidAlgorithmParameterException { |
|
548 |
core.implInit(opmode, key, params, random); |
|
549 |
} |
|
550 |
protected void engineInit(int opmode, Key key, SecureRandom random) |
|
551 |
throws InvalidKeyException { |
|
552 |
core.implInit(opmode, key, random); |
|
553 |
} |
|
554 |
protected void engineSetMode(String mode) |
|
555 |
throws NoSuchAlgorithmException { |
|
556 |
core.implSetMode(mode); |
|
557 |
} |
|
558 |
protected void engineSetPadding(String paddingScheme) |
|
559 |
throws NoSuchPaddingException { |
|
560 |
core.implSetPadding(paddingScheme); |
|
561 |
} |
|
562 |
protected Key engineUnwrap(byte[] wrappedKey, |
|
563 |
String wrappedKeyAlgorithm, |
|
564 |
int wrappedKeyType) |
|
565 |
throws InvalidKeyException, NoSuchAlgorithmException { |
|
566 |
return core.implUnwrap(wrappedKey, wrappedKeyAlgorithm, |
|
567 |
wrappedKeyType); |
|
568 |
} |
|
569 |
protected byte[] engineUpdate(byte[] in, int inOff, int inLen) { |
|
570 |
return core.implUpdate(in, inOff, inLen); |
|
571 |
} |
|
572 |
protected int engineUpdate(byte[] in, int inOff, int inLen, |
|
573 |
byte[] out, int outOff) |
|
574 |
throws ShortBufferException { |
|
575 |
return core.implUpdate(in, inOff, inLen, out, outOff); |
|
576 |
} |
|
577 |
protected byte[] engineWrap(Key key) |
|
578 |
throws IllegalBlockSizeException, InvalidKeyException { |
|
579 |
return core.implWrap(key); |
|
580 |
} |
|
581 |
} |
|
14405
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
582 |
|
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
583 |
public static final class PBEWithSHA1AndRC2_128 extends CipherSpi { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
584 |
private final PKCS12PBECipherCore core; |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
585 |
public PBEWithSHA1AndRC2_128() throws NoSuchAlgorithmException { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
586 |
core = new PKCS12PBECipherCore("RC2", 16); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
587 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
588 |
protected byte[] engineDoFinal(byte[] in, int inOff, int inLen) |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
589 |
throws IllegalBlockSizeException, BadPaddingException { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
590 |
return core.implDoFinal(in, inOff, inLen); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
591 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
592 |
protected int engineDoFinal(byte[] in, int inOff, int inLen, |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
593 |
byte[] out, int outOff) |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
594 |
throws ShortBufferException, IllegalBlockSizeException, |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
595 |
BadPaddingException { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
596 |
return core.implDoFinal(in, inOff, inLen, out, outOff); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
597 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
598 |
protected int engineGetBlockSize() { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
599 |
return core.implGetBlockSize(); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
600 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
601 |
protected byte[] engineGetIV() { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
602 |
return core.implGetIV(); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
603 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
604 |
protected int engineGetKeySize(Key key) throws InvalidKeyException { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
605 |
return core.implGetKeySize(key); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
606 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
607 |
protected int engineGetOutputSize(int inLen) { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
608 |
return core.implGetOutputSize(inLen); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
609 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
610 |
protected AlgorithmParameters engineGetParameters() { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
611 |
return core.implGetParameters(); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
612 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
613 |
protected void engineInit(int opmode, Key key, |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
614 |
AlgorithmParameterSpec params, |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
615 |
SecureRandom random) |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
616 |
throws InvalidKeyException, InvalidAlgorithmParameterException { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
617 |
core.implInit(opmode, key, params, random); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
618 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
619 |
protected void engineInit(int opmode, Key key, |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
620 |
AlgorithmParameters params, |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
621 |
SecureRandom random) |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
622 |
throws InvalidKeyException, InvalidAlgorithmParameterException { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
623 |
core.implInit(opmode, key, params, random); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
624 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
625 |
protected void engineInit(int opmode, Key key, SecureRandom random) |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
626 |
throws InvalidKeyException { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
627 |
core.implInit(opmode, key, random); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
628 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
629 |
protected void engineSetMode(String mode) |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
630 |
throws NoSuchAlgorithmException { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
631 |
core.implSetMode(mode); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
632 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
633 |
protected void engineSetPadding(String paddingScheme) |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
634 |
throws NoSuchPaddingException { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
635 |
core.implSetPadding(paddingScheme); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
636 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
637 |
protected Key engineUnwrap(byte[] wrappedKey, |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
638 |
String wrappedKeyAlgorithm, |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
639 |
int wrappedKeyType) |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
640 |
throws InvalidKeyException, NoSuchAlgorithmException { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
641 |
return core.implUnwrap(wrappedKey, wrappedKeyAlgorithm, |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
642 |
wrappedKeyType); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
643 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
644 |
protected byte[] engineUpdate(byte[] in, int inOff, int inLen) { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
645 |
return core.implUpdate(in, inOff, inLen); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
646 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
647 |
protected int engineUpdate(byte[] in, int inOff, int inLen, |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
648 |
byte[] out, int outOff) |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
649 |
throws ShortBufferException { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
650 |
return core.implUpdate(in, inOff, inLen, out, outOff); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
651 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
652 |
protected byte[] engineWrap(Key key) |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
653 |
throws IllegalBlockSizeException, InvalidKeyException { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
654 |
return core.implWrap(key); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
655 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
656 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
657 |
|
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
658 |
public static final class PBEWithSHA1AndRC4_40 extends CipherSpi { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
659 |
private static final int RC4_KEYSIZE = 5; |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
660 |
private final PKCS12PBECipherCore core; |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
661 |
private final ARCFOURCipher cipher; |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
662 |
|
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
663 |
public PBEWithSHA1AndRC4_40() throws NoSuchAlgorithmException { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
664 |
core = new PKCS12PBECipherCore("RC4", RC4_KEYSIZE); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
665 |
cipher = new ARCFOURCipher(); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
666 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
667 |
protected byte[] engineDoFinal(byte[] in, int inOff, int inLen) |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
668 |
throws IllegalBlockSizeException, BadPaddingException { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
669 |
return cipher.engineDoFinal(in, inOff, inLen); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
670 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
671 |
protected int engineDoFinal(byte[] in, int inOff, int inLen, |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
672 |
byte[] out, int outOff) |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
673 |
throws ShortBufferException, IllegalBlockSizeException, |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
674 |
BadPaddingException { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
675 |
return cipher.engineDoFinal(in, inOff, inLen, out, outOff); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
676 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
677 |
protected int engineGetBlockSize() { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
678 |
return cipher.engineGetBlockSize(); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
679 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
680 |
protected byte[] engineGetIV() { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
681 |
return cipher.engineGetIV(); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
682 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
683 |
protected int engineGetKeySize(Key key) throws InvalidKeyException { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
684 |
return RC4_KEYSIZE; |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
685 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
686 |
protected int engineGetOutputSize(int inLen) { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
687 |
return cipher.engineGetOutputSize(inLen); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
688 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
689 |
protected AlgorithmParameters engineGetParameters() { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
690 |
return core.implGetParameters(); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
691 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
692 |
protected void engineInit(int opmode, Key key, |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
693 |
AlgorithmParameterSpec params, |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
694 |
SecureRandom random) |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
695 |
throws InvalidKeyException, InvalidAlgorithmParameterException { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
696 |
core.implInit(opmode, key, params, random, cipher); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
697 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
698 |
protected void engineInit(int opmode, Key key, |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
699 |
AlgorithmParameters params, |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
700 |
SecureRandom random) |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
701 |
throws InvalidKeyException, InvalidAlgorithmParameterException { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
702 |
core.implInit(opmode, key, params, random, cipher); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
703 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
704 |
protected void engineInit(int opmode, Key key, SecureRandom random) |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
705 |
throws InvalidKeyException { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
706 |
core.implInit(opmode, key, random, cipher); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
707 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
708 |
protected void engineSetMode(String mode) |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
709 |
throws NoSuchAlgorithmException { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
710 |
if (mode.equalsIgnoreCase("ECB") == false) { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
711 |
throw new NoSuchAlgorithmException("Unsupported mode " + mode); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
712 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
713 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
714 |
protected void engineSetPadding(String paddingScheme) |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
715 |
throws NoSuchPaddingException { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
716 |
if (paddingScheme.equalsIgnoreCase("NoPadding") == false) { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
717 |
throw new NoSuchPaddingException("Padding must be NoPadding"); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
718 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
719 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
720 |
protected Key engineUnwrap(byte[] wrappedKey, |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
721 |
String wrappedKeyAlgorithm, |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
722 |
int wrappedKeyType) |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
723 |
throws InvalidKeyException, NoSuchAlgorithmException { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
724 |
return cipher.engineUnwrap(wrappedKey, wrappedKeyAlgorithm, |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
725 |
wrappedKeyType); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
726 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
727 |
protected byte[] engineUpdate(byte[] in, int inOff, int inLen) { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
728 |
return cipher.engineUpdate(in, inOff, inLen); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
729 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
730 |
protected int engineUpdate(byte[] in, int inOff, int inLen, |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
731 |
byte[] out, int outOff) |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
732 |
throws ShortBufferException { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
733 |
return cipher.engineUpdate(in, inOff, inLen, out, outOff); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
734 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
735 |
protected byte[] engineWrap(Key key) |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
736 |
throws IllegalBlockSizeException, InvalidKeyException { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
737 |
return cipher.engineWrap(key); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
738 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
739 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
740 |
|
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
741 |
public static final class PBEWithSHA1AndRC4_128 extends CipherSpi { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
742 |
private static final int RC4_KEYSIZE = 16; |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
743 |
private final PKCS12PBECipherCore core; |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
744 |
private final ARCFOURCipher cipher; |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
745 |
|
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
746 |
public PBEWithSHA1AndRC4_128() throws NoSuchAlgorithmException { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
747 |
core = new PKCS12PBECipherCore("RC4", RC4_KEYSIZE); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
748 |
cipher = new ARCFOURCipher(); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
749 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
750 |
protected byte[] engineDoFinal(byte[] in, int inOff, int inLen) |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
751 |
throws IllegalBlockSizeException, BadPaddingException { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
752 |
return cipher.engineDoFinal(in, inOff, inLen); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
753 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
754 |
protected int engineDoFinal(byte[] in, int inOff, int inLen, |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
755 |
byte[] out, int outOff) |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
756 |
throws ShortBufferException, IllegalBlockSizeException, |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
757 |
BadPaddingException { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
758 |
return cipher.engineDoFinal(in, inOff, inLen, out, outOff); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
759 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
760 |
protected int engineGetBlockSize() { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
761 |
return cipher.engineGetBlockSize(); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
762 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
763 |
protected byte[] engineGetIV() { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
764 |
return cipher.engineGetIV(); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
765 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
766 |
protected int engineGetKeySize(Key key) throws InvalidKeyException { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
767 |
return RC4_KEYSIZE; |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
768 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
769 |
protected int engineGetOutputSize(int inLen) { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
770 |
return cipher.engineGetOutputSize(inLen); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
771 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
772 |
protected AlgorithmParameters engineGetParameters() { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
773 |
return core.implGetParameters(); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
774 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
775 |
protected void engineInit(int opmode, Key key, |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
776 |
AlgorithmParameterSpec params, |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
777 |
SecureRandom random) |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
778 |
throws InvalidKeyException, InvalidAlgorithmParameterException { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
779 |
core.implInit(opmode, key, params, random, cipher); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
780 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
781 |
protected void engineInit(int opmode, Key key, |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
782 |
AlgorithmParameters params, |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
783 |
SecureRandom random) |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
784 |
throws InvalidKeyException, InvalidAlgorithmParameterException { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
785 |
core.implInit(opmode, key, params, random, cipher); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
786 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
787 |
protected void engineInit(int opmode, Key key, SecureRandom random) |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
788 |
throws InvalidKeyException { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
789 |
core.implInit(opmode, key, random, cipher); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
790 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
791 |
protected void engineSetMode(String mode) |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
792 |
throws NoSuchAlgorithmException { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
793 |
if (mode.equalsIgnoreCase("ECB") == false) { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
794 |
throw new NoSuchAlgorithmException("Unsupported mode " + mode); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
795 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
796 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
797 |
protected void engineSetPadding(String paddingScheme) |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
798 |
throws NoSuchPaddingException { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
799 |
if (paddingScheme.equalsIgnoreCase("NoPadding") == false) { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
800 |
throw new NoSuchPaddingException("Padding must be NoPadding"); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
801 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
802 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
803 |
protected Key engineUnwrap(byte[] wrappedKey, |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
804 |
String wrappedKeyAlgorithm, |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
805 |
int wrappedKeyType) |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
806 |
throws InvalidKeyException, NoSuchAlgorithmException { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
807 |
return cipher.engineUnwrap(wrappedKey, wrappedKeyAlgorithm, |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
808 |
wrappedKeyType); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
809 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
810 |
protected byte[] engineUpdate(byte[] in, int inOff, int inLen) { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
811 |
return cipher.engineUpdate(in, inOff, inLen); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
812 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
813 |
protected int engineUpdate(byte[] in, int inOff, int inLen, |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
814 |
byte[] out, int outOff) |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
815 |
throws ShortBufferException { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
816 |
return cipher.engineUpdate(in, inOff, inLen, out, outOff); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
817 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
818 |
protected byte[] engineWrap(Key key) |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
819 |
throws IllegalBlockSizeException, InvalidKeyException { |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
820 |
return cipher.engineWrap(key); |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
821 |
} |
e7fff80005c1
6383200: PBE: need new algorithm support in password based encryption
vinnie
parents:
14342
diff
changeset
|
822 |
} |
2 | 823 |
} |