Java Code Examples for java.security.spec.MGF1ParameterSpec#SHA1

The following examples show how to use java.security.spec.MGF1ParameterSpec#SHA1 . 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: 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 2
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 3
Source File: AndroidOaepKeystoreSecretKeyWrapper.java    From Android-Vault with Apache License 2.0 5 votes vote down vote up
@Override
public AlgorithmParameterSpec buildCipherAlgorithmParameterSpec() {
    return new OAEPParameterSpec(
            MESSAGE_DIGEST_ALGORITHM_NAME,
            MASK_GENERATION_FUNCTION_ALGORITHM_NAME,
            MGF1ParameterSpec.SHA1,
            PSource.PSpecified.DEFAULT);
}
 
Example 4
Source File: OAEPParameterSpecTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * getDigestAlgorithm() method testing.
 */
public void testGetDigestAlgorithm() {
    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.getDigestAlgorithm().equals(mdName));
}
 
Example 5
Source File: OAEPParameterSpecTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * 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));
}
 
Example 6
Source File: OAEPParameterSpecTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * getMGFParameters() method testing.
 */
public void testGetMGFParameters() {
    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.getMGFParameters() == mgfSpec);
}
 
Example 7
Source File: OAEPParameterSpecTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * getPSource() method testing.
 */
public void testGetPSource() {
    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.getPSource() == pSrc);
}
 
Example 8
Source File: PSSParameterSpecTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Test for <code>getTrailerField()</code> method<br>
 * Assertion: returns trailer field value
 */
public final void testGetTrailerField() {
    PSSParameterSpec pssps = new PSSParameterSpec("SHA-1", "MGF1",
            MGF1ParameterSpec.SHA1, 20, 1);
    assertEquals(1, pssps.getTrailerField());
}
 
Example 9
Source File: PSSParameterSpecTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Test #1 for <code>getMGFParameters()</code> method
 * Assertion: returns mask generation function parameters
 */
public final void testGetMGFParameters01() {
    PSSParameterSpec pssps = new PSSParameterSpec("SHA-1", "MGF1",
            MGF1ParameterSpec.SHA1, 20, 1);
    assertTrue(MGF1ParameterSpec.SHA1.equals(pssps.getMGFParameters()));
}
 
Example 10
Source File: PSSParameterSpecTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Test for <code>getMGFAlgorithm()</code> method
 * Assertion: returns mask generation function algorithm name
 */
public final void testGetMGFAlgorithm() {
    PSSParameterSpec pssps = new PSSParameterSpec("SHA-1", "MGF1",
            MGF1ParameterSpec.SHA1, 20, 1);
    assertEquals("MGF1", pssps.getMGFAlgorithm());
}
 
Example 11
Source File: PSSParameterSpecTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Test for <code>getDigestAlgorithm()</code> method
 * Assertion: returns message digest algorithm name
 */
public final void testGetDigestAlgorithm() {
    PSSParameterSpec pssps = new PSSParameterSpec("SHA-1", "MGF1",
            MGF1ParameterSpec.SHA1, 20, 1);
    assertEquals("SHA-1", pssps.getDigestAlgorithm());
}
 
Example 12
Source File: RsaJceKeyCipher.java    From aws-encryption-sdk-java with Apache License 2.0 4 votes vote down vote up
RsaJceKeyCipher(PublicKey wrappingKey, PrivateKey unwrappingKey, String transformation) {
    super(wrappingKey, unwrappingKey);

    final Matcher matcher = SUPPORTED_TRANSFORMATIONS.matcher(transformation);
    if (matcher.matches()) {
        final String hashUnknownCase = matcher.group(1);
        if (hashUnknownCase != null) {
            // OAEP mode a.k.a PKCS #1v2
            final String hash = hashUnknownCase.toUpperCase();
            transformation_ = "RSA/ECB/OAEPPadding";

            final MGF1ParameterSpec mgf1Spec;
            switch (hash) {
                case "SHA-1":
                    mgf1Spec = MGF1ParameterSpec.SHA1;
                    break;
                case "SHA-224":
                    LOGGER.warning(transformation + " is not officially supported by the JceMasterKey");
                    mgf1Spec = MGF1ParameterSpec.SHA224;
                    break;
                case "SHA-256":
                    mgf1Spec = MGF1ParameterSpec.SHA256;
                    break;
                case "SHA-384":
                    mgf1Spec = MGF1ParameterSpec.SHA384;
                    break;
                case "SHA-512":
                    mgf1Spec = MGF1ParameterSpec.SHA512;
                    break;
                default:
                    throw new IllegalArgumentException("Unsupported algorithm: " + transformation);
            }
            parameterSpec_ = new OAEPParameterSpec(hash, "MGF1", mgf1Spec, PSource.PSpecified.DEFAULT);
        } else {
            // PKCS #1 v1.x
            transformation_ = transformation;
            parameterSpec_ = null;
        }
    } else {
        LOGGER.warning(transformation + " is not officially supported by the JceMasterKey");
        // Unsupported transformation, just use exactly what we are given
        transformation_ = transformation;
        parameterSpec_ = null;
    }
}
 
