Search in sources :

Example 16 with Digest

use of com.github.zhenwei.core.crypto.Digest in project LinLong-Java by zhenwei1108.

the class BcRSAContentVerifierProviderBuilder method createSigner.

protected Signer createSigner(AlgorithmIdentifier sigAlgId) throws OperatorCreationException {
    AlgorithmIdentifier digAlg = digestAlgorithmFinder.find(sigAlgId);
    Digest dig = digestProvider.get(digAlg);
    return new RSADigestSigner(dig);
}
Also used : Digest(com.github.zhenwei.core.crypto.Digest) RSADigestSigner(com.github.zhenwei.core.crypto.signers.RSADigestSigner) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)

Example 17 with Digest

use of com.github.zhenwei.core.crypto.Digest in project LinLong-Java by zhenwei1108.

the class BcDSAContentVerifierProviderBuilder method createSigner.

protected Signer createSigner(AlgorithmIdentifier sigAlgId) throws OperatorCreationException {
    AlgorithmIdentifier digAlg = digestAlgorithmFinder.find(sigAlgId);
    Digest dig = digestProvider.get(digAlg);
    return new DSADigestSigner(new DSASigner(), dig);
}
Also used : DSASigner(com.github.zhenwei.core.crypto.signers.DSASigner) DSADigestSigner(com.github.zhenwei.core.crypto.signers.DSADigestSigner) Digest(com.github.zhenwei.core.crypto.Digest) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)

Example 18 with Digest

use of com.github.zhenwei.core.crypto.Digest in project LinLong-Java by zhenwei1108.

the class CipherSpi method initFromSpec.

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

Example 19 with Digest

use of com.github.zhenwei.core.crypto.Digest in project LinLong-Java by zhenwei1108.

the class CipherSpi method engineInit.

protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    CipherParameters param;
    if (key instanceof DHPublicKey) {
        param = ElGamalUtil.generatePublicKeyParameter((PublicKey) key);
    } else if (key instanceof DHPrivateKey) {
        param = ElGamalUtil.generatePrivateKeyParameter((PrivateKey) key);
    } else {
        throw new InvalidKeyException("unknown key type passed to ElGamal");
    }
    if (params instanceof OAEPParameterSpec) {
        OAEPParameterSpec spec = (OAEPParameterSpec) params;
        paramSpec = params;
        if (!spec.getMGFAlgorithm().equalsIgnoreCase("MGF1") && !spec.getMGFAlgorithm().equals(PKCSObjectIdentifiers.id_mgf1.getId())) {
            throw new InvalidAlgorithmParameterException("unknown mask generation function specified");
        }
        if (!(spec.getMGFParameters() instanceof MGF1ParameterSpec)) {
            throw new InvalidAlgorithmParameterException("unkown MGF parameters");
        }
        Digest digest = DigestFactory.getDigest(spec.getDigestAlgorithm());
        if (digest == null) {
            throw new InvalidAlgorithmParameterException("no match on digest algorithm: " + spec.getDigestAlgorithm());
        }
        MGF1ParameterSpec mgfParams = (MGF1ParameterSpec) spec.getMGFParameters();
        Digest mgfDigest = DigestFactory.getDigest(mgfParams.getDigestAlgorithm());
        if (mgfDigest == null) {
            throw new InvalidAlgorithmParameterException("no match on MGF digest algorithm: " + mgfParams.getDigestAlgorithm());
        }
        cipher = new OAEPEncoding(new ElGamalEngine(), digest, mgfDigest, ((PSource.PSpecified) spec.getPSource()).getValue());
    } else if (params != null) {
        throw new InvalidAlgorithmParameterException("unknown parameter type.");
    }
    if (random != null) {
        param = new ParametersWithRandom(param, random);
    }
    switch(opmode) {
        case javax.crypto.Cipher.ENCRYPT_MODE:
        case javax.crypto.Cipher.WRAP_MODE:
            cipher.init(true, param);
            break;
        case javax.crypto.Cipher.DECRYPT_MODE:
        case javax.crypto.Cipher.UNWRAP_MODE:
            cipher.init(false, param);
            break;
        default:
            throw new InvalidParameterException("unknown opmode " + opmode + " passed to ElGamal");
    }
}
Also used : DHPrivateKey(javax.crypto.interfaces.DHPrivateKey) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) DHPublicKey(javax.crypto.interfaces.DHPublicKey) Digest(com.github.zhenwei.core.crypto.Digest) PublicKey(java.security.PublicKey) DHPublicKey(javax.crypto.interfaces.DHPublicKey) ParametersWithRandom(com.github.zhenwei.core.crypto.params.ParametersWithRandom) InvalidKeyException(java.security.InvalidKeyException) ElGamalEngine(com.github.zhenwei.core.crypto.engines.ElGamalEngine) OAEPParameterSpec(javax.crypto.spec.OAEPParameterSpec) CipherParameters(com.github.zhenwei.core.crypto.CipherParameters) InvalidParameterException(java.security.InvalidParameterException) OAEPEncoding(com.github.zhenwei.core.crypto.encodings.OAEPEncoding) MGF1ParameterSpec(java.security.spec.MGF1ParameterSpec)

