use of com.github.zhenwei.core.crypto.params.KeyParameter in project LinLong-Java by zhenwei1108.
the class PKCS5S2ParametersGenerator method generateDerivedKey.
private byte[] generateDerivedKey(int dkLen) {
int hLen = hMac.getMacSize();
int l = (dkLen + hLen - 1) / hLen;
byte[] iBuf = new byte[4];
byte[] outBytes = new byte[l * hLen];
int outPos = 0;
CipherParameters param = new KeyParameter(password);
hMac.init(param);
for (int i = 1; i <= l; i++) {
// Increment the value in 'iBuf'
int pos = 3;
while (++iBuf[pos] == 0) {
--pos;
}
F(salt, iterationCount, iBuf, outBytes, outPos);
outPos += hLen;
}
return outBytes;
}
use of com.github.zhenwei.core.crypto.params.KeyParameter in project LinLong-Java by zhenwei1108.
the class HKDFBytesGenerator method init.
public void init(DerivationParameters param) {
if (!(param instanceof HKDFParameters)) {
throw new IllegalArgumentException("HKDF parameters required for HKDFBytesGenerator");
}
HKDFParameters params = (HKDFParameters) param;
if (params.skipExtract()) {
// use IKM directly as PRK
hMacHash.init(new KeyParameter(params.getIKM()));
} else {
hMacHash.init(extract(params.getSalt(), params.getIKM()));
}
info = params.getInfo();
generatedBytes = 0;
currentT = new byte[hashLen];
}
use of com.github.zhenwei.core.crypto.params.KeyParameter in project LinLong-Java by zhenwei1108.
the class Salsa20Engine method init.
/**
* initialise a Salsa20 cipher.
*
* @param forEncryption whether or not we are for encryption.
* @param params the parameters required to set up the cipher.
* @throws IllegalArgumentException if the params argument is inappropriate.
*/
public void init(boolean forEncryption, CipherParameters params) {
if (!(params instanceof ParametersWithIV)) {
throw new IllegalArgumentException(getAlgorithmName() + " Init parameters must include an IV");
}
ParametersWithIV ivParams = (ParametersWithIV) params;
byte[] iv = ivParams.getIV();
if (iv == null || iv.length != getNonceSize()) {
throw new IllegalArgumentException(getAlgorithmName() + " requires exactly " + getNonceSize() + " bytes of IV");
}
CipherParameters keyParam = ivParams.getParameters();
if (keyParam == null) {
if (!initialised) {
throw new IllegalStateException(getAlgorithmName() + " KeyParameter can not be null for first initialisation");
}
setKey(null, iv);
} else if (keyParam instanceof KeyParameter) {
setKey(((KeyParameter) keyParam).getKey(), iv);
} else {
throw new IllegalArgumentException(getAlgorithmName() + " Init parameters must contain a KeyParameter (or null for re-init)");
}
reset();
initialised = true;
}
use of com.github.zhenwei.core.crypto.params.KeyParameter in project LinLong-Java by zhenwei1108.
the class TEAEngine method init.
/**
* initialise
*
* @param forEncryption whether or not we are for encryption.
* @param params the parameters required to set up the cipher.
* @throws IllegalArgumentException if the params argument is inappropriate.
*/
public void init(boolean forEncryption, CipherParameters params) {
if (!(params instanceof KeyParameter)) {
throw new IllegalArgumentException("invalid parameter passed to TEA init - " + params.getClass().getName());
}
_forEncryption = forEncryption;
_initialised = true;
KeyParameter p = (KeyParameter) params;
setKey(p.getKey());
}
use of com.github.zhenwei.core.crypto.params.KeyParameter in project LinLong-Java by zhenwei1108.
the class ThreefishEngine method init.
/**
* Initialise the engine.
*
* @param params an instance of {@link TweakableBlockCipherParameters}, or {@link KeyParameter}
* (to use a 0 tweak)
*/
public void init(boolean forEncryption, CipherParameters params) throws IllegalArgumentException {
final byte[] keyBytes;
final byte[] tweakBytes;
if (params instanceof TweakableBlockCipherParameters) {
TweakableBlockCipherParameters tParams = (TweakableBlockCipherParameters) params;
keyBytes = tParams.getKey().getKey();
tweakBytes = tParams.getTweak();
} else if (params instanceof KeyParameter) {
keyBytes = ((KeyParameter) params).getKey();
tweakBytes = null;
} else {
throw new IllegalArgumentException("Invalid parameter passed to Threefish init - " + params.getClass().getName());
}
long[] keyWords = null;
long[] tweakWords = null;
if (keyBytes != null) {
if (keyBytes.length != this.blocksizeBytes) {
throw new IllegalArgumentException("Threefish key must be same size as block (" + blocksizeBytes + " bytes)");
}
keyWords = new long[blocksizeWords];
for (int i = 0; i < keyWords.length; i++) {
keyWords[i] = bytesToWord(keyBytes, i * 8);
}
}
if (tweakBytes != null) {
if (tweakBytes.length != TWEAK_SIZE_BYTES) {
throw new IllegalArgumentException("Threefish tweak must be " + TWEAK_SIZE_BYTES + " bytes");
}
tweakWords = new long[] { bytesToWord(tweakBytes, 0), bytesToWord(tweakBytes, 8) };
}
init(forEncryption, keyWords, tweakWords);
}
Aggregations