|
1 | 1 | /* |
2 | | - * Copyright (c) 2004, 2022, 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 |
@@ -292,10 +311,8 @@ protected byte[] engineGetIV() { |
292 | 311 | // actual impl for various engineInit(...) methods |
293 | 312 | private void implInit(int opmode, Key key, byte[] iv, SecureRandom random) |
294 | 313 | throws InvalidKeyException, InvalidAlgorithmParameterException { |
295 | | - byte[] keyBytes = key.getEncoded(); |
296 | | - if (keyBytes == null) { |
297 | | - throw new InvalidKeyException("Null key"); |
298 | | - } |
| 314 | + byte[] keyBytes = checkKey(key, fixedKeySize); |
| 315 | + |
299 | 316 | this.opmode = opmode; |
300 | 317 | boolean decrypting = (opmode == Cipher.DECRYPT_MODE || |
301 | 318 | opmode == Cipher.UNWRAP_MODE); |
@@ -656,21 +673,11 @@ protected AlgorithmParameters engineGetParameters() { |
656 | 673 | * @exception InvalidKeyException if <code>key</code> is invalid. |
657 | 674 | */ |
658 | 675 | protected int engineGetKeySize(Key key) throws InvalidKeyException { |
659 | | - byte[] encoded = key.getEncoded(); |
660 | | - if (encoded == null) { |
661 | | - throw new InvalidKeyException("Cannot decide key length"); |
662 | | - } |
| 676 | + byte[] keyBytes = checkKey(key, fixedKeySize); |
| 677 | + // only need length; erase immediately |
| 678 | + Arrays.fill(keyBytes, (byte) 0); |
| 679 | + return Math.multiplyExact(keyBytes.length, 8); |
663 | 680 |
|
664 | | - // only need length |
665 | | - Arrays.fill(encoded, (byte) 0); |
666 | | - int keyLen = encoded.length; |
667 | | - if (!key.getAlgorithm().equalsIgnoreCase("AES") || |
668 | | - !AESCrypt.isKeySizeValid(keyLen) || |
669 | | - (fixedKeySize != -1 && fixedKeySize != keyLen)) { |
670 | | - throw new InvalidKeyException("Invalid key length: " + |
671 | | - keyLen + " bytes"); |
672 | | - } |
673 | | - return Math.multiplyExact(keyLen, 8); |
674 | 681 | } |
675 | 682 |
|
676 | 683 | /** |
|
0 commit comments