|
6 | 6 |
|
7 | 7 | import junit.framework.TestCase; |
8 | 8 | import org.bouncycastle.asn1.ASN1ObjectIdentifier; |
| 9 | +import org.bouncycastle.asn1.nist.NISTObjectIdentifiers; |
| 10 | +import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers; |
9 | 11 | import org.bouncycastle.asn1.x509.AlgorithmIdentifier; |
| 12 | +import org.bouncycastle.asn1.x9.X9ObjectIdentifiers; |
10 | 13 | import org.bouncycastle.jcajce.provider.asymmetric.util.KdfUtil; |
| 14 | +import org.bouncycastle.jcajce.spec.KEMParameterSpec; |
11 | 15 | import org.bouncycastle.jcajce.spec.KTSParameterSpec; |
12 | 16 | import org.bouncycastle.util.Arrays; |
13 | 17 |
|
@@ -152,4 +156,78 @@ public void testResolveAlgorithm() |
152 | 156 | assertEquals("AES does not match AES-KWP", expected.getMessage()); |
153 | 157 | } |
154 | 158 | } |
| 159 | + |
| 160 | + /** |
| 161 | + * A spec built through a Builder never carries a null otherInfo, but the KEMKDFSpec |
| 162 | + * constructor is protected and reachable by a subclass - the deprecated KEMParameterSpec passes |
| 163 | + * null itself - and three of makeKeyBytes' KDF branches read the otherInfo length without a |
| 164 | + * guard. So the normalisation belongs in the constructor: kdf2/kdf3 and HKDF happen to tolerate |
| 165 | + * a null through KDFParameters/HKDFParameters, but KMAC-128, KMAC-256 and SHAKE-256 threw |
| 166 | + * NullPointerException out of the KEM operation before this. |
| 167 | + */ |
| 168 | + public void testNullOtherInfoIsNormalised() |
| 169 | + { |
| 170 | + AlgorithmIdentifier[] kdfs = new AlgorithmIdentifier[] |
| 171 | + { |
| 172 | + // the branches that were null-tolerant already, kept as the compatibility assertion |
| 173 | + new AlgorithmIdentifier(X9ObjectIdentifiers.id_kdf_kdf2, |
| 174 | + new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha256)), |
| 175 | + new AlgorithmIdentifier(X9ObjectIdentifiers.id_kdf_kdf3, |
| 176 | + new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha256)), |
| 177 | + new AlgorithmIdentifier(PKCSObjectIdentifiers.id_alg_hkdf_with_sha256), |
| 178 | + // and the three that read otherInfo.length directly |
| 179 | + new AlgorithmIdentifier(NISTObjectIdentifiers.id_Kmac128), |
| 180 | + new AlgorithmIdentifier(NISTObjectIdentifiers.id_Kmac256), |
| 181 | + new AlgorithmIdentifier(NISTObjectIdentifiers.id_shake256) |
| 182 | + }; |
| 183 | + |
| 184 | + for (int i = 0; i != kdfs.length; i++) |
| 185 | + { |
| 186 | + AlgorithmIdentifier kdf = kdfs[i]; |
| 187 | + String name = kdf.getAlgorithm().toString(); |
| 188 | + |
| 189 | + assertEquals("otherInfo must be normalised for " + name, |
| 190 | + 0, new NullOtherInfoSpec(kdf).getOtherInfo().length); |
| 191 | + |
| 192 | + byte[] secret = secret(); |
| 193 | + byte[] keyBytes = KdfUtil.makeKeyBytes(new NullOtherInfoSpec(kdf), secret); |
| 194 | + |
| 195 | + assertEquals(name + ": derived key size", 32, keyBytes.length); |
| 196 | + assertTrue(name + ": the secret must still be erased", isErased(secret)); |
| 197 | + |
| 198 | + // and the derived bytes must match what an explicitly empty otherInfo gives, so the |
| 199 | + // normalisation is not quietly changing anyone's key material |
| 200 | + byte[] explicit = KdfUtil.makeKeyBytes(new EmptyOtherInfoSpec(kdf), secret()); |
| 201 | + assertTrue(name + ": null and empty otherInfo must derive the same key", |
| 202 | + Arrays.areEqual(keyBytes, explicit)); |
| 203 | + } |
| 204 | + |
| 205 | + // the in-tree subclass that passes null itself |
| 206 | + assertNotNull("KEMParameterSpec must not report a null otherInfo", |
| 207 | + new KEMParameterSpec("AES", 256).getOtherInfo()); |
| 208 | + assertEquals(0, new KEMParameterSpec("AES", 256).getOtherInfo().length); |
| 209 | + } |
| 210 | + |
| 211 | + /** |
| 212 | + * An out-of-provider subclass reaching the protected constructor with a null otherInfo - the |
| 213 | + * shape KdfUtil's javadoc invites when it says the helpers are public for callers building |
| 214 | + * their own KEM integration. |
| 215 | + */ |
| 216 | + private static class NullOtherInfoSpec |
| 217 | + extends KTSParameterSpec |
| 218 | + { |
| 219 | + NullOtherInfoSpec(AlgorithmIdentifier kdfAlgorithm) |
| 220 | + { |
| 221 | + super("AES", 256, null, kdfAlgorithm, null); |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + private static class EmptyOtherInfoSpec |
| 226 | + extends KTSParameterSpec |
| 227 | + { |
| 228 | + EmptyOtherInfoSpec(AlgorithmIdentifier kdfAlgorithm) |
| 229 | + { |
| 230 | + super("AES", 256, null, kdfAlgorithm, new byte[0]); |
| 231 | + } |
| 232 | + } |
155 | 233 | } |
0 commit comments