|
40 | 40 |
|
41 | 41 | import org.bouncycastle.asn1.ASN1ObjectIdentifier; |
42 | 42 | import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers; |
| 43 | +import org.bouncycastle.crypto.Digest; |
43 | 44 | import org.bouncycastle.crypto.agreement.DHStandardGroups; |
| 45 | +import org.bouncycastle.crypto.digests.SHA256Digest; |
| 46 | +import org.bouncycastle.crypto.digests.SHA384Digest; |
| 47 | +import org.bouncycastle.crypto.digests.SHA512Digest; |
| 48 | +import org.bouncycastle.crypto.generators.HKDFBytesGenerator; |
| 49 | +import org.bouncycastle.crypto.params.HKDFParameters; |
44 | 50 | import org.bouncycastle.internal.asn1.bsi.BSIObjectIdentifiers; |
45 | 51 | import org.bouncycastle.jcajce.provider.config.ConfigurableProvider; |
46 | 52 | import org.bouncycastle.jcajce.spec.DHDomainParameterSpec; |
@@ -1634,6 +1640,91 @@ public void performTest() |
1634 | 1640 | testMinSpecValue(); |
1635 | 1641 | testGenerateUsingStandardGroup(); |
1636 | 1642 | testHybridValueParameterSpec(); |
| 1643 | + |
| 1644 | + testECDHwithHKDF("ECDHwithSHA256HKDF", new SHA256Digest(), "secp256r1"); |
| 1645 | + testECDHwithHKDF("ECDHwithSHA384HKDF", new SHA384Digest(), "secp384r1"); |
| 1646 | + testECDHwithHKDF("ECDHwithSHA512HKDF", new SHA512Digest(), "secp521r1"); |
| 1647 | + } |
| 1648 | + |
| 1649 | + /** |
| 1650 | + * The ECDH HKDF agreements against an independent derivation: the shared secret from plain ECDH, |
| 1651 | + * then RFC 5869 HKDF taking the UserKeyingMaterialSpec's keying material as info and its salt, when |
| 1652 | + * present, as salt. Run with no spec, with keying material only, and with keying material and a |
| 1653 | + * salt, since a salt the agreement dropped would still agree with the other side. |
| 1654 | + */ |
| 1655 | + private void testECDHwithHKDF(String algorithm, Digest digest, String curveName) |
| 1656 | + throws Exception |
| 1657 | + { |
| 1658 | + KeyPairGenerator g = KeyPairGenerator.getInstance("EC", "BC"); |
| 1659 | + |
| 1660 | + g.initialize(ECNamedCurveTable.getParameterSpec(curveName)); |
| 1661 | + |
| 1662 | + KeyPair aKeyPair = g.generateKeyPair(); |
| 1663 | + KeyPair bKeyPair = g.generateKeyPair(); |
| 1664 | + |
| 1665 | + KeyAgreement rawAgree = KeyAgreement.getInstance("ECDH", "BC"); |
| 1666 | + |
| 1667 | + rawAgree.init(aKeyPair.getPrivate()); |
| 1668 | + rawAgree.doPhase(bKeyPair.getPublic(), true); |
| 1669 | + |
| 1670 | + byte[] z = rawAgree.generateSecret(); |
| 1671 | + byte[] info = Hex.decode("000102030405060708090a0b0c0d0e0f"); |
| 1672 | + byte[] salt = Hex.decode("beeffeed"); |
| 1673 | + |
| 1674 | + byte[] noSpec = checkECDHwithHKDF(algorithm, aKeyPair, bKeyPair, null, hkdf(digest, z, null, null)); |
| 1675 | + byte[] unsalted = checkECDHwithHKDF(algorithm, aKeyPair, bKeyPair, new UserKeyingMaterialSpec(info), |
| 1676 | + hkdf(digest, z, null, info)); |
| 1677 | + byte[] salted = checkECDHwithHKDF(algorithm, aKeyPair, bKeyPair, new UserKeyingMaterialSpec(info, salt), |
| 1678 | + hkdf(digest, z, salt, info)); |
| 1679 | + |
| 1680 | + isTrue(algorithm + " ignored the HKDF info", !Arrays.areEqual(noSpec, unsalted)); |
| 1681 | + isTrue(algorithm + " ignored the HKDF salt", !Arrays.areEqual(unsalted, salted)); |
| 1682 | + } |
| 1683 | + |
| 1684 | + private byte[] checkECDHwithHKDF(String algorithm, KeyPair aKeyPair, KeyPair bKeyPair, |
| 1685 | + UserKeyingMaterialSpec spec, byte[] expected) |
| 1686 | + throws Exception |
| 1687 | + { |
| 1688 | + byte[] aKey = agreeECDHwithHKDF(algorithm, aKeyPair.getPrivate(), bKeyPair.getPublic(), spec); |
| 1689 | + byte[] bKey = agreeECDHwithHKDF(algorithm, bKeyPair.getPrivate(), aKeyPair.getPublic(), spec); |
| 1690 | + |
| 1691 | + isTrue(algorithm + " 2-way test failed", Arrays.areEqual(aKey, bKey)); |
| 1692 | + isTrue(algorithm + " key does not match HKDF over the ECDH shared secret", Arrays.areEqual(expected, aKey)); |
| 1693 | + |
| 1694 | + return aKey; |
| 1695 | + } |
| 1696 | + |
| 1697 | + private byte[] agreeECDHwithHKDF(String algorithm, PrivateKey privKey, PublicKey pubKey, |
| 1698 | + UserKeyingMaterialSpec spec) |
| 1699 | + throws Exception |
| 1700 | + { |
| 1701 | + KeyAgreement agreement = KeyAgreement.getInstance(algorithm, "BC"); |
| 1702 | + |
| 1703 | + if (spec == null) |
| 1704 | + { |
| 1705 | + agreement.init(privKey); |
| 1706 | + } |
| 1707 | + else |
| 1708 | + { |
| 1709 | + agreement.init(privKey, spec); |
| 1710 | + } |
| 1711 | + |
| 1712 | + agreement.doPhase(pubKey, true); |
| 1713 | + |
| 1714 | + return agreement.generateSecret("AES[256]").getEncoded(); |
| 1715 | + } |
| 1716 | + |
| 1717 | + private static byte[] hkdf(Digest digest, byte[] ikm, byte[] salt, byte[] info) |
| 1718 | + { |
| 1719 | + HKDFBytesGenerator kdf = new HKDFBytesGenerator(digest); |
| 1720 | + |
| 1721 | + kdf.init(new HKDFParameters(ikm, salt, info)); |
| 1722 | + |
| 1723 | + byte[] key = new byte[32]; |
| 1724 | + |
| 1725 | + kdf.generateBytes(key, 0, key.length); |
| 1726 | + |
| 1727 | + return key; |
1637 | 1728 | } |
1638 | 1729 |
|
1639 | 1730 | /** |
|
0 commit comments