org.bouncycastle.crypto.general.EcKeyPairGenerator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bc-fips Show documentation
Show all versions of bc-fips Show documentation
The FIPS 140-3 Bouncy Castle Crypto package is a Java implementation of cryptographic algorithms certified to FIPS 140-3 level 1. This jar contains JCE provider and low-level API for the BC-FJA version 2.0.0, FIPS Certificate #4743. Please see certificate for certified platform details.
/***************************************************************/
/****** DO NOT EDIT THIS CLASS bc-java SOURCE FILE ******/
/***************************************************************/
package org.bouncycastle.crypto.general;
import java.math.BigInteger;
import java.security.SecureRandom;
import org.bouncycastle.crypto.internal.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.internal.AsymmetricCipherKeyPairGenerator;
import org.bouncycastle.crypto.internal.KeyGenerationParameters;
import org.bouncycastle.crypto.internal.params.EcDomainParameters;
import org.bouncycastle.crypto.internal.params.EcPrivateKeyParameters;
import org.bouncycastle.crypto.internal.params.EcPublicKeyParameters;
import org.bouncycastle.math.ec.ECConstants;
import org.bouncycastle.math.ec.ECMultiplier;
import org.bouncycastle.math.ec.ECPoint;
import org.bouncycastle.math.ec.FixedPointCombMultiplier;
import org.bouncycastle.math.ec.WNafUtil;
class EcKeyPairGenerator
implements AsymmetricCipherKeyPairGenerator, ECConstants
{
EcDomainParameters params;
SecureRandom random;
public void init(
KeyGenerationParameters param)
{
EcKeyGenerationParameters ecP = (EcKeyGenerationParameters)param;
this.random = ecP.getRandom();
this.params = ecP.getDomainParameters();
if (this.random == null)
{
throw new IllegalArgumentException("No random provided where one required.");
}
}
/**
* Given the domain parameters this routine generates an EC key
* pair in accordance with X9.62 section 5.2.1 pages 26, 27.
*/
public AsymmetricCipherKeyPair generateKeyPair()
{
BigInteger n = params.getN();
int nBitLength = n.bitLength();
int minWeight = nBitLength >>> 2;
BigInteger d;
for (;;)
{
d = new BigInteger(nBitLength, random);
if (d.compareTo(TWO) < 0 || (d.compareTo(n) >= 0))
{
continue;
}
/*
* Require a minimum weight of the NAF representation, since low-weight primes may be
* weak against a version of the number-field-sieve for the discrete-logarithm-problem.
*
* See "The number field sieve for integers of low weight", Oliver Schirokauer.
*/
if (WNafUtil.getNafWeight(d) < minWeight)
{
continue;
}
break;
}
ECPoint Q = createBasePointMultiplier().multiply(params.getG(), d);
return new AsymmetricCipherKeyPair(
new EcPublicKeyParameters(Q, params),
new EcPrivateKeyParameters(d, params));
}
protected ECMultiplier createBasePointMultiplier()
{
return new FixedPointCombMultiplier();
}
}