Search in sources :

Example 31 with OAEPParameterSpec

use of javax.crypto.spec.OAEPParameterSpec in project wycheproof by google.

the class RsaOaepTest method testOaep.

/**
 * Tests the signature verification with test vectors in a given JSON file.
 *
 * <p> Example format for test vectors
 * { "algorithm" : "RSA-OAEP",
 *   "schema" : "rsaes_oaep_decrypt_schema.json",
 *   "generatorVersion" : "0.7",
 *   ...
 *   "testGroups" : [
 *     {
 *       "d" : "...",
 *       "e" : "10001",
 *       "n" : "...",
 *       "keysize" : 2048,
 *       "sha" : "SHA-256",
 *       "mgf" : "MGF1",
 *       "mgfSha" : "SHA-256",
 *       "privateKeyPem" : "-----BEGIN RSA PRIVATE KEY-----\n...",
 *       "privateKeyPkcs8" : "...",
 *       "type" : "RSAES",
 *       "tests" : [
 *         {
 *           "tcId" : 1,
 *           "comment" : "",
 *           "msg" : "30313233343030",
 *           "ct" : "...",
 *           "label" : "",
 *           "result" : "valid",
 *           "flags" : [],
 *         },
 *        ...
 *
 * @param filename the filename of the test vectors
 * @param allowSkippingKeys if true then keys that cannot be constructed will not fail the test.
 *        Most of the tests below are using allowSkippingKeys == false. The reason for doing this
 *        is that providers have distinctive defaults. E.g., no OAEPParameterSpec is given then
 *        BouncyCastle and Conscrypt use the same hash function for hashing the label and for the
 *        mask generation function, while jdk uses MGF1SHA1. This is unfortunate and probably
 *        difficult to fix. Hence, the tests below simply require that providers support each
 *        others default parameters under the assumption that the OAEPParameterSpec is fully
 *        specified.
 */
public void testOaep(String filename, boolean allowSkippingKeys) throws Exception {
    JsonObject test = JsonUtil.getTestVectors(filename);
    // Compares the expected and actual JSON schema of the test vector file.
    // Mismatched JSON schemas will likely lead to a test failure.
    String generatorVersion = getString(test, "generatorVersion");
    String expectedSchema = "rsaes_oaep_decrypt_schema.json";
    String actualSchema = getString(test, "schema");
    if (!expectedSchema.equals(actualSchema)) {
        System.out.println("Expecting test vectors with schema " + expectedSchema + " found vectors with schema " + actualSchema + " generatorVersion:" + generatorVersion);
    }
    int numTests = test.get("numberOfTests").getAsInt();
    int cntTests = 0;
    int errors = 0;
    int skippedKeys = 0;
    for (JsonElement g : test.getAsJsonArray("testGroups")) {
        JsonObject group = g.getAsJsonObject();
        PrivateKey key;
        try {
            key = getPrivateKey(group);
        } catch (GeneralSecurityException ex) {
            skippedKeys++;
            if (!allowSkippingKeys) {
                System.out.printf("Key generation throws:%s\n", ex.toString());
            }
            continue;
        }
        String algorithm = getOaepAlgorithmName(group);
        Cipher decrypter = Cipher.getInstance(algorithm);
        for (JsonElement t : group.getAsJsonArray("tests")) {
            cntTests++;
            JsonObject testcase = t.getAsJsonObject();
            int tcid = testcase.get("tcId").getAsInt();
            String messageHex = TestUtil.bytesToHex(getBytes(testcase, "msg"));
            OAEPParameterSpec params = getOaepParameters(group, testcase);
            byte[] ciphertext = getBytes(testcase, "ct");
            String ciphertextHex = TestUtil.bytesToHex(ciphertext);
            String result = getString(testcase, "result");
            decrypter.init(Cipher.DECRYPT_MODE, key, params);
            byte[] decrypted = null;
            try {
                decrypted = decrypter.doFinal(ciphertext);
            } catch (GeneralSecurityException ex) {
                decrypted = null;
            } catch (Exception ex) {
                // Other exceptions (i.e. unchecked exceptions) are considered as error
                // since a third party should never be able to cause such exceptions.
                System.out.printf("Decryption throws %s. filename:%s tcId:%d ct:%s\n", ex.toString(), filename, tcid, ciphertextHex);
                decrypted = null;
            // TODO(bleichen): BouncyCastle throws some non-conforming exceptions.
            // For the moment we do not count this as a problem to avoid that
            // more serious bugs remain hidden.
            // errors++;
            }
            if (decrypted == null && result.equals("valid")) {
                System.out.printf("Valid ciphertext not decrypted. filename:%s tcId:%d ct:%s\n", filename, tcid, ciphertextHex);
                errors++;
            } else if (decrypted != null) {
                String decryptedHex = TestUtil.bytesToHex(decrypted);
                if (result.equals("invalid")) {
                    System.out.printf("Invalid ciphertext decrypted. filename:%s tcId:%d expected:%s decrypted:%s\n", filename, tcid, messageHex, decryptedHex);
                    errors++;
                } else if (!decryptedHex.equals(messageHex)) {
                    System.out.printf("Incorrect decryption. filename:%s tcId:%d expected:%s decrypted:%s\n", filename, tcid, messageHex, decryptedHex);
                    errors++;
                }
            }
        }
    }
    assertEquals(0, errors);
    if (skippedKeys > 0) {
        System.out.println("RSAES-OAEP: file:" + filename + " skipped key:" + skippedKeys);
        assertTrue(allowSkippingKeys);
    } else {
        assertEquals(numTests, cntTests);
    }
}
Also used : PrivateKey(java.security.PrivateKey) JsonElement(com.google.gson.JsonElement) GeneralSecurityException(java.security.GeneralSecurityException) JsonObject(com.google.gson.JsonObject) Cipher(javax.crypto.Cipher) GeneralSecurityException(java.security.GeneralSecurityException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) OAEPParameterSpec(javax.crypto.spec.OAEPParameterSpec)

