Search in sources :

Example 1 with SHA3Digest

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

the class NewHope method sha3.

static void sha3(byte[] sharedKey) {
    SHA3Digest d = new SHA3Digest(256);
    d.update(sharedKey, 0, 32);
    d.doFinal(sharedKey, 0);
}
Also used : SHA3Digest(com.github.zhenwei.core.crypto.digests.SHA3Digest)

Example 2 with SHA3Digest

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

the class BcDefaultDigestProvider method createTable.

private static Map createTable() {
    Map table = new HashMap();
    table.put(OIWObjectIdentifiers.idSHA1, new BcDigestProvider() {

        public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) {
            return new SHA1Digest();
        }
    });
    table.put(NISTObjectIdentifiers.id_sha224, new BcDigestProvider() {

        public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) {
            return new SHA224Digest();
        }
    });
    table.put(NISTObjectIdentifiers.id_sha256, new BcDigestProvider() {

        public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) {
            return new SHA256Digest();
        }
    });
    table.put(NISTObjectIdentifiers.id_sha384, new BcDigestProvider() {

        public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) {
            return new SHA384Digest();
        }
    });
    table.put(NISTObjectIdentifiers.id_sha512, new BcDigestProvider() {

        public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) {
            return new SHA512Digest();
        }
    });
    table.put(NISTObjectIdentifiers.id_sha3_224, new BcDigestProvider() {

        public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) {
            return new SHA3Digest(224);
        }
    });
    table.put(NISTObjectIdentifiers.id_sha3_256, new BcDigestProvider() {

        public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) {
            return new SHA3Digest(256);
        }
    });
    table.put(NISTObjectIdentifiers.id_sha3_384, new BcDigestProvider() {

        public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) {
            return new SHA3Digest(384);
        }
    });
    table.put(NISTObjectIdentifiers.id_sha3_512, new BcDigestProvider() {

        public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) {
            return new SHA3Digest(512);
        }
    });
    table.put(NISTObjectIdentifiers.id_shake128, new BcDigestProvider() {

        public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) {
            return new SHAKEDigest(128);
        }
    });
    table.put(NISTObjectIdentifiers.id_shake256, new BcDigestProvider() {

        public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) {
            return new SHAKEDigest(256);
        }
    });
    table.put(NISTObjectIdentifiers.id_shake128_len, new BcDigestProvider() {

        public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) {
            return new AdjustedXof(new SHAKEDigest(128), ASN1Integer.getInstance(digestAlgorithmIdentifier.getParameters()).intValueExact());
        }
    });
    table.put(NISTObjectIdentifiers.id_shake256_len, new BcDigestProvider() {

        public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) {
            return new AdjustedXof(new SHAKEDigest(256), ASN1Integer.getInstance(digestAlgorithmIdentifier.getParameters()).intValueExact());
        }
    });
    table.put(PKCSObjectIdentifiers.md5, new BcDigestProvider() {

        public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) {
            return new MD5Digest();
        }
    });
    table.put(PKCSObjectIdentifiers.md4, new BcDigestProvider() {

        public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) {
            return new MD4Digest();
        }
    });
    table.put(PKCSObjectIdentifiers.md2, new BcDigestProvider() {

        public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) {
            return new MD2Digest();
        }
    });
    table.put(CryptoProObjectIdentifiers.gostR3411, new BcDigestProvider() {

        public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) {
            return new GOST3411Digest();
        }
    });
    table.put(RosstandartObjectIdentifiers.id_tc26_gost_3411_12_256, new BcDigestProvider() {

        public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) {
            return new GOST3411_2012_256Digest();
        }
    });
    table.put(RosstandartObjectIdentifiers.id_tc26_gost_3411_12_512, new BcDigestProvider() {

        public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) {
            return new GOST3411_2012_512Digest();
        }
    });
    table.put(TeleTrusTObjectIdentifiers.ripemd128, new BcDigestProvider() {

        public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) {
            return new RIPEMD128Digest();
        }
    });
    table.put(TeleTrusTObjectIdentifiers.ripemd160, new BcDigestProvider() {

        public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) {
            return new RIPEMD160Digest();
        }
    });
    table.put(TeleTrusTObjectIdentifiers.ripemd256, new BcDigestProvider() {

        public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) {
            return new RIPEMD256Digest();
        }
    });
    table.put(GMObjectIdentifiers.sm3, new BcDigestProvider() {

        public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) {
            return new SM3Digest();
        }
    });
    return Collections.unmodifiableMap(table);
}
Also used : ExtendedDigest(com.github.zhenwei.core.crypto.ExtendedDigest) SHA512Digest(com.github.zhenwei.core.crypto.digests.SHA512Digest) RIPEMD128Digest(com.github.zhenwei.core.crypto.digests.RIPEMD128Digest) MD2Digest(com.github.zhenwei.core.crypto.digests.MD2Digest) SHA224Digest(com.github.zhenwei.core.crypto.digests.SHA224Digest) HashMap(java.util.HashMap) SHA1Digest(com.github.zhenwei.core.crypto.digests.SHA1Digest) GOST3411Digest(com.github.zhenwei.core.crypto.digests.GOST3411Digest) RIPEMD160Digest(com.github.zhenwei.core.crypto.digests.RIPEMD160Digest) RIPEMD256Digest(com.github.zhenwei.core.crypto.digests.RIPEMD256Digest) MD4Digest(com.github.zhenwei.core.crypto.digests.MD4Digest) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier) GOST3411_2012_256Digest(com.github.zhenwei.core.crypto.digests.GOST3411_2012_256Digest) SHA3Digest(com.github.zhenwei.core.crypto.digests.SHA3Digest) MD5Digest(com.github.zhenwei.core.crypto.digests.MD5Digest) SM3Digest(com.github.zhenwei.core.crypto.digests.SM3Digest) SHA256Digest(com.github.zhenwei.core.crypto.digests.SHA256Digest) GOST3411_2012_512Digest(com.github.zhenwei.core.crypto.digests.GOST3411_2012_512Digest) HashMap(java.util.HashMap) Map(java.util.Map) SHA384Digest(com.github.zhenwei.core.crypto.digests.SHA384Digest) SHAKEDigest(com.github.zhenwei.core.crypto.digests.SHAKEDigest)