Example 13
Source File: PSSParameters.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected void engineInit(byte[] encoded) throws IOException {
    // first initialize with the DEFAULT values before
    // retrieving from the encoding bytes
    String mdName = DEFAULT.getDigestAlgorithm();
    MGF1ParameterSpec mgfSpec = (MGF1ParameterSpec) DEFAULT.getMGFParameters();
    int saltLength = DEFAULT.getSaltLength();
    int trailerField = DEFAULT.getTrailerField();

    DerInputStream der = new DerInputStream(encoded);
    DerValue[] datum = der.getSequence(4);

    for (DerValue d : datum) {
        if (d.isContextSpecific((byte) 0x00)) {
            // hash algid
            mdName = AlgorithmId.parse
                (d.data.getDerValue()).getName();
        } else if (d.isContextSpecific((byte) 0x01)) {
            // mgf algid
            AlgorithmId val = AlgorithmId.parse(d.data.getDerValue());
            if (!val.getOID().equals(AlgorithmId.mgf1_oid)) {
                throw new IOException("Only MGF1 mgf is supported");
            }
            AlgorithmId params = AlgorithmId.parse(
                new DerValue(val.getEncodedParams()));
            String mgfDigestName = params.getName();
            switch (mgfDigestName) {
            case "SHA-1":
                mgfSpec = MGF1ParameterSpec.SHA1;
                break;
            case "SHA-224":
                mgfSpec = MGF1ParameterSpec.SHA224;
                break;
            case "SHA-256":
                mgfSpec = MGF1ParameterSpec.SHA256;
                break;
            case "SHA-384":
                mgfSpec = MGF1ParameterSpec.SHA384;
                break;
            case "SHA-512":
                mgfSpec = MGF1ParameterSpec.SHA512;
                break;
            case "SHA-512/224":
                mgfSpec = MGF1ParameterSpec.SHA512_224;
                break;
            case "SHA-512/256":
                mgfSpec = MGF1ParameterSpec.SHA512_256;
                break;
            default:
                throw new IOException
                    ("Unrecognized message digest algorithm " +
                    mgfDigestName);
            }
        } else if (d.isContextSpecific((byte) 0x02)) {
            // salt length
            saltLength = d.data.getDerValue().getInteger();
            if (saltLength < 0) {
                throw new IOException("Negative value for saltLength");
            }
        } else if (d.isContextSpecific((byte) 0x03)) {
            // trailer field
            trailerField = d.data.getDerValue().getInteger();
            if (trailerField != 1) {
                throw new IOException("Unsupported trailerField value " +
                trailerField);
            }
        } else {
            throw new IOException("Invalid encoded PSSParameters");
        }
    }

    this.spec = new PSSParameterSpec(mdName, "MGF1", mgfSpec,
            saltLength, trailerField);
}
 