Example 32 with OAEPParameterSpec

use of javax.crypto.spec.OAEPParameterSpec in project wycheproof by google.

the class RsaOaepTest method getOaepParameters.

protected static OAEPParameterSpec getOaepParameters(JsonObject group, JsonObject test) throws Exception {
    String sha = getString(group, "sha");
    String mgf = getString(group, "mgf");
    String mgfSha = getString(group, "mgfSha");
    PSource p = PSource.PSpecified.DEFAULT;
    if (test.has("label")) {
        p = new PSource.PSpecified(getBytes(test, "label"));
    }
    return new OAEPParameterSpec(sha, mgf, new MGF1ParameterSpec(mgfSha), p);
}
Also used : PSource(javax.crypto.spec.PSource) OAEPParameterSpec(javax.crypto.spec.OAEPParameterSpec) MGF1ParameterSpec(java.security.spec.MGF1ParameterSpec)

Example 33 with OAEPParameterSpec

use of javax.crypto.spec.OAEPParameterSpec in project wycheproof by google.

the class RsaOaepTest method printParameters.

protected static void printParameters(AlgorithmParameterSpec params) {
    if (params instanceof OAEPParameterSpec) {
        OAEPParameterSpec oaepParams = (OAEPParameterSpec) params;
        System.out.println("OAEPParameterSpec");
        System.out.println("digestAlgorithm:" + oaepParams.getDigestAlgorithm());
        System.out.println("mgfAlgorithm:" + oaepParams.getMGFAlgorithm());
        printParameters(oaepParams.getMGFParameters());
    } else if (params instanceof MGF1ParameterSpec) {
        MGF1ParameterSpec mgf1Params = (MGF1ParameterSpec) params;
        System.out.println("MGF1ParameterSpec");
        System.out.println("digestAlgorithm:" + mgf1Params.getDigestAlgorithm());
    } else {
        System.out.println(params.toString());
    }
}
Also used : OAEPParameterSpec(javax.crypto.spec.OAEPParameterSpec) MGF1ParameterSpec(java.security.spec.MGF1ParameterSpec)

