use of com.github.zhenwei.core.crypto.params.KeyParameter in project LinLong-Java by zhenwei1108.
the class ISO9797Alg3Mac method init.
public void init(CipherParameters params) {
reset();
if (!(params instanceof KeyParameter || params instanceof ParametersWithIV)) {
throw new IllegalArgumentException("params must be an instance of KeyParameter or ParametersWithIV");
}
// KeyParameter must contain a double or triple length DES key,
// however the underlying cipher is a single DES. The middle and
// right key are used only in the final step.
KeyParameter kp;
if (params instanceof KeyParameter) {
kp = (KeyParameter) params;
} else {
kp = (KeyParameter) ((ParametersWithIV) params).getParameters();
}
KeyParameter key1;
byte[] keyvalue = kp.getKey();
if (keyvalue.length == 16) {
// Double length DES key
key1 = new KeyParameter(keyvalue, 0, 8);
this.lastKey2 = new KeyParameter(keyvalue, 8, 8);
this.lastKey3 = key1;
} else if (keyvalue.length == 24) {
// Triple length DES key
key1 = new KeyParameter(keyvalue, 0, 8);
this.lastKey2 = new KeyParameter(keyvalue, 8, 8);
this.lastKey3 = new KeyParameter(keyvalue, 16, 8);
} else {
throw new IllegalArgumentException("Key must be either 112 or 168 bit long");
}
if (params instanceof ParametersWithIV) {
cipher.init(true, new ParametersWithIV(key1, ((ParametersWithIV) params).getIV()));
} else {
cipher.init(true, key1);
}
}
use of com.github.zhenwei.core.crypto.params.KeyParameter in project LinLong-Java by zhenwei1108.
the class Poly1305 method setKey.
private void setKey(final byte[] key, final byte[] nonce) {
if (key.length != 32) {
throw new IllegalArgumentException("Poly1305 key must be 256 bits.");
}
if (cipher != null && (nonce == null || nonce.length != BLOCK_SIZE)) {
throw new IllegalArgumentException("Poly1305 requires a 128 bit IV.");
}
// Extract r portion of key (and "clamp" the values)
int t0 = Pack.littleEndianToInt(key, 0);
int t1 = Pack.littleEndianToInt(key, 4);
int t2 = Pack.littleEndianToInt(key, 8);
int t3 = Pack.littleEndianToInt(key, 12);
// NOTE: The masks perform the key "clamping" implicitly
r0 = t0 & 0x03FFFFFF;
r1 = ((t0 >>> 26) | (t1 << 6)) & 0x03FFFF03;
r2 = ((t1 >>> 20) | (t2 << 12)) & 0x03FFC0FF;
r3 = ((t2 >>> 14) | (t3 << 18)) & 0x03F03FFF;
r4 = (t3 >>> 8) & 0x000FFFFF;
// Precompute multipliers
s1 = r1 * 5;
s2 = r2 * 5;
s3 = r3 * 5;
s4 = r4 * 5;
final byte[] kBytes;
final int kOff;
if (cipher == null) {
kBytes = key;
kOff = BLOCK_SIZE;
} else {
// Compute encrypted nonce
kBytes = new byte[BLOCK_SIZE];
kOff = 0;
cipher.init(true, new KeyParameter(key, BLOCK_SIZE, BLOCK_SIZE));
cipher.processBlock(nonce, 0, kBytes, 0);
}
k0 = Pack.littleEndianToInt(kBytes, kOff + 0);
k1 = Pack.littleEndianToInt(kBytes, kOff + 4);
k2 = Pack.littleEndianToInt(kBytes, kOff + 8);
k3 = Pack.littleEndianToInt(kBytes, kOff + 12);
}
use of com.github.zhenwei.core.crypto.params.KeyParameter in project LinLong-Java by zhenwei1108.
the class Poly1305 method init.
/**
* Initialises the Poly1305 MAC.
*
* @param params if used with a block cipher, then a {@link ParametersWithIV} containing a 128 bit
* nonce and a {@link KeyParameter} with a 256 bit key complying to the {@link
* Poly1305KeyGenerator Poly1305 key format}, otherwise just the {@link
* KeyParameter}.
*/
public void init(CipherParameters params) throws IllegalArgumentException {
byte[] nonce = null;
if (cipher != null) {
if (!(params instanceof ParametersWithIV)) {
throw new IllegalArgumentException("Poly1305 requires an IV when used with a block cipher.");
}
ParametersWithIV ivParams = (ParametersWithIV) params;
nonce = ivParams.getIV();
params = ivParams.getParameters();
}
if (!(params instanceof KeyParameter)) {
throw new IllegalArgumentException("Poly1305 requires a key.");
}
KeyParameter keyParams = (KeyParameter) params;
setKey(keyParams.getKey(), nonce);
reset();
}
use of com.github.zhenwei.core.crypto.params.KeyParameter in project LinLong-Java by zhenwei1108.
the class SipHash method init.
public void init(CipherParameters params) throws IllegalArgumentException {
if (!(params instanceof KeyParameter)) {
throw new IllegalArgumentException("'params' must be an instance of KeyParameter");
}
KeyParameter keyParameter = (KeyParameter) params;
byte[] key = keyParameter.getKey();
if (key.length != 16) {
throw new IllegalArgumentException("'params' must be a 128-bit key");
}
this.k0 = Pack.littleEndianToLong(key, 0);
this.k1 = Pack.littleEndianToLong(key, 8);
reset();
}
use of com.github.zhenwei.core.crypto.params.KeyParameter in project LinLong-Java by zhenwei1108.
the class SkeinMac method init.
/**
* Initialises the Skein digest with the provided parameters.<br> See {@link SkeinParameters} for
* details on the parameterisation of the Skein hash function.
*
* @param params an instance of {@link SkeinParameters} or {@link KeyParameter}.
*/
public void init(CipherParameters params) throws IllegalArgumentException {
SkeinParameters skeinParameters;
if (params instanceof SkeinParameters) {
skeinParameters = (SkeinParameters) params;
} else if (params instanceof KeyParameter) {
skeinParameters = new SkeinParameters.Builder().setKey(((KeyParameter) params).getKey()).build();
} else {
throw new IllegalArgumentException("Invalid parameter passed to Skein MAC init - " + params.getClass().getName());
}
if (skeinParameters.getKey() == null) {
throw new IllegalArgumentException("Skein MAC requires a key parameter.");
}
engine.init(skeinParameters);
}
Aggregations