Search in sources :

Example 1 with RsaSsaPkcs1PrivateKey

use of com.google.crypto.tink.proto.RsaSsaPkcs1PrivateKey in project tink by google.

the class RsaSsaPkcs1SignKeyManagerTest method createCorruptedModulusPrimitive_throws.

@Test
public void createCorruptedModulusPrimitive_throws() throws Exception {
    RsaSsaPkcs1KeyFormat format = createKeyFormat(HashType.SHA512, 4096, RSAKeyGenParameterSpec.F4);
    RsaSsaPkcs1PrivateKey originalKey = factory.createKey(format);
    byte[] originalN = originalKey.getPublicKey().getN().toByteArray();
    originalN[0] = (byte) (originalN[0] ^ 0x01);
    ByteString corruptedN = ByteString.copyFrom(originalN);
    RsaSsaPkcs1PublicKey corruptedPub = RsaSsaPkcs1PublicKey.newBuilder().setVersion(originalKey.getPublicKey().getVersion()).setN(corruptedN).setE(originalKey.getPublicKey().getE()).build();
    RsaSsaPkcs1PrivateKey corruptedKey = RsaSsaPkcs1PrivateKey.newBuilder().setVersion(originalKey.getVersion()).setPublicKey(corruptedPub).setD(originalKey.getD()).setP(originalKey.getP()).setQ(originalKey.getQ()).setDp(originalKey.getDp()).setDq(originalKey.getDq()).setCrt(originalKey.getCrt()).build();
    assertThrows(GeneralSecurityException.class, () -> manager.getPrimitive(corruptedKey, PublicKeySign.class));
}
Also used : RsaSsaPkcs1PrivateKey(com.google.crypto.tink.proto.RsaSsaPkcs1PrivateKey) RsaSsaPkcs1PublicKey(com.google.crypto.tink.proto.RsaSsaPkcs1PublicKey) ByteString(com.google.protobuf.ByteString) PublicKeySign(com.google.crypto.tink.PublicKeySign) RsaSsaPkcs1KeyFormat(com.google.crypto.tink.proto.RsaSsaPkcs1KeyFormat) Test(org.junit.Test)

Example 2 with RsaSsaPkcs1PrivateKey

use of com.google.crypto.tink.proto.RsaSsaPkcs1PrivateKey in project tink by google.

the class RsaSsaPkcs1SignKeyManagerTest method getPublicKey_correctValues.

@Test
public void getPublicKey_correctValues() throws Exception {
    if (TestUtil.isTsan()) {
        // factory.createKey is too slow in Tsan.
        return;
    }
    RsaSsaPkcs1KeyFormat format = createKeyFormat(HashType.SHA256, 3072, RSAKeyGenParameterSpec.F4);
    RsaSsaPkcs1PrivateKey key = factory.createKey(format);
    assertThat(manager.getPublicKey(key)).isEqualTo(key.getPublicKey());
}
Also used : RsaSsaPkcs1PrivateKey(com.google.crypto.tink.proto.RsaSsaPkcs1PrivateKey) RsaSsaPkcs1KeyFormat(com.google.crypto.tink.proto.RsaSsaPkcs1KeyFormat) Test(org.junit.Test)

Example 3 with RsaSsaPkcs1PrivateKey

use of com.google.crypto.tink.proto.RsaSsaPkcs1PrivateKey in project tink by google.

the class RsaSsaPkcs1SignKeyManagerTest method createKey_alwaysNewElement.

@Test
public void createKey_alwaysNewElement() throws Exception {
    if (TestUtil.isTsan()) {
        // factory.createKey is too slow in Tsan.
        return;
    }
    RsaSsaPkcs1KeyFormat format = createKeyFormat(HashType.SHA256, 3072, RSAKeyGenParameterSpec.F4);
    Set<String> keys = new TreeSet<>();
    // Calls newKey multiple times and make sure that they generate different keys -- takes about a
    // second per key.
    int numTests = 5;
    for (int i = 0; i < numTests; i++) {
        RsaSsaPkcs1PrivateKey key = factory.createKey(format);
        keys.add(TestUtil.hexEncode(key.getQ().toByteArray()));
        keys.add(TestUtil.hexEncode(key.getP().toByteArray()));
    }
    assertThat(keys).hasSize(2 * numTests);
}
Also used : RsaSsaPkcs1PrivateKey(com.google.crypto.tink.proto.RsaSsaPkcs1PrivateKey) TreeSet(java.util.TreeSet) ByteString(com.google.protobuf.ByteString) RsaSsaPkcs1KeyFormat(com.google.crypto.tink.proto.RsaSsaPkcs1KeyFormat) Test(org.junit.Test)

