01: package org.bouncycastle.crypto.generators;
02:
03: import java.math.BigInteger;
04: import java.security.SecureRandom;
05:
06: import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
07: import org.bouncycastle.crypto.AsymmetricCipherKeyPairGenerator;
08: import org.bouncycastle.crypto.KeyGenerationParameters;
09: import org.bouncycastle.crypto.params.ECDomainParameters;
10: import org.bouncycastle.crypto.params.ECKeyGenerationParameters;
11: import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
12: import org.bouncycastle.crypto.params.ECPublicKeyParameters;
13: import org.bouncycastle.math.ec.ECConstants;
14: import org.bouncycastle.math.ec.ECPoint;
15:
16: public class ECKeyPairGenerator implements
17: AsymmetricCipherKeyPairGenerator, ECConstants {
18: ECDomainParameters params;
19: SecureRandom random;
20:
21: public void init(KeyGenerationParameters param) {
22: ECKeyGenerationParameters ecP = (ECKeyGenerationParameters) param;
23:
24: this .random = ecP.getRandom();
25: this .params = ecP.getDomainParameters();
26: }
27:
28: /**
29: * Given the domain parameters this routine generates an EC key
30: * pair in accordance with X9.62 section 5.2.1 pages 26, 27.
31: */
32: public AsymmetricCipherKeyPair generateKeyPair() {
33: BigInteger n = params.getN();
34: int nBitLength = n.bitLength();
35: BigInteger d;
36:
37: do {
38: d = new BigInteger(nBitLength, random);
39: } while (d.equals(ZERO) || (d.compareTo(n) >= 0));
40:
41: ECPoint Q = params.getG().multiply(d);
42:
43: return new AsymmetricCipherKeyPair(new ECPublicKeyParameters(Q,
44: params), new ECPrivateKeyParameters(d, params));
45: }
46: }
|