Java Code Examples for javax.crypto.interfaces.DHPrivateKey#getX()

The following examples show how to use javax.crypto.interfaces.DHPrivateKey#getX() . 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: DHUtil.java    From TorrentEngine with GNU General Public License v3.0 6 votes vote down vote up
static public AsymmetricKeyParameter generatePrivateKeyParameter(
    PrivateKey    key)
    throws InvalidKeyException
{
    if (key instanceof DHPrivateKey)
    {
        DHPrivateKey    k = (DHPrivateKey)key;

        return new DHPrivateKeyParameters(k.getX(),
            new DHParameters(k.getParams().getP(), k.getParams().getG(), null, k.getParams().getL()));
    }
                    
    throw new InvalidKeyException("can't identify DH private key.");
}
 
Example 2
Source File: KeyAgreementSpi.java    From ripple-lib-java with ISC License 6 votes vote down vote up
protected void engineInit(
    Key             key,
    SecureRandom    random) 
    throws InvalidKeyException
{
    if (!(key instanceof DHPrivateKey))
    {
        throw new InvalidKeyException("DHKeyAgreement requires DHPrivateKey");
    }

    DHPrivateKey    privKey = (DHPrivateKey)key;

    this.p = privKey.getParams().getP();
    this.g = privKey.getParams().getG();
    this.x = this.result = privKey.getX();
}
 
Example 3
Source File: KeyAgreementSpi.java    From RipplePower with Apache License 2.0 6 votes vote down vote up
protected void engineInit(
    Key             key,
    SecureRandom    random) 
    throws InvalidKeyException
{
    if (!(key instanceof DHPrivateKey))
    {
        throw new InvalidKeyException("DHKeyAgreement requires DHPrivateKey");
    }

    DHPrivateKey    privKey = (DHPrivateKey)key;

    this.p = privKey.getParams().getP();
    this.g = privKey.getParams().getG();
    this.x = this.result = privKey.getX();
}
 
Example 4
Source File: DHUtil.java    From BiglyBT with GNU General Public License v2.0 5 votes vote down vote up
static public AsymmetricKeyParameter generatePrivateKeyParameter(
    PrivateKey    key)
    throws InvalidKeyException
{
    if (key instanceof DHPrivateKey)
    {
        DHPrivateKey    k = (DHPrivateKey)key;

        return new DHPrivateKeyParameters(k.getX(),
            new DHParameters(k.getParams().getP(), k.getParams().getG(), null, k.getParams().getL()));
    }

    throw new InvalidKeyException("can't identify DH private key.");
}
 
Example 5
Source File: DHUtil.java    From ripple-lib-java with ISC License 5 votes vote down vote up
static public AsymmetricKeyParameter generatePrivateKeyParameter(
    PrivateKey    key)
    throws InvalidKeyException
{
    if (key instanceof DHPrivateKey)
    {
        DHPrivateKey    k = (DHPrivateKey)key;

        return new DHPrivateKeyParameters(k.getX(),
            new DHParameters(k.getParams().getP(), k.getParams().getG(), null, k.getParams().getL()));
    }
                    
    throw new InvalidKeyException("can't identify DH private key.");
}
 
Example 6
Source File: KeyAgreementSpi.java    From ripple-lib-java with ISC License 5 votes vote down vote up
protected void engineInit(
    Key                     key,
    AlgorithmParameterSpec  params,
    SecureRandom            random) 
    throws InvalidKeyException, InvalidAlgorithmParameterException
{
    if (!(key instanceof DHPrivateKey))
    {
        throw new InvalidKeyException("DHKeyAgreement requires DHPrivateKey for initialisation");
    }
    DHPrivateKey    privKey = (DHPrivateKey)key;

    if (params != null)
    {
        if (!(params instanceof DHParameterSpec))
        {
            throw new InvalidAlgorithmParameterException("DHKeyAgreement only accepts DHParameterSpec");
        }
        DHParameterSpec p = (DHParameterSpec)params;

        this.p = p.getP();
        this.g = p.getG();
    }
    else
    {
        this.p = privKey.getParams().getP();
        this.g = privKey.getParams().getG();
    }

    this.x = this.result = privKey.getX();
}
 
Example 7
Source File: DHUtil.java    From ripple-lib-java with ISC License 5 votes vote down vote up
static public AsymmetricKeyParameter generatePrivateKeyParameter(
    PrivateKey    key)
    throws InvalidKeyException
{
    if (key instanceof DHPrivateKey)
    {
        DHPrivateKey    k = (DHPrivateKey)key;

        return new DHPrivateKeyParameters(k.getX(),
            new DHParameters(k.getParams().getP(), k.getParams().getG(), null, k.getParams().getL()));
    }
                    
    throw new InvalidKeyException("can't identify DH private key.");
}
 
