forked from I2P_Developers/i2p.i2p
Test: Move ElGamalTest from core to router to follow tested class
This commit is contained in:
@ -24,7 +24,6 @@ public class CryptoTestSuite {
|
|||||||
suite.addTestSuite(CryptixAESEngineTest.class);
|
suite.addTestSuite(CryptixAESEngineTest.class);
|
||||||
suite.addTestSuite(CryptixRijndael_AlgorithmTest.class);
|
suite.addTestSuite(CryptixRijndael_AlgorithmTest.class);
|
||||||
suite.addTestSuite(DSATest.class);
|
suite.addTestSuite(DSATest.class);
|
||||||
suite.addTestSuite(ElGamalTest.class);
|
|
||||||
suite.addTestSuite(HMACSHA256Test.class);
|
suite.addTestSuite(HMACSHA256Test.class);
|
||||||
suite.addTestSuite(KeyGeneratorTest.class);
|
suite.addTestSuite(KeyGeneratorTest.class);
|
||||||
suite.addTestSuite(SHA1HashTest.class);
|
suite.addTestSuite(SHA1HashTest.class);
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package net.i2p.crypto;
|
package net.i2p.router.crypto;
|
||||||
/*
|
/*
|
||||||
* free (adj.): unencumbered; not under the control of others
|
* free (adj.): unencumbered; not under the control of others
|
||||||
* Written by jrandom in 2003 and released into the public domain
|
* Written by jrandom in 2003 and released into the public domain
|
||||||
@ -16,6 +16,8 @@ import java.util.Set;
|
|||||||
|
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
import net.i2p.I2PAppContext;
|
import net.i2p.I2PAppContext;
|
||||||
|
import net.i2p.crypto.KeyGenerator;
|
||||||
|
import net.i2p.crypto.SHA256Generator;
|
||||||
import net.i2p.data.Base64;
|
import net.i2p.data.Base64;
|
||||||
import net.i2p.data.DataFormatException;
|
import net.i2p.data.DataFormatException;
|
||||||
import net.i2p.data.DataHelper;
|
import net.i2p.data.DataHelper;
|
||||||
@ -24,6 +26,7 @@ import net.i2p.data.PrivateKey;
|
|||||||
import net.i2p.data.PublicKey;
|
import net.i2p.data.PublicKey;
|
||||||
import net.i2p.data.SessionKey;
|
import net.i2p.data.SessionKey;
|
||||||
import net.i2p.data.SessionTag;
|
import net.i2p.data.SessionTag;
|
||||||
|
import net.i2p.router.crypto.ElGamalAESEngine;
|
||||||
import net.i2p.util.RandomSource;
|
import net.i2p.util.RandomSource;
|
||||||
|
|
||||||
public class ElGamalTest extends TestCase{
|
public class ElGamalTest extends TestCase{
|
||||||
@ -126,7 +129,7 @@ public class ElGamalTest extends TestCase{
|
|||||||
|
|
||||||
protected void setUp() {
|
protected void setUp() {
|
||||||
_context = I2PAppContext.getGlobalContext();
|
_context = I2PAppContext.getGlobalContext();
|
||||||
Object o = YKGenerator.class;
|
//Object o = YKGenerator.class;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testBasicAES(){
|
public void testBasicAES(){
|
||||||
@ -155,12 +158,13 @@ public class ElGamalTest extends TestCase{
|
|||||||
|
|
||||||
String msg = "Hello world";
|
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>();
|
Set<SessionTag> foundTags = new HashSet<SessionTag>();
|
||||||
SessionKey foundKey = new SessionKey();
|
SessionKey foundKey = new SessionKey();
|
||||||
byte decrypted[] = null;
|
byte decrypted[] = null;
|
||||||
try{
|
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){
|
}catch(DataFormatException dfe){
|
||||||
dfe.printStackTrace();
|
dfe.printStackTrace();
|
||||||
fail();
|
fail();
|
||||||
@ -180,10 +184,11 @@ public class ElGamalTest extends TestCase{
|
|||||||
SessionKey key = _context.sessionKeyManager().getCurrentKey(pubKey);
|
SessionKey key = _context.sessionKeyManager().getCurrentKey(pubKey);
|
||||||
if (key == null)
|
if (key == null)
|
||||||
key = _context.sessionKeyManager().createSession(pubKey);
|
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;
|
byte[] decrypted = null;
|
||||||
try{
|
try{
|
||||||
decrypted = _context.elGamalAESEngine().decrypt(encrypted, privKey, _context.sessionKeyManager());
|
decrypted = e.decrypt(encrypted, privKey, _context.sessionKeyManager());
|
||||||
}catch(DataFormatException dfe){
|
}catch(DataFormatException dfe){
|
||||||
dfe.printStackTrace();
|
dfe.printStackTrace();
|
||||||
fail();
|
fail();
|
||||||
@ -256,6 +261,7 @@ public class ElGamalTest extends TestCase{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void testLoop(){
|
public void testLoop(){
|
||||||
|
ElGamalAESEngine e = new ElGamalAESEngine(_context);
|
||||||
for(int i = 0; i < 5; i++){
|
for(int i = 0; i < 5; i++){
|
||||||
Object keys[] = KeyGenerator.getInstance().generatePKIKeypair();
|
Object keys[] = KeyGenerator.getInstance().generatePKIKeypair();
|
||||||
PublicKey pubKey = (PublicKey)keys[0];
|
PublicKey pubKey = (PublicKey)keys[0];
|
||||||
@ -267,10 +273,10 @@ public class ElGamalTest extends TestCase{
|
|||||||
if (key == null)
|
if (key == null)
|
||||||
key = _context.sessionKeyManager().createSession(pubKey);
|
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;
|
byte[] decrypted = null;
|
||||||
try{
|
try{
|
||||||
decrypted = _context.elGamalAESEngine().decrypt(encrypted, privKey, _context.sessionKeyManager());
|
decrypted = e.decrypt(encrypted, privKey, _context.sessionKeyManager());
|
||||||
}catch(DataFormatException dfe){
|
}catch(DataFormatException dfe){
|
||||||
dfe.printStackTrace();
|
dfe.printStackTrace();
|
||||||
fail();
|
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(){
|
public void testYKGen(){
|
||||||
RandomSource.getInstance().nextBoolean();
|
RandomSource.getInstance().nextBoolean();
|
||||||
I2PAppContext context = I2PAppContext.getGlobalContext();
|
I2PAppContext context = I2PAppContext.getGlobalContext();
|
||||||
@ -378,4 +386,5 @@ public class ElGamalTest extends TestCase{
|
|||||||
ykgen.getNextYK();
|
ykgen.getNextYK();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
****/
|
||||||
}
|
}
|
@ -21,6 +21,7 @@ import net.i2p.data.PrivateKey;
|
|||||||
import net.i2p.data.PublicKey;
|
import net.i2p.data.PublicKey;
|
||||||
import net.i2p.data.SessionKey;
|
import net.i2p.data.SessionKey;
|
||||||
import net.i2p.data.SessionTag;
|
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[] msg = DataHelper.getASCII("msg 1");
|
||||||
|
|
||||||
byte emsg[] = _context.elGamalAESEngine().encrypt(msg, pubKey, curKey, null, null, 64);
|
ElGamalAESEngine e = new ElGamalAESEngine(_context);
|
||||||
byte dmsg[] = _context.elGamalAESEngine().decrypt(emsg, privKey, skm);
|
byte emsg[] = e.encrypt(msg, pubKey, curKey, null, null, 64);
|
||||||
|
byte dmsg[] = e.decrypt(emsg, privKey, skm);
|
||||||
assertTrue(DataHelper.eq(dmsg, msg));
|
assertTrue(DataHelper.eq(dmsg, msg));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,8 +64,9 @@ public class SessionEncryptionTest extends TestCase{
|
|||||||
|
|
||||||
byte[] msg = DataHelper.getASCII("msg 2");
|
byte[] msg = DataHelper.getASCII("msg 2");
|
||||||
|
|
||||||
byte emsg[] = _context.elGamalAESEngine().encrypt(msg, pubKey, curKey, null, null, 64);
|
ElGamalAESEngine e = new ElGamalAESEngine(_context);
|
||||||
byte dmsg[] = _context.elGamalAESEngine().decrypt(emsg, privKey, skm);
|
byte emsg[] = e.encrypt(msg, pubKey, curKey, null, null, 64);
|
||||||
|
byte dmsg[] = e.decrypt(emsg, privKey, skm);
|
||||||
assertTrue(DataHelper.eq(dmsg, msg));
|
assertTrue(DataHelper.eq(dmsg, msg));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -101,9 +104,10 @@ public class SessionEncryptionTest extends TestCase{
|
|||||||
byte[] msg4 = DataHelper.getASCII("msg 4");
|
byte[] msg4 = DataHelper.getASCII("msg 4");
|
||||||
byte[] msg5 = DataHelper.getASCII("msg 5");
|
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));
|
assertTrue(DataHelper.eq(dmsg1, msg1));
|
||||||
|
|
||||||
|
|
||||||
@ -116,9 +120,9 @@ public class SessionEncryptionTest extends TestCase{
|
|||||||
|
|
||||||
assertNotNull(curTag);
|
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));
|
assertTrue(DataHelper.eq(dmsg2, msg2));
|
||||||
|
|
||||||
|
|
||||||
@ -130,9 +134,9 @@ public class SessionEncryptionTest extends TestCase{
|
|||||||
assertNotNull(curTag);
|
assertNotNull(curTag);
|
||||||
assertNotNull(curKey);
|
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));
|
assertTrue(DataHelper.eq(dmsg3, msg3));
|
||||||
|
|
||||||
|
|
||||||
@ -146,9 +150,9 @@ public class SessionEncryptionTest extends TestCase{
|
|||||||
assertNotNull(curTag);
|
assertNotNull(curTag);
|
||||||
assertNotNull(curKey);
|
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));
|
assertTrue(DataHelper.eq(dmsg4, msg4));
|
||||||
|
|
||||||
|
|
||||||
@ -158,9 +162,9 @@ public class SessionEncryptionTest extends TestCase{
|
|||||||
assertNotNull(curTag);
|
assertNotNull(curTag);
|
||||||
assertNotNull(curKey);
|
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));
|
assertTrue(DataHelper.eq(dmsg5, msg5));
|
||||||
|
|
||||||
|
|
||||||
@ -201,9 +205,10 @@ public class SessionEncryptionTest extends TestCase{
|
|||||||
byte[] msg4 = DataHelper.getASCII("msg 4");
|
byte[] msg4 = DataHelper.getASCII("msg 4");
|
||||||
byte[] msg5 = DataHelper.getASCII("msg 5");
|
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));
|
assertTrue(DataHelper.eq(dmsg1, msg1));
|
||||||
|
|
||||||
|
|
||||||
@ -216,9 +221,9 @@ public class SessionEncryptionTest extends TestCase{
|
|||||||
|
|
||||||
assertNotNull(curTag);
|
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));
|
assertTrue(DataHelper.eq(dmsg2, msg2));
|
||||||
|
|
||||||
|
|
||||||
@ -229,9 +234,9 @@ public class SessionEncryptionTest extends TestCase{
|
|||||||
assertNotNull(curTag);
|
assertNotNull(curTag);
|
||||||
assertNotNull(curKey);
|
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));
|
assertTrue(DataHelper.eq(dmsg3, msg3));
|
||||||
|
|
||||||
|
|
||||||
@ -245,9 +250,9 @@ public class SessionEncryptionTest extends TestCase{
|
|||||||
assertNotNull(curTag);
|
assertNotNull(curTag);
|
||||||
assertNotNull(curKey);
|
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));
|
assertTrue(DataHelper.eq(dmsg4, msg4));
|
||||||
|
|
||||||
|
|
||||||
@ -258,9 +263,9 @@ public class SessionEncryptionTest extends TestCase{
|
|||||||
assertNotNull(curTag);
|
assertNotNull(curTag);
|
||||||
assertNotNull(curKey);
|
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));
|
assertTrue(DataHelper.eq(dmsg5, msg5));
|
||||||
|
|
||||||
|
|
||||||
@ -278,6 +283,7 @@ public class SessionEncryptionTest extends TestCase{
|
|||||||
SessionKeyManager skm = new TransientSessionKeyManager(_context);
|
SessionKeyManager skm = new TransientSessionKeyManager(_context);
|
||||||
SessionKey curKey = skm.createSession(pubKey);
|
SessionKey curKey = skm.createSession(pubKey);
|
||||||
|
|
||||||
|
ElGamalAESEngine e = new ElGamalAESEngine(_context);
|
||||||
for (int i = 0; i < 1000; i++) {
|
for (int i = 0; i < 1000; i++) {
|
||||||
Set<SessionTag> tags = null;
|
Set<SessionTag> tags = null;
|
||||||
SessionKey nextKey = null;
|
SessionKey nextKey = null;
|
||||||
@ -293,9 +299,9 @@ public class SessionEncryptionTest extends TestCase{
|
|||||||
|
|
||||||
byte[] msg = DataHelper.getASCII("msg " + i);
|
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));
|
assertTrue(DataHelper.eq(dmsg, msg));
|
||||||
|
|
||||||
if ( (tags != null) && (tags.size() > 0) ) {
|
if ( (tags != null) && (tags.size() > 0) ) {
|
||||||
|
Reference in New Issue
Block a user