示例#1
0
def ois_verify():
    if request.data and type(request.data) is dict:
        data = request.data
    else:
        data = request.json
    if data.get("protocol_name") == PROTOCOL:
        payload = data.get("payload")
        token = data.get("session_token")
        s_1 = int(payload.get("s1"))
        s_2 = int(payload.get("s2"))
        current_app.logger.info(
            f"[OIS] Received s_1:\n{pformat(s_1)}\ns_2:\n{pformat(s_2)}")
        session = Session.query.filter_by(session_token=token).first()
        A = string_to_point_FQ(session.payload.get("A"))
        X = string_to_point_FQ(session.payload.get("X"))
        c = session.payload.get("c")
        try:
            db.session.delete(session)
            db.session.commit()
        except:
            db.create_all()
            db.session.rollback()
            db.session.delete(session)
            db.session.commit()
        answer = OIS.verify(A, X, c, (s_1, s_2))
        current_app.logger.info(f"[OIS] Verification: {pformat(answer)}")
        if answer:
            return jsonify({"verified": answer}), 200
        else:
            return jsonify({"verified": answer}), 403
示例#2
0
def ois_init():
    if request.data and type(request.data) is dict:
        data = request.data
    else:
        data = request.json
    current_app.logger.info(f"{data.get('alt')}")
    if data.get("protocol_name") == PROTOCOL:
        payload = data.get("payload")
        current_app.logger.info(f"[OIS] Received payload:\n{pformat(payload)}")
        A = payload.get("A")
        X = payload.get("X")
        token = generate_token()
        c = OIS.gen_challenge()
        current_app.logger.info(f"[OIS] Generated c:\n{pformat(c)}")
        db_data = {"A": A, "X": X, "c": c}
        try:
            db.session.add(Session(session_token=token, payload=db_data))
            db.session.commit()
        except:
            db.create_all()
            db.session.rollback()
            db.session.add(Session(session_token=token, payload=db_data))
            db.session.commit()
        response = {"session_token": token, "payload": {"c": str(c)}}
        current_app.logger.info(f"[OIS] Sent response")
        return jsonify(response)
示例#3
0
def msis_verify():
    if request.data and type(request.data) is dict:
        data = request.data
    else:
        data = request.json
    if data.get("protocol_name") == PROTOCOL:
        payload = data.get("payload")
        token = data.get("session_token")
        S = string_to_point_FQ2(payload.get("S"))
        current_app.logger.info(f"[MSIS] Received S:\n{pformat(S)}")
        session = Session.query.filter_by(session_token=token).first()
        A = string_to_point_FQ(session.payload.get("A"))
        X = string_to_point_FQ(session.payload.get("X"))
        c = session.payload.get("c")
        g_hat = MSIS.gen_g2_generator(X, c)
        try:
            db.session.delete(session)
            db.session.commit()
        except:
            db.create_all()
            db.session.rollback()
            db.session.delete(session)
            db.session.commit()
        answer = MSIS.verify(A, X, c, g_hat, S)
        current_app.logger.info(f"[MSIS] Verification: {pformat(answer)}")
        if answer:
            return jsonify({"verified": answer}), 200
        else:
            return jsonify({"verified": answer}), 403
示例#4
0
def sigma_init():
    if request.data and type(request.data) is dict:
        data = request.data
    else:
        data = request.json
    if data.get("protocol_name") == PROTOCOL:
        payload = data.get("payload")
        current_app.logger.info(f"[SIGMA] Received payload:\n{pformat(payload)}")
        X_str = payload.get("X")
        X = string_to_point_FQ(X_str)
        y, Y = SIGMA.gen_commit()
        current_app.logger.info(f"[SIGMA] Generated Y:\n{pformat(Y)}")
        sign_msg = X_str + point_to_string_FQ(Y)
        signature = SIGMA.sign_message(sigma_sk, sign_msg)
        current_app.logger.info(f"[SIGMA] Generated Signature:\n{pformat(signature)}")
        mac_key = SIGMA.gen_mac_key(X * y)
        mac = SIGMA.auth_message(mac_key, point_to_string_FQ(sigma_pk))
        current_app.logger.info(f"[SIGMA] Generated MAC:\n{pformat(mac)}")
        token = generate_token()
        db_data = {
            "X": X_str,
            "Y": point_to_string_FQ(Y),
            "y": y
        }
        try:
            db.session.add(Session(session_token=token, payload=db_data))
            db.session.commit()
        except:
            db.create_all()
            db.session.rollback()
            db.session.add(Session(session_token=token, payload=db_data))
            db.session.commit()
        response = {
            "session_token": token,
            "payload": {
                "b_mac": mac,
                "B": point_to_string_FQ(sigma_pk),
                "Y": point_to_string_FQ(Y),
                "sig": {
                    "X": point_to_string_FQ(signature[0]),
                    "s": str(signature[1]),
                    "msg": sign_msg
                }
            }
        }
        current_app.logger.info(f"[SIGMA] Sent response")
        return jsonify(response)
示例#5
0
def sigma_exchange():
    if request.data and type(request.data) is dict:
        data = request.data
    else:
        data = request.json
    if data.get("protocol_name") == PROTOCOL:
        token = data.get("session_token")
        payload = data.get("payload")
        current_app.logger.info(f"[SIGMA] Received payload:\n{pformat(payload)}")
        session = Session.query.filter_by(session_token=token).first()
        X = string_to_point_FQ(session.payload.get("X"))
        Y = string_to_point_FQ(session.payload.get("Y"))
        y = session.payload.get("y")
        try:
            db.session.delete(session)
            db.session.commit()
        except:
            db.create_all()
            db.session.rollback()
            db.session.delete(session)
            db.session.commit()
        pk_a = string_to_point_FQ(payload.get("A"))
        a_mac = payload.get("a_mac")
        sig_a = payload.get("sig")
        if sig_a.get("msg") is not None:
            sign_msg = sig_a.get("msg")
        else:
            sign_msg = point_to_string_FQ(Y) + point_to_string_FQ(X)
        sign_X = string_to_point_FQ(sig_a.get("X"))
        sign_s = int(sig_a.get("s"))
        assert(SIGMA.verify_signature(pk_a, sign_X, sign_s, sign_msg))
        current_app.logger.info(f"[SIGMA] Verified signature")
        mac_key = SIGMA.gen_mac_key(X * y)
        assert(SIGMA.verify_mac(mac_key, point_to_string_FQ(pk_a), a_mac))
        current_app.logger.info(f"[SIGMA] Verified MAC")
        msg = payload.get("msg")
        K = SIGMA.gen_session_key(X * y)
        enc_msg = SIGMA.encode_msg(msg, K)
        return jsonify({
            "msg": enc_msg
        })