Search in sources :

Example 56 with KeyParameter

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

the class BrokenJCEBlockCipher method engineInit.

protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    CipherParameters param;
    // 
    if (key instanceof BCPBEKey) {
        param = Util.makePBEParameters((BCPBEKey) key, params, pbeType, pbeHash, cipher.getUnderlyingCipher().getAlgorithmName(), pbeKeySize, pbeIvSize);
        if (pbeIvSize != 0) {
            ivParam = (ParametersWithIV) param;
        }
    } else if (params == null) {
        param = new KeyParameter(key.getEncoded());
    } else if (params instanceof IvParameterSpec) {
        if (ivLength != 0) {
            param = new ParametersWithIV(new KeyParameter(key.getEncoded()), ((IvParameterSpec) params).getIV());
            ivParam = (ParametersWithIV) param;
        } else {
            param = new KeyParameter(key.getEncoded());
        }
    } else if (params instanceof RC2ParameterSpec) {
        RC2ParameterSpec rc2Param = (RC2ParameterSpec) params;
        param = new RC2Parameters(key.getEncoded(), ((RC2ParameterSpec) params).getEffectiveKeyBits());
        if (rc2Param.getIV() != null && ivLength != 0) {
            param = new ParametersWithIV(param, rc2Param.getIV());
            ivParam = (ParametersWithIV) param;
        }
    } else if (params instanceof RC5ParameterSpec) {
        RC5ParameterSpec rc5Param = (RC5ParameterSpec) params;
        param = new RC5Parameters(key.getEncoded(), ((RC5ParameterSpec) params).getRounds());
        if (rc5Param.getWordSize() != 32) {
            throw new IllegalArgumentException("can only accept RC5 word size 32 (at the moment...)");
        }
        if ((rc5Param.getIV() != null) && (ivLength != 0)) {
            param = new ParametersWithIV(param, rc5Param.getIV());
            ivParam = (ParametersWithIV) param;
        }
    } else {
        throw new InvalidAlgorithmParameterException("unknown parameter type.");
    }
    if ((ivLength != 0) && !(param instanceof ParametersWithIV)) {
        if (random == null) {
            random = CryptoServicesRegistrar.getSecureRandom();
        }
        if ((opmode == Cipher.ENCRYPT_MODE) || (opmode == Cipher.WRAP_MODE)) {
            byte[] iv = new byte[ivLength];
            random.nextBytes(iv);
            param = new ParametersWithIV(param, iv);
            ivParam = (ParametersWithIV) param;
        } else {
            throw new InvalidAlgorithmParameterException("no IV set when one expected");
        }
    }
    switch(opmode) {
        case Cipher.ENCRYPT_MODE:
        case Cipher.WRAP_MODE:
            cipher.init(true, param);
            break;
        case Cipher.DECRYPT_MODE:
        case Cipher.UNWRAP_MODE:
            cipher.init(false, param);
            break;
        default:
            System.out.println("eeek!");
    }
}
Also used : CipherParameters(com.github.zhenwei.core.crypto.CipherParameters) ParametersWithIV(com.github.zhenwei.core.crypto.params.ParametersWithIV) RC2Parameters(com.github.zhenwei.core.crypto.params.RC2Parameters) RC5ParameterSpec(javax.crypto.spec.RC5ParameterSpec) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) RC5Parameters(com.github.zhenwei.core.crypto.params.RC5Parameters) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter) BCPBEKey(com.github.zhenwei.provider.jcajce.provider.symmetric.util.BCPBEKey) IvParameterSpec(javax.crypto.spec.IvParameterSpec) RC2ParameterSpec(javax.crypto.spec.RC2ParameterSpec)

Example 57 with KeyParameter

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

the class ChaCha20Poly1305 method initMAC.

private void initMAC() {
    byte[] firstBlock = new byte[64];
    try {
        chacha20.processBytes(firstBlock, 0, 64, firstBlock, 0);
        poly1305.init(new KeyParameter(firstBlock, 0, 32));
    } finally {
        Arrays.clear(firstBlock);
    }
}
Also used : KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter)

Example 58 with KeyParameter

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

the class GCMBlockCipher method init.

/**
 * NOTE: MAC sizes from 32 bits to 128 bits (must be a multiple of 8) are supported. The default
 * is 128 bits. Sizes less than 96 are not recommended, but are supported for specialized
 * applications.
 */
