javax.crypto.spec.DHParameterSpec Java Examples

The following examples show how to use javax.crypto.spec.DHParameterSpec. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ParameterCache.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #2
Source File: ParameterCache.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #3
Source File: ParameterCache.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #4
Source File: DHKeyPairGenerator.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Initializes this key pair generator for the specified parameter
 * set and source of randomness.
 *
 * <p>The given parameter set contains the prime modulus, the base
 * generator, and optionally the requested size in bits of the random
 * exponent (private value).
 *
 * @param algParams the parameter set used to generate the key pair
 * @param random the source of randomness
 *
 * @exception InvalidAlgorithmParameterException if the given parameters
 * are inappropriate for this key pair generator
 */
public void initialize(AlgorithmParameterSpec algParams,
        SecureRandom random) throws InvalidAlgorithmParameterException {
    if (!(algParams instanceof DHParameterSpec)){
        throw new InvalidAlgorithmParameterException
            ("Inappropriate parameter type");
    }

    params = (DHParameterSpec)algParams;
    pSize = params.getP().bitLength();
    try {
        checkKeySize(pSize);
    } catch (InvalidParameterException ipe) {
        throw new InvalidAlgorithmParameterException(ipe.getMessage());
    }

    // exponent size is optional, could be 0
    lSize = params.getL();

    // Require exponentSize < primeSize
    if ((lSize != 0) && (lSize > pSize)) {
        throw new InvalidAlgorithmParameterException
            ("Exponent size must not be larger than modulus size");
    }
    this.random = random;
}
 
Example #5
Source File: ParameterCache.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #6
Source File: DHKeyPairGenerator.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Initializes this key pair generator for the specified parameter
 * set and source of randomness.
 *
 * <p>The given parameter set contains the prime modulus, the base
 * generator, and optionally the requested size in bits of the random
 * exponent (private value).
 *
 * @param algParams the parameter set used to generate the key pair
 * @param random the source of randomness
 *
 * @exception InvalidAlgorithmParameterException if the given parameters
 * are inappropriate for this key pair generator
 */
public void initialize(AlgorithmParameterSpec algParams,
        SecureRandom random) throws InvalidAlgorithmParameterException {
    if (!(algParams instanceof DHParameterSpec)){
        throw new InvalidAlgorithmParameterException
            ("Inappropriate parameter type");
    }

    params = (DHParameterSpec)algParams;
    pSize = params.getP().bitLength();
    try {
        checkKeySize(pSize);
    } catch (InvalidParameterException ipe) {
        throw new InvalidAlgorithmParameterException(ipe.getMessage());
    }

    // exponent size is optional, could be 0
    lSize = params.getL();

    // Require exponentSize < primeSize
    if ((lSize != 0) && (lSize > pSize)) {
        throw new InvalidAlgorithmParameterException
            ("Exponent size must not be larger than modulus size");
    }
    this.random = random;
}
 
Example #7
Source File: DHKeyPairGenerator.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Initializes this key pair generator for the specified parameter
 * set and source of randomness.
 *
 * <p>The given parameter set contains the prime modulus, the base
 * generator, and optionally the requested size in bits of the random
 * exponent (private value).
 *
 * @param params the parameter set used to generate the key pair
 * @param random the source of randomness
 *
 * @exception InvalidAlgorithmParameterException if the given parameters
 * are inappropriate for this key pair generator
 */
public void initialize(AlgorithmParameterSpec algParams,
        SecureRandom random) throws InvalidAlgorithmParameterException {
    if (!(algParams instanceof DHParameterSpec)){
        throw new InvalidAlgorithmParameterException
            ("Inappropriate parameter type");
    }

    params = (DHParameterSpec)algParams;
    pSize = params.getP().bitLength();
    try {
        checkKeySize(pSize);
    } catch (InvalidParameterException ipe) {
        throw new InvalidAlgorithmParameterException(ipe.getMessage());
    }

    // exponent size is optional, could be 0
    lSize = params.getL();

    // Require exponentSize < primeSize
    if ((lSize != 0) && (lSize > pSize)) {
        throw new InvalidAlgorithmParameterException
            ("Exponent size must not be larger than modulus size");
    }
    this.random = random;
}
 
Example #8
Source File: DHKeyPairGenerator.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Initializes this key pair generator for the specified parameter
 * set and source of randomness.
 *
 * <p>The given parameter set contains the prime modulus, the base
 * generator, and optionally the requested size in bits of the random
 * exponent (private value).
 *
 * @param params the parameter set used to generate the key pair
 * @param random the source of randomness
 *
 * @exception InvalidAlgorithmParameterException if the given parameters
 * are inappropriate for this key pair generator
 */
