diff --git a/core/java/test/junit/net/i2p/crypto/CryptoTestSuite.java b/core/java/test/junit/net/i2p/crypto/CryptoTestSuite.java index dc2f7b01c7650c8fb935089ef82060ca9fab13a7..74d08a26d9708656712350d18ac65b118a1ee42c 100644 --- a/core/java/test/junit/net/i2p/crypto/CryptoTestSuite.java +++ b/core/java/test/junit/net/i2p/crypto/CryptoTestSuite.java @@ -24,7 +24,6 @@ public class CryptoTestSuite { suite.addTestSuite(CryptixAESEngineTest.class); suite.addTestSuite(CryptixRijndael_AlgorithmTest.class); suite.addTestSuite(DSATest.class); - suite.addTestSuite(ElGamalTest.class); suite.addTestSuite(HMACSHA256Test.class); suite.addTestSuite(KeyGeneratorTest.class); suite.addTestSuite(SHA1HashTest.class); diff --git a/core/java/test/junit/net/i2p/crypto/ElGamalTest.java b/router/java/test/junit/net/i2p/router/crypto/ElGamalTest.java similarity index 94% rename from core/java/test/junit/net/i2p/crypto/ElGamalTest.java rename to router/java/test/junit/net/i2p/router/crypto/ElGamalTest.java index 912b6636e10af1cc4fea861e37d5eb8943af0030..37986d6e4985c52e51b2d9dae2052548bf3dfb02 100644 --- a/core/java/test/junit/net/i2p/crypto/ElGamalTest.java +++ b/router/java/test/junit/net/i2p/router/crypto/ElGamalTest.java @@ -1,4 +1,4 @@ -package net.i2p.crypto; +package net.i2p.router.crypto; /* * free (adj.): unencumbered; not under the control of others * Written by jrandom in 2003 and released into the public domain @@ -16,6 +16,8 @@ import java.util.Set; import junit.framework.TestCase; import net.i2p.I2PAppContext; +import net.i2p.crypto.KeyGenerator; +import net.i2p.crypto.SHA256Generator; import net.i2p.data.Base64; import net.i2p.data.DataFormatException; import net.i2p.data.DataHelper; @@ -24,6 +26,7 @@ import net.i2p.data.PrivateKey; import net.i2p.data.PublicKey; import net.i2p.data.SessionKey; import net.i2p.data.SessionTag; +import net.i2p.router.crypto.ElGamalAESEngine; import net.i2p.util.RandomSource; public class ElGamalTest extends TestCase{ @@ -126,7 +129,7 @@ public class ElGamalTest extends TestCase{ protected void setUp() { _context = I2PAppContext.getGlobalContext(); - Object o = YKGenerator.class; + //Object o = YKGenerator.class; } public void testBasicAES(){ @@ -155,12 +158,13 @@ public class ElGamalTest extends TestCase{ String msg = "Hello world"; - byte encrypted[] = _context.elGamalAESEngine().encryptAESBlock(DataHelper.getASCII(msg), sessionKey, iv, null, null, 64); + ElGamalAESEngine e = new ElGamalAESEngine(_context); + byte encrypted[] = e.encryptAESBlock(DataHelper.getASCII(msg), sessionKey, iv, null, null, 64); Set<SessionTag> foundTags = new HashSet<SessionTag>(); SessionKey foundKey = new SessionKey(); byte decrypted[] = null; try{ - decrypted = _context.elGamalAESEngine().decryptAESBlock(encrypted, 0, encrypted.length, sessionKey, iv, null, foundTags, foundKey); + decrypted = e.decryptAESBlock(encrypted, 0, encrypted.length, sessionKey, iv, null, foundTags, foundKey); }catch(DataFormatException dfe){ dfe.printStackTrace(); fail(); @@ -180,10 +184,11 @@ public class ElGamalTest extends TestCase{ SessionKey key = _context.sessionKeyManager().getCurrentKey(pubKey); if (key == null) key = _context.sessionKeyManager().createSession(pubKey); - byte[] encrypted = _context.elGamalAESEngine().encrypt(DataHelper.getASCII(msg), pubKey, key, null, null, 64); + ElGamalAESEngine e = new ElGamalAESEngine(_context); + byte[] encrypted = e.encrypt(DataHelper.getASCII(msg), pubKey, key, null, null, 64); byte[] decrypted = null; try{ - decrypted = _context.elGamalAESEngine().decrypt(encrypted, privKey, _context.sessionKeyManager()); + decrypted = e.decrypt(encrypted, privKey, _context.sessionKeyManager()); }catch(DataFormatException dfe){ dfe.printStackTrace(); fail(); @@ -256,6 +261,7 @@ public class ElGamalTest extends TestCase{ } public void testLoop(){ + ElGamalAESEngine e = new ElGamalAESEngine(_context); for(int i = 0; i < 5; i++){ Object keys[] = KeyGenerator.getInstance().generatePKIKeypair(); PublicKey pubKey = (PublicKey)keys[0]; @@ -267,10 +273,10 @@ public class ElGamalTest extends TestCase{ if (key == null) key = _context.sessionKeyManager().createSession(pubKey); - byte[] encrypted = _context.elGamalAESEngine().encrypt(msg, pubKey, key, null, null, 1024); + byte[] encrypted = e.encrypt(msg, pubKey, key, null, null, 1024); byte[] decrypted = null; try{ - decrypted = _context.elGamalAESEngine().decrypt(encrypted, privKey, _context.sessionKeyManager()); + decrypted = e.decrypt(encrypted, privKey, _context.sessionKeyManager()); }catch(DataFormatException dfe){ dfe.printStackTrace(); fail(); @@ -370,6 +376,8 @@ public class ElGamalTest extends TestCase{ } } +/**** +Package private, move back to net.i2p.crypto if we want to test it public void testYKGen(){ RandomSource.getInstance().nextBoolean(); I2PAppContext context = I2PAppContext.getGlobalContext(); @@ -378,4 +386,5 @@ public class ElGamalTest extends TestCase{ ykgen.getNextYK(); } } +****/ } diff --git a/router/java/test/junit/net/i2p/router/crypto/SessionEncryptionTest.java b/router/java/test/junit/net/i2p/router/crypto/SessionEncryptionTest.java index 392cbdd1b64ab5b9d9cb5bb0992ec3d319dd85df..eccdc7775f1f0f37836652496476b28bd71000fa 100644 --- a/router/java/test/junit/net/i2p/router/crypto/SessionEncryptionTest.java +++ b/router/java/test/junit/net/i2p/router/crypto/SessionEncryptionTest.java @@ -21,6 +21,7 @@ import net.i2p.data.PrivateKey; import net.i2p.data.PublicKey; import net.i2p.data.SessionKey; import net.i2p.data.SessionTag; +import net.i2p.router.crypto.ElGamalAESEngine; /** * @@ -48,8 +49,9 @@ public class SessionEncryptionTest extends TestCase{ byte[] msg = DataHelper.getASCII("msg 1"); - byte emsg[] = _context.elGamalAESEngine().encrypt(msg, pubKey, curKey, null, null, 64); - byte dmsg[] = _context.elGamalAESEngine().decrypt(emsg, privKey, skm); + ElGamalAESEngine e = new ElGamalAESEngine(_context); + byte emsg[] = e.encrypt(msg, pubKey, curKey, null, null, 64); + byte dmsg[] = e.decrypt(emsg, privKey, skm); assertTrue(DataHelper.eq(dmsg, msg)); } @@ -62,8 +64,9 @@ public class SessionEncryptionTest extends TestCase{ byte[] msg = DataHelper.getASCII("msg 2"); - byte emsg[] = _context.elGamalAESEngine().encrypt(msg, pubKey, curKey, null, null, 64); - byte dmsg[] = _context.elGamalAESEngine().decrypt(emsg, privKey, skm); + ElGamalAESEngine e = new ElGamalAESEngine(_context); + byte emsg[] = e.encrypt(msg, pubKey, curKey, null, null, 64); + byte dmsg[] = e.decrypt(emsg, privKey, skm); assertTrue(DataHelper.eq(dmsg, msg)); } @@ -101,9 +104,10 @@ public class SessionEncryptionTest extends TestCase{ byte[] msg4 = DataHelper.getASCII("msg 4"); byte[] msg5 = DataHelper.getASCII("msg 5"); - byte emsg1[] = _context.elGamalAESEngine().encrypt(msg1, pubKey, curKey, firstTags, null, 64); + ElGamalAESEngine e = new ElGamalAESEngine(_context); + byte emsg1[] = e.encrypt(msg1, pubKey, curKey, firstTags, null, 64); - byte dmsg1[] = _context.elGamalAESEngine().decrypt(emsg1, privKey, skm); + byte dmsg1[] = e.decrypt(emsg1, privKey, skm); assertTrue(DataHelper.eq(dmsg1, msg1)); @@ -116,9 +120,9 @@ public class SessionEncryptionTest extends TestCase{ assertNotNull(curTag); - byte emsg2[] = _context.elGamalAESEngine().encrypt(msg2, pubKey, curKey, null, curTag, 64); + byte emsg2[] = e.encrypt(msg2, pubKey, curKey, null, curTag, 64); - byte dmsg2[] = _context.elGamalAESEngine().decrypt(emsg2, privKey, skm); + byte dmsg2[] = e.decrypt(emsg2, privKey, skm); assertTrue(DataHelper.eq(dmsg2, msg2)); @@ -130,9 +134,9 @@ public class SessionEncryptionTest extends TestCase{ assertNotNull(curTag); assertNotNull(curKey); - byte emsg3[] = _context.elGamalAESEngine().encrypt(msg3, pubKey, curKey, secondTags, curTag, 64); + byte emsg3[] = e.encrypt(msg3, pubKey, curKey, secondTags, curTag, 64); - byte dmsg3[] = _context.elGamalAESEngine().decrypt(emsg3, privKey, skm); + byte dmsg3[] = e.decrypt(emsg3, privKey, skm); assertTrue(DataHelper.eq(dmsg3, msg3)); @@ -146,9 +150,9 @@ public class SessionEncryptionTest extends TestCase{ assertNotNull(curTag); assertNotNull(curKey); - byte emsg4[] = _context.elGamalAESEngine().encrypt(msg4, pubKey, curKey, null, curTag, 64); + byte emsg4[] = e.encrypt(msg4, pubKey, curKey, null, curTag, 64); - byte dmsg4[] = _context.elGamalAESEngine().decrypt(emsg4, privKey, skm); + byte dmsg4[] = e.decrypt(emsg4, privKey, skm); assertTrue(DataHelper.eq(dmsg4, msg4)); @@ -158,9 +162,9 @@ public class SessionEncryptionTest extends TestCase{ assertNotNull(curTag); assertNotNull(curKey); - byte emsg5[] = _context.elGamalAESEngine().encrypt(msg5, pubKey, curKey, null, curTag, 64); + byte emsg5[] = e.encrypt(msg5, pubKey, curKey, null, curTag, 64); - byte dmsg5[] = _context.elGamalAESEngine().decrypt(emsg5, privKey, skm); + byte dmsg5[] = e.decrypt(emsg5, privKey, skm); assertTrue(DataHelper.eq(dmsg5, msg5)); @@ -201,9 +205,10 @@ public class SessionEncryptionTest extends TestCase{ byte[] msg4 = DataHelper.getASCII("msg 4"); byte[] msg5 = DataHelper.getASCII("msg 5"); - byte emsg1[] = _context.elGamalAESEngine().encrypt(msg1, pubKey, curKey, firstTags, null, 64); + ElGamalAESEngine e = new ElGamalAESEngine(_context); + byte emsg1[] = e.encrypt(msg1, pubKey, curKey, firstTags, null, 64); - byte dmsg1[] = _context.elGamalAESEngine().decrypt(emsg1, privKey, skm); + byte dmsg1[] = e.decrypt(emsg1, privKey, skm); assertTrue(DataHelper.eq(dmsg1, msg1)); @@ -216,9 +221,9 @@ public class SessionEncryptionTest extends TestCase{ assertNotNull(curTag); - byte emsg2[] = _context.elGamalAESEngine().encrypt(msg2, pubKey, curKey, null, curTag, 64); + byte emsg2[] = e.encrypt(msg2, pubKey, curKey, null, curTag, 64); - byte dmsg2[] = _context.elGamalAESEngine().decrypt(emsg2, privKey, skm); + byte dmsg2[] = e.decrypt(emsg2, privKey, skm); assertTrue(DataHelper.eq(dmsg2, msg2)); @@ -229,9 +234,9 @@ public class SessionEncryptionTest extends TestCase{ assertNotNull(curTag); assertNotNull(curKey); - byte emsg3[] = _context.elGamalAESEngine().encrypt(msg3, pubKey, curKey, secondTags, curTag, nextKey, 64); + byte emsg3[] = e.encrypt(msg3, pubKey, curKey, secondTags, curTag, nextKey, 64); - byte dmsg3[] = _context.elGamalAESEngine().decrypt(emsg3, privKey, skm); + byte dmsg3[] = e.decrypt(emsg3, privKey, skm); assertTrue(DataHelper.eq(dmsg3, msg3)); @@ -245,9 +250,9 @@ public class SessionEncryptionTest extends TestCase{ assertNotNull(curTag); assertNotNull(curKey); - byte emsg4[] = _context.elGamalAESEngine().encrypt(msg4, pubKey, curKey, null, curTag, 64); + byte emsg4[] = e.encrypt(msg4, pubKey, curKey, null, curTag, 64); - byte dmsg4[] = _context.elGamalAESEngine().decrypt(emsg4, privKey, skm); + byte dmsg4[] = e.decrypt(emsg4, privKey, skm); assertTrue(DataHelper.eq(dmsg4, msg4)); @@ -258,9 +263,9 @@ public class SessionEncryptionTest extends TestCase{ assertNotNull(curTag); assertNotNull(curKey); - byte emsg5[] = _context.elGamalAESEngine().encrypt(msg5, pubKey, curKey, null, curTag, 64); + byte emsg5[] = e.encrypt(msg5, pubKey, curKey, null, curTag, 64); - byte dmsg5[] = _context.elGamalAESEngine().decrypt(emsg5, privKey, skm); + byte dmsg5[] = e.decrypt(emsg5, privKey, skm); assertTrue(DataHelper.eq(dmsg5, msg5)); @@ -278,6 +283,7 @@ public class SessionEncryptionTest extends TestCase{ SessionKeyManager skm = new TransientSessionKeyManager(_context); SessionKey curKey = skm.createSession(pubKey); + ElGamalAESEngine e = new ElGamalAESEngine(_context); for (int i = 0; i < 1000; i++) { Set<SessionTag> tags = null; SessionKey nextKey = null; @@ -293,9 +299,9 @@ public class SessionEncryptionTest extends TestCase{ byte[] msg = DataHelper.getASCII("msg " + i); - byte emsg[] = _context.elGamalAESEngine().encrypt(msg, pubKey, curKey, tags, curTag, nextKey, 64); + byte emsg[] = e.encrypt(msg, pubKey, curKey, tags, curTag, nextKey, 64); - byte dmsg[] = _context.elGamalAESEngine().decrypt(emsg, privKey, skm); + byte dmsg[] = e.decrypt(emsg, privKey, skm); assertTrue(DataHelper.eq(dmsg, msg)); if ( (tags != null) && (tags.size() > 0) ) {