use of com.github.zhenwei.core.crypto.params.KeyParameter in project LinLong-Java by zhenwei1108.
the class GOST28147Mac method recursiveInit.
private void recursiveInit(CipherParameters params) throws IllegalArgumentException {
if (params == null) {
return;
}
CipherParameters child = null;
if (params instanceof ParametersWithSBox) {
ParametersWithSBox param = (ParametersWithSBox) params;
//
// Set the S-Box
//
System.arraycopy(param.getSBox(), 0, this.S, 0, param.getSBox().length);
child = param.getParameters();
} else if (params instanceof KeyParameter) {
workingKey = generateWorkingKey(((KeyParameter) params).getKey());
} else if (params instanceof ParametersWithIV) {
ParametersWithIV p = (ParametersWithIV) params;
System.arraycopy(p.getIV(), 0, mac, 0, mac.length);
// don't skip the initial CM5Func
macIV = p.getIV();
child = p.getParameters();
} else {
throw new IllegalArgumentException("invalid parameter passed to GOST28147 init - " + params.getClass().getName());
}
recursiveInit(child);
}
use of com.github.zhenwei.core.crypto.params.KeyParameter in project LinLong-Java by zhenwei1108.
the class KGMac method init.
/**
* Initialises the GMAC - requires a {@link ParametersWithIV} providing a {@link KeyParameter} and
* a nonce.
*/
public void init(final CipherParameters params) throws IllegalArgumentException {
if (params instanceof ParametersWithIV) {
final ParametersWithIV param = (ParametersWithIV) params;
final byte[] iv = param.getIV();
final KeyParameter keyParam = (KeyParameter) param.getParameters();
// GCM is always operated in encrypt mode to calculate MAC
cipher.init(true, new AEADParameters(keyParam, macSizeBits, iv));
} else {
throw new IllegalArgumentException("KGMAC requires ParametersWithIV");
}
}
use of com.github.zhenwei.core.crypto.params.KeyParameter in project LinLong-Java by zhenwei1108.
the class KMAC method init.
public void init(CipherParameters params) throws IllegalArgumentException {
KeyParameter kParam = (KeyParameter) params;
this.key = Arrays.clone(kParam.getKey());
this.initialised = true;
reset();
}
use of com.github.zhenwei.core.crypto.params.KeyParameter in project LinLong-Java by zhenwei1108.
the class GOST28147Engine method init.
/**
* initialise an GOST28147 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 ParametersWithSBox) {
ParametersWithSBox param = (ParametersWithSBox) params;
//
// Set the S-Box
//
byte[] sBox = param.getSBox();
if (sBox.length != Sbox_Default.length) {
throw new IllegalArgumentException("invalid S-box passed to GOST28147 init");
}
this.S = Arrays.clone(sBox);
//
if (param.getParameters() != null) {
workingKey = generateWorkingKey(forEncryption, ((KeyParameter) param.getParameters()).getKey());
}
} else if (params instanceof KeyParameter) {
workingKey = generateWorkingKey(forEncryption, ((KeyParameter) params).getKey());
} else if (params != null) {
throw new IllegalArgumentException("invalid parameter passed to GOST28147 init - " + params.getClass().getName());
}
}
use of com.github.zhenwei.core.crypto.params.KeyParameter in project LinLong-Java by zhenwei1108.
the class Grainv1Engine method init.
/**
* Initialize a Grain v1 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) throws IllegalArgumentException {
/**
* Grain encryption and decryption is completely symmetrical, so the
* 'forEncryption' is irrelevant.
*/
if (!(params instanceof ParametersWithIV)) {
throw new IllegalArgumentException("Grain v1 Init parameters must include an IV");
}
ParametersWithIV ivParams = (ParametersWithIV) params;
byte[] iv = ivParams.getIV();
if (iv == null || iv.length != 8) {
throw new IllegalArgumentException("Grain v1 requires exactly 8 bytes of IV");
}
if (!(ivParams.getParameters() instanceof KeyParameter)) {
throw new IllegalArgumentException("Grain v1 Init parameters must include a key");
}
KeyParameter key = (KeyParameter) ivParams.getParameters();
/**
* Initialize variables.
*/
workingIV = new byte[key.getKey().length];
workingKey = new byte[key.getKey().length];
lfsr = new int[STATE_SIZE];
nfsr = new int[STATE_SIZE];
out = new byte[2];
System.arraycopy(iv, 0, workingIV, 0, iv.length);
System.arraycopy(key.getKey(), 0, workingKey, 0, key.getKey().length);
reset();
}
Aggregations