|
1 | 1 | /* |
2 | | - * Copyright (c) 2004, 2021, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2004, 2023, 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 |
@@ -123,6 +123,25 @@ public AES256_KWP_NoPadding() { |
123 | 123 | } |
124 | 124 | } |
125 | 125 |
|
| 126 | + // validate the key algorithm/encoding and then returns the key bytes |
| 127 | + // which callers should erase after use |
| 128 | + private static byte[] checkKey(Key key, int fixedKeySize) |
| 129 | + throws InvalidKeyException { |
| 130 | + |
| 131 | + byte[] keyBytes = key.getEncoded(); |
| 132 | + if (keyBytes == null) { |
| 133 | + throw new InvalidKeyException("Null key"); |
| 134 | + } |
| 135 | + int keyLen = keyBytes.length; |
| 136 | + if (!key.getAlgorithm().equalsIgnoreCase("AES") || |
| 137 | + !AESCrypt.isKeySizeValid(keyLen) || |
| 138 | + (fixedKeySize != -1 && fixedKeySize != keyLen)) { |
| 139 | + throw new InvalidKeyException("Invalid key length: " + |
| 140 | + keyLen + " bytes"); |
| 141 | + } |
| 142 | + return keyBytes; |
| 143 | + } |
| 144 | + |
126 | 145 | // store the specified bytes, e.g. in[inOfs...(inOfs+inLen-1)] into |
127 | 146 | // 'dataBuf' starting at 'dataIdx'. |
128 | 147 | // NOTE: if 'in' is null, this method will ensure that 'dataBuf' has enough |
@@ -294,10 +313,8 @@ protected byte[] engineGetIV() { |
294 | 313 | // actual impl for various engineInit(...) methods |
295 | 314 | private void implInit(int opmode, Key key, byte[] iv, SecureRandom random) |
296 | 315 | throws InvalidKeyException, InvalidAlgorithmParameterException { |
297 | | - byte[] keyBytes = key.getEncoded(); |
298 | | - if (keyBytes == null) { |
299 | | - throw new InvalidKeyException("Null key"); |
300 | | - } |
| 316 | + byte[] keyBytes = checkKey(key, fixedKeySize); |
| 317 | + |
301 | 318 | this.opmode = opmode; |
302 | 319 | boolean decrypting = (opmode == Cipher.DECRYPT_MODE || |
303 | 320 | opmode == Cipher.UNWRAP_MODE); |
@@ -658,21 +675,11 @@ protected AlgorithmParameters engineGetParameters() { |
658 | 675 | * @exception InvalidKeyException if <code>key</code> is invalid. |
659 | 676 | */ |
660 | 677 | protected int engineGetKeySize(Key key) throws InvalidKeyException { |
661 | | - byte[] encoded = key.getEncoded(); |
662 | | - if (encoded == null) { |
663 | | - throw new InvalidKeyException("Cannot decide key length"); |
664 | | - } |
| 678 | + byte[] keyBytes = checkKey(key, fixedKeySize); |
| 679 | + // only need length; erase immediately |
| 680 | + Arrays.fill(keyBytes, (byte) 0); |
| 681 | + return Math.multiplyExact(keyBytes.length, 8); |
665 | 682 |
|
666 | | - // only need length |
667 | | - Arrays.fill(encoded, (byte) 0); |
668 | | - int keyLen = encoded.length; |
669 | | - if (!key.getAlgorithm().equalsIgnoreCase("AES") || |
670 | | - !AESCrypt.isKeySizeValid(keyLen) || |
671 | | - (fixedKeySize != -1 && fixedKeySize != keyLen)) { |
672 | | - throw new InvalidKeyException("Invalid key length: " + |
673 | | - keyLen + " bytes"); |
674 | | - } |
675 | | - return Math.multiplyExact(keyLen, 8); |
676 | 683 | } |
677 | 684 |
|
678 | 685 | /** |
|
0 commit comments