use of org.spongycastle.crypto.params.RSAPrivateCrtKeyParameters 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.RSAPrivateCrtKeyParameters in project universa by UniversaBlockchain.
the class RSAOAEPPrivateKey method generate.
/**
* Generate a new key pair, with all options specified.
*
* @param bitStrength bit strength of the key, e.g. 2048
* @param e RSA public exponent
* @param certainty RSA key generation certainty
* @param mgf1HashType The type of the hash(digest) function used for OAEP MGF1 hash generation.
*/
public void generate(int bitStrength, byte[] e, int certainty, HashType oaepHashType, HashType mgf1HashType) {
final RSAKeyPairGenerator keyGen = new RSAKeyPairGenerator();
keyGen.init(new RSAKeyGenerationParameters(BigIntegers.fromUnsignedByteArray(e), new SecureRandom(), bitStrength, certainty));
final AsymmetricCipherKeyPair keyPair = keyGen.generateKeyPair();
final RSAPrivateCrtKeyParameters privateKey = (RSAPrivateCrtKeyParameters) keyPair.getPrivate();
if (mgf1HashType == null) {
mgf1HashType = DEFAULT_MGF1_HASH;
}
// Don't worry we are passing thread-unsafe hash and mgf1Hash Digest instances:
// init() will clone them anyway.
init(e, BigIntegers.asUnsignedByteArray(privateKey.getP()), BigIntegers.asUnsignedByteArray(privateKey.getQ()), oaepHashType, mgf1HashType, new SecureRandom());
}
use of org.spongycastle.crypto.params.RSAPrivateCrtKeyParameters in project universa by UniversaBlockchain.
the class RSAOAEPPrivateKey method init.
/**
* Hidden (package-private) initializer, for internal/unittest usage.
*/
void init(byte[] e, byte[] p, byte[] q, HashType oaepHashType, HashType mgf1HashType, SecureRandom rng) {
final RSAKeyPair keyPair = RSAKeyPair.fromExponents(e, p, q);
final RSAPrivateCrtKeyParameters privParameters = new RSAPrivateCrtKeyParameters(BigIntegers.fromUnsignedByteArray(keyPair.n), BigIntegers.fromUnsignedByteArray(keyPair.e), BigIntegers.fromUnsignedByteArray(keyPair.d), BigIntegers.fromUnsignedByteArray(keyPair.p), BigIntegers.fromUnsignedByteArray(keyPair.q), BigIntegers.fromUnsignedByteArray(keyPair.dP), BigIntegers.fromUnsignedByteArray(keyPair.dQ), BigIntegers.fromUnsignedByteArray(keyPair.qInv));
final AsymmetricBlockCipher decryptor = makeDecryptor(mgf1HashType);
// Private key goes together with its public key.
final RSAOAEPPublicKey publicKey = new RSAOAEPPublicKey();
publicKey.init(keyPair.n, keyPair.e, oaepHashType, mgf1HashType, rng);
state = new RSAOAEPPrivateKey.State(decryptor, privParameters, publicKey, oaepHashType, mgf1HashType, rng);
resetDecryptor();
}
use of org.spongycastle.crypto.params.RSAPrivateCrtKeyParameters in project universa by UniversaBlockchain.
the class NativeRSACoreEngine method init.
/**
* initialise the RSA engine.
*
* @param forEncryption true if we are encrypting, false otherwise.
* @param param the necessary RSA key parameters.
*/
public void init(boolean forEncryption, CipherParameters param) {
if (param instanceof ParametersWithRandom) {
ParametersWithRandom rParam = (ParametersWithRandom) param;
key = (RSAKeyParameters) rParam.getParameters();
} else {
key = (RSAKeyParameters) param;
}
this.forEncryption = forEncryption;
if (key instanceof RSAPrivateCrtKeyParameters) {
isPrivate = true;
//
// we have the extra factors, use the Chinese Remainder Theorem - the author
// wishes to express his thanks to Dirk Bonekaemper at rtsffm.com for
// advice regarding the expression of this.
//
RSAPrivateCrtKeyParameters crtKey = (RSAPrivateCrtKeyParameters) key;
p = new GmpInteger(crtKey.getP());
q = new GmpInteger(crtKey.getQ());
dP = new GmpInteger(crtKey.getDP());
dQ = new GmpInteger(crtKey.getDQ());
qInv = crtKey.getQInv();
exponent = modulus = null;
} else {
isPrivate = false;
exponent = new GmpInteger(key.getExponent());
modulus = new GmpInteger(key.getModulus());
isSmallExponent = exponent.bitLength() < 64;
p = q = dP = dQ = null;
qInv = null;
}
}
use of org.spongycastle.crypto.params.RSAPrivateCrtKeyParameters 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