use of net.i2p.data.SessionKey in project i2p.i2p by i2p.
the class MessageWrapper method wrap.
/**
* Garlic wrap a message from nobody, destined for a router,
* to hide the contents from the OBEP.
* Forces ElGamal.
*
* @return null on encrypt failure
* @since 0.9.5
*/
static GarlicMessage wrap(RouterContext ctx, I2NPMessage m, RouterInfo to) {
PayloadGarlicConfig payload = new PayloadGarlicConfig();
payload.setCertificate(Certificate.NULL_CERT);
payload.setId(ctx.random().nextLong(I2NPMessage.MAX_ID_VALUE));
payload.setPayload(m);
payload.setRecipient(to);
payload.setDeliveryInstructions(DeliveryInstructions.LOCAL);
payload.setExpiration(m.getMessageExpiration());
SessionKey sentKey = ctx.keyGenerator().generateSessionKey();
PublicKey key = to.getIdentity().getPublicKey();
GarlicMessage msg = GarlicMessageBuilder.buildMessage(ctx, payload, null, null, key, sentKey, null);
return msg;
}
use of net.i2p.data.SessionKey in project i2p.i2p-bote by i2p.
the class Util method encrypt.
/**
* Encrypts data with an I2P public key
*/
public static byte[] encrypt(byte[] data, PublicKey key) {
I2PAppContext appContext = I2PAppContext.getGlobalContext();
SessionKeyManager sessionKeyMgr = new net.i2p.crypto.SessionKeyManager(appContext) {
};
SessionKey sessionKey = sessionKeyMgr.createSession(key);
return appContext.elGamalAESEngine().encrypt(data, key, sessionKey, null, null, null, 0);
}
use of net.i2p.data.SessionKey in project i2p.i2p-bote by i2p.
the class EncryptedOutputStream method encryptAndWrite.
/**
* Writes the header, then encrypts the internal buffer and writes the encrypted
* data to the underlying <code>OutputStream</code>.
* @throws IOException
*/
// for net.i2p.crypto.AESEngine
@SuppressWarnings("deprecation")
private void encryptAndWrite() throws IOException {
downstream.write(START_OF_FILE);
downstream.write(FORMAT_VERSION);
FileEncryptionConstants.KDF_PARAMETERS.writeTo(downstream);
downstream.write(derivedKey.salt);
byte[] iv = new byte[BLOCK_SIZE];
I2PAppContext appContext = I2PAppContext.getGlobalContext();
appContext.random().nextBytes(iv);
downstream.write(iv);
byte[] data = outputBuffer.toByteArray();
SessionKey key = new SessionKey(derivedKey.key);
byte[] encryptedData = appContext.aes().safeEncrypt(data, key, iv, 0);
downstream.write(encryptedData);
}
use of net.i2p.data.SessionKey in project i2p.i2p by i2p.
the class ConfigKeyringHandler method processForm.
@Override
protected void processForm() {
if (_action == null)
return;
boolean adding = _action.equals(_t("Add key"));
if (adding || _action.equals(_t("Delete key"))) {
if (_peer == null)
addFormError(_t("You must enter a destination"));
if (_key == null && adding)
addFormError(_t("You must enter a key"));
if (_peer == null || (_key == null && adding))
return;
Hash h = ConvertToHash.getHash(_peer);
if (adding) {
SessionKey sk = new SessionKey();
try {
sk.fromBase64(_key);
} catch (DataFormatException dfe) {
}
if (h == null || h.getData() == null) {
addFormError(_t("Invalid destination"));
} else if (_context.clientManager().isLocal(h)) {
// don't bother translating
addFormError("Cannot add key for local destination. Enable encryption in the Hidden Services Manager.");
} else if (sk.getData() == null) {
addFormError(_t("Invalid key"));
} else {
_context.keyRing().put(h, sk);
addFormNotice(_t("Key for {0} added to keyring", h.toBase32()));
}
} else {
// Delete
if (h != null && h.getData() != null) {
if (_context.clientManager().isLocal(h)) {
// don't bother translating
addFormError("Cannot remove key for local destination. Disable encryption in the Hidden Services Manager.");
} else if (_context.keyRing().remove(h) != null) {
addFormNotice(_t("Key for {0} removed from keyring", h.toBase32()));
} else {
addFormNotice(_t("Key for {0} not found in keyring", h.toBase32()));
}
} else {
addFormError(_t("Invalid destination"));
}
}
} else {
// addFormError(_t("Unsupported"));
}
}
use of net.i2p.data.SessionKey in project i2p.i2p by i2p.
the class SessionKeyManager method createSession.
/**
* Generate a new session key and associate it with the specified target.
*
* Racy if called after getCurrentKey() to check for a current session;
* use getCurrentOrNewKey() in that case.
*/
public SessionKey createSession(PublicKey target) {
SessionKey key = KeyGenerator.getInstance().generateSessionKey();
createSession(target, key);
return key;
}
Aggregations