Exemple #1
0
def __menu_handler(args):

    if not Path(args.module).is_file():
        print("(-p11) path does not exist")
        exit()

    with HsmClient(slot=args.slot, pin=args.pin, pkcs11_lib=args.module) as c:

        iv = c.generate_random(size=16)
        # CKM_AES_KEY_WRAP
        mech = 0x00002109
        wrapped_key_bytes = c.wrap_key(key_handle=args.handle,
                                       wrap_key_handle=args.wrapHandle,
                                       wrap_key_iv=iv,
                                       wrap_key_mech=mech)
        print("iv: {}".format(bytes_to_hex(iv)))
        print("wrapped_key_bytes: {}".format(bytes_to_hex(wrapped_key_bytes)))
Exemple #2
0
def __menu_handler(args):

    if not Path(args.module).is_file():
        print("(-module) path does not exist")
        exit()

    print("starting test...")

    with HsmClient(slot=args.slot, pin=args.pin, pkcs11_lib=args.module) as c:

        unique_tag = bytes_to_hex(os.urandom(4))
        key_handles = c.create_rsa_key_pair(public_key_label="RSA_PUB_TEST_KEY_{}".format(unique_tag),
                                            private_key_label="RSA_PVT_TEST_KEY_{}".format(unique_tag),
                                            mechanism=HsmMech[args.genMech],
                                            key_length=args.keySize,
                                            token=False,
                                            sign_verify=True,
                                            encrypt_decrypt=False,
                                            wrap_unwrap=False,
                                            public_private=False)

        pvt_h = key_handles[1]
        data = os.urandom(args.dataSize)

        # get start time
        t0 = time()
        try:
            for i in range(1, args.ops + 1):

                c.sign(handle=pvt_h,
                       data=data,
                       mechanism=HsmMech[args.signMech],
                       pss_salt_length=args.pssSaltLength)

        except KeyboardInterrupt:
            print("interrupted")

    # get stop time
    t1 = time()

    print("end test")

    elapsed = t1 - t0
    total_ops = args.ops
    print("\n-------------------------------------")
    print("RESULTS")
    print("-------------------------------------")
    print("test: rsasign-test")
    print("key_size: {}".format(args.keySize))
    print("sign_mech: {}".format(args.signMech))
    print("gen_mech: {}".format(args.genMech))
    print("total_ops: {}".format(total_ops))
    print("elapsed_time_ms: " + str(round(elapsed * 1000, 4)))
    print("ops/sec: " + str(round(total_ops / elapsed, 2)))
    print("-------------------------------------\n")
Exemple #3
0
def __menu_handler(args):

    if not Path(args.module).is_file():
        print("(-module) path does not exist")
        exit()

    with HsmClient(slot=args.slot, pin=args.pin, pkcs11_lib=args.module) as c:
        result = c.generate_random(size=args.size)

    if args.encoding == "hex":
        print(bytes_to_hex(result))
    elif args.encoding == "base64":
        print(str(b64encode(result))[2:-1])
Exemple #4
0
def __menu_handler(args):

    if not Path(args.module).is_file():
        print("(-module) path does not exist")
        exit()

    print("starting test...")

    with HsmClient(slot=args.slot, pin=args.pin, pkcs11_lib=args.module) as c:
        # get start time
        t0 = time()
        try:
            for i in range(1, args.ops + 1):
                unique_tag = bytes_to_hex(os.urandom(4))
                c.create_ecc_key_pair(public_key_label="EC_PUB_TEST_KEY_{}".format(unique_tag),
                                      private_key_label="EC_PVT_TEST_KEY_{}".format(unique_tag),
                                      ec_params=EcCurveOids[args.curveName],
                                      token=args.persist,
                                      sign_verify=True,
                                      encrypt_decrypt=False,
                                      wrap_unwrap=False,
                                      public_private=False)

        except KeyboardInterrupt:
            print("interrupted")
        # get stop time
        t1 = time()

    print("end test")

    elapsed = t1 - t0
    total_ops = args.ops
    print("\n-------------------------------------")
    print("RESULTS")
    print("-------------------------------------")
    print("test: ecgen-test")
    print("curve: {}".format(args.curveName))
    print("total_ops: {}".format(total_ops))
    print("elapsed_time_ms: " + str(round(elapsed * 1000, 4)))
    print("ops/sec: " + str(round(total_ops / elapsed, 2)))
    print("-------------------------------------\n")
Exemple #5
0
def __menu_handler(args):

    if not Path(args.module).is_file():
        print("(-p11) path does not exist")
        exit()

    # test to see if the user provided a pss salt length for a PSS algorithm
    if "PSS" in args.mech and args.pssSaltLength is None:
        print("-pss-length must be provided when a PSS mechanism is specified")
        return
    else:
        if args.pssSaltLength is None:
            args.pssSaltLength = 0

    with HsmClient(slot=args.slot, pin=args.pin, pkcs11_lib=args.module) as c:

        sig = c.sign(handle=args.keyHandle,
                     data=hex_to_bytes(args.data),
                     mechanism=HsmMech[args.mech],
                     pss_salt_length=args.pssSaltLength)

        print(bytes_to_hex(sig))