use of com.github.zhenwei.core.crypto.params.RC2Parameters in project LinLong-Java by zhenwei1108.
the class RC2Engine method init.
/**
* initialise a RC2 cipher.
*
* @param encrypting 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 encrypting, CipherParameters params) {
this.encrypting = encrypting;
if (params instanceof RC2Parameters) {
RC2Parameters param = (RC2Parameters) params;
workingKey = generateWorkingKey(param.getKey(), param.getEffectiveKeyBits());
} else if (params instanceof KeyParameter) {
byte[] key = ((KeyParameter) params).getKey();
workingKey = generateWorkingKey(key, key.length * 8);
} else {
throw new IllegalArgumentException("invalid parameter passed to RC2 init - " + params.getClass().getName());
}
}
use of com.github.zhenwei.core.crypto.params.RC2Parameters in project LinLong-Java by zhenwei1108.
the class BaseMac method engineInit.
protected void engineInit(Key key, final AlgorithmParameterSpec params) throws InvalidKeyException, InvalidAlgorithmParameterException {
CipherParameters param;
if (key == null) {
throw new InvalidKeyException("key is null");
}
if (key instanceof PKCS12Key) {
SecretKey k;
PBEParameterSpec pbeSpec;
try {
k = (SecretKey) key;
} catch (Exception e) {
throw new InvalidKeyException("PKCS12 requires a SecretKey/PBEKey");
}
try {
pbeSpec = (PBEParameterSpec) params;
} catch (Exception e) {
throw new InvalidAlgorithmParameterException("PKCS12 requires a PBEParameterSpec");
}
if (k instanceof PBEKey && pbeSpec == null) {
pbeSpec = new PBEParameterSpec(((PBEKey) k).getSalt(), ((PBEKey) k).getIterationCount());
}
int digest = SHA1;
int keySize = 160;
if (macEngine.getAlgorithmName().startsWith("GOST")) {
digest = GOST3411;
keySize = 256;
} else if (macEngine instanceof HMac) {
if (!macEngine.getAlgorithmName().startsWith("SHA-1")) {
if (macEngine.getAlgorithmName().startsWith("SHA-224")) {
digest = SHA224;
keySize = 224;
} else if (macEngine.getAlgorithmName().startsWith("SHA-256")) {
digest = SHA256;
keySize = 256;
} else if (macEngine.getAlgorithmName().startsWith("SHA-384")) {
digest = SHA384;
keySize = 384;
} else if (macEngine.getAlgorithmName().startsWith("SHA-512")) {
digest = SHA512;
keySize = 512;
} else if (macEngine.getAlgorithmName().startsWith("RIPEMD160")) {
digest = RIPEMD160;
keySize = 160;
} else {
throw new InvalidAlgorithmParameterException("no PKCS12 mapping for HMAC: " + macEngine.getAlgorithmName());
}
}
}
// TODO: add correct handling for other digests
param = Util.makePBEMacParameters(k, PKCS12, digest, keySize, pbeSpec);
} else if (key instanceof BCPBEKey) {
BCPBEKey k = (BCPBEKey) key;
if (k.getParam() != null) {
param = k.getParam();
} else if (params instanceof PBEParameterSpec) {
param = Util.makePBEMacParameters(k, params);
} else {
throw new InvalidAlgorithmParameterException("PBE requires PBE parameters to be set.");
}
} else {
if (params instanceof PBEParameterSpec) {
throw new InvalidAlgorithmParameterException("inappropriate parameter type: " + params.getClass().getName());
}
param = new KeyParameter(key.getEncoded());
}
final KeyParameter keyParam;
if (param instanceof ParametersWithIV) {
keyParam = (KeyParameter) ((ParametersWithIV) param).getParameters();
} else {
keyParam = (KeyParameter) param;
}
if (params instanceof AEADParameterSpec) {
AEADParameterSpec aeadSpec = (AEADParameterSpec) params;
param = new AEADParameters(keyParam, aeadSpec.getMacSizeInBits(), aeadSpec.getNonce(), aeadSpec.getAssociatedData());
} else if (params instanceof IvParameterSpec) {
param = new ParametersWithIV(keyParam, ((IvParameterSpec) params).getIV());
} else if (params instanceof RC2ParameterSpec) {
param = new ParametersWithIV(new RC2Parameters(keyParam.getKey(), ((RC2ParameterSpec) params).getEffectiveKeyBits()), ((RC2ParameterSpec) params).getIV());
} else if (params instanceof SkeinParameterSpec) {
param = new SkeinParameters.Builder(copyMap(((SkeinParameterSpec) params).getParameters())).setKey(keyParam.getKey()).build();
} else if (params == null) {
param = new KeyParameter(key.getEncoded());
} else if (gcmSpecClass != null && gcmSpecClass.isAssignableFrom(params.getClass())) {
param = GcmSpecUtil.extractAeadParameters(keyParam, params);
} else if (!(params instanceof PBEParameterSpec)) {
throw new InvalidAlgorithmParameterException("unknown parameter type: " + params.getClass().getName());
}
try {
macEngine.init(param);
} catch (Exception e) {
throw new InvalidAlgorithmParameterException("cannot initialize MAC: " + e.getMessage());
}
}
use of com.github.zhenwei.core.crypto.params.RC2Parameters in project LinLong-Java by zhenwei1108.
the class BaseBlockCipher method engineInit.
protected void engineInit(int opmode, Key key, final AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
CipherParameters param;
this.pbeSpec = null;
this.pbeAlgorithm = null;
this.engineParams = null;
this.aeadParams = null;
//
if (!(key instanceof SecretKey)) {
throw new InvalidKeyException("Key for algorithm " + ((key != null) ? key.getAlgorithm() : null) + " not suitable for symmetric enryption.");
}
//
if (params == null && (baseEngine != null && baseEngine.getAlgorithmName().startsWith("RC5-64"))) {
throw new InvalidAlgorithmParameterException("RC5 requires an RC5ParametersSpec to be passed in.");
}
//
if (scheme == PKCS12 || key instanceof PKCS12Key) {
SecretKey k;
try {
k = (SecretKey) key;
} catch (Exception e) {
throw new InvalidKeyException("PKCS12 requires a SecretKey/PBEKey");
}
if (params instanceof PBEParameterSpec) {
pbeSpec = (PBEParameterSpec) params;
}
if (k instanceof PBEKey && pbeSpec == null) {
PBEKey pbeKey = (PBEKey) k;
if (pbeKey.getSalt() == null) {
throw new InvalidAlgorithmParameterException("PBEKey requires parameters to specify salt");
}
pbeSpec = new PBEParameterSpec(pbeKey.getSalt(), pbeKey.getIterationCount());
}
if (pbeSpec == null && !(k instanceof PBEKey)) {
throw new InvalidKeyException("Algorithm requires a PBE key");
}
if (key instanceof BCPBEKey) {
// PKCS#12 sets an IV, if we get a key that doesn't have ParametersWithIV we need to reject it. If the
// key has no parameters it means it's an old-school JCE PBE Key - we use getEncoded() on it.
CipherParameters pbeKeyParam = ((BCPBEKey) key).getParam();
if (pbeKeyParam instanceof ParametersWithIV) {
param = pbeKeyParam;
} else if (pbeKeyParam == null) {
param = Util.makePBEParameters(k.getEncoded(), PKCS12, digest, keySizeInBits, ivLength * 8, pbeSpec, cipher.getAlgorithmName());
} else {
throw new InvalidKeyException("Algorithm requires a PBE key suitable for PKCS12");
}
} else {
param = Util.makePBEParameters(k.getEncoded(), PKCS12, digest, keySizeInBits, ivLength * 8, pbeSpec, cipher.getAlgorithmName());
}
if (param instanceof ParametersWithIV) {
ivParam = (ParametersWithIV) param;
}
} else if (key instanceof PBKDF1Key) {
PBKDF1Key k = (PBKDF1Key) key;
if (params instanceof PBEParameterSpec) {
pbeSpec = (PBEParameterSpec) params;
}
if (k instanceof PBKDF1KeyWithParameters && pbeSpec == null) {
pbeSpec = new PBEParameterSpec(((PBKDF1KeyWithParameters) k).getSalt(), ((PBKDF1KeyWithParameters) k).getIterationCount());
}
param = Util.makePBEParameters(k.getEncoded(), PKCS5S1, digest, keySizeInBits, ivLength * 8, pbeSpec, cipher.getAlgorithmName());
if (param instanceof ParametersWithIV) {
ivParam = (ParametersWithIV) param;
}
} 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 = adjustParameters(params, k.getParam());
} else if (params instanceof PBEParameterSpec) {
pbeSpec = (PBEParameterSpec) params;
param = Util.makePBEParameters(k, params, cipher.getUnderlyingCipher().getAlgorithmName());
} else {
throw new InvalidAlgorithmParameterException("PBE requires PBE parameters to be set.");
}
if (param instanceof ParametersWithIV) {
ivParam = (ParametersWithIV) param;
}
} else if (key instanceof PBEKey) {
PBEKey k = (PBEKey) key;
pbeSpec = (PBEParameterSpec) params;
if (k instanceof PKCS12KeyWithParameters && pbeSpec == null) {
pbeSpec = new PBEParameterSpec(k.getSalt(), k.getIterationCount());
}
param = Util.makePBEParameters(k.getEncoded(), scheme, digest, keySizeInBits, ivLength * 8, pbeSpec, cipher.getAlgorithmName());
if (param instanceof ParametersWithIV) {
ivParam = (ParametersWithIV) param;
}
} else if (!(key instanceof RepeatedSecretKeySpec)) {
if (scheme == PKCS5S1 || scheme == PKCS5S1_UTF8 || scheme == PKCS5S2 || scheme == PKCS5S2_UTF8) {
throw new InvalidKeyException("Algorithm requires a PBE key");
}
param = new KeyParameter(key.getEncoded());
} else {
param = null;
}
if (params instanceof AEADParameterSpec) {
if (!isAEADModeName(modeName) && !(cipher instanceof AEADGenericBlockCipher)) {
throw new InvalidAlgorithmParameterException("AEADParameterSpec can only be used with AEAD modes.");
}
AEADParameterSpec aeadSpec = (AEADParameterSpec) params;
KeyParameter keyParam;
if (param instanceof ParametersWithIV) {
keyParam = (KeyParameter) ((ParametersWithIV) param).getParameters();
} else {
keyParam = (KeyParameter) param;
}
param = aeadParams = new AEADParameters(keyParam, aeadSpec.getMacSizeInBits(), aeadSpec.getNonce(), aeadSpec.getAssociatedData());
} else if (params instanceof IvParameterSpec) {
if (ivLength != 0) {
IvParameterSpec p = (IvParameterSpec) params;
if (p.getIV().length != ivLength && !(cipher instanceof AEADGenericBlockCipher) && fixedIv) {
throw new InvalidAlgorithmParameterException("IV must be " + ivLength + " bytes long.");
}
if (param instanceof ParametersWithIV) {
param = new ParametersWithIV(((ParametersWithIV) param).getParameters(), p.getIV());
} else {
param = new ParametersWithIV(param, p.getIV());
}
ivParam = (ParametersWithIV) param;
} else {
if (modeName != null && modeName.equals("ECB")) {
throw new InvalidAlgorithmParameterException("ECB mode does not use an IV");
}
}
} else if (params instanceof GOST28147ParameterSpec) {
GOST28147ParameterSpec gost28147Param = (GOST28147ParameterSpec) params;
param = new ParametersWithSBox(new KeyParameter(key.getEncoded()), ((GOST28147ParameterSpec) params).getSbox());
if (gost28147Param.getIV() != null && ivLength != 0) {
if (param instanceof ParametersWithIV) {
param = new ParametersWithIV(((ParametersWithIV) param).getParameters(), gost28147Param.getIV());
} else {
param = new ParametersWithIV(param, gost28147Param.getIV());
}
ivParam = (ParametersWithIV) param;
}
} else if (params instanceof RC2ParameterSpec) {
RC2ParameterSpec rc2Param = (RC2ParameterSpec) params;
param = new RC2Parameters(key.getEncoded(), ((RC2ParameterSpec) params).getEffectiveKeyBits());
if (rc2Param.getIV() != null && ivLength != 0) {
if (param instanceof ParametersWithIV) {
param = new ParametersWithIV(((ParametersWithIV) param).getParameters(), rc2Param.getIV());
} else {
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 (baseEngine.getAlgorithmName().startsWith("RC5")) {
if (baseEngine.getAlgorithmName().equals("RC5-32")) {
if (rc5Param.getWordSize() != 32) {
throw new InvalidAlgorithmParameterException("RC5 already set up for a word size of 32 not " + rc5Param.getWordSize() + ".");
}
} else if (baseEngine.getAlgorithmName().equals("RC5-64")) {
if (rc5Param.getWordSize() != 64) {
throw new InvalidAlgorithmParameterException("RC5 already set up for a word size of 64 not " + rc5Param.getWordSize() + ".");
}
}
} else {
throw new InvalidAlgorithmParameterException("RC5 parameters passed to a cipher that is not RC5.");
}
if ((rc5Param.getIV() != null) && (ivLength != 0)) {
if (param instanceof ParametersWithIV) {
param = new ParametersWithIV(((ParametersWithIV) param).getParameters(), rc5Param.getIV());
} else {
param = new ParametersWithIV(param, rc5Param.getIV());
}
ivParam = (ParametersWithIV) param;
}
} else if (params instanceof FPEParameterSpec) {
FPEParameterSpec spec = (FPEParameterSpec) params;
param = new FPEParameters((KeyParameter) param, spec.getRadix(), spec.getTweak(), spec.isUsingInverseFunction());
} else if (gcmSpecClass != null && gcmSpecClass.isInstance(params)) {
if (!isAEADModeName(modeName) && !(cipher instanceof AEADGenericBlockCipher)) {
throw new InvalidAlgorithmParameterException("GCMParameterSpec can only be used with AEAD modes.");
}
final KeyParameter keyParam;
if (param instanceof ParametersWithIV) {
keyParam = (KeyParameter) ((ParametersWithIV) param).getParameters();
} else {
keyParam = (KeyParameter) param;
}
param = aeadParams = GcmSpecUtil.extractAeadParameters(keyParam, params);
} else if (params != null && !(params instanceof PBEParameterSpec)) {
throw new InvalidAlgorithmParameterException("unknown parameter type.");
}
if ((ivLength != 0) && !(param instanceof ParametersWithIV) && !(param instanceof AEADParameters)) {
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 if (cipher.getUnderlyingCipher().getAlgorithmName().indexOf("PGPCFB") < 0) {
throw new InvalidAlgorithmParameterException("no IV set when one expected");
}
}
if (random != null && padded) {
param = new ParametersWithRandom(param, random);
}
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");
}
if (cipher instanceof AEADGenericBlockCipher && aeadParams == null) {
AEADCipher aeadCipher = ((AEADGenericBlockCipher) cipher).cipher;
aeadParams = new AEADParameters((KeyParameter) ivParam.getParameters(), aeadCipher.getMac().length * 8, ivParam.getIV());
}
} catch (IllegalArgumentException e) {
throw new InvalidAlgorithmParameterException(e.getMessage(), e);
} catch (Exception e) {
throw new InvalidKeyOrParametersException(e.getMessage(), e);
}
}
use of com.github.zhenwei.core.crypto.params.RC2Parameters in project LinLong-Java by zhenwei1108.
the class PEMUtilities method crypt.
static byte[] crypt(boolean encrypt, byte[] bytes, char[] password, String dekAlgName, byte[] iv) throws PEMException {
byte[] ivValue = iv;
String blockMode = "CBC";
BlockCipher engine;
BlockCipherPadding padding = new PKCS7Padding();
KeyParameter sKey;
// Figure out block mode and padding.
if (dekAlgName.endsWith("-CFB")) {
blockMode = "CFB";
padding = null;
}
if (dekAlgName.endsWith("-ECB") || "DES-EDE".equals(dekAlgName) || "DES-EDE3".equals(dekAlgName)) {
// ECB is actually the default (though seldom used) when OpenSSL
// uses DES-EDE (des2) or DES-EDE3 (des3).
blockMode = "ECB";
ivValue = null;
}
if (dekAlgName.endsWith("-OFB")) {
blockMode = "OFB";
padding = null;
}
// Figure out algorithm and key size.
if (dekAlgName.startsWith("DES-EDE")) {
// "DES-EDE" is actually des2 in OpenSSL-speak!
// "DES-EDE3" is des3.
boolean des2 = !dekAlgName.startsWith("DES-EDE3");
sKey = getKey(password, 24, iv, des2);
engine = new DESedeEngine();
} else if (dekAlgName.startsWith("DES-")) {
sKey = getKey(password, 8, iv);
engine = new DESEngine();
} else if (dekAlgName.startsWith("BF-")) {
sKey = getKey(password, 16, iv);
engine = new BlowfishEngine();
} else if (dekAlgName.startsWith("RC2-")) {
int keyBits = 128;
if (dekAlgName.startsWith("RC2-40-")) {
keyBits = 40;
} else if (dekAlgName.startsWith("RC2-64-")) {
keyBits = 64;
}
sKey = new RC2Parameters(getKey(password, keyBits / 8, iv).getKey(), keyBits);
;
engine = new RC2Engine();
} else if (dekAlgName.startsWith("AES-")) {
byte[] salt = iv;
if (salt.length > 8) {
salt = new byte[8];
System.arraycopy(iv, 0, salt, 0, 8);
}
int keyBits;
if (dekAlgName.startsWith("AES-128-")) {
keyBits = 128;
} else if (dekAlgName.startsWith("AES-192-")) {
keyBits = 192;
} else if (dekAlgName.startsWith("AES-256-")) {
keyBits = 256;
} else {
throw new EncryptionException("unknown AES encryption with private key: " + dekAlgName);
}
sKey = getKey(password, keyBits / 8, salt);
engine = new AESEngine();
} else {
throw new EncryptionException("unknown encryption with private key: " + dekAlgName);
}
if (blockMode.equals("CBC")) {
engine = new CBCBlockCipher(engine);
} else if (blockMode.equals("CFB")) {
engine = new CFBBlockCipher(engine, engine.getBlockSize() * 8);
} else if (blockMode.equals("OFB")) {
engine = new OFBBlockCipher(engine, engine.getBlockSize() * 8);
}
try {
BufferedBlockCipher c;
if (padding == null) {
c = new BufferedBlockCipher(engine);
} else {
c = new PaddedBufferedBlockCipher(engine, padding);
}
if (// ECB block mode
ivValue == null) {
c.init(encrypt, sKey);
} else {
c.init(encrypt, new ParametersWithIV(sKey, ivValue));
}
byte[] out = new byte[c.getOutputSize(bytes.length)];
int procLen = c.processBytes(bytes, 0, bytes.length, out, 0);
procLen += c.doFinal(out, procLen);
if (procLen == out.length) {
return out;
} else {
byte[] rv = new byte[procLen];
System.arraycopy(out, 0, rv, 0, procLen);
return rv;
}
} catch (Exception e) {
throw new EncryptionException("exception using cipher - please check password and data.", e);
}
}
use of com.github.zhenwei.core.crypto.params.RC2Parameters 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!");
}
}
Aggregations