use of net.i2p.data.PrivateKey in project i2p.i2p by i2p.
the class ElGamalTest method testElGamal.
public void testElGamal() {
for (int i = 0; i < 2; i++) {
Object[] keys = KeyGenerator.getInstance().generatePKIKeypair();
PublicKey pubKey = (PublicKey) keys[0];
PrivateKey privKey = (PrivateKey) keys[1];
SessionKey key = KeyGenerator.getInstance().generateSessionKey();
ByteArrayOutputStream elgSrc = new ByteArrayOutputStream(256);
try {
key.writeBytes(elgSrc);
} catch (DataFormatException dfe) {
dfe.printStackTrace();
fail();
} catch (IOException ioe) {
ioe.printStackTrace();
fail();
}
byte[] preIV = new byte[32];
RandomSource.getInstance().nextBytes(preIV);
try {
elgSrc.write(preIV);
elgSrc.flush();
} catch (IOException ioe) {
ioe.printStackTrace();
fail();
}
byte[] elgEncr = _context.elGamalEngine().encrypt(elgSrc.toByteArray(), pubKey);
byte[] elgDecr = _context.elGamalEngine().decrypt(elgEncr, privKey);
ByteArrayInputStream bais = new ByteArrayInputStream(elgDecr);
SessionKey nk = new SessionKey();
try {
nk.readBytes(bais);
} catch (DataFormatException dfe) {
dfe.printStackTrace();
fail();
} catch (IOException ioe) {
ioe.printStackTrace();
fail();
}
byte[] postpreIV = new byte[32];
int read = 0;
try {
read = bais.read(postpreIV);
} catch (IOException ioe) {
ioe.printStackTrace();
fail();
}
assertEquals(read, postpreIV.length);
assertTrue(DataHelper.eq(preIV, postpreIV));
assertEquals(key, nk);
}
}
use of net.i2p.data.PrivateKey in project i2p.i2p by i2p.
the class ElGamalTest method testLoop.
public void testLoop() {
for (int i = 0; i < 5; i++) {
Object[] keys = KeyGenerator.getInstance().generatePKIKeypair();
PublicKey pubKey = (PublicKey) keys[0];
PrivateKey privKey = (PrivateKey) keys[1];
byte[] msg = new byte[400];
RandomSource.getInstance().nextBytes(msg);
SessionKey key = _context.sessionKeyManager().getCurrentKey(pubKey);
if (key == null)
key = _context.sessionKeyManager().createSession(pubKey);
byte[] encrypted = _context.elGamalAESEngine().encrypt(msg, pubKey, key, null, null, 1024);
byte[] decrypted = null;
try {
decrypted = _context.elGamalAESEngine().decrypt(encrypted, privKey, _context.sessionKeyManager());
} catch (DataFormatException dfe) {
dfe.printStackTrace();
fail();
}
assertTrue(DataHelper.eq(msg, decrypted));
}
}
use of net.i2p.data.PrivateKey in project i2p.i2p by i2p.
the class ElGamalTest method testMultiple.
public void testMultiple() {
Object[] keys = KeyGenerator.getInstance().generatePKIKeypair();
byte[] message = new byte[222];
for (int x = 0; x < 25; x++) {
_context.random().nextBytes(message);
keys = KeyGenerator.getInstance().generatePKIKeypair();
PublicKey pubkey = (PublicKey) keys[0];
PrivateKey privkey = (PrivateKey) keys[1];
byte[] e = _context.elGamalEngine().encrypt(message, pubkey);
byte[] d = _context.elGamalEngine().decrypt(e, privkey);
assertTrue(DataHelper.eq(d, message));
}
}
use of net.i2p.data.PrivateKey in project i2p.i2p by i2p.
the class RouterInfoTest method createDataStructure.
@SuppressWarnings("deprecation")
public DataStructure createDataStructure() throws DataFormatException {
RouterInfo info = new RouterInfo();
HashSet<RouterAddress> addresses = new HashSet<RouterAddress>();
DataStructure structure = (new RouterAddressTest()).createDataStructure();
addresses.add((RouterAddress) structure);
info.setAddresses(addresses);
PublicKey pubKey = null;
SigningPublicKey signingPubKey = null;
PrivateKey privKey = null;
SigningPrivateKey signingPrivKey = null;
Object[] obj = KeyGenerator.getInstance().generatePKIKeypair();
pubKey = (PublicKey) obj[0];
privKey = (PrivateKey) obj[1];
obj = KeyGenerator.getInstance().generateSigningKeypair();
signingPubKey = (SigningPublicKey) obj[0];
signingPrivKey = (SigningPrivateKey) obj[1];
_log.debug("SigningPublicKey: " + signingPubKey);
_log.debug("SigningPrivateKey: " + signingPrivKey);
RouterIdentity ident = new RouterIdentity();
ident.setCertificate(new Certificate(Certificate.CERTIFICATE_TYPE_NULL, null));
ident.setPublicKey(pubKey);
ident.setSigningPublicKey(signingPubKey);
info.setIdentity(ident);
Properties options = new Properties();
for (int i = 0; i < 16; i++) {
options.setProperty("option." + i, "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890$:." + i);
}
options.setProperty("netConnectionSpeed", "OC12");
info.setOptions(options);
HashSet<Hash> peers = new HashSet<Hash>();
structure = (new HashTest()).createDataStructure();
peers.add((Hash) structure);
info.setPeers(peers);
info.setPublished(System.currentTimeMillis());
// info.setVersion(69);
info.sign(signingPrivKey);
return info;
}
use of net.i2p.data.PrivateKey in project i2p.i2p by i2p.
the class SessionEncryptionTest method testRekeying.
/**
* Run tagsIncluded useTag rekey
* 1 yes (2) no no
* 2 no yes no
* 3 yes (2) yes yes
* 4 no yes no
* 5 no yes no
*/
public void testRekeying() throws Exception {
Object[] keys = KeyGenerator.getInstance().generatePKIKeypair();
PublicKey pubKey = (PublicKey) keys[0];
PrivateKey privKey = (PrivateKey) keys[1];
SessionKeyManager skm = new TransientSessionKeyManager(_context);
SessionKey curKey = skm.createSession(pubKey);
SessionKey nextKey = KeyGenerator.getInstance().generateSessionKey();
SessionTag tag1 = new SessionTag(true);
SessionTag tag2 = new SessionTag(true);
SessionTag tag3 = new SessionTag(true);
SessionTag tag4 = new SessionTag(true);
HashSet<SessionTag> firstTags = new HashSet<SessionTag>();
firstTags.add(tag1);
firstTags.add(tag2);
HashSet<SessionTag> secondTags = new HashSet<SessionTag>();
secondTags.add(tag3);
secondTags.add(tag4);
byte[] msg1 = DataHelper.getASCII("msg 1");
byte[] msg2 = DataHelper.getASCII("msg 2");
byte[] msg3 = DataHelper.getASCII("msg 3");
byte[] msg4 = DataHelper.getASCII("msg 4");
byte[] msg5 = DataHelper.getASCII("msg 5");
byte[] emsg1 = _context.elGamalAESEngine().encrypt(msg1, pubKey, curKey, firstTags, null, 64);
byte[] dmsg1 = _context.elGamalAESEngine().decrypt(emsg1, privKey, skm);
assertTrue(DataHelper.eq(dmsg1, msg1));
TagSetHandle tsh = skm.tagsDelivered(pubKey, curKey, firstTags);
skm.tagsAcked(pubKey, curKey, tsh);
curKey = skm.getCurrentKey(pubKey);
SessionTag curTag = skm.consumeNextAvailableTag(pubKey, curKey);
assertNotNull(curTag);
byte[] emsg2 = _context.elGamalAESEngine().encrypt(msg2, pubKey, curKey, null, curTag, 64);
byte[] dmsg2 = _context.elGamalAESEngine().decrypt(emsg2, privKey, skm);
assertTrue(DataHelper.eq(dmsg2, msg2));
curKey = skm.getCurrentKey(pubKey);
curTag = skm.consumeNextAvailableTag(pubKey, curKey);
assertNotNull(curTag);
assertNotNull(curKey);
byte[] emsg3 = _context.elGamalAESEngine().encrypt(msg3, pubKey, curKey, secondTags, curTag, nextKey, 64);
byte[] dmsg3 = _context.elGamalAESEngine().decrypt(emsg3, privKey, skm);
assertTrue(DataHelper.eq(dmsg3, msg3));
// note nextKey not curKey
tsh = skm.tagsDelivered(pubKey, nextKey, secondTags);
skm.tagsAcked(pubKey, nextKey, tsh);
curKey = skm.getCurrentKey(pubKey);
curTag = skm.consumeNextAvailableTag(pubKey, curKey);
assertNotNull(curTag);
assertNotNull(curKey);
byte[] emsg4 = _context.elGamalAESEngine().encrypt(msg4, pubKey, curKey, null, curTag, 64);
byte[] dmsg4 = _context.elGamalAESEngine().decrypt(emsg4, privKey, skm);
assertTrue(DataHelper.eq(dmsg4, msg4));
curKey = skm.getCurrentKey(pubKey);
curTag = skm.consumeNextAvailableTag(pubKey, curKey);
assertNotNull(curTag);
assertNotNull(curKey);
byte[] emsg5 = _context.elGamalAESEngine().encrypt(msg5, pubKey, curKey, null, curTag, 64);
byte[] dmsg5 = _context.elGamalAESEngine().decrypt(emsg5, privKey, skm);
assertTrue(DataHelper.eq(dmsg5, msg5));
}
Aggregations