Search in sources :

Example 1 with SMException

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;
}
Also used : OtrOutputStream(net.java.otr4j.io.OtrOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) SMException(net.java.otr4j.crypto.SM.SMException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 2 with SMException

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);
}
Also used : OtrException(net.java.otr4j.OtrException) SMException(net.java.otr4j.crypto.SM.SMException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Aggregations

MessageDigest (java.security.MessageDigest)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 SMException (net.java.otr4j.crypto.SM.SMException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 OtrException (net.java.otr4j.OtrException)1 OtrOutputStream (net.java.otr4j.io.OtrOutputStream)1