Example 14
Source File: PSSParameters.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
@Override
protected void engineInit(byte[] encoded) throws IOException {
    // first initialize with the DEFAULT values before
    // retrieving from the encoding bytes
    String mdName = DEFAULT.getDigestAlgorithm();
    MGF1ParameterSpec mgfSpec = (MGF1ParameterSpec) DEFAULT.getMGFParameters();
    int saltLength = DEFAULT.getSaltLength();
    int trailerField = DEFAULT.getTrailerField();

    DerInputStream der = new DerInputStream(encoded);
    DerValue[] datum = der.getSequence(4);

    for (DerValue d : datum) {
        if (d.isContextSpecific((byte) 0x00)) {
            // hash algid
            mdName = AlgorithmId.parse
                (d.data.getDerValue()).getName();
        } else if (d.isContextSpecific((byte) 0x01)) {
            // mgf algid
            AlgorithmId val = AlgorithmId.parse(d.data.getDerValue());
            if (!val.getOID().equals(AlgorithmId.mgf1_oid)) {
                throw new IOException("Only MGF1 mgf is supported");
            }
            AlgorithmId params = AlgorithmId.parse(
                new DerValue(val.getEncodedParams()));
            String mgfDigestName = params.getName();
            switch (mgfDigestName) {
            case "SHA-1":
                mgfSpec = MGF1ParameterSpec.SHA1;
                break;
            case "SHA-224":
                mgfSpec = MGF1ParameterSpec.SHA224;
                break;
            case "SHA-256":
                mgfSpec = MGF1ParameterSpec.SHA256;
                break;
            case "SHA-384":
                mgfSpec = MGF1ParameterSpec.SHA384;
                break;
            case "SHA-512":
                mgfSpec = MGF1ParameterSpec.SHA512;
                break;
            case "SHA-512/224":
                mgfSpec = MGF1ParameterSpec.SHA512_224;
                break;
            case "SHA-512/256":
                mgfSpec = MGF1ParameterSpec.SHA512_256;
                break;
            default:
                throw new IOException
                    ("Unrecognized message digest algorithm " +
                    mgfDigestName);
            }
        } else if (d.isContextSpecific((byte) 0x02)) {
            // salt length
            saltLength = d.data.getDerValue().getInteger();
            if (saltLength < 0) {
                throw new IOException("Negative value for saltLength");
            }
        } else if (d.isContextSpecific((byte) 0x03)) {
            // trailer field
            trailerField = d.data.getDerValue().getInteger();
            if (trailerField != 1) {
                throw new IOException("Unsupported trailerField value " +
                trailerField);
            }
        } else {
            throw new IOException("Invalid encoded PSSParameters");
        }
    }

    this.spec = new PSSParameterSpec(mdName, "MGF1", mgfSpec,
            saltLength, trailerField);
}
 
Example 15
Source File: PSSParameters.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected void engineInit(byte[] encoded) throws IOException {
    // first initialize with the DEFAULT values before
    // retrieving from the encoding bytes
    String mdName = DEFAULT.getDigestAlgorithm();
    MGF1ParameterSpec mgfSpec = (MGF1ParameterSpec) DEFAULT.getMGFParameters();
    int saltLength = DEFAULT.getSaltLength();
    int trailerField = DEFAULT.getTrailerField();

    DerInputStream der = new DerInputStream(encoded);
    DerValue[] datum = der.getSequence(4);

    for (DerValue d : datum) {
        if (d.isContextSpecific((byte) 0x00)) {
            // hash algid
            mdName = AlgorithmId.parse
                (d.data.getDerValue()).getName();
        } else if (d.isContextSpecific((byte) 0x01)) {
            // mgf algid
            AlgorithmId val = AlgorithmId.parse(d.data.getDerValue());
            if (!val.getOID().equals(AlgorithmId.mgf1_oid)) {
                throw new IOException("Only MGF1 mgf is supported");
            }
            AlgorithmId params = AlgorithmId.parse(
                new DerValue(val.getEncodedParams()));
            String mgfDigestName = params.getName();
            switch (mgfDigestName) {
            case "SHA-1":
                mgfSpec = MGF1ParameterSpec.SHA1;
                break;
            case "SHA-224":
                mgfSpec = MGF1ParameterSpec.SHA224;
                break;
            case "SHA-256":
                mgfSpec = MGF1ParameterSpec.SHA256;
                break;
            case "SHA-384":
                mgfSpec = MGF1ParameterSpec.SHA384;
                break;
            case "SHA-512":
                mgfSpec = MGF1ParameterSpec.SHA512;
                break;
            case "SHA-512/224":
                mgfSpec = MGF1ParameterSpec.SHA512_224;
                break;
            case "SHA-512/256":
                mgfSpec = MGF1ParameterSpec.SHA512_256;
                break;
            default:
                throw new IOException
                    ("Unrecognized message digest algorithm " +
                    mgfDigestName);
            }
        } else if (d.isContextSpecific((byte) 0x02)) {
            // salt length
            saltLength = d.data.getDerValue().getInteger();
            if (saltLength < 0) {
                throw new IOException("Negative value for saltLength");
            }
        } else if (d.isContextSpecific((byte) 0x03)) {
            // trailer field
            trailerField = d.data.getDerValue().getInteger();
            if (trailerField != 1) {
                throw new IOException("Unsupported trailerField value " +
                trailerField);
            }
        } else {
            throw new IOException("Invalid encoded PSSParameters");
        }
    }

    this.spec = new PSSParameterSpec(mdName, "MGF1", mgfSpec,
            saltLength, trailerField);
}
 
