java.security.spec.MGF1ParameterSpec Java Examples

The following examples show how to use java.security.spec.MGF1ParameterSpec. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PSSParameters.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected void engineInit(AlgorithmParameterSpec paramSpec)
        throws InvalidParameterSpecException {
    if (!(paramSpec instanceof PSSParameterSpec)) {
        throw new InvalidParameterSpecException
            ("Inappropriate parameter specification");
    }
    PSSParameterSpec spec = (PSSParameterSpec) paramSpec;

    String mgfName = spec.getMGFAlgorithm();
    if (!spec.getMGFAlgorithm().equalsIgnoreCase("MGF1")) {
        throw new InvalidParameterSpecException("Unsupported mgf " +
            mgfName + "; MGF1 only");
    }
    AlgorithmParameterSpec mgfSpec = spec.getMGFParameters();
    if (!(mgfSpec instanceof MGF1ParameterSpec)) {
        throw new InvalidParameterSpecException("Inappropriate mgf " +
            "parameters; non-null MGF1ParameterSpec only");
    }
    this.spec = spec;
}
 
Example #2
Source File: TestOAEPParameterSpec.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private static boolean compareMGF(OAEPParameterSpec s1,
    OAEPParameterSpec s2) {
    String alg1 = s1.getMGFAlgorithm();
    String alg2 = s2.getMGFAlgorithm();
    if (alg1.equals(alg2)) {
        MGF1ParameterSpec mp1 = (MGF1ParameterSpec)s1.getMGFParameters();
        MGF1ParameterSpec mp2 = (MGF1ParameterSpec)s2.getMGFParameters();
        alg1 = mp1.getDigestAlgorithm();
        alg2 = mp2.getDigestAlgorithm();
        if (alg1.equals(alg2)) {
            return true;
        } else {
            System.out.println("MGF's MD algos: " + alg1 + " vs " + alg2);
            return false;
        }
    } else {
        System.out.println("MGF algos: " + alg1 + " vs " + alg2);
        return false;
    }
}
 
Example #3
Source File: TestOAEPParameterSpec.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] argv) throws Exception {
    boolean status = true;
    byte[] p = { (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04 };
    status &= runTest("SHA-224", MGF1ParameterSpec.SHA224, p);
    status &= runTest("SHA-256", MGF1ParameterSpec.SHA256, p);
    status &= runTest("SHA-384", MGF1ParameterSpec.SHA384, p);
    status &= runTest("SHA-512", MGF1ParameterSpec.SHA512, p);
    status &= runTest("SHA", MGF1ParameterSpec.SHA1, new byte[0]);
    status &= runTest("SHA-1", MGF1ParameterSpec.SHA1, new byte[0]);
    status &= runTest("SHA1", MGF1ParameterSpec.SHA1, new byte[0]);
    if (status) {
        System.out.println("Test Passed");
    } else {
        throw new Exception("One or More Test Failed");
    }
}
 
Example #4
Source File: TestOAEPParameterSpec.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] argv) throws Exception {
    boolean status = true;
    byte[] p = { (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04 };
    status &= runTest("SHA-224", MGF1ParameterSpec.SHA224, p);
    status &= runTest("SHA-256", MGF1ParameterSpec.SHA256, p);
    status &= runTest("SHA-384", MGF1ParameterSpec.SHA384, p);
    status &= runTest("SHA-512", MGF1ParameterSpec.SHA512, p);
    status &= runTest("SHA", MGF1ParameterSpec.SHA1, new byte[0]);
    status &= runTest("SHA-1", MGF1ParameterSpec.SHA1, new byte[0]);
    status &= runTest("SHA1", MGF1ParameterSpec.SHA1, new byte[0]);
    if (status) {
        System.out.println("Test Passed");
    } else {
        throw new Exception("One or More Test Failed");
    }
}
 
Example #5
Source File: TestOAEPParameterSpec.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static boolean compareMGF(OAEPParameterSpec s1,
    OAEPParameterSpec s2) {
    String alg1 = s1.getMGFAlgorithm();
    String alg2 = s2.getMGFAlgorithm();
    if (alg1.equals(alg2)) {
        MGF1ParameterSpec mp1 = (MGF1ParameterSpec)s1.getMGFParameters();
        MGF1ParameterSpec mp2 = (MGF1ParameterSpec)s2.getMGFParameters();
        alg1 = mp1.getDigestAlgorithm();
        alg2 = mp2.getDigestAlgorithm();
        if (alg1.equals(alg2)) {
            return true;
        } else {
            System.out.println("MGF's MD algos: " + alg1 + " vs " + alg2);
            return false;
        }
    } else {
        System.out.println("MGF algos: " + alg1 + " vs " + alg2);
        return false;
    }
}
 
Example #6
Source File: TestOAEPParameterSpec.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
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);
}
 
