Esempio n. 1
0
def test_ethereum():
    # 建立秘密共享
    # ecdsaKeyClient, ecdsaKeyServer = ecdsa_gen()
    # f = open("kc.dat", "wb")
    # f.write(ecdsaKeyClient.exportShare())
    # f.close()
    # f = open("ks.dat", "wb")
    # f.write(ecdsaKeyServer.exportShare())
    # f.close()
    # f = open("pubkey.dat", "wb")
    # f.write(ecdsaKeyClient.getPublic())
    # f.close()
    # ecdsa_backup(ecdsaKeyClient, ecdsaKeyServer)
    # return
    # 读取已建立的秘密共享
    f = open("kc.dat", "rb")
    ecdsaKeyClient = mpc_crypto.Ecdsa(CLIENT, f.read())
    f.close()
    f = open("ks.dat", "rb")
    ecdsaKeyServer = mpc_crypto.Ecdsa(SERVER, f.read())
    f.close()
    ecdsa_backup(ecdsaKeyClient, ecdsaKeyServer)
    f = open("tx.raw", "rb")
    ecdsa_sign_test_data(ecdsaKeyClient, ecdsaKeyServer, f.read())
    f.close()
Esempio n. 2
0
def test_ecdsa():
    # 建立秘密共享
    ecdsaKeyClient, ecdsaKeyServer = ecdsa_gen()
    oldClientShare = ecdsaKeyClient.exportShare()
    oldServerShare = ecdsaKeyServer.exportShare()
    ecdsa_backup(ecdsaKeyClient, ecdsaKeyServer)
    # 刷新秘密
    print("before refresh", getMd5FromHex(ecdsaKeyClient.exportShare()),
          getMd5FromHex(ecdsaKeyServer.exportShare()))
    refresh_shares(ecdsaKeyClient, ecdsaKeyServer)
    print("after refresh", getMd5FromHex(ecdsaKeyClient.exportShare()),
          getMd5FromHex(ecdsaKeyServer.exportShare()))
    ecdsa_backup(ecdsaKeyClient, ecdsaKeyServer)
    return
    # 重建两方
    testClient = mpc_crypto.Ecdsa(CLIENT)
    testClient.setShare(ecdsaKeyClient.share)
    testServer = mpc_crypto.Ecdsa(SERVER)
    testServer.setShare(ecdsaKeyServer.share)
    # 签名
    ecdsa_sign(testClient, testServer)
    # 旧分享密钥签名测试
    oldClient = mpc_crypto.Ecdsa(CLIENT)
    oldClient.importShare(oldClientShare)
    oldServer = mpc_crypto.Ecdsa(SERVER)
    oldServer.importShare(oldServerShare)
    print('oldShare', getMd5FromHex(oldClientShare),
          getMd5FromHex(oldServerShare))
    # 签名
    ecdsa_sign2(oldClient, oldServer, testClient)
    ecdsa_sign2(testClient, testServer, oldClient)
    # ecdsa_sign2(testClient, oldServer, oldClient)
    # 备份
    ecdsa_backup(testClient, testServer)
Esempio n. 3
0
def ecdsa_gen():
    print("test_ecdsa_gen...")
    clientObj = mpc_crypto.Ecdsa(CLIENT)
    serverObj = mpc_crypto.Ecdsa(SERVER)
    clientObj.initGenerate()
    serverObj.initGenerate()
    exec_client_server(clientObj, serverObj)
    print(" ok")
    return clientObj, serverObj
Esempio n. 4
0
def run_getpubkey(inShare, cryptoType):
    print("Getting public key...")

    if cryptoType == 'ECDSA':
        obj = mpc_crypto.Ecdsa(peer, inShare)
    elif cryptoType == 'EDDSA':
        obj = mpc_crypto.Eddsa(peer, inShare)
    else:
        sys.exit("getpubkey not supported for " + cryptoType)

    with obj:
        pk = obj.getPublic()
    print("ok")
    return pk
Esempio n. 5
0
def run_generate():
    print("Generating key...")
    if args.type == 'EDDSA':
        obj = mpc_crypto.Eddsa(peer)
        obj.initGenerate()
    elif args.type == 'ECDSA':
        obj = mpc_crypto.Ecdsa(peer)
        obj.initGenerate()
    elif args.type == 'generic':
        obj = mpc_crypto.GenericSecret(peer)
        obj.initGenerate(args.size)
    else:
        sys.exit("Generate not supported for " + args.type)
    with obj:
        exec_mpc_exchange(obj)
        print(" ok")
        return obj.exportShare()
Esempio n. 6
0
def run_sign(inShare):
    print(args.type + " signing...")
    if args.type == 'ECDSA':
        obj = mpc_crypto.Ecdsa(peer, inShare)
    elif args.type == 'EDDSA':
        obj = mpc_crypto.Eddsa(peer, inShare)
    else:
        sys.exit("Sign not supported for " + args.type)

    if not args.data_file:
        sys.exit("Input data missing")
    with open(args.data_file, "rb") as f:
        inData = f.read()
    with obj:
        obj.initSign(inData, True)
        exec_mpc_exchange(obj)
        sig = obj.getSignResult()
    print("ok")
    return sig
Esempio n. 7
0
def run_get_public(inShare, cryptoType):
    print(cryptoType + " get_public...")
    if not args.data_file:
        sys.exit("Input data missing")
    with open(args.data_file, "rb") as f:
        inData = f.read()

    if cryptoType == 'ECDSA':
        if len(inData) > 32:
            sys.exit(
                "Input too long. Data should be hashed before ECDSA signing.")
        obj = mpc_crypto.Ecdsa(peer, inShare)
    elif cryptoType == 'EDDSA':
        obj = mpc_crypto.Eddsa(peer, inShare)
    else:
        sys.exit("Sign not supported for " + cryptoType)

    with obj:
        # sig = bytes(mpc_crypto.serializePubBIP32(obj.share), 'utf-8')
        sig = bytes(obj.get_public().hex(), 'utf-8')
    print("ok", sig)
    return sig
Esempio n. 8
0
def run_sign(inShare, cryptoType):
    print(cryptoType + " signing...")
    if not args.data_file:
        sys.exit("Input data missing")
    with open(args.data_file, "rb") as f:
        inData = f.read()

    if cryptoType == 'ECDSA':
        if len(inData) > 32:
            sys.exit(
                "Input too long. Data should be hashed before ECDSA signing.")
        obj = mpc_crypto.Ecdsa(peer, inShare)
    elif cryptoType == 'EDDSA':
        obj = mpc_crypto.Eddsa(peer, inShare)
    else:
        sys.exit("Sign not supported for " + cryptoType)

    with obj:
        obj.initSign(inData)
        exec_mpc_exchange(obj)
        sig = bytes(obj.getSignResult().hex(), 'utf-8')
    print("ok", sig)
    return sig
Esempio n. 9
0
def run_verify(inShare, cryptoType):
    print("Verifying...")
    if not args.data_file:
        sys.exit("Input data missing")
    with open(args.data_file, "r") as f:
        filecontent = f.read()
        inData = bytes.fromhex(filecontent)

    if not args.sig_file:
        sys.exit("Signature file missing")
    with open(args.sig_file, "rb") as f:
        sigData = f.read()

    if cryptoType == 'ECDSA':
        obj = mpc_crypto.Ecdsa(peer, inShare)
    elif cryptoType == 'EDDSA':
        obj = mpc_crypto.Eddsa(peer, inShare)
    else:
        sys.exit("verify not supported for " + cryptoType)

    with obj:
        obj.verify(inData, sigData)
    print("ok")
    return 1