|
| 1 | +package org.bouncycastle.jcajce.provider.asymmetric.compositekem; |
| 2 | + |
| 3 | +import java.security.InvalidAlgorithmParameterException; |
| 4 | +import java.security.KeyPair; |
| 5 | +import java.security.KeyPairGenerator; |
| 6 | +import java.security.PrivateKey; |
| 7 | +import java.security.PublicKey; |
| 8 | +import java.security.SecureRandom; |
| 9 | +import java.security.spec.AlgorithmParameterSpec; |
| 10 | + |
| 11 | +import org.bouncycastle.asn1.ASN1ObjectIdentifier; |
| 12 | +import org.bouncycastle.internal.asn1.iana.IANAObjectIdentifiers; |
| 13 | +import org.bouncycastle.jcajce.CompositePrivateKey; |
| 14 | +import org.bouncycastle.jcajce.CompositePublicKey; |
| 15 | +import org.bouncycastle.util.Exceptions; |
| 16 | + |
| 17 | +/** |
| 18 | + * KeyPairGenerator for Composite ML-KEM (draft-ietf-lamps-pq-composite-kem). The concrete composite |
| 19 | + * is selected by the subclasses at the end of this file. The generated keys are a |
| 20 | + * {@link CompositePublicKey} / {@link CompositePrivateKey} pair whose components are, in order, the |
| 21 | + * ML-KEM key and the traditional (RSA / ECDH / X25519 / X448) key. |
| 22 | + */ |
| 23 | +public class KeyPairGeneratorSpi |
| 24 | + extends java.security.KeyPairGeneratorSpi |
| 25 | +{ |
| 26 | + private final ASN1ObjectIdentifier algorithm; |
| 27 | + private final KeyPairGenerator[] generators; |
| 28 | + |
| 29 | + KeyPairGeneratorSpi(ASN1ObjectIdentifier algorithm) |
| 30 | + { |
| 31 | + this.algorithm = algorithm; |
| 32 | + |
| 33 | + String[] algorithms = CompositeIndex.getPairing(algorithm); |
| 34 | + AlgorithmParameterSpec[] initSpecs = CompositeIndex.getKeyPairSpecs(algorithm); |
| 35 | + |
| 36 | + this.generators = new KeyPairGenerator[algorithms.length]; |
| 37 | + for (int i = 0; i != algorithms.length; i++) |
| 38 | + { |
| 39 | + try |
| 40 | + { |
| 41 | + this.generators[i] = KeyPairGenerator.getInstance(CompositeIndex.getBaseName(algorithms[i]), "BC"); |
| 42 | + |
| 43 | + AlgorithmParameterSpec initSpec = initSpecs[i]; |
| 44 | + if (initSpec != null) |
| 45 | + { |
| 46 | + this.generators[i].initialize(initSpec); |
| 47 | + } |
| 48 | + } |
| 49 | + catch (Exception e) |
| 50 | + { |
| 51 | + throw Exceptions.illegalStateException("unable to create base generator: " + e.getMessage(), e); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * There is no notion of a key size for composite KEMs - the parameter set is fixed by the |
| 58 | + * algorithm. Use {@link #initialize(AlgorithmParameterSpec, SecureRandom)} (with a null spec) |
| 59 | + * only to supply a custom SecureRandom. |
| 60 | + */ |
| 61 | + public void initialize(int keySize, SecureRandom random) |
| 62 | + { |
| 63 | + throw new IllegalArgumentException("use AlgorithmParameterSpec"); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * A custom AlgorithmParameterSpec is not supported - the composite parameter set is determined |
| 68 | + * by the algorithm name. This method only serves to set a custom SecureRandom on the component |
| 69 | + * generators. |
| 70 | + * |
| 71 | + * @param paramSpec must be null. |
| 72 | + * @param secureRandom a SecureRandom used by the component key generators. |
| 73 | + */ |
| 74 | + public void initialize(AlgorithmParameterSpec paramSpec, SecureRandom secureRandom) |
| 75 | + throws InvalidAlgorithmParameterException |
| 76 | + { |
| 77 | + if (paramSpec != null) |
| 78 | + { |
| 79 | + throw new IllegalArgumentException("use initialize only for custom SecureRandom; AlgorithmParameterSpec must be null because it is determined by the algorithm name"); |
| 80 | + } |
| 81 | + |
| 82 | + AlgorithmParameterSpec[] initSpecs = CompositeIndex.getKeyPairSpecs(algorithm); |
| 83 | + for (int i = 0; i != initSpecs.length; i++) |
| 84 | + { |
| 85 | + AlgorithmParameterSpec initSpec = initSpecs[i]; |
| 86 | + if (initSpec != null) |
| 87 | + { |
| 88 | + this.generators[i].initialize(initSpec, secureRandom); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + public KeyPair generateKeyPair() |
| 94 | + { |
| 95 | + PublicKey[] publicKeys = new PublicKey[generators.length]; |
| 96 | + PrivateKey[] privateKeys = new PrivateKey[generators.length]; |
| 97 | + for (int i = 0; i < generators.length; i++) |
| 98 | + { |
| 99 | + KeyPair keyPair = generators[i].generateKeyPair(); |
| 100 | + publicKeys[i] = keyPair.getPublic(); |
| 101 | + privateKeys[i] = keyPair.getPrivate(); |
| 102 | + } |
| 103 | + CompositePublicKey compositePublicKey = new CompositePublicKey(this.algorithm, publicKeys); |
| 104 | + CompositePrivateKey compositePrivateKey = new CompositePrivateKey(this.algorithm, privateKeys); |
| 105 | + return new KeyPair(compositePublicKey, compositePrivateKey); |
| 106 | + } |
| 107 | + |
| 108 | + public static final class MLKEM768_RSA2048_SHA3_256 |
| 109 | + extends KeyPairGeneratorSpi |
| 110 | + { |
| 111 | + public MLKEM768_RSA2048_SHA3_256() |
| 112 | + { |
| 113 | + super(IANAObjectIdentifiers.id_MLKEM768_RSA2048_SHA3_256); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + public static final class MLKEM768_RSA3072_SHA3_256 |
| 118 | + extends KeyPairGeneratorSpi |
| 119 | + { |
| 120 | + public MLKEM768_RSA3072_SHA3_256() |
| 121 | + { |
| 122 | + super(IANAObjectIdentifiers.id_MLKEM768_RSA3072_SHA3_256); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + public static final class MLKEM768_RSA4096_SHA3_256 |
| 127 | + extends KeyPairGeneratorSpi |
| 128 | + { |
| 129 | + public MLKEM768_RSA4096_SHA3_256() |
| 130 | + { |
| 131 | + super(IANAObjectIdentifiers.id_MLKEM768_RSA4096_SHA3_256); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + public static final class MLKEM768_X25519_SHA3_256 |
| 136 | + extends KeyPairGeneratorSpi |
| 137 | + { |
| 138 | + public MLKEM768_X25519_SHA3_256() |
| 139 | + { |
| 140 | + super(IANAObjectIdentifiers.id_MLKEM768_X25519_SHA3_256); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + public static final class MLKEM768_ECDH_P256_SHA3_256 |
| 145 | + extends KeyPairGeneratorSpi |
| 146 | + { |
| 147 | + public MLKEM768_ECDH_P256_SHA3_256() |
| 148 | + { |
| 149 | + super(IANAObjectIdentifiers.id_MLKEM768_ECDH_P256_SHA3_256); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + public static final class MLKEM768_ECDH_P384_SHA3_256 |
| 154 | + extends KeyPairGeneratorSpi |
| 155 | + { |
| 156 | + public MLKEM768_ECDH_P384_SHA3_256() |
| 157 | + { |
| 158 | + super(IANAObjectIdentifiers.id_MLKEM768_ECDH_P384_SHA3_256); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + public static final class MLKEM768_ECDH_BP256_SHA3_256 |
| 163 | + extends KeyPairGeneratorSpi |
| 164 | + { |
| 165 | + public MLKEM768_ECDH_BP256_SHA3_256() |
| 166 | + { |
| 167 | + super(IANAObjectIdentifiers.id_MLKEM768_ECDH_BP256_SHA3_256); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + public static final class MLKEM1024_RSA3072_SHA3_256 |
| 172 | + extends KeyPairGeneratorSpi |
| 173 | + { |
| 174 | + public MLKEM1024_RSA3072_SHA3_256() |
| 175 | + { |
| 176 | + super(IANAObjectIdentifiers.id_MLKEM1024_RSA3072_SHA3_256); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + public static final class MLKEM1024_ECDH_P384_SHA3_256 |
| 181 | + extends KeyPairGeneratorSpi |
| 182 | + { |
| 183 | + public MLKEM1024_ECDH_P384_SHA3_256() |
| 184 | + { |
| 185 | + super(IANAObjectIdentifiers.id_MLKEM1024_ECDH_P384_SHA3_256); |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + public static final class MLKEM1024_ECDH_BP384_SHA3_256 |
| 190 | + extends KeyPairGeneratorSpi |
| 191 | + { |
| 192 | + public MLKEM1024_ECDH_BP384_SHA3_256() |
| 193 | + { |
| 194 | + super(IANAObjectIdentifiers.id_MLKEM1024_ECDH_BP384_SHA3_256); |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + public static final class MLKEM1024_X448_SHA3_256 |
| 199 | + extends KeyPairGeneratorSpi |
| 200 | + { |
| 201 | + public MLKEM1024_X448_SHA3_256() |
| 202 | + { |
| 203 | + super(IANAObjectIdentifiers.id_MLKEM1024_X448_SHA3_256); |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + public static final class MLKEM1024_ECDH_P521_SHA3_256 |
| 208 | + extends KeyPairGeneratorSpi |
| 209 | + { |
| 210 | + public MLKEM1024_ECDH_P521_SHA3_256() |
| 211 | + { |
| 212 | + super(IANAObjectIdentifiers.id_MLKEM1024_ECDH_P521_SHA3_256); |
| 213 | + } |
| 214 | + } |
| 215 | +} |
0 commit comments