|
1 | 1 | package org.bouncycastle.jcajce.provider.asymmetric.util; |
2 | 2 |
|
| 3 | +import java.security.InvalidAlgorithmParameterException; |
| 4 | +import java.security.spec.AlgorithmParameterSpec; |
| 5 | + |
3 | 6 | import org.bouncycastle.asn1.ASN1ObjectIdentifier; |
4 | 7 | import org.bouncycastle.asn1.ASN1OctetString; |
5 | 8 | import org.bouncycastle.asn1.nist.NISTObjectIdentifiers; |
|
21 | 24 | import org.bouncycastle.crypto.params.KDFParameters; |
22 | 25 | import org.bouncycastle.crypto.params.KeyParameter; |
23 | 26 | import org.bouncycastle.jcajce.spec.KEMKDFSpec; |
| 27 | +import org.bouncycastle.jcajce.spec.KTSParameterSpec; |
24 | 28 | import org.bouncycastle.util.Arrays; |
25 | 29 |
|
26 | 30 | public class KdfUtil |
27 | 31 | { |
| 32 | + /** |
| 33 | + * Validate a KTSParameterSpec offered to a KEM, or build the default spec - the shared secret |
| 34 | + * used as it comes, with no KDF - when the caller supplied none. |
| 35 | + * <p> |
| 36 | + * Everything a KEM cannot honour is rejected here rather than at encapsulate/decapsulate time, |
| 37 | + * where it would surface as an unchecked exception the javax.crypto.KEM API does not declare. |
| 38 | + * Note that without a KDF the secret is the mechanism's own session key, so the requested size |
| 39 | + * must be a whole number of bytes no larger than that key: javax.crypto.KEM validates |
| 40 | + * encapsulate()'s range against secretSize(), so that size has to be honest rather than |
| 41 | + * quietly shortened the way {@link WrapUtil} shortens a KEK to the secret it has. |
| 42 | + * |
| 43 | + * @param spec the caller-supplied spec, or null for the default. |
| 44 | + * @param algorithmName the KEM's name, used in the exception messages. |
| 45 | + * @param parameterSetName the name of the key's parameter set, used in the exception messages. |
| 46 | + * @param sessionKeySize the size in bits of the mechanism's own session key for that set. |
| 47 | + * @return the spec to use. |
| 48 | + * @throws InvalidAlgorithmParameterException if the spec cannot be honoured. |
| 49 | + */ |
| 50 | + public static KTSParameterSpec resolveKemSpec(AlgorithmParameterSpec spec, String algorithmName, |
| 51 | + String parameterSetName, int sessionKeySize) |
| 52 | + throws InvalidAlgorithmParameterException |
| 53 | + { |
| 54 | + if (spec == null) |
| 55 | + { |
| 56 | + // Do not wrap key, no KDF |
| 57 | + return new KTSParameterSpec.Builder("Generic", sessionKeySize).withNoKdf().build(); |
| 58 | + } |
| 59 | + |
| 60 | + if (!(spec instanceof KTSParameterSpec)) |
| 61 | + { |
| 62 | + throw new InvalidAlgorithmParameterException(algorithmName + " can only accept KTSParameterSpec"); |
| 63 | + } |
| 64 | + |
| 65 | + KTSParameterSpec ktsSpec = (KTSParameterSpec)spec; |
| 66 | + |
| 67 | + // KTSParameterSpec does not check its own algorithm name, and a null one is only noticed |
| 68 | + // once it has been substituted for a "Generic" request and handed to the SecretKey. |
| 69 | + if (ktsSpec.getKeyAlgorithmName() == null) |
| 70 | + { |
| 71 | + throw new InvalidAlgorithmParameterException("KTSParameterSpec has no key algorithm name"); |
| 72 | + } |
| 73 | + |
| 74 | + // Nor does it check its own key size. A size below 8 would yield a zero-length secret key |
| 75 | + // (makeKeyBytes rounds the byte count up while a KEM's secretSize() rounds it down), one |
| 76 | + // that is not a whole number of bytes would silently deliver fewer bits than were asked |
| 77 | + // for, and one within 7 of Integer.MAX_VALUE would overflow the rounding-up itself. |
| 78 | + int keySize = ktsSpec.getKeySize(); |
| 79 | + if (keySize <= 0 || (keySize % 8) != 0) |
| 80 | + { |
| 81 | + throw new InvalidAlgorithmParameterException( |
| 82 | + "KTSParameterSpec key size must be a positive whole number of bytes: " + keySize); |
| 83 | + } |
| 84 | + |
| 85 | + if (ktsSpec.getKdfAlgorithm() == null) |
| 86 | + { |
| 87 | + if (keySize > sessionKeySize) |
| 88 | + { |
| 89 | + throw new InvalidAlgorithmParameterException("no KDF specified and " + parameterSetName |
| 90 | + + " produces a " + sessionKeySize + " bit secret, " + keySize + " requested"); |
| 91 | + } |
| 92 | + } |
| 93 | + else if (!isSupportedKdf(ktsSpec.getKdfAlgorithm())) |
| 94 | + { |
| 95 | + throw new InvalidAlgorithmParameterException("unsupported KDF: " |
| 96 | + + ktsSpec.getKdfAlgorithm().getAlgorithm()); |
| 97 | + } |
| 98 | + |
| 99 | + return ktsSpec; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Reconcile the algorithm name passed to a KEM's encapsulate/decapsulate with the one its |
| 104 | + * KTSParameterSpec names: "Generic" on either side defers to the other, and a genuine mismatch |
| 105 | + * is refused. |
| 106 | + * |
| 107 | + * @param parameterSpec the spec the KEM was created with. |
| 108 | + * @param algorithm the algorithm name the caller asked for. |
| 109 | + * @return the algorithm name to label the secret key with. |
| 110 | + */ |
| 111 | + public static String resolveAlgorithm(KTSParameterSpec parameterSpec, String algorithm) |
| 112 | + { |
| 113 | + String keyAlgName = parameterSpec.getKeyAlgorithmName(); |
| 114 | + |
| 115 | + if ("Generic".equals(keyAlgName)) |
| 116 | + { |
| 117 | + return algorithm; |
| 118 | + } |
| 119 | + // if algorithm is Generic then use parameterSpec to wrap key |
| 120 | + if ("Generic".equals(algorithm)) |
| 121 | + { |
| 122 | + return keyAlgName; |
| 123 | + } |
| 124 | + // check spec algorithm mismatch provided algorithm |
| 125 | + if (!algorithm.equals(keyAlgName)) |
| 126 | + { |
| 127 | + throw new UnsupportedOperationException(keyAlgName + " does not match " + algorithm); |
| 128 | + } |
| 129 | + |
| 130 | + return algorithm; |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Return true if makeKeyBytes can service the passed in KDF algorithm identifier. |
| 135 | + */ |
| 136 | + private static boolean isSupportedKdf(AlgorithmIdentifier kdfAlgorithm) |
| 137 | + { |
| 138 | + ASN1ObjectIdentifier kdfOid = kdfAlgorithm.getAlgorithm(); |
| 139 | + |
| 140 | + try |
| 141 | + { |
| 142 | + if (X9ObjectIdentifiers.id_kdf_kdf2.equals(kdfOid) || X9ObjectIdentifiers.id_kdf_kdf3.equals(kdfOid)) |
| 143 | + { |
| 144 | + if (kdfAlgorithm.getParameters() == null) |
| 145 | + { |
| 146 | + return false; |
| 147 | + } |
| 148 | + |
| 149 | + ASN1ObjectIdentifier digOid = AlgorithmIdentifier.getInstance( |
| 150 | + kdfAlgorithm.getParameters()).getAlgorithm(); |
| 151 | + |
| 152 | + // the digests getDigest() knows |
| 153 | + return NISTObjectIdentifiers.id_sha256.equals(digOid) |
| 154 | + || NISTObjectIdentifiers.id_sha512.equals(digOid) |
| 155 | + || NISTObjectIdentifiers.id_shake128.equals(digOid) |
| 156 | + || NISTObjectIdentifiers.id_shake256.equals(digOid); |
| 157 | + } |
| 158 | + |
| 159 | + if (PKCSObjectIdentifiers.id_alg_hkdf_with_sha256.equals(kdfOid) |
| 160 | + || PKCSObjectIdentifiers.id_alg_hkdf_with_sha384.equals(kdfOid) |
| 161 | + || PKCSObjectIdentifiers.id_alg_hkdf_with_sha512.equals(kdfOid)) |
| 162 | + { |
| 163 | + // HKDF parameter support has not been added |
| 164 | + return kdfAlgorithm.getParameters() == null; |
| 165 | + } |
| 166 | + |
| 167 | + if (NISTObjectIdentifiers.id_Kmac128.equals(kdfOid) || NISTObjectIdentifiers.id_Kmac256.equals(kdfOid)) |
| 168 | + { |
| 169 | + if (kdfAlgorithm.getParameters() != null) |
| 170 | + { |
| 171 | + ASN1OctetString.getInstance(kdfAlgorithm.getParameters()); |
| 172 | + } |
| 173 | + return true; |
| 174 | + } |
| 175 | + |
| 176 | + return NISTObjectIdentifiers.id_shake256.equals(kdfOid); |
| 177 | + } |
| 178 | + catch (IllegalArgumentException e) |
| 179 | + { |
| 180 | + // a parameters field that will not decode as the branch requires |
| 181 | + return false; |
| 182 | + } |
| 183 | + } |
| 184 | + |
28 | 185 | /** |
29 | 186 | * Generate a byte[] secret key from the passed in secret. Note: passed in secret will be erased after use. |
30 | 187 | * |
|
0 commit comments