use of com.github.zhenwei.core.crypto.CipherParameters in project LinLong-Java by zhenwei1108.
the class CCMBlockCipher method init.
public void init(boolean forEncryption, CipherParameters params) throws IllegalArgumentException {
this.forEncryption = forEncryption;
CipherParameters cipherParameters;
if (params instanceof AEADParameters) {
AEADParameters param = (AEADParameters) params;
nonce = param.getNonce();
initialAssociatedText = param.getAssociatedText();
macSize = getMacSize(forEncryption, param.getMacSize());
cipherParameters = param.getKey();
} else if (params instanceof ParametersWithIV) {
ParametersWithIV param = (ParametersWithIV) params;
nonce = param.getIV();
initialAssociatedText = null;
macSize = getMacSize(forEncryption, 64);
cipherParameters = param.getParameters();
} else {
throw new IllegalArgumentException("invalid parameters passed to CCM: " + params.getClass().getName());
}
// NOTE: Very basic support for key re-use, but no performance gain from it
if (cipherParameters != null) {
keyParam = cipherParameters;
}
if (nonce == null || nonce.length < 7 || nonce.length > 13) {
throw new IllegalArgumentException("nonce must have length from 7 to 13 octets");
}
reset();
}
use of com.github.zhenwei.core.crypto.CipherParameters in project LinLong-Java by zhenwei1108.
the class ChaCha20Poly1305 method init.
public void init(boolean forEncryption, CipherParameters params) throws IllegalArgumentException {
KeyParameter initKeyParam;
byte[] initNonce;
CipherParameters chacha20Params;
if (params instanceof AEADParameters) {
AEADParameters aeadParams = (AEADParameters) params;
int macSizeBits = aeadParams.getMacSize();
if ((MAC_SIZE * 8) != macSizeBits) {
throw new IllegalArgumentException("Invalid value for MAC size: " + macSizeBits);
}
initKeyParam = aeadParams.getKey();
initNonce = aeadParams.getNonce();
chacha20Params = new ParametersWithIV(initKeyParam, initNonce);
this.initialAAD = aeadParams.getAssociatedText();
} else if (params instanceof ParametersWithIV) {
ParametersWithIV ivParams = (ParametersWithIV) params;
initKeyParam = (KeyParameter) ivParams.getParameters();
initNonce = ivParams.getIV();
chacha20Params = ivParams;
this.initialAAD = null;
} else {
throw new IllegalArgumentException("invalid parameters passed to ChaCha20Poly1305");
}
// Validate key
if (null == initKeyParam) {
if (State.UNINITIALIZED == state) {
throw new IllegalArgumentException("Key must be specified in initial init");
}
} else {
if (KEY_SIZE != initKeyParam.getKey().length) {
throw new IllegalArgumentException("Key must be 256 bits");
}
}
// Validate nonce
if (null == initNonce || NONCE_SIZE != initNonce.length) {
throw new IllegalArgumentException("Nonce must be 96 bits");
}
// Check for encryption with reused nonce
if (State.UNINITIALIZED != state && forEncryption && Arrays.areEqual(nonce, initNonce)) {
if (null == initKeyParam || Arrays.areEqual(key, initKeyParam.getKey())) {
throw new IllegalArgumentException("cannot reuse nonce for ChaCha20Poly1305 encryption");
}
}
if (null != initKeyParam) {
System.arraycopy(initKeyParam.getKey(), 0, key, 0, KEY_SIZE);
}
System.arraycopy(initNonce, 0, nonce, 0, NONCE_SIZE);
chacha20.init(true, chacha20Params);
this.state = forEncryption ? State.ENC_INIT : State.DEC_INIT;
reset(true, false);
}
use of com.github.zhenwei.core.crypto.CipherParameters in project LinLong-Java by zhenwei1108.
the class PSSSigner method init.
public void init(boolean forSigning, CipherParameters param) {
CipherParameters params;
if (param instanceof ParametersWithRandom) {
ParametersWithRandom p = (ParametersWithRandom) param;
params = p.getParameters();
random = p.getRandom();
} else {
params = param;
if (forSigning) {
random = CryptoServicesRegistrar.getSecureRandom();
}
}
RSAKeyParameters kParam;
if (params instanceof RSABlindingParameters) {
kParam = ((RSABlindingParameters) params).getPublicKey();
// pass on random
cipher.init(forSigning, param);
} else {
kParam = (RSAKeyParameters) params;
cipher.init(forSigning, params);
}
emBits = kParam.getModulus().bitLength() - 1;
if (emBits < (8 * hLen + 8 * sLen + 9)) {
throw new IllegalArgumentException("key too small for specified hash and salt lengths");
}
block = new byte[(emBits + 7) / 8];
reset();
}
use of com.github.zhenwei.core.crypto.CipherParameters in project LinLong-Java by zhenwei1108.
the class SignatureSpi method engineInitVerify.
protected void engineInitVerify(PublicKey publicKey) throws InvalidKeyException {
CipherParameters param;
if (publicKey instanceof BCDSTU4145PublicKey) {
param = ((BCDSTU4145PublicKey) publicKey).engineGetKeyParameters();
digest = new GOST3411Digest(expandSbox(((BCDSTU4145PublicKey) publicKey).getSbox()));
} else {
param = ECUtil.generatePublicKeyParameter(publicKey);
digest = new GOST3411Digest(expandSbox(DSTU4145Params.getDefaultDKE()));
}
signer.init(false, param);
}
use of com.github.zhenwei.core.crypto.CipherParameters in project LinLong-Java by zhenwei1108.
the class IESCipher method engineDoFinal.
// Finalisation methods
public byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen) throws IllegalBlockSizeException, BadPaddingException {
if (inputLen != 0) {
buffer.write(input, inputOffset, inputLen);
}
byte[] in = buffer.toByteArray();
buffer.reset();
// Convert parameters for use in IESEngine
CipherParameters params = new IESWithCipherParameters(engineSpec.getDerivationV(), engineSpec.getEncodingV(), engineSpec.getMacKeySize(), engineSpec.getCipherKeySize());
if (engineSpec.getNonce() != null) {
params = new ParametersWithIV(params, engineSpec.getNonce());
}
DHParameters dhParams = ((DHKeyParameters) key).getParameters();
byte[] V;
if (otherKeyParameter != null) {
try {
if (state == Cipher.ENCRYPT_MODE || state == Cipher.WRAP_MODE) {
engine.init(true, otherKeyParameter, key, params);
} else {
engine.init(false, key, otherKeyParameter, params);
}
return engine.processBlock(in, 0, in.length);
} catch (Exception e) {
throw new BadBlockException("unable to process block", e);
}
}
if (state == Cipher.ENCRYPT_MODE || state == Cipher.WRAP_MODE) {
// Generate the ephemeral key pair
DHKeyPairGenerator gen = new DHKeyPairGenerator();
gen.init(new DHKeyGenerationParameters(random, dhParams));
EphemeralKeyPairGenerator kGen = new EphemeralKeyPairGenerator(gen, new KeyEncoder() {
public byte[] getEncoded(AsymmetricKeyParameter keyParameter) {
byte[] Vloc = new byte[(((DHKeyParameters) keyParameter).getParameters().getP().bitLength() + 7) / 8];
byte[] Vtmp = BigIntegers.asUnsignedByteArray(((DHPublicKeyParameters) keyParameter).getY());
if (Vtmp.length > Vloc.length) {
throw new IllegalArgumentException("Senders's public key longer than expected.");
} else {
System.arraycopy(Vtmp, 0, Vloc, Vloc.length - Vtmp.length, Vtmp.length);
}
return Vloc;
}
});
// Encrypt the buffer
try {
engine.init(key, params, kGen);
return engine.processBlock(in, 0, in.length);
} catch (Exception e) {
throw new BadBlockException("unable to process block", e);
}
} else if (state == Cipher.DECRYPT_MODE || state == Cipher.UNWRAP_MODE) {
// Decrypt the buffer
try {
engine.init(key, params, new DHIESPublicKeyParser(((DHKeyParameters) key).getParameters()));
return engine.processBlock(in, 0, in.length);
} catch (InvalidCipherTextException e) {
throw new BadBlockException("unable to process block", e);
}
} else {
throw new IllegalStateException("IESCipher not initialised");
}
}
Aggregations