|
1 | 1 | /* |
2 | | - * Copyright (c) 2004, 2020, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2004, 2021, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
@@ -198,7 +198,12 @@ protected void engineInit(int opmode, Key key, SecureRandom random) |
198 | 198 | "only be used for key wrapping and unwrapping"); |
199 | 199 | } |
200 | 200 | AESCipher.checkKeySize(key, fixedKeySize); |
201 | | - cipher.init(decrypting, key.getAlgorithm(), key.getEncoded()); |
| 201 | + byte[] encoded = key.getEncoded(); |
| 202 | + try { |
| 203 | + cipher.init(decrypting, key.getAlgorithm(), encoded); |
| 204 | + } finally { |
| 205 | + Arrays.fill(encoded, (byte)0); |
| 206 | + } |
202 | 207 | } |
203 | 208 |
|
204 | 209 | /** |
@@ -374,6 +379,7 @@ protected AlgorithmParameters engineGetParameters() { |
374 | 379 | */ |
375 | 380 | protected int engineGetKeySize(Key key) throws InvalidKeyException { |
376 | 381 | byte[] encoded = key.getEncoded(); |
| 382 | + Arrays.fill(encoded, (byte)0); |
377 | 383 | if (!AESCrypt.isKeySizeValid(encoded.length)) { |
378 | 384 | throw new InvalidKeyException("Invalid key length: " + |
379 | 385 | encoded.length + " bytes"); |
@@ -404,38 +410,42 @@ protected byte[] engineWrap(Key key) |
404 | 410 | throw new InvalidKeyException("Cannot get an encoding of " + |
405 | 411 | "the key to be wrapped"); |
406 | 412 | } |
407 | | - byte[] out = new byte[Math.addExact(keyVal.length, 8)]; |
| 413 | + try { |
| 414 | + byte[] out = new byte[Math.addExact(keyVal.length, 8)]; |
408 | 415 |
|
409 | | - if (keyVal.length == 8) { |
410 | | - System.arraycopy(IV, 0, out, 0, IV.length); |
411 | | - System.arraycopy(keyVal, 0, out, IV.length, 8); |
412 | | - cipher.encryptBlock(out, 0, out, 0); |
413 | | - } else { |
414 | | - if (keyVal.length % 8 != 0) { |
415 | | - throw new IllegalBlockSizeException("length of the " + |
416 | | - "to be wrapped key should be multiples of 8 bytes"); |
417 | | - } |
418 | | - System.arraycopy(IV, 0, out, 0, IV.length); |
419 | | - System.arraycopy(keyVal, 0, out, IV.length, keyVal.length); |
420 | | - int N = keyVal.length/8; |
421 | | - byte[] buffer = new byte[blksize]; |
422 | | - for (int j = 0; j < 6; j++) { |
423 | | - for (int i = 1; i <= N; i++) { |
424 | | - int T = i + j*N; |
425 | | - System.arraycopy(out, 0, buffer, 0, IV.length); |
426 | | - System.arraycopy(out, i*8, buffer, IV.length, 8); |
427 | | - cipher.encryptBlock(buffer, 0, buffer, 0); |
428 | | - for (int k = 1; T != 0; k++) { |
429 | | - byte v = (byte) T; |
430 | | - buffer[IV.length - k] ^= v; |
431 | | - T >>>= 8; |
| 416 | + if (keyVal.length == 8) { |
| 417 | + System.arraycopy(IV, 0, out, 0, IV.length); |
| 418 | + System.arraycopy(keyVal, 0, out, IV.length, 8); |
| 419 | + cipher.encryptBlock(out, 0, out, 0); |
| 420 | + } else { |
| 421 | + if (keyVal.length % 8 != 0) { |
| 422 | + throw new IllegalBlockSizeException("length of the " + |
| 423 | + "to be wrapped key should be multiples of 8 bytes"); |
| 424 | + } |
| 425 | + System.arraycopy(IV, 0, out, 0, IV.length); |
| 426 | + System.arraycopy(keyVal, 0, out, IV.length, keyVal.length); |
| 427 | + int N = keyVal.length / 8; |
| 428 | + byte[] buffer = new byte[blksize]; |
| 429 | + for (int j = 0; j < 6; j++) { |
| 430 | + for (int i = 1; i <= N; i++) { |
| 431 | + int T = i + j * N; |
| 432 | + System.arraycopy(out, 0, buffer, 0, IV.length); |
| 433 | + System.arraycopy(out, i * 8, buffer, IV.length, 8); |
| 434 | + cipher.encryptBlock(buffer, 0, buffer, 0); |
| 435 | + for (int k = 1; T != 0; k++) { |
| 436 | + byte v = (byte) T; |
| 437 | + buffer[IV.length - k] ^= v; |
| 438 | + T >>>= 8; |
| 439 | + } |
| 440 | + System.arraycopy(buffer, 0, out, 0, IV.length); |
| 441 | + System.arraycopy(buffer, 8, out, 8 * i, 8); |
432 | 442 | } |
433 | | - System.arraycopy(buffer, 0, out, 0, IV.length); |
434 | | - System.arraycopy(buffer, 8, out, 8*i, 8); |
435 | 443 | } |
436 | 444 | } |
| 445 | + return out; |
| 446 | + } finally { |
| 447 | + Arrays.fill(keyVal, (byte)0); |
437 | 448 | } |
438 | | - return out; |
439 | 449 | } |
440 | 450 |
|
441 | 451 | /** |
@@ -474,38 +484,43 @@ protected Key engineUnwrap(byte[] wrappedKey, |
474 | 484 | } |
475 | 485 | byte[] out = new byte[wrappedKeyLen - 8]; |
476 | 486 | byte[] buffer = new byte[blksize]; |
477 | | - if (wrappedKeyLen == 16) { |
478 | | - cipher.decryptBlock(wrappedKey, 0, buffer, 0); |
479 | | - for (int i = 0; i < IV.length; i++) { |
480 | | - if (IV[i] != buffer[i]) { |
481 | | - throw new InvalidKeyException("Integrity check failed"); |
| 487 | + try { |
| 488 | + if (wrappedKeyLen == 16) { |
| 489 | + cipher.decryptBlock(wrappedKey, 0, buffer, 0); |
| 490 | + for (int i = 0; i < IV.length; i++) { |
| 491 | + if (IV[i] != buffer[i]) { |
| 492 | + throw new InvalidKeyException("Integrity check failed"); |
| 493 | + } |
482 | 494 | } |
483 | | - } |
484 | | - System.arraycopy(buffer, IV.length, out, 0, out.length); |
485 | | - } else { |
486 | | - System.arraycopy(wrappedKey, 0, buffer, 0, IV.length); |
487 | | - System.arraycopy(wrappedKey, IV.length, out, 0, out.length); |
488 | | - int N = out.length/8; |
489 | | - for (int j = 5; j >= 0; j--) { |
490 | | - for (int i = N; i > 0; i--) { |
491 | | - int T = i + j*N; |
492 | | - System.arraycopy(out, 8*(i-1), buffer, IV.length, 8); |
493 | | - for (int k = 1; T != 0; k++) { |
494 | | - byte v = (byte) T; |
495 | | - buffer[IV.length - k] ^= v; |
496 | | - T >>>= 8; |
| 495 | + System.arraycopy(buffer, IV.length, out, 0, out.length); |
| 496 | + } else { |
| 497 | + System.arraycopy(wrappedKey, 0, buffer, 0, IV.length); |
| 498 | + System.arraycopy(wrappedKey, IV.length, out, 0, out.length); |
| 499 | + int N = out.length / 8; |
| 500 | + for (int j = 5; j >= 0; j--) { |
| 501 | + for (int i = N; i > 0; i--) { |
| 502 | + int T = i + j * N; |
| 503 | + System.arraycopy(out, 8 * (i - 1), buffer, IV.length, 8); |
| 504 | + for (int k = 1; T != 0; k++) { |
| 505 | + byte v = (byte) T; |
| 506 | + buffer[IV.length - k] ^= v; |
| 507 | + T >>>= 8; |
| 508 | + } |
| 509 | + cipher.decryptBlock(buffer, 0, buffer, 0); |
| 510 | + System.arraycopy(buffer, IV.length, out, 8 * (i - 1), 8); |
497 | 511 | } |
498 | | - cipher.decryptBlock(buffer, 0, buffer, 0); |
499 | | - System.arraycopy(buffer, IV.length, out, 8*(i-1), 8); |
500 | 512 | } |
501 | | - } |
502 | | - for (int i = 0; i < IV.length; i++) { |
503 | | - if (IV[i] != buffer[i]) { |
504 | | - throw new InvalidKeyException("Integrity check failed"); |
| 513 | + for (int i = 0; i < IV.length; i++) { |
| 514 | + if (IV[i] != buffer[i]) { |
| 515 | + throw new InvalidKeyException("Integrity check failed"); |
| 516 | + } |
505 | 517 | } |
506 | 518 | } |
| 519 | + return ConstructKeys.constructKey(out, wrappedKeyAlgorithm, |
| 520 | + wrappedKeyType); |
| 521 | + } finally { |
| 522 | + Arrays.fill(out, (byte)0); |
| 523 | + Arrays.fill(buffer, (byte)0); |
507 | 524 | } |
508 | | - return ConstructKeys.constructKey(out, wrappedKeyAlgorithm, |
509 | | - wrappedKeyType); |
510 | 525 | } |
511 | 526 | } |
0 commit comments