Search in sources :

Example 21 with PrivateKey

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);
    }
}
Also used : PrivateKey(net.i2p.data.PrivateKey) DataFormatException(net.i2p.data.DataFormatException) ByteArrayInputStream(java.io.ByteArrayInputStream) PublicKey(net.i2p.data.PublicKey) SessionKey(net.i2p.data.SessionKey) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 22 with PrivateKey

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

Example 23 with PrivateKey

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

Example 24 with PrivateKey

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;
}
Also used : SigningPublicKey(net.i2p.data.SigningPublicKey) PrivateKey(net.i2p.data.PrivateKey) SigningPrivateKey(net.i2p.data.SigningPrivateKey) SigningPublicKey(net.i2p.data.SigningPublicKey) PublicKey(net.i2p.data.PublicKey) DataStructure(net.i2p.data.DataStructure) Properties(java.util.Properties) Hash(net.i2p.data.Hash) HashTest(net.i2p.data.HashTest) SigningPrivateKey(net.i2p.data.SigningPrivateKey) HashSet(java.util.HashSet) Certificate(net.i2p.data.Certificate)

Example 25 with PrivateKey

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));
}
Also used : PrivateKey(net.i2p.data.PrivateKey) PublicKey(net.i2p.data.PublicKey) SessionKey(net.i2p.data.SessionKey) SessionKeyManager(net.i2p.crypto.SessionKeyManager) SessionTag(net.i2p.data.SessionTag) HashSet(java.util.HashSet) TagSetHandle(net.i2p.crypto.TagSetHandle)

Aggregations

PrivateKey (net.i2p.data.PrivateKey)28 PublicKey (net.i2p.data.PublicKey)23 DataFormatException (net.i2p.data.DataFormatException)11 SigningPrivateKey (net.i2p.data.SigningPrivateKey)11 SessionKey (net.i2p.data.SessionKey)10 IOException (java.io.IOException)8 SigningPublicKey (net.i2p.data.SigningPublicKey)8 SessionKeyManager (net.i2p.crypto.SessionKeyManager)6 SigType (net.i2p.crypto.SigType)6 SimpleDataStructure (net.i2p.data.SimpleDataStructure)6 HashSet (java.util.HashSet)5 Destination (net.i2p.data.Destination)5 GeneralSecurityException (java.security.GeneralSecurityException)4 SessionTag (net.i2p.data.SessionTag)4 BufferedInputStream (java.io.BufferedInputStream)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 File (java.io.File)3 FileInputStream (java.io.FileInputStream)3 InputStream (java.io.InputStream)3 Properties (java.util.Properties)3