public void initialize(AlgorithmParameterSpec algParams,
        SecureRandom random) throws InvalidAlgorithmParameterException {
    if (!(algParams instanceof DHParameterSpec)){
        throw new InvalidAlgorithmParameterException
            ("Inappropriate parameter type");
    }

    params = (DHParameterSpec)algParams;
    pSize = params.getP().bitLength();
    try {
        checkKeySize(pSize);
    } catch (InvalidParameterException ipe) {
        throw new InvalidAlgorithmParameterException(ipe.getMessage());
    }

    // exponent size is optional, could be 0
    lSize = params.getL();

    // Require exponentSize < primeSize
    if ((lSize != 0) && (lSize > pSize)) {
        throw new InvalidAlgorithmParameterException
            ("Exponent size must not be larger than modulus size");
    }
    this.random = random;
}
 
Example #9
Source File: RTMPHandshake.java    From red5-server-common with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a Diffie-Hellman key pair.
 * 
 * @return dh keypair
 */
protected KeyPair generateKeyPair() {
    KeyPair keyPair = null;
    DHParameterSpec keySpec = new DHParameterSpec(DH_MODULUS, DH_BASE);
    try {
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DH");
        keyGen.initialize(keySpec);
        keyPair = keyGen.generateKeyPair();
        keyAgreement = KeyAgreement.getInstance("DH");
        // key agreement is initialized with "this" ends private key
        keyAgreement.init(keyPair.getPrivate());
    } catch (Exception e) {
        log.error("Error generating keypair", e);
    }
    return keyPair;
}
 
Example #10
Source File: SupportedGroupsExtension.java    From openjsse with GNU General Public License v2.0 6 votes vote down vote up
static DHParameterSpec getDHParameterSpec(NamedGroup namedGroup) {
    if (namedGroup.type != NamedGroupType.NAMED_GROUP_FFDHE) {
        throw new RuntimeException(
                "Not a named DH group: " + namedGroup);
    }

    AlgorithmParameters params = namedGroupParams.get(namedGroup);
    if (params == null) {
        throw new RuntimeException(
                "Not a supported DH named group: " + namedGroup);
    }

    try {
        return params.getParameterSpec(DHParameterSpec.class);
    } catch (InvalidParameterSpecException ipse) {
        // should be unlikely
        return getPredefinedDHParameterSpec(namedGroup);
    }
}
 
Example #11
Source File: KeyPairGeneratorSpi.java    From ripple-lib-java with ISC License 6 votes vote down vote up
public void initialize(
    AlgorithmParameterSpec params,
    SecureRandom random)
    throws InvalidAlgorithmParameterException
{
    if (!(params instanceof DHParameterSpec))
    {
        throw new InvalidAlgorithmParameterException("parameter object not a DHParameterSpec");
    }
    DHParameterSpec dhParams = (DHParameterSpec)params;

    param = new DHKeyGenerationParameters(random, new DHParameters(dhParams.getP(), dhParams.getG(), null, dhParams.getL()));

    engine.init(param);
    initialised = true;
}
 
Example #12
Source File: AlgorithmParametersSpi.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
protected AlgorithmParameterSpec localEngineGetParameterSpec(
    Class paramSpec) 
    throws InvalidParameterSpecException
{
    if (paramSpec == DHParameterSpec.class)
    {
        return currentSpec;
    }

    throw new InvalidParameterSpecException("unknown parameter spec passed to DH parameters object.");
}
 
Example #13
Source File: DHPublicKey.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the key parameters.
 *
 * @return the key parameters
 */
public DHParameterSpec getParams() {
    if (this.l != 0) {
        return new DHParameterSpec(this.p, this.g, this.l);
    } else {
        return new DHParameterSpec(this.p, this.g);
    }
}
 
Example #14
Source File: KeyUtil.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Returns whether the Diffie-Hellman public key is valid or not.
 *
 * Per RFC 2631 and NIST SP800-56A, the following algorithm is used to
 * validate Diffie-Hellman public keys:
 * 1. Verify that y lies within the interval [2,p-1]. If it does not,
 *    the key is invalid.
 * 2. Compute y^q mod p. If the result == 1, the key is valid.
 *    Otherwise the key is invalid.
 */
private static void validateDHPublicKey(DHPublicKey publicKey)
        throws InvalidKeyException {
    DHParameterSpec paramSpec = publicKey.getParams();

    BigInteger p = paramSpec.getP();
    BigInteger g = paramSpec.getG();
    BigInteger y = publicKey.getY();

    validateDHPublicKey(p, g, y);
}
 