Example #7
Source File: TestOAEPParameterSpec.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] argv) throws Exception {
    boolean status = true;
    byte[] p = { (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04 };
    status &= runTest("SHA-224", MGF1ParameterSpec.SHA224, p);
    status &= runTest("SHA-256", MGF1ParameterSpec.SHA256, p);
    status &= runTest("SHA-384", MGF1ParameterSpec.SHA384, p);
    status &= runTest("SHA-512", MGF1ParameterSpec.SHA512, p);
    status &= runTest("SHA", MGF1ParameterSpec.SHA1, new byte[0]);
    status &= runTest("SHA-1", MGF1ParameterSpec.SHA1, new byte[0]);
    status &= runTest("SHA1", MGF1ParameterSpec.SHA1, new byte[0]);
    if (status) {
        System.out.println("Test Passed");
    } else {
        throw new Exception("One or More Test Failed");
    }
}
 
Example #8
Source File: TestOAEPParameterSpec.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private static boolean compareMGF(OAEPParameterSpec s1,
    OAEPParameterSpec s2) {
    String alg1 = s1.getMGFAlgorithm();
    String alg2 = s2.getMGFAlgorithm();
    if (alg1.equals(alg2)) {
        MGF1ParameterSpec mp1 = (MGF1ParameterSpec)s1.getMGFParameters();
        MGF1ParameterSpec mp2 = (MGF1ParameterSpec)s2.getMGFParameters();
        alg1 = mp1.getDigestAlgorithm();
        alg2 = mp2.getDigestAlgorithm();
        if (alg1.equals(alg2)) {
            return true;
        } else {
            System.out.println("MGF's MD algos: " + alg1 + " vs " + alg2);
            return false;
        }
    } else {
        System.out.println("MGF algos: " + alg1 + " vs " + alg2);
        return false;
    }
}
 
Example #9
Source File: TestOAEPParameterSpec.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
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);
}
 
Example #10
Source File: TestOAEPParameterSpec.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] argv) throws Exception {
    boolean status = true;
    byte[] p = { (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04 };
    status &= runTest("SHA-224", MGF1ParameterSpec.SHA224, p);
    status &= runTest("SHA-256", MGF1ParameterSpec.SHA256, p);
    status &= runTest("SHA-384", MGF1ParameterSpec.SHA384, p);
    status &= runTest("SHA-512", MGF1ParameterSpec.SHA512, p);
    status &= runTest("SHA", MGF1ParameterSpec.SHA1, new byte[0]);
    status &= runTest("SHA-1", MGF1ParameterSpec.SHA1, new byte[0]);
    status &= runTest("SHA1", MGF1ParameterSpec.SHA1, new byte[0]);
    if (status) {
        System.out.println("Test Passed");
    } else {
        throw new Exception("One or More Test Failed");
    }
}
 
Example #11
Source File: TestOAEPParameterSpec.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] argv) throws Exception {
    boolean status = true;
    byte[] p = { (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04 };
    status &= runTest("SHA-224", MGF1ParameterSpec.SHA224, p);
    status &= runTest("SHA-256", MGF1ParameterSpec.SHA256, p);
    status &= runTest("SHA-384", MGF1ParameterSpec.SHA384, p);
    status &= runTest("SHA-512", MGF1ParameterSpec.SHA512, p);
    status &= runTest("SHA", MGF1ParameterSpec.SHA1, new byte[0]);
    status &= runTest("SHA-1", MGF1ParameterSpec.SHA1, new byte[0]);
    status &= runTest("SHA1", MGF1ParameterSpec.SHA1, new byte[0]);
    if (status) {
        System.out.println("Test Passed");
    } else {
        throw new Exception("One or More Test Failed");
    }
}
 
Example #12
Source File: TestOAEPParameterSpec.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private static boolean compareMGF(OAEPParameterSpec s1,
    OAEPParameterSpec s2) {
    String alg1 = s1.getMGFAlgorithm();
    String alg2 = s2.getMGFAlgorithm();
    if (alg1.equals(alg2)) {
        MGF1ParameterSpec mp1 = (MGF1ParameterSpec)s1.getMGFParameters();
        MGF1ParameterSpec mp2 = (MGF1ParameterSpec)s2.getMGFParameters();
        alg1 = mp1.getDigestAlgorithm();
        alg2 = mp2.getDigestAlgorithm();
        if (alg1.equals(alg2)) {
            return true;
        } else {
            System.out.println("MGF's MD algos: " + alg1 + " vs " + alg2);
            return false;
        }
    } else {
        System.out.println("MGF algos: " + alg1 + " vs " + alg2);
        return false;
    }
}
 
Example #13
Source File: TestOAEPParameterSpec.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
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);
}
 