Example 8
Source File: DHUtil.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
static public AsymmetricKeyParameter generatePrivateKeyParameter(
    PrivateKey    key)
    throws InvalidKeyException
{
    if (key instanceof DHPrivateKey)
    {
        DHPrivateKey    k = (DHPrivateKey)key;

        return new DHPrivateKeyParameters(k.getX(),
            new DHParameters(k.getParams().getP(), k.getParams().getG(), null, k.getParams().getL()));
    }
                    
    throw new InvalidKeyException("can't identify DH private key.");
}
 
Example 9
Source File: DHUtil.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
static public AsymmetricKeyParameter generatePrivateKeyParameter(
    PrivateKey    key)
    throws InvalidKeyException
{
    if (key instanceof DHPrivateKey)
    {
        DHPrivateKey    k = (DHPrivateKey)key;

        return new DHPrivateKeyParameters(k.getX(),
            new DHParameters(k.getParams().getP(), k.getParams().getG(), null, k.getParams().getL()));
    }
                    
    throw new InvalidKeyException("can't identify DH private key.");
}
 
Example 10
Source File: DiffieHellmanSession.java    From openid4java with Apache License 2.0 5 votes vote down vote up
protected byte[] getDigestedZZ(String otherPublicKeyBase64)
{
    DHPublicKey  dhPublicKey  = stringToPublicKey(otherPublicKeyBase64);
    DHPrivateKey dhPrivateKey = getPrivateKey();
    BigInteger xa = dhPrivateKey.getX();
    BigInteger yb = dhPublicKey.getY();
    BigInteger p  = _dhParameterSpec.getP();

    BigInteger zz = yb.modPow(xa, p);

    return _hDigest.digest(zz.toByteArray());
}
 
Example 11
Source File: BCElGamalPrivateKey.java    From ripple-lib-java with ISC License 4 votes vote down vote up
BCElGamalPrivateKey(
    DHPrivateKey key)
{
    this.x = key.getX();
    this.elSpec = new ElGamalParameterSpec(key.getParams().getP(), key.getParams().getG());
}
 
Example 12
Source File: BCDHPrivateKey.java    From RipplePower with Apache License 2.0 4 votes vote down vote up
BCDHPrivateKey(
    DHPrivateKey key)
{
    this.x = key.getX();
    this.dhSpec = key.getParams();
}
 
Example 13
Source File: JCEDHPrivateKey.java    From RipplePower with Apache License 2.0 4 votes vote down vote up
JCEDHPrivateKey(
    DHPrivateKey    key)
{
    this.x = key.getX();
    this.dhSpec = key.getParams();
}
 
Example 14
Source File: BCElGamalPrivateKey.java    From RipplePower with Apache License 2.0 4 votes vote down vote up
BCElGamalPrivateKey(
    DHPrivateKey key)
{
    this.x = key.getX();
    this.elSpec = new ElGamalParameterSpec(key.getParams().getP(), key.getParams().getG());
}
 
Example 15
Source File: JCEElGamalPrivateKey.java    From ripple-lib-java with ISC License 4 votes vote down vote up
JCEElGamalPrivateKey(
    DHPrivateKey    key)
{
    this.x = key.getX();
    this.elSpec = new ElGamalParameterSpec(key.getParams().getP(), key.getParams().getG());
}
 
Example 16
Source File: JCEDHPrivateKey.java    From ripple-lib-java with ISC License 4 votes vote down vote up
JCEDHPrivateKey(
    DHPrivateKey    key)
{
    this.x = key.getX();
    this.dhSpec = key.getParams();
}
 
Example 17
Source File: JCEElGamalPrivateKey.java    From RipplePower with Apache License 2.0 4 votes vote down vote up
JCEElGamalPrivateKey(
    DHPrivateKey    key)
{
    this.x = key.getX();
    this.elSpec = new ElGamalParameterSpec(key.getParams().getP(), key.getParams().getG());
}
 
