Search in sources :

Example 1 with ParametersWithUKM

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

the class GOST28147WrapEngine method init.

public void init(boolean forWrapping, CipherParameters param) {
    if (param instanceof ParametersWithRandom) {
        ParametersWithRandom pr = (ParametersWithRandom) param;
        param = pr.getParameters();
    }
    ParametersWithUKM pU = (ParametersWithUKM) param;
    cipher.init(forWrapping, pU.getParameters());
    mac.init(new ParametersWithIV(pU.getParameters(), pU.getUKM()));
}
Also used : ParametersWithIV(com.github.zhenwei.core.crypto.params.ParametersWithIV) ParametersWithUKM(com.github.zhenwei.core.crypto.params.ParametersWithUKM) ParametersWithRandom(com.github.zhenwei.core.crypto.params.ParametersWithRandom)

Example 2 with ParametersWithUKM

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

the class BaseWrapCipher method engineInit.

protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    CipherParameters param;
    if (key instanceof BCPBEKey) {
        BCPBEKey k = (BCPBEKey) key;
        if (params instanceof PBEParameterSpec) {
            param = Util.makePBEParameters(k, params, wrapEngine.getAlgorithmName());
        } else if (k.getParam() != null) {
            param = k.getParam();
        } else {
            throw new InvalidAlgorithmParameterException("PBE requires PBE parameters to be set.");
        }
    } else {
        param = new KeyParameter(key.getEncoded());
    }
    if (params instanceof IvParameterSpec) {
        IvParameterSpec ivSpec = (IvParameterSpec) params;
        this.iv = ivSpec.getIV();
        param = new ParametersWithIV(param, iv);
    }
    if (params instanceof GOST28147WrapParameterSpec) {
        GOST28147WrapParameterSpec spec = (GOST28147WrapParameterSpec) params;
        byte[] sBox = spec.getSBox();
        if (sBox != null) {
            param = new ParametersWithSBox(param, sBox);
        }
        param = new ParametersWithUKM(param, spec.getUKM());
    }
    if (param instanceof KeyParameter && ivSize != 0) {
        if (opmode == Cipher.WRAP_MODE || opmode == Cipher.ENCRYPT_MODE) {
            iv = new byte[ivSize];
            random.nextBytes(iv);
            param = new ParametersWithIV(param, iv);
        }
    }
    if (random != null) {
        param = new ParametersWithRandom(param, random);
    }
    try {
        switch(opmode) {
            case Cipher.WRAP_MODE:
                wrapEngine.init(true, param);
                this.wrapStream = null;
                this.forWrapping = true;
                break;
            case Cipher.UNWRAP_MODE:
                wrapEngine.init(false, param);
                this.wrapStream = null;
                this.forWrapping = false;
                break;
            case Cipher.ENCRYPT_MODE:
                wrapEngine.init(true, param);
                this.wrapStream = new ErasableOutputStream();
                this.forWrapping = true;
                break;
            case Cipher.DECRYPT_MODE:
                wrapEngine.init(false, param);
                this.wrapStream = new ErasableOutputStream();
                this.forWrapping = false;
                break;
            default:
                throw new InvalidParameterException("Unknown mode parameter passed to init.");
        }
    } catch (Exception e) {
        throw new InvalidKeyOrParametersException(e.getMessage(), e);
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) GOST28147WrapParameterSpec(com.github.zhenwei.provider.jcajce.spec.GOST28147WrapParameterSpec) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter) ParametersWithRandom(com.github.zhenwei.core.crypto.params.ParametersWithRandom) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) InvalidParameterException(java.security.InvalidParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) InvalidCipherTextException(com.github.zhenwei.core.crypto.InvalidCipherTextException) ShortBufferException(javax.crypto.ShortBufferException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) NoSuchProviderException(java.security.NoSuchProviderException) CipherParameters(com.github.zhenwei.core.crypto.CipherParameters) ParametersWithIV(com.github.zhenwei.core.crypto.params.ParametersWithIV) InvalidParameterException(java.security.InvalidParameterException) ParametersWithUKM(com.github.zhenwei.core.crypto.params.ParametersWithUKM) ParametersWithSBox(com.github.zhenwei.core.crypto.params.ParametersWithSBox) IvParameterSpec(javax.crypto.spec.IvParameterSpec) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Example 3 with ParametersWithUKM

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

the class CryptoProWrapEngine method init.

public void init(boolean forWrapping, CipherParameters param) {
    if (param instanceof ParametersWithRandom) {
        ParametersWithRandom pr = (ParametersWithRandom) param;
        param = pr.getParameters();
    }
    ParametersWithUKM pU = (ParametersWithUKM) param;
    byte[] sBox = null;
    KeyParameter kParam;
    if (pU.getParameters() instanceof ParametersWithSBox) {
        kParam = (KeyParameter) ((ParametersWithSBox) pU.getParameters()).getParameters();
        sBox = ((ParametersWithSBox) pU.getParameters()).getSBox();
    } else {
        kParam = (KeyParameter) pU.getParameters();
    }
    kParam = new KeyParameter(cryptoProDiversify(kParam.getKey(), pU.getUKM(), sBox));
    if (sBox != null) {
        super.init(forWrapping, new ParametersWithUKM(new ParametersWithSBox(kParam, sBox), pU.getUKM()));
    } else {
        super.init(forWrapping, new ParametersWithUKM(kParam, pU.getUKM()));
    }
}
Also used : ParametersWithUKM(com.github.zhenwei.core.crypto.params.ParametersWithUKM) ParametersWithSBox(com.github.zhenwei.core.crypto.params.ParametersWithSBox) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter) ParametersWithRandom(com.github.zhenwei.core.crypto.params.ParametersWithRandom)

Example 4 with ParametersWithUKM

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

the class ECVKOAgreement method init.

public void init(CipherParameters key) {
    ParametersWithUKM p = (ParametersWithUKM) key;
    this.key = (ECPrivateKeyParameters) p.getParameters();
    this.ukm = toInteger(p.getUKM());
}
Also used : ParametersWithUKM(com.github.zhenwei.core.crypto.params.ParametersWithUKM)

Aggregations

ParametersWithUKM (com.github.zhenwei.core.crypto.params.ParametersWithUKM)4 ParametersWithRandom (com.github.zhenwei.core.crypto.params.ParametersWithRandom)3 KeyParameter (com.github.zhenwei.core.crypto.params.KeyParameter)2 ParametersWithIV (com.github.zhenwei.core.crypto.params.ParametersWithIV)2 ParametersWithSBox (com.github.zhenwei.core.crypto.params.ParametersWithSBox)2 CipherParameters (com.github.zhenwei.core.crypto.CipherParameters)1 InvalidCipherTextException (com.github.zhenwei.core.crypto.InvalidCipherTextException)1 GOST28147WrapParameterSpec (com.github.zhenwei.provider.jcajce.spec.GOST28147WrapParameterSpec)1 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)1 InvalidKeyException (java.security.InvalidKeyException)1 InvalidParameterException (java.security.InvalidParameterException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 NoSuchProviderException (java.security.NoSuchProviderException)1 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)1 BadPaddingException (javax.crypto.BadPaddingException)1 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)1 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)1 ShortBufferException (javax.crypto.ShortBufferException)1 IvParameterSpec (javax.crypto.spec.IvParameterSpec)1 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)1