Example #14
Source File: AlgorithmParametersSpi.java    From RipplePower with Apache License 2.0 6 votes vote down vote up
/**
 * Return the PKCS#1 ASN.1 structure RSAES-OAEP-params.
 */
protected byte[] engineGetEncoded() 
{
    AlgorithmIdentifier hashAlgorithm = new AlgorithmIdentifier(
                                                    DigestFactory.getOID(currentSpec.getDigestAlgorithm()),
                                                    DERNull.INSTANCE);
    MGF1ParameterSpec mgfSpec = (MGF1ParameterSpec)currentSpec.getMGFParameters();
    AlgorithmIdentifier maskGenAlgorithm = new AlgorithmIdentifier(
                                                    PKCSObjectIdentifiers.id_mgf1,
                                                    new AlgorithmIdentifier(DigestFactory.getOID(mgfSpec.getDigestAlgorithm()), DERNull.INSTANCE));
    PSource.PSpecified      pSource = (PSource.PSpecified)currentSpec.getPSource();
    AlgorithmIdentifier pSourceAlgorithm = new AlgorithmIdentifier(
                                                    PKCSObjectIdentifiers.id_pSpecified, new DEROctetString(pSource.getValue()));
    RSAESOAEPparams oaepP = new RSAESOAEPparams(hashAlgorithm, maskGenAlgorithm, pSourceAlgorithm);
    
    try
    {
        return oaepP.getEncoded(ASN1Encoding.DER);
    }
    catch (IOException e)
    {
        throw new RuntimeException("Error encoding OAEPParameters");
    }
}
 
Example #15
Source File: PFSecurityUtils.java    From PFLockScreen-Android with Apache License 2.0 6 votes vote down vote up
private void initEncodeCipher(Cipher cipher, String alias, KeyStore keyStore)
        throws PFSecurityException {
    try {
        final PublicKey key = keyStore.getCertificate(alias).getPublicKey();
        final PublicKey unrestricted = KeyFactory.getInstance(key.getAlgorithm()).generatePublic(
                new X509EncodedKeySpec(key.getEncoded()));
        final OAEPParameterSpec spec = new OAEPParameterSpec("SHA-256", "MGF1",
                MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT);
        cipher.init(Cipher.ENCRYPT_MODE, unrestricted, spec);
    } catch (KeyStoreException | InvalidKeySpecException |
            NoSuchAlgorithmException | InvalidKeyException |
            InvalidAlgorithmParameterException e) {
        throw new PFSecurityException(
                "Can not initialize Encode Cipher:" + e.getMessage(),
                PFSecurityUtilsErrorCodes.ERROR_INIT_ENDECODE_CIPHER
        );
    }
}
 
Example #16
Source File: TestOAEPParameterSpec.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] argv) throws Exception {
    boolean status = true;
    byte[] p = { (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04 };
    status &= runTest("SHA-224", MGF1ParameterSpec.SHA224, p);
    status &= runTest("SHA-256", MGF1ParameterSpec.SHA256, p);
    status &= runTest("SHA-384", MGF1ParameterSpec.SHA384, p);
    status &= runTest("SHA-512", MGF1ParameterSpec.SHA512, p);
    status &= runTest("SHA", MGF1ParameterSpec.SHA1, new byte[0]);
    status &= runTest("SHA-1", MGF1ParameterSpec.SHA1, new byte[0]);
    status &= runTest("SHA1", MGF1ParameterSpec.SHA1, new byte[0]);
    if (status) {
        System.out.println("Test Passed");
    } else {
        throw new Exception("One or More Test Failed");
    }
}
 
Example #17
Source File: TestOAEPParameterSpec.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] argv) throws Exception {
    boolean status = true;
    byte[] p = { (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04 };
    status &= runTest("SHA-224", MGF1ParameterSpec.SHA224, p);
    status &= runTest("SHA-256", MGF1ParameterSpec.SHA256, p);
    status &= runTest("SHA-384", MGF1ParameterSpec.SHA384, p);
    status &= runTest("SHA-512", MGF1ParameterSpec.SHA512, p);
    status &= runTest("SHA", MGF1ParameterSpec.SHA1, new byte[0]);
    status &= runTest("SHA-1", MGF1ParameterSpec.SHA1, new byte[0]);
    status &= runTest("SHA1", MGF1ParameterSpec.SHA1, new byte[0]);
    if (status) {
        System.out.println("Test Passed");
    } else {
        throw new Exception("One or More Test Failed");
    }
}
 
Example #18
Source File: TestOAEPParameterSpec.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static boolean compareMGF(OAEPParameterSpec s1,
    OAEPParameterSpec s2) {
    String alg1 = s1.getMGFAlgorithm();
    String alg2 = s2.getMGFAlgorithm();
    if (alg1.equals(alg2)) {
        MGF1ParameterSpec mp1 = (MGF1ParameterSpec)s1.getMGFParameters();
        MGF1ParameterSpec mp2 = (MGF1ParameterSpec)s2.getMGFParameters();
        alg1 = mp1.getDigestAlgorithm();
        alg2 = mp2.getDigestAlgorithm();
        if (alg1.equals(alg2)) {
            return true;
        } else {
            System.out.println("MGF's MD algos: " + alg1 + " vs " + alg2);
            return false;
        }
    } else {
        System.out.println("MGF algos: " + alg1 + " vs " + alg2);
        return false;
    }
}
 
Example #19
Source File: TestOAEPParameterSpec.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
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);
}
 