Example 3 with SHA3Digest

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

the class Sphincs256KeyPairGeneratorSpi method initialize.

public void initialize(AlgorithmParameterSpec params, SecureRandom random) throws InvalidAlgorithmParameterException {
    if (!(params instanceof SPHINCS256KeyGenParameterSpec)) {
        throw new InvalidAlgorithmParameterException("parameter object not a SPHINCS256KeyGenParameterSpec");
    }
    SPHINCS256KeyGenParameterSpec sphincsParams = (SPHINCS256KeyGenParameterSpec) params;
    if (sphincsParams.getTreeDigest().equals(SPHINCS256KeyGenParameterSpec.SHA512_256)) {
        treeDigest = NISTObjectIdentifiers.id_sha512_256;
        param = new SPHINCS256KeyGenerationParameters(random, new SHA512tDigest(256));
    } else if (sphincsParams.getTreeDigest().equals(SPHINCS256KeyGenParameterSpec.SHA3_256)) {
        treeDigest = NISTObjectIdentifiers.id_sha3_256;
        param = new SPHINCS256KeyGenerationParameters(random, new SHA3Digest(256));
    }
    engine.init(param);
    initialised = true;
}
Also used : SHA512tDigest(com.github.zhenwei.core.crypto.digests.SHA512tDigest) SHA3Digest(com.github.zhenwei.core.crypto.digests.SHA3Digest) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) SPHINCS256KeyGenParameterSpec(com.github.zhenwei.provider.jcajce.spec.SPHINCS256KeyGenParameterSpec) SPHINCS256KeyGenerationParameters(com.github.zhenwei.core.pqc.crypto.sphincs.SPHINCS256KeyGenerationParameters)

Example 4 with SHA3Digest

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

the class BcFKSKeyStoreSpi method generateKey.

