Java Code Examples for javax.crypto.spec.PSource#getAlgorithm()

The following examples show how to use javax.crypto.spec.PSource#getAlgorithm() . 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: TestOAEPParameterSpec.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private static boolean comparePSource(OAEPParameterSpec s1,
    OAEPParameterSpec s2) {
    PSource src1 = s1.getPSource();
    PSource src2 = s2.getPSource();
    String alg1 = src1.getAlgorithm();
    String alg2 = src2.getAlgorithm();
    if (alg1.equals(alg2)) {
        // assumes they are PSource.PSpecified
        return Arrays.equals(((PSource.PSpecified) src1).getValue(),
            ((PSource.PSpecified) src2).getValue());
    } else {
        System.out.println("PSource algos: " + alg1 + " vs " + alg2);
        return false;
    }
}
 
Example 2
Source File: TestOAEPParameterSpec.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static boolean comparePSource(OAEPParameterSpec s1,
    OAEPParameterSpec s2) {
    PSource src1 = s1.getPSource();
    PSource src2 = s2.getPSource();
    String alg1 = src1.getAlgorithm();
    String alg2 = src2.getAlgorithm();
    if (alg1.equals(alg2)) {
        // assumes they are PSource.PSpecified
        return Arrays.equals(((PSource.PSpecified) src1).getValue(),
            ((PSource.PSpecified) src2).getValue());
    } else {
        System.out.println("PSource algos: " + alg1 + " vs " + alg2);
        return false;
    }
}
 