Example #20
Source File: CSignature.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected boolean engineVerify(byte[] sigBytes) throws SignatureException {
    ensureInit();
    if (fallbackSignature != null) {
        needsReset = false;
        return fallbackSignature.verify(sigBytes);
    } else {
        byte[] hash = getDigestValue();
        return verifyCngSignedHash(
                2, hash, hash.length,
                sigBytes, sigBytes.length,
                pssParams.getSaltLength(),
                ((MGF1ParameterSpec)
                        pssParams.getMGFParameters()).getDigestAlgorithm(),
                publicKey.getHCryptProvider(),
                publicKey.getHCryptKey()
        );
    }
}
 
Example #21
Source File: AlgorithmParametersSpi.java    From RipplePower with Apache License 2.0 6 votes vote down vote up
/**
 * Return the PKCS#1 ASN.1 structure RSASSA-PSS-params.
 */
protected byte[] engineGetEncoded() 
    throws IOException
{
    PSSParameterSpec pssSpec = currentSpec;
    AlgorithmIdentifier hashAlgorithm = new AlgorithmIdentifier(
                                        DigestFactory.getOID(pssSpec.getDigestAlgorithm()),
                                        DERNull.INSTANCE);
    MGF1ParameterSpec mgfSpec = (MGF1ParameterSpec)pssSpec.getMGFParameters();
    AlgorithmIdentifier maskGenAlgorithm = new AlgorithmIdentifier(
                                        PKCSObjectIdentifiers.id_mgf1,
                                        new AlgorithmIdentifier(DigestFactory.getOID(mgfSpec.getDigestAlgorithm()), DERNull.INSTANCE));
    RSASSAPSSparams pssP = new RSASSAPSSparams(hashAlgorithm, maskGenAlgorithm, new ASN1Integer(pssSpec.getSaltLength()), new ASN1Integer(pssSpec.getTrailerField()));
    
    return pssP.getEncoded("DER");
}
 
Example #22
Source File: TestOAEPParameterSpec.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
private static boolean compareMGF(OAEPParameterSpec s1,
    OAEPParameterSpec s2) {
    String alg1 = s1.getMGFAlgorithm();
    String alg2 = s2.getMGFAlgorithm();
    if (alg1.equals(alg2)) {
        MGF1ParameterSpec mp1 = (MGF1ParameterSpec)s1.getMGFParameters();
        MGF1ParameterSpec mp2 = (MGF1ParameterSpec)s2.getMGFParameters();
        alg1 = mp1.getDigestAlgorithm();
        alg2 = mp2.getDigestAlgorithm();
        if (alg1.equals(alg2)) {
            return true;
        } else {
            System.out.println("MGF's MD algos: " + alg1 + " vs " + alg2);
            return false;
        }
    } else {
        System.out.println("MGF algos: " + alg1 + " vs " + alg2);
        return false;
    }
}
 
Example #23
Source File: TestOAEPParameterSpec.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
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);
}
 
