Search in sources :

Example 46 with SessionKey

use of net.i2p.data.SessionKey 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));
    }
}
Also used : PrivateKey(net.i2p.data.PrivateKey) DataFormatException(net.i2p.data.DataFormatException) PublicKey(net.i2p.data.PublicKey) SessionKey(net.i2p.data.SessionKey)

Example 47 with SessionKey

use of net.i2p.data.SessionKey in project i2p.i2p by i2p.

the class HMACSHA256Bench method runTest.

private static void runTest(I2PAppContext ctx) {
    SessionKey key = ctx.keyGenerator().generateSessionKey();
    byte[] output = new byte[32];
    ctx.hmac().calculate(key, "qwerty".getBytes(), 0, 6, output, 0);
    int times = 100000;
    long shorttime = 0;
    long medtime = 0;
    long longtime = 0;
    long minShort = 0;
    long maxShort = 0;
    long minMed = 0;
    long maxMed = 0;
    long minLong = 0;
    long maxLong = 0;
    long shorttime1 = 0;
    long medtime1 = 0;
    long longtime1 = 0;
    long minShort1 = 0;
    long maxShort1 = 0;
    long minMed1 = 0;
    long maxMed1 = 0;
    long minLong1 = 0;
    long maxLong1 = 0;
    byte[] smess = "abc".getBytes();
    StringBuilder buf = new StringBuilder();
    for (int x = 0; x < 2 * 1024; x++) {
        buf.append("a");
    }
    // new String("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq").getBytes();
    byte[] mmess = DataHelper.getASCII(buf.toString());
    buf = new StringBuilder();
    for (int x = 0; x < 10000; x++) {
        buf.append("a");
    }
    byte[] lmess = DataHelper.getASCII(buf.toString());
    // warm up the engines
    ctx.hmac().calculate(key, smess, 0, smess.length, output, 0);
    ctx.hmac().calculate(key, mmess, 0, mmess.length, output, 0);
    ctx.hmac().calculate(key, lmess, 0, lmess.length, output, 0);
    long before = System.currentTimeMillis();
    for (int x = 0; x < times; x++) ctx.hmac().calculate(key, smess, 0, smess.length, output, 0);
    long after = System.currentTimeMillis();
    display(times, before, after, smess.length, "3 byte");
    before = System.currentTimeMillis();
    for (int x = 0; x < times; x++) ctx.hmac().calculate(key, mmess, 0, mmess.length, output, 0);
    after = System.currentTimeMillis();
    display(times, before, after, mmess.length, "2KB");
    before = System.currentTimeMillis();
    for (int x = 0; x < times; x++) ctx.hmac().calculate(key, lmess, 0, lmess.length, output, 0);
    after = System.currentTimeMillis();
    display(times, before, after, lmess.length, "10KB");
}
Also used : SessionKey(net.i2p.data.SessionKey)

Example 48 with SessionKey

use of net.i2p.data.SessionKey in project i2p.i2p by i2p.

the class AES256Test method testLong.

@SuppressWarnings("deprecation")
public void testLong() {
    I2PAppContext ctx = new I2PAppContext();
    SessionKey key = ctx.keyGenerator().generateSessionKey();
    byte[] iv = new byte[16];
    RandomSource.getInstance().nextBytes(iv);
    byte[] lbuf = new byte[1024];
    RandomSource.getInstance().nextBytes(lbuf);
    byte[] le = ctx.aes().safeEncrypt(lbuf, key, iv, 2048);
    byte[] ld = ctx.aes().safeDecrypt(le, key, iv);
    assertTrue(DataHelper.eq(ld, lbuf));
}
Also used : I2PAppContext(net.i2p.I2PAppContext) SessionKey(net.i2p.data.SessionKey)

Example 49 with SessionKey

use of net.i2p.data.SessionKey in project i2p.i2p by i2p.

the class CryptixAESEngineTest method testEDBlock.

public static void testEDBlock() {
    I2PAppContext ctx = I2PAppContext.getGlobalContext();
    SessionKey key = ctx.keyGenerator().generateSessionKey();
    byte[] iv = new byte[16];
    byte[] orig = new byte[16];
    byte[] encrypted = new byte[16];
    byte[] decrypted = new byte[16];
    ctx.random().nextBytes(iv);
    ctx.random().nextBytes(orig);
    CryptixAESEngine aes = new CryptixAESEngine(ctx);
    aes.encryptBlock(orig, 0, key, encrypted, 0);
    aes.decryptBlock(encrypted, 0, key, decrypted, 0);
    assertTrue(DataHelper.eq(decrypted, orig));
}
Also used : I2PAppContext(net.i2p.I2PAppContext) SessionKey(net.i2p.data.SessionKey)

Example 50 with SessionKey

use of net.i2p.data.SessionKey in project i2p.i2p by i2p.

the class CryptixAESEngineTest method testED2.

public static void testED2() {
    I2PAppContext ctx = I2PAppContext.getGlobalContext();
    SessionKey key = ctx.keyGenerator().generateSessionKey();
    byte[] iv = new byte[16];
    byte[] orig = new byte[128];
    byte[] data = new byte[128];
    ctx.random().nextBytes(iv);
    ctx.random().nextBytes(orig);
    CryptixAESEngine aes = new CryptixAESEngine(ctx);
    aes.encrypt(orig, 0, data, 0, key, iv, data.length);
    aes.decrypt(data, 0, data, 0, key, iv, data.length);
    assertTrue(DataHelper.eq(data, orig));
}
Also used : I2PAppContext(net.i2p.I2PAppContext) SessionKey(net.i2p.data.SessionKey)

Aggregations

SessionKey (net.i2p.data.SessionKey)69 SessionTag (net.i2p.data.SessionTag)15 PublicKey (net.i2p.data.PublicKey)14 I2PAppContext (net.i2p.I2PAppContext)13 HashSet (java.util.HashSet)11 Hash (net.i2p.data.Hash)11 SessionKeyManager (net.i2p.crypto.SessionKeyManager)10 PrivateKey (net.i2p.data.PrivateKey)10 InetAddress (java.net.InetAddress)9 DataFormatException (net.i2p.data.DataFormatException)9 UnknownHostException (java.net.UnknownHostException)7 TagSetHandle (net.i2p.crypto.TagSetHandle)5 Map (java.util.Map)4 GarlicMessage (net.i2p.data.i2np.GarlicMessage)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Set (java.util.Set)3 EncryptedBuildRecord (net.i2p.data.i2np.EncryptedBuildRecord)3 BigInteger (java.math.BigInteger)2