private byte[] generateKey(KeyDerivationFunc pbkdAlgorithm, String purpose, char[] password, int defKeySize) throws IOException {
    byte[] encPassword = PBEParametersGenerator.PKCS12PasswordToBytes(password);
    byte[] differentiator = PBEParametersGenerator.PKCS12PasswordToBytes(purpose.toCharArray());
    int keySizeInBytes = defKeySize;
    if (MiscObjectIdentifiers.id_scrypt.equals(pbkdAlgorithm.getAlgorithm())) {
        ScryptParams params = ScryptParams.getInstance(pbkdAlgorithm.getParameters());
        if (params.getKeyLength() != null) {
            keySizeInBytes = params.getKeyLength().intValue();
        } else if (keySizeInBytes == -1) {
            throw new IOException("no keyLength found in ScryptParams");
        }
        return SCrypt.generate(Arrays.concatenate(encPassword, differentiator), params.getSalt(), params.getCostParameter().intValue(), params.getBlockSize().intValue(), params.getBlockSize().intValue(), keySizeInBytes);
    } else if (pbkdAlgorithm.getAlgorithm().equals(PKCSObjectIdentifiers.id_PBKDF2)) {
        PBKDF2Params pbkdf2Params = PBKDF2Params.getInstance(pbkdAlgorithm.getParameters());
        if (pbkdf2Params.getKeyLength() != null) {
            keySizeInBytes = pbkdf2Params.getKeyLength().intValue();
        } else if (keySizeInBytes == -1) {
            throw new IOException("no keyLength found in PBKDF2Params");
        }
        if (pbkdf2Params.getPrf().getAlgorithm().equals(PKCSObjectIdentifiers.id_hmacWithSHA512)) {
            PKCS5S2ParametersGenerator pGen = new PKCS5S2ParametersGenerator(new SHA512Digest());
            pGen.init(Arrays.concatenate(encPassword, differentiator), pbkdf2Params.getSalt(), pbkdf2Params.getIterationCount().intValue());
            return ((KeyParameter) pGen.generateDerivedParameters(keySizeInBytes * 8)).getKey();
        } else if (pbkdf2Params.getPrf().getAlgorithm().equals(NISTObjectIdentifiers.id_hmacWithSHA3_512)) {
            PKCS5S2ParametersGenerator pGen = new PKCS5S2ParametersGenerator(new SHA3Digest(512));
            pGen.init(Arrays.concatenate(encPassword, differentiator), pbkdf2Params.getSalt(), pbkdf2Params.getIterationCount().intValue());
            return ((KeyParameter) pGen.generateDerivedParameters(keySizeInBytes * 8)).getKey();
        } else {
            throw new IOException("BCFKS KeyStore: unrecognized MAC PBKD PRF: " + pbkdf2Params.getPrf().getAlgorithm());
        }
    } else {
        throw new IOException("BCFKS KeyStore: unrecognized MAC PBKD.");
    }
}
Also used : SHA512Digest(com.github.zhenwei.core.crypto.digests.SHA512Digest) SHA3Digest(com.github.zhenwei.core.crypto.digests.SHA3Digest) PKCS5S2ParametersGenerator(com.github.zhenwei.core.crypto.generators.PKCS5S2ParametersGenerator) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter) PBKDF2Params(com.github.zhenwei.core.asn1.pkcs.PBKDF2Params) IOException(java.io.IOException) ScryptParams(com.github.zhenwei.core.asn1.misc.ScryptParams)

Aggregations

SHA3Digest (com.github.zhenwei.core.crypto.digests.SHA3Digest)4 SHA512Digest (com.github.zhenwei.core.crypto.digests.SHA512Digest)2 ScryptParams (com.github.zhenwei.core.asn1.misc.ScryptParams)1 PBKDF2Params (com.github.zhenwei.core.asn1.pkcs.PBKDF2Params)1 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)1 ExtendedDigest (com.github.zhenwei.core.crypto.ExtendedDigest)1 GOST3411Digest (com.github.zhenwei.core.crypto.digests.GOST3411Digest)1 GOST3411_2012_256Digest (com.github.zhenwei.core.crypto.digests.GOST3411_2012_256Digest)1 GOST3411_2012_512Digest (com.github.zhenwei.core.crypto.digests.GOST3411_2012_512Digest)1 MD2Digest (com.github.zhenwei.core.crypto.digests.MD2Digest)1 MD4Digest (com.github.zhenwei.core.crypto.digests.MD4Digest)1 MD5Digest (com.github.zhenwei.core.crypto.digests.MD5Digest)1 RIPEMD128Digest (com.github.zhenwei.core.crypto.digests.RIPEMD128Digest)1 RIPEMD160Digest (com.github.zhenwei.core.crypto.digests.RIPEMD160Digest)1 RIPEMD256Digest (com.github.zhenwei.core.crypto.digests.RIPEMD256Digest)1 SHA1Digest (com.github.zhenwei.core.crypto.digests.SHA1Digest)1 SHA224Digest (com.github.zhenwei.core.crypto.digests.SHA224Digest)1 SHA256Digest (com.github.zhenwei.core.crypto.digests.SHA256Digest)1 SHA384Digest (com.github.zhenwei.core.crypto.digests.SHA384Digest)1 SHA512tDigest (com.github.zhenwei.core.crypto.digests.SHA512tDigest)1