BCRSAPublicKey( RSAKeyParameters key) { this.modulus = key.getModulus(); this.publicExponent = key.getExponent(); }
BCRSAPublicKey( RSAKeyParameters key) { this.algorithmIdentifier = DEFAULT_ALGORITHM_IDENTIFIER; this.modulus = key.getModulus(); this.publicExponent = key.getExponent(); }
BCRSAPrivateKey( RSAKeyParameters key) { this.modulus = key.getModulus(); this.privateExponent = key.getExponent(); }
JCERSAPrivateKey( RSAKeyParameters key) { this.modulus = key.getModulus(); this.privateExponent = key.getExponent(); }
JCERSAPublicKey( RSAKeyParameters key) { this.modulus = key.getModulus(); this.publicExponent = key.getExponent(); }
JCERSAPublicKey( RSAKeyParameters key) { this.modulus = key.getModulus(); this.publicExponent = key.getExponent(); }
BCRSAPrivateKey( RSAKeyParameters key) { this.modulus = key.getModulus(); this.privateExponent = key.getExponent(); }
JCERSAPrivateKey( RSAKeyParameters key) { this.modulus = key.getModulus(); this.privateExponent = key.getExponent(); }
protected RSAKeyParameters validateRSAPublicKey(RSAKeyParameters key) throws IOException { // TODO What is the minimum bit length required? // key.getModulus().bitLength(); if (!key.getExponent().isProbablePrime(2)) { throw new TlsFatalAlert(AlertDescription.illegal_parameter); } return key; } }
private BigInteger blindMessage( BigInteger msg) { BigInteger blindMsg = blindingFactor; blindMsg = msg.multiply(blindMsg.modPow(key.getExponent(), key.getModulus())); blindMsg = blindMsg.mod(key.getModulus()); return blindMsg; }
protected RSAKeyParameters validateRSAPublicKey(RSAKeyParameters key) throws IOException { // TODO What is the minimum bit length required? // key.getModulus().bitLength(); if (!key.getExponent().isProbablePrime(2)) { throw new TlsFatalAlert(AlertDescription.illegal_parameter); } return key; } }
protected RSAKeyParameters validateRSAPublicKey(RSAKeyParameters key) throws IOException { // TODO What is the minimum bit length required? // key.getModulus().bitLength(); if (!key.getExponent().isProbablePrime(2)) { throw new TlsFatalAlert(AlertDescription.illegal_parameter); } return key; } }
private BigInteger blindMessage( BigInteger msg) { BigInteger blindMsg = blindingFactor; blindMsg = msg.multiply(blindMsg.modPow(key.getExponent(), key.getModulus())); blindMsg = blindMsg.mod(key.getModulus()); return blindMsg; }
/** * Decrypt an encapsulated session key. * * @param in the input buffer for the encapsulated key. * @param inOff the offset for the input buffer. * @param inLen the length of the encapsulated key. * @param keyLen the length of the session key. * @return the session key. */ public CipherParameters decrypt(byte[] in, int inOff, int inLen, int keyLen) throws IllegalArgumentException { if (!key.isPrivate()) { throw new IllegalArgumentException("Private key required for decryption"); } BigInteger n = key.getModulus(); BigInteger d = key.getExponent(); // Decode the input byte[] C = new byte[inLen]; System.arraycopy(in, inOff, C, 0, C.length); BigInteger c = new BigInteger(1, C); // Decrypt the ephemeral random and encode it BigInteger r = c.modPow(d, n); return generateKey(n, r, keyLen); }
/** * 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); }
/** * {@inheritDoc} */ @NonNull @Override public Map<String, Object> toHash() throws IllegalStateException { if (state == null) { throw new IllegalStateException(); } else { return Collections.unmodifiableMap(new HashMap<String, Object>() {{ put("n", BigIntegers.asUnsignedByteArray(state.keyParameters.getModulus())); put("e", BigIntegers.asUnsignedByteArray(state.keyParameters.getExponent())); // Optional fields. if (!state.mgf1HashType.equals(DEFAULT_MGF1_HASH)) { put("mgf1Hash", state.mgf1HashType.getAlgorithmName()); } }}); } }
/** * Test {@link RSAOAEPPublicKey#toHash} where some data is default. */ @Test public void toHashWithDefaultData() throws Exception { // Test random public key. AbstractPublicKey goodPublicKey1 = new RSAOAEPPublicKey( BigIntegers.asUnsignedByteArray(randomPublicKey1.getModulus()), BigIntegers.asUnsignedByteArray(randomPublicKey1.getExponent()), HashType.SHA1, HashType.SHA1, new SecureRandom()); Map map1 = goodPublicKey1.toHash(); assertArrayEquals((byte[]) map1.get("n"), BigIntegers.asUnsignedByteArray(randomPublicKey1.getModulus())); assertArrayEquals((byte[]) map1.get("e"), BigIntegers.asUnsignedByteArray(randomPublicKey1.getExponent())); // With the default values (SHA-256 for hash, SHA-1 for MGF1), // hash and mgf1Hash fields should be missing from the hash. assertFalse(map1.containsKey("hash")); assertFalse(map1.containsKey("mgf1Hash")); }
/** * Test {@link RSAOAEPPublicKey#toHash}. */ @Test public void toHash() throws Exception { // Test sample RSA vectors. AbstractPublicKey rsaPublicKey = oaepSpec.getPublicKey(); Map mapRSA = rsaPublicKey.toHash(); assertArrayEquals((byte[]) mapRSA.get("n"), oaepSpec.n); assertArrayEquals((byte[]) mapRSA.get("e"), oaepSpec.e); assertFalse(mapRSA.containsKey("mgf1Hash")); // SHA-1 is the default value // Test random public key. AbstractPublicKey goodPublicKey1 = new RSAOAEPPublicKey( BigIntegers.asUnsignedByteArray(randomPublicKey1.getModulus()), BigIntegers.asUnsignedByteArray(randomPublicKey1.getExponent()), HashType.SHA512, HashType.SHA512, new SecureRandom()); Map map1 = goodPublicKey1.toHash(); assertArrayEquals((byte[]) map1.get("n"), BigIntegers.asUnsignedByteArray(randomPublicKey1.getModulus())); assertArrayEquals((byte[]) map1.get("e"), BigIntegers.asUnsignedByteArray(randomPublicKey1.getExponent())); assertEquals(map1.get("mgf1Hash"), "SHA-512"); }
/** * Render the public key in PKCS#1 v2.1 ASN.1 format. * (see: RFC 3447 section A.1.1) * * @throws TelehashException */ public byte[] getPKCS1V21Encoded() throws TelehashException { if (! (mKey instanceof RSAKeyParameters)) { throw new TelehashException("key is not RSA"); } RSAKeyParameters rsaPublicKey = (RSAKeyParameters)mKey; ASN1EncodableVector v = new ASN1EncodableVector(); v.add(new ASN1Integer(rsaPublicKey.getModulus())); v.add(new ASN1Integer(rsaPublicKey.getExponent())); ASN1Sequence sequence = new DERSequence(v); try { return sequence.getEncoded(); } catch (IOException e) { throw new TelehashException("cannot encode key", e); } }
} else { isPrivate = false; exponent = new GmpInteger(key.getExponent()); modulus = new GmpInteger(key.getModulus()); isSmallExponent = exponent.bitLength() < 64;