Search in sources :

Example 1 with KeyDerivationFunction

use of org.xwiki.crypto.password.KeyDerivationFunction in project xwiki-commons by xwiki.

the class DefaultKeyDerivationFunctionFactory method getInstance.

@Override
public KeyDerivationFunction getInstance(byte[] encoded) {
    KeyDerivationFunc func = KeyDerivationFunc.getInstance(ASN1Sequence.getInstance(encoded));
    KeyDerivationFunctionFactory factory = getFactory(func.getAlgorithm().getId());
    KeyDerivationFunction kdf = getBcInstance(factory, func);
    if (kdf == null) {
        kdf = factory.getInstance(encoded);
    }
    return kdf;
}
Also used : KeyDerivationFunction(org.xwiki.crypto.password.KeyDerivationFunction) KeyDerivationFunc(org.bouncycastle.asn1.pkcs.KeyDerivationFunc) KeyDerivationFunctionFactory(org.xwiki.crypto.password.KeyDerivationFunctionFactory)

Example 2 with KeyDerivationFunction

use of org.xwiki.crypto.password.KeyDerivationFunction 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 3 with KeyDerivationFunction

use of org.xwiki.crypto.password.KeyDerivationFunction 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 4 with KeyDerivationFunction

use of org.xwiki.crypto.password.KeyDerivationFunction in project xwiki-commons by xwiki.

the class BcPBES2AesCipherFactory method getInstance.

@Override
protected PasswordBasedCipher getInstance(boolean forEncryption, byte[] password, KeyDerivationFunc kdfParams, EncryptionScheme scheme) {
    KeyDerivationFunction kdf = getKeyDerivationFunction(kdfParams);
    // Set key size according to the encryption scheme algorithm used.
    kdf.overrideKeySize(getAESKeySize(scheme.getAlgorithm()));
    return getPasswordBasedCipher(forEncryption, kdf, new KeyWithIVParameters(kdf.derive(password).getKey(), ((ASN1OctetString) scheme.getParameters()).getOctets()));
}
Also used : ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) KeyDerivationFunction(org.xwiki.crypto.password.KeyDerivationFunction) KeyWithIVParameters(org.xwiki.crypto.params.cipher.symmetric.KeyWithIVParameters)

Example 5 with KeyDerivationFunction

use of org.xwiki.crypto.password.KeyDerivationFunction in project xwiki-commons by xwiki.

the class BcScryptKeyDerivationFunctionFactoryTest method scryptSerializationDeserializationTest.

@Test
public void scryptSerializationDeserializationTest() throws Exception {
    byte[] password = PasswordToByteConverter.convert("password");
    KeyDerivationFunction kdf = getKDFInstance(new ScryptParameters(64, 512, 8));
    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) ScryptParameters(org.xwiki.crypto.password.params.ScryptParameters) KeyWithIVParameters(org.xwiki.crypto.params.cipher.symmetric.KeyWithIVParameters) Test(org.junit.Test)

Aggregations

KeyDerivationFunction (org.xwiki.crypto.password.KeyDerivationFunction)8 KeyWithIVParameters (org.xwiki.crypto.params.cipher.symmetric.KeyWithIVParameters)5 Test (org.junit.Test)4 PBKDF2Parameters (org.xwiki.crypto.password.params.PBKDF2Parameters)2 ScryptParameters (org.xwiki.crypto.password.params.ScryptParameters)2 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)1 KeyDerivationFunc (org.bouncycastle.asn1.pkcs.KeyDerivationFunc)1 RC2CBCParameter (org.bouncycastle.asn1.pkcs.RC2CBCParameter)1 KeyDerivationFunctionFactory (org.xwiki.crypto.password.KeyDerivationFunctionFactory)1 RC5CBCParameter (org.xwiki.crypto.password.internal.pbe.RC5CBCParameter)1