Search in sources :

Example 1 with ParametersWithIV

use of org.bouncycastle.crypto.params.ParametersWithIV in project robovm by robovm.

the class BaseMac method engineInit.

protected void engineInit(Key key, AlgorithmParameterSpec params) throws InvalidKeyException, InvalidAlgorithmParameterException {
    CipherParameters param;
    if (key == null) {
        throw new InvalidKeyException("key is null");
    }
    if (key instanceof BCPBEKey) {
        BCPBEKey k = (BCPBEKey) key;
        if (k.getParam() != null) {
            param = k.getParam();
        } else if (params instanceof PBEParameterSpec) {
            param = PBE.Util.makePBEMacParameters(k, params);
        } else {
            throw new InvalidAlgorithmParameterException("PBE requires PBE parameters to be set.");
        }
    } else if (params instanceof IvParameterSpec) {
        param = new ParametersWithIV(new KeyParameter(key.getEncoded()), ((IvParameterSpec) params).getIV());
    } else if (params == null) {
        param = new KeyParameter(key.getEncoded());
    } else {
        throw new InvalidAlgorithmParameterException("unknown parameter type.");
    }
    macEngine.init(param);
}
Also used : CipherParameters(org.bouncycastle.crypto.CipherParameters) ParametersWithIV(org.bouncycastle.crypto.params.ParametersWithIV) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) IvParameterSpec(javax.crypto.spec.IvParameterSpec) InvalidKeyException(java.security.InvalidKeyException) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Example 2 with ParametersWithIV

use of org.bouncycastle.crypto.params.ParametersWithIV in project robovm by robovm.

the class DESedeWrapEngine method wrap.

/**
    * Method wrap
    *
    * @param in
    * @param inOff
    * @param inLen
    * @return the wrapped bytes.
    */
public byte[] wrap(byte[] in, int inOff, int inLen) {
    if (!forWrapping) {
        throw new IllegalStateException("Not initialized for wrapping");
    }
    byte[] keyToBeWrapped = new byte[inLen];
    System.arraycopy(in, inOff, keyToBeWrapped, 0, inLen);
    // Compute the CMS Key Checksum, (section 5.6.1), call this CKS.
    byte[] CKS = calculateCMSKeyChecksum(keyToBeWrapped);
    // Let WKCKS = WK || CKS where || is concatenation.
    byte[] WKCKS = new byte[keyToBeWrapped.length + CKS.length];
    System.arraycopy(keyToBeWrapped, 0, WKCKS, 0, keyToBeWrapped.length);
    System.arraycopy(CKS, 0, WKCKS, keyToBeWrapped.length, CKS.length);
    // Encrypt WKCKS in CBC mode using KEK as the key and IV as the
    // initialization vector. Call the results TEMP1.
    int blockSize = engine.getBlockSize();
    if (WKCKS.length % blockSize != 0) {
        throw new IllegalStateException("Not multiple of block length");
    }
    engine.init(true, paramPlusIV);
    byte[] TEMP1 = new byte[WKCKS.length];
    for (int currentBytePos = 0; currentBytePos != WKCKS.length; currentBytePos += blockSize) {
        engine.processBlock(WKCKS, currentBytePos, TEMP1, currentBytePos);
    }
    // Let TEMP2 = IV || TEMP1.
    byte[] TEMP2 = new byte[this.iv.length + TEMP1.length];
    System.arraycopy(this.iv, 0, TEMP2, 0, this.iv.length);
    System.arraycopy(TEMP1, 0, TEMP2, this.iv.length, TEMP1.length);
    // Reverse the order of the octets in TEMP2 and call the result TEMP3.
    byte[] TEMP3 = reverse(TEMP2);
    // Encrypt TEMP3 in CBC mode using the KEK and an initialization vector
    // of 0x 4a dd a2 2c 79 e8 21 05. The resulting cipher text is the desired
    // result. It is 40 octets long if a 168 bit key is being wrapped.
    ParametersWithIV param2 = new ParametersWithIV(this.param, IV2);
    this.engine.init(true, param2);
    for (int currentBytePos = 0; currentBytePos != TEMP3.length; currentBytePos += blockSize) {
        engine.processBlock(TEMP3, currentBytePos, TEMP3, currentBytePos);
    }
    return TEMP3;
}
Also used : ParametersWithIV(org.bouncycastle.crypto.params.ParametersWithIV)

Example 3 with ParametersWithIV

use of org.bouncycastle.crypto.params.ParametersWithIV in project robovm by robovm.

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.
     * @exception 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(org.bouncycastle.crypto.params.ParametersWithIV) KeyParameter(org.bouncycastle.crypto.params.KeyParameter)

Example 4 with ParametersWithIV

use of org.bouncycastle.crypto.params.ParametersWithIV in project robovm by robovm.

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(org.bouncycastle.crypto.params.ParametersWithIV) KeyParameter(org.bouncycastle.crypto.params.KeyParameter)

Example 5 with ParametersWithIV

use of org.bouncycastle.crypto.params.ParametersWithIV in project robovm by robovm.

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(org.bouncycastle.crypto.params.ParametersWithIV) KeyParameter(org.bouncycastle.crypto.params.KeyParameter)

Aggregations

ParametersWithIV (org.bouncycastle.crypto.params.ParametersWithIV)42 KeyParameter (org.bouncycastle.crypto.params.KeyParameter)21 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)9 IvParameterSpec (javax.crypto.spec.IvParameterSpec)9 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)9 CipherParameters (org.bouncycastle.crypto.CipherParameters)9 InvalidKeyException (java.security.InvalidKeyException)7 SecureRandom (java.security.SecureRandom)7 InvalidCipherTextException (org.bouncycastle.crypto.InvalidCipherTextException)6 SecretKey (javax.crypto.SecretKey)5 CBCBlockCipher (org.bouncycastle.crypto.modes.CBCBlockCipher)4 AEADParameters (org.bouncycastle.crypto.params.AEADParameters)4 ParametersWithRandom (org.bouncycastle.crypto.params.ParametersWithRandom)4 InvalidParameterException (java.security.InvalidParameterException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 BadPaddingException (javax.crypto.BadPaddingException)2 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)2 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)2 ShortBufferException (javax.crypto.ShortBufferException)2 BlockCipher (org.bouncycastle.crypto.BlockCipher)2