Search in sources :

Example 81 with KeyParameter

use of org.bouncycastle.crypto.params.KeyParameter in project web3sdk by FISCO-BCOS.

the class Wallet method generateAes128CtrDerivedKey.

private static byte[] generateAes128CtrDerivedKey(byte[] password, byte[] salt, int c, String prf) throws CipherException {
    if (!prf.equals("hmac-sha256")) {
        throw new CipherException("Unsupported prf:" + prf);
    }
    // Java 8 supports this, but you have to convert the password to a character array, see
    // http://stackoverflow.com/a/27928435/3211687
    PKCS5S2ParametersGenerator gen = new PKCS5S2ParametersGenerator(new SHA256Digest());
    gen.init(password, salt, c);
    return ((KeyParameter) gen.generateDerivedParameters(256)).getKey();
}
Also used : PKCS5S2ParametersGenerator(org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator) SHA256Digest(org.bouncycastle.crypto.digests.SHA256Digest) KeyParameter(org.bouncycastle.crypto.params.KeyParameter)

Example 82 with KeyParameter

use of org.bouncycastle.crypto.params.KeyParameter in project web3sdk by FISCO-BCOS.

the class Hash method hmacSha512.

public static byte[] hmacSha512(byte[] key, byte[] input) {
    HMac hMac = new HMac(new SHA512Digest());
    hMac.init(new KeyParameter(key));
    hMac.update(input, 0, input.length);
    byte[] out = new byte[64];
    hMac.doFinal(out, 0);
    return out;
}
Also used : SHA512Digest(org.bouncycastle.crypto.digests.SHA512Digest) HMac(org.bouncycastle.crypto.macs.HMac) KeyParameter(org.bouncycastle.crypto.params.KeyParameter)

Example 83 with KeyParameter

use of org.bouncycastle.crypto.params.KeyParameter in project Conversations by siacs.

the class ScramMechanism method hmac.

private byte[] hmac(final byte[] key, final byte[] input) throws InvalidKeyException {
    final HMac hMac = getHMAC();
    hMac.init(new KeyParameter(key));
    hMac.update(input, 0, input.length);
    final byte[] out = new byte[hMac.getMacSize()];
    hMac.doFinal(out, 0);
    return out;
}
Also used : HMac(org.bouncycastle.crypto.macs.HMac) KeyParameter(org.bouncycastle.crypto.params.KeyParameter)

Example 84 with KeyParameter

use of org.bouncycastle.crypto.params.KeyParameter in project Skein3Fish by wernerd.

the class SkeinTest method checkKATVectors.

boolean checkKATVectors() {
    KatResult kr = new KatResult();
    ParametersForSkein pfs;
    while (scanner.fillResult(kr)) {
        // Skip Tree vectors in this test function
        if (kr.restOfLine.contains("Tree")) {
            notProcessed++;
            continue;
        }
        if (kr.restOfLine.contains("MAC")) {
            pfs = new ParametersForSkein(new KeyParameter(kr.macKey), kr.stateSize, kr.hashBitLength);
            SkeinMac sm = new SkeinMac();
            sm.init(pfs);
            sm.updateBits(kr.msg, 0, kr.msgLength);
            byte[] mac = new byte[sm.getMacSize()];
            sm.doFinal(mac, 0);
            if (!Arrays.equals(mac, kr.result)) {
                System.out.println(kr.stateSize + "-" + kr.hashBitLength + "-" + kr.msgLength + "-" + kr.restOfLine);
                hexdump("Computed mac", mac, mac.length);
                hexdump("Expected result", kr.result, kr.result.length);
                return false;
            }
            processed++;
            continue;
        }
        Skein skein = new Skein(kr.stateSize, kr.hashBitLength);
        skein.updateBits(kr.msg, 0, kr.msgLength);
        byte[] hash = skein.doFinal();
        if (!Arrays.equals(hash, kr.result)) {
            System.out.println(kr.stateSize + "-" + kr.hashBitLength + "-" + kr.msgLength + "-" + kr.restOfLine);
            hexdump("Computed hash", hash, hash.length);
            hexdump("Expected result", kr.result, kr.result.length);
            return false;
        }
        // Enable the next few line so you can check some results manually
        // if ((kr.msgLength & 1) == 1) {
        // System.out.println(kr.stateSize + "-" + kr.hashBitLength + "-"
        // + kr.msgLength + "-" + kr.restOfLine);
        // hexdump("Computed hash", hash, hash.length);
        // hexdump("Expected result", kr.result, kr.result.length);
        // }
        processed++;
    }
    return true;
}
Also used : ParametersForSkein(org.bouncycastle.crypto.params.ParametersForSkein) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) ParametersForSkein(org.bouncycastle.crypto.params.ParametersForSkein) SkeinMac(org.bouncycastle.crypto.macs.SkeinMac)

