~brian-thomason/+junk/bouncycastle

« back to all changes in this revision

Viewing changes to src/org/bouncycastle/crypto/generators/ECKeyPairGenerator.java

  • Committer: Brian Thomason
  • Date: 2011-12-20 17:20:32 UTC
  • Revision ID: brian.thomason@canonical.com-20111220172032-rdtm13jgdxtksacr
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.bouncycastle.crypto.generators;
 
2
 
 
3
import java.math.BigInteger;
 
4
import java.security.SecureRandom;
 
5
 
 
6
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
 
7
import org.bouncycastle.crypto.AsymmetricCipherKeyPairGenerator;
 
8
import org.bouncycastle.crypto.KeyGenerationParameters;
 
9
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
 
17
    implements AsymmetricCipherKeyPairGenerator, ECConstants
 
18
{
 
19
    ECDomainParameters  params;
 
20
    SecureRandom        random;
 
21
 
 
22
    public void init(
 
23
        KeyGenerationParameters param)
 
24
    {
 
25
        ECKeyGenerationParameters  ecP = (ECKeyGenerationParameters)param;
 
26
 
 
27
        this.random = ecP.getRandom();
 
28
        this.params = ecP.getDomainParameters();
 
29
    }
 
30
 
 
31
    /**
 
32
     * Given the domain parameters this routine generates an EC key
 
33
     * pair in accordance with X9.62 section 5.2.1 pages 26, 27.
 
34
     */
 
35
    public AsymmetricCipherKeyPair generateKeyPair()
 
36
    {
 
37
        BigInteger n = params.getN();
 
38
        int        nBitLength = n.bitLength();
 
39
        BigInteger d;
 
40
 
 
41
        do
 
42
        {
 
43
            d = new BigInteger(nBitLength, random);
 
44
        }
 
45
        while (d.equals(ZERO)  || (d.compareTo(n) >= 0));
 
46
 
 
47
        ECPoint Q = params.getG().multiply(d);
 
48
 
 
49
        return new AsymmetricCipherKeyPair(
 
50
            new ECPublicKeyParameters(Q, params),
 
51
            new ECPrivateKeyParameters(d, params));
 
52
    }
 
53
}