/** * Return a value that can be used as x1 or x3 during round 1. * <p> * The returned value is a random value in the range <tt>[0, q-1]</tt>. */ public static BigInteger generateX1( BigInteger q, SecureRandom random) { BigInteger min = ZERO; BigInteger max = q.subtract(ONE); return BigIntegers.createRandomInRange(min, max, random); }
private static BigInteger generatePrivateKey(BigInteger q, SecureRandom random) { // TODO Prefer this method? (change test cases that used fixed random) // B.1.1 Key Pair Generation Using Extra Random Bits // BigInteger c = new BigInteger(q.bitLength() + 64, random); // return c.mod(q.subtract(ONE)).add(ONE); // B.1.2 Key Pair Generation by Testing Candidates return BigIntegers.createRandomInRange(ONE, q.subtract(ONE), random); }
/** * Return a value that can be used as x2 or x4 during round 1. * <p> * The returned value is a random value in the range <tt>[1, q-1]</tt>. */ public static BigInteger generateX2( BigInteger q, SecureRandom random) { BigInteger min = ONE; BigInteger max = q.subtract(ONE); return BigIntegers.createRandomInRange(min, max, random); }
private BigInteger generateRandomElement(BigInteger p, SecureRandom random) { return BigIntegers.createRandomInRange(ONE, p.subtract(ONE), random); }
private BigInteger generateRandomElement(BigInteger p, SecureRandom random) { return BigIntegers.createRandomInRange(ONE, p.subtract(ONE), random); }
public static BigInteger generatePrivateValue(BigInteger N, BigInteger g, SecureRandom random) { int minBits = Math.min(256, N.bitLength() / 2); BigInteger min = ONE.shiftLeft(minBits - 1); BigInteger max = N.subtract(ONE); return BigIntegers.createRandomInRange(min, max, random); }
public static BigInteger generatePrivateValue(Digest digest, BigInteger N, BigInteger g, SecureRandom random) { int minBits = Math.min(256, N.bitLength() / 2); BigInteger min = ONE.shiftLeft(minBits - 1); BigInteger max = N.subtract(ONE); return BigIntegers.createRandomInRange(min, max, random); }
public static BigInteger generatePrivateValue(Digest digest, BigInteger N, BigInteger g, SecureRandom random) { int minBits = Math.min(256, N.bitLength() / 2); BigInteger min = ONE.shiftLeft(minBits - 1); BigInteger max = N.subtract(ONE); return BigIntegers.createRandomInRange(min, max, random); }
private static BigInteger calculateGenerator_FIPS186_2(BigInteger p, BigInteger q, SecureRandom r) { BigInteger e = p.subtract(ONE).divide(q); BigInteger pSub2 = p.subtract(TWO); for (;;) { BigInteger h = BigIntegers.createRandomInRange(TWO, pSub2, r); BigInteger g = h.modPow(e, p); if (g.bitLength() > 1) { return g; } } }
private static BigInteger calculateGenerator_FIPS186_2(BigInteger p, BigInteger q, SecureRandom r) { BigInteger e = p.subtract(ONE).divide(q); BigInteger pSub2 = p.subtract(TWO); for (;;) { BigInteger h = BigIntegers.createRandomInRange(TWO, pSub2, r); BigInteger g = h.modPow(e, p); if (g.bitLength() > 1) { return g; } } }
static BigInteger selectGenerator(BigInteger p, SecureRandom random) { BigInteger pMinusTwo = p.subtract(TWO); BigInteger g; /* * RFC 2631 2.2.1.2 (and see: Handbook of Applied Cryptography 4.81) */ do { BigInteger h = BigIntegers.createRandomInRange(TWO, pMinusTwo, random); g = h.modPow(TWO, p); } while (g.equals(ONE)); return g; } }
private static BigInteger generatePrivateKey(BigInteger q, SecureRandom random) { // B.1.2 Key Pair Generation by Testing Candidates int minWeight = q.bitLength() >>> 2; for (;;) { // TODO Prefer this method? (change test cases that used fixed random) // B.1.1 Key Pair Generation Using Extra Random Bits // BigInteger x = new BigInteger(q.bitLength() + 64, random).mod(q.subtract(ONE)).add(ONE); BigInteger x = BigIntegers.createRandomInRange(ONE, q.subtract(ONE), random); if (WNafUtil.getNafWeight(x) >= minWeight) { return x; } } }
static BigInteger selectGenerator(BigInteger p, BigInteger q, SecureRandom random) { BigInteger pMinusTwo = p.subtract(TWO); BigInteger g; /* * (see: Handbook of Applied Cryptography 4.80) */ // do // { // g = BigIntegers.createRandomInRange(TWO, pMinusTwo, random); // } // while (g.modPow(TWO, p).equals(ONE) || g.modPow(q, p).equals(ONE)); /* * RFC 2631 2.2.1.2 (and see: Handbook of Applied Cryptography 4.81) */ do { BigInteger h = BigIntegers.createRandomInRange(TWO, pMinusTwo, random); g = h.modPow(TWO, p); } while (g.equals(ONE)); return g; } }
static BigInteger selectGenerator(BigInteger p, BigInteger q, SecureRandom random) { BigInteger pMinusTwo = p.subtract(TWO); BigInteger g; /* * (see: Handbook of Applied Cryptography 4.80) */ // do // { // g = BigIntegers.createRandomInRange(TWO, pMinusTwo, random); // } // while (g.modPow(TWO, p).equals(ONE) || g.modPow(q, p).equals(ONE)); /* * RFC 2631 2.2.1.2 (and see: Handbook of Applied Cryptography 4.81) */ do { BigInteger h = BigIntegers.createRandomInRange(TWO, pMinusTwo, random); g = h.modPow(TWO, p); } while (g.equals(ONE)); return g; } }
return createRandomInRange(ZERO, max.subtract(min), random).add(min);
/** * Calculate a zero knowledge proof of x using Schnorr's signature. * The returned array has two elements {g^v, r = v-x*h} for x. */ public static BigInteger[] calculateZeroKnowledgeProof( BigInteger p, BigInteger q, BigInteger g, BigInteger gx, BigInteger x, String participantId, Digest digest, SecureRandom random) { BigInteger[] zeroKnowledgeProof = new BigInteger[2]; /* Generate a random v, and compute g^v */ BigInteger vMin = ZERO; BigInteger vMax = q.subtract(ONE); BigInteger v = BigIntegers.createRandomInRange(vMin, vMax, random); BigInteger gv = g.modPow(v, p); BigInteger h = calculateHashForZeroKnowledgeProof(g, gv, gx, participantId, digest); // h zeroKnowledgeProof[0] = gv; zeroKnowledgeProof[1] = v.subtract(x.multiply(h)).mod(q); // r = v-x*h return zeroKnowledgeProof; }
private static ECFieldElement[] findBetaValues(ECCurve c) { BigInteger q = c.getField().getCharacteristic(); BigInteger e = q.divide(ECConstants.THREE); // Search for a random value that generates a non-trival cube root of 1 SecureRandom random = new SecureRandom(); BigInteger b; do { BigInteger r = BigIntegers.createRandomInRange(ECConstants.TWO, q.subtract(ECConstants.TWO), random); b = r.modPow(e, q); } while (b.equals(ECConstants.ONE)); ECFieldElement beta = c.fromBigInteger(b); return new ECFieldElement[]{ beta, beta.square() }; }
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); }
/** * Generate and encapsulate a random session key. * * @param out the output buffer for the encapsulated key. * @param outOff the offset for the output buffer. * @param keyLen the length of the random session key. * @return the random session key. */ public CipherParameters encrypt(byte[] out, int outOff, int keyLen) throws IllegalArgumentException { if (key.isPrivate()) { throw new IllegalArgumentException("Public key required for encryption"); } BigInteger n = key.getModulus(); BigInteger e = key.getExponent(); // Generate the ephemeral random and encode it BigInteger r = BigIntegers.createRandomInRange(ZERO, n.subtract(ONE), rnd); // Encrypt the random and encode it BigInteger c = r.modPow(e, n); byte[] C = BigIntegers.asUnsignedByteArray((n.bitLength() + 7) / 8, c); System.arraycopy(C, 0, out, outOff, C.length); return generateKey(n, r, keyLen); }
BigInteger r = BigIntegers.createRandomInRange(ONE, n, rnd);