123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- #include <openssl/ec.h>
- #include <openssl/bn.h>
- #include <openssl/rsa.h>
- #include <openssl/evp.h>
- #include <openssl/err.h>
- #include <openssl/rand.h>
- #include <openssl/ecdh.h>
- #include <stdio.h>
- int generate_sha256_hash(const unsigned char *message, size_t message_len, unsigned char *digest);
- int ec_key_gen(unsigned char* pub_key_x, unsigned char* pub_key_y, unsigned char* priv_key)
- {
- // unsigned char entropy_buf[ADD_ENTROPY_SIZE] = {0};
- // RAND_add(entropy_buf, sizeof(entropy_buf), ADD_ENTROPY_SIZE);
- // RAND_seed(entropy_buf, sizeof(entropy_buf));
- EC_KEY * ec_key = NULL;
-
- ec_key = EC_KEY_new();
- if (NULL == ec_key) {
- return 0x42;
- }
- EC_GROUP* ec_group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
- if (NULL == ec_group) {
- //EC_KEY_free(ec_key);
- return 0x0f;
- }
- if (0 == EC_KEY_set_group (ec_key, ec_group)) {
- EC_KEY_free(ec_key);
- EC_GROUP_free(ec_group);
- return 0x43; //break;
- }
- EC_KEY_set_asn1_flag(ec_key, OPENSSL_EC_NAMED_CURVE);
- int ret = EC_KEY_generate_key(ec_key);
- if (!ret) {
- // printf("EC_KEY_generate_key failure\n");
- return 0x01;
- }
- ///////////////////////// Openssl code ////////////
- const EC_POINT *public_k = NULL;
- const BIGNUM *private_k = NULL;
- public_k = EC_KEY_get0_public_key(ec_key);
- if (NULL == public_k)
- {
- EC_KEY_free(ec_key);
- return 0x04;
- }
- private_k = EC_KEY_get0_private_key(ec_key);
- if (NULL == private_k)
- {
- EC_KEY_free(ec_key);
- return 0x05;
- }
- if(!BN_bn2bin(private_k, priv_key))
- {
- EC_KEY_free(ec_key);
- return 0x06;
- }
- BIGNUM *pub_k_x = NULL;
- BIGNUM *pub_k_y = NULL;
- pub_k_x = BN_new();
- pub_k_y = BN_new();
- if (NULL == pub_k_x || NULL == pub_k_y) {
- return 0x42;//SGX_ERROR_OUT_OF_MEMORY;
- }
- // extract two BNs representing the public key
- //
- if (!EC_POINT_get_affine_coordinates_GFp(ec_group, public_k, pub_k_x, pub_k_y, NULL))
- //EC_KEY_free(ec_key);
- {
- //printf("%ld\n", ERR_get_error());
- BN_clear_free(pub_k_x); BN_clear_free(pub_k_y);
- EC_KEY_free(ec_key);
- EC_GROUP_free(ec_group);
- return 0x07;
- }
- ret = BN_bn2bin(pub_k_x, pub_key_x);
- BN_clear_free(pub_k_x);
- if(ret == 0)
- {
- BN_clear_free(pub_k_y);
- EC_KEY_free(ec_key);
- EC_GROUP_free(ec_group);
- return 0x08;
- }
- ret = BN_bn2bin(pub_k_y, pub_key_y);
- BN_clear_free(pub_k_y);
- EC_KEY_free(ec_key);
- EC_GROUP_free(ec_group);
- if(ret == 0)
- return 0x09;
- return 0;
- }
- unsigned long check_key(unsigned char* given_key_x, unsigned char* given_key_y) //, unsigned char* priv_key, unsigned char* shared_key_x, unsigned char* shared_key_y)
- {
- EC_GROUP* ec_group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
- if (NULL == ec_group) {
- //EC_KEY_free(ec_key);
- return 0x0f;
- }
- EC_POINT *ec_point = NULL;
- BIGNUM *b_x = NULL;
- BIGNUM *b_y = NULL;
- ec_point = EC_POINT_new(ec_group);
- if (NULL == ec_point) {
- return 0x42; //retval = SGX_ERROR_OUT_OF_MEMORY;
- // break;
- }
- // converts the x value of the point, represented as positive integer in big-endian into a BIGNUM
- b_x = BN_bin2bn(given_key_x, 32, NULL);
- if (NULL == b_x) {
- EC_POINT_free(ec_point);
- // return ERR_get_error();
- return 0xfd;
- }
- // converts the y value of the point, represented as positive integer in big-endian into a BIGNUM
- b_y = BN_bin2bn(given_key_y, 32, NULL);
- if (NULL == b_y) {
- BN_clear_free(b_x);
- EC_POINT_free(ec_point);
- return 0xfe;
- }
- // sets point based on x,y coordinates
- int ret = EC_POINT_set_affine_coordinates_GFp(ec_group, ec_point, b_x, b_y, NULL);
- if (1 != ret) {
- BN_clear_free(b_x);
- BN_clear_free(b_y);
- EC_POINT_free(ec_point);
- return ERR_get_error();
- }
- // checks if point is on curve
- //
- int ret_point_on_curve = EC_POINT_is_on_curve(ec_group, ec_point, NULL);
- if (1 != ret_point_on_curve) {
- EC_POINT_free(ec_point);
- BN_clear_free(b_x);
- BN_clear_free(b_y);
- return ret_point_on_curve << 4; //ret_point_on_curve;// break;
- }
-
- BN_clear_free(b_x);
- BN_clear_free(b_y);
- EC_POINT_free(ec_point);
- return 0;
- }
- unsigned long compute_shared_ECDHE_key(unsigned char* given_key_x, unsigned char* given_key_y, unsigned char* priv_key, unsigned char* shared_key, unsigned char* derived_key)
- {
- // int valid_point = check_key(given_key_x, given_key_y);
- // if(valid_point != 0)
- // return valid_point;
- unsigned long long_ret = 0;
- EC_GROUP* ec_group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
- if (NULL == ec_group) {
- return 0x0f;
- }
- EC_POINT *given_point = NULL;
- BIGNUM *b_x = NULL;
- BIGNUM *b_y = NULL;
- EC_KEY *private_key = NULL;
- given_point = EC_POINT_new(ec_group);
- if (NULL == given_point) {
- return 0x42; //retval = SGX_ERROR_OUT_OF_MEMORY;
- // break;
- }
- // converts the x value of the point, represented as positive integer in big-endian into a BIGNUM
- b_x = BN_bin2bn(given_key_x, 32, NULL);
- if (NULL == b_x) {
- EC_POINT_free(given_point);
- return 0xfd;
- }
- // converts the y value of the point, represented as positive integer in big-endian into a BIGNUM
- b_y = BN_bin2bn(given_key_y, 32, NULL);
- if (NULL == b_y) {
- EC_POINT_free(given_point);
- BN_clear_free(b_x);
- return 0xfe;
- }
- // sets point based on x,y coordinates
- int ret = EC_POINT_set_affine_coordinates_GFp(ec_group, given_point, b_x, b_y, NULL);
- if (1 != ret) {
- EC_POINT_free(given_point);
- BN_clear_free(b_x);
- BN_clear_free(b_y);
- return ERR_get_error();
- }
- // checks if point is on curve
- //
- int ret_point_on_curve = EC_POINT_is_on_curve(ec_group, given_point, NULL);
- if (1 != ret_point_on_curve) {
- EC_POINT_free(given_point);
- BN_clear_free(b_x);
- BN_clear_free(b_y);
- if(ret_point_on_curve == 0)
- return 0x43;
- else
- return ERR_get_error();
- }
-
- // create empty shared key BN
- //
- private_key = EC_KEY_new();
- if (private_key == NULL) {
- EC_POINT_free(given_point);
- BN_clear_free(b_x);
- BN_clear_free(b_y);
- return 0x42;//ret = SGX_ERROR_OUT_OF_MEMORY;
- }
- // init private key group (set curve)
- //
- if (EC_KEY_set_group (private_key, ec_group) != 1) {
- EC_POINT_free(given_point);
- EC_KEY_free(private_key);
- BN_clear_free(b_x);
- BN_clear_free(b_y);
- return ERR_get_error(); //break;
- }
- // init private key with BN value
- //
- BIGNUM *BN_dh_privB = NULL;
- BN_dh_privB = BN_bin2bn(priv_key, 32, NULL);
- if(BN_dh_privB == NULL) {
- EC_POINT_free(given_point);
- EC_KEY_free(private_key);
- BN_clear_free(b_x);
- BN_clear_free(b_y);
- return 0x44;
- }
- if (EC_KEY_set_private_key(private_key, BN_dh_privB) != 1) {
- EC_POINT_free(given_point);
- EC_KEY_free(private_key);
- BN_clear_free(b_x);
- BN_clear_free(b_y);
- BN_clear_free(BN_dh_privB);
- return ERR_get_error();
- }
- // calculate shared dh key
- //
- int shared_key_len = 32; //sizeof(sgx_ec256_dh_shared_t);
- shared_key_len = ECDH_compute_key(shared_key, 32, given_point, private_key, NULL);
- if (shared_key_len <= 0) {
- long_ret = ERR_get_error();
- }
- else
- long_ret = generate_sha256_hash(shared_key, 32, derived_key);
- BN_clear_free(BN_dh_privB);
- BN_clear_free(b_x);
- BN_clear_free(b_y);
- EC_POINT_free(given_point);
- EC_KEY_free(private_key);
- return long_ret;
- }
- int generate_sha256_hash(const unsigned char *message, size_t message_len, unsigned char *digest)
- {
- EVP_MD_CTX *mdctx; unsigned int digest_len;
- if((mdctx = EVP_MD_CTX_create()) == NULL)
- {
- //printf("EVP_MD_CTX_create returned NULL - could not create context\n"); fflush(stdout);
- return 0x1;
- }
- if(EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL) != 1)
- {
- //printf("EVP_DigestInit_ex returned 0 - could not initialize hash with SHA256\n"); fflush(stdout);
- return 0x2;
- }
- if(EVP_DigestUpdate(mdctx, message, message_len) != 1)
- {
- //printf("EVP_DigestUpdate returned 0 - could not compute SHA256 hash\n"); fflush(stdout);
- return 0x3;
- }
- if(1 != EVP_DigestFinal_ex(mdctx, digest, &digest_len))
- {
- //printf("EVP_DigestFinal_ex returned 0 - could not finalize SHA256 hash\n"); fflush(stdout);
- return 0x4;
- }
- if(digest_len != 32)
- {
- // printf("EVP_DigestFinal_ex returned a digest length of 0x%x instead of 0x20\n", digest_len); fflush(stdout);
- return 0x5;
- }
- EVP_MD_CTX_destroy(mdctx);
- return 0;
- }
|