Search in sources :

Example 21 with KeyParameter

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

the class BaseMac method engineInit.

protected void engineInit(Key key, final AlgorithmParameterSpec params) throws InvalidKeyException, InvalidAlgorithmParameterException {
    CipherParameters param;
    if (key == null) {
        throw new InvalidKeyException("key is null");
    }
    if (key instanceof PKCS12Key) {
        SecretKey k;
        PBEParameterSpec pbeSpec;
        try {
            k = (SecretKey) key;
        } catch (Exception e) {
            throw new InvalidKeyException("PKCS12 requires a SecretKey/PBEKey");
        }
        try {
            pbeSpec = (PBEParameterSpec) params;
        } catch (Exception e) {
            throw new InvalidAlgorithmParameterException("PKCS12 requires a PBEParameterSpec");
        }
        if (k instanceof PBEKey && pbeSpec == null) {
            pbeSpec = new PBEParameterSpec(((PBEKey) k).getSalt(), ((PBEKey) k).getIterationCount());
        }
        int digest = SHA1;
        int keySize = 160;
        if (macEngine.getAlgorithmName().startsWith("GOST")) {
            digest = GOST3411;
            keySize = 256;
        } else if (macEngine instanceof HMac) {
            if (!macEngine.getAlgorithmName().startsWith("SHA-1")) {
                if (macEngine.getAlgorithmName().startsWith("SHA-224")) {
                    digest = SHA224;
                    keySize = 224;
                } else if (macEngine.getAlgorithmName().startsWith("SHA-256")) {
                    digest = SHA256;
                    keySize = 256;
                } else if (macEngine.getAlgorithmName().startsWith("SHA-384")) {
                    digest = SHA384;
                    keySize = 384;
                } else if (macEngine.getAlgorithmName().startsWith("SHA-512")) {
                    digest = SHA512;
                    keySize = 512;
                } else if (macEngine.getAlgorithmName().startsWith("RIPEMD160")) {
                    digest = RIPEMD160;
                    keySize = 160;
                } else {
                    throw new InvalidAlgorithmParameterException("no PKCS12 mapping for HMAC: " + macEngine.getAlgorithmName());
                }
            }
        }
        // TODO: add correct handling for other digests
        param = Util.makePBEMacParameters(k, PKCS12, digest, keySize, pbeSpec);
    } else if (key instanceof BCPBEKey) {
        BCPBEKey k = (BCPBEKey) key;
        if (k.getParam() != null) {
            param = k.getParam();
        } else if (params instanceof PBEParameterSpec) {
            param = Util.makePBEMacParameters(k, params);
        } else {
            throw new InvalidAlgorithmParameterException("PBE requires PBE parameters to be set.");
        }
    } else {
        if (params instanceof PBEParameterSpec) {
            throw new InvalidAlgorithmParameterException("inappropriate parameter type: " + params.getClass().getName());
        }
        param = new KeyParameter(key.getEncoded());
    }
    final KeyParameter keyParam;
    if (param instanceof ParametersWithIV) {
        keyParam = (KeyParameter) ((ParametersWithIV) param).getParameters();
    } else {
        keyParam = (KeyParameter) param;
    }
    if (params instanceof AEADParameterSpec) {
        AEADParameterSpec aeadSpec = (AEADParameterSpec) params;
        param = new AEADParameters(keyParam, aeadSpec.getMacSizeInBits(), aeadSpec.getNonce(), aeadSpec.getAssociatedData());
    } else if (params instanceof IvParameterSpec) {
        param = new ParametersWithIV(keyParam, ((IvParameterSpec) params).getIV());
    } else if (params instanceof RC2ParameterSpec) {
        param = new ParametersWithIV(new RC2Parameters(keyParam.getKey(), ((RC2ParameterSpec) params).getEffectiveKeyBits()), ((RC2ParameterSpec) params).getIV());
    } else if (params instanceof SkeinParameterSpec) {
        param = new SkeinParameters.Builder(copyMap(((SkeinParameterSpec) params).getParameters())).setKey(keyParam.getKey()).build();
    } else if (params == null) {
        param = new KeyParameter(key.getEncoded());
    } else if (gcmSpecClass != null && gcmSpecClass.isAssignableFrom(params.getClass())) {
        param = GcmSpecUtil.extractAeadParameters(keyParam, params);
    } else if (!(params instanceof PBEParameterSpec)) {
        throw new InvalidAlgorithmParameterException("unknown parameter type: " + params.getClass().getName());
    }
    try {
        macEngine.init(param);
    } catch (Exception e) {
        throw new InvalidAlgorithmParameterException("cannot initialize MAC: " + e.getMessage());
    }
}
Also used : RC2Parameters(com.github.zhenwei.core.crypto.params.RC2Parameters) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) HMac(com.github.zhenwei.core.crypto.macs.HMac) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter) PBEKey(javax.crypto.interfaces.PBEKey) InvalidKeyException(java.security.InvalidKeyException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) InvalidKeyException(java.security.InvalidKeyException) AEADParameterSpec(com.github.zhenwei.provider.jcajce.spec.AEADParameterSpec) CipherParameters(com.github.zhenwei.core.crypto.CipherParameters) ParametersWithIV(com.github.zhenwei.core.crypto.params.ParametersWithIV) SecretKey(javax.crypto.SecretKey) AEADParameters(com.github.zhenwei.core.crypto.params.AEADParameters) SkeinParameterSpec(com.github.zhenwei.provider.jcajce.spec.SkeinParameterSpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) PKCS12Key(com.github.zhenwei.provider.jcajce.PKCS12Key) RC2ParameterSpec(javax.crypto.spec.RC2ParameterSpec) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Example 22 with KeyParameter

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