public void init(boolean forEncryption, CipherParameters params) throws IllegalArgumentException {
    this.forEncryption = forEncryption;
    this.macBlock = null;
    this.initialised = true;
    KeyParameter keyParam;
    byte[] newNonce = null;
    if (params instanceof AEADParameters) {
        AEADParameters param = (AEADParameters) params;
        newNonce = param.getNonce();
        initialAssociatedText = param.getAssociatedText();
        int macSizeBits = param.getMacSize();
        if (macSizeBits < 32 || macSizeBits > 128 || macSizeBits % 8 != 0) {
            throw new IllegalArgumentException("Invalid value for MAC size: " + macSizeBits);
        }
        macSize = macSizeBits / 8;
        keyParam = param.getKey();
    } else if (params instanceof ParametersWithIV) {
        ParametersWithIV param = (ParametersWithIV) params;
        newNonce = param.getIV();
        initialAssociatedText = null;
        macSize = 16;
        keyParam = (KeyParameter) param.getParameters();
    } else {
        throw new IllegalArgumentException("invalid parameters passed to GCM");
    }
    int bufLength = forEncryption ? BLOCK_SIZE : (BLOCK_SIZE + macSize);
    this.bufBlock = new byte[bufLength];
    if (newNonce == null || newNonce.length < 1) {
        throw new IllegalArgumentException("IV must be at least 1 byte");
    }
    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");
            }
        }
    }
    nonce = newNonce;
    if (keyParam != null) {
        lastKey = keyParam.getKey();
    }
    // if keyParam is null we're reusing the last key.
    if (keyParam != null) {
        cipher.init(true, keyParam);
        this.H = new byte[BLOCK_SIZE];
        cipher.processBlock(H, 0, H, 0);
        // GCMMultiplier tables don't change unless the key changes (and are expensive to init)
        multiplier.init(H);
        exp = null;
    } else if (this.H == null) {
        throw new IllegalArgumentException("Key must be specified in initial init");
    }
    this.J0 = new byte[BLOCK_SIZE];
    if (nonce.length == 12) {
        System.arraycopy(nonce, 0, J0, 0, nonce.length);
        this.J0[BLOCK_SIZE - 1] = 0x01;
    } else {
        gHASH(J0, nonce, nonce.length);
        byte[] X = new byte[BLOCK_SIZE];
        Pack.longToBigEndian((long) nonce.length * 8, X, 8);
        gHASHBlock(J0, X);
    }
    this.S = new byte[BLOCK_SIZE];
    this.S_at = new byte[BLOCK_SIZE];
    this.S_atPre = new byte[BLOCK_SIZE];
    this.atBlock = new byte[BLOCK_SIZE];
    this.atBlockPos = 0;
    this.atLength = 0;
    this.atLengthPre = 0;
    this.counter = Arrays.clone(J0);
    // page 8, len(P) <= 2^39 - 256, 1 block used by tag but done on J0
    this.blocksRemaining = -2;
    this.bufOff = 0;
    this.totalLength = 0;
    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)

Example 59 with KeyParameter

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

the class GCMSIVBlockCipher method init.

public void init(final boolean pEncrypt, final CipherParameters cipherParameters) throws IllegalArgumentException {
    /* Set defaults */
    byte[] myInitialAEAD = null;
    byte[] myNonce = null;
    KeyParameter myKey = null;
    /* Access parameters */
    if (cipherParameters instanceof AEADParameters) {
        final AEADParameters myAEAD = (AEADParameters) cipherParameters;
        myInitialAEAD = myAEAD.getAssociatedText();
        myNonce = myAEAD.getNonce();
        myKey = myAEAD.getKey();
    } else if (cipherParameters instanceof ParametersWithIV) {
        final ParametersWithIV myParms = (ParametersWithIV) cipherParameters;
        myNonce = myParms.getIV();
        myKey = (KeyParameter) myParms.getParameters();
    } else {
        throw new IllegalArgumentException("invalid parameters passed to GCM-SIV");
    }
    /* Check nonceSize */
    if (myNonce == null || myNonce.length != NONCELEN) {
        throw new IllegalArgumentException("Invalid nonce");
    }
    /* Check keysize */
    if (myKey == null || (myKey.getKey().length != BUFLEN && myKey.getKey().length != (BUFLEN << 1))) {
        throw new IllegalArgumentException("Invalid key");
    }
    /* Reset details */
    forEncryption = pEncrypt;
    theInitialAEAD = myInitialAEAD;
    theNonce = myNonce;
    /* Initialise the keys */
    deriveKeys(myKey);
    resetStreams();
}
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 60 with KeyParameter

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

the class HKDFBytesGenerator method extract.

/**
 * Performs the extract part of the key derivation function.
 *
 * @param salt the salt to use
 * @param ikm  the input keying material
 * @return the PRK as KeyParameter
 */
private KeyParameter extract(byte[] salt, byte[] ikm) {
    if (salt == null) {
        // TODO check if hashLen is indeed same as HMAC size
        hMacHash.init(new KeyParameter(new byte[hashLen]));
    } else {
        hMacHash.init(new KeyParameter(salt));
    }
    hMacHash.update(ikm, 0, ikm.length);
    byte[] prk = new byte[hashLen];
    hMacHash.doFinal(prk, 0);
    return new KeyParameter(prk);
}
Also used : 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