use of com.github.zhenwei.core.crypto.params.KeyParameter in project LinLong-Java by zhenwei1108.
the class BrokenJCEBlockCipher method engineInit.
protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
CipherParameters param;
//
if (key instanceof BCPBEKey) {
param = Util.makePBEParameters((BCPBEKey) key, params, pbeType, pbeHash, cipher.getUnderlyingCipher().getAlgorithmName(), pbeKeySize, pbeIvSize);
if (pbeIvSize != 0) {
ivParam = (ParametersWithIV) param;
}
} else if (params == null) {
param = new KeyParameter(key.getEncoded());
} else if (params instanceof IvParameterSpec) {
if (ivLength != 0) {
param = new ParametersWithIV(new KeyParameter(key.getEncoded()), ((IvParameterSpec) params).getIV());
ivParam = (ParametersWithIV) param;
} else {
param = new KeyParameter(key.getEncoded());
}
} else if (params instanceof RC2ParameterSpec) {
RC2ParameterSpec rc2Param = (RC2ParameterSpec) params;
param = new RC2Parameters(key.getEncoded(), ((RC2ParameterSpec) params).getEffectiveKeyBits());
if (rc2Param.getIV() != null && ivLength != 0) {
param = new ParametersWithIV(param, rc2Param.getIV());
ivParam = (ParametersWithIV) param;
}
} else if (params instanceof RC5ParameterSpec) {
RC5ParameterSpec rc5Param = (RC5ParameterSpec) params;
param = new RC5Parameters(key.getEncoded(), ((RC5ParameterSpec) params).getRounds());
if (rc5Param.getWordSize() != 32) {
throw new IllegalArgumentException("can only accept RC5 word size 32 (at the moment...)");
}
if ((rc5Param.getIV() != null) && (ivLength != 0)) {
param = new ParametersWithIV(param, rc5Param.getIV());
ivParam = (ParametersWithIV) param;
}
} else {
throw new InvalidAlgorithmParameterException("unknown parameter type.");
}
if ((ivLength != 0) && !(param instanceof ParametersWithIV)) {
if (random == null) {
random = CryptoServicesRegistrar.getSecureRandom();
}
if ((opmode == Cipher.ENCRYPT_MODE) || (opmode == Cipher.WRAP_MODE)) {
byte[] iv = new byte[ivLength];
random.nextBytes(iv);
param = new ParametersWithIV(param, iv);
ivParam = (ParametersWithIV) param;
} else {
throw new InvalidAlgorithmParameterException("no IV set when one expected");
}
}
switch(opmode) {
case Cipher.ENCRYPT_MODE:
case Cipher.WRAP_MODE:
cipher.init(true, param);
break;
case Cipher.DECRYPT_MODE:
case Cipher.UNWRAP_MODE:
cipher.init(false, param);
break;
default:
System.out.println("eeek!");
}
}
use of com.github.zhenwei.core.crypto.params.KeyParameter in project LinLong-Java by zhenwei1108.
the class ChaCha20Poly1305 method initMAC.
private void initMAC() {
byte[] firstBlock = new byte[64];
try {
chacha20.processBytes(firstBlock, 0, 64, firstBlock, 0);
poly1305.init(new KeyParameter(firstBlock, 0, 32));
} finally {
Arrays.clear(firstBlock);
}
}
use of com.github.zhenwei.core.crypto.params.KeyParameter in project LinLong-Java by zhenwei1108.
the class GCMBlockCipher method init.
/**
* NOTE: MAC sizes from 32 bits to 128 bits (must be a multiple of 8) are supported. The default
* is 128 bits. Sizes less than 96 are not recommended, but are supported for specialized
* applications.
*/
public void init(boolean forEncryption, CipherParameters params) throws IllegalArgumentException {
this.forEncryption = forEncryption;
this.macBlock = null;
this.initialised = true;
KeyParameter keyParam;
byte[] newNonce = null;
if (params instanceof AEADParameters) {
AEADParameters param = (AEADParameters) params;
newNonce = param.getNonce();
initialAssociatedText = param.getAssociatedText();
int macSizeBits = param.getMacSize();
if (macSizeBits < 32 || macSizeBits > 128 || macSizeBits % 8 != 0) {
throw new IllegalArgumentException("Invalid value for MAC size: " + macSizeBits);
}
macSize = macSizeBits / 8;
keyParam = param.getKey();
} else if (params instanceof ParametersWithIV) {
ParametersWithIV param = (ParametersWithIV) params;
newNonce = param.getIV();
initialAssociatedText = null;
macSize = 16;
keyParam = (KeyParameter) param.getParameters();
} else {
throw new IllegalArgumentException("invalid parameters passed to GCM");
}
int bufLength = forEncryption ? BLOCK_SIZE : (BLOCK_SIZE + macSize);
this.bufBlock = new byte[bufLength];
if (newNonce == null || newNonce.length < 1) {
throw new IllegalArgumentException("IV must be at least 1 byte");
}
if (forEncryption) {
if (nonce != null && Arrays.areEqual(nonce, newNonce)) {
if (keyParam == null) {
throw new IllegalArgumentException("cannot reuse nonce for GCM encryption");
}
if (lastKey != null && Arrays.areEqual(lastKey, keyParam.getKey())) {
throw new IllegalArgumentException("cannot reuse nonce for GCM encryption");
}
}
}
nonce = newNonce;
if (keyParam != null) {
lastKey = keyParam.getKey();
}
// if keyParam is null we're reusing the last key.
if (keyParam != null) {
cipher.init(true, keyParam);
this.H = new byte[BLOCK_SIZE];
cipher.processBlock(H, 0, H, 0);
// GCMMultiplier tables don't change unless the key changes (and are expensive to init)
multiplier.init(H);
exp = null;
} else if (this.H == null) {
throw new IllegalArgumentException("Key must be specified in initial init");
}
this.J0 = new byte[BLOCK_SIZE];
if (nonce.length == 12) {
System.arraycopy(nonce, 0, J0, 0, nonce.length);
this.J0[BLOCK_SIZE - 1] = 0x01;
} else {
gHASH(J0, nonce, nonce.length);
byte[] X = new byte[BLOCK_SIZE];
Pack.longToBigEndian((long) nonce.length * 8, X, 8);
gHASHBlock(J0, X);
}
this.S = new byte[BLOCK_SIZE];
this.S_at = new byte[BLOCK_SIZE];
this.S_atPre = new byte[BLOCK_SIZE];
this.atBlock = new byte[BLOCK_SIZE];
this.atBlockPos = 0;
this.atLength = 0;
this.atLengthPre = 0;
this.counter = Arrays.clone(J0);
// page 8, len(P) <= 2^39 - 256, 1 block used by tag but done on J0
this.blocksRemaining = -2;
this.bufOff = 0;
this.totalLength = 0;
if (initialAssociatedText != null) {
processAADBytes(initialAssociatedText, 0, initialAssociatedText.length);
}
}
use of com.github.zhenwei.core.crypto.params.KeyParameter in project LinLong-Java by zhenwei1108.
the class GCMSIVBlockCipher method init.
public void init(final boolean pEncrypt, final CipherParameters cipherParameters) throws IllegalArgumentException {
/* Set defaults */
byte[] myInitialAEAD = null;
byte[] myNonce = null;
KeyParameter myKey = null;
/* Access parameters */
if (cipherParameters instanceof AEADParameters) {
final AEADParameters myAEAD = (AEADParameters) cipherParameters;
myInitialAEAD = myAEAD.getAssociatedText();
myNonce = myAEAD.getNonce();
myKey = myAEAD.getKey();
} else if (cipherParameters instanceof ParametersWithIV) {
final ParametersWithIV myParms = (ParametersWithIV) cipherParameters;
myNonce = myParms.getIV();
myKey = (KeyParameter) myParms.getParameters();
} else {
throw new IllegalArgumentException("invalid parameters passed to GCM-SIV");
}
/* Check nonceSize */
if (myNonce == null || myNonce.length != NONCELEN) {
throw new IllegalArgumentException("Invalid nonce");
}
/* Check keysize */
if (myKey == null || (myKey.getKey().length != BUFLEN && myKey.getKey().length != (BUFLEN << 1))) {
throw new IllegalArgumentException("Invalid key");
}
/* Reset details */
forEncryption = pEncrypt;
theInitialAEAD = myInitialAEAD;
theNonce = myNonce;
/* Initialise the keys */
deriveKeys(myKey);
resetStreams();
}
use of com.github.zhenwei.core.crypto.params.KeyParameter in project LinLong-Java by zhenwei1108.
the class HKDFBytesGenerator method extract.
/**
* Performs the extract part of the key derivation function.
*
* @param salt the salt to use
* @param ikm the input keying material
* @return the PRK as KeyParameter
*/
private KeyParameter extract(byte[] salt, byte[] ikm) {
if (salt == null) {
// TODO check if hashLen is indeed same as HMAC size
hMacHash.init(new KeyParameter(new byte[hashLen]));
} else {
hMacHash.init(new KeyParameter(salt));
}
hMacHash.update(ikm, 0, ikm.length);
byte[] prk = new byte[hashLen];
hMacHash.doFinal(prk, 0);
return new KeyParameter(prk);
}
Aggregations