Search in sources :

Example 1 with KeyParameter

use of org.xwiki.crypto.params.cipher.symmetric.KeyParameter in project xwiki-commons by xwiki.

the class BcPKCS5S2KeyDerivationFunctionFactoryTest method pbkdf2KeyWithRandomSaltAndIterationCount.

@Test
public void pbkdf2KeyWithRandomSaltAndIterationCount() throws Exception {
    byte[] password = PasswordToByteConverter.convert("password");
    PBKDF2Parameters kdfParam1 = new PBKDF2Parameters(16);
    KeyParameter params1 = getKDFInstance(kdfParam1).derive(password);
    PBKDF2Parameters kdfParam2 = new PBKDF2Parameters(16);
    KeyParameter params2 = getKDFInstance(kdfParam2).derive(password);
    assertThat(params1.getKey(), not(equalTo(params2.getKey())));
    assertThat(params1.getKey().length, equalTo(16));
    assertThat(kdfParam1.getSalt(), not(equalTo(kdfParam2.getSalt())));
    assertThat(kdfParam1.getIterationCount(), not(equalTo(kdfParam2.getIterationCount())));
}
Also used : PBKDF2Parameters(org.xwiki.crypto.password.params.PBKDF2Parameters) KeyParameter(org.xwiki.crypto.params.cipher.symmetric.KeyParameter) Test(org.junit.Test)

Example 2 with KeyParameter

use of org.xwiki.crypto.params.cipher.symmetric.KeyParameter in project xwiki-commons by xwiki.

the class BcPBES2Rc2CipherFactory method getInstance.

@Override
public PasswordBasedCipher getInstance(boolean forEncryption, SymmetricCipherParameters password, KeyDerivationFunction kdf) {
    KeyWithIVParameters params;
    if (password instanceof KeyWithIVParameters) {
        KeyParameter passkey = ((KeyWithIVParameters) password).getKeyParameter();
        if (passkey instanceof RC2KeyParameters) {
            params = new KeyWithIVParameters(new RC2KeyParameters(kdf.derive(passkey.getKey()).getKey(), ((RC2KeyParameters) passkey).getEffectiveBits()), ((KeyWithIVParameters) password).getIV());
        } else {
            params = new KeyWithIVParameters(kdf.derive(((KeyWithIVParameters) password).getKey()), ((KeyWithIVParameters) password).getIV());
        }
    } else if (password instanceof RC2KeyParameters) {
        params = kdf.derive(((KeyParameter) password).getKey(), getIVSize());
        params = new KeyWithIVParameters(new RC2KeyParameters(params.getKey(), ((RC2KeyParameters) password).getEffectiveBits()), params.getIV());
    } else if (password instanceof KeyParameter) {
        params = kdf.derive(((KeyParameter) password).getKey(), getIVSize());
    } else {
        throw new IllegalArgumentException("Invalid cipher parameters for RC2 password based cipher: " + password.getClass().getName());
    }
    // ensure RC2Version is computable
    getRC2Version(params);
    return getPasswordBasedCipher(forEncryption, kdf, params);
}
Also used : KeyParameter(org.xwiki.crypto.params.cipher.symmetric.KeyParameter) RC2KeyParameters(org.xwiki.crypto.params.cipher.symmetric.RC2KeyParameters) KeyWithIVParameters(org.xwiki.crypto.params.cipher.symmetric.KeyWithIVParameters)

Example 3 with KeyParameter

use of org.xwiki.crypto.params.cipher.symmetric.KeyParameter in project xwiki-commons by xwiki.

the class BcPKCS5S2KeyDerivationFunctionFactoryTest method pbkdf2KeyWithRandomSalt.