Example #24
Source File: TestOAEPParameterSpec.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private static boolean compareMGF(OAEPParameterSpec s1,
    OAEPParameterSpec s2) {
    String alg1 = s1.getMGFAlgorithm();
    String alg2 = s2.getMGFAlgorithm();
    if (alg1.equals(alg2)) {
        MGF1ParameterSpec mp1 = (MGF1ParameterSpec)s1.getMGFParameters();
        MGF1ParameterSpec mp2 = (MGF1ParameterSpec)s2.getMGFParameters();
        alg1 = mp1.getDigestAlgorithm();
        alg2 = mp2.getDigestAlgorithm();
        if (alg1.equals(alg2)) {
            return true;
        } else {
            System.out.println("MGF's MD algos: " + alg1 + " vs " + alg2);
            return false;
        }
    } else {
        System.out.println("MGF algos: " + alg1 + " vs " + alg2);
        return false;
    }
}
 
Example #25
Source File: TestOAEPParameterSpec.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] argv) throws Exception {
    boolean status = true;
    byte[] p = { (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04 };
    status &= runTest("SHA-224", MGF1ParameterSpec.SHA224, p);
    status &= runTest("SHA-256", MGF1ParameterSpec.SHA256, p);
    status &= runTest("SHA-384", MGF1ParameterSpec.SHA384, p);
    status &= runTest("SHA-512", MGF1ParameterSpec.SHA512, p);
    status &= runTest("SHA-512/224", MGF1ParameterSpec.SHA512_224, p);
    status &= runTest("SHA-512/256", MGF1ParameterSpec.SHA512_256, p);
    status &= runTest("SHA", MGF1ParameterSpec.SHA1, new byte[0]);
    status &= runTest("SHA-1", MGF1ParameterSpec.SHA1, new byte[0]);
    status &= runTest("SHA1", MGF1ParameterSpec.SHA1, new byte[0]);
    if (status) {
        System.out.println("Test Passed");
    } else {
        throw new Exception("One or More Test Failed");
    }
}
 
Example #26
Source File: BaseEncryptionManager.java    From samples-android with Apache License 2.0 6 votes vote down vote up
private void initEncodeCipher(String keyAlias, int mode) throws GeneralSecurityException {
    PublicKey key = mKeyStore.getCertificate(keyAlias).getPublicKey();

    // workaround for using public key
    // from https://developer.android.com/reference/android/security/keystore/KeyGenParameterSpec.html#known-issues
    PublicKey unrestricted = KeyFactory.getInstance(key.getAlgorithm())
            .generatePublic(new X509EncodedKeySpec(key.getEncoded()));

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        // from https://code.google.com/p/android/issues/detail?id=197719
        OAEPParameterSpec spec = new OAEPParameterSpec("SHA-256", "MGF1",
                MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT);

        mCipher.init(mode, unrestricted, spec);
    } else {
        mCipher.init(mode, unrestricted);
    }
}
 
Example #27
Source File: TestOAEPParameterSpec.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] argv) throws Exception {
    boolean status = true;
    byte[] p = { (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04 };
    status &= runTest("SHA-224", MGF1ParameterSpec.SHA224, p);
    status &= runTest("SHA-256", MGF1ParameterSpec.SHA256, p);
    status &= runTest("SHA-384", MGF1ParameterSpec.SHA384, p);
    status &= runTest("SHA-512", MGF1ParameterSpec.SHA512, p);
    status &= runTest("SHA", MGF1ParameterSpec.SHA1, new byte[0]);
    status &= runTest("SHA-1", MGF1ParameterSpec.SHA1, new byte[0]);
    status &= runTest("SHA1", MGF1ParameterSpec.SHA1, new byte[0]);
    if (status) {
        System.out.println("Test Passed");
    } else {
        throw new Exception("One or More Test Failed");
    }
}
 
Example #28
Source File: TestOAEPParameterSpec.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private static boolean compareMGF(OAEPParameterSpec s1,
    OAEPParameterSpec s2) {
    String alg1 = s1.getMGFAlgorithm();
    String alg2 = s2.getMGFAlgorithm();
    if (alg1.equals(alg2)) {
        MGF1ParameterSpec mp1 = (MGF1ParameterSpec)s1.getMGFParameters();
        MGF1ParameterSpec mp2 = (MGF1ParameterSpec)s2.getMGFParameters();
        alg1 = mp1.getDigestAlgorithm();
        alg2 = mp2.getDigestAlgorithm();
        if (alg1.equals(alg2)) {
            return true;
        } else {
            System.out.println("MGF's MD algos: " + alg1 + " vs " + alg2);
            return false;
        }
    } else {
        System.out.println("MGF algos: " + alg1 + " vs " + alg2);
        return false;
    }
}
 
Example #29
Source File: TestOAEPParameterSpec.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
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);
}
 
Example #30
Source File: TestOAEPParameterSpec.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
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);
}