|
30 | 30 | import org.bouncycastle.jcajce.spec.KTSParameterSpec; |
31 | 31 | import org.bouncycastle.util.Arrays; |
32 | 32 |
|
| 33 | +/** |
| 34 | + * The KEM secret-key derivation toolkit shared by every KEM in the provider, public so a caller |
| 35 | + * building its own KEM integration can use the same pieces. The intended sequence is the one the |
| 36 | + * provider's javax.crypto.KEM services follow: {@link #resolveKemSpec} to validate the caller's |
| 37 | + * KTSParameterSpec (or build the default) when the encapsulator or decapsulator is created, |
| 38 | + * {@link #resolveAlgorithm} to reconcile the requested algorithm name with the spec's per |
| 39 | + * operation, and {@link #makeSecretKey} to derive the key - or {@link #makeKeyBytes} where raw |
| 40 | + * bytes are wanted, as the KEM KeyGenerator services do. The secret-bearing inputs are erased by |
| 41 | + * the methods that consume them; each javadoc says exactly when. |
| 42 | + */ |
33 | 43 | public class KdfUtil |
34 | 44 | { |
35 | 45 | /** |
@@ -119,8 +129,9 @@ else if (!isSupportedKdf(ktsSpec.getKdfAlgorithm())) |
119 | 129 | * particular - before calling this, and destroy the SecretWithEncapsulation it came from |
120 | 130 | * afterwards; that is left to the caller so the ordering stays visible at the call site. |
121 | 131 | * <p> |
122 | | - * The requested range is validated here rather than left to SecretKeySpec, so that an |
123 | | - * out-of-range request cannot reach it after the secret has been erased. |
| 132 | + * The requested range is validated before deriving, so an out-of-range request is reported as |
| 133 | + * the range error it is instead of surfacing from SecretKeySpec - the secret is erased either |
| 134 | + * way. |
124 | 135 | * |
125 | 136 | * @param parameterSpec the KDF and output size to derive with. |
126 | 137 | * @param kemSecret the mechanism's shared secret (erased before this returns). |
@@ -262,10 +273,11 @@ private static boolean isSupportedKdf(AlgorithmIdentifier kdfAlgorithm) |
262 | 273 | } |
263 | 274 |
|
264 | 275 | /** |
265 | | - * Generate a byte[] secret key from the passed in secret. Note: passed in secret will be erased after use. |
| 276 | + * Generate a byte[] secret key from the passed in secret. Note: the passed in secret will be |
| 277 | + * erased before this returns, on the failure paths included. |
266 | 278 | * |
267 | 279 | * @param kdfSpec definition of the KDF and the output size to produce. |
268 | | - * @param secret the secret value to initialize the KDF with (erased after secret key generation). |
| 280 | + * @param secret the secret value to initialize the KDF with (erased before this returns). |
269 | 281 | * @return a generated secret key. |
270 | 282 | * @throws IllegalArgumentException if the spec asks for no KDF and the requested key size is |
271 | 283 | * larger than the shared secret the mechanism produced. |
|
0 commit comments