/** * Reconstruct key from got byte array. Calls when we receive session key from remote party. * @param key is byte array with packed key. */ public void reconstructSessionKey(byte[] key) throws EncryptionError { sessionKey = new SymmetricKey(key); }
public NetworkV2(NetConfig netConfig, NodeInfo myInfo, PrivateKey myKey) throws IOException { super(netConfig); this.myInfo = myInfo; this.myKey = myKey; adapter = new UDPAdapter(myKey, new SymmetricKey(), myInfo, netConfig); // adapter.setVerboseLevel(DatagramAdapter.VerboseLevel.BASE); adapter.receive(this::onReceived); adapter.addErrorsCallback(this::exceptionCallback); }
/** * Restart {@link UDPAdapter} * @throws IOException if something went wrong */ public void restartUDPAdapter() throws IOException { if(adapter != null) adapter.shutdown(); adapter = new UDPAdapter(myKey, new SymmetricKey(), myInfo, netConfig); adapter.receive(this::onReceived); adapter.addErrorsCallback(this::exceptionCallback); }
/** * Helps to create block cipher with a given class and key, by properly initializing it. * @param cipherClass cipher class to instantiate * @param key * @return properly initialized encpytor * @throws EncryptionError */ public static BlockCipher makeCipher(Class<? extends BlockCipher> cipherClass, byte[] key) throws EncryptionError { try { BlockCipher cipher = cipherClass.newInstance(); cipher.initialize(BlockCipher.Direction.DECRYPT, new SymmetricKey(key)); return cipher; } catch (InstantiationException | IllegalAccessException e) { throw new EncryptionError("failed to instantiate BlockCipher class " + cipherClass .getName(), e); } }
public AbstractKey unpackKey(byte[] data) throws EncryptionError { switch (algorythm) { case RSAPublic: return new PublicKey(data, this); case RSAPrivate: return new PrivateKey(data, this); case AES256: return new SymmetricKey(data, this); } throw new EncryptionError("can't unpack key: " + this); }
private void createSessionKey() throws EncryptionError { if (sessionKey == null) { sessionKey = new SymmetricKey(); Binder data = Binder.fromKeysValues( "sk", sessionKey.pack() ); encryptedAnswer = publicKey.encrypt(Boss.pack(data)); } }
/** * We have received {@link PacketTypes#ACK} packet. Need to stop retransmitting of ack-ed packet. * @param packet received {@link Packet} */ private void onReceiveAck(Packet packet) throws EncryptionError, SymmetricKey.AuthenticationFailed { report(logLabel, ()->"received ack from " + packet.senderNodeId, VerboseLevel.DETAILED); Session session = getOrCreateSession(packet.senderNodeId); if (session != null) { if (session.state.get() == Session.STATE_EXCHANGING) { Integer ackPacketId = Boss.load(new SymmetricKey(session.sessionKey.getKey()).etaDecrypt(packet.payload)); session.removePacketFromRetransmitMap(ackPacketId); } } }
public TestLocalNetwork(NetConfig netConfig, NodeInfo myInfo, PrivateKey myKey) throws IOException { super(netConfig); this.myInfo = myInfo; this.myKey = myKey; adapter = new UDPAdapter(myKey, new SymmetricKey(), myInfo, netConfig); // adapter.setVerboseLevel(DatagramAdapter.VerboseLevel.BASE); adapter.receive(this::onReceived); adapter.addErrorsCallback(this::exceptionCallback); }
/** * ACK packets are used only for respond to DATA packets. Retransmission of handshake's packet types stops on each * next handshake step. But last step need to be ACK-ed. For this used {@link PacketTypes#SESSION_ACK} packet. * @param session is {@link Session} in which sending is. * @throws EncryptionError */ private void sendSessionAck(Session session) throws EncryptionError { report(logLabel, ()->"send session_ack to "+session.remoteNodeInfo.getNumber(), VerboseLevel.BASE); Packet packet = new Packet(0, myNodeInfo.getNumber(), session.remoteNodeInfo.getNumber(), PacketTypes.SESSION_ACK, new SymmetricKey(session.sessionKey.getKey()).etaEncrypt(Do.randomBytes(32))); sendPacket(session.remoteNodeInfo, packet); }
/** * Each adapter will try to send blocks until have got special {@link Packet} with type {@link PacketTypes#ACK}, * that means receiver have got block. So when we got packet and all is ok - call this method. * @param sessionReader is {@link SessionReader} in which sending is. * @param packetId is id of packet we have got. */ private void sendAck(SessionReader sessionReader, Integer packetId) throws EncryptionError { report(logLabel, ()->"send ack to "+sessionReader.remoteNodeInfo.getNumber(), VerboseLevel.DETAILED); Packet packet = new Packet(0, myNodeInfo.getNumber(), sessionReader.remoteNodeInfo.getNumber(), PacketTypes.ACK, new SymmetricKey(sessionReader.sessionKey.getKey()).etaEncrypt(Boss.pack(packetId))); sendPacket(sessionReader.remoteNodeInfo, packet); }
@Test public void bytesRange() throws Exception { SymmetricKey k = new SymmetricKey(); byte[] src = new byte[4]; src[0] = (byte) 255; src[1] = (byte) 254; src[2] = (byte) 253; src[3] = (byte) 252; assertThat(k.decrypt(k.encrypt(src)), equalTo(src)); }
public static BasicHttpClientSession reconstructSession(Binder binder) { BasicHttpClientSession restoringSession = new BasicHttpClientSession(); restoringSession.setConnectMessage(binder.getOrThrow("connectMessage")); // restoringSession.privateKey = binder.getBinaryOrThrowgetBinaryOrThrowgetBinaryOrThrow("privateKey"); restoringSession.setSessionKey(new SymmetricKey(binder.getBinaryOrThrow("sessionKey"))); restoringSession.setSessionId(binder.getLongOrThrow("sessionId")); // restoringSession.nodePublicKey = binder.getBinaryOrThrow("nodePublicKey"); return restoringSession; }
private void processHelloAnswer(Binder result) throws EncryptionError { byte[] data = result.getBinaryOrThrow("data"); byte[] signature = result.getBinaryOrThrow("signature"); setRemoteKey(result.getBinaryOrThrow("public_key")); if (!remoteKey.verify(data, signature, HashType.SHA256)) throw new EncryptionError("bad signature in hello answer"); Binder answer = Boss.unpack(myKey.decrypt(data)); if (!Arrays.equals(answer.getBinaryOrThrow("nonce"), myNonce)) throw new EncryptionError("nonce mismatch"); remoteSessionKey = new SymmetricKey(answer.getBinary("session_key")); }
@Test public void bigVolume() throws Exception { SymmetricKey k = new SymmetricKey(); byte[] src = CTRTransformer.randomBytes(0x23456); assertThat(k.decrypt(k.encrypt(src)), equalTo(src)); }
/** * Test {@link AES256} wrapper for decrypting. * */ @org.junit.Test public void encrypt() throws EncryptionError { AES256 engine = new AES256(); engine.initialize(BlockCipher.Direction.ENCRYPT, new SymmetricKey(key1)); byte[] result = engine.transformBlock(plaintext1); assertArrayEquals(ciphertext1, result); } /**
@Test public void etaEncrypt() throws Exception { SymmetricKey k = new SymmetricKey(); byte[] plainText = "Hello, world!".getBytes(); byte[] cipherText = k.etaEncrypt(plainText); // Bytes.dump(cipherText); assertEquals(16 + 32 + plainText.length, cipherText.length); byte[] decryptedText = k.etaDecrypt(cipherText); assertArrayEquals(plainText, decryptedText); exception.expect(SymmetricKey.AuthenticationFailed.class); cipherText[19] += 1; k.etaDecrypt(cipherText); }
/** * Test {@link AES256} wrapper for decrypting. * */ @org.junit.Test public void decrypt() throws EncryptionError { AES256 engine = new AES256(); engine.initialize(BlockCipher.Direction.DECRYPT, new SymmetricKey(key1)); byte[] result = engine.transformBlock(ciphertext1); assertArrayEquals(plaintext1, result); } }
@Test public void symmetricKeyMustHaveRightInfo() { SymmetricKey k = new SymmetricKey(); KeyInfo h = k.info(); assertEquals(KeyInfo.Algorythm.AES256, h.getAlgorythm()); assertEquals(32, h.getKeyLength()); assertEquals(KeyInfo.PRF.None, h.getPRF()); }
@Test public void saveAndRestore() throws Exception { KeyRing kr = new KeyRing(); SymmetricKey sk1 = new SymmetricKey(); SymmetricKey sk2 = new SymmetricKey(); AbstractKey privateKey = TestKeys.privateKey(0); AbstractKey publicKey1 = TestKeys.privateKey(1).getPublicKey(); AbstractKey publicKey2 = privateKey.getPublicKey(); kr.addKeys( sk1, sk2, privateKey, publicKey1, publicKey2); Binder b = kr.toBinder(); KeyRing kr2 = KeyRing.fromBinder(b); for(AbstractKey k: kr2.keySet()) { assertTrue(kr2.contains(k)); assertTrue(kr.contains(k)); } assertEquals(kr, kr2); }
@Test public void testEncrypted() throws Exception { Capsule c1 = new Capsule(); c1.setPrivateData("hello", "world", "I'm", "the coffer"); PrivateKey k1 = TestKeys.privateKey(0); PrivateKey k2 = TestKeys.privateKey(1); SymmetricKey k3 = new SymmetricKey(); SymmetricKey k4 = new SymmetricKey(); c1.addKeys(k1.getPublicKey()); c1.addKeys(k3); byte[] packed = c1.pack(); Capsule c2 = new Capsule(packed, new KRing(k3)); assertEquals(c1, c2); c2 = new Capsule(packed, k1.asKeySource()); assertEquals(c1, c2); exception.expect(Capsule.DecryptionFailedException.class); c2 = new Capsule(packed, new KRing(k2)); assertEquals(c1, c2); }