Example 4 with RsaSsaPkcs1PrivateKey

use of com.google.crypto.tink.proto.RsaSsaPkcs1PrivateKey in project tink by google.

the class RsaSsaPkcs1VerifyKeyManagerTest method validateKey_generated.

@Test
public void validateKey_generated() throws Exception {
    if (TestUtil.isTsan()) {
        // factory.createKey is too slow in Tsan.
        return;
    }
    RsaSsaPkcs1KeyFormat keyFormat = RsaSsaPkcs1KeyFormat.newBuilder().setParams(RsaSsaPkcs1Params.newBuilder().setHashType(HashType.SHA256)).setModulusSizeInBits(3072).setPublicExponent(ByteString.copyFrom(RSAKeyGenParameterSpec.F4.toByteArray())).build();
    RsaSsaPkcs1PrivateKey privateKey = factory.createKey(keyFormat);
    RsaSsaPkcs1PublicKey publicKey = signManager.getPublicKey(privateKey);
    verifyManager.validateKey(publicKey);
}
Also used : RsaSsaPkcs1PrivateKey(com.google.crypto.tink.proto.RsaSsaPkcs1PrivateKey) RsaSsaPkcs1PublicKey(com.google.crypto.tink.proto.RsaSsaPkcs1PublicKey) RsaSsaPkcs1KeyFormat(com.google.crypto.tink.proto.RsaSsaPkcs1KeyFormat) Test(org.junit.Test)

Example 5 with RsaSsaPkcs1PrivateKey

use of com.google.crypto.tink.proto.RsaSsaPkcs1PrivateKey in project tink by google.

the class RsaSsaPkcs1VerifyKeyManagerTest method createPrimitive_anotherKey_throws.

@Test
public void createPrimitive_anotherKey_throws() throws Exception {
    if (TestUtil.isTsan()) {
        // factory.createKey is too slow in Tsan.
        return;
    }
    RsaSsaPkcs1KeyFormat keyFormat = RsaSsaPkcs1KeyFormat.newBuilder().setParams(RsaSsaPkcs1Params.newBuilder().setHashType(HashType.SHA256)).setModulusSizeInBits(3072).setPublicExponent(ByteString.copyFrom(RSAKeyGenParameterSpec.F4.toByteArray())).build();
    RsaSsaPkcs1PrivateKey privateKey = factory.createKey(keyFormat);
    // Create a different key.
    RsaSsaPkcs1PublicKey publicKey = signManager.getPublicKey(factory.createKey(keyFormat));
    PublicKeySign signer = signManager.getPrimitive(privateKey, PublicKeySign.class);
    PublicKeyVerify verifier = verifyManager.getPrimitive(publicKey, PublicKeyVerify.class);
    byte[] message = Random.randBytes(135);
    byte[] signature = signer.sign(message);
    assertThrows(GeneralSecurityException.class, () -> verifier.verify(signature, message));
}
Also used : RsaSsaPkcs1PrivateKey(com.google.crypto.tink.proto.RsaSsaPkcs1PrivateKey) RsaSsaPkcs1PublicKey(com.google.crypto.tink.proto.RsaSsaPkcs1PublicKey) PublicKeyVerify(com.google.crypto.tink.PublicKeyVerify) PublicKeySign(com.google.crypto.tink.PublicKeySign) RsaSsaPkcs1KeyFormat(com.google.crypto.tink.proto.RsaSsaPkcs1KeyFormat) Test(org.junit.Test)

Aggregations

RsaSsaPkcs1KeyFormat (com.google.crypto.tink.proto.RsaSsaPkcs1KeyFormat)9 RsaSsaPkcs1PrivateKey (com.google.crypto.tink.proto.RsaSsaPkcs1PrivateKey)9 Test (org.junit.Test)9 PublicKeySign (com.google.crypto.tink.PublicKeySign)4 RsaSsaPkcs1PublicKey (com.google.crypto.tink.proto.RsaSsaPkcs1PublicKey)4 PublicKeyVerify (com.google.crypto.tink.PublicKeyVerify)3 ByteString (com.google.protobuf.ByteString)2 RsaSsaPkcs1VerifyJce (com.google.crypto.tink.subtle.RsaSsaPkcs1VerifyJce)1 BigInteger (java.math.BigInteger)1 KeyFactory (java.security.KeyFactory)1 RSAPublicKey (java.security.interfaces.RSAPublicKey)1 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)1 TreeSet (java.util.TreeSet)1