Search in sources :

Example 11 with Digest

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

the class BcECContentVerifierProviderBuilder method createSigner.

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

Example 12 with Digest

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

the class PSSSignatureSpi method engineSetParameter.

protected void engineSetParameter(AlgorithmParameterSpec params) throws InvalidAlgorithmParameterException {
    if (params == null) {
        if (originalSpec != null) {
            params = originalSpec;
        } else {
            // Java 11 bug
            return;
        }
    }
    if (!isInitState) {
        throw new ProviderException("cannot call setParameter in the middle of update");
    }
    if (params instanceof PSSParameterSpec) {
        PSSParameterSpec newParamSpec = (PSSParameterSpec) params;
        if (originalSpec != null) {
            if (!DigestFactory.isSameDigest(originalSpec.getDigestAlgorithm(), newParamSpec.getDigestAlgorithm())) {
                throw new InvalidAlgorithmParameterException("parameter must be using " + originalSpec.getDigestAlgorithm());
            }
        }
        Digest mgfDigest;
        if (newParamSpec.getMGFAlgorithm().equalsIgnoreCase("MGF1") || newParamSpec.getMGFAlgorithm().equals(PKCSObjectIdentifiers.id_mgf1.getId())) {
            if (!(newParamSpec.getMGFParameters() instanceof MGF1ParameterSpec)) {
                throw new InvalidAlgorithmParameterException("unknown MGF parameters");
            }
            MGF1ParameterSpec mgfParams = (MGF1ParameterSpec) newParamSpec.getMGFParameters();
            if (!DigestFactory.isSameDigest(mgfParams.getDigestAlgorithm(), newParamSpec.getDigestAlgorithm())) {
                throw new InvalidAlgorithmParameterException("digest algorithm for MGF should be the same as for PSS parameters.");
            }
            mgfDigest = DigestFactory.getDigest(mgfParams.getDigestAlgorithm());
        } else if (newParamSpec.getMGFAlgorithm().equals("SHAKE128") || newParamSpec.getMGFAlgorithm().equals("SHAKE256")) {
            mgfDigest = DigestFactory.getDigest(newParamSpec.getMGFAlgorithm());
        } else {
            throw new InvalidAlgorithmParameterException("unknown mask generation function specified");
        }
        if (mgfDigest == null) {
            throw new InvalidAlgorithmParameterException("no match on MGF algorithm: " + newParamSpec.getMGFAlgorithm());
        }
        this.engineParams = null;
        this.paramSpec = newParamSpec;
        this.mgfDigest = mgfDigest;
        this.saltLength = paramSpec.getSaltLength();
        this.trailer = getTrailer(paramSpec.getTrailerField());
        setupContentDigest();
        if (key != null) {
            pss = new com.github.zhenwei.core.crypto.signers.PSSSigner(signer, contentDigest, mgfDigest, saltLength, trailer);
            if (key.isPrivate()) {
                pss.init(true, key);
            } else {
                pss.init(false, key);
            }
        }
    } else {
        throw new InvalidAlgorithmParameterException("Only PSSParameterSpec supported");
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) ProviderException(java.security.ProviderException) Digest(com.github.zhenwei.core.crypto.Digest) PSSParameterSpec(java.security.spec.PSSParameterSpec) MGF1ParameterSpec(java.security.spec.MGF1ParameterSpec)

Example 13 with Digest

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

the class Mac method hmac.

public byte[] hmac(DigestAlgEnum digestAlgEnum, Key key, byte[] source) {
    Digest digest;
    switch(digestAlgEnum) {
        case SHA1:
            digest = new SHA1Digest();
            break;
        case SHA224:
            digest = new SHA224Digest();
            break;
        case SHA256:
            digest = new SHA256Digest();
            break;
        case SHA384:
            digest = new SHA384Digest();
            break;
        case SHA512:
            digest = new SHA512Digest();
            break;
        // default sm3 digest
        default:
            digest = new SM3Digest();
            break;
    }
    HMac hMac = new HMac(digest);
    hMac.init(new KeyParameter(key.getEncoded()));
    hMac.update(source, 0, source.length);
    byte[] result = new byte[hMac.getMacSize()];
    hMac.doFinal(result, 0);
    return result;
}
Also used : Digest(com.github.zhenwei.core.crypto.Digest) HMac(com.github.zhenwei.core.crypto.macs.HMac) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter)

Example 14 with Digest

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

the class LM_OTS method lm_ots_validate_signature_calculate.

public static byte[] lm_ots_validate_signature_calculate(LMSContext context) {
    LMOtsPublicKey publicKey = context.getPublicKey();
    LMOtsParameters parameter = publicKey.getParameter();
    Object sig = context.getSignature();
    LMOtsSignature signature;
    if (sig instanceof LMSSignature) {
        signature = ((LMSSignature) sig).getOtsSignature();
    } else {
        signature = (LMOtsSignature) sig;
    }
    int n = parameter.getN();
    int w = parameter.getW();
    int p = parameter.getP();
    byte[] Q = context.getQ();
    int cs = cksm(Q, n, parameter);
    Q[n] = (byte) ((cs >>> 8) & 0xFF);
    Q[n + 1] = (byte) cs;
    byte[] I = publicKey.getI();
    int q = publicKey.getQ();
    Digest finalContext = DigestUtil.getDigest(parameter.getDigestOID());
    LmsUtils.byteArray(I, finalContext);
    LmsUtils.u32str(q, finalContext);
    LmsUtils.u16str(D_PBLC, finalContext);
    byte[] tmp = Composer.compose().bytes(I).u32str(q).padUntil(0, ITER_PREV + n).build();
    int max_digit = (1 << w) - 1;
    byte[] y = signature.getY();
    Digest ctx = DigestUtil.getDigest(parameter.getDigestOID());
    for (int i = 0; i < p; i++) {
        Pack.shortToBigEndian((short) i, tmp, ITER_K);
        System.arraycopy(y, i * n, tmp, ITER_PREV, n);
        int a = coef(Q, i, w);
        for (int j = a; j < max_digit; j++) {
            tmp[ITER_J] = (byte) j;
            ctx.update(tmp, 0, ITER_PREV + n);
            ctx.doFinal(tmp, ITER_PREV);
        }
        finalContext.update(tmp, ITER_PREV, n);
    }
    byte[] K = new byte[n];
    finalContext.doFinal(K, 0);
    return K;
}
Also used : Digest(com.github.zhenwei.core.crypto.Digest)

Example 15 with Digest

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

the class BcDigestCalculatorProvider method get.

public DigestCalculator get(final AlgorithmIdentifier algorithm) throws OperatorCreationException {
    Digest dig = digestProvider.get(algorithm);
    final DigestOutputStream stream = new DigestOutputStream(dig);
    return new DigestCalculator() {

        public AlgorithmIdentifier getAlgorithmIdentifier() {
            return algorithm;
        }

        public OutputStream getOutputStream() {
            return stream;
        }

        public byte[] getDigest() {
            return stream.getDigest();
        }
    };
}
Also used : Digest(com.github.zhenwei.core.crypto.Digest) DigestCalculator(com.github.zhenwei.pkix.operator.DigestCalculator)

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