Search in sources :

Example 1 with AbstractPrivateKey

use of com.icodici.crypto.AbstractPrivateKey in project universa by UniversaBlockchain.

the class RSAOAEPPrivateKeyTest method getPublicKey.

/**
 * Test {@link RSAOAEPPrivateKey#getPublicKey}.
 */
@Test
public void getPublicKey() throws Exception {
    AbstractPublicKey randomPublicKey4096 = randomPrivateKey4096.getPublicKey();
    assertTrue(randomPublicKey4096 instanceof RSAOAEPPublicKey);
    AbstractPrivateKey rsaPrivateKey = oaepSpec.getPrivateKey();
    AbstractPublicKey rsaPublicKey = rsaPrivateKey.getPublicKey();
    ((RSAOAEPPublicKey) rsaPublicKey).resetEncryptor();
    assertArrayEquals(rsaPublicKey.encrypt(oaepSpec.M), oaepSpec.C);
}
Also used : AbstractPublicKey(com.icodici.crypto.AbstractPublicKey) AbstractPrivateKey(com.icodici.crypto.AbstractPrivateKey) Test(org.junit.Test)

Example 2 with AbstractPrivateKey

use of com.icodici.crypto.AbstractPrivateKey in project universa by UniversaBlockchain.

the class RSAOAEPPrivateKeyTest method updateFromHashGoodRSASpec.

/**
 * Test {@link RSAOAEPPrivateKey#updateFromHash} on success scenarios,
 * using RSA spec vectors.
 */
@Test
public void updateFromHashGoodRSASpec() throws Exception {
    Map mapRSA = new HashMap<String, Object>() {

        {
            put("e", oaepSpec.e);
            put("p", oaepSpec.p);
            put("q", oaepSpec.q);
            put("hash", "SHA-1");
            put("mgf1Hash", "SHA-1");
        }
    };
    AbstractPrivateKey rsaPrivateKey = new RSAOAEPPrivateKey();
    rsaPrivateKey.updateFromHash(mapRSA);
    AbstractPublicKey rsaPublicKey = rsaPrivateKey.getPublicKey();
    // We've read the private key from hash (and created the public one);
    // but, to test it over the RSA spec vectors,
    // we need to hack into the state.rng (and substitute it with our custom one)
    // of both keys, even though they are declared as final.
    // 
    // Here we fix public key.
    RSAOAEPPublicKey.State publicState = ((RSAOAEPPublicKey) rsaPublicKey).state;
    Field publicRngField = RSAOAEPPublicKey.State.class.getDeclaredField("rng");
    publicRngField.setAccessible(true);
    publicRngField.set(publicState, oaepSpec.getRandSeed());
    // Here we fix private key.
    RSAOAEPPrivateKey.State privateState = ((RSAOAEPPrivateKey) rsaPrivateKey).state;
    Field privateRngField = RSAOAEPPrivateKey.State.class.getDeclaredField("rng");
    privateRngField.setAccessible(true);
    privateRngField.set(privateState, pssSpec.getRandSalt());
    // Now we can even test the encryption/decryption on the RSA OAEP spec vectors,
    // as well as signing.
    assertTrue(rsaPrivateKey.isInitialized());
    assertEquals(rsaPrivateKey.getBitStrength(), 1024);
    ((RSAOAEPPrivateKey) rsaPrivateKey).resetDecryptor();
    assertArrayEquals(rsaPrivateKey.decrypt(oaepSpec.C), oaepSpec.M);
    // Also test that the public key counterpart is also deserialized properly
    // and is capable of encrypting the data (after deserialization) as needed.
    assertTrue(rsaPublicKey.isInitialized());
    assertEquals(rsaPublicKey.getBitStrength(), 1024);
    ((RSAOAEPPublicKey) rsaPublicKey).resetEncryptor();
    assertArrayEquals(rsaPublicKey.encrypt(oaepSpec.M), oaepSpec.C);
}
Also used : Field(java.lang.reflect.Field) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) AbstractPublicKey(com.icodici.crypto.AbstractPublicKey) AbstractPrivateKey(com.icodici.crypto.AbstractPrivateKey) Test(org.junit.Test)

Example 3 with AbstractPrivateKey

use of com.icodici.crypto.AbstractPrivateKey in project universa by UniversaBlockchain.

