From 7f24b7229ef694833ef20639fc0b33f53fdef707 Mon Sep 17 00:00:00 2001 From: Zhi Guan Date: Thu, 16 Mar 2017 23:20:13 +0800 Subject: [PATCH] add zeromem engine anti memory leakage attack --- engines/zeromem/cba_ecdh_engine.c | 266 +++++ engines/zeromem/ec.h | 49 + engines/zeromem/ec2m_kern.c | 174 +++ engines/zeromem/ec2m_kern.h | 36 + engines/zeromem/ec_inv.c | 104 ++ engines/zeromem/ec_main.c | 37 + engines/zeromem/engine/myengine.c | 260 +++++ engines/zeromem/engine/myengine.h | 175 +++ engines/zeromem/engine/mytest.c | 106 ++ engines/zeromem/kernel/cba-ecc.c | 208 ++++ engines/zeromem/kernel/install.sh | 5 + engines/zeromem/kernel_test.c | 217 ++++ engines/zeromem/kernel_test.h | 6 + engines/zeromem/sys_ec2m.c | 22 + engines/zeromem/sys_ec2m.h | 16 + engines/zeromem/test.c | 1653 +++++++++++++++++++++++++++++ engines/zeromem/test.h | 17 + engines/zeromem/util.c | 215 ++++ engines/zeromem/util.h | 82 ++ 19 files changed, 3648 insertions(+) create mode 100755 engines/zeromem/cba_ecdh_engine.c create mode 100755 engines/zeromem/ec.h create mode 100755 engines/zeromem/ec2m_kern.c create mode 100755 engines/zeromem/ec2m_kern.h create mode 100755 engines/zeromem/ec_inv.c create mode 100755 engines/zeromem/ec_main.c create mode 100755 engines/zeromem/engine/myengine.c create mode 100755 engines/zeromem/engine/myengine.h create mode 100755 engines/zeromem/engine/mytest.c create mode 100755 engines/zeromem/kernel/cba-ecc.c create mode 100755 engines/zeromem/kernel/install.sh create mode 100755 engines/zeromem/kernel_test.c create mode 100755 engines/zeromem/kernel_test.h create mode 100755 engines/zeromem/sys_ec2m.c create mode 100755 engines/zeromem/sys_ec2m.h create mode 100755 engines/zeromem/test.c create mode 100755 engines/zeromem/test.h create mode 100755 engines/zeromem/util.c create mode 100755 engines/zeromem/util.h diff --git a/engines/zeromem/cba_ecdh_engine.c b/engines/zeromem/cba_ecdh_engine.c new file mode 100755 index 00000000..104a31f0 --- /dev/null +++ b/engines/zeromem/cba_ecdh_engine.c @@ -0,0 +1,266 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#ifndef OPENSSL_NO_RSA +#include +#endif +#include +#include +#include "ec2m_kern.h" + +struct ecdh_method +{ + const char *name; + int (*compute_key)(void *key, size_t outlen, const EC_POINT *pub_key, EC_KEY *ecdh, + void *(*KDF)(const void *in, size_t inlen, void *out, size_t *outlen)); + int flags; + char *app_data; +}; + + +#ifndef OPENSSL_NO_HW + +/* the header file of vender */ +//#include "hwdevice.h" + +/* Constants used when creating the ENGINE */ +static const char *engine_hwdev_id = "cba_ecdh"; +static const char *engine_hwdev_name = "cold boot resistant ECDH"; +#ifndef OPENSSL_NO_DYNAMIC_ENGINE +/* Compatibility hack, the dynamic library uses this form in the path */ +static const char *engine_hwdev_id_alt = "cba_ecdh"; +#endif + +static int compute_key(void *out, size_t outlen, + const EC_POINT *pub_key, EC_KEY *ecdh, + void *(*KDF)(const void *in, size_t inlen, void *out, size_t *outlen)) +{ + const EC_GROUP* group; + int ret; + + group = EC_KEY_get0_group(ecdh); + + // only use our solution if the curve name is SECT163K1 + if (EC_GROUP_get_curve_name(group) == NID_sect163k1) { + const BIGNUM* rkey; + BN_CTX *ctx; + BIGNUM* x, *y; + mm256_point_t p, q; + mm_256 mkey; + int r; + + ctx = BN_CTX_new(); + BN_CTX_start(ctx); + + x = BN_CTX_get(ctx); + y = BN_CTX_get(ctx); + + rkey = EC_KEY_get0_private_key(ecdh); + memset(&mkey, 0, sizeof(mkey)); + memcpy(&mkey, rkey->d, sizeof(rkey->d[0]) * rkey->top); + ec2m_import_key(&mkey); + + r = EC_POINT_get_affine_coordinates_GF2m(group, pub_key, x, y, ctx); + memset(&p, 0, sizeof(p)); + memcpy(&p.x, x->d, sizeof(x->d[0]) * x->top); + memcpy(&p.y, y->d, sizeof(y->d[0]) * y->top); + p.z.iv[0] = 1; + + r = ec2m_private_operation(&p, &q); + if (r < 0) { + fprintf(stderr, "invalid result: %d\n", r); + } + + int xlen = (163 + 7) / 8; + if (KDF != 0) + { + if (KDF(&q.x, xlen, out, &outlen) == NULL) + { + ECDHerr(ECDH_F_ECDH_COMPUTE_KEY,ECDH_R_KDF_FAILED); + return -1; + } + ret = outlen; + } + else + { + /* no KDF, just copy as much as we can */ + if (outlen > xlen) + outlen = xlen; + memcpy(out, &q.x, outlen); + ret = outlen; + } + + BN_CTX_end(ctx); + BN_CTX_free(ctx); + + } else { + // use the default method + const ECDH_METHOD* meth = ECDH_OpenSSL(); + return meth->compute_key(out, outlen, pub_key, ecdh, KDF); + } + + return ret; +} + +static ECDH_METHOD ecdh_meth = { + "CBA resistant ECDH method", + compute_key, + 0, + NULL +}; + +static int hwdev_destroy(ENGINE *e) +{ + fprintf(stderr, "arrive at hwdev_destroy\n"); + return 1; +} + +static int hwdev_init(ENGINE *e) +{ + fprintf(stderr, "arrive at hwdev_init\n"); + ec2m_kern_init(); + return 1; +} + +static int hwdev_finish(ENGINE *e) +{ + fprintf(stderr, "arrive at hwdev_finish\n"); + ec2m_kern_clean(); + return 1; +} + +/* The definitions for control commands specific to this engine */ +#define HWDEV_CMD_INIT (ENGINE_CMD_BASE) +#define HWDEV_CMD_EXIT (ENGINE_CMD_BASE + 1) +#define HWDEV_CMD_TEST (ENGINE_CMD_BASE + 2) +static const ENGINE_CMD_DEFN hwdev_cmd_defns[] = { + {HWDEV_CMD_INIT, + "INIT", + "init the hardware device before using", + ENGINE_CMD_FLAG_STRING}, /* may be the password */ + {HWDEV_CMD_EXIT, + "EXIT", + "exit the hardware device after using", + ENGINE_CMD_FLAG_NO_INPUT}, + {HWDEV_CMD_TEST, + "TEST", + "run the test case of the hardware device", + ENGINE_CMD_FLAG_NUMERIC}, /* may be the number of test case */ + {0, NULL, NULL, 0} + }; + +/* This internal function is used by ENGINE_chil() and possibly by the + * "dynamic" ENGINE support too */ +static int hwdev_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void)) +{ + int to_return = 1; + + switch(cmd) { + case HWDEV_CMD_INIT: + fprintf(stderr, "arrive at HWDEV_CMD_INIT, password: %s\n", + (const char *)p); + break; + case HWDEV_CMD_EXIT: + fprintf(stderr, "arrive at HWDEV_CMD_EXIT, no parameters\n"); + break; + case HWDEV_CMD_TEST: + fprintf(stderr, "arrive at HWDEV_CMD_TEST, case id: %ld\n", + i); + break; + /* The command isn't understood by this engine */ + default: + to_return = 0; + break; + } + + return to_return; +} + +static EVP_PKEY *hwdev_load_privkey(ENGINE *eng, const char *key_id, + UI_METHOD *ui_method, void *callback_data) +{ + fprintf(stderr, "arrive at hwdev_load_privkey\n"); + EVP_PKEY *res = NULL; + + return res; +} + +static EVP_PKEY *hwdev_load_pubkey(ENGINE *eng, const char *key_id, + UI_METHOD *ui_method, void *callback_data) +{ + fprintf(stderr, "arrive at hwdev_load_pubkey\n"); + EVP_PKEY *res = NULL; + + return res; +} + +static int bind_helper(ENGINE *e) +{ + fprintf(stderr, "arrive at bind_helper\n"); + if(!ENGINE_set_id(e, engine_hwdev_id) || + !ENGINE_set_name(e, engine_hwdev_name) || + !ENGINE_set_ECDH(e, &ecdh_meth) || + !ENGINE_set_destroy_function(e, hwdev_destroy) || + !ENGINE_set_init_function(e, hwdev_init) || + !ENGINE_set_finish_function(e, hwdev_finish) || + !ENGINE_set_ctrl_function(e, hwdev_ctrl) || + !ENGINE_set_load_privkey_function(e, hwdev_load_privkey) || + !ENGINE_set_load_pubkey_function(e, hwdev_load_pubkey) || + !ENGINE_set_cmd_defns(e, hwdev_cmd_defns)) + return 0; + + return 1; +} + +static ENGINE *engine_hwdev(void) +{ + fprintf(stderr, "arrive at engine_test\n"); + ENGINE *ret = ENGINE_new(); + if(!ret) { + return NULL; + } + + if(!bind_helper(ret)) { + ENGINE_free(ret); + return NULL; + } + + return ret; +} + +void ENGINE_load_test(void) +{ + fprintf(stderr, "arrive at ENGINE_load_test\n"); + /* Copied from eng_[openssl|dyn].c */ + ENGINE *toadd = engine_hwdev(); + if(!toadd) return; + ENGINE_add(toadd); + ENGINE_free(toadd); + ERR_clear_error(); +} +//#endif + + +/* This stuff is needed if this ENGINE is being compiled into a self-contained + * shared-library. */ +#ifndef OPENSSL_NO_DYNAMIC_ENGINE +static int bind_fn(ENGINE *e, const char *id) +{ + fprintf(stderr, "arrive at bind_fn\n"); + if(id && (strcmp(id, engine_hwdev_id) != 0) && + (strcmp(id, engine_hwdev_id_alt) != 0)) + return 0; + if(!bind_helper(e)) + return 0; + return 1; +} +IMPLEMENT_DYNAMIC_CHECK_FN() +IMPLEMENT_DYNAMIC_BIND_FN(bind_fn) +#endif /* OPENSSL_NO_DYNAMIC_ENGINE */ + +#endif /* !OPENSSL_NO_HW */ diff --git a/engines/zeromem/ec.h b/engines/zeromem/ec.h new file mode 100755 index 00000000..10afc4b3 --- /dev/null +++ b/engines/zeromem/ec.h @@ -0,0 +1,49 @@ +#ifndef _EC_H_ +#define _EC_H_ + +#ifdef EC_DEV +#include +#else +#include +#endif +//extern unsigned int sqr_table[1 << 16]; + +typedef struct _struct_mm_128{ + union{ + float fv[4]; + double dv[2]; + uint64_t iv[2]; + uint8_t bv[16]; + }; +} mm_128; + +typedef struct _struct_mm_256{ + union{ + float fv[8]; + double dv[4]; + uint64_t iv[4]; + uint8_t bv[32]; + }; +} mm_256; + +typedef struct { + mm_256 x; + mm_256 y; + mm_256 z; +} mm256_point_t; + +extern void gf2_add(mm_256* a, mm_256* b, mm_256* r); +extern void gf2_mul(mm_256* a, mm_256* b, mm_256* r1, mm_256* r2); +extern void gf2_mod(mm_256* a1, mm_256* a2, mm_256* r); +extern void gf2_sqr(mm_256* a, mm_256* r1, mm_256* r2); +extern void gf2_mod_mul(mm_256* a, mm_256* b, mm_256* r); +extern void gf2_mod_sqr(mm_256* a, mm_256* r); +extern void gf2m_inv(mm_256* a, mm_256 *r); +extern void gf2m_inv_asm(mm_256* a, mm_256 *r); + +extern void gf2_point_dbl(mm256_point_t* pa, mm256_point_t* pr, int a, int b); +extern void gf2_point_add(mm256_point_t* pa, mm256_point_t* pb, mm256_point_t* pr, int a, int b); +extern void gf2_point_mul(mm256_point_t* p, mm_256* k, mm256_point_t* q, int a, int b); +extern void gf2_point_mul_with_preset_key(mm256_point_t* p, mm256_point_t* q, int a, int b); + +#endif diff --git a/engines/zeromem/ec2m_kern.c b/engines/zeromem/ec2m_kern.c new file mode 100755 index 00000000..5e439e2d --- /dev/null +++ b/engines/zeromem/ec2m_kern.c @@ -0,0 +1,174 @@ +#include "ec2m_kern.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include "util.h" + +int sock_fd; + +int init_netlink(int unit, int portId){ + struct sockaddr_nl src_addr; + sock_fd=socket(PF_NETLINK, SOCK_RAW, unit); + if(sock_fd<0) + return -1; + + memset(&src_addr, 0, sizeof(src_addr)); + src_addr.nl_family = AF_NETLINK; + src_addr.nl_pid = portId; /* self pid */ + /* interested in group 1<<0 */ + bind(sock_fd, (struct sockaddr*)&src_addr, + sizeof(src_addr)); //绑定netlink + + if(sock_fd < 0) + return -1; + + return 0; +} + +int send_request(const int func, const void* msg, int mlen) +{ + struct nlmsghdr *nlh = NULL; + struct iovec iov; + struct msghdr mhdr; + struct sockaddr_nl dest_addr; + struct ec2m_request_st req; + const int len = mlen + sizeof(struct ec2m_request_st); + + req.func = func; + req.len = len; + + memset(&dest_addr, 0, sizeof(dest_addr)); + dest_addr.nl_family = AF_NETLINK; + dest_addr.nl_pid = 0; /* For Linux Kernel */ + dest_addr.nl_groups = 0; /* unicast */ + + memset(&mhdr, 0, sizeof(mhdr)); + nlh = (struct nlmsghdr *)malloc(NLMSG_SPACE(len)); + memset(nlh, 0, NLMSG_SPACE(len)); + nlh->nlmsg_len = NLMSG_SPACE(len); + nlh->nlmsg_pid = getpid(); + + nlh->nlmsg_flags = 0; + + memcpy((void*)NLMSG_DATA(nlh), &req, sizeof(req)); + memcpy((void*)NLMSG_DATA(nlh) + sizeof(req), msg, mlen); + + iov.iov_base = (void *)nlh; + iov.iov_len = nlh->nlmsg_len; + + mhdr.msg_name = (void *)&dest_addr; + mhdr.msg_namelen = sizeof(dest_addr); + mhdr.msg_iov = &iov; + mhdr.msg_iovlen = 1; + + sendmsg(sock_fd,&mhdr,0); //通过netlink发送消息 + + + return OK; +} + +int recv_response(void* buf, int len) +{ + struct ec2m_response_st resp; + struct nlmsghdr *nlh = NULL; + struct iovec iov; + struct msghdr mhdr; + struct sockaddr_nl dest_addr; + int buflen; + + memset(&dest_addr, 0, sizeof(dest_addr)); + dest_addr.nl_family = AF_NETLINK; + dest_addr.nl_pid = 0; /* For Linux Kernel */ + dest_addr.nl_groups = 0; /* unicast */ + + memset(&mhdr, 0, sizeof(mhdr)); + nlh = (struct nlmsghdr *)malloc(NLMSG_SPACE(MAX_PAYLOAD)); + memset(nlh, 0, NLMSG_SPACE(MAX_PAYLOAD)); + nlh->nlmsg_len = NLMSG_SPACE(MAX_PAYLOAD); + nlh->nlmsg_pid = getpid(); + nlh->nlmsg_flags = 0; + + iov.iov_base = (void *)nlh; + iov.iov_len = nlh->nlmsg_len; + + mhdr.msg_name = (void *)&dest_addr; + mhdr.msg_namelen = sizeof(dest_addr); + mhdr.msg_iov = &iov; + mhdr.msg_iovlen = 1; + + buflen = recvmsg(sock_fd, &mhdr, 0); + if(buflen < 0){ + fprintf(stderr, "invalid retval of recvmsg %d\n", buflen); + + return buflen; + } + + buflen -= NLMSG_HDRLEN; + assert(buflen >= sizeof(resp)); + memcpy(&resp, NLMSG_DATA(nlh), sizeof(resp)); + buflen -= sizeof(resp); + assert(buflen == len); + + if (buflen > 0 && buf != NULL) { + memcpy(buf, NLMSG_DATA(nlh) + sizeof(resp), buflen); + } + /* printf("resp: %d, len: %d\n", resp.result, buflen); */ + + return resp.result; +} + +int ec2m_kern_init() +{ + int r; + + r = init_netlink(NETLINK_ECC, getpid()); + + if (r < 0) + return r; + + return 0; +} + +void ec2m_kern_clean() +{ + close(sock_fd); +} + + +int ec2m_import_key(mm_256* key) +{ + int r; + + /* printf("key: %016lx%016lx%016lx\n", key->iv[2], key->iv[1], key->iv[0]); */ + r = send_request(REQ_IMPORT_KEY, key, sizeof(mm_256)); + if (r < 0) + return r; + + r = recv_response(NULL, 0); + if (r < 0) + return r; + + + return 0; + +} + +int ec2m_private_operation(mm256_point_t*p, mm256_point_t*q) +{ + int r; + + r = send_request(REQ_PRIVATE_OP, p, sizeof(mm256_point_t)); + if (r < 0) + return r; + + r = recv_response(q, sizeof(mm256_point_t)); + if (r < 0) + return r; + + return 0; +} diff --git a/engines/zeromem/ec2m_kern.h b/engines/zeromem/ec2m_kern.h new file mode 100755 index 00000000..6b1b6be0 --- /dev/null +++ b/engines/zeromem/ec2m_kern.h @@ -0,0 +1,36 @@ +#ifndef _EC2M_KERN_H_ +#define _EC2M_KERN_H_ + +#define NETLINK_ECC 31 +#define MAX_PAYLOAD 1024 + +#define REQ_IMPORT_KEY 1 +#define REQ_PRIVATE_OP 2 + +#define OK 0 +#define FAIL -1 + +#ifdef __KERNEL__ +#include +#else +#include +#endif +#include "ec.h" + +struct ec2m_request_st { + int func; + int len; +}; + +struct ec2m_response_st +{ + int result; +}; + +extern int ec2m_kern_init(void); +extern void ec2m_kern_clean(void); + +extern int ec2m_import_key(mm_256 *key); +extern int ec2m_private_operation(mm256_point_t*p, mm256_point_t*q); + +#endif diff --git a/engines/zeromem/ec_inv.c b/engines/zeromem/ec_inv.c new file mode 100755 index 00000000..7b18c3ab --- /dev/null +++ b/engines/zeromem/ec_inv.c @@ -0,0 +1,104 @@ +#include "ec.h" +#include "string.h" +#include "stdio.h" + +int is_one(mm_256* a) +{ + int i; + if (a->iv[0] != 1) + return 0; + + for (i = 1; i < 4; i++) { + if (a->iv[i] != 0) + return 0; + } + + return 1; + +} + +void shift_right(mm_256* a) +{ + int i; + + for (i = 0; i < 3; i++) { + a->iv[i] = (a->iv[i] >> 1) | (a->iv[i + 1] << 63); + } + a->iv[3] >>= 1; +} + +void add(mm_256* a, mm_256*b) +{ + int i; + + for (i = 0; i < 4; i++) { + b->iv[i] = b->iv[i] ^ a->iv[i]; + } +} + +int deg(mm_256* a) +{ + int cnt = 0; + int i; + uint64_t c; + + for (i = 3; i >= 0; i--) { + if (a->iv[i] != 0) { + break; + } + } + cnt = i * 64; + c = a->iv[i]; + while (c != 0) { + cnt ++; + c >>= 1; + } + return cnt; +} + +void gf2m_inv(mm_256* a, mm_256 *r) +{ + mm_256 b, c, u, v, f, t; + + // b = 1 + memset(&b, 0, sizeof(b)); + b.iv[0] = 1; + // c = 0 + memset(&c, 0, sizeof(c)); + // u = a + u = *a; + // v = f + memset(&v, 0, sizeof(v)); + memset(&f, 0, sizeof(f)); + f.bv[0] = 0xc9; + f.bv[20] = 0x8; + v = f; + + while (1) { + while ((u.bv[0] & 0x1) == 0) { + shift_right(&u); + + if ((b.iv[0] & 0x1) != 0) { + add(&f, &b); + } + shift_right(&b); + } + if (is_one(&u)) + break; + + if (deg(&u) < deg(&v)) { + t = u; + u = v; + v = t; + + t = b; + b = c; + c = t; + } + add(&v, &u); + add(&c, &b); + /* break; */ + } + + *r = b; +} diff --git a/engines/zeromem/ec_main.c b/engines/zeromem/ec_main.c new file mode 100755 index 00000000..7b8cf0fe --- /dev/null +++ b/engines/zeromem/ec_main.c @@ -0,0 +1,37 @@ +#include +#include +#include +#include "ec.h" +#include "test.h" + +int main(int argc, char** argv){ + /* + int i; + + for(i = 0; i < argc; i++){ + printf("arg %d: %s\n", i, argv[i]); + } + */ + + char* cmd = argv[1]; + if(!initDomainParameters(argc, argv)){ + return 1; + } + if(strcmp(cmd, "testFieldArithmetic") == 0){ + return testFieldArithmetic(); + } else if(strcmp(cmd, "testPointArithmetic") == 0){ + return testPointArithmetic(); + } else if(strcmp(cmd, "testAES") == 0){ + return testAES(); + } else if(strcmp(cmd, "benchmark_ec2") == 0){ + return benchmark_EC2(); + } else if(strcmp(cmd, "testKernelEc2m") == 0){ + return testKernelEc2m(); + } else if(strcmp(cmd, "testMisc") == 0){ + return testMisc(); + } else if(strcmp(cmd, "testCycles") == 0){ + return benchmark_cycles(); + } + + return 1; +} diff --git a/engines/zeromem/engine/myengine.c b/engines/zeromem/engine/myengine.c new file mode 100755 index 00000000..3879b4d0 --- /dev/null +++ b/engines/zeromem/engine/myengine.c @@ -0,0 +1,260 @@ +#include +#include +#include +#include +#include +#include +#include +#include "myengine.h" +#include "../ec.h" +#include "../util.h" + +int get_affine(const EC_GROUP* group, const EC_POINT* point, BIGNUM* x, BIGNUM* y, BN_CTX *ctx){ + int ret = 0; + if(EC_POINT_is_at_infinity(group, point)){ + return 0; + } + if(x == NULL || y == NULL) + return 0; + if(BN_cmp(&point->Z, BN_value_one()) == 0){ + if(!BN_copy(x, &point->X) || !BN_copy(y, &point->Y)) + return 0; + BN_set_negative(x, 0); + BN_set_negative(y, 0); + } else { + BIGNUM* z = BN_new(); + if(!BN_GF2m_mod_inv(z, &point->Z, &group->field, ctx)){ + printf("could not get the inv\n"); + return 0; + } + if(!BN_GF2m_mod_mul(x, &point->X, z, &group->field, ctx)){ + return 0; + } + if(!BN_GF2m_mod_sqr(z, z, &group->field, ctx)){ + return 0; + } + if(!BN_GF2m_mod_mul(y, &point->Y, z, &group->field, ctx)){ + return 0; + } + } + return 1; +} + +static int my_ecdh_compute_key(void *out, size_t outlen, const EC_POINT *pub_key, + EC_KEY *ecdh, void *(*KDF)(const void *in, size_t inlen, void *out, size_t *outlen)) +{ + BN_CTX *ctx; + EC_POINT *tmp=NULL; + BIGNUM *x=NULL, *y=NULL; + const BIGNUM *priv_key; + const EC_GROUP* group; + int ret= -1; + size_t buflen, len; + unsigned char *buf=NULL; + mm256_point_t mPK; + mm_256 mUK; + mm256_point_t mR; + + group = EC_KEY_get0_group(ecdh); + printf("curve_name: %d, field type: %d, degree: %d, a: %x, b: %x\n", EC_GROUP_get_curve_name(group), EC_METHOD_get_field_type(EC_GROUP_method_of(group)), EC_GROUP_get_degree(group), BN_get_word(&group->a), BN_get_word(&group->b)); + + // compute with the syscall only when the filetype is NID_X9_62_characteristic_two_field and the degree is 163 + if (!( + EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_characteristic_two_field + && EC_GROUP_get_degree(group) == 163 + )) + { + ECDH_METHOD* temp = ECDH_get_default_method(); + return temp->compute_key(out, len, pub_key, ecdh, KDF); + } + + if (outlen > INT_MAX) + { + ECDHerr(ECDH_F_ECDH_COMPUTE_KEY,ERR_R_MALLOC_FAILURE); /* sort of, anyway */ + return -1; + } + + if ((ctx = BN_CTX_new()) == NULL) goto err; + BN_CTX_start(ctx); + x = BN_CTX_get(ctx); + y = BN_CTX_get(ctx); + + priv_key = EC_KEY_get0_private_key(ecdh); + if (priv_key == NULL) + { + ECDHerr(ECDH_F_ECDH_COMPUTE_KEY,ECDH_R_NO_PRIVATE_VALUE); + goto err; + } + + if ((tmp=EC_POINT_new(group)) == NULL) + { + ECDHerr(ECDH_F_ECDH_COMPUTE_KEY,ERR_R_MALLOC_FAILURE); + goto err; + } + + bn_to_mm256(priv_key, &mUK); + + printf("%s\n", BN_bn2hex(priv_key)); + + print_mm_256(&mUK); + printf("\n"); + + EC_POINT_to_mm_point(pub_key, &mPK); + + print_EC_POINT(pub_key); + printf("\n"); + print_mm_point(&mPK); + printf("\n"); + + init_sqr_table(); + + gf2_point_mul(&mPK, &mUK, &mR, BN_get_word(&group->a), BN_get_word(&group->b)); + print_mm_point(&mR); + printf("\n"); + + if (!EC_POINT_mul(group, tmp, NULL, pub_key, priv_key, ctx)) + { + ECDHerr(ECDH_F_ECDH_COMPUTE_KEY,ECDH_R_POINT_ARITHMETIC_FAILURE); + goto err; + } + print_EC_POINT(tmp); + printf("\n"); + + + if (!get_affine(group, tmp, x, y, ctx)) + { + ECDHerr(ECDH_F_ECDH_COMPUTE_KEY,ECDH_R_POINT_ARITHMETIC_FAILURE); + goto err; + } + + buflen = (EC_GROUP_get_degree(group) + 7)/8; + len = BN_num_bytes(x); + if (len > buflen) + { + ECDHerr(ECDH_F_ECDH_COMPUTE_KEY,ERR_R_INTERNAL_ERROR); + goto err; + } + if ((buf = OPENSSL_malloc(buflen)) == NULL) + { + ECDHerr(ECDH_F_ECDH_COMPUTE_KEY,ERR_R_MALLOC_FAILURE); + goto err; + } + + memset(buf, 0, buflen - len); + if (len != (size_t)BN_bn2bin(x, buf + buflen - len)) + { + ECDHerr(ECDH_F_ECDH_COMPUTE_KEY,ERR_R_BN_LIB); + goto err; + } + + if (KDF != 0) + { + if (KDF(buf, buflen, out, &outlen) == NULL) + { + ECDHerr(ECDH_F_ECDH_COMPUTE_KEY,ECDH_R_KDF_FAILED); + goto err; + } + ret = outlen; + } + else + { + /* no KDF, just copy as much as we can */ + if (outlen > buflen) + outlen = buflen; + memcpy(out, buf, outlen); + ret = outlen; + } + printf("ECC compute key done!\n"); + err: + if (tmp) EC_POINT_free(tmp); + if (ctx) BN_CTX_end(ctx); + if (ctx) BN_CTX_free(ctx); + if (buf) OPENSSL_free(buf); + return(ret); + + + /* if(1){ */ + /* ECDH_METHOD* temp = ECDH_get_default_method(); */ + /* return temp->compute_key(out, len, pub_key, ecdh, KDF); */ + /* } */ + /* return 1; */ +} + +/**************************************************************************** + * Functions to handle the engine * +*****************************************************************************/ + +static int bind_my(ENGINE *e) +{ + //const RSA_METHOD *meth1; + if(!ENGINE_set_id(e, engine_my_id) + || !ENGINE_set_name(e, engine_my_name) + || !ENGINE_set_ECDH(e, &my_ecdh) + //|| !ENGINE_set_ciphers(e, my_ciphers) + //|| !ENGINE_set_digests(e, my_digests) + || !ENGINE_set_destroy_function(e, my_destroy) + || !ENGINE_set_init_function(e, my_init) + || !ENGINE_set_finish_function(e, my_finish) + /* || !ENGINE_set_ctrl_function(e, my_ctrl) */ + /* || !ENGINE_set_cmd_defns(e, my_cmd_defns) */) + return 0; + return 1; + } + + +#ifdef ENGINE_DYNAMIC_SUPPORT +static int bind_helper(ENGINE *e, const char *id) +{ + if(id && (strcmp(id, engine_my_id) != 0)) + return 0; + if(!bind_my(e)) + return 0; + return 1; +} +IMPLEMENT_DYNAMIC_CHECK_FN() +IMPLEMENT_DYNAMIC_BIND_FN(bind_helper) +#else +static ENGINE *engine_my(void) +{ + ENGINE *ret = ENGINE_new(); + if(!ret) + return NULL; + if(!bind_my(ret)) + { + ENGINE_free(ret); + return NULL; + } + return ret; +} + +void ENGINE_load_myengine(void) +{ + /* Copied from eng_[openssl|dyn].c */ + ENGINE *toadd = engine_my(); + if(!toadd) return; + ENGINE_add(toadd); + ENGINE_free(toadd); + ERR_clear_error(); +} +#endif + + +static int my_init(ENGINE *e) +{ + printf("my_init\n"); + return 1; +} + + +static int my_finish(ENGINE *e) +{ + printf("my_finih\n"); + return 1; +} + + +static int my_destroy(ENGINE *e) +{ + printf("my_destroy\n"); + return 1; +} diff --git a/engines/zeromem/engine/myengine.h b/engines/zeromem/engine/myengine.h new file mode 100755 index 00000000..5009125d --- /dev/null +++ b/engines/zeromem/engine/myengine.h @@ -0,0 +1,175 @@ +#define INT_MAX 32767 + +#include +typedef struct ec_extra_data_st { + struct ec_extra_data_st *next; + void *data; + void *(*dup_func)(void *); + void (*free_func)(void *); + void (*clear_free_func)(void *); +} EC_EXTRA_DATA; + +typedef struct ec_key_st { + int version; + + EC_GROUP *group; + + EC_POINT *pub_key; + BIGNUM *priv_key; + + unsigned int enc_flag; + point_conversion_form_t conv_form; + + int references; + int flags; + + EC_EXTRA_DATA *method_data; +} EC_KEY; +static const char *engine_my_id = "111"; +static const char *engine_my_name = "myengine"; + + +/**************************************************************************** + * Functions to handle the engine * + ***************************************************************************/ +static int my_destroy(ENGINE *e); +static int my_init(ENGINE *e); +static int my_finish(ENGINE *e); + + +/**************************************************************************** + * Engine commands * +*****************************************************************************/ +static const ENGINE_CMD_DEFN my_cmd_defns[] = +{ + {0, NULL, NULL, 0} +}; + +static int my_ecdh_compute_key(void *out, size_t len, const EC_POINT *pub_key, +EC_KEY *ecdh, void *(*KDF)(const void *in, size_t inlen, void *out, size_t *outlen)); + +/* +some definations missing in openssl header files public accessible +*/ + +struct ec_point_st { + const EC_METHOD *meth; + + /* All members except 'meth' are handled by the method functions, + * * even if they appear generic */ + + BIGNUM X; + BIGNUM Y; + BIGNUM Z; /* Jacobian projective coordinates: + * (X, Y, Z) represents (X/Z^2, Y/Z^3) if Z != 0 */ + int Z_is_one; /* enable optimized point arithmetics for special case */ +} /* EC_POINT */; + +typedef struct ec_point_st EC_POINT; + +struct ec_group_st { + const EC_METHOD *meth; + + EC_POINT *generator; /* optional */ + BIGNUM order, cofactor; + + int curve_name;/* optional NID for named curve */ + int asn1_flag; /* flag to control the asn1 encoding */ + point_conversion_form_t asn1_form; + + unsigned char *seed; /* optional seed for parameters (appears in ASN1) */ + size_t seed_len; + + struct EC_EXTRA_DATA *extra_data; /* linked list */ + + /* The following members are handled by the method functions, + * even if they appear generic */ + + BIGNUM field; /* Field specification. + * For curves over GF(p), this is the modulus; + * for curves over GF(2^m), this is the + * irreducible polynomial defining the field. + */ + + int poly[6]; /* Field specification for curves over GF(2^m). + * The irreducible f(t) is then of the form: + * t^poly[0] + t^poly[1] + ... + t^poly[k] + * where m = poly[0] > poly[1] > ... > poly[k] = 0. + * The array is terminated with poly[k+1]=-1. + * All elliptic curve irreducibles have at most 5 + * non-zero terms. + */ + + BIGNUM a, b; /* Curve coefficients. + * (Here the assumption is that BIGNUMs can be used + * or abused for all kinds of fields, not just GF(p).) + * For characteristic > 3, the curve is defined + * by a Weierstrass equation of the form + * y^2 = x^3 + a*x + b. + * For characteristic 2, the curve is defined by + * an equation of the form + * y^2 + x*y = x^3 + a*x^2 + b. + */ + + int a_is_minus3; /* enable optimized point arithmetics for special case */ + + void *field_data1; /* method-specific (e.g., Montgomery structure) */ + void *field_data2; /* method-specific */ + int (*field_mod_func)(BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *); /* method-specific */ +} /* EC_GROUP */; + +struct ec_key_st { + int version; + + EC_GROUP *group; + + EC_POINT *pub_key; + BIGNUM *priv_key; + + unsigned int enc_flag; + point_conversion_form_t conv_form; + + int references; + int flags; + + struct EC_EXTRA_DATA *method_data; +} /* EC_KEY */; + + +struct ecdh_method + { + const char *name; + int (*compute_key)(void *key, size_t outlen, const EC_POINT *pub_key, EC_KEY *ecdh,void *(*KDF)(const void *in, size_t inlen, void *out, size_t *outlen)); +#if 0 + int (*init)(EC_KEY *eckey); + int (*finish)(EC_KEY *eckey); +#endif + int flags; + char *app_data; + }; + +static ECDH_METHOD my_ecdh = { + "myengine", + my_ecdh_compute_key, +#if 0 + NULL, /* init */ + NULL, /* finish */ +#endif + 0, /* flags */ + NULL /* app_data */ +}; + +/**************************************************************************** + * Symetric cipher and digest function registrars * +*****************************************************************************/ + +static int my_ciphers(ENGINE *e, const EVP_CIPHER **cipher,const int **nids, int nid); + +static int my_digests(ENGINE *e, const EVP_MD **digest,const int **nids, int nid); + + +static int my_cipher_nids[] ={ NID_des_cbc, NID_des_ede3_cbc, NID_desx_cbc, 0 }; +static int my_digest_nids[] ={ NID_md2, NID_md5, 0 }; + +/*__declspec(dllexport)*/ void ENGINE_load_myengine(void); + diff --git a/engines/zeromem/engine/mytest.c b/engines/zeromem/engine/mytest.c new file mode 100755 index 00000000..401aa109 --- /dev/null +++ b/engines/zeromem/engine/mytest.c @@ -0,0 +1,106 @@ +//test.c +#include +#include +#include +#include +#include +#include + +static void display_engine_list() +{ + ENGINE *h; + int loop; + + h = ENGINE_get_first(); + loop = 0; + printf("listing available engine types\n"); + while(h) + { + printf("engine %i, id = \"%s\", name = \"%s\"\n", + loop++, ENGINE_get_id(h), ENGINE_get_name(h)); + h = ENGINE_get_next(h); + } + printf("end of list\n"); + /* ENGINE_get_first() increases the struct_ref counter, so we + must call ENGINE_free() to decrease it again */ + ENGINE_free(h); +} + + +void test() +{ + ENGINE *e = NULL; + int rv; + unsigned char buf[1024]; + EVP_PKEY *evpKey; + + EC_KEY *key; + EC_POINT *pubkey; + EC_GROUP *group; + EC_builtin_curve *curves; + int crv_len; + char shareKey1[10240],shareKey2[10240]; + int ret,nid,size,i,sig_len; + int len1,len2; + + crv_len = EC_get_builtin_curves(NULL, 0); + curves = (EC_builtin_curve *)malloc(sizeof(EC_builtin_curve) * crv_len); + EC_get_builtin_curves(curves, crv_len); + nid = NID_sect163k1; + group=EC_GROUP_new_by_curve_name(nid); + + key=EC_KEY_new(); + ret=EC_KEY_set_group(key,group); + ret=EC_KEY_generate_key(key); + ret=EC_KEY_check_key(key); + pubkey = EC_KEY_get0_public_key(key); + + ENGINE_load_myengine(); + display_engine_list(); + + len1=ECDH_compute_key(shareKey1, 10240, pubkey, key, NULL); + e = ENGINE_by_id("111"); + printf("get myengine engine OK.name:%s\n",ENGINE_get_name(e)); + ENGINE_register_ECDH(e); + //rv = ENGINE_set_default(e,ENGINE_METHOD_ALL); + + + len2=ECDH_compute_key(shareKey2, 10240, pubkey, key, NULL); + + printf("len: %d, %d\n", len1, len2); + if(len1!=len2) + { + printf("err: %d, %d\n", len1, len2); + } + else + { + ret=memcmp(shareKey1,shareKey2,len1); + if(ret==0) + { + printf("right\n"); + } + else + printf("wrong\n"); + } + printf("test ok!\n"); + /*ENGINE_register_RSA(e); + rv = ENGINE_set_default(e,ENGINE_METHOD_ALL); + evpKey = EVP_PKEY_new(); + rsa = RSA_generate_key(1024,RSA_F4,NULL,NULL); + rv = EVP_PKEY_set1_RSA(evpKey,rsa); + rv = EVP_PKEY_encrypt(buf,buf,128,evpKey); +*/ +/* rv = ENGINE_finish(e); + + rv = ENGINE_free(e); + printf("test end.\n"); + return;*/ + } + +int main() +{ + test(); + return 0; +} + + diff --git a/engines/zeromem/kernel/cba-ecc.c b/engines/zeromem/kernel/cba-ecc.c new file mode 100755 index 00000000..9d44029d --- /dev/null +++ b/engines/zeromem/kernel/cba-ecc.c @@ -0,0 +1,208 @@ +#include +#include + +#include +#include + +#include +#include +#include +#include "../ec2m_kern.h" +#include "../ec.h" + +unsigned int sqr_table[1 << 16]; +struct sock *sock_fd = NULL; + +mm_256 gkey; + +void init_sqr_table(void){ + unsigned int i, j; + unsigned int t; + unsigned int n; + for(i = 0; i < sizeof(sqr_table) / sizeof(sqr_table[0]); i++){ + t = 0; + j = i; + n = 16; + while(n-- > 0){ + t = t << 2; + t |= ((j >> n) & 0x1); + } + sqr_table[i] = t; + } +} + +void import_key(void* info) { + mm_256* key; + mm_256 tkey; + + int cpu_id; + unsigned long irqs; + cpu_id = get_cpu(); + local_irq_save(irqs); + + //printk(KERN_INFO"%s on %d\n", __FUNCTION__, cpu_id); + key = (mm_256*)info; + // print value in dr0-3 previously + __asm__( + "movq %%dr0, %%rax\n\t" + "vmovq %%rax, %%xmm15\n\t" + "movq %%dr1, %%rax\n\t" + "vpinsrq $1, %%rax, %%xmm15, %%xmm15\n\t" + "movq %%dr2, %%rax\n\t" + "vmovq %%rax, %%xmm14\n\t" + "vinsertf128 $1, %%xmm14, %%ymm15, %%ymm15\n\t" + "vmovdqu %%ymm15, %0\n\t" + :"=m"(tkey) + : + : "rax", "memory" + ); + + //printk(KERN_INFO"debug regs:%016llx%016llx%016llx%016llx\n", tkey.iv[3], tkey.iv[2], tkey.iv[1], tkey.iv[0]); + //printk(KERN_INFO"key: %016llx%016llx%016llx\n", key->iv[2], key->iv[1], key->iv[0]); + gkey = *key; + + + __asm__( + "movq %0, %%dr0\n" + "movq %1, %%dr1\n" + "movq %2, %%dr2\n" + : + :"r"(key->iv[0]),"r"(key->iv[1]),"r"(key->iv[2]) + : "memory" + ); + local_irq_restore(irqs); + put_cpu(); +} + +int k_ec2m_import_key(mm_256* key) +{ + import_key(key); + smp_call_function(import_key, key, 1); + return OK; +} + +int k_ec2m_private_op(mm256_point_t* Q, mm256_point_t* P) +{ + int cpu_id; + unsigned long irqs; + cpu_id = get_cpu(); + local_irq_save(irqs); + + //printk(KERN_INFO"%s on %d\n", __FUNCTION__, cpu_id); + + __asm__ __volatile__( + "movq %%dr0, %%rax\n\t" + "vmovq %%rax, %%xmm15\n\t" + "movq %%dr1, %%rax\n\t" + "vpinsrq $1, %%rax, %%xmm15, %%xmm15\n\t" + "movq %%dr2, %%rax\n\t" + "vmovq %%rax, %%xmm14\n\t" + "vinsertf128 $1, %%xmm14, %%ymm15, %%ymm15\n\t" + : + : + : "rax", "memory" + ); + gf2_point_mul_with_preset_key(P, Q, 1, 1); + + local_irq_restore(irqs); + put_cpu(); + + return OK; +} + + +void nl_recv_msg(struct sk_buff* skb){ + struct nlmsghdr *nlh; + struct sk_buff* out; + struct ec2m_request_st* req; + struct ec2m_response_st resp; + int pid; + int size; + char *buf; + int r; + + nlh=(struct nlmsghdr*)skb->data; + size = nlmsg_len(nlh);// - NLMSG_HDRLEN; + + pid = nlh->nlmsg_pid; /*pid of sending process */ + /* printk(KERN_INFO "Netlink received a new msg from %d, size: %d\n", pid, size); */ + buf = nlmsg_data(nlh); + req = (struct ec2m_request_st*)buf; + /* printk(KERN_INFO "got a request: %d, len: %d", req->func, req->len); */ + + switch (req->func) { + case REQ_IMPORT_KEY: + { + mm_256* key; + key = (mm_256*) (buf + sizeof(struct ec2m_request_st)); + resp.result = k_ec2m_import_key(key); + size = sizeof(struct ec2m_response_st); + buf = kmalloc(size, GFP_KERNEL); + memcpy(buf, &resp, sizeof(resp)); + break; + } + case REQ_PRIVATE_OP: + { + mm256_point_t* P; + mm256_point_t Q; + P = (mm256_point_t*) (buf + sizeof(struct ec2m_request_st)); + resp.result = k_ec2m_private_op(&Q, P); + size = sizeof(struct ec2m_response_st) + sizeof(mm256_point_t); + buf = kmalloc(size, GFP_KERNEL); + memcpy(buf, &resp, sizeof(resp)); + memcpy(buf + sizeof(resp), &Q, sizeof(Q)); + break; + } + + } + + out = nlmsg_new(size, 0); + nlh = nlmsg_put(out, 0, 0, NLMSG_DONE, size, 0); + NETLINK_CB(out).dst_group = 0; /* not in mcast group */ + memcpy(nlmsg_data(nlh), buf, size); + r = nlmsg_unicast(sock_fd, out, pid); + if (r < 0){ + printk(KERN_INFO "forward msg to %d failed, err code %d\n", pid, r); + } + kfree(buf); +} + + +int init_netlink(void){ + struct netlink_kernel_cfg cfg = {0}; + cfg.input = nl_recv_msg; + sock_fd = netlink_kernel_create(&init_net, NETLINK_ECC, &cfg ); + + if(!sock_fd) + { + printk(KERN_ALERT "Error creating socket.\n"); + return -1; + } + printk(KERN_ALERT "creating socket successfully.\n"); + + return 0; +} + + + +int __init ecc_init(void) { + // init netlink + init_netlink(); + init_sqr_table(); + + return 0; +} + + +void __exit ecc_exit(void) { + // netlink clean up + if(sock_fd != NULL) + netlink_kernel_release(sock_fd); +} + + +module_init(ecc_init); +module_exit(ecc_exit); + + +MODULE_LICENSE("GPL"); diff --git a/engines/zeromem/kernel/install.sh b/engines/zeromem/kernel/install.sh new file mode 100755 index 00000000..00f78fe4 --- /dev/null +++ b/engines/zeromem/kernel/install.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +sudo rmmod ecc +sudo insmod ecc.ko +dmesg|tail diff --git a/engines/zeromem/kernel_test.c b/engines/zeromem/kernel_test.c new file mode 100755 index 00000000..fc1eab49 --- /dev/null +++ b/engines/zeromem/kernel_test.c @@ -0,0 +1,217 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "kernel_test.h" +#include "ec.h" +#include "ec2m_kern.h" +#include "util.h" + + +#define _NR_sse_switch 312 + +struct ec_point_st { + const EC_METHOD *meth; + + /* All members except 'meth' are handled by the method functions, + * even if they appear generic */ + + BIGNUM X; + BIGNUM Y; + BIGNUM Z; /* Jacobian projective coordinates: + * (X, Y, Z) represents (X/Z^2, Y/Z^3) if Z != 0 */ + int Z_is_one; /* enable optimized point arithmetics for special case */ +} /* EC_POINT */; + +int testSSE(){ + // try perform an simple packed add operation + mm_256 a, b, c; + + syscall(_NR_sse_switch, 0); + + a.iv[0] = 0; + a.iv[1] = 1; + b.iv[0] = 2; + b.iv[1] = 3; + __asm__ __volatile__ ("vmovdqu %0, %%ymm0" : : "m"(a)); + __asm__ __volatile__ ("vmovdqu %0, %%ymm1" : : "m"(b)); + __asm__ __volatile__ ("vaddpd %ymm0, %ymm1, %ymm1"); + __asm__ __volatile__ ("vmovdqu %%ymm1, %0" : "=m"(c) :); + printf("%ld, %ld\n", c.iv[0], c.iv[1]); + + //syscall(_NR_sse_switch, 0); + + return 1; +} + +void print_num(mm_256* m) +{ + int i; + int nonzero = 0; + + for (i = sizeof(mm_256) - 1; i >= 0; i--) { + if (!nonzero){ + if (m->bv[i] != 0) + nonzero = 1; + else + continue; + } + printf("%02x", m->bv[i]); + } + if (!nonzero) + printf("0"); +} + + +void print_point(mm256_point_t* p) +{ + print_num(&p->x); + printf(", "); + print_num(&p->y); + printf(", "); + print_num(&p->z); +} + +void print_ec_point(EC_POINT* p) +{ + printf("("); + BN_print_fp(stdout, &p->X); + printf(":"); + BN_print_fp(stdout, &p->Y); + printf(":"); + BN_print_fp(stdout, &p->Z); + printf(")"); +} + + + +int testAPI() +{ + int r; + int nid; + EC_KEY *key; + BIO *bio_out; + const BIGNUM* rkey; + const EC_GROUP* group; + const EC_POINT* ukey; + const EC_POINT* G, *pr; + BIGNUM* x, *y; + + BN_CTX* ctx; + mm_256 mkey; + mm256_point_t mp, mq; + mm_256 z_; + + + init_sqr_table(); + + + ctx = BN_CTX_new(); + BN_CTX_start(ctx); + x = BN_CTX_get(ctx); + y = BN_CTX_get(ctx); + + // open stdout as bio + bio_out = BIO_new_fp(stdout, BIO_NOCLOSE); + + // get curve nid + /* nid = EC_curve_nist2nid("sect163k1"); */ + nid = OBJ_sn2nid(SN_sect163k1); + + // generate the key + key = EC_KEY_new_by_curve_name(nid); + assert(key != NULL); + r = EC_KEY_generate_key(key); + assert(r == 1); + + // print key + EC_KEY_print(bio_out, key, 0); + // get group + group = EC_KEY_get0_group(key); + // get generator + G = EC_GROUP_get0_generator(group); + // get private key + rkey = EC_KEY_get0_private_key(key); + memset(&mkey, 0, sizeof(mkey)); + memcpy(&mkey, rkey->d, rkey->top * sizeof(rkey->d[0])); + print_num(&mkey); + printf("\n"); + + // get the public key + ukey = EC_KEY_get0_public_key(key); + + // init api + r = ec2m_kern_init(); + assert(r == 0); + printf("ec2m init done.\n"); + + + // import the private key + r = ec2m_import_key(&mkey); + assert(r == 0); + + // calculate r=G*k + // r should be equal to the public key + EC_POINT_get_affine_coordinates_GF2m(group, G, x, y, ctx); + memset(&mq, 0, sizeof(mq)); + memset(&mp, 0, sizeof(mp)); + memcpy(&mp.x, x->d, sizeof(x->d[0]) * x->top); + memcpy(&mp.y, y->d, sizeof(y->d[0]) * y->top); + mp.z.iv[0] = 1; + + bn_expand2(x, 3); + bn_expand2(y, 3); + + gf2_point_mul(&mp, &mkey, &mq, 1, 1); + print_mm_point(&mq); + printf("\n"); + + r = ec2m_private_operation(&mp, &mq); + assert(r == 0); + + print_mm_point(&mq); + printf("\n"); + + /* printf("inv(z): "); */ + /* gf2m_inv(&mq.z, &z_); */ + /* print_num(&z_); */ + /* printf("\n"); */ + + /* gf2_mod_mul(&mq.x, &z_, &mq.x); */ + /* gf2_mod_mul(&mq.y, &z_, &mq.y); */ + /* gf2_mod_mul(&mq.z, &z_, &mq.z); */ + /* print_mm_point(&mq); */ + /* printf("\n"); */ + + pr = EC_POINT_new(group); + EC_POINT_mul(group, pr, NULL, G, rkey, ctx); + print_ec_point(pr); + printf("\n"); + EC_POINT_get_affine_coordinates_GF2m(group, pr, x, y, ctx); + + + ec2m_kern_clean(); + + BN_CTX_end(ctx); + BN_CTX_free(ctx); + + + return 0; + +} + + +int main() +{ + testSSE(); + testAPI(); + + return 0; + +} diff --git a/engines/zeromem/kernel_test.h b/engines/zeromem/kernel_test.h new file mode 100755 index 00000000..7dbd1eee --- /dev/null +++ b/engines/zeromem/kernel_test.h @@ -0,0 +1,6 @@ +#ifndef _KERNEL_TEST_H_ +#define _KERNEL_TEST_H_ + +extern int testSSE(); + +#endif diff --git a/engines/zeromem/sys_ec2m.c b/engines/zeromem/sys_ec2m.c new file mode 100755 index 00000000..084735ac --- /dev/null +++ b/engines/zeromem/sys_ec2m.c @@ -0,0 +1,22 @@ +#include +#include "sys_ec2m.h" + +int sys_ec2m_alloc(void) +{ + return syscall(__NR_ec2m_alloc); +} + +int sys_ec2m_free(int rid) +{ + return syscall(__NR_ec2m_free, rid); +} + +int sys_ec2m_setkey(int rid, mm_256* key, int a, int b) +{ + return syscall(__NR_ec2m_setkey, rid, (void*)key, a, b); +} + +int sys_ec2m_encrypt(int rid, mm256_point_t* bufin, mm256_point_t* bufout) +{ + return syscall(__NR_ec2m_encrypt, rid, (void*)bufin, (void*)bufout); +} diff --git a/engines/zeromem/sys_ec2m.h b/engines/zeromem/sys_ec2m.h new file mode 100755 index 00000000..ec204a0a --- /dev/null +++ b/engines/zeromem/sys_ec2m.h @@ -0,0 +1,16 @@ +#ifndef _SYS_EC2M_H_ +#define _SYS_EC2M_H_ + +#define __NR_ec2m_alloc 312 +#define __NR_ec2m_free 313 +#define __NR_ec2m_setkey 314 +#define __NR_ec2m_encrypt 315 + +#include "ec.h" + +extern int sys_ec2m_alloc(void); +extern int sys_ec2m_free(int rid); +extern int sys_ec2m_setkey(int rid, mm_256* key, int a, int b); +extern int sys_ec2m_encrypt(int rid, mm256_point_t* bufin, mm256_point_t* bufout); + +#endif diff --git a/engines/zeromem/test.c b/engines/zeromem/test.c new file mode 100755 index 00000000..bedf149f --- /dev/null +++ b/engines/zeromem/test.c @@ -0,0 +1,1653 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "ec.h" +#include "aes_tight.h" +#include "util.h" +#include "sys_ec2m.h" +#include "ec2m_kern.h" + +#include "test.h" + +/* + * list all embedded elliptic curves in openssl: + * # openssl ecparam -list_curves + */ +#define curve_sect163k1 "sect163k1" +#define curve_sect163r1 "sect163r1" +#define curve_sect233k1 "sect233k1" +#define curve_sect233r1 "sect233r1" + +/* Lopez-Dahab coordinates */ +#define __LD__ + +/* Affine Coordinates */ +#define __AFFINE__ + +const int sz_buf = 1024; +const int cntUS = 1000000; + +BN_CTX *ctx; +EC_GROUP *ec_group; +const EC_POINT *G; +static BIGNUM *p = NULL; +static BIGNUM *a = NULL; +static BIGNUM *b = NULL; +static BIGNUM *x = NULL; +static BIGNUM *y = NULL; +static BIGNUM *n = NULL; +static BIGNUM *h = NULL; +static int a_is_one = 0; +static int b_is_one = 0; + +int initDomainParameters(int argc, char** argv){ + ctx = BN_CTX_new(); + ec_group = EC_GROUP_new_by_curve_name(OBJ_sn2nid("sect163k1")); + p = BN_new(); + a = BN_new(); + b = BN_new(); + x = BN_new(); + y = BN_new(); + n = BN_new(); + h = BN_new(); + + assert(EC_GROUP_get_curve_GF2m(ec_group, p, a, b, NULL)); + assert(EC_GROUP_get_order(ec_group, n, NULL)); + assert(EC_GROUP_get_cofactor(ec_group, h, NULL)); + G = EC_GROUP_get0_generator(ec_group); + assert(G); + assert(EC_POINT_get_affine_coordinates_GF2m(ec_group, G, x, y, NULL)); + + if (BN_is_one(a)) + a_is_one = 1; + if (BN_is_one(b)) + b_is_one = 1; + + init_sqr_table(); + return 1; +} + +void domain_parameters_print() { + assert(p && a && b && x && y && n && h); + + printf("p = 0x %s\n", BN_bn2str(p)); + printf("a = 0x %s\n", BN_bn2str(a)); + printf("b = 0x %s\n", BN_bn2str(b)); + printf("x = 0x %s\n", BN_bn2str(x)); + printf("y = 0x %s\n", BN_bn2str(y)); + printf("n = 0x %s\n", BN_bn2str(n)); + printf("h = 0x %s\n", BN_bn2str(h)); +} + +void ec_point_set_infinity(ec_point_t *P) { + BN_one(P->X); + BN_zero(P->Y); + BN_zero(P->Z); +} + +void ec_point_set_affine_xy(ec_point_t *P, const BIGNUM *ax, const BIGNUM *ay) { + BN_copy(P->X, ax); + BN_copy(P->Y, ay); + BN_one(P->Z); +} + +void ec_point_ld_to_affine(ec_point_t *P) { +} + + +/* in Lopez-Dahab co-ordinates + * the point at infinity (oo) is (1: 0: 0) + * and -(X: Y: Z) is (X: X+Y: Z) + */ +int ec_point_is_at_infinity(const ec_point_t __LD__ *P) { + assert(P->X && P->Y && P->Z); + if (BN_is_one(P->X) && BN_is_zero(P->Y) && BN_is_zero(P->Z)) + return 1; + return 0; +} + +void ec_point_copy(ec_point_t *R, const ec_point_t *P) { + BN_copy(R->X, P->X); + BN_copy(R->Y, P->Y); + BN_copy(R->Z, P->Z); +} + +/* + * Algorithm 3.24 in "Guide to Elliptic Curve Cryptography" + * P = (X1: Y1: Z1) + * R = 2P = (X3: Y3: Z3) + */ +void ec_point_double(ec_point_t __LD__ *R, const ec_point_t __LD__ *P) { + int r; + BN_CTX *ctx = BN_CTX_new(); + const BIGNUM *X1 = P->X; + const BIGNUM *Y1 = P->Y; + const BIGNUM *Z1 = P->Z; + BIGNUM *X3 = R->X; + BIGNUM *Y3 = R->Y; + BIGNUM *Z3 = R->Z; + BIGNUM *T1 = BN_new(); + BIGNUM *T2 = BN_new(); + + debug(" 1. if P == oo, return P. "); + if (ec_point_is_at_infinity(P)) { + debug("P == oo\n"); + ec_point_copy(R, P); + return; + } else { + debug("P != oo\n"); + } + + debug(" 2. T1 = Z1^2"); + r = BN_GF2m_mod_sqr(T1, Z1, p, ctx); + assert(r); + debug(" = %s\n", BN_bn2str(T1)); + + debug(" 3. T2 = X1^2"); + r = BN_GF2m_mod_sqr(T2, X1, p, ctx); + assert(r); + debug(" = %s\n", BN_bn2str(T2)); + + debug(" 4. Z3 = T1 * T2"); + r = BN_GF2m_mod_mul(Z3, T1, T2, p, ctx); + assert(r); + debug(" = %s\n", BN_bn2str(Z3)); + + debug(" 5. X3 = T2^2"); + r = BN_GF2m_mod_sqr(X3, T2, p, ctx); + assert(r); + debug(" = %s\n", BN_bn2str(X3)); + + debug(" 6. T1 = T1^2"); + r = BN_GF2m_mod_sqr(T1, T1, p, ctx); + assert(r); + debug(" = %s\n", BN_bn2str(T1)); + + debug(" 7. T2 = T1 * b"); + if (b_is_one) + BN_copy(T2, T1); + else + r = BN_GF2m_mod_mul(T2, T1, b, p, ctx); + assert(r); + debug(" = %s\n", BN_bn2str(T2)); + + debug(" 8. X3 = X3 + T2"); + r = BN_GF2m_add(X3, X3, T2); + assert(r); + debug(" = %s\n", BN_bn2str(X3)); + + debug(" 9. T1 = Y1^2"); + r = BN_GF2m_mod_sqr(T1, Y1, p, ctx); + assert(r); + debug(" = %s\n", BN_bn2str(T1)); + + debug("10. if a==1, T1 = T1 + Z3, "); + if (a_is_one) { + debug("a == 1, T1 = T1 + Z3"); + r = BN_GF2m_add(T1, T1, Z3); + assert(r); + debug(" = %s\n", BN_bn2str(T1)); + } else { + debug("a != 1, do nothing\n"); + } + + debug("11. T1 = T1 + T2"); + r = BN_GF2m_add(T1, T1, T2); + assert(r); + debug(" = %s\n", BN_bn2str(T1)); + + debug("12. Y3 = X3 * T1"); + r = BN_GF2m_mod_mul(Y3, X3, T1, p, ctx); + assert(r); + debug(" = %s\n", BN_bn2str(X3)); + + debug("13. T1 = T2 * Z3"); + r = BN_GF2m_mod_mul(T1, T2, Z3, p, ctx); + assert(r); + debug(" = %s\n", BN_bn2str(T1)); + + debug("14. Y3 = Y3 + T1"); + r = BN_GF2m_add(Y3, Y3, T1); + assert(r); + debug(" = %s\n", BN_bn2str(Y3)); + + debug("15. return (X3: Y3: Z3) = (%s: %s: %s)\n", BN_bn2str(X3), BN_bn2str(Y3), BN_bn2str(Z3)); + return; +} + +void ec_point_add(ec_point_t __LD__ *R, const ec_point_t __LD__ *P, const ec_point_t __AFFINE__ *Q) { + int r; + BN_CTX *ctx = BN_CTX_new(); + const BIGNUM *X1 = P->X; + const BIGNUM *Y1 = P->Y; + const BIGNUM *Z1 = P->Z; + const BIGNUM *x2 = Q->X; + const BIGNUM *y2 = Q->Y; + BIGNUM *X3 = R->X; + BIGNUM *Y3 = R->Y; + BIGNUM *Z3 = R->Z; + BIGNUM *T1 = BN_new(); + BIGNUM *T2 = BN_new(); + BIGNUM *T3 = BN_new(); + + debug(" 1. if Q == oo, return P. Q should not be oo\n"); + + debug(" 2. if P == oo, return Q. "); + if (ec_point_is_at_infinity(P)) { + debug(" P == oo, return Q\n"); + ec_point_copy(R, Q); + return; + } else { + debug(" P != oo\n"); + } + + debug(" 3. T1 = Z1 * x2"); + r = BN_GF2m_mod_mul(T1, Z1, x2, p, ctx); + assert(r); + debug(" = %s\n", BN_bn2str(T1)); + + debug(" 4. T2 = Z1^2"); + r = BN_GF2m_mod_sqr(T2, Z1, p, ctx); + assert(r); + debug(" = %s\n", BN_bn2str(T2)); + + debug(" 5. X3 = X1 + T1"); + r = BN_GF2m_add(X3, X1, T1); + assert(r); + debug(" = %s\n", BN_bn2str(X3)); + + debug(" 6. T1 = Z1 * X3"); + r = BN_GF2m_mod_mul(T1, Z1, X3, p, ctx); + assert(r); + debug(" = %s\n", BN_bn2str(T1)); + + debug(" 7. T3 = T2 * y2"); + r = BN_GF2m_mod_mul(T3, T2, y2, p, ctx); + assert(r); + debug(" = %s\n", BN_bn2str(T3)); + + debug(" 8. Y3 = Y1 + T3"); + r = BN_GF2m_add(Y3, Y1, T3); + assert(r); + debug(" = %s\n", BN_bn2str(Y3)); + + /* 9. if X3 == 0, + if Y3 == 0, (X3: Y3: Z3) = 2(x2: y2: 1) + else return oo + */ + debug(" 9. if X3 == 0 { if Y3== 0, return 2(x2: y2: 1) } else return oo\n"); + if (BN_is_zero(X3)) { + debug("X3 == 0\n"); + if (BN_is_zero(Y3)) { + debug("Y3 == 0\n"); + ec_point_double(R, P); + return; + } + } + + + debug("10. Z3 = T1^2"); + r = BN_GF2m_mod_sqr(Z3, T1, p, ctx); + assert(r); + debug(" = %s\n", BN_bn2str(Z3)); + + debug("11. T3 = T1 * Y3"); + r = BN_GF2m_mod_mul(T3, T1, Y3, p, ctx); + debug(" = %s\n", BN_bn2str(T3)); + + debug("12. if a==1, T1 = T1 + T2\n"); + if (a_is_one) { + debug("a == 1, T1 = T1 + T2"); + r = BN_GF2m_add(T1, T1, T2); + debug(" = %s\n", BN_bn2str(T1)); + } + + debug("13. T2 = X3^2"); + r = BN_GF2m_mod_sqr(T2, X3, p, ctx); + assert(r); + debug(" = %s\n", BN_bn2str(T2)); + + + debug("14. X3 = T2 * T1"); + r = BN_GF2m_mod_mul(X3, T2, T1, p, ctx); + assert(r); + debug(" = %s\n", BN_bn2str(X3)); + + debug("15. T2 = Y3^2"); + r = BN_GF2m_mod_sqr(T2, Y3, p, ctx); + assert(r); + debug(" = %s\n", BN_bn2str(T2)); + + debug("16. X3 = X3 + T2"); + r = BN_GF2m_add(X3, X3, T2); + assert(r); + debug(" = %s\n", BN_bn2str(X3)); + + debug("17. X3 = X3 + T3"); + r = BN_GF2m_add(X3, X3, T3); + assert(r); + debug(" = %s\n", BN_bn2str(X3)); + + debug("18. T2 = x2 * Z3"); + r = BN_GF2m_mod_mul(T2, x2, Z3, p, ctx); + assert(r); + debug(" = %s\n", BN_bn2str(T2)); + + debug("19. T2 = T2 + X3"); + r = BN_GF2m_add(T2, T2, X3); + assert(r); + debug(" = %s\n", BN_bn2str(T2)); + + debug("20. T1 = Z3^2"); + r = BN_GF2m_mod_sqr(T1, Z3, p, ctx); + assert(r); + debug(" = %s\n", BN_bn2str(T1)); + + debug("21. T3 = T3 + Z3"); + r = BN_GF2m_add(T3, T3, Z3); + assert(r); + debug(" = %s\n", BN_bn2str(T3)); + + debug("22. Y3 = T3 * T2"); + r = BN_GF2m_mod_mul(Y3, T3, T2, p, ctx); + assert(r); + debug(" = %s\n", BN_bn2str(Y3)); + + debug("23. T2 = x2 + y2"); + r = BN_GF2m_add(T2, x2, y2); + assert(r); + debug(" = %s\n", BN_bn2str(T2)); + + debug("24. T3 = T1 * T2"); + r = BN_GF2m_mod_mul(T3, T1, T2, p, ctx); + assert(r); + debug(" = %s\n", BN_bn2str(T3)); + + debug("25. Y3 = Y3 + T3"); + r = BN_GF2m_add(Y3, Y3, T3); + assert(r); + debug(" = %s\n", BN_bn2str(Y3)); + + debug("26. return (X3: Y3: Z3) = (%s: %s: %s)\n", BN_bn2str(X3), BN_bn2str(Y3), BN_bn2str(Z3)); + return; +} + +void ec_point_multiply(ec_point_t __LD__ *R, const ec_point_t __AFFINE__ *P, const BIGNUM* K){ + const int t = 163; + int i; + int b; + ec_point_t Q; + debug("1. Q = infinity\n"); + ec_point_init(&Q); + BN_set_word(Q.X, 1); + BN_set_word(Q.Y, 0); + BN_set_word(Q.Z, 0); + + debug("2. for i from t - 1 downto 0 do\n"); + for(i = t - 1; i >= 0; i--){ + b = BN_is_bit_set(K, i); + if(b){ + // printf("k_%d = %d\n", i, b); + } + debug("2.1 Q = 2Q\n"); + ec_point_double(R, &Q); + ec_point_copy(&Q, R); + + debug("2.2 if ki = 1 then Q = Q + P\n"); + if(b == 1){ + ec_point_add(R, &Q, P); + ec_point_copy(&Q, R); + } + } + + debug("3. return Q\n"); + ec_point_copy(R, &Q); +} + +int testFieldArithmetic(){ + mm_256 ma, mb, mr; + char* pa, *pb, *pr; + BIGNUM* ta = BN_new(); + BIGNUM* tb = BN_new(); + BIGNUM* tr = BN_new(); + + int passed = 0; + int failed = 0; + printf("test arithmetic operations on gf2m:\n"); + + assert(BN_rand_range(ta, n)); + assert(BN_rand_range(tb, n)); + + // addition + BN_GF2m_add(tr, ta, tb); + pa = BN_bn2hex(ta); + pb = BN_bn2hex(tb); + pr = BN_bn2hex(tr); + printf("0x%s + 0x%s = 0x%s ... ", pa, pb, pr); + OPENSSL_free(pa); + OPENSSL_free(pb); + OPENSSL_free(pr); + + bn_to_mm256(ta, &ma); + bn_to_mm256(tb, &mb); + gf2_add(&ma, &mb, &mr); + if(cmp_mm_256_with_bn(&mr, tr) == 0){ + passed ++; + printf("passed!\n"); + } else { + failed ++; + mm256_to_bn(&mr, tr); + pr = BN_bn2hex(tr); + printf("failed! got %s\n", pr); + OPENSSL_free(pr); + } + + // multiplication + BN_GF2m_mod_mul(tr, ta, tb, p, ctx); + pa = BN_bn2hex(ta); + pb = BN_bn2hex(tb); + pr = BN_bn2hex(tr); + printf("0x%s * 0x%s = 0x%s ... ", pa, pb, pr); + OPENSSL_free(pa); + OPENSSL_free(pb); + OPENSSL_free(pr); + + bn_to_mm256(ta, &ma); + bn_to_mm256(tb, &mb); + gf2_mod_mul(&ma, &mb, &mr); + if(cmp_mm_256_with_bn(&mr, tr) == 0){ + passed ++; + printf("passed!\n"); + } else { + failed ++; + mm256_to_bn(&mr, tr); + pr = BN_bn2hex(tr); + printf("failed! got 0x%s\n", pr); + OPENSSL_free(pr); + } + + // square mod + + BN_GF2m_mod_sqr(tr, ta, p, ctx); + pa = BN_bn2hex(ta); + pr = BN_bn2hex(tr); + printf("0x%s ^ 2 = 0x%s ... ", pa, pr); + OPENSSL_free(pa); + OPENSSL_free(pr); + + bn_to_mm256(ta, &ma); + gf2_mod_sqr(&ma, &mr); + if(cmp_mm_256_with_bn(&mr, tr) == 0){ + passed ++; + printf("passed!\n"); + } else { + failed ++; + mm256_to_bn(&mr, tr); + pr = BN_bn2hex(tr); + printf("failed! got 0x%s\n", pr); + OPENSSL_free(pr); + } + + mm_256 mrt; + gf2_sqr(&ma, &mr, &mrt); + mm256_to_bn(&mr, tr); + pr = BN_bn2hex(tr); + mm256_to_bn(&mrt, ta); + pa = BN_bn2hex(ta); + printf("sqr: (%s, %s)\n", pa, pr); + OPENSSL_free(pr); + OPENSSL_free(pa); + + /* ma.iv[0] = 1; */ + /* ma.iv[1] = 2; */ + /* ma.iv[2] = 3; */ + /* mb.iv[0] = 1; */ + /* mb.iv[1] = 1; */ + /* mb.iv[2] = 1; */ + + /* gf2_mul(&ma, &mb, &mr, &mrt); */ + /* mm256_to_bn(&mr, tr); */ + /* pr = BN_bn2hex(tr); */ + /* mm256_to_bn(&mrt, ta); */ + /* pa = BN_bn2hex(ta); */ + /* printf("mul: (%s, %s)\n", pa, pr); */ + /* OPENSSL_free(pr); */ + /* OPENSSL_free(pa); */ + + bn_to_mm256(ta, &ma); + gf2m_inv_asm(&ma, &mr); + mm256_to_bn(&mr, tr); + pa = BN_bn2hex(ta); + pr = BN_bn2hex(tr); + printf("inv: %s, %s\n", pa, pr); + OPENSSL_free(pr); + OPENSSL_free(pa); + + bn_to_mm256(ta, &ma); + gf2m_inv(&ma, &mr); + mm256_to_bn(&mr, tr); + pa = BN_bn2hex(ta); + pr = BN_bn2hex(tr); + printf("inv: %s, %s\n", pa, pr); + OPENSSL_free(pr); + OPENSSL_free(pa); + + mb = mr; + gf2_mod_mul(&ma, &mb, &mr); + mm256_to_bn(&ma, ta); + mm256_to_bn(&mb, tb); + mm256_to_bn(&mr, tr); + pa = BN_bn2hex(ta); + pb = BN_bn2hex(tb); + pr = BN_bn2hex(tr); + printf("0x%s * 0x%s = 0x%s ... ", pa, pb, pr); + OPENSSL_free(pa); + OPENSSL_free(pb); + OPENSSL_free(pr); + + + // summary + printf("%d/%d test(s) passed.\n", passed, (passed + failed)); + + return failed; +} + +int testAES(){ + const int sz_buf = 1024; + const int sz_ymm_group = 512; + int passed = 0, failed = 0; + uint8_t key[SIZE_AES_KEY_256] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}; + uint8_t pt[SIZE_AES_BLOCK] = {'T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 't', 'e', 's', 't', '!', 0x0}; + uint8_t ct[SIZE_AES_BLOCK]; + AES_KEY ssl_key; + uint8_t ssl_ct[sz_buf]; + uint8_t buf1[sz_buf], buf2[sz_buf]; + int i; + + // aes 128 + printf("AES128:\n"); + printf("plaintext: "); + printHex(pt, SIZE_AES_BLOCK); + printf("\n"); + // openssl + assert(AES_set_encrypt_key(key, 128, &ssl_key) == 0); + AES_encrypt(pt, ssl_ct, &ssl_key); + printf("ciphertext by openssl: "); + printHex(ssl_ct, SIZE_AES_BLOCK); + printf("\n"); + + // tight aes + memcpy(ct, pt, SIZE_AES_BLOCK); + tight_aes_128_set_key(key); + tight_aes_128_enc(ct); + printf("ciphertext by tight aes 128: "); + printHex(ct, SIZE_AES_BLOCK); + printf("\n"); + + if(memcmp(ssl_ct, ct, SIZE_AES_BLOCK) == 0){ + passed++; + printf("passed!\n"); + } else { + failed++; + printf("failed!\n"); + } + + // decrypt by tight aes + tight_aes_128_set_key(key); + //tight_aes_enc(ct); + tight_aes_128_dec(ct); + printf("plain by tight aes 128: "); + printHex(ct, SIZE_AES_BLOCK); + printf("\n"); + + if(memcmp(pt, ct, SIZE_AES_BLOCK) == 0){ + passed++; + printf("passed!\n"); + } else { + failed++; + printf("failed!\n"); + } + + // aes 256 + printf("AES256:\n"); + printf("plaintext: "); + printHex(pt, SIZE_AES_BLOCK); + printf("\n"); + // openssl + assert(AES_set_encrypt_key(key, 256, &ssl_key) == 0); + AES_encrypt(pt, ssl_ct, &ssl_key); + printf("ciphertext by openssl: "); + printHex(ssl_ct, SIZE_AES_BLOCK); + printf("\n"); + + // tight aes + memcpy(ct, pt, SIZE_AES_BLOCK); + tight_aes_256_set_key(key); + tight_aes_256_enc(ct); + printf("ciphertext by tight aes 256: "); + printHex(ct, SIZE_AES_BLOCK); + printf("\n"); + + if(memcmp(ssl_ct, ct, SIZE_AES_BLOCK) == 0){ + passed++; + printf("passed!\n"); + } else { + failed++; + printf("failed!\n"); + } + + // decrypt by tight aes + tight_aes_256_set_key(key); + //memcpy(ct, pt, sizeof(pt)); + //tight_aes_enc(ct); + tight_aes_256_dec(ct); + printf("plaintext by tight aes 256: "); + printHex(ct, SIZE_AES_BLOCK); + printf("\n"); + + if(memcmp(pt, ct, SIZE_AES_BLOCK) == 0){ + passed++; + printf("passed!\n"); + } else { + failed++; + printf("failed!\n"); + } + + // test encrypt ymm group + printf("encrypt ymm group:\n"); + memset(buf1, 0, sz_buf); + memset(buf2, 0, sz_buf); + memset(ssl_ct, 0, sz_buf); + for(i = 0; i < sz_ymm_group; i++) + buf1[i] = rand() & 0xff; + tight_aes_256_set_key(key); + load_ymm_group(buf1); + aes_256_enc_ymm_group(buf2); + printf("plaintext in ymm group:\n"); + for(i = 0; i < 16; i++){ + printHex(buf1 + i * 32, 32); + printf("\n"); + } + for(i = 0; i < 32; i++){ + AES_encrypt(buf1 + i * 16, ssl_ct + i * 16, &ssl_key); + } + if(memcmp(ssl_ct, buf2, sz_ymm_group) == 0){ + passed++; + printf("passed!\n"); + } else { + failed++; + printf("failed!\n"); + for(i = 0; i < 16; i++){ + printf("ymm%d\n", i); + printHex(buf2 + i * 32, 32); + printf("\n"); + printHex(ssl_ct + i * 32, 32); + printf("\n"); + } + } + + // test decrypt ymm group + printf("decrypt ymm group:\n"); + tight_aes_256_set_key(key); + aes_256_dec_ymm_group(ssl_ct); + save_ymm_group(buf2); + if(memcmp(buf1, buf2, sz_ymm_group) == 0){ + passed++; + printf("passed!\n"); + } else { + failed++; + printf("failed!\n"); + for(i = 0; i < 16; i++){ + printf("ymm%d\n", i); + printHex(buf1 + i * 32, 32); + printf("\n"); + printHex(buf2 + i * 32, 32); + printf("\n"); + } + } + + printf("%d/%d test(s) passed.\n", passed, (passed + failed)); + + return failed; +} + +struct ec_method_st { + /* Various method flags */ + int flags; + /* used by EC_METHOD_get_field_type: */ + int field_type; /* a NID */ + + /* used by EC_GROUP_new, EC_GROUP_free, EC_GROUP_clear_free, EC_GROUP_copy: */ + int (*group_init)(EC_GROUP *); + void (*group_finish)(EC_GROUP *); + void (*group_clear_finish)(EC_GROUP *); + int (*group_copy)(EC_GROUP *, const EC_GROUP *); + + /* used by EC_GROUP_set_curve_GFp, EC_GROUP_get_curve_GFp, */ + /* EC_GROUP_set_curve_GF2m, and EC_GROUP_get_curve_GF2m: */ + int (*group_set_curve)(EC_GROUP *, const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *); + int (*group_get_curve)(const EC_GROUP *, BIGNUM *p, BIGNUM *a, BIGNUM *b, BN_CTX *); + + /* used by EC_GROUP_get_degree: */ + int (*group_get_degree)(const EC_GROUP *); + + /* used by EC_GROUP_check: */ + int (*group_check_discriminant)(const EC_GROUP *, BN_CTX *); +/* used by EC_POINT_new, EC_POINT_free, EC_POINT_clear_free, EC_POINT_copy: */ + int (*point_init)(EC_POINT *); + void (*point_finish)(EC_POINT *); + void (*point_clear_finish)(EC_POINT *); + int (*point_copy)(EC_POINT *, const EC_POINT *); + + /* used by EC_POINT_set_to_infinity, + * EC_POINT_set_Jprojective_coordinates_GFp, + * EC_POINT_get_Jprojective_coordinates_GFp, + * EC_POINT_set_affine_coordinates_GFp, ..._GF2m, + * EC_POINT_get_affine_coordinates_GFp, ..._GF2m, + * EC_POINT_set_compressed_coordinates_GFp, ..._GF2m: + */ + int (*point_set_to_infinity)(const EC_GROUP *, EC_POINT *); + int (*point_set_Jprojective_coordinates_GFp)(const EC_GROUP *, EC_POINT *, + const BIGNUM *x, const BIGNUM *y, const BIGNUM *z, BN_CTX *); + int (*point_get_Jprojective_coordinates_GFp)(const EC_GROUP *, const EC_POINT *, + BIGNUM *x, BIGNUM *y, BIGNUM *z, BN_CTX *); + int (*point_set_affine_coordinates)(const EC_GROUP *, EC_POINT *, + const BIGNUM *x, const BIGNUM *y, BN_CTX *); + int (*point_get_affine_coordinates)(const EC_GROUP *, const EC_POINT *, + BIGNUM *x, BIGNUM *y, BN_CTX *); + int (*point_set_compressed_coordinates)(const EC_GROUP *, EC_POINT *, + const BIGNUM *x, int y_bit, BN_CTX *); + + /* used by EC_POINT_point2oct, EC_POINT_oct2point: */ +size_t (*point2oct)(const EC_GROUP *, const EC_POINT *, point_conversion_form_t form, + unsigned char *buf, size_t len, BN_CTX *); + int (*oct2point)(const EC_GROUP *, EC_POINT *, + const unsigned char *buf, size_t len, BN_CTX *); + + /* used by EC_POINT_add, EC_POINT_dbl, ECP_POINT_invert: */ + int (*add)(const EC_GROUP *, EC_POINT *r, const EC_POINT *a, const EC_POINT *b, BN_CTX *); + int (*dbl)(const EC_GROUP *, EC_POINT *r, const EC_POINT *a, BN_CTX *); + int (*invert)(const EC_GROUP *, EC_POINT *, BN_CTX *); + + /* used by EC_POINT_is_at_infinity, EC_POINT_is_on_curve, EC_POINT_cmp: */ + int (*is_at_infinity)(const EC_GROUP *, const EC_POINT *); + int (*is_on_curve)(const EC_GROUP *, const EC_POINT *, BN_CTX *); + int (*point_cmp)(const EC_GROUP *, const EC_POINT *a, const EC_POINT *b, BN_CTX *); + + /* used by EC_POINT_make_affine, EC_POINTs_make_affine: */ + int (*make_affine)(const EC_GROUP *, EC_POINT *, BN_CTX *); + int (*points_make_affine)(const EC_GROUP *, size_t num, EC_POINT *[], BN_CTX *); + + /* used by EC_POINTs_mul, EC_POINT_mul, EC_POINT_precompute_mult, EC_POINT_have_precompute_mult + * (default implementations are used if the 'mul' pointer is 0): */ + int (*mul)(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar, + size_t num, const EC_POINT *points[], const BIGNUM *scalars[], BN_CTX *); + int (*precompute_mult)(EC_GROUP *group, BN_CTX *); + int (*have_precompute_mult)(const EC_GROUP *group); + + + /* internal functions */ + + /* 'field_mul', 'field_sqr', and 'field_div' can be used by 'add' and 'dbl' so that + * the same implementations of point operations can be used with different + * optimized implementations of expensive field operations: */ + int (*field_mul)(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *); + int (*field_sqr)(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, BN_CTX *); + int (*field_div)(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *); + + int (*field_encode)(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, BN_CTX *); /* e.g. to Montgomery */ + int (*field_decode)(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, BN_CTX *); /* e.g. from Montgomery */ + int (*field_set_to_one)(const EC_GROUP *, BIGNUM *r, BN_CTX *); +} /* EC_METHOD */; + +typedef struct ec_extra_data_st { + struct ec_extra_data_st *next; + void *data; + void *(*dup_func)(void *); + void (*free_func)(void *); + void (*clear_free_func)(void *); +} EC_EXTRA_DATA; /* used in EC_GROUP */ + +struct ec_group_st { + const EC_METHOD *meth; + + EC_POINT *generator; /* optional */ + BIGNUM order, cofactor; + + int curve_name;/* optional NID for named curve */ + int asn1_flag; /* flag to control the asn1 encoding */ + point_conversion_form_t asn1_form; + + unsigned char *seed; /* optional seed for parameters (appears in ASN1) */ + size_t seed_len; + + EC_EXTRA_DATA *extra_data; /* linked list */ + + /* The following members are handled by the method functions, + * even if they appear generic */ + + BIGNUM field; /* Field specification. + * For curves over GF(p), this is the modulus; + * for curves over GF(2^m), this is the + * irreducible polynomial defining the field. + */ + + int poly[6]; /* Field specification for curves over GF(2^m). + * The irreducible f(t) is then of the form: + * t^poly[0] + t^poly[1] + ... + t^poly[k] + * where m = poly[0] > poly[1] > ... > poly[k] = 0. + * The array is terminated with poly[k+1]=-1. + * All elliptic curve irreducibles have at most 5 + * non-zero terms. + */ + BIGNUM a, b; /* Curve coefficients. + * (Here the assumption is that BIGNUMs can be used + * or abused for all kinds of fields, not just GF(p).) + * For characteristic > 3, the curve is defined + * by a Weierstrass equation of the form + * y^2 = x^3 + a*x + b. + * For characteristic 2, the curve is defined by + * an equation of the form + * y^2 + x*y = x^3 + a*x^2 + b. + */ + + int a_is_minus3; /* enable optimized point arithmetics for special case */ + + void *field_data1; /* method-specific (e.g., Montgomery structure) */ + void *field_data2; /* method-specific */ + int (*field_mod_func)(BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *); /* method-specific */ +} /* EC_GROUP */; + +static int gf2m_Mdouble(const EC_GROUP *group, BIGNUM *x, BIGNUM *z, BN_CTX *ctx) + { + BIGNUM *t1; + int ret = 0; + + /* Since Mdouble is static we can guarantee that ctx != NULL. */ + BN_CTX_start(ctx); + t1 = BN_CTX_get(ctx); + if (t1 == NULL) goto err; + + if (!group->meth->field_sqr(group, x, x, ctx)) goto err; + if (!group->meth->field_sqr(group, t1, z, ctx)) goto err; + if (!group->meth->field_mul(group, z, x, t1, ctx)) goto err; + if (!group->meth->field_sqr(group, x, x, ctx)) goto err; + if (!group->meth->field_sqr(group, t1, t1, ctx)) goto err; + if (!group->meth->field_mul(group, t1, &group->b, t1, ctx)) goto err; + if (!BN_GF2m_add(x, x, t1)) goto err; + + ret = 1; + + err: + BN_CTX_end(ctx); + return ret; + } + +static int gf2m_Madd(const EC_GROUP *group, const BIGNUM *x, BIGNUM *x1, BIGNUM *z1, + const BIGNUM *x2, const BIGNUM *z2, BN_CTX *ctx) + { + BIGNUM *t1, *t2; + int ret = 0; + + /* Since Madd is static we can guarantee that ctx != NULL. */ + BN_CTX_start(ctx); + t1 = BN_CTX_get(ctx); + t2 = BN_CTX_get(ctx); + if (t2 == NULL) goto err; + + if (!BN_copy(t1, x)) goto err; + if (!group->meth->field_mul(group, x1, x1, z2, ctx)) goto err; + if (!group->meth->field_mul(group, z1, z1, x2, ctx)) goto err; + if (!group->meth->field_mul(group, t2, x1, z1, ctx)) goto err; + if (!BN_GF2m_add(z1, z1, x1)) goto err; + if (!group->meth->field_sqr(group, z1, z1, ctx)) goto err; + if (!group->meth->field_mul(group, x1, z1, t1, ctx)) goto err; + if (!BN_GF2m_add(x1, x1, t2)) goto err; + + ret = 1; + + err: + BN_CTX_end(ctx); + return ret; + } + +int benchmark_EC2() { + const int cntTest = 1000; + + int i; + struct timeval ts, te; + long td; + + BIGNUM* bnsrc1[cntTest]; + BIGNUM* bnsrc2[cntTest]; + BIGNUM* bndst; + mm_256 mmsrc1[cntTest]; + mm_256 mmsrc2[cntTest]; + mm_256 mmdst; + + ec_point_t epsrc[cntTest], epdst; + BIGNUM *bnk[cntTest]; + mm256_point_t mpsrc[cntTest], mpdst; + mm_256 mk[cntTest]; + + int r; + int nid; + + EC_KEY *key; + const BIGNUM* rkey; + const EC_GROUP* group; + const EC_POINT* ukey; + const EC_POINT* G; + EC_POINT* br; + + ctx = BN_CTX_new(); + + nid = OBJ_sn2nid(SN_sect163k1); + + // generate the key + key = EC_KEY_new_by_curve_name(nid); + assert(key != NULL); + r = EC_KEY_generate_key(key); + assert(r == 1); + + group = EC_KEY_get0_group(key); + // get generator + G = EC_GROUP_get0_generator(group); + // get private key + rkey = EC_KEY_get0_private_key(key); + ukey = EC_KEY_get0_public_key(key); + br = EC_POINT_new(group); + + // 1. generate $cntTest test cases + bndst = BN_new(); + ec_point_init(&epdst); + for(i = 0; i < cntTest; i++){ + bnsrc1[i] = BN_new(); + bnsrc2[i] = BN_new(); + assert(BN_rand_range(bnsrc1[i], n)); + assert(BN_rand_range(bnsrc2[i], n)); + bn_to_mm256(bnsrc1[i], &mmsrc1[i]); + bn_to_mm256(bnsrc2[i], &mmsrc2[i]); + + bnk[i] = BN_new(); + ec_point_init(&epsrc[i]); + assert(BN_rand_range(epsrc[i].X, n)); + assert(BN_rand_range(epsrc[i].Y, n)); + assert(BN_rand_range(epsrc[i].Z, n)); + assert(BN_rand_range(bnk[i], n)); + + bn_point_to_mm_point(&epsrc[i], mpsrc + i); + bn_to_mm256(bnk[i], mk + i); + } + // do addition / multiplication / square for $cntTest times + gettimeofday(&ts, NULL); + for(i = 0; i < cntTest; i++){ + BN_GF2m_add(bndst, bnsrc1[i], bnsrc2[i]); + } + gettimeofday(&te, NULL); + td = cntUS * (te.tv_sec - ts.tv_sec) + (te.tv_usec - ts.tv_usec); + printf("bignum addition: "); + printf("%d cases, %lfs used, %lfus for each cases\n", cntTest, (double)td / cntUS, (double)td / cntTest); + + gettimeofday(&ts, NULL); + for(i = 0; i < cntTest; i++){ + gf2_add(&mmsrc1[i], &mmsrc2[i], &mmdst); + } + gettimeofday(&te, NULL); + td = cntUS * (te.tv_sec - ts.tv_sec) + (te.tv_usec - ts.tv_usec); + printf("secure addition: "); + printf("%d cases, %lfs used, %lfus for each cases\n", cntTest, (double)td / cntUS, (double)td / cntTest); + + gettimeofday(&ts, NULL); + for(i = 0; i < cntTest; i++){ + BN_GF2m_mod_mul_arr(bndst, bnsrc1[i], bnsrc2[i], group->poly, ctx); + } + gettimeofday(&te, NULL); + td = cntUS * (te.tv_sec - ts.tv_sec) + (te.tv_usec - ts.tv_usec); + printf("bignum multiplication: "); + printf("%d cases, %lfs used, %lfus for each cases\n", cntTest, (double)td / cntUS, (double)td / cntTest); + + gettimeofday(&ts, NULL); + for(i = 0; i < cntTest; i++){ + gf2_mod_mul(&mmsrc1[i], &mmsrc2[i], &mmdst); + } + gettimeofday(&te, NULL); + td = cntUS * (te.tv_sec - ts.tv_sec) + (te.tv_usec - ts.tv_usec); + printf("secure multiplication: "); + printf("%d cases, %lfs used, %lfus for each cases\n", cntTest, (double)td / cntUS, (double)td / cntTest); + + mm_256 mmt; + gettimeofday(&ts, NULL); + for(i = 0; i < cntTest; i++){ + gf2_mul(&mmsrc1[i], &mmsrc2[i], &mmdst, &mmt); + } + gettimeofday(&te, NULL); + td = cntUS * (te.tv_sec - ts.tv_sec) + (te.tv_usec - ts.tv_usec); + printf("secure multiplication only: "); + printf("%d cases, %lfs used, %lfus for each cases\n", cntTest, (double)td / cntUS, (double)td / cntTest); + + gettimeofday(&ts, NULL); + for(i = 0; i < cntTest; i++){ + BN_GF2m_mod_sqr_arr(bndst, bnsrc1[i], group->poly, ctx); + } + gettimeofday(&te, NULL); + td = cntUS * (te.tv_sec - ts.tv_sec) + (te.tv_usec - ts.tv_usec); + printf("bignum square: "); + printf("%d cases, %lfs used, %lfus for each cases\n", cntTest, (double)td / cntUS, (double)td / cntTest); + + gettimeofday(&ts, NULL); + for(i = 0; i < cntTest; i++){ + gf2_mod_sqr(&mmsrc1[i], &mmdst); + //gf2_sqr(&mmsrc1[i], &mmdst, &t); + } + gettimeofday(&te, NULL); + td = cntUS * (te.tv_sec - ts.tv_sec) + (te.tv_usec - ts.tv_usec); + printf("secure squre: "); + printf("%d cases, %lfs used, %lfus for each cases\n", cntTest, (double)td / cntUS, (double)td / cntTest); + + gettimeofday(&ts, NULL); + for(i = 0; i < cntTest; i++){ + BN_GF2m_mod_inv(bndst, bnsrc1[i], p, ctx); + } + gettimeofday(&te, NULL); + td = cntUS * (te.tv_sec - ts.tv_sec) + (te.tv_usec - ts.tv_usec); + printf("bignum inv: "); + printf("%d cases, %lfs used, %lfus for each cases\n", cntTest, (double)td / cntUS, (double)td / cntTest); + + gettimeofday(&ts, NULL); + for(i = 0; i < cntTest; i++){ + gf2m_inv(&mmsrc1[i], &mmdst); + /* gf2_mod_sqr(&mmsrc1[i], &mmdst); */ + //gf2_sqr(&mmsrc1[i], &mmdst, &t); + } + gettimeofday(&te, NULL); + td = cntUS * (te.tv_sec - ts.tv_sec) + (te.tv_usec - ts.tv_usec); + printf("secure inv: "); + printf("%d cases, %lfs used, %lfus for each cases\n", cntTest, (double)td / cntUS, (double)td / cntTest); + + gettimeofday(&ts, NULL); + for(i = 0; i < cntTest; i++){ + gf2m_inv_asm(&mmsrc1[i], &mmdst); + /* gf2_mod_sqr(&mmsrc1[i], &mmdst); */ + //gf2_sqr(&mmsrc1[i], &mmdst, &t); + } + gettimeofday(&te, NULL); + td = cntUS * (te.tv_sec - ts.tv_sec) + (te.tv_usec - ts.tv_usec); + printf("secure inv asm: "); + printf("%d cases, %lfs used, %lfus for each cases\n", cntTest, (double)td / cntUS, (double)td / cntTest); + + BIGNUM* x1, *z1, * x2, *z2; + x1 = BN_CTX_get(ctx); + z1 = BN_CTX_get(ctx); + x2 = BN_CTX_get(ctx); + z2 = BN_CTX_get(ctx); + + BN_rand(x1, EC_GROUP_get_degree(group), 0, 1); + BN_rand(z1, EC_GROUP_get_degree(group), 0, 1); + BN_rand(x2, EC_GROUP_get_degree(group), 0, 1); + BN_rand(z2, EC_GROUP_get_degree(group), 0, 1); + + gettimeofday(&ts, NULL); + // openssl point multiplication + for(i = 0; i < cntTest; i++){ + gf2m_Mdouble(group, x1, z1, ctx); + } + gettimeofday(&te, NULL); + td = cntUS * (te.tv_sec - ts.tv_sec) + (te.tv_usec - ts.tv_usec); + printf("openssl mont point dbl: "); + printf("%d cases, %lfs used, %lfus for each cases\n", cntTest, (double)td / cntUS, (double)td / cntTest); + + gettimeofday(&ts, NULL); + // openssl point multiplication + for(i = 0; i < cntTest; i++){ + gf2m_Madd(group, rkey, x1, z1, x2, z2, ctx); + } + gettimeofday(&te, NULL); + td = cntUS * (te.tv_sec - ts.tv_sec) + (te.tv_usec - ts.tv_usec); + printf("openssl mont point add: "); + printf("%d cases, %lfs used, %lfus for each cases\n", cntTest, (double)td / cntUS, (double)td / cntTest); + + gettimeofday(&ts, NULL); + // openssl point multiplication + for(i = 0; i < cntTest; i++){ + EC_POINT_add(group, br, G, ukey, ctx); + } + gettimeofday(&te, NULL); + td = cntUS * (te.tv_sec - ts.tv_sec) + (te.tv_usec - ts.tv_usec); + printf("openssl point add: "); + printf("%d cases, %lfs used, %lfus for each cases\n", cntTest, (double)td / cntUS, (double)td / cntTest); + + gettimeofday(&ts, NULL); + for(i = 0; i < cntTest; i++){ + gf2_point_add(mpsrc + i, mpsrc + (i + 1) % cntTest, &mpdst, 1, 1); + } + gettimeofday(&te, NULL); + td = cntUS * (te.tv_sec - ts.tv_sec) + (te.tv_usec - ts.tv_usec); + printf("secure point addition: "); + printf("%d cases, %lfs used, %lfus for each cases\n", cntTest, (double)td / cntUS, (double)td / cntTest); + + gettimeofday(&ts, NULL); + // openssl point multiplication + for(i = 0; i < cntTest; i++){ + EC_POINT_dbl(group, br, G, ctx); + } + gettimeofday(&te, NULL); + td = cntUS * (te.tv_sec - ts.tv_sec) + (te.tv_usec - ts.tv_usec); + printf("openssl point doubling: "); + printf("%d cases, %lfs used, %lfus for each cases\n", cntTest, (double)td / cntUS, (double)td / cntTest); + + gettimeofday(&ts, NULL); + for(i = 0; i < cntTest; i++){ + gf2_point_dbl(mpsrc + i, &mpdst, 1, 1); + } + gettimeofday(&te, NULL); + td = cntUS * (te.tv_sec - ts.tv_sec) + (te.tv_usec - ts.tv_usec); + printf("secure point doubling: "); + printf("%d cases, %lfs used, %lfus for each cases\n", cntTest, (double)td / cntUS, (double)td / cntTest); + + + gettimeofday(&ts, NULL); + // openssl point multiplication + for(i = 0; i < cntTest; i++){ + EC_POINT_mul(group, br, NULL, G, rkey, ctx); + } + gettimeofday(&te, NULL); + td = cntUS * (te.tv_sec - ts.tv_sec) + (te.tv_usec - ts.tv_usec); + printf("openssl point mul: "); + printf("%d cases, %lfs used, %lfus for each cases\n", cntTest, (double)td / cntUS, (double)td / cntTest); + + + gettimeofday(&ts, NULL); + for(i = 0; i < cntTest; i++){ + gf2_point_mul(mpsrc + i, mk + i, &mpdst, 1, 1); + //gf2_point_dbl(mp + i, mr + i, 1, 1); + } + gettimeofday(&te, NULL); + td = cntUS * (te.tv_sec - ts.tv_sec) + (te.tv_usec - ts.tv_usec); + printf("secure point multiplication: "); + printf("%d cases, %lfs used, %lfus for each cases\n", cntTest, (double)td / cntUS, (double)td / cntTest); + + + ec2m_kern_init(); + ec2m_import_key(&mk[0]); + + gettimeofday(&ts, NULL); + + for(i = 0; i < cntTest; i++){ + ec2m_private_operation(mpsrc + i, &mpdst); + } + gettimeofday(&te, NULL); + td = cntUS * (te.tv_sec - ts.tv_sec) + (te.tv_usec - ts.tv_usec); + printf("kernel point multiplication: "); + printf("%d cases, %lfs used, %lfus for each cases\n", cntTest, (double)td / cntUS, (double)td / cntTest); + ec2m_kern_clean(); + + return 0; +} + +int testPointArithmetic(){ + int passed = 0, failed = 0; + BIGNUM* K; + + ec_point_t P, Q, R, T; + mm256_point_t mp, mq, mr; + mm_256 mk; + domain_parameters_print(); + + ec_point_init(&P); + ec_point_init(&Q); + ec_point_init(&R); + ec_point_init(&T); + + // point double + // special cases + // P = infinity + BN_set_word(P.X, 1); + bn_point_to_mm_point(&P, &mp); + ec_point_double(&R, &P); + gf2_point_dbl(&mp, &mr, 1, 1); + + print_bn_point(&P); + printf(" * 2 = "); + print_bn_point(&R); + if(cmp_mm_point_with_bn_point(&mr, &R) == 0){ + passed++; + printf(" ... passed!\n"); + } else { + failed++; + printf(" ... failed! got "); + print_mm_point(&mr); + printf("\n"); + } + + // a general case + ec_point_set_affine_xy(&P, x, y); + //BN_rand_range(P.Z, n); + bn_point_to_mm_point(&P, &mp); + + ec_point_double(&R, &P); + + gf2_point_dbl(&mp, &mr, 1, 1); + + print_bn_point(&P); + printf(" * 2 = "); + print_bn_point(&R); + if(cmp_mm_point_with_bn_point(&mr, &R) == 0){ + passed++; + printf(" ... passed!\n"); + } else { + failed++; + printf(" ... failed! got "); + print_mm_point(&mr); + printf("\n"); + } + // double again + ec_point_copy(&P, &R); + bn_point_to_mm_point(&P, &mp); + + ec_point_double(&R, &P); + + gf2_point_dbl(&mp, &mr, 1, 1); + + print_bn_point(&P); + printf(" * 2 = "); + print_bn_point(&R); + if(cmp_mm_point_with_bn_point(&mr, &R) == 0){ + passed++; + printf(" ... passed!\n"); + } else { + failed++; + printf(" ... failed! got "); + print_mm_point(&mr); + printf("\n"); + } + + // point add + + ec_point_copy(&T, &R); + // special cases + // P = infinity + BN_set_word(P.X, 1); + BN_set_word(P.Y, 0); + BN_set_word(P.Z, 0); + ec_point_copy(&Q, &T); + BN_set_word(Q.Z, 1); + bn_point_to_mm_point(&P, &mp); + bn_point_to_mm_point(&Q, &mq); + ec_point_add(&R, &P, &Q); + gf2_point_add(&mp, &mq, &mr, 1, 1); + + print_bn_point(&P); + printf(" + "); + print_bn_point(&Q); + printf(" = "); + print_bn_point(&R); + if(cmp_mm_point_with_bn_point(&mr, &R) == 0){ + passed++; + printf(" ... passed!\n"); + } else { + failed++; + printf(" ... failed! got "); + print_mm_point(&mr); + printf("\n"); + } + + // a general case + ec_point_copy(&P, &T); + ec_point_set_affine_xy(&Q, x, y); + bn_point_to_mm_point(&P, &mp); + bn_point_to_mm_point(&Q, &mq); + ec_point_add(&R, &P, &Q); + gf2_point_add(&mp, &mq, &mr, BN_get_word(a), BN_get_word(b)); + + print_bn_point(&P); + printf(" + "); + print_affine_bn_point(&Q); + printf(" = "); + print_bn_point(&R); + if(cmp_mm_point_with_bn_point(&mr, &R) == 0){ + passed++; + printf(" ... passed!\n"); + } else { + failed++; + printf(" ... failed! got "); + print_mm_point(&mr); + printf("\n"); + } + + // point multiply + ec_point_set_affine_xy(&P, x, y); + bn_point_to_mm_point(&P, &mp); + K = BN_new(); + BN_rand_range(K, n); + bn_to_mm256(K, &mk); + + ec_point_multiply(&R, &P, K); + + gf2_point_mul(&mp, &mk, &mr, BN_get_word(a), BN_get_word(b)); + + print_bn_point(&P); + printf(" * "); + printf("%s", BN_bn2str(K)); + printf(" = "); + print_bn_point(&R); + if(cmp_mm_point_with_bn_point(&mr, &R) == 0){ + passed++; + printf(" ... passed!\n"); + } else { + failed++; + printf(" ... failed! got "); + print_mm_point(&mr); + printf("\n"); + } + + // point multiply with key preset + __asm__ __volatile__ ("vmovdqu %0, %%ymm15" : : "m"(mk)); + gf2_point_mul_with_preset_key(&mp, &mr, BN_get_word(a), BN_get_word(b)); + + print_bn_point(&P); + printf(" * "); + printf("%s", BN_bn2str(K)); + printf(" = "); + print_bn_point(&R); + if(cmp_mm_point_with_bn_point(&mr, &R) == 0){ + passed++; + printf(" ... passed!\n"); + } else { + failed++; + printf(" ... failed! got "); + print_mm_point(&mr); + printf("\n"); + } + + ec2m_kern_init(); + + ec2m_import_key(&mk); + ec2m_private_operation(&mp, &mr); + print_bn_point(&P); + printf(" * "); + printf("%s", BN_bn2str(K)); + printf(" = "); + print_bn_point(&R); + if(cmp_mm_point_with_bn_point(&mr, &R) == 0){ + passed++; + printf(" ... passed!\n"); + } else { + failed++; + printf(" ... failed! got "); + print_mm_point(&mr); + printf("\n"); + } + ec2m_kern_clean(); + + printf("%d/%d test(s) passed.\n", passed, (passed + failed)); + + return failed; +} + +int testKernelEc2m() { + int r; + int rid; + ec_point_t P, R; + mm256_point_t mp, mr; + BIGNUM* tk = BN_new(); + mm_256 mk; + BN_rand_range(tk, n); + bn_to_mm256(tk, &mk); + + ec_point_init(&P); + ec_point_init(&R); + ec_point_set_affine_xy(&P, x, y); + bn_point_to_mm_point(&P, &mp); + + printf("alloc ec2m resource... "); + rid = sys_ec2m_alloc(); + printf(" got %d\n", rid); + if(rid < 0) + return 1; + + r = sys_ec2m_setkey(rid, &mk, BN_get_word(a), BN_get_word(b)); + printf("setkey: %d\n", r); + + printf("encrypt: %d\n", r); + // point multiply + ec_point_set_affine_xy(&P, x, y); + ec_point_multiply(&R, &P, tk); + + sys_ec2m_encrypt(rid, &mp, &mr); + + print_bn_point(&P); + printf(" * "); + printf("%s", BN_bn2str(tk)); + printf(" = "); + print_bn_point(&R); + if(cmp_mm_point_with_bn_point(&mr, &R) == 0){ + printf(" ... passed!\n"); + } else { + printf(" ... failed! got "); + print_mm_point(&mr); + printf("\n"); + } + + r = sys_ec2m_free(rid); + printf("free: %d\n", r); + return 0; +} + +int testMisc(){ + int passed = 0, failed = 0; + BIGNUM* K; + + ec_point_t P, Q, R, T; + mm256_point_t mp, mr; + mm_256 mk; + domain_parameters_print(); + + ec_point_init(&P); + ec_point_init(&Q); + ec_point_init(&R); + ec_point_init(&T); + + /* + // point double + // a general case + ec_point_set_affine_xy(&P, x, y); + BN_rand_range(P.Z, n); + BN_set_word(P.Z, 4); + bn_point_to_mm_point(&P, &mp); + + ec_point_double(&R, &P); + + gf2_point_dbl(&mp, &mr, 1, 1); + + print_bn_point(&P); + printf(" * 2 = "); + print_bn_point(&R); + if(cmp_mm_point_with_bn_point(&mr, &R) == 0){ + passed++; + printf(" ... passed!\n"); + } else { + failed++; + printf(" ... failed! got "); + print_mm_point(&mr); + printf("\n"); + } + + // point add + ec_point_copy(&T, &R); + // special cases + // P = infinity + BN_set_word(P.X, 1); + BN_set_word(P.Y, 0); + BN_set_word(P.Z, 0); + ec_point_copy(&Q, &T); + BN_set_word(Q.Z, 1); + bn_point_to_mm_point(&P, &mp); + bn_point_to_mm_point(&Q, &mq); + ec_point_add(&R, &P, &Q); + gf2_point_add(&mp, &mq, &mr, 1, 1); + + print_bn_point(&P); + printf(" + "); + print_bn_point(&Q); + printf(" = "); + print_bn_point(&R); + if(cmp_mm_point_with_bn_point(&mr, &R) == 0){ + passed++; + printf(" ... passed!\n"); + } else { + failed++; + printf(" ... failed! got "); + print_mm_point(&mr); + printf("\n"); + } + */ + + // point multiply + ec_point_set_affine_xy(&P, x, y); + bn_point_to_mm_point(&P, &mp); + K = BN_new(); + BN_rand_range(K, n); + //K->d[0] = 3; + //K->d[2] = 2; + bn_to_mm256(K, &mk); + + ec_point_multiply(&R, &P, K); + + gf2_point_mul(&mp, &mk, &mr, BN_get_word(a), BN_get_word(b)); + + print_bn_point(&P); + printf(" * "); + printf("%s", BN_bn2str(K)); + printf(" = "); + print_bn_point(&R); + if(cmp_mm_point_with_bn_point(&mr, &R) == 0){ + passed++; + printf(" ... passed!\n"); + } else { + failed++; + printf(" ... failed! got "); + print_mm_point(&mr); + printf("\n"); + } + + return failed; +} + +int benchmark_cycles(){ + mm_256 ma, mb, mr; + const int cases = 1000; + unsigned long hi_s, lo_s, hi_e, lo_e, s, e; + unsigned long td[cases]; + unsigned long t_base, t_min, t_sum, t_avg; + int i; + const char* item; + + // calculate the bases + for(i = 0; i < cases; i++){ + rdtsc_begin(hi_s, lo_s); + rdtsc_end(hi_e, lo_e); + s = (hi_s << 32) | lo_s; + e = (hi_e << 32) | lo_e; + td[i] = e - s; + } + + item = "base"; + t_min = td[0]; + t_sum = 0; + for(i = 0; i < cases; i++){ + if(t_min > td[i]){ + t_min = td[i]; + } + t_sum += td[i]; + } + t_avg = t_sum / cases; + t_base = t_min; +#ifdef KERN + printk(KERN_INFO"base: %lu\n", t_min); +#else + printf("%s: %lu, %lu, %lu\n", item, t_min, t_sum, t_avg); +#endif + + gf2_add(&ma, &mb, &mr); + for(i = 0; i < cases; i++){ + rdtsc_begin(hi_s, lo_s); + gf2_add(&ma, &mb, &mr); + rdtsc_end(hi_e, lo_e); + s = (hi_s << 32) | lo_s; + e = (hi_e << 32) | lo_e; + td[i] = e - s; + } + + item = "add"; + t_sum = 0; + t_min = td[0]; + for(i = 0; i < cases; i++){ + if(t_min > td[i]){ + t_min = td[i]; + } + t_sum += td[i]; + } + t_avg = t_sum / cases; +#ifdef KERN + printk(KERN_INFO "add: %lu, %lu\n", t_min, t_min - t_base); +#else + printf("%s: %lu, %lu, %lu, %lu\n", item, t_min, t_min - t_base, t_sum, t_avg); +#endif + + for(i = 0; i < cases; i++){ + rdtsc_begin(hi_s, lo_s); + gf2_mod_mul(&ma, &mb, &mr); + rdtsc_end(hi_e, lo_e); + s = (hi_s << 32) | lo_s; + e = (hi_e << 32) | lo_e; + td[i] = e - s; + } + + item = "mul"; + t_sum = 0; + t_min = td[0]; + for(i = 0; i < cases; i++){ + if(t_min > td[i]){ + t_min = td[i]; + } + t_sum += td[i]; + } + t_avg = t_sum / cases; +#ifdef KERN + printk(KERN_INFO "add: %lu, %lu\n", t_min, t_min - t_base); +#else + printf("%s: %lu, %lu, %lu, %lu\n", item, t_min, t_min - t_base, t_sum, t_avg); +#endif + + for(i = 0; i < cases; i++){ + rdtsc_begin(hi_s, lo_s); + gf2_mod_sqr(&ma, &mr); + rdtsc_end(hi_e, lo_e); + s = (hi_s << 32) | lo_s; + e = (hi_e << 32) | lo_e; + td[i] = e - s; + } + item = "sqr"; + t_sum = 0; + t_min = td[0]; + for(i = 0; i < cases; i++){ + if(t_min > td[i]){ + t_min = td[i]; + } + t_sum += td[i]; + } + t_avg = t_sum / cases; +#ifdef KERN + printk(KERN_INFO "add: %lu, %lu\n", t_min, t_min - t_base); +#else + printf("%s: %lu, %lu, %lu, %lu\n", item, t_min, t_min - t_base, t_sum, t_avg); +#endif + return 0; +} diff --git a/engines/zeromem/test.h b/engines/zeromem/test.h new file mode 100755 index 00000000..373f5cf0 --- /dev/null +++ b/engines/zeromem/test.h @@ -0,0 +1,17 @@ +#ifndef _TEST_H_ +#define _TEST_H_ + + +extern int initDomainParameters(int argc, char** argv); +extern int testFieldArithmetic(); +extern int testPointArithmetic(); +extern int testAES(); +extern int testKernelEc2m(); +extern int testMisc(); +extern int testEC2M(); +extern int benchmark_cycles(); + +extern int benchmark_EC2(); + + +#endif diff --git a/engines/zeromem/util.c b/engines/zeromem/util.c new file mode 100755 index 00000000..7bc5098d --- /dev/null +++ b/engines/zeromem/util.c @@ -0,0 +1,215 @@ +#include +#include +#include +#include +#include +#include "util.h" + +unsigned int sqr_table[1 << 16]; + +void print_mm_256(mm_256* m){ + /* printf("(%lu, %lu, %lu, %lu)", m->iv[3], m->iv[2], m->iv[1], m->iv[0]); */ + BIGNUM *bn = BN_new(); + mm256_to_bn(m, bn); + printf("%s", BN_bn2hex(bn)); +} + +void init_sqr_table(){ + unsigned int i, j; + unsigned int t; + unsigned int n; + for(i = 0; i < sizeof(sqr_table) / sizeof(sqr_table[0]); i++){ + t = 0; + j = i; + n = 16; + while(n-- > 0){ + t = t << 2; + t |= ((j >> n) & 0x1); + } + sqr_table[i] = t; + } +} + +void ec_point_init(ec_point_t *P) { + P->X = BN_new(); + P->Y = BN_new(); + P->Z = BN_new(); +} + +void ec_point_free(ec_point_t *P){ + OPENSSL_free(P->X); + OPENSSL_free(P->Y); + OPENSSL_free(P->Z); +} + +void bn_to_mm256(const BIGNUM* bn, mm_256* m){ + memset(m, 0, sizeof(mm_256)); + assert(bn->top <= 4); + int i; + + for(i = 0; i < bn->top; i++){ + m->iv[i] = bn->d[i]; + } +} + +void mm256_to_bn(const mm_256* m, BIGNUM* bn){ + BN_zero(bn); + int i = 4; + while(i-- > 0){ + BN_lshift(bn, bn, 64); + BN_add_word(bn, m->iv[i]); + } +} + +void bn_point_to_mm_point(const ec_point_t* src, mm256_point_t* dst){ + bn_to_mm256(src->X, &dst->x); + bn_to_mm256(src->Y, &dst->y); + bn_to_mm256(src->Z, &dst->z); +} + +void EC_POINT_to_mm_point(const ec_point_st* src, mm256_point_t* dst) +{ + bn_to_mm256(&src->X, &dst->x); + bn_to_mm256(&src->Y, &dst->y); + bn_to_mm256(&src->Z, &dst->z); +} + +void mm_point_to_EC_POINT(const mm256_point_t* src, ec_point_st* dst) +{ + mm256_to_bn(&(src->x), &dst->X); + mm256_to_bn(&(src->y), &dst->Y); + mm256_to_bn(&(src->z), &dst->Z); +} + +void mm_point_to_bn_point(const mm256_point_t* src, ec_point_t* dst){ + mm256_to_bn(&(src->x), dst->X); + mm256_to_bn(&(src->y), dst->Y); + mm256_to_bn(&(src->z), dst->Z); +} + +int cmp_mm_256_with_bn(mm_256* a, BIGNUM* bn){ + mm_256 b; + bn_to_mm256(bn, &b); + return memcmp(a, &b, sizeof(mm_256)); +} + +int cmp_mm_point_with_bn_point(mm256_point_t* a, ec_point_t* b){ + mm256_point_t t; + bn_point_to_mm_point(b, &t); + return memcmp(a, &t, sizeof(mm256_point_t)); +} + +void print_bn_point(ec_point_t* p){ + char *px, *py, *pz; + px = BN_bn2str(p->X); + py = BN_bn2str(p->Y); + pz = BN_bn2str(p->Z); + printf("(%s: %s: %s)", px, py, pz); + OPENSSL_free(px); + OPENSSL_free(py); + OPENSSL_free(pz); +} + +void print_EC_POINT(ec_point_st*p) +{ + char *px, *py, *pz; + + if(p->X.d) + px = BN_bn2str(&p->X); + else + px = ""; + if(p->Y.d) + py = BN_bn2str(&p->Y); + else + py = ""; + if(p->Z.d) + pz = BN_bn2str(&p->Z); + else + pz = ""; + + printf("(%s: %s: %s)", px, py, pz); + + if(p->X.d) + OPENSSL_free(px); + if(p->Y.d) + OPENSSL_free(py); + if(p->Z.d) + OPENSSL_free(pz); +} + +void print_mm_point(mm256_point_t* p){ + ec_point_t t; + ec_point_init(&t); + mm_point_to_bn_point(p, &t); + print_bn_point(&t); + ec_point_free(&t); +} + +void print_affine_bn_point(ec_point_t* p){ + char *px, *py; + px = BN_bn2str(p->X); + py = BN_bn2str(p->Y); + printf("(%s, %s)", px, py); + OPENSSL_free(px); + OPENSSL_free(py); +} + +void print_affine_mm_point(mm256_point_t* p){ + ec_point_t t; + ec_point_init(&t); + mm_point_to_bn_point(p, &t); + print_affine_bn_point(&t); + ec_point_free(&t); +} + +void printHex(uint8_t* str, uint32_t len){ + uint32_t i; + for(i = 0; i < len; i++){ + printf("%02x", str[i]); + } +} + +void save_ymm_group(uint8_t* buf){ + __asm__("vmovdqu %ymm0, (%rdi)\n\t" + "vmovdqu %ymm1, 32(%rdi)\n\t" + "vmovdqu %ymm2, 64(%rdi)\n\t" + "vmovdqu %ymm3, 96(%rdi)\n\t" + "vmovdqu %ymm4, 128(%rdi)\n\t" + "vmovdqu %ymm5, 160(%rdi)\n\t" + "vmovdqu %ymm6, 192(%rdi)\n\t" + "vmovdqu %ymm7, 224(%rdi)\n\t" + "vmovdqu %ymm8, 256(%rdi)\n\t" + "vmovdqu %ymm9, 288(%rdi)\n\t" + "vmovdqu %ymm10, 320(%rdi)\n\t" + "vmovdqu %ymm11, 352(%rdi)\n\t" + "vmovdqu %ymm12, 384(%rdi)\n\t" + "vmovdqu %ymm13, 416(%rdi)\n\t" + "vmovdqu %ymm14, 448(%rdi)\n\t" + "vmovdqu %ymm15, 480(%rdi)\n\t" + ); + __asm__("" ::: "memory"); +} + +void load_ymm_group(uint8_t* buf){ + __asm__("vmovdqu (%rdi), %ymm0\n\t" + "vmovdqu 32(%rdi), %ymm1\n\t" + "vmovdqu 64(%rdi), %ymm2\n\t" + "vmovdqu 96(%rdi), %ymm3\n\t" + "vmovdqu 128(%rdi), %ymm4\n\t" + "vmovdqu 160(%rdi), %ymm5\n\t" + "vmovdqu 192(%rdi), %ymm6\n\t" + "vmovdqu 224(%rdi), %ymm7\n\t" + "vmovdqu 256(%rdi), %ymm8\n\t" + "vmovdqu 288(%rdi), %ymm9\n\t" + "vmovdqu 320(%rdi), %ymm10\n\t" + "vmovdqu 352(%rdi), %ymm11\n\t" + "vmovdqu 384(%rdi), %ymm12\n\t" + "vmovdqu 416(%rdi), %ymm13\n\t" + "vmovdqu 448(%rdi), %ymm14\n\t" + "vmovdqu 480(%rdi), %ymm15\n\t" + ); + __asm__("" ::: "memory"); +} + +void dummy_print(const char* format, ...){ +} diff --git a/engines/zeromem/util.h b/engines/zeromem/util.h new file mode 100755 index 00000000..a54b72eb --- /dev/null +++ b/engines/zeromem/util.h @@ -0,0 +1,82 @@ +#ifndef _UTIL_H_ +#define _UTIL_H_ + +#include "ec.h" +#include + +//#define DEBUG + +//#define PRINT_DEC +#ifdef PRINT_DEC +#define BN_bn2str(bn) BN_bn2dec(bn) +#else +#define BN_bn2str(bn) BN_bn2hex(bn) +#endif + +extern void dummy_print(const char* format, ...); + +#ifdef DEBUG +#define debug printf +#else +#define debug dummy_print +#endif + +extern unsigned int sqr_table[1 << 16]; + +#define rdtsc_begin(hi, lo)\ + asm volatile ("CPUID\n\t"\ + "RDTSCP\n\t"\ + "movq %%rdx, %0\n\t"\ + "movq %%rax, %1\n\t" : "=r" (hi), "=r" (lo) :: "%rax", "%rbx", "%rcx", "%rdx"); + +#define rdtsc_end(hi, lo)\ + asm volatile ("RDTSCP\n\t"\ + "movq %%rdx, %0\n\t"\ + "movq %%rax, %1\n\t"\ + "CPUID\n\t" : "=r" (hi), "=r" (lo) :: "%rax", "%rbx", "%rcx", "%rdx"); + +typedef struct { + BIGNUM *X; + BIGNUM *Y; + BIGNUM *Z; +} ec_point_t; + +typedef struct{ + const struct EC_METHOD *meth; + + /* All members except 'meth' are handled by the method functions, + * even if they appear generic */ + + BIGNUM X; + BIGNUM Y; + BIGNUM Z; /* Jacobian projective coordinates: + * (X, Y, Z) represents (X/Z^2, Y/Z^3) if Z != 0 */ + int Z_is_one; /* enable optimized point arithmetics for special case */ +} ec_point_st /* EC_POINT */; + +extern void init_sqr_table(); +extern void bn_to_mm256(const BIGNUM* bn, mm_256 *m); +extern void mm256_to_bn(const mm_256 *m, BIGNUM* bn); +extern void bn_point_to_mm_point(const ec_point_t* src, mm256_point_t* dst); +extern void mm_point_to_bn_point(const mm256_point_t* src, ec_point_t* dst); +extern void EC_POINT_to_mm_point(const ec_point_st* src, mm256_point_t* dst); +extern void mm_point_to_EC_POINT(const mm256_point_t* src, ec_point_st* dst); + +extern void ec_point_init(ec_point_t *P); +extern void ec_point_free(ec_point_t *P); + +extern int cmp_mm_256_with_bn(mm_256* a, BIGNUM* bn); +extern int cmp_mm_point_with_bn_point(mm256_point_t* a, ec_point_t* b); + +extern void print_affine_bn_point(ec_point_t* p); +extern void print_affine_mm_point(mm256_point_t* p); +extern void print_bn_point(ec_point_t* p); +extern void print_mm_point(mm256_point_t* p); +extern void print_EC_POINT(ec_point_st* p); + +extern void printHex(uint8_t* str, uint32_t len); + +extern void save_ymm_group(uint8_t* buf); +extern void load_ymm_group(uint8_t* buf); + +#endif