private static DHParameters fromSafeP(String hexP) { BigInteger p = fromHex(hexP), q = p.shiftRight(1); return new DHParameters(p, TWO, q); }
BigInteger calculatePrivate(DHParameters dhParams, SecureRandom random) { BigInteger p = dhParams.getP(); int limit = dhParams.getL(); if (limit != 0) { return new BigInteger(limit, random).setBit(limit - 1); } BigInteger min = TWO; int m = dhParams.getM(); if (m != 0) { min = ONE.shiftLeft(m - 1); } BigInteger max = p.subtract(TWO); BigInteger q = dhParams.getQ(); if (q != null) { max = q.subtract(TWO); } return BigIntegers.createRandomInRange(min, max, random); }
public int getFieldSize() { return (key.getParameters().getP().bitLength() + 7) / 8; }
/** * given a short term public key from a given party calculate the next * message in the agreement sequence. */ public BigInteger calculateAgreement( CipherParameters pubKey) { DHPublicKeyParameters pub = (DHPublicKeyParameters)pubKey; if (!pub.getParameters().equals(dhParams)) { throw new IllegalArgumentException("Diffie-Hellman public key has wrong parameters."); } return pub.getY().modPow(key.getX(), dhParams.getP()); } }
private BigInteger validate(BigInteger y, DHParameters dhParams) { if (y == null) { throw new NullPointerException("y value cannot be null"); } // TLS check if (y.compareTo(TWO) < 0 || y.compareTo(dhParams.getP().subtract(TWO)) > 0) { throw new IllegalArgumentException("invalid DH public key"); } if (dhParams.getQ() != null) { if (ONE.equals(y.modPow(dhParams.getQ(), dhParams.getP()))) { return y; } throw new IllegalArgumentException("Y value does not appear to be in correct group"); } else { return y; // we can't validate without Q. } }
public int hashCode() { int code = isPrivate() ? 0 : 1; if (params != null) { code ^= params.hashCode(); } return code; } }
public DHParameters( BigInteger p, BigInteger g, BigInteger q, int l) { this(p, g, q, getDefaultMParam(l), l, null, null); }
public boolean equals( Object obj) { if (!(obj instanceof DHKeyParameters)) { return false; } DHKeyParameters dhKey = (DHKeyParameters)obj; if (params == null) { return dhKey.getParameters() == null; } else { return params.equals(dhKey.getParameters()); } }
public AsymmetricKeyParameter readKey(InputStream stream) throws IOException { byte[] V = new byte[(dhParams.getP().bitLength() + 7) / 8]; Streams.readFully(stream, V, 0, V.length); return new DHPublicKeyParameters(new BigInteger(1, V), dhParams); } }
/** * given a message from a given party and the corresponding public key, * calculate the next message in the agreement sequence. In this case * this will represent the shared secret. */ public BigInteger calculateAgreement( DHPublicKeyParameters pub, BigInteger message) { if (!pub.getParameters().equals(dhParams)) { throw new IllegalArgumentException("Diffie-Hellman public key has wrong parameters."); } BigInteger p = dhParams.getP(); return message.modPow(key.getX(), p).multiply(pub.getY().modPow(privateValue, p)).mod(p); } }
public int hashCode() { int code = isPrivate() ? 0 : 1; if (params != null) { code ^= params.hashCode(); } return code; } }
public DHParameters( BigInteger p, BigInteger g, BigInteger q, int l) { this(p, g, q, getDefaultMParam(l), l, null, null); }