Search in sources :

Example 1 with KeyWithIVParameters

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

the class BcPKCS5S2KeyDerivationFunctionFactoryTest method pbkdf2SerializationDeserializationTest.

@Test
public void pbkdf2SerializationDeserializationTest() throws Exception {
    byte[] password = PasswordToByteConverter.convert("password");
    KeyDerivationFunction kdf = getKDFInstance(new PBKDF2Parameters(32, 1000));
    KeyWithIVParameters params = kdf.derive(password, 8);
    KeyDerivationFunction kdf2 = factory.getInstance(kdf.getEncoded());
    KeyWithIVParameters params2 = kdf2.derive(password, 8);
    assertThat(params.getKey(), equalTo(params2.getKey()));
    assertThat(params2.getIV(), equalTo(params2.getIV()));
}
Also used : KeyDerivationFunction(org.xwiki.crypto.password.KeyDerivationFunction) PBKDF2Parameters(org.xwiki.crypto.password.params.PBKDF2Parameters) KeyWithIVParameters(org.xwiki.crypto.params.cipher.symmetric.KeyWithIVParameters) Test(org.junit.Test)

Example 2 with KeyWithIVParameters

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

the class BcPKCS5S2KeyDerivationFunctionFactoryTest method pbkdf2KeyWithIVWithRandomSalt.

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

Example 3 with KeyWithIVParameters

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

the class BcPKCS5S2KeyDerivationFunctionFactoryTest method pbkdf2KeyWithIVWithRandomIterationCount.

@Test
public void pbkdf2KeyWithIVWithRandomIterationCount() 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);
    KeyWithIVParameters params1 = getKDFInstance(kdfParam1).derive(password, 12);
    PBKDF2Parameters kdfParam2 = new PBKDF2Parameters(24, salt);
    KeyWithIVParameters params2 = getKDFInstance(kdfParam2).derive(password, 12);
    assertThat(params1.getKey(), not(equalTo(params2.getKey())));
    assertThat(params1.getKey().length, equalTo(24));
    assertThat(params1.getIV().length, equalTo(12));
    assertThat(kdfParam1.getSalt(), equalTo(kdfParam2.getSalt()));
    assertThat(kdfParam1.getIterationCount(), not(equalTo(kdfParam2.getIterationCount())));
}
Also used : PBKDF2Parameters(org.xwiki.crypto.password.params.PBKDF2Parameters) KeyWithIVParameters(org.xwiki.crypto.params.cipher.symmetric.KeyWithIVParameters) Test(org.junit.Test)

Example 4 with KeyWithIVParameters

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

the class DefaultKeyDerivationFunctionFactoryTest method Pbkdf2DecodingTest.

@Test
public void Pbkdf2DecodingTest() throws Exception {
    byte[] password = PasswordToByteConverter.convert("password");
    byte[] encoded = Base64.decode("MCYGCSqGSIb3DQEFDDAZBBAcO15L0rQ01O2RJ5UhyqCdAgID6AIBIA==");
    KeyDerivationFunction kdf = getKDFInstance(new PBKDF2Parameters(32, 1000, Hex.decode("1c3b5e4bd2b434d4ed91279521caa09d")));
    KeyWithIVParameters params = kdf.derive(password, 8);
    KeyDerivationFunction kdf2 = factory.getInstance(encoded);
    KeyWithIVParameters params2 = kdf2.derive(password, 8);
    assertThat(kdf.getEncoded(), equalTo(encoded));
    assertThat(params.getKey(), equalTo(params2.getKey()));
    assertThat(params2.getIV(), equalTo(params2.getIV()));
}
Also used : KeyDerivationFunction(org.xwiki.crypto.password.KeyDerivationFunction) PBKDF2Parameters(org.xwiki.crypto.password.params.PBKDF2Parameters) KeyWithIVParameters(org.xwiki.crypto.params.cipher.symmetric.KeyWithIVParameters) Test(org.junit.Test)

Example 5 with KeyWithIVParameters

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

the class AbstractBcPBKDF2 method derive.

@Override
public KeyWithIVParameters derive(byte[] password, int ivSize) {
    this.generator.init(password, this.parameters.getSalt(), this.parameters.getIterationCount());
    ParametersWithIV keyParam = (ParametersWithIV) this.generator.generateDerivedParameters(getKeySize() * 8, ivSize * 8);
    return new KeyWithIVParameters(((KeyParameter) keyParam.getParameters()).getKey(), keyParam.getIV());
}
Also used : ParametersWithIV(org.bouncycastle.crypto.params.ParametersWithIV) KeyWithIVParameters(org.xwiki.crypto.params.cipher.symmetric.KeyWithIVParameters)

Aggregations

KeyWithIVParameters (org.xwiki.crypto.params.cipher.symmetric.KeyWithIVParameters)18 Test (org.junit.Test)10 PBKDF2Parameters (org.xwiki.crypto.password.params.PBKDF2Parameters)8 KeyDerivationFunction (org.xwiki.crypto.password.KeyDerivationFunction)5 RC2KeyParameters (org.xwiki.crypto.params.cipher.symmetric.RC2KeyParameters)4 PasswordBasedCipher (org.xwiki.crypto.password.PasswordBasedCipher)3 Cipher (org.xwiki.crypto.cipher.Cipher)2 KeyParameter (org.xwiki.crypto.params.cipher.symmetric.KeyParameter)2 PasswordBasedCipherFactory (org.xwiki.crypto.password.PasswordBasedCipherFactory)2 ScryptParameters (org.xwiki.crypto.password.params.ScryptParameters)2 BigInteger (java.math.BigInteger)1 EncryptedPrivateKeyInfo (javax.crypto.EncryptedPrivateKeyInfo)1 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)1 ParametersWithIV (org.bouncycastle.crypto.params.ParametersWithIV)1