use of org.spongycastle.crypto.params.RSAKeyParameters in project universa by UniversaBlockchain.
the class RSAOAEPPrivateKeyTest method toHashDefaultData.
/**
* Test {@link RSAOAEPPrivateKey#toHash} where some data is default.
*/
@Test
public void toHashDefaultData() throws Exception {
// Test random key pair (4096 bit, SHA-256/256).
AbstractPublicKey randomPublicKey4096SHA256 = randomPrivateKey4096SHADefault.getPublicKey();
Map mapPrivate4096SHA256 = randomPrivateKey4096SHADefault.toHash(), mapPublic4096SHA256 = randomPublicKey4096SHA256.toHash();
RSAPrivateCrtKeyParameters privateKeyParameters4096SHA256 = ((RSAOAEPPrivateKey) randomPrivateKey4096SHADefault).state.keyParameters;
RSAKeyParameters publicKeyParameters4096 = ((RSAOAEPPublicKey) (randomPrivateKey4096SHADefault.getPublicKey())).state.keyParameters;
// Check private key serialization.
assertArrayEquals((byte[]) mapPrivate4096SHA256.get("e"), BigIntegers.asUnsignedByteArray(privateKeyParameters4096SHA256.getPublicExponent()));
assertArrayEquals((byte[]) mapPrivate4096SHA256.get("p"), BigIntegers.asUnsignedByteArray(privateKeyParameters4096SHA256.getP()));
assertArrayEquals((byte[]) mapPrivate4096SHA256.get("q"), BigIntegers.asUnsignedByteArray(privateKeyParameters4096SHA256.getQ()));
// With the default values (SHA-1), hash and mgf1Hash fields should be missing from the hash.
assertFalse(mapPrivate4096SHA256.containsKey("mgf1Hash"));
// Check public key serialization.
assertArrayEquals((byte[]) mapPublic4096SHA256.get("n"), BigIntegers.asUnsignedByteArray(publicKeyParameters4096.getModulus()));
assertArrayEquals((byte[]) mapPublic4096SHA256.get("e"), BigIntegers.asUnsignedByteArray(publicKeyParameters4096.getExponent()));
// With the default values (SHA-256), hash and mgf1Hash fields should be missing from the hash.
assertFalse(mapPublic4096SHA256.containsKey("mgf1Hash"));
}
use of org.spongycastle.crypto.params.RSAKeyParameters in project universa by UniversaBlockchain.
the class RSAOAEPPublicKey method init.
/**
* Hidden (package-private) initializer, for internal/unittest usage.
*/
void init(byte[] n, byte[] e, HashType oaepHashType, HashType mgf1HashType, SecureRandom rng) {
final RSAKeyParameters pubParameters = new RSAKeyParameters(false, BigIntegers.fromUnsignedByteArray(n), BigIntegers.fromUnsignedByteArray(e));
state = new State(makeEncryptor(mgf1HashType), pubParameters, oaepHashType, mgf1HashType, rng);
resetEncryptor();
}
use of org.spongycastle.crypto.params.RSAKeyParameters in project universa by UniversaBlockchain.
the class OAEPEncodingTest method randomEncodeDecode.
/**
* Make sure the SpongyCastle OAEPEncoding encoding and decoding operations
* do not lose or corrupt data.
* We test it:
* For each hash function we may use with OEAP (like, SHA1 or SHA512),
* and for each RSA key size (among 1024, 2048, 4096)
* we create multiple (NUMBER_OF_RANDOM_ENCRYPTION_KEY_PAIRS) RSA key pairs;
* for each of the key pair we test encryption-decryption cycle
* with NUMBER_OF_RANDOM_ENCRYPTION_DECRYPTION_CYCLES_PER_KEY_PAIR random messages
* (each of the maximum possible size for this cipher configuration)
* and make sure the result matches the original random message.
*
* @throws Exception
*/
@Test
public void randomEncodeDecode() throws Exception {
RSAKeyPairGenerator keyGen = new RSAKeyPairGenerator();
for (Digest digest : DIGESTS) {
for (int i = 0; i < NUMBER_OF_RANDOM_ENCRYPTION_KEY_PAIRS; i++) {
// Create key pair
SecureRandom rng = new SecureRandom();
int publicExponent = PUBLIC_EXPONENTS[rng.nextInt(PUBLIC_EXPONENTS.length)];
int keySize = KEY_SIZES[rng.nextInt(KEY_SIZES.length)];
keyGen.init(new RSAKeyGenerationParameters(BigInteger.valueOf(publicExponent), new SecureRandom(), keySize, RSA_KEY_CERTAINTY));
AsymmetricCipherKeyPair keyPair = keyGen.generateKeyPair();
RSAKeyParameters publicKey = (RSAKeyParameters) keyPair.getPublic(), privateKey = (RSAKeyParameters) keyPair.getPrivate();
assertEquals(keySize, publicKey.getModulus().bitLength());
// though actually it is sufficient to keysize <= publicKey.getModulus().bitLength()
int maxMessageSize = keySize / 8 - 2 - 2 * digest.getDigestSize(), minMessageSize = 1, messageSize = (maxMessageSize >= minMessageSize) ? rng.nextInt(maxMessageSize - minMessageSize + 1) + minMessageSize : 0;
// messageSize may become negative with too small RSA key size and too large digest.
if (messageSize > 0) {
// For each key pair we do multiple encryption-decryption cycles
for (int j = 0; j < NUMBER_OF_RANDOM_ENCRYPTION_DECRYPTION_CYCLES_PER_KEY_PAIR; j++) {
// Create random message
byte[] message = new byte[messageSize];
rng.nextBytes(message);
AsymmetricBlockCipher encoder = new OAEPEncoding(RSAEngineFactory.make(), digest), decoder = new OAEPEncoding(RSAEngineFactory.make(), digest);
encoder.init(true, publicKey);
decoder.init(false, privateKey);
byte[] encoded = encoder.processBlock(message, 0, message.length);
byte[] decoded = decoder.processBlock(encoded, 0, encoded.length);
// Finally, test the encoding/decoding cycle
String assertMessage = String.format("Digest %s,\n message %s,\n public key %s / %s,\n private key %s / %s", digest, Hex.toHexString(message), publicKey.getExponent(), publicKey.getModulus(), privateKey.getExponent(), privateKey.getModulus());
assertArrayEquals(assertMessage, message, decoded);
}
}
}
}
}
use of org.spongycastle.crypto.params.RSAKeyParameters in project universa by UniversaBlockchain.
the class RSAOAEPPrivateKeyTest method toHash.
/**
* Test {@link RSAOAEPPrivateKey#toHash}.
*/
@Test
public void toHash() throws Exception {
// Test sample RSA vectors.
AbstractPrivateKey rsaPrivateKey = oaepSpec.getPrivateKey();
Map mapRSA = rsaPrivateKey.toHash();
assertArrayEquals((byte[]) mapRSA.get("e"), oaepSpec.e);
assertArrayEquals((byte[]) mapRSA.get("p"), oaepSpec.p);
assertArrayEquals((byte[]) mapRSA.get("q"), oaepSpec.q);
// SHA-1 is the default value
assertFalse(mapRSA.containsKey("mgf1Hash"));
// Test random key pair (4096 bit, SHA-512).
AbstractPublicKey randomPublicKey4096 = randomPrivateKey4096.getPublicKey();
Map mapPrivate4096 = randomPrivateKey4096.toHash(), mapPublic4096 = randomPublicKey4096.toHash();
RSAPrivateCrtKeyParameters privateKeyParameters4096 = ((RSAOAEPPrivateKey) randomPrivateKey4096).state.keyParameters;
RSAKeyParameters publicKeyParameters4096 = ((RSAOAEPPublicKey) (randomPrivateKey4096.getPublicKey())).state.keyParameters;
// Check private key serialization.
assertArrayEquals((byte[]) mapPrivate4096.get("e"), BigIntegers.asUnsignedByteArray(privateKeyParameters4096.getPublicExponent()));
assertArrayEquals((byte[]) mapPrivate4096.get("p"), BigIntegers.asUnsignedByteArray(privateKeyParameters4096.getP()));
assertArrayEquals((byte[]) mapPrivate4096.get("q"), BigIntegers.asUnsignedByteArray(privateKeyParameters4096.getQ()));
assertEquals(mapPrivate4096.get("mgf1Hash"), "SHA-512");
// Check public key serialization.
assertArrayEquals((byte[]) mapPublic4096.get("n"), BigIntegers.asUnsignedByteArray(publicKeyParameters4096.getModulus()));
assertArrayEquals((byte[]) mapPublic4096.get("e"), BigIntegers.asUnsignedByteArray(publicKeyParameters4096.getExponent()));
assertEquals(mapPublic4096.get("mgf1Hash"), "SHA-512");
}
Aggregations