the class FPEFF3_1Engine method init.

public void init(boolean forEncryption, CipherParameters parameters) {
    this.forEncryption = forEncryption;
    this.fpeParameters = (FPEParameters) parameters;
    baseCipher.init(!fpeParameters.isUsingInverseFunction(), new KeyParameter(Arrays.reverse(fpeParameters.getKey().getKey())));
    if (fpeParameters.getTweak().length != 7) {
        throw new IllegalArgumentException("tweak should be 56 bits");
    }
}
Also used : KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter)

Example 23 with KeyParameter

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

the class OpenSSLPBEParametersGenerator method generateDerivedParameters.

/**
 * Generate a key with initialisation vector parameter derived from the password, salt, and
 * iteration count we are currently initialised with.
 *
 * @param keySize the size of the key we want (in bits)
 * @param ivSize  the size of the iv we want (in bits)
 * @return a ParametersWithIV object.
 * @throws IllegalArgumentException if keySize + ivSize is larger than the base hash size.
 */
public CipherParameters generateDerivedParameters(int keySize, int ivSize) {
    keySize = keySize / 8;
    ivSize = ivSize / 8;
    byte[] dKey = generateDerivedKey(keySize + ivSize);
    return new ParametersWithIV(new KeyParameter(dKey, 0, keySize), dKey, keySize, ivSize);
}
Also used : ParametersWithIV(com.github.zhenwei.core.crypto.params.ParametersWithIV) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter)

Example 24 with KeyParameter

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

the class PKCS12ParametersGenerator method generateDerivedParameters.

/**
 * Generate a key with initialisation vector parameter derived from the password, salt, and
 * iteration count we are currently initialised with.
 *
 * @param keySize the size of the key we want (in bits)
 * @param ivSize  the size of the iv we want (in bits)
 * @return a ParametersWithIV object.
 */
public CipherParameters generateDerivedParameters(int keySize, int ivSize) {
    keySize = keySize / 8;
    ivSize = ivSize / 8;
    byte[] dKey = generateDerivedKey(KEY_MATERIAL, keySize);
    byte[] iv = generateDerivedKey(IV_MATERIAL, ivSize);
    return new ParametersWithIV(new KeyParameter(dKey, 0, keySize), iv, 0, ivSize);
}
Also used : ParametersWithIV(com.github.zhenwei.core.crypto.params.ParametersWithIV) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter)

Example 25 with KeyParameter

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

the class PKCS5S2ParametersGenerator method generateDerivedParameters.

/**
 * Generate a key with initialisation vector parameter derived from the password, salt, and
 * iteration count we are currently initialised with.
 *
 * @param keySize the size of the key we want (in bits)
 * @param ivSize  the size of the iv we want (in bits)
 * @return a ParametersWithIV object.
 */
public CipherParameters generateDerivedParameters(int keySize, int ivSize) {
    keySize = keySize / 8;
    ivSize = ivSize / 8;
    byte[] dKey = generateDerivedKey(keySize + ivSize);
    return new ParametersWithIV(new KeyParameter(dKey, 0, keySize), dKey, keySize, ivSize);
}
Also used : ParametersWithIV(com.github.zhenwei.core.crypto.params.ParametersWithIV) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter)

Aggregations

KeyParameter (com.github.zhenwei.core.crypto.params.KeyParameter)91 ParametersWithIV (com.github.zhenwei.core.crypto.params.ParametersWithIV)41 CipherParameters (com.github.zhenwei.core.crypto.CipherParameters)15 AEADParameters (com.github.zhenwei.core.crypto.params.AEADParameters)10 InvalidCipherTextException (com.github.zhenwei.core.crypto.InvalidCipherTextException)6 ParametersWithSBox (com.github.zhenwei.core.crypto.params.ParametersWithSBox)6 RC2Parameters (com.github.zhenwei.core.crypto.params.RC2Parameters)6 BigInteger (java.math.BigInteger)6 BufferedBlockCipher (com.github.zhenwei.core.crypto.BufferedBlockCipher)4 StreamCipher (com.github.zhenwei.core.crypto.StreamCipher)4 PKCS5S2ParametersGenerator (com.github.zhenwei.core.crypto.generators.PKCS5S2ParametersGenerator)4 HMac (com.github.zhenwei.core.crypto.macs.HMac)4 AsymmetricKeyParameter (com.github.zhenwei.core.crypto.params.AsymmetricKeyParameter)4 IESWithCipherParameters (com.github.zhenwei.core.crypto.params.IESWithCipherParameters)4 ParametersWithRandom (com.github.zhenwei.core.crypto.params.ParametersWithRandom)4 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)4 IvParameterSpec (javax.crypto.spec.IvParameterSpec)4 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)3 RC5Parameters (com.github.zhenwei.core.crypto.params.RC5Parameters)3 CMSException (com.github.zhenwei.pkix.cms.CMSException)3