use of java.security.spec.AlgorithmParameterSpec in project robovm by robovm.
the class CipherTest method testDoFinalbyteArrayintintbyteArrayint.
/*
* Class under test for int doFinal(byte[], int, int, byte[], int)
*/
public void testDoFinalbyteArrayintintbyteArrayint() throws Exception {
byte[] b = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
byte[] b1 = new byte[30];
AlgorithmParameterSpec ap = new IvParameterSpec(IV);
Cipher c = Cipher.getInstance("DES/CBC/NoPadding");
c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap);
try {
c.doFinal(b, 0, 10, b1, 5);
fail();
} catch (IllegalBlockSizeException expected) {
}
c = Cipher.getInstance("DES/CBC/NoPadding");
try {
c.doFinal(b, 0, 10, b1, 5);
fail();
} catch (IllegalStateException expected) {
}
c = Cipher.getInstance("DES/CBC/NoPadding");
c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap);
int len = c.doFinal(b, 0, 16, b1, 0);
assertEquals(16, len);
c = Cipher.getInstance("DES/CBC/PKCS5Padding");
c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_DES, ap);
try {
c.doFinal(b1, 0, 24, new byte[42], 0);
fail();
} catch (BadPaddingException expected) {
}
b1 = new byte[6];
c = Cipher.getInstance("DESede");
c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES);
try {
c.doFinal(b, 3, 6, b1, 5);
fail();
} catch (IllegalBlockSizeException maybeExpected) {
assertTrue(StandardNames.IS_RI);
} catch (ShortBufferException maybeExpected) {
assertFalse(StandardNames.IS_RI);
}
}
use of java.security.spec.AlgorithmParameterSpec in project robovm by robovm.
the class KeyAgreementTest method testInit04.
/**
* Test for the methods:
* <code>init(Key key, AlgorithmParameterSpec params)</code>
* <code>init(Key key, AlgorithmParameterSpec params, SecureRandom random)</code>
* <code>generateSecret()</code>
* Assertions: initializes KeyAgreement and returns byte array
*/
public void testInit04() throws Exception, InvalidAlgorithmParameterException {
if (!DEFSupported) {
fail(NotSupportMsg);
return;
}
createKeys();
KeyAgreement[] kAgs = createKAs();
DHParameterSpec dhPs = ((DHPrivateKey) privKey).getParams();
AlgorithmParameterSpec aps = new RSAKeyGenParameterSpec(10, new BigInteger("10"));
byte[] bbRes1;
byte[] bbRes2;
byte[] bbRes3;
SecureRandom randomNull = null;
SecureRandom random = new SecureRandom();
for (int i = 0; i < kAgs.length; i++) {
kAgs[i].init(privKey, dhPs);
kAgs[i].doPhase(publKey, true);
bbRes1 = kAgs[i].generateSecret();
kAgs[i].init(privKey, dhPs, random);
kAgs[i].doPhase(publKey, true);
bbRes2 = kAgs[i].generateSecret();
assertEquals("Incorrect byte array length", bbRes1.length, bbRes2.length);
for (int j = 0; j < bbRes1.length; j++) {
assertEquals("Incorrect byte (index: ".concat(Integer.toString(i)).concat(")"), bbRes1[j], bbRes2[j]);
}
kAgs[i].init(privKey, dhPs, randomNull);
kAgs[i].doPhase(publKey, true);
bbRes3 = kAgs[i].generateSecret();
assertEquals("Incorrect byte array length", bbRes1.length, bbRes3.length);
for (int j = 0; j < bbRes1.length; j++) {
assertEquals("Incorrect byte (index: ".concat(Integer.toString(i)).concat(")"), bbRes1[j], bbRes3[j]);
}
try {
kAgs[i].init(publKey, dhPs, random);
fail("InvalidKeyException expected");
} catch (InvalidKeyException e) {
//expected
}
try {
kAgs[i].init(privKey, aps, random);
fail("InvalidAlgorithmParameterException expected");
} catch (InvalidAlgorithmParameterException e) {
//expected
}
}
}
use of java.security.spec.AlgorithmParameterSpec in project robovm by robovm.
the class APSpecSpi method testKeyGeneratorSpi01.
/**
* Test for <code>KeyGeneratorSpi</code> constructor Assertion: constructs
* KeyGeneratorSpi
*/
public void testKeyGeneratorSpi01() throws InvalidAlgorithmParameterException {
Mock_KeyGeneratorSpi kgSpi = new Mock_KeyGeneratorSpi();
assertNull("Not null result", kgSpi.engineGenerateKey());
try {
kgSpi.engineInit(77, new SecureRandom());
fail("IllegalArgumentException must be thrown");
} catch (IllegalArgumentException e) {
}
try {
kgSpi.engineInit(new SecureRandom());
fail("IllegalArgumentException must be thrown");
} catch (IllegalArgumentException e) {
}
AlgorithmParameterSpec aps = null;
try {
kgSpi.engineInit(aps, new SecureRandom());
fail("InvalidAlgorithmParameterException must be thrown when parameter is null");
} catch (InvalidAlgorithmParameterException e) {
}
aps = new APSpecSpi();
kgSpi.engineInit(aps, new SecureRandom());
}
use of java.security.spec.AlgorithmParameterSpec in project robovm by robovm.
the class myKeyGenerator method testKeyGenerator.
/**
* Test for <code>KeyGenerator</code> constructor Assertion: returns
* KeyGenerator object
*/
public void testKeyGenerator() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
if (!DEFSupported) {
fail(NotSupportMsg);
return;
}
KeyGeneratorSpi spi = new MyKeyGeneratorSpi();
KeyGenerator keyG = new myKeyGenerator(spi, defaultProvider, defaultAlgorithm);
assertEquals("Incorrect algorithm", keyG.getAlgorithm(), defaultAlgorithm);
assertEquals("Incorrect provider", keyG.getProvider(), defaultProvider);
AlgorithmParameterSpec params = null;
int keysize = 0;
try {
keyG.init(params, null);
fail("InvalidAlgorithmParameterException must be thrown");
} catch (InvalidAlgorithmParameterException e) {
}
try {
keyG.init(keysize, null);
fail("IllegalArgumentException must be thrown");
} catch (IllegalArgumentException e) {
}
keyG = new myKeyGenerator(null, null, null);
assertNull("Algorithm must be null", keyG.getAlgorithm());
assertNull("Provider must be null", keyG.getProvider());
try {
keyG.init(params, null);
fail("NullPointerException must be thrown");
} catch (NullPointerException e) {
}
try {
keyG.init(keysize, null);
fail("NullPointerException or InvalidParameterException must be thrown");
} catch (InvalidParameterException e) {
} catch (NullPointerException e) {
}
}
use of java.security.spec.AlgorithmParameterSpec in project robovm by robovm.
the class myKeyGenerator method testInitParams.
/*
* Test for <code>init(AlgorithmParameterSpec params)</code> and
* <code>init(AlgorithmParameterSpec params, SecureRandom random)</code> methods
* Assertion: throws InvalidAlgorithmParameterException when params is null
*/
public void testInitParams() throws Exception {
if (!DEFSupported) {
fail(NotSupportMsg);
return;
}
KeyGenerator[] kgs = createKGs();
AlgorithmParameterSpec aps = null;
for (int i = 0; i < kgs.length; i++) {
try {
kgs[i].init(aps);
fail("InvalidAlgorithmParameterException must be thrown");
} catch (InvalidAlgorithmParameterException e) {
}
try {
kgs[i].init(aps, new SecureRandom());
fail("InvalidAlgorithmParameterException must be thrown");
} catch (InvalidAlgorithmParameterException e) {
}
}
}
Aggregations