diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/.keep" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/.keep" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/include/.keep" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/include/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/include/include/common_utils.h" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/include/include/common_utils.h" new file mode 100644 index 0000000000000000000000000000000000000000..9251edce48cf8aeb25404fcf5391335698a83a06 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/include/include/common_utils.h" @@ -0,0 +1,69 @@ +#ifndef SECGEAR_SM_COMMON_UTILS +#define SECGEAR_SM_COMMON_UTILS + +#include +#define debugPrint (printf("%s: %s: %d\n",__FILE__,__FUNCTION__,__LINE__),fflush(stdout),1) + +typedef __uint8_t uint8; +typedef __uint16_t uint16; +typedef __uint32_t uint32; +typedef __uint64_t uint64; + +#define CONCAT_8x4(x0, x1, x2, x3) ((((uint32)(x0) << 8 | (x1)) << 8 | (x2)) << 8 | (x3)) // X = (x0 || x1 || x2 || x3) +#define CONCAT_16x2(x0, x1) ((uint32)(x0) << 16 | (x1)) // X = (x0 || x1) +#define EXTRACT_8_32(X, i) (((X) >> (((i) ^ 3) << 3)) & 0xFF) // X = (x0 || x1 || x2 || x3) +#define EXTRACT_16_32(X, i) (((X) >> (((i) ^ 1) << 4)) & 0xFFFF) // X = (x0 || x1) +#define EXTRACT_16H_31(X) (((X) >> 15) & 0xFFFF) // Higher 16-bit from 31-bit +#define EXTRACT_16L_31(X) ((X) & 0xFFFF) // Lower 16-bit from 31-bit + +#define ROTATE(a, n) (((a) << (n)) | (((a) & 0xFFFFFFFF) >> ((32 - (n)) & 31))) +#define ROTATE_31(a, n) ((((a) << (n)) & 0x7FFFFFFF) | (((a) & 0x7FFFFFFF) >> (31 - (n)))) + +#define GET_BIT_VALUE_32(x, i) (((x) >> (31 - (i))) & 1) + +const uint32 PREFIX_MASK_32[32] = { + 0x00000000, + 0x80000000, + 0xC0000000, + 0xE0000000, + 0xF0000000, + 0xF8000000, + 0xFC000000, + 0xFE000000, + 0xFF000000, + 0xFF800000, + 0xFFC00000, + 0xFFE00000, + 0xFFF00000, + 0xFFF80000, + 0xFFFC0000, + 0xFFFE0000, + 0xFFFF0000, + 0xFFFF8000, + 0xFFFFC000, + 0xFFFFE000, + 0xFFFFF000, + 0xFFFFF800, + 0xFFFFFC00, + 0xFFFFFE00, + 0xFFFFFF00, + 0xFFFFFF80, + 0xFFFFFFC0, + 0xFFFFFFE0, + 0xFFFFFFF0, + 0xFFFFFFF8, + 0xFFFFFFFC, + 0xFFFFFFFE +}; + +# define HOST_c2l(c,l) (l =(((unsigned long)(*((c)++)))<<24), \ + l|=(((unsigned long)(*((c)++)))<<16), \ + l|=(((unsigned long)(*((c)++)))<< 8), \ + l|=(((unsigned long)(*((c)++))) ) ) +# define HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l)>>24)&0xff), \ + *((c)++)=(unsigned char)(((l)>>16)&0xff), \ + *((c)++)=(unsigned char)(((l)>> 8)&0xff), \ + *((c)++)=(unsigned char)(((l) )&0xff), \ + l) + +#endif \ No newline at end of file diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/include/include/ec.h" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/include/include/ec.h" new file mode 100644 index 0000000000000000000000000000000000000000..56adbe751f17342f84c17e3736f8806deb7f40b5 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/include/include/ec.h" @@ -0,0 +1,545 @@ +#include /* BigNum */ +#include "common_utils.h" + +typedef struct ec_point_st { + BIGNUM *X; + BIGNUM *Y; + int is_infinity; +} EC_POINT; + +int EC_POINT_init(EC_POINT *point) { + point->X = BN_new(); + point->Y = BN_new(); + point->is_infinity = 0; + if (point->X == NULL || point->Y == NULL) { + BN_free(point->X); + BN_free(point->Y); + return 0; + } + return 1; +} + +EC_POINT *EC_POINT_new() { + EC_POINT *ret = NULL; + ret = malloc(sizeof(*ret)); + if (ret == NULL) { + return NULL; + } + if (!EC_POINT_init(ret)) { + free(ret); + return NULL; + } + return ret; +} + +int EC_POINT_setXY(EC_POINT *r, const BIGNUM *x, const BIGNUM *y) { + if (r == NULL) { + return 0; + } + if (!BN_copy(r->X, x) || !(BN_copy(r->Y, y))) { + return 0; + } + return 1; +} + +const EC_POINT *EC_point_infinity() { + static const EC_POINT const_infinity = { NULL, NULL, 1 }; + return &const_infinity; +} + +int EC_POINT_is_infinity(const EC_POINT *point) { + return point->is_infinity; +} + +int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src) { + if (dest == NULL) { + return 0; + } + if (EC_POINT_is_infinity(src)) { + dest->is_infinity = 1; + } + else{ + dest->is_infinity = 0; + if (!BN_copy(dest->X, src->X) || !BN_copy(dest->Y, src->Y)) return 0; + } + return 1; +} + +#define NORMAL 0x1 +#define COMPRESS 0x2 +#define MIX 0x4 + +int EC_POINT_ecp2bin(uint8 *out, const EC_POINT *point, int p_bytes, int flag) { + uint8 PC; + uint8 *p = out, *buf; + int i; + buf = malloc(p_bytes); + if (EC_POINT_is_infinity(point)) { + return 0; + } + if (flag == NORMAL) { + PC = 0x04; + *(p++) = PC; + if (!BN_bn2binpad(point->X, buf, p_bytes)) { + return 0; + } + for (i = 0; i < p_bytes; ++i) { + *(p++) = buf[i]; + } + if (!BN_bn2binpad(point->Y, buf, p_bytes)) { + return 0; + } + for (i = 0; i < p_bytes; ++i) { + *(p++) = buf[i]; + } + } + else { + goto err; /* not supported */ + } + return 1; + err: + return 0; +} + +int EC_POINT_bin2ecp(EC_POINT *point, const uint8 *src, int p_bytes) { + if (point == NULL) { + return 0; + } + uint8 PC = *src; + const uint8 *data = src + 1; + uint8 *buf; + int i; + if (PC == 0x04) { + if (!BN_bin2bn(data, p_bytes, point->X) + || !BN_bin2bn(data+p_bytes, p_bytes, point->Y)) { + return 0; + } + } + else { + goto err; /* not supported */ + } + return 1; + err: + return 0; +} + +int EC_POINT_ecp2bin_normal(uint8 *out, const EC_POINT *point, int p_bytes) { + return EC_POINT_ecp2bin(out, point, p_bytes, NORMAL); +} + +// int EC_POINT_ecp2bin_compressed(uint8 *out, const EC_POINT *point, int p_bytes) { +// return EC_POINT_ecp2bin(out, point, p_bytes, COMPRESS); +// } + +// int EC_POINT_ecp2bin_mix(uint8 *out, const EC_POINT *point, int p_bytes) { +// return EC_POINT_ecp2bin(out, point, p_bytes, MIX); +// } + +typedef struct ec_group_st { + EC_POINT *generator; /* point G */ + BIGNUM *order; /* n */ + BIGNUM *cofactor; /* h */ + BIGNUM *field; /* module p */ + BIGNUM *a, *b; /* ECC parameter */ +} EC_GROUP; + +int EC_GROUP_init(EC_GROUP *group) { + group->generator = EC_POINT_new(); + group->order = BN_new(); + group->cofactor = BN_new(); + group->field = BN_new(); + group->a = BN_new(); + group->b = BN_new(); + if (group->generator == NULL || group->b == NULL) { + free(group->generator); + BN_free(group->order); + BN_free(group->cofactor); + BN_free(group->field); + BN_free(group->a); + BN_free(group->b); + return 0; + } + return 1; +} + +EC_GROUP *EC_GROUP_new() { + EC_GROUP *ret = NULL; + ret = malloc(sizeof(*ret)); + if (ret == NULL) { + return NULL; + } + if (!EC_GROUP_init(ret)) { + free(ret); + return NULL; + } + return ret; +} + +int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src) { + if (dest == NULL) { + return 0; + } + if (dest == src) { + return 1; + } + if (!EC_POINT_copy(dest->generator, src->generator) + || !BN_copy(dest->order, src->order) + || !BN_copy(dest->cofactor, src->cofactor) + || !BN_copy(dest->field, src->field) + || !BN_copy(dest->a, src->a) + || !BN_copy(dest->b, src->b)) { + return 0; + } + return 1; +} + +int EC_GROUP_set_param(EC_GROUP *group, const BIGNUM *field, const BIGNUM *a, const BIGNUM *b, const BIGNUM *order, const BIGNUM *cofactor, const EC_POINT *point) { + if (group == NULL) { + return 0; + } + group->field = BN_new(); + group->a = BN_new(); + group->b = BN_new(); + group->order = BN_new(); + group->cofactor = BN_new(); + group->generator = EC_POINT_new(); + if (group->cofactor == NULL + || group->generator == NULL) { + return 0; + } + if (!BN_copy(group->field, field) + || !BN_copy(group->a, a) + || !BN_copy(group->b, b) + || !BN_copy(group->order, order) + || !BN_copy(group->cofactor, cofactor) + || !EC_POINT_copy(group->generator, point)) { + return 0; + } + return 1; +} + +int EC_POINT_double(EC_POINT *r, const EC_POINT *a, const EC_GROUP *group) { + const BIGNUM *p; + BN_CTX *ctx = NULL; + int ret = 0; + BIGNUM *ax, *ay, *lnum, *ldeno; + BIGNUM *tmp, *bn_value_3; + if (r == NULL) { + return 0; + } + if (EC_POINT_is_infinity(a)) { + return EC_POINT_copy(r, EC_point_infinity()); + } + p = group->field; + ctx = BN_CTX_new(); + if (ctx == NULL) { + return 0; + } + BN_CTX_start(ctx); + ax = BN_CTX_get(ctx); + ay = BN_CTX_get(ctx); + lnum = BN_CTX_get(ctx); + ldeno = BN_CTX_get(ctx); + tmp = BN_CTX_get(ctx); + bn_value_3 = BN_CTX_get(ctx); + if (bn_value_3 == NULL) { + goto done; + } + if (!BN_copy(ax, a->X) + || !BN_copy(ay, a->Y)) { + goto done; + } + if (!BN_set_word(bn_value_3, 3) + || !BN_mod_sqr(lnum, ax, p, ctx) + || !BN_mod_mul(lnum, lnum, bn_value_3, p, ctx) + || !BN_mod_add_quick(lnum, lnum, group->a, p) + || !BN_mod_lshift1_quick(ldeno, ay, p) + || !BN_mod_inverse(ldeno, ldeno, p, ctx) + || !BN_mod_mul(lnum, lnum, ldeno, p, ctx)) { + goto done; + } + if (!BN_mod_sqr(r->X, lnum, p, ctx)) { + goto done; + } + if (!BN_mod_lshift1_quick(tmp, ax, p)) { + goto done; + } + if (!BN_mod_sub_quick(r->X, r->X, tmp, p)) { + goto done; + } + if (!BN_mod_sub_quick(r->Y, ax, r->X, p)) { + goto done; + } + if (!BN_mod_mul(r->Y, lnum, r->Y, p, ctx)) { + goto done; + } + if (!BN_mod_sub_quick(r->Y, r->Y, ay, p)) { + goto done; + } + if (r == NULL) { + r = EC_POINT_new(); + if (r == NULL) { + goto done; + } + } + ret = 1; + done: + BN_CTX_end(ctx); + BN_CTX_free(ctx); + return ret; +} + +int EC_POINT_add(EC_POINT *r, const EC_POINT *a, const EC_POINT *b, const EC_GROUP *group) { + const BIGNUM *p; + BN_CTX *ctx = NULL; + int ret = 0; + BIGNUM *ax, *ay, *bx, *by, *lnum, *ldeno; + BIGNUM *tmp, *bn_value_3; + if (r == NULL) { + return 0; + } + if (a == b) { + return EC_POINT_double(r, a, group); + } + if (EC_POINT_is_infinity(a)) { + return EC_POINT_copy(r, b); + } + if (EC_POINT_is_infinity(b)) { + return EC_POINT_copy(r, a); + } + p = group->field; + ctx = BN_CTX_new(); + if (ctx == NULL) { + return 0; + } + BN_CTX_start(ctx); + ax = BN_CTX_get(ctx); + ay = BN_CTX_get(ctx); + bx = BN_CTX_get(ctx); + by = BN_CTX_get(ctx); + lnum = BN_CTX_get(ctx); + ldeno = BN_CTX_get(ctx); + tmp = BN_CTX_get(ctx); + bn_value_3 = BN_CTX_get(ctx); + if (bn_value_3 == NULL) { + goto done; + } + if (!BN_copy(ax, a->X) + || !BN_copy(ay, a->Y) + || !BN_copy(bx, b->X) + || !BN_copy(by, b->Y)) { + goto done; + } + if (BN_cmp(ax, bx) == 0) { + if (!BN_mod_add_quick(tmp, ay, by, p)) { + goto done; + } + if (BN_is_zero(tmp) + || !EC_POINT_copy(r, EC_point_infinity())) { + goto done; + } + if (!BN_set_word(bn_value_3, 3)) { + goto done; + } + if (!BN_mod_sqr(lnum, ax, p, ctx) + || !BN_mod_mul(lnum, lnum, bn_value_3, p, ctx) + || !BN_mod_add_quick(lnum, lnum, group->a, p) + || !BN_mod_lshift1_quick(ldeno, ay, p) + || !BN_mod_inverse(ldeno, ldeno, p, ctx) + || !BN_mod_mul(lnum, lnum, ldeno, p, ctx)) { + goto done; + } + } + else { + if (!BN_mod_sub_quick(lnum, by, ay, p)) { + goto done; + } + if (!BN_mod_sub_quick(ldeno, bx, ax, p)) { + goto done; + } + if (!BN_mod_inverse(ldeno, ldeno, p, ctx)) { + goto done; + } + if (!BN_mod_mul(lnum, lnum, ldeno, p, ctx)) { + goto done; + } + } + if (!BN_mod_sqr(tmp, lnum, p, ctx)) { + goto done; + } + if (!BN_mod_sub_quick(tmp, tmp, ax, p)) { + goto done; + } + if (!BN_mod_sub_quick(r->X, tmp, bx, p)) { + goto done; + } + if (!BN_mod_sub_quick(r->Y, ax, r->X, p)) { + goto done; + } + if (!BN_mod_mul(r->Y, lnum, r->Y, p, ctx)) { + goto done; + } + if (!BN_mod_sub_quick(r->Y, r->Y, ay, p)) { + goto done; + } + if (r == NULL) { + r = EC_POINT_new(); + if (r == NULL) { + goto done; + } + } + ret = 1; + done: + BN_CTX_end(ctx); + BN_CTX_free(ctx); + return ret; +} + +int EC_POINT_mul(EC_POINT *r, const EC_POINT *a, const BIGNUM *b, const EC_GROUP *group) { + if (r == NULL) { + return 0; + } + if (BN_is_negative(b)) { + EC_POINT *_a = EC_POINT_new(); + BIGNUM *_b = BN_new(); + if (!EC_POINT_copy(_a, a) + || !BN_copy(_b, b) + || !BN_mod_sub_quick(_a->Y, group->field, _a->Y, group->field)) { + return 0; + } + BN_set_negative(_b, 0); + return EC_POINT_mul(r, _a, _b, group); + } + if (!EC_POINT_copy(r, EC_point_infinity())){ + return 0; + } + int bits = BN_num_bits(b); + for (int i = bits-1; i >= 0; --i) { + if (!EC_POINT_double(r, r, group)) { + return 0; + } + if (BN_is_bit_set(b, i)) { + if (!EC_POINT_add(r, r, a, group)) { + return 0; + } + } + } + return 1; +} + +/* + * Only for curve y^2 = x^3 + ax + b. + * Please check infinity before calling. + */ +int EC_POINT_on_curve_simple(const EC_POINT *point, const EC_GROUP *group) { + if (point == NULL || group == NULL) { + return 0; + } + if (EC_POINT_is_infinity(point)) { + return 1; + } + int ret = 0; + BN_CTX *ctx; + BIGNUM *p, *a, *b, *x, *y, *lhs, *rhs; + EC_POINT *Nq; + ctx = BN_CTX_new(); + if (ctx == NULL) { + return ret; + } + BN_CTX_start(ctx); + p = group->field; + a = group->a; + b = group->b; + x = point->X; + y = point->Y; + lhs = BN_CTX_get(ctx); + rhs = BN_CTX_get(ctx); + Nq = EC_POINT_new(); + if (p == NULL + || a == NULL + || b == NULL + || x == NULL + || y == NULL + || rhs == NULL + || Nq == NULL) { + return ret; + } + if (!BN_mod_mul(lhs, y, y, p, ctx) + || !BN_mod_mul(rhs, x, x, p, ctx) + || !BN_mod_add_quick(rhs, rhs, a, p) + || !BN_mod_mul(rhs, rhs, x, p, ctx) + || !BN_mod_add_quick(rhs, rhs, b, p) + || !EC_POINT_mul(Nq, point, group->order, group)) { + return ret; + } + if (BN_cmp(lhs, rhs) != 0 + || !EC_POINT_is_infinity(Nq)) { + return ret; + } + ret = 1; + BN_CTX_end(ctx); + BN_CTX_free(ctx); + return ret; +} + +typedef struct ec_key_st { + EC_GROUP *group; + EC_POINT *pub_key; + BIGNUM *priv_key; +} EC_KEY; + +int EC_KEY_init(EC_KEY *key) { + key->group = EC_GROUP_new(); + key->pub_key = EC_POINT_new(); + key->priv_key = BN_new(); + if (key->group == NULL + || key->pub_key == NULL + || key->priv_key == NULL) { + free(key->group); + free(key->pub_key); + BN_free(key->priv_key); + return 0; + } + return 1; +} + +EC_KEY *EC_KEY_new() { + EC_KEY *ret = NULL; + ret = malloc(sizeof(*ret)); + if (ret == NULL) { + return NULL; + } + if (!EC_KEY_init(ret)) { + free(ret); + return NULL; + } + return ret; +} + +int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group) { + if (!EC_GROUP_copy(key->group, group)) { + return 0; + } + return 1; +} + +int EC_KEY_set_priv_key(EC_KEY *key, const BIGNUM *priv_key) { + BIGNUM *tmp; + tmp = BN_dup(priv_key); + if (tmp == NULL) { + return 0; + } + BN_clear_free(key->priv_key); + key->priv_key = tmp; + return 1; +} + +int EC_KEY_generate_pub_key(EC_KEY *key) { + return EC_POINT_mul(key->pub_key, key->group->generator, key->priv_key, key->group); +} + +const BIGNUM *EC_KEY_get_priv_key(const EC_KEY *key) { + return key->priv_key; +} + +int EC_KEY_generate_key() {} diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/sm2/sm2.h" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/sm2/sm2.h" new file mode 100644 index 0000000000000000000000000000000000000000..97e051f86da114e645fdd11a9ad767a75aa6f0c3 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/sm2/sm2.h" @@ -0,0 +1,288 @@ +#ifndef SECGEAR_SM2_H +#define SECGEAR_SM2_H + +/* + * Ref: + * GB/T 32918.1-2016 + * GB/T 32918.2-2016 + * GB/T 32918.3-2016 + * GB/T 32918.4-2016 + * GB/T 32918.5-2017 + */ + +#include +#include "../include/common_utils.h" +#include "../include/ec.h" +#include "../sm3/sm3.h" +#define SM2ERR(msg) fprintf(stderr,msg);goto err; +/* + * export C_INCLUDE_PATH="/PATH/TO/OPENSSL/include"(/opt/homebrew/Cellar/openssl@1.1/1.1.1m/include) + * export CPLUS_INCLUDE_PATH="/PATH/TO/OPENSSL/include" + * export LIBRARY_PATH="/PATH/TO/OPENSSL/lib" + */ + +/* Parameters */ +/* EC: y^2 = x^3 + ax + b */ + +#define SM2_PARAM_CBLOCK 32 +#define SM2_PARAM_LBLOCK (SM2_PARAM_CBLOCK/4) + +const uint8 SM2_P[SM2_PARAM_CBLOCK] = { + 0xFF, 0xFF, 0xFF, 0xFE, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + 0x00, 0x00, 0x00, 0x00, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, +}; +const uint8 SM2_A[SM2_PARAM_CBLOCK] = { + 0xFF, 0xFF, 0xFF, 0xFE, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + 0x00, 0x00, 0x00, 0x00, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFC, +}; +const uint8 SM2_B[SM2_PARAM_CBLOCK] = { + 0x28, 0xE9, 0xFA, 0x9E, + 0x9D, 0x9F, 0x5E, 0x34, + 0x4D, 0x5A, 0x9E, 0x4B, + 0xCF, 0x65, 0x09, 0xA7, + 0xF3, 0x97, 0x89, 0xF5, + 0x15, 0xAB, 0x8F, 0x92, + 0xDD, 0xBC, 0xBD, 0x41, + 0x4D, 0x94, 0x0E, 0x93, +}; +const uint8 SM2_N[SM2_PARAM_CBLOCK] = { + 0xFF, 0xFF, 0xFF, 0xFE, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + 0x72, 0x03, 0xDF, 0x6B, + 0x21, 0xC6, 0x05, 0x2B, + 0x53, 0xBB, 0xF4, 0x09, + 0x39, 0xD5, 0x41, 0x23, +}; +const uint8 SM2_XG[SM2_PARAM_CBLOCK] = { + 0x32, 0xC4, 0xAE, 0x2C, + 0x1F, 0x19, 0x81, 0x19, + 0x5F, 0x99, 0x04, 0x46, + 0x6A, 0x39, 0xC9, 0x94, + 0x8F, 0xE3, 0x0B, 0xBF, + 0xF2, 0x66, 0x0B, 0xE1, + 0x71, 0x5A, 0x45, 0x89, + 0x33, 0x4C, 0x74, 0xC7, +}; +const uint8 SM2_YG[SM2_PARAM_CBLOCK] = { + 0xBC, 0x37, 0x36, 0xA2, + 0xF4, 0xF6, 0x77, 0x9C, + 0x59, 0xBD, 0xCE, 0xE3, + 0x6B, 0x69, 0x21, 0x53, + 0xD0, 0xA9, 0x87, 0x7C, + 0xC6, 0x2A, 0x47, 0x40, + 0x02, 0xDF, 0x32, 0xE5, + 0x21, 0x39, 0xF0, 0xA0, +}; + +typedef struct sm2_sig_st { + BIGNUM *r; + BIGNUM *s; +} SM2_SIG; + +int SM2_SIG_init(SM2_SIG *sig) { + sig->r = BN_new(); + sig->s = BN_new(); + if (sig->r == NULL || sig->s == NULL) { + BN_free(sig->r); + BN_free(sig->s); + return 0; + } + return 1; +} + +SM2_SIG *SM2_SIG_new() { + SM2_SIG *ret; + ret = malloc(sizeof(*ret)); + if (ret == NULL) { + return NULL; + } + if (!SM2_SIG_init(ret)) { + free(ret); + return NULL; + } + return ret; +} + +int sm2_compute_z(uint8 *out, const uint8 *id, const size_t idlen, const EC_KEY *key){ + /* ZA = SM3(ENTLA || IDA || a || b || xG || yG || xA || yA) */ + uint16 entlen = idlen << 3; + // printf("entlen: %04X\n", entlen); + uint8 e_bytes, *buf; + int p_bytes; + SM3_CTX c; + sm3_init(&c); + // debugPrint; + if (idlen > (UINT16_MAX >> 3)) { + SM2ERR("SM2_compute_z(): idlen too large!\n"); + } + e_bytes = entlen >> 8; + // printf("ENTL_H: %02X\n", e_bytes); + sm3_update(&c, &e_bytes, 1); + e_bytes = entlen & 0xFF; + // printf("ENTL_L: %02X\n", e_bytes); + sm3_update(&c, &e_bytes, 1); + // printf("ID_A: "); + // for (int i=0; igroup->field); + // debugPrint; + buf = malloc(p_bytes); + // debugPrint; + if (!BN_bn2binpad(key->group->a, buf, p_bytes)) { + goto err; + } + // printf("a: "); + // for (int i=0; igroup->b, buf, p_bytes)) { + goto err; + } + // printf("b: "); + // for (int i=0; igroup->generator->X, buf, p_bytes)) { + goto err; + } + // printf("XG: "); + // for (int i=0; igroup->generator->Y, buf, p_bytes)) { + goto err; + } + // printf("YG: "); + // for (int i=0; ipub_key->X, buf, p_bytes) < 0) { + goto err; + } + // printf("XA: "); + // for (int i=0; ipub_key->Y, buf, p_bytes) < 0) { + goto err; + } + // printf("YA: "); + // for (int i=0; i> 3)) { + return 0; + } + } + return 1; +} + +#endif \ No newline at end of file diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/sm2/sm2_crypt.h" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/sm2/sm2_crypt.h" new file mode 100644 index 0000000000000000000000000000000000000000..14928f17576c2375d4d942efa2c52ef942ff1790 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/sm2/sm2_crypt.h" @@ -0,0 +1,257 @@ +#include "sm2.h" + +/* + * Ref: + * GB/T 32918.4-2016 + */ + +/* Encrypt */ + +int sm2_encrypt(uint8 *out, const uint8 *M, const uint64 klen, const EC_KEY *key) { + SM3_CTX c; + BIGNUM *n, *k, *h, *x_2, *y_2; + EC_POINT *G, *C1, *S, *PB, *tmp_point; + EC_GROUP *group; + uint8 *_C1, *_C2, *_C3, *t, *buf; + size_t C1len, MlenCBLOCK, buflen, i; + int p_bytes; + group = key->group; + n = group->order; + h = group->cofactor; + G = group->generator; + PB = key->pub_key; + x_2 = BN_new(); + y_2 = BN_new(); + k = BN_new(); + C1 = EC_POINT_new(); + tmp_point = EC_POINT_new(); + S = EC_POINT_new(); + if (group == NULL + || n == NULL + || h == NULL + || k == NULL + || G == NULL + || S == NULL + || PB == NULL) { + return 0; + } + p_bytes = BN_num_bytes(group->field); + MlenCBLOCK = klen >> 3; + C1len = (p_bytes << 1 | 1); + buflen = p_bytes << 1; + _C1 = malloc(C1len); + _C2 = malloc(MlenCBLOCK); + _C3 = malloc(SM3_DIGEST_LENGTH); + buf = malloc(buflen); + t = malloc(MlenCBLOCK); + if (t == NULL) { + goto err; + } + for (;;) { + /* A1: k = rand(1, n-1) */ + do { + if (!BN_rand_range(k, n)) { + goto err; + } + } while(BN_is_zero(k)); + #ifdef GBT32918_4_2016_A2_TEST /* For GB/T 32918.4-2016 A.2 test */ + if (p_bytes * 8 == 192) { + BN_hex2bn(&k, "384F30353073AEECE7A1654330A96204D37982A3E15B2CB5"); + /* FP-192 */ + } + else { + /* FP-256 */ + BN_hex2bn(&k, "4C62EEFD6ECFC2B95B92FD6C3D9575148AFA17425546D49018E5388D49DD7B4F"); + } + #endif + #ifdef GBT32918_5_2017_TEST + BN_hex2bn(&k, "59276E27D506861A16680F3AD9C02DCCEF3CC1FA3CDBE4CE6D54B80DEAC1BC21"); + #endif + printf("k: %s\n", BN_bn2hex(k)); + /* A2: C1 = [k]G = (x1, y1), C1 -> bytes */ + if (!EC_POINT_mul(C1, G, k, group) + || !EC_POINT_ecp2bin_normal(_C1, C1, p_bytes)) { + goto err; + } + printf("C1: (%s, \n\t%s)\n", BN_bn2hex(C1->X), BN_bn2hex(C1->Y)); + printf("C1 bin: "); + for (int i=0; i bytes */ + if (!EC_POINT_mul(tmp_point, PB, k, group)) { + goto err; + } + x_2 = tmp_point->X; + y_2 = tmp_point->Y; + printf("(x2, y2): (%s, \n\t%s)\n", BN_bn2hex(x_2), BN_bn2hex(y_2)); + /* A5: t = KDF(x2 || y2, klen), if t == 0, goto A1 */ + printf("klen: %llu\n", klen); + if (!BN_bn2binpad(x_2, buf, p_bytes) + || !BN_bn2binpad(y_2, buf+p_bytes, p_bytes) + || !sm2_KDF(t, buf, buflen, klen)) { + goto err; + } + for (i = 0; i < MlenCBLOCK; ++i) { + if (t[i]) { + goto A6; + } + } + } + /* A6: C2 = M ^ t */ + A6: + printf("t: "); + for (int i=0; igroup; + h = group->cofactor; + dB = key->priv_key; + x_2 = BN_new(); + y_2 = BN_new(); + C1 = EC_POINT_new(); + tmp_point = EC_POINT_new(); + S = EC_POINT_new(); + if (group == NULL + || h == NULL + || x_2 == NULL + || y_2 == NULL + || dB == NULL + || S == NULL) { + return 0; + } + p_bytes = BN_num_bytes(group->field); + MlenCBLOCK = klen >> 3; + C1len = (p_bytes << 1 | 1); + buflen = p_bytes << 1; + _C1 = malloc(C1len); + _C2 = malloc(MlenCBLOCK); + _C3 = malloc(SM3_DIGEST_LENGTH); + u = malloc(SM3_DIGEST_LENGTH); + buf = malloc(buflen); + t = malloc(MlenCBLOCK); + if (t == NULL) { + goto err; + } + /* C = C1 || C3 || C2 */ + if (!memcpy(_C1, C, C1len) + || !memcpy(_C2, C+C1len+SM3_DIGEST_LENGTH, MlenCBLOCK) + || !memcpy(_C3, C+C1len, SM3_DIGEST_LENGTH)) { + goto err; + } + /* B1: C -> C1, check if C1 on the curve */ + if (!EC_POINT_bin2ecp(C1, _C1, p_bytes) + || !EC_POINT_on_curve_simple(C1, group)) { + goto err; + } + /* B2: S = [h]C1, if S == O, goto err */ + if (!EC_POINT_mul(S, C1, h, group) + || EC_POINT_is_infinity(S)) { + goto err; + } + /* B3: [dB]C1 = (x2, y2), x2, y2 -> bytes */ + if (!EC_POINT_mul(tmp_point, C1, dB, group)) { + goto err; + } + x_2 = tmp_point->X; + y_2 = tmp_point->Y; + printf("(x2, y2): (%s, \n\t%s)\n", BN_bn2hex(x_2), BN_bn2hex(y_2)); + /* B4: t = KDF(x2 || y2, klen), if t = 0, goto err */ + if (!BN_bn2binpad(x_2, buf, p_bytes) + || !BN_bn2binpad(y_2, buf+p_bytes, p_bytes) + || !sm2_KDF(t, buf, buflen, klen)) { + goto err; + } + for (i = 0; i < MlenCBLOCK; ++i) { + if (t[i]) { + goto B5; + } + } + goto err; + B5: + /* B5: C -> C2, M' = C2 ^ t*/ + printf("t: "); + for (int i=0; igroup, userB->group)); + // if (!EC_GROUP_same(userA->group, userB->group)) { + // return 0; + // } + int w = (BN_num_bits(userA->group->order) + 1) / 2 - 1; /* For n is not pow of 2 */ + size_t p_bytes = BN_num_bytes(userB->group->field), k_bytes = (klen >> 3) + ((klen & 7) ? 1 : 0), Zlen; + uint8 const_byte; + printf("w = %d\n", w); + SM3_CTX c; + BN_CTX *ctx; + ctx = BN_CTX_new(); + if (ctx == NULL) { + return 0; + } + BN_CTX_start(ctx); + /* A-pre: */ + BIGNUM *rA, *x_1, *y_1, *tmpa, *tA; + const BIGNUM *nA, *dA, *hA; + EC_POINT *RA, *U, *tmp_pointA; + const EC_POINT *GA, *PA; + EC_GROUP *groupA; + uint8 *Z, *K, *buf, *tmp_char, *SA, *S1, *SB, *S2, *buf_digest; + SA = malloc(SM3_DIGEST_LENGTH); + S1 = malloc(SM3_DIGEST_LENGTH); + SB = malloc(SM3_DIGEST_LENGTH); + S2 = malloc(SM3_DIGEST_LENGTH); + buf_digest = malloc(SM3_DIGEST_LENGTH); + if (buf_digest == NULL) { + return 0; + } + groupA = userA->group; + nA = groupA->order; + dA = userA->priv_key; + hA = groupA->cofactor; + x_1 = BN_CTX_get(ctx); + y_1 = BN_CTX_get(ctx); + tmpa = BN_CTX_get(ctx); + tA = BN_CTX_get(ctx); + rA = BN_CTX_get(ctx); + GA = groupA->generator; + PA = userA->pub_key; + U = EC_POINT_new(); + tmp_pointA = EC_POINT_new(); + RA = EC_POINT_new(); + if (groupA == NULL + || nA == NULL + || dA == NULL + || hA == NULL + || rA == NULL + || GA == NULL + || PA == NULL + || RA == NULL) { + return 0; + } + /* A1: rA = rand(1, n-1) */ + do { + if (!BN_rand_range(rA, nA)) { + return 0; + } + } while(BN_is_zero(rA)); + #ifdef GBT32918_3_2016_A2_TEST + /* For GB/T 32918.3-2016 A.2 test */ + BN_hex2bn(&rA, "83A2C9C8B96E5AF70BD480B472409A9A327257F1EBB73F5B073354B248668563"); + #endif + #ifdef GBT32918_5_2017_TEST + BN_hex2bn(&rA, "D4DE15474DB74D06491C440D305E012400990F3E390C7E87153C12DB2EA60BB3"); + #endif + printf("rA: %s\n", BN_bn2hex(rA)); + /* A2: RA = [rA]G = (x1, y1) */ + if (!EC_POINT_mul(RA, GA, rA, groupA)) { + return 0; + } + printf("RA: (%s, \n\t%s)\n", BN_bn2hex(RA->X), BN_bn2hex(RA->Y)); + /* A3: send RA to userB */ + /* B-pre: */ + BIGNUM *rB, *x_2, *y_2, *tmpb, *tB; + const BIGNUM *nB, *dB, *hB; + EC_POINT *RB, *V, *tmp_pointB; + const EC_POINT *GB, *PB; + EC_GROUP *groupB; + groupB = userB->group; + nB = groupB->order; + dB = userB->priv_key; + hB = groupB->cofactor; + x_2 = BN_CTX_get(ctx); + y_2 = BN_CTX_get(ctx); + tmpb = BN_CTX_get(ctx); + tB = BN_CTX_get(ctx); + rB = BN_CTX_get(ctx); + GB = groupB->generator; + PB = userB->pub_key; + V = EC_POINT_new(); + tmp_pointB = EC_POINT_new(); + RB = EC_POINT_new(); + if (groupB == NULL + || nB == NULL + || dB == NULL + || hB == NULL + || rB == NULL + || GB == NULL + || PB == NULL + || RB == NULL) { + return 0; + } + /* B1: rB = rand(1, n-1) */ + do { + if (!BN_rand_range(rB, nB)) { + return 0; + } + } while(BN_is_zero(rB)); + #ifdef GBT32918_3_2016_A2_TEST + /* For GB/T 32918.3-2016 A.2 test */ + BN_hex2bn(&rB, "33FE21940342161C55619C4A0C060293D543C80AF19748CE176D83477DE71C80"); + #endif + #ifdef GBT32918_5_2017_TEST + BN_hex2bn(&rB, "7E07124814B309489125EAED101113164EBF0F3458C5BD88335C1F9D596243D6"); + #endif + printf("rB: %s\n", BN_bn2hex(rB)); + /* B2: RB = [rB]G = (x2, y2) */ + if (!EC_POINT_mul(RB, GB, rB, groupB)) { + return 0; + } + printf("RB: (%s, \n\t%s)\n", BN_bn2hex(RB->X), BN_bn2hex(RB->Y)); + /* B3: \bar{x_2} = 2^w + (x_2 & (2^w - 1)) */ + if (!BN_one(tmpb) + || !BN_lshift(tmpb, tmpb, w) + || !BN_mod(x_2, RB->X, tmpb, ctx) + || !BN_add(x_2, x_2, tmpb)) { + return 0; + } + printf("bar{x2}: %s\n", BN_bn2hex(x_2)); + /* B4: tB = (dB + \bar{x_2} \times rB) % n */ + if (!BN_mod_mul(tB, x_2, rB, nB, ctx) + || !BN_mod_add_quick(tB, tB, dB, nB)) { + return 0; + } + printf("tB: %s\n", BN_bn2hex(tB)); + /* B5: verify RA is on curve, \bar{x_1} = 2^w + (x_1 & (2^w - 1)) */ + if (!EC_POINT_on_curve_simple(RA, groupB)) { + return 0; + } + printf("On curve: %d\n", EC_POINT_on_curve_simple(RA, groupB)); + if (!BN_one(tmpb) + || !BN_lshift(tmpb, tmpb, w) + || !BN_mod(x_1, RA->X, tmpb, ctx) + || !BN_add(x_1, x_1, tmpb)) { + return 0; + } + printf("bar{x1}: %s\n", BN_bn2hex(x_1)); + /* B6: V = [h \times tB](PA + [\bar{x1}]RA) = (xV, yV) */ + if (!EC_POINT_mul(tmp_pointB, RA, x_1, groupB) + || !printf("[bar{x1}]RA: (%s, \n\t%s)\n", BN_bn2hex(tmp_pointB->X), BN_bn2hex(tmp_pointB->Y)) + || !EC_POINT_add(tmp_pointB, tmp_pointB, PA, groupB) + || !printf("PA + [bar{x1}]RA: (%s, \n\t%s)\n", BN_bn2hex(tmp_pointB->X), BN_bn2hex(tmp_pointB->Y)) + || !BN_mul(tmpb, hB, tB, ctx) + || !EC_POINT_mul(V, tmp_pointB, tmpb, groupB) + || EC_POINT_is_infinity(V)) { + return 0; + } + printf("V: (%s, \n\t%s)\n", BN_bn2hex(V->X), BN_bn2hex(V->Y)); + /* B7: KB = KDF(xV || yV || ZA || ZB, klen) */ + K = malloc((klen + 7) >> 3); + Zlen = (p_bytes + SM3_DIGEST_LENGTH) * 2; + Z = malloc(Zlen); + buf = malloc(p_bytes); + tmp_char = Z; + if (!BN_bn2binpad(V->X, buf, p_bytes) + || !memcpy(tmp_char, buf, p_bytes) + || !(tmp_char += p_bytes) + || !BN_bn2binpad(V->Y, buf, p_bytes) + || !memcpy(tmp_char, buf, p_bytes) + || !(tmp_char += p_bytes) + || !memcpy(tmp_char, ZA, SM3_DIGEST_LENGTH) + || !(tmp_char += p_bytes) + || !memcpy(tmp_char, ZB, SM3_DIGEST_LENGTH)) { + return 0; + } + printf("Z: "); + for (int i=0; iX, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !sm3_update(&c, ZA, SM3_DIGEST_LENGTH) + || !sm3_update(&c, ZB, SM3_DIGEST_LENGTH) + || !BN_bn2binpad(RA->X, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !BN_bn2binpad(RA->Y, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !BN_bn2binpad(RB->X, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !BN_bn2binpad(RB->Y, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !sm3_final(buf_digest, &c)) { + return 0; + } + printf("SM3(xV || ZA || ZB || x1 || y1 || x2 || y2): "); + for (int i=0; iY, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !sm3_update(&c, buf_digest, SM3_DIGEST_LENGTH) + || !sm3_final(SB, &c)) { + return 0; + } + printf("SB: "); + for (int i=0; iX, tmpa, ctx) + || !BN_add(x_1, x_1, tmpa)) { + return 0; + } + printf("bar{x1}: %s\n", BN_bn2hex(x_1)); + /* A5: tA = (dA + \bar{x_1} \times rA) % n */ + if (!BN_mod_mul(tA, x_1, rA, nA, ctx) + || !BN_mod_add_quick(tA, tA, dA, nA)) { + return 0; + } + printf("tA: %s\n", BN_bn2hex(tA)); + /* A6: verify RB is on curve, \bar{x_2} = 2^w + (x_2 & (2^w - 1)) */ + if (!EC_POINT_on_curve_simple(RB, groupA)) { + return 0; + } + printf("On curve: %d\n", EC_POINT_on_curve_simple(RB, groupA)); + if (!BN_one(tmpa) + || !BN_lshift(tmpa, tmpa, w) + || !BN_mod(x_2, RB->X, tmpa, ctx) + || !BN_add(x_2, x_2, tmpa)) { + return 0; + } + printf("bar{x2}: %s\n", BN_bn2hex(x_2)); + /* A7: U = [h \times tA](PB + [\bar{x2}]RB) = (xU, yU) */ + if (!EC_POINT_mul(tmp_pointA, RB, x_2, groupA) + || !printf("[bar{x2}]RB: (%s, \n\t%s)\n", BN_bn2hex(tmp_pointA->X), BN_bn2hex(tmp_pointA->Y)) + || !EC_POINT_add(tmp_pointA, tmp_pointA, PB, groupA) + || !printf("PB + [bar{x2}]RB: (%s, \n\t%s)\n", BN_bn2hex(tmp_pointA->X), BN_bn2hex(tmp_pointA->Y)) + || !BN_mul(tmpa, hA, tA, ctx) + || !EC_POINT_mul(U, tmp_pointA, tmpa, groupA) + || EC_POINT_is_infinity(U)) { + return 0; + } + printf("U: (%s, \n\t%s)\n", BN_bn2hex(U->X), BN_bn2hex(U->Y)); + /* A8: KA = KDF(xU || yU || ZA || ZB, klen) */ + K = malloc((klen + 7) >> 3); + Zlen = (p_bytes + SM3_DIGEST_LENGTH) * 2; + Z = malloc(Zlen); + buf = malloc(p_bytes); + tmp_char = Z; + if (!BN_bn2binpad(U->X, buf, p_bytes) + || !memcpy(tmp_char, buf, p_bytes) + || !(tmp_char += p_bytes) + || !BN_bn2binpad(U->Y, buf, p_bytes) + || !memcpy(tmp_char, buf, p_bytes) + || !(tmp_char += p_bytes) + || !memcpy(tmp_char, ZA, SM3_DIGEST_LENGTH) + || !(tmp_char += p_bytes) + || !memcpy(tmp_char, ZB, SM3_DIGEST_LENGTH)) { + return 0; + } + printf("Z: "); + for (int i=0; iX, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !sm3_update(&c, ZA, SM3_DIGEST_LENGTH) + || !sm3_update(&c, ZB, SM3_DIGEST_LENGTH) + || !BN_bn2binpad(RA->X, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !BN_bn2binpad(RA->Y, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !BN_bn2binpad(RB->X, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !BN_bn2binpad(RB->Y, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !sm3_final(buf_digest, &c)) { + return 0; + } + printf("SM3(xU || ZA || ZB || x1 || y1 || x2 || y2): "); + for (int i=0; iY, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !sm3_update(&c, buf_digest, SM3_DIGEST_LENGTH) + || !sm3_final(S1, &c)) { + return 0; + } + printf("S1: "); + for (int i=0; iX, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !sm3_update(&c, ZA, SM3_DIGEST_LENGTH) + || !sm3_update(&c, ZB, SM3_DIGEST_LENGTH) + || !BN_bn2binpad(RA->X, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !BN_bn2binpad(RA->Y, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !BN_bn2binpad(RB->X, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !BN_bn2binpad(RB->Y, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !sm3_final(buf_digest, &c)) { + return 0; + } + printf("SM3(xU || ZA || ZB || x1 || y1 || x2 || y2): "); + for (int i=0; iY, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !sm3_update(&c, buf_digest, SM3_DIGEST_LENGTH) + || !sm3_final(SA, &c)) { + return 0; + } + printf("SA: "); + for (int i=0; iX, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !sm3_update(&c, ZA, SM3_DIGEST_LENGTH) + || !sm3_update(&c, ZB, SM3_DIGEST_LENGTH) + || !BN_bn2binpad(RA->X, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !BN_bn2binpad(RA->Y, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !BN_bn2binpad(RB->X, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !BN_bn2binpad(RB->Y, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !sm3_final(buf_digest, &c)) { + return 0; + } + printf("SM3(xV || ZA || ZB || x1 || y1 || x2 || y2): "); + for (int i=0; iY, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !sm3_update(&c, buf_digest, SM3_DIGEST_LENGTH) + || !sm3_final(S2, &c)) { + return 0; + } + printf("SA: "); + for (int i=0; i>3, ID_A, ID_B, Pp, Pa, Pb, Pn, PXG, PYG, PdA, PdB, Pklen); + return 0; +} \ No newline at end of file diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/sm2/sm2_sign.h" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/sm2/sm2_sign.h" new file mode 100644 index 0000000000000000000000000000000000000000..8ab76644ebca7fa5f938f1019bf6a10d460b2148 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/sm2/sm2_sign.h" @@ -0,0 +1,259 @@ +#include "sm2.h" + +/* + * Ref: + * GB/T 32918.2-2016 + */ + +/** SM2 signature generation. + * \param msg massage for signing. + * \param msglen byte length of msg. + * \param id identifier of user A. + * \param idlen byte length of id. + * \param key key of user A. + * \return the sign generated. + */ +SM2_SIG *sm2_sig_gen(const uint8 *msg, const size_t msglen, const uint8 *id, const size_t idlen, const EC_KEY *key) { + SM3_CTX c; + sm3_init(&c); + uint8 *buf; + BN_CTX *ctx; + BIGNUM *e, *n, *k, *x, *r, *s, *rk, *dA, *tmp; + EC_POINT *kG, *G; + SM2_SIG *sig; + // debugPrint; + ctx = BN_CTX_new(); + if (ctx == NULL) { + return 0; + } + sig = SM2_SIG_new(); + if (sig == NULL) { + return 0; + } + BN_CTX_start(ctx); + e = BN_CTX_get(ctx); + n = key->group->order; + k = BN_CTX_get(ctx); + x = BN_CTX_get(ctx); + r = sig->r; + s = sig->s; + rk = BN_CTX_get(ctx); + dA = key->priv_key; + tmp = BN_CTX_get(ctx); + G = key->group->generator; + kG = EC_POINT_new(); + if (n == NULL + || r == NULL + || s == NULL + || dA == NULL + || tmp == NULL + || G == NULL + || kG == NULL) { + return 0; + } + // debugPrint; + printf("n: %s\n",BN_bn2hex(n)); + buf = malloc(SM3_DIGEST_LENGTH); + // debugPrint; + /* A1: \bar{M} = ZA || M */ + if (!sm2_compute_z(buf, id, idlen, key)) { + goto err; + } + // debugPrint; + printf("Z: "); + for (int i=0; i bn */ + sm3_final(buf, &c); + // debugPrint; + printf("e: "); + for (int i=0; i bn */ + if (!EC_POINT_mul(kG, G, k, key->group)) { + return 0; + } + // debugPrint; + printf("[k]G: (%s, \n\t%s)\n", BN_bn2hex(kG->X), BN_bn2hex(kG->Y)); + // debugPrint; + x = kG->X; + // debugPrint; + /* A5: r = (e + x1) % n, if r=0 or r+k=n, goto A3 */ + if (!BN_mod_add(r, e, x, n, ctx)) { + SM2ERR("sm2_sign(): internal error!\n"); + } + printf("r: %s\n", BN_bn2hex(r)); + // debugPrint; + if (BN_is_zero(r)) continue; + // debugPrint; + if (!BN_add(rk, r, k)) { + SM2ERR("sm2_sign(): internal error!\n"); + } + // debugPrint; + if (BN_cmp(rk, n) == 0) continue; + // debugPrint; + /* A6: s = ((1+dA)^-1 \times (k- r \times dA)) % n, if s=0, goto A3 */ + // wait for dA + // abort(); + // debugPrint; + if (!BN_add(s, dA, BN_value_one()) + || !BN_mod_inverse(s, s, n, ctx) + || !printf("(1+dA)^-1: %s\n", BN_bn2hex(s)) + || !BN_mod_mul(tmp, dA, r, n, ctx) + || !BN_sub(tmp, k, tmp) + || !BN_mod_mul(s, s, tmp, n, ctx)) { + SM2ERR("sm2_sign(): internal error!\n"); + } + // debugPrint; + printf("s: %s\n", BN_bn2hex(s)); + if (BN_is_zero(s)) continue; + // debugPrint; + /* A7: r,s -> byte/u8[], sig = (r, s) */ + // sig->r = r; + // sig->s = s; + break; + } + err: + done: + BN_free(e); + BN_CTX_free(ctx); + // ecpoint free + // abort(); + printf("(r, s) = (%s,\n\t\t%s)\n", BN_bn2hex(r), BN_bn2hex(s)); + return sig; +} + +/* (2) Sign Verify */ + +/** SM2 signature verification. + * \param msg massage for signing. + * \param msglen byte length of msg. + * \param id identifier of user A. + * \param idlen byte length of id. + * \param key key of user A. + * \param sig signature to be verified. + * \return 1 on success and 0 if an error occurred. + */ +int sm2_sig_verify(const uint8 *msg, const size_t msglen, const uint8 *id, const size_t idlen, const EC_KEY *key, const SM2_SIG *sig) { + SM3_CTX c; + sm3_init(&c); + BN_CTX *ctx; + const BIGNUM *r, *s, *n; + BIGNUM *e, *t, *R; + const EC_POINT *G, *PA; + EC_POINT *sG, *tPA, *tmp_point; + uint8 *buf; + r = sig->r; + s = sig->s; + n = key->group->order; + G = key->group->generator; + PA = key->pub_key; + ctx = BN_CTX_new(); + if (ctx == NULL) { + goto err; + } + BN_CTX_start(ctx); + e = BN_CTX_get(ctx); + t = BN_CTX_get(ctx); + R = BN_CTX_get(ctx); + sG = EC_POINT_new(); + tPA = EC_POINT_new(); + tmp_point = EC_POINT_new(); + if (R == NULL || tmp_point == NULL) { + goto err; + } + buf = malloc(SM3_DIGEST_LENGTH); + /* B1: check r' \in [1, n-1], otherwise goto BERR */ + /* B2: check s' \in [1, n-1], otherwise goto BERR */ + if (BN_cmp(r, BN_value_one()) < 0 + || BN_cmp(s, BN_value_one()) < 0 + || BN_cmp(n, r) <= 0 + || BN_cmp(n, s) <= 0) { + goto err; + } + printf("(r, s) = (%s,\n\t\t%s)\n", BN_bn2hex(r), BN_bn2hex(s)); + /* B3: \bar{M'} = ZA || M' */ + if (!sm2_compute_z(buf, id, idlen, key)) { + goto err; + } + if (!sm3_update(&c, buf, SM3_DIGEST_LENGTH) + || !sm3_update(&c, msg, msglen)) { + SM2ERR("sm2_sign(): compute e error!\n"); + } + /* B4: e' = SM3(\bar{M'}), e -> bn */ + // debugPrint; + sm3_final(buf, &c); + // debugPrint; + printf("e: "); + for (int i=0; i bn; t = (r'+s')%n, if t=0, goto BERR */ + if (!BN_mod_add_quick(t, r, s, n) + || !printf("t: %s\n", BN_bn2hex(t)) + || BN_is_zero(t)) { + goto err; + } + /* B6: (x1', y1') = [s']G + [t]PA */ + if (!EC_POINT_mul(sG, G, s, key->group) + || !printf("sG: (%s,\n\t%s)\n", BN_bn2hex(sG->X), BN_bn2hex(sG->Y)) + || !EC_POINT_mul(tPA, PA, t, key->group) + || !printf("tPA: (%s,\n\t%s)\n", BN_bn2hex(tPA->X), BN_bn2hex(tPA->Y)) + || !EC_POINT_add(tmp_point, sG, tPA, key->group)) { + goto err; + } + // debugPrint; + printf("tmp: (%s,\n\t%s)\n", BN_bn2hex(tmp_point->X), BN_bn2hex(tmp_point->Y)); + // debugPrint; + /* B7: x1' -> bn; R = (e' + x1')%n, return R=r' */ + if (!BN_mod_add_quick(R, e, tmp_point->X, n)) { + goto err; + } + // debugPrint; + printf("R: %s\n", BN_bn2hex(R)); + // debugPrint; + if (BN_cmp(R, r) == 0) { + return 1; + } + // debugPrint; + err: + BN_CTX_end(ctx); + /* BERR: not pass, return 0 */ + return 0; +} diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/sm2/sm2_sign_test.c" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/sm2/sm2_sign_test.c" new file mode 100644 index 0000000000000000000000000000000000000000..04da92462717beeda262a2af9965a1a23f9c31e2 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/sm2/sm2_sign_test.c" @@ -0,0 +1,91 @@ +#define GBT32918_2_2016_A2_TEST /* For GB/T 32918.2-2016 A.2 test */ +#include "sm2.h" +#include "sm2_tests.h" + +const uint8 ID_A[] = "ALICE123@YAHOO.COM"; +const uint8 M[] = "message digest"; +const uint8 Pp[] = { + 0x85, 0x42, 0xD6, 0x9E, + 0x4C, 0x04, 0x4F, 0x18, + 0xE8, 0xB9, 0x24, 0x35, + 0xBF, 0x6F, 0xF7, 0xDE, + 0x45, 0x72, 0x83, 0x91, + 0x5C, 0x45, 0x51, 0x7D, + 0x72, 0x2E, 0xDB, 0x8B, + 0x08, 0xF1, 0xDF, 0xC3, +}; +const uint8 Pa[] = { + 0x78, 0x79, 0x68, 0xB4, + 0xFA, 0x32, 0xC3, 0xFD, + 0x24, 0x17, 0x84, 0x2E, + 0x73, 0xBB, 0xFE, 0xFF, + 0x2F, 0x3C, 0x84, 0x8B, + 0x68, 0x31, 0xD7, 0xE0, + 0xEC, 0x65, 0x22, 0x8B, + 0x39, 0x37, 0xE4, 0x98, +}; +const uint8 Pb[] = { + 0x63, 0xE4, 0xC6, 0xD3, + 0xB2, 0x3B, 0x0C, 0x84, + 0x9C, 0xF8, 0x42, 0x41, + 0x48, 0x4B, 0xFE, 0x48, + 0xF6, 0x1D, 0x59, 0xA5, + 0xB1, 0x6B, 0xA0, 0x6E, + 0x6E, 0x12, 0xD1, 0xDA, + 0x27, 0xC5, 0x24, 0x9A, +}; +const uint8 PXG[] = { + 0x42, 0x1D, 0xEB, 0xD6, + 0x1B, 0x62, 0xEA, 0xB6, + 0x74, 0x64, 0x34, 0xEB, + 0xC3, 0xCC, 0x31, 0x5E, + 0x32, 0x22, 0x0B, 0x3B, + 0xAD, 0xD5, 0x0B, 0xDC, + 0x4C, 0x4E, 0x6C, 0x14, + 0x7F, 0xED, 0xD4, 0x3D, +}; +const uint8 PYG[] = { + 0x06, 0x80, 0x51, 0x2B, + 0xCB, 0xB4, 0x2C, 0x07, + 0xD4, 0x73, 0x49, 0xD2, + 0x15, 0x3B, 0x70, 0xC4, + 0xE5, 0xD7, 0xFD, 0xFC, + 0xBF, 0xA3, 0x6E, 0xA1, + 0xA8, 0x58, 0x41, 0xB9, + 0xE4, 0x6E, 0x09, 0xA2, +}; +const uint8 Pn[] = { + 0x85, 0x42, 0xD6, 0x9E, + 0x4C, 0x04, 0x4F, 0x18, + 0xE8, 0xB9, 0x24, 0x35, + 0xBF, 0x6F, 0xF7, 0xDD, + 0x29, 0x77, 0x20, 0x63, + 0x04, 0x85, 0x62, 0x8D, + 0x5A, 0xE7, 0x4E, 0xE7, + 0xC3, 0x2E, 0x79, 0xB7, +}; +const uint8 PdA[] = { + 0x12, 0x8B, 0x2F, 0xA8, + 0xBD, 0x43, 0x3C, 0x6C, + 0x06, 0x8C, 0x8D, 0x80, + 0x3D, 0xFF, 0x79, 0x79, + 0x2A, 0x51, 0x9A, 0x55, + 0x17, 0x1B, 0x1B, 0x65, + 0x0C, 0x23, 0x66, 0x1D, + 0x15, 0x89, 0x72, 0x63, +}; +const uint8 Pk[] = { + 0x6C, 0xB2, 0x8D, 0x99, + 0x38, 0x5C, 0x17, 0x5C, + 0x94, 0xF9, 0x4E, 0x93, + 0x48, 0x17, 0x66, 0x3F, + 0xC1, 0x76, 0xD9, 0x25, + 0xDD, 0x72, 0xB7, 0x27, + 0x26, 0x0D, 0xBA, 0xAE, + 0x1F, 0xB2, 0xF9, 0x6F, +}; + +int main() { + sm2signtest(256>>3, M, ID_A, Pp, Pa, Pb, Pn, PXG, PYG, PdA, Pk); + return 0; +} \ No newline at end of file diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/sm2/sm2_tests.h" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/sm2/sm2_tests.h" new file mode 100644 index 0000000000000000000000000000000000000000..730d75cbec11b382f53f1595f907c18ee0c02023 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/sm2/sm2_tests.h" @@ -0,0 +1,236 @@ +#ifndef SECGEAR_SM2_TESTS_H +#define SECGEAR_SM2_TESTS_H + +#include "sm2_sign.h" +#include "sm2_exchange.h" +#include "sm2_crypt.h" + +void sm2signtest(const int p_bytes, const uint8 *_M, const uint8 *_ID_A, const uint8 *_p, const uint8 *_a, const uint8 *_b, const uint8 *_n, const uint8 *_XG, const uint8 *_YG, const uint8 *_dA, const uint8 *_k) { + printf("-----------Sign test start--------------\n"); + /* A.3 is not supported */ + int _w; + printf("ID_A: "); + uint16 entlenC = strlen((char *)_ID_A); + uint16 entlA = entlenC*8; + for (int i=0; iX), BN_bn2hex(G->Y)); + _w = EC_GROUP_set_param(group, p, a, b, n, BN_value_one(), G); + _w = EC_KEY_set_group(userA, group); + _w = EC_KEY_set_priv_key(userA, dA); + printf("userA.dA: %s\n", BN_bn2hex(userA->priv_key)); + _w = EC_KEY_generate_pub_key(userA); + printf("userA.PA: (%s, \n\t%s)\n", BN_bn2hex(userA->pub_key->X), BN_bn2hex(userA->pub_key->Y)); + _w = EC_POINT_mul(kG, G, k, group); + printf("kG: (%s, \n\t%s)\n", BN_bn2hex(kG->X), BN_bn2hex(kG->Y)); + SM2_SIG *sig; + printf("------Sign Generation Start-----\n"); + sig = sm2_sig_gen(_M, strlen((char *)_M), _ID_A, strlen((char *)_ID_A), userA); + printf("------Sign Generation End-----\n"); + printf("(r, s) = (%s,\n\t\t%s)\n", BN_bn2hex(sig->r), BN_bn2hex(sig->s)); + printf("--------Sign Verify Start-------\n"); + _w = sm2_sig_verify(_M, strlen((char *)_M), _ID_A, strlen((char *)_ID_A), userA, sig); + printf("--------Sign Verify End-------\n"); + printf("Verify: %d\n", _w); + printf("-----------Sign test end--------------\n"); +} + +/* h = 1 by default */ +void sm2exchangetest(const int p_bytes, const uint8 *_ID_A, const uint8 *_ID_B, const uint8 *_p, const uint8 *_a, const uint8 *_b, const uint8 *_n, const uint8 *_XG, const uint8 *_YG, const uint8 *_dA, const uint8 *_dB, const uint64 klen) { + printf("-----------Exchange test start--------------\n"); + /* A.3 is not supported */ + int _w; + printf("ID_A: "); + uint16 entlenAC = strlen((char *)_ID_A); + uint16 entlA = entlenAC*8; + for (int i=0; iX), BN_bn2hex(G->Y)); + _w = EC_GROUP_set_param(group, p, a, b, n, h, G); + _w = EC_KEY_set_group(userA, group); + _w = EC_KEY_set_priv_key(userA, dA); + printf("userA.dA: %s\n", BN_bn2hex(userA->priv_key)); + _w = EC_KEY_generate_pub_key(userA); + printf("userA.PA: (%s, \n\t%s)\n", BN_bn2hex(userA->pub_key->X), BN_bn2hex(userA->pub_key->Y)); + _w = EC_KEY_set_group(userB, group); + _w = EC_KEY_set_priv_key(userB, dB); + printf("userB.dB: %s\n", BN_bn2hex(userB->priv_key)); + _w = EC_KEY_generate_pub_key(userB); + printf("userB.PB: (%s, \n\t%s)\n", BN_bn2hex(userB->pub_key->X), BN_bn2hex(userB->pub_key->Y)); + _w = sm2_compute_z(ZA, _ID_A, strlen((char *)_ID_A), userA); + printf("ZA: "); + for (int i=0; iX), BN_bn2hex(G->Y)); + MlenCBLOCK = strlen((char *)M); + printf("M: %s\n", M); + printf("Message: "); + for (int i=0; ipriv_key)); + _w = EC_KEY_generate_pub_key(userB); + printf("userB.PB: (%s, \n\t%s)\n", BN_bn2hex(userB->pub_key->X), BN_bn2hex(userB->pub_key->Y)); + klen = MlenCBLOCK * 8; + pwlen = (p_bytes << 1 | 1) + MlenCBLOCK + SM3_DIGEST_LENGTH; + pw = malloc(pwlen); + _w = sm2_encrypt(pw, M, klen, userB); + printf("encrypt = %d\n", _w); + printf("pw: "); + for (int i=0; i> 3; +const uint8 sign_ID_A[] = "1234567812345678"; +const uint8 sign_M[] = "message digest"; +const uint8 sign_dA[] = { + 0x39, 0x45, 0x20, 0x8F, + 0x7B, 0x21, 0x44, 0xB1, + 0x3F, 0x36, 0xE3, 0x8A, + 0xC6, 0xD3, 0x9F, 0x95, + 0x88, 0x93, 0x93, 0x69, + 0x28, 0x60, 0xB5, 0x1A, + 0x42, 0xFB, 0x81, 0xEF, + 0x4D, 0xF7, 0xC5, 0xB8, +}; +const uint8 sign_k[] = { + 0x59, 0x27, 0x6E, 0x27, + 0xD5, 0x06, 0x86, 0x1A, + 0x16, 0x68, 0x0F, 0x3A, + 0xD9, 0xC0, 0x2D, 0xCC, + 0xEF, 0x3C, 0xC1, 0xFA, + 0x3C, 0xDB, 0xE4, 0xCE, + 0x6D, 0x54, 0xB8, 0x0D, + 0xEA, 0xC1, 0xBC, 0x21, +}; + +const uint8 exchange_ID_A[] = "1234567812345678"; +const uint8 exchange_ID_B[] = "1234567812345678"; +const uint8 exchange_dA[] = { + 0x81, 0xEB, 0x26, 0xE9, + 0x41, 0xBB, 0x5A, 0xF1, + 0x6D, 0xF1, 0x16, 0x49, + 0x5F, 0x90, 0x69, 0x52, + 0x72, 0xAE, 0x2C, 0xD6, + 0x3D, 0x6C, 0x4A, 0xE1, + 0x67, 0x84, 0x18, 0xBE, + 0x48, 0x23, 0x00, 0x29, +}; +const uint8 exchange_dB[] = { + 0x78, 0x51, 0x29, 0x91, + 0x7D, 0x45, 0xA9, 0xEA, + 0x54, 0x37, 0xA5, 0x93, + 0x56, 0xB8, 0x23, 0x38, + 0xEA, 0xAD, 0xDA, 0x6C, + 0xEB, 0x19, 0x90, 0x88, + 0xF1, 0x4A, 0xE1, 0x0D, + 0xEF, 0xA2, 0x29, 0xB5, +}; +const uint64 exchange_klen = 128; + +const uint8 crypt_M[] = "encryption standard"; +const uint8 crypt_dB[] = { + 0x39, 0x45, 0x20, 0x8F, + 0x7B, 0x21, 0x44, 0xB1, + 0x3F, 0x36, 0xE3, 0x8A, + 0xC6, 0xD3, 0x9F, 0x95, + 0x88, 0x93, 0x93, 0x69, + 0x28, 0x60, 0xB5, 0x1A, + 0x42, 0xFB, 0x81, 0xEF, + 0x4D, 0xF7, 0xC5, 0xB8, +}; + +int main(){ + printf("A.2 start: \n"); + sm2signtest(SM2_p_bytes, sign_M, sign_ID_A, SM2_P, SM2_A, SM2_B, SM2_N, SM2_XG, SM2_YG, sign_dA, sign_k); + printf("A.2 end: \n\n\n"); + /* PASSED */ + + printf("B.2 start: \n"); + sm2exchangetest(SM2_p_bytes, exchange_ID_A, exchange_ID_B, SM2_P, SM2_A, SM2_B, SM2_N, SM2_XG, SM2_YG, exchange_dA, exchange_dB, exchange_klen); + printf("B.2 end: \n\n\n"); + /* PASSED */ + + printf("C.2 start: \n"); + sm2crypttest(SM2_p_bytes, crypt_M, SM2_P, SM2_A, SM2_B, SM2_N, SM2_XG, SM2_YG, crypt_dB); + printf("C.2 end: \n\n\n"); + /* PASSED */ + return 0; +} \ No newline at end of file diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/sm3/sm3.h" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/sm3/sm3.h" new file mode 100644 index 0000000000000000000000000000000000000000..195d6790d2f9b57a6a27ab84b5cbf88d4aad37a3 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/sm3/sm3.h" @@ -0,0 +1,183 @@ +#ifndef SECGEAR_SM3_H +#define SECGEAR_SM3_H + +/* + * Ref: + * GB/T 32905-2016 + */ + +#include +#include "../include/common_utils.h" + +#define SM3_DIGEST_LENGTH 32 +#define SM3_WORD uint32 + +#define SM3_CBLOCK 64 +#define SM3_LBLOCK (SM3_CBLOCK/4) + +typedef struct SM3state_st { + SM3_WORD A, B, C, D, E, F, G, H; + SM3_WORD Nl, Nh; + SM3_WORD data[SM3_LBLOCK]; + unsigned int num; +} SM3_CTX; + +#define P0(X) (X ^ ROTATE(X, 9) ^ ROTATE(X, 17)) +#define P1(X) (X ^ ROTATE(X, 15) ^ ROTATE(X, 23)) + +#define FF(J,X,Y,Z) (J<16 ? (X ^ Y ^ Z) : ((X & Y) | (X & Z) | (Y & Z))) +#define GG(J,X,Y,Z) (J<16 ? (X ^ Y ^ Z) : ((X & Y) | ((~X) & Z))) + +#define T(J) (J<16 ? 0x79cc4519 : 0x7a879d8a) + +#define EXPAND(W0,W7,W13,W3,W10) \ + (P1(W0 ^ W7 ^ ROTATE(W13, 15)) ^ ROTATE(W3, 7) ^ W10) + +#define SM3_A 0x7380166fUL +#define SM3_B 0x4914b2b9UL +#define SM3_C 0x172442d7UL +#define SM3_D 0xda8a0600UL +#define SM3_E 0xa96f30bcUL +#define SM3_F 0x163138aaUL +#define SM3_G 0xe38dee4dUL +#define SM3_H 0xb0fb0e4eUL + +#define SM3_MAKE_STRING(c, s) \ + do { \ + unsigned long ll; \ + ll=(c)->A; (void)HOST_l2c(ll, (s)); \ + ll=(c)->B; (void)HOST_l2c(ll, (s)); \ + ll=(c)->C; (void)HOST_l2c(ll, (s)); \ + ll=(c)->D; (void)HOST_l2c(ll, (s)); \ + ll=(c)->E; (void)HOST_l2c(ll, (s)); \ + ll=(c)->F; (void)HOST_l2c(ll, (s)); \ + ll=(c)->G; (void)HOST_l2c(ll, (s)); \ + ll=(c)->H; (void)HOST_l2c(ll, (s)); \ + } while (0) + +void sm3_block_data_order(SM3_CTX *ctx, const void *p, size_t num) { + const unsigned char *data = p; + SM3_WORD A, B, C, D, E, F, G, H; + SM3_WORD w[68]={}, ww[64]={}; + while (num--) { + A = ctx->A; + B = ctx->B; + C = ctx->C; + D = ctx->D; + E = ctx->E; + F = ctx->F; + G = ctx->G; + H = ctx->H; + for (int j = 0; j < 16; ++j) { + HOST_c2l(data, w[j]); + } + for (int j = 16; j < 68; ++j) { + w[j] = EXPAND(w[j-16], w[j-9], w[j-3], w[j-13], w[j-6]); + } + for (int j = 0; j < 64; ++j) { + ww[j] = w[j] ^ w[j+4]; + } + for (int j = 0; j < 64; j++) { + SM3_WORD SS1, SS2, TT1, TT2; + SS1 = ROTATE(ROTATE(A, 12) + E + ROTATE(T(j), j&31), 7); + SS2 = SS1 ^ ROTATE(A, 12); + TT1 = FF(j, A, B, C) + D + SS2 + ww[j]; + TT2 = GG(j, E, F, G) + H + SS1 + w[j]; + D = C; + C = ROTATE(B,9); + B = A; + A = TT1; + H = G; + G = ROTATE(F, 19); + F = E; + E = P0(TT2); + } + ctx->A ^= A; + ctx->B ^= B; + ctx->C ^= C; + ctx->D ^= D; + ctx->E ^= E; + ctx->F ^= F; + ctx->G ^= G; + ctx->H ^= H; + } +} + +int sm3_init(SM3_CTX *c) { + memset(c, 0, sizeof(*c)); + c->A = SM3_A; + c->B = SM3_B; + c->C = SM3_C; + c->D = SM3_D; + c->E = SM3_E; + c->F = SM3_F; + c->G = SM3_G; + c->H = SM3_H; + return 1; +} + +int sm3_update(SM3_CTX *c, const void *data_, size_t len) { + const unsigned char *data = data_; + unsigned char *p; + SM3_WORD l; + size_t n; + if (len == 0) return 1; + l = (c->Nl + (((SM3_WORD) len) << 3)) & 0xffffffffUL; + if (l < c->Nl) c->Nh++; + c->Nh += (SM3_WORD) (len >> 29); + c->Nl = l; + n = c->num; + if (n != 0) { + p = (unsigned char *)c->data; + if (len >= SM3_CBLOCK || len + n >= SM3_CBLOCK) { + memcpy(p + n, data, SM3_CBLOCK - n); + sm3_block_data_order(c, p, 1); + n = SM3_CBLOCK - n; + data += n; + len -= n; + c->num = 0; + memset(p, 0, SM3_CBLOCK); + } + else { + memcpy(p + n, data, len); + c->num += (unsigned int)len; + return 1; + } + } + n = len / SM3_CBLOCK; + if (n > 0) { + sm3_block_data_order(c, data, n); + n *= SM3_CBLOCK; + data += n; + len -= n; + } + if (len != 0) { + p = (unsigned char *)c->data; + c->num = (unsigned int)len; + memcpy(p, data, len); + } + return 1; +} + +int sm3_final(unsigned char *md, SM3_CTX *c) { + unsigned char *p = (unsigned char *)c->data; + size_t n = c->num; + p[n] = 0x80; + n++; + if (n > (SM3_CBLOCK - 8)) { + memset(p + n, 0, SM3_CBLOCK - n); + n = 0; + sm3_block_data_order(c, p, 1); + } + memset(p + n, 0, SM3_CBLOCK - 8 - n); + p += SM3_CBLOCK - 8; + (void)HOST_l2c(c->Nh, p); + (void)HOST_l2c(c->Nl, p); + p -= SM3_CBLOCK; + sm3_block_data_order(c, p, 1); + c->num = 0; + SM3_MAKE_STRING(c, md); + return 1; +} + +#endif \ No newline at end of file diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/sm3/sm3test.c" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/sm3/sm3test.c" new file mode 100644 index 0000000000000000000000000000000000000000..00d67544cd11c59d1bcb028ee607d725e09d405b --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/sm3/sm3test.c" @@ -0,0 +1,53 @@ +#include "sm3.h" +#include +#include + +uint8 p1[] = {0x61, 0x62, 0x63}; +uint8 p2[] = { + 0x61, 0x62, 0x63, 0x64, + 0x61, 0x62, 0x63, 0x64, + 0x61, 0x62, 0x63, 0x64, + 0x61, 0x62, 0x63, 0x64, + 0x61, 0x62, 0x63, 0x64, + 0x61, 0x62, 0x63, 0x64, + 0x61, 0x62, 0x63, 0x64, + 0x61, 0x62, 0x63, 0x64, + 0x61, 0x62, 0x63, 0x64, + 0x61, 0x62, 0x63, 0x64, + 0x61, 0x62, 0x63, 0x64, + 0x61, 0x62, 0x63, 0x64, + 0x61, 0x62, 0x63, 0x64, + 0x61, 0x62, 0x63, 0x64, + 0x61, 0x62, 0x63, 0x64, + 0x61, 0x62, 0x63, 0x64, +}; + +void sm3test(uint8 *md, size_t len, uint8 *res) { + SM3_CTX c; + sm3_init(&c); + sm3_update(&c, md, len); + sm3_final(res, &c); +} + +int main(){ + uint8 res[32]={}; + printf("Test 1: \n"); + // printf("len = %lu\n",sizeof (p1)); + sm3test(p1, 3, res); + for(int i=0;i<32;i++) { + printf("%02X",res[i]); + } + putchar('\n'); + printf("Test 2: \n"); + sm3test(p2, 64, res); + for(int i=0;i<32;i++) { + printf("%02x",res[i]); + } + putchar('\n'); + return 0; +} + +// Test 1: +// 66C7F0F462EEEDD9D1F2D46BDC10E4E24167C4875CF2F7A2297DA02B8F4BA8E0 +// Test 2: +// debe9ff92275b8a138604889c18e5a4d6fdb70e5387e5765293dcba39c0c5732 diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/sm4/sm4.h" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/sm4/sm4.h" new file mode 100644 index 0000000000000000000000000000000000000000..8474251aff1317c5f8f5f7ba15e3d51678b65b0d --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/sm4/sm4.h" @@ -0,0 +1,107 @@ +#ifndef SECGEAR_SM4_H +#define SECGEAR_SM4_H + +/* + * Ref: + * GB/T 32907-2016 + */ + +#include +#include "../include/common_utils.h" + +#define ENCRYPT 0 +#define DECRYPT 1 +const uint8 Sbox[256] = { + 0xD6, 0x90, 0xE9, 0xFE, 0xCC, 0xE1, 0x3D, 0xB7, 0x16, 0xB6, 0x14, 0xC2, 0x28, 0xFB, 0x2C, 0x05, + 0x2B, 0x67, 0x9A, 0x76, 0x2A, 0xBE, 0x04, 0xC3, 0xAA, 0x44, 0x13, 0x26, 0x49, 0x86, 0x06, 0x99, + 0x9C, 0x42, 0x50, 0xF4, 0x91, 0xEF, 0x98, 0x7A, 0x33, 0x54, 0x0B, 0x43, 0xED, 0xCF, 0xAC, 0x62, + 0xE4, 0xB3, 0x1C, 0xA9, 0xC9, 0x08, 0xE8, 0x95, 0x80, 0xDF, 0x94, 0xFA, 0x75, 0x8F, 0x3F, 0xA6, + 0x47, 0x07, 0xA7, 0xFC, 0xF3, 0x73, 0x17, 0xBA, 0x83, 0x59, 0x3C, 0x19, 0xE6, 0x85, 0x4F, 0xA8, + 0x68, 0x6B, 0x81, 0xB2, 0x71, 0x64, 0xDA, 0x8B, 0xF8, 0xEB, 0x0F, 0x4B, 0x70, 0x56, 0x9D, 0x35, + 0x1E, 0x24, 0x0E, 0x5E, 0x63, 0x58, 0xD1, 0xA2, 0x25, 0x22, 0x7C, 0x3B, 0x01, 0x21, 0x78, 0x87, + 0xD4, 0x00, 0x46, 0x57, 0x9F, 0xD3, 0x27, 0x52, 0x4C, 0x36, 0x02, 0xE7, 0xA0, 0xC4, 0xC8, 0x9E, + 0xEA, 0xBF, 0x8A, 0xD2, 0x40, 0xC7, 0x38, 0xB5, 0xA3, 0xF7, 0xF2, 0xCE, 0xF9, 0x61, 0x15, 0xA1, + 0xE0, 0xAE, 0x5D, 0xA4, 0x9B, 0x34, 0x1A, 0x55, 0xAD, 0x93, 0x32, 0x30, 0xF5, 0x8C, 0xB1, 0xE3, + 0x1D, 0xF6, 0xE2, 0x2E, 0x82, 0x66, 0xCA, 0x60, 0xC0, 0x29, 0x23, 0xAB, 0x0D, 0x53, 0x4E, 0x6F, + 0xD5, 0xDB, 0x37, 0x45, 0xDE, 0xFD, 0x8E, 0x2F, 0x03, 0xFF, 0x6A, 0x72, 0x6D, 0x6C, 0x5B, 0x51, + 0x8D, 0x1B, 0xAF, 0x92, 0xBB, 0xDD, 0xBC, 0x7F, 0x11, 0xD9, 0x5C, 0x41, 0x1F, 0x10, 0x5A, 0xD8, + 0x0A, 0xC1, 0x31, 0x88, 0xA5, 0xCD, 0x7B, 0xBD, 0x2D, 0x74, 0xD0, 0x12, 0xB8, 0xE5, 0xB4, 0xB0, + 0x89, 0x69, 0x97, 0x4A, 0x0C, 0x96, 0x77, 0x7E, 0x65, 0xB9, 0xF1, 0x09, 0xC5, 0x6E, 0xC6, 0x84, + 0x18, 0xF0, 0x7D, 0xEC, 0x3A, 0xDC, 0x4D, 0x20, 0x79, 0xEE, 0x5F, 0x3E, 0xD7, 0xCB, 0x39, 0x48, +}; +const uint32 FK[4] = { + 0xA3B1BAC6, 0x56AA3350, 0x677D9197, 0xB27022DC, +}; +const uint32 CK[32] = { + 0x00070E15, 0x1C232A31, 0x383F464D, 0x545B6269, + 0x70777E85, 0x8C939AA1, 0xA8AFB6BD, 0xC4CBD2D9, + 0xE0E7EEF5, 0xFC030A11, 0x181F262D, 0x343B4249, + 0x50575E65, 0x6C737A81, 0x888F969D, 0xA4ABB2B9, + 0xC0C7CED5, 0xDCE3EAF1, 0xF8FF060D, 0x141B2229, + 0x30373E45, 0x4C535A61, 0x686F767D, 0x848B9299, + 0xA0A7AEB5, 0xBCC3CAD1, 0xD8DFE6ED, 0xF4FB0209, + 0x10171E25, 0x2C333A41, 0x484F565D, 0x646B7279, +}; +#define L(B) ((B) ^ (ROTATE((B), 2)) ^ (ROTATE((B), 10)) ^ (ROTATE((B), 18)) ^ (ROTATE((B), 24))) +#define LP(B) ((B) ^ (ROTATE((B), 13)) ^ (ROTATE((B), 23))) +#define S(X) (((uint32)Sbox[((X) >> 24) & 0xFF] << 24) | \ + ((uint32)Sbox[((X) >> 16) & 0xFF] << 16) | \ + ((uint32)Sbox[((X) >> 8) & 0xFF] << 8) | \ + ((uint32)Sbox[(X) & 0xFF])) +#define T(X) L(S(X)) +#define TP(X) LP(S(X)) +#define F(X0, X1, X2, X3, rk) ((X0) ^ T((X1) ^ (X2) ^ (X3) ^ (rk))) + +typedef struct sm4_ctx_st { + uint32 M[4]; + uint32 rk[32]; +} SM4_CTX; + +int sm4_init(SM4_CTX *ctx, const uint32 *M, const uint32 *MK) { + memset(ctx, 0, sizeof(*ctx)); + uint32 K[36]; + memset(K, 0, sizeof(K)); + int i; + for (i = 0; i < 4; ++i) { + K[i] = MK[i] ^ FK[i]; + ctx->M[i] = M[i]; + } + for (i = 0; i < 32; ++i) { + ctx->rk[i] = K[i+4] = K[i] ^ TP(K[i+1] ^ K[i+2] ^ K[i+3] ^ CK[i]); + } + return 1; +} + +int sm4_round(SM4_CTX *ctx, int crypt_flag) { + int offset = (crypt_flag ? 31 : 0), i; + uint32 X[36]; + memset(X, 0, sizeof(X)); + for (i = 0; i < 4; ++i) { + X[i] = ctx->M[i]; + } + for (i = 0; i < 32; ++i) { + X[i+4] = F(X[i], X[i+1], X[i+2], X[i+3], ctx->rk[i^offset]); + } + ctx->M[0] = X[35]; + ctx->M[1] = X[34]; + ctx->M[2] = X[33]; + ctx->M[3] = X[32]; + return 1; +} + +int sm4_encrypt(SM4_CTX *ctx) { + return sm4_round(ctx, ENCRYPT); +} + +int sm4_decrypt(SM4_CTX *ctx) { + return sm4_round(ctx, DECRYPT); +} + +int sm4_final(uint32 *out, SM4_CTX *ctx) { + if (!memcpy(out, ctx->M, sizeof(ctx->M))) { + return 0; + } + return 1; +} + +#endif \ No newline at end of file diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/sm4/sm4test.c" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/sm4/sm4test.c" new file mode 100644 index 0000000000000000000000000000000000000000..b951234cf4d64a8f0ec3bdf15b4f51c58f0cbcc2 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/sm4/sm4test.c" @@ -0,0 +1,42 @@ +#include +#include "sm4.h" + +const uint32 message[] = { + 0x01234567, 0x89ABCDEF, + 0xFEDCBA98, 0x76543210, +}; +const uint32 key[] = { + 0x01234567, 0x89ABCDEF, + 0xFEDCBA98, 0x76543210, +}; +uint32 res[4]; + +void sm4test(const uint32 *M, const uint32 *MK, const int rounds) { + printf("---------SM4 encrypt test (%d)--------------\n", rounds); + SM4_CTX c; + sm4_init(&c, M, MK); + for (int i=0; i +#include "../include/common_utils.h" +#include "../include/ec.h" +#include "../sm3/sm3.h" +#include "../sm4/sm4.h" +/* + * export C_INCLUDE_PATH="/PATH/TO/OPENSSL/include" + * export CPLUS_INCLUDE_PATH="/PATH/TO/OPENSSL/include" + * export LIBRARY_PATH="/PATH/TO/OPENSSL/lib" + */ + +#define SM9_PARAM_CBLOCK 32 +#define SM9_PARAM_LBLOCK (SM9_PARAM_CBLOCK/4) + +const uint8 SM9_T[SM9_PARAM_CBLOCK] = { + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x60, 0x00, 0x00, 0x00, + 0x00, 0x58, 0xF9, 0x8A, +}; +const uint8 SM9_TR[] = {}; +const uint8 SM9_Q[SM9_PARAM_CBLOCK] = { + 0xB6, 0x40, 0x00, 0x00, + 0x02, 0xA3, 0xA6, 0xF1, + 0xD6, 0x03, 0xAB, 0x4F, + 0xF5, 0x8E, 0xC7, 0x45, + 0x21, 0xF2, 0x93, 0x4B, + 0x1A, 0x7A, 0xEE, 0xDB, + 0xE5, 0x6F, 0x9B, 0x27, + 0xE3, 0x51, 0x45, 0x7D, +}; +const uint8 SM9_A[SM9_PARAM_CBLOCK] = { + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, +}; +const uint8 SM9_B[SM9_PARAM_CBLOCK] = { + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x05, +}; +const uint8 SM9_N[SM9_PARAM_CBLOCK] = { + 0xB6, 0x40, 0x00, 0x00, + 0x02, 0xA3, 0xA6, 0xF1, + 0xD6, 0x03, 0xAB, 0x4F, + 0xF5, 0x8E, 0xC7, 0x44, + 0x49, 0xF2, 0x93, 0x4B, + 0x18, 0xEA, 0x8B, 0xEE, + 0xE5, 0x6E, 0xE1, 0x9C, + 0xD6, 0x9E, 0xCF, 0x25, +}; +const uint8 SM9_CF = 1; +const uint8 SM9_K = 12; +const uint8 SM9_D1 = 1; +const uint8 SM9_D2 = 2; +const uint8 SM9_CID = 0x12; +const uint8 SM9_XP1[SM9_PARAM_CBLOCK] = { + 0x93, 0xDE, 0x05, 0x1D, + 0x62, 0xBF, 0x71, 0x8F, + 0xF5, 0xED, 0x07, 0x04, + 0x48, 0x7D, 0x01, 0xD6, + 0xE1, 0xE4, 0x08, 0x69, + 0x09, 0xDC, 0x32, 0x80, + 0xE8, 0xC4, 0xE4, 0x81, + 0x7C, 0x66, 0xDD, 0xDD, +}; +const uint8 SM9_YP1[SM9_PARAM_CBLOCK] = { + 0x21, 0xFE, 0x8D, 0xDA, + 0x4F, 0x21, 0xE6, 0x07, + 0x63, 0x10, 0x65, 0x12, + 0x5C, 0x39, 0x5B, 0xBC, + 0x1C, 0x1C, 0x00, 0xCB, + 0xFA, 0x60, 0x24, 0x35, + 0x0C, 0x46, 0x4C, 0xD7, + 0x0A, 0x3E, 0xA6, 0x16, +}; +const uint8 SM9_XP21[SM9_PARAM_CBLOCK] = { + 0x85, 0xAE, 0xF3, 0xD0, + 0x78, 0x64, 0x0C, 0x98, + 0x59, 0x7B, 0x60, 0x27, + 0xB4, 0x41, 0xA0, 0x1F, + 0xF1, 0xDD, 0x2C, 0x19, + 0x0F, 0x5E, 0x93, 0xC4, + 0x54, 0x80, 0x6C, 0x11, + 0xD8, 0x80, 0x61, 0x41, +}; +const uint8 SM9_XP22[SM9_PARAM_CBLOCK] = { + 0x37, 0x22, 0x75, 0x52, + 0x92, 0x13, 0x0B, 0x08, + 0xD2, 0xAA, 0xB9, 0x7F, + 0xD3, 0x4E, 0xC1, 0x20, + 0xEE, 0x26, 0x59, 0x48, + 0xD1, 0x9C, 0x17, 0xAB, + 0xF9, 0xB7, 0x21, 0x3B, + 0xAF, 0x82, 0xD6, 0x5B, +}; +const uint8 SM9_YP21[SM9_PARAM_CBLOCK] = { + 0x17, 0x50, 0x9B, 0x09, + 0x2E, 0x84, 0x5C, 0x12, + 0x66, 0xBA, 0x0D, 0x26, + 0x2C, 0xBE, 0xE6, 0xED, + 0x07, 0x36, 0xA9, 0x6F, + 0xA3, 0x47, 0xC8, 0xBD, + 0x85, 0x6D, 0xC7, 0x6B, + 0x84, 0xEB, 0xEB, 0x96, +}; +const uint8 SM9_YP22[SM9_PARAM_CBLOCK] = { + 0xA7, 0xCF, 0x28, 0xD5, + 0x19, 0xBE, 0x3D, 0xA6, + 0x5F, 0x31, 0x70, 0x15, + 0x3D, 0x27, 0x8F, 0xF2, + 0x47, 0xEF, 0xBA, 0x98, + 0xA7, 0x1A, 0x08, 0x11, + 0x62, 0x15, 0xBB, 0xA5, + 0xC9, 0x99, 0xA7, 0xC7, +}; +const uint8 SM9_EID = 0x04; + +/* + * H1 if typ == 1, H2 if typ == 2. + */ + +typedef struct sm9_ctx_st { + // +} SM9_CTX; + +int sm9_init(SM3_CTX *ctx) { + memset(ctx, 0, sizeof(*ctx)); + return 1; +} + +int sm9_H(BIGNUM *h, const uint8 typ, const uint8 *Z, const size_t Zlen, const EC_GROUP *group) { + const uint64 v = 256; /* SM3: 256bit digest */ + if (h == NULL) { + return 0; + } + int ret = 0; + uint32 hlen, ct = 1, lmt, tmp; + uint8 *_ct, *tmp_u8, *digest, *K, *p; + BN_CTX *ctx; + ctx = BN_CTX_new(); + if (ctx == NULL) { + return 0; + } + BN_CTX_start(ctx); + BIGNUM *tmp_bn = BN_new(), *n; + n = group->order; + if (tmp_bn == NULL || n == NULL) { + goto err; + } + if (!BN_copy(h, BN_value_one()) + || !BN_mul_word(h, 5) + || !BN_exp(h, n, h, ctx)) { + goto err; + } + hlen = (BN_num_bits(h) + 31) >> 5; + K = (uint8 *)malloc(hlen); + hlen = 8 * hlen; + lmt = (hlen + v - 1) / v; + _ct = (uint8 *)malloc(4UL); + digest = (uint8 *)malloc(SM3_DIGEST_LENGTH); + SM3_CTX c; + while (ct < lmt) { + tmp_u8 = _ct; + HOST_l2c(ct, tmp_u8); + if (!sm3_init(&c) + || !sm3_update(&c, &typ, 1) + || !sm3_update(&c, Z, Zlen) + || !sm3_update(&c, _ct, 4UL) + || !sm3_final(digest, &c) + || !memcpy(p, digest, SM3_DIGEST_LENGTH)) { + goto err; + } + ++ct; + p += SM3_DIGEST_LENGTH; + } + tmp_u8 = _ct; + HOST_l2c(ct, tmp_u8); + if (!sm3_init(&c) + || !sm3_update(&c, Z, Zlen) + || !sm3_update(&c, _ct, 4UL) + || !sm3_final(digest, &c)) { + goto err; + } + tmp = hlen % v; + if (!tmp) { + if (!memcpy(p, digest, SM3_DIGEST_LENGTH)) { + goto err; + } + } + else { + if (!memcpy(p, digest, tmp >> 3)) { + goto err; + } + } + BN_bin2bn(K, hlen >> 3, h); + if (!BN_sub(tmp_bn, n, BN_value_one()) + || !BN_mod(h, h, tmp_bn, ctx)) { + goto err; + } + ret = 1; + err: + BN_CTX_end(ctx); + BN_CTX_free(ctx); + return ret; +} + +int sm9_H1(BIGNUM *h, const uint8 typ, const uint8 *Z, const size_t Zlen, const EC_GROUP *group) { + return sm9_H(h, 0x01, Z, Zlen, group); +} + +int sm9_H2(BIGNUM *h, const uint8 typ, const uint8 *Z, const size_t Zlen, const EC_GROUP *group) { + return sm9_H(h, 0x02, Z, Zlen, group); +} + +int sm9_KDF(uint8 *K, const uint8 *Z, const size_t Zlen, const uint64 klen) { + const uint64 v = 256; /* SM3: 256bit digest */ + if (v*0xFFFFFFFFU <= klen) { + return 0; + } + if (klen & 7) { + return 0; + } + uint32 ct = 1, lmt = (klen+v-1)/v, tmp; + uint8 *_ct, *tmp_u8, *digest, *p = K; + _ct = (uint8 *)malloc(4UL); + digest = (uint8 *)malloc(SM3_DIGEST_LENGTH); + SM3_CTX c; + while (ct < lmt) { + tmp_u8 = _ct; + HOST_l2c(ct, tmp_u8); + if (!sm3_init(&c) + || !sm3_update(&c, Z, Zlen) + || !sm3_update(&c, _ct, 4UL) + || !sm3_final(digest, &c) + || !memcpy(p, digest, SM3_DIGEST_LENGTH)) { + return 0; + } + ++ct; + p += SM3_DIGEST_LENGTH; + } + tmp_u8 = _ct; + HOST_l2c(ct, tmp_u8); + if (!sm3_init(&c) + || !sm3_update(&c, Z, Zlen) + || !sm3_update(&c, _ct, 4UL) + || !sm3_final(digest, &c)) { + return 0; + } + tmp = klen % v; + if (!tmp) { + if (!memcpy(p, digest, SM3_DIGEST_LENGTH)) { + return 0; + } + } + else { + if (!memcpy(p, digest, tmp >> 3)) { + return 0; + } + } + return 1; +} + +typedef struct sm9_sig_st { + BIGNUM *h; + BIGNUM *S; +} SM9_SIG; + +int SM9_SIG_init(SM9_SIG *sig) { + sig->h = BN_new(); + sig->S = BN_new(); + if (sig->h == NULL || sig->S == NULL) { + BN_free(sig->h); + BN_free(sig->S); + return 0; + } + return 1; +} + +SM9_SIG *SM9_SIG_new() { + SM9_SIG *ret; + ret = malloc(sizeof(*ret)); + if (ret == NULL) { + return NULL; + } + if (!SM9_SIG_init(ret)) { + free(ret); + return NULL; + } + return ret; +} + +#endif \ No newline at end of file diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/zuc/zuc.h" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/zuc/zuc.h" new file mode 100644 index 0000000000000000000000000000000000000000..711fcd482d5f5dc7d5be9f655fc5a3bd4fdaf725 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/zuc/zuc.h" @@ -0,0 +1,376 @@ +#ifndef SECGEAR_ZUC_H +#define SECGEAR_ZUC_H + +#include +#include "../include/common_utils.h" + +/* S_BOX BEGIN */ + +const uint8 ZUC_S[2][256] = { + { + 0x3E, 0x72, 0x5B, 0x47, 0xCA, 0xE0, 0x00, 0x33, 0x04, 0xD1, 0x54, 0x98, 0x09, 0xB9, 0x6D, 0xCB, + 0x7B, 0x1B, 0xF9, 0x32, 0xAF, 0x9D, 0x6A, 0xA5, 0xB8, 0x2D, 0xFC, 0x1D, 0x08, 0x53, 0x03, 0x90, + 0x4D, 0x4E, 0x84, 0x99, 0xE4, 0xCE, 0xD9, 0x91, 0xDD, 0xB6, 0x85, 0x48, 0x8B, 0x29, 0x6E, 0xAC, + 0xCD, 0xC1, 0xF8, 0x1E, 0x73, 0x43, 0x69, 0xC6, 0xB5, 0xBD, 0xFD, 0x39, 0x63, 0x20, 0xD4, 0x38, + 0x76, 0x7D, 0xB2, 0xA7, 0xCF, 0xED, 0x57, 0xC5, 0xF3, 0x2C, 0xBB, 0x14, 0x21, 0x06, 0x55, 0x9B, + 0xE3, 0xEF, 0x5E, 0x31, 0x4F, 0x7F, 0x5A, 0xA4, 0x0D, 0x82, 0x51, 0x49, 0x5F, 0xBA, 0x58, 0x1C, + 0x4A, 0x16, 0xD5, 0x17, 0xA8, 0x92, 0x24, 0x1F, 0x8C, 0xFF, 0xD8, 0xAE, 0x2E, 0x01, 0xD3, 0xAD, + 0x3B, 0x4B, 0xDA, 0x46, 0xEB, 0xC9, 0xDE, 0x9A, 0x8F, 0x87, 0xD7, 0x3A, 0x80, 0x6F, 0x2F, 0xC8, + 0xB1, 0xB4, 0x37, 0xF7, 0x0A, 0x22, 0x13, 0x28, 0x7C, 0xCC, 0x3C, 0x89, 0xC7, 0xC3, 0x96, 0x56, + 0x07, 0xBF, 0x7E, 0xF0, 0x0B, 0x2B, 0x97, 0x52, 0x35, 0x41, 0x79, 0x61, 0xA6, 0x4C, 0x10, 0xFE, + 0xBC, 0x26, 0x95, 0x88, 0x8A, 0xB0, 0xA3, 0xFB, 0xC0, 0x18, 0x94, 0xF2, 0xE1, 0xE5, 0xE9, 0x5D, + 0xD0, 0xDC, 0x11, 0x66, 0x64, 0x5C, 0xEC, 0x59, 0x42, 0x75, 0x12, 0xF5, 0x74, 0x9C, 0xAA, 0x23, + 0x0E, 0x86, 0xAB, 0xBE, 0x2A, 0x02, 0xE7, 0x67, 0xE6, 0x44, 0xA2, 0x6C, 0xC2, 0x93, 0x9F, 0xF1, + 0xF6, 0xFA, 0x36, 0xD2, 0x50, 0x68, 0x9E, 0x62, 0x71, 0x15, 0x3D, 0xD6, 0x40, 0xC4, 0xE2, 0x0F, + 0x8E, 0x83, 0x77, 0x6B, 0x25, 0x05, 0x3F, 0x0C, 0x30, 0xEA, 0x70, 0xB7, 0xA1, 0xE8, 0xA9, 0x65, + 0x8D, 0x27, 0x1A, 0xDB, 0x81, 0xB3, 0xA0, 0xF4, 0x45, 0x7A, 0x19, 0xDF, 0xEE, 0x78, 0x34, 0x60, + }, + { + 0x55, 0xC2, 0x63, 0x71, 0x3B, 0xC8, 0x47, 0x86, 0x9F, 0x3C, 0xDA, 0x5B, 0x29, 0xAA, 0xFD, 0x77, + 0x8C, 0xC5, 0x94, 0x0C, 0xA6, 0x1A, 0x13, 0x00, 0xE3, 0xA8, 0x16, 0x72, 0x40, 0xF9, 0xF8, 0x42, + 0x44, 0x26, 0x68, 0x96, 0x81, 0xD9, 0x45, 0x3E, 0x10, 0x76, 0xC6, 0xA7, 0x8B, 0x39, 0x43, 0xE1, + 0x3A, 0xB5, 0x56, 0x2A, 0xC0, 0x6D, 0xB3, 0x05, 0x22, 0x66, 0xBF, 0xDC, 0x0B, 0xFA, 0x62, 0x48, + 0xDD, 0x20, 0x11, 0x06, 0x36, 0xC9, 0xC1, 0xCF, 0xF6, 0x27, 0x52, 0xBB, 0x69, 0xF5, 0xD4, 0x87, + 0x7F, 0x84, 0x4C, 0xD2, 0x9C, 0x57, 0xA4, 0xBC, 0x4F, 0x9A, 0xDF, 0xFE, 0xD6, 0x8D, 0x7A, 0xEB, + 0x2B, 0x53, 0xD8, 0x5C, 0xA1, 0x14, 0x17, 0xFB, 0x23, 0xD5, 0x7D, 0x30, 0x67, 0x73, 0x08, 0x09, + 0xEE, 0xB7, 0x70, 0x3F, 0x61, 0xB2, 0x19, 0x8E, 0x4E, 0xE5, 0x4B, 0x93, 0x8F, 0x5D, 0xDB, 0xA9, + 0xAD, 0xF1, 0xAE, 0x2E, 0xCB, 0x0D, 0xFC, 0xF4, 0x2D, 0x46, 0x6E, 0x1D, 0x97, 0xE8, 0xD1, 0xE9, + 0x4D, 0x37, 0xA5, 0x75, 0x5E, 0x83, 0x9E, 0xAB, 0x82, 0x9D, 0xB9, 0x1C, 0xE0, 0xCD, 0x49, 0x89, + 0x01, 0xB6, 0xBD, 0x58, 0x24, 0xA2, 0x5F, 0x38, 0x78, 0x99, 0x15, 0x90, 0x50, 0xB8, 0x95, 0xE4, + 0xD0, 0x91, 0xC7, 0xCE, 0xED, 0x0F, 0xB4, 0x6F, 0xA0, 0xCC, 0xF0, 0x02, 0x4A, 0x79, 0xC3, 0xDE, + 0xA3, 0xEF, 0xEA, 0x51, 0xE6, 0x6B, 0x18, 0xEC, 0x1B, 0x2C, 0x80, 0xF7, 0x74, 0xE7, 0xFF, 0x21, + 0x5A, 0x6A, 0x54, 0x1E, 0x41, 0x31, 0x92, 0x35, 0xC4, 0x33, 0x07, 0x0A, 0xBA, 0x7E, 0x0E, 0x34, + 0x88, 0xB1, 0x98, 0x7C, 0xF3, 0x3D, 0x60, 0x6C, 0x7B, 0xCA, 0xD3, 0x1F, 0x32, 0x65, 0x04, 0x28, + 0x64, 0xBE, 0x85, 0x9B, 0x2F, 0x59, 0x8A, 0xD7, 0xB0, 0x25, 0xAC, 0xAF, 0x12, 0x03, 0xE2, 0xF2, + }, +}; + +const uint16 ZUC_D[16] = { + 042327, 023274, 061153, 011536, + 053611, 032742, 070465, 004657, + 046570, 027423, 065704, 015361, + 057046, 036115, 074232, 043654, +}; + +#define ZUC_S0(x) ZUC_S[0][x] +#define ZUC_S1(x) ZUC_S[1][x] +#define ZUC_S_BOX(X) \ + CONCAT_8x4( \ + ZUC_S0(EXTRACT_8_32(X, 0)), \ + ZUC_S1(EXTRACT_8_32(X, 1)), \ + ZUC_S0(EXTRACT_8_32(X, 2)), \ + ZUC_S1(EXTRACT_8_32(X, 3)) \ + ) + +/* S_BOX END */ + +/* MOD_OPERATIONS BEGIN */ + +#define ZUC_MOD_NUM 0x7FFFFFFF +uint32 zuc_add(uint32 a, uint32 b) { + uint32 c = a + b; + return (c & ZUC_MOD_NUM) + (c >> 31); +} +uint32 zuc_mul(uint32 a, uint32 b) { + uint32 res = 0; + for (int i = 0; i < 31; ++i) { + if (b & (1 << i)) { + res = zuc_add(res, ROTATE_31(a, i)); + } + } + return res; +} +uint32 zuc_mul_powof2(uint32 a, uint32 log2b) { + return ROTATE_31(a, log2b); +} + +/* MOD_OPERATIONS END */ + +/* LFSR BEGIN */ + +typedef struct ZUC_LFSR_st { + uint32 S[16]; /* 31-bit */ +} ZUC_LFSR; + +void zuc_lfsr_init(ZUC_LFSR *lfsr, uint32 u) { /* u: 31-bit */ + uint32 v = zuc_add( + zuc_add( + zuc_add( + zuc_mul_powof2(lfsr->S[15], 15), + zuc_mul_powof2(lfsr->S[13], 17) + ), + zuc_add( + zuc_mul_powof2(lfsr->S[10], 21), + zuc_mul_powof2(lfsr->S[4], 20) + ) + ), + zuc_add(lfsr->S[0], zuc_mul_powof2(lfsr->S[0], 8)) + ); + uint32 S16 = zuc_add(v, u); + S16 = zuc_add(S16, ZUC_MOD_NUM); + for (int i = 0; i < 15; ++i) { + lfsr->S[i] = lfsr->S[i + 1]; + } + lfsr->S[15] = S16; +} +void zuc_lfsr_work(ZUC_LFSR *lfsr) { + uint32 S16 = zuc_add( + zuc_add( + zuc_add( + zuc_mul_powof2(lfsr->S[15], 15), + zuc_mul_powof2(lfsr->S[13], 17) + ), + zuc_add( + zuc_mul_powof2(lfsr->S[10], 21), + zuc_mul_powof2(lfsr->S[4], 20) + ) + ), + zuc_add(lfsr->S[0], zuc_mul_powof2(lfsr->S[0], 8)) + ); + S16 = zuc_add(S16, ZUC_MOD_NUM); + for (int i = 0; i < 15; ++i) { + lfsr->S[i] = lfsr->S[i + 1]; + } + lfsr->S[15] = S16; +} + +/* LFSR END */ + +/* BR BEGIN */ + +typedef struct ZUC_BR_st { + uint32 X[4]; /* 32-bit */ +} ZUC_BR_CTX; + +void zuc_br(ZUC_BR_CTX *br, ZUC_LFSR *lfsr) { + br->X[0] = CONCAT_16x2(EXTRACT_16H_31(lfsr->S[15]), EXTRACT_16L_31(lfsr->S[14])); + br->X[1] = CONCAT_16x2(EXTRACT_16L_31(lfsr->S[11]), EXTRACT_16H_31(lfsr->S[9])); + br->X[2] = CONCAT_16x2(EXTRACT_16L_31(lfsr->S[7]), EXTRACT_16H_31(lfsr->S[5])); + br->X[3] = CONCAT_16x2(EXTRACT_16L_31(lfsr->S[2]), EXTRACT_16H_31(lfsr->S[0])); +} + +/* BR END */ + +/* F BEGIN */ + +#define ZUC_L1(X) \ + ((X) ^ ROTATE((X), 2) ^ ROTATE((X), 10) ^ ROTATE((X), 18) ^ ROTATE((X), 24)) +#define ZUC_L2(X) \ + ((X) ^ ROTATE((X), 8) ^ ROTATE((X), 14) ^ ROTATE((X), 22) ^ ROTATE((X), 30)) + +typedef struct ZUC_F_st { + uint32 R1, R2; /* 32-bit */ +} ZUC_F_CTX; + +uint32 zuc_f(ZUC_F_CTX *f, ZUC_BR_CTX *br) { /* X: 32-bit */ + uint32 X0 = br->X[0], X1 = br->X[1], X2 = br->X[2]; + uint32 R1 = f->R1, R2 = f->R2; + uint32 W = (X0 ^ R1) + R2; + uint32 W1 = R1 + X1; + uint32 W2 = R2 ^ X2; + R1 = ZUC_S_BOX(ZUC_L1(CONCAT_16x2( + EXTRACT_16_32(W1, 1), + EXTRACT_16_32(W2, 0) + ))); + R2 = ZUC_S_BOX(ZUC_L2(CONCAT_16x2( + EXTRACT_16_32(W2, 1), + EXTRACT_16_32(W1, 0) + ))); + f->R1 = R1; + f->R2 = R2; + return W; +} + +/* F END */ + +/* ZUC_MAIN BEGIN */ + +#define ZUC_INIT_CONCAT(k, d, iv) (((uint32)(k) << 15 | (d)) << 8 | (iv)) +#define ZUC_INTEGRITY_CONCAT(K1, K2, offset) \ + (((K1) << (offset)) | ((offset) ? (K2) >> (32 - (offset)) : 0)) + +typedef struct ZUCstate_st { + ZUC_LFSR lfsr; + ZUC_BR_CTX br; + ZUC_F_CTX f; +} ZUC_CTX; + +/* Initiate phase. Both k and iv are uint8 arrays of length 16. */ +int zuc_init(ZUC_CTX *z, uint8 k[], uint8 iv[]) { + memset(z, 0, sizeof(*z)); + for (int i = 0; i < 16; ++i) { + z->lfsr.S[i] = ZUC_INIT_CONCAT(k[i], ZUC_D[i], iv[i]); + } + #ifdef SECGEAR_ZUC_DEBUG + printf("LSFR init: \n"); + for (int i = 0; i < 16; ++i) { + printf("%08x%c", z->lfsr.S[i], " \n"[(i & 7) == 7]); + } + #endif + for (int i = 0; i < 32; ++i) { + zuc_br(&z->br, &z->lfsr); + uint32 W = zuc_f(&z->f, &z->br); + zuc_lfsr_init(&z->lfsr, W >> 1); + #ifdef SECGEAR_ZUC_DEBUG + printf("Init round %2d: \n", i); + printf("BR_X: "); + for (int i = 0; i < 4; ++i) { + printf("%08x%c", z->br.X[i], " \n"[i == 3]); + } + printf("F_R: %08x %08x\n", z->f.R1, z->f.R2); + printf("W = %08x S15 = %08x\n", W, z->lfsr.S[15]); + #endif + } + return 1; +} + +/* Work phase. Z is uint32 array that stores output keys. */ +/* len is the length of the key in the unit of 32-bit. */ +int zuc_work(ZUC_CTX *z, uint32 Z[], size_t len) { + zuc_br(&z->br, &z->lfsr); + #ifndef SECGEAR_ZUC_DEBUG + (void)zuc_f(&z->f, &z->br); + #else + uint32 _Z = zuc_f(&z->f, &z->br) ^ z->br.X[3]; + #endif + zuc_lfsr_work(&z->lfsr); + #ifdef SECGEAR_ZUC_DEBUG + printf("Key stream: \n"); + printf("BR_X: "); + for (int i = 0; i < 4; ++i) { + printf("%08x%c", z->br.X[i], " \n"[i == 3]); + } + printf("F_R: %08x %08x\n", z->f.R1, z->f.R2); + printf("Z = %08x S15 = %08x\n", _Z, z->lfsr.S[15]); + #endif + for (size_t i = 0; i < len; ++i) { + zuc_br(&z->br, &z->lfsr); + Z[i] = zuc_f(&z->f, &z->br) ^ z->br.X[3]; + zuc_lfsr_work(&z->lfsr); + #ifdef SECGEAR_ZUC_DEBUG + printf("Key round %2zu: \n", i); + printf("BR_X: "); + for (int i = 0; i < 4; ++i) { + printf("%08x%c", z->br.X[i], " \n"[i == 3]); + } + printf("F_R: %08x %08x\n", z->f.R1, z->f.R2); + printf("Z = %08x S15 = %08x\n", Z[i], z->lfsr.S[15]); + #endif + } + return 1; +} + +/* Generate 32-bit ZUC key stream. Use the array Z[] to receive the output. */ +int zuc_final(uint8 k[], uint8 iv[], uint32 Z[], size_t len) { + ZUC_CTX res, *z; + z = &res; + zuc_init(z, k, iv); + #ifdef SECGEAR_ZUC_DEBUG + printf("After init: \n"); + printf("LSFR: \n"); + for (int i = 0; i < 16; ++i) { + printf("%08x%c", z->lfsr.S[i], " \n"[(i & 7) == 7]); + } + printf("F_R: %08x %08x\n", z->f.R1, z->f.R2); + #endif + zuc_work(z, Z, len); + #ifdef SECGEAR_ZUC_DEBUG + printf("Output: \n"); + for (size_t i = 0; i < len; ++i) { + printf("z[%2zu] = %08x\n", i, Z[i]); + } + #endif + return 1; +} + +/* Use the array obs[] to receive the output. */ +int zuc_confidentiality(uint32 count, uint8 bearer, uint8 direction, uint8 ck[], + uint32 length, uint32 ibs[], uint32 obs[]) { + /* + * @param COUNT (length = 32b, counter) + * @param BEARER (length = 5b, bearer identifier) + * @param DIRECTION (length = 1b, direction identifier) + * @param CK (length = 128b = 8b * 16, confidential key) + * @param LENGTH (length = 32b, bit length of input bit stream) + * @param IBS (length = LENGTH b = 32b * L, input bit stream) + * @param OBS (length = LENGTH b = 32b * L, output bit stream) + */ + + /* Initiate k and iv for ZUC. */ + bearer &= 0x1F; direction &= 1; + uint8 _k[16], _iv[16], _count[4]; + memcpy(_k, ck, sizeof _k); + for (int i = 0; i < 4; ++i) { + _count[i] = EXTRACT_8_32(count, i); + } + _iv[0] = _count[0]; _iv[1] = _count[1]; _iv[2] = _count[2]; _iv[3] = _count[3]; + _iv[4] = (uint8)((bearer << 1 | direction) << 2); _iv[5] = _iv[6] = _iv[7] = 0; + for (int i = 8; i < 16; ++i) { + _iv[i] = _iv[i ^ 8]; + } + size_t L = (length >> 5) + ((length & 0x1F) ? 1 : 0); + uint32 Z[L]; + zuc_final(_k, _iv, Z, L); + for (size_t i = 0; i < L; ++i) { + obs[i] = ibs[i] ^ Z[i]; + } + if (length & 0x1F) { + obs[L - 1] &= PREFIX_MASK_32[length & 0x1F]; + } + #ifdef SECGEAR_ZUC_DEBUG_2 + printf("Output: \n"); + for (size_t i = 0; i < L; ++i) { + printf("%08x%c", obs[i], " \n"[i % 10 == 9]); + } + putchar('\n'); + #endif + return 1; +} + +uint32 zuc_integrity(uint32 count, uint8 bearer, uint8 direction, uint8 ik[], + uint32 length, uint32 M[]) { + /* + * @param COUNT (length = 32b, counter) + * @param BEARER (length = 5b, bearer identifier) + * @param DIRECTION (length = 1b, direction identifier) + * @param IK (length = 128b = 8b * 16, integrity key) + * @param LENGTH (length = 32b, bit length of input bit stream) + * @param M (length = LENGTH b = 32b * L, input massage stream) + * @return MAC (length = 32b, massage verification code) + */ + + /* Initiate k and iv for ZUC. */ + bearer &= 0x1F; direction &= 1; + uint8 _k[16], _iv[16], _count[4]; + memcpy(_k, ik, sizeof _k); + for (int i = 0; i < 4; ++i) { + _count[i] = EXTRACT_8_32(count, i); + } + _iv[0] = _count[0]; _iv[1] = _count[1]; _iv[2] = _count[2]; _iv[3] = _count[3]; + _iv[4] = (uint8)(bearer << 3); _iv[5] = _iv[6] = _iv[7] = 0; + _iv[8] = _iv[0] ^ (uint8)(direction << 7); + _iv[9] = _iv[1]; _iv[10] = _iv[2]; _iv[11] = _iv[3]; + _iv[12] = _iv[4]; _iv[13] = _iv[5]; + _iv[14] = _iv[6] ^ (uint8)(direction << 7); + _iv[15] = _iv[7]; + size_t L = (length >> 5) + ((length & 0x1F) ? 1 : 0) + 2; + uint32 Z[L]; + uint32 T = 0; + zuc_final(_k, _iv, Z, L); + for (size_t i = 0; i < length; ++i) { + if (GET_BIT_VALUE_32(M[i >> 5], i & 0x1F)) { + T = T ^ ZUC_INTEGRITY_CONCAT(Z[i >> 5], Z[(i >> 5) + 1], i & 0x1F); + } + } + T = T ^ ZUC_INTEGRITY_CONCAT(Z[length >> 5], Z[(length >> 5) + 1], length & 0x1F); + T = T ^ Z[L - 1]; + #ifdef SECGEAR_ZUC_DEBUG_3 + printf("Output: \n"); + printf("%08x\n", T); + #endif + return T; +} + +/* ZUC_MAIN END */ + +#endif \ No newline at end of file diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/zuc/zuctest1.c" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/zuc/zuctest1.c" new file mode 100644 index 0000000000000000000000000000000000000000..b8a8386212f6eee06f4c8b3b37f9d23eb3f2e568 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/zuc/zuctest1.c" @@ -0,0 +1,84 @@ +#define SECGEAR_ZUC_DEBUG +#include +#include "zuc.h" + +uint8 k_test1[16] = { + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, +}; +uint8 iv_test1[16] = { + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, +}; + +uint8 k_test2[16] = { + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, +}; +uint8 iv_test2[16] = { + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, +}; + +uint8 k_test3[16] = { + 0x3D, 0x4C, 0x4B, 0xE9, + 0x6A, 0x82, 0xFD, 0xAE, + 0xB5, 0x8F, 0x64, 0x1D, + 0xB1, 0x7B, 0x45, 0x5B, +}; +uint8 iv_test3[16] = { + 0x84, 0x31, 0x9A, 0xA8, + 0xDE, 0x69, 0x15, 0xCA, + 0x1F, 0x6B, 0xDA, 0x6B, + 0xFB, 0xD8, 0xC7, 0x66, +}; + +void zuc_test(uint8 k[], uint8 iv[]) { + // ZUC_CTX res; + // ZUC_CTX *z = &res; + // zuc_init(z, k, iv); + // #ifdef SECGEAR_ZUC_DEBUG + // printf("After init: \n"); + // printf("LSFR: \n"); + // for (int i = 0; i < 16; ++i) { + // printf("%08x%c", z->lfsr.S[i], " \n"[(i & 7) == 7]); + // } + // printf("F_R: %08x %08x\n", z->f.R1, z->f.R2); + // #endif + const size_t LEN = 32; + uint32 keys[LEN]; + zuc_final(k, iv, keys, LEN); + // zuc_work(z, keys, LEN); + // printf("Output: \n"); + // for (size_t i = 0; i < LEN; ++i) { + // printf("z[%2zu] = %08x\n", i, keys[i]); + // } +} + + + +int main(){ + printf("Test 1: \n"); + zuc_test(k_test1, iv_test1); + printf("Test 2: \n"); + zuc_test(k_test2, iv_test2); + printf("Test 3: \n"); + zuc_test(k_test3, iv_test3); + // uint32 a,b,log2b; + // scanf("%u%u",&a,&log2b); + // b = 1u << log2b; + // // scanf("%u%u",&a,&b); + // printf("%u %u\n",a,b); + // printf("add: %u\n",zuc_add(a,b)); + // printf("mul: %u\n",zuc_mul(a,b)); + // printf("mul with 2p: %u\n",zuc_mul_powof2(a,log2b)); + return 0; +} \ No newline at end of file diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/zuc/zuctest2.c" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/zuc/zuctest2.c" new file mode 100644 index 0000000000000000000000000000000000000000..04899d4bed7fb26195f3862f3909896d7a219be8 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/zuc/zuctest2.c" @@ -0,0 +1,80 @@ +#define SECGEAR_ZUC_DEBUG_2 +#include +#include "zuc.h" + +uint8 ck_test1[16] = { + 0x17, 0x3d, 0x14, 0xba, + 0x50, 0x03, 0x73, 0x1d, + 0x7a, 0x60, 0x04, 0x94, + 0x70, 0xf0, 0x0a, 0x29, +}; +uint32 count_test1 = 0x66035492; +uint8 bearer_test1 = 0xf; +uint8 dirc_test1 = 0x0; +uint32 len_test1 = 0xc1; +uint32 ibs_test1[] = { + 0x6cf65340, 0x735552ab, 0x0c9752fa, 0x6f9025fe, 0x0bd675d9, + 0x005875b2, 0x00000000, +}; + +uint8 ck_test2[16] = { + 0xe5, 0xbd, 0x3e, 0xa0, + 0xeb, 0x55, 0xad, 0xe8, + 0x66, 0xc6, 0xac, 0x58, + 0xbd, 0x54, 0x30, 0x2a, +}; +uint32 count_test2 = 0x56823; +uint8 bearer_test2 = 0x18; +uint8 dirc_test2 = 0x1; +uint32 len_test2 = 0x320; +uint32 ibs_test2[] = { + 0x14a8ef69, 0x3d678507, 0xbbe7270a, 0x7f67ff50, 0x06c3525b, + 0x9807e467, 0xc4e56000, 0xba338f5d, 0x42955903, 0x67518222, + 0x46c80d3b, 0x38f07f4b, 0xe2d8ff58, 0x05f51322, 0x29bde93b, + 0xbbdcaf38, 0x2bf1ee97, 0x3fbf9977, 0xbada8945, 0x847a2a6c, + 0x9ad34a66, 0x7554e04d, 0x1f7fa2c3, 0x3241bd8f, 0x01ba220d, +}; + +uint8 ck_test3[16] = { + 0xe1, 0x3f, 0xed, 0x21, + 0xb4, 0x6e, 0x4e, 0x7e, + 0xc3, 0x12, 0x53, 0xb2, + 0xbb, 0x17, 0xb3, 0xe0, +}; +uint32 count_test3 = 0x2738cdaa; +uint8 bearer_test3 = 0x1a; +uint8 dirc_test3 = 0x0; +uint32 len_test3 = 0xfb3; +uint32 ibs_test3[] = { + 0x8d74e20d, 0x54894e06, 0xd3cb13cb, 0x3933065e, 0x8674be62, 0xadb1c72b, 0x3a646965, 0xab63cb7b, 0x7854dfdc, 0x27e84929, + 0xf49c64b8, 0x72a490b1, 0x3f957b64, 0x827e71f4, 0x1fbd4269, 0xa42c97f8, 0x24537027, 0xf86e9f4a, 0xd82d1df4, 0x51690fdd, + 0x98b6d03f, 0x3a0ebe3a, 0x312d6b84, 0x0ba5a182, 0x0b2a2c97, 0x09c090d2, 0x45ed267c, 0xf845ae41, 0xfa975d33, 0x33ac3009, + 0xfd40eba9, 0xeb5b8857, 0x14b768b6, 0x97138baf, 0x21380eca, 0x49f644d4, 0x8689e421, 0x5760b906, 0x739f0d2b, 0x3f091133, + 0xca15d981, 0xcbe401ba, 0xf72d05ac, 0xe05cccb2, 0xd297f4ef, 0x6a5f58d9, 0x1246cfa7, 0x7215b892, 0xab441d52, 0x78452795, + 0xccb7f5d7, 0x9057a1c4, 0xf77f80d4, 0x6db2033c, 0xb79bedf8, 0xe60551ce, 0x10c667f6, 0x2a97abaf, 0xabbcd677, 0x2018df96, + 0xa282ea73, 0x7ce2cb33, 0x1211f60d, 0x5354ce78, 0xf9918d9c, 0x206ca042, 0xc9b62387, 0xdd709604, 0xa50af16d, 0x8d35a890, + 0x6be484cf, 0x2e74a928, 0x99403643, 0x53249b27, 0xb4c9ae29, 0xeddfc7da, 0x6418791a, 0x4e7baa06, 0x60fa6451, 0x1f2d685c, + 0xc3a5ff70, 0xe0d2b742, 0x92e3b8a0, 0xcd6b04b1, 0xc790b8ea, 0xd2703708, 0x540dea2f, 0xc09c3da7, 0x70f65449, 0xe84d817a, + 0x4f551055, 0xe19ab850, 0x18a0028b, 0x71a144d9, 0x6791e9a3, 0x57793350, 0x4eee0060, 0x340c69d2, 0x74e1bf9d, 0x805dcbcc, + 0x1a6faa97, 0x6800b6ff, 0x2b671dc4, 0x63652fa8, 0xa33ee509, 0x74c1c21b, 0xe01eabb2, 0x16743026, 0x9d72ee51, 0x1c9dde30, + 0x797c9a25, 0xd86ce74f, 0x5b961be5, 0xfdfb6807, 0x814039e7, 0x137636bd, 0x1d7fa9e0, 0x9efd2007, 0x505906a5, 0xac45dfde, + 0xed7757bb, 0xee745749, 0xc2963335, 0x0bee0ea6, 0xf409df45, 0x80160000, +}; + +void zuc_test2(uint32 count, uint8 bearer, uint8 direction, uint8 ck[], + uint32 length, uint32 ibs[], uint32 obs[]) { + zuc_confidentiality(count, bearer, direction, ck, length, ibs, obs); +} + + + +int main(){ + uint32 obs_test[130]; + printf("Test 1: \n"); + zuc_test2(count_test1, bearer_test1, dirc_test1, ck_test1, len_test1, ibs_test1, obs_test); + printf("Test 2: \n"); + zuc_test2(count_test2, bearer_test2, dirc_test2, ck_test2, len_test2, ibs_test2, obs_test); + printf("Test 3: \n"); + zuc_test2(count_test3, bearer_test3, dirc_test3, ck_test3, len_test3, ibs_test3, obs_test); + return 0; +} \ No newline at end of file diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/zuc/zuctest3.c" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/zuc/zuctest3.c" new file mode 100644 index 0000000000000000000000000000000000000000..545ee9280eb7ec83e112bb46df37114b2a082307 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/C_origin/zuc/zuctest3.c" @@ -0,0 +1,83 @@ +#define SECGEAR_ZUC_DEBUG_3 +#include +#include "zuc.h" + +uint8 ik_test1[16] = { + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, +}; +uint32 count_test1 = 0; +uint8 bearer_test1 = 0; +uint8 dirc_test1 = 0; +uint32 len_test1 = 1; +uint32 m_test1[] = { + 0x00000000, +}; + +uint8 ik_test2[16] = { + 0xc9, 0xe6, 0xce, 0xc4, + 0x60, 0x7c, 0x72, 0xdb, + 0x00, 0x0a, 0xef, 0xa8, + 0x83, 0x85, 0xab, 0x0a, +}; +uint32 count_test2 = 0xa94059da; +uint8 bearer_test2 = 0xa; +uint8 dirc_test2 = 0x1; +uint32 len_test2 = 0x241; +uint32 m_test2[] = { + 0x983b41d4, 0x7d780c9e, 0x1ad11d7e, 0xb70391b1, 0xde0b35da, 0x2dc62f83, 0xe7b78d63, 0x06ca0ea0, 0x7e941b7b, + 0xe91348f9, 0xfcb170e2, 0x217fecd9, 0x7f9f68ad, 0xb16e5d7d, 0x21e569d2, 0x80ed775c, 0xebde3f40, 0x93c53881, + 0x00000000, +}; + +uint8 ik_test3[16] = { + 0x6b, 0x8b, 0x08, 0xee, + 0x79, 0xe0, 0xb5, 0x98, + 0x2d, 0x6d, 0x12, 0x8e, + 0xa9, 0xf2, 0x20, 0xcb, +}; +uint32 count_test3 = 0x561eb2dd; +uint8 bearer_test3 = 0x1c; +uint8 dirc_test3 = 0x0; +uint32 len_test3 = 0x1626; +uint32 m_test3[] = { + 0x5bad7247, 0x10ba1c56, 0xd5a315f8, 0xd40f6e09, 0x3780be8e, 0x8de07b69, 0x92432018, 0xe08ed96a, 0x5734af8b, + 0xad8a575d, 0x3a1f162f, 0x85045cc7, 0x70925571, 0xd9f5b94e, 0x454a77c1, 0x6e72936b, 0xf016ae15, 0x7499f054, + 0x3b5d52ca, 0xa6dbeab6, 0x97d2bb73, 0xe41b8075, 0xdce79b4b, 0x86044f66, 0x1d4485a5, 0x43dd7860, 0x6e0419e8, + 0x059859d3, 0xcb2b67ce, 0x0977603f, 0x81ff839e, 0x33185954, 0x4cfbc8d0, 0x0fef1a4c, 0x8510fb54, 0x7d6b06c6, + 0x11ef44f1, 0xbce107cf, 0xa45a06aa, 0xb360152b, 0x28dc1ebe, 0x6f7fe09b, 0x0516f9a5, 0xb02a1bd8, 0x4bb0181e, + 0x2e89e19b, 0xd8125930, 0xd178682f, 0x3862dc51, 0xb636f04e, 0x720c47c3, 0xce51ad70, 0xd94b9b22, 0x55fbae90, + 0x6549f499, 0xf8c6d399, 0x47ed5e5d, 0xf8e2def1, 0x13253e7b, 0x08d0a76b, 0x6bfc68c8, 0x12f375c7, 0x9b8fe5fd, + 0x85976aa6, 0xd46b4a23, 0x39d8ae51, 0x47f680fb, 0xe70f978b, 0x38effd7b, 0x2f7866a2, 0x2554e193, 0xa94e98a6, + 0x8b74bd25, 0xbb2b3f5f, 0xb0a5fd59, 0x887f9ab6, 0x8159b717, 0x8d5b7b67, 0x7cb546bf, 0x41eadca2, 0x16fc1085, + 0x0128f8bd, 0xef5c8d89, 0xf96afa4f, 0xa8b54885, 0x565ed838, 0xa950fee5, 0xf1c3b0a4, 0xf6fb71e5, 0x4dfd169e, + 0x82cecc72, 0x66c850e6, 0x7c5ef0ba, 0x960f5214, 0x060e71eb, 0x172a75fc, 0x1486835c, 0xbea65344, 0x65b055c9, + 0x6a72e410, 0x52241823, 0x25d83041, 0x4b40214d, 0xaa8091d2, 0xe0fb010a, 0xe15c6de9, 0x0850973b, 0xdf1e423b, + 0xe148a237, 0xb87a0c9f, 0x34d4b476, 0x05b803d7, 0x43a86a90, 0x399a4af3, 0x96d3a120, 0x0a62f3d9, 0x507962e8, + 0xe5bee6d3, 0xda2bb3f7, 0x237664ac, 0x7a292823, 0x900bc635, 0x03b29e80, 0xd63f6067, 0xbf8e1716, 0xac25beba, + 0x350deb62, 0xa99fe031, 0x85eb4f69, 0x937ecd38, 0x7941fda5, 0x44ba67db, 0x09117749, 0x38b01827, 0xbcc69c92, + 0xb3f772a9, 0xd2859ef0, 0x03398b1f, 0x6bbad7b5, 0x74f7989a, 0x1d10b2df, 0x798e0dbf, 0x30d65874, 0x64d24878, + 0xcd00c0ea, 0xee8a1a0c, 0xc753a279, 0x79e11b41, 0xdb1de3d5, 0x038afaf4, 0x9f5c682c, 0x3748d8a3, 0xa9ec54e6, + 0xa371275f, 0x1683510f, 0x8e4f9093, 0x8f9ab6e1, 0x34c2cfdf, 0x4841cba8, 0x8e0cff2b, 0x0bcc8e6a, 0xdcb71109, + 0xb5198fec, 0xf1bb7e5c, 0x531aca50, 0xa56a8a3b, 0x6de59862, 0xd41fa113, 0xd9cd9578, 0x08f08571, 0xd9a4bb79, + 0x2af271f6, 0xcc6dbb8d, 0xc7ec36e3, 0x6be1ed30, 0x8164c31c, 0x7c0afc54, 0x1c000000, +}; + +uint32 zuc_test3(uint32 count, uint8 bearer, uint8 direction, uint8 ik[], + uint32 length, uint32 M[]) { + return zuc_integrity(count, bearer, direction, ik, length, M); +} + + + +int main(){ + printf("Test 1: \n"); + zuc_test3(count_test1, bearer_test1, dirc_test1, ik_test1, len_test1, m_test1); + printf("Test 2: \n"); + zuc_test3(count_test2, bearer_test2, dirc_test2, ik_test2, len_test2, m_test2); + printf("Test 3: \n"); + zuc_test3(count_test3, bearer_test3, dirc_test3, ik_test3, len_test3, m_test3); + return 0; +} \ No newline at end of file diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/.keep" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/CMakeLists.txt" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/CMakeLists.txt" new file mode 100644 index 0000000000000000000000000000000000000000..bfc5416eb3a010e07a87c02e635c0d6041237940 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/CMakeLists.txt" @@ -0,0 +1,38 @@ +# Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved. +# secGear is licensed under the Mulan PSL v2. +# You can use this software according to the terms and conditions of the Mulan PSL v2. +# You may obtain a copy of Mulan PSL v2 at: +# http://license.coscl.org.cn/MulanPSL2 +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR +# PURPOSE. +# See the Mulan PSL v2 for more details. + +cmake_minimum_required(VERSION 3.10 FATAL_ERROR) +project(enclave_SM2) + +set(CMAKE_C_STANDARD 99) + +set(CURRENT_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) + +#set edl name +set(EDL_FILE enclave_SM2.edl) +#set(LOCAL_ROOT_PATH "$ENV{CC_SDK}") +#set(SECGEAR_INSTALL_PATH /usr/lib64/) + +if(CC_GP) + set(CODETYPE trustzone) + set(CODEGEN codegen_arm64) + execute_process(COMMAND uuidgen -r OUTPUT_VARIABLE UUID) + string(REPLACE "\n" "" UUID ${UUID}) + add_definitions(-DPATH="/data/${UUID}.sec") +endif() + +if(CC_SGX) + set(CODETYPE sgx) + set(CODEGEN codegen_x86_64) + add_definitions(-DPATH="${CMAKE_CURRENT_BINARY_DIR}/enclave/enclave.signed.so") +endif() + +add_subdirectory(${CURRENT_ROOT_PATH}/enclave) +add_subdirectory(${CURRENT_ROOT_PATH}/host) diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/enclave/CMakeLists.txt" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/enclave/CMakeLists.txt" new file mode 100644 index 0000000000000000000000000000000000000000..7d04cf03463356de870bcee0c37dbd6819e0326c --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/enclave/CMakeLists.txt" @@ -0,0 +1,162 @@ +# Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved. +# secGear is licensed under the Mulan PSL v2. +# You can use this software according to the terms and conditions of the Mulan PSL v2. +# You may obtain a copy of Mulan PSL v2 at: +# http://license.coscl.org.cn/MulanPSL2 +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR +# PURPOSE. +# See the Mulan PSL v2 for more details. + +#set auto code prefix +set(PREFIX enclave_SM2) + +set(CMAKE_C_STANDARD 99) +#set sign key +set(PEM Enclave_private.pem) + +#set sign tool +set(SIGN_TOOL ${LOCAL_ROOT_PATH}/tools/sign_tool/sign_tool.sh) + +#set enclave src code +set(SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/enclave_SM2.c) + +#set log level +set(PRINT_LEVEL 3) +add_definitions(-DPRINT_LEVEL=${PRINT_LEVEL}) + +if(CC_GP) + #set signed output + set(OUTPUT ${UUID}.sec) + #set itrustee device key + set(DEVICEPEM ${CMAKE_CURRENT_SOURCE_DIR}/rsa_public_key_cloud.pem) + #set whilelist. default: /vendor/bin/teec_hello + set(WHITE_LIST_0 /vendor/bin/enclave_SM2) + set(WHITE_LIST_OWNER root) + set(WHITE_LIST_1 /vendor/bin/secgear_enclave_SM2) + set(WHITELIST WHITE_LIST_0 WHITE_LIST_1) + + set(AUTO_FILES ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_t.h ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_t.c ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_args.h) + add_custom_command(OUTPUT ${AUTO_FILES} + DEPENDS ${CURRENT_ROOT_PATH}/${EDL_FILE} + COMMAND ${CODEGEN} --${CODETYPE} --trusted ${CURRENT_ROOT_PATH}/${EDL_FILE} --search-path ${LOCAL_ROOT_PATH}/inc/host_inc/gp) +endif() + +if(CC_SGX) + set(OUTPUT enclave.signed.so) + set(AUTO_FILES ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_t.h ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_t.c) + add_custom_command(OUTPUT ${AUTO_FILES} + DEPENDS ${CURRENT_ROOT_PATH}/${EDL_FILE} + COMMAND ${CODEGEN} --${CODETYPE} --trusted ${CURRENT_ROOT_PATH}/${EDL_FILE} --search-path ${LOCAL_ROOT_PATH}/inc/host_inc/sgx --search-path ${SGXSDK}/include) +endif() + +set(COMMON_C_FLAGS "-W -Wall -fno-short-enums -fno-omit-frame-pointer -fstack-protector \ + -Wstack-protector --param ssp-buffer-size=4 -frecord-gcc-switches -Wextra -nostdinc -nodefaultlibs \ + -fno-peephole -fno-peephole2 -Wno-main -Wno-error=unused-parameter \ + -Wno-error=unused-but-set-variable -Wno-error=format-truncation=") + +set(COMMON_C_LINK_FLAGS "-Wl,-z,now -Wl,-z,relro -Wl,-z,noexecstack -Wl,-nostdlib -nodefaultlibs -nostartfiles") +set(COMMON_CXX_FLAGS "${COMMON_C_FLAGS}") + +if(CC_GP) + configure_file("${CMAKE_CURRENT_SOURCE_DIR}/manifest.txt.in" "${CMAKE_CURRENT_SOURCE_DIR}/manifest.txt") + + set(CMAKE_C_FLAGS "${COMMON_C_FLAGS} -march=armv8-a ") + set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -s -fPIC") + set(CMAKE_SHARED_LINKER_FLAGS "${COMMON_C_LINK_FLAGS} -Wl,-s") + + set(ITRUSTEE_TEEDIR ${iTrusteeSDK}/) + set(ITRUSTEE_LIBC ${iTrusteeSDK}/thirdparty/open_source/musl/libc) + + if(${CMAKE_VERSION} VERSION_LESS "3.13.0") + link_directories(${SECGEAR_INSTALL_PATH}) + endif() + + add_library(${PREFIX} SHARED ${SOURCE_FILES} ${AUTO_FILES}) + + target_include_directories( ${PREFIX} PRIVATE + ${CMAKE_CURRENT_BINARY_DIR} + ${LOCAL_ROOT_PATH}/inc/host_inc + ${LOCAL_ROOT_PATH}/inc/host_inc/gp + ${LOCAL_ROOT_PATH}/inc/enclave_inc + ${LOCAL_ROOT_PATH}/inc/enclave_inc/gp + ${ITRUSTEE_TEEDIR}/include/TA + ${ITRUSTEE_TEEDIR}/include/TA/huawei_ext + ${ITRUSTEE_LIBC}/arch/aarch64 + ${ITRUSTEE_LIBC}/ + ${ITRUSTEE_LIBC}/arch/arm/bits + ${ITRUSTEE_LIBC}/arch/generic + ${ITRUSTEE_LIBC}/arch/arm + ${LOCAL_ROOT_PATH}/inc/enclave_inc/gp/itrustee) + + if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.13.0") + target_link_directories(${PREFIX} PRIVATE ${SECGEAR_INSTALL_PATH}) + endif() + + foreach(WHITE_LIST ${WHITELIST}) + add_definitions(-D${WHITE_LIST}="${${WHITE_LIST}}") + endforeach(WHITE_LIST) + add_definitions(-DWHITE_LIST_OWNER="${WHITE_LIST_OWNER}") + + target_link_libraries(${PREFIX} -lsecgear_tee) + + add_custom_command(TARGET ${PREFIX} + POST_BUILD + COMMAND bash ${SIGN_TOOL} -d sign -x trustzone -i /home/sunny/secGear/debug/lib/lib${PREFIX}.so -c ${CMAKE_CURRENT_SOURCE_DIR}/manifest.txt -m ${CMAKE_CURRENT_SOURCE_DIR}/config_cloud.ini + -o ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT}) + + install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT} + DESTINATION /data + PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE) + +endif() + +if(CC_SGX) + set(SGX_DIR ${SGXSDK}) + set(CMAKE_C_FLAGS "${COMMON_C_FLAGS} -m64 -fvisibility=hidden") + set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -std=c++11 -O2 -nostdinc++") + set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -s") + set(LINK_LIBRARY_PATH ${SGX_DIR}/lib64) + + if(CC_SIM) + set(Trts_Library_Name sgx_trts_sim) + set(Service_Library_Name sgx_tservice_sim) + else() + set(Trts_Library_Name sgx_trts) + set(Service_Library_Name sgx_tservice) + endif() + + set(Crypto_Library_Name sgx_tcrypto) + + set(CMAKE_SHARED_LINKER_FLAGS "${COMMON_C_LINK_FLAGS} -Wl,-z,defs -Wl,-pie -Bstatic -Bsymbolic -eenclave_entry \ + -Wl,--export-dynamic -Wl,--defsym,__ImageBase=0 -Wl,--gc-sections -Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/Enclave.lds") + + if(${CMAKE_VERSION} VERSION_LESS "3.13.0") + link_directories(${LINK_LIBRARY_PATH}) + endif() + + add_library(${PREFIX} SHARED ${SOURCE_FILES} ${AUTO_FILES}) + + target_include_directories(${PREFIX} PRIVATE + ${CMAKE_CURRENT_BINARY_DIR} + ${SGX_DIR}/include/tlibc + ${SGX_DIR}/include/libcxx + ${SGX_DIR}/include + ${LOCAL_ROOT_PATH}/inc/host_inc + ${LOCAL_ROOT_PATH}/inc/host_inc/sgx) + + if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.13.0") + target_link_directories(${PREFIX} PRIVATE + ${LINK_LIBRARY_PATH}) + endif() + + target_link_libraries(${PREFIX} -Wl,--whole-archive ${Trts_Library_Name} -Wl,--no-whole-archive + -Wl,--start-group -lsgx_tstdc -lsgx_tcxx -l${Crypto_Library_Name} -l${Service_Library_Name} -Wl,--end-group) + add_custom_command(TARGET ${PREFIX} + POST_BUILD + COMMAND umask 0177 + COMMAND openssl genrsa -3 -out ${PEM} 3072 + COMMAND bash ${SIGN_TOOL} -d sign -x sgx -i ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/lib${PREFIX}.so -k ${PEM} -o ${OUTPUT} -c ${CMAKE_CURRENT_SOURCE_DIR}/Enclave.config.xml) +endif() + +set_target_properties(${PREFIX} PROPERTIES SKIP_BUILD_RPATH TRUE) diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/enclave/Enclave.config.xml" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/enclave/Enclave.config.xml" new file mode 100644 index 0000000000000000000000000000000000000000..a5e05e0175b43f97645fde0ea33a1fe0562398cf --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/enclave/Enclave.config.xml" @@ -0,0 +1,12 @@ + + 0 + 0 + 0x100000 + 0x80000 + 10 + 1 + + 1 + 0 + 0xFFFFFFFF + diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/enclave/Enclave.lds" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/enclave/Enclave.lds" new file mode 100644 index 0000000000000000000000000000000000000000..ab77e6478ef3c3448356e80b9884a8a533dd32c6 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/enclave/Enclave.lds" @@ -0,0 +1,11 @@ +enclave.so +{ + global: + g_global_data_sim; + g_global_data; + enclave_entry; + g_peak_heap_used; + local: + *; +}; + diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/enclave/enclave_SM2.c" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/enclave/enclave_SM2.c" new file mode 100644 index 0000000000000000000000000000000000000000..9d06a889e4dbc9e531dc388b1c352325094bec93 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/enclave/enclave_SM2.c" @@ -0,0 +1,116 @@ +#include "Enclave1_t.h" +#define SM2_EXCHANGE_OPTIONAL +#include "sm2.h" +#include "sm2_tests.h" + +/* + * Ref: + * GB/T 32918.5-2017 + */ + +const int SM2_p_bytes = 256 >> 3; +uint8 *sign_ID_A; +uint8 *sign_M; +uint8 sign_dA[SM2_p_bytes]; +uint8 sign_k[SM2_p_bytes]; + +void setSignIDA(uint8 *src, size_t len) { + if (len < MAX_LEN) { + memcpy(sign_ID_A, src, len + 1); + } +} + +void setSignM(uint8 *src, size_t len) { + if (len < MAX_LEN) { + memcpy(sign_M, src, len + 1); + } +} + +void setSignDA(uint8 *src) { + memcpy(sign_dA, src, SM2_p_bytes); +} + +void setSignK(uint8 *src) { + memcpy(sign_k, src, SM2_p_bytes); +} + +void getSignResult(uint8 *buf, size_t len) { + if (len > strlen(sign_buffer)) { + memcpy(buf, sign_buffer, strlen(sign_buffer) + 1); + } +} + +void getSignVerifyResult(uint8 buf) { + memcpy(buf, &sign_verify_ret, 1); +} + +void callsm2signtest() { + sm2signtest(SM2_p_bytes, sign_M, sign_ID_A, SM2_P, SM2_A, SM2_B, SM2_N, SM2_XG, SM2_YG, sign_dA, sign_k); +} + +uint8 *exchange_ID_A; +uint8 *exchange_ID_B; +uint8 exchange_dA[SM2_p_bytes]; +uint8 exchange_dB[SM2_p_bytes]; +uint64 exchange_klen = 128; + +void setExchangeIDA(uint8 *src, size_t len) { + if (len < MAX_LEN) { + memcpy(exchange_ID_A, src, len + 1); + } +} + +void setExchangeIDB(uint8 *src, size_t len) { + if (len < MAX_LEN) { + memcpy(exchange_ID_B, src, len + 1); + } +} + +void setExchangeDA(uint8 *src) { + memcpy(exchange_dA, src, SM2_p_bytes); +} + +void setExchangeDB(uint8 *src) { + memcpy(exchange_dB, src, SM2_p_bytes); +} + +void setExchangeKLen(uint64 *src) { + memcpy(exchange_klen, src, sizeof(uint64)); +} + +void getExchangeResult(uint8 buf) { + memcpy(buf, &exchange_ret, 1); +} + +void callsm2exchangetest() { + sm2exchangetest(SM2_p_bytes, exchange_ID_A, exchange_ID_B, SM2_P, SM2_A, SM2_B, SM2_N, SM2_XG, SM2_YG, exchange_dA, exchange_dB, exchange_klen); +} + +uint8 *crypt_M; +uint8 crypt_dB[SM2_p_bytes]; + +void setCryptM(uint8 *src, size_t len) { + if (len < MAX_LEN) { + memcpy(crypt_M, src, len + 1); + } +} + +void setCryptDB(uint8 *src) { + memcpy(crypt_dB, src, SM2_p_bytes); +} + +void getCryptPW(uint8 *buf, size_t len) { + if (len > strlen(encrypt_pw)) { + memcpy(buf, encrypt_pw, strlen(encrypt_pw) + 1); + } +} + +void getCryptMSG(uint8 *buf, size_t len) { + if (len > strlen(decrypt_msg)) { + memcpy(buf, decrypt_msg, strlen(decrypt_msg) + 1); + } +} + +void callsm2crypttest() { + sm2crypttest(SM2_p_bytes, crypt_M, SM2_P, SM2_A, SM2_B, SM2_N, SM2_XG, SM2_YG, crypt_dB); +} diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/enclave/manifest.txt.in" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/enclave/manifest.txt.in" new file mode 100644 index 0000000000000000000000000000000000000000..7b8ecf5937d99b0c7f87478984017cdc92c44ed1 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/enclave/manifest.txt.in" @@ -0,0 +1,8 @@ +gpd.ta.appID: @UUID@ +gpd.ta.service_name: test0108 +gpd.ta.singleInstance: true +gpd.ta.multiSession: false +gpd.ta.multiCommand: false +gpd.ta.instanceKeepAlive: false +gpd.ta.dataSize: 16384 +gpd.ta.stackSize: 20480 diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/enclave/rsa_public_key_cloud.pem" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/enclave/rsa_public_key_cloud.pem" new file mode 100644 index 0000000000000000000000000000000000000000..a321f63ed2fb0459c03fcb63de8f2f21618d8e56 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/enclave/rsa_public_key_cloud.pem" @@ -0,0 +1,11 @@ +-----BEGIN PUBLIC KEY----- +MIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIBigKCAYEAzAPwbnbgBg7JgXERA9Bx +p7GLI1S3e1zL83RMd2+GXb6kO4yMKUL3NUCE2HhA2BtQYmLyGovx59UUcKnU58is +Xux++kH+A2shmOPjYvEFuX0Kt8tc19b8M9b/iHsY8ZmKykqia2a5U+IrECRFJo5p +DWUnl7jrHVtq78BSR1c7iXG1frrEC0AYCuqKJo/fxfmOKL0Y9mENCB3nAwjn9unD +BsO/OhkqvvB3nkeuMfNKPh4wCqtQPve13eTojbuxjX/3ePijplTI5X2Gr+n6Ximn +fYRlytQmMgMl/db0ARSKNApq9bmwzVNrnGWWZWJksdRvf6iL7t17Gs4L9AApOuC9 +WkzxPvwp5ZUqjsGd4oJGWeC6ZE6BTw2vxE+xMFI9uAKHxq9pBKkcGMa0g4fANNNV ++W+8JZGanxEXKB3y/M7BCyQAPCWOHC/RNjmRA1gczLYCPzC4pWu935UZdF1RR6zY +CD3t+FoOGGET/g4CwWgyhb5qkp65Hs6ayYt/DUAqo+yBAgMBAAE= +-----END PUBLIC KEY----- diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/enclave/sm2.h" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/enclave/sm2.h" new file mode 100644 index 0000000000000000000000000000000000000000..27bf24930a8a2c02117f8c9d84d1cf2d6876f825 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/enclave/sm2.h" @@ -0,0 +1,207 @@ +#ifndef SECGEAR_SM2_H +#define SECGEAR_SM2_H + +/* + * Ref: + * GB/T 32918.1-2016 + * GB/T 32918.2-2016 + * GB/T 32918.3-2016 + * GB/T 32918.4-2016 + * GB/T 32918.5-2017 + */ + +#include +#include "../include/common_utils.h" +#include "../include/ec.h" +#include "../sm3/sm3.h" +#define SM2ERR(msg) goto err; +/* + * export C_INCLUDE_PATH="/PATH/TO/OPENSSL/include"(/opt/homebrew/Cellar/openssl@1.1/1.1.1m/include) + * export CPLUS_INCLUDE_PATH="/PATH/TO/OPENSSL/include" + * export LIBRARY_PATH="/PATH/TO/OPENSSL/lib" + */ + +/* Parameters */ +/* EC: y^2 = x^3 + ax + b */ + +#define SM2_PARAM_CBLOCK 32 +#define SM2_PARAM_LBLOCK (SM2_PARAM_CBLOCK/4) + +const uint8 SM2_P[SM2_PARAM_CBLOCK] = { + 0xFF, 0xFF, 0xFF, 0xFE, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + 0x00, 0x00, 0x00, 0x00, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, +}; +const uint8 SM2_A[SM2_PARAM_CBLOCK] = { + 0xFF, 0xFF, 0xFF, 0xFE, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + 0x00, 0x00, 0x00, 0x00, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFC, +}; +const uint8 SM2_B[SM2_PARAM_CBLOCK] = { + 0x28, 0xE9, 0xFA, 0x9E, + 0x9D, 0x9F, 0x5E, 0x34, + 0x4D, 0x5A, 0x9E, 0x4B, + 0xCF, 0x65, 0x09, 0xA7, + 0xF3, 0x97, 0x89, 0xF5, + 0x15, 0xAB, 0x8F, 0x92, + 0xDD, 0xBC, 0xBD, 0x41, + 0x4D, 0x94, 0x0E, 0x93, +}; +const uint8 SM2_N[SM2_PARAM_CBLOCK] = { + 0xFF, 0xFF, 0xFF, 0xFE, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + 0x72, 0x03, 0xDF, 0x6B, + 0x21, 0xC6, 0x05, 0x2B, + 0x53, 0xBB, 0xF4, 0x09, + 0x39, 0xD5, 0x41, 0x23, +}; +const uint8 SM2_XG[SM2_PARAM_CBLOCK] = { + 0x32, 0xC4, 0xAE, 0x2C, + 0x1F, 0x19, 0x81, 0x19, + 0x5F, 0x99, 0x04, 0x46, + 0x6A, 0x39, 0xC9, 0x94, + 0x8F, 0xE3, 0x0B, 0xBF, + 0xF2, 0x66, 0x0B, 0xE1, + 0x71, 0x5A, 0x45, 0x89, + 0x33, 0x4C, 0x74, 0xC7, +}; +const uint8 SM2_YG[SM2_PARAM_CBLOCK] = { + 0xBC, 0x37, 0x36, 0xA2, + 0xF4, 0xF6, 0x77, 0x9C, + 0x59, 0xBD, 0xCE, 0xE3, + 0x6B, 0x69, 0x21, 0x53, + 0xD0, 0xA9, 0x87, 0x7C, + 0xC6, 0x2A, 0x47, 0x40, + 0x02, 0xDF, 0x32, 0xE5, + 0x21, 0x39, 0xF0, 0xA0, +}; + +typedef struct sm2_sig_st { + BIGNUM *r; + BIGNUM *s; +} SM2_SIG; + +int SM2_SIG_init(SM2_SIG *sig) { + sig->r = BN_new(); + sig->s = BN_new(); + if (sig->r == NULL || sig->s == NULL) { + BN_free(sig->r); + BN_free(sig->s); + return 0; + } + return 1; +} + +SM2_SIG *SM2_SIG_new() { + SM2_SIG *ret; + ret = malloc(sizeof(*ret)); + if (ret == NULL) { + return NULL; + } + if (!SM2_SIG_init(ret)) { + free(ret); + return NULL; + } + return ret; +} + +int sm2_compute_z(uint8 *out, const uint8 *id, const size_t idlen, const EC_KEY *key){ + /* ZA = SM3(ENTLA || IDA || a || b || xG || yG || xA || yA) */ + uint16 entlen = idlen << 3; + uint8 e_bytes, *buf; + int p_bytes; + SM3_CTX c; + sm3_init(&c); + if (idlen > (UINT16_MAX >> 3)) { + SM2ERR("SM2_compute_z(): idlen too large!\n"); + } + e_bytes = entlen >> 8; + sm3_update(&c, &e_bytes, 1); + e_bytes = entlen & 0xFF; + sm3_update(&c, &e_bytes, 1); + if (!sm3_update(&c, id, idlen)) { + goto err; + } + p_bytes = BN_num_bytes(key->group->field); + buf = malloc(p_bytes); + if (!BN_bn2binpad(key->group->a, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !BN_bn2binpad(key->group->b, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !BN_bn2binpad(key->group->generator->X, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !BN_bn2binpad(key->group->generator->Y, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || BN_bn2binpad(key->pub_key->X, buf, p_bytes) < 0 + || !sm3_update(&c, buf, p_bytes) + || BN_bn2binpad(key->pub_key->Y, buf, p_bytes) < 0 + || !sm3_update(&c, buf, p_bytes)) { + goto err; + } + sm3_final(out, &c); + return 1; + err: + return 0; +} + +int sm2_KDF(uint8 *K, const uint8 *Z, const size_t Zlen, const uint64 klen) { + const uint64 v = 256; /* SM3: 256bit digest */ + if (v*0xFFFFFFFFU <= klen) { + return 0; + } + if (klen & 7) { + return 0; + } + uint32 ct = 1, lmt = (klen+v-1)/v, tmp; + uint8 *_ct, *tmp_u8, *digest, *p = K; + _ct = malloc(4UL); + digest = malloc(SM3_DIGEST_LENGTH); + SM3_CTX c; + while (ct < lmt) { + tmp_u8 = _ct; + HOST_l2c(ct, tmp_u8); + if (!sm3_init(&c) + || !sm3_update(&c, Z, Zlen) + || !sm3_update(&c, _ct, 4UL) + || !sm3_final(digest, &c) + || !memcpy(p, digest, SM3_DIGEST_LENGTH)) { + return 0; + } + ++ct; + p += SM3_DIGEST_LENGTH; + } + tmp_u8 = _ct; + HOST_l2c(ct, tmp_u8); + if (!sm3_init(&c) + || !sm3_update(&c, Z, Zlen) + || !sm3_update(&c, _ct, 4UL) + || !sm3_final(digest, &c)) { + return 0; + } + tmp = klen % v; + if (!tmp) { + if (!memcpy(p, digest, SM3_DIGEST_LENGTH)) { + return 0; + } + } + else { + if (!memcpy(p, digest, tmp >> 3)) { + return 0; + } + } + return 1; +} + +#endif \ No newline at end of file diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/enclave/sm2_crypt.h" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/enclave/sm2_crypt.h" new file mode 100644 index 0000000000000000000000000000000000000000..8ceebef54446c6e1f5b3b2d45e5ab6e435e6dbf4 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/enclave/sm2_crypt.h" @@ -0,0 +1,204 @@ +#include "sm2.h" + +/* + * Ref: + * GB/T 32918.4-2016 + */ + +/* Encrypt */ + +int sm2_encrypt(uint8 *out, const uint8 *M, const uint64 klen, const EC_KEY *key) { + SM3_CTX c; + BIGNUM *n, *k, *h, *x_2, *y_2; + EC_POINT *G, *C1, *S, *PB, *tmp_point; + EC_GROUP *group; + uint8 *_C1, *_C2, *_C3, *t, *buf; + size_t C1len, MlenCBLOCK, buflen, i; + int p_bytes; + group = key->group; + n = group->order; + h = group->cofactor; + G = group->generator; + PB = key->pub_key; + x_2 = BN_new(); + y_2 = BN_new(); + k = BN_new(); + C1 = EC_POINT_new(); + tmp_point = EC_POINT_new(); + S = EC_POINT_new(); + if (group == NULL + || n == NULL + || h == NULL + || k == NULL + || G == NULL + || S == NULL + || PB == NULL) { + return 0; + } + p_bytes = BN_num_bytes(group->field); + MlenCBLOCK = klen >> 3; + C1len = (p_bytes << 1 | 1); + buflen = p_bytes << 1; + _C1 = malloc(C1len); + _C2 = malloc(MlenCBLOCK); + _C3 = malloc(SM3_DIGEST_LENGTH); + buf = malloc(buflen); + t = malloc(MlenCBLOCK); + if (t == NULL) { + goto err; + } + for (;;) { + /* A1: k = rand(1, n-1) */ + do { + if (!BN_rand_range(k, n)) { + goto err; + } + } while(BN_is_zero(k)); + /* A2: C1 = [k]G = (x1, y1), C1 -> bytes */ + if (!EC_POINT_mul(C1, G, k, group) + || !EC_POINT_ecp2bin_normal(_C1, C1, p_bytes)) { + goto err; + } + /* A3: S = [h]PB, if s = O, goto err */ + if (!EC_POINT_mul(S, PB, h, group) + || EC_POINT_is_infinity(S)) { + goto err; + } + /* A4: [k]PB = (x2, y2), x2, y2 -> bytes */ + if (!EC_POINT_mul(tmp_point, PB, k, group)) { + goto err; + } + x_2 = tmp_point->X; + y_2 = tmp_point->Y; + /* A5: t = KDF(x2 || y2, klen), if t == 0, goto A1 */ + if (!BN_bn2binpad(x_2, buf, p_bytes) + || !BN_bn2binpad(y_2, buf+p_bytes, p_bytes) + || !sm2_KDF(t, buf, buflen, klen)) { + goto err; + } + for (i = 0; i < MlenCBLOCK; ++i) { + if (t[i]) { + goto A6; + } + } + } + /* A6: C2 = M ^ t */ + A6: + for (i = 0; i < MlenCBLOCK; ++i) { + _C2[i] = M[i] ^ t[i]; + } + /* A7: C3 = SM3(x2 || M || y2) */ + if (!sm3_init(&c) + || !BN_bn2binpad(x_2, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !sm3_update(&c, M, MlenCBLOCK) + || !BN_bn2binpad(y_2, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !sm3_final(_C3, &c)) { + goto err; + } + /* A8: C = C1 || C3 || C2 */ + if (!memcpy(out, _C1, C1len) + || !memcpy(out+C1len, _C3, SM3_DIGEST_LENGTH) + || !memcpy(out+C1len+SM3_DIGEST_LENGTH, _C2, MlenCBLOCK)) { + goto err; + } + return 1; + err: + return 0; +} + +/* Decrypt */ + +int sm2_decrypt(uint8 *_M, const uint8 *C, const uint64 klen, const EC_KEY *key) { + SM3_CTX c; + BIGNUM *dB, *h, *x_2, *y_2; + EC_POINT *C1, *S, *tmp_point; + EC_GROUP *group; + uint8 *_C1, *_C2, *_C3, *t, *buf, *u; + size_t C1len, MlenCBLOCK, buflen, i; + int p_bytes; + group = key->group; + h = group->cofactor; + dB = key->priv_key; + x_2 = BN_new(); + y_2 = BN_new(); + C1 = EC_POINT_new(); + tmp_point = EC_POINT_new(); + S = EC_POINT_new(); + if (group == NULL + || h == NULL + || x_2 == NULL + || y_2 == NULL + || dB == NULL + || S == NULL) { + return 0; + } + p_bytes = BN_num_bytes(group->field); + MlenCBLOCK = klen >> 3; + C1len = (p_bytes << 1 | 1); + buflen = p_bytes << 1; + _C1 = malloc(C1len); + _C2 = malloc(MlenCBLOCK); + _C3 = malloc(SM3_DIGEST_LENGTH); + u = malloc(SM3_DIGEST_LENGTH); + buf = malloc(buflen); + t = malloc(MlenCBLOCK); + if (t == NULL) { + goto err; + } + /* C = C1 || C3 || C2 */ + if (!memcpy(_C1, C, C1len) + || !memcpy(_C2, C+C1len+SM3_DIGEST_LENGTH, MlenCBLOCK) + || !memcpy(_C3, C+C1len, SM3_DIGEST_LENGTH)) { + goto err; + } + /* B1: C -> C1, check if C1 on the curve */ + if (!EC_POINT_bin2ecp(C1, _C1, p_bytes) + || !EC_POINT_on_curve_simple(C1, group)) { + goto err; + } + /* B2: S = [h]C1, if S == O, goto err */ + if (!EC_POINT_mul(S, C1, h, group) + || EC_POINT_is_infinity(S)) { + goto err; + } + /* B3: [dB]C1 = (x2, y2), x2, y2 -> bytes */ + if (!EC_POINT_mul(tmp_point, C1, dB, group)) { + goto err; + } + x_2 = tmp_point->X; + y_2 = tmp_point->Y; + /* B4: t = KDF(x2 || y2, klen), if t = 0, goto err */ + if (!BN_bn2binpad(x_2, buf, p_bytes) + || !BN_bn2binpad(y_2, buf+p_bytes, p_bytes) + || !sm2_KDF(t, buf, buflen, klen)) { + goto err; + } + for (i = 0; i < MlenCBLOCK; ++i) { + if (t[i]) { + goto B5; + } + } + goto err; + B5: + /* B5: C -> C2, M' = C2 ^ t*/ + for (i = 0; i < MlenCBLOCK; ++i) { + _M[i] = _C2[i] ^ t[i]; + } + /* B6: u = SM3(x2 || M' || y2), check if u = C3 */ + if (!sm3_init(&c) + || !BN_bn2binpad(x_2, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !sm3_update(&c, _M, MlenCBLOCK) + || !BN_bn2binpad(y_2, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !sm3_final(u, &c) + || memcmp(u, _C3, SM3_DIGEST_LENGTH) != 0) { + goto err; + } + /* B7: output M' */ + return 1; + err: + return 0; +} diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/enclave/sm2_exchange.h" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/enclave/sm2_exchange.h" new file mode 100644 index 0000000000000000000000000000000000000000..3e06a2481af3de3ca04b4c6f2de590a1b597a259 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/enclave/sm2_exchange.h" @@ -0,0 +1,335 @@ +#include "sm2.h" + +/* + * Ref: + * GB/T 32918.3-2016 + */ + +/* + * This function should **NOT** be used directly. + * The exchange process should obey the definition in GB/T 32918.3-2016 + */ +int sm2_exchange(const EC_KEY *userA, const EC_KEY *userB, const uint64 klen, const uint8 *ZA, const uint8 *ZB) { + int w = (BN_num_bits(userA->group->order) + 1) / 2 - 1; /* For n is not pow of 2 */ + size_t p_bytes = BN_num_bytes(userB->group->field), k_bytes = (klen >> 3) + ((klen & 7) ? 1 : 0), Zlen; + uint8 const_byte; + SM3_CTX c; + BN_CTX *ctx; + ctx = BN_CTX_new(); + if (ctx == NULL) { + return 0; + } + BN_CTX_start(ctx); + /* A-pre: */ + BIGNUM *rA, *x_1, *y_1, *tmpa, *tA; + const BIGNUM *nA, *dA, *hA; + EC_POINT *RA, *U, *tmp_pointA; + const EC_POINT *GA, *PA; + EC_GROUP *groupA; + uint8 *Z, *K, *buf, *tmp_char, *SA, *S1, *SB, *S2, *buf_digest; + SA = malloc(SM3_DIGEST_LENGTH); + S1 = malloc(SM3_DIGEST_LENGTH); + SB = malloc(SM3_DIGEST_LENGTH); + S2 = malloc(SM3_DIGEST_LENGTH); + buf_digest = malloc(SM3_DIGEST_LENGTH); + if (buf_digest == NULL) { + return 0; + } + groupA = userA->group; + nA = groupA->order; + dA = userA->priv_key; + hA = groupA->cofactor; + x_1 = BN_CTX_get(ctx); + y_1 = BN_CTX_get(ctx); + tmpa = BN_CTX_get(ctx); + tA = BN_CTX_get(ctx); + rA = BN_CTX_get(ctx); + GA = groupA->generator; + PA = userA->pub_key; + U = EC_POINT_new(); + tmp_pointA = EC_POINT_new(); + RA = EC_POINT_new(); + if (groupA == NULL + || nA == NULL + || dA == NULL + || hA == NULL + || rA == NULL + || GA == NULL + || PA == NULL + || RA == NULL) { + return 0; + } + /* A1: rA = rand(1, n-1) */ + do { + if (!BN_rand_range(rA, nA)) { + return 0; + } + } while(BN_is_zero(rA)); + /* A2: RA = [rA]G = (x1, y1) */ + if (!EC_POINT_mul(RA, GA, rA, groupA)) { + return 0; + } + /* A3: send RA to userB */ + /* B-pre: */ + BIGNUM *rB, *x_2, *y_2, *tmpb, *tB; + const BIGNUM *nB, *dB, *hB; + EC_POINT *RB, *V, *tmp_pointB; + const EC_POINT *GB, *PB; + EC_GROUP *groupB; + groupB = userB->group; + nB = groupB->order; + dB = userB->priv_key; + hB = groupB->cofactor; + x_2 = BN_CTX_get(ctx); + y_2 = BN_CTX_get(ctx); + tmpb = BN_CTX_get(ctx); + tB = BN_CTX_get(ctx); + rB = BN_CTX_get(ctx); + GB = groupB->generator; + PB = userB->pub_key; + V = EC_POINT_new(); + tmp_pointB = EC_POINT_new(); + RB = EC_POINT_new(); + if (groupB == NULL + || nB == NULL + || dB == NULL + || hB == NULL + || rB == NULL + || GB == NULL + || PB == NULL + || RB == NULL) { + return 0; + } + /* B1: rB = rand(1, n-1) */ + do { + if (!BN_rand_range(rB, nB)) { + return 0; + } + } while(BN_is_zero(rB)); + /* B2: RB = [rB]G = (x2, y2) */ + if (!EC_POINT_mul(RB, GB, rB, groupB)) { + return 0; + } + /* B3: \bar{x_2} = 2^w + (x_2 & (2^w - 1)) */ + if (!BN_one(tmpb) + || !BN_lshift(tmpb, tmpb, w) + || !BN_mod(x_2, RB->X, tmpb, ctx) + || !BN_add(x_2, x_2, tmpb)) { + return 0; + } + /* B4: tB = (dB + \bar{x_2} \times rB) % n */ + if (!BN_mod_mul(tB, x_2, rB, nB, ctx) + || !BN_mod_add_quick(tB, tB, dB, nB)) { + return 0; + } + /* B5: verify RA is on curve, \bar{x_1} = 2^w + (x_1 & (2^w - 1)) */ + if (!EC_POINT_on_curve_simple(RA, groupB)) { + return 0; + } + if (!BN_one(tmpb) + || !BN_lshift(tmpb, tmpb, w) + || !BN_mod(x_1, RA->X, tmpb, ctx) + || !BN_add(x_1, x_1, tmpb)) { + return 0; + } + /* B6: V = [h \times tB](PA + [\bar{x1}]RA) = (xV, yV) */ + if (!EC_POINT_mul(tmp_pointB, RA, x_1, groupB) + || !EC_POINT_add(tmp_pointB, tmp_pointB, PA, groupB) + || !BN_mul(tmpb, hB, tB, ctx) + || !EC_POINT_mul(V, tmp_pointB, tmpb, groupB) + || EC_POINT_is_infinity(V)) { + return 0; + } + /* B7: KB = KDF(xV || yV || ZA || ZB, klen) */ + K = malloc((klen + 7) >> 3); + Zlen = (p_bytes + SM3_DIGEST_LENGTH) * 2; + Z = malloc(Zlen); + buf = malloc(p_bytes); + tmp_char = Z; + if (!BN_bn2binpad(V->X, buf, p_bytes) + || !memcpy(tmp_char, buf, p_bytes) + || !(tmp_char += p_bytes) + || !BN_bn2binpad(V->Y, buf, p_bytes) + || !memcpy(tmp_char, buf, p_bytes) + || !(tmp_char += p_bytes) + || !memcpy(tmp_char, ZA, SM3_DIGEST_LENGTH) + || !(tmp_char += p_bytes) + || !memcpy(tmp_char, ZB, SM3_DIGEST_LENGTH)) { + return 0; + } + if (!sm2_KDF(K, Z, Zlen, klen)) { + return 0; + } + #ifdef SM2_EXCHANGE_OPTIONAL + /* B8: SB = SM3(0x02 || yV || SM3(xV || ZA || ZB || x1 || y1 || x2 || y2)) (optional) */ + if (!sm3_init(&c) + || !BN_bn2binpad(V->X, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !sm3_update(&c, ZA, SM3_DIGEST_LENGTH) + || !sm3_update(&c, ZB, SM3_DIGEST_LENGTH) + || !BN_bn2binpad(RA->X, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !BN_bn2binpad(RA->Y, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !BN_bn2binpad(RB->X, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !BN_bn2binpad(RB->Y, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !sm3_final(buf_digest, &c)) { + return 0; + } + const_byte = 2; + if (!sm3_init(&c) + || !(sm3_update(&c, &const_byte, 1), 1) + || !BN_bn2binpad(V->Y, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !sm3_update(&c, buf_digest, SM3_DIGEST_LENGTH) + || !sm3_final(SB, &c)) { + return 0; + } + #endif + /* B9: Send RB, (and SB, optionals) to userA */ + /* A4: \bar{x_1} = 2^w + (x_1 & (2^w - 1)) */ + if (!BN_one(tmpa) + || !BN_lshift(tmpa, tmpa, w) + || !BN_mod(x_1, RA->X, tmpa, ctx) + || !BN_add(x_1, x_1, tmpa)) { + return 0; + } + /* A5: tA = (dA + \bar{x_1} \times rA) % n */ + if (!BN_mod_mul(tA, x_1, rA, nA, ctx) + || !BN_mod_add_quick(tA, tA, dA, nA)) { + return 0; + } + /* A6: verify RB is on curve, \bar{x_2} = 2^w + (x_2 & (2^w - 1)) */ + if (!EC_POINT_on_curve_simple(RB, groupA)) { + return 0; + } + if (!BN_one(tmpa) + || !BN_lshift(tmpa, tmpa, w) + || !BN_mod(x_2, RB->X, tmpa, ctx) + || !BN_add(x_2, x_2, tmpa)) { + return 0; + } + /* A7: U = [h \times tA](PB + [\bar{x2}]RB) = (xU, yU) */ + if (!EC_POINT_mul(tmp_pointA, RB, x_2, groupA) + || !EC_POINT_add(tmp_pointA, tmp_pointA, PB, groupA) + || !BN_mul(tmpa, hA, tA, ctx) + || !EC_POINT_mul(U, tmp_pointA, tmpa, groupA) + || EC_POINT_is_infinity(U)) { + return 0; + } + /* A8: KA = KDF(xU || yU || ZA || ZB, klen) */ + K = malloc((klen + 7) >> 3); + Zlen = (p_bytes + SM3_DIGEST_LENGTH) * 2; + Z = malloc(Zlen); + buf = malloc(p_bytes); + tmp_char = Z; + if (!BN_bn2binpad(U->X, buf, p_bytes) + || !memcpy(tmp_char, buf, p_bytes) + || !(tmp_char += p_bytes) + || !BN_bn2binpad(U->Y, buf, p_bytes) + || !memcpy(tmp_char, buf, p_bytes) + || !(tmp_char += p_bytes) + || !memcpy(tmp_char, ZA, SM3_DIGEST_LENGTH) + || !(tmp_char += p_bytes) + || !memcpy(tmp_char, ZB, SM3_DIGEST_LENGTH)) { + return 0; + } + if (!sm2_KDF(K, Z, Zlen, klen)) { + return 0; + } + #ifdef SM2_EXCHANGE_OPTIONAL + /* A9: S1 = SM3(0x02 || yU || SM3(xU || ZA || ZB || x1 || y1 || x2 || y2)), verify S1==SB (optional) */ + if (!sm3_init(&c) + || !BN_bn2binpad(U->X, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !sm3_update(&c, ZA, SM3_DIGEST_LENGTH) + || !sm3_update(&c, ZB, SM3_DIGEST_LENGTH) + || !BN_bn2binpad(RA->X, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !BN_bn2binpad(RA->Y, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !BN_bn2binpad(RB->X, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !BN_bn2binpad(RB->Y, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !sm3_final(buf_digest, &c)) { + return 0; + } + const_byte = 2; + if (!sm3_init(&c) + || !(sm3_update(&c, &const_byte, 1), 1) + || !BN_bn2binpad(V->Y, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !sm3_update(&c, buf_digest, SM3_DIGEST_LENGTH) + || !sm3_final(S1, &c)) { + return 0; + } + if (memcmp(S1, SB, SM3_DIGEST_LENGTH) != 0) { + return 0; + } + #endif + #ifdef SM2_EXCHANGE_OPTIONAL + /* A10: SA = SM3(0x03 || yU || SM3(xU || ZA || ZB || x1 || y1 || x2 || y2)) (optional) */ + if (!sm3_init(&c) + || !BN_bn2binpad(U->X, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !sm3_update(&c, ZA, SM3_DIGEST_LENGTH) + || !sm3_update(&c, ZB, SM3_DIGEST_LENGTH) + || !BN_bn2binpad(RA->X, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !BN_bn2binpad(RA->Y, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !BN_bn2binpad(RB->X, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !BN_bn2binpad(RB->Y, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !sm3_final(buf_digest, &c)) { + return 0; + } + const_byte = 3; + if (!sm3_init(&c) + || !(sm3_update(&c, &const_byte, 1), 1) + || !BN_bn2binpad(U->Y, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !sm3_update(&c, buf_digest, SM3_DIGEST_LENGTH) + || !sm3_final(SA, &c)) { + return 0; + } + #endif + #ifdef SM2_EXCHANGE_OPTIONAL + /* B10: S2 = SM3(0x03 || yV || SM3(xV || ZA || ZB || x1 || y1 || x2 || y2)), verify S2==SA (optional) */ + if (!sm3_init(&c) + || !BN_bn2binpad(V->X, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !sm3_update(&c, ZA, SM3_DIGEST_LENGTH) + || !sm3_update(&c, ZB, SM3_DIGEST_LENGTH) + || !BN_bn2binpad(RA->X, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !BN_bn2binpad(RA->Y, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !BN_bn2binpad(RB->X, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !BN_bn2binpad(RB->Y, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !sm3_final(buf_digest, &c)) { + return 0; + } + const_byte = 3; + if (!sm3_init(&c) + || !(sm3_update(&c, &const_byte, 1), 1) + || !BN_bn2binpad(V->Y, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !sm3_update(&c, buf_digest, SM3_DIGEST_LENGTH) + || !sm3_final(S2, &c)) { + return 0; + } + if (memcmp(S2, SA, SM3_DIGEST_LENGTH) != 0) { + return 0; + } + #endif + return 1; + fail: + return 0; +} diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/enclave/sm2_sign.h" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/enclave/sm2_sign.h" new file mode 100644 index 0000000000000000000000000000000000000000..c9016036e5009d64164b07257999050c285dedd6 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/enclave/sm2_sign.h" @@ -0,0 +1,189 @@ +#include "sm2.h" + +/* + * Ref: + * GB/T 32918.2-2016 + */ + +/** SM2 signature generation. + * \param msg massage for signing. + * \param msglen byte length of msg. + * \param id identifier of user A. + * \param idlen byte length of id. + * \param key key of user A. + * \return the sign generated. + */ +SM2_SIG *sm2_sig_gen(const uint8 *msg, const size_t msglen, const uint8 *id, const size_t idlen, const EC_KEY *key) { + SM3_CTX c; + sm3_init(&c); + uint8 *buf; + BN_CTX *ctx; + BIGNUM *e, *n, *k, *x, *r, *s, *rk, *dA, *tmp; + EC_POINT *kG, *G; + SM2_SIG *sig; + ctx = BN_CTX_new(); + if (ctx == NULL) { + return 0; + } + sig = SM2_SIG_new(); + if (sig == NULL) { + return 0; + } + BN_CTX_start(ctx); + e = BN_CTX_get(ctx); + n = key->group->order; + k = BN_CTX_get(ctx); + x = BN_CTX_get(ctx); + r = sig->r; + s = sig->s; + rk = BN_CTX_get(ctx); + dA = key->priv_key; + tmp = BN_CTX_get(ctx); + G = key->group->generator; + kG = EC_POINT_new(); + if (n == NULL + || r == NULL + || s == NULL + || dA == NULL + || tmp == NULL + || G == NULL + || kG == NULL) { + return 0; + } + buf = malloc(SM3_DIGEST_LENGTH); + /* A1: \bar{M} = ZA || M */ + if (!sm2_compute_z(buf, id, idlen, key)) { + goto err; + } + if (!sm3_update(&c, buf, SM3_DIGEST_LENGTH) + || !sm3_update(&c, msg, msglen)) { + SM2ERR("sm2_sign(): compute e error!\n"); + } + /* A2: e = SM3(\bar{M}), e -> bn */ + sm3_final(buf, &c); + e = BN_bin2bn(buf, SM3_DIGEST_LENGTH, NULL); + if (e == NULL) { + SM2ERR("sm2_sign(): e error!\n"); + } + for (;;) { + /* A3: k = rand(1, n-1) */ + if (!BN_rand_range(k, n)) { + SM2ERR("sm2_sign(): gen k error!\n"); + } + if (BN_is_zero(k)) continue; + /* A4: (x1, y1) = [k]G, x1 -> bn */ + if (!EC_POINT_mul(kG, G, k, key->group)) { + return 0; + } + x = kG->X; + /* A5: r = (e + x1) % n, if r=0 or r+k=n, goto A3 */ + if (!BN_mod_add(r, e, x, n, ctx)) { + SM2ERR("sm2_sign(): internal error!\n"); + } + if (BN_is_zero(r)) continue; + if (!BN_add(rk, r, k)) { + SM2ERR("sm2_sign(): internal error!\n"); + } + if (BN_cmp(rk, n) == 0) continue; + /* A6: s = ((1+dA)^-1 \times (k- r \times dA)) % n, if s=0, goto A3 */ + if (!BN_add(s, dA, BN_value_one()) + || !BN_mod_inverse(s, s, n, ctx) + || !printf("(1+dA)^-1: %s\n", BN_bn2hex(s)) + || !BN_mod_mul(tmp, dA, r, n, ctx) + || !BN_sub(tmp, k, tmp) + || !BN_mod_mul(s, s, tmp, n, ctx)) { + SM2ERR("sm2_sign(): internal error!\n"); + } + if (BN_is_zero(s)) continue; + /* A7: r,s -> byte/u8[], sig = (r, s) */ + break; + } + err: + done: + BN_free(e); + BN_CTX_free(ctx); + return sig; +} + +/* (2) Sign Verify */ + +/** SM2 signature verification. + * \param msg massage for signing. + * \param msglen byte length of msg. + * \param id identifier of user A. + * \param idlen byte length of id. + * \param key key of user A. + * \param sig signature to be verified. + * \return 1 on success and 0 if an error occurred. + */ +int sm2_sig_verify(const uint8 *msg, const size_t msglen, const uint8 *id, const size_t idlen, const EC_KEY *key, const SM2_SIG *sig) { + SM3_CTX c; + sm3_init(&c); + BN_CTX *ctx; + const BIGNUM *r, *s, *n; + BIGNUM *e, *t, *R; + const EC_POINT *G, *PA; + EC_POINT *sG, *tPA, *tmp_point; + uint8 *buf; + r = sig->r; + s = sig->s; + n = key->group->order; + G = key->group->generator; + PA = key->pub_key; + ctx = BN_CTX_new(); + if (ctx == NULL) { + goto err; + } + BN_CTX_start(ctx); + e = BN_CTX_get(ctx); + t = BN_CTX_get(ctx); + R = BN_CTX_get(ctx); + sG = EC_POINT_new(); + tPA = EC_POINT_new(); + tmp_point = EC_POINT_new(); + if (R == NULL || tmp_point == NULL) { + goto err; + } + buf = malloc(SM3_DIGEST_LENGTH); + /* B1: check r' \in [1, n-1], otherwise goto BERR */ + /* B2: check s' \in [1, n-1], otherwise goto BERR */ + if (BN_cmp(r, BN_value_one()) < 0 + || BN_cmp(s, BN_value_one()) < 0 + || BN_cmp(n, r) <= 0 + || BN_cmp(n, s) <= 0) { + goto err; + } + /* B3: \bar{M'} = ZA || M' */ + if (!sm2_compute_z(buf, id, idlen, key)) { + goto err; + } + if (!sm3_update(&c, buf, SM3_DIGEST_LENGTH) + || !sm3_update(&c, msg, msglen)) { + SM2ERR("sm2_sign(): compute e error!\n"); + } + /* B4: e' = SM3(\bar{M'}), e -> bn */ + sm3_final(buf, &c); + e = BN_bin2bn(buf, SM3_DIGEST_LENGTH, NULL); + /* B5: r', s' -> bn; t = (r'+s')%n, if t=0, goto BERR */ + if (!BN_mod_add_quick(t, r, s, n) + || BN_is_zero(t)) { + goto err; + } + /* B6: (x1', y1') = [s']G + [t]PA */ + if (!EC_POINT_mul(sG, G, s, key->group) + || !EC_POINT_mul(tPA, PA, t, key->group) + || !EC_POINT_add(tmp_point, sG, tPA, key->group)) { + goto err; + } + /* B7: x1' -> bn; R = (e' + x1')%n, return R=r' */ + if (!BN_mod_add_quick(R, e, tmp_point->X, n)) { + goto err; + } + if (BN_cmp(R, r) == 0) { + return 1; + } + err: + BN_CTX_end(ctx); + /* BERR: not pass, return 0 */ + return 0; +} diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/enclave/sm2_tests.h" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/enclave/sm2_tests.h" new file mode 100644 index 0000000000000000000000000000000000000000..922559d541067fa26cc70bba7d7c570b73946c39 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/enclave/sm2_tests.h" @@ -0,0 +1,152 @@ +#ifndef SECGEAR_SM2_TESTS_H +#define SECGEAR_SM2_TESTS_H + +#include "sm2_sign.h" +#include "sm2_exchange.h" +#include "sm2_crypt.h" + +uint8 *sign_buffer, sign_verify_ret; + +void sm2signtest(const int p_bytes, const uint8 *_M, const uint8 *_ID_A, const uint8 *_p, const uint8 *_a, const uint8 *_b, const uint8 *_n, const uint8 *_XG, const uint8 *_YG, const uint8 *_dA, const uint8 *_k) { + /* A.3 is not supported */ + int _w; + uint16 entlenC = strlen((char *)_ID_A); + uint16 entlA = entlenC*8; + BIGNUM *p, *a, *b, *n, *xG, *yG, *dA, *k; + EC_POINT *G, *kG; + EC_GROUP *group; + EC_KEY *userA; + group = EC_GROUP_new(); + p = BN_new(); + a = BN_new(); + b = BN_new(); + n = BN_new(); + xG = BN_new(); + yG = BN_new(); + dA = BN_new(); + k = BN_new(); + G = EC_POINT_new(); + kG = EC_POINT_new(); + userA = EC_KEY_new(); + BN_bin2bn(_p, p_bytes, p); + BN_bin2bn(_a, p_bytes, a); + BN_bin2bn(_b, p_bytes, b); + BN_bin2bn(_n, p_bytes, n); + BN_bin2bn(_XG, p_bytes, xG); + BN_bin2bn(_YG, p_bytes, yG); + BN_bin2bn(_dA, p_bytes, dA); + BN_bin2bn(_k, p_bytes, k); + EC_POINT_setXY(G, xG, yG); + _w = EC_GROUP_set_param(group, p, a, b, n, BN_value_one(), G); + _w = EC_KEY_set_group(userA, group); + _w = EC_KEY_set_priv_key(userA, dA); + _w = EC_KEY_generate_pub_key(userA); + _w = EC_POINT_mul(kG, G, k, group); + SM2_SIG *sig; + sig = sm2_sig_gen(_M, strlen((char *)_M), _ID_A, strlen((char *)_ID_A), userA); + sign_buffer = (uint8 *)malloc(p_bytes * 4); + memcpy(sign_buffer, BN_bn2hex(sig->r), p_bytes * 2); + memcpy(sign_buffer + p_bytes * 2, BN_bn2hex(sig->s), p_bytes * 2); + + _w = sm2_sig_verify(_M, strlen((char *)_M), _ID_A, strlen((char *)_ID_A), userA, sig); + sign_verify_ret = _w; +} + +uint8 exchange_ret; + +/* h = 1 by default */ +void sm2exchangetest(const int p_bytes, const uint8 *_ID_A, const uint8 *_ID_B, const uint8 *_p, const uint8 *_a, const uint8 *_b, const uint8 *_n, const uint8 *_XG, const uint8 *_YG, const uint8 *_dA, const uint8 *_dB, const uint64 klen) { + /* A.3 is not supported */ + int _w; + uint16 entlenAC = strlen((char *)_ID_A); + uint16 entlA = entlenAC*8; + uint16 entlenBC = strlen((char *)_ID_B); + uint16 entlB = entlenBC*8; + BIGNUM *p, *a, *b, *h, *n, *xG, *yG, *dA, *dB; + EC_POINT *G, *kG; + EC_GROUP *group; + EC_KEY *userA, *userB; + uint8 *ZA, *ZB; + ZA = malloc(SM3_DIGEST_LENGTH); + ZB = malloc(SM3_DIGEST_LENGTH); + group = EC_GROUP_new(); + p = BN_new(); + a = BN_new(); + b = BN_new(); + h = (BIGNUM *)BN_value_one(); + n = BN_new(); + xG = BN_new(); + yG = BN_new(); + dA = BN_new(); + dB = BN_new(); + G = EC_POINT_new(); + kG = EC_POINT_new(); + userA = EC_KEY_new(); + userB = EC_KEY_new(); + BN_bin2bn(_p, p_bytes, p); + BN_bin2bn(_a, p_bytes, a); + BN_bin2bn(_b, p_bytes, b); + BN_bin2bn(_n, p_bytes, n); + BN_bin2bn(_XG, p_bytes, xG); + BN_bin2bn(_YG, p_bytes, yG); + BN_bin2bn(_dA, p_bytes, dA); + BN_bin2bn(_dB, p_bytes, dB); + EC_POINT_setXY(G, xG, yG); + _w = EC_GROUP_set_param(group, p, a, b, n, h, G); + _w = EC_KEY_set_group(userA, group); + _w = EC_KEY_set_priv_key(userA, dA); + _w = EC_KEY_generate_pub_key(userA); + _w = EC_KEY_set_group(userB, group); + _w = EC_KEY_set_priv_key(userB, dB); + _w = EC_KEY_generate_pub_key(userB); + _w = sm2_compute_z(ZA, _ID_A, strlen((char *)_ID_A), userA); + _w = sm2_compute_z(ZB, _ID_B, strlen((char *)_ID_B), userB); + _w = sm2_exchange(userA, userB, klen, ZA, ZB); + exchange_ret = _w; +} + +uint8 *encrypt_pw, *decrypt_msg; + +void sm2crypttest(int p_bytes, const uint8 *M, const uint8 *_p, const uint8 *_a, const uint8 *_b, const uint8 *_n, const uint8 *_XG, const uint8 *_YG, const uint8 *_dB) { + /* A.3 is not supported */ + int _w; + BIGNUM *p, *a, *b, *n, *xG, *yG, *dB; + EC_POINT *G; + EC_GROUP *group; + EC_KEY *userB; + uint64 klen; + int MlenCBLOCK; + size_t pwlen; + group = EC_GROUP_new(); + p = BN_new(); + a = BN_new(); + b = BN_new(); + n = BN_new(); + xG = BN_new(); + yG = BN_new(); + dB = BN_new(); + G = EC_POINT_new(); + userB = EC_KEY_new(); + BN_bin2bn(_p, p_bytes, p); + BN_bin2bn(_a, p_bytes, a); + BN_bin2bn(_b, p_bytes, b); + BN_bin2bn(_n, p_bytes, n); + BN_bin2bn(_XG, p_bytes, xG); + BN_bin2bn(_YG, p_bytes, yG); + BN_bin2bn(_dB, p_bytes, dB); + EC_POINT_setXY(G, xG, yG); + MlenCBLOCK = strlen((char *)M); + _w = EC_GROUP_set_param(group, p, a, b, n, BN_value_one(), G); + _w = EC_KEY_set_group(userB, group); + _w = EC_KEY_set_priv_key(userB, dB); + _w = EC_KEY_generate_pub_key(userB); + klen = MlenCBLOCK * 8; + pwlen = (p_bytes << 1 | 1) + MlenCBLOCK + SM3_DIGEST_LENGTH; + encrypt_pw = malloc(pwlen); + _w = sm2_encrypt(encrypt_pw, M, klen, userB); + decrypt_msg = malloc(MlenCBLOCK+1); + memset(decrypt_msg, 0, MlenCBLOCK+1); + _w = sm2_decrypt(decrypt_msg, encrypt_pw, klen, userB); +} + +#endif \ No newline at end of file diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/enclave_SM2.edl" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/enclave_SM2.edl" new file mode 100644 index 0000000000000000000000000000000000000000..87d3e6527245fea773722f28c3dab4106278902e --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/enclave_SM2.edl" @@ -0,0 +1,30 @@ +enclave { + include "secgear_urts.h" + from "secgear_tstdc.edl" import *; + + trusted { + public void callsm2signtest(); + public void callsm2exchangetest(); + public void callsm2crypttest(); + public void setSignIDA([in, size=len] uint8 *src, size_t len); + public void setSignM([in, size=len] uint8 *src, size_t len); + public void setSignDA([in, count=32] uint8 *src); + public void setSignK([in, count=32] uint8 *src); + public void getSignResult([out, size=len] uint8 *buf, size_t len); + public void getSignVerifyResult([out, count=1] uint8 buf); + public void setExchangeIDA([in, size=len] uint8 *src, size_t len); + public void setExchangeIDB([in, size=len] uint8 *src, size_t len); + public void setExchangeDA([in, count=32] uint8 *src); + public void setExchangeDB([in, count=32] uint8 *src); + public void setExchangeKLen([in, count=1] uint64 *src); + public void getExchangeResult([out, count=1] uint8 buf); + public void setCryptM([in, size=len] uint8 *src, size_t len); + public void setCryptDB([in, count=32] uint8 *src); + public void getCryptPW([out, size=len] uint8 *buf, size_t len); + public void getCryptMSG([out, size=len] uint8 *buf, size_t len); + }; + + untrusted { + + }; +}; diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/host/CMakeLists.txt" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/host/CMakeLists.txt" new file mode 100644 index 0000000000000000000000000000000000000000..0d42633f0242abb9b09efd3c582d366f98bba224 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/host/CMakeLists.txt" @@ -0,0 +1,88 @@ +# Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved. +# secGear is licensed under the Mulan PSL v2. +# You can use this software according to the terms and conditions of the Mulan PSL v2. +# You may obtain a copy of Mulan PSL v2 at: +# http://license.coscl.org.cn/MulanPSL2 +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR +# PURPOSE. +# See the Mulan PSL v2 for more details. + +#set auto code prefix +set(PREFIX enclave_SM2) +#set host exec name +set(OUTPUT secgear_enclave_SM2) +#set host src code +set(SOURCE_FILE ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp) + +set(CMAKE_CXX_STANDARD 11) + +#set auto code +if(CC_GP) + set(AUTO_FILES ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_u.h ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_u.c ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_args.h) + add_custom_command(OUTPUT ${AUTO_FILES} + DEPENDS ${CURRENT_ROOT_PATH}/${EDL_FILE} + COMMAND ${CODEGEN} --${CODETYPE} --untrusted ${CURRENT_ROOT_PATH}/${EDL_FILE} --search-path ${LOCAL_ROOT_PATH}/inc/host_inc/gp) +endif() + +if(CC_SGX) + set(AUTO_FILES ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_u.h ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_u.c) + add_custom_command(OUTPUT ${AUTO_FILES} + DEPENDS ${CURRENT_ROOT_PATH}/${EDL_FILE} + COMMAND ${CODEGEN} --${CODETYPE} --untrusted ${CURRENT_ROOT_PATH}/${EDL_FILE} --search-path ${LOCAL_ROOT_PATH}/inc/host_inc/sgx --search-path ${SGXSDK}/include) +endif() + +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIE") +set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -O2 -fPIE") +set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -s") + +if(CC_GP) + if(${CMAKE_VERSION} VERSION_LESS "3.13.0") + link_directories(${SECGEAR_INSTALL_PATH}) + endif() + add_executable(${OUTPUT} ${SOURCE_FILE} ${AUTO_FILES}) + target_include_directories(${OUTPUT} PRIVATE + ${LOCAL_ROOT_PATH}/inc/host_inc + ${LOCAL_ROOT_PATH}/inc/host_inc/gp + ${CMAKE_CURRENT_BINARY_DIR}) + if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.13.0") + target_link_directories(${OUTPUT} PRIVATE ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}) + endif() +endif() + +if(CC_SGX) + if(${CMAKE_VERSION} VERSION_LESS "3.13.0") + link_directories(${CMAKE_LIBRARY_OUTPUT_DIRECTORY}) + endif() + add_executable(${OUTPUT} ${SOURCE_FILE} ${AUTO_FILES}) + target_include_directories(${OUTPUT} PRIVATE + ${LOCAL_ROOT_PATH}/inc/host_inc + ${LOCAL_ROOT_PATH}/inc/host_inc/sgx + ${CMAKE_CURRENT_BINARY_DIR}) + if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.13.0") + target_link_directories(${OUTPUT} PRIVATE ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}) + endif() +endif() + +if(CC_SIM) + target_link_libraries(${OUTPUT} secgearsim) +else() + target_link_libraries(${OUTPUT} secgear) +endif() +set_target_properties(${OUTPUT} PROPERTIES SKIP_BUILD_RPATH TRUE) + +if(CC_GP) + #itrustee install whitelist /vender/bin/teec_hello + install(TARGETS ${OUTPUT} + RUNTIME + DESTINATION /vendor/bin/ + PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ) +endif() + +if(CC_SGX) + install(TARGETS ${OUTPUT} + RUNTIME + DESTINATION ${CMAKE_BINARY_DIR}/bin/ + PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ) +endif() + diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/host/main.cpp" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/host/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..ef1710ed7add7aab01e04c59703b5d4077866572 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/host/main.cpp" @@ -0,0 +1,127 @@ +#include"enclave.h" +#include"Enclave1_u.h" +#include +#include + +#define MAX_LEN 1024 + +#define SM3_DIGEST_LENGTH 32 +const int SM2_p_bytes = 256 >> 3; +const uint8 sign_ID_A[] = "1234567812345678"; +const uint8 sign_M[] = "message digest"; +const uint8 sign_dA[] = { + 0x39, 0x45, 0x20, 0x8F, + 0x7B, 0x21, 0x44, 0xB1, + 0x3F, 0x36, 0xE3, 0x8A, + 0xC6, 0xD3, 0x9F, 0x95, + 0x88, 0x93, 0x93, 0x69, + 0x28, 0x60, 0xB5, 0x1A, + 0x42, 0xFB, 0x81, 0xEF, + 0x4D, 0xF7, 0xC5, 0xB8, +}; +const uint8 sign_k[] = { + 0x59, 0x27, 0x6E, 0x27, + 0xD5, 0x06, 0x86, 0x1A, + 0x16, 0x68, 0x0F, 0x3A, + 0xD9, 0xC0, 0x2D, 0xCC, + 0xEF, 0x3C, 0xC1, 0xFA, + 0x3C, 0xDB, 0xE4, 0xCE, + 0x6D, 0x54, 0xB8, 0x0D, + 0xEA, 0xC1, 0xBC, 0x21, +}; + +const uint8 exchange_ID_A[] = "1234567812345678"; +const uint8 exchange_ID_B[] = "1234567812345678"; +const uint8 exchange_dA[] = { + 0x81, 0xEB, 0x26, 0xE9, + 0x41, 0xBB, 0x5A, 0xF1, + 0x6D, 0xF1, 0x16, 0x49, + 0x5F, 0x90, 0x69, 0x52, + 0x72, 0xAE, 0x2C, 0xD6, + 0x3D, 0x6C, 0x4A, 0xE1, + 0x67, 0x84, 0x18, 0xBE, + 0x48, 0x23, 0x00, 0x29, +}; +const uint8 exchange_dB[] = { + 0x78, 0x51, 0x29, 0x91, + 0x7D, 0x45, 0xA9, 0xEA, + 0x54, 0x37, 0xA5, 0x93, + 0x56, 0xB8, 0x23, 0x38, + 0xEA, 0xAD, 0xDA, 0x6C, + 0xEB, 0x19, 0x90, 0x88, + 0xF1, 0x4A, 0xE1, 0x0D, + 0xEF, 0xA2, 0x29, 0xB5, +}; +const uint64 exchange_klen = 128; + +const uint8 crypt_M[] = "encryption standard"; +const uint8 crypt_dB[] = { + 0x39, 0x45, 0x20, 0x8F, + 0x7B, 0x21, 0x44, 0xB1, + 0x3F, 0x36, 0xE3, 0x8A, + 0xC6, 0xD3, 0x9F, 0x95, + 0x88, 0x93, 0x93, 0x69, + 0x28, 0x60, 0xB5, 0x1A, + 0x42, 0xFB, 0x81, 0xEF, + 0x4D, 0xF7, 0xC5, 0xB8, +}; + +int main(int argc, char* argv[]) { + cc_enclave_t *eid; + cc_enclave_result_t ret; + char *path = transferFromString(PATH); + + ret = cc_enclave_create(path, AUTO_ENCLAVE_TYPE, 0, SECGEAR_DEBUG_FLAG, NULL, 0, &eid); + if (ret != CC_SUCCESS) { + printf("ERR: %#x ,failed to create enclave.\n", ret); + return -1; + } + + u_int8_t buffer[MAX_LEN], tmpu8; + memset(buffer, 0, sizeof buffer); + printf("A.2 start: \n"); + setSignIDA(eid, sign_ID_A, strlen(sign_ID_A)); + setSignM(eid, sign_M, strlen(sign_M)); + setSignDA(eid, sign_dA); + setSignK(eid, sign_k); + callsm2signtest(eid); + getSignResult(eid, buffer, MAX_LEN); + printf("sign sig: %s\n", buffer); + getSignVerifyResult(eid, tmpu8); + printf("sign verify: %x\n", tmpu8); + printf("A.2 end: \n\n\n"); + /* PASSED */ + + printf("B.2 start: \n"); + setExchangeIDA(eid, exchange_ID_A, strlen(exchange_ID_A)); + setExchangeIDB(eid, exchange_ID_B, strlen(exchange_ID_B)); + setExchangeDA(eid, exchange_dA); + setExchangeDB(eid, exchange_dB); + setExchangeKLen(eid, exchange_klen); + callsm2exchangetest(eid); + getExchangeResult(eid, tmpu8); + printf("exchange: %x\n", tmpu8); + printf("B.2 end: \n\n\n"); + /* PASSED */ + + printf("C.2 start: \n"); + setCryptM(eid, crypt_M, strlen(crypt_M)); + setCryptDB(eid, crypt_dB); + callsm2crypttest(eid); + int pwlen = (SM2_p_bytes << 1 | 1) + strlen(crypt_M) + SM3_DIGEST_LENGTH; + getCryptPW(eid, buffer, pwlen); + printf("encrypt pw: "); + for (int i=0; i +#define debugPrint (printf("%s: %s: %d\n",__FILE__,__FUNCTION__,__LINE__),fflush(stdout),1) + +typedef __uint8_t uint8; +typedef __uint16_t uint16; +typedef __uint32_t uint32; +typedef __uint64_t uint64; + +#define CONCAT_8x4(x0, x1, x2, x3) ((((uint32)(x0) << 8 | (x1)) << 8 | (x2)) << 8 | (x3)) // X = (x0 || x1 || x2 || x3) +#define CONCAT_16x2(x0, x1) ((uint32)(x0) << 16 | (x1)) // X = (x0 || x1) +#define EXTRACT_8_32(X, i) (((X) >> (((i) ^ 3) << 3)) & 0xFF) // X = (x0 || x1 || x2 || x3) +#define EXTRACT_16_32(X, i) (((X) >> (((i) ^ 1) << 4)) & 0xFFFF) // X = (x0 || x1) +#define EXTRACT_16H_31(X) (((X) >> 15) & 0xFFFF) // Higher 16-bit from 31-bit +#define EXTRACT_16L_31(X) ((X) & 0xFFFF) // Lower 16-bit from 31-bit + +#define ROTATE(a, n) (((a) << (n)) | (((a) & 0xFFFFFFFF) >> ((32 - (n)) & 31))) +#define ROTATE_31(a, n) ((((a) << (n)) & 0x7FFFFFFF) | (((a) & 0x7FFFFFFF) >> (31 - (n)))) + +#define GET_BIT_VALUE_32(x, i) (((x) >> (31 - (i))) & 1) + +const uint32 PREFIX_MASK_32[32] = { + 0x00000000, + 0x80000000, + 0xC0000000, + 0xE0000000, + 0xF0000000, + 0xF8000000, + 0xFC000000, + 0xFE000000, + 0xFF000000, + 0xFF800000, + 0xFFC00000, + 0xFFE00000, + 0xFFF00000, + 0xFFF80000, + 0xFFFC0000, + 0xFFFE0000, + 0xFFFF0000, + 0xFFFF8000, + 0xFFFFC000, + 0xFFFFE000, + 0xFFFFF000, + 0xFFFFF800, + 0xFFFFFC00, + 0xFFFFFE00, + 0xFFFFFF00, + 0xFFFFFF80, + 0xFFFFFFC0, + 0xFFFFFFE0, + 0xFFFFFFF0, + 0xFFFFFFF8, + 0xFFFFFFFC, + 0xFFFFFFFE +}; + +# define HOST_c2l(c,l) (l =(((unsigned long)(*((c)++)))<<24), \ + l|=(((unsigned long)(*((c)++)))<<16), \ + l|=(((unsigned long)(*((c)++)))<< 8), \ + l|=(((unsigned long)(*((c)++))) ) ) +# define HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l)>>24)&0xff), \ + *((c)++)=(unsigned char)(((l)>>16)&0xff), \ + *((c)++)=(unsigned char)(((l)>> 8)&0xff), \ + *((c)++)=(unsigned char)(((l) )&0xff), \ + l) + +#endif \ No newline at end of file diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/include/ec.h" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/include/ec.h" new file mode 100644 index 0000000000000000000000000000000000000000..56adbe751f17342f84c17e3736f8806deb7f40b5 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/include/ec.h" @@ -0,0 +1,545 @@ +#include /* BigNum */ +#include "common_utils.h" + +typedef struct ec_point_st { + BIGNUM *X; + BIGNUM *Y; + int is_infinity; +} EC_POINT; + +int EC_POINT_init(EC_POINT *point) { + point->X = BN_new(); + point->Y = BN_new(); + point->is_infinity = 0; + if (point->X == NULL || point->Y == NULL) { + BN_free(point->X); + BN_free(point->Y); + return 0; + } + return 1; +} + +EC_POINT *EC_POINT_new() { + EC_POINT *ret = NULL; + ret = malloc(sizeof(*ret)); + if (ret == NULL) { + return NULL; + } + if (!EC_POINT_init(ret)) { + free(ret); + return NULL; + } + return ret; +} + +int EC_POINT_setXY(EC_POINT *r, const BIGNUM *x, const BIGNUM *y) { + if (r == NULL) { + return 0; + } + if (!BN_copy(r->X, x) || !(BN_copy(r->Y, y))) { + return 0; + } + return 1; +} + +const EC_POINT *EC_point_infinity() { + static const EC_POINT const_infinity = { NULL, NULL, 1 }; + return &const_infinity; +} + +int EC_POINT_is_infinity(const EC_POINT *point) { + return point->is_infinity; +} + +int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src) { + if (dest == NULL) { + return 0; + } + if (EC_POINT_is_infinity(src)) { + dest->is_infinity = 1; + } + else{ + dest->is_infinity = 0; + if (!BN_copy(dest->X, src->X) || !BN_copy(dest->Y, src->Y)) return 0; + } + return 1; +} + +#define NORMAL 0x1 +#define COMPRESS 0x2 +#define MIX 0x4 + +int EC_POINT_ecp2bin(uint8 *out, const EC_POINT *point, int p_bytes, int flag) { + uint8 PC; + uint8 *p = out, *buf; + int i; + buf = malloc(p_bytes); + if (EC_POINT_is_infinity(point)) { + return 0; + } + if (flag == NORMAL) { + PC = 0x04; + *(p++) = PC; + if (!BN_bn2binpad(point->X, buf, p_bytes)) { + return 0; + } + for (i = 0; i < p_bytes; ++i) { + *(p++) = buf[i]; + } + if (!BN_bn2binpad(point->Y, buf, p_bytes)) { + return 0; + } + for (i = 0; i < p_bytes; ++i) { + *(p++) = buf[i]; + } + } + else { + goto err; /* not supported */ + } + return 1; + err: + return 0; +} + +int EC_POINT_bin2ecp(EC_POINT *point, const uint8 *src, int p_bytes) { + if (point == NULL) { + return 0; + } + uint8 PC = *src; + const uint8 *data = src + 1; + uint8 *buf; + int i; + if (PC == 0x04) { + if (!BN_bin2bn(data, p_bytes, point->X) + || !BN_bin2bn(data+p_bytes, p_bytes, point->Y)) { + return 0; + } + } + else { + goto err; /* not supported */ + } + return 1; + err: + return 0; +} + +int EC_POINT_ecp2bin_normal(uint8 *out, const EC_POINT *point, int p_bytes) { + return EC_POINT_ecp2bin(out, point, p_bytes, NORMAL); +} + +// int EC_POINT_ecp2bin_compressed(uint8 *out, const EC_POINT *point, int p_bytes) { +// return EC_POINT_ecp2bin(out, point, p_bytes, COMPRESS); +// } + +// int EC_POINT_ecp2bin_mix(uint8 *out, const EC_POINT *point, int p_bytes) { +// return EC_POINT_ecp2bin(out, point, p_bytes, MIX); +// } + +typedef struct ec_group_st { + EC_POINT *generator; /* point G */ + BIGNUM *order; /* n */ + BIGNUM *cofactor; /* h */ + BIGNUM *field; /* module p */ + BIGNUM *a, *b; /* ECC parameter */ +} EC_GROUP; + +int EC_GROUP_init(EC_GROUP *group) { + group->generator = EC_POINT_new(); + group->order = BN_new(); + group->cofactor = BN_new(); + group->field = BN_new(); + group->a = BN_new(); + group->b = BN_new(); + if (group->generator == NULL || group->b == NULL) { + free(group->generator); + BN_free(group->order); + BN_free(group->cofactor); + BN_free(group->field); + BN_free(group->a); + BN_free(group->b); + return 0; + } + return 1; +} + +EC_GROUP *EC_GROUP_new() { + EC_GROUP *ret = NULL; + ret = malloc(sizeof(*ret)); + if (ret == NULL) { + return NULL; + } + if (!EC_GROUP_init(ret)) { + free(ret); + return NULL; + } + return ret; +} + +int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src) { + if (dest == NULL) { + return 0; + } + if (dest == src) { + return 1; + } + if (!EC_POINT_copy(dest->generator, src->generator) + || !BN_copy(dest->order, src->order) + || !BN_copy(dest->cofactor, src->cofactor) + || !BN_copy(dest->field, src->field) + || !BN_copy(dest->a, src->a) + || !BN_copy(dest->b, src->b)) { + return 0; + } + return 1; +} + +int EC_GROUP_set_param(EC_GROUP *group, const BIGNUM *field, const BIGNUM *a, const BIGNUM *b, const BIGNUM *order, const BIGNUM *cofactor, const EC_POINT *point) { + if (group == NULL) { + return 0; + } + group->field = BN_new(); + group->a = BN_new(); + group->b = BN_new(); + group->order = BN_new(); + group->cofactor = BN_new(); + group->generator = EC_POINT_new(); + if (group->cofactor == NULL + || group->generator == NULL) { + return 0; + } + if (!BN_copy(group->field, field) + || !BN_copy(group->a, a) + || !BN_copy(group->b, b) + || !BN_copy(group->order, order) + || !BN_copy(group->cofactor, cofactor) + || !EC_POINT_copy(group->generator, point)) { + return 0; + } + return 1; +} + +int EC_POINT_double(EC_POINT *r, const EC_POINT *a, const EC_GROUP *group) { + const BIGNUM *p; + BN_CTX *ctx = NULL; + int ret = 0; + BIGNUM *ax, *ay, *lnum, *ldeno; + BIGNUM *tmp, *bn_value_3; + if (r == NULL) { + return 0; + } + if (EC_POINT_is_infinity(a)) { + return EC_POINT_copy(r, EC_point_infinity()); + } + p = group->field; + ctx = BN_CTX_new(); + if (ctx == NULL) { + return 0; + } + BN_CTX_start(ctx); + ax = BN_CTX_get(ctx); + ay = BN_CTX_get(ctx); + lnum = BN_CTX_get(ctx); + ldeno = BN_CTX_get(ctx); + tmp = BN_CTX_get(ctx); + bn_value_3 = BN_CTX_get(ctx); + if (bn_value_3 == NULL) { + goto done; + } + if (!BN_copy(ax, a->X) + || !BN_copy(ay, a->Y)) { + goto done; + } + if (!BN_set_word(bn_value_3, 3) + || !BN_mod_sqr(lnum, ax, p, ctx) + || !BN_mod_mul(lnum, lnum, bn_value_3, p, ctx) + || !BN_mod_add_quick(lnum, lnum, group->a, p) + || !BN_mod_lshift1_quick(ldeno, ay, p) + || !BN_mod_inverse(ldeno, ldeno, p, ctx) + || !BN_mod_mul(lnum, lnum, ldeno, p, ctx)) { + goto done; + } + if (!BN_mod_sqr(r->X, lnum, p, ctx)) { + goto done; + } + if (!BN_mod_lshift1_quick(tmp, ax, p)) { + goto done; + } + if (!BN_mod_sub_quick(r->X, r->X, tmp, p)) { + goto done; + } + if (!BN_mod_sub_quick(r->Y, ax, r->X, p)) { + goto done; + } + if (!BN_mod_mul(r->Y, lnum, r->Y, p, ctx)) { + goto done; + } + if (!BN_mod_sub_quick(r->Y, r->Y, ay, p)) { + goto done; + } + if (r == NULL) { + r = EC_POINT_new(); + if (r == NULL) { + goto done; + } + } + ret = 1; + done: + BN_CTX_end(ctx); + BN_CTX_free(ctx); + return ret; +} + +int EC_POINT_add(EC_POINT *r, const EC_POINT *a, const EC_POINT *b, const EC_GROUP *group) { + const BIGNUM *p; + BN_CTX *ctx = NULL; + int ret = 0; + BIGNUM *ax, *ay, *bx, *by, *lnum, *ldeno; + BIGNUM *tmp, *bn_value_3; + if (r == NULL) { + return 0; + } + if (a == b) { + return EC_POINT_double(r, a, group); + } + if (EC_POINT_is_infinity(a)) { + return EC_POINT_copy(r, b); + } + if (EC_POINT_is_infinity(b)) { + return EC_POINT_copy(r, a); + } + p = group->field; + ctx = BN_CTX_new(); + if (ctx == NULL) { + return 0; + } + BN_CTX_start(ctx); + ax = BN_CTX_get(ctx); + ay = BN_CTX_get(ctx); + bx = BN_CTX_get(ctx); + by = BN_CTX_get(ctx); + lnum = BN_CTX_get(ctx); + ldeno = BN_CTX_get(ctx); + tmp = BN_CTX_get(ctx); + bn_value_3 = BN_CTX_get(ctx); + if (bn_value_3 == NULL) { + goto done; + } + if (!BN_copy(ax, a->X) + || !BN_copy(ay, a->Y) + || !BN_copy(bx, b->X) + || !BN_copy(by, b->Y)) { + goto done; + } + if (BN_cmp(ax, bx) == 0) { + if (!BN_mod_add_quick(tmp, ay, by, p)) { + goto done; + } + if (BN_is_zero(tmp) + || !EC_POINT_copy(r, EC_point_infinity())) { + goto done; + } + if (!BN_set_word(bn_value_3, 3)) { + goto done; + } + if (!BN_mod_sqr(lnum, ax, p, ctx) + || !BN_mod_mul(lnum, lnum, bn_value_3, p, ctx) + || !BN_mod_add_quick(lnum, lnum, group->a, p) + || !BN_mod_lshift1_quick(ldeno, ay, p) + || !BN_mod_inverse(ldeno, ldeno, p, ctx) + || !BN_mod_mul(lnum, lnum, ldeno, p, ctx)) { + goto done; + } + } + else { + if (!BN_mod_sub_quick(lnum, by, ay, p)) { + goto done; + } + if (!BN_mod_sub_quick(ldeno, bx, ax, p)) { + goto done; + } + if (!BN_mod_inverse(ldeno, ldeno, p, ctx)) { + goto done; + } + if (!BN_mod_mul(lnum, lnum, ldeno, p, ctx)) { + goto done; + } + } + if (!BN_mod_sqr(tmp, lnum, p, ctx)) { + goto done; + } + if (!BN_mod_sub_quick(tmp, tmp, ax, p)) { + goto done; + } + if (!BN_mod_sub_quick(r->X, tmp, bx, p)) { + goto done; + } + if (!BN_mod_sub_quick(r->Y, ax, r->X, p)) { + goto done; + } + if (!BN_mod_mul(r->Y, lnum, r->Y, p, ctx)) { + goto done; + } + if (!BN_mod_sub_quick(r->Y, r->Y, ay, p)) { + goto done; + } + if (r == NULL) { + r = EC_POINT_new(); + if (r == NULL) { + goto done; + } + } + ret = 1; + done: + BN_CTX_end(ctx); + BN_CTX_free(ctx); + return ret; +} + +int EC_POINT_mul(EC_POINT *r, const EC_POINT *a, const BIGNUM *b, const EC_GROUP *group) { + if (r == NULL) { + return 0; + } + if (BN_is_negative(b)) { + EC_POINT *_a = EC_POINT_new(); + BIGNUM *_b = BN_new(); + if (!EC_POINT_copy(_a, a) + || !BN_copy(_b, b) + || !BN_mod_sub_quick(_a->Y, group->field, _a->Y, group->field)) { + return 0; + } + BN_set_negative(_b, 0); + return EC_POINT_mul(r, _a, _b, group); + } + if (!EC_POINT_copy(r, EC_point_infinity())){ + return 0; + } + int bits = BN_num_bits(b); + for (int i = bits-1; i >= 0; --i) { + if (!EC_POINT_double(r, r, group)) { + return 0; + } + if (BN_is_bit_set(b, i)) { + if (!EC_POINT_add(r, r, a, group)) { + return 0; + } + } + } + return 1; +} + +/* + * Only for curve y^2 = x^3 + ax + b. + * Please check infinity before calling. + */ +int EC_POINT_on_curve_simple(const EC_POINT *point, const EC_GROUP *group) { + if (point == NULL || group == NULL) { + return 0; + } + if (EC_POINT_is_infinity(point)) { + return 1; + } + int ret = 0; + BN_CTX *ctx; + BIGNUM *p, *a, *b, *x, *y, *lhs, *rhs; + EC_POINT *Nq; + ctx = BN_CTX_new(); + if (ctx == NULL) { + return ret; + } + BN_CTX_start(ctx); + p = group->field; + a = group->a; + b = group->b; + x = point->X; + y = point->Y; + lhs = BN_CTX_get(ctx); + rhs = BN_CTX_get(ctx); + Nq = EC_POINT_new(); + if (p == NULL + || a == NULL + || b == NULL + || x == NULL + || y == NULL + || rhs == NULL + || Nq == NULL) { + return ret; + } + if (!BN_mod_mul(lhs, y, y, p, ctx) + || !BN_mod_mul(rhs, x, x, p, ctx) + || !BN_mod_add_quick(rhs, rhs, a, p) + || !BN_mod_mul(rhs, rhs, x, p, ctx) + || !BN_mod_add_quick(rhs, rhs, b, p) + || !EC_POINT_mul(Nq, point, group->order, group)) { + return ret; + } + if (BN_cmp(lhs, rhs) != 0 + || !EC_POINT_is_infinity(Nq)) { + return ret; + } + ret = 1; + BN_CTX_end(ctx); + BN_CTX_free(ctx); + return ret; +} + +typedef struct ec_key_st { + EC_GROUP *group; + EC_POINT *pub_key; + BIGNUM *priv_key; +} EC_KEY; + +int EC_KEY_init(EC_KEY *key) { + key->group = EC_GROUP_new(); + key->pub_key = EC_POINT_new(); + key->priv_key = BN_new(); + if (key->group == NULL + || key->pub_key == NULL + || key->priv_key == NULL) { + free(key->group); + free(key->pub_key); + BN_free(key->priv_key); + return 0; + } + return 1; +} + +EC_KEY *EC_KEY_new() { + EC_KEY *ret = NULL; + ret = malloc(sizeof(*ret)); + if (ret == NULL) { + return NULL; + } + if (!EC_KEY_init(ret)) { + free(ret); + return NULL; + } + return ret; +} + +int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group) { + if (!EC_GROUP_copy(key->group, group)) { + return 0; + } + return 1; +} + +int EC_KEY_set_priv_key(EC_KEY *key, const BIGNUM *priv_key) { + BIGNUM *tmp; + tmp = BN_dup(priv_key); + if (tmp == NULL) { + return 0; + } + BN_clear_free(key->priv_key); + key->priv_key = tmp; + return 1; +} + +int EC_KEY_generate_pub_key(EC_KEY *key) { + return EC_POINT_mul(key->pub_key, key->group->generator, key->priv_key, key->group); +} + +const BIGNUM *EC_KEY_get_priv_key(const EC_KEY *key) { + return key->priv_key; +} + +int EC_KEY_generate_key() {} diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/sm3/sm3.h" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/sm3/sm3.h" new file mode 100644 index 0000000000000000000000000000000000000000..195d6790d2f9b57a6a27ab84b5cbf88d4aad37a3 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM2/sm3/sm3.h" @@ -0,0 +1,183 @@ +#ifndef SECGEAR_SM3_H +#define SECGEAR_SM3_H + +/* + * Ref: + * GB/T 32905-2016 + */ + +#include +#include "../include/common_utils.h" + +#define SM3_DIGEST_LENGTH 32 +#define SM3_WORD uint32 + +#define SM3_CBLOCK 64 +#define SM3_LBLOCK (SM3_CBLOCK/4) + +typedef struct SM3state_st { + SM3_WORD A, B, C, D, E, F, G, H; + SM3_WORD Nl, Nh; + SM3_WORD data[SM3_LBLOCK]; + unsigned int num; +} SM3_CTX; + +#define P0(X) (X ^ ROTATE(X, 9) ^ ROTATE(X, 17)) +#define P1(X) (X ^ ROTATE(X, 15) ^ ROTATE(X, 23)) + +#define FF(J,X,Y,Z) (J<16 ? (X ^ Y ^ Z) : ((X & Y) | (X & Z) | (Y & Z))) +#define GG(J,X,Y,Z) (J<16 ? (X ^ Y ^ Z) : ((X & Y) | ((~X) & Z))) + +#define T(J) (J<16 ? 0x79cc4519 : 0x7a879d8a) + +#define EXPAND(W0,W7,W13,W3,W10) \ + (P1(W0 ^ W7 ^ ROTATE(W13, 15)) ^ ROTATE(W3, 7) ^ W10) + +#define SM3_A 0x7380166fUL +#define SM3_B 0x4914b2b9UL +#define SM3_C 0x172442d7UL +#define SM3_D 0xda8a0600UL +#define SM3_E 0xa96f30bcUL +#define SM3_F 0x163138aaUL +#define SM3_G 0xe38dee4dUL +#define SM3_H 0xb0fb0e4eUL + +#define SM3_MAKE_STRING(c, s) \ + do { \ + unsigned long ll; \ + ll=(c)->A; (void)HOST_l2c(ll, (s)); \ + ll=(c)->B; (void)HOST_l2c(ll, (s)); \ + ll=(c)->C; (void)HOST_l2c(ll, (s)); \ + ll=(c)->D; (void)HOST_l2c(ll, (s)); \ + ll=(c)->E; (void)HOST_l2c(ll, (s)); \ + ll=(c)->F; (void)HOST_l2c(ll, (s)); \ + ll=(c)->G; (void)HOST_l2c(ll, (s)); \ + ll=(c)->H; (void)HOST_l2c(ll, (s)); \ + } while (0) + +void sm3_block_data_order(SM3_CTX *ctx, const void *p, size_t num) { + const unsigned char *data = p; + SM3_WORD A, B, C, D, E, F, G, H; + SM3_WORD w[68]={}, ww[64]={}; + while (num--) { + A = ctx->A; + B = ctx->B; + C = ctx->C; + D = ctx->D; + E = ctx->E; + F = ctx->F; + G = ctx->G; + H = ctx->H; + for (int j = 0; j < 16; ++j) { + HOST_c2l(data, w[j]); + } + for (int j = 16; j < 68; ++j) { + w[j] = EXPAND(w[j-16], w[j-9], w[j-3], w[j-13], w[j-6]); + } + for (int j = 0; j < 64; ++j) { + ww[j] = w[j] ^ w[j+4]; + } + for (int j = 0; j < 64; j++) { + SM3_WORD SS1, SS2, TT1, TT2; + SS1 = ROTATE(ROTATE(A, 12) + E + ROTATE(T(j), j&31), 7); + SS2 = SS1 ^ ROTATE(A, 12); + TT1 = FF(j, A, B, C) + D + SS2 + ww[j]; + TT2 = GG(j, E, F, G) + H + SS1 + w[j]; + D = C; + C = ROTATE(B,9); + B = A; + A = TT1; + H = G; + G = ROTATE(F, 19); + F = E; + E = P0(TT2); + } + ctx->A ^= A; + ctx->B ^= B; + ctx->C ^= C; + ctx->D ^= D; + ctx->E ^= E; + ctx->F ^= F; + ctx->G ^= G; + ctx->H ^= H; + } +} + +int sm3_init(SM3_CTX *c) { + memset(c, 0, sizeof(*c)); + c->A = SM3_A; + c->B = SM3_B; + c->C = SM3_C; + c->D = SM3_D; + c->E = SM3_E; + c->F = SM3_F; + c->G = SM3_G; + c->H = SM3_H; + return 1; +} + +int sm3_update(SM3_CTX *c, const void *data_, size_t len) { + const unsigned char *data = data_; + unsigned char *p; + SM3_WORD l; + size_t n; + if (len == 0) return 1; + l = (c->Nl + (((SM3_WORD) len) << 3)) & 0xffffffffUL; + if (l < c->Nl) c->Nh++; + c->Nh += (SM3_WORD) (len >> 29); + c->Nl = l; + n = c->num; + if (n != 0) { + p = (unsigned char *)c->data; + if (len >= SM3_CBLOCK || len + n >= SM3_CBLOCK) { + memcpy(p + n, data, SM3_CBLOCK - n); + sm3_block_data_order(c, p, 1); + n = SM3_CBLOCK - n; + data += n; + len -= n; + c->num = 0; + memset(p, 0, SM3_CBLOCK); + } + else { + memcpy(p + n, data, len); + c->num += (unsigned int)len; + return 1; + } + } + n = len / SM3_CBLOCK; + if (n > 0) { + sm3_block_data_order(c, data, n); + n *= SM3_CBLOCK; + data += n; + len -= n; + } + if (len != 0) { + p = (unsigned char *)c->data; + c->num = (unsigned int)len; + memcpy(p, data, len); + } + return 1; +} + +int sm3_final(unsigned char *md, SM3_CTX *c) { + unsigned char *p = (unsigned char *)c->data; + size_t n = c->num; + p[n] = 0x80; + n++; + if (n > (SM3_CBLOCK - 8)) { + memset(p + n, 0, SM3_CBLOCK - n); + n = 0; + sm3_block_data_order(c, p, 1); + } + memset(p + n, 0, SM3_CBLOCK - 8 - n); + p += SM3_CBLOCK - 8; + (void)HOST_l2c(c->Nh, p); + (void)HOST_l2c(c->Nl, p); + p -= SM3_CBLOCK; + sm3_block_data_order(c, p, 1); + c->num = 0; + SM3_MAKE_STRING(c, md); + return 1; +} + +#endif \ No newline at end of file diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM4/CMakeLists.txt" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM4/CMakeLists.txt" new file mode 100644 index 0000000000000000000000000000000000000000..acd2ef7ffa085644d041a298b6c455d91c8369d0 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM4/CMakeLists.txt" @@ -0,0 +1,38 @@ +# Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved. +# secGear is licensed under the Mulan PSL v2. +# You can use this software according to the terms and conditions of the Mulan PSL v2. +# You may obtain a copy of Mulan PSL v2 at: +# http://license.coscl.org.cn/MulanPSL2 +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR +# PURPOSE. +# See the Mulan PSL v2 for more details. + +cmake_minimum_required(VERSION 3.10 FATAL_ERROR) +project(enclave_SM4) + +set(CMAKE_C_STANDARD 99) + +set(CURRENT_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) + +#set edl name +set(EDL_FILE enclave_SM4.edl) +#set(LOCAL_ROOT_PATH "$ENV{CC_SDK}") +#set(SECGEAR_INSTALL_PATH /usr/lib64/) + +if(CC_GP) + set(CODETYPE trustzone) + set(CODEGEN codegen_arm64) + execute_process(COMMAND uuidgen -r OUTPUT_VARIABLE UUID) + string(REPLACE "\n" "" UUID ${UUID}) + add_definitions(-DPATH="/data/${UUID}.sec") +endif() + +if(CC_SGX) + set(CODETYPE sgx) + set(CODEGEN codegen_x86_64) + add_definitions(-DPATH="${CMAKE_CURRENT_BINARY_DIR}/enclave/enclave.signed.so") +endif() + +add_subdirectory(${CURRENT_ROOT_PATH}/enclave) +add_subdirectory(${CURRENT_ROOT_PATH}/host) diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM4/enclave/CMakeLists.txt" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM4/enclave/CMakeLists.txt" new file mode 100644 index 0000000000000000000000000000000000000000..7c4b75dead54c81772c8d26c7484a442eea1315d --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM4/enclave/CMakeLists.txt" @@ -0,0 +1,162 @@ +# Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved. +# secGear is licensed under the Mulan PSL v2. +# You can use this software according to the terms and conditions of the Mulan PSL v2. +# You may obtain a copy of Mulan PSL v2 at: +# http://license.coscl.org.cn/MulanPSL2 +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR +# PURPOSE. +# See the Mulan PSL v2 for more details. + +#set auto code prefix +set(PREFIX enclave_SM4) + +set(CMAKE_C_STANDARD 99) +#set sign key +set(PEM Enclave_private.pem) + +#set sign tool +set(SIGN_TOOL ${LOCAL_ROOT_PATH}/tools/sign_tool/sign_tool.sh) + +#set enclave src code +set(SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/enclave_SM4.c) + +#set log level +set(PRINT_LEVEL 3) +add_definitions(-DPRINT_LEVEL=${PRINT_LEVEL}) + +if(CC_GP) + #set signed output + set(OUTPUT ${UUID}.sec) + #set itrustee device key + set(DEVICEPEM ${CMAKE_CURRENT_SOURCE_DIR}/rsa_public_key_cloud.pem) + #set whilelist. default: /vendor/bin/teec_hello + set(WHITE_LIST_0 /vendor/bin/enclave_SM4) + set(WHITE_LIST_OWNER root) + set(WHITE_LIST_1 /vendor/bin/secgear_enclave_SM4) + set(WHITELIST WHITE_LIST_0 WHITE_LIST_1) + + set(AUTO_FILES ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_t.h ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_t.c ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_args.h) + add_custom_command(OUTPUT ${AUTO_FILES} + DEPENDS ${CURRENT_ROOT_PATH}/${EDL_FILE} + COMMAND ${CODEGEN} --${CODETYPE} --trusted ${CURRENT_ROOT_PATH}/${EDL_FILE} --search-path ${LOCAL_ROOT_PATH}/inc/host_inc/gp) +endif() + +if(CC_SGX) + set(OUTPUT enclave.signed.so) + set(AUTO_FILES ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_t.h ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_t.c) + add_custom_command(OUTPUT ${AUTO_FILES} + DEPENDS ${CURRENT_ROOT_PATH}/${EDL_FILE} + COMMAND ${CODEGEN} --${CODETYPE} --trusted ${CURRENT_ROOT_PATH}/${EDL_FILE} --search-path ${LOCAL_ROOT_PATH}/inc/host_inc/sgx --search-path ${SGXSDK}/include) +endif() + +set(COMMON_C_FLAGS "-W -Wall -fno-short-enums -fno-omit-frame-pointer -fstack-protector \ + -Wstack-protector --param ssp-buffer-size=4 -frecord-gcc-switches -Wextra -nostdinc -nodefaultlibs \ + -fno-peephole -fno-peephole2 -Wno-main -Wno-error=unused-parameter \ + -Wno-error=unused-but-set-variable -Wno-error=format-truncation=") + +set(COMMON_C_LINK_FLAGS "-Wl,-z,now -Wl,-z,relro -Wl,-z,noexecstack -Wl,-nostdlib -nodefaultlibs -nostartfiles") +set(COMMON_CXX_FLAGS "${COMMON_C_FLAGS}") + +if(CC_GP) + configure_file("${CMAKE_CURRENT_SOURCE_DIR}/manifest.txt.in" "${CMAKE_CURRENT_SOURCE_DIR}/manifest.txt") + + set(CMAKE_C_FLAGS "${COMMON_C_FLAGS} -march=armv8-a ") + set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -s -fPIC") + set(CMAKE_SHARED_LINKER_FLAGS "${COMMON_C_LINK_FLAGS} -Wl,-s") + + set(ITRUSTEE_TEEDIR ${iTrusteeSDK}/) + set(ITRUSTEE_LIBC ${iTrusteeSDK}/thirdparty/open_source/musl/libc) + + if(${CMAKE_VERSION} VERSION_LESS "3.13.0") + link_directories(${SECGEAR_INSTALL_PATH}) + endif() + + add_library(${PREFIX} SHARED ${SOURCE_FILES} ${AUTO_FILES}) + + target_include_directories( ${PREFIX} PRIVATE + ${CMAKE_CURRENT_BINARY_DIR} + ${LOCAL_ROOT_PATH}/inc/host_inc + ${LOCAL_ROOT_PATH}/inc/host_inc/gp + ${LOCAL_ROOT_PATH}/inc/enclave_inc + ${LOCAL_ROOT_PATH}/inc/enclave_inc/gp + ${ITRUSTEE_TEEDIR}/include/TA + ${ITRUSTEE_TEEDIR}/include/TA/huawei_ext + ${ITRUSTEE_LIBC}/arch/aarch64 + ${ITRUSTEE_LIBC}/ + ${ITRUSTEE_LIBC}/arch/arm/bits + ${ITRUSTEE_LIBC}/arch/generic + ${ITRUSTEE_LIBC}/arch/arm + ${LOCAL_ROOT_PATH}/inc/enclave_inc/gp/itrustee) + + if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.13.0") + target_link_directories(${PREFIX} PRIVATE ${SECGEAR_INSTALL_PATH}) + endif() + + foreach(WHITE_LIST ${WHITELIST}) + add_definitions(-D${WHITE_LIST}="${${WHITE_LIST}}") + endforeach(WHITE_LIST) + add_definitions(-DWHITE_LIST_OWNER="${WHITE_LIST_OWNER}") + + target_link_libraries(${PREFIX} -lsecgear_tee) + + add_custom_command(TARGET ${PREFIX} + POST_BUILD + COMMAND bash ${SIGN_TOOL} -d sign -x trustzone -i /home/sunny/secGear/debug/lib/lib${PREFIX}.so -c ${CMAKE_CURRENT_SOURCE_DIR}/manifest.txt -m ${CMAKE_CURRENT_SOURCE_DIR}/config_cloud.ini + -o ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT}) + + install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT} + DESTINATION /data + PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE) + +endif() + +if(CC_SGX) + set(SGX_DIR ${SGXSDK}) + set(CMAKE_C_FLAGS "${COMMON_C_FLAGS} -m64 -fvisibility=hidden") + set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -std=c++11 -O2 -nostdinc++") + set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -s") + set(LINK_LIBRARY_PATH ${SGX_DIR}/lib64) + + if(CC_SIM) + set(Trts_Library_Name sgx_trts_sim) + set(Service_Library_Name sgx_tservice_sim) + else() + set(Trts_Library_Name sgx_trts) + set(Service_Library_Name sgx_tservice) + endif() + + set(Crypto_Library_Name sgx_tcrypto) + + set(CMAKE_SHARED_LINKER_FLAGS "${COMMON_C_LINK_FLAGS} -Wl,-z,defs -Wl,-pie -Bstatic -Bsymbolic -eenclave_entry \ + -Wl,--export-dynamic -Wl,--defsym,__ImageBase=0 -Wl,--gc-sections -Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/Enclave.lds") + + if(${CMAKE_VERSION} VERSION_LESS "3.13.0") + link_directories(${LINK_LIBRARY_PATH}) + endif() + + add_library(${PREFIX} SHARED ${SOURCE_FILES} ${AUTO_FILES}) + + target_include_directories(${PREFIX} PRIVATE + ${CMAKE_CURRENT_BINARY_DIR} + ${SGX_DIR}/include/tlibc + ${SGX_DIR}/include/libcxx + ${SGX_DIR}/include + ${LOCAL_ROOT_PATH}/inc/host_inc + ${LOCAL_ROOT_PATH}/inc/host_inc/sgx) + + if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.13.0") + target_link_directories(${PREFIX} PRIVATE + ${LINK_LIBRARY_PATH}) + endif() + + target_link_libraries(${PREFIX} -Wl,--whole-archive ${Trts_Library_Name} -Wl,--no-whole-archive + -Wl,--start-group -lsgx_tstdc -lsgx_tcxx -l${Crypto_Library_Name} -l${Service_Library_Name} -Wl,--end-group) + add_custom_command(TARGET ${PREFIX} + POST_BUILD + COMMAND umask 0177 + COMMAND openssl genrsa -3 -out ${PEM} 3072 + COMMAND bash ${SIGN_TOOL} -d sign -x sgx -i ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/lib${PREFIX}.so -k ${PEM} -o ${OUTPUT} -c ${CMAKE_CURRENT_SOURCE_DIR}/Enclave.config.xml) +endif() + +set_target_properties(${PREFIX} PROPERTIES SKIP_BUILD_RPATH TRUE) diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM4/enclave/Enclave.config.xml" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM4/enclave/Enclave.config.xml" new file mode 100644 index 0000000000000000000000000000000000000000..a5e05e0175b43f97645fde0ea33a1fe0562398cf --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM4/enclave/Enclave.config.xml" @@ -0,0 +1,12 @@ + + 0 + 0 + 0x100000 + 0x80000 + 10 + 1 + + 1 + 0 + 0xFFFFFFFF + diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM4/enclave/Enclave.lds" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM4/enclave/Enclave.lds" new file mode 100644 index 0000000000000000000000000000000000000000..ab77e6478ef3c3448356e80b9884a8a533dd32c6 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM4/enclave/Enclave.lds" @@ -0,0 +1,11 @@ +enclave.so +{ + global: + g_global_data_sim; + g_global_data; + enclave_entry; + g_peak_heap_used; + local: + *; +}; + diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM4/enclave/enclave_SM4.c" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM4/enclave/enclave_SM4.c" new file mode 100644 index 0000000000000000000000000000000000000000..a86647497e7f1117748e7d9fd1a700ea35b0e5a5 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM4/enclave/enclave_SM4.c" @@ -0,0 +1,42 @@ +#include "Enclave1_t.h" +#include +#include "sm4.h" + +uint32 message[4]; +uint32 key[4]; +uint32 res_enc[4], res_dec[4]; + +void sm4test(const uint32 *M, const uint32 *MK, const int rounds) { + SM4_CTX c; + sm4_init(&c, M, MK); + for (int i=0; i +#include "../include/common_utils.h" + +#define ENCRYPT 0 +#define DECRYPT 1 +const uint8 Sbox[256] = { + 0xD6, 0x90, 0xE9, 0xFE, 0xCC, 0xE1, 0x3D, 0xB7, 0x16, 0xB6, 0x14, 0xC2, 0x28, 0xFB, 0x2C, 0x05, + 0x2B, 0x67, 0x9A, 0x76, 0x2A, 0xBE, 0x04, 0xC3, 0xAA, 0x44, 0x13, 0x26, 0x49, 0x86, 0x06, 0x99, + 0x9C, 0x42, 0x50, 0xF4, 0x91, 0xEF, 0x98, 0x7A, 0x33, 0x54, 0x0B, 0x43, 0xED, 0xCF, 0xAC, 0x62, + 0xE4, 0xB3, 0x1C, 0xA9, 0xC9, 0x08, 0xE8, 0x95, 0x80, 0xDF, 0x94, 0xFA, 0x75, 0x8F, 0x3F, 0xA6, + 0x47, 0x07, 0xA7, 0xFC, 0xF3, 0x73, 0x17, 0xBA, 0x83, 0x59, 0x3C, 0x19, 0xE6, 0x85, 0x4F, 0xA8, + 0x68, 0x6B, 0x81, 0xB2, 0x71, 0x64, 0xDA, 0x8B, 0xF8, 0xEB, 0x0F, 0x4B, 0x70, 0x56, 0x9D, 0x35, + 0x1E, 0x24, 0x0E, 0x5E, 0x63, 0x58, 0xD1, 0xA2, 0x25, 0x22, 0x7C, 0x3B, 0x01, 0x21, 0x78, 0x87, + 0xD4, 0x00, 0x46, 0x57, 0x9F, 0xD3, 0x27, 0x52, 0x4C, 0x36, 0x02, 0xE7, 0xA0, 0xC4, 0xC8, 0x9E, + 0xEA, 0xBF, 0x8A, 0xD2, 0x40, 0xC7, 0x38, 0xB5, 0xA3, 0xF7, 0xF2, 0xCE, 0xF9, 0x61, 0x15, 0xA1, + 0xE0, 0xAE, 0x5D, 0xA4, 0x9B, 0x34, 0x1A, 0x55, 0xAD, 0x93, 0x32, 0x30, 0xF5, 0x8C, 0xB1, 0xE3, + 0x1D, 0xF6, 0xE2, 0x2E, 0x82, 0x66, 0xCA, 0x60, 0xC0, 0x29, 0x23, 0xAB, 0x0D, 0x53, 0x4E, 0x6F, + 0xD5, 0xDB, 0x37, 0x45, 0xDE, 0xFD, 0x8E, 0x2F, 0x03, 0xFF, 0x6A, 0x72, 0x6D, 0x6C, 0x5B, 0x51, + 0x8D, 0x1B, 0xAF, 0x92, 0xBB, 0xDD, 0xBC, 0x7F, 0x11, 0xD9, 0x5C, 0x41, 0x1F, 0x10, 0x5A, 0xD8, + 0x0A, 0xC1, 0x31, 0x88, 0xA5, 0xCD, 0x7B, 0xBD, 0x2D, 0x74, 0xD0, 0x12, 0xB8, 0xE5, 0xB4, 0xB0, + 0x89, 0x69, 0x97, 0x4A, 0x0C, 0x96, 0x77, 0x7E, 0x65, 0xB9, 0xF1, 0x09, 0xC5, 0x6E, 0xC6, 0x84, + 0x18, 0xF0, 0x7D, 0xEC, 0x3A, 0xDC, 0x4D, 0x20, 0x79, 0xEE, 0x5F, 0x3E, 0xD7, 0xCB, 0x39, 0x48, +}; +const uint32 FK[4] = { + 0xA3B1BAC6, 0x56AA3350, 0x677D9197, 0xB27022DC, +}; +const uint32 CK[32] = { + 0x00070E15, 0x1C232A31, 0x383F464D, 0x545B6269, + 0x70777E85, 0x8C939AA1, 0xA8AFB6BD, 0xC4CBD2D9, + 0xE0E7EEF5, 0xFC030A11, 0x181F262D, 0x343B4249, + 0x50575E65, 0x6C737A81, 0x888F969D, 0xA4ABB2B9, + 0xC0C7CED5, 0xDCE3EAF1, 0xF8FF060D, 0x141B2229, + 0x30373E45, 0x4C535A61, 0x686F767D, 0x848B9299, + 0xA0A7AEB5, 0xBCC3CAD1, 0xD8DFE6ED, 0xF4FB0209, + 0x10171E25, 0x2C333A41, 0x484F565D, 0x646B7279, +}; +#define L(B) ((B) ^ (ROTATE((B), 2)) ^ (ROTATE((B), 10)) ^ (ROTATE((B), 18)) ^ (ROTATE((B), 24))) +#define LP(B) ((B) ^ (ROTATE((B), 13)) ^ (ROTATE((B), 23))) +#define S(X) (((uint32)Sbox[((X) >> 24) & 0xFF] << 24) | \ + ((uint32)Sbox[((X) >> 16) & 0xFF] << 16) | \ + ((uint32)Sbox[((X) >> 8) & 0xFF] << 8) | \ + ((uint32)Sbox[(X) & 0xFF])) +#define T(X) L(S(X)) +#define TP(X) LP(S(X)) +#define F(X0, X1, X2, X3, rk) ((X0) ^ T((X1) ^ (X2) ^ (X3) ^ (rk))) + +typedef struct sm4_ctx_st { + uint32 M[4]; + uint32 rk[32]; +} SM4_CTX; + +int sm4_init(SM4_CTX *ctx, const uint32 *M, const uint32 *MK) { + memset(ctx, 0, sizeof(*ctx)); + uint32 K[36]; + memset(K, 0, sizeof(K)); + int i; + for (i = 0; i < 4; ++i) { + K[i] = MK[i] ^ FK[i]; + ctx->M[i] = M[i]; + } + for (i = 0; i < 32; ++i) { + ctx->rk[i] = K[i+4] = K[i] ^ TP(K[i+1] ^ K[i+2] ^ K[i+3] ^ CK[i]); + } + return 1; +} + +int sm4_round(SM4_CTX *ctx, int crypt_flag) { + int offset = (crypt_flag ? 31 : 0), i; + uint32 X[36]; + memset(X, 0, sizeof(X)); + for (i = 0; i < 4; ++i) { + X[i] = ctx->M[i]; + } + for (i = 0; i < 32; ++i) { + X[i+4] = F(X[i], X[i+1], X[i+2], X[i+3], ctx->rk[i^offset]); + } + ctx->M[0] = X[35]; + ctx->M[1] = X[34]; + ctx->M[2] = X[33]; + ctx->M[3] = X[32]; + return 1; +} + +int sm4_encrypt(SM4_CTX *ctx) { + return sm4_round(ctx, ENCRYPT); +} + +int sm4_decrypt(SM4_CTX *ctx) { + return sm4_round(ctx, DECRYPT); +} + +int sm4_final(uint32 *out, SM4_CTX *ctx) { + if (!memcpy(out, ctx->M, sizeof(ctx->M))) { + return 0; + } + return 1; +} + +#endif \ No newline at end of file diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM4/enclave_SM4.edl" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM4/enclave_SM4.edl" new file mode 100644 index 0000000000000000000000000000000000000000..3de3690306660b78462d1431c50a0442ba712d55 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM4/enclave_SM4.edl" @@ -0,0 +1,16 @@ +enclave { + include "secgear_urts.h" + from "secgear_tstdc.edl" import *; + + trusted { + public void setKeys([in, count=4] uint32_t *src); + public void setKeys([in, count=4] uint32_t *src); + public void callsm4test1(); + public void callsm4test2(); + public void getKeys([out, count=4] uint32_t *buf); + }; + + untrusted { + + }; +}; diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM4/host/CMakeLists.txt" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM4/host/CMakeLists.txt" new file mode 100644 index 0000000000000000000000000000000000000000..2dae205cce16639bb89887ff41bc436d33ff94b3 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM4/host/CMakeLists.txt" @@ -0,0 +1,88 @@ +# Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved. +# secGear is licensed under the Mulan PSL v2. +# You can use this software according to the terms and conditions of the Mulan PSL v2. +# You may obtain a copy of Mulan PSL v2 at: +# http://license.coscl.org.cn/MulanPSL2 +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR +# PURPOSE. +# See the Mulan PSL v2 for more details. + +#set auto code prefix +set(PREFIX enclave_SM4) +#set host exec name +set(OUTPUT secgear_enclave_SM4) +#set host src code +set(SOURCE_FILE ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp) + +set(CMAKE_CXX_STANDARD 11) + +#set auto code +if(CC_GP) + set(AUTO_FILES ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_u.h ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_u.c ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_args.h) + add_custom_command(OUTPUT ${AUTO_FILES} + DEPENDS ${CURRENT_ROOT_PATH}/${EDL_FILE} + COMMAND ${CODEGEN} --${CODETYPE} --untrusted ${CURRENT_ROOT_PATH}/${EDL_FILE} --search-path ${LOCAL_ROOT_PATH}/inc/host_inc/gp) +endif() + +if(CC_SGX) + set(AUTO_FILES ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_u.h ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_u.c) + add_custom_command(OUTPUT ${AUTO_FILES} + DEPENDS ${CURRENT_ROOT_PATH}/${EDL_FILE} + COMMAND ${CODEGEN} --${CODETYPE} --untrusted ${CURRENT_ROOT_PATH}/${EDL_FILE} --search-path ${LOCAL_ROOT_PATH}/inc/host_inc/sgx --search-path ${SGXSDK}/include) +endif() + +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIE") +set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -O2 -fPIE") +set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -s") + +if(CC_GP) + if(${CMAKE_VERSION} VERSION_LESS "3.13.0") + link_directories(${SECGEAR_INSTALL_PATH}) + endif() + add_executable(${OUTPUT} ${SOURCE_FILE} ${AUTO_FILES}) + target_include_directories(${OUTPUT} PRIVATE + ${LOCAL_ROOT_PATH}/inc/host_inc + ${LOCAL_ROOT_PATH}/inc/host_inc/gp + ${CMAKE_CURRENT_BINARY_DIR}) + if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.13.0") + target_link_directories(${OUTPUT} PRIVATE ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}) + endif() +endif() + +if(CC_SGX) + if(${CMAKE_VERSION} VERSION_LESS "3.13.0") + link_directories(${CMAKE_LIBRARY_OUTPUT_DIRECTORY}) + endif() + add_executable(${OUTPUT} ${SOURCE_FILE} ${AUTO_FILES}) + target_include_directories(${OUTPUT} PRIVATE + ${LOCAL_ROOT_PATH}/inc/host_inc + ${LOCAL_ROOT_PATH}/inc/host_inc/sgx + ${CMAKE_CURRENT_BINARY_DIR}) + if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.13.0") + target_link_directories(${OUTPUT} PRIVATE ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}) + endif() +endif() + +if(CC_SIM) + target_link_libraries(${OUTPUT} secgearsim) +else() + target_link_libraries(${OUTPUT} secgear) +endif() +set_target_properties(${OUTPUT} PROPERTIES SKIP_BUILD_RPATH TRUE) + +if(CC_GP) + #itrustee install whitelist /vender/bin/teec_hello + install(TARGETS ${OUTPUT} + RUNTIME + DESTINATION /vendor/bin/ + PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ) +endif() + +if(CC_SGX) + install(TARGETS ${OUTPUT} + RUNTIME + DESTINATION ${CMAKE_BINARY_DIR}/bin/ + PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ) +endif() + diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM4/host/main.cpp" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM4/host/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..960a50d416c668aa5a09cc1451aa56923e539fbc --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM4/host/main.cpp" @@ -0,0 +1,51 @@ +#include"enclave.h" +#include"Enclave1_u.h" +#include +#include + +const u_int32_t message[4] = { + 0x01234567, 0x89ABCDEF, + 0xFEDCBA98, 0x76543210, +}; +const u_int32_t key[4] = { + 0x01234567, 0x89ABCDEF, + 0xFEDCBA98, 0x76543210, +}; + +int main(int argc, char* argv[]) { + cc_enclave_t *eid; + cc_enclave_result_t ret; + char *path = transferFromString(PATH); + + ret = cc_enclave_create(path, AUTO_ENCLAVE_TYPE, 0, SECGEAR_DEBUG_FLAG, NULL, 0, &eid); + if (ret != CC_SUCCESS) { + printf("ERR: %#x ,failed to create enclave.\n", ret); + return -1; + } + + u_int32_t res[4]; + setSM4Message(eid, message); + setSM4Key(eid, key); + callsm4test1(eid); + getSM4Result(eid, res); + printf("encrypt 1: "); + for (int i=0; i<4; i++){ + printf("%08X", res[i]); + } + printf("\n"); + + setSM4Message(eid, message); + setSM4Key(eid, key); + callsm4test2(eid); + getSM4Result(eid, res); + printf("encrypt 100000: "); + for (int i=0; i<4; i++){ + printf("%08X", res[i]); + } + printf("\n"); + + if (CC_SUCCESS != cc_enclave_destroy(eid)) { + return -1; + } + return 0; +} diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM4/include/common_utils.h" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM4/include/common_utils.h" new file mode 100644 index 0000000000000000000000000000000000000000..9251edce48cf8aeb25404fcf5391335698a83a06 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM4/include/common_utils.h" @@ -0,0 +1,69 @@ +#ifndef SECGEAR_SM_COMMON_UTILS +#define SECGEAR_SM_COMMON_UTILS + +#include +#define debugPrint (printf("%s: %s: %d\n",__FILE__,__FUNCTION__,__LINE__),fflush(stdout),1) + +typedef __uint8_t uint8; +typedef __uint16_t uint16; +typedef __uint32_t uint32; +typedef __uint64_t uint64; + +#define CONCAT_8x4(x0, x1, x2, x3) ((((uint32)(x0) << 8 | (x1)) << 8 | (x2)) << 8 | (x3)) // X = (x0 || x1 || x2 || x3) +#define CONCAT_16x2(x0, x1) ((uint32)(x0) << 16 | (x1)) // X = (x0 || x1) +#define EXTRACT_8_32(X, i) (((X) >> (((i) ^ 3) << 3)) & 0xFF) // X = (x0 || x1 || x2 || x3) +#define EXTRACT_16_32(X, i) (((X) >> (((i) ^ 1) << 4)) & 0xFFFF) // X = (x0 || x1) +#define EXTRACT_16H_31(X) (((X) >> 15) & 0xFFFF) // Higher 16-bit from 31-bit +#define EXTRACT_16L_31(X) ((X) & 0xFFFF) // Lower 16-bit from 31-bit + +#define ROTATE(a, n) (((a) << (n)) | (((a) & 0xFFFFFFFF) >> ((32 - (n)) & 31))) +#define ROTATE_31(a, n) ((((a) << (n)) & 0x7FFFFFFF) | (((a) & 0x7FFFFFFF) >> (31 - (n)))) + +#define GET_BIT_VALUE_32(x, i) (((x) >> (31 - (i))) & 1) + +const uint32 PREFIX_MASK_32[32] = { + 0x00000000, + 0x80000000, + 0xC0000000, + 0xE0000000, + 0xF0000000, + 0xF8000000, + 0xFC000000, + 0xFE000000, + 0xFF000000, + 0xFF800000, + 0xFFC00000, + 0xFFE00000, + 0xFFF00000, + 0xFFF80000, + 0xFFFC0000, + 0xFFFE0000, + 0xFFFF0000, + 0xFFFF8000, + 0xFFFFC000, + 0xFFFFE000, + 0xFFFFF000, + 0xFFFFF800, + 0xFFFFFC00, + 0xFFFFFE00, + 0xFFFFFF00, + 0xFFFFFF80, + 0xFFFFFFC0, + 0xFFFFFFE0, + 0xFFFFFFF0, + 0xFFFFFFF8, + 0xFFFFFFFC, + 0xFFFFFFFE +}; + +# define HOST_c2l(c,l) (l =(((unsigned long)(*((c)++)))<<24), \ + l|=(((unsigned long)(*((c)++)))<<16), \ + l|=(((unsigned long)(*((c)++)))<< 8), \ + l|=(((unsigned long)(*((c)++))) ) ) +# define HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l)>>24)&0xff), \ + *((c)++)=(unsigned char)(((l)>>16)&0xff), \ + *((c)++)=(unsigned char)(((l)>> 8)&0xff), \ + *((c)++)=(unsigned char)(((l) )&0xff), \ + l) + +#endif \ No newline at end of file diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM4/include/ec.h" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM4/include/ec.h" new file mode 100644 index 0000000000000000000000000000000000000000..56adbe751f17342f84c17e3736f8806deb7f40b5 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_SM4/include/ec.h" @@ -0,0 +1,545 @@ +#include /* BigNum */ +#include "common_utils.h" + +typedef struct ec_point_st { + BIGNUM *X; + BIGNUM *Y; + int is_infinity; +} EC_POINT; + +int EC_POINT_init(EC_POINT *point) { + point->X = BN_new(); + point->Y = BN_new(); + point->is_infinity = 0; + if (point->X == NULL || point->Y == NULL) { + BN_free(point->X); + BN_free(point->Y); + return 0; + } + return 1; +} + +EC_POINT *EC_POINT_new() { + EC_POINT *ret = NULL; + ret = malloc(sizeof(*ret)); + if (ret == NULL) { + return NULL; + } + if (!EC_POINT_init(ret)) { + free(ret); + return NULL; + } + return ret; +} + +int EC_POINT_setXY(EC_POINT *r, const BIGNUM *x, const BIGNUM *y) { + if (r == NULL) { + return 0; + } + if (!BN_copy(r->X, x) || !(BN_copy(r->Y, y))) { + return 0; + } + return 1; +} + +const EC_POINT *EC_point_infinity() { + static const EC_POINT const_infinity = { NULL, NULL, 1 }; + return &const_infinity; +} + +int EC_POINT_is_infinity(const EC_POINT *point) { + return point->is_infinity; +} + +int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src) { + if (dest == NULL) { + return 0; + } + if (EC_POINT_is_infinity(src)) { + dest->is_infinity = 1; + } + else{ + dest->is_infinity = 0; + if (!BN_copy(dest->X, src->X) || !BN_copy(dest->Y, src->Y)) return 0; + } + return 1; +} + +#define NORMAL 0x1 +#define COMPRESS 0x2 +#define MIX 0x4 + +int EC_POINT_ecp2bin(uint8 *out, const EC_POINT *point, int p_bytes, int flag) { + uint8 PC; + uint8 *p = out, *buf; + int i; + buf = malloc(p_bytes); + if (EC_POINT_is_infinity(point)) { + return 0; + } + if (flag == NORMAL) { + PC = 0x04; + *(p++) = PC; + if (!BN_bn2binpad(point->X, buf, p_bytes)) { + return 0; + } + for (i = 0; i < p_bytes; ++i) { + *(p++) = buf[i]; + } + if (!BN_bn2binpad(point->Y, buf, p_bytes)) { + return 0; + } + for (i = 0; i < p_bytes; ++i) { + *(p++) = buf[i]; + } + } + else { + goto err; /* not supported */ + } + return 1; + err: + return 0; +} + +int EC_POINT_bin2ecp(EC_POINT *point, const uint8 *src, int p_bytes) { + if (point == NULL) { + return 0; + } + uint8 PC = *src; + const uint8 *data = src + 1; + uint8 *buf; + int i; + if (PC == 0x04) { + if (!BN_bin2bn(data, p_bytes, point->X) + || !BN_bin2bn(data+p_bytes, p_bytes, point->Y)) { + return 0; + } + } + else { + goto err; /* not supported */ + } + return 1; + err: + return 0; +} + +int EC_POINT_ecp2bin_normal(uint8 *out, const EC_POINT *point, int p_bytes) { + return EC_POINT_ecp2bin(out, point, p_bytes, NORMAL); +} + +// int EC_POINT_ecp2bin_compressed(uint8 *out, const EC_POINT *point, int p_bytes) { +// return EC_POINT_ecp2bin(out, point, p_bytes, COMPRESS); +// } + +// int EC_POINT_ecp2bin_mix(uint8 *out, const EC_POINT *point, int p_bytes) { +// return EC_POINT_ecp2bin(out, point, p_bytes, MIX); +// } + +typedef struct ec_group_st { + EC_POINT *generator; /* point G */ + BIGNUM *order; /* n */ + BIGNUM *cofactor; /* h */ + BIGNUM *field; /* module p */ + BIGNUM *a, *b; /* ECC parameter */ +} EC_GROUP; + +int EC_GROUP_init(EC_GROUP *group) { + group->generator = EC_POINT_new(); + group->order = BN_new(); + group->cofactor = BN_new(); + group->field = BN_new(); + group->a = BN_new(); + group->b = BN_new(); + if (group->generator == NULL || group->b == NULL) { + free(group->generator); + BN_free(group->order); + BN_free(group->cofactor); + BN_free(group->field); + BN_free(group->a); + BN_free(group->b); + return 0; + } + return 1; +} + +EC_GROUP *EC_GROUP_new() { + EC_GROUP *ret = NULL; + ret = malloc(sizeof(*ret)); + if (ret == NULL) { + return NULL; + } + if (!EC_GROUP_init(ret)) { + free(ret); + return NULL; + } + return ret; +} + +int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src) { + if (dest == NULL) { + return 0; + } + if (dest == src) { + return 1; + } + if (!EC_POINT_copy(dest->generator, src->generator) + || !BN_copy(dest->order, src->order) + || !BN_copy(dest->cofactor, src->cofactor) + || !BN_copy(dest->field, src->field) + || !BN_copy(dest->a, src->a) + || !BN_copy(dest->b, src->b)) { + return 0; + } + return 1; +} + +int EC_GROUP_set_param(EC_GROUP *group, const BIGNUM *field, const BIGNUM *a, const BIGNUM *b, const BIGNUM *order, const BIGNUM *cofactor, const EC_POINT *point) { + if (group == NULL) { + return 0; + } + group->field = BN_new(); + group->a = BN_new(); + group->b = BN_new(); + group->order = BN_new(); + group->cofactor = BN_new(); + group->generator = EC_POINT_new(); + if (group->cofactor == NULL + || group->generator == NULL) { + return 0; + } + if (!BN_copy(group->field, field) + || !BN_copy(group->a, a) + || !BN_copy(group->b, b) + || !BN_copy(group->order, order) + || !BN_copy(group->cofactor, cofactor) + || !EC_POINT_copy(group->generator, point)) { + return 0; + } + return 1; +} + +int EC_POINT_double(EC_POINT *r, const EC_POINT *a, const EC_GROUP *group) { + const BIGNUM *p; + BN_CTX *ctx = NULL; + int ret = 0; + BIGNUM *ax, *ay, *lnum, *ldeno; + BIGNUM *tmp, *bn_value_3; + if (r == NULL) { + return 0; + } + if (EC_POINT_is_infinity(a)) { + return EC_POINT_copy(r, EC_point_infinity()); + } + p = group->field; + ctx = BN_CTX_new(); + if (ctx == NULL) { + return 0; + } + BN_CTX_start(ctx); + ax = BN_CTX_get(ctx); + ay = BN_CTX_get(ctx); + lnum = BN_CTX_get(ctx); + ldeno = BN_CTX_get(ctx); + tmp = BN_CTX_get(ctx); + bn_value_3 = BN_CTX_get(ctx); + if (bn_value_3 == NULL) { + goto done; + } + if (!BN_copy(ax, a->X) + || !BN_copy(ay, a->Y)) { + goto done; + } + if (!BN_set_word(bn_value_3, 3) + || !BN_mod_sqr(lnum, ax, p, ctx) + || !BN_mod_mul(lnum, lnum, bn_value_3, p, ctx) + || !BN_mod_add_quick(lnum, lnum, group->a, p) + || !BN_mod_lshift1_quick(ldeno, ay, p) + || !BN_mod_inverse(ldeno, ldeno, p, ctx) + || !BN_mod_mul(lnum, lnum, ldeno, p, ctx)) { + goto done; + } + if (!BN_mod_sqr(r->X, lnum, p, ctx)) { + goto done; + } + if (!BN_mod_lshift1_quick(tmp, ax, p)) { + goto done; + } + if (!BN_mod_sub_quick(r->X, r->X, tmp, p)) { + goto done; + } + if (!BN_mod_sub_quick(r->Y, ax, r->X, p)) { + goto done; + } + if (!BN_mod_mul(r->Y, lnum, r->Y, p, ctx)) { + goto done; + } + if (!BN_mod_sub_quick(r->Y, r->Y, ay, p)) { + goto done; + } + if (r == NULL) { + r = EC_POINT_new(); + if (r == NULL) { + goto done; + } + } + ret = 1; + done: + BN_CTX_end(ctx); + BN_CTX_free(ctx); + return ret; +} + +int EC_POINT_add(EC_POINT *r, const EC_POINT *a, const EC_POINT *b, const EC_GROUP *group) { + const BIGNUM *p; + BN_CTX *ctx = NULL; + int ret = 0; + BIGNUM *ax, *ay, *bx, *by, *lnum, *ldeno; + BIGNUM *tmp, *bn_value_3; + if (r == NULL) { + return 0; + } + if (a == b) { + return EC_POINT_double(r, a, group); + } + if (EC_POINT_is_infinity(a)) { + return EC_POINT_copy(r, b); + } + if (EC_POINT_is_infinity(b)) { + return EC_POINT_copy(r, a); + } + p = group->field; + ctx = BN_CTX_new(); + if (ctx == NULL) { + return 0; + } + BN_CTX_start(ctx); + ax = BN_CTX_get(ctx); + ay = BN_CTX_get(ctx); + bx = BN_CTX_get(ctx); + by = BN_CTX_get(ctx); + lnum = BN_CTX_get(ctx); + ldeno = BN_CTX_get(ctx); + tmp = BN_CTX_get(ctx); + bn_value_3 = BN_CTX_get(ctx); + if (bn_value_3 == NULL) { + goto done; + } + if (!BN_copy(ax, a->X) + || !BN_copy(ay, a->Y) + || !BN_copy(bx, b->X) + || !BN_copy(by, b->Y)) { + goto done; + } + if (BN_cmp(ax, bx) == 0) { + if (!BN_mod_add_quick(tmp, ay, by, p)) { + goto done; + } + if (BN_is_zero(tmp) + || !EC_POINT_copy(r, EC_point_infinity())) { + goto done; + } + if (!BN_set_word(bn_value_3, 3)) { + goto done; + } + if (!BN_mod_sqr(lnum, ax, p, ctx) + || !BN_mod_mul(lnum, lnum, bn_value_3, p, ctx) + || !BN_mod_add_quick(lnum, lnum, group->a, p) + || !BN_mod_lshift1_quick(ldeno, ay, p) + || !BN_mod_inverse(ldeno, ldeno, p, ctx) + || !BN_mod_mul(lnum, lnum, ldeno, p, ctx)) { + goto done; + } + } + else { + if (!BN_mod_sub_quick(lnum, by, ay, p)) { + goto done; + } + if (!BN_mod_sub_quick(ldeno, bx, ax, p)) { + goto done; + } + if (!BN_mod_inverse(ldeno, ldeno, p, ctx)) { + goto done; + } + if (!BN_mod_mul(lnum, lnum, ldeno, p, ctx)) { + goto done; + } + } + if (!BN_mod_sqr(tmp, lnum, p, ctx)) { + goto done; + } + if (!BN_mod_sub_quick(tmp, tmp, ax, p)) { + goto done; + } + if (!BN_mod_sub_quick(r->X, tmp, bx, p)) { + goto done; + } + if (!BN_mod_sub_quick(r->Y, ax, r->X, p)) { + goto done; + } + if (!BN_mod_mul(r->Y, lnum, r->Y, p, ctx)) { + goto done; + } + if (!BN_mod_sub_quick(r->Y, r->Y, ay, p)) { + goto done; + } + if (r == NULL) { + r = EC_POINT_new(); + if (r == NULL) { + goto done; + } + } + ret = 1; + done: + BN_CTX_end(ctx); + BN_CTX_free(ctx); + return ret; +} + +int EC_POINT_mul(EC_POINT *r, const EC_POINT *a, const BIGNUM *b, const EC_GROUP *group) { + if (r == NULL) { + return 0; + } + if (BN_is_negative(b)) { + EC_POINT *_a = EC_POINT_new(); + BIGNUM *_b = BN_new(); + if (!EC_POINT_copy(_a, a) + || !BN_copy(_b, b) + || !BN_mod_sub_quick(_a->Y, group->field, _a->Y, group->field)) { + return 0; + } + BN_set_negative(_b, 0); + return EC_POINT_mul(r, _a, _b, group); + } + if (!EC_POINT_copy(r, EC_point_infinity())){ + return 0; + } + int bits = BN_num_bits(b); + for (int i = bits-1; i >= 0; --i) { + if (!EC_POINT_double(r, r, group)) { + return 0; + } + if (BN_is_bit_set(b, i)) { + if (!EC_POINT_add(r, r, a, group)) { + return 0; + } + } + } + return 1; +} + +/* + * Only for curve y^2 = x^3 + ax + b. + * Please check infinity before calling. + */ +int EC_POINT_on_curve_simple(const EC_POINT *point, const EC_GROUP *group) { + if (point == NULL || group == NULL) { + return 0; + } + if (EC_POINT_is_infinity(point)) { + return 1; + } + int ret = 0; + BN_CTX *ctx; + BIGNUM *p, *a, *b, *x, *y, *lhs, *rhs; + EC_POINT *Nq; + ctx = BN_CTX_new(); + if (ctx == NULL) { + return ret; + } + BN_CTX_start(ctx); + p = group->field; + a = group->a; + b = group->b; + x = point->X; + y = point->Y; + lhs = BN_CTX_get(ctx); + rhs = BN_CTX_get(ctx); + Nq = EC_POINT_new(); + if (p == NULL + || a == NULL + || b == NULL + || x == NULL + || y == NULL + || rhs == NULL + || Nq == NULL) { + return ret; + } + if (!BN_mod_mul(lhs, y, y, p, ctx) + || !BN_mod_mul(rhs, x, x, p, ctx) + || !BN_mod_add_quick(rhs, rhs, a, p) + || !BN_mod_mul(rhs, rhs, x, p, ctx) + || !BN_mod_add_quick(rhs, rhs, b, p) + || !EC_POINT_mul(Nq, point, group->order, group)) { + return ret; + } + if (BN_cmp(lhs, rhs) != 0 + || !EC_POINT_is_infinity(Nq)) { + return ret; + } + ret = 1; + BN_CTX_end(ctx); + BN_CTX_free(ctx); + return ret; +} + +typedef struct ec_key_st { + EC_GROUP *group; + EC_POINT *pub_key; + BIGNUM *priv_key; +} EC_KEY; + +int EC_KEY_init(EC_KEY *key) { + key->group = EC_GROUP_new(); + key->pub_key = EC_POINT_new(); + key->priv_key = BN_new(); + if (key->group == NULL + || key->pub_key == NULL + || key->priv_key == NULL) { + free(key->group); + free(key->pub_key); + BN_free(key->priv_key); + return 0; + } + return 1; +} + +EC_KEY *EC_KEY_new() { + EC_KEY *ret = NULL; + ret = malloc(sizeof(*ret)); + if (ret == NULL) { + return NULL; + } + if (!EC_KEY_init(ret)) { + free(ret); + return NULL; + } + return ret; +} + +int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group) { + if (!EC_GROUP_copy(key->group, group)) { + return 0; + } + return 1; +} + +int EC_KEY_set_priv_key(EC_KEY *key, const BIGNUM *priv_key) { + BIGNUM *tmp; + tmp = BN_dup(priv_key); + if (tmp == NULL) { + return 0; + } + BN_clear_free(key->priv_key); + key->priv_key = tmp; + return 1; +} + +int EC_KEY_generate_pub_key(EC_KEY *key) { + return EC_POINT_mul(key->pub_key, key->group->generator, key->priv_key, key->group); +} + +const BIGNUM *EC_KEY_get_priv_key(const EC_KEY *key) { + return key->priv_key; +} + +int EC_KEY_generate_key() {} diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/CMakeLists.txt" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/CMakeLists.txt" new file mode 100644 index 0000000000000000000000000000000000000000..e1b266d0fa36a029c025979ef2fc5a0f1b81274b --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/CMakeLists.txt" @@ -0,0 +1,38 @@ +# Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved. +# secGear is licensed under the Mulan PSL v2. +# You can use this software according to the terms and conditions of the Mulan PSL v2. +# You may obtain a copy of Mulan PSL v2 at: +# http://license.coscl.org.cn/MulanPSL2 +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR +# PURPOSE. +# See the Mulan PSL v2 for more details. + +cmake_minimum_required(VERSION 3.10 FATAL_ERROR) +project(enclave_ZUC) + +set(CMAKE_C_STANDARD 99) + +set(CURRENT_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) + +#set edl name +set(EDL_FILE enclave_ZUC.edl) +#set(LOCAL_ROOT_PATH "$ENV{CC_SDK}") +#set(SECGEAR_INSTALL_PATH /usr/lib64/) + +if(CC_GP) + set(CODETYPE trustzone) + set(CODEGEN codegen_arm64) + execute_process(COMMAND uuidgen -r OUTPUT_VARIABLE UUID) + string(REPLACE "\n" "" UUID ${UUID}) + add_definitions(-DPATH="/data/${UUID}.sec") +endif() + +if(CC_SGX) + set(CODETYPE sgx) + set(CODEGEN codegen_x86_64) + add_definitions(-DPATH="${CMAKE_CURRENT_BINARY_DIR}/enclave/enclave.signed.so") +endif() + +add_subdirectory(${CURRENT_ROOT_PATH}/enclave) +add_subdirectory(${CURRENT_ROOT_PATH}/host) diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/enclave/CMakeLists.txt" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/enclave/CMakeLists.txt" new file mode 100644 index 0000000000000000000000000000000000000000..d600d3883204c773447cb2e254ade5dd0d8b4606 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/enclave/CMakeLists.txt" @@ -0,0 +1,162 @@ +# Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved. +# secGear is licensed under the Mulan PSL v2. +# You can use this software according to the terms and conditions of the Mulan PSL v2. +# You may obtain a copy of Mulan PSL v2 at: +# http://license.coscl.org.cn/MulanPSL2 +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR +# PURPOSE. +# See the Mulan PSL v2 for more details. + +#set auto code prefix +set(PREFIX enclave_ZUC) + +set(CMAKE_C_STANDARD 99) +#set sign key +set(PEM Enclave_private.pem) + +#set sign tool +set(SIGN_TOOL ${LOCAL_ROOT_PATH}/tools/sign_tool/sign_tool.sh) + +#set enclave src code +set(SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/enclave_ZUC.c) + +#set log level +set(PRINT_LEVEL 3) +add_definitions(-DPRINT_LEVEL=${PRINT_LEVEL}) + +if(CC_GP) + #set signed output + set(OUTPUT ${UUID}.sec) + #set itrustee device key + set(DEVICEPEM ${CMAKE_CURRENT_SOURCE_DIR}/rsa_public_key_cloud.pem) + #set whilelist. default: /vendor/bin/teec_hello + set(WHITE_LIST_0 /vendor/bin/enclave_ZUC) + set(WHITE_LIST_OWNER root) + set(WHITE_LIST_1 /vendor/bin/secgear_enclave_ZUC) + set(WHITELIST WHITE_LIST_0 WHITE_LIST_1) + + set(AUTO_FILES ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_t.h ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_t.c ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_args.h) + add_custom_command(OUTPUT ${AUTO_FILES} + DEPENDS ${CURRENT_ROOT_PATH}/${EDL_FILE} + COMMAND ${CODEGEN} --${CODETYPE} --trusted ${CURRENT_ROOT_PATH}/${EDL_FILE} --search-path ${LOCAL_ROOT_PATH}/inc/host_inc/gp) +endif() + +if(CC_SGX) + set(OUTPUT enclave.signed.so) + set(AUTO_FILES ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_t.h ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_t.c) + add_custom_command(OUTPUT ${AUTO_FILES} + DEPENDS ${CURRENT_ROOT_PATH}/${EDL_FILE} + COMMAND ${CODEGEN} --${CODETYPE} --trusted ${CURRENT_ROOT_PATH}/${EDL_FILE} --search-path ${LOCAL_ROOT_PATH}/inc/host_inc/sgx --search-path ${SGXSDK}/include) +endif() + +set(COMMON_C_FLAGS "-W -Wall -fno-short-enums -fno-omit-frame-pointer -fstack-protector \ + -Wstack-protector --param ssp-buffer-size=4 -frecord-gcc-switches -Wextra -nostdinc -nodefaultlibs \ + -fno-peephole -fno-peephole2 -Wno-main -Wno-error=unused-parameter \ + -Wno-error=unused-but-set-variable -Wno-error=format-truncation=") + +set(COMMON_C_LINK_FLAGS "-Wl,-z,now -Wl,-z,relro -Wl,-z,noexecstack -Wl,-nostdlib -nodefaultlibs -nostartfiles") +set(COMMON_CXX_FLAGS "${COMMON_C_FLAGS}") + +if(CC_GP) + configure_file("${CMAKE_CURRENT_SOURCE_DIR}/manifest.txt.in" "${CMAKE_CURRENT_SOURCE_DIR}/manifest.txt") + + set(CMAKE_C_FLAGS "${COMMON_C_FLAGS} -march=armv8-a ") + set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -s -fPIC") + set(CMAKE_SHARED_LINKER_FLAGS "${COMMON_C_LINK_FLAGS} -Wl,-s") + + set(ITRUSTEE_TEEDIR ${iTrusteeSDK}/) + set(ITRUSTEE_LIBC ${iTrusteeSDK}/thirdparty/open_source/musl/libc) + + if(${CMAKE_VERSION} VERSION_LESS "3.13.0") + link_directories(${SECGEAR_INSTALL_PATH}) + endif() + + add_library(${PREFIX} SHARED ${SOURCE_FILES} ${AUTO_FILES}) + + target_include_directories( ${PREFIX} PRIVATE + ${CMAKE_CURRENT_BINARY_DIR} + ${LOCAL_ROOT_PATH}/inc/host_inc + ${LOCAL_ROOT_PATH}/inc/host_inc/gp + ${LOCAL_ROOT_PATH}/inc/enclave_inc + ${LOCAL_ROOT_PATH}/inc/enclave_inc/gp + ${ITRUSTEE_TEEDIR}/include/TA + ${ITRUSTEE_TEEDIR}/include/TA/huawei_ext + ${ITRUSTEE_LIBC}/arch/aarch64 + ${ITRUSTEE_LIBC}/ + ${ITRUSTEE_LIBC}/arch/arm/bits + ${ITRUSTEE_LIBC}/arch/generic + ${ITRUSTEE_LIBC}/arch/arm + ${LOCAL_ROOT_PATH}/inc/enclave_inc/gp/itrustee) + + if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.13.0") + target_link_directories(${PREFIX} PRIVATE ${SECGEAR_INSTALL_PATH}) + endif() + + foreach(WHITE_LIST ${WHITELIST}) + add_definitions(-D${WHITE_LIST}="${${WHITE_LIST}}") + endforeach(WHITE_LIST) + add_definitions(-DWHITE_LIST_OWNER="${WHITE_LIST_OWNER}") + + target_link_libraries(${PREFIX} -lsecgear_tee) + + add_custom_command(TARGET ${PREFIX} + POST_BUILD + COMMAND bash ${SIGN_TOOL} -d sign -x trustzone -i /home/sunny/secGear/debug/lib/lib${PREFIX}.so -c ${CMAKE_CURRENT_SOURCE_DIR}/manifest.txt -m ${CMAKE_CURRENT_SOURCE_DIR}/config_cloud.ini + -o ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT}) + + install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT} + DESTINATION /data + PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE) + +endif() + +if(CC_SGX) + set(SGX_DIR ${SGXSDK}) + set(CMAKE_C_FLAGS "${COMMON_C_FLAGS} -m64 -fvisibility=hidden") + set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -std=c++11 -O2 -nostdinc++") + set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -s") + set(LINK_LIBRARY_PATH ${SGX_DIR}/lib64) + + if(CC_SIM) + set(Trts_Library_Name sgx_trts_sim) + set(Service_Library_Name sgx_tservice_sim) + else() + set(Trts_Library_Name sgx_trts) + set(Service_Library_Name sgx_tservice) + endif() + + set(Crypto_Library_Name sgx_tcrypto) + + set(CMAKE_SHARED_LINKER_FLAGS "${COMMON_C_LINK_FLAGS} -Wl,-z,defs -Wl,-pie -Bstatic -Bsymbolic -eenclave_entry \ + -Wl,--export-dynamic -Wl,--defsym,__ImageBase=0 -Wl,--gc-sections -Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/Enclave.lds") + + if(${CMAKE_VERSION} VERSION_LESS "3.13.0") + link_directories(${LINK_LIBRARY_PATH}) + endif() + + add_library(${PREFIX} SHARED ${SOURCE_FILES} ${AUTO_FILES}) + + target_include_directories(${PREFIX} PRIVATE + ${CMAKE_CURRENT_BINARY_DIR} + ${SGX_DIR}/include/tlibc + ${SGX_DIR}/include/libcxx + ${SGX_DIR}/include + ${LOCAL_ROOT_PATH}/inc/host_inc + ${LOCAL_ROOT_PATH}/inc/host_inc/sgx) + + if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.13.0") + target_link_directories(${PREFIX} PRIVATE + ${LINK_LIBRARY_PATH}) + endif() + + target_link_libraries(${PREFIX} -Wl,--whole-archive ${Trts_Library_Name} -Wl,--no-whole-archive + -Wl,--start-group -lsgx_tstdc -lsgx_tcxx -l${Crypto_Library_Name} -l${Service_Library_Name} -Wl,--end-group) + add_custom_command(TARGET ${PREFIX} + POST_BUILD + COMMAND umask 0177 + COMMAND openssl genrsa -3 -out ${PEM} 3072 + COMMAND bash ${SIGN_TOOL} -d sign -x sgx -i ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/lib${PREFIX}.so -k ${PEM} -o ${OUTPUT} -c ${CMAKE_CURRENT_SOURCE_DIR}/Enclave.config.xml) +endif() + +set_target_properties(${PREFIX} PROPERTIES SKIP_BUILD_RPATH TRUE) diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/enclave/Enclave.config.xml" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/enclave/Enclave.config.xml" new file mode 100644 index 0000000000000000000000000000000000000000..a5e05e0175b43f97645fde0ea33a1fe0562398cf --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/enclave/Enclave.config.xml" @@ -0,0 +1,12 @@ + + 0 + 0 + 0x100000 + 0x80000 + 10 + 1 + + 1 + 0 + 0xFFFFFFFF + diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/enclave/Enclave.lds" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/enclave/Enclave.lds" new file mode 100644 index 0000000000000000000000000000000000000000..ab77e6478ef3c3448356e80b9884a8a533dd32c6 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/enclave/Enclave.lds" @@ -0,0 +1,11 @@ +enclave.so +{ + global: + g_global_data_sim; + g_global_data; + enclave_entry; + g_peak_heap_used; + local: + *; +}; + diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/enclave/enclave_ZUC.c" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/enclave/enclave_ZUC.c" new file mode 100644 index 0000000000000000000000000000000000000000..01cc67a017f138935110eea06341e448c6ca4b54 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/enclave/enclave_ZUC.c" @@ -0,0 +1,3 @@ +#include "Enclave1_t.h" +#include +#include "zuc.h" \ No newline at end of file diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/enclave/manifest.txt.in" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/enclave/manifest.txt.in" new file mode 100644 index 0000000000000000000000000000000000000000..7b8ecf5937d99b0c7f87478984017cdc92c44ed1 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/enclave/manifest.txt.in" @@ -0,0 +1,8 @@ +gpd.ta.appID: @UUID@ +gpd.ta.service_name: test0108 +gpd.ta.singleInstance: true +gpd.ta.multiSession: false +gpd.ta.multiCommand: false +gpd.ta.instanceKeepAlive: false +gpd.ta.dataSize: 16384 +gpd.ta.stackSize: 20480 diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/enclave/rsa_public_key_cloud.pem" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/enclave/rsa_public_key_cloud.pem" new file mode 100644 index 0000000000000000000000000000000000000000..a321f63ed2fb0459c03fcb63de8f2f21618d8e56 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/enclave/rsa_public_key_cloud.pem" @@ -0,0 +1,11 @@ +-----BEGIN PUBLIC KEY----- +MIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIBigKCAYEAzAPwbnbgBg7JgXERA9Bx +p7GLI1S3e1zL83RMd2+GXb6kO4yMKUL3NUCE2HhA2BtQYmLyGovx59UUcKnU58is +Xux++kH+A2shmOPjYvEFuX0Kt8tc19b8M9b/iHsY8ZmKykqia2a5U+IrECRFJo5p +DWUnl7jrHVtq78BSR1c7iXG1frrEC0AYCuqKJo/fxfmOKL0Y9mENCB3nAwjn9unD +BsO/OhkqvvB3nkeuMfNKPh4wCqtQPve13eTojbuxjX/3ePijplTI5X2Gr+n6Ximn +fYRlytQmMgMl/db0ARSKNApq9bmwzVNrnGWWZWJksdRvf6iL7t17Gs4L9AApOuC9 +WkzxPvwp5ZUqjsGd4oJGWeC6ZE6BTw2vxE+xMFI9uAKHxq9pBKkcGMa0g4fANNNV ++W+8JZGanxEXKB3y/M7BCyQAPCWOHC/RNjmRA1gczLYCPzC4pWu935UZdF1RR6zY +CD3t+FoOGGET/g4CwWgyhb5qkp65Hs6ayYt/DUAqo+yBAgMBAAE= +-----END PUBLIC KEY----- diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/enclave/zuc.h" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/enclave/zuc.h" new file mode 100644 index 0000000000000000000000000000000000000000..d8ea3eafb0ebec9c5b1cd1db436c8a3fee0cb041 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/enclave/zuc.h" @@ -0,0 +1,314 @@ +#ifndef SECGEAR_ZUC_H +#define SECGEAR_ZUC_H + +#include +#include "../include/common_utils.h" + +/* S_BOX BEGIN */ + +const uint8 ZUC_S[2][256] = { + { + 0x3E, 0x72, 0x5B, 0x47, 0xCA, 0xE0, 0x00, 0x33, 0x04, 0xD1, 0x54, 0x98, 0x09, 0xB9, 0x6D, 0xCB, + 0x7B, 0x1B, 0xF9, 0x32, 0xAF, 0x9D, 0x6A, 0xA5, 0xB8, 0x2D, 0xFC, 0x1D, 0x08, 0x53, 0x03, 0x90, + 0x4D, 0x4E, 0x84, 0x99, 0xE4, 0xCE, 0xD9, 0x91, 0xDD, 0xB6, 0x85, 0x48, 0x8B, 0x29, 0x6E, 0xAC, + 0xCD, 0xC1, 0xF8, 0x1E, 0x73, 0x43, 0x69, 0xC6, 0xB5, 0xBD, 0xFD, 0x39, 0x63, 0x20, 0xD4, 0x38, + 0x76, 0x7D, 0xB2, 0xA7, 0xCF, 0xED, 0x57, 0xC5, 0xF3, 0x2C, 0xBB, 0x14, 0x21, 0x06, 0x55, 0x9B, + 0xE3, 0xEF, 0x5E, 0x31, 0x4F, 0x7F, 0x5A, 0xA4, 0x0D, 0x82, 0x51, 0x49, 0x5F, 0xBA, 0x58, 0x1C, + 0x4A, 0x16, 0xD5, 0x17, 0xA8, 0x92, 0x24, 0x1F, 0x8C, 0xFF, 0xD8, 0xAE, 0x2E, 0x01, 0xD3, 0xAD, + 0x3B, 0x4B, 0xDA, 0x46, 0xEB, 0xC9, 0xDE, 0x9A, 0x8F, 0x87, 0xD7, 0x3A, 0x80, 0x6F, 0x2F, 0xC8, + 0xB1, 0xB4, 0x37, 0xF7, 0x0A, 0x22, 0x13, 0x28, 0x7C, 0xCC, 0x3C, 0x89, 0xC7, 0xC3, 0x96, 0x56, + 0x07, 0xBF, 0x7E, 0xF0, 0x0B, 0x2B, 0x97, 0x52, 0x35, 0x41, 0x79, 0x61, 0xA6, 0x4C, 0x10, 0xFE, + 0xBC, 0x26, 0x95, 0x88, 0x8A, 0xB0, 0xA3, 0xFB, 0xC0, 0x18, 0x94, 0xF2, 0xE1, 0xE5, 0xE9, 0x5D, + 0xD0, 0xDC, 0x11, 0x66, 0x64, 0x5C, 0xEC, 0x59, 0x42, 0x75, 0x12, 0xF5, 0x74, 0x9C, 0xAA, 0x23, + 0x0E, 0x86, 0xAB, 0xBE, 0x2A, 0x02, 0xE7, 0x67, 0xE6, 0x44, 0xA2, 0x6C, 0xC2, 0x93, 0x9F, 0xF1, + 0xF6, 0xFA, 0x36, 0xD2, 0x50, 0x68, 0x9E, 0x62, 0x71, 0x15, 0x3D, 0xD6, 0x40, 0xC4, 0xE2, 0x0F, + 0x8E, 0x83, 0x77, 0x6B, 0x25, 0x05, 0x3F, 0x0C, 0x30, 0xEA, 0x70, 0xB7, 0xA1, 0xE8, 0xA9, 0x65, + 0x8D, 0x27, 0x1A, 0xDB, 0x81, 0xB3, 0xA0, 0xF4, 0x45, 0x7A, 0x19, 0xDF, 0xEE, 0x78, 0x34, 0x60, + }, + { + 0x55, 0xC2, 0x63, 0x71, 0x3B, 0xC8, 0x47, 0x86, 0x9F, 0x3C, 0xDA, 0x5B, 0x29, 0xAA, 0xFD, 0x77, + 0x8C, 0xC5, 0x94, 0x0C, 0xA6, 0x1A, 0x13, 0x00, 0xE3, 0xA8, 0x16, 0x72, 0x40, 0xF9, 0xF8, 0x42, + 0x44, 0x26, 0x68, 0x96, 0x81, 0xD9, 0x45, 0x3E, 0x10, 0x76, 0xC6, 0xA7, 0x8B, 0x39, 0x43, 0xE1, + 0x3A, 0xB5, 0x56, 0x2A, 0xC0, 0x6D, 0xB3, 0x05, 0x22, 0x66, 0xBF, 0xDC, 0x0B, 0xFA, 0x62, 0x48, + 0xDD, 0x20, 0x11, 0x06, 0x36, 0xC9, 0xC1, 0xCF, 0xF6, 0x27, 0x52, 0xBB, 0x69, 0xF5, 0xD4, 0x87, + 0x7F, 0x84, 0x4C, 0xD2, 0x9C, 0x57, 0xA4, 0xBC, 0x4F, 0x9A, 0xDF, 0xFE, 0xD6, 0x8D, 0x7A, 0xEB, + 0x2B, 0x53, 0xD8, 0x5C, 0xA1, 0x14, 0x17, 0xFB, 0x23, 0xD5, 0x7D, 0x30, 0x67, 0x73, 0x08, 0x09, + 0xEE, 0xB7, 0x70, 0x3F, 0x61, 0xB2, 0x19, 0x8E, 0x4E, 0xE5, 0x4B, 0x93, 0x8F, 0x5D, 0xDB, 0xA9, + 0xAD, 0xF1, 0xAE, 0x2E, 0xCB, 0x0D, 0xFC, 0xF4, 0x2D, 0x46, 0x6E, 0x1D, 0x97, 0xE8, 0xD1, 0xE9, + 0x4D, 0x37, 0xA5, 0x75, 0x5E, 0x83, 0x9E, 0xAB, 0x82, 0x9D, 0xB9, 0x1C, 0xE0, 0xCD, 0x49, 0x89, + 0x01, 0xB6, 0xBD, 0x58, 0x24, 0xA2, 0x5F, 0x38, 0x78, 0x99, 0x15, 0x90, 0x50, 0xB8, 0x95, 0xE4, + 0xD0, 0x91, 0xC7, 0xCE, 0xED, 0x0F, 0xB4, 0x6F, 0xA0, 0xCC, 0xF0, 0x02, 0x4A, 0x79, 0xC3, 0xDE, + 0xA3, 0xEF, 0xEA, 0x51, 0xE6, 0x6B, 0x18, 0xEC, 0x1B, 0x2C, 0x80, 0xF7, 0x74, 0xE7, 0xFF, 0x21, + 0x5A, 0x6A, 0x54, 0x1E, 0x41, 0x31, 0x92, 0x35, 0xC4, 0x33, 0x07, 0x0A, 0xBA, 0x7E, 0x0E, 0x34, + 0x88, 0xB1, 0x98, 0x7C, 0xF3, 0x3D, 0x60, 0x6C, 0x7B, 0xCA, 0xD3, 0x1F, 0x32, 0x65, 0x04, 0x28, + 0x64, 0xBE, 0x85, 0x9B, 0x2F, 0x59, 0x8A, 0xD7, 0xB0, 0x25, 0xAC, 0xAF, 0x12, 0x03, 0xE2, 0xF2, + }, +}; + +const uint16 ZUC_D[16] = { + 042327, 023274, 061153, 011536, + 053611, 032742, 070465, 004657, + 046570, 027423, 065704, 015361, + 057046, 036115, 074232, 043654, +}; + +#define ZUC_S0(x) ZUC_S[0][x] +#define ZUC_S1(x) ZUC_S[1][x] +#define ZUC_S_BOX(X) \ + CONCAT_8x4( \ + ZUC_S0(EXTRACT_8_32(X, 0)), \ + ZUC_S1(EXTRACT_8_32(X, 1)), \ + ZUC_S0(EXTRACT_8_32(X, 2)), \ + ZUC_S1(EXTRACT_8_32(X, 3)) \ + ) + +/* S_BOX END */ + +/* MOD_OPERATIONS BEGIN */ + +#define ZUC_MOD_NUM 0x7FFFFFFF +uint32 zuc_add(uint32 a, uint32 b) { + uint32 c = a + b; + return (c & ZUC_MOD_NUM) + (c >> 31); +} +uint32 zuc_mul(uint32 a, uint32 b) { + uint32 res = 0; + for (int i = 0; i < 31; ++i) { + if (b & (1 << i)) { + res = zuc_add(res, ROTATE_31(a, i)); + } + } + return res; +} +uint32 zuc_mul_powof2(uint32 a, uint32 log2b) { + return ROTATE_31(a, log2b); +} + +/* MOD_OPERATIONS END */ + +/* LFSR BEGIN */ + +typedef struct ZUC_LFSR_st { + uint32 S[16]; /* 31-bit */ +} ZUC_LFSR; + +void zuc_lfsr_init(ZUC_LFSR *lfsr, uint32 u) { /* u: 31-bit */ + uint32 v = zuc_add( + zuc_add( + zuc_add( + zuc_mul_powof2(lfsr->S[15], 15), + zuc_mul_powof2(lfsr->S[13], 17) + ), + zuc_add( + zuc_mul_powof2(lfsr->S[10], 21), + zuc_mul_powof2(lfsr->S[4], 20) + ) + ), + zuc_add(lfsr->S[0], zuc_mul_powof2(lfsr->S[0], 8)) + ); + uint32 S16 = zuc_add(v, u); + S16 = zuc_add(S16, ZUC_MOD_NUM); + for (int i = 0; i < 15; ++i) { + lfsr->S[i] = lfsr->S[i + 1]; + } + lfsr->S[15] = S16; +} +void zuc_lfsr_work(ZUC_LFSR *lfsr) { + uint32 S16 = zuc_add( + zuc_add( + zuc_add( + zuc_mul_powof2(lfsr->S[15], 15), + zuc_mul_powof2(lfsr->S[13], 17) + ), + zuc_add( + zuc_mul_powof2(lfsr->S[10], 21), + zuc_mul_powof2(lfsr->S[4], 20) + ) + ), + zuc_add(lfsr->S[0], zuc_mul_powof2(lfsr->S[0], 8)) + ); + S16 = zuc_add(S16, ZUC_MOD_NUM); + for (int i = 0; i < 15; ++i) { + lfsr->S[i] = lfsr->S[i + 1]; + } + lfsr->S[15] = S16; +} + +/* LFSR END */ + +/* BR BEGIN */ + +typedef struct ZUC_BR_st { + uint32 X[4]; /* 32-bit */ +} ZUC_BR_CTX; + +void zuc_br(ZUC_BR_CTX *br, ZUC_LFSR *lfsr) { + br->X[0] = CONCAT_16x2(EXTRACT_16H_31(lfsr->S[15]), EXTRACT_16L_31(lfsr->S[14])); + br->X[1] = CONCAT_16x2(EXTRACT_16L_31(lfsr->S[11]), EXTRACT_16H_31(lfsr->S[9])); + br->X[2] = CONCAT_16x2(EXTRACT_16L_31(lfsr->S[7]), EXTRACT_16H_31(lfsr->S[5])); + br->X[3] = CONCAT_16x2(EXTRACT_16L_31(lfsr->S[2]), EXTRACT_16H_31(lfsr->S[0])); +} + +/* BR END */ + +/* F BEGIN */ + +#define ZUC_L1(X) \ + ((X) ^ ROTATE((X), 2) ^ ROTATE((X), 10) ^ ROTATE((X), 18) ^ ROTATE((X), 24)) +#define ZUC_L2(X) \ + ((X) ^ ROTATE((X), 8) ^ ROTATE((X), 14) ^ ROTATE((X), 22) ^ ROTATE((X), 30)) + +typedef struct ZUC_F_st { + uint32 R1, R2; /* 32-bit */ +} ZUC_F_CTX; + +uint32 zuc_f(ZUC_F_CTX *f, ZUC_BR_CTX *br) { /* X: 32-bit */ + uint32 X0 = br->X[0], X1 = br->X[1], X2 = br->X[2]; + uint32 R1 = f->R1, R2 = f->R2; + uint32 W = (X0 ^ R1) + R2; + uint32 W1 = R1 + X1; + uint32 W2 = R2 ^ X2; + R1 = ZUC_S_BOX(ZUC_L1(CONCAT_16x2( + EXTRACT_16_32(W1, 1), + EXTRACT_16_32(W2, 0) + ))); + R2 = ZUC_S_BOX(ZUC_L2(CONCAT_16x2( + EXTRACT_16_32(W2, 1), + EXTRACT_16_32(W1, 0) + ))); + f->R1 = R1; + f->R2 = R2; + return W; +} + +/* F END */ + +/* ZUC_MAIN BEGIN */ + +#define ZUC_INIT_CONCAT(k, d, iv) (((uint32)(k) << 15 | (d)) << 8 | (iv)) +#define ZUC_INTEGRITY_CONCAT(K1, K2, offset) \ + (((K1) << (offset)) | ((offset) ? (K2) >> (32 - (offset)) : 0)) + +typedef struct ZUCstate_st { + ZUC_LFSR lfsr; + ZUC_BR_CTX br; + ZUC_F_CTX f; +} ZUC_CTX; + +/* Initiate phase. Both k and iv are uint8 arrays of length 16. */ +int zuc_init(ZUC_CTX *z, uint8 k[], uint8 iv[]) { + memset(z, 0, sizeof(*z)); + for (int i = 0; i < 16; ++i) { + z->lfsr.S[i] = ZUC_INIT_CONCAT(k[i], ZUC_D[i], iv[i]); + } + for (int i = 0; i < 32; ++i) { + zuc_br(&z->br, &z->lfsr); + uint32 W = zuc_f(&z->f, &z->br); + zuc_lfsr_init(&z->lfsr, W >> 1); + } + return 1; +} + +/* Work phase. Z is uint32 array that stores output keys. */ +/* len is the length of the key in the unit of 32-bit. */ +int zuc_work(ZUC_CTX *z, uint32 Z[], size_t len) { + zuc_br(&z->br, &z->lfsr); + (void)zuc_f(&z->f, &z->br); + zuc_lfsr_work(&z->lfsr); + for (size_t i = 0; i < len; ++i) { + zuc_br(&z->br, &z->lfsr); + Z[i] = zuc_f(&z->f, &z->br) ^ z->br.X[3]; + zuc_lfsr_work(&z->lfsr); + } + return 1; +} + +/* Generate 32-bit ZUC key stream. Use the array Z[] to receive the output. */ +int zuc_final(uint8 k[], uint8 iv[], uint32 Z[], size_t len) { + ZUC_CTX res, *z; + z = &res; + zuc_init(z, k, iv); + zuc_work(z, Z, len); + return 1; +} + +/* Use the array obs[] to receive the output. */ +int zuc_confidentiality(uint32 count, uint8 bearer, uint8 direction, uint8 ck[], + uint32 length, uint32 ibs[], uint32 obs[]) { + /* + * @param COUNT (length = 32b, counter) + * @param BEARER (length = 5b, bearer identifier) + * @param DIRECTION (length = 1b, direction identifier) + * @param CK (length = 128b = 8b * 16, confidential key) + * @param LENGTH (length = 32b, bit length of input bit stream) + * @param IBS (length = LENGTH b = 32b * L, input bit stream) + * @param OBS (length = LENGTH b = 32b * L, output bit stream) + */ + + /* Initiate k and iv for ZUC. */ + bearer &= 0x1F; direction &= 1; + uint8 _k[16], _iv[16], _count[4]; + memcpy(_k, ck, sizeof _k); + for (int i = 0; i < 4; ++i) { + _count[i] = EXTRACT_8_32(count, i); + } + _iv[0] = _count[0]; _iv[1] = _count[1]; _iv[2] = _count[2]; _iv[3] = _count[3]; + _iv[4] = (uint8)((bearer << 1 | direction) << 2); _iv[5] = _iv[6] = _iv[7] = 0; + for (int i = 8; i < 16; ++i) { + _iv[i] = _iv[i ^ 8]; + } + size_t L = (length >> 5) + ((length & 0x1F) ? 1 : 0); + uint32 Z[L]; + zuc_final(_k, _iv, Z, L); + for (size_t i = 0; i < L; ++i) { + obs[i] = ibs[i] ^ Z[i]; + } + if (length & 0x1F) { + obs[L - 1] &= PREFIX_MASK_32[length & 0x1F]; + } + return 1; +} + +uint32 zuc_integrity(uint32 count, uint8 bearer, uint8 direction, uint8 ik[], + uint32 length, uint32 M[]) { + /* + * @param COUNT (length = 32b, counter) + * @param BEARER (length = 5b, bearer identifier) + * @param DIRECTION (length = 1b, direction identifier) + * @param IK (length = 128b = 8b * 16, integrity key) + * @param LENGTH (length = 32b, bit length of input bit stream) + * @param M (length = LENGTH b = 32b * L, input massage stream) + * @return MAC (length = 32b, massage verification code) + */ + + /* Initiate k and iv for ZUC. */ + bearer &= 0x1F; direction &= 1; + uint8 _k[16], _iv[16], _count[4]; + memcpy(_k, ik, sizeof _k); + for (int i = 0; i < 4; ++i) { + _count[i] = EXTRACT_8_32(count, i); + } + _iv[0] = _count[0]; _iv[1] = _count[1]; _iv[2] = _count[2]; _iv[3] = _count[3]; + _iv[4] = (uint8)(bearer << 3); _iv[5] = _iv[6] = _iv[7] = 0; + _iv[8] = _iv[0] ^ (uint8)(direction << 7); + _iv[9] = _iv[1]; _iv[10] = _iv[2]; _iv[11] = _iv[3]; + _iv[12] = _iv[4]; _iv[13] = _iv[5]; + _iv[14] = _iv[6] ^ (uint8)(direction << 7); + _iv[15] = _iv[7]; + size_t L = (length >> 5) + ((length & 0x1F) ? 1 : 0) + 2; + uint32 Z[L]; + uint32 T = 0; + zuc_final(_k, _iv, Z, L); + for (size_t i = 0; i < length; ++i) { + if (GET_BIT_VALUE_32(M[i >> 5], i & 0x1F)) { + T = T ^ ZUC_INTEGRITY_CONCAT(Z[i >> 5], Z[(i >> 5) + 1], i & 0x1F); + } + } + T = T ^ ZUC_INTEGRITY_CONCAT(Z[length >> 5], Z[(length >> 5) + 1], length & 0x1F); + T = T ^ Z[L - 1]; + return T; +} + +/* ZUC_MAIN END */ + +#endif \ No newline at end of file diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/enclave/zuctest1.c" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/enclave/zuctest1.c" new file mode 100644 index 0000000000000000000000000000000000000000..c1cb8ebddc8bbc995ed54d8b6672a640fdefd761 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/enclave/zuctest1.c" @@ -0,0 +1,58 @@ +#define SECGEAR_ZUC_DEBUG +#include +#include "zuc.h" + +uint8 k_test1[16] = { + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, +}; +uint8 iv_test1[16] = { + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, +}; + +uint8 k_test2[16] = { + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, +}; +uint8 iv_test2[16] = { + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, +}; + +uint8 k_test3[16] = { + 0x3D, 0x4C, 0x4B, 0xE9, + 0x6A, 0x82, 0xFD, 0xAE, + 0xB5, 0x8F, 0x64, 0x1D, + 0xB1, 0x7B, 0x45, 0x5B, +}; +uint8 iv_test3[16] = { + 0x84, 0x31, 0x9A, 0xA8, + 0xDE, 0x69, 0x15, 0xCA, + 0x1F, 0x6B, 0xDA, 0x6B, + 0xFB, 0xD8, 0xC7, 0x66, +}; + +void zuc_test(uint8 k[], uint8 iv[]) { + const size_t LEN = 32; + uint32 keys[LEN]; + zuc_final(k, iv, keys, LEN); +} + +int main(){ + printf("Test 1: \n"); + zuc_test(k_test1, iv_test1); + printf("Test 2: \n"); + zuc_test(k_test2, iv_test2); + printf("Test 3: \n"); + zuc_test(k_test3, iv_test3); + return 0; +} \ No newline at end of file diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/enclave/zuctest2.c" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/enclave/zuctest2.c" new file mode 100644 index 0000000000000000000000000000000000000000..2db840499c0fcca631695764ad081c74b731c725 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/enclave/zuctest2.c" @@ -0,0 +1,78 @@ +#define SECGEAR_ZUC_DEBUG_2 +#include +#include "zuc.h" + +uint8 ck_test1[16] = { + 0x17, 0x3d, 0x14, 0xba, + 0x50, 0x03, 0x73, 0x1d, + 0x7a, 0x60, 0x04, 0x94, + 0x70, 0xf0, 0x0a, 0x29, +}; +uint32 count_test1 = 0x66035492; +uint8 bearer_test1 = 0xf; +uint8 dirc_test1 = 0x0; +uint32 len_test1 = 0xc1; +uint32 ibs_test1[] = { + 0x6cf65340, 0x735552ab, 0x0c9752fa, 0x6f9025fe, 0x0bd675d9, + 0x005875b2, 0x00000000, +}; + +uint8 ck_test2[16] = { + 0xe5, 0xbd, 0x3e, 0xa0, + 0xeb, 0x55, 0xad, 0xe8, + 0x66, 0xc6, 0xac, 0x58, + 0xbd, 0x54, 0x30, 0x2a, +}; +uint32 count_test2 = 0x56823; +uint8 bearer_test2 = 0x18; +uint8 dirc_test2 = 0x1; +uint32 len_test2 = 0x320; +uint32 ibs_test2[] = { + 0x14a8ef69, 0x3d678507, 0xbbe7270a, 0x7f67ff50, 0x06c3525b, + 0x9807e467, 0xc4e56000, 0xba338f5d, 0x42955903, 0x67518222, + 0x46c80d3b, 0x38f07f4b, 0xe2d8ff58, 0x05f51322, 0x29bde93b, + 0xbbdcaf38, 0x2bf1ee97, 0x3fbf9977, 0xbada8945, 0x847a2a6c, + 0x9ad34a66, 0x7554e04d, 0x1f7fa2c3, 0x3241bd8f, 0x01ba220d, +}; + +uint8 ck_test3[16] = { + 0xe1, 0x3f, 0xed, 0x21, + 0xb4, 0x6e, 0x4e, 0x7e, + 0xc3, 0x12, 0x53, 0xb2, + 0xbb, 0x17, 0xb3, 0xe0, +}; +uint32 count_test3 = 0x2738cdaa; +uint8 bearer_test3 = 0x1a; +uint8 dirc_test3 = 0x0; +uint32 len_test3 = 0xfb3; +uint32 ibs_test3[] = { + 0x8d74e20d, 0x54894e06, 0xd3cb13cb, 0x3933065e, 0x8674be62, 0xadb1c72b, 0x3a646965, 0xab63cb7b, 0x7854dfdc, 0x27e84929, + 0xf49c64b8, 0x72a490b1, 0x3f957b64, 0x827e71f4, 0x1fbd4269, 0xa42c97f8, 0x24537027, 0xf86e9f4a, 0xd82d1df4, 0x51690fdd, + 0x98b6d03f, 0x3a0ebe3a, 0x312d6b84, 0x0ba5a182, 0x0b2a2c97, 0x09c090d2, 0x45ed267c, 0xf845ae41, 0xfa975d33, 0x33ac3009, + 0xfd40eba9, 0xeb5b8857, 0x14b768b6, 0x97138baf, 0x21380eca, 0x49f644d4, 0x8689e421, 0x5760b906, 0x739f0d2b, 0x3f091133, + 0xca15d981, 0xcbe401ba, 0xf72d05ac, 0xe05cccb2, 0xd297f4ef, 0x6a5f58d9, 0x1246cfa7, 0x7215b892, 0xab441d52, 0x78452795, + 0xccb7f5d7, 0x9057a1c4, 0xf77f80d4, 0x6db2033c, 0xb79bedf8, 0xe60551ce, 0x10c667f6, 0x2a97abaf, 0xabbcd677, 0x2018df96, + 0xa282ea73, 0x7ce2cb33, 0x1211f60d, 0x5354ce78, 0xf9918d9c, 0x206ca042, 0xc9b62387, 0xdd709604, 0xa50af16d, 0x8d35a890, + 0x6be484cf, 0x2e74a928, 0x99403643, 0x53249b27, 0xb4c9ae29, 0xeddfc7da, 0x6418791a, 0x4e7baa06, 0x60fa6451, 0x1f2d685c, + 0xc3a5ff70, 0xe0d2b742, 0x92e3b8a0, 0xcd6b04b1, 0xc790b8ea, 0xd2703708, 0x540dea2f, 0xc09c3da7, 0x70f65449, 0xe84d817a, + 0x4f551055, 0xe19ab850, 0x18a0028b, 0x71a144d9, 0x6791e9a3, 0x57793350, 0x4eee0060, 0x340c69d2, 0x74e1bf9d, 0x805dcbcc, + 0x1a6faa97, 0x6800b6ff, 0x2b671dc4, 0x63652fa8, 0xa33ee509, 0x74c1c21b, 0xe01eabb2, 0x16743026, 0x9d72ee51, 0x1c9dde30, + 0x797c9a25, 0xd86ce74f, 0x5b961be5, 0xfdfb6807, 0x814039e7, 0x137636bd, 0x1d7fa9e0, 0x9efd2007, 0x505906a5, 0xac45dfde, + 0xed7757bb, 0xee745749, 0xc2963335, 0x0bee0ea6, 0xf409df45, 0x80160000, +}; + +void zuc_test2(uint32 count, uint8 bearer, uint8 direction, uint8 ck[], + uint32 length, uint32 ibs[], uint32 obs[]) { + zuc_confidentiality(count, bearer, direction, ck, length, ibs, obs); +} + +int main(){ + uint32 obs_test[130]; + printf("Test 1: \n"); + zuc_test2(count_test1, bearer_test1, dirc_test1, ck_test1, len_test1, ibs_test1, obs_test); + printf("Test 2: \n"); + zuc_test2(count_test2, bearer_test2, dirc_test2, ck_test2, len_test2, ibs_test2, obs_test); + printf("Test 3: \n"); + zuc_test2(count_test3, bearer_test3, dirc_test3, ck_test3, len_test3, ibs_test3, obs_test); + return 0; +} \ No newline at end of file diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/enclave/zuctest3.c" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/enclave/zuctest3.c" new file mode 100644 index 0000000000000000000000000000000000000000..7a09e8cebc73961c9b9286dafe12fb35b6b8b61c --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/enclave/zuctest3.c" @@ -0,0 +1,81 @@ +#define SECGEAR_ZUC_DEBUG_3 +#include +#include "zuc.h" + +uint8 ik_test1[16] = { + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, +}; +uint32 count_test1 = 0; +uint8 bearer_test1 = 0; +uint8 dirc_test1 = 0; +uint32 len_test1 = 1; +uint32 m_test1[] = { + 0x00000000, +}; + +uint8 ik_test2[16] = { + 0xc9, 0xe6, 0xce, 0xc4, + 0x60, 0x7c, 0x72, 0xdb, + 0x00, 0x0a, 0xef, 0xa8, + 0x83, 0x85, 0xab, 0x0a, +}; +uint32 count_test2 = 0xa94059da; +uint8 bearer_test2 = 0xa; +uint8 dirc_test2 = 0x1; +uint32 len_test2 = 0x241; +uint32 m_test2[] = { + 0x983b41d4, 0x7d780c9e, 0x1ad11d7e, 0xb70391b1, 0xde0b35da, 0x2dc62f83, 0xe7b78d63, 0x06ca0ea0, 0x7e941b7b, + 0xe91348f9, 0xfcb170e2, 0x217fecd9, 0x7f9f68ad, 0xb16e5d7d, 0x21e569d2, 0x80ed775c, 0xebde3f40, 0x93c53881, + 0x00000000, +}; + +uint8 ik_test3[16] = { + 0x6b, 0x8b, 0x08, 0xee, + 0x79, 0xe0, 0xb5, 0x98, + 0x2d, 0x6d, 0x12, 0x8e, + 0xa9, 0xf2, 0x20, 0xcb, +}; +uint32 count_test3 = 0x561eb2dd; +uint8 bearer_test3 = 0x1c; +uint8 dirc_test3 = 0x0; +uint32 len_test3 = 0x1626; +uint32 m_test3[] = { + 0x5bad7247, 0x10ba1c56, 0xd5a315f8, 0xd40f6e09, 0x3780be8e, 0x8de07b69, 0x92432018, 0xe08ed96a, 0x5734af8b, + 0xad8a575d, 0x3a1f162f, 0x85045cc7, 0x70925571, 0xd9f5b94e, 0x454a77c1, 0x6e72936b, 0xf016ae15, 0x7499f054, + 0x3b5d52ca, 0xa6dbeab6, 0x97d2bb73, 0xe41b8075, 0xdce79b4b, 0x86044f66, 0x1d4485a5, 0x43dd7860, 0x6e0419e8, + 0x059859d3, 0xcb2b67ce, 0x0977603f, 0x81ff839e, 0x33185954, 0x4cfbc8d0, 0x0fef1a4c, 0x8510fb54, 0x7d6b06c6, + 0x11ef44f1, 0xbce107cf, 0xa45a06aa, 0xb360152b, 0x28dc1ebe, 0x6f7fe09b, 0x0516f9a5, 0xb02a1bd8, 0x4bb0181e, + 0x2e89e19b, 0xd8125930, 0xd178682f, 0x3862dc51, 0xb636f04e, 0x720c47c3, 0xce51ad70, 0xd94b9b22, 0x55fbae90, + 0x6549f499, 0xf8c6d399, 0x47ed5e5d, 0xf8e2def1, 0x13253e7b, 0x08d0a76b, 0x6bfc68c8, 0x12f375c7, 0x9b8fe5fd, + 0x85976aa6, 0xd46b4a23, 0x39d8ae51, 0x47f680fb, 0xe70f978b, 0x38effd7b, 0x2f7866a2, 0x2554e193, 0xa94e98a6, + 0x8b74bd25, 0xbb2b3f5f, 0xb0a5fd59, 0x887f9ab6, 0x8159b717, 0x8d5b7b67, 0x7cb546bf, 0x41eadca2, 0x16fc1085, + 0x0128f8bd, 0xef5c8d89, 0xf96afa4f, 0xa8b54885, 0x565ed838, 0xa950fee5, 0xf1c3b0a4, 0xf6fb71e5, 0x4dfd169e, + 0x82cecc72, 0x66c850e6, 0x7c5ef0ba, 0x960f5214, 0x060e71eb, 0x172a75fc, 0x1486835c, 0xbea65344, 0x65b055c9, + 0x6a72e410, 0x52241823, 0x25d83041, 0x4b40214d, 0xaa8091d2, 0xe0fb010a, 0xe15c6de9, 0x0850973b, 0xdf1e423b, + 0xe148a237, 0xb87a0c9f, 0x34d4b476, 0x05b803d7, 0x43a86a90, 0x399a4af3, 0x96d3a120, 0x0a62f3d9, 0x507962e8, + 0xe5bee6d3, 0xda2bb3f7, 0x237664ac, 0x7a292823, 0x900bc635, 0x03b29e80, 0xd63f6067, 0xbf8e1716, 0xac25beba, + 0x350deb62, 0xa99fe031, 0x85eb4f69, 0x937ecd38, 0x7941fda5, 0x44ba67db, 0x09117749, 0x38b01827, 0xbcc69c92, + 0xb3f772a9, 0xd2859ef0, 0x03398b1f, 0x6bbad7b5, 0x74f7989a, 0x1d10b2df, 0x798e0dbf, 0x30d65874, 0x64d24878, + 0xcd00c0ea, 0xee8a1a0c, 0xc753a279, 0x79e11b41, 0xdb1de3d5, 0x038afaf4, 0x9f5c682c, 0x3748d8a3, 0xa9ec54e6, + 0xa371275f, 0x1683510f, 0x8e4f9093, 0x8f9ab6e1, 0x34c2cfdf, 0x4841cba8, 0x8e0cff2b, 0x0bcc8e6a, 0xdcb71109, + 0xb5198fec, 0xf1bb7e5c, 0x531aca50, 0xa56a8a3b, 0x6de59862, 0xd41fa113, 0xd9cd9578, 0x08f08571, 0xd9a4bb79, + 0x2af271f6, 0xcc6dbb8d, 0xc7ec36e3, 0x6be1ed30, 0x8164c31c, 0x7c0afc54, 0x1c000000, +}; + +uint32 zuc_test3(uint32 count, uint8 bearer, uint8 direction, uint8 ik[], + uint32 length, uint32 M[]) { + return zuc_integrity(count, bearer, direction, ik, length, M); +} + +int main(){ + printf("Test 1: \n"); + zuc_test3(count_test1, bearer_test1, dirc_test1, ik_test1, len_test1, m_test1); + printf("Test 2: \n"); + zuc_test3(count_test2, bearer_test2, dirc_test2, ik_test2, len_test2, m_test2); + printf("Test 3: \n"); + zuc_test3(count_test3, bearer_test3, dirc_test3, ik_test3, len_test3, m_test3); + return 0; +} \ No newline at end of file diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/enclave_ZUC.edl" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/enclave_ZUC.edl" new file mode 100644 index 0000000000000000000000000000000000000000..0e454ee2a21540888c71f4ad7beca325d45c15ad --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/enclave_ZUC.edl" @@ -0,0 +1,27 @@ +enclave { + include "secgear_urts.h" + from "secgear_tstdc.edl" import *; + + trusted { + public void setRSAsource([in, size=len] char* src, size_t len); + public void getRSAresult([out, size=len] char* buf, size_t len); + public void calcEmbeddingResult(); + public void initKeys([in] uint32_t *src); + public void registerSeed([in] uint32_t *src); + public void setKeys([in,count=2] uint32_t *K); + public void getKeys([out,count=2] uint32_t *K); + public void setD([in, size=len] char* src, size_t len); + public void setCD([in, size=len] char* src, size_t len); + public void setN([in, size=len] char* src, size_t len); + public void getCD([out, size=len] char* buf, size_t len); + public void getX([out, size=len] char* buf, size_t len); + public void getMu([out, size=len] char* buf, size_t len); + public void getK([out, size=len] char* buf, size_t len); + public void getNWID([out, size=len] char* buf, size_t len); + public void setNPW([in, size=len] char* src, size_t len); + }; + + untrusted { + + }; +}; diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/host/CMakeLists.txt" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/host/CMakeLists.txt" new file mode 100644 index 0000000000000000000000000000000000000000..1330657eb38dcf372c8ffde55188b18bb72a1180 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/host/CMakeLists.txt" @@ -0,0 +1,88 @@ +# Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved. +# secGear is licensed under the Mulan PSL v2. +# You can use this software according to the terms and conditions of the Mulan PSL v2. +# You may obtain a copy of Mulan PSL v2 at: +# http://license.coscl.org.cn/MulanPSL2 +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR +# PURPOSE. +# See the Mulan PSL v2 for more details. + +#set auto code prefix +set(PREFIX enclave_ZUC) +#set host exec name +set(OUTPUT secgear_enclave_ZUC) +#set host src code +set(SOURCE_FILE ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp) + +set(CMAKE_CXX_STANDARD 11) + +#set auto code +if(CC_GP) + set(AUTO_FILES ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_u.h ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_u.c ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_args.h) + add_custom_command(OUTPUT ${AUTO_FILES} + DEPENDS ${CURRENT_ROOT_PATH}/${EDL_FILE} + COMMAND ${CODEGEN} --${CODETYPE} --untrusted ${CURRENT_ROOT_PATH}/${EDL_FILE} --search-path ${LOCAL_ROOT_PATH}/inc/host_inc/gp) +endif() + +if(CC_SGX) + set(AUTO_FILES ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_u.h ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_u.c) + add_custom_command(OUTPUT ${AUTO_FILES} + DEPENDS ${CURRENT_ROOT_PATH}/${EDL_FILE} + COMMAND ${CODEGEN} --${CODETYPE} --untrusted ${CURRENT_ROOT_PATH}/${EDL_FILE} --search-path ${LOCAL_ROOT_PATH}/inc/host_inc/sgx --search-path ${SGXSDK}/include) +endif() + +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIE") +set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -O2 -fPIE") +set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -s") + +if(CC_GP) + if(${CMAKE_VERSION} VERSION_LESS "3.13.0") + link_directories(${SECGEAR_INSTALL_PATH}) + endif() + add_executable(${OUTPUT} ${SOURCE_FILE} ${AUTO_FILES}) + target_include_directories(${OUTPUT} PRIVATE + ${LOCAL_ROOT_PATH}/inc/host_inc + ${LOCAL_ROOT_PATH}/inc/host_inc/gp + ${CMAKE_CURRENT_BINARY_DIR}) + if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.13.0") + target_link_directories(${OUTPUT} PRIVATE ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}) + endif() +endif() + +if(CC_SGX) + if(${CMAKE_VERSION} VERSION_LESS "3.13.0") + link_directories(${CMAKE_LIBRARY_OUTPUT_DIRECTORY}) + endif() + add_executable(${OUTPUT} ${SOURCE_FILE} ${AUTO_FILES}) + target_include_directories(${OUTPUT} PRIVATE + ${LOCAL_ROOT_PATH}/inc/host_inc + ${LOCAL_ROOT_PATH}/inc/host_inc/sgx + ${CMAKE_CURRENT_BINARY_DIR}) + if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.13.0") + target_link_directories(${OUTPUT} PRIVATE ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}) + endif() +endif() + +if(CC_SIM) + target_link_libraries(${OUTPUT} secgearsim) +else() + target_link_libraries(${OUTPUT} secgear) +endif() +set_target_properties(${OUTPUT} PROPERTIES SKIP_BUILD_RPATH TRUE) + +if(CC_GP) + #itrustee install whitelist /vender/bin/teec_hello + install(TARGETS ${OUTPUT} + RUNTIME + DESTINATION /vendor/bin/ + PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ) +endif() + +if(CC_SGX) + install(TARGETS ${OUTPUT} + RUNTIME + DESTINATION ${CMAKE_BINARY_DIR}/bin/ + PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ) +endif() + diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/host/main.cpp" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/host/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..9add3a7ec8d756fad4de2f60aa79049bd1daf2c0 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/host/main.cpp" @@ -0,0 +1,27 @@ +#include"enclave.h" +#include"Enclave1_u.h" +#include +#include + +#define MAX_LEN 1024 + +using namespace std; + +int main(int argc, char* argv[]) { + cc_enclave_t *eid; + cc_enclave_result_t ret; + char *path = transferFromString(PATH); + + ret = cc_enclave_create(path, AUTO_ENCLAVE_TYPE, 0, SECGEAR_DEBUG_FLAG, NULL, 0, &eid); + if (ret != CC_SUCCESS) { + printf("ERR: %#x ,failed to create enclave.\n", ret); + return -1; + } + + + + if (CC_SUCCESS != cc_enclave_destroy(eid)) { + return -1; + } + return 0; +} diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/include/common_utils.h" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/include/common_utils.h" new file mode 100644 index 0000000000000000000000000000000000000000..9251edce48cf8aeb25404fcf5391335698a83a06 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/include/common_utils.h" @@ -0,0 +1,69 @@ +#ifndef SECGEAR_SM_COMMON_UTILS +#define SECGEAR_SM_COMMON_UTILS + +#include +#define debugPrint (printf("%s: %s: %d\n",__FILE__,__FUNCTION__,__LINE__),fflush(stdout),1) + +typedef __uint8_t uint8; +typedef __uint16_t uint16; +typedef __uint32_t uint32; +typedef __uint64_t uint64; + +#define CONCAT_8x4(x0, x1, x2, x3) ((((uint32)(x0) << 8 | (x1)) << 8 | (x2)) << 8 | (x3)) // X = (x0 || x1 || x2 || x3) +#define CONCAT_16x2(x0, x1) ((uint32)(x0) << 16 | (x1)) // X = (x0 || x1) +#define EXTRACT_8_32(X, i) (((X) >> (((i) ^ 3) << 3)) & 0xFF) // X = (x0 || x1 || x2 || x3) +#define EXTRACT_16_32(X, i) (((X) >> (((i) ^ 1) << 4)) & 0xFFFF) // X = (x0 || x1) +#define EXTRACT_16H_31(X) (((X) >> 15) & 0xFFFF) // Higher 16-bit from 31-bit +#define EXTRACT_16L_31(X) ((X) & 0xFFFF) // Lower 16-bit from 31-bit + +#define ROTATE(a, n) (((a) << (n)) | (((a) & 0xFFFFFFFF) >> ((32 - (n)) & 31))) +#define ROTATE_31(a, n) ((((a) << (n)) & 0x7FFFFFFF) | (((a) & 0x7FFFFFFF) >> (31 - (n)))) + +#define GET_BIT_VALUE_32(x, i) (((x) >> (31 - (i))) & 1) + +const uint32 PREFIX_MASK_32[32] = { + 0x00000000, + 0x80000000, + 0xC0000000, + 0xE0000000, + 0xF0000000, + 0xF8000000, + 0xFC000000, + 0xFE000000, + 0xFF000000, + 0xFF800000, + 0xFFC00000, + 0xFFE00000, + 0xFFF00000, + 0xFFF80000, + 0xFFFC0000, + 0xFFFE0000, + 0xFFFF0000, + 0xFFFF8000, + 0xFFFFC000, + 0xFFFFE000, + 0xFFFFF000, + 0xFFFFF800, + 0xFFFFFC00, + 0xFFFFFE00, + 0xFFFFFF00, + 0xFFFFFF80, + 0xFFFFFFC0, + 0xFFFFFFE0, + 0xFFFFFFF0, + 0xFFFFFFF8, + 0xFFFFFFFC, + 0xFFFFFFFE +}; + +# define HOST_c2l(c,l) (l =(((unsigned long)(*((c)++)))<<24), \ + l|=(((unsigned long)(*((c)++)))<<16), \ + l|=(((unsigned long)(*((c)++)))<< 8), \ + l|=(((unsigned long)(*((c)++))) ) ) +# define HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l)>>24)&0xff), \ + *((c)++)=(unsigned char)(((l)>>16)&0xff), \ + *((c)++)=(unsigned char)(((l)>> 8)&0xff), \ + *((c)++)=(unsigned char)(((l) )&0xff), \ + l) + +#endif \ No newline at end of file diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/include/ec.h" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/include/ec.h" new file mode 100644 index 0000000000000000000000000000000000000000..56adbe751f17342f84c17e3736f8806deb7f40b5 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Formal/enclave_ZUC/include/ec.h" @@ -0,0 +1,545 @@ +#include /* BigNum */ +#include "common_utils.h" + +typedef struct ec_point_st { + BIGNUM *X; + BIGNUM *Y; + int is_infinity; +} EC_POINT; + +int EC_POINT_init(EC_POINT *point) { + point->X = BN_new(); + point->Y = BN_new(); + point->is_infinity = 0; + if (point->X == NULL || point->Y == NULL) { + BN_free(point->X); + BN_free(point->Y); + return 0; + } + return 1; +} + +EC_POINT *EC_POINT_new() { + EC_POINT *ret = NULL; + ret = malloc(sizeof(*ret)); + if (ret == NULL) { + return NULL; + } + if (!EC_POINT_init(ret)) { + free(ret); + return NULL; + } + return ret; +} + +int EC_POINT_setXY(EC_POINT *r, const BIGNUM *x, const BIGNUM *y) { + if (r == NULL) { + return 0; + } + if (!BN_copy(r->X, x) || !(BN_copy(r->Y, y))) { + return 0; + } + return 1; +} + +const EC_POINT *EC_point_infinity() { + static const EC_POINT const_infinity = { NULL, NULL, 1 }; + return &const_infinity; +} + +int EC_POINT_is_infinity(const EC_POINT *point) { + return point->is_infinity; +} + +int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src) { + if (dest == NULL) { + return 0; + } + if (EC_POINT_is_infinity(src)) { + dest->is_infinity = 1; + } + else{ + dest->is_infinity = 0; + if (!BN_copy(dest->X, src->X) || !BN_copy(dest->Y, src->Y)) return 0; + } + return 1; +} + +#define NORMAL 0x1 +#define COMPRESS 0x2 +#define MIX 0x4 + +int EC_POINT_ecp2bin(uint8 *out, const EC_POINT *point, int p_bytes, int flag) { + uint8 PC; + uint8 *p = out, *buf; + int i; + buf = malloc(p_bytes); + if (EC_POINT_is_infinity(point)) { + return 0; + } + if (flag == NORMAL) { + PC = 0x04; + *(p++) = PC; + if (!BN_bn2binpad(point->X, buf, p_bytes)) { + return 0; + } + for (i = 0; i < p_bytes; ++i) { + *(p++) = buf[i]; + } + if (!BN_bn2binpad(point->Y, buf, p_bytes)) { + return 0; + } + for (i = 0; i < p_bytes; ++i) { + *(p++) = buf[i]; + } + } + else { + goto err; /* not supported */ + } + return 1; + err: + return 0; +} + +int EC_POINT_bin2ecp(EC_POINT *point, const uint8 *src, int p_bytes) { + if (point == NULL) { + return 0; + } + uint8 PC = *src; + const uint8 *data = src + 1; + uint8 *buf; + int i; + if (PC == 0x04) { + if (!BN_bin2bn(data, p_bytes, point->X) + || !BN_bin2bn(data+p_bytes, p_bytes, point->Y)) { + return 0; + } + } + else { + goto err; /* not supported */ + } + return 1; + err: + return 0; +} + +int EC_POINT_ecp2bin_normal(uint8 *out, const EC_POINT *point, int p_bytes) { + return EC_POINT_ecp2bin(out, point, p_bytes, NORMAL); +} + +// int EC_POINT_ecp2bin_compressed(uint8 *out, const EC_POINT *point, int p_bytes) { +// return EC_POINT_ecp2bin(out, point, p_bytes, COMPRESS); +// } + +// int EC_POINT_ecp2bin_mix(uint8 *out, const EC_POINT *point, int p_bytes) { +// return EC_POINT_ecp2bin(out, point, p_bytes, MIX); +// } + +typedef struct ec_group_st { + EC_POINT *generator; /* point G */ + BIGNUM *order; /* n */ + BIGNUM *cofactor; /* h */ + BIGNUM *field; /* module p */ + BIGNUM *a, *b; /* ECC parameter */ +} EC_GROUP; + +int EC_GROUP_init(EC_GROUP *group) { + group->generator = EC_POINT_new(); + group->order = BN_new(); + group->cofactor = BN_new(); + group->field = BN_new(); + group->a = BN_new(); + group->b = BN_new(); + if (group->generator == NULL || group->b == NULL) { + free(group->generator); + BN_free(group->order); + BN_free(group->cofactor); + BN_free(group->field); + BN_free(group->a); + BN_free(group->b); + return 0; + } + return 1; +} + +EC_GROUP *EC_GROUP_new() { + EC_GROUP *ret = NULL; + ret = malloc(sizeof(*ret)); + if (ret == NULL) { + return NULL; + } + if (!EC_GROUP_init(ret)) { + free(ret); + return NULL; + } + return ret; +} + +int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src) { + if (dest == NULL) { + return 0; + } + if (dest == src) { + return 1; + } + if (!EC_POINT_copy(dest->generator, src->generator) + || !BN_copy(dest->order, src->order) + || !BN_copy(dest->cofactor, src->cofactor) + || !BN_copy(dest->field, src->field) + || !BN_copy(dest->a, src->a) + || !BN_copy(dest->b, src->b)) { + return 0; + } + return 1; +} + +int EC_GROUP_set_param(EC_GROUP *group, const BIGNUM *field, const BIGNUM *a, const BIGNUM *b, const BIGNUM *order, const BIGNUM *cofactor, const EC_POINT *point) { + if (group == NULL) { + return 0; + } + group->field = BN_new(); + group->a = BN_new(); + group->b = BN_new(); + group->order = BN_new(); + group->cofactor = BN_new(); + group->generator = EC_POINT_new(); + if (group->cofactor == NULL + || group->generator == NULL) { + return 0; + } + if (!BN_copy(group->field, field) + || !BN_copy(group->a, a) + || !BN_copy(group->b, b) + || !BN_copy(group->order, order) + || !BN_copy(group->cofactor, cofactor) + || !EC_POINT_copy(group->generator, point)) { + return 0; + } + return 1; +} + +int EC_POINT_double(EC_POINT *r, const EC_POINT *a, const EC_GROUP *group) { + const BIGNUM *p; + BN_CTX *ctx = NULL; + int ret = 0; + BIGNUM *ax, *ay, *lnum, *ldeno; + BIGNUM *tmp, *bn_value_3; + if (r == NULL) { + return 0; + } + if (EC_POINT_is_infinity(a)) { + return EC_POINT_copy(r, EC_point_infinity()); + } + p = group->field; + ctx = BN_CTX_new(); + if (ctx == NULL) { + return 0; + } + BN_CTX_start(ctx); + ax = BN_CTX_get(ctx); + ay = BN_CTX_get(ctx); + lnum = BN_CTX_get(ctx); + ldeno = BN_CTX_get(ctx); + tmp = BN_CTX_get(ctx); + bn_value_3 = BN_CTX_get(ctx); + if (bn_value_3 == NULL) { + goto done; + } + if (!BN_copy(ax, a->X) + || !BN_copy(ay, a->Y)) { + goto done; + } + if (!BN_set_word(bn_value_3, 3) + || !BN_mod_sqr(lnum, ax, p, ctx) + || !BN_mod_mul(lnum, lnum, bn_value_3, p, ctx) + || !BN_mod_add_quick(lnum, lnum, group->a, p) + || !BN_mod_lshift1_quick(ldeno, ay, p) + || !BN_mod_inverse(ldeno, ldeno, p, ctx) + || !BN_mod_mul(lnum, lnum, ldeno, p, ctx)) { + goto done; + } + if (!BN_mod_sqr(r->X, lnum, p, ctx)) { + goto done; + } + if (!BN_mod_lshift1_quick(tmp, ax, p)) { + goto done; + } + if (!BN_mod_sub_quick(r->X, r->X, tmp, p)) { + goto done; + } + if (!BN_mod_sub_quick(r->Y, ax, r->X, p)) { + goto done; + } + if (!BN_mod_mul(r->Y, lnum, r->Y, p, ctx)) { + goto done; + } + if (!BN_mod_sub_quick(r->Y, r->Y, ay, p)) { + goto done; + } + if (r == NULL) { + r = EC_POINT_new(); + if (r == NULL) { + goto done; + } + } + ret = 1; + done: + BN_CTX_end(ctx); + BN_CTX_free(ctx); + return ret; +} + +int EC_POINT_add(EC_POINT *r, const EC_POINT *a, const EC_POINT *b, const EC_GROUP *group) { + const BIGNUM *p; + BN_CTX *ctx = NULL; + int ret = 0; + BIGNUM *ax, *ay, *bx, *by, *lnum, *ldeno; + BIGNUM *tmp, *bn_value_3; + if (r == NULL) { + return 0; + } + if (a == b) { + return EC_POINT_double(r, a, group); + } + if (EC_POINT_is_infinity(a)) { + return EC_POINT_copy(r, b); + } + if (EC_POINT_is_infinity(b)) { + return EC_POINT_copy(r, a); + } + p = group->field; + ctx = BN_CTX_new(); + if (ctx == NULL) { + return 0; + } + BN_CTX_start(ctx); + ax = BN_CTX_get(ctx); + ay = BN_CTX_get(ctx); + bx = BN_CTX_get(ctx); + by = BN_CTX_get(ctx); + lnum = BN_CTX_get(ctx); + ldeno = BN_CTX_get(ctx); + tmp = BN_CTX_get(ctx); + bn_value_3 = BN_CTX_get(ctx); + if (bn_value_3 == NULL) { + goto done; + } + if (!BN_copy(ax, a->X) + || !BN_copy(ay, a->Y) + || !BN_copy(bx, b->X) + || !BN_copy(by, b->Y)) { + goto done; + } + if (BN_cmp(ax, bx) == 0) { + if (!BN_mod_add_quick(tmp, ay, by, p)) { + goto done; + } + if (BN_is_zero(tmp) + || !EC_POINT_copy(r, EC_point_infinity())) { + goto done; + } + if (!BN_set_word(bn_value_3, 3)) { + goto done; + } + if (!BN_mod_sqr(lnum, ax, p, ctx) + || !BN_mod_mul(lnum, lnum, bn_value_3, p, ctx) + || !BN_mod_add_quick(lnum, lnum, group->a, p) + || !BN_mod_lshift1_quick(ldeno, ay, p) + || !BN_mod_inverse(ldeno, ldeno, p, ctx) + || !BN_mod_mul(lnum, lnum, ldeno, p, ctx)) { + goto done; + } + } + else { + if (!BN_mod_sub_quick(lnum, by, ay, p)) { + goto done; + } + if (!BN_mod_sub_quick(ldeno, bx, ax, p)) { + goto done; + } + if (!BN_mod_inverse(ldeno, ldeno, p, ctx)) { + goto done; + } + if (!BN_mod_mul(lnum, lnum, ldeno, p, ctx)) { + goto done; + } + } + if (!BN_mod_sqr(tmp, lnum, p, ctx)) { + goto done; + } + if (!BN_mod_sub_quick(tmp, tmp, ax, p)) { + goto done; + } + if (!BN_mod_sub_quick(r->X, tmp, bx, p)) { + goto done; + } + if (!BN_mod_sub_quick(r->Y, ax, r->X, p)) { + goto done; + } + if (!BN_mod_mul(r->Y, lnum, r->Y, p, ctx)) { + goto done; + } + if (!BN_mod_sub_quick(r->Y, r->Y, ay, p)) { + goto done; + } + if (r == NULL) { + r = EC_POINT_new(); + if (r == NULL) { + goto done; + } + } + ret = 1; + done: + BN_CTX_end(ctx); + BN_CTX_free(ctx); + return ret; +} + +int EC_POINT_mul(EC_POINT *r, const EC_POINT *a, const BIGNUM *b, const EC_GROUP *group) { + if (r == NULL) { + return 0; + } + if (BN_is_negative(b)) { + EC_POINT *_a = EC_POINT_new(); + BIGNUM *_b = BN_new(); + if (!EC_POINT_copy(_a, a) + || !BN_copy(_b, b) + || !BN_mod_sub_quick(_a->Y, group->field, _a->Y, group->field)) { + return 0; + } + BN_set_negative(_b, 0); + return EC_POINT_mul(r, _a, _b, group); + } + if (!EC_POINT_copy(r, EC_point_infinity())){ + return 0; + } + int bits = BN_num_bits(b); + for (int i = bits-1; i >= 0; --i) { + if (!EC_POINT_double(r, r, group)) { + return 0; + } + if (BN_is_bit_set(b, i)) { + if (!EC_POINT_add(r, r, a, group)) { + return 0; + } + } + } + return 1; +} + +/* + * Only for curve y^2 = x^3 + ax + b. + * Please check infinity before calling. + */ +int EC_POINT_on_curve_simple(const EC_POINT *point, const EC_GROUP *group) { + if (point == NULL || group == NULL) { + return 0; + } + if (EC_POINT_is_infinity(point)) { + return 1; + } + int ret = 0; + BN_CTX *ctx; + BIGNUM *p, *a, *b, *x, *y, *lhs, *rhs; + EC_POINT *Nq; + ctx = BN_CTX_new(); + if (ctx == NULL) { + return ret; + } + BN_CTX_start(ctx); + p = group->field; + a = group->a; + b = group->b; + x = point->X; + y = point->Y; + lhs = BN_CTX_get(ctx); + rhs = BN_CTX_get(ctx); + Nq = EC_POINT_new(); + if (p == NULL + || a == NULL + || b == NULL + || x == NULL + || y == NULL + || rhs == NULL + || Nq == NULL) { + return ret; + } + if (!BN_mod_mul(lhs, y, y, p, ctx) + || !BN_mod_mul(rhs, x, x, p, ctx) + || !BN_mod_add_quick(rhs, rhs, a, p) + || !BN_mod_mul(rhs, rhs, x, p, ctx) + || !BN_mod_add_quick(rhs, rhs, b, p) + || !EC_POINT_mul(Nq, point, group->order, group)) { + return ret; + } + if (BN_cmp(lhs, rhs) != 0 + || !EC_POINT_is_infinity(Nq)) { + return ret; + } + ret = 1; + BN_CTX_end(ctx); + BN_CTX_free(ctx); + return ret; +} + +typedef struct ec_key_st { + EC_GROUP *group; + EC_POINT *pub_key; + BIGNUM *priv_key; +} EC_KEY; + +int EC_KEY_init(EC_KEY *key) { + key->group = EC_GROUP_new(); + key->pub_key = EC_POINT_new(); + key->priv_key = BN_new(); + if (key->group == NULL + || key->pub_key == NULL + || key->priv_key == NULL) { + free(key->group); + free(key->pub_key); + BN_free(key->priv_key); + return 0; + } + return 1; +} + +EC_KEY *EC_KEY_new() { + EC_KEY *ret = NULL; + ret = malloc(sizeof(*ret)); + if (ret == NULL) { + return NULL; + } + if (!EC_KEY_init(ret)) { + free(ret); + return NULL; + } + return ret; +} + +int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group) { + if (!EC_GROUP_copy(key->group, group)) { + return 0; + } + return 1; +} + +int EC_KEY_set_priv_key(EC_KEY *key, const BIGNUM *priv_key) { + BIGNUM *tmp; + tmp = BN_dup(priv_key); + if (tmp == NULL) { + return 0; + } + BN_clear_free(key->priv_key); + key->priv_key = tmp; + return 1; +} + +int EC_KEY_generate_pub_key(EC_KEY *key) { + return EC_POINT_mul(key->pub_key, key->group->generator, key->priv_key, key->group); +} + +const BIGNUM *EC_KEY_get_priv_key(const EC_KEY *key) { + return key->priv_key; +} + +int EC_KEY_generate_key() {} diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/.keep" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/CMakeLists.txt" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/CMakeLists.txt" new file mode 100644 index 0000000000000000000000000000000000000000..bfc5416eb3a010e07a87c02e635c0d6041237940 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/CMakeLists.txt" @@ -0,0 +1,38 @@ +# Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved. +# secGear is licensed under the Mulan PSL v2. +# You can use this software according to the terms and conditions of the Mulan PSL v2. +# You may obtain a copy of Mulan PSL v2 at: +# http://license.coscl.org.cn/MulanPSL2 +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR +# PURPOSE. +# See the Mulan PSL v2 for more details. + +cmake_minimum_required(VERSION 3.10 FATAL_ERROR) +project(enclave_SM2) + +set(CMAKE_C_STANDARD 99) + +set(CURRENT_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) + +#set edl name +set(EDL_FILE enclave_SM2.edl) +#set(LOCAL_ROOT_PATH "$ENV{CC_SDK}") +#set(SECGEAR_INSTALL_PATH /usr/lib64/) + +if(CC_GP) + set(CODETYPE trustzone) + set(CODEGEN codegen_arm64) + execute_process(COMMAND uuidgen -r OUTPUT_VARIABLE UUID) + string(REPLACE "\n" "" UUID ${UUID}) + add_definitions(-DPATH="/data/${UUID}.sec") +endif() + +if(CC_SGX) + set(CODETYPE sgx) + set(CODEGEN codegen_x86_64) + add_definitions(-DPATH="${CMAKE_CURRENT_BINARY_DIR}/enclave/enclave.signed.so") +endif() + +add_subdirectory(${CURRENT_ROOT_PATH}/enclave) +add_subdirectory(${CURRENT_ROOT_PATH}/host) diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/enclave/CMakeLists.txt" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/enclave/CMakeLists.txt" new file mode 100644 index 0000000000000000000000000000000000000000..7d04cf03463356de870bcee0c37dbd6819e0326c --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/enclave/CMakeLists.txt" @@ -0,0 +1,162 @@ +# Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved. +# secGear is licensed under the Mulan PSL v2. +# You can use this software according to the terms and conditions of the Mulan PSL v2. +# You may obtain a copy of Mulan PSL v2 at: +# http://license.coscl.org.cn/MulanPSL2 +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR +# PURPOSE. +# See the Mulan PSL v2 for more details. + +#set auto code prefix +set(PREFIX enclave_SM2) + +set(CMAKE_C_STANDARD 99) +#set sign key +set(PEM Enclave_private.pem) + +#set sign tool +set(SIGN_TOOL ${LOCAL_ROOT_PATH}/tools/sign_tool/sign_tool.sh) + +#set enclave src code +set(SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/enclave_SM2.c) + +#set log level +set(PRINT_LEVEL 3) +add_definitions(-DPRINT_LEVEL=${PRINT_LEVEL}) + +if(CC_GP) + #set signed output + set(OUTPUT ${UUID}.sec) + #set itrustee device key + set(DEVICEPEM ${CMAKE_CURRENT_SOURCE_DIR}/rsa_public_key_cloud.pem) + #set whilelist. default: /vendor/bin/teec_hello + set(WHITE_LIST_0 /vendor/bin/enclave_SM2) + set(WHITE_LIST_OWNER root) + set(WHITE_LIST_1 /vendor/bin/secgear_enclave_SM2) + set(WHITELIST WHITE_LIST_0 WHITE_LIST_1) + + set(AUTO_FILES ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_t.h ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_t.c ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_args.h) + add_custom_command(OUTPUT ${AUTO_FILES} + DEPENDS ${CURRENT_ROOT_PATH}/${EDL_FILE} + COMMAND ${CODEGEN} --${CODETYPE} --trusted ${CURRENT_ROOT_PATH}/${EDL_FILE} --search-path ${LOCAL_ROOT_PATH}/inc/host_inc/gp) +endif() + +if(CC_SGX) + set(OUTPUT enclave.signed.so) + set(AUTO_FILES ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_t.h ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_t.c) + add_custom_command(OUTPUT ${AUTO_FILES} + DEPENDS ${CURRENT_ROOT_PATH}/${EDL_FILE} + COMMAND ${CODEGEN} --${CODETYPE} --trusted ${CURRENT_ROOT_PATH}/${EDL_FILE} --search-path ${LOCAL_ROOT_PATH}/inc/host_inc/sgx --search-path ${SGXSDK}/include) +endif() + +set(COMMON_C_FLAGS "-W -Wall -fno-short-enums -fno-omit-frame-pointer -fstack-protector \ + -Wstack-protector --param ssp-buffer-size=4 -frecord-gcc-switches -Wextra -nostdinc -nodefaultlibs \ + -fno-peephole -fno-peephole2 -Wno-main -Wno-error=unused-parameter \ + -Wno-error=unused-but-set-variable -Wno-error=format-truncation=") + +set(COMMON_C_LINK_FLAGS "-Wl,-z,now -Wl,-z,relro -Wl,-z,noexecstack -Wl,-nostdlib -nodefaultlibs -nostartfiles") +set(COMMON_CXX_FLAGS "${COMMON_C_FLAGS}") + +if(CC_GP) + configure_file("${CMAKE_CURRENT_SOURCE_DIR}/manifest.txt.in" "${CMAKE_CURRENT_SOURCE_DIR}/manifest.txt") + + set(CMAKE_C_FLAGS "${COMMON_C_FLAGS} -march=armv8-a ") + set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -s -fPIC") + set(CMAKE_SHARED_LINKER_FLAGS "${COMMON_C_LINK_FLAGS} -Wl,-s") + + set(ITRUSTEE_TEEDIR ${iTrusteeSDK}/) + set(ITRUSTEE_LIBC ${iTrusteeSDK}/thirdparty/open_source/musl/libc) + + if(${CMAKE_VERSION} VERSION_LESS "3.13.0") + link_directories(${SECGEAR_INSTALL_PATH}) + endif() + + add_library(${PREFIX} SHARED ${SOURCE_FILES} ${AUTO_FILES}) + + target_include_directories( ${PREFIX} PRIVATE + ${CMAKE_CURRENT_BINARY_DIR} + ${LOCAL_ROOT_PATH}/inc/host_inc + ${LOCAL_ROOT_PATH}/inc/host_inc/gp + ${LOCAL_ROOT_PATH}/inc/enclave_inc + ${LOCAL_ROOT_PATH}/inc/enclave_inc/gp + ${ITRUSTEE_TEEDIR}/include/TA + ${ITRUSTEE_TEEDIR}/include/TA/huawei_ext + ${ITRUSTEE_LIBC}/arch/aarch64 + ${ITRUSTEE_LIBC}/ + ${ITRUSTEE_LIBC}/arch/arm/bits + ${ITRUSTEE_LIBC}/arch/generic + ${ITRUSTEE_LIBC}/arch/arm + ${LOCAL_ROOT_PATH}/inc/enclave_inc/gp/itrustee) + + if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.13.0") + target_link_directories(${PREFIX} PRIVATE ${SECGEAR_INSTALL_PATH}) + endif() + + foreach(WHITE_LIST ${WHITELIST}) + add_definitions(-D${WHITE_LIST}="${${WHITE_LIST}}") + endforeach(WHITE_LIST) + add_definitions(-DWHITE_LIST_OWNER="${WHITE_LIST_OWNER}") + + target_link_libraries(${PREFIX} -lsecgear_tee) + + add_custom_command(TARGET ${PREFIX} + POST_BUILD + COMMAND bash ${SIGN_TOOL} -d sign -x trustzone -i /home/sunny/secGear/debug/lib/lib${PREFIX}.so -c ${CMAKE_CURRENT_SOURCE_DIR}/manifest.txt -m ${CMAKE_CURRENT_SOURCE_DIR}/config_cloud.ini + -o ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT}) + + install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT} + DESTINATION /data + PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE) + +endif() + +if(CC_SGX) + set(SGX_DIR ${SGXSDK}) + set(CMAKE_C_FLAGS "${COMMON_C_FLAGS} -m64 -fvisibility=hidden") + set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -std=c++11 -O2 -nostdinc++") + set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -s") + set(LINK_LIBRARY_PATH ${SGX_DIR}/lib64) + + if(CC_SIM) + set(Trts_Library_Name sgx_trts_sim) + set(Service_Library_Name sgx_tservice_sim) + else() + set(Trts_Library_Name sgx_trts) + set(Service_Library_Name sgx_tservice) + endif() + + set(Crypto_Library_Name sgx_tcrypto) + + set(CMAKE_SHARED_LINKER_FLAGS "${COMMON_C_LINK_FLAGS} -Wl,-z,defs -Wl,-pie -Bstatic -Bsymbolic -eenclave_entry \ + -Wl,--export-dynamic -Wl,--defsym,__ImageBase=0 -Wl,--gc-sections -Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/Enclave.lds") + + if(${CMAKE_VERSION} VERSION_LESS "3.13.0") + link_directories(${LINK_LIBRARY_PATH}) + endif() + + add_library(${PREFIX} SHARED ${SOURCE_FILES} ${AUTO_FILES}) + + target_include_directories(${PREFIX} PRIVATE + ${CMAKE_CURRENT_BINARY_DIR} + ${SGX_DIR}/include/tlibc + ${SGX_DIR}/include/libcxx + ${SGX_DIR}/include + ${LOCAL_ROOT_PATH}/inc/host_inc + ${LOCAL_ROOT_PATH}/inc/host_inc/sgx) + + if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.13.0") + target_link_directories(${PREFIX} PRIVATE + ${LINK_LIBRARY_PATH}) + endif() + + target_link_libraries(${PREFIX} -Wl,--whole-archive ${Trts_Library_Name} -Wl,--no-whole-archive + -Wl,--start-group -lsgx_tstdc -lsgx_tcxx -l${Crypto_Library_Name} -l${Service_Library_Name} -Wl,--end-group) + add_custom_command(TARGET ${PREFIX} + POST_BUILD + COMMAND umask 0177 + COMMAND openssl genrsa -3 -out ${PEM} 3072 + COMMAND bash ${SIGN_TOOL} -d sign -x sgx -i ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/lib${PREFIX}.so -k ${PEM} -o ${OUTPUT} -c ${CMAKE_CURRENT_SOURCE_DIR}/Enclave.config.xml) +endif() + +set_target_properties(${PREFIX} PROPERTIES SKIP_BUILD_RPATH TRUE) diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/enclave/Enclave.config.xml" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/enclave/Enclave.config.xml" new file mode 100644 index 0000000000000000000000000000000000000000..a5e05e0175b43f97645fde0ea33a1fe0562398cf --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/enclave/Enclave.config.xml" @@ -0,0 +1,12 @@ + + 0 + 0 + 0x100000 + 0x80000 + 10 + 1 + + 1 + 0 + 0xFFFFFFFF + diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/enclave/Enclave.lds" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/enclave/Enclave.lds" new file mode 100644 index 0000000000000000000000000000000000000000..ab77e6478ef3c3448356e80b9884a8a533dd32c6 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/enclave/Enclave.lds" @@ -0,0 +1,11 @@ +enclave.so +{ + global: + g_global_data_sim; + g_global_data; + enclave_entry; + g_peak_heap_used; + local: + *; +}; + diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/enclave/enclave_SM2.c" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/enclave/enclave_SM2.c" new file mode 100644 index 0000000000000000000000000000000000000000..6685bb0e8ad8427a437fb90eadc053f41a6285d4 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/enclave/enclave_SM2.c" @@ -0,0 +1,117 @@ +#include "Enclave1_t.h" +#define GBT32918_5_2017_TEST +#define SM2_EXCHANGE_OPTIONAL +#include "sm2.h" +#include "sm2_tests.h" + +/* + * Ref: + * GB/T 32918.5-2017 + */ + +const int SM2_p_bytes = 256 >> 3; +uint8 *sign_ID_A; +uint8 *sign_M; +uint8 sign_dA[SM2_p_bytes]; +uint8 sign_k[SM2_p_bytes]; + +void setSignIDA(uint8 *src, size_t len) { + if (len < MAX_LEN) { + memcpy(sign_ID_A, src, len + 1); + } +} + +void setSignM(uint8 *src, size_t len) { + if (len < MAX_LEN) { + memcpy(sign_M, src, len + 1); + } +} + +void setSignDA(uint8 *src) { + memcpy(sign_dA, src, SM2_p_bytes); +} + +void setSignK(uint8 *src) { + memcpy(sign_k, src, SM2_p_bytes); +} + +void getSignResult(uint8 *buf, size_t len) { + if (len > strlen(sign_buffer)) { + memcpy(buf, sign_buffer, strlen(sign_buffer) + 1); + } +} + +void getSignVerifyResult(uint8 buf) { + memcpy(buf, &sign_verify_ret, 1); +} + +void callsm2signtest() { + sm2signtest(SM2_p_bytes, sign_M, sign_ID_A, SM2_P, SM2_A, SM2_B, SM2_N, SM2_XG, SM2_YG, sign_dA, sign_k); +} + +uint8 *exchange_ID_A; +uint8 *exchange_ID_B; +uint8 exchange_dA[SM2_p_bytes]; +uint8 exchange_dB[SM2_p_bytes]; +uint64 exchange_klen = 128; + +void setExchangeIDA(uint8 *src, size_t len) { + if (len < MAX_LEN) { + memcpy(exchange_ID_A, src, len + 1); + } +} + +void setExchangeIDB(uint8 *src, size_t len) { + if (len < MAX_LEN) { + memcpy(exchange_ID_B, src, len + 1); + } +} + +void setExchangeDA(uint8 *src) { + memcpy(exchange_dA, src, SM2_p_bytes); +} + +void setExchangeDB(uint8 *src) { + memcpy(exchange_dB, src, SM2_p_bytes); +} + +void setExchangeKLen(uint64 *src) { + memcpy(exchange_klen, src, sizeof(uint64)); +} + +void getExchangeResult(uint8 buf) { + memcpy(buf, &exchange_ret, 1); +} + +void callsm2exchangetest() { + sm2exchangetest(SM2_p_bytes, exchange_ID_A, exchange_ID_B, SM2_P, SM2_A, SM2_B, SM2_N, SM2_XG, SM2_YG, exchange_dA, exchange_dB, exchange_klen); +} + +uint8 *crypt_M; +uint8 crypt_dB[SM2_p_bytes]; + +void setCryptM(uint8 *src, size_t len) { + if (len < MAX_LEN) { + memcpy(crypt_M, src, len + 1); + } +} + +void setCryptDB(uint8 *src) { + memcpy(crypt_dB, src, SM2_p_bytes); +} + +void getCryptPW(uint8 *buf, size_t len) { + if (len > strlen(encrypt_pw)) { + memcpy(buf, encrypt_pw, strlen(encrypt_pw) + 1); + } +} + +void getCryptMSG(uint8 *buf, size_t len) { + if (len > strlen(decrypt_msg)) { + memcpy(buf, decrypt_msg, strlen(decrypt_msg) + 1); + } +} + +void callsm2crypttest() { + sm2crypttest(SM2_p_bytes, crypt_M, SM2_P, SM2_A, SM2_B, SM2_N, SM2_XG, SM2_YG, crypt_dB); +} diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/enclave/manifest.txt.in" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/enclave/manifest.txt.in" new file mode 100644 index 0000000000000000000000000000000000000000..7b8ecf5937d99b0c7f87478984017cdc92c44ed1 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/enclave/manifest.txt.in" @@ -0,0 +1,8 @@ +gpd.ta.appID: @UUID@ +gpd.ta.service_name: test0108 +gpd.ta.singleInstance: true +gpd.ta.multiSession: false +gpd.ta.multiCommand: false +gpd.ta.instanceKeepAlive: false +gpd.ta.dataSize: 16384 +gpd.ta.stackSize: 20480 diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/enclave/rsa_public_key_cloud.pem" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/enclave/rsa_public_key_cloud.pem" new file mode 100644 index 0000000000000000000000000000000000000000..a321f63ed2fb0459c03fcb63de8f2f21618d8e56 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/enclave/rsa_public_key_cloud.pem" @@ -0,0 +1,11 @@ +-----BEGIN PUBLIC KEY----- +MIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIBigKCAYEAzAPwbnbgBg7JgXERA9Bx +p7GLI1S3e1zL83RMd2+GXb6kO4yMKUL3NUCE2HhA2BtQYmLyGovx59UUcKnU58is +Xux++kH+A2shmOPjYvEFuX0Kt8tc19b8M9b/iHsY8ZmKykqia2a5U+IrECRFJo5p +DWUnl7jrHVtq78BSR1c7iXG1frrEC0AYCuqKJo/fxfmOKL0Y9mENCB3nAwjn9unD +BsO/OhkqvvB3nkeuMfNKPh4wCqtQPve13eTojbuxjX/3ePijplTI5X2Gr+n6Ximn +fYRlytQmMgMl/db0ARSKNApq9bmwzVNrnGWWZWJksdRvf6iL7t17Gs4L9AApOuC9 +WkzxPvwp5ZUqjsGd4oJGWeC6ZE6BTw2vxE+xMFI9uAKHxq9pBKkcGMa0g4fANNNV ++W+8JZGanxEXKB3y/M7BCyQAPCWOHC/RNjmRA1gczLYCPzC4pWu935UZdF1RR6zY +CD3t+FoOGGET/g4CwWgyhb5qkp65Hs6ayYt/DUAqo+yBAgMBAAE= +-----END PUBLIC KEY----- diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/enclave/sm2.h" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/enclave/sm2.h" new file mode 100644 index 0000000000000000000000000000000000000000..27bf24930a8a2c02117f8c9d84d1cf2d6876f825 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/enclave/sm2.h" @@ -0,0 +1,207 @@ +#ifndef SECGEAR_SM2_H +#define SECGEAR_SM2_H + +/* + * Ref: + * GB/T 32918.1-2016 + * GB/T 32918.2-2016 + * GB/T 32918.3-2016 + * GB/T 32918.4-2016 + * GB/T 32918.5-2017 + */ + +#include +#include "../include/common_utils.h" +#include "../include/ec.h" +#include "../sm3/sm3.h" +#define SM2ERR(msg) goto err; +/* + * export C_INCLUDE_PATH="/PATH/TO/OPENSSL/include"(/opt/homebrew/Cellar/openssl@1.1/1.1.1m/include) + * export CPLUS_INCLUDE_PATH="/PATH/TO/OPENSSL/include" + * export LIBRARY_PATH="/PATH/TO/OPENSSL/lib" + */ + +/* Parameters */ +/* EC: y^2 = x^3 + ax + b */ + +#define SM2_PARAM_CBLOCK 32 +#define SM2_PARAM_LBLOCK (SM2_PARAM_CBLOCK/4) + +const uint8 SM2_P[SM2_PARAM_CBLOCK] = { + 0xFF, 0xFF, 0xFF, 0xFE, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + 0x00, 0x00, 0x00, 0x00, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, +}; +const uint8 SM2_A[SM2_PARAM_CBLOCK] = { + 0xFF, 0xFF, 0xFF, 0xFE, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + 0x00, 0x00, 0x00, 0x00, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFC, +}; +const uint8 SM2_B[SM2_PARAM_CBLOCK] = { + 0x28, 0xE9, 0xFA, 0x9E, + 0x9D, 0x9F, 0x5E, 0x34, + 0x4D, 0x5A, 0x9E, 0x4B, + 0xCF, 0x65, 0x09, 0xA7, + 0xF3, 0x97, 0x89, 0xF5, + 0x15, 0xAB, 0x8F, 0x92, + 0xDD, 0xBC, 0xBD, 0x41, + 0x4D, 0x94, 0x0E, 0x93, +}; +const uint8 SM2_N[SM2_PARAM_CBLOCK] = { + 0xFF, 0xFF, 0xFF, 0xFE, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + 0x72, 0x03, 0xDF, 0x6B, + 0x21, 0xC6, 0x05, 0x2B, + 0x53, 0xBB, 0xF4, 0x09, + 0x39, 0xD5, 0x41, 0x23, +}; +const uint8 SM2_XG[SM2_PARAM_CBLOCK] = { + 0x32, 0xC4, 0xAE, 0x2C, + 0x1F, 0x19, 0x81, 0x19, + 0x5F, 0x99, 0x04, 0x46, + 0x6A, 0x39, 0xC9, 0x94, + 0x8F, 0xE3, 0x0B, 0xBF, + 0xF2, 0x66, 0x0B, 0xE1, + 0x71, 0x5A, 0x45, 0x89, + 0x33, 0x4C, 0x74, 0xC7, +}; +const uint8 SM2_YG[SM2_PARAM_CBLOCK] = { + 0xBC, 0x37, 0x36, 0xA2, + 0xF4, 0xF6, 0x77, 0x9C, + 0x59, 0xBD, 0xCE, 0xE3, + 0x6B, 0x69, 0x21, 0x53, + 0xD0, 0xA9, 0x87, 0x7C, + 0xC6, 0x2A, 0x47, 0x40, + 0x02, 0xDF, 0x32, 0xE5, + 0x21, 0x39, 0xF0, 0xA0, +}; + +typedef struct sm2_sig_st { + BIGNUM *r; + BIGNUM *s; +} SM2_SIG; + +int SM2_SIG_init(SM2_SIG *sig) { + sig->r = BN_new(); + sig->s = BN_new(); + if (sig->r == NULL || sig->s == NULL) { + BN_free(sig->r); + BN_free(sig->s); + return 0; + } + return 1; +} + +SM2_SIG *SM2_SIG_new() { + SM2_SIG *ret; + ret = malloc(sizeof(*ret)); + if (ret == NULL) { + return NULL; + } + if (!SM2_SIG_init(ret)) { + free(ret); + return NULL; + } + return ret; +} + +int sm2_compute_z(uint8 *out, const uint8 *id, const size_t idlen, const EC_KEY *key){ + /* ZA = SM3(ENTLA || IDA || a || b || xG || yG || xA || yA) */ + uint16 entlen = idlen << 3; + uint8 e_bytes, *buf; + int p_bytes; + SM3_CTX c; + sm3_init(&c); + if (idlen > (UINT16_MAX >> 3)) { + SM2ERR("SM2_compute_z(): idlen too large!\n"); + } + e_bytes = entlen >> 8; + sm3_update(&c, &e_bytes, 1); + e_bytes = entlen & 0xFF; + sm3_update(&c, &e_bytes, 1); + if (!sm3_update(&c, id, idlen)) { + goto err; + } + p_bytes = BN_num_bytes(key->group->field); + buf = malloc(p_bytes); + if (!BN_bn2binpad(key->group->a, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !BN_bn2binpad(key->group->b, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !BN_bn2binpad(key->group->generator->X, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !BN_bn2binpad(key->group->generator->Y, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || BN_bn2binpad(key->pub_key->X, buf, p_bytes) < 0 + || !sm3_update(&c, buf, p_bytes) + || BN_bn2binpad(key->pub_key->Y, buf, p_bytes) < 0 + || !sm3_update(&c, buf, p_bytes)) { + goto err; + } + sm3_final(out, &c); + return 1; + err: + return 0; +} + +int sm2_KDF(uint8 *K, const uint8 *Z, const size_t Zlen, const uint64 klen) { + const uint64 v = 256; /* SM3: 256bit digest */ + if (v*0xFFFFFFFFU <= klen) { + return 0; + } + if (klen & 7) { + return 0; + } + uint32 ct = 1, lmt = (klen+v-1)/v, tmp; + uint8 *_ct, *tmp_u8, *digest, *p = K; + _ct = malloc(4UL); + digest = malloc(SM3_DIGEST_LENGTH); + SM3_CTX c; + while (ct < lmt) { + tmp_u8 = _ct; + HOST_l2c(ct, tmp_u8); + if (!sm3_init(&c) + || !sm3_update(&c, Z, Zlen) + || !sm3_update(&c, _ct, 4UL) + || !sm3_final(digest, &c) + || !memcpy(p, digest, SM3_DIGEST_LENGTH)) { + return 0; + } + ++ct; + p += SM3_DIGEST_LENGTH; + } + tmp_u8 = _ct; + HOST_l2c(ct, tmp_u8); + if (!sm3_init(&c) + || !sm3_update(&c, Z, Zlen) + || !sm3_update(&c, _ct, 4UL) + || !sm3_final(digest, &c)) { + return 0; + } + tmp = klen % v; + if (!tmp) { + if (!memcpy(p, digest, SM3_DIGEST_LENGTH)) { + return 0; + } + } + else { + if (!memcpy(p, digest, tmp >> 3)) { + return 0; + } + } + return 1; +} + +#endif \ No newline at end of file diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/enclave/sm2_crypt.h" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/enclave/sm2_crypt.h" new file mode 100644 index 0000000000000000000000000000000000000000..c9dc1362c287d3941b5c5dfe7da9771aa1624765 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/enclave/sm2_crypt.h" @@ -0,0 +1,217 @@ +#include "sm2.h" + +/* + * Ref: + * GB/T 32918.4-2016 + */ + +/* Encrypt */ + +int sm2_encrypt(uint8 *out, const uint8 *M, const uint64 klen, const EC_KEY *key) { + SM3_CTX c; + BIGNUM *n, *k, *h, *x_2, *y_2; + EC_POINT *G, *C1, *S, *PB, *tmp_point; + EC_GROUP *group; + uint8 *_C1, *_C2, *_C3, *t, *buf; + size_t C1len, MlenCBLOCK, buflen, i; + int p_bytes; + group = key->group; + n = group->order; + h = group->cofactor; + G = group->generator; + PB = key->pub_key; + x_2 = BN_new(); + y_2 = BN_new(); + k = BN_new(); + C1 = EC_POINT_new(); + tmp_point = EC_POINT_new(); + S = EC_POINT_new(); + if (group == NULL + || n == NULL + || h == NULL + || k == NULL + || G == NULL + || S == NULL + || PB == NULL) { + return 0; + } + p_bytes = BN_num_bytes(group->field); + MlenCBLOCK = klen >> 3; + C1len = (p_bytes << 1 | 1); + buflen = p_bytes << 1; + _C1 = malloc(C1len); + _C2 = malloc(MlenCBLOCK); + _C3 = malloc(SM3_DIGEST_LENGTH); + buf = malloc(buflen); + t = malloc(MlenCBLOCK); + if (t == NULL) { + goto err; + } + for (;;) { + /* A1: k = rand(1, n-1) */ + do { + if (!BN_rand_range(k, n)) { + goto err; + } + } while(BN_is_zero(k)); + #ifdef GBT32918_4_2016_A2_TEST /* For GB/T 32918.4-2016 A.2 test */ + if (p_bytes * 8 == 192) { + /* FP-192 */ + BN_hex2bn(&k, "384F30353073AEECE7A1654330A96204D37982A3E15B2CB5"); + } + else { + /* FP-256 */ + BN_hex2bn(&k, "4C62EEFD6ECFC2B95B92FD6C3D9575148AFA17425546D49018E5388D49DD7B4F"); + } + #endif + #ifdef GBT32918_5_2017_TEST + BN_hex2bn(&k, "59276E27D506861A16680F3AD9C02DCCEF3CC1FA3CDBE4CE6D54B80DEAC1BC21"); + #endif + /* A2: C1 = [k]G = (x1, y1), C1 -> bytes */ + if (!EC_POINT_mul(C1, G, k, group) + || !EC_POINT_ecp2bin_normal(_C1, C1, p_bytes)) { + goto err; + } + /* A3: S = [h]PB, if s = O, goto err */ + if (!EC_POINT_mul(S, PB, h, group) + || EC_POINT_is_infinity(S)) { + goto err; + } + /* A4: [k]PB = (x2, y2), x2, y2 -> bytes */ + if (!EC_POINT_mul(tmp_point, PB, k, group)) { + goto err; + } + x_2 = tmp_point->X; + y_2 = tmp_point->Y; + /* A5: t = KDF(x2 || y2, klen), if t == 0, goto A1 */ + if (!BN_bn2binpad(x_2, buf, p_bytes) + || !BN_bn2binpad(y_2, buf+p_bytes, p_bytes) + || !sm2_KDF(t, buf, buflen, klen)) { + goto err; + } + for (i = 0; i < MlenCBLOCK; ++i) { + if (t[i]) { + goto A6; + } + } + } + /* A6: C2 = M ^ t */ + A6: + for (i = 0; i < MlenCBLOCK; ++i) { + _C2[i] = M[i] ^ t[i]; + } + /* A7: C3 = SM3(x2 || M || y2) */ + if (!sm3_init(&c) + || !BN_bn2binpad(x_2, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !sm3_update(&c, M, MlenCBLOCK) + || !BN_bn2binpad(y_2, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !sm3_final(_C3, &c)) { + goto err; + } + /* A8: C = C1 || C3 || C2 */ + if (!memcpy(out, _C1, C1len) + || !memcpy(out+C1len, _C3, SM3_DIGEST_LENGTH) + || !memcpy(out+C1len+SM3_DIGEST_LENGTH, _C2, MlenCBLOCK)) { + goto err; + } + return 1; + err: + return 0; +} + +/* Decrypt */ + +int sm2_decrypt(uint8 *_M, const uint8 *C, const uint64 klen, const EC_KEY *key) { + SM3_CTX c; + BIGNUM *dB, *h, *x_2, *y_2; + EC_POINT *C1, *S, *tmp_point; + EC_GROUP *group; + uint8 *_C1, *_C2, *_C3, *t, *buf, *u; + size_t C1len, MlenCBLOCK, buflen, i; + int p_bytes; + group = key->group; + h = group->cofactor; + dB = key->priv_key; + x_2 = BN_new(); + y_2 = BN_new(); + C1 = EC_POINT_new(); + tmp_point = EC_POINT_new(); + S = EC_POINT_new(); + if (group == NULL + || h == NULL + || x_2 == NULL + || y_2 == NULL + || dB == NULL + || S == NULL) { + return 0; + } + p_bytes = BN_num_bytes(group->field); + MlenCBLOCK = klen >> 3; + C1len = (p_bytes << 1 | 1); + buflen = p_bytes << 1; + _C1 = malloc(C1len); + _C2 = malloc(MlenCBLOCK); + _C3 = malloc(SM3_DIGEST_LENGTH); + u = malloc(SM3_DIGEST_LENGTH); + buf = malloc(buflen); + t = malloc(MlenCBLOCK); + if (t == NULL) { + goto err; + } + /* C = C1 || C3 || C2 */ + if (!memcpy(_C1, C, C1len) + || !memcpy(_C2, C+C1len+SM3_DIGEST_LENGTH, MlenCBLOCK) + || !memcpy(_C3, C+C1len, SM3_DIGEST_LENGTH)) { + goto err; + } + /* B1: C -> C1, check if C1 on the curve */ + if (!EC_POINT_bin2ecp(C1, _C1, p_bytes) + || !EC_POINT_on_curve_simple(C1, group)) { + goto err; + } + /* B2: S = [h]C1, if S == O, goto err */ + if (!EC_POINT_mul(S, C1, h, group) + || EC_POINT_is_infinity(S)) { + goto err; + } + /* B3: [dB]C1 = (x2, y2), x2, y2 -> bytes */ + if (!EC_POINT_mul(tmp_point, C1, dB, group)) { + goto err; + } + x_2 = tmp_point->X; + y_2 = tmp_point->Y; + /* B4: t = KDF(x2 || y2, klen), if t = 0, goto err */ + if (!BN_bn2binpad(x_2, buf, p_bytes) + || !BN_bn2binpad(y_2, buf+p_bytes, p_bytes) + || !sm2_KDF(t, buf, buflen, klen)) { + goto err; + } + for (i = 0; i < MlenCBLOCK; ++i) { + if (t[i]) { + goto B5; + } + } + goto err; + B5: + /* B5: C -> C2, M' = C2 ^ t*/ + for (i = 0; i < MlenCBLOCK; ++i) { + _M[i] = _C2[i] ^ t[i]; + } + /* B6: u = SM3(x2 || M' || y2), check if u = C3 */ + if (!sm3_init(&c) + || !BN_bn2binpad(x_2, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !sm3_update(&c, _M, MlenCBLOCK) + || !BN_bn2binpad(y_2, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !sm3_final(u, &c) + || memcmp(u, _C3, SM3_DIGEST_LENGTH) != 0) { + goto err; + } + /* B7: output M' */ + return 1; + err: + return 0; +} diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/enclave/sm2_exchange.h" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/enclave/sm2_exchange.h" new file mode 100644 index 0000000000000000000000000000000000000000..bde0756fee923ad9768b49c27b533bc739158497 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/enclave/sm2_exchange.h" @@ -0,0 +1,349 @@ +#include "sm2.h" + +/* + * Ref: + * GB/T 32918.3-2016 + */ + +/* + * This function should **NOT** be used directly. + * The exchange process should obey the definition in GB/T 32918.3-2016 + */ +int sm2_exchange(const EC_KEY *userA, const EC_KEY *userB, const uint64 klen, const uint8 *ZA, const uint8 *ZB) { + int w = (BN_num_bits(userA->group->order) + 1) / 2 - 1; /* For n is not pow of 2 */ + size_t p_bytes = BN_num_bytes(userB->group->field), k_bytes = (klen >> 3) + ((klen & 7) ? 1 : 0), Zlen; + uint8 const_byte; + SM3_CTX c; + BN_CTX *ctx; + ctx = BN_CTX_new(); + if (ctx == NULL) { + return 0; + } + BN_CTX_start(ctx); + /* A-pre: */ + BIGNUM *rA, *x_1, *y_1, *tmpa, *tA; + const BIGNUM *nA, *dA, *hA; + EC_POINT *RA, *U, *tmp_pointA; + const EC_POINT *GA, *PA; + EC_GROUP *groupA; + uint8 *Z, *K, *buf, *tmp_char, *SA, *S1, *SB, *S2, *buf_digest; + SA = malloc(SM3_DIGEST_LENGTH); + S1 = malloc(SM3_DIGEST_LENGTH); + SB = malloc(SM3_DIGEST_LENGTH); + S2 = malloc(SM3_DIGEST_LENGTH); + buf_digest = malloc(SM3_DIGEST_LENGTH); + if (buf_digest == NULL) { + return 0; + } + groupA = userA->group; + nA = groupA->order; + dA = userA->priv_key; + hA = groupA->cofactor; + x_1 = BN_CTX_get(ctx); + y_1 = BN_CTX_get(ctx); + tmpa = BN_CTX_get(ctx); + tA = BN_CTX_get(ctx); + rA = BN_CTX_get(ctx); + GA = groupA->generator; + PA = userA->pub_key; + U = EC_POINT_new(); + tmp_pointA = EC_POINT_new(); + RA = EC_POINT_new(); + if (groupA == NULL + || nA == NULL + || dA == NULL + || hA == NULL + || rA == NULL + || GA == NULL + || PA == NULL + || RA == NULL) { + return 0; + } + /* A1: rA = rand(1, n-1) */ + do { + if (!BN_rand_range(rA, nA)) { + return 0; + } + } while(BN_is_zero(rA)); + #ifdef GBT32918_3_2016_A2_TEST + /* For GB/T 32918.3-2016 A.2 test */ + BN_hex2bn(&rA, "83A2C9C8B96E5AF70BD480B472409A9A327257F1EBB73F5B073354B248668563"); + #endif + #ifdef GBT32918_5_2017_TEST + BN_hex2bn(&rA, "D4DE15474DB74D06491C440D305E012400990F3E390C7E87153C12DB2EA60BB3"); + #endif + /* A2: RA = [rA]G = (x1, y1) */ + if (!EC_POINT_mul(RA, GA, rA, groupA)) { + return 0; + } + /* A3: send RA to userB */ + /* B-pre: */ + BIGNUM *rB, *x_2, *y_2, *tmpb, *tB; + const BIGNUM *nB, *dB, *hB; + EC_POINT *RB, *V, *tmp_pointB; + const EC_POINT *GB, *PB; + EC_GROUP *groupB; + groupB = userB->group; + nB = groupB->order; + dB = userB->priv_key; + hB = groupB->cofactor; + x_2 = BN_CTX_get(ctx); + y_2 = BN_CTX_get(ctx); + tmpb = BN_CTX_get(ctx); + tB = BN_CTX_get(ctx); + rB = BN_CTX_get(ctx); + GB = groupB->generator; + PB = userB->pub_key; + V = EC_POINT_new(); + tmp_pointB = EC_POINT_new(); + RB = EC_POINT_new(); + if (groupB == NULL + || nB == NULL + || dB == NULL + || hB == NULL + || rB == NULL + || GB == NULL + || PB == NULL + || RB == NULL) { + return 0; + } + /* B1: rB = rand(1, n-1) */ + do { + if (!BN_rand_range(rB, nB)) { + return 0; + } + } while(BN_is_zero(rB)); + #ifdef GBT32918_3_2016_A2_TEST + /* For GB/T 32918.3-2016 A.2 test */ + BN_hex2bn(&rB, "33FE21940342161C55619C4A0C060293D543C80AF19748CE176D83477DE71C80"); + #endif + #ifdef GBT32918_5_2017_TEST + BN_hex2bn(&rB, "7E07124814B309489125EAED101113164EBF0F3458C5BD88335C1F9D596243D6"); + #endif + /* B2: RB = [rB]G = (x2, y2) */ + if (!EC_POINT_mul(RB, GB, rB, groupB)) { + return 0; + } + /* B3: \bar{x_2} = 2^w + (x_2 & (2^w - 1)) */ + if (!BN_one(tmpb) + || !BN_lshift(tmpb, tmpb, w) + || !BN_mod(x_2, RB->X, tmpb, ctx) + || !BN_add(x_2, x_2, tmpb)) { + return 0; + } + /* B4: tB = (dB + \bar{x_2} \times rB) % n */ + if (!BN_mod_mul(tB, x_2, rB, nB, ctx) + || !BN_mod_add_quick(tB, tB, dB, nB)) { + return 0; + } + /* B5: verify RA is on curve, \bar{x_1} = 2^w + (x_1 & (2^w - 1)) */ + if (!EC_POINT_on_curve_simple(RA, groupB)) { + return 0; + } + if (!BN_one(tmpb) + || !BN_lshift(tmpb, tmpb, w) + || !BN_mod(x_1, RA->X, tmpb, ctx) + || !BN_add(x_1, x_1, tmpb)) { + return 0; + } + /* B6: V = [h \times tB](PA + [\bar{x1}]RA) = (xV, yV) */ + if (!EC_POINT_mul(tmp_pointB, RA, x_1, groupB) + || !EC_POINT_add(tmp_pointB, tmp_pointB, PA, groupB) + || !BN_mul(tmpb, hB, tB, ctx) + || !EC_POINT_mul(V, tmp_pointB, tmpb, groupB) + || EC_POINT_is_infinity(V)) { + return 0; + } + /* B7: KB = KDF(xV || yV || ZA || ZB, klen) */ + K = malloc((klen + 7) >> 3); + Zlen = (p_bytes + SM3_DIGEST_LENGTH) * 2; + Z = malloc(Zlen); + buf = malloc(p_bytes); + tmp_char = Z; + if (!BN_bn2binpad(V->X, buf, p_bytes) + || !memcpy(tmp_char, buf, p_bytes) + || !(tmp_char += p_bytes) + || !BN_bn2binpad(V->Y, buf, p_bytes) + || !memcpy(tmp_char, buf, p_bytes) + || !(tmp_char += p_bytes) + || !memcpy(tmp_char, ZA, SM3_DIGEST_LENGTH) + || !(tmp_char += p_bytes) + || !memcpy(tmp_char, ZB, SM3_DIGEST_LENGTH)) { + return 0; + } + if (!sm2_KDF(K, Z, Zlen, klen)) { + return 0; + } + #ifdef SM2_EXCHANGE_OPTIONAL + /* B8: SB = SM3(0x02 || yV || SM3(xV || ZA || ZB || x1 || y1 || x2 || y2)) (optional) */ + if (!sm3_init(&c) + || !BN_bn2binpad(V->X, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !sm3_update(&c, ZA, SM3_DIGEST_LENGTH) + || !sm3_update(&c, ZB, SM3_DIGEST_LENGTH) + || !BN_bn2binpad(RA->X, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !BN_bn2binpad(RA->Y, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !BN_bn2binpad(RB->X, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !BN_bn2binpad(RB->Y, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !sm3_final(buf_digest, &c)) { + return 0; + } + const_byte = 2; + if (!sm3_init(&c) + || !(sm3_update(&c, &const_byte, 1), 1) + || !BN_bn2binpad(V->Y, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !sm3_update(&c, buf_digest, SM3_DIGEST_LENGTH) + || !sm3_final(SB, &c)) { + return 0; + } + #endif + /* B9: Send RB, (and SB, optionals) to userA */ + /* A4: \bar{x_1} = 2^w + (x_1 & (2^w - 1)) */ + if (!BN_one(tmpa) + || !BN_lshift(tmpa, tmpa, w) + || !BN_mod(x_1, RA->X, tmpa, ctx) + || !BN_add(x_1, x_1, tmpa)) { + return 0; + } + /* A5: tA = (dA + \bar{x_1} \times rA) % n */ + if (!BN_mod_mul(tA, x_1, rA, nA, ctx) + || !BN_mod_add_quick(tA, tA, dA, nA)) { + return 0; + } + /* A6: verify RB is on curve, \bar{x_2} = 2^w + (x_2 & (2^w - 1)) */ + if (!EC_POINT_on_curve_simple(RB, groupA)) { + return 0; + } + if (!BN_one(tmpa) + || !BN_lshift(tmpa, tmpa, w) + || !BN_mod(x_2, RB->X, tmpa, ctx) + || !BN_add(x_2, x_2, tmpa)) { + return 0; + } + /* A7: U = [h \times tA](PB + [\bar{x2}]RB) = (xU, yU) */ + if (!EC_POINT_mul(tmp_pointA, RB, x_2, groupA) + || !EC_POINT_add(tmp_pointA, tmp_pointA, PB, groupA) + || !BN_mul(tmpa, hA, tA, ctx) + || !EC_POINT_mul(U, tmp_pointA, tmpa, groupA) + || EC_POINT_is_infinity(U)) { + return 0; + } + /* A8: KA = KDF(xU || yU || ZA || ZB, klen) */ + K = malloc((klen + 7) >> 3); + Zlen = (p_bytes + SM3_DIGEST_LENGTH) * 2; + Z = malloc(Zlen); + buf = malloc(p_bytes); + tmp_char = Z; + if (!BN_bn2binpad(U->X, buf, p_bytes) + || !memcpy(tmp_char, buf, p_bytes) + || !(tmp_char += p_bytes) + || !BN_bn2binpad(U->Y, buf, p_bytes) + || !memcpy(tmp_char, buf, p_bytes) + || !(tmp_char += p_bytes) + || !memcpy(tmp_char, ZA, SM3_DIGEST_LENGTH) + || !(tmp_char += p_bytes) + || !memcpy(tmp_char, ZB, SM3_DIGEST_LENGTH)) { + return 0; + } + if (!sm2_KDF(K, Z, Zlen, klen)) { + return 0; + } + #ifdef SM2_EXCHANGE_OPTIONAL + /* A9: S1 = SM3(0x02 || yU || SM3(xU || ZA || ZB || x1 || y1 || x2 || y2)), verify S1==SB (optional) */ + if (!sm3_init(&c) + || !BN_bn2binpad(U->X, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !sm3_update(&c, ZA, SM3_DIGEST_LENGTH) + || !sm3_update(&c, ZB, SM3_DIGEST_LENGTH) + || !BN_bn2binpad(RA->X, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !BN_bn2binpad(RA->Y, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !BN_bn2binpad(RB->X, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !BN_bn2binpad(RB->Y, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !sm3_final(buf_digest, &c)) { + return 0; + } + const_byte = 2; + if (!sm3_init(&c) + || !(sm3_update(&c, &const_byte, 1), 1) + || !BN_bn2binpad(V->Y, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !sm3_update(&c, buf_digest, SM3_DIGEST_LENGTH) + || !sm3_final(S1, &c)) { + return 0; + } + if (memcmp(S1, SB, SM3_DIGEST_LENGTH) != 0) { + return 0; + } + #endif + #ifdef SM2_EXCHANGE_OPTIONAL + /* A10: SA = SM3(0x03 || yU || SM3(xU || ZA || ZB || x1 || y1 || x2 || y2)) (optional) */ + if (!sm3_init(&c) + || !BN_bn2binpad(U->X, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !sm3_update(&c, ZA, SM3_DIGEST_LENGTH) + || !sm3_update(&c, ZB, SM3_DIGEST_LENGTH) + || !BN_bn2binpad(RA->X, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !BN_bn2binpad(RA->Y, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !BN_bn2binpad(RB->X, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !BN_bn2binpad(RB->Y, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !sm3_final(buf_digest, &c)) { + return 0; + } + const_byte = 3; + if (!sm3_init(&c) + || !(sm3_update(&c, &const_byte, 1), 1) + || !BN_bn2binpad(U->Y, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !sm3_update(&c, buf_digest, SM3_DIGEST_LENGTH) + || !sm3_final(SA, &c)) { + return 0; + } + #endif + #ifdef SM2_EXCHANGE_OPTIONAL + /* B10: S2 = SM3(0x03 || yV || SM3(xV || ZA || ZB || x1 || y1 || x2 || y2)), verify S2==SA (optional) */ + if (!sm3_init(&c) + || !BN_bn2binpad(V->X, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !sm3_update(&c, ZA, SM3_DIGEST_LENGTH) + || !sm3_update(&c, ZB, SM3_DIGEST_LENGTH) + || !BN_bn2binpad(RA->X, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !BN_bn2binpad(RA->Y, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !BN_bn2binpad(RB->X, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !BN_bn2binpad(RB->Y, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !sm3_final(buf_digest, &c)) { + return 0; + } + const_byte = 3; + if (!sm3_init(&c) + || !(sm3_update(&c, &const_byte, 1), 1) + || !BN_bn2binpad(V->Y, buf, p_bytes) + || !sm3_update(&c, buf, p_bytes) + || !sm3_update(&c, buf_digest, SM3_DIGEST_LENGTH) + || !sm3_final(S2, &c)) { + return 0; + } + if (memcmp(S2, SA, SM3_DIGEST_LENGTH) != 0) { + return 0; + } + #endif + return 1; + fail: + return 0; +} diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/enclave/sm2_sign.h" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/enclave/sm2_sign.h" new file mode 100644 index 0000000000000000000000000000000000000000..38baf47ef7ce1d6d2b23e15f055074cfab9df447 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/enclave/sm2_sign.h" @@ -0,0 +1,196 @@ +#include "sm2.h" + +/* + * Ref: + * GB/T 32918.2-2016 + */ + +/** SM2 signature generation. + * \param msg massage for signing. + * \param msglen byte length of msg. + * \param id identifier of user A. + * \param idlen byte length of id. + * \param key key of user A. + * \return the sign generated. + */ +SM2_SIG *sm2_sig_gen(const uint8 *msg, const size_t msglen, const uint8 *id, const size_t idlen, const EC_KEY *key) { + SM3_CTX c; + sm3_init(&c); + uint8 *buf; + BN_CTX *ctx; + BIGNUM *e, *n, *k, *x, *r, *s, *rk, *dA, *tmp; + EC_POINT *kG, *G; + SM2_SIG *sig; + ctx = BN_CTX_new(); + if (ctx == NULL) { + return 0; + } + sig = SM2_SIG_new(); + if (sig == NULL) { + return 0; + } + BN_CTX_start(ctx); + e = BN_CTX_get(ctx); + n = key->group->order; + k = BN_CTX_get(ctx); + x = BN_CTX_get(ctx); + r = sig->r; + s = sig->s; + rk = BN_CTX_get(ctx); + dA = key->priv_key; + tmp = BN_CTX_get(ctx); + G = key->group->generator; + kG = EC_POINT_new(); + if (n == NULL + || r == NULL + || s == NULL + || dA == NULL + || tmp == NULL + || G == NULL + || kG == NULL) { + return 0; + } + buf = malloc(SM3_DIGEST_LENGTH); + /* A1: \bar{M} = ZA || M */ + if (!sm2_compute_z(buf, id, idlen, key)) { + goto err; + } + if (!sm3_update(&c, buf, SM3_DIGEST_LENGTH) + || !sm3_update(&c, msg, msglen)) { + SM2ERR("sm2_sign(): compute e error!\n"); + } + /* A2: e = SM3(\bar{M}), e -> bn */ + sm3_final(buf, &c); + e = BN_bin2bn(buf, SM3_DIGEST_LENGTH, NULL); + if (e == NULL) { + SM2ERR("sm2_sign(): e error!\n"); + } + for (;;) { + /* A3: k = rand(1, n-1) */ + if (!BN_rand_range(k, n)) { + SM2ERR("sm2_sign(): gen k error!\n"); + } + #ifdef GBT32918_2_2016_A2_TEST + /* For GB/T 32918.2-2016 A.2 test */ + BN_hex2bn(&k, "6CB28D99385C175C94F94E934817663FC176D925DD72B727260DBAAE1FB2F96F"); + #endif + #ifdef GBT32918_5_2017_TEST + BN_hex2bn(&k, "59276E27D506861A16680F3AD9C02DCCEF3CC1FA3CDBE4CE6D54B80DEAC1BC21"); + #endif + if (BN_is_zero(k)) continue; + /* A4: (x1, y1) = [k]G, x1 -> bn */ + if (!EC_POINT_mul(kG, G, k, key->group)) { + return 0; + } + x = kG->X; + /* A5: r = (e + x1) % n, if r=0 or r+k=n, goto A3 */ + if (!BN_mod_add(r, e, x, n, ctx)) { + SM2ERR("sm2_sign(): internal error!\n"); + } + if (BN_is_zero(r)) continue; + if (!BN_add(rk, r, k)) { + SM2ERR("sm2_sign(): internal error!\n"); + } + if (BN_cmp(rk, n) == 0) continue; + /* A6: s = ((1+dA)^-1 \times (k- r \times dA)) % n, if s=0, goto A3 */ + if (!BN_add(s, dA, BN_value_one()) + || !BN_mod_inverse(s, s, n, ctx) + || !printf("(1+dA)^-1: %s\n", BN_bn2hex(s)) + || !BN_mod_mul(tmp, dA, r, n, ctx) + || !BN_sub(tmp, k, tmp) + || !BN_mod_mul(s, s, tmp, n, ctx)) { + SM2ERR("sm2_sign(): internal error!\n"); + } + if (BN_is_zero(s)) continue; + /* A7: r,s -> byte/u8[], sig = (r, s) */ + break; + } + err: + done: + BN_free(e); + BN_CTX_free(ctx); + return sig; +} + +/* (2) Sign Verify */ + +/** SM2 signature verification. + * \param msg massage for signing. + * \param msglen byte length of msg. + * \param id identifier of user A. + * \param idlen byte length of id. + * \param key key of user A. + * \param sig signature to be verified. + * \return 1 on success and 0 if an error occurred. + */ +int sm2_sig_verify(const uint8 *msg, const size_t msglen, const uint8 *id, const size_t idlen, const EC_KEY *key, const SM2_SIG *sig) { + SM3_CTX c; + sm3_init(&c); + BN_CTX *ctx; + const BIGNUM *r, *s, *n; + BIGNUM *e, *t, *R; + const EC_POINT *G, *PA; + EC_POINT *sG, *tPA, *tmp_point; + uint8 *buf; + r = sig->r; + s = sig->s; + n = key->group->order; + G = key->group->generator; + PA = key->pub_key; + ctx = BN_CTX_new(); + if (ctx == NULL) { + goto err; + } + BN_CTX_start(ctx); + e = BN_CTX_get(ctx); + t = BN_CTX_get(ctx); + R = BN_CTX_get(ctx); + sG = EC_POINT_new(); + tPA = EC_POINT_new(); + tmp_point = EC_POINT_new(); + if (R == NULL || tmp_point == NULL) { + goto err; + } + buf = malloc(SM3_DIGEST_LENGTH); + /* B1: check r' \in [1, n-1], otherwise goto BERR */ + /* B2: check s' \in [1, n-1], otherwise goto BERR */ + if (BN_cmp(r, BN_value_one()) < 0 + || BN_cmp(s, BN_value_one()) < 0 + || BN_cmp(n, r) <= 0 + || BN_cmp(n, s) <= 0) { + goto err; + } + /* B3: \bar{M'} = ZA || M' */ + if (!sm2_compute_z(buf, id, idlen, key)) { + goto err; + } + if (!sm3_update(&c, buf, SM3_DIGEST_LENGTH) + || !sm3_update(&c, msg, msglen)) { + SM2ERR("sm2_sign(): compute e error!\n"); + } + /* B4: e' = SM3(\bar{M'}), e -> bn */ + sm3_final(buf, &c); + e = BN_bin2bn(buf, SM3_DIGEST_LENGTH, NULL); + /* B5: r', s' -> bn; t = (r'+s')%n, if t=0, goto BERR */ + if (!BN_mod_add_quick(t, r, s, n) + || BN_is_zero(t)) { + goto err; + } + /* B6: (x1', y1') = [s']G + [t]PA */ + if (!EC_POINT_mul(sG, G, s, key->group) + || !EC_POINT_mul(tPA, PA, t, key->group) + || !EC_POINT_add(tmp_point, sG, tPA, key->group)) { + goto err; + } + /* B7: x1' -> bn; R = (e' + x1')%n, return R=r' */ + if (!BN_mod_add_quick(R, e, tmp_point->X, n)) { + goto err; + } + if (BN_cmp(R, r) == 0) { + return 1; + } + err: + BN_CTX_end(ctx); + /* BERR: not pass, return 0 */ + return 0; +} diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/enclave/sm2_tests.h" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/enclave/sm2_tests.h" new file mode 100644 index 0000000000000000000000000000000000000000..922559d541067fa26cc70bba7d7c570b73946c39 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/enclave/sm2_tests.h" @@ -0,0 +1,152 @@ +#ifndef SECGEAR_SM2_TESTS_H +#define SECGEAR_SM2_TESTS_H + +#include "sm2_sign.h" +#include "sm2_exchange.h" +#include "sm2_crypt.h" + +uint8 *sign_buffer, sign_verify_ret; + +void sm2signtest(const int p_bytes, const uint8 *_M, const uint8 *_ID_A, const uint8 *_p, const uint8 *_a, const uint8 *_b, const uint8 *_n, const uint8 *_XG, const uint8 *_YG, const uint8 *_dA, const uint8 *_k) { + /* A.3 is not supported */ + int _w; + uint16 entlenC = strlen((char *)_ID_A); + uint16 entlA = entlenC*8; + BIGNUM *p, *a, *b, *n, *xG, *yG, *dA, *k; + EC_POINT *G, *kG; + EC_GROUP *group; + EC_KEY *userA; + group = EC_GROUP_new(); + p = BN_new(); + a = BN_new(); + b = BN_new(); + n = BN_new(); + xG = BN_new(); + yG = BN_new(); + dA = BN_new(); + k = BN_new(); + G = EC_POINT_new(); + kG = EC_POINT_new(); + userA = EC_KEY_new(); + BN_bin2bn(_p, p_bytes, p); + BN_bin2bn(_a, p_bytes, a); + BN_bin2bn(_b, p_bytes, b); + BN_bin2bn(_n, p_bytes, n); + BN_bin2bn(_XG, p_bytes, xG); + BN_bin2bn(_YG, p_bytes, yG); + BN_bin2bn(_dA, p_bytes, dA); + BN_bin2bn(_k, p_bytes, k); + EC_POINT_setXY(G, xG, yG); + _w = EC_GROUP_set_param(group, p, a, b, n, BN_value_one(), G); + _w = EC_KEY_set_group(userA, group); + _w = EC_KEY_set_priv_key(userA, dA); + _w = EC_KEY_generate_pub_key(userA); + _w = EC_POINT_mul(kG, G, k, group); + SM2_SIG *sig; + sig = sm2_sig_gen(_M, strlen((char *)_M), _ID_A, strlen((char *)_ID_A), userA); + sign_buffer = (uint8 *)malloc(p_bytes * 4); + memcpy(sign_buffer, BN_bn2hex(sig->r), p_bytes * 2); + memcpy(sign_buffer + p_bytes * 2, BN_bn2hex(sig->s), p_bytes * 2); + + _w = sm2_sig_verify(_M, strlen((char *)_M), _ID_A, strlen((char *)_ID_A), userA, sig); + sign_verify_ret = _w; +} + +uint8 exchange_ret; + +/* h = 1 by default */ +void sm2exchangetest(const int p_bytes, const uint8 *_ID_A, const uint8 *_ID_B, const uint8 *_p, const uint8 *_a, const uint8 *_b, const uint8 *_n, const uint8 *_XG, const uint8 *_YG, const uint8 *_dA, const uint8 *_dB, const uint64 klen) { + /* A.3 is not supported */ + int _w; + uint16 entlenAC = strlen((char *)_ID_A); + uint16 entlA = entlenAC*8; + uint16 entlenBC = strlen((char *)_ID_B); + uint16 entlB = entlenBC*8; + BIGNUM *p, *a, *b, *h, *n, *xG, *yG, *dA, *dB; + EC_POINT *G, *kG; + EC_GROUP *group; + EC_KEY *userA, *userB; + uint8 *ZA, *ZB; + ZA = malloc(SM3_DIGEST_LENGTH); + ZB = malloc(SM3_DIGEST_LENGTH); + group = EC_GROUP_new(); + p = BN_new(); + a = BN_new(); + b = BN_new(); + h = (BIGNUM *)BN_value_one(); + n = BN_new(); + xG = BN_new(); + yG = BN_new(); + dA = BN_new(); + dB = BN_new(); + G = EC_POINT_new(); + kG = EC_POINT_new(); + userA = EC_KEY_new(); + userB = EC_KEY_new(); + BN_bin2bn(_p, p_bytes, p); + BN_bin2bn(_a, p_bytes, a); + BN_bin2bn(_b, p_bytes, b); + BN_bin2bn(_n, p_bytes, n); + BN_bin2bn(_XG, p_bytes, xG); + BN_bin2bn(_YG, p_bytes, yG); + BN_bin2bn(_dA, p_bytes, dA); + BN_bin2bn(_dB, p_bytes, dB); + EC_POINT_setXY(G, xG, yG); + _w = EC_GROUP_set_param(group, p, a, b, n, h, G); + _w = EC_KEY_set_group(userA, group); + _w = EC_KEY_set_priv_key(userA, dA); + _w = EC_KEY_generate_pub_key(userA); + _w = EC_KEY_set_group(userB, group); + _w = EC_KEY_set_priv_key(userB, dB); + _w = EC_KEY_generate_pub_key(userB); + _w = sm2_compute_z(ZA, _ID_A, strlen((char *)_ID_A), userA); + _w = sm2_compute_z(ZB, _ID_B, strlen((char *)_ID_B), userB); + _w = sm2_exchange(userA, userB, klen, ZA, ZB); + exchange_ret = _w; +} + +uint8 *encrypt_pw, *decrypt_msg; + +void sm2crypttest(int p_bytes, const uint8 *M, const uint8 *_p, const uint8 *_a, const uint8 *_b, const uint8 *_n, const uint8 *_XG, const uint8 *_YG, const uint8 *_dB) { + /* A.3 is not supported */ + int _w; + BIGNUM *p, *a, *b, *n, *xG, *yG, *dB; + EC_POINT *G; + EC_GROUP *group; + EC_KEY *userB; + uint64 klen; + int MlenCBLOCK; + size_t pwlen; + group = EC_GROUP_new(); + p = BN_new(); + a = BN_new(); + b = BN_new(); + n = BN_new(); + xG = BN_new(); + yG = BN_new(); + dB = BN_new(); + G = EC_POINT_new(); + userB = EC_KEY_new(); + BN_bin2bn(_p, p_bytes, p); + BN_bin2bn(_a, p_bytes, a); + BN_bin2bn(_b, p_bytes, b); + BN_bin2bn(_n, p_bytes, n); + BN_bin2bn(_XG, p_bytes, xG); + BN_bin2bn(_YG, p_bytes, yG); + BN_bin2bn(_dB, p_bytes, dB); + EC_POINT_setXY(G, xG, yG); + MlenCBLOCK = strlen((char *)M); + _w = EC_GROUP_set_param(group, p, a, b, n, BN_value_one(), G); + _w = EC_KEY_set_group(userB, group); + _w = EC_KEY_set_priv_key(userB, dB); + _w = EC_KEY_generate_pub_key(userB); + klen = MlenCBLOCK * 8; + pwlen = (p_bytes << 1 | 1) + MlenCBLOCK + SM3_DIGEST_LENGTH; + encrypt_pw = malloc(pwlen); + _w = sm2_encrypt(encrypt_pw, M, klen, userB); + decrypt_msg = malloc(MlenCBLOCK+1); + memset(decrypt_msg, 0, MlenCBLOCK+1); + _w = sm2_decrypt(decrypt_msg, encrypt_pw, klen, userB); +} + +#endif \ No newline at end of file diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/enclave_SM2.edl" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/enclave_SM2.edl" new file mode 100644 index 0000000000000000000000000000000000000000..87d3e6527245fea773722f28c3dab4106278902e --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/enclave_SM2.edl" @@ -0,0 +1,30 @@ +enclave { + include "secgear_urts.h" + from "secgear_tstdc.edl" import *; + + trusted { + public void callsm2signtest(); + public void callsm2exchangetest(); + public void callsm2crypttest(); + public void setSignIDA([in, size=len] uint8 *src, size_t len); + public void setSignM([in, size=len] uint8 *src, size_t len); + public void setSignDA([in, count=32] uint8 *src); + public void setSignK([in, count=32] uint8 *src); + public void getSignResult([out, size=len] uint8 *buf, size_t len); + public void getSignVerifyResult([out, count=1] uint8 buf); + public void setExchangeIDA([in, size=len] uint8 *src, size_t len); + public void setExchangeIDB([in, size=len] uint8 *src, size_t len); + public void setExchangeDA([in, count=32] uint8 *src); + public void setExchangeDB([in, count=32] uint8 *src); + public void setExchangeKLen([in, count=1] uint64 *src); + public void getExchangeResult([out, count=1] uint8 buf); + public void setCryptM([in, size=len] uint8 *src, size_t len); + public void setCryptDB([in, count=32] uint8 *src); + public void getCryptPW([out, size=len] uint8 *buf, size_t len); + public void getCryptMSG([out, size=len] uint8 *buf, size_t len); + }; + + untrusted { + + }; +}; diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/host/CMakeLists.txt" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/host/CMakeLists.txt" new file mode 100644 index 0000000000000000000000000000000000000000..0d42633f0242abb9b09efd3c582d366f98bba224 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/host/CMakeLists.txt" @@ -0,0 +1,88 @@ +# Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved. +# secGear is licensed under the Mulan PSL v2. +# You can use this software according to the terms and conditions of the Mulan PSL v2. +# You may obtain a copy of Mulan PSL v2 at: +# http://license.coscl.org.cn/MulanPSL2 +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR +# PURPOSE. +# See the Mulan PSL v2 for more details. + +#set auto code prefix +set(PREFIX enclave_SM2) +#set host exec name +set(OUTPUT secgear_enclave_SM2) +#set host src code +set(SOURCE_FILE ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp) + +set(CMAKE_CXX_STANDARD 11) + +#set auto code +if(CC_GP) + set(AUTO_FILES ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_u.h ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_u.c ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_args.h) + add_custom_command(OUTPUT ${AUTO_FILES} + DEPENDS ${CURRENT_ROOT_PATH}/${EDL_FILE} + COMMAND ${CODEGEN} --${CODETYPE} --untrusted ${CURRENT_ROOT_PATH}/${EDL_FILE} --search-path ${LOCAL_ROOT_PATH}/inc/host_inc/gp) +endif() + +if(CC_SGX) + set(AUTO_FILES ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_u.h ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_u.c) + add_custom_command(OUTPUT ${AUTO_FILES} + DEPENDS ${CURRENT_ROOT_PATH}/${EDL_FILE} + COMMAND ${CODEGEN} --${CODETYPE} --untrusted ${CURRENT_ROOT_PATH}/${EDL_FILE} --search-path ${LOCAL_ROOT_PATH}/inc/host_inc/sgx --search-path ${SGXSDK}/include) +endif() + +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIE") +set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -O2 -fPIE") +set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -s") + +if(CC_GP) + if(${CMAKE_VERSION} VERSION_LESS "3.13.0") + link_directories(${SECGEAR_INSTALL_PATH}) + endif() + add_executable(${OUTPUT} ${SOURCE_FILE} ${AUTO_FILES}) + target_include_directories(${OUTPUT} PRIVATE + ${LOCAL_ROOT_PATH}/inc/host_inc + ${LOCAL_ROOT_PATH}/inc/host_inc/gp + ${CMAKE_CURRENT_BINARY_DIR}) + if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.13.0") + target_link_directories(${OUTPUT} PRIVATE ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}) + endif() +endif() + +if(CC_SGX) + if(${CMAKE_VERSION} VERSION_LESS "3.13.0") + link_directories(${CMAKE_LIBRARY_OUTPUT_DIRECTORY}) + endif() + add_executable(${OUTPUT} ${SOURCE_FILE} ${AUTO_FILES}) + target_include_directories(${OUTPUT} PRIVATE + ${LOCAL_ROOT_PATH}/inc/host_inc + ${LOCAL_ROOT_PATH}/inc/host_inc/sgx + ${CMAKE_CURRENT_BINARY_DIR}) + if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.13.0") + target_link_directories(${OUTPUT} PRIVATE ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}) + endif() +endif() + +if(CC_SIM) + target_link_libraries(${OUTPUT} secgearsim) +else() + target_link_libraries(${OUTPUT} secgear) +endif() +set_target_properties(${OUTPUT} PROPERTIES SKIP_BUILD_RPATH TRUE) + +if(CC_GP) + #itrustee install whitelist /vender/bin/teec_hello + install(TARGETS ${OUTPUT} + RUNTIME + DESTINATION /vendor/bin/ + PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ) +endif() + +if(CC_SGX) + install(TARGETS ${OUTPUT} + RUNTIME + DESTINATION ${CMAKE_BINARY_DIR}/bin/ + PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ) +endif() + diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/host/main.cpp" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/host/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..ef1710ed7add7aab01e04c59703b5d4077866572 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/host/main.cpp" @@ -0,0 +1,127 @@ +#include"enclave.h" +#include"Enclave1_u.h" +#include +#include + +#define MAX_LEN 1024 + +#define SM3_DIGEST_LENGTH 32 +const int SM2_p_bytes = 256 >> 3; +const uint8 sign_ID_A[] = "1234567812345678"; +const uint8 sign_M[] = "message digest"; +const uint8 sign_dA[] = { + 0x39, 0x45, 0x20, 0x8F, + 0x7B, 0x21, 0x44, 0xB1, + 0x3F, 0x36, 0xE3, 0x8A, + 0xC6, 0xD3, 0x9F, 0x95, + 0x88, 0x93, 0x93, 0x69, + 0x28, 0x60, 0xB5, 0x1A, + 0x42, 0xFB, 0x81, 0xEF, + 0x4D, 0xF7, 0xC5, 0xB8, +}; +const uint8 sign_k[] = { + 0x59, 0x27, 0x6E, 0x27, + 0xD5, 0x06, 0x86, 0x1A, + 0x16, 0x68, 0x0F, 0x3A, + 0xD9, 0xC0, 0x2D, 0xCC, + 0xEF, 0x3C, 0xC1, 0xFA, + 0x3C, 0xDB, 0xE4, 0xCE, + 0x6D, 0x54, 0xB8, 0x0D, + 0xEA, 0xC1, 0xBC, 0x21, +}; + +const uint8 exchange_ID_A[] = "1234567812345678"; +const uint8 exchange_ID_B[] = "1234567812345678"; +const uint8 exchange_dA[] = { + 0x81, 0xEB, 0x26, 0xE9, + 0x41, 0xBB, 0x5A, 0xF1, + 0x6D, 0xF1, 0x16, 0x49, + 0x5F, 0x90, 0x69, 0x52, + 0x72, 0xAE, 0x2C, 0xD6, + 0x3D, 0x6C, 0x4A, 0xE1, + 0x67, 0x84, 0x18, 0xBE, + 0x48, 0x23, 0x00, 0x29, +}; +const uint8 exchange_dB[] = { + 0x78, 0x51, 0x29, 0x91, + 0x7D, 0x45, 0xA9, 0xEA, + 0x54, 0x37, 0xA5, 0x93, + 0x56, 0xB8, 0x23, 0x38, + 0xEA, 0xAD, 0xDA, 0x6C, + 0xEB, 0x19, 0x90, 0x88, + 0xF1, 0x4A, 0xE1, 0x0D, + 0xEF, 0xA2, 0x29, 0xB5, +}; +const uint64 exchange_klen = 128; + +const uint8 crypt_M[] = "encryption standard"; +const uint8 crypt_dB[] = { + 0x39, 0x45, 0x20, 0x8F, + 0x7B, 0x21, 0x44, 0xB1, + 0x3F, 0x36, 0xE3, 0x8A, + 0xC6, 0xD3, 0x9F, 0x95, + 0x88, 0x93, 0x93, 0x69, + 0x28, 0x60, 0xB5, 0x1A, + 0x42, 0xFB, 0x81, 0xEF, + 0x4D, 0xF7, 0xC5, 0xB8, +}; + +int main(int argc, char* argv[]) { + cc_enclave_t *eid; + cc_enclave_result_t ret; + char *path = transferFromString(PATH); + + ret = cc_enclave_create(path, AUTO_ENCLAVE_TYPE, 0, SECGEAR_DEBUG_FLAG, NULL, 0, &eid); + if (ret != CC_SUCCESS) { + printf("ERR: %#x ,failed to create enclave.\n", ret); + return -1; + } + + u_int8_t buffer[MAX_LEN], tmpu8; + memset(buffer, 0, sizeof buffer); + printf("A.2 start: \n"); + setSignIDA(eid, sign_ID_A, strlen(sign_ID_A)); + setSignM(eid, sign_M, strlen(sign_M)); + setSignDA(eid, sign_dA); + setSignK(eid, sign_k); + callsm2signtest(eid); + getSignResult(eid, buffer, MAX_LEN); + printf("sign sig: %s\n", buffer); + getSignVerifyResult(eid, tmpu8); + printf("sign verify: %x\n", tmpu8); + printf("A.2 end: \n\n\n"); + /* PASSED */ + + printf("B.2 start: \n"); + setExchangeIDA(eid, exchange_ID_A, strlen(exchange_ID_A)); + setExchangeIDB(eid, exchange_ID_B, strlen(exchange_ID_B)); + setExchangeDA(eid, exchange_dA); + setExchangeDB(eid, exchange_dB); + setExchangeKLen(eid, exchange_klen); + callsm2exchangetest(eid); + getExchangeResult(eid, tmpu8); + printf("exchange: %x\n", tmpu8); + printf("B.2 end: \n\n\n"); + /* PASSED */ + + printf("C.2 start: \n"); + setCryptM(eid, crypt_M, strlen(crypt_M)); + setCryptDB(eid, crypt_dB); + callsm2crypttest(eid); + int pwlen = (SM2_p_bytes << 1 | 1) + strlen(crypt_M) + SM3_DIGEST_LENGTH; + getCryptPW(eid, buffer, pwlen); + printf("encrypt pw: "); + for (int i=0; i +#define debugPrint (printf("%s: %s: %d\n",__FILE__,__FUNCTION__,__LINE__),fflush(stdout),1) + +typedef __uint8_t uint8; +typedef __uint16_t uint16; +typedef __uint32_t uint32; +typedef __uint64_t uint64; + +#define CONCAT_8x4(x0, x1, x2, x3) ((((uint32)(x0) << 8 | (x1)) << 8 | (x2)) << 8 | (x3)) // X = (x0 || x1 || x2 || x3) +#define CONCAT_16x2(x0, x1) ((uint32)(x0) << 16 | (x1)) // X = (x0 || x1) +#define EXTRACT_8_32(X, i) (((X) >> (((i) ^ 3) << 3)) & 0xFF) // X = (x0 || x1 || x2 || x3) +#define EXTRACT_16_32(X, i) (((X) >> (((i) ^ 1) << 4)) & 0xFFFF) // X = (x0 || x1) +#define EXTRACT_16H_31(X) (((X) >> 15) & 0xFFFF) // Higher 16-bit from 31-bit +#define EXTRACT_16L_31(X) ((X) & 0xFFFF) // Lower 16-bit from 31-bit + +#define ROTATE(a, n) (((a) << (n)) | (((a) & 0xFFFFFFFF) >> ((32 - (n)) & 31))) +#define ROTATE_31(a, n) ((((a) << (n)) & 0x7FFFFFFF) | (((a) & 0x7FFFFFFF) >> (31 - (n)))) + +#define GET_BIT_VALUE_32(x, i) (((x) >> (31 - (i))) & 1) + +const uint32 PREFIX_MASK_32[32] = { + 0x00000000, + 0x80000000, + 0xC0000000, + 0xE0000000, + 0xF0000000, + 0xF8000000, + 0xFC000000, + 0xFE000000, + 0xFF000000, + 0xFF800000, + 0xFFC00000, + 0xFFE00000, + 0xFFF00000, + 0xFFF80000, + 0xFFFC0000, + 0xFFFE0000, + 0xFFFF0000, + 0xFFFF8000, + 0xFFFFC000, + 0xFFFFE000, + 0xFFFFF000, + 0xFFFFF800, + 0xFFFFFC00, + 0xFFFFFE00, + 0xFFFFFF00, + 0xFFFFFF80, + 0xFFFFFFC0, + 0xFFFFFFE0, + 0xFFFFFFF0, + 0xFFFFFFF8, + 0xFFFFFFFC, + 0xFFFFFFFE +}; + +# define HOST_c2l(c,l) (l =(((unsigned long)(*((c)++)))<<24), \ + l|=(((unsigned long)(*((c)++)))<<16), \ + l|=(((unsigned long)(*((c)++)))<< 8), \ + l|=(((unsigned long)(*((c)++))) ) ) +# define HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l)>>24)&0xff), \ + *((c)++)=(unsigned char)(((l)>>16)&0xff), \ + *((c)++)=(unsigned char)(((l)>> 8)&0xff), \ + *((c)++)=(unsigned char)(((l) )&0xff), \ + l) + +#endif \ No newline at end of file diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/include/ec.h" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/include/ec.h" new file mode 100644 index 0000000000000000000000000000000000000000..56adbe751f17342f84c17e3736f8806deb7f40b5 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/include/ec.h" @@ -0,0 +1,545 @@ +#include /* BigNum */ +#include "common_utils.h" + +typedef struct ec_point_st { + BIGNUM *X; + BIGNUM *Y; + int is_infinity; +} EC_POINT; + +int EC_POINT_init(EC_POINT *point) { + point->X = BN_new(); + point->Y = BN_new(); + point->is_infinity = 0; + if (point->X == NULL || point->Y == NULL) { + BN_free(point->X); + BN_free(point->Y); + return 0; + } + return 1; +} + +EC_POINT *EC_POINT_new() { + EC_POINT *ret = NULL; + ret = malloc(sizeof(*ret)); + if (ret == NULL) { + return NULL; + } + if (!EC_POINT_init(ret)) { + free(ret); + return NULL; + } + return ret; +} + +int EC_POINT_setXY(EC_POINT *r, const BIGNUM *x, const BIGNUM *y) { + if (r == NULL) { + return 0; + } + if (!BN_copy(r->X, x) || !(BN_copy(r->Y, y))) { + return 0; + } + return 1; +} + +const EC_POINT *EC_point_infinity() { + static const EC_POINT const_infinity = { NULL, NULL, 1 }; + return &const_infinity; +} + +int EC_POINT_is_infinity(const EC_POINT *point) { + return point->is_infinity; +} + +int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src) { + if (dest == NULL) { + return 0; + } + if (EC_POINT_is_infinity(src)) { + dest->is_infinity = 1; + } + else{ + dest->is_infinity = 0; + if (!BN_copy(dest->X, src->X) || !BN_copy(dest->Y, src->Y)) return 0; + } + return 1; +} + +#define NORMAL 0x1 +#define COMPRESS 0x2 +#define MIX 0x4 + +int EC_POINT_ecp2bin(uint8 *out, const EC_POINT *point, int p_bytes, int flag) { + uint8 PC; + uint8 *p = out, *buf; + int i; + buf = malloc(p_bytes); + if (EC_POINT_is_infinity(point)) { + return 0; + } + if (flag == NORMAL) { + PC = 0x04; + *(p++) = PC; + if (!BN_bn2binpad(point->X, buf, p_bytes)) { + return 0; + } + for (i = 0; i < p_bytes; ++i) { + *(p++) = buf[i]; + } + if (!BN_bn2binpad(point->Y, buf, p_bytes)) { + return 0; + } + for (i = 0; i < p_bytes; ++i) { + *(p++) = buf[i]; + } + } + else { + goto err; /* not supported */ + } + return 1; + err: + return 0; +} + +int EC_POINT_bin2ecp(EC_POINT *point, const uint8 *src, int p_bytes) { + if (point == NULL) { + return 0; + } + uint8 PC = *src; + const uint8 *data = src + 1; + uint8 *buf; + int i; + if (PC == 0x04) { + if (!BN_bin2bn(data, p_bytes, point->X) + || !BN_bin2bn(data+p_bytes, p_bytes, point->Y)) { + return 0; + } + } + else { + goto err; /* not supported */ + } + return 1; + err: + return 0; +} + +int EC_POINT_ecp2bin_normal(uint8 *out, const EC_POINT *point, int p_bytes) { + return EC_POINT_ecp2bin(out, point, p_bytes, NORMAL); +} + +// int EC_POINT_ecp2bin_compressed(uint8 *out, const EC_POINT *point, int p_bytes) { +// return EC_POINT_ecp2bin(out, point, p_bytes, COMPRESS); +// } + +// int EC_POINT_ecp2bin_mix(uint8 *out, const EC_POINT *point, int p_bytes) { +// return EC_POINT_ecp2bin(out, point, p_bytes, MIX); +// } + +typedef struct ec_group_st { + EC_POINT *generator; /* point G */ + BIGNUM *order; /* n */ + BIGNUM *cofactor; /* h */ + BIGNUM *field; /* module p */ + BIGNUM *a, *b; /* ECC parameter */ +} EC_GROUP; + +int EC_GROUP_init(EC_GROUP *group) { + group->generator = EC_POINT_new(); + group->order = BN_new(); + group->cofactor = BN_new(); + group->field = BN_new(); + group->a = BN_new(); + group->b = BN_new(); + if (group->generator == NULL || group->b == NULL) { + free(group->generator); + BN_free(group->order); + BN_free(group->cofactor); + BN_free(group->field); + BN_free(group->a); + BN_free(group->b); + return 0; + } + return 1; +} + +EC_GROUP *EC_GROUP_new() { + EC_GROUP *ret = NULL; + ret = malloc(sizeof(*ret)); + if (ret == NULL) { + return NULL; + } + if (!EC_GROUP_init(ret)) { + free(ret); + return NULL; + } + return ret; +} + +int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src) { + if (dest == NULL) { + return 0; + } + if (dest == src) { + return 1; + } + if (!EC_POINT_copy(dest->generator, src->generator) + || !BN_copy(dest->order, src->order) + || !BN_copy(dest->cofactor, src->cofactor) + || !BN_copy(dest->field, src->field) + || !BN_copy(dest->a, src->a) + || !BN_copy(dest->b, src->b)) { + return 0; + } + return 1; +} + +int EC_GROUP_set_param(EC_GROUP *group, const BIGNUM *field, const BIGNUM *a, const BIGNUM *b, const BIGNUM *order, const BIGNUM *cofactor, const EC_POINT *point) { + if (group == NULL) { + return 0; + } + group->field = BN_new(); + group->a = BN_new(); + group->b = BN_new(); + group->order = BN_new(); + group->cofactor = BN_new(); + group->generator = EC_POINT_new(); + if (group->cofactor == NULL + || group->generator == NULL) { + return 0; + } + if (!BN_copy(group->field, field) + || !BN_copy(group->a, a) + || !BN_copy(group->b, b) + || !BN_copy(group->order, order) + || !BN_copy(group->cofactor, cofactor) + || !EC_POINT_copy(group->generator, point)) { + return 0; + } + return 1; +} + +int EC_POINT_double(EC_POINT *r, const EC_POINT *a, const EC_GROUP *group) { + const BIGNUM *p; + BN_CTX *ctx = NULL; + int ret = 0; + BIGNUM *ax, *ay, *lnum, *ldeno; + BIGNUM *tmp, *bn_value_3; + if (r == NULL) { + return 0; + } + if (EC_POINT_is_infinity(a)) { + return EC_POINT_copy(r, EC_point_infinity()); + } + p = group->field; + ctx = BN_CTX_new(); + if (ctx == NULL) { + return 0; + } + BN_CTX_start(ctx); + ax = BN_CTX_get(ctx); + ay = BN_CTX_get(ctx); + lnum = BN_CTX_get(ctx); + ldeno = BN_CTX_get(ctx); + tmp = BN_CTX_get(ctx); + bn_value_3 = BN_CTX_get(ctx); + if (bn_value_3 == NULL) { + goto done; + } + if (!BN_copy(ax, a->X) + || !BN_copy(ay, a->Y)) { + goto done; + } + if (!BN_set_word(bn_value_3, 3) + || !BN_mod_sqr(lnum, ax, p, ctx) + || !BN_mod_mul(lnum, lnum, bn_value_3, p, ctx) + || !BN_mod_add_quick(lnum, lnum, group->a, p) + || !BN_mod_lshift1_quick(ldeno, ay, p) + || !BN_mod_inverse(ldeno, ldeno, p, ctx) + || !BN_mod_mul(lnum, lnum, ldeno, p, ctx)) { + goto done; + } + if (!BN_mod_sqr(r->X, lnum, p, ctx)) { + goto done; + } + if (!BN_mod_lshift1_quick(tmp, ax, p)) { + goto done; + } + if (!BN_mod_sub_quick(r->X, r->X, tmp, p)) { + goto done; + } + if (!BN_mod_sub_quick(r->Y, ax, r->X, p)) { + goto done; + } + if (!BN_mod_mul(r->Y, lnum, r->Y, p, ctx)) { + goto done; + } + if (!BN_mod_sub_quick(r->Y, r->Y, ay, p)) { + goto done; + } + if (r == NULL) { + r = EC_POINT_new(); + if (r == NULL) { + goto done; + } + } + ret = 1; + done: + BN_CTX_end(ctx); + BN_CTX_free(ctx); + return ret; +} + +int EC_POINT_add(EC_POINT *r, const EC_POINT *a, const EC_POINT *b, const EC_GROUP *group) { + const BIGNUM *p; + BN_CTX *ctx = NULL; + int ret = 0; + BIGNUM *ax, *ay, *bx, *by, *lnum, *ldeno; + BIGNUM *tmp, *bn_value_3; + if (r == NULL) { + return 0; + } + if (a == b) { + return EC_POINT_double(r, a, group); + } + if (EC_POINT_is_infinity(a)) { + return EC_POINT_copy(r, b); + } + if (EC_POINT_is_infinity(b)) { + return EC_POINT_copy(r, a); + } + p = group->field; + ctx = BN_CTX_new(); + if (ctx == NULL) { + return 0; + } + BN_CTX_start(ctx); + ax = BN_CTX_get(ctx); + ay = BN_CTX_get(ctx); + bx = BN_CTX_get(ctx); + by = BN_CTX_get(ctx); + lnum = BN_CTX_get(ctx); + ldeno = BN_CTX_get(ctx); + tmp = BN_CTX_get(ctx); + bn_value_3 = BN_CTX_get(ctx); + if (bn_value_3 == NULL) { + goto done; + } + if (!BN_copy(ax, a->X) + || !BN_copy(ay, a->Y) + || !BN_copy(bx, b->X) + || !BN_copy(by, b->Y)) { + goto done; + } + if (BN_cmp(ax, bx) == 0) { + if (!BN_mod_add_quick(tmp, ay, by, p)) { + goto done; + } + if (BN_is_zero(tmp) + || !EC_POINT_copy(r, EC_point_infinity())) { + goto done; + } + if (!BN_set_word(bn_value_3, 3)) { + goto done; + } + if (!BN_mod_sqr(lnum, ax, p, ctx) + || !BN_mod_mul(lnum, lnum, bn_value_3, p, ctx) + || !BN_mod_add_quick(lnum, lnum, group->a, p) + || !BN_mod_lshift1_quick(ldeno, ay, p) + || !BN_mod_inverse(ldeno, ldeno, p, ctx) + || !BN_mod_mul(lnum, lnum, ldeno, p, ctx)) { + goto done; + } + } + else { + if (!BN_mod_sub_quick(lnum, by, ay, p)) { + goto done; + } + if (!BN_mod_sub_quick(ldeno, bx, ax, p)) { + goto done; + } + if (!BN_mod_inverse(ldeno, ldeno, p, ctx)) { + goto done; + } + if (!BN_mod_mul(lnum, lnum, ldeno, p, ctx)) { + goto done; + } + } + if (!BN_mod_sqr(tmp, lnum, p, ctx)) { + goto done; + } + if (!BN_mod_sub_quick(tmp, tmp, ax, p)) { + goto done; + } + if (!BN_mod_sub_quick(r->X, tmp, bx, p)) { + goto done; + } + if (!BN_mod_sub_quick(r->Y, ax, r->X, p)) { + goto done; + } + if (!BN_mod_mul(r->Y, lnum, r->Y, p, ctx)) { + goto done; + } + if (!BN_mod_sub_quick(r->Y, r->Y, ay, p)) { + goto done; + } + if (r == NULL) { + r = EC_POINT_new(); + if (r == NULL) { + goto done; + } + } + ret = 1; + done: + BN_CTX_end(ctx); + BN_CTX_free(ctx); + return ret; +} + +int EC_POINT_mul(EC_POINT *r, const EC_POINT *a, const BIGNUM *b, const EC_GROUP *group) { + if (r == NULL) { + return 0; + } + if (BN_is_negative(b)) { + EC_POINT *_a = EC_POINT_new(); + BIGNUM *_b = BN_new(); + if (!EC_POINT_copy(_a, a) + || !BN_copy(_b, b) + || !BN_mod_sub_quick(_a->Y, group->field, _a->Y, group->field)) { + return 0; + } + BN_set_negative(_b, 0); + return EC_POINT_mul(r, _a, _b, group); + } + if (!EC_POINT_copy(r, EC_point_infinity())){ + return 0; + } + int bits = BN_num_bits(b); + for (int i = bits-1; i >= 0; --i) { + if (!EC_POINT_double(r, r, group)) { + return 0; + } + if (BN_is_bit_set(b, i)) { + if (!EC_POINT_add(r, r, a, group)) { + return 0; + } + } + } + return 1; +} + +/* + * Only for curve y^2 = x^3 + ax + b. + * Please check infinity before calling. + */ +int EC_POINT_on_curve_simple(const EC_POINT *point, const EC_GROUP *group) { + if (point == NULL || group == NULL) { + return 0; + } + if (EC_POINT_is_infinity(point)) { + return 1; + } + int ret = 0; + BN_CTX *ctx; + BIGNUM *p, *a, *b, *x, *y, *lhs, *rhs; + EC_POINT *Nq; + ctx = BN_CTX_new(); + if (ctx == NULL) { + return ret; + } + BN_CTX_start(ctx); + p = group->field; + a = group->a; + b = group->b; + x = point->X; + y = point->Y; + lhs = BN_CTX_get(ctx); + rhs = BN_CTX_get(ctx); + Nq = EC_POINT_new(); + if (p == NULL + || a == NULL + || b == NULL + || x == NULL + || y == NULL + || rhs == NULL + || Nq == NULL) { + return ret; + } + if (!BN_mod_mul(lhs, y, y, p, ctx) + || !BN_mod_mul(rhs, x, x, p, ctx) + || !BN_mod_add_quick(rhs, rhs, a, p) + || !BN_mod_mul(rhs, rhs, x, p, ctx) + || !BN_mod_add_quick(rhs, rhs, b, p) + || !EC_POINT_mul(Nq, point, group->order, group)) { + return ret; + } + if (BN_cmp(lhs, rhs) != 0 + || !EC_POINT_is_infinity(Nq)) { + return ret; + } + ret = 1; + BN_CTX_end(ctx); + BN_CTX_free(ctx); + return ret; +} + +typedef struct ec_key_st { + EC_GROUP *group; + EC_POINT *pub_key; + BIGNUM *priv_key; +} EC_KEY; + +int EC_KEY_init(EC_KEY *key) { + key->group = EC_GROUP_new(); + key->pub_key = EC_POINT_new(); + key->priv_key = BN_new(); + if (key->group == NULL + || key->pub_key == NULL + || key->priv_key == NULL) { + free(key->group); + free(key->pub_key); + BN_free(key->priv_key); + return 0; + } + return 1; +} + +EC_KEY *EC_KEY_new() { + EC_KEY *ret = NULL; + ret = malloc(sizeof(*ret)); + if (ret == NULL) { + return NULL; + } + if (!EC_KEY_init(ret)) { + free(ret); + return NULL; + } + return ret; +} + +int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group) { + if (!EC_GROUP_copy(key->group, group)) { + return 0; + } + return 1; +} + +int EC_KEY_set_priv_key(EC_KEY *key, const BIGNUM *priv_key) { + BIGNUM *tmp; + tmp = BN_dup(priv_key); + if (tmp == NULL) { + return 0; + } + BN_clear_free(key->priv_key); + key->priv_key = tmp; + return 1; +} + +int EC_KEY_generate_pub_key(EC_KEY *key) { + return EC_POINT_mul(key->pub_key, key->group->generator, key->priv_key, key->group); +} + +const BIGNUM *EC_KEY_get_priv_key(const EC_KEY *key) { + return key->priv_key; +} + +int EC_KEY_generate_key() {} diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/sm3/sm3.h" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/sm3/sm3.h" new file mode 100644 index 0000000000000000000000000000000000000000..195d6790d2f9b57a6a27ab84b5cbf88d4aad37a3 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM2/sm3/sm3.h" @@ -0,0 +1,183 @@ +#ifndef SECGEAR_SM3_H +#define SECGEAR_SM3_H + +/* + * Ref: + * GB/T 32905-2016 + */ + +#include +#include "../include/common_utils.h" + +#define SM3_DIGEST_LENGTH 32 +#define SM3_WORD uint32 + +#define SM3_CBLOCK 64 +#define SM3_LBLOCK (SM3_CBLOCK/4) + +typedef struct SM3state_st { + SM3_WORD A, B, C, D, E, F, G, H; + SM3_WORD Nl, Nh; + SM3_WORD data[SM3_LBLOCK]; + unsigned int num; +} SM3_CTX; + +#define P0(X) (X ^ ROTATE(X, 9) ^ ROTATE(X, 17)) +#define P1(X) (X ^ ROTATE(X, 15) ^ ROTATE(X, 23)) + +#define FF(J,X,Y,Z) (J<16 ? (X ^ Y ^ Z) : ((X & Y) | (X & Z) | (Y & Z))) +#define GG(J,X,Y,Z) (J<16 ? (X ^ Y ^ Z) : ((X & Y) | ((~X) & Z))) + +#define T(J) (J<16 ? 0x79cc4519 : 0x7a879d8a) + +#define EXPAND(W0,W7,W13,W3,W10) \ + (P1(W0 ^ W7 ^ ROTATE(W13, 15)) ^ ROTATE(W3, 7) ^ W10) + +#define SM3_A 0x7380166fUL +#define SM3_B 0x4914b2b9UL +#define SM3_C 0x172442d7UL +#define SM3_D 0xda8a0600UL +#define SM3_E 0xa96f30bcUL +#define SM3_F 0x163138aaUL +#define SM3_G 0xe38dee4dUL +#define SM3_H 0xb0fb0e4eUL + +#define SM3_MAKE_STRING(c, s) \ + do { \ + unsigned long ll; \ + ll=(c)->A; (void)HOST_l2c(ll, (s)); \ + ll=(c)->B; (void)HOST_l2c(ll, (s)); \ + ll=(c)->C; (void)HOST_l2c(ll, (s)); \ + ll=(c)->D; (void)HOST_l2c(ll, (s)); \ + ll=(c)->E; (void)HOST_l2c(ll, (s)); \ + ll=(c)->F; (void)HOST_l2c(ll, (s)); \ + ll=(c)->G; (void)HOST_l2c(ll, (s)); \ + ll=(c)->H; (void)HOST_l2c(ll, (s)); \ + } while (0) + +void sm3_block_data_order(SM3_CTX *ctx, const void *p, size_t num) { + const unsigned char *data = p; + SM3_WORD A, B, C, D, E, F, G, H; + SM3_WORD w[68]={}, ww[64]={}; + while (num--) { + A = ctx->A; + B = ctx->B; + C = ctx->C; + D = ctx->D; + E = ctx->E; + F = ctx->F; + G = ctx->G; + H = ctx->H; + for (int j = 0; j < 16; ++j) { + HOST_c2l(data, w[j]); + } + for (int j = 16; j < 68; ++j) { + w[j] = EXPAND(w[j-16], w[j-9], w[j-3], w[j-13], w[j-6]); + } + for (int j = 0; j < 64; ++j) { + ww[j] = w[j] ^ w[j+4]; + } + for (int j = 0; j < 64; j++) { + SM3_WORD SS1, SS2, TT1, TT2; + SS1 = ROTATE(ROTATE(A, 12) + E + ROTATE(T(j), j&31), 7); + SS2 = SS1 ^ ROTATE(A, 12); + TT1 = FF(j, A, B, C) + D + SS2 + ww[j]; + TT2 = GG(j, E, F, G) + H + SS1 + w[j]; + D = C; + C = ROTATE(B,9); + B = A; + A = TT1; + H = G; + G = ROTATE(F, 19); + F = E; + E = P0(TT2); + } + ctx->A ^= A; + ctx->B ^= B; + ctx->C ^= C; + ctx->D ^= D; + ctx->E ^= E; + ctx->F ^= F; + ctx->G ^= G; + ctx->H ^= H; + } +} + +int sm3_init(SM3_CTX *c) { + memset(c, 0, sizeof(*c)); + c->A = SM3_A; + c->B = SM3_B; + c->C = SM3_C; + c->D = SM3_D; + c->E = SM3_E; + c->F = SM3_F; + c->G = SM3_G; + c->H = SM3_H; + return 1; +} + +int sm3_update(SM3_CTX *c, const void *data_, size_t len) { + const unsigned char *data = data_; + unsigned char *p; + SM3_WORD l; + size_t n; + if (len == 0) return 1; + l = (c->Nl + (((SM3_WORD) len) << 3)) & 0xffffffffUL; + if (l < c->Nl) c->Nh++; + c->Nh += (SM3_WORD) (len >> 29); + c->Nl = l; + n = c->num; + if (n != 0) { + p = (unsigned char *)c->data; + if (len >= SM3_CBLOCK || len + n >= SM3_CBLOCK) { + memcpy(p + n, data, SM3_CBLOCK - n); + sm3_block_data_order(c, p, 1); + n = SM3_CBLOCK - n; + data += n; + len -= n; + c->num = 0; + memset(p, 0, SM3_CBLOCK); + } + else { + memcpy(p + n, data, len); + c->num += (unsigned int)len; + return 1; + } + } + n = len / SM3_CBLOCK; + if (n > 0) { + sm3_block_data_order(c, data, n); + n *= SM3_CBLOCK; + data += n; + len -= n; + } + if (len != 0) { + p = (unsigned char *)c->data; + c->num = (unsigned int)len; + memcpy(p, data, len); + } + return 1; +} + +int sm3_final(unsigned char *md, SM3_CTX *c) { + unsigned char *p = (unsigned char *)c->data; + size_t n = c->num; + p[n] = 0x80; + n++; + if (n > (SM3_CBLOCK - 8)) { + memset(p + n, 0, SM3_CBLOCK - n); + n = 0; + sm3_block_data_order(c, p, 1); + } + memset(p + n, 0, SM3_CBLOCK - 8 - n); + p += SM3_CBLOCK - 8; + (void)HOST_l2c(c->Nh, p); + (void)HOST_l2c(c->Nl, p); + p -= SM3_CBLOCK; + sm3_block_data_order(c, p, 1); + c->num = 0; + SM3_MAKE_STRING(c, md); + return 1; +} + +#endif \ No newline at end of file diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM4/CMakeLists.txt" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM4/CMakeLists.txt" new file mode 100644 index 0000000000000000000000000000000000000000..acd2ef7ffa085644d041a298b6c455d91c8369d0 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM4/CMakeLists.txt" @@ -0,0 +1,38 @@ +# Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved. +# secGear is licensed under the Mulan PSL v2. +# You can use this software according to the terms and conditions of the Mulan PSL v2. +# You may obtain a copy of Mulan PSL v2 at: +# http://license.coscl.org.cn/MulanPSL2 +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR +# PURPOSE. +# See the Mulan PSL v2 for more details. + +cmake_minimum_required(VERSION 3.10 FATAL_ERROR) +project(enclave_SM4) + +set(CMAKE_C_STANDARD 99) + +set(CURRENT_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) + +#set edl name +set(EDL_FILE enclave_SM4.edl) +#set(LOCAL_ROOT_PATH "$ENV{CC_SDK}") +#set(SECGEAR_INSTALL_PATH /usr/lib64/) + +if(CC_GP) + set(CODETYPE trustzone) + set(CODEGEN codegen_arm64) + execute_process(COMMAND uuidgen -r OUTPUT_VARIABLE UUID) + string(REPLACE "\n" "" UUID ${UUID}) + add_definitions(-DPATH="/data/${UUID}.sec") +endif() + +if(CC_SGX) + set(CODETYPE sgx) + set(CODEGEN codegen_x86_64) + add_definitions(-DPATH="${CMAKE_CURRENT_BINARY_DIR}/enclave/enclave.signed.so") +endif() + +add_subdirectory(${CURRENT_ROOT_PATH}/enclave) +add_subdirectory(${CURRENT_ROOT_PATH}/host) diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM4/enclave/CMakeLists.txt" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM4/enclave/CMakeLists.txt" new file mode 100644 index 0000000000000000000000000000000000000000..7c4b75dead54c81772c8d26c7484a442eea1315d --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM4/enclave/CMakeLists.txt" @@ -0,0 +1,162 @@ +# Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved. +# secGear is licensed under the Mulan PSL v2. +# You can use this software according to the terms and conditions of the Mulan PSL v2. +# You may obtain a copy of Mulan PSL v2 at: +# http://license.coscl.org.cn/MulanPSL2 +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR +# PURPOSE. +# See the Mulan PSL v2 for more details. + +#set auto code prefix +set(PREFIX enclave_SM4) + +set(CMAKE_C_STANDARD 99) +#set sign key +set(PEM Enclave_private.pem) + +#set sign tool +set(SIGN_TOOL ${LOCAL_ROOT_PATH}/tools/sign_tool/sign_tool.sh) + +#set enclave src code +set(SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/enclave_SM4.c) + +#set log level +set(PRINT_LEVEL 3) +add_definitions(-DPRINT_LEVEL=${PRINT_LEVEL}) + +if(CC_GP) + #set signed output + set(OUTPUT ${UUID}.sec) + #set itrustee device key + set(DEVICEPEM ${CMAKE_CURRENT_SOURCE_DIR}/rsa_public_key_cloud.pem) + #set whilelist. default: /vendor/bin/teec_hello + set(WHITE_LIST_0 /vendor/bin/enclave_SM4) + set(WHITE_LIST_OWNER root) + set(WHITE_LIST_1 /vendor/bin/secgear_enclave_SM4) + set(WHITELIST WHITE_LIST_0 WHITE_LIST_1) + + set(AUTO_FILES ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_t.h ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_t.c ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_args.h) + add_custom_command(OUTPUT ${AUTO_FILES} + DEPENDS ${CURRENT_ROOT_PATH}/${EDL_FILE} + COMMAND ${CODEGEN} --${CODETYPE} --trusted ${CURRENT_ROOT_PATH}/${EDL_FILE} --search-path ${LOCAL_ROOT_PATH}/inc/host_inc/gp) +endif() + +if(CC_SGX) + set(OUTPUT enclave.signed.so) + set(AUTO_FILES ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_t.h ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_t.c) + add_custom_command(OUTPUT ${AUTO_FILES} + DEPENDS ${CURRENT_ROOT_PATH}/${EDL_FILE} + COMMAND ${CODEGEN} --${CODETYPE} --trusted ${CURRENT_ROOT_PATH}/${EDL_FILE} --search-path ${LOCAL_ROOT_PATH}/inc/host_inc/sgx --search-path ${SGXSDK}/include) +endif() + +set(COMMON_C_FLAGS "-W -Wall -fno-short-enums -fno-omit-frame-pointer -fstack-protector \ + -Wstack-protector --param ssp-buffer-size=4 -frecord-gcc-switches -Wextra -nostdinc -nodefaultlibs \ + -fno-peephole -fno-peephole2 -Wno-main -Wno-error=unused-parameter \ + -Wno-error=unused-but-set-variable -Wno-error=format-truncation=") + +set(COMMON_C_LINK_FLAGS "-Wl,-z,now -Wl,-z,relro -Wl,-z,noexecstack -Wl,-nostdlib -nodefaultlibs -nostartfiles") +set(COMMON_CXX_FLAGS "${COMMON_C_FLAGS}") + +if(CC_GP) + configure_file("${CMAKE_CURRENT_SOURCE_DIR}/manifest.txt.in" "${CMAKE_CURRENT_SOURCE_DIR}/manifest.txt") + + set(CMAKE_C_FLAGS "${COMMON_C_FLAGS} -march=armv8-a ") + set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -s -fPIC") + set(CMAKE_SHARED_LINKER_FLAGS "${COMMON_C_LINK_FLAGS} -Wl,-s") + + set(ITRUSTEE_TEEDIR ${iTrusteeSDK}/) + set(ITRUSTEE_LIBC ${iTrusteeSDK}/thirdparty/open_source/musl/libc) + + if(${CMAKE_VERSION} VERSION_LESS "3.13.0") + link_directories(${SECGEAR_INSTALL_PATH}) + endif() + + add_library(${PREFIX} SHARED ${SOURCE_FILES} ${AUTO_FILES}) + + target_include_directories( ${PREFIX} PRIVATE + ${CMAKE_CURRENT_BINARY_DIR} + ${LOCAL_ROOT_PATH}/inc/host_inc + ${LOCAL_ROOT_PATH}/inc/host_inc/gp + ${LOCAL_ROOT_PATH}/inc/enclave_inc + ${LOCAL_ROOT_PATH}/inc/enclave_inc/gp + ${ITRUSTEE_TEEDIR}/include/TA + ${ITRUSTEE_TEEDIR}/include/TA/huawei_ext + ${ITRUSTEE_LIBC}/arch/aarch64 + ${ITRUSTEE_LIBC}/ + ${ITRUSTEE_LIBC}/arch/arm/bits + ${ITRUSTEE_LIBC}/arch/generic + ${ITRUSTEE_LIBC}/arch/arm + ${LOCAL_ROOT_PATH}/inc/enclave_inc/gp/itrustee) + + if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.13.0") + target_link_directories(${PREFIX} PRIVATE ${SECGEAR_INSTALL_PATH}) + endif() + + foreach(WHITE_LIST ${WHITELIST}) + add_definitions(-D${WHITE_LIST}="${${WHITE_LIST}}") + endforeach(WHITE_LIST) + add_definitions(-DWHITE_LIST_OWNER="${WHITE_LIST_OWNER}") + + target_link_libraries(${PREFIX} -lsecgear_tee) + + add_custom_command(TARGET ${PREFIX} + POST_BUILD + COMMAND bash ${SIGN_TOOL} -d sign -x trustzone -i /home/sunny/secGear/debug/lib/lib${PREFIX}.so -c ${CMAKE_CURRENT_SOURCE_DIR}/manifest.txt -m ${CMAKE_CURRENT_SOURCE_DIR}/config_cloud.ini + -o ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT}) + + install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT} + DESTINATION /data + PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE) + +endif() + +if(CC_SGX) + set(SGX_DIR ${SGXSDK}) + set(CMAKE_C_FLAGS "${COMMON_C_FLAGS} -m64 -fvisibility=hidden") + set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -std=c++11 -O2 -nostdinc++") + set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -s") + set(LINK_LIBRARY_PATH ${SGX_DIR}/lib64) + + if(CC_SIM) + set(Trts_Library_Name sgx_trts_sim) + set(Service_Library_Name sgx_tservice_sim) + else() + set(Trts_Library_Name sgx_trts) + set(Service_Library_Name sgx_tservice) + endif() + + set(Crypto_Library_Name sgx_tcrypto) + + set(CMAKE_SHARED_LINKER_FLAGS "${COMMON_C_LINK_FLAGS} -Wl,-z,defs -Wl,-pie -Bstatic -Bsymbolic -eenclave_entry \ + -Wl,--export-dynamic -Wl,--defsym,__ImageBase=0 -Wl,--gc-sections -Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/Enclave.lds") + + if(${CMAKE_VERSION} VERSION_LESS "3.13.0") + link_directories(${LINK_LIBRARY_PATH}) + endif() + + add_library(${PREFIX} SHARED ${SOURCE_FILES} ${AUTO_FILES}) + + target_include_directories(${PREFIX} PRIVATE + ${CMAKE_CURRENT_BINARY_DIR} + ${SGX_DIR}/include/tlibc + ${SGX_DIR}/include/libcxx + ${SGX_DIR}/include + ${LOCAL_ROOT_PATH}/inc/host_inc + ${LOCAL_ROOT_PATH}/inc/host_inc/sgx) + + if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.13.0") + target_link_directories(${PREFIX} PRIVATE + ${LINK_LIBRARY_PATH}) + endif() + + target_link_libraries(${PREFIX} -Wl,--whole-archive ${Trts_Library_Name} -Wl,--no-whole-archive + -Wl,--start-group -lsgx_tstdc -lsgx_tcxx -l${Crypto_Library_Name} -l${Service_Library_Name} -Wl,--end-group) + add_custom_command(TARGET ${PREFIX} + POST_BUILD + COMMAND umask 0177 + COMMAND openssl genrsa -3 -out ${PEM} 3072 + COMMAND bash ${SIGN_TOOL} -d sign -x sgx -i ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/lib${PREFIX}.so -k ${PEM} -o ${OUTPUT} -c ${CMAKE_CURRENT_SOURCE_DIR}/Enclave.config.xml) +endif() + +set_target_properties(${PREFIX} PROPERTIES SKIP_BUILD_RPATH TRUE) diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM4/enclave/Enclave.config.xml" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM4/enclave/Enclave.config.xml" new file mode 100644 index 0000000000000000000000000000000000000000..a5e05e0175b43f97645fde0ea33a1fe0562398cf --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM4/enclave/Enclave.config.xml" @@ -0,0 +1,12 @@ + + 0 + 0 + 0x100000 + 0x80000 + 10 + 1 + + 1 + 0 + 0xFFFFFFFF + diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM4/enclave/Enclave.lds" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM4/enclave/Enclave.lds" new file mode 100644 index 0000000000000000000000000000000000000000..ab77e6478ef3c3448356e80b9884a8a533dd32c6 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM4/enclave/Enclave.lds" @@ -0,0 +1,11 @@ +enclave.so +{ + global: + g_global_data_sim; + g_global_data; + enclave_entry; + g_peak_heap_used; + local: + *; +}; + diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM4/enclave/enclave_SM4.c" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM4/enclave/enclave_SM4.c" new file mode 100644 index 0000000000000000000000000000000000000000..a86647497e7f1117748e7d9fd1a700ea35b0e5a5 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM4/enclave/enclave_SM4.c" @@ -0,0 +1,42 @@ +#include "Enclave1_t.h" +#include +#include "sm4.h" + +uint32 message[4]; +uint32 key[4]; +uint32 res_enc[4], res_dec[4]; + +void sm4test(const uint32 *M, const uint32 *MK, const int rounds) { + SM4_CTX c; + sm4_init(&c, M, MK); + for (int i=0; i +#include "../include/common_utils.h" + +#define ENCRYPT 0 +#define DECRYPT 1 +const uint8 Sbox[256] = { + 0xD6, 0x90, 0xE9, 0xFE, 0xCC, 0xE1, 0x3D, 0xB7, 0x16, 0xB6, 0x14, 0xC2, 0x28, 0xFB, 0x2C, 0x05, + 0x2B, 0x67, 0x9A, 0x76, 0x2A, 0xBE, 0x04, 0xC3, 0xAA, 0x44, 0x13, 0x26, 0x49, 0x86, 0x06, 0x99, + 0x9C, 0x42, 0x50, 0xF4, 0x91, 0xEF, 0x98, 0x7A, 0x33, 0x54, 0x0B, 0x43, 0xED, 0xCF, 0xAC, 0x62, + 0xE4, 0xB3, 0x1C, 0xA9, 0xC9, 0x08, 0xE8, 0x95, 0x80, 0xDF, 0x94, 0xFA, 0x75, 0x8F, 0x3F, 0xA6, + 0x47, 0x07, 0xA7, 0xFC, 0xF3, 0x73, 0x17, 0xBA, 0x83, 0x59, 0x3C, 0x19, 0xE6, 0x85, 0x4F, 0xA8, + 0x68, 0x6B, 0x81, 0xB2, 0x71, 0x64, 0xDA, 0x8B, 0xF8, 0xEB, 0x0F, 0x4B, 0x70, 0x56, 0x9D, 0x35, + 0x1E, 0x24, 0x0E, 0x5E, 0x63, 0x58, 0xD1, 0xA2, 0x25, 0x22, 0x7C, 0x3B, 0x01, 0x21, 0x78, 0x87, + 0xD4, 0x00, 0x46, 0x57, 0x9F, 0xD3, 0x27, 0x52, 0x4C, 0x36, 0x02, 0xE7, 0xA0, 0xC4, 0xC8, 0x9E, + 0xEA, 0xBF, 0x8A, 0xD2, 0x40, 0xC7, 0x38, 0xB5, 0xA3, 0xF7, 0xF2, 0xCE, 0xF9, 0x61, 0x15, 0xA1, + 0xE0, 0xAE, 0x5D, 0xA4, 0x9B, 0x34, 0x1A, 0x55, 0xAD, 0x93, 0x32, 0x30, 0xF5, 0x8C, 0xB1, 0xE3, + 0x1D, 0xF6, 0xE2, 0x2E, 0x82, 0x66, 0xCA, 0x60, 0xC0, 0x29, 0x23, 0xAB, 0x0D, 0x53, 0x4E, 0x6F, + 0xD5, 0xDB, 0x37, 0x45, 0xDE, 0xFD, 0x8E, 0x2F, 0x03, 0xFF, 0x6A, 0x72, 0x6D, 0x6C, 0x5B, 0x51, + 0x8D, 0x1B, 0xAF, 0x92, 0xBB, 0xDD, 0xBC, 0x7F, 0x11, 0xD9, 0x5C, 0x41, 0x1F, 0x10, 0x5A, 0xD8, + 0x0A, 0xC1, 0x31, 0x88, 0xA5, 0xCD, 0x7B, 0xBD, 0x2D, 0x74, 0xD0, 0x12, 0xB8, 0xE5, 0xB4, 0xB0, + 0x89, 0x69, 0x97, 0x4A, 0x0C, 0x96, 0x77, 0x7E, 0x65, 0xB9, 0xF1, 0x09, 0xC5, 0x6E, 0xC6, 0x84, + 0x18, 0xF0, 0x7D, 0xEC, 0x3A, 0xDC, 0x4D, 0x20, 0x79, 0xEE, 0x5F, 0x3E, 0xD7, 0xCB, 0x39, 0x48, +}; +const uint32 FK[4] = { + 0xA3B1BAC6, 0x56AA3350, 0x677D9197, 0xB27022DC, +}; +const uint32 CK[32] = { + 0x00070E15, 0x1C232A31, 0x383F464D, 0x545B6269, + 0x70777E85, 0x8C939AA1, 0xA8AFB6BD, 0xC4CBD2D9, + 0xE0E7EEF5, 0xFC030A11, 0x181F262D, 0x343B4249, + 0x50575E65, 0x6C737A81, 0x888F969D, 0xA4ABB2B9, + 0xC0C7CED5, 0xDCE3EAF1, 0xF8FF060D, 0x141B2229, + 0x30373E45, 0x4C535A61, 0x686F767D, 0x848B9299, + 0xA0A7AEB5, 0xBCC3CAD1, 0xD8DFE6ED, 0xF4FB0209, + 0x10171E25, 0x2C333A41, 0x484F565D, 0x646B7279, +}; +#define L(B) ((B) ^ (ROTATE((B), 2)) ^ (ROTATE((B), 10)) ^ (ROTATE((B), 18)) ^ (ROTATE((B), 24))) +#define LP(B) ((B) ^ (ROTATE((B), 13)) ^ (ROTATE((B), 23))) +#define S(X) (((uint32)Sbox[((X) >> 24) & 0xFF] << 24) | \ + ((uint32)Sbox[((X) >> 16) & 0xFF] << 16) | \ + ((uint32)Sbox[((X) >> 8) & 0xFF] << 8) | \ + ((uint32)Sbox[(X) & 0xFF])) +#define T(X) L(S(X)) +#define TP(X) LP(S(X)) +#define F(X0, X1, X2, X3, rk) ((X0) ^ T((X1) ^ (X2) ^ (X3) ^ (rk))) + +typedef struct sm4_ctx_st { + uint32 M[4]; + uint32 rk[32]; +} SM4_CTX; + +int sm4_init(SM4_CTX *ctx, const uint32 *M, const uint32 *MK) { + memset(ctx, 0, sizeof(*ctx)); + uint32 K[36]; + memset(K, 0, sizeof(K)); + int i; + for (i = 0; i < 4; ++i) { + K[i] = MK[i] ^ FK[i]; + ctx->M[i] = M[i]; + } + for (i = 0; i < 32; ++i) { + ctx->rk[i] = K[i+4] = K[i] ^ TP(K[i+1] ^ K[i+2] ^ K[i+3] ^ CK[i]); + } + return 1; +} + +int sm4_round(SM4_CTX *ctx, int crypt_flag) { + int offset = (crypt_flag ? 31 : 0), i; + uint32 X[36]; + memset(X, 0, sizeof(X)); + for (i = 0; i < 4; ++i) { + X[i] = ctx->M[i]; + } + for (i = 0; i < 32; ++i) { + X[i+4] = F(X[i], X[i+1], X[i+2], X[i+3], ctx->rk[i^offset]); + } + ctx->M[0] = X[35]; + ctx->M[1] = X[34]; + ctx->M[2] = X[33]; + ctx->M[3] = X[32]; + return 1; +} + +int sm4_encrypt(SM4_CTX *ctx) { + return sm4_round(ctx, ENCRYPT); +} + +int sm4_decrypt(SM4_CTX *ctx) { + return sm4_round(ctx, DECRYPT); +} + +int sm4_final(uint32 *out, SM4_CTX *ctx) { + if (!memcpy(out, ctx->M, sizeof(ctx->M))) { + return 0; + } + return 1; +} + +#endif \ No newline at end of file diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM4/enclave_SM4.edl" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM4/enclave_SM4.edl" new file mode 100644 index 0000000000000000000000000000000000000000..3de3690306660b78462d1431c50a0442ba712d55 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM4/enclave_SM4.edl" @@ -0,0 +1,16 @@ +enclave { + include "secgear_urts.h" + from "secgear_tstdc.edl" import *; + + trusted { + public void setKeys([in, count=4] uint32_t *src); + public void setKeys([in, count=4] uint32_t *src); + public void callsm4test1(); + public void callsm4test2(); + public void getKeys([out, count=4] uint32_t *buf); + }; + + untrusted { + + }; +}; diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM4/host/CMakeLists.txt" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM4/host/CMakeLists.txt" new file mode 100644 index 0000000000000000000000000000000000000000..2dae205cce16639bb89887ff41bc436d33ff94b3 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM4/host/CMakeLists.txt" @@ -0,0 +1,88 @@ +# Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved. +# secGear is licensed under the Mulan PSL v2. +# You can use this software according to the terms and conditions of the Mulan PSL v2. +# You may obtain a copy of Mulan PSL v2 at: +# http://license.coscl.org.cn/MulanPSL2 +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR +# PURPOSE. +# See the Mulan PSL v2 for more details. + +#set auto code prefix +set(PREFIX enclave_SM4) +#set host exec name +set(OUTPUT secgear_enclave_SM4) +#set host src code +set(SOURCE_FILE ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp) + +set(CMAKE_CXX_STANDARD 11) + +#set auto code +if(CC_GP) + set(AUTO_FILES ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_u.h ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_u.c ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_args.h) + add_custom_command(OUTPUT ${AUTO_FILES} + DEPENDS ${CURRENT_ROOT_PATH}/${EDL_FILE} + COMMAND ${CODEGEN} --${CODETYPE} --untrusted ${CURRENT_ROOT_PATH}/${EDL_FILE} --search-path ${LOCAL_ROOT_PATH}/inc/host_inc/gp) +endif() + +if(CC_SGX) + set(AUTO_FILES ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_u.h ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_u.c) + add_custom_command(OUTPUT ${AUTO_FILES} + DEPENDS ${CURRENT_ROOT_PATH}/${EDL_FILE} + COMMAND ${CODEGEN} --${CODETYPE} --untrusted ${CURRENT_ROOT_PATH}/${EDL_FILE} --search-path ${LOCAL_ROOT_PATH}/inc/host_inc/sgx --search-path ${SGXSDK}/include) +endif() + +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIE") +set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -O2 -fPIE") +set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -s") + +if(CC_GP) + if(${CMAKE_VERSION} VERSION_LESS "3.13.0") + link_directories(${SECGEAR_INSTALL_PATH}) + endif() + add_executable(${OUTPUT} ${SOURCE_FILE} ${AUTO_FILES}) + target_include_directories(${OUTPUT} PRIVATE + ${LOCAL_ROOT_PATH}/inc/host_inc + ${LOCAL_ROOT_PATH}/inc/host_inc/gp + ${CMAKE_CURRENT_BINARY_DIR}) + if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.13.0") + target_link_directories(${OUTPUT} PRIVATE ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}) + endif() +endif() + +if(CC_SGX) + if(${CMAKE_VERSION} VERSION_LESS "3.13.0") + link_directories(${CMAKE_LIBRARY_OUTPUT_DIRECTORY}) + endif() + add_executable(${OUTPUT} ${SOURCE_FILE} ${AUTO_FILES}) + target_include_directories(${OUTPUT} PRIVATE + ${LOCAL_ROOT_PATH}/inc/host_inc + ${LOCAL_ROOT_PATH}/inc/host_inc/sgx + ${CMAKE_CURRENT_BINARY_DIR}) + if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.13.0") + target_link_directories(${OUTPUT} PRIVATE ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}) + endif() +endif() + +if(CC_SIM) + target_link_libraries(${OUTPUT} secgearsim) +else() + target_link_libraries(${OUTPUT} secgear) +endif() +set_target_properties(${OUTPUT} PROPERTIES SKIP_BUILD_RPATH TRUE) + +if(CC_GP) + #itrustee install whitelist /vender/bin/teec_hello + install(TARGETS ${OUTPUT} + RUNTIME + DESTINATION /vendor/bin/ + PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ) +endif() + +if(CC_SGX) + install(TARGETS ${OUTPUT} + RUNTIME + DESTINATION ${CMAKE_BINARY_DIR}/bin/ + PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ) +endif() + diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM4/host/main.cpp" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM4/host/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..960a50d416c668aa5a09cc1451aa56923e539fbc --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM4/host/main.cpp" @@ -0,0 +1,51 @@ +#include"enclave.h" +#include"Enclave1_u.h" +#include +#include + +const u_int32_t message[4] = { + 0x01234567, 0x89ABCDEF, + 0xFEDCBA98, 0x76543210, +}; +const u_int32_t key[4] = { + 0x01234567, 0x89ABCDEF, + 0xFEDCBA98, 0x76543210, +}; + +int main(int argc, char* argv[]) { + cc_enclave_t *eid; + cc_enclave_result_t ret; + char *path = transferFromString(PATH); + + ret = cc_enclave_create(path, AUTO_ENCLAVE_TYPE, 0, SECGEAR_DEBUG_FLAG, NULL, 0, &eid); + if (ret != CC_SUCCESS) { + printf("ERR: %#x ,failed to create enclave.\n", ret); + return -1; + } + + u_int32_t res[4]; + setSM4Message(eid, message); + setSM4Key(eid, key); + callsm4test1(eid); + getSM4Result(eid, res); + printf("encrypt 1: "); + for (int i=0; i<4; i++){ + printf("%08X", res[i]); + } + printf("\n"); + + setSM4Message(eid, message); + setSM4Key(eid, key); + callsm4test2(eid); + getSM4Result(eid, res); + printf("encrypt 100000: "); + for (int i=0; i<4; i++){ + printf("%08X", res[i]); + } + printf("\n"); + + if (CC_SUCCESS != cc_enclave_destroy(eid)) { + return -1; + } + return 0; +} diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM4/include/common_utils.h" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM4/include/common_utils.h" new file mode 100644 index 0000000000000000000000000000000000000000..9251edce48cf8aeb25404fcf5391335698a83a06 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM4/include/common_utils.h" @@ -0,0 +1,69 @@ +#ifndef SECGEAR_SM_COMMON_UTILS +#define SECGEAR_SM_COMMON_UTILS + +#include +#define debugPrint (printf("%s: %s: %d\n",__FILE__,__FUNCTION__,__LINE__),fflush(stdout),1) + +typedef __uint8_t uint8; +typedef __uint16_t uint16; +typedef __uint32_t uint32; +typedef __uint64_t uint64; + +#define CONCAT_8x4(x0, x1, x2, x3) ((((uint32)(x0) << 8 | (x1)) << 8 | (x2)) << 8 | (x3)) // X = (x0 || x1 || x2 || x3) +#define CONCAT_16x2(x0, x1) ((uint32)(x0) << 16 | (x1)) // X = (x0 || x1) +#define EXTRACT_8_32(X, i) (((X) >> (((i) ^ 3) << 3)) & 0xFF) // X = (x0 || x1 || x2 || x3) +#define EXTRACT_16_32(X, i) (((X) >> (((i) ^ 1) << 4)) & 0xFFFF) // X = (x0 || x1) +#define EXTRACT_16H_31(X) (((X) >> 15) & 0xFFFF) // Higher 16-bit from 31-bit +#define EXTRACT_16L_31(X) ((X) & 0xFFFF) // Lower 16-bit from 31-bit + +#define ROTATE(a, n) (((a) << (n)) | (((a) & 0xFFFFFFFF) >> ((32 - (n)) & 31))) +#define ROTATE_31(a, n) ((((a) << (n)) & 0x7FFFFFFF) | (((a) & 0x7FFFFFFF) >> (31 - (n)))) + +#define GET_BIT_VALUE_32(x, i) (((x) >> (31 - (i))) & 1) + +const uint32 PREFIX_MASK_32[32] = { + 0x00000000, + 0x80000000, + 0xC0000000, + 0xE0000000, + 0xF0000000, + 0xF8000000, + 0xFC000000, + 0xFE000000, + 0xFF000000, + 0xFF800000, + 0xFFC00000, + 0xFFE00000, + 0xFFF00000, + 0xFFF80000, + 0xFFFC0000, + 0xFFFE0000, + 0xFFFF0000, + 0xFFFF8000, + 0xFFFFC000, + 0xFFFFE000, + 0xFFFFF000, + 0xFFFFF800, + 0xFFFFFC00, + 0xFFFFFE00, + 0xFFFFFF00, + 0xFFFFFF80, + 0xFFFFFFC0, + 0xFFFFFFE0, + 0xFFFFFFF0, + 0xFFFFFFF8, + 0xFFFFFFFC, + 0xFFFFFFFE +}; + +# define HOST_c2l(c,l) (l =(((unsigned long)(*((c)++)))<<24), \ + l|=(((unsigned long)(*((c)++)))<<16), \ + l|=(((unsigned long)(*((c)++)))<< 8), \ + l|=(((unsigned long)(*((c)++))) ) ) +# define HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l)>>24)&0xff), \ + *((c)++)=(unsigned char)(((l)>>16)&0xff), \ + *((c)++)=(unsigned char)(((l)>> 8)&0xff), \ + *((c)++)=(unsigned char)(((l) )&0xff), \ + l) + +#endif \ No newline at end of file diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM4/include/ec.h" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM4/include/ec.h" new file mode 100644 index 0000000000000000000000000000000000000000..56adbe751f17342f84c17e3736f8806deb7f40b5 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_SM4/include/ec.h" @@ -0,0 +1,545 @@ +#include /* BigNum */ +#include "common_utils.h" + +typedef struct ec_point_st { + BIGNUM *X; + BIGNUM *Y; + int is_infinity; +} EC_POINT; + +int EC_POINT_init(EC_POINT *point) { + point->X = BN_new(); + point->Y = BN_new(); + point->is_infinity = 0; + if (point->X == NULL || point->Y == NULL) { + BN_free(point->X); + BN_free(point->Y); + return 0; + } + return 1; +} + +EC_POINT *EC_POINT_new() { + EC_POINT *ret = NULL; + ret = malloc(sizeof(*ret)); + if (ret == NULL) { + return NULL; + } + if (!EC_POINT_init(ret)) { + free(ret); + return NULL; + } + return ret; +} + +int EC_POINT_setXY(EC_POINT *r, const BIGNUM *x, const BIGNUM *y) { + if (r == NULL) { + return 0; + } + if (!BN_copy(r->X, x) || !(BN_copy(r->Y, y))) { + return 0; + } + return 1; +} + +const EC_POINT *EC_point_infinity() { + static const EC_POINT const_infinity = { NULL, NULL, 1 }; + return &const_infinity; +} + +int EC_POINT_is_infinity(const EC_POINT *point) { + return point->is_infinity; +} + +int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src) { + if (dest == NULL) { + return 0; + } + if (EC_POINT_is_infinity(src)) { + dest->is_infinity = 1; + } + else{ + dest->is_infinity = 0; + if (!BN_copy(dest->X, src->X) || !BN_copy(dest->Y, src->Y)) return 0; + } + return 1; +} + +#define NORMAL 0x1 +#define COMPRESS 0x2 +#define MIX 0x4 + +int EC_POINT_ecp2bin(uint8 *out, const EC_POINT *point, int p_bytes, int flag) { + uint8 PC; + uint8 *p = out, *buf; + int i; + buf = malloc(p_bytes); + if (EC_POINT_is_infinity(point)) { + return 0; + } + if (flag == NORMAL) { + PC = 0x04; + *(p++) = PC; + if (!BN_bn2binpad(point->X, buf, p_bytes)) { + return 0; + } + for (i = 0; i < p_bytes; ++i) { + *(p++) = buf[i]; + } + if (!BN_bn2binpad(point->Y, buf, p_bytes)) { + return 0; + } + for (i = 0; i < p_bytes; ++i) { + *(p++) = buf[i]; + } + } + else { + goto err; /* not supported */ + } + return 1; + err: + return 0; +} + +int EC_POINT_bin2ecp(EC_POINT *point, const uint8 *src, int p_bytes) { + if (point == NULL) { + return 0; + } + uint8 PC = *src; + const uint8 *data = src + 1; + uint8 *buf; + int i; + if (PC == 0x04) { + if (!BN_bin2bn(data, p_bytes, point->X) + || !BN_bin2bn(data+p_bytes, p_bytes, point->Y)) { + return 0; + } + } + else { + goto err; /* not supported */ + } + return 1; + err: + return 0; +} + +int EC_POINT_ecp2bin_normal(uint8 *out, const EC_POINT *point, int p_bytes) { + return EC_POINT_ecp2bin(out, point, p_bytes, NORMAL); +} + +// int EC_POINT_ecp2bin_compressed(uint8 *out, const EC_POINT *point, int p_bytes) { +// return EC_POINT_ecp2bin(out, point, p_bytes, COMPRESS); +// } + +// int EC_POINT_ecp2bin_mix(uint8 *out, const EC_POINT *point, int p_bytes) { +// return EC_POINT_ecp2bin(out, point, p_bytes, MIX); +// } + +typedef struct ec_group_st { + EC_POINT *generator; /* point G */ + BIGNUM *order; /* n */ + BIGNUM *cofactor; /* h */ + BIGNUM *field; /* module p */ + BIGNUM *a, *b; /* ECC parameter */ +} EC_GROUP; + +int EC_GROUP_init(EC_GROUP *group) { + group->generator = EC_POINT_new(); + group->order = BN_new(); + group->cofactor = BN_new(); + group->field = BN_new(); + group->a = BN_new(); + group->b = BN_new(); + if (group->generator == NULL || group->b == NULL) { + free(group->generator); + BN_free(group->order); + BN_free(group->cofactor); + BN_free(group->field); + BN_free(group->a); + BN_free(group->b); + return 0; + } + return 1; +} + +EC_GROUP *EC_GROUP_new() { + EC_GROUP *ret = NULL; + ret = malloc(sizeof(*ret)); + if (ret == NULL) { + return NULL; + } + if (!EC_GROUP_init(ret)) { + free(ret); + return NULL; + } + return ret; +} + +int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src) { + if (dest == NULL) { + return 0; + } + if (dest == src) { + return 1; + } + if (!EC_POINT_copy(dest->generator, src->generator) + || !BN_copy(dest->order, src->order) + || !BN_copy(dest->cofactor, src->cofactor) + || !BN_copy(dest->field, src->field) + || !BN_copy(dest->a, src->a) + || !BN_copy(dest->b, src->b)) { + return 0; + } + return 1; +} + +int EC_GROUP_set_param(EC_GROUP *group, const BIGNUM *field, const BIGNUM *a, const BIGNUM *b, const BIGNUM *order, const BIGNUM *cofactor, const EC_POINT *point) { + if (group == NULL) { + return 0; + } + group->field = BN_new(); + group->a = BN_new(); + group->b = BN_new(); + group->order = BN_new(); + group->cofactor = BN_new(); + group->generator = EC_POINT_new(); + if (group->cofactor == NULL + || group->generator == NULL) { + return 0; + } + if (!BN_copy(group->field, field) + || !BN_copy(group->a, a) + || !BN_copy(group->b, b) + || !BN_copy(group->order, order) + || !BN_copy(group->cofactor, cofactor) + || !EC_POINT_copy(group->generator, point)) { + return 0; + } + return 1; +} + +int EC_POINT_double(EC_POINT *r, const EC_POINT *a, const EC_GROUP *group) { + const BIGNUM *p; + BN_CTX *ctx = NULL; + int ret = 0; + BIGNUM *ax, *ay, *lnum, *ldeno; + BIGNUM *tmp, *bn_value_3; + if (r == NULL) { + return 0; + } + if (EC_POINT_is_infinity(a)) { + return EC_POINT_copy(r, EC_point_infinity()); + } + p = group->field; + ctx = BN_CTX_new(); + if (ctx == NULL) { + return 0; + } + BN_CTX_start(ctx); + ax = BN_CTX_get(ctx); + ay = BN_CTX_get(ctx); + lnum = BN_CTX_get(ctx); + ldeno = BN_CTX_get(ctx); + tmp = BN_CTX_get(ctx); + bn_value_3 = BN_CTX_get(ctx); + if (bn_value_3 == NULL) { + goto done; + } + if (!BN_copy(ax, a->X) + || !BN_copy(ay, a->Y)) { + goto done; + } + if (!BN_set_word(bn_value_3, 3) + || !BN_mod_sqr(lnum, ax, p, ctx) + || !BN_mod_mul(lnum, lnum, bn_value_3, p, ctx) + || !BN_mod_add_quick(lnum, lnum, group->a, p) + || !BN_mod_lshift1_quick(ldeno, ay, p) + || !BN_mod_inverse(ldeno, ldeno, p, ctx) + || !BN_mod_mul(lnum, lnum, ldeno, p, ctx)) { + goto done; + } + if (!BN_mod_sqr(r->X, lnum, p, ctx)) { + goto done; + } + if (!BN_mod_lshift1_quick(tmp, ax, p)) { + goto done; + } + if (!BN_mod_sub_quick(r->X, r->X, tmp, p)) { + goto done; + } + if (!BN_mod_sub_quick(r->Y, ax, r->X, p)) { + goto done; + } + if (!BN_mod_mul(r->Y, lnum, r->Y, p, ctx)) { + goto done; + } + if (!BN_mod_sub_quick(r->Y, r->Y, ay, p)) { + goto done; + } + if (r == NULL) { + r = EC_POINT_new(); + if (r == NULL) { + goto done; + } + } + ret = 1; + done: + BN_CTX_end(ctx); + BN_CTX_free(ctx); + return ret; +} + +int EC_POINT_add(EC_POINT *r, const EC_POINT *a, const EC_POINT *b, const EC_GROUP *group) { + const BIGNUM *p; + BN_CTX *ctx = NULL; + int ret = 0; + BIGNUM *ax, *ay, *bx, *by, *lnum, *ldeno; + BIGNUM *tmp, *bn_value_3; + if (r == NULL) { + return 0; + } + if (a == b) { + return EC_POINT_double(r, a, group); + } + if (EC_POINT_is_infinity(a)) { + return EC_POINT_copy(r, b); + } + if (EC_POINT_is_infinity(b)) { + return EC_POINT_copy(r, a); + } + p = group->field; + ctx = BN_CTX_new(); + if (ctx == NULL) { + return 0; + } + BN_CTX_start(ctx); + ax = BN_CTX_get(ctx); + ay = BN_CTX_get(ctx); + bx = BN_CTX_get(ctx); + by = BN_CTX_get(ctx); + lnum = BN_CTX_get(ctx); + ldeno = BN_CTX_get(ctx); + tmp = BN_CTX_get(ctx); + bn_value_3 = BN_CTX_get(ctx); + if (bn_value_3 == NULL) { + goto done; + } + if (!BN_copy(ax, a->X) + || !BN_copy(ay, a->Y) + || !BN_copy(bx, b->X) + || !BN_copy(by, b->Y)) { + goto done; + } + if (BN_cmp(ax, bx) == 0) { + if (!BN_mod_add_quick(tmp, ay, by, p)) { + goto done; + } + if (BN_is_zero(tmp) + || !EC_POINT_copy(r, EC_point_infinity())) { + goto done; + } + if (!BN_set_word(bn_value_3, 3)) { + goto done; + } + if (!BN_mod_sqr(lnum, ax, p, ctx) + || !BN_mod_mul(lnum, lnum, bn_value_3, p, ctx) + || !BN_mod_add_quick(lnum, lnum, group->a, p) + || !BN_mod_lshift1_quick(ldeno, ay, p) + || !BN_mod_inverse(ldeno, ldeno, p, ctx) + || !BN_mod_mul(lnum, lnum, ldeno, p, ctx)) { + goto done; + } + } + else { + if (!BN_mod_sub_quick(lnum, by, ay, p)) { + goto done; + } + if (!BN_mod_sub_quick(ldeno, bx, ax, p)) { + goto done; + } + if (!BN_mod_inverse(ldeno, ldeno, p, ctx)) { + goto done; + } + if (!BN_mod_mul(lnum, lnum, ldeno, p, ctx)) { + goto done; + } + } + if (!BN_mod_sqr(tmp, lnum, p, ctx)) { + goto done; + } + if (!BN_mod_sub_quick(tmp, tmp, ax, p)) { + goto done; + } + if (!BN_mod_sub_quick(r->X, tmp, bx, p)) { + goto done; + } + if (!BN_mod_sub_quick(r->Y, ax, r->X, p)) { + goto done; + } + if (!BN_mod_mul(r->Y, lnum, r->Y, p, ctx)) { + goto done; + } + if (!BN_mod_sub_quick(r->Y, r->Y, ay, p)) { + goto done; + } + if (r == NULL) { + r = EC_POINT_new(); + if (r == NULL) { + goto done; + } + } + ret = 1; + done: + BN_CTX_end(ctx); + BN_CTX_free(ctx); + return ret; +} + +int EC_POINT_mul(EC_POINT *r, const EC_POINT *a, const BIGNUM *b, const EC_GROUP *group) { + if (r == NULL) { + return 0; + } + if (BN_is_negative(b)) { + EC_POINT *_a = EC_POINT_new(); + BIGNUM *_b = BN_new(); + if (!EC_POINT_copy(_a, a) + || !BN_copy(_b, b) + || !BN_mod_sub_quick(_a->Y, group->field, _a->Y, group->field)) { + return 0; + } + BN_set_negative(_b, 0); + return EC_POINT_mul(r, _a, _b, group); + } + if (!EC_POINT_copy(r, EC_point_infinity())){ + return 0; + } + int bits = BN_num_bits(b); + for (int i = bits-1; i >= 0; --i) { + if (!EC_POINT_double(r, r, group)) { + return 0; + } + if (BN_is_bit_set(b, i)) { + if (!EC_POINT_add(r, r, a, group)) { + return 0; + } + } + } + return 1; +} + +/* + * Only for curve y^2 = x^3 + ax + b. + * Please check infinity before calling. + */ +int EC_POINT_on_curve_simple(const EC_POINT *point, const EC_GROUP *group) { + if (point == NULL || group == NULL) { + return 0; + } + if (EC_POINT_is_infinity(point)) { + return 1; + } + int ret = 0; + BN_CTX *ctx; + BIGNUM *p, *a, *b, *x, *y, *lhs, *rhs; + EC_POINT *Nq; + ctx = BN_CTX_new(); + if (ctx == NULL) { + return ret; + } + BN_CTX_start(ctx); + p = group->field; + a = group->a; + b = group->b; + x = point->X; + y = point->Y; + lhs = BN_CTX_get(ctx); + rhs = BN_CTX_get(ctx); + Nq = EC_POINT_new(); + if (p == NULL + || a == NULL + || b == NULL + || x == NULL + || y == NULL + || rhs == NULL + || Nq == NULL) { + return ret; + } + if (!BN_mod_mul(lhs, y, y, p, ctx) + || !BN_mod_mul(rhs, x, x, p, ctx) + || !BN_mod_add_quick(rhs, rhs, a, p) + || !BN_mod_mul(rhs, rhs, x, p, ctx) + || !BN_mod_add_quick(rhs, rhs, b, p) + || !EC_POINT_mul(Nq, point, group->order, group)) { + return ret; + } + if (BN_cmp(lhs, rhs) != 0 + || !EC_POINT_is_infinity(Nq)) { + return ret; + } + ret = 1; + BN_CTX_end(ctx); + BN_CTX_free(ctx); + return ret; +} + +typedef struct ec_key_st { + EC_GROUP *group; + EC_POINT *pub_key; + BIGNUM *priv_key; +} EC_KEY; + +int EC_KEY_init(EC_KEY *key) { + key->group = EC_GROUP_new(); + key->pub_key = EC_POINT_new(); + key->priv_key = BN_new(); + if (key->group == NULL + || key->pub_key == NULL + || key->priv_key == NULL) { + free(key->group); + free(key->pub_key); + BN_free(key->priv_key); + return 0; + } + return 1; +} + +EC_KEY *EC_KEY_new() { + EC_KEY *ret = NULL; + ret = malloc(sizeof(*ret)); + if (ret == NULL) { + return NULL; + } + if (!EC_KEY_init(ret)) { + free(ret); + return NULL; + } + return ret; +} + +int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group) { + if (!EC_GROUP_copy(key->group, group)) { + return 0; + } + return 1; +} + +int EC_KEY_set_priv_key(EC_KEY *key, const BIGNUM *priv_key) { + BIGNUM *tmp; + tmp = BN_dup(priv_key); + if (tmp == NULL) { + return 0; + } + BN_clear_free(key->priv_key); + key->priv_key = tmp; + return 1; +} + +int EC_KEY_generate_pub_key(EC_KEY *key) { + return EC_POINT_mul(key->pub_key, key->group->generator, key->priv_key, key->group); +} + +const BIGNUM *EC_KEY_get_priv_key(const EC_KEY *key) { + return key->priv_key; +} + +int EC_KEY_generate_key() {} diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/CMakeLists.txt" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/CMakeLists.txt" new file mode 100644 index 0000000000000000000000000000000000000000..e1b266d0fa36a029c025979ef2fc5a0f1b81274b --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/CMakeLists.txt" @@ -0,0 +1,38 @@ +# Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved. +# secGear is licensed under the Mulan PSL v2. +# You can use this software according to the terms and conditions of the Mulan PSL v2. +# You may obtain a copy of Mulan PSL v2 at: +# http://license.coscl.org.cn/MulanPSL2 +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR +# PURPOSE. +# See the Mulan PSL v2 for more details. + +cmake_minimum_required(VERSION 3.10 FATAL_ERROR) +project(enclave_ZUC) + +set(CMAKE_C_STANDARD 99) + +set(CURRENT_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) + +#set edl name +set(EDL_FILE enclave_ZUC.edl) +#set(LOCAL_ROOT_PATH "$ENV{CC_SDK}") +#set(SECGEAR_INSTALL_PATH /usr/lib64/) + +if(CC_GP) + set(CODETYPE trustzone) + set(CODEGEN codegen_arm64) + execute_process(COMMAND uuidgen -r OUTPUT_VARIABLE UUID) + string(REPLACE "\n" "" UUID ${UUID}) + add_definitions(-DPATH="/data/${UUID}.sec") +endif() + +if(CC_SGX) + set(CODETYPE sgx) + set(CODEGEN codegen_x86_64) + add_definitions(-DPATH="${CMAKE_CURRENT_BINARY_DIR}/enclave/enclave.signed.so") +endif() + +add_subdirectory(${CURRENT_ROOT_PATH}/enclave) +add_subdirectory(${CURRENT_ROOT_PATH}/host) diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/enclave/CMakeLists.txt" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/enclave/CMakeLists.txt" new file mode 100644 index 0000000000000000000000000000000000000000..d600d3883204c773447cb2e254ade5dd0d8b4606 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/enclave/CMakeLists.txt" @@ -0,0 +1,162 @@ +# Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved. +# secGear is licensed under the Mulan PSL v2. +# You can use this software according to the terms and conditions of the Mulan PSL v2. +# You may obtain a copy of Mulan PSL v2 at: +# http://license.coscl.org.cn/MulanPSL2 +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR +# PURPOSE. +# See the Mulan PSL v2 for more details. + +#set auto code prefix +set(PREFIX enclave_ZUC) + +set(CMAKE_C_STANDARD 99) +#set sign key +set(PEM Enclave_private.pem) + +#set sign tool +set(SIGN_TOOL ${LOCAL_ROOT_PATH}/tools/sign_tool/sign_tool.sh) + +#set enclave src code +set(SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/enclave_ZUC.c) + +#set log level +set(PRINT_LEVEL 3) +add_definitions(-DPRINT_LEVEL=${PRINT_LEVEL}) + +if(CC_GP) + #set signed output + set(OUTPUT ${UUID}.sec) + #set itrustee device key + set(DEVICEPEM ${CMAKE_CURRENT_SOURCE_DIR}/rsa_public_key_cloud.pem) + #set whilelist. default: /vendor/bin/teec_hello + set(WHITE_LIST_0 /vendor/bin/enclave_ZUC) + set(WHITE_LIST_OWNER root) + set(WHITE_LIST_1 /vendor/bin/secgear_enclave_ZUC) + set(WHITELIST WHITE_LIST_0 WHITE_LIST_1) + + set(AUTO_FILES ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_t.h ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_t.c ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_args.h) + add_custom_command(OUTPUT ${AUTO_FILES} + DEPENDS ${CURRENT_ROOT_PATH}/${EDL_FILE} + COMMAND ${CODEGEN} --${CODETYPE} --trusted ${CURRENT_ROOT_PATH}/${EDL_FILE} --search-path ${LOCAL_ROOT_PATH}/inc/host_inc/gp) +endif() + +if(CC_SGX) + set(OUTPUT enclave.signed.so) + set(AUTO_FILES ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_t.h ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_t.c) + add_custom_command(OUTPUT ${AUTO_FILES} + DEPENDS ${CURRENT_ROOT_PATH}/${EDL_FILE} + COMMAND ${CODEGEN} --${CODETYPE} --trusted ${CURRENT_ROOT_PATH}/${EDL_FILE} --search-path ${LOCAL_ROOT_PATH}/inc/host_inc/sgx --search-path ${SGXSDK}/include) +endif() + +set(COMMON_C_FLAGS "-W -Wall -fno-short-enums -fno-omit-frame-pointer -fstack-protector \ + -Wstack-protector --param ssp-buffer-size=4 -frecord-gcc-switches -Wextra -nostdinc -nodefaultlibs \ + -fno-peephole -fno-peephole2 -Wno-main -Wno-error=unused-parameter \ + -Wno-error=unused-but-set-variable -Wno-error=format-truncation=") + +set(COMMON_C_LINK_FLAGS "-Wl,-z,now -Wl,-z,relro -Wl,-z,noexecstack -Wl,-nostdlib -nodefaultlibs -nostartfiles") +set(COMMON_CXX_FLAGS "${COMMON_C_FLAGS}") + +if(CC_GP) + configure_file("${CMAKE_CURRENT_SOURCE_DIR}/manifest.txt.in" "${CMAKE_CURRENT_SOURCE_DIR}/manifest.txt") + + set(CMAKE_C_FLAGS "${COMMON_C_FLAGS} -march=armv8-a ") + set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -s -fPIC") + set(CMAKE_SHARED_LINKER_FLAGS "${COMMON_C_LINK_FLAGS} -Wl,-s") + + set(ITRUSTEE_TEEDIR ${iTrusteeSDK}/) + set(ITRUSTEE_LIBC ${iTrusteeSDK}/thirdparty/open_source/musl/libc) + + if(${CMAKE_VERSION} VERSION_LESS "3.13.0") + link_directories(${SECGEAR_INSTALL_PATH}) + endif() + + add_library(${PREFIX} SHARED ${SOURCE_FILES} ${AUTO_FILES}) + + target_include_directories( ${PREFIX} PRIVATE + ${CMAKE_CURRENT_BINARY_DIR} + ${LOCAL_ROOT_PATH}/inc/host_inc + ${LOCAL_ROOT_PATH}/inc/host_inc/gp + ${LOCAL_ROOT_PATH}/inc/enclave_inc + ${LOCAL_ROOT_PATH}/inc/enclave_inc/gp + ${ITRUSTEE_TEEDIR}/include/TA + ${ITRUSTEE_TEEDIR}/include/TA/huawei_ext + ${ITRUSTEE_LIBC}/arch/aarch64 + ${ITRUSTEE_LIBC}/ + ${ITRUSTEE_LIBC}/arch/arm/bits + ${ITRUSTEE_LIBC}/arch/generic + ${ITRUSTEE_LIBC}/arch/arm + ${LOCAL_ROOT_PATH}/inc/enclave_inc/gp/itrustee) + + if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.13.0") + target_link_directories(${PREFIX} PRIVATE ${SECGEAR_INSTALL_PATH}) + endif() + + foreach(WHITE_LIST ${WHITELIST}) + add_definitions(-D${WHITE_LIST}="${${WHITE_LIST}}") + endforeach(WHITE_LIST) + add_definitions(-DWHITE_LIST_OWNER="${WHITE_LIST_OWNER}") + + target_link_libraries(${PREFIX} -lsecgear_tee) + + add_custom_command(TARGET ${PREFIX} + POST_BUILD + COMMAND bash ${SIGN_TOOL} -d sign -x trustzone -i /home/sunny/secGear/debug/lib/lib${PREFIX}.so -c ${CMAKE_CURRENT_SOURCE_DIR}/manifest.txt -m ${CMAKE_CURRENT_SOURCE_DIR}/config_cloud.ini + -o ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT}) + + install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT} + DESTINATION /data + PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE) + +endif() + +if(CC_SGX) + set(SGX_DIR ${SGXSDK}) + set(CMAKE_C_FLAGS "${COMMON_C_FLAGS} -m64 -fvisibility=hidden") + set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -std=c++11 -O2 -nostdinc++") + set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -s") + set(LINK_LIBRARY_PATH ${SGX_DIR}/lib64) + + if(CC_SIM) + set(Trts_Library_Name sgx_trts_sim) + set(Service_Library_Name sgx_tservice_sim) + else() + set(Trts_Library_Name sgx_trts) + set(Service_Library_Name sgx_tservice) + endif() + + set(Crypto_Library_Name sgx_tcrypto) + + set(CMAKE_SHARED_LINKER_FLAGS "${COMMON_C_LINK_FLAGS} -Wl,-z,defs -Wl,-pie -Bstatic -Bsymbolic -eenclave_entry \ + -Wl,--export-dynamic -Wl,--defsym,__ImageBase=0 -Wl,--gc-sections -Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/Enclave.lds") + + if(${CMAKE_VERSION} VERSION_LESS "3.13.0") + link_directories(${LINK_LIBRARY_PATH}) + endif() + + add_library(${PREFIX} SHARED ${SOURCE_FILES} ${AUTO_FILES}) + + target_include_directories(${PREFIX} PRIVATE + ${CMAKE_CURRENT_BINARY_DIR} + ${SGX_DIR}/include/tlibc + ${SGX_DIR}/include/libcxx + ${SGX_DIR}/include + ${LOCAL_ROOT_PATH}/inc/host_inc + ${LOCAL_ROOT_PATH}/inc/host_inc/sgx) + + if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.13.0") + target_link_directories(${PREFIX} PRIVATE + ${LINK_LIBRARY_PATH}) + endif() + + target_link_libraries(${PREFIX} -Wl,--whole-archive ${Trts_Library_Name} -Wl,--no-whole-archive + -Wl,--start-group -lsgx_tstdc -lsgx_tcxx -l${Crypto_Library_Name} -l${Service_Library_Name} -Wl,--end-group) + add_custom_command(TARGET ${PREFIX} + POST_BUILD + COMMAND umask 0177 + COMMAND openssl genrsa -3 -out ${PEM} 3072 + COMMAND bash ${SIGN_TOOL} -d sign -x sgx -i ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/lib${PREFIX}.so -k ${PEM} -o ${OUTPUT} -c ${CMAKE_CURRENT_SOURCE_DIR}/Enclave.config.xml) +endif() + +set_target_properties(${PREFIX} PROPERTIES SKIP_BUILD_RPATH TRUE) diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/enclave/Enclave.config.xml" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/enclave/Enclave.config.xml" new file mode 100644 index 0000000000000000000000000000000000000000..a5e05e0175b43f97645fde0ea33a1fe0562398cf --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/enclave/Enclave.config.xml" @@ -0,0 +1,12 @@ + + 0 + 0 + 0x100000 + 0x80000 + 10 + 1 + + 1 + 0 + 0xFFFFFFFF + diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/enclave/Enclave.lds" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/enclave/Enclave.lds" new file mode 100644 index 0000000000000000000000000000000000000000..ab77e6478ef3c3448356e80b9884a8a533dd32c6 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/enclave/Enclave.lds" @@ -0,0 +1,11 @@ +enclave.so +{ + global: + g_global_data_sim; + g_global_data; + enclave_entry; + g_peak_heap_used; + local: + *; +}; + diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/enclave/enclave_ZUC.c" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/enclave/enclave_ZUC.c" new file mode 100644 index 0000000000000000000000000000000000000000..01cc67a017f138935110eea06341e448c6ca4b54 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/enclave/enclave_ZUC.c" @@ -0,0 +1,3 @@ +#include "Enclave1_t.h" +#include +#include "zuc.h" \ No newline at end of file diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/enclave/manifest.txt.in" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/enclave/manifest.txt.in" new file mode 100644 index 0000000000000000000000000000000000000000..7b8ecf5937d99b0c7f87478984017cdc92c44ed1 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/enclave/manifest.txt.in" @@ -0,0 +1,8 @@ +gpd.ta.appID: @UUID@ +gpd.ta.service_name: test0108 +gpd.ta.singleInstance: true +gpd.ta.multiSession: false +gpd.ta.multiCommand: false +gpd.ta.instanceKeepAlive: false +gpd.ta.dataSize: 16384 +gpd.ta.stackSize: 20480 diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/enclave/rsa_public_key_cloud.pem" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/enclave/rsa_public_key_cloud.pem" new file mode 100644 index 0000000000000000000000000000000000000000..a321f63ed2fb0459c03fcb63de8f2f21618d8e56 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/enclave/rsa_public_key_cloud.pem" @@ -0,0 +1,11 @@ +-----BEGIN PUBLIC KEY----- +MIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIBigKCAYEAzAPwbnbgBg7JgXERA9Bx +p7GLI1S3e1zL83RMd2+GXb6kO4yMKUL3NUCE2HhA2BtQYmLyGovx59UUcKnU58is +Xux++kH+A2shmOPjYvEFuX0Kt8tc19b8M9b/iHsY8ZmKykqia2a5U+IrECRFJo5p +DWUnl7jrHVtq78BSR1c7iXG1frrEC0AYCuqKJo/fxfmOKL0Y9mENCB3nAwjn9unD +BsO/OhkqvvB3nkeuMfNKPh4wCqtQPve13eTojbuxjX/3ePijplTI5X2Gr+n6Ximn +fYRlytQmMgMl/db0ARSKNApq9bmwzVNrnGWWZWJksdRvf6iL7t17Gs4L9AApOuC9 +WkzxPvwp5ZUqjsGd4oJGWeC6ZE6BTw2vxE+xMFI9uAKHxq9pBKkcGMa0g4fANNNV ++W+8JZGanxEXKB3y/M7BCyQAPCWOHC/RNjmRA1gczLYCPzC4pWu935UZdF1RR6zY +CD3t+FoOGGET/g4CwWgyhb5qkp65Hs6ayYt/DUAqo+yBAgMBAAE= +-----END PUBLIC KEY----- diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/enclave/zuc.h" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/enclave/zuc.h" new file mode 100644 index 0000000000000000000000000000000000000000..d8ea3eafb0ebec9c5b1cd1db436c8a3fee0cb041 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/enclave/zuc.h" @@ -0,0 +1,314 @@ +#ifndef SECGEAR_ZUC_H +#define SECGEAR_ZUC_H + +#include +#include "../include/common_utils.h" + +/* S_BOX BEGIN */ + +const uint8 ZUC_S[2][256] = { + { + 0x3E, 0x72, 0x5B, 0x47, 0xCA, 0xE0, 0x00, 0x33, 0x04, 0xD1, 0x54, 0x98, 0x09, 0xB9, 0x6D, 0xCB, + 0x7B, 0x1B, 0xF9, 0x32, 0xAF, 0x9D, 0x6A, 0xA5, 0xB8, 0x2D, 0xFC, 0x1D, 0x08, 0x53, 0x03, 0x90, + 0x4D, 0x4E, 0x84, 0x99, 0xE4, 0xCE, 0xD9, 0x91, 0xDD, 0xB6, 0x85, 0x48, 0x8B, 0x29, 0x6E, 0xAC, + 0xCD, 0xC1, 0xF8, 0x1E, 0x73, 0x43, 0x69, 0xC6, 0xB5, 0xBD, 0xFD, 0x39, 0x63, 0x20, 0xD4, 0x38, + 0x76, 0x7D, 0xB2, 0xA7, 0xCF, 0xED, 0x57, 0xC5, 0xF3, 0x2C, 0xBB, 0x14, 0x21, 0x06, 0x55, 0x9B, + 0xE3, 0xEF, 0x5E, 0x31, 0x4F, 0x7F, 0x5A, 0xA4, 0x0D, 0x82, 0x51, 0x49, 0x5F, 0xBA, 0x58, 0x1C, + 0x4A, 0x16, 0xD5, 0x17, 0xA8, 0x92, 0x24, 0x1F, 0x8C, 0xFF, 0xD8, 0xAE, 0x2E, 0x01, 0xD3, 0xAD, + 0x3B, 0x4B, 0xDA, 0x46, 0xEB, 0xC9, 0xDE, 0x9A, 0x8F, 0x87, 0xD7, 0x3A, 0x80, 0x6F, 0x2F, 0xC8, + 0xB1, 0xB4, 0x37, 0xF7, 0x0A, 0x22, 0x13, 0x28, 0x7C, 0xCC, 0x3C, 0x89, 0xC7, 0xC3, 0x96, 0x56, + 0x07, 0xBF, 0x7E, 0xF0, 0x0B, 0x2B, 0x97, 0x52, 0x35, 0x41, 0x79, 0x61, 0xA6, 0x4C, 0x10, 0xFE, + 0xBC, 0x26, 0x95, 0x88, 0x8A, 0xB0, 0xA3, 0xFB, 0xC0, 0x18, 0x94, 0xF2, 0xE1, 0xE5, 0xE9, 0x5D, + 0xD0, 0xDC, 0x11, 0x66, 0x64, 0x5C, 0xEC, 0x59, 0x42, 0x75, 0x12, 0xF5, 0x74, 0x9C, 0xAA, 0x23, + 0x0E, 0x86, 0xAB, 0xBE, 0x2A, 0x02, 0xE7, 0x67, 0xE6, 0x44, 0xA2, 0x6C, 0xC2, 0x93, 0x9F, 0xF1, + 0xF6, 0xFA, 0x36, 0xD2, 0x50, 0x68, 0x9E, 0x62, 0x71, 0x15, 0x3D, 0xD6, 0x40, 0xC4, 0xE2, 0x0F, + 0x8E, 0x83, 0x77, 0x6B, 0x25, 0x05, 0x3F, 0x0C, 0x30, 0xEA, 0x70, 0xB7, 0xA1, 0xE8, 0xA9, 0x65, + 0x8D, 0x27, 0x1A, 0xDB, 0x81, 0xB3, 0xA0, 0xF4, 0x45, 0x7A, 0x19, 0xDF, 0xEE, 0x78, 0x34, 0x60, + }, + { + 0x55, 0xC2, 0x63, 0x71, 0x3B, 0xC8, 0x47, 0x86, 0x9F, 0x3C, 0xDA, 0x5B, 0x29, 0xAA, 0xFD, 0x77, + 0x8C, 0xC5, 0x94, 0x0C, 0xA6, 0x1A, 0x13, 0x00, 0xE3, 0xA8, 0x16, 0x72, 0x40, 0xF9, 0xF8, 0x42, + 0x44, 0x26, 0x68, 0x96, 0x81, 0xD9, 0x45, 0x3E, 0x10, 0x76, 0xC6, 0xA7, 0x8B, 0x39, 0x43, 0xE1, + 0x3A, 0xB5, 0x56, 0x2A, 0xC0, 0x6D, 0xB3, 0x05, 0x22, 0x66, 0xBF, 0xDC, 0x0B, 0xFA, 0x62, 0x48, + 0xDD, 0x20, 0x11, 0x06, 0x36, 0xC9, 0xC1, 0xCF, 0xF6, 0x27, 0x52, 0xBB, 0x69, 0xF5, 0xD4, 0x87, + 0x7F, 0x84, 0x4C, 0xD2, 0x9C, 0x57, 0xA4, 0xBC, 0x4F, 0x9A, 0xDF, 0xFE, 0xD6, 0x8D, 0x7A, 0xEB, + 0x2B, 0x53, 0xD8, 0x5C, 0xA1, 0x14, 0x17, 0xFB, 0x23, 0xD5, 0x7D, 0x30, 0x67, 0x73, 0x08, 0x09, + 0xEE, 0xB7, 0x70, 0x3F, 0x61, 0xB2, 0x19, 0x8E, 0x4E, 0xE5, 0x4B, 0x93, 0x8F, 0x5D, 0xDB, 0xA9, + 0xAD, 0xF1, 0xAE, 0x2E, 0xCB, 0x0D, 0xFC, 0xF4, 0x2D, 0x46, 0x6E, 0x1D, 0x97, 0xE8, 0xD1, 0xE9, + 0x4D, 0x37, 0xA5, 0x75, 0x5E, 0x83, 0x9E, 0xAB, 0x82, 0x9D, 0xB9, 0x1C, 0xE0, 0xCD, 0x49, 0x89, + 0x01, 0xB6, 0xBD, 0x58, 0x24, 0xA2, 0x5F, 0x38, 0x78, 0x99, 0x15, 0x90, 0x50, 0xB8, 0x95, 0xE4, + 0xD0, 0x91, 0xC7, 0xCE, 0xED, 0x0F, 0xB4, 0x6F, 0xA0, 0xCC, 0xF0, 0x02, 0x4A, 0x79, 0xC3, 0xDE, + 0xA3, 0xEF, 0xEA, 0x51, 0xE6, 0x6B, 0x18, 0xEC, 0x1B, 0x2C, 0x80, 0xF7, 0x74, 0xE7, 0xFF, 0x21, + 0x5A, 0x6A, 0x54, 0x1E, 0x41, 0x31, 0x92, 0x35, 0xC4, 0x33, 0x07, 0x0A, 0xBA, 0x7E, 0x0E, 0x34, + 0x88, 0xB1, 0x98, 0x7C, 0xF3, 0x3D, 0x60, 0x6C, 0x7B, 0xCA, 0xD3, 0x1F, 0x32, 0x65, 0x04, 0x28, + 0x64, 0xBE, 0x85, 0x9B, 0x2F, 0x59, 0x8A, 0xD7, 0xB0, 0x25, 0xAC, 0xAF, 0x12, 0x03, 0xE2, 0xF2, + }, +}; + +const uint16 ZUC_D[16] = { + 042327, 023274, 061153, 011536, + 053611, 032742, 070465, 004657, + 046570, 027423, 065704, 015361, + 057046, 036115, 074232, 043654, +}; + +#define ZUC_S0(x) ZUC_S[0][x] +#define ZUC_S1(x) ZUC_S[1][x] +#define ZUC_S_BOX(X) \ + CONCAT_8x4( \ + ZUC_S0(EXTRACT_8_32(X, 0)), \ + ZUC_S1(EXTRACT_8_32(X, 1)), \ + ZUC_S0(EXTRACT_8_32(X, 2)), \ + ZUC_S1(EXTRACT_8_32(X, 3)) \ + ) + +/* S_BOX END */ + +/* MOD_OPERATIONS BEGIN */ + +#define ZUC_MOD_NUM 0x7FFFFFFF +uint32 zuc_add(uint32 a, uint32 b) { + uint32 c = a + b; + return (c & ZUC_MOD_NUM) + (c >> 31); +} +uint32 zuc_mul(uint32 a, uint32 b) { + uint32 res = 0; + for (int i = 0; i < 31; ++i) { + if (b & (1 << i)) { + res = zuc_add(res, ROTATE_31(a, i)); + } + } + return res; +} +uint32 zuc_mul_powof2(uint32 a, uint32 log2b) { + return ROTATE_31(a, log2b); +} + +/* MOD_OPERATIONS END */ + +/* LFSR BEGIN */ + +typedef struct ZUC_LFSR_st { + uint32 S[16]; /* 31-bit */ +} ZUC_LFSR; + +void zuc_lfsr_init(ZUC_LFSR *lfsr, uint32 u) { /* u: 31-bit */ + uint32 v = zuc_add( + zuc_add( + zuc_add( + zuc_mul_powof2(lfsr->S[15], 15), + zuc_mul_powof2(lfsr->S[13], 17) + ), + zuc_add( + zuc_mul_powof2(lfsr->S[10], 21), + zuc_mul_powof2(lfsr->S[4], 20) + ) + ), + zuc_add(lfsr->S[0], zuc_mul_powof2(lfsr->S[0], 8)) + ); + uint32 S16 = zuc_add(v, u); + S16 = zuc_add(S16, ZUC_MOD_NUM); + for (int i = 0; i < 15; ++i) { + lfsr->S[i] = lfsr->S[i + 1]; + } + lfsr->S[15] = S16; +} +void zuc_lfsr_work(ZUC_LFSR *lfsr) { + uint32 S16 = zuc_add( + zuc_add( + zuc_add( + zuc_mul_powof2(lfsr->S[15], 15), + zuc_mul_powof2(lfsr->S[13], 17) + ), + zuc_add( + zuc_mul_powof2(lfsr->S[10], 21), + zuc_mul_powof2(lfsr->S[4], 20) + ) + ), + zuc_add(lfsr->S[0], zuc_mul_powof2(lfsr->S[0], 8)) + ); + S16 = zuc_add(S16, ZUC_MOD_NUM); + for (int i = 0; i < 15; ++i) { + lfsr->S[i] = lfsr->S[i + 1]; + } + lfsr->S[15] = S16; +} + +/* LFSR END */ + +/* BR BEGIN */ + +typedef struct ZUC_BR_st { + uint32 X[4]; /* 32-bit */ +} ZUC_BR_CTX; + +void zuc_br(ZUC_BR_CTX *br, ZUC_LFSR *lfsr) { + br->X[0] = CONCAT_16x2(EXTRACT_16H_31(lfsr->S[15]), EXTRACT_16L_31(lfsr->S[14])); + br->X[1] = CONCAT_16x2(EXTRACT_16L_31(lfsr->S[11]), EXTRACT_16H_31(lfsr->S[9])); + br->X[2] = CONCAT_16x2(EXTRACT_16L_31(lfsr->S[7]), EXTRACT_16H_31(lfsr->S[5])); + br->X[3] = CONCAT_16x2(EXTRACT_16L_31(lfsr->S[2]), EXTRACT_16H_31(lfsr->S[0])); +} + +/* BR END */ + +/* F BEGIN */ + +#define ZUC_L1(X) \ + ((X) ^ ROTATE((X), 2) ^ ROTATE((X), 10) ^ ROTATE((X), 18) ^ ROTATE((X), 24)) +#define ZUC_L2(X) \ + ((X) ^ ROTATE((X), 8) ^ ROTATE((X), 14) ^ ROTATE((X), 22) ^ ROTATE((X), 30)) + +typedef struct ZUC_F_st { + uint32 R1, R2; /* 32-bit */ +} ZUC_F_CTX; + +uint32 zuc_f(ZUC_F_CTX *f, ZUC_BR_CTX *br) { /* X: 32-bit */ + uint32 X0 = br->X[0], X1 = br->X[1], X2 = br->X[2]; + uint32 R1 = f->R1, R2 = f->R2; + uint32 W = (X0 ^ R1) + R2; + uint32 W1 = R1 + X1; + uint32 W2 = R2 ^ X2; + R1 = ZUC_S_BOX(ZUC_L1(CONCAT_16x2( + EXTRACT_16_32(W1, 1), + EXTRACT_16_32(W2, 0) + ))); + R2 = ZUC_S_BOX(ZUC_L2(CONCAT_16x2( + EXTRACT_16_32(W2, 1), + EXTRACT_16_32(W1, 0) + ))); + f->R1 = R1; + f->R2 = R2; + return W; +} + +/* F END */ + +/* ZUC_MAIN BEGIN */ + +#define ZUC_INIT_CONCAT(k, d, iv) (((uint32)(k) << 15 | (d)) << 8 | (iv)) +#define ZUC_INTEGRITY_CONCAT(K1, K2, offset) \ + (((K1) << (offset)) | ((offset) ? (K2) >> (32 - (offset)) : 0)) + +typedef struct ZUCstate_st { + ZUC_LFSR lfsr; + ZUC_BR_CTX br; + ZUC_F_CTX f; +} ZUC_CTX; + +/* Initiate phase. Both k and iv are uint8 arrays of length 16. */ +int zuc_init(ZUC_CTX *z, uint8 k[], uint8 iv[]) { + memset(z, 0, sizeof(*z)); + for (int i = 0; i < 16; ++i) { + z->lfsr.S[i] = ZUC_INIT_CONCAT(k[i], ZUC_D[i], iv[i]); + } + for (int i = 0; i < 32; ++i) { + zuc_br(&z->br, &z->lfsr); + uint32 W = zuc_f(&z->f, &z->br); + zuc_lfsr_init(&z->lfsr, W >> 1); + } + return 1; +} + +/* Work phase. Z is uint32 array that stores output keys. */ +/* len is the length of the key in the unit of 32-bit. */ +int zuc_work(ZUC_CTX *z, uint32 Z[], size_t len) { + zuc_br(&z->br, &z->lfsr); + (void)zuc_f(&z->f, &z->br); + zuc_lfsr_work(&z->lfsr); + for (size_t i = 0; i < len; ++i) { + zuc_br(&z->br, &z->lfsr); + Z[i] = zuc_f(&z->f, &z->br) ^ z->br.X[3]; + zuc_lfsr_work(&z->lfsr); + } + return 1; +} + +/* Generate 32-bit ZUC key stream. Use the array Z[] to receive the output. */ +int zuc_final(uint8 k[], uint8 iv[], uint32 Z[], size_t len) { + ZUC_CTX res, *z; + z = &res; + zuc_init(z, k, iv); + zuc_work(z, Z, len); + return 1; +} + +/* Use the array obs[] to receive the output. */ +int zuc_confidentiality(uint32 count, uint8 bearer, uint8 direction, uint8 ck[], + uint32 length, uint32 ibs[], uint32 obs[]) { + /* + * @param COUNT (length = 32b, counter) + * @param BEARER (length = 5b, bearer identifier) + * @param DIRECTION (length = 1b, direction identifier) + * @param CK (length = 128b = 8b * 16, confidential key) + * @param LENGTH (length = 32b, bit length of input bit stream) + * @param IBS (length = LENGTH b = 32b * L, input bit stream) + * @param OBS (length = LENGTH b = 32b * L, output bit stream) + */ + + /* Initiate k and iv for ZUC. */ + bearer &= 0x1F; direction &= 1; + uint8 _k[16], _iv[16], _count[4]; + memcpy(_k, ck, sizeof _k); + for (int i = 0; i < 4; ++i) { + _count[i] = EXTRACT_8_32(count, i); + } + _iv[0] = _count[0]; _iv[1] = _count[1]; _iv[2] = _count[2]; _iv[3] = _count[3]; + _iv[4] = (uint8)((bearer << 1 | direction) << 2); _iv[5] = _iv[6] = _iv[7] = 0; + for (int i = 8; i < 16; ++i) { + _iv[i] = _iv[i ^ 8]; + } + size_t L = (length >> 5) + ((length & 0x1F) ? 1 : 0); + uint32 Z[L]; + zuc_final(_k, _iv, Z, L); + for (size_t i = 0; i < L; ++i) { + obs[i] = ibs[i] ^ Z[i]; + } + if (length & 0x1F) { + obs[L - 1] &= PREFIX_MASK_32[length & 0x1F]; + } + return 1; +} + +uint32 zuc_integrity(uint32 count, uint8 bearer, uint8 direction, uint8 ik[], + uint32 length, uint32 M[]) { + /* + * @param COUNT (length = 32b, counter) + * @param BEARER (length = 5b, bearer identifier) + * @param DIRECTION (length = 1b, direction identifier) + * @param IK (length = 128b = 8b * 16, integrity key) + * @param LENGTH (length = 32b, bit length of input bit stream) + * @param M (length = LENGTH b = 32b * L, input massage stream) + * @return MAC (length = 32b, massage verification code) + */ + + /* Initiate k and iv for ZUC. */ + bearer &= 0x1F; direction &= 1; + uint8 _k[16], _iv[16], _count[4]; + memcpy(_k, ik, sizeof _k); + for (int i = 0; i < 4; ++i) { + _count[i] = EXTRACT_8_32(count, i); + } + _iv[0] = _count[0]; _iv[1] = _count[1]; _iv[2] = _count[2]; _iv[3] = _count[3]; + _iv[4] = (uint8)(bearer << 3); _iv[5] = _iv[6] = _iv[7] = 0; + _iv[8] = _iv[0] ^ (uint8)(direction << 7); + _iv[9] = _iv[1]; _iv[10] = _iv[2]; _iv[11] = _iv[3]; + _iv[12] = _iv[4]; _iv[13] = _iv[5]; + _iv[14] = _iv[6] ^ (uint8)(direction << 7); + _iv[15] = _iv[7]; + size_t L = (length >> 5) + ((length & 0x1F) ? 1 : 0) + 2; + uint32 Z[L]; + uint32 T = 0; + zuc_final(_k, _iv, Z, L); + for (size_t i = 0; i < length; ++i) { + if (GET_BIT_VALUE_32(M[i >> 5], i & 0x1F)) { + T = T ^ ZUC_INTEGRITY_CONCAT(Z[i >> 5], Z[(i >> 5) + 1], i & 0x1F); + } + } + T = T ^ ZUC_INTEGRITY_CONCAT(Z[length >> 5], Z[(length >> 5) + 1], length & 0x1F); + T = T ^ Z[L - 1]; + return T; +} + +/* ZUC_MAIN END */ + +#endif \ No newline at end of file diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/enclave/zuctest1.c" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/enclave/zuctest1.c" new file mode 100644 index 0000000000000000000000000000000000000000..c1cb8ebddc8bbc995ed54d8b6672a640fdefd761 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/enclave/zuctest1.c" @@ -0,0 +1,58 @@ +#define SECGEAR_ZUC_DEBUG +#include +#include "zuc.h" + +uint8 k_test1[16] = { + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, +}; +uint8 iv_test1[16] = { + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, +}; + +uint8 k_test2[16] = { + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, +}; +uint8 iv_test2[16] = { + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, +}; + +uint8 k_test3[16] = { + 0x3D, 0x4C, 0x4B, 0xE9, + 0x6A, 0x82, 0xFD, 0xAE, + 0xB5, 0x8F, 0x64, 0x1D, + 0xB1, 0x7B, 0x45, 0x5B, +}; +uint8 iv_test3[16] = { + 0x84, 0x31, 0x9A, 0xA8, + 0xDE, 0x69, 0x15, 0xCA, + 0x1F, 0x6B, 0xDA, 0x6B, + 0xFB, 0xD8, 0xC7, 0x66, +}; + +void zuc_test(uint8 k[], uint8 iv[]) { + const size_t LEN = 32; + uint32 keys[LEN]; + zuc_final(k, iv, keys, LEN); +} + +int main(){ + printf("Test 1: \n"); + zuc_test(k_test1, iv_test1); + printf("Test 2: \n"); + zuc_test(k_test2, iv_test2); + printf("Test 3: \n"); + zuc_test(k_test3, iv_test3); + return 0; +} \ No newline at end of file diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/enclave/zuctest2.c" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/enclave/zuctest2.c" new file mode 100644 index 0000000000000000000000000000000000000000..2db840499c0fcca631695764ad081c74b731c725 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/enclave/zuctest2.c" @@ -0,0 +1,78 @@ +#define SECGEAR_ZUC_DEBUG_2 +#include +#include "zuc.h" + +uint8 ck_test1[16] = { + 0x17, 0x3d, 0x14, 0xba, + 0x50, 0x03, 0x73, 0x1d, + 0x7a, 0x60, 0x04, 0x94, + 0x70, 0xf0, 0x0a, 0x29, +}; +uint32 count_test1 = 0x66035492; +uint8 bearer_test1 = 0xf; +uint8 dirc_test1 = 0x0; +uint32 len_test1 = 0xc1; +uint32 ibs_test1[] = { + 0x6cf65340, 0x735552ab, 0x0c9752fa, 0x6f9025fe, 0x0bd675d9, + 0x005875b2, 0x00000000, +}; + +uint8 ck_test2[16] = { + 0xe5, 0xbd, 0x3e, 0xa0, + 0xeb, 0x55, 0xad, 0xe8, + 0x66, 0xc6, 0xac, 0x58, + 0xbd, 0x54, 0x30, 0x2a, +}; +uint32 count_test2 = 0x56823; +uint8 bearer_test2 = 0x18; +uint8 dirc_test2 = 0x1; +uint32 len_test2 = 0x320; +uint32 ibs_test2[] = { + 0x14a8ef69, 0x3d678507, 0xbbe7270a, 0x7f67ff50, 0x06c3525b, + 0x9807e467, 0xc4e56000, 0xba338f5d, 0x42955903, 0x67518222, + 0x46c80d3b, 0x38f07f4b, 0xe2d8ff58, 0x05f51322, 0x29bde93b, + 0xbbdcaf38, 0x2bf1ee97, 0x3fbf9977, 0xbada8945, 0x847a2a6c, + 0x9ad34a66, 0x7554e04d, 0x1f7fa2c3, 0x3241bd8f, 0x01ba220d, +}; + +uint8 ck_test3[16] = { + 0xe1, 0x3f, 0xed, 0x21, + 0xb4, 0x6e, 0x4e, 0x7e, + 0xc3, 0x12, 0x53, 0xb2, + 0xbb, 0x17, 0xb3, 0xe0, +}; +uint32 count_test3 = 0x2738cdaa; +uint8 bearer_test3 = 0x1a; +uint8 dirc_test3 = 0x0; +uint32 len_test3 = 0xfb3; +uint32 ibs_test3[] = { + 0x8d74e20d, 0x54894e06, 0xd3cb13cb, 0x3933065e, 0x8674be62, 0xadb1c72b, 0x3a646965, 0xab63cb7b, 0x7854dfdc, 0x27e84929, + 0xf49c64b8, 0x72a490b1, 0x3f957b64, 0x827e71f4, 0x1fbd4269, 0xa42c97f8, 0x24537027, 0xf86e9f4a, 0xd82d1df4, 0x51690fdd, + 0x98b6d03f, 0x3a0ebe3a, 0x312d6b84, 0x0ba5a182, 0x0b2a2c97, 0x09c090d2, 0x45ed267c, 0xf845ae41, 0xfa975d33, 0x33ac3009, + 0xfd40eba9, 0xeb5b8857, 0x14b768b6, 0x97138baf, 0x21380eca, 0x49f644d4, 0x8689e421, 0x5760b906, 0x739f0d2b, 0x3f091133, + 0xca15d981, 0xcbe401ba, 0xf72d05ac, 0xe05cccb2, 0xd297f4ef, 0x6a5f58d9, 0x1246cfa7, 0x7215b892, 0xab441d52, 0x78452795, + 0xccb7f5d7, 0x9057a1c4, 0xf77f80d4, 0x6db2033c, 0xb79bedf8, 0xe60551ce, 0x10c667f6, 0x2a97abaf, 0xabbcd677, 0x2018df96, + 0xa282ea73, 0x7ce2cb33, 0x1211f60d, 0x5354ce78, 0xf9918d9c, 0x206ca042, 0xc9b62387, 0xdd709604, 0xa50af16d, 0x8d35a890, + 0x6be484cf, 0x2e74a928, 0x99403643, 0x53249b27, 0xb4c9ae29, 0xeddfc7da, 0x6418791a, 0x4e7baa06, 0x60fa6451, 0x1f2d685c, + 0xc3a5ff70, 0xe0d2b742, 0x92e3b8a0, 0xcd6b04b1, 0xc790b8ea, 0xd2703708, 0x540dea2f, 0xc09c3da7, 0x70f65449, 0xe84d817a, + 0x4f551055, 0xe19ab850, 0x18a0028b, 0x71a144d9, 0x6791e9a3, 0x57793350, 0x4eee0060, 0x340c69d2, 0x74e1bf9d, 0x805dcbcc, + 0x1a6faa97, 0x6800b6ff, 0x2b671dc4, 0x63652fa8, 0xa33ee509, 0x74c1c21b, 0xe01eabb2, 0x16743026, 0x9d72ee51, 0x1c9dde30, + 0x797c9a25, 0xd86ce74f, 0x5b961be5, 0xfdfb6807, 0x814039e7, 0x137636bd, 0x1d7fa9e0, 0x9efd2007, 0x505906a5, 0xac45dfde, + 0xed7757bb, 0xee745749, 0xc2963335, 0x0bee0ea6, 0xf409df45, 0x80160000, +}; + +void zuc_test2(uint32 count, uint8 bearer, uint8 direction, uint8 ck[], + uint32 length, uint32 ibs[], uint32 obs[]) { + zuc_confidentiality(count, bearer, direction, ck, length, ibs, obs); +} + +int main(){ + uint32 obs_test[130]; + printf("Test 1: \n"); + zuc_test2(count_test1, bearer_test1, dirc_test1, ck_test1, len_test1, ibs_test1, obs_test); + printf("Test 2: \n"); + zuc_test2(count_test2, bearer_test2, dirc_test2, ck_test2, len_test2, ibs_test2, obs_test); + printf("Test 3: \n"); + zuc_test2(count_test3, bearer_test3, dirc_test3, ck_test3, len_test3, ibs_test3, obs_test); + return 0; +} \ No newline at end of file diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/enclave/zuctest3.c" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/enclave/zuctest3.c" new file mode 100644 index 0000000000000000000000000000000000000000..7a09e8cebc73961c9b9286dafe12fb35b6b8b61c --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/enclave/zuctest3.c" @@ -0,0 +1,81 @@ +#define SECGEAR_ZUC_DEBUG_3 +#include +#include "zuc.h" + +uint8 ik_test1[16] = { + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, +}; +uint32 count_test1 = 0; +uint8 bearer_test1 = 0; +uint8 dirc_test1 = 0; +uint32 len_test1 = 1; +uint32 m_test1[] = { + 0x00000000, +}; + +uint8 ik_test2[16] = { + 0xc9, 0xe6, 0xce, 0xc4, + 0x60, 0x7c, 0x72, 0xdb, + 0x00, 0x0a, 0xef, 0xa8, + 0x83, 0x85, 0xab, 0x0a, +}; +uint32 count_test2 = 0xa94059da; +uint8 bearer_test2 = 0xa; +uint8 dirc_test2 = 0x1; +uint32 len_test2 = 0x241; +uint32 m_test2[] = { + 0x983b41d4, 0x7d780c9e, 0x1ad11d7e, 0xb70391b1, 0xde0b35da, 0x2dc62f83, 0xe7b78d63, 0x06ca0ea0, 0x7e941b7b, + 0xe91348f9, 0xfcb170e2, 0x217fecd9, 0x7f9f68ad, 0xb16e5d7d, 0x21e569d2, 0x80ed775c, 0xebde3f40, 0x93c53881, + 0x00000000, +}; + +uint8 ik_test3[16] = { + 0x6b, 0x8b, 0x08, 0xee, + 0x79, 0xe0, 0xb5, 0x98, + 0x2d, 0x6d, 0x12, 0x8e, + 0xa9, 0xf2, 0x20, 0xcb, +}; +uint32 count_test3 = 0x561eb2dd; +uint8 bearer_test3 = 0x1c; +uint8 dirc_test3 = 0x0; +uint32 len_test3 = 0x1626; +uint32 m_test3[] = { + 0x5bad7247, 0x10ba1c56, 0xd5a315f8, 0xd40f6e09, 0x3780be8e, 0x8de07b69, 0x92432018, 0xe08ed96a, 0x5734af8b, + 0xad8a575d, 0x3a1f162f, 0x85045cc7, 0x70925571, 0xd9f5b94e, 0x454a77c1, 0x6e72936b, 0xf016ae15, 0x7499f054, + 0x3b5d52ca, 0xa6dbeab6, 0x97d2bb73, 0xe41b8075, 0xdce79b4b, 0x86044f66, 0x1d4485a5, 0x43dd7860, 0x6e0419e8, + 0x059859d3, 0xcb2b67ce, 0x0977603f, 0x81ff839e, 0x33185954, 0x4cfbc8d0, 0x0fef1a4c, 0x8510fb54, 0x7d6b06c6, + 0x11ef44f1, 0xbce107cf, 0xa45a06aa, 0xb360152b, 0x28dc1ebe, 0x6f7fe09b, 0x0516f9a5, 0xb02a1bd8, 0x4bb0181e, + 0x2e89e19b, 0xd8125930, 0xd178682f, 0x3862dc51, 0xb636f04e, 0x720c47c3, 0xce51ad70, 0xd94b9b22, 0x55fbae90, + 0x6549f499, 0xf8c6d399, 0x47ed5e5d, 0xf8e2def1, 0x13253e7b, 0x08d0a76b, 0x6bfc68c8, 0x12f375c7, 0x9b8fe5fd, + 0x85976aa6, 0xd46b4a23, 0x39d8ae51, 0x47f680fb, 0xe70f978b, 0x38effd7b, 0x2f7866a2, 0x2554e193, 0xa94e98a6, + 0x8b74bd25, 0xbb2b3f5f, 0xb0a5fd59, 0x887f9ab6, 0x8159b717, 0x8d5b7b67, 0x7cb546bf, 0x41eadca2, 0x16fc1085, + 0x0128f8bd, 0xef5c8d89, 0xf96afa4f, 0xa8b54885, 0x565ed838, 0xa950fee5, 0xf1c3b0a4, 0xf6fb71e5, 0x4dfd169e, + 0x82cecc72, 0x66c850e6, 0x7c5ef0ba, 0x960f5214, 0x060e71eb, 0x172a75fc, 0x1486835c, 0xbea65344, 0x65b055c9, + 0x6a72e410, 0x52241823, 0x25d83041, 0x4b40214d, 0xaa8091d2, 0xe0fb010a, 0xe15c6de9, 0x0850973b, 0xdf1e423b, + 0xe148a237, 0xb87a0c9f, 0x34d4b476, 0x05b803d7, 0x43a86a90, 0x399a4af3, 0x96d3a120, 0x0a62f3d9, 0x507962e8, + 0xe5bee6d3, 0xda2bb3f7, 0x237664ac, 0x7a292823, 0x900bc635, 0x03b29e80, 0xd63f6067, 0xbf8e1716, 0xac25beba, + 0x350deb62, 0xa99fe031, 0x85eb4f69, 0x937ecd38, 0x7941fda5, 0x44ba67db, 0x09117749, 0x38b01827, 0xbcc69c92, + 0xb3f772a9, 0xd2859ef0, 0x03398b1f, 0x6bbad7b5, 0x74f7989a, 0x1d10b2df, 0x798e0dbf, 0x30d65874, 0x64d24878, + 0xcd00c0ea, 0xee8a1a0c, 0xc753a279, 0x79e11b41, 0xdb1de3d5, 0x038afaf4, 0x9f5c682c, 0x3748d8a3, 0xa9ec54e6, + 0xa371275f, 0x1683510f, 0x8e4f9093, 0x8f9ab6e1, 0x34c2cfdf, 0x4841cba8, 0x8e0cff2b, 0x0bcc8e6a, 0xdcb71109, + 0xb5198fec, 0xf1bb7e5c, 0x531aca50, 0xa56a8a3b, 0x6de59862, 0xd41fa113, 0xd9cd9578, 0x08f08571, 0xd9a4bb79, + 0x2af271f6, 0xcc6dbb8d, 0xc7ec36e3, 0x6be1ed30, 0x8164c31c, 0x7c0afc54, 0x1c000000, +}; + +uint32 zuc_test3(uint32 count, uint8 bearer, uint8 direction, uint8 ik[], + uint32 length, uint32 M[]) { + return zuc_integrity(count, bearer, direction, ik, length, M); +} + +int main(){ + printf("Test 1: \n"); + zuc_test3(count_test1, bearer_test1, dirc_test1, ik_test1, len_test1, m_test1); + printf("Test 2: \n"); + zuc_test3(count_test2, bearer_test2, dirc_test2, ik_test2, len_test2, m_test2); + printf("Test 3: \n"); + zuc_test3(count_test3, bearer_test3, dirc_test3, ik_test3, len_test3, m_test3); + return 0; +} \ No newline at end of file diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/enclave_ZUC.edl" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/enclave_ZUC.edl" new file mode 100644 index 0000000000000000000000000000000000000000..0e454ee2a21540888c71f4ad7beca325d45c15ad --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/enclave_ZUC.edl" @@ -0,0 +1,27 @@ +enclave { + include "secgear_urts.h" + from "secgear_tstdc.edl" import *; + + trusted { + public void setRSAsource([in, size=len] char* src, size_t len); + public void getRSAresult([out, size=len] char* buf, size_t len); + public void calcEmbeddingResult(); + public void initKeys([in] uint32_t *src); + public void registerSeed([in] uint32_t *src); + public void setKeys([in,count=2] uint32_t *K); + public void getKeys([out,count=2] uint32_t *K); + public void setD([in, size=len] char* src, size_t len); + public void setCD([in, size=len] char* src, size_t len); + public void setN([in, size=len] char* src, size_t len); + public void getCD([out, size=len] char* buf, size_t len); + public void getX([out, size=len] char* buf, size_t len); + public void getMu([out, size=len] char* buf, size_t len); + public void getK([out, size=len] char* buf, size_t len); + public void getNWID([out, size=len] char* buf, size_t len); + public void setNPW([in, size=len] char* src, size_t len); + }; + + untrusted { + + }; +}; diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/host/CMakeLists.txt" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/host/CMakeLists.txt" new file mode 100644 index 0000000000000000000000000000000000000000..1330657eb38dcf372c8ffde55188b18bb72a1180 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/host/CMakeLists.txt" @@ -0,0 +1,88 @@ +# Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved. +# secGear is licensed under the Mulan PSL v2. +# You can use this software according to the terms and conditions of the Mulan PSL v2. +# You may obtain a copy of Mulan PSL v2 at: +# http://license.coscl.org.cn/MulanPSL2 +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR +# PURPOSE. +# See the Mulan PSL v2 for more details. + +#set auto code prefix +set(PREFIX enclave_ZUC) +#set host exec name +set(OUTPUT secgear_enclave_ZUC) +#set host src code +set(SOURCE_FILE ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp) + +set(CMAKE_CXX_STANDARD 11) + +#set auto code +if(CC_GP) + set(AUTO_FILES ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_u.h ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_u.c ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_args.h) + add_custom_command(OUTPUT ${AUTO_FILES} + DEPENDS ${CURRENT_ROOT_PATH}/${EDL_FILE} + COMMAND ${CODEGEN} --${CODETYPE} --untrusted ${CURRENT_ROOT_PATH}/${EDL_FILE} --search-path ${LOCAL_ROOT_PATH}/inc/host_inc/gp) +endif() + +if(CC_SGX) + set(AUTO_FILES ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_u.h ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_u.c) + add_custom_command(OUTPUT ${AUTO_FILES} + DEPENDS ${CURRENT_ROOT_PATH}/${EDL_FILE} + COMMAND ${CODEGEN} --${CODETYPE} --untrusted ${CURRENT_ROOT_PATH}/${EDL_FILE} --search-path ${LOCAL_ROOT_PATH}/inc/host_inc/sgx --search-path ${SGXSDK}/include) +endif() + +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIE") +set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -O2 -fPIE") +set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -s") + +if(CC_GP) + if(${CMAKE_VERSION} VERSION_LESS "3.13.0") + link_directories(${SECGEAR_INSTALL_PATH}) + endif() + add_executable(${OUTPUT} ${SOURCE_FILE} ${AUTO_FILES}) + target_include_directories(${OUTPUT} PRIVATE + ${LOCAL_ROOT_PATH}/inc/host_inc + ${LOCAL_ROOT_PATH}/inc/host_inc/gp + ${CMAKE_CURRENT_BINARY_DIR}) + if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.13.0") + target_link_directories(${OUTPUT} PRIVATE ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}) + endif() +endif() + +if(CC_SGX) + if(${CMAKE_VERSION} VERSION_LESS "3.13.0") + link_directories(${CMAKE_LIBRARY_OUTPUT_DIRECTORY}) + endif() + add_executable(${OUTPUT} ${SOURCE_FILE} ${AUTO_FILES}) + target_include_directories(${OUTPUT} PRIVATE + ${LOCAL_ROOT_PATH}/inc/host_inc + ${LOCAL_ROOT_PATH}/inc/host_inc/sgx + ${CMAKE_CURRENT_BINARY_DIR}) + if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.13.0") + target_link_directories(${OUTPUT} PRIVATE ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}) + endif() +endif() + +if(CC_SIM) + target_link_libraries(${OUTPUT} secgearsim) +else() + target_link_libraries(${OUTPUT} secgear) +endif() +set_target_properties(${OUTPUT} PROPERTIES SKIP_BUILD_RPATH TRUE) + +if(CC_GP) + #itrustee install whitelist /vender/bin/teec_hello + install(TARGETS ${OUTPUT} + RUNTIME + DESTINATION /vendor/bin/ + PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ) +endif() + +if(CC_SGX) + install(TARGETS ${OUTPUT} + RUNTIME + DESTINATION ${CMAKE_BINARY_DIR}/bin/ + PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ) +endif() + diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/host/main.cpp" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/host/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..9add3a7ec8d756fad4de2f60aa79049bd1daf2c0 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/host/main.cpp" @@ -0,0 +1,27 @@ +#include"enclave.h" +#include"Enclave1_u.h" +#include +#include + +#define MAX_LEN 1024 + +using namespace std; + +int main(int argc, char* argv[]) { + cc_enclave_t *eid; + cc_enclave_result_t ret; + char *path = transferFromString(PATH); + + ret = cc_enclave_create(path, AUTO_ENCLAVE_TYPE, 0, SECGEAR_DEBUG_FLAG, NULL, 0, &eid); + if (ret != CC_SUCCESS) { + printf("ERR: %#x ,failed to create enclave.\n", ret); + return -1; + } + + + + if (CC_SUCCESS != cc_enclave_destroy(eid)) { + return -1; + } + return 0; +} diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/include/common_utils.h" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/include/common_utils.h" new file mode 100644 index 0000000000000000000000000000000000000000..9251edce48cf8aeb25404fcf5391335698a83a06 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/include/common_utils.h" @@ -0,0 +1,69 @@ +#ifndef SECGEAR_SM_COMMON_UTILS +#define SECGEAR_SM_COMMON_UTILS + +#include +#define debugPrint (printf("%s: %s: %d\n",__FILE__,__FUNCTION__,__LINE__),fflush(stdout),1) + +typedef __uint8_t uint8; +typedef __uint16_t uint16; +typedef __uint32_t uint32; +typedef __uint64_t uint64; + +#define CONCAT_8x4(x0, x1, x2, x3) ((((uint32)(x0) << 8 | (x1)) << 8 | (x2)) << 8 | (x3)) // X = (x0 || x1 || x2 || x3) +#define CONCAT_16x2(x0, x1) ((uint32)(x0) << 16 | (x1)) // X = (x0 || x1) +#define EXTRACT_8_32(X, i) (((X) >> (((i) ^ 3) << 3)) & 0xFF) // X = (x0 || x1 || x2 || x3) +#define EXTRACT_16_32(X, i) (((X) >> (((i) ^ 1) << 4)) & 0xFFFF) // X = (x0 || x1) +#define EXTRACT_16H_31(X) (((X) >> 15) & 0xFFFF) // Higher 16-bit from 31-bit +#define EXTRACT_16L_31(X) ((X) & 0xFFFF) // Lower 16-bit from 31-bit + +#define ROTATE(a, n) (((a) << (n)) | (((a) & 0xFFFFFFFF) >> ((32 - (n)) & 31))) +#define ROTATE_31(a, n) ((((a) << (n)) & 0x7FFFFFFF) | (((a) & 0x7FFFFFFF) >> (31 - (n)))) + +#define GET_BIT_VALUE_32(x, i) (((x) >> (31 - (i))) & 1) + +const uint32 PREFIX_MASK_32[32] = { + 0x00000000, + 0x80000000, + 0xC0000000, + 0xE0000000, + 0xF0000000, + 0xF8000000, + 0xFC000000, + 0xFE000000, + 0xFF000000, + 0xFF800000, + 0xFFC00000, + 0xFFE00000, + 0xFFF00000, + 0xFFF80000, + 0xFFFC0000, + 0xFFFE0000, + 0xFFFF0000, + 0xFFFF8000, + 0xFFFFC000, + 0xFFFFE000, + 0xFFFFF000, + 0xFFFFF800, + 0xFFFFFC00, + 0xFFFFFE00, + 0xFFFFFF00, + 0xFFFFFF80, + 0xFFFFFFC0, + 0xFFFFFFE0, + 0xFFFFFFF0, + 0xFFFFFFF8, + 0xFFFFFFFC, + 0xFFFFFFFE +}; + +# define HOST_c2l(c,l) (l =(((unsigned long)(*((c)++)))<<24), \ + l|=(((unsigned long)(*((c)++)))<<16), \ + l|=(((unsigned long)(*((c)++)))<< 8), \ + l|=(((unsigned long)(*((c)++))) ) ) +# define HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l)>>24)&0xff), \ + *((c)++)=(unsigned char)(((l)>>16)&0xff), \ + *((c)++)=(unsigned char)(((l)>> 8)&0xff), \ + *((c)++)=(unsigned char)(((l) )&0xff), \ + l) + +#endif \ No newline at end of file diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/include/ec.h" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/include/ec.h" new file mode 100644 index 0000000000000000000000000000000000000000..56adbe751f17342f84c17e3736f8806deb7f40b5 --- /dev/null +++ "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/Informal/enclave_ZUC/include/ec.h" @@ -0,0 +1,545 @@ +#include /* BigNum */ +#include "common_utils.h" + +typedef struct ec_point_st { + BIGNUM *X; + BIGNUM *Y; + int is_infinity; +} EC_POINT; + +int EC_POINT_init(EC_POINT *point) { + point->X = BN_new(); + point->Y = BN_new(); + point->is_infinity = 0; + if (point->X == NULL || point->Y == NULL) { + BN_free(point->X); + BN_free(point->Y); + return 0; + } + return 1; +} + +EC_POINT *EC_POINT_new() { + EC_POINT *ret = NULL; + ret = malloc(sizeof(*ret)); + if (ret == NULL) { + return NULL; + } + if (!EC_POINT_init(ret)) { + free(ret); + return NULL; + } + return ret; +} + +int EC_POINT_setXY(EC_POINT *r, const BIGNUM *x, const BIGNUM *y) { + if (r == NULL) { + return 0; + } + if (!BN_copy(r->X, x) || !(BN_copy(r->Y, y))) { + return 0; + } + return 1; +} + +const EC_POINT *EC_point_infinity() { + static const EC_POINT const_infinity = { NULL, NULL, 1 }; + return &const_infinity; +} + +int EC_POINT_is_infinity(const EC_POINT *point) { + return point->is_infinity; +} + +int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src) { + if (dest == NULL) { + return 0; + } + if (EC_POINT_is_infinity(src)) { + dest->is_infinity = 1; + } + else{ + dest->is_infinity = 0; + if (!BN_copy(dest->X, src->X) || !BN_copy(dest->Y, src->Y)) return 0; + } + return 1; +} + +#define NORMAL 0x1 +#define COMPRESS 0x2 +#define MIX 0x4 + +int EC_POINT_ecp2bin(uint8 *out, const EC_POINT *point, int p_bytes, int flag) { + uint8 PC; + uint8 *p = out, *buf; + int i; + buf = malloc(p_bytes); + if (EC_POINT_is_infinity(point)) { + return 0; + } + if (flag == NORMAL) { + PC = 0x04; + *(p++) = PC; + if (!BN_bn2binpad(point->X, buf, p_bytes)) { + return 0; + } + for (i = 0; i < p_bytes; ++i) { + *(p++) = buf[i]; + } + if (!BN_bn2binpad(point->Y, buf, p_bytes)) { + return 0; + } + for (i = 0; i < p_bytes; ++i) { + *(p++) = buf[i]; + } + } + else { + goto err; /* not supported */ + } + return 1; + err: + return 0; +} + +int EC_POINT_bin2ecp(EC_POINT *point, const uint8 *src, int p_bytes) { + if (point == NULL) { + return 0; + } + uint8 PC = *src; + const uint8 *data = src + 1; + uint8 *buf; + int i; + if (PC == 0x04) { + if (!BN_bin2bn(data, p_bytes, point->X) + || !BN_bin2bn(data+p_bytes, p_bytes, point->Y)) { + return 0; + } + } + else { + goto err; /* not supported */ + } + return 1; + err: + return 0; +} + +int EC_POINT_ecp2bin_normal(uint8 *out, const EC_POINT *point, int p_bytes) { + return EC_POINT_ecp2bin(out, point, p_bytes, NORMAL); +} + +// int EC_POINT_ecp2bin_compressed(uint8 *out, const EC_POINT *point, int p_bytes) { +// return EC_POINT_ecp2bin(out, point, p_bytes, COMPRESS); +// } + +// int EC_POINT_ecp2bin_mix(uint8 *out, const EC_POINT *point, int p_bytes) { +// return EC_POINT_ecp2bin(out, point, p_bytes, MIX); +// } + +typedef struct ec_group_st { + EC_POINT *generator; /* point G */ + BIGNUM *order; /* n */ + BIGNUM *cofactor; /* h */ + BIGNUM *field; /* module p */ + BIGNUM *a, *b; /* ECC parameter */ +} EC_GROUP; + +int EC_GROUP_init(EC_GROUP *group) { + group->generator = EC_POINT_new(); + group->order = BN_new(); + group->cofactor = BN_new(); + group->field = BN_new(); + group->a = BN_new(); + group->b = BN_new(); + if (group->generator == NULL || group->b == NULL) { + free(group->generator); + BN_free(group->order); + BN_free(group->cofactor); + BN_free(group->field); + BN_free(group->a); + BN_free(group->b); + return 0; + } + return 1; +} + +EC_GROUP *EC_GROUP_new() { + EC_GROUP *ret = NULL; + ret = malloc(sizeof(*ret)); + if (ret == NULL) { + return NULL; + } + if (!EC_GROUP_init(ret)) { + free(ret); + return NULL; + } + return ret; +} + +int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src) { + if (dest == NULL) { + return 0; + } + if (dest == src) { + return 1; + } + if (!EC_POINT_copy(dest->generator, src->generator) + || !BN_copy(dest->order, src->order) + || !BN_copy(dest->cofactor, src->cofactor) + || !BN_copy(dest->field, src->field) + || !BN_copy(dest->a, src->a) + || !BN_copy(dest->b, src->b)) { + return 0; + } + return 1; +} + +int EC_GROUP_set_param(EC_GROUP *group, const BIGNUM *field, const BIGNUM *a, const BIGNUM *b, const BIGNUM *order, const BIGNUM *cofactor, const EC_POINT *point) { + if (group == NULL) { + return 0; + } + group->field = BN_new(); + group->a = BN_new(); + group->b = BN_new(); + group->order = BN_new(); + group->cofactor = BN_new(); + group->generator = EC_POINT_new(); + if (group->cofactor == NULL + || group->generator == NULL) { + return 0; + } + if (!BN_copy(group->field, field) + || !BN_copy(group->a, a) + || !BN_copy(group->b, b) + || !BN_copy(group->order, order) + || !BN_copy(group->cofactor, cofactor) + || !EC_POINT_copy(group->generator, point)) { + return 0; + } + return 1; +} + +int EC_POINT_double(EC_POINT *r, const EC_POINT *a, const EC_GROUP *group) { + const BIGNUM *p; + BN_CTX *ctx = NULL; + int ret = 0; + BIGNUM *ax, *ay, *lnum, *ldeno; + BIGNUM *tmp, *bn_value_3; + if (r == NULL) { + return 0; + } + if (EC_POINT_is_infinity(a)) { + return EC_POINT_copy(r, EC_point_infinity()); + } + p = group->field; + ctx = BN_CTX_new(); + if (ctx == NULL) { + return 0; + } + BN_CTX_start(ctx); + ax = BN_CTX_get(ctx); + ay = BN_CTX_get(ctx); + lnum = BN_CTX_get(ctx); + ldeno = BN_CTX_get(ctx); + tmp = BN_CTX_get(ctx); + bn_value_3 = BN_CTX_get(ctx); + if (bn_value_3 == NULL) { + goto done; + } + if (!BN_copy(ax, a->X) + || !BN_copy(ay, a->Y)) { + goto done; + } + if (!BN_set_word(bn_value_3, 3) + || !BN_mod_sqr(lnum, ax, p, ctx) + || !BN_mod_mul(lnum, lnum, bn_value_3, p, ctx) + || !BN_mod_add_quick(lnum, lnum, group->a, p) + || !BN_mod_lshift1_quick(ldeno, ay, p) + || !BN_mod_inverse(ldeno, ldeno, p, ctx) + || !BN_mod_mul(lnum, lnum, ldeno, p, ctx)) { + goto done; + } + if (!BN_mod_sqr(r->X, lnum, p, ctx)) { + goto done; + } + if (!BN_mod_lshift1_quick(tmp, ax, p)) { + goto done; + } + if (!BN_mod_sub_quick(r->X, r->X, tmp, p)) { + goto done; + } + if (!BN_mod_sub_quick(r->Y, ax, r->X, p)) { + goto done; + } + if (!BN_mod_mul(r->Y, lnum, r->Y, p, ctx)) { + goto done; + } + if (!BN_mod_sub_quick(r->Y, r->Y, ay, p)) { + goto done; + } + if (r == NULL) { + r = EC_POINT_new(); + if (r == NULL) { + goto done; + } + } + ret = 1; + done: + BN_CTX_end(ctx); + BN_CTX_free(ctx); + return ret; +} + +int EC_POINT_add(EC_POINT *r, const EC_POINT *a, const EC_POINT *b, const EC_GROUP *group) { + const BIGNUM *p; + BN_CTX *ctx = NULL; + int ret = 0; + BIGNUM *ax, *ay, *bx, *by, *lnum, *ldeno; + BIGNUM *tmp, *bn_value_3; + if (r == NULL) { + return 0; + } + if (a == b) { + return EC_POINT_double(r, a, group); + } + if (EC_POINT_is_infinity(a)) { + return EC_POINT_copy(r, b); + } + if (EC_POINT_is_infinity(b)) { + return EC_POINT_copy(r, a); + } + p = group->field; + ctx = BN_CTX_new(); + if (ctx == NULL) { + return 0; + } + BN_CTX_start(ctx); + ax = BN_CTX_get(ctx); + ay = BN_CTX_get(ctx); + bx = BN_CTX_get(ctx); + by = BN_CTX_get(ctx); + lnum = BN_CTX_get(ctx); + ldeno = BN_CTX_get(ctx); + tmp = BN_CTX_get(ctx); + bn_value_3 = BN_CTX_get(ctx); + if (bn_value_3 == NULL) { + goto done; + } + if (!BN_copy(ax, a->X) + || !BN_copy(ay, a->Y) + || !BN_copy(bx, b->X) + || !BN_copy(by, b->Y)) { + goto done; + } + if (BN_cmp(ax, bx) == 0) { + if (!BN_mod_add_quick(tmp, ay, by, p)) { + goto done; + } + if (BN_is_zero(tmp) + || !EC_POINT_copy(r, EC_point_infinity())) { + goto done; + } + if (!BN_set_word(bn_value_3, 3)) { + goto done; + } + if (!BN_mod_sqr(lnum, ax, p, ctx) + || !BN_mod_mul(lnum, lnum, bn_value_3, p, ctx) + || !BN_mod_add_quick(lnum, lnum, group->a, p) + || !BN_mod_lshift1_quick(ldeno, ay, p) + || !BN_mod_inverse(ldeno, ldeno, p, ctx) + || !BN_mod_mul(lnum, lnum, ldeno, p, ctx)) { + goto done; + } + } + else { + if (!BN_mod_sub_quick(lnum, by, ay, p)) { + goto done; + } + if (!BN_mod_sub_quick(ldeno, bx, ax, p)) { + goto done; + } + if (!BN_mod_inverse(ldeno, ldeno, p, ctx)) { + goto done; + } + if (!BN_mod_mul(lnum, lnum, ldeno, p, ctx)) { + goto done; + } + } + if (!BN_mod_sqr(tmp, lnum, p, ctx)) { + goto done; + } + if (!BN_mod_sub_quick(tmp, tmp, ax, p)) { + goto done; + } + if (!BN_mod_sub_quick(r->X, tmp, bx, p)) { + goto done; + } + if (!BN_mod_sub_quick(r->Y, ax, r->X, p)) { + goto done; + } + if (!BN_mod_mul(r->Y, lnum, r->Y, p, ctx)) { + goto done; + } + if (!BN_mod_sub_quick(r->Y, r->Y, ay, p)) { + goto done; + } + if (r == NULL) { + r = EC_POINT_new(); + if (r == NULL) { + goto done; + } + } + ret = 1; + done: + BN_CTX_end(ctx); + BN_CTX_free(ctx); + return ret; +} + +int EC_POINT_mul(EC_POINT *r, const EC_POINT *a, const BIGNUM *b, const EC_GROUP *group) { + if (r == NULL) { + return 0; + } + if (BN_is_negative(b)) { + EC_POINT *_a = EC_POINT_new(); + BIGNUM *_b = BN_new(); + if (!EC_POINT_copy(_a, a) + || !BN_copy(_b, b) + || !BN_mod_sub_quick(_a->Y, group->field, _a->Y, group->field)) { + return 0; + } + BN_set_negative(_b, 0); + return EC_POINT_mul(r, _a, _b, group); + } + if (!EC_POINT_copy(r, EC_point_infinity())){ + return 0; + } + int bits = BN_num_bits(b); + for (int i = bits-1; i >= 0; --i) { + if (!EC_POINT_double(r, r, group)) { + return 0; + } + if (BN_is_bit_set(b, i)) { + if (!EC_POINT_add(r, r, a, group)) { + return 0; + } + } + } + return 1; +} + +/* + * Only for curve y^2 = x^3 + ax + b. + * Please check infinity before calling. + */ +int EC_POINT_on_curve_simple(const EC_POINT *point, const EC_GROUP *group) { + if (point == NULL || group == NULL) { + return 0; + } + if (EC_POINT_is_infinity(point)) { + return 1; + } + int ret = 0; + BN_CTX *ctx; + BIGNUM *p, *a, *b, *x, *y, *lhs, *rhs; + EC_POINT *Nq; + ctx = BN_CTX_new(); + if (ctx == NULL) { + return ret; + } + BN_CTX_start(ctx); + p = group->field; + a = group->a; + b = group->b; + x = point->X; + y = point->Y; + lhs = BN_CTX_get(ctx); + rhs = BN_CTX_get(ctx); + Nq = EC_POINT_new(); + if (p == NULL + || a == NULL + || b == NULL + || x == NULL + || y == NULL + || rhs == NULL + || Nq == NULL) { + return ret; + } + if (!BN_mod_mul(lhs, y, y, p, ctx) + || !BN_mod_mul(rhs, x, x, p, ctx) + || !BN_mod_add_quick(rhs, rhs, a, p) + || !BN_mod_mul(rhs, rhs, x, p, ctx) + || !BN_mod_add_quick(rhs, rhs, b, p) + || !EC_POINT_mul(Nq, point, group->order, group)) { + return ret; + } + if (BN_cmp(lhs, rhs) != 0 + || !EC_POINT_is_infinity(Nq)) { + return ret; + } + ret = 1; + BN_CTX_end(ctx); + BN_CTX_free(ctx); + return ret; +} + +typedef struct ec_key_st { + EC_GROUP *group; + EC_POINT *pub_key; + BIGNUM *priv_key; +} EC_KEY; + +int EC_KEY_init(EC_KEY *key) { + key->group = EC_GROUP_new(); + key->pub_key = EC_POINT_new(); + key->priv_key = BN_new(); + if (key->group == NULL + || key->pub_key == NULL + || key->priv_key == NULL) { + free(key->group); + free(key->pub_key); + BN_free(key->priv_key); + return 0; + } + return 1; +} + +EC_KEY *EC_KEY_new() { + EC_KEY *ret = NULL; + ret = malloc(sizeof(*ret)); + if (ret == NULL) { + return NULL; + } + if (!EC_KEY_init(ret)) { + free(ret); + return NULL; + } + return ret; +} + +int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group) { + if (!EC_GROUP_copy(key->group, group)) { + return 0; + } + return 1; +} + +int EC_KEY_set_priv_key(EC_KEY *key, const BIGNUM *priv_key) { + BIGNUM *tmp; + tmp = BN_dup(priv_key); + if (tmp == NULL) { + return 0; + } + BN_clear_free(key->priv_key); + key->priv_key = tmp; + return 1; +} + +int EC_KEY_generate_pub_key(EC_KEY *key) { + return EC_POINT_mul(key->pub_key, key->group->generator, key->priv_key, key->group); +} + +const BIGNUM *EC_KEY_get_priv_key(const EC_KEY *key) { + return key->priv_key; +} + +int EC_KEY_generate_key() {} diff --git "a/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/\347\216\213\345\260\217\346\272\252-22b970374-\347\273\223\351\241\271\346\212\245\345\221\212.pdf" "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/\347\216\213\345\260\217\346\272\252-22b970374-\347\273\223\351\241\271\346\212\245\345\221\212.pdf" new file mode 100644 index 0000000000000000000000000000000000000000..87cc2a7482a6d2363667eee046596bd54ceb64d6 Binary files /dev/null and "b/summer2022\345\237\272\344\272\216secGear\345\256\236\347\216\260\351\203\250\345\210\206\345\233\275\345\257\206\347\256\227\346\263\225/\347\216\213\345\260\217\346\272\252-22b970374-\347\273\223\351\241\271\346\212\245\345\221\212.pdf" differ