use of com.github.zhenwei.core.asn1.misc.ScryptParams in project LinLong-Java by zhenwei1108.
the class BcFKSKeyStoreSpi method generateKey.
private byte[] generateKey(KeyDerivationFunc pbkdAlgorithm, String purpose, char[] password, int defKeySize) throws IOException {
byte[] encPassword = PBEParametersGenerator.PKCS12PasswordToBytes(password);
byte[] differentiator = PBEParametersGenerator.PKCS12PasswordToBytes(purpose.toCharArray());
int keySizeInBytes = defKeySize;
if (MiscObjectIdentifiers.id_scrypt.equals(pbkdAlgorithm.getAlgorithm())) {
ScryptParams params = ScryptParams.getInstance(pbkdAlgorithm.getParameters());
if (params.getKeyLength() != null) {
keySizeInBytes = params.getKeyLength().intValue();
} else if (keySizeInBytes == -1) {
throw new IOException("no keyLength found in ScryptParams");
}
return SCrypt.generate(Arrays.concatenate(encPassword, differentiator), params.getSalt(), params.getCostParameter().intValue(), params.getBlockSize().intValue(), params.getBlockSize().intValue(), keySizeInBytes);
} else if (pbkdAlgorithm.getAlgorithm().equals(PKCSObjectIdentifiers.id_PBKDF2)) {
PBKDF2Params pbkdf2Params = PBKDF2Params.getInstance(pbkdAlgorithm.getParameters());
if (pbkdf2Params.getKeyLength() != null) {
keySizeInBytes = pbkdf2Params.getKeyLength().intValue();
} else if (keySizeInBytes == -1) {
throw new IOException("no keyLength found in PBKDF2Params");
}
if (pbkdf2Params.getPrf().getAlgorithm().equals(PKCSObjectIdentifiers.id_hmacWithSHA512)) {
PKCS5S2ParametersGenerator pGen = new PKCS5S2ParametersGenerator(new SHA512Digest());
pGen.init(Arrays.concatenate(encPassword, differentiator), pbkdf2Params.getSalt(), pbkdf2Params.getIterationCount().intValue());
return ((KeyParameter) pGen.generateDerivedParameters(keySizeInBytes * 8)).getKey();
} else if (pbkdf2Params.getPrf().getAlgorithm().equals(NISTObjectIdentifiers.id_hmacWithSHA3_512)) {
PKCS5S2ParametersGenerator pGen = new PKCS5S2ParametersGenerator(new SHA3Digest(512));
pGen.init(Arrays.concatenate(encPassword, differentiator), pbkdf2Params.getSalt(), pbkdf2Params.getIterationCount().intValue());
return ((KeyParameter) pGen.generateDerivedParameters(keySizeInBytes * 8)).getKey();
} else {
throw new IOException("BCFKS KeyStore: unrecognized MAC PBKD PRF: " + pbkdf2Params.getPrf().getAlgorithm());
}
} else {
throw new IOException("BCFKS KeyStore: unrecognized MAC PBKD.");
}
}
use of com.github.zhenwei.core.asn1.misc.ScryptParams in project LinLong-Java by zhenwei1108.
the class BcFKSKeyStoreSpi method isSimilarHmacPbkd.
private boolean isSimilarHmacPbkd(PBKDFConfig storePBKDFConfig, KeyDerivationFunc hmacPkbdAlgorithm) {
if (!storePBKDFConfig.getAlgorithm().equals(hmacPkbdAlgorithm.getAlgorithm())) {
return false;
}
if (MiscObjectIdentifiers.id_scrypt.equals(hmacPkbdAlgorithm.getAlgorithm())) {
if (!(storePBKDFConfig instanceof ScryptConfig)) {
return false;
}
ScryptConfig scryptConfig = (ScryptConfig) storePBKDFConfig;
ScryptParams sParams = ScryptParams.getInstance(hmacPkbdAlgorithm.getParameters());
if (scryptConfig.getSaltLength() != sParams.getSalt().length || scryptConfig.getBlockSize() != sParams.getBlockSize().intValue() || scryptConfig.getCostParameter() != sParams.getCostParameter().intValue() || scryptConfig.getParallelizationParameter() != sParams.getParallelizationParameter().intValue()) {
return false;
}
} else {
if (!(storePBKDFConfig instanceof PBKDF2Config)) {
return false;
}
PBKDF2Config pbkdf2Config = (PBKDF2Config) storePBKDFConfig;
PBKDF2Params pbkdf2Params = PBKDF2Params.getInstance(hmacPkbdAlgorithm.getParameters());
if (pbkdf2Config.getSaltLength() != pbkdf2Params.getSalt().length || pbkdf2Config.getIterationCount() != pbkdf2Params.getIterationCount().intValue()) {
return false;
}
}
return true;
}
Aggregations