the class RSAOAEPPrivateKeyTest method sign.

/**
 * Test {@link RSAOAEPPrivateKey#sign}.
 */
@Test
public void sign() throws Exception {
    // Test on RSA vectors.
    AbstractPrivateKey rsaPrivateKey = pssSpec.getPrivateKey();
    assertArrayEquals(rsaPrivateKey.sign(pssSpec.M, HashType.SHA1, RSASSAPSSTestVectors.salt), pssSpec.S);
}
Also used : AbstractPrivateKey(com.icodici.crypto.AbstractPrivateKey) Test(org.junit.Test)

Example 4 with AbstractPrivateKey

use of com.icodici.crypto.AbstractPrivateKey in project universa by UniversaBlockchain.

the class RSAOAEPPrivateKeyTest method updateFromHashGoodSample.

/**
 * Test {@link RSAOAEPPrivateKey#updateFromHash} on success scenarios,
 * on some sample vector.
 */
@Test
public void updateFromHashGoodSample() throws Exception {
    Map map = new HashMap<String, Object>() {

        {
            put("e", Hex.decode("010001"));
            put("p", Hex.decode("d4f56e648a7fe26ab052144abfd9a9cdb3ebf220ac05c3e3eb13e42c245ab0578e185a8782bf9b4c1fcc1f577a01cf9c86afef7b4fbb22ecf4fee4409f94c40da7527e0a084ae274494826a7352de4611d36b21bd3d43b4684d997dffff76765b2033a2dd949ccc5521bf1b2889c75a4ce5ce7eb09269dfeaa1f859449c4f359fcbbfc6a00593c1948665c494e7c7edd185c541a7709de5e0528345d0fc31bd214b4d36a7f96cea8fc003730fbeff97bb14267c14dad794d4f949a65fa10fcbc27d1df170128c42d294161ccdaec5e4886da42920d4f6d89dad7a038625111cd75f5d0268009d4fed07adb08da146729e3bf8e882646ad97df0a1e9391656951"));
            put("q", Hex.decode("c7524e21b6d44fe1e7467168b5888047216584821d4b12370f5ca42eefdf729c9f328fe4eaad06ae52aff40bf099d5e8383b34a6a5ea6d52fadf1178546b7afa48eee25801015fe174294a59c0f3fe24f8c3dcdc1572c96b602235e4c26e0fa85c2e9852f529574afb7ccc53ba47c6705de1170bd5b41cd8ca80efd1c324e1a5091a0a5a895d00b857185fd73fd284da14503b6b48a5ee165ce981732ae163d21ae4426204bb8a7e23c6793d4e9c9501a57abbf5457f745ff62e78d8bb250fe41bec2b3c6f77e64876a1d63d0b19ead7e393c7507358b5dbc4ce2c21554c00aa8e8d38c78e72ce0ed306e9c3ae0cd4cc1e92842caf7b70e883daf43b0cf25267"));
            put("mgf1Hash", "SHA-512");
        }
    };
    AbstractPrivateKey privateKey = new RSAOAEPPrivateKey();
    privateKey.updateFromHash(map);
    AbstractPublicKey publicKey = privateKey.getPublicKey();
    assertTrue(privateKey.isInitialized());
    assertEquals(privateKey.getBitStrength(), 4096);
    // Also test that the public key counterpart is also deserialized properly
    // and is capable of encrypting the data (after deserialization) as needed.
    assertTrue(publicKey.isInitialized());
    assertEquals(publicKey.getBitStrength(), 4096);
    RSAOAEPPublicKey.State publicKeyState = ((RSAOAEPPublicKey) publicKey).state;
    // Default
    assertEquals(publicKeyState.oaepHashType, HashType.SHA1);
    assertEquals(publicKeyState.mgf1HashType, HashType.SHA512);
    RSAOAEPPrivateKey.State privateKeyState = ((RSAOAEPPrivateKey) privateKey).state;
    // Default
    assertEquals(privateKeyState.oaepHashType, HashType.SHA1);
    assertEquals(privateKeyState.mgf1HashType, HashType.SHA512);
}
Also used : HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) AbstractPublicKey(com.icodici.crypto.AbstractPublicKey) AbstractPrivateKey(com.icodici.crypto.AbstractPrivateKey) Test(org.junit.Test)

