use of net.i2p.data.PublicKey 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.PublicKey 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));
}
use of net.i2p.data.PublicKey in project i2p.i2p by i2p.
the class SessionEncryptionTest method testLongSession.
/**
* 20 tags every 10 messages, rekey every 50
*/
public void testLongSession() 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);
for (int i = 0; i < 1000; i++) {
Set<SessionTag> tags = null;
SessionKey nextKey = null;
curKey = skm.getCurrentKey(pubKey);
SessionTag curTag = skm.consumeNextAvailableTag(pubKey, curKey);
int availTags = skm.getAvailableTags(pubKey, curKey);
if ((availTags < 1)) {
tags = generateNewTags(50);
}
if (i % 50 == 0)
nextKey = KeyGenerator.getInstance().generateSessionKey();
byte[] msg = DataHelper.getASCII("msg " + i);
byte[] emsg = _context.elGamalAESEngine().encrypt(msg, pubKey, curKey, tags, curTag, nextKey, 64);
byte[] dmsg = _context.elGamalAESEngine().decrypt(emsg, privKey, skm);
assertTrue(DataHelper.eq(dmsg, msg));
if ((tags != null) && (tags.size() > 0)) {
if (nextKey == null) {
TagSetHandle tsh = skm.tagsDelivered(pubKey, curKey, tags);
skm.tagsAcked(pubKey, curKey, tsh);
} else {
TagSetHandle tsh = skm.tagsDelivered(pubKey, nextKey, tags);
skm.tagsAcked(pubKey, nextKey, tsh);
}
}
}
}
use of net.i2p.data.PublicKey in project i2p.i2p by i2p.
the class SessionEncryptionTest method testNoSessions2.
public void testNoSessions2() 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);
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);
assertTrue(DataHelper.eq(dmsg, msg));
}
use of net.i2p.data.PublicKey in project i2p.i2p by i2p.
the class RouterIdentityTest method createDataStructure.
public DataStructure createDataStructure() throws DataFormatException {
RouterIdentity ident = new RouterIdentity();
Certificate cert = (Certificate) (new CertificateTest()).createDataStructure();
ident.setCertificate(cert);
PublicKey pk = (PublicKey) (new PublicKeyTest()).createDataStructure();
ident.setPublicKey(pk);
SigningPublicKey k = (SigningPublicKey) (new SigningPublicKeyTest()).createDataStructure();
ident.setSigningPublicKey(k);
return ident;
}
Aggregations