Example #15
Source File: AlgorithmParameterGeneratorSpi.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
protected AlgorithmParameters engineGenerateParameters()
{
    ElGamalParametersGenerator pGen = new ElGamalParametersGenerator();

    if (random != null)
    {
        pGen.init(strength, 20, random);
    }
    else
    {
        pGen.init(strength, 20, new SecureRandom());
    }

    ElGamalParameters p = pGen.generateParameters();

    AlgorithmParameters params;

    try
    {
        params = createParametersInstance("ElGamal");
        params.init(new DHParameterSpec(p.getP(), p.getG(), l));
    }
    catch (Exception e)
    {
        throw new RuntimeException(e.getMessage());
    }

    return params;
}
 
Example #16
Source File: DHParameters.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
protected void engineInit(AlgorithmParameterSpec paramSpec)
    throws InvalidParameterSpecException {
        if (!(paramSpec instanceof DHParameterSpec)) {
            throw new InvalidParameterSpecException
                ("Inappropriate parameter specification");
        }
        this.p = ((DHParameterSpec)paramSpec).getP();
        this.g = ((DHParameterSpec)paramSpec).getG();
        this.l = ((DHParameterSpec)paramSpec).getL();
}
 
Example #17
Source File: AlgorithmParametersSpi.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
protected AlgorithmParameterSpec localEngineGetParameterSpec(
    Class paramSpec)
    throws InvalidParameterSpecException
{
    if (paramSpec == ElGamalParameterSpec.class)
    {
        return currentSpec;
    }
    else if (paramSpec == DHParameterSpec.class)
    {
        return new DHParameterSpec(currentSpec.getP(), currentSpec.getG());
    }

    throw new InvalidParameterSpecException("unknown parameter spec passed to ElGamal parameters object.");
}
 
Example #18
Source File: DHPublicKey.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public boolean equals(Object obj) {
    if (this == obj) return true;

    if (!(obj instanceof javax.crypto.interfaces.DHPublicKey)) {
        return false;
    }

    javax.crypto.interfaces.DHPublicKey other =
        (javax.crypto.interfaces.DHPublicKey) obj;
    DHParameterSpec otherParams = other.getParams();
    return ((this.y.compareTo(other.getY()) == 0) &&
            (this.p.compareTo(otherParams.getP()) == 0) &&
            (this.g.compareTo(otherParams.getG()) == 0));
}
 
Example #19
Source File: AlgorithmParametersSpi.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
protected void engineInit(
    AlgorithmParameterSpec paramSpec) 
    throws InvalidParameterSpecException
{
    if (!(paramSpec instanceof DHParameterSpec))
    {
        throw new InvalidParameterSpecException("DHParameterSpec required to initialise a Diffie-Hellman algorithm parameters object");
    }

    this.currentSpec = (DHParameterSpec)paramSpec;
}
 
Example #20
Source File: DHPrivateKey.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the key parameters.
 *
 * @return the key parameters
 */
public DHParameterSpec getParams() {
    if (this.l != 0) {
        return new DHParameterSpec(this.p, this.g, this.l);
    } else {
        return new DHParameterSpec(this.p, this.g);
    }
}
 
Example #21
Source File: AlgorithmParameterGeneratorSpi.java    From ripple-lib-java with ISC License 5 votes vote down vote up
protected AlgorithmParameters engineGenerateParameters()
{
    DHParametersGenerator pGen = new DHParametersGenerator();

    if (random != null)
    {
        pGen.init(strength, 20, random);
    }
    else
    {
        pGen.init(strength, 20, new SecureRandom());
    }

    DHParameters p = pGen.generateParameters();

    AlgorithmParameters params;

    try
    {
        params = createParametersInstance("DH");
        params.init(new DHParameterSpec(p.getP(), p.getG(), l));
    }
    catch (Exception e)
    {
        throw new RuntimeException(e.getMessage());
    }

    return params;
}
 
Example #22
Source File: DHPublicKey.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the key parameters.
 *
 * @return the key parameters
 */
public DHParameterSpec getParams() {
    if (this.l != 0) {
        return new DHParameterSpec(this.p, this.g, this.l);
    } else {
        return new DHParameterSpec(this.p, this.g);
    }
}
 
Example #23
Source File: KeyUtil.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns whether the Diffie-Hellman public key is valid or not.
 *
 * Per RFC 2631 and NIST SP800-56A, the following algorithm is used to
 * validate Diffie-Hellman public keys:
 * 1. Verify that y lies within the interval [2,p-1]. If it does not,
 *    the key is invalid.
 * 2. Compute y^q mod p. If the result == 1, the key is valid.
 *    Otherwise the key is invalid.
 */
