use of net.i2p.data.PublicKey in project i2p.i2p by i2p.
the class ElGamalTest method testVerifySelf.
public void testVerifySelf() {
Object[] keypair = _context.keyGenerator().generatePKIKeypair();
PublicKey pub = (PublicKey) keypair[0];
PrivateKey priv = (PrivateKey) keypair[1];
for (int i = 0; i < UNENCRYPTED.length; i++) {
byte[] orig = DataHelper.getASCII(UNENCRYPTED[i]);
byte[] encrypted = _context.elGamalEngine().encrypt(orig, pub);
byte[] decrypted = _context.elGamalEngine().decrypt(encrypted, priv);
assertTrue(DataHelper.eq(decrypted, orig));
}
}
use of net.i2p.data.PublicKey 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.PublicKey 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.PublicKey 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.PublicKey in project i2p.i2p by i2p.
the class BuildMessageTestStandalone method testBuildMessage.
public void testBuildMessage() {
I2PAppContext ctx = I2PAppContext.getGlobalContext();
Log log = ctx.logManager().getLog(getClass());
List<Integer> order = pickOrder();
TunnelCreatorConfig cfg = createConfig(ctx);
_replyRouter = new Hash();
byte[] h = new byte[Hash.HASH_LENGTH];
Arrays.fill(h, (byte) 0xFF);
_replyRouter.setData(h);
_replyTunnel = 42;
// populate and encrypt the message
TunnelBuildMessage msg = new TunnelBuildMessage(ctx);
for (int i = 0; i < order.size(); i++) {
int hop = order.get(i).intValue();
PublicKey key = null;
if (hop < _pubKeys.length)
key = _pubKeys[hop];
BuildMessageGenerator.createRecord(i, hop, msg, cfg, _replyRouter, _replyTunnel, ctx, key);
}
BuildMessageGenerator.layeredEncrypt(ctx, msg, cfg, order);
log.debug("\n================================================================" + "\nMessage fully encrypted" + "\n================================================================");
// now msg is fully encrypted, so lets go through the hops, decrypting and replying
// as necessary
BuildMessageProcessor proc = new BuildMessageProcessor(ctx);
for (int i = 0; i < cfg.getLength(); i++) {
// this not only decrypts the current hop's record, but encrypts the other records
// with the reply key
BuildRequestRecord req = proc.decrypt(msg, _peers[i], _privKeys[i]);
// If false, no records matched the _peers[i], or the decryption failed
assertTrue("foo @ " + i, req != null);
long ourId = req.readReceiveTunnelId();
byte[] replyIV = req.readReplyIV();
long nextId = req.readNextTunnelId();
Hash nextPeer = req.readNextIdentity();
boolean isInGW = req.readIsInboundGateway();
boolean isOutEnd = req.readIsOutboundEndpoint();
long time = req.readRequestTime();
long now = (ctx.clock().now() / (60l * 60l * 1000l)) * (60 * 60 * 1000);
int ourSlot = -1;
EncryptedBuildRecord reply = BuildResponseRecord.create(ctx, 0, req.readReplyKey(), req.readReplyIV(), -1);
for (int j = 0; j < TunnelBuildMessage.MAX_RECORD_COUNT; j++) {
if (msg.getRecord(j) == null) {
ourSlot = j;
msg.setRecord(j, reply);
break;
}
}
log.debug("Read slot " + ourSlot + " containing hop " + i + " @ " + _peers[i].toBase64() + " receives on " + ourId + " w/ replyIV " + Base64.encode(replyIV) + " sending to " + nextId + " on " + nextPeer.toBase64() + " inGW? " + isInGW + " outEnd? " + isOutEnd + " time difference " + (now - time));
}
log.debug("\n================================================================" + "\nAll hops traversed and replies gathered" + "\n================================================================");
// now all of the replies are populated, toss 'em into a reply message and handle it
TunnelBuildReplyMessage reply = new TunnelBuildReplyMessage(ctx);
for (int i = 0; i < TunnelBuildMessage.MAX_RECORD_COUNT; i++) reply.setRecord(i, msg.getRecord(i));
int[] statuses = (new BuildReplyHandler(ctx)).decrypt(reply, cfg, order);
if (statuses == null)
throw new RuntimeException("bar");
boolean allAgree = true;
for (int i = 0; i < cfg.getLength(); i++) {
Hash peer = cfg.getPeer(i);
int record = order.get(i).intValue();
if (statuses[record] != 0)
allAgree = false;
// else
// penalize peer according to the rejection cause
}
log.debug("\n================================================================" + "\nAll peers agree? " + allAgree + "\n================================================================");
}
Aggregations