Example 18
Source File: DhTest.java    From wycheproof with Apache License 2.0 4 votes vote down vote up
private void testKeyPair(KeyPair keyPair, int expectedKeySize) throws Exception {
  DHPrivateKey priv = (DHPrivateKey) keyPair.getPrivate();
  BigInteger p = priv.getParams().getP();
  BigInteger g = priv.getParams().getG();
  int keySize = p.bitLength();
  assertEquals("wrong key size", expectedKeySize, keySize);

  // Checks the key size of the private key.
  // NIST SP 800-56A requires that x is in the range (1, q-1).
  // Such a choice would require a full key validation. Since such a validation
  // requires the value q (which is not present in the DH parameters) larger keys
  // should be chosen to prevent attacks.
  int minPrivateKeyBits = keySize / 2;
  BigInteger x = priv.getX();
  assertTrue(x.bitLength() >= minPrivateKeyBits - 32);
  // TODO(bleichen): add tests for weak random number generators.

  // Verify the DH parameters.
  System.out.println("p=" + p.toString(16));
  System.out.println("g=" + g.toString(16));
  System.out.println("testKeyPairGenerator L=" + priv.getParams().getL());
  // Basic parameter checks
  assertTrue("Expecting g > 1", g.compareTo(BigInteger.ONE) > 0);
  assertTrue("Expecting g < p - 1", g.compareTo(p.subtract(BigInteger.ONE)) < 0);
  // Expecting p to be prime.
  // No high certainty is needed, since this is a unit test.
  assertTrue(p.isProbablePrime(4));
  // The order of g should be a large prime divisor q of p-1.
  // (see e.g. NIST SP 800-56A, section 5.5.1.1.)
  // If the order of g is composite then the Decision Diffie Hellman assumption is
  // not satisfied for the group generated by g. Moreover, attacks using Pohlig-Hellman
  // might be feasible.
  // A good way to achieve these requirements is to select a safe prime p (i.e. a prime
  // where q=(p-1)/2 is prime too. NIST SP 800-56A does not require (or even recommend)
  // safe primes and allows Diffie-Hellman parameters where q is significantly smaller.
  // Unfortunately, the key does not contain q and thus the conditions above  cannot be
  // tested easily.
  // We perform a partial test that performs a partial factorization of p-1 and then
  // test whether one of the small factors found by the partial factorization divides
  // the order of g.
  boolean isSafePrime = p.shiftRight(1).isProbablePrime(4);
  System.out.println("p is a safe prime:" + isSafePrime);
  BigInteger r; // p-1 divided by small prime factors.
  if (isSafePrime) {
    r = p.shiftRight(1);
  } else {
    BigInteger p1 = p.subtract(BigInteger.ONE);
    r = p1.divide(smoothDivisor(p1));
  }
  System.out.println("r=" + r.toString(16));
  assertEquals(
      "g likely does not generate a prime oder subgroup", BigInteger.ONE, g.modPow(r, p));

  // Checks that there are not too many short prime factors.
  // I.e., subgroup confinment attacks can find at least keySize - r.bitLength() bits of the key.
  // At least 160 unknown bits should remain.
  // Only very weak parameters are detected here, since the factorization above only finds small
  // prime factors.
  assertTrue(minPrivateKeyBits - (keySize - r.bitLength()) > 160);

  // DH parameters are sometime misconfigures and g and q are swapped.
  // A large g that divides p-1 is suspicious.
  if (g.bitLength() >= 160) {
    assertTrue(p.mod(g).compareTo(BigInteger.ONE) > 0);
  }
}
 
Example 19
Source File: BCDHPrivateKey.java    From ripple-lib-java with ISC License 4 votes vote down vote up
BCDHPrivateKey(
    DHPrivateKey key)
{
    this.x = key.getX();
    this.dhSpec = key.getParams();
}
 
Example 20
Source File: SupportedDHKeys.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private static void checkKeyPair(KeyPair kp, int pSize,
            Provider provider) throws Exception {

    DHPrivateKey privateKey = (DHPrivateKey)kp.getPrivate();
    BigInteger p = privateKey.getParams().getP();
    if (p.bitLength() != pSize) {
        throw new Exception(
            "Invalid modulus size: " + p.bitLength() + "/" + pSize);
    }

    // System.out.println("P(" + pSize + "): " + p.toString());
    if (!p.isProbablePrime(128)) {
        throw new Exception("Good luck, the modulus is composite!");
    }

    DHPublicKey publicKey = (DHPublicKey)kp.getPublic();
    p = publicKey.getParams().getP();
    if (p.bitLength() != pSize) {
        throw new Exception(
            "Invalid modulus size: " + p.bitLength() + "/" + pSize);
    }

    BigInteger leftOpen = BigInteger.ONE;
    BigInteger rightOpen = p.subtract(BigInteger.ONE);

    // ignore the private key range checking on Solaris at present
    if (!provider.getName().equals("SunPKCS11-Solaris")) {
        BigInteger x = privateKey.getX();
        if ((x.compareTo(leftOpen) <= 0) ||
                (x.compareTo(rightOpen) >= 0)) {
            throw new Exception(
                "X outside range [2, p - 2]:  x: " + x + " p: " + p);
        }
    }

    BigInteger y = publicKey.getY();
    if ((y.compareTo(leftOpen) <= 0) ||
            (y.compareTo(rightOpen) >= 0)) {
        throw new Exception(
            "Y outside range [2, p - 2]:  y: " + y + " p: " + p);
    }
}