Example 5 with AbstractPrivateKey

use of com.icodici.crypto.AbstractPrivateKey in project universa by UniversaBlockchain.

the class RSAOAEPPrivateKeyTest method updateFromHashGoodSampleWithDefaultData.

/**
 * Test {@link RSAOAEPPrivateKey#updateFromHash} on success scenarios,
 * on some sample vector where some data is default.
 */
@Test
public void updateFromHashGoodSampleWithDefaultData() throws Exception {
    Map map = new HashMap<String, byte[]>() {

        {
            put("e", Hex.decode("010001"));
            put("p", Hex.decode("d4f56e648a7fe26ab052144abfd9a9cdb3ebf220ac05c3e3eb13e42c245ab0578e185a8782bf9b4c1fcc1f577a01cf9c86afef7b4fbb22ecf4fee4409f94c40da7527e0a084ae274494826a7352de4611d36b21bd3d43b4684d997dffff76765b2033a2dd949ccc5521bf1b2889c75a4ce5ce7eb09269dfeaa1f859449c4f359fcbbfc6a00593c1948665c494e7c7edd185c541a7709de5e0528345d0fc31bd214b4d36a7f96cea8fc003730fbeff97bb14267c14dad794d4f949a65fa10fcbc27d1df170128c42d294161ccdaec5e4886da42920d4f6d89dad7a038625111cd75f5d0268009d4fed07adb08da146729e3bf8e882646ad97df0a1e9391656951"));
            put("q", Hex.decode("c7524e21b6d44fe1e7467168b5888047216584821d4b12370f5ca42eefdf729c9f328fe4eaad06ae52aff40bf099d5e8383b34a6a5ea6d52fadf1178546b7afa48eee25801015fe174294a59c0f3fe24f8c3dcdc1572c96b602235e4c26e0fa85c2e9852f529574afb7ccc53ba47c6705de1170bd5b41cd8ca80efd1c324e1a5091a0a5a895d00b857185fd73fd284da14503b6b48a5ee165ce981732ae163d21ae4426204bb8a7e23c6793d4e9c9501a57abbf5457f745ff62e78d8bb250fe41bec2b3c6f77e64876a1d63d0b19ead7e393c7507358b5dbc4ce2c21554c00aa8e8d38c78e72ce0ed306e9c3ae0cd4cc1e92842caf7b70e883daf43b0cf25267"));
        // Not putting any of the optional fields.
        }
    };
    AbstractPrivateKey privateKey = new RSAOAEPPrivateKey();
    privateKey.updateFromHash(map);
    AbstractPublicKey publicKey = privateKey.getPublicKey();
    assertTrue(privateKey.isInitialized());
    assertEquals(privateKey.getBitStrength(), 4096);
    // Also test that the public key counterpart is also deserialized properly
    // and is capable of encrypting the data (after deserialization) as needed.
    assertTrue(publicKey.isInitialized());
    assertEquals(publicKey.getBitStrength(), 4096);
    RSAOAEPPublicKey.State publicKeyState = ((RSAOAEPPublicKey) publicKey).state;
    assertEquals(publicKeyState.oaepHashType, HashType.SHA1);
    assertEquals(publicKeyState.mgf1HashType, HashType.SHA1);
    RSAOAEPPrivateKey.State privateKeyState = ((RSAOAEPPrivateKey) privateKey).state;
    assertEquals(privateKeyState.oaepHashType, HashType.SHA1);
    assertEquals(privateKeyState.mgf1HashType, HashType.SHA1);
}
Also used : HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) AbstractPublicKey(com.icodici.crypto.AbstractPublicKey) AbstractPrivateKey(com.icodici.crypto.AbstractPrivateKey) Test(org.junit.Test)

Aggregations

AbstractPrivateKey (com.icodici.crypto.AbstractPrivateKey)8 Test (org.junit.Test)8 AbstractPublicKey (com.icodici.crypto.AbstractPublicKey)7 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Field (java.lang.reflect.Field)1 SecureRandom (java.security.SecureRandom)1 RSAKeyParameters (org.spongycastle.crypto.params.RSAKeyParameters)1 RSAPrivateCrtKeyParameters (org.spongycastle.crypto.params.RSAPrivateCrtKeyParameters)1