use of org.bouncycastle.asn1.pkcs.KeyDerivationFunc in project xwiki-commons by xwiki.
the class BcPKCS5S2KeyDerivationFunctionFactory method getInstance.
@Override
public KeyDerivationFunction getInstance(KeyDerivationFunctionParameters params) {
if (!(params instanceof PBKDF2Parameters)) {
throw new IllegalArgumentException("Invalid parameter used for PKCS5S2 function: " + params.getClass().getName());
}
PBKDF2Parameters kdfParams = (PBKDF2Parameters) params;
PKCS5S2ParametersGenerator generator;
BcDigestFactory factory = null;
if (kdfParams.getPseudoRandomFuntionHint() != null) {
factory = this.getDigestFactory(kdfParams.getPseudoRandomFuntionHint());
generator = new PKCS5S2ParametersGenerator(factory.getDigestInstance());
} else {
generator = new PKCS5S2ParametersGenerator();
}
return new AbstractBcPBKDF2(generator, (PBKDF2Parameters) params, (factory != null) ? toHmacAlgId(factory.getAlgorithmIdentifier()) : HMAC_SHA1) {
@Override
public KeyDerivationFunc getKeyDerivationFunction() {
PBKDF2Parameters parameters = (PBKDF2Parameters) getParameters();
AlgorithmIdentifier algId = getPRFAlgorithmIdentifier();
return new KeyDerivationFunc(PKCSObjectIdentifiers.id_PBKDF2, (isKeySizeOverwritten()) ? new PBKDF2Params(parameters.getSalt(), parameters.getIterationCount(), algId) : new PBKDF2Params(parameters.getSalt(), parameters.getIterationCount(), parameters.getKeySize(), algId));
}
};
}
use of org.bouncycastle.asn1.pkcs.KeyDerivationFunc in project xwiki-commons by xwiki.
the class BcPKCS5S2KeyDerivationFunctionFactory method getInstance.
@Override
public KeyDerivationFunction getInstance(ASN1Encodable parameters) {
KeyDerivationFunc kdf = KeyDerivationFunc.getInstance(parameters);
if (!kdf.getAlgorithm().equals(PKCSObjectIdentifiers.id_PBKDF2)) {
throw new IllegalArgumentException("Illegal algorithm identifier for PBKDF2: " + kdf.getAlgorithm().getId());
}
PBKDF2Params params = PBKDF2Params.getInstance(kdf.getParameters());
return getInstance(new PBKDF2Parameters((params.getKeyLength() != null) ? params.getKeyLength().intValue() : -1, params.getIterationCount().intValue(), params.getSalt(), toDigestHint(params.getPseudoRandomFunctionIdentifier())));
}
use of org.bouncycastle.asn1.pkcs.KeyDerivationFunc 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;
}
use of org.bouncycastle.asn1.pkcs.KeyDerivationFunc in project xwiki-commons by xwiki.
the class AbstractBcPBES2Cipher method getPBEParameters.
@Override
public AlgorithmIdentifier getPBEParameters() throws IOException {
KeyDerivationFunc kdfParams;
if (getKeyDerivationFunction() instanceof AbstractBcKDF) {
kdfParams = ((AbstractBcKDF) getKeyDerivationFunction()).getKeyDerivationFunction();
} else {
kdfParams = KeyDerivationFunc.getInstance(getKeyDerivationFunction().getEncoded());
}
EncryptionScheme scheme = getScheme(getParameters());
return new AlgorithmIdentifier(PKCSObjectIdentifiers.id_PBES2, new PBES2Parameters(kdfParams, scheme));
}
use of org.bouncycastle.asn1.pkcs.KeyDerivationFunc 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()));
}
Aggregations