Search in sources :

Example 1 with AsymmetricBlockCipher

use of org.spongycastle.crypto.AsymmetricBlockCipher in project universa by UniversaBlockchain.

the class OAEPEncodingTest method encodeBlock.

/**
 * Make sure the SpongyCastle OAEPEncoding encodes the block according
 * to the test vectors from RSA OAEP specification.
 *
 * @throws Exception
 */
@Test
public void encodeBlock() throws Exception {
    AsymmetricBlockCipher encoder = new OAEPEncoding(RSAEngineFactory.make());
    encoder.init(true, new ParametersWithRandom(oaepSpec.pubParameters, oaepSpec.getRandSeed()));
    byte[] encoded = encoder.processBlock(oaepSpec.M, 0, oaepSpec.M.length);
    assertArrayEquals(encoded, oaepSpec.C);
}
Also used : ParametersWithRandom(org.spongycastle.crypto.params.ParametersWithRandom) OAEPEncoding(org.spongycastle.crypto.encodings.OAEPEncoding) AsymmetricBlockCipher(org.spongycastle.crypto.AsymmetricBlockCipher) Test(org.junit.Test)

Example 2 with AsymmetricBlockCipher

use of org.spongycastle.crypto.AsymmetricBlockCipher in project universa by UniversaBlockchain.

the class OAEPEncodingTest method decodeBlock.

/**
 * Make sure the SpongyCastle OAEPEncoding decodes the block according
 * to the test vectors from RSA OAEP specification.
 *
 * @throws Exception
 */
@Test
public void decodeBlock() throws Exception {
    AsymmetricBlockCipher decoder = new OAEPEncoding(RSAEngineFactory.make());
    decoder.init(false, oaepSpec.privParameters);
    byte[] decoded = decoder.processBlock(oaepSpec.C, 0, oaepSpec.C.length);
    assertArrayEquals(decoded, oaepSpec.M);
}
Also used : OAEPEncoding(org.spongycastle.crypto.encodings.OAEPEncoding) AsymmetricBlockCipher(org.spongycastle.crypto.AsymmetricBlockCipher) Test(org.junit.Test)

Example 3 with AsymmetricBlockCipher

use of org.spongycastle.crypto.AsymmetricBlockCipher 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);
                }
            }
        }
    }
}
Also used : Digest(org.spongycastle.crypto.Digest) SHA256Digest(org.spongycastle.crypto.digests.SHA256Digest) SHA1Digest(org.spongycastle.crypto.digests.SHA1Digest) SHA512Digest(org.spongycastle.crypto.digests.SHA512Digest) SHA224Digest(org.spongycastle.crypto.digests.SHA224Digest) RSAKeyPairGenerator(org.spongycastle.crypto.generators.RSAKeyPairGenerator) SecureRandom(java.security.SecureRandom) RSAKeyGenerationParameters(org.spongycastle.crypto.params.RSAKeyGenerationParameters) OAEPEncoding(org.spongycastle.crypto.encodings.OAEPEncoding) RSAKeyParameters(org.spongycastle.crypto.params.RSAKeyParameters) AsymmetricCipherKeyPair(org.spongycastle.crypto.AsymmetricCipherKeyPair) AsymmetricBlockCipher(org.spongycastle.crypto.AsymmetricBlockCipher) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)3 AsymmetricBlockCipher (org.spongycastle.crypto.AsymmetricBlockCipher)3 OAEPEncoding (org.spongycastle.crypto.encodings.OAEPEncoding)3 SecureRandom (java.security.SecureRandom)1 AsymmetricCipherKeyPair (org.spongycastle.crypto.AsymmetricCipherKeyPair)1 Digest (org.spongycastle.crypto.Digest)1 SHA1Digest (org.spongycastle.crypto.digests.SHA1Digest)1 SHA224Digest (org.spongycastle.crypto.digests.SHA224Digest)1 SHA256Digest (org.spongycastle.crypto.digests.SHA256Digest)1 SHA512Digest (org.spongycastle.crypto.digests.SHA512Digest)1 RSAKeyPairGenerator (org.spongycastle.crypto.generators.RSAKeyPairGenerator)1 ParametersWithRandom (org.spongycastle.crypto.params.ParametersWithRandom)1 RSAKeyGenerationParameters (org.spongycastle.crypto.params.RSAKeyGenerationParameters)1 RSAKeyParameters (org.spongycastle.crypto.params.RSAKeyParameters)1