Example 85 with KeyParameter

use of org.bouncycastle.crypto.params.KeyParameter in project Skein3Fish by wernerd.

the class ThreefishCipher method init.

public void init(boolean forEncryption, CipherParameters params) throws IllegalArgumentException {
    if (params instanceof ParametersForThreefish) {
        ParametersForThreefish pft = (ParametersForThreefish) params;
        stateSize = pft.getStateSize();
        setCipher(stateSize);
        if (cipher == null)
            throw new IllegalArgumentException("Threefish: unsupported state size: " + stateSize);
        byte[] key = ((KeyParameter) pft.getParameters()).getKey();
        if (key.length != (stateSize / 8))
            throw new IllegalArgumentException("Threefish: key length does not match state size: " + key.length);
        long[] tweak = pft.getTweak();
        if (tweak == null)
            throw new IllegalArgumentException("Threefish: tweak data not set");
        cipher.setTweak(tweak);
        // Get a long array for cipher key and moves the byte key buffer to it
        long[] keyLong = new long[stateSize / 64];
        for (int i = 0; i < keyLong.length; i++) keyLong[i] = ByteLong.GetUInt64(key, i * 8);
        cipher.setKey(keyLong);
        this.forEncryption = forEncryption;
        // Allocate buffers
        cipherIn = new long[stateSize / 64];
        cipherOut = new long[stateSize / 64];
        return;
    }
    throw new IllegalArgumentException("Threfish: invalid parameter passed to init - " + params.getClass().getName());
}
Also used : KeyParameter(org.bouncycastle.crypto.params.KeyParameter) ParametersForThreefish(org.bouncycastle.crypto.params.ParametersForThreefish)

Aggregations

KeyParameter (org.bouncycastle.crypto.params.KeyParameter)119 ParametersWithIV (org.bouncycastle.crypto.params.ParametersWithIV)51 AESEngine (org.bouncycastle.crypto.engines.AESEngine)37 CipherParameters (org.bouncycastle.crypto.CipherParameters)35 GCMBlockCipher (org.bouncycastle.crypto.modes.GCMBlockCipher)24 AEADParameters (org.bouncycastle.crypto.params.AEADParameters)22 CBCBlockCipher (org.bouncycastle.crypto.modes.CBCBlockCipher)21 PaddedBufferedBlockCipher (org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher)21 IvParameterSpec (javax.crypto.spec.IvParameterSpec)19 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)16 InvalidCipherTextException (org.bouncycastle.crypto.InvalidCipherTextException)16 AEADBlockCipher (org.bouncycastle.crypto.modes.AEADBlockCipher)16 InvalidKeyException (java.security.InvalidKeyException)13 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)12 SecretKeySpec (javax.crypto.spec.SecretKeySpec)12 BufferedBlockCipher (org.bouncycastle.crypto.BufferedBlockCipher)12 PKCS5S2ParametersGenerator (org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator)12 SecureRandom (java.security.SecureRandom)11 SecretKey (javax.crypto.SecretKey)10 BlockCipher (org.bouncycastle.crypto.BlockCipher)8