Example 16
Source File: PSSParameterSpecTest.java    From j2objc with Apache License 2.0 3 votes vote down vote up
/**
 * Test #2 for
 * <code>
 * PSSParameterSpec(String,String,AlgorithmParameterSpec,int,int)
 * </code> ctor<br>
 * Assertion:
 * throws <code>NullPointerException</code>
 * if <code>mdName</code> is null
 */
public final void testPSSParameterSpec0202() {
    try {
        new PSSParameterSpec(null, "MGF1", MGF1ParameterSpec.SHA1, 20, 1);
        fail("Expected NPE not thrown");
    } catch (NullPointerException e) {
    }
}
 
Example 17
Source File: PSSParameterSpecTest.java    From j2objc with Apache License 2.0 3 votes vote down vote up
/**
 * Test #3 for
 * <code>
 * PSSParameterSpec(String,String,AlgorithmParameterSpec,int,int)
 * </code> ctor<br>
 * Assertion:
 * throws <code>NullPointerException</code>
 * if <code>mgfName</code> is null
 */
public final void testPSSParameterSpec0203() {
    try {
        new PSSParameterSpec("SHA-1", null, MGF1ParameterSpec.SHA1, 20, 1);
        fail("Expected NPE not thrown");
    } catch (NullPointerException e) {
    }
}
 
Example 18
Source File: PSSParameterSpecTest.java    From j2objc with Apache License 2.0 3 votes vote down vote up
/**
 * Test #4 for
 * <code>
 * PSSParameterSpec(String,String,AlgorithmParameterSpec,int,int)
 * </code> ctor<br>
 * Assertion:
 * throws <code>IllegalArgumentException<code>
 * if <code>saltLen<code> less than 0
 */
public final void testPSSParameterSpec0204() {
    try {
        new PSSParameterSpec("SHA-1", "MGF1",
                MGF1ParameterSpec.SHA1, -20, 1);
        fail("Expected IAE not thrown");
    } catch (IllegalArgumentException e) {
    }
}
 
Example 19
Source File: PSSParameterSpecTest.java    From j2objc with Apache License 2.0 3 votes vote down vote up
/**
 * Test #5 for
 * <code>
 * PSSParameterSpec(String,String,AlgorithmParameterSpec,int,int)
 * </code> ctor<br>
 * Assertion:
 * throws <code>IllegalArgumentException</code>
 * if <code>trailerField</code> less than 0
 */
public final void testPSSParameterSpec0205() {
    try {
        new PSSParameterSpec("SHA-1", "MGF1",
                MGF1ParameterSpec.SHA1, 20, -1);
        fail("Expected IAE not thrown");
    } catch (IllegalArgumentException e) {
    }
}
 
Example 20
Source File: PSSParameterSpecTest.java    From j2objc with Apache License 2.0 2 votes vote down vote up
/**
 * Test #1 for
 * <code>
 * PSSParameterSpec(String,String,AlgorithmParameterSpec,int,int)
 * </code> ctor<br>
 * Assertion: constructs using valid parameters
 * <code>PSSParameterSpec<code> object
 */
public final void testPSSParameterSpec0201() {
    AlgorithmParameterSpec aps = new PSSParameterSpec("SHA-1", "MGF1",
            MGF1ParameterSpec.SHA1, 20, 1);
    assertTrue(aps instanceof PSSParameterSpec);
}