Search in sources :

Example 1 with AEADParameters

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

the class KGCMBlockCipher method init.

public void init(boolean forEncryption, CipherParameters params) throws IllegalArgumentException {
    this.forEncryption = forEncryption;
    KeyParameter engineParam;
    if (params instanceof AEADParameters) {
        AEADParameters param = (AEADParameters) params;
        byte[] iv = param.getNonce();
        int diff = this.iv.length - iv.length;
        Arrays.fill(this.iv, (byte) 0);
        System.arraycopy(iv, 0, this.iv, diff, iv.length);
        initialAssociatedText = param.getAssociatedText();
        int macSizeBits = param.getMacSize();
        if (macSizeBits < MIN_MAC_BITS || macSizeBits > (blockSize << 3) || (macSizeBits & 7) != 0) {
            throw new IllegalArgumentException("Invalid value for MAC size: " + macSizeBits);
        }
        macSize = macSizeBits >>> 3;
        engineParam = param.getKey();
        if (initialAssociatedText != null) {
            processAADBytes(initialAssociatedText, 0, initialAssociatedText.length);
        }
    } else if (params instanceof ParametersWithIV) {
        ParametersWithIV param = (ParametersWithIV) params;
        byte[] iv = param.getIV();
        int diff = this.iv.length - iv.length;
        Arrays.fill(this.iv, (byte) 0);
        System.arraycopy(iv, 0, this.iv, diff, iv.length);
        initialAssociatedText = null;
        // Set default mac size
        macSize = blockSize;
        engineParam = (KeyParameter) param.getParameters();
    } else {
        throw new IllegalArgumentException("Invalid parameter passed");
    }
    // TODO Nonce re-use check (sample code from GCMBlockCipher)
    // if (forEncryption)
    // {
    // if (nonce != null && Arrays.areEqual(nonce, newNonce))
    // {
    // if (keyParam == null)
    // {
    // throw new IllegalArgumentException("cannot reuse nonce for GCM encryption");
    // }
    // if (lastKey != null && Arrays.areEqual(lastKey, keyParam.getKey()))
    // {
    // throw new IllegalArgumentException("cannot reuse nonce for GCM encryption");
    // }
    // }
    // }
    this.macBlock = new byte[blockSize];
    ctrEngine.init(true, new ParametersWithIV(engineParam, this.iv));
    engine.init(true, engineParam);
}
Also used : ParametersWithIV(com.github.zhenwei.core.crypto.params.ParametersWithIV) AEADParameters(com.github.zhenwei.core.crypto.params.AEADParameters) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter)

Example 2 with AEADParameters

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

the class OCBBlockCipher method init.

