use of net.java.otr4j.io.OtrOutputStream in project Zom-Android by zom.
the class OtrSm method computeSessionId.
/* Compute secret session ID as hash of agreed secret */
private static byte[] computeSessionId(BigInteger s) throws SMException {
byte[] sdata;
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
OtrOutputStream oos = new OtrOutputStream(out);
oos.write(0x00);
oos.writeBigInt(s);
sdata = out.toByteArray();
oos.close();
} catch (IOException e1) {
throw new SMException(e1);
}
/* Calculate the session id */
MessageDigest sha256;
try {
sha256 = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
throw new SMException("cannot find SHA-256");
}
byte[] res = sha256.digest(sdata);
byte[] secure_session_id = new byte[8];
System.arraycopy(res, 0, secure_session_id, 0, 8);
return secure_session_id;
}
use of net.java.otr4j.io.OtrOutputStream in project Zom-Android by zom.
the class SessionImpl method transformSending.
// Retransmit last sent message. Spec document does not mention where or
// when that should happen, must check libotr code.
public String transformSending(String msgText, List<TLV> tlvs) throws OtrException {
switch(this.getSessionStatus()) {
case PLAINTEXT:
if (getSessionPolicy().getRequireEncryption()) {
lastSentMessage = msgText;
doTransmitLastMessage = true;
this.startSession();
return null;
} else
// specification.
return msgText;
case ENCRYPTED:
this.lastSentMessage = msgText;
if (DEBUG_ENABLED)
Log.d(LOG_TAG, getSessionID().getLocalUserId() + " sends an encrypted message to " + getSessionID().getRemoteUserId() + " through " + getSessionID().getProtocolName() + ".");
// Get encryption keys.
SessionKeys encryptionKeys = this.getEncryptionSessionKeys();
int senderKeyID = encryptionKeys.getLocalKeyID();
int receipientKeyID = encryptionKeys.getRemoteKeyID();
// Increment CTR.
encryptionKeys.incrementSendingCtr();
byte[] ctr = encryptionKeys.getSendingCtr();
ByteArrayOutputStream out = new ByteArrayOutputStream();
if (msgText != null && msgText.length() > 0)
try {
out.write(msgText.getBytes("UTF8"));
} catch (IOException e) {
throw new OtrException(e);
}
// Append tlvs
if (tlvs != null && tlvs.size() > 0) {
out.write((byte) 0x00);
OtrOutputStream eoos = new OtrOutputStream(out);
for (TLV tlv : tlvs) {
try {
eoos.writeShort(tlv.type);
eoos.writeTlvData(tlv.value);
eoos.close();
} catch (IOException e) {
throw new OtrException(e);
}
}
}
OtrCryptoEngine otrCryptoEngine = new OtrCryptoEngineImpl();
byte[] data = out.toByteArray();
// Encrypt message.
if (DEBUG_ENABLED)
Log.d(LOG_TAG, "Encrypting message with keyids (localKeyID, remoteKeyID) = (" + senderKeyID + ", " + receipientKeyID + ")");
byte[] encryptedMsg = otrCryptoEngine.aesEncrypt(encryptionKeys.getSendingAESKey(), ctr, data);
// Get most recent keys to get the next D-H public key.
SessionKeys mostRecentKeys = this.getMostRecentSessionKeys();
DHPublicKey nextDH = (DHPublicKey) mostRecentKeys.getLocalPair().getPublic();
// Calculate T.
MysteriousT t = new MysteriousT(2, 0, senderKeyID, receipientKeyID, nextDH, ctr, encryptedMsg);
// Calculate T hash.
byte[] sendingMACKey = encryptionKeys.getSendingMACKey();
if (DEBUG_ENABLED)
Log.d(LOG_TAG, "Transforming T to byte[] to calculate it's HmacSHA1.");
byte[] serializedT;
try {
serializedT = SerializationUtils.toByteArray(t);
} catch (IOException e) {
throw new OtrException(e);
}
byte[] mac = otrCryptoEngine.sha1Hmac(serializedT, sendingMACKey, SerializationConstants.TYPE_LEN_MAC);
// Get old MAC keys to be revealed.
byte[] oldKeys = this.collectOldMacKeys();
DataMessage m = new DataMessage(t, mac, oldKeys);
try {
return SerializationUtils.toString(m);
} catch (IOException e) {
throw new OtrException(e);
}
case FINISHED:
this.lastSentMessage = msgText;
showError("Your message to " + sessionID.getRemoteUserId() + " was not sent. Either end your private conversation, or restart it.");
return null;
default:
if (DEBUG_ENABLED)
Log.d(LOG_TAG, "Unknown message state, not processing.");
return msgText;
}
}
use of net.java.otr4j.io.OtrOutputStream in project Zom-Android by zom.
the class IOTest method testIOShort.
public void testIOShort() throws Exception {
int source = 10;
ByteArrayOutputStream out = new ByteArrayOutputStream();
OtrOutputStream oos = new OtrOutputStream(out);
oos.writeShort(source);
oos.close();
byte[] converted = out.toByteArray();
ByteArrayInputStream bin = new ByteArrayInputStream(converted);
OtrInputStream ois = new OtrInputStream(bin);
int result = ois.readShort();
ois.close();
assertEquals(source, result);
}
use of net.java.otr4j.io.OtrOutputStream in project Zom-Android by zom.
the class IOTest method testIODHPublicKey.
public void testIODHPublicKey() throws Exception {
KeyPair pair = new OtrCryptoEngineImpl().generateDHKeyPair();
DHPublicKey source = (DHPublicKey) pair.getPublic();
ByteArrayOutputStream out = new ByteArrayOutputStream();
OtrOutputStream oos = new OtrOutputStream(out);
oos.writeDHPublicKey(source);
oos.close();
byte[] converted = out.toByteArray();
ByteArrayInputStream bin = new ByteArrayInputStream(converted);
OtrInputStream ois = new OtrInputStream(bin);
DHPublicKey result = ois.readDHPublicKey();
ois.close();
assertTrue(source.getY().compareTo(result.getY()) == 0);
}
use of net.java.otr4j.io.OtrOutputStream in project Zom-Android by zom.
the class IOTest method testIOData.
public void testIOData() throws Exception {
byte[] source = new byte[] { 1, 1, 1, 1 };
ByteArrayOutputStream out = new ByteArrayOutputStream();
OtrOutputStream oos = new OtrOutputStream(out);
oos.writeData(source);
oos.close();
byte[] converted = out.toByteArray();
ByteArrayInputStream bin = new ByteArrayInputStream(converted);
OtrInputStream ois = new OtrInputStream(bin);
byte[] result = ois.readData();
ois.close();
assertTrue(java.util.Arrays.equals(source, result));
}
Aggregations