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

The following examples show how to use java.security.spec.MGF1ParameterSpec#SHA224 . 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 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 2
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 3
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 4
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;
    }
}