161 }
162 int n = ((RSAKey)key).getModulus().bitLength();
163 // strip off the leading extra 0x00 byte prefix
164 int realByteSize = (n + 7) >> 3;
165 return realByteSize * 8;
166 }
167
168 // see JCE spec
169 @Override
170 protected synchronized void engineInit(int opmode, Key key, SecureRandom random)
171 throws InvalidKeyException {
172 try {
173 engineInit(opmode, key, (AlgorithmParameterSpec)null, random);
174 } catch (InvalidAlgorithmParameterException e) {
175 throw new InvalidKeyException("init() failed", e);
176 }
177 }
178
179 // see JCE spec
180 @Override
181 protected synchronized void engineInit(int opmode, Key newKey,
182 AlgorithmParameterSpec params, SecureRandom random)
183 throws InvalidKeyException, InvalidAlgorithmParameterException {
184 if (newKey == null) {
185 throw new InvalidKeyException("Key cannot be null");
186 }
187 if (opmode != Cipher.ENCRYPT_MODE &&
188 opmode != Cipher.DECRYPT_MODE &&
189 opmode != Cipher.WRAP_MODE &&
190 opmode != Cipher.UNWRAP_MODE) {
191 throw new InvalidAlgorithmParameterException
192 ("Unsupported mode: " + opmode);
193 }
194 if (params != null) {
195 if (!(params instanceof TlsRsaPremasterSecretParameterSpec)) {
196 throw new InvalidAlgorithmParameterException(
197 "No Parameters can be specified");
198 }
199 spec = params;
200 this.random = random; // for TLS RSA premaster secret
314 protected synchronized byte[] engineWrap(Key key) throws IllegalBlockSizeException,
315 InvalidKeyException {
316 try {
317 byte[] encodedKey = key.getEncoded();
318 if ((encodedKey == null) || (encodedKey.length == 0)) {
319 throw new InvalidKeyException("Cannot get an encoding of " +
320 "the key to be wrapped");
321 }
322 if (encodedKey.length > buffer.length) {
323 throw new InvalidKeyException("Key is too long for wrapping");
324 }
325 return engineDoFinal(encodedKey, 0, encodedKey.length);
326 } catch (BadPaddingException e) {
327 // Should never happen for key wrapping
328 throw new UcryptoException("Internal Error", e);
329 }
330 }
331
332 // see JCE spec
333 @Override
334 protected synchronized Key engineUnwrap(byte[] wrappedKey,
335 String wrappedKeyAlgorithm, int wrappedKeyType)
336 throws InvalidKeyException, NoSuchAlgorithmException {
337
338 if (wrappedKey.length > buffer.length) {
339 throw new InvalidKeyException("Key is too long for unwrapping");
340 }
341
342 boolean isTlsRsaPremasterSecret =
343 wrappedKeyAlgorithm.equals("TlsRsaPremasterSecret");
344 Exception failover = null;
345
346 byte[] encodedKey = null;
347 try {
348 encodedKey = engineDoFinal(wrappedKey, 0, wrappedKey.length);
349 } catch (BadPaddingException bpe) {
350 if (isTlsRsaPremasterSecret) {
351 failover = bpe;
352 } else {
353 throw new InvalidKeyException("Unwrapping failed", bpe);
|
161 }
162 int n = ((RSAKey)key).getModulus().bitLength();
163 // strip off the leading extra 0x00 byte prefix
164 int realByteSize = (n + 7) >> 3;
165 return realByteSize * 8;
166 }
167
168 // see JCE spec
169 @Override
170 protected synchronized void engineInit(int opmode, Key key, SecureRandom random)
171 throws InvalidKeyException {
172 try {
173 engineInit(opmode, key, (AlgorithmParameterSpec)null, random);
174 } catch (InvalidAlgorithmParameterException e) {
175 throw new InvalidKeyException("init() failed", e);
176 }
177 }
178
179 // see JCE spec
180 @Override
181 @SuppressWarnings("deprecation")
182 protected synchronized void engineInit(int opmode, Key newKey,
183 AlgorithmParameterSpec params, SecureRandom random)
184 throws InvalidKeyException, InvalidAlgorithmParameterException {
185 if (newKey == null) {
186 throw new InvalidKeyException("Key cannot be null");
187 }
188 if (opmode != Cipher.ENCRYPT_MODE &&
189 opmode != Cipher.DECRYPT_MODE &&
190 opmode != Cipher.WRAP_MODE &&
191 opmode != Cipher.UNWRAP_MODE) {
192 throw new InvalidAlgorithmParameterException
193 ("Unsupported mode: " + opmode);
194 }
195 if (params != null) {
196 if (!(params instanceof TlsRsaPremasterSecretParameterSpec)) {
197 throw new InvalidAlgorithmParameterException(
198 "No Parameters can be specified");
199 }
200 spec = params;
201 this.random = random; // for TLS RSA premaster secret
315 protected synchronized byte[] engineWrap(Key key) throws IllegalBlockSizeException,
316 InvalidKeyException {
317 try {
318 byte[] encodedKey = key.getEncoded();
319 if ((encodedKey == null) || (encodedKey.length == 0)) {
320 throw new InvalidKeyException("Cannot get an encoding of " +
321 "the key to be wrapped");
322 }
323 if (encodedKey.length > buffer.length) {
324 throw new InvalidKeyException("Key is too long for wrapping");
325 }
326 return engineDoFinal(encodedKey, 0, encodedKey.length);
327 } catch (BadPaddingException e) {
328 // Should never happen for key wrapping
329 throw new UcryptoException("Internal Error", e);
330 }
331 }
332
333 // see JCE spec
334 @Override
335 @SuppressWarnings("deprecation")
336 protected synchronized Key engineUnwrap(byte[] wrappedKey,
337 String wrappedKeyAlgorithm, int wrappedKeyType)
338 throws InvalidKeyException, NoSuchAlgorithmException {
339
340 if (wrappedKey.length > buffer.length) {
341 throw new InvalidKeyException("Key is too long for unwrapping");
342 }
343
344 boolean isTlsRsaPremasterSecret =
345 wrappedKeyAlgorithm.equals("TlsRsaPremasterSecret");
346 Exception failover = null;
347
348 byte[] encodedKey = null;
349 try {
350 encodedKey = engineDoFinal(wrappedKey, 0, wrappedKey.length);
351 } catch (BadPaddingException bpe) {
352 if (isTlsRsaPremasterSecret) {
353 failover = bpe;
354 } else {
355 throw new InvalidKeyException("Unwrapping failed", bpe);
|