|
| 1 | +package org.bouncycastle.jcajce.provider.test; |
| 2 | + |
| 3 | +import java.security.InvalidAlgorithmParameterException; |
| 4 | + |
| 5 | +import javax.crypto.SecretKey; |
| 6 | + |
| 7 | +import junit.framework.TestCase; |
| 8 | +import org.bouncycastle.asn1.ASN1ObjectIdentifier; |
| 9 | +import org.bouncycastle.asn1.x509.AlgorithmIdentifier; |
| 10 | +import org.bouncycastle.jcajce.provider.asymmetric.util.KdfUtil; |
| 11 | +import org.bouncycastle.jcajce.spec.KTSParameterSpec; |
| 12 | +import org.bouncycastle.util.Arrays; |
| 13 | + |
| 14 | +/** |
| 15 | + * Direct coverage of the public KEM helpers on KdfUtil - resolveKemSpec, resolveAlgorithm and |
| 16 | + * makeSecretKey. The provider's own KEM services pre-validate everything they pass in, so the |
| 17 | + * guards here are reachable only by an outside caller, and a provider-driven test can never |
| 18 | + * exercise them; the erase-on-every-exit contract in particular is asserted here for each failure |
| 19 | + * path, since a caller who hits one must not be left holding a live secret the javadoc says was |
| 20 | + * erased. |
| 21 | + */ |
| 22 | +public class KdfUtilTest |
| 23 | + extends TestCase |
| 24 | +{ |
| 25 | + private static byte[] secret() |
| 26 | + { |
| 27 | + byte[] secret = new byte[32]; |
| 28 | + Arrays.fill(secret, (byte)0x5a); |
| 29 | + return secret; |
| 30 | + } |
| 31 | + |
| 32 | + private static boolean isErased(byte[] secret) |
| 33 | + { |
| 34 | + return Arrays.areAllZeroes(secret, 0, secret.length); |
| 35 | + } |
| 36 | + |
| 37 | + public void testMakeSecretKeyHappyPath() |
| 38 | + { |
| 39 | + KTSParameterSpec spec = new KTSParameterSpec.Builder("AES", 256).build(); |
| 40 | + byte[] secret = secret(); |
| 41 | + |
| 42 | + SecretKey key = KdfUtil.makeSecretKey(spec, secret, 0, 16, "AES"); |
| 43 | + |
| 44 | + assertEquals("AES", key.getAlgorithm()); |
| 45 | + assertEquals(16, key.getEncoded().length); |
| 46 | + assertTrue("input secret not erased", isErased(secret)); |
| 47 | + |
| 48 | + // the same derivation over the same secret bytes gives the same key, and a later window |
| 49 | + // of it differs from the leading one |
| 50 | + SecretKey again = KdfUtil.makeSecretKey(spec, secret(), 0, 16, "AES"); |
| 51 | + assertTrue(Arrays.areEqual(key.getEncoded(), again.getEncoded())); |
| 52 | + SecretKey tail = KdfUtil.makeSecretKey(spec, secret(), 16, 32, "AES"); |
| 53 | + assertFalse(Arrays.areEqual(key.getEncoded(), tail.getEncoded())); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * The erase happens on every exit, the failure paths included - a caller who hits an exception |
| 58 | + * must not be left holding a live secret. |
| 59 | + */ |
| 60 | + public void testMakeSecretKeyErasesOnEveryExit() |
| 61 | + { |
| 62 | + KTSParameterSpec spec = new KTSParameterSpec.Builder("AES", 256).build(); |
| 63 | + |
| 64 | + // null algorithm name |
| 65 | + byte[] secret = secret(); |
| 66 | + try |
| 67 | + { |
| 68 | + KdfUtil.makeSecretKey(spec, secret, 0, 16, null); |
| 69 | + fail("null algorithm accepted"); |
| 70 | + } |
| 71 | + catch (NullPointerException expected) |
| 72 | + { |
| 73 | + assertTrue("secret not erased on the null-algorithm path", isErased(secret)); |
| 74 | + } |
| 75 | + |
| 76 | + // out-of-range request, refused before deriving |
| 77 | + secret = secret(); |
| 78 | + try |
| 79 | + { |
| 80 | + KdfUtil.makeSecretKey(spec, secret, 0, 999, "AES"); |
| 81 | + fail("out-of-range request accepted"); |
| 82 | + } |
| 83 | + catch (IllegalArgumentException expected) |
| 84 | + { |
| 85 | + assertTrue("secret not erased on the range path", isErased(secret)); |
| 86 | + } |
| 87 | + |
| 88 | + // a KDF the derivation itself refuses |
| 89 | + KTSParameterSpec badKdf = new KTSParameterSpec.Builder("AES", 256) |
| 90 | + .withKdfAlgorithm(new AlgorithmIdentifier(new ASN1ObjectIdentifier("1.2.3.4"))).build(); |
| 91 | + secret = secret(); |
| 92 | + try |
| 93 | + { |
| 94 | + KdfUtil.makeSecretKey(badKdf, secret, 0, 16, "AES"); |
| 95 | + fail("unknown KDF accepted"); |
| 96 | + } |
| 97 | + catch (IllegalArgumentException expected) |
| 98 | + { |
| 99 | + assertTrue("secret not erased on the derivation path", isErased(secret)); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + public void testResolveKemSpecGuards() |
| 104 | + throws Exception |
| 105 | + { |
| 106 | + // a bad session key size is the caller's own error, not a spec failure |
| 107 | + try |
| 108 | + { |
| 109 | + KdfUtil.resolveKemSpec(null, "TEST", "test-set", 100); |
| 110 | + fail("non-whole-byte session key size accepted"); |
| 111 | + } |
| 112 | + catch (IllegalArgumentException expected) |
| 113 | + { |
| 114 | + } |
| 115 | + |
| 116 | + // null spec resolves to the mechanism's own secret, no KDF |
| 117 | + KTSParameterSpec resolved = KdfUtil.resolveKemSpec(null, "TEST", "test-set", 192); |
| 118 | + assertEquals("Generic", resolved.getKeyAlgorithmName()); |
| 119 | + assertEquals(192, resolved.getKeySize()); |
| 120 | + assertNull(resolved.getKdfAlgorithm()); |
| 121 | + |
| 122 | + // a KDF-less request above the session key is refused, naming the set and both sizes |
| 123 | + try |
| 124 | + { |
| 125 | + KdfUtil.resolveKemSpec(new KTSParameterSpec.Builder("AES", 256).withNoKdf().build(), |
| 126 | + "TEST", "test-set", 192); |
| 127 | + fail("unsatisfiable KDF-less spec accepted"); |
| 128 | + } |
| 129 | + catch (InvalidAlgorithmParameterException expected) |
| 130 | + { |
| 131 | + assertEquals("no KDF specified and test-set produces a 192 bit secret, 256 requested", |
| 132 | + expected.getMessage()); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + public void testResolveAlgorithm() |
| 137 | + { |
| 138 | + KTSParameterSpec generic = new KTSParameterSpec.Builder("Generic", 256).build(); |
| 139 | + KTSParameterSpec aes = new KTSParameterSpec.Builder("AES", 256).build(); |
| 140 | + |
| 141 | + assertEquals("AES", KdfUtil.resolveAlgorithm(generic, "AES")); |
| 142 | + assertEquals("AES", KdfUtil.resolveAlgorithm(aes, "Generic")); |
| 143 | + assertEquals("AES", KdfUtil.resolveAlgorithm(aes, "AES")); |
| 144 | + |
| 145 | + try |
| 146 | + { |
| 147 | + KdfUtil.resolveAlgorithm(aes, "AES-KWP"); |
| 148 | + fail("algorithm mismatch accepted"); |
| 149 | + } |
| 150 | + catch (UnsupportedOperationException expected) |
| 151 | + { |
| 152 | + assertEquals("AES does not match AES-KWP", expected.getMessage()); |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments