use of java.security.SecureRandom in project robovm by robovm.
the class KeyAgreementSpiTest method testKeyAgreementSpi01.
/**
* Test for <code>KeyAgreementSpi</code> constructor Assertion: constructs
* KeyAgreementSpi
*/
public void testKeyAgreementSpi01() throws InvalidKeyException, ShortBufferException, NoSuchAlgorithmException, InvalidAlgorithmParameterException {
Mock_KeyAgreementSpi kaSpi = new Mock_KeyAgreementSpi();
assertNull("Not null result", kaSpi.engineDoPhase(null, true));
try {
kaSpi.engineDoPhase(null, false);
fail("IllegalStateException must be thrown");
} catch (IllegalStateException e) {
}
byte[] bb = kaSpi.engineGenerateSecret();
assertEquals("Length is not 0", bb.length, 0);
assertEquals("Returned integer is not 0", kaSpi.engineGenerateSecret(new byte[1], 10), -1);
assertNull("Not null result", kaSpi.engineGenerateSecret("aaa"));
try {
kaSpi.engineGenerateSecret("");
fail("NoSuchAlgorithmException must be thrown");
} catch (NoSuchAlgorithmException e) {
}
Key key = null;
try {
kaSpi.engineInit(key, new SecureRandom());
fail("IllegalArgumentException must be thrown");
} catch (IllegalArgumentException e) {
}
AlgorithmParameterSpec params = null;
try {
kaSpi.engineInit(key, params, new SecureRandom());
fail("IllegalArgumentException must be thrown");
} catch (IllegalArgumentException e) {
}
}
use of java.security.SecureRandom in project robovm by robovm.
the class KeyAgreementTest method testInit03.
/**
* Test for the methods: <code>init(Key key)</code>
* <code>init(Key key, SecureRandom random)</code>
* <code>generateSecret()</code>
* Assertions: initializes KeyAgreement and returns byte array
*/
public void testInit03() throws Exception {
if (!DEFSupported) {
fail(NotSupportMsg);
return;
}
createKeys();
KeyAgreement[] kAgs = createKAs();
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);
kAgs[i].doPhase(publKey, true);
bbRes1 = kAgs[i].generateSecret();
kAgs[i].init(privKey, 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, 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]);
}
}
}
use of java.security.SecureRandom in project robovm by robovm.
the class CipherTest method test_initWithKeyAlgorithmParameterSpecSecureRandom.
/**
* javax.crypto.Cipher#init(int, java.security.Key,
* java.security.spec.AlgorithmParameterSpec,
* java.security.SecureRandom)
*/
public void test_initWithKeyAlgorithmParameterSpecSecureRandom() throws Exception {
AlgorithmParameterSpec ap = new IvParameterSpec(IV);
Cipher cipher = Cipher.getInstance(ALGORITHM_3DES + "/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES, ap, new SecureRandom());
byte[] cipherIV = cipher.getIV();
assertTrue("IVs differ", Arrays.equals(cipherIV, IV));
cipher = Cipher.getInstance("DES/CBC/NoPadding");
try {
cipher.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES, ap, new SecureRandom());
fail();
} catch (InvalidKeyException expected) {
}
cipher = Cipher.getInstance("DES/CBC/NoPadding");
ap = new RSAKeyGenParameterSpec(10, new BigInteger("10"));
try {
cipher.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap, new SecureRandom());
fail();
} catch (InvalidAlgorithmParameterException expected) {
}
}
use of java.security.SecureRandom in project robovm by robovm.
the class CipherTest method test_wrap_java_security_Key.
public void test_wrap_java_security_Key() throws Exception {
AlgorithmParameterSpec ap = new IvParameterSpec(IV);
Cipher c = Cipher.getInstance("DES/CBC/PKCS5Padding");
c.init(Cipher.WRAP_MODE, CIPHER_KEY_DES, ap, new SecureRandom());
assertNotNull(c.wrap(CIPHER_KEY_DES));
assertNotNull(c.wrap(CIPHER_KEY_3DES));
String certName = Support_Resources.getURL("test.cert");
InputStream is = new URL(certName).openConnection().getInputStream();
CertificateFactory cf = CertificateFactory.getInstance("X.509");
Certificate cert = cf.generateCertificate(is);
assertNotNull(c.wrap(cert.getPublicKey()));
c = Cipher.getInstance("DES/CBC/NoPadding");
c.init(Cipher.WRAP_MODE, CIPHER_KEY_DES, ap, new SecureRandom());
try {
assertNotNull(c.wrap(cert.getPublicKey()));
fail();
} catch (IllegalBlockSizeException expected) {
}
c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_DES, ap, new SecureRandom());
try {
c.wrap(CIPHER_KEY_DES);
fail();
} catch (IllegalStateException expected) {
}
c.init(Cipher.WRAP_MODE, CIPHER_KEY_DES, ap, new SecureRandom());
try {
c.wrap(new Mock_Key());
fail();
} catch (InvalidKeyException expected) {
}
}
use of java.security.SecureRandom 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
}
}
}
Aggregations