private static void validateDHPublicKey(DHPublicKey publicKey)
        throws InvalidKeyException {
    DHParameterSpec paramSpec = publicKey.getParams();

    BigInteger p = paramSpec.getP();
    BigInteger g = paramSpec.getG();
    BigInteger y = publicKey.getY();

    validateDHPublicKey(p, g, y);
}
 
Example #24
Source File: DHParameters.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
protected <T extends AlgorithmParameterSpec>
    T engineGetParameterSpec(Class<T> paramSpec)
    throws InvalidParameterSpecException {

    if (DHParameterSpec.class.isAssignableFrom(paramSpec)) {
        return paramSpec.cast(new DHParameterSpec(this.p, this.g, this.l));
    } else {
        throw new InvalidParameterSpecException
            ("Inappropriate parameter Specification");
    }
}
 
Example #25
Source File: BCDHPublicKey.java    From ripple-lib-java with ISC License 5 votes vote down vote up
private void readObject(
    ObjectInputStream   in)
    throws IOException, ClassNotFoundException
{
    in.defaultReadObject();

    this.dhSpec = new DHParameterSpec((BigInteger)in.readObject(), (BigInteger)in.readObject(), in.readInt());
    this.info = null;
}
 
Example #26
Source File: DHPrivateKey.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the key parameters.
 *
 * @return the key parameters
 */
public DHParameterSpec getParams() {
    if (this.l != 0) {
        return new DHParameterSpec(this.p, this.g, this.l);
    } else {
        return new DHParameterSpec(this.p, this.g);
    }
}
 
Example #27
Source File: DHKeyPairGenerator.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Initializes this key pair generator for the specified parameter
 * set and source of randomness.
 *
 * <p>The given parameter set contains the prime modulus, the base
 * generator, and optionally the requested size in bits of the random
 * exponent (private value).
 *
 * @param params the parameter set used to generate the key pair
 * @param random the source of randomness
 *
 * @exception InvalidAlgorithmParameterException if the given parameters
 * are inappropriate for this key pair generator
 */
public void initialize(AlgorithmParameterSpec algParams,
        SecureRandom random) throws InvalidAlgorithmParameterException {
    if (!(algParams instanceof DHParameterSpec)){
        throw new InvalidAlgorithmParameterException
            ("Inappropriate parameter type");
    }

    params = (DHParameterSpec)algParams;
    pSize = params.getP().bitLength();
    if ((pSize < 512) || (pSize > 2048) ||
        (pSize % 64 != 0)) {
        throw new InvalidAlgorithmParameterException
            ("Prime size must be multiple of 64, and can only range "
             + "from 512 to 2048 (inclusive)");
    }

    // exponent size is optional, could be 0
    lSize = params.getL();

    // Require exponentSize < primeSize
    if ((lSize != 0) && (lSize > pSize)) {
        throw new InvalidAlgorithmParameterException
            ("Exponent size must not be larger than modulus size");
    }
    this.random = random;
}
 
Example #28
Source File: DHPublicKey.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the key parameters.
 *
 * @return the key parameters
 */
public DHParameterSpec getParams() {
    if (this.l != 0) {
        return new DHParameterSpec(this.p, this.g, this.l);
    } else {
        return new DHParameterSpec(this.p, this.g);
    }
}
 
Example #29
Source File: DHPublicKey.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public boolean equals(Object obj) {
    if (this == obj) return true;

    if (!(obj instanceof javax.crypto.interfaces.DHPublicKey)) {
        return false;
    }

    javax.crypto.interfaces.DHPublicKey other =
        (javax.crypto.interfaces.DHPublicKey) obj;
    DHParameterSpec otherParams = other.getParams();
    return ((this.y.compareTo(other.getY()) == 0) &&
            (this.p.compareTo(otherParams.getP()) == 0) &&
            (this.g.compareTo(otherParams.getG()) == 0));
}
 
Example #30
Source File: DHKeyExchange.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
private static DHPublicKeySpec getDHPublicKeySpec(PublicKey key) {
    if (key instanceof DHPublicKey) {
        DHPublicKey dhKey = (DHPublicKey)key;
        DHParameterSpec params = dhKey.getParams();
        return new DHPublicKeySpec(dhKey.getY(),
                                params.getP(), params.getG());
    }
    try {
        KeyFactory factory = KeyFactory.getInstance("DiffieHellman");
        return factory.getKeySpec(key, DHPublicKeySpec.class);
    } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
        // unlikely
        throw new RuntimeException("Unable to get DHPublicKeySpec", e);
    }
}