01: package org.bouncycastle.crypto.generators;
02:
03: import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
04: import org.bouncycastle.crypto.AsymmetricCipherKeyPairGenerator;
05: import org.bouncycastle.crypto.KeyGenerationParameters;
06: import org.bouncycastle.crypto.params.GOST3410KeyGenerationParameters;
07: import org.bouncycastle.crypto.params.GOST3410Parameters;
08: import org.bouncycastle.crypto.params.GOST3410PrivateKeyParameters;
09: import org.bouncycastle.crypto.params.GOST3410PublicKeyParameters;
10:
11: import java.math.BigInteger;
12: import java.security.SecureRandom;
13:
14: /**
15: * a GOST3410 key pair generator.
16: * This generates GOST3410 keys in line with the method described
17: * in GOST R 34.10-94.
18: */
19: public class GOST3410KeyPairGenerator implements
20: AsymmetricCipherKeyPairGenerator {
21: private static final BigInteger ZERO = BigInteger.valueOf(0);
22:
23: private GOST3410KeyGenerationParameters param;
24:
25: public void init(KeyGenerationParameters param) {
26: this .param = (GOST3410KeyGenerationParameters) param;
27: }
28:
29: public AsymmetricCipherKeyPair generateKeyPair() {
30: BigInteger p, q, a, x, y;
31: GOST3410Parameters GOST3410Params = param.getParameters();
32: SecureRandom random = param.getRandom();
33:
34: q = GOST3410Params.getQ();
35: p = GOST3410Params.getP();
36: a = GOST3410Params.getA();
37:
38: do {
39: x = new BigInteger(256, random);
40: } while (x.equals(ZERO) || x.compareTo(q) >= 0);
41:
42: //
43: // calculate the public key.
44: //
45: y = a.modPow(x, p);
46:
47: return new AsymmetricCipherKeyPair(
48: new GOST3410PublicKeyParameters(y, GOST3410Params),
49: new GOST3410PrivateKeyParameters(x, GOST3410Params));
50: }
51: }
|