|
| 1 | +package org.bouncycastle.pqc.jcajce.provider; |
| 2 | + |
| 3 | +import org.bouncycastle.asn1.ASN1ObjectIdentifier; |
| 4 | +import org.bouncycastle.asn1.nist.NISTObjectIdentifiers; |
| 5 | +import org.bouncycastle.asn1.x509.AlgorithmIdentifier; |
| 6 | +import org.bouncycastle.asn1.x9.X9ObjectIdentifiers; |
| 7 | +import org.bouncycastle.crypto.DerivationFunction; |
| 8 | +import org.bouncycastle.crypto.Digest; |
| 9 | +import org.bouncycastle.crypto.Xof; |
| 10 | +import org.bouncycastle.crypto.agreement.kdf.ConcatenationKDFGenerator; |
| 11 | +import org.bouncycastle.crypto.digests.SHA256Digest; |
| 12 | +import org.bouncycastle.crypto.digests.SHA512Digest; |
| 13 | +import org.bouncycastle.crypto.digests.SHAKEDigest; |
| 14 | +import org.bouncycastle.crypto.generators.KDF2BytesGenerator; |
| 15 | +import org.bouncycastle.crypto.params.KDFParameters; |
| 16 | +import org.bouncycastle.jcajce.spec.KTSParameterSpec; |
| 17 | + |
| 18 | +import java.security.InvalidKeyException; |
| 19 | + |
| 20 | +public class Util |
| 21 | +{ |
| 22 | + public static byte[] makeKeyBytes(KTSParameterSpec ktsSpec, byte[] secret) |
| 23 | + throws InvalidKeyException |
| 24 | + { |
| 25 | + AlgorithmIdentifier kdfAlgorithm = ktsSpec.getKdfAlgorithm(); |
| 26 | + byte[] otherInfo = ktsSpec.getOtherInfo(); |
| 27 | + byte[] keyBytes = new byte[(ktsSpec.getKeySize() + 7) / 8]; |
| 28 | + |
| 29 | + if (X9ObjectIdentifiers.id_kdf_kdf2.equals(kdfAlgorithm.getAlgorithm())) |
| 30 | + { |
| 31 | + AlgorithmIdentifier digAlg = AlgorithmIdentifier.getInstance(kdfAlgorithm.getParameters()); |
| 32 | + DerivationFunction kdf = new KDF2BytesGenerator(getDigest(digAlg.getAlgorithm())); |
| 33 | + |
| 34 | + kdf.init(new KDFParameters(secret, otherInfo)); |
| 35 | + |
| 36 | + kdf.generateBytes(keyBytes, 0, keyBytes.length); |
| 37 | + } |
| 38 | + else if (X9ObjectIdentifiers.id_kdf_kdf3.equals(kdfAlgorithm.getAlgorithm())) |
| 39 | + { |
| 40 | + AlgorithmIdentifier digAlg = AlgorithmIdentifier.getInstance(kdfAlgorithm.getParameters()); |
| 41 | + DerivationFunction kdf = new ConcatenationKDFGenerator(getDigest(digAlg.getAlgorithm())); |
| 42 | + |
| 43 | + kdf.init(new KDFParameters(secret, otherInfo)); |
| 44 | + |
| 45 | + kdf.generateBytes(keyBytes, 0, keyBytes.length); |
| 46 | + } |
| 47 | + else if (NISTObjectIdentifiers.id_shake256.equals(kdfAlgorithm.getAlgorithm())) |
| 48 | + { |
| 49 | + Xof xof = new SHAKEDigest(256); |
| 50 | + |
| 51 | + xof.update(secret, 0, secret.length); |
| 52 | + xof.update(otherInfo, 0, otherInfo.length); |
| 53 | + |
| 54 | + xof.doFinal(keyBytes, 0, keyBytes.length); |
| 55 | + } |
| 56 | + else |
| 57 | + { |
| 58 | + throw new InvalidKeyException("Unrecognized KDF: " + kdfAlgorithm.getAlgorithm()); |
| 59 | + } |
| 60 | + |
| 61 | + return keyBytes; |
| 62 | + } |
| 63 | + |
| 64 | + static Digest getDigest(ASN1ObjectIdentifier oid) |
| 65 | + { |
| 66 | + if (oid.equals(NISTObjectIdentifiers.id_sha256)) |
| 67 | + { |
| 68 | + return new SHA256Digest(); |
| 69 | + } |
| 70 | + if (oid.equals(NISTObjectIdentifiers.id_sha512)) |
| 71 | + { |
| 72 | + return new SHA512Digest(); |
| 73 | + } |
| 74 | + if (oid.equals(NISTObjectIdentifiers.id_shake128)) |
| 75 | + { |
| 76 | + return new SHAKEDigest(128); |
| 77 | + } |
| 78 | + if (oid.equals(NISTObjectIdentifiers.id_shake256)) |
| 79 | + { |
| 80 | + return new SHAKEDigest(256); |
| 81 | + } |
| 82 | + |
| 83 | + throw new IllegalArgumentException("unrecognized digest OID: " + oid); |
| 84 | + } |
| 85 | +} |
0 commit comments