Example 34 with OAEPParameterSpec

use of javax.crypto.spec.OAEPParameterSpec in project wycheproof by google.

the class RsaOaepTest method testDefaults.

/**
 * This is not a real test. The JCE algorithm names only specify one hash algorithm. But OAEP
 * uses two hases. One hash algorithm is used to hash the labels. The other hash algorithm is
 * used for the mask generation function.
 *
 * <p>Different provider use different default values for the hash function that is not specified
 * in the algorithm name. Jdk uses mgfsha1 as default. BouncyCastle and Conscrypt use the same
 * hash for labels and mgf. Every provider allows to specify all the parameters using
 * an OAEPParameterSpec instance.
 *
 * <p>This test simply tries a number of algorithm names for RSA-OAEP and prints the OAEP
 * parameters for the case where no OAEPParameterSpec is used.
 */
// TODO(bleichen): jdk11 will also add parameters to the RSA keys. This will need more tests.
@Test
public void testDefaults() throws Exception {
    String pubKey = "30820122300d06092a864886f70d01010105000382010f003082010a02820101" + "00bdf90898577911c71c4d9520c5f75108548e8dfd389afdbf9c997769b8594e" + "7dc51c6a1b88d1670ec4bb03fa550ba6a13d02c430bfe88ae4e2075163017f4d" + "8926ce2e46e068e88962f38112fc2dbd033e84e648d4a816c0f5bd89cadba0b4" + "d6cac01832103061cbb704ebacd895def6cff9d988c5395f2169a6807207333d" + "569150d7f569f7ebf4718ddbfa2cdbde4d82a9d5d8caeb467f71bfc0099b0625" + "a59d2bad12e3ff48f2fd50867b89f5f876ce6c126ced25f28b1996ee21142235" + "fb3aef9fe58d9e4ef6e4922711a3bbcd8adcfe868481fd1aa9c13e5c658f5172" + "617204314665092b4d8dca1b05dc7f4ecd7578b61edeb949275be8751a5a1fab" + "c30203010001";
    KeyFactory kf;
    kf = KeyFactory.getInstance("RSA");
    X509EncodedKeySpec x509keySpec = new X509EncodedKeySpec(TestUtil.hexToBytes(pubKey));
    PublicKey key = kf.generatePublic(x509keySpec);
    for (String oaepName : OaepAlgorithmNames) {
        try {
            Cipher c = Cipher.getInstance(oaepName);
            c.init(Cipher.ENCRYPT_MODE, key);
            System.out.println("Algorithm " + oaepName + " uses the following defaults");
            AlgorithmParameters params = c.getParameters();
            printParameters(params.getParameterSpec(OAEPParameterSpec.class));
        } catch (NoSuchAlgorithmException ex) {
            continue;
        }
    }
}
Also used : PublicKey(java.security.PublicKey) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) Cipher(javax.crypto.Cipher) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyFactory(java.security.KeyFactory) AlgorithmParameters(java.security.AlgorithmParameters) OAEPParameterSpec(javax.crypto.spec.OAEPParameterSpec) Test(org.junit.Test)

Aggregations

OAEPParameterSpec (javax.crypto.spec.OAEPParameterSpec)34 PSource (javax.crypto.spec.PSource)19 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)15 MGF1ParameterSpec (java.security.spec.MGF1ParameterSpec)11 Cipher (javax.crypto.Cipher)10 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)6 InvalidKeyException (java.security.InvalidKeyException)6 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)6 URISyntaxException (java.net.URISyntaxException)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 InvalidParameterSpecException (java.security.spec.InvalidParameterSpecException)4 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)4 AlgorithmParameters (java.security.AlgorithmParameters)3 XMLSecurityException (org.apache.xml.security.exceptions.XMLSecurityException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 URI (java.net.URI)2 InvalidParameterException (java.security.InvalidParameterException)2 Key (java.security.Key)2 NoSuchProviderException (java.security.NoSuchProviderException)2 SecureRandom (java.security.SecureRandom)2