public void init(boolean forEncryption, CipherParameters parameters) throws IllegalArgumentException {
    boolean oldForEncryption = this.forEncryption;
    this.forEncryption = forEncryption;
    this.macBlock = null;
    KeyParameter keyParameter;
    byte[] N;
    if (parameters instanceof AEADParameters) {
        AEADParameters aeadParameters = (AEADParameters) parameters;
        N = aeadParameters.getNonce();
        initialAssociatedText = aeadParameters.getAssociatedText();
        int macSizeBits = aeadParameters.getMacSize();
        if (macSizeBits < 64 || macSizeBits > 128 || macSizeBits % 8 != 0) {
            throw new IllegalArgumentException("Invalid value for MAC size: " + macSizeBits);
        }
        macSize = macSizeBits / 8;
        keyParameter = aeadParameters.getKey();
    } else if (parameters instanceof ParametersWithIV) {
        ParametersWithIV parametersWithIV = (ParametersWithIV) parameters;
        N = parametersWithIV.getIV();
        initialAssociatedText = null;
        macSize = 16;
        keyParameter = (KeyParameter) parametersWithIV.getParameters();
    } else {
        throw new IllegalArgumentException("invalid parameters passed to OCB");
    }
    this.hashBlock = new byte[16];
    this.mainBlock = new byte[forEncryption ? BLOCK_SIZE : (BLOCK_SIZE + macSize)];
    if (N == null) {
        N = new byte[0];
    }
    if (N.length > 15) {
        throw new IllegalArgumentException("IV must be no more than 15 bytes");
    }
    if (keyParameter != null) {
        // hashCipher always used in forward mode
        hashCipher.init(true, keyParameter);
        mainCipher.init(forEncryption, keyParameter);
        KtopInput = null;
    } else if (oldForEncryption != forEncryption) {
        throw new IllegalArgumentException("cannot change encrypting state without providing key.");
    }
    this.L_Asterisk = new byte[16];
    hashCipher.processBlock(L_Asterisk, 0, L_Asterisk, 0);
    this.L_Dollar = OCB_double(L_Asterisk);
    this.L = new Vector();
    this.L.addElement(OCB_double(L_Dollar));
    /*
     * NONCE-DEPENDENT AND PER-ENCRYPTION/DECRYPTION INITIALISATION
     */
    int bottom = processNonce(N);
    int bits = bottom % 8, bytes = bottom / 8;
    if (bits == 0) {
        System.arraycopy(Stretch, bytes, OffsetMAIN_0, 0, 16);
    } else {
        for (int i = 0; i < 16; ++i) {
            int b1 = Stretch[bytes] & 0xff;
            int b2 = Stretch[++bytes] & 0xff;
            this.OffsetMAIN_0[i] = (byte) ((b1 << bits) | (b2 >>> (8 - bits)));
        }
    }
    this.hashBlockPos = 0;
    this.mainBlockPos = 0;
    this.hashBlockCount = 0;
    this.mainBlockCount = 0;
    this.OffsetHASH = new byte[16];
    this.Sum = new byte[16];
    System.arraycopy(this.OffsetMAIN_0, 0, this.OffsetMAIN, 0, 16);
    this.Checksum = new byte[16];
    if (initialAssociatedText != null) {
        processAADBytes(initialAssociatedText, 0, initialAssociatedText.length);
    }
}
Also used : ParametersWithIV(com.github.zhenwei.core.crypto.params.ParametersWithIV) AEADParameters(com.github.zhenwei.core.crypto.params.AEADParameters) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter) Vector(java.util.Vector)

Example 3 with AEADParameters

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

the class CCMBlockCipher method init.

public void init(boolean forEncryption, CipherParameters params) throws IllegalArgumentException {
    this.forEncryption = forEncryption;
    CipherParameters cipherParameters;
    if (params instanceof AEADParameters) {
        AEADParameters param = (AEADParameters) params;
        nonce = param.getNonce();
        initialAssociatedText = param.getAssociatedText();
        macSize = getMacSize(forEncryption, param.getMacSize());
        cipherParameters = param.getKey();
    } else if (params instanceof ParametersWithIV) {
        ParametersWithIV param = (ParametersWithIV) params;
        nonce = param.getIV();
        initialAssociatedText = null;
        macSize = getMacSize(forEncryption, 64);
        cipherParameters = param.getParameters();
    } else {
        throw new IllegalArgumentException("invalid parameters passed to CCM: " + params.getClass().getName());
    }
    // NOTE: Very basic support for key re-use, but no performance gain from it
    if (cipherParameters != null) {
        keyParam = cipherParameters;
    }
    if (nonce == null || nonce.length < 7 || nonce.length > 13) {
        throw new IllegalArgumentException("nonce must have length from 7 to 13 octets");
    }
    reset();
}
Also used : CipherParameters(com.github.zhenwei.core.crypto.CipherParameters) ParametersWithIV(com.github.zhenwei.core.crypto.params.ParametersWithIV) AEADParameters(com.github.zhenwei.core.crypto.params.AEADParameters)

Example 4 with AEADParameters

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

the class ChaCha20Poly1305 method init.

