use of javax.crypto.spec.DHParameterSpec in project robovm by robovm.
the class DHParameterSpecTest method testDHParameterSpec.
/**
* DHParameterSpec class testing. Tests the equivalence of parameters
* specified in the constructor with the values returned by getters.
* The tested object is created by different constructors.
*/
public void testDHParameterSpec() {
BigInteger[] ps = { new BigInteger("-1000000000000"), BigInteger.ZERO, BigInteger.ONE, new BigInteger("1000000000000") };
BigInteger[] gs = { new BigInteger("-1000000000000"), BigInteger.ZERO, BigInteger.ONE, new BigInteger("1000000000000") };
int[] ls = { Integer.MIN_VALUE, 0, 1, Integer.MAX_VALUE };
for (int i = 0; i < ps.length; i++) {
DHParameterSpec dhps = new DHParameterSpec(ps[i], gs[i]);
assertEquals("The value returned by getP() must be " + "equal to the value specified in the constructor", dhps.getP(), ps[i]);
assertEquals("The value returned by getG() must be " + "equal to the value specified in the constructor", dhps.getG(), gs[i]);
}
for (int i = 0; i < ps.length; i++) {
DHParameterSpec dhps = new DHParameterSpec(ps[i], gs[i], ls[i]);
assertEquals("The value returned by getP() must be " + "equal to the value specified in the constructor", dhps.getP(), ps[i]);
assertEquals("The value returned by getG() must be " + "equal to the value specified in the constructor", dhps.getG(), gs[i]);
assertEquals("The value returned by getL() must be " + "equal to the value specified in the constructor", dhps.getL(), ls[i]);
}
}
use of javax.crypto.spec.DHParameterSpec in project geode by apache.
the class GMSEncrypt method initDHKeys.
/**
* Initialize the Diffie-Hellman keys. This method is not thread safe
*/
private void initDHKeys(DistributionConfig config) throws Exception {
dhSKAlgo = config.getSecurityUDPDHAlgo();
// that has authenticator defined.
if ((dhSKAlgo != null && dhSKAlgo.length() > 0)) {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DH");
DHParameterSpec dhSpec = new DHParameterSpec(dhP, dhG, dhL);
keyGen.initialize(dhSpec);
KeyPair keypair = keyGen.generateKeyPair();
// Get the generated public and private keys
dhPrivateKey = keypair.getPrivate();
dhPublicKey = keypair.getPublic();
}
}
use of javax.crypto.spec.DHParameterSpec in project jdk8u_jdk by JetBrains.
the class ParameterCache method getDHParameterSpec.
/**
* Return DH parameters for the given keylength. Uses cache if possible,
* generates new parameters and adds them to the cache otherwise.
*/
public static DHParameterSpec getDHParameterSpec(int keyLength, SecureRandom random) throws NoSuchAlgorithmException, InvalidParameterSpecException {
DHParameterSpec spec = getCachedDHParameterSpec(keyLength);
if (spec != null) {
return spec;
}
AlgorithmParameterGenerator gen = AlgorithmParameterGenerator.getInstance("DH");
gen.init(keyLength, random);
AlgorithmParameters params = gen.generateParameters();
spec = params.getParameterSpec(DHParameterSpec.class);
dhCache.put(Integer.valueOf(keyLength), spec);
return spec;
}
use of javax.crypto.spec.DHParameterSpec in project jdk8u_jdk by JetBrains.
the class P11KeyPairGenerator method generateKeyPair.
// see JCA spec
public KeyPair generateKeyPair() {
token.ensureValid();
CK_ATTRIBUTE[] publicKeyTemplate;
CK_ATTRIBUTE[] privateKeyTemplate;
long keyType;
if (algorithm.equals("RSA")) {
keyType = CKK_RSA;
publicKeyTemplate = new CK_ATTRIBUTE[] { new CK_ATTRIBUTE(CKA_MODULUS_BITS, keySize), new CK_ATTRIBUTE(CKA_PUBLIC_EXPONENT, rsaPublicExponent) };
privateKeyTemplate = new CK_ATTRIBUTE[] {};
} else if (algorithm.equals("DSA")) {
keyType = CKK_DSA;
DSAParameterSpec dsaParams;
if (params == null) {
try {
dsaParams = ParameterCache.getDSAParameterSpec(keySize, random);
} catch (GeneralSecurityException e) {
throw new ProviderException("Could not generate DSA parameters", e);
}
} else {
dsaParams = (DSAParameterSpec) params;
}
publicKeyTemplate = new CK_ATTRIBUTE[] { new CK_ATTRIBUTE(CKA_PRIME, dsaParams.getP()), new CK_ATTRIBUTE(CKA_SUBPRIME, dsaParams.getQ()), new CK_ATTRIBUTE(CKA_BASE, dsaParams.getG()) };
privateKeyTemplate = new CK_ATTRIBUTE[] {};
} else if (algorithm.equals("DH")) {
keyType = CKK_DH;
DHParameterSpec dhParams;
int privateBits;
if (params == null) {
try {
dhParams = ParameterCache.getDHParameterSpec(keySize, random);
} catch (GeneralSecurityException e) {
throw new ProviderException("Could not generate DH parameters", e);
}
privateBits = 0;
} else {
dhParams = (DHParameterSpec) params;
privateBits = dhParams.getL();
}
if (privateBits <= 0) {
// XXX find better defaults
privateBits = (keySize >= 1024) ? 768 : 512;
}
publicKeyTemplate = new CK_ATTRIBUTE[] { new CK_ATTRIBUTE(CKA_PRIME, dhParams.getP()), new CK_ATTRIBUTE(CKA_BASE, dhParams.getG()) };
privateKeyTemplate = new CK_ATTRIBUTE[] { new CK_ATTRIBUTE(CKA_VALUE_BITS, privateBits) };
} else if (algorithm.equals("EC")) {
keyType = CKK_EC;
byte[] encodedParams = P11ECKeyFactory.encodeParameters((ECParameterSpec) params);
publicKeyTemplate = new CK_ATTRIBUTE[] { new CK_ATTRIBUTE(CKA_EC_PARAMS, encodedParams) };
privateKeyTemplate = new CK_ATTRIBUTE[] {};
} else {
throw new ProviderException("Unknown algorithm: " + algorithm);
}
Session session = null;
try {
session = token.getObjSession();
publicKeyTemplate = token.getAttributes(O_GENERATE, CKO_PUBLIC_KEY, keyType, publicKeyTemplate);
privateKeyTemplate = token.getAttributes(O_GENERATE, CKO_PRIVATE_KEY, keyType, privateKeyTemplate);
long[] keyIDs = token.p11.C_GenerateKeyPair(session.id(), new CK_MECHANISM(mechanism), publicKeyTemplate, privateKeyTemplate);
PublicKey publicKey = P11Key.publicKey(session, keyIDs[0], algorithm, keySize, publicKeyTemplate);
PrivateKey privateKey = P11Key.privateKey(session, keyIDs[1], algorithm, keySize, privateKeyTemplate);
return new KeyPair(publicKey, privateKey);
} catch (PKCS11Exception e) {
throw new ProviderException(e);
} finally {
token.releaseSession(session);
}
}
use of javax.crypto.spec.DHParameterSpec in project jdk8u_jdk by JetBrains.
the class P11KeyPairGenerator method initialize.
// see JCA spec
public void initialize(AlgorithmParameterSpec params, SecureRandom random) throws InvalidAlgorithmParameterException {
token.ensureValid();
int tmpKeySize;
if (algorithm.equals("DH")) {
if (params instanceof DHParameterSpec == false) {
throw new InvalidAlgorithmParameterException("DHParameterSpec required for Diffie-Hellman");
}
DHParameterSpec dhParams = (DHParameterSpec) params;
tmpKeySize = dhParams.getP().bitLength();
checkKeySize(tmpKeySize, null);
// XXX sanity check params
} else if (algorithm.equals("RSA")) {
if (params instanceof RSAKeyGenParameterSpec == false) {
throw new InvalidAlgorithmParameterException("RSAKeyGenParameterSpec required for RSA");
}
RSAKeyGenParameterSpec rsaParams = (RSAKeyGenParameterSpec) params;
tmpKeySize = rsaParams.getKeysize();
checkKeySize(tmpKeySize, rsaParams);
// override the supplied params to null
params = null;
this.rsaPublicExponent = rsaParams.getPublicExponent();
// XXX sanity check params
} else if (algorithm.equals("DSA")) {
if (params instanceof DSAParameterSpec == false) {
throw new InvalidAlgorithmParameterException("DSAParameterSpec required for DSA");
}
DSAParameterSpec dsaParams = (DSAParameterSpec) params;
tmpKeySize = dsaParams.getP().bitLength();
checkKeySize(tmpKeySize, null);
// XXX sanity check params
} else if (algorithm.equals("EC")) {
ECParameterSpec ecParams;
if (params instanceof ECParameterSpec) {
ecParams = P11ECKeyFactory.getECParameterSpec((ECParameterSpec) params);
if (ecParams == null) {
throw new InvalidAlgorithmParameterException("Unsupported curve: " + params);
}
} else if (params instanceof ECGenParameterSpec) {
String name = ((ECGenParameterSpec) params).getName();
ecParams = P11ECKeyFactory.getECParameterSpec(name);
if (ecParams == null) {
throw new InvalidAlgorithmParameterException("Unknown curve name: " + name);
}
// override the supplied params with the derived one
params = ecParams;
} else {
throw new InvalidAlgorithmParameterException("ECParameterSpec or ECGenParameterSpec required for EC");
}
tmpKeySize = ecParams.getCurve().getField().getFieldSize();
checkKeySize(tmpKeySize, null);
} else {
throw new ProviderException("Unknown algorithm: " + algorithm);
}
this.keySize = tmpKeySize;
this.params = params;
this.random = random;
}
Aggregations