use of org.bouncycastle.crypto.params.KeyParameter in project XobotOS by xamarin.
the class JCEBlockCipher 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 JCEPBEKey) {
JCEPBEKey k = (JCEPBEKey) 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) {
pbeSpec = (PBEParameterSpec) params;
param = PBE.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 (params == null) {
param = new KeyParameter(key.getEncoded());
} else if (params instanceof IvParameterSpec) {
if (ivLength != 0) {
IvParameterSpec p = (IvParameterSpec) params;
if (p.getIV().length != ivLength && !isAEADModeName(modeName)) {
throw new InvalidAlgorithmParameterException("IV must be " + ivLength + " bytes long.");
}
param = new ParametersWithIV(new KeyParameter(key.getEncoded()), p.getIV());
ivParam = (ParametersWithIV) param;
} else {
if (modeName != null && modeName.equals("ECB")) {
throw new InvalidAlgorithmParameterException("ECB mode does not use an IV");
}
param = new KeyParameter(key.getEncoded());
}
} else // BEGIN android-removed
// 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)
// {
// 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)
// {
// 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))
// {
// param = new ParametersWithIV(param, rc5Param.getIV());
// ivParam = (ParametersWithIV)param;
// }
// }
// END android-removed
{
throw new InvalidAlgorithmParameterException("unknown parameter type.");
}
if ((ivLength != 0) && !(param instanceof ParametersWithIV)) {
SecureRandom ivRandom = random;
if (ivRandom == null) {
ivRandom = new SecureRandom();
}
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");
}
} catch (Exception e) {
throw new InvalidKeyException(e.getMessage());
}
}
use of org.bouncycastle.crypto.params.KeyParameter in project XobotOS by xamarin.
the class WrapCipherSpi method engineInit.
protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
CipherParameters param;
if (key instanceof JCEPBEKey) {
JCEPBEKey k = (JCEPBEKey) key;
if (params instanceof PBEParameterSpec) {
param = PBE.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 javax.crypto.spec.IvParameterSpec) {
IvParameterSpec iv = (IvParameterSpec) params;
param = new ParametersWithIV(param, iv.getIV());
}
if (param instanceof KeyParameter && ivSize != 0) {
iv = new byte[ivSize];
random.nextBytes(iv);
param = new ParametersWithIV(param, iv);
}
switch(opmode) {
case Cipher.WRAP_MODE:
wrapEngine.init(true, param);
break;
case Cipher.UNWRAP_MODE:
wrapEngine.init(false, param);
break;
case Cipher.ENCRYPT_MODE:
case Cipher.DECRYPT_MODE:
throw new IllegalArgumentException("engine only valid for wrapping");
default:
System.out.println("eeek!");
}
}
use of org.bouncycastle.crypto.params.KeyParameter in project XobotOS by xamarin.
the class PEMUtilities method getKey.
private static SecretKey getKey(char[] password, String algorithm, int keyLength, byte[] salt, boolean des2) {
OpenSSLPBEParametersGenerator pGen = new OpenSSLPBEParametersGenerator();
pGen.init(PBEParametersGenerator.PKCS5PasswordToBytes(password), salt);
KeyParameter keyParam;
keyParam = (KeyParameter) pGen.generateDerivedParameters(keyLength * 8);
byte[] key = keyParam.getKey();
if (des2 && key.length >= 24) {
// For DES2, we must copy first 8 bytes into the last 8 bytes.
System.arraycopy(key, 0, key, 16, 8);
}
return new javax.crypto.spec.SecretKeySpec(key, algorithm);
}
use of org.bouncycastle.crypto.params.KeyParameter in project XobotOS by xamarin.
the class DESedeWrapEngine method init.
/**
* Method init
*
* @param forWrapping
* @param param
*/
public void init(boolean forWrapping, CipherParameters param) {
this.forWrapping = forWrapping;
this.engine = new CBCBlockCipher(new DESedeEngine());
SecureRandom sr;
if (param instanceof ParametersWithRandom) {
ParametersWithRandom pr = (ParametersWithRandom) param;
param = pr.getParameters();
sr = pr.getRandom();
} else {
sr = new SecureRandom();
}
if (param instanceof KeyParameter) {
this.param = (KeyParameter) param;
if (this.forWrapping) {
// Hm, we have no IV but we want to wrap ?!?
// well, then we have to create our own IV.
this.iv = new byte[8];
sr.nextBytes(iv);
this.paramPlusIV = new ParametersWithIV(this.param, this.iv);
}
} else if (param instanceof ParametersWithIV) {
this.paramPlusIV = (ParametersWithIV) param;
this.iv = this.paramPlusIV.getIV();
this.param = (KeyParameter) this.paramPlusIV.getParameters();
if (this.forWrapping) {
if ((this.iv == null) || (this.iv.length != 8)) {
throw new IllegalArgumentException("IV is not 8 octets");
}
} else {
throw new IllegalArgumentException("You should not supply an IV for unwrapping");
}
}
}
use of org.bouncycastle.crypto.params.KeyParameter in project XobotOS by xamarin.
the class GCMBlockCipher method init.
public void init(boolean forEncryption, CipherParameters params) throws IllegalArgumentException {
this.forEncryption = forEncryption;
this.macBlock = null;
if (params instanceof AEADParameters) {
AEADParameters param = (AEADParameters) params;
nonce = param.getNonce();
A = param.getAssociatedText();
int macSizeBits = param.getMacSize();
if (macSizeBits < 96 || 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;
nonce = param.getIV();
A = 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 (nonce == null || nonce.length < 1) {
throw new IllegalArgumentException("IV must be at least 1 byte");
}
if (A == null) {
// Avoid lots of null checks
A = new byte[0];
}
// Cipher always used in forward mode
cipher.init(true, keyParam);
// TODO This should be configurable by init parameters
// (but must be 16 if nonce length not 12) (BLOCK_SIZE?)
// this.tagLength = 16;
this.H = new byte[BLOCK_SIZE];
cipher.processBlock(ZEROES, 0, H, 0);
multiplier.init(H);
this.initS = gHASH(A);
if (nonce.length == 12) {
this.J0 = new byte[16];
System.arraycopy(nonce, 0, J0, 0, nonce.length);
this.J0[15] = 0x01;
} else {
this.J0 = gHASH(nonce);
byte[] X = new byte[16];
packLength((long) nonce.length * 8, X, 8);
xor(this.J0, X);
multiplier.multiplyH(this.J0);
}
this.S = Arrays.clone(initS);
this.counter = Arrays.clone(J0);
this.bufOff = 0;
this.totalLength = 0;
}
Aggregations