def gjss_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") msg = payload.get("msg") current_app.logger.info(f"[GJSS] Received payload:\n{pformat(payload)}") A = string_to_point_FQ(payload.get("A")) sigma = payload.get("sigma") s = int(sigma.get("s")) c = int(sigma.get("c")) r = int(sigma.get("r")) z = string_to_point_FQ(sigma.get("z")) h = GJSS.gen_h(msg, r) current_app.logger.info(f"[GJSS] Generated h:\n{pformat(h)}") u, v = GJSS.calc_commits(s, c, z, h, A) current_app.logger.info(f"[GJSS] Calculated u:\n{pformat(u)}\nv:\n{pformat(v)}") c_prim = GJSS.gen_challenge(h, A, z, u, v) current_app.logger.info(f"[GJSS] Generated c'\n{pformat(c)}") answer = GJSS.verify(c, c_prim) current_app.logger.info(f"[GJSS] Validation: {pformat(answer)}") return jsonify({ "valid": answer })
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
def naxos_ake(url, cipher): message = "Test" sk, pk = NAXOS.keygen() ephemeral = NAXOS.gen_ephemeral(128) if cipher == "salsa": res = requests.get(url=url + "/salsa/protocols/naxos/pkey") enc_data = res.json() data = salsa_decrypt_json(enc_data) elif cipher == "chacha": res = requests.get(url=url + "/chacha/protocols/naxos/pkey") enc_data = res.json() data = chacha_decrypt_json(enc_data) else: res = requests.get(url=url + "/protocols/naxos/pkey") data = res.json() pk_b = string_to_point_FQ(data.get("B")) X = NAXOS.calc_commit(ephemeral, sk) exchange_json = { "protocol_name": "naxos", "payload": { "X": point_to_string_FQ(X), "A": point_to_string_FQ(pk), "msg": message } } data = post_stage(url, cipher, "naxos", exchange_json, "exchange") Y = string_to_point_FQ(data.get("Y")) enc_msg = data.get("msg") K = NAXOS.calc_keyA(Y, sk, pk_b, ephemeral, pk) m = NAXOS.encode_msg(message, K) print(enc_msg == m)
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
def sigma_ake(url, cipher): message = "Test" sk, pk = SIGMA.keygen() x, X = SIGMA.gen_commit() init_json = { "protocol_name": "sigma", "payload": { "X": point_to_string_FQ(X), } } init_data = post_stage(url, cipher, "sigma", init_json, "init") token = init_data.get("session_token") payload = init_data.get("payload") pk_b = string_to_point_FQ(payload.get("B")) Y = string_to_point_FQ(payload.get("Y")) b_mac = payload.get("b_mac") sig_b = payload.get("sig") if sig_b.get("msg") is not None: sign_msg = sig_b.get("msg") else: sign_msg = point_to_string_FQ(X) + point_to_string_FQ(Y) sign_X = string_to_point_FQ(sig_b.get("X")) sign_s = int(sig_b.get("s")) assert (SIGMA.verify_signature(pk_b, sign_X, sign_s, sign_msg)) mac_key = SIGMA.gen_mac_key(Y * x) assert (SIGMA.verify_mac(mac_key, point_to_string_FQ(pk_b), b_mac)) sign_a_msg = point_to_string_FQ(Y) + point_to_string_FQ(X) signature = SIGMA.sign_message(sk, sign_a_msg) mac = SIGMA.auth_message(mac_key, point_to_string_FQ(pk)) exchange_json = { "protocol_name": "sigma", "session_token": token, "payload": { "a_mac": mac, "A": point_to_string_FQ(pk), "msg": message, "sig": { "X": point_to_string_FQ(signature[0]), "s": str(signature[1]), "msg": sign_a_msg } } } data = post_stage(url, cipher, "sigma", exchange_json, "exchange") enc_msg = data.get("msg") K = SIGMA.gen_session_key(Y * x) m = SIGMA.encode_msg(message, K) print(enc_msg == m)
def naxos_exchange(): 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"[NAXOS] Received payload:\n{pformat(payload)}") X = string_to_point_FQ(payload.get("X")) A = string_to_point_FQ(payload.get("A")) msg = payload.get("msg") ephemeral = NAXOS.gen_ephemeral(128) Y = NAXOS.calc_commit(ephemeral, naxos_sk) current_app.logger.info(f"[NAXOS] Calculated Y:\n{pformat(Y)}") K = NAXOS.calc_keyB(A, ephemeral, naxos_sk, X, naxos_pk) enc_msg = NAXOS.encode_msg(msg, K) return jsonify({"Y": point_to_string_FQ(Y), "msg": enc_msg})
def sss_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") msg = payload.get("msg") current_app.logger.info(f"[SSS] Received payload:\n{pformat(payload)}") A = string_to_point_FQ(payload.get("A")) X = string_to_point_FQ(payload.get("X")) s = int(payload.get("s")) c = SSS.gen_challenge(msg, X) current_app.logger.info(f"[SSS] Generated c:\n{pformat(c)}") answer = SSS.verify(A, X, c, s) current_app.logger.info(f"[SSS] Validation: {pformat(answer)}") return jsonify({ "valid": answer })
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 })
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)
def blsss_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") msg = payload.get("msg") current_app.logger.info( f"[BLSSS] Received payload:\n{pformat(payload)}") A = string_to_point_FQ(payload.get("A")) sigma = string_to_point_FQ2(payload.get("sigma")) h = BLSSS.gen_g2_generator(msg) current_app.logger.info(f"[BLSSS] Generated h:\n{pformat(h)}") answer = BLSSS.verify(sigma, A, h) current_app.logger.info(f"[BLSSS] Validation: {pformat(answer)}") return jsonify({"valid": answer})
def gen_h(msg, r): payload = [f'{msg}{str(r)}'] return string_to_point_FQ( call_node("./schemas/protocols/hash_map_g1.js", payload))