use of org.bouncycastle.crypto.params.KeyParameter in project XobotOS by xamarin.
the class PKCS5S1ParametersGenerator method generateDerivedParameters.
/**
* Generate a key with initialisation vector parameter derived from
* the password, salt, and iteration count we are currently initialised
* with.
*
* @param keySize the size of the key we want (in bits)
* @param ivSize the size of the iv we want (in bits)
* @return a ParametersWithIV object.
* @exception IllegalArgumentException if keySize + ivSize is larger than the base hash size.
*/
public CipherParameters generateDerivedParameters(int keySize, int ivSize) {
keySize = keySize / 8;
ivSize = ivSize / 8;
if ((keySize + ivSize) > digest.getDigestSize()) {
throw new IllegalArgumentException("Can't generate a derived key " + (keySize + ivSize) + " bytes long.");
}
byte[] dKey = generateDerivedKey();
return new ParametersWithIV(new KeyParameter(dKey, 0, keySize), dKey, keySize, ivSize);
}
use of org.bouncycastle.crypto.params.KeyParameter in project Skein3Fish by wernerd.
the class ThreefishCipher method init.
public void init(boolean forEncryption, CipherParameters params) throws IllegalArgumentException {
if (params instanceof ParametersForThreefish) {
ParametersForThreefish pft = (ParametersForThreefish) params;
stateSize = pft.getStateSize();
setCipher(stateSize);
if (cipher == null)
throw new IllegalArgumentException("Threefish: unsupported state size: " + stateSize);
byte[] key = ((KeyParameter) pft.getParameters()).getKey();
if (key.length != (stateSize / 8))
throw new IllegalArgumentException("Threefish: key length does not match state size: " + key.length);
long[] tweak = pft.getTweak();
if (tweak == null)
throw new IllegalArgumentException("Threefish: tweak data not set");
cipher.setTweak(tweak);
// Get a long array for cipher key and moves the byte key buffer to it
long[] keyLong = new long[stateSize / 64];
for (int i = 0; i < keyLong.length; i++) keyLong[i] = ByteLong.GetUInt64(key, i * 8);
cipher.setKey(keyLong);
this.forEncryption = forEncryption;
// Allocate buffers
cipherIn = new long[stateSize / 64];
cipherOut = new long[stateSize / 64];
return;
}
throw new IllegalArgumentException("Threfish: invalid parameter passed to init - " + params.getClass().getName());
}
use of org.bouncycastle.crypto.params.KeyParameter in project Skein3Fish by wernerd.
the class SkeinMac method init.
public void init(CipherParameters params) throws IllegalArgumentException {
ParametersForSkein p = (ParametersForSkein) params;
KeyParameter kp = (KeyParameter) (p.getParameters());
skein = new Skein(p.getStateSize(), p.getMacSize(), 0, kp.getKey());
Xsave = skein.getState();
}
use of org.bouncycastle.crypto.params.KeyParameter in project Skein3Fish by wernerd.
the class SkeinTest method checkKATVectors.
boolean checkKATVectors() {
KatResult kr = new KatResult();
ParametersForSkein pfs;
while (scanner.fillResult(kr)) {
// Skip Tree vectors in this test function
if (kr.restOfLine.contains("Tree")) {
notProcessed++;
continue;
}
if (kr.restOfLine.contains("MAC")) {
pfs = new ParametersForSkein(new KeyParameter(kr.macKey), kr.stateSize, kr.hashBitLength);
SkeinMac sm = new SkeinMac();
sm.init(pfs);
sm.updateBits(kr.msg, 0, kr.msgLength);
byte[] mac = new byte[sm.getMacSize()];
sm.doFinal(mac, 0);
if (!Arrays.equals(mac, kr.result)) {
System.out.println(kr.stateSize + "-" + kr.hashBitLength + "-" + kr.msgLength + "-" + kr.restOfLine);
hexdump("Computed mac", mac, mac.length);
hexdump("Expected result", kr.result, kr.result.length);
return false;
}
processed++;
continue;
}
Skein skein = new Skein(kr.stateSize, kr.hashBitLength);
skein.updateBits(kr.msg, 0, kr.msgLength);
byte[] hash = skein.doFinal();
if (!Arrays.equals(hash, kr.result)) {
System.out.println(kr.stateSize + "-" + kr.hashBitLength + "-" + kr.msgLength + "-" + kr.restOfLine);
hexdump("Computed hash", hash, hash.length);
hexdump("Expected result", kr.result, kr.result.length);
return false;
}
// Enable the next few line so you can check some results manually
// if ((kr.msgLength & 1) == 1) {
// System.out.println(kr.stateSize + "-" + kr.hashBitLength + "-"
// + kr.msgLength + "-" + kr.restOfLine);
// hexdump("Computed hash", hash, hash.length);
// hexdump("Expected result", kr.result, kr.result.length);
// }
processed++;
}
return true;
}
use of org.bouncycastle.crypto.params.KeyParameter in project robovm by robovm.
the class BaseBlockCipher 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 (params == null && baseEngine.getAlgorithmName().startsWith("RC5-64")) {
throw new InvalidAlgorithmParameterException("RC5 requires an RC5ParametersSpec to be passed in.");
}
//
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();
if (params instanceof IvParameterSpec) {
IvParameterSpec iv = (IvParameterSpec) params;
param = new ParametersWithIV(param, iv.getIV());
}
} 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.");
}
if (key instanceof RepeatedSecretKeySpec) {
param = new ParametersWithIV(null, p.getIV());
ivParam = (ParametersWithIV) param;
} else {
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());
}
}
Aggregations