use of com.github.zhenwei.core.crypto.params.KeyParameter in project LinLong-Java by zhenwei1108.
the class BaseStreamCipher method engineInit.
protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
CipherParameters param;
this.pbeSpec = null;
this.pbeAlgorithm = null;
this.engineParams = null;
//
if (!(key instanceof SecretKey)) {
throw new InvalidKeyException("Key for algorithm " + key.getAlgorithm() + " not suitable for symmetric enryption.");
}
if (key instanceof PKCS12Key) {
PKCS12Key k = (PKCS12Key) key;
pbeSpec = (PBEParameterSpec) params;
if (k instanceof PKCS12KeyWithParameters && pbeSpec == null) {
pbeSpec = new PBEParameterSpec(((PKCS12KeyWithParameters) k).getSalt(), ((PKCS12KeyWithParameters) k).getIterationCount());
}
param = Util.makePBEParameters(k.getEncoded(), PKCS12, digest, keySizeInBits, ivLength * 8, pbeSpec, cipher.getAlgorithmName());
} else if (key instanceof BCPBEKey) {
BCPBEKey k = (BCPBEKey) key;
if (k.getOID() != null) {
pbeAlgorithm = k.getOID().getId();
} else {
pbeAlgorithm = k.getAlgorithm();
}
if (k.getParam() != null) {
param = k.getParam();
pbeSpec = new PBEParameterSpec(k.getSalt(), k.getIterationCount());
} else if (params instanceof PBEParameterSpec) {
param = Util.makePBEParameters(k, params, cipher.getAlgorithmName());
pbeSpec = (PBEParameterSpec) params;
} else {
throw new InvalidAlgorithmParameterException("PBE requires PBE parameters to be set.");
}
if (k.getIvSize() != 0) {
ivParam = (ParametersWithIV) param;
}
} else if (params == null) {
if (digest > 0) {
throw new InvalidKeyException("Algorithm requires a PBE key");
}
param = new KeyParameter(key.getEncoded());
} else if (params instanceof IvParameterSpec) {
param = new ParametersWithIV(new KeyParameter(key.getEncoded()), ((IvParameterSpec) params).getIV());
ivParam = (ParametersWithIV) param;
} else {
throw new InvalidAlgorithmParameterException("unknown parameter type.");
}
if ((ivLength != 0) && !(param instanceof ParametersWithIV)) {
SecureRandom ivRandom = random;
if (ivRandom == null) {
ivRandom = CryptoServicesRegistrar.getSecureRandom();
}
if ((opmode == Cipher.ENCRYPT_MODE) || (opmode == Cipher.WRAP_MODE)) {
byte[] iv = new byte[ivLength];
ivRandom.nextBytes(iv);
param = new ParametersWithIV(param, iv);
ivParam = (ParametersWithIV) param;
} else {
throw new InvalidAlgorithmParameterException("no IV set when one expected");
}
}
try {
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:
throw new InvalidParameterException("unknown opmode " + opmode + " passed");
}
} catch (Exception e) {
throw new InvalidKeyException(e.getMessage());
}
}
use of com.github.zhenwei.core.crypto.params.KeyParameter in project LinLong-Java by zhenwei1108.
the class BaseWrapCipher method engineInit.
protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
CipherParameters param;
if (key instanceof BCPBEKey) {
BCPBEKey k = (BCPBEKey) key;
if (params instanceof PBEParameterSpec) {
param = Util.makePBEParameters(k, params, wrapEngine.getAlgorithmName());
} else if (k.getParam() != null) {
param = k.getParam();
} else {
throw new InvalidAlgorithmParameterException("PBE requires PBE parameters to be set.");
}
} else {
param = new KeyParameter(key.getEncoded());
}
if (params instanceof IvParameterSpec) {
IvParameterSpec ivSpec = (IvParameterSpec) params;
this.iv = ivSpec.getIV();
param = new ParametersWithIV(param, iv);
}
if (params instanceof GOST28147WrapParameterSpec) {
GOST28147WrapParameterSpec spec = (GOST28147WrapParameterSpec) params;
byte[] sBox = spec.getSBox();
if (sBox != null) {
param = new ParametersWithSBox(param, sBox);
}
param = new ParametersWithUKM(param, spec.getUKM());
}
if (param instanceof KeyParameter && ivSize != 0) {
if (opmode == Cipher.WRAP_MODE || opmode == Cipher.ENCRYPT_MODE) {
iv = new byte[ivSize];
random.nextBytes(iv);
param = new ParametersWithIV(param, iv);
}
}
if (random != null) {
param = new ParametersWithRandom(param, random);
}
try {
switch(opmode) {
case Cipher.WRAP_MODE:
wrapEngine.init(true, param);
this.wrapStream = null;
this.forWrapping = true;
break;
case Cipher.UNWRAP_MODE:
wrapEngine.init(false, param);
this.wrapStream = null;
this.forWrapping = false;
break;
case Cipher.ENCRYPT_MODE:
wrapEngine.init(true, param);
this.wrapStream = new ErasableOutputStream();
this.forWrapping = true;
break;
case Cipher.DECRYPT_MODE:
wrapEngine.init(false, param);
this.wrapStream = new ErasableOutputStream();
this.forWrapping = false;
break;
default:
throw new InvalidParameterException("Unknown mode parameter passed to init.");
}
} catch (Exception e) {
throw new InvalidKeyOrParametersException(e.getMessage(), e);
}
}
use of com.github.zhenwei.core.crypto.params.KeyParameter in project LinLong-Java by zhenwei1108.
the class TLSKDF method hmac_hash.
private static void hmac_hash(Mac mac, byte[] secret, byte[] seed, byte[] out) {
mac.init(new KeyParameter(secret));
byte[] a = seed;
int size = mac.getMacSize();
int iterations = (out.length + size - 1) / size;
byte[] buf = new byte[mac.getMacSize()];
byte[] buf2 = new byte[mac.getMacSize()];
for (int i = 0; i < iterations; i++) {
mac.update(a, 0, a.length);
mac.doFinal(buf, 0);
a = buf;
mac.update(a, 0, a.length);
mac.update(seed, 0, seed.length);
mac.doFinal(buf2, 0);
System.arraycopy(buf2, 0, out, (size * i), Math.min(size, out.length - (size * i)));
}
}
use of com.github.zhenwei.core.crypto.params.KeyParameter in project LinLong-Java by zhenwei1108.
the class Seed method prg.
static void prg(byte[] r, int rOff, long rlen, byte[] key, int keyOff) {
byte[] nonce = new byte[8];
StreamCipher cipher = new ChaChaEngine(12);
cipher.init(true, new ParametersWithIV(new KeyParameter(key, keyOff, 32), nonce));
cipher.processBytes(r, rOff, (int) rlen, r, rOff);
// crypto_stream_chacha12(r, rlen, nonce, key);
}
use of com.github.zhenwei.core.crypto.params.KeyParameter in project LinLong-Java by zhenwei1108.
the class BcPasswordRecipientInfoGenerator method generateEncryptedBytes.
public byte[] generateEncryptedBytes(AlgorithmIdentifier keyEncryptionAlgorithm, byte[] derivedKey, GenericKey contentEncryptionKey) throws CMSException {
byte[] contentEncryptionKeySpec = ((KeyParameter) CMSUtils.getBcKey(contentEncryptionKey)).getKey();
Wrapper keyEncryptionCipher = EnvelopedDataHelper.createRFC3211Wrapper(keyEncryptionAlgorithm.getAlgorithm());
keyEncryptionCipher.init(true, new ParametersWithIV(new KeyParameter(derivedKey), ASN1OctetString.getInstance(keyEncryptionAlgorithm.getParameters()).getOctets()));
return keyEncryptionCipher.wrap(contentEncryptionKeySpec, 0, contentEncryptionKeySpec.length);
}
Aggregations