Example 3
Source File: OAEPParameters.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
protected void engineInit(AlgorithmParameterSpec paramSpec)
    throws InvalidParameterSpecException {
    if (!(paramSpec instanceof OAEPParameterSpec)) {
        throw new InvalidParameterSpecException
            ("Inappropriate parameter specification");
    }
    OAEPParameterSpec spec = (OAEPParameterSpec) paramSpec;
    mdName = spec.getDigestAlgorithm();
    String mgfName = spec.getMGFAlgorithm();
    if (!mgfName.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.mgfSpec = (MGF1ParameterSpec) mgfSpec;
    PSource pSrc = spec.getPSource();
    if (pSrc.getAlgorithm().equals("PSpecified")) {
        p = ((PSource.PSpecified) pSrc).getValue();
    } else {
        throw new InvalidParameterSpecException("Unsupported pSource " +
            pSrc.getAlgorithm() + "; PSpecified only");
    }
}
 
Example 4
Source File: OAEPParameters.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
protected void engineInit(AlgorithmParameterSpec paramSpec)
    throws InvalidParameterSpecException {
    if (!(paramSpec instanceof OAEPParameterSpec)) {
        throw new InvalidParameterSpecException
            ("Inappropriate parameter specification");
    }
    OAEPParameterSpec spec = (OAEPParameterSpec) paramSpec;
    mdName = spec.getDigestAlgorithm();
    String mgfName = spec.getMGFAlgorithm();
    if (!mgfName.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.mgfSpec = (MGF1ParameterSpec) mgfSpec;
    PSource pSrc = spec.getPSource();
    if (pSrc.getAlgorithm().equals("PSpecified")) {
        p = ((PSource.PSpecified) pSrc).getValue();
    } else {
        throw new InvalidParameterSpecException("Unsupported pSource " +
            pSrc.getAlgorithm() + "; PSpecified only");
    }
}
 
Example 5
Source File: OAEPParameters.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
protected void engineInit(AlgorithmParameterSpec paramSpec)
    throws InvalidParameterSpecException {
    if (!(paramSpec instanceof OAEPParameterSpec)) {
        throw new InvalidParameterSpecException
            ("Inappropriate parameter specification");
    }
    OAEPParameterSpec spec = (OAEPParameterSpec) paramSpec;
    mdName = spec.getDigestAlgorithm();
    String mgfName = spec.getMGFAlgorithm();
    if (!mgfName.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.mgfSpec = (MGF1ParameterSpec) mgfSpec;
    PSource pSrc = spec.getPSource();
    if (pSrc.getAlgorithm().equals("PSpecified")) {
        p = ((PSource.PSpecified) pSrc).getValue();
    } else {
        throw new InvalidParameterSpecException("Unsupported pSource " +
            pSrc.getAlgorithm() + "; PSpecified only");
    }
}
 
Example 6
Source File: OAEPParameters.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
protected void engineInit(AlgorithmParameterSpec paramSpec)
    throws InvalidParameterSpecException {
    if (!(paramSpec instanceof OAEPParameterSpec)) {
        throw new InvalidParameterSpecException
            ("Inappropriate parameter specification");
    }
    OAEPParameterSpec spec = (OAEPParameterSpec) paramSpec;
    mdName = spec.getDigestAlgorithm();
    String mgfName = spec.getMGFAlgorithm();
    if (!mgfName.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.mgfSpec = (MGF1ParameterSpec) mgfSpec;
    PSource pSrc = spec.getPSource();
    if (pSrc.getAlgorithm().equals("PSpecified")) {
        p = ((PSource.PSpecified) pSrc).getValue();
    } else {
        throw new InvalidParameterSpecException("Unsupported pSource " +
            pSrc.getAlgorithm() + "; PSpecified only");
    }
}
 
Example 7
Source File: OAEPParameters.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
protected void engineInit(AlgorithmParameterSpec paramSpec)
    throws InvalidParameterSpecException {
    if (!(paramSpec instanceof OAEPParameterSpec)) {
        throw new InvalidParameterSpecException
            ("Inappropriate parameter specification");
    }
    OAEPParameterSpec spec = (OAEPParameterSpec) paramSpec;
    mdName = spec.getDigestAlgorithm();
    String mgfName = spec.getMGFAlgorithm();
    if (!mgfName.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.mgfSpec = (MGF1ParameterSpec) mgfSpec;
    PSource pSrc = spec.getPSource();
    if (pSrc.getAlgorithm().equals("PSpecified")) {
        p = ((PSource.PSpecified) pSrc).getValue();
    } else {
        throw new InvalidParameterSpecException("Unsupported pSource " +
            pSrc.getAlgorithm() + "; PSpecified only");
    }
}
 
Example 8
Source File: OAEPParameters.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
protected void engineInit(AlgorithmParameterSpec paramSpec)
    throws InvalidParameterSpecException {
    if (!(paramSpec instanceof OAEPParameterSpec)) {
        throw new InvalidParameterSpecException
            ("Inappropriate parameter specification");
    }
    OAEPParameterSpec spec = (OAEPParameterSpec) paramSpec;
    mdName = spec.getDigestAlgorithm();
    String mgfName = spec.getMGFAlgorithm();
    if (!mgfName.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.mgfSpec = (MGF1ParameterSpec) mgfSpec;
    PSource pSrc = spec.getPSource();
    if (pSrc.getAlgorithm().equals("PSpecified")) {
        p = ((PSource.PSpecified) pSrc).getValue();
    } else {
        throw new InvalidParameterSpecException("Unsupported pSource " +
            pSrc.getAlgorithm() + "; PSpecified only");
    }
}
 
Example 9
Source File: TestOAEPParameterSpec.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static boolean comparePSource(OAEPParameterSpec s1,
    OAEPParameterSpec s2) {
    PSource src1 = s1.getPSource();
    PSource src2 = s2.getPSource();
    String alg1 = src1.getAlgorithm();
    String alg2 = src2.getAlgorithm();
    if (alg1.equals(alg2)) {
        // assumes they are PSource.PSpecified
        return Arrays.equals(((PSource.PSpecified) src1).getValue(),
            ((PSource.PSpecified) src2).getValue());
    } else {
        System.out.println("PSource algos: " + alg1 + " vs " + alg2);
        return false;
    }
}
 
Example 10
Source File: OAEPParameters.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
protected void engineInit(AlgorithmParameterSpec paramSpec)
    throws InvalidParameterSpecException {
    if (!(paramSpec instanceof OAEPParameterSpec)) {
        throw new InvalidParameterSpecException
            ("Inappropriate parameter specification");
    }
    OAEPParameterSpec spec = (OAEPParameterSpec) paramSpec;
    mdName = spec.getDigestAlgorithm();
    String mgfName = spec.getMGFAlgorithm();
    if (!mgfName.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.mgfSpec = (MGF1ParameterSpec) mgfSpec;
    PSource pSrc = spec.getPSource();
    if (pSrc.getAlgorithm().equals("PSpecified")) {
        p = ((PSource.PSpecified) pSrc).getValue();
    } else {
        throw new InvalidParameterSpecException("Unsupported pSource " +
            pSrc.getAlgorithm() + "; PSpecified only");
    }
}
 
Example 11
Source File: OAEPParameters.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
protected void engineInit(AlgorithmParameterSpec paramSpec)
    throws InvalidParameterSpecException {
    if (!(paramSpec instanceof OAEPParameterSpec)) {
        throw new InvalidParameterSpecException
            ("Inappropriate parameter specification");
    }
    OAEPParameterSpec spec = (OAEPParameterSpec) paramSpec;
    mdName = spec.getDigestAlgorithm();
    String mgfName = spec.getMGFAlgorithm();
    if (!mgfName.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.mgfSpec = (MGF1ParameterSpec) mgfSpec;
    PSource pSrc = spec.getPSource();
    if (pSrc.getAlgorithm().equals("PSpecified")) {
        p = ((PSource.PSpecified) pSrc).getValue();
    } else {
        throw new InvalidParameterSpecException("Unsupported pSource " +
            pSrc.getAlgorithm() + "; PSpecified only");
    }
}
 
Example 12
Source File: OAEPParameters.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
protected void engineInit(AlgorithmParameterSpec paramSpec)
    throws InvalidParameterSpecException {
    if (!(paramSpec instanceof OAEPParameterSpec)) {
        throw new InvalidParameterSpecException
            ("Inappropriate parameter specification");
    }
    OAEPParameterSpec spec = (OAEPParameterSpec) paramSpec;
    mdName = spec.getDigestAlgorithm();
    String mgfName = spec.getMGFAlgorithm();
    if (!mgfName.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.mgfSpec = (MGF1ParameterSpec) mgfSpec;
    PSource pSrc = spec.getPSource();
    if (pSrc.getAlgorithm().equals("PSpecified")) {
        p = ((PSource.PSpecified) pSrc).getValue();
    } else {
        throw new InvalidParameterSpecException("Unsupported pSource " +
            pSrc.getAlgorithm() + "; PSpecified only");
    }
}
 
Example 13
Source File: RSAPadding.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
private RSAPadding(int type, int paddedSize, SecureRandom random,
        OAEPParameterSpec spec) throws InvalidKeyException,
        InvalidAlgorithmParameterException {
    this.type = type;
    this.paddedSize = paddedSize;
    this.random = random;
    if (paddedSize < 64) {
        // sanity check, already verified in RSASignature/RSACipher
        throw new InvalidKeyException("Padded size must be at least 64");
    }
    switch (type) {
    case PAD_BLOCKTYPE_1:
    case PAD_BLOCKTYPE_2:
        maxDataSize = paddedSize - 11;
        break;
    case PAD_NONE:
        maxDataSize = paddedSize;
        break;
    case PAD_OAEP_MGF1:
        String mdName = "SHA-1";
        String mgfMdName = "SHA-1";
        byte[] digestInput = null;
        try {
            if (spec != null) {
                mdName = spec.getDigestAlgorithm();
                String mgfName = spec.getMGFAlgorithm();
                if (!mgfName.equalsIgnoreCase("MGF1")) {
                    throw new InvalidAlgorithmParameterException
                        ("Unsupported MGF algo: " + mgfName);
                }
                mgfMdName = ((MGF1ParameterSpec)spec.getMGFParameters())
                        .getDigestAlgorithm();
                PSource pSrc = spec.getPSource();
                String pSrcAlgo = pSrc.getAlgorithm();
                if (!pSrcAlgo.equalsIgnoreCase("PSpecified")) {
                    throw new InvalidAlgorithmParameterException
                        ("Unsupported pSource algo: " + pSrcAlgo);
                }
                digestInput = ((PSource.PSpecified) pSrc).getValue();
            }
            md = MessageDigest.getInstance(mdName);
            mgfMd = MessageDigest.getInstance(mgfMdName);
        } catch (NoSuchAlgorithmException e) {
            throw new InvalidKeyException
                    ("Digest " + mdName + " not available", e);
        }
        lHash = getInitialHash(md, digestInput);
        int digestLen = lHash.length;
        maxDataSize = paddedSize - 2 - 2 * digestLen;
        if (maxDataSize <= 0) {
            throw new InvalidKeyException
                    ("Key is too short for encryption using OAEPPadding" +
                     " with " + mdName + " and MGF1" + mgfMdName);
        }
        break;
    default:
        throw new InvalidKeyException("Invalid padding: " + type);
    }
}
 
Example 14
Source File: RSAPadding.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
private RSAPadding(int type, int paddedSize, SecureRandom random,
        OAEPParameterSpec spec) throws InvalidKeyException,
        InvalidAlgorithmParameterException {
    this.type = type;
    this.paddedSize = paddedSize;
    this.random = random;
    if (paddedSize < 64) {
        // sanity check, already verified in RSASignature/RSACipher
        throw new InvalidKeyException("Padded size must be at least 64");
    }
    switch (type) {
    case PAD_BLOCKTYPE_1:
    case PAD_BLOCKTYPE_2:
        maxDataSize = paddedSize - 11;
        break;
    case PAD_NONE:
        maxDataSize = paddedSize;
        break;
    case PAD_OAEP_MGF1:
        String mdName = "SHA-1";
        String mgfMdName = mdName;
        byte[] digestInput = null;
        try {
            if (spec != null) {
                mdName = spec.getDigestAlgorithm();
                String mgfName = spec.getMGFAlgorithm();
                if (!mgfName.equalsIgnoreCase("MGF1")) {
                    throw new InvalidAlgorithmParameterException
                        ("Unsupported MGF algo: " + mgfName);
                }
                mgfMdName = ((MGF1ParameterSpec)spec.getMGFParameters())
                        .getDigestAlgorithm();
                PSource pSrc = spec.getPSource();
                String pSrcAlgo = pSrc.getAlgorithm();
                if (!pSrcAlgo.equalsIgnoreCase("PSpecified")) {
                    throw new InvalidAlgorithmParameterException
                        ("Unsupported pSource algo: " + pSrcAlgo);
                }
                digestInput = ((PSource.PSpecified) pSrc).getValue();
            }
            md = MessageDigest.getInstance(mdName);
            mgf = new MGF1(mgfMdName);
        } catch (NoSuchAlgorithmException e) {
            throw new InvalidKeyException("Digest not available", e);
        }
        lHash = getInitialHash(md, digestInput);
        int digestLen = lHash.length;
        maxDataSize = paddedSize - 2 - 2 * digestLen;
        if (maxDataSize <= 0) {
            throw new InvalidKeyException
                    ("Key is too short for encryption using OAEPPadding" +
                     " with " + mdName + " and " + mgf.getName());
        }
        break;
    default:
        throw new InvalidKeyException("Invalid padding: " + type);
    }
}
 
Example 15
Source File: RSAPadding.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
private RSAPadding(int type, int paddedSize, SecureRandom random,
        OAEPParameterSpec spec) throws InvalidKeyException,
        InvalidAlgorithmParameterException {
    this.type = type;
    this.paddedSize = paddedSize;
    this.random = random;
    if (paddedSize < 64) {
        // sanity check, already verified in RSASignature/RSACipher
        throw new InvalidKeyException("Padded size must be at least 64");
    }
    switch (type) {
    case PAD_BLOCKTYPE_1:
    case PAD_BLOCKTYPE_2:
        maxDataSize = paddedSize - 11;
        break;
    case PAD_NONE:
        maxDataSize = paddedSize;
        break;
    case PAD_OAEP_MGF1:
        String mdName = "SHA-1";
        String mgfMdName = "SHA-1";
        byte[] digestInput = null;
        try {
            if (spec != null) {
                mdName = spec.getDigestAlgorithm();
                String mgfName = spec.getMGFAlgorithm();
                if (!mgfName.equalsIgnoreCase("MGF1")) {
                    throw new InvalidAlgorithmParameterException
                        ("Unsupported MGF algo: " + mgfName);
                }
                mgfMdName = ((MGF1ParameterSpec)spec.getMGFParameters()).getDigestAlgorithm();
                PSource pSrc = spec.getPSource();
                String pSrcAlgo = pSrc.getAlgorithm();
                if (!pSrcAlgo.equalsIgnoreCase("PSpecified")) {
                    throw new InvalidAlgorithmParameterException
                        ("Unsupported pSource algo: " + pSrcAlgo);
                }
                digestInput = ((PSource.PSpecified) pSrc).getValue();
            }
            md = MessageDigest.getInstance(mdName);
            mgfMd = MessageDigest.getInstance(mgfMdName);
        } catch (NoSuchAlgorithmException e) {
            throw new InvalidKeyException
                    ("Digest " + mdName + " not available", e);
        }
        lHash = getInitialHash(md, digestInput);
        int digestLen = lHash.length;
        maxDataSize = paddedSize - 2 - 2 * digestLen;
        if (maxDataSize <= 0) {
            throw new InvalidKeyException
                    ("Key is too short for encryption using OAEPPadding" +
                     " with " + mdName + " and MGF1" + mgfMdName);
        }
        break;
    default:
        throw new InvalidKeyException("Invalid padding: " + type);
    }
}
 
Example 16
Source File: RSAPadding.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
private RSAPadding(int type, int paddedSize, SecureRandom random,
        OAEPParameterSpec spec) throws InvalidKeyException,
        InvalidAlgorithmParameterException {
    this.type = type;
    this.paddedSize = paddedSize;
    this.random = random;
    if (paddedSize < 64) {
        // sanity check, already verified in RSASignature/RSACipher
        throw new InvalidKeyException("Padded size must be at least 64");
    }
    switch (type) {
    case PAD_BLOCKTYPE_1:
    case PAD_BLOCKTYPE_2:
        maxDataSize = paddedSize - 11;
        break;
    case PAD_NONE:
        maxDataSize = paddedSize;
        break;
    case PAD_OAEP_MGF1:
        String mdName = "SHA-1";
        String mgfMdName = "SHA-1";
        byte[] digestInput = null;
        try {
            if (spec != null) {
                mdName = spec.getDigestAlgorithm();
                String mgfName = spec.getMGFAlgorithm();
                if (!mgfName.equalsIgnoreCase("MGF1")) {
                    throw new InvalidAlgorithmParameterException
                        ("Unsupported MGF algo: " + mgfName);
                }
                mgfMdName = ((MGF1ParameterSpec)spec.getMGFParameters())
                        .getDigestAlgorithm();
                PSource pSrc = spec.getPSource();
                String pSrcAlgo = pSrc.getAlgorithm();
                if (!pSrcAlgo.equalsIgnoreCase("PSpecified")) {
                    throw new InvalidAlgorithmParameterException
                        ("Unsupported pSource algo: " + pSrcAlgo);
                }
                digestInput = ((PSource.PSpecified) pSrc).getValue();
            }
            md = MessageDigest.getInstance(mdName);
            mgfMd = MessageDigest.getInstance(mgfMdName);
        } catch (NoSuchAlgorithmException e) {
            throw new InvalidKeyException
                    ("Digest " + mdName + " not available", e);
        }
        lHash = getInitialHash(md, digestInput);
        int digestLen = lHash.length;
        maxDataSize = paddedSize - 2 - 2 * digestLen;
        if (maxDataSize <= 0) {
            throw new InvalidKeyException
                    ("Key is too short for encryption using OAEPPadding" +
                     " with " + mdName + " and MGF1" + mgfMdName);
        }
        break;
    default:
        throw new InvalidKeyException("Invalid padding: " + type);
    }
}
 
Example 17
Source File: RSAPadding.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
private RSAPadding(int type, int paddedSize, SecureRandom random,
        OAEPParameterSpec spec) throws InvalidKeyException,
        InvalidAlgorithmParameterException {
    this.type = type;
    this.paddedSize = paddedSize;
    this.random = random;
    if (paddedSize < 64) {
        // sanity check, already verified in RSASignature/RSACipher
        throw new InvalidKeyException("Padded size must be at least 64");
    }
    switch (type) {
    case PAD_BLOCKTYPE_1:
    case PAD_BLOCKTYPE_2:
        maxDataSize = paddedSize - 11;
        break;
    case PAD_NONE:
        maxDataSize = paddedSize;
        break;
    case PAD_OAEP_MGF1:
        String mdName = "SHA-1";
        String mgfMdName = mdName;
        byte[] digestInput = null;
        try {
            if (spec != null) {
                mdName = spec.getDigestAlgorithm();
                String mgfName = spec.getMGFAlgorithm();
                if (!mgfName.equalsIgnoreCase("MGF1")) {
                    throw new InvalidAlgorithmParameterException
                        ("Unsupported MGF algo: " + mgfName);
                }
                mgfMdName = ((MGF1ParameterSpec)spec.getMGFParameters())
                        .getDigestAlgorithm();
                PSource pSrc = spec.getPSource();
                String pSrcAlgo = pSrc.getAlgorithm();
                if (!pSrcAlgo.equalsIgnoreCase("PSpecified")) {
                    throw new InvalidAlgorithmParameterException
                        ("Unsupported pSource algo: " + pSrcAlgo);
                }
                digestInput = ((PSource.PSpecified) pSrc).getValue();
            }
            md = MessageDigest.getInstance(mdName);
            mgf = new MGF1(mgfMdName);
        } catch (NoSuchAlgorithmException e) {
            throw new InvalidKeyException("Digest not available", e);
        }
        lHash = getInitialHash(md, digestInput);
        int digestLen = lHash.length;
        maxDataSize = paddedSize - 2 - 2 * digestLen;
        if (maxDataSize <= 0) {
            throw new InvalidKeyException
                    ("Key is too short for encryption using OAEPPadding" +
                     " with " + mdName + " and " + mgf.getName());
        }
        break;
    default:
        throw new InvalidKeyException("Invalid padding: " + type);
    }
}
 
Example 18
Source File: RSAPadding.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
private RSAPadding(int type, int paddedSize, SecureRandom random,
        OAEPParameterSpec spec) throws InvalidKeyException,
        InvalidAlgorithmParameterException {
    this.type = type;
    this.paddedSize = paddedSize;
    this.random = random;
    if (paddedSize < 64) {
        // sanity check, already verified in RSASignature/RSACipher
        throw new InvalidKeyException("Padded size must be at least 64");
    }
    switch (type) {
    case PAD_BLOCKTYPE_1:
    case PAD_BLOCKTYPE_2:
        maxDataSize = paddedSize - 11;
        break;
    case PAD_NONE:
        maxDataSize = paddedSize;
        break;
    case PAD_OAEP_MGF1:
        String mdName = "SHA-1";
        String mgfMdName = mdName;
        byte[] digestInput = null;
        try {
            if (spec != null) {
                mdName = spec.getDigestAlgorithm();
                String mgfName = spec.getMGFAlgorithm();
                if (!mgfName.equalsIgnoreCase("MGF1")) {
                    throw new InvalidAlgorithmParameterException
                        ("Unsupported MGF algo: " + mgfName);
                }
                mgfMdName = ((MGF1ParameterSpec)spec.getMGFParameters())
                        .getDigestAlgorithm();
                PSource pSrc = spec.getPSource();
                String pSrcAlgo = pSrc.getAlgorithm();
                if (!pSrcAlgo.equalsIgnoreCase("PSpecified")) {
                    throw new InvalidAlgorithmParameterException
                        ("Unsupported pSource algo: " + pSrcAlgo);
                }
                digestInput = ((PSource.PSpecified) pSrc).getValue();
            }
            md = MessageDigest.getInstance(mdName);
            mgf = new MGF1(mgfMdName);
        } catch (NoSuchAlgorithmException e) {
            throw new InvalidKeyException("Digest not available", e);
        }
        lHash = getInitialHash(md, digestInput);
        int digestLen = lHash.length;
        maxDataSize = paddedSize - 2 - 2 * digestLen;
        if (maxDataSize <= 0) {
            throw new InvalidKeyException
                    ("Key is too short for encryption using OAEPPadding" +
                     " with " + mdName + " and " + mgf.getName());
        }
        break;
    default:
        throw new InvalidKeyException("Invalid padding: " + type);
    }
}
 
Example 19
Source File: RSAPadding.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
private RSAPadding(int type, int paddedSize, SecureRandom random,
        OAEPParameterSpec spec) throws InvalidKeyException,
        InvalidAlgorithmParameterException {
    this.type = type;
    this.paddedSize = paddedSize;
    this.random = random;
    if (paddedSize < 64) {
        // sanity check, already verified in RSASignature/RSACipher
        throw new InvalidKeyException("Padded size must be at least 64");
    }
    switch (type) {
    case PAD_BLOCKTYPE_1:
    case PAD_BLOCKTYPE_2:
        maxDataSize = paddedSize - 11;
        break;
    case PAD_NONE:
        maxDataSize = paddedSize;
        break;
    case PAD_OAEP_MGF1:
        String mdName = "SHA-1";
        String mgfMdName = "SHA-1";
        byte[] digestInput = null;
        try {
            if (spec != null) {
                mdName = spec.getDigestAlgorithm();
                String mgfName = spec.getMGFAlgorithm();
                if (!mgfName.equalsIgnoreCase("MGF1")) {
                    throw new InvalidAlgorithmParameterException
                        ("Unsupported MGF algo: " + mgfName);
                }
                mgfMdName = ((MGF1ParameterSpec)spec.getMGFParameters())
                        .getDigestAlgorithm();
                PSource pSrc = spec.getPSource();
                String pSrcAlgo = pSrc.getAlgorithm();
                if (!pSrcAlgo.equalsIgnoreCase("PSpecified")) {
                    throw new InvalidAlgorithmParameterException
                        ("Unsupported pSource algo: " + pSrcAlgo);
                }
                digestInput = ((PSource.PSpecified) pSrc).getValue();
            }
            md = MessageDigest.getInstance(mdName);
            mgfMd = MessageDigest.getInstance(mgfMdName);
        } catch (NoSuchAlgorithmException e) {
            throw new InvalidKeyException
                    ("Digest " + mdName + " not available", e);
        }
        lHash = getInitialHash(md, digestInput);
        int digestLen = lHash.length;
        maxDataSize = paddedSize - 2 - 2 * digestLen;
        if (maxDataSize <= 0) {
            throw new InvalidKeyException
                    ("Key is too short for encryption using OAEPPadding" +
                     " with " + mdName + " and MGF1" + mgfMdName);
        }
        break;
    default:
        throw new InvalidKeyException("Invalid padding: " + type);
    }
}
 
Example 20
Source File: RSAPadding.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
private RSAPadding(int type, int paddedSize, SecureRandom random,
        OAEPParameterSpec spec) throws InvalidKeyException,
        InvalidAlgorithmParameterException {
    this.type = type;
    this.paddedSize = paddedSize;
    this.random = random;
    if (paddedSize < 64) {
        // sanity check, already verified in RSASignature/RSACipher
        throw new InvalidKeyException("Padded size must be at least 64");
    }
    switch (type) {
    case PAD_BLOCKTYPE_1:
    case PAD_BLOCKTYPE_2:
        maxDataSize = paddedSize - 11;
        break;
    case PAD_NONE:
        maxDataSize = paddedSize;
        break;
    case PAD_OAEP_MGF1:
        String mdName = "SHA-1";
        String mgfMdName = "SHA-1";
        byte[] digestInput = null;
        try {
            if (spec != null) {
                mdName = spec.getDigestAlgorithm();
                String mgfName = spec.getMGFAlgorithm();
                if (!mgfName.equalsIgnoreCase("MGF1")) {
                    throw new InvalidAlgorithmParameterException
                        ("Unsupported MGF algo: " + mgfName);
                }
                mgfMdName = ((MGF1ParameterSpec)spec.getMGFParameters())
                        .getDigestAlgorithm();
                PSource pSrc = spec.getPSource();
                String pSrcAlgo = pSrc.getAlgorithm();
                if (!pSrcAlgo.equalsIgnoreCase("PSpecified")) {
                    throw new InvalidAlgorithmParameterException
                        ("Unsupported pSource algo: " + pSrcAlgo);
                }
                digestInput = ((PSource.PSpecified) pSrc).getValue();
            }
            md = MessageDigest.getInstance(mdName);
            mgfMd = MessageDigest.getInstance(mgfMdName);
        } catch (NoSuchAlgorithmException e) {
            throw new InvalidKeyException
                    ("Digest " + mdName + " not available", e);
        }
        lHash = getInitialHash(md, digestInput);
        int digestLen = lHash.length;
        maxDataSize = paddedSize - 2 - 2 * digestLen;
        if (maxDataSize <= 0) {
            throw new InvalidKeyException
                    ("Key is too short for encryption using OAEPPadding" +
                     " with " + mdName + " and MGF1" + mgfMdName);
        }
        break;
    default:
        throw new InvalidKeyException("Invalid padding: " + type);
    }
}