@Test
public void pbkdf2KeyWithRandomSalt() throws Exception {
    byte[] password = PasswordToByteConverter.convert("password");
    PBKDF2Parameters kdfParam1 = new PBKDF2Parameters(32, 5);
    KeyParameter params1 = getKDFInstance(kdfParam1).derive(password);
    PBKDF2Parameters kdfParam2 = new PBKDF2Parameters(32, 5);
    KeyParameter params2 = getKDFInstance(kdfParam2).derive(password);
    assertThat(params1.getKey(), not(equalTo(params2.getKey())));
    assertThat(params1.getKey().length, equalTo(32));
    assertThat(kdfParam1.getIterationCount(), equalTo(kdfParam2.getIterationCount()));
    assertThat(kdfParam1.getSalt(), not(equalTo(kdfParam2.getSalt())));
}
Also used : PBKDF2Parameters(org.xwiki.crypto.password.params.PBKDF2Parameters) KeyParameter(org.xwiki.crypto.params.cipher.symmetric.KeyParameter) Test(org.junit.Test)

Example 4 with KeyParameter

use of org.xwiki.crypto.params.cipher.symmetric.KeyParameter in project xwiki-commons by xwiki.

the class BcPKCS5S2KeyDerivationFunctionFactoryTest method pbkdf2KeyWithRandomIterationCount.

@Test
public void pbkdf2KeyWithRandomIterationCount() throws Exception {
    byte[] salt = Hex.decode("12 34 56 78 78 56 34 12");
    byte[] password = PasswordToByteConverter.convert("password");
    PBKDF2Parameters kdfParam1 = new PBKDF2Parameters(24, salt);
    KeyParameter params1 = getKDFInstance(kdfParam1).derive(password);
    PBKDF2Parameters kdfParam2 = new PBKDF2Parameters(24, salt);
    KeyParameter params2 = getKDFInstance(kdfParam2).derive(password);
    assertThat(params1.getKey(), not(equalTo(params2.getKey())));
    assertThat(params1.getKey().length, equalTo(24));
    assertThat(kdfParam1.getSalt(), equalTo(kdfParam2.getSalt()));
    assertThat(kdfParam1.getIterationCount(), not(equalTo(kdfParam2.getIterationCount())));
}
Also used : PBKDF2Parameters(org.xwiki.crypto.password.params.PBKDF2Parameters) KeyParameter(org.xwiki.crypto.params.cipher.symmetric.KeyParameter) Test(org.junit.Test)

Example 5 with KeyParameter

use of org.xwiki.crypto.params.cipher.symmetric.KeyParameter in project xwiki-commons by xwiki.

the class BcPBES2Rc2CipherFactory method getRC2Version.

private int getRC2Version(KeyWithIVParameters parameters) {
    KeyParameter keyParams = parameters.getKeyParameter();
    int keySize;
    if (keyParams instanceof RC2KeyParameters) {
        keySize = ((RC2KeyParameters) keyParams).getEffectiveBits();
    } else {
        keySize = keyParams.getKey().length * 8;
    }
    switch(keySize) {
        case 40:
            return 160;
        case 64:
            return 120;
        case 128:
            return 58;
        default:
            if (keySize < 256) {
                throw new IllegalArgumentException("Invalid cipher key size for PBES2 RC2 password based cipher: " + keySize + " bits. Valid key size are 40, 64, 128 and 256 and more.");
            }
            return keySize;
    }
}
Also used : KeyParameter(org.xwiki.crypto.params.cipher.symmetric.KeyParameter) RC2KeyParameters(org.xwiki.crypto.params.cipher.symmetric.RC2KeyParameters)

Aggregations

KeyParameter (org.xwiki.crypto.params.cipher.symmetric.KeyParameter)6 Test (org.junit.Test)3 RC2KeyParameters (org.xwiki.crypto.params.cipher.symmetric.RC2KeyParameters)3 PBKDF2Parameters (org.xwiki.crypto.password.params.PBKDF2Parameters)3 KeyWithIVParameters (org.xwiki.crypto.params.cipher.symmetric.KeyWithIVParameters)2 BigInteger (java.math.BigInteger)1