use of net.java.otr4j.crypto.SM.SMException 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.crypto.SM.SMException in project Zom-Android by zom.
the class OtrSm method initRespondSmp.
/**
* Respond to or initiate an SMP negotiation
*
* @param question The question to present to the peer, if initiating. May
* be null for no question.
* @param secret The secret.
* @param initiating Whether we are initiating or responding to an initial
* request.
*
* @return TLVs to send to the peer
*/
public List<TLV> initRespondSmp(String question, String secret, boolean initiating) throws OtrException {
if (question != null && !initiating)
throw new IllegalArgumentException("Only supply a question if initiating");
/*
* Construct the combined secret as a SHA256 hash of:
* Version byte (0x01), Initiator fingerprint (20 bytes),
* responder fingerprint (20 bytes), secure session id, input secret
*/
byte[] our_fp = Hex.decode(keyManager.getLocalFingerprint(sessionID));
String remoteFingerprint = keyManager.getRemoteFingerprint(sessionID);
if (remoteFingerprint == null)
throw new OtrException("no fingerprint for remote user");
byte[] their_fp = Hex.decode(remoteFingerprint);
byte[] sessionId;
try {
sessionId = computeSessionId(session.getS());
} catch (SMException ex) {
throw new OtrException(ex);
}
byte[] bytes = secret.getBytes(SerializationUtils.UTF8);
int combined_buf_len = 41 + sessionId.length + bytes.length;
byte[] combined_buf = new byte[combined_buf_len];
combined_buf[0] = 1;
if (initiating) {
System.arraycopy(our_fp, 0, combined_buf, 1, 20);
System.arraycopy(their_fp, 0, combined_buf, 21, 20);
} else {
System.arraycopy(their_fp, 0, combined_buf, 1, 20);
System.arraycopy(our_fp, 0, combined_buf, 21, 20);
}
System.arraycopy(sessionId, 0, combined_buf, 41, sessionId.length);
System.arraycopy(bytes, 0, combined_buf, 41 + sessionId.length, bytes.length);
MessageDigest sha256;
try {
sha256 = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException ex) {
throw new OtrException(ex);
}
byte[] combined_secret = sha256.digest(combined_buf);
byte[] smpmsg;
try {
if (initiating) {
smpmsg = SM.step1(smstate, combined_secret);
} else {
smpmsg = SM.step2b(smstate, combined_secret);
}
} catch (SMException ex) {
throw new OtrException(ex);
}
// If we've got a question, attach it to the smpmsg
if (question != null) {
bytes = question.getBytes(SerializationUtils.UTF8);
byte[] qsmpmsg = new byte[bytes.length + 1 + smpmsg.length];
System.arraycopy(bytes, 0, qsmpmsg, 0, bytes.length);
System.arraycopy(smpmsg, 0, qsmpmsg, bytes.length + 1, smpmsg.length);
smpmsg = qsmpmsg;
}
TLV sendtlv = new TLV(initiating ? (question != null ? TLV.SMP1Q : TLV.SMP1) : TLV.SMP2, smpmsg);
smstate.nextExpected = initiating ? SM.EXPECT2 : SM.EXPECT3;
return makeTlvList(sendtlv);
}
Aggregations