Example 20 with Digest

use of com.github.zhenwei.core.crypto.Digest in project LinLong-Java by zhenwei1108.

the class Ed25519 method implSign.

private static void implSign(byte[] sk, int skOff, byte[] pk, int pkOff, byte[] ctx, byte phflag, byte[] m, int mOff, int mLen, byte[] sig, int sigOff) {
    if (!checkContextVar(ctx, phflag)) {
        throw new IllegalArgumentException("ctx");
    }
    Digest d = createDigest();
    byte[] h = new byte[d.getDigestSize()];
    d.update(sk, skOff, SECRET_KEY_SIZE);
    d.doFinal(h, 0);
    byte[] s = new byte[SCALAR_BYTES];
    pruneScalar(h, 0, s);
    implSign(d, h, s, pk, pkOff, ctx, phflag, m, mOff, mLen, sig, sigOff);
}
Also used : Digest(com.github.zhenwei.core.crypto.Digest) SHA512Digest(com.github.zhenwei.core.crypto.digests.SHA512Digest)

Aggregations

Digest (com.github.zhenwei.core.crypto.Digest)30 MGF1ParameterSpec (java.security.spec.MGF1ParameterSpec)5 OAEPEncoding (com.github.zhenwei.core.crypto.encodings.OAEPEncoding)4 BigInteger (java.math.BigInteger)4 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)3 CipherParameters (com.github.zhenwei.core.crypto.CipherParameters)3 SHA256Digest (com.github.zhenwei.core.crypto.digests.SHA256Digest)3 SHA512Digest (com.github.zhenwei.core.crypto.digests.SHA512Digest)3 KeyParameter (com.github.zhenwei.core.crypto.params.KeyParameter)3 DSADigestSigner (com.github.zhenwei.core.crypto.signers.DSADigestSigner)3 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)3 ElGamalEngine (com.github.zhenwei.core.crypto.engines.ElGamalEngine)2 RSABlindedEngine (com.github.zhenwei.core.crypto.engines.RSABlindedEngine)2 AsymmetricKeyParameter (com.github.zhenwei.core.crypto.params.AsymmetricKeyParameter)2 IESWithCipherParameters (com.github.zhenwei.core.crypto.params.IESWithCipherParameters)2 ParametersWithIV (com.github.zhenwei.core.crypto.params.ParametersWithIV)2 ParametersWithRandom (com.github.zhenwei.core.crypto.params.ParametersWithRandom)2 ECDSASigner (com.github.zhenwei.core.crypto.signers.ECDSASigner)2 InvalidKeyException (java.security.InvalidKeyException)2 InvalidParameterException (java.security.InvalidParameterException)2