Search in sources :

Example 6 with MGF1ParameterSpec

use of java.security.spec.MGF1ParameterSpec in project j2objc by google.

the class MGF1ParameterSpecTest method testMGF1ParameterSpec01.

//
// Tests
//
/**
     * Test #1 for <code>MGF1ParameterSpec</code> constructor<br>
     * Assertion: constructs new <code>MGF1ParameterSpec</code>
     * object using valid parameter
     */
public final void testMGF1ParameterSpec01() {
    try {
        MGF1ParameterSpec pgf = new MGF1ParameterSpec(testAlgName);
        assertNotNull(pgf);
        assertTrue(pgf instanceof MGF1ParameterSpec);
    } catch (Exception e) {
        fail("Unexpected exception: " + e);
    }
}
Also used : MGF1ParameterSpec(java.security.spec.MGF1ParameterSpec)

Example 7 with MGF1ParameterSpec

use of java.security.spec.MGF1ParameterSpec in project jdk8u_jdk by JetBrains.

the class TestOAEPPadding method testEncryptDecrypt.

private static void testEncryptDecrypt(OAEPParameterSpec spec, int dataLength) throws Exception {
    System.out.print("Testing OAEP with hash ");
    if (spec != null) {
        System.out.print(spec.getDigestAlgorithm() + " and MGF " + ((MGF1ParameterSpec) spec.getMGFParameters()).getDigestAlgorithm());
    } else {
        System.out.print("Default");
    }
    System.out.println(", " + dataLength + " bytes");
    Cipher c = Cipher.getInstance("RSA/ECB/OAEPPadding", cp);
    if (spec != null) {
        c.init(Cipher.ENCRYPT_MODE, publicKey, spec);
    } else {
        c.init(Cipher.ENCRYPT_MODE, publicKey);
    }
    byte[] data = new byte[dataLength];
    byte[] enc = c.doFinal(data);
    if (spec != null) {
        c.init(Cipher.DECRYPT_MODE, privateKey, spec);
    } else {
        c.init(Cipher.DECRYPT_MODE, privateKey);
    }
    byte[] dec = c.doFinal(enc);
    if (Arrays.equals(data, dec) == false) {
        throw new Exception("Data does not match");
    }
}
Also used : Cipher(javax.crypto.Cipher) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) MGF1ParameterSpec(java.security.spec.MGF1ParameterSpec)

Example 8 with MGF1ParameterSpec

use of java.security.spec.MGF1ParameterSpec in project robovm by robovm.

the class CipherSpi method initFromSpec.

private void initFromSpec(OAEPParameterSpec pSpec) throws NoSuchPaddingException {
    MGF1ParameterSpec mgfParams = (MGF1ParameterSpec) pSpec.getMGFParameters();
    Digest digest = DigestFactory.getDigest(mgfParams.getDigestAlgorithm());
    if (digest == null) {
        throw new NoSuchPaddingException("no match on OAEP constructor for digest algorithm: " + mgfParams.getDigestAlgorithm());
    }
    cipher = new OAEPEncoding(new RSABlindedEngine(), digest, ((PSource.PSpecified) pSpec.getPSource()).getValue());
    paramSpec = pSpec;
}
Also used : Digest(org.bouncycastle.crypto.Digest) RSABlindedEngine(org.bouncycastle.crypto.engines.RSABlindedEngine) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) OAEPEncoding(org.bouncycastle.crypto.encodings.OAEPEncoding) MGF1ParameterSpec(java.security.spec.MGF1ParameterSpec)

Example 9 with MGF1ParameterSpec

use of java.security.spec.MGF1ParameterSpec in project XobotOS by xamarin.

the class JCERSACipher method initFromSpec.

private void initFromSpec(OAEPParameterSpec pSpec) throws NoSuchPaddingException {
    MGF1ParameterSpec mgfParams = (MGF1ParameterSpec) pSpec.getMGFParameters();
    Digest digest = JCEDigestUtil.getDigest(mgfParams.getDigestAlgorithm());
    if (digest == null) {
        throw new NoSuchPaddingException("no match on OAEP constructor for digest algorithm: " + mgfParams.getDigestAlgorithm());
    }
    cipher = new OAEPEncoding(new RSABlindedEngine(), digest, ((PSource.PSpecified) pSpec.getPSource()).getValue());
    paramSpec = pSpec;
}
Also used : Digest(org.bouncycastle.crypto.Digest) RSABlindedEngine(org.bouncycastle.crypto.engines.RSABlindedEngine) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) OAEPEncoding(org.bouncycastle.crypto.encodings.OAEPEncoding) MGF1ParameterSpec(java.security.spec.MGF1ParameterSpec)

Example 10 with MGF1ParameterSpec

use of java.security.spec.MGF1ParameterSpec in project XobotOS by xamarin.

the class JCERSACipher 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) {
                throw new InvalidKeyException("mode 1 requires RSAPrivateKey");
            }
            param = RSAUtil.generatePublicKeyParameter((RSAPublicKey) key);
        } else if (key instanceof RSAPrivateKey) {
            if (publicKeyOnly) {
                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 = JCEDigestUtil.getDigest(spec.getDigestAlgorithm());
            if (digest == null) {
                throw new InvalidAlgorithmParameterException("no match on digest algorithm: " + spec.getDigestAlgorithm());
            }
            MGF1ParameterSpec mgfParams = (MGF1ParameterSpec) spec.getMGFParameters();
            Digest mgfDigest = JCEDigestUtil.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());
        }
    }
    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)

Aggregations

MGF1ParameterSpec (java.security.spec.MGF1ParameterSpec)13 OAEPParameterSpec (javax.crypto.spec.OAEPParameterSpec)4 Digest (org.bouncycastle.crypto.Digest)4 OAEPEncoding (org.bouncycastle.crypto.encodings.OAEPEncoding)4 RSABlindedEngine (org.bouncycastle.crypto.engines.RSABlindedEngine)4 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)2 InvalidKeyException (java.security.InvalidKeyException)2 InvalidParameterException (java.security.InvalidParameterException)2 SecureRandom (java.security.SecureRandom)2 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)2 RSAPublicKey (java.security.interfaces.RSAPublicKey)2 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)2 PSource (javax.crypto.spec.PSource)2 CipherParameters (org.bouncycastle.crypto.CipherParameters)2 ParametersWithRandom (org.bouncycastle.crypto.params.ParametersWithRandom)2 SignatureAlgorithm (io.jsonwebtoken.SignatureAlgorithm)1 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)1 InvalidParameterSpecException (java.security.spec.InvalidParameterSpecException)1 PSSParameterSpec (java.security.spec.PSSParameterSpec)1 HashMap (java.util.HashMap)1