public void init(boolean forEncryption, CipherParameters params) throws IllegalArgumentException {
    KeyParameter initKeyParam;
    byte[] initNonce;
    CipherParameters chacha20Params;
    if (params instanceof AEADParameters) {
        AEADParameters aeadParams = (AEADParameters) params;
        int macSizeBits = aeadParams.getMacSize();
        if ((MAC_SIZE * 8) != macSizeBits) {
            throw new IllegalArgumentException("Invalid value for MAC size: " + macSizeBits);
        }
        initKeyParam = aeadParams.getKey();
        initNonce = aeadParams.getNonce();
        chacha20Params = new ParametersWithIV(initKeyParam, initNonce);
        this.initialAAD = aeadParams.getAssociatedText();
    } else if (params instanceof ParametersWithIV) {
        ParametersWithIV ivParams = (ParametersWithIV) params;
        initKeyParam = (KeyParameter) ivParams.getParameters();
        initNonce = ivParams.getIV();
        chacha20Params = ivParams;
        this.initialAAD = null;
    } else {
        throw new IllegalArgumentException("invalid parameters passed to ChaCha20Poly1305");
    }
    // Validate key
    if (null == initKeyParam) {
        if (State.UNINITIALIZED == state) {
            throw new IllegalArgumentException("Key must be specified in initial init");
        }
    } else {
        if (KEY_SIZE != initKeyParam.getKey().length) {
            throw new IllegalArgumentException("Key must be 256 bits");
        }
    }
    // Validate nonce
    if (null == initNonce || NONCE_SIZE != initNonce.length) {
        throw new IllegalArgumentException("Nonce must be 96 bits");
    }
    // Check for encryption with reused nonce
    if (State.UNINITIALIZED != state && forEncryption && Arrays.areEqual(nonce, initNonce)) {
        if (null == initKeyParam || Arrays.areEqual(key, initKeyParam.getKey())) {
            throw new IllegalArgumentException("cannot reuse nonce for ChaCha20Poly1305 encryption");
        }
    }
    if (null != initKeyParam) {
        System.arraycopy(initKeyParam.getKey(), 0, key, 0, KEY_SIZE);
    }
    System.arraycopy(initNonce, 0, nonce, 0, NONCE_SIZE);
    chacha20.init(true, chacha20Params);
    this.state = forEncryption ? State.ENC_INIT : State.DEC_INIT;
    reset(true, false);
}
Also used : CipherParameters(com.github.zhenwei.core.crypto.CipherParameters) ParametersWithIV(com.github.zhenwei.core.crypto.params.ParametersWithIV) AEADParameters(com.github.zhenwei.core.crypto.params.AEADParameters) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter)

Example 5 with AEADParameters

use of com.github.zhenwei.core.crypto.params.AEADParameters 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)

Aggregations

AEADParameters (com.github.zhenwei.core.crypto.params.AEADParameters)13 ParametersWithIV (com.github.zhenwei.core.crypto.params.ParametersWithIV)13 KeyParameter (com.github.zhenwei.core.crypto.params.KeyParameter)10 CipherParameters (com.github.zhenwei.core.crypto.CipherParameters)6 RC2Parameters (com.github.zhenwei.core.crypto.params.RC2Parameters)3 PKCS12Key (com.github.zhenwei.provider.jcajce.PKCS12Key)2 AEADParameterSpec (com.github.zhenwei.provider.jcajce.spec.AEADParameterSpec)2 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)2 InvalidKeyException (java.security.InvalidKeyException)2 ASN1Null (com.github.zhenwei.core.asn1.ASN1Null)1 ASN1ObjectIdentifier (com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)1 ASN1Primitive (com.github.zhenwei.core.asn1.ASN1Primitive)1 CAST5CBCParameters (com.github.zhenwei.core.asn1.misc.CAST5CBCParameters)1 RC2CBCParameter (com.github.zhenwei.core.asn1.pkcs.RC2CBCParameter)1 BufferedBlockCipher (com.github.zhenwei.core.crypto.BufferedBlockCipher)1 DataLengthException (com.github.zhenwei.core.crypto.DataLengthException)1 InvalidCipherTextException (com.github.zhenwei.core.crypto.InvalidCipherTextException)1 OutputLengthException (com.github.zhenwei.core.crypto.OutputLengthException)1 StreamCipher (com.github.zhenwei.core.crypto.StreamCipher)1 RC4Engine (com.github.zhenwei.core.crypto.engines.RC4Engine)1