Search in sources :

Example 1 with OAEPParameterSpec

use of javax.crypto.spec.OAEPParameterSpec in project robovm by robovm.

the class CipherSpi method engineInit.

protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    CipherParameters param;
    if (params == null || params instanceof OAEPParameterSpec) {
        if (key instanceof RSAPublicKey) {
            if (privateKeyOnly && opmode == Cipher.ENCRYPT_MODE) {
                throw new InvalidKeyException("mode 1 requires RSAPrivateKey");
            }
            param = RSAUtil.generatePublicKeyParameter((RSAPublicKey) key);
        } else if (key instanceof RSAPrivateKey) {
            if (publicKeyOnly && opmode == Cipher.ENCRYPT_MODE) {
                throw new InvalidKeyException("mode 2 requires RSAPublicKey");
            }
            param = RSAUtil.generatePrivateKeyParameter((RSAPrivateKey) key);
        } else {
            throw new InvalidKeyException("unknown key type passed to RSA");
        }
        if (params != null) {
            OAEPParameterSpec spec = (OAEPParameterSpec) params;
            paramSpec = params;
            if (!spec.getMGFAlgorithm().equalsIgnoreCase("MGF1") && !spec.getMGFAlgorithm().equals(PKCSObjectIdentifiers.id_mgf1.getId())) {
                throw new InvalidAlgorithmParameterException("unknown mask generation function specified");
            }
            if (!(spec.getMGFParameters() instanceof MGF1ParameterSpec)) {
                throw new InvalidAlgorithmParameterException("unkown MGF parameters");
            }
            Digest digest = DigestFactory.getDigest(spec.getDigestAlgorithm());
            if (digest == null) {
                throw new InvalidAlgorithmParameterException("no match on digest algorithm: " + spec.getDigestAlgorithm());
            }
            MGF1ParameterSpec mgfParams = (MGF1ParameterSpec) spec.getMGFParameters();
            Digest mgfDigest = DigestFactory.getDigest(mgfParams.getDigestAlgorithm());
            if (mgfDigest == null) {
                throw new InvalidAlgorithmParameterException("no match on MGF digest algorithm: " + mgfParams.getDigestAlgorithm());
            }
            cipher = new OAEPEncoding(new RSABlindedEngine(), digest, mgfDigest, ((PSource.PSpecified) spec.getPSource()).getValue());
        }
    } else {
        throw new IllegalArgumentException("unknown parameter type.");
    }
    if (!(cipher instanceof RSABlindedEngine)) {
        if (random != null) {
            param = new ParametersWithRandom(param, random);
        } else {
            param = new ParametersWithRandom(param, new SecureRandom());
        }
    }
    bOut.reset();
    switch(opmode) {
        case Cipher.ENCRYPT_MODE:
        case Cipher.WRAP_MODE:
            cipher.init(true, param);
            break;
        case Cipher.DECRYPT_MODE:
        case Cipher.UNWRAP_MODE:
            cipher.init(false, param);
            break;
        default:
            throw new InvalidParameterException("unknown opmode " + opmode + " passed to RSA");
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) Digest(org.bouncycastle.crypto.Digest) ParametersWithRandom(org.bouncycastle.crypto.params.ParametersWithRandom) SecureRandom(java.security.SecureRandom) InvalidKeyException(java.security.InvalidKeyException) OAEPParameterSpec(javax.crypto.spec.OAEPParameterSpec) CipherParameters(org.bouncycastle.crypto.CipherParameters) InvalidParameterException(java.security.InvalidParameterException) RSAPublicKey(java.security.interfaces.RSAPublicKey) RSABlindedEngine(org.bouncycastle.crypto.engines.RSABlindedEngine) OAEPEncoding(org.bouncycastle.crypto.encodings.OAEPEncoding) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) MGF1ParameterSpec(java.security.spec.MGF1ParameterSpec)

Example 2 with OAEPParameterSpec

use of javax.crypto.spec.OAEPParameterSpec in project robovm by robovm.

the class OAEPParameterSpecTest method testOAEPParameterSpec.

/**
     * OAEPParameterSpec(String mdName, String mgfName, AlgorithmParameterSpec
     * mgfSpec, PSource pSrc) method testing. Tests that NullPointerException
     * is thrown in the case of inappropriate constructor parameters and checks
     * the value of DEFAULT field.
     */
public void testOAEPParameterSpec() {
    // using init values for OAEPParameterSpec.DEFAULT
    String mdName = "SHA-1";
    String mgfName = "MGF1";
    AlgorithmParameterSpec mgfSpec = MGF1ParameterSpec.SHA1;
    PSource pSrc = PSource.PSpecified.DEFAULT;
    try {
        new OAEPParameterSpec(null, mgfName, mgfSpec, pSrc);
        fail("NullPointerException should be thrown in the case of " + "null mdName.");
    } catch (NullPointerException e) {
    }
    try {
        new OAEPParameterSpec(mdName, null, mgfSpec, pSrc);
        fail("NullPointerException should be thrown in the case of " + "null mgfName.");
    } catch (NullPointerException e) {
    }
    try {
        new OAEPParameterSpec(mdName, mgfName, mgfSpec, null);
        fail("NullPointerException should be thrown in the case of " + "null pSrc.");
    } catch (NullPointerException e) {
    }
    assertTrue("The message digest algorithm name of " + "OAEPParameterSpec.DEFAULT field should be " + mdName, OAEPParameterSpec.DEFAULT.getDigestAlgorithm().equals(mdName));
    assertTrue("The mask generation function algorithm name of " + "OAEPParameterSpec.DEFAULT field should be " + mgfName, OAEPParameterSpec.DEFAULT.getMGFAlgorithm().equals(mgfName));
    assertTrue("The mask generation function parameters of " + "OAEPParameterSpec.DEFAULT field should be the same object " + "as MGF1ParameterSpec.SHA1", OAEPParameterSpec.DEFAULT.getMGFParameters() == MGF1ParameterSpec.SHA1);
    assertTrue("The source of the encoding input P of " + "OAEPParameterSpec.DEFAULT field should be the same object " + "PSource.PSpecified.DEFAULT", OAEPParameterSpec.DEFAULT.getPSource() == PSource.PSpecified.DEFAULT);
}
Also used : PSource(javax.crypto.spec.PSource) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) OAEPParameterSpec(javax.crypto.spec.OAEPParameterSpec)

Example 3 with OAEPParameterSpec

use of javax.crypto.spec.OAEPParameterSpec in project robovm by robovm.

the class OAEPParameterSpecTest method testGetMGFAlgorithm.

/**
     * getMGFAlgorithm() method testing.
     */
public void testGetMGFAlgorithm() {
    String mdName = "SHA-1";
    String mgfName = "MGF1";
    AlgorithmParameterSpec mgfSpec = MGF1ParameterSpec.SHA1;
    PSource pSrc = PSource.PSpecified.DEFAULT;
    OAEPParameterSpec ps = new OAEPParameterSpec(mdName, mgfName, mgfSpec, pSrc);
    assertTrue("The returned value does not equal to the " + "value specified in the constructor.", ps.getMGFAlgorithm().equals(mgfName));
}
Also used : PSource(javax.crypto.spec.PSource) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) OAEPParameterSpec(javax.crypto.spec.OAEPParameterSpec)

Example 4 with OAEPParameterSpec

use of javax.crypto.spec.OAEPParameterSpec in project jdk8u_jdk by JetBrains.

the class TestOAEPParameterSpec method runTest.

private static boolean runTest(String mdName, MGF1ParameterSpec mgfSpec, byte[] p) throws Exception {
    OAEPParameterSpec spec = new OAEPParameterSpec(mdName, "MGF1", mgfSpec, new PSource.PSpecified(p));
    cp = Security.getProvider("SunJCE");
    System.out.println("Testing provider " + cp.getName() + "...");
    AlgorithmParameters ap = AlgorithmParameters.getInstance("OAEP", cp);
    ap.init(spec);
    byte[] encoding = ap.getEncoded();
    AlgorithmParameters ap2 = AlgorithmParameters.getInstance("OAEP", cp);
    ap2.init(encoding);
    OAEPParameterSpec spec2 = (OAEPParameterSpec) ap2.getParameterSpec(OAEPParameterSpec.class);
    return compareSpec(spec, spec2);
}
Also used : PSource(javax.crypto.spec.PSource) OAEPParameterSpec(javax.crypto.spec.OAEPParameterSpec)

Example 5 with OAEPParameterSpec

use of javax.crypto.spec.OAEPParameterSpec in project santuario-java by apache.

the class XMLCipher method constructOAEPParameters.

/**
 * Construct an OAEPParameterSpec object from the given parameters
 */
private OAEPParameterSpec constructOAEPParameters(String encryptionAlgorithm, String digestAlgorithm, String mgfAlgorithm, byte[] oaepParams) {
    if (XMLCipher.RSA_OAEP.equals(encryptionAlgorithm) || XMLCipher.RSA_OAEP_11.equals(encryptionAlgorithm)) {
        String jceDigestAlgorithm = "SHA-1";
        if (digestAlgorithm != null) {
            jceDigestAlgorithm = JCEMapper.translateURItoJCEID(digestAlgorithm);
        }
        PSource.PSpecified pSource = PSource.PSpecified.DEFAULT;
        if (oaepParams != null) {
            pSource = new PSource.PSpecified(oaepParams);
        }
        MGF1ParameterSpec mgfParameterSpec = new MGF1ParameterSpec("SHA-1");
        if (XMLCipher.RSA_OAEP_11.equals(encryptionAlgorithm)) {
            if (EncryptionConstants.MGF1_SHA256.equals(mgfAlgorithm)) {
                mgfParameterSpec = new MGF1ParameterSpec("SHA-256");
            } else if (EncryptionConstants.MGF1_SHA384.equals(mgfAlgorithm)) {
                mgfParameterSpec = new MGF1ParameterSpec("SHA-384");
            } else if (EncryptionConstants.MGF1_SHA512.equals(mgfAlgorithm)) {
                mgfParameterSpec = new MGF1ParameterSpec("SHA-512");
            }
        }
        return new OAEPParameterSpec(jceDigestAlgorithm, "MGF1", mgfParameterSpec, pSource);
    }
    return null;
}
Also used : PSource(javax.crypto.spec.PSource) MGF1ParameterSpec(java.security.spec.MGF1ParameterSpec) OAEPParameterSpec(javax.crypto.spec.OAEPParameterSpec)

Aggregations

OAEPParameterSpec (javax.crypto.spec.OAEPParameterSpec)32 PSource (javax.crypto.spec.PSource)19 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)13 MGF1ParameterSpec (java.security.spec.MGF1ParameterSpec)11 Cipher (javax.crypto.Cipher)8 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 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 BadPaddingException (javax.crypto.BadPaddingException)2