123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403 |
- #include "string.h"
- #include "se_tcrypto_common.h"
- #include <openssl/evp.h>
- #include <openssl/ec.h>
- #include <openssl/err.h>
- #include "sgx_tcrypto.h"
- #define POINT_NOT_ON_CURVE 0x1007c06b
- sgx_status_t sgx_ecc256_open_context(sgx_ecc_state_handle_t* p_ecc_handle)
- {
- if (p_ecc_handle == NULL) {
- return SGX_ERROR_INVALID_PARAMETER;
- }
- sgx_status_t retval = SGX_SUCCESS;
- CLEAR_OPENSSL_ERROR_QUEUE;
-
- EC_GROUP* ec_group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
- if (NULL == ec_group) {
- GET_LAST_OPENSSL_ERROR;
- retval = SGX_ERROR_UNEXPECTED;
- } else {
- *p_ecc_handle = (void*)ec_group;
- }
- return retval;
- }
- sgx_status_t sgx_ecc256_close_context(sgx_ecc_state_handle_t ecc_handle)
- {
- if (ecc_handle == NULL) {
- return SGX_ERROR_INVALID_PARAMETER;
- }
- EC_GROUP_free((EC_GROUP*)ecc_handle);
- return SGX_SUCCESS;
- }
- sgx_status_t sgx_ecc256_create_key_pair(sgx_ec256_private_t *p_private,
- sgx_ec256_public_t *p_public,
- sgx_ecc_state_handle_t ecc_handle)
- {
- if ((ecc_handle == NULL) || (p_private == NULL) || (p_public == NULL)) {
- return SGX_ERROR_INVALID_PARAMETER;
- }
- EC_GROUP *ec_group = (EC_GROUP*) ecc_handle;
- EC_KEY *ec_key = NULL;
- BIGNUM *pub_k_x = NULL;
- BIGNUM *pub_k_y = NULL;
- const EC_POINT *public_k = NULL;
- const BIGNUM *private_k = NULL;
- sgx_status_t ret = SGX_ERROR_UNEXPECTED;
- CLEAR_OPENSSL_ERROR_QUEUE;
- do {
-
-
- ec_key = EC_KEY_new();
- if (NULL == ec_key) {
- ret = SGX_ERROR_OUT_OF_MEMORY;
- break;
- }
-
-
- if (0 == EC_KEY_set_group (ec_key, ec_group)) {
- break;
- }
-
-
- if (0 == EC_KEY_generate_key(ec_key)) {
- break;
- }
- pub_k_x = BN_new();
- pub_k_y = BN_new();
- if (NULL == pub_k_x || NULL == pub_k_y) {
- ret = SGX_ERROR_OUT_OF_MEMORY;
- break;
- }
-
-
-
- public_k = EC_KEY_get0_public_key(ec_key);
- if (NULL == ec_key) {
- break;
- }
- private_k = EC_KEY_get0_private_key(ec_key);
- if (NULL == ec_key) {
- break;
- }
-
-
- if (!EC_POINT_get_affine_coordinates_GFp(ec_group, public_k, pub_k_x, pub_k_y, NULL)) {
- break;
- }
-
-
- if (-1 == BN_bn2lebinpad(private_k, (unsigned char*)p_private, SGX_ECP256_KEY_SIZE)) {
- break;
- }
-
-
- if (-1 == BN_bn2lebinpad(pub_k_x, (unsigned char*)p_public->gx, SGX_ECP256_KEY_SIZE)) {
- break;
- }
-
-
- if (-1 == BN_bn2lebinpad(pub_k_y, (unsigned char*)p_public->gy, SGX_ECP256_KEY_SIZE)) {
- break;
- }
- ret = SGX_SUCCESS;
- } while(0);
- if (SGX_SUCCESS != ret) {
- GET_LAST_OPENSSL_ERROR;
-
-
- memset_s(p_private, sizeof(p_private), 0, sizeof(p_private));
- memset_s(p_public->gx, sizeof(p_public->gx), 0, sizeof(p_public->gx));
- memset_s(p_public->gy, sizeof(p_public->gy), 0, sizeof(p_public->gy));
- }
-
-
- EC_KEY_free(ec_key);
- BN_clear_free(pub_k_x);
- BN_clear_free(pub_k_y);
- return ret;
- }
- sgx_status_t sgx_ecc256_check_point(const sgx_ec256_public_t *p_point,
- const sgx_ecc_state_handle_t ecc_handle,
- int *p_valid)
- {
- if ((ecc_handle == NULL) || (p_point == NULL) || (p_valid == NULL)) {
- return SGX_ERROR_INVALID_PARAMETER;
- }
- sgx_status_t retval = SGX_ERROR_UNEXPECTED;
- EC_POINT *ec_point = NULL;
- BIGNUM *b_x = NULL;
- BIGNUM *b_y = NULL;
- int ret_point_on_curve = 0;
- unsigned long internal_openssl_error = 0;
- CLEAR_OPENSSL_ERROR_QUEUE;
- do {
-
-
- b_x = BN_lebin2bn(p_point->gx, SGX_ECP256_KEY_SIZE, NULL);
- if (NULL == b_x) {
- break;
- }
-
-
- b_y = BN_lebin2bn(p_point->gy, SGX_ECP256_KEY_SIZE, NULL);
- if (NULL == b_y) {
- break;
- }
-
-
- ec_point = EC_POINT_new((const EC_GROUP*)ecc_handle);
- if (NULL == ec_point) {
- retval = SGX_ERROR_OUT_OF_MEMORY;
- break;
- }
-
-
- if (1 != EC_POINT_set_affine_coordinates_GFp((const EC_GROUP*)ecc_handle, ec_point, b_x, b_y, NULL)) {
- internal_openssl_error = ERR_get_error();
- if (internal_openssl_error == POINT_NOT_ON_CURVE) {
-
- *p_valid = 0;
- retval = SGX_SUCCESS;
- } else {
- #ifdef DEBUG
- openssl_last_err = internal_openssl_error;
- #endif
- }
- break;
- }
-
-
- ret_point_on_curve = EC_POINT_is_on_curve((const EC_GROUP*)ecc_handle, ec_point, NULL);
- if (-1 == ret_point_on_curve) {
- break;
- }
- *p_valid = ret_point_on_curve;
- retval = SGX_SUCCESS;
- } while(0);
- #ifdef DEBUG
- if (SGX_SUCCESS != retval && 0 != openssl_last_err) {
- GET_LAST_OPENSSL_ERROR;
- }
- #endif
- if (ec_point)
- EC_POINT_clear_free(ec_point);
- if (b_x)
- BN_clear_free(b_x);
- if (b_y)
- BN_clear_free(b_y);
- return retval;
- }
- sgx_status_t sgx_ecc256_compute_shared_dhkey(sgx_ec256_private_t *p_private_b,
- sgx_ec256_public_t *p_public_ga,
- sgx_ec256_dh_shared_t *p_shared_key,
- sgx_ecc_state_handle_t ecc_handle)
- {
- if ((ecc_handle == NULL) || (p_private_b == NULL) || (p_public_ga == NULL) || (p_shared_key == NULL)) {
- return SGX_ERROR_INVALID_PARAMETER;
- }
- sgx_status_t ret = SGX_ERROR_UNEXPECTED;
- EC_GROUP *ec_group = (EC_GROUP*) ecc_handle;
- EC_POINT *point_pubA = NULL;
- EC_KEY* private_key = NULL;
- BIGNUM *BN_dh_privB = NULL;
- BIGNUM *pubA_gx = NULL;
- BIGNUM *pubA_gy = NULL;
- BIGNUM *tmp = NULL;
- CLEAR_OPENSSL_ERROR_QUEUE;
- do {
-
-
- BN_dh_privB = BN_lebin2bn((unsigned char*)p_private_b->r, sizeof(sgx_ec256_private_t), 0);
- if (BN_dh_privB == NULL) {
- break;
- }
- pubA_gx = BN_lebin2bn((unsigned char*)p_public_ga->gx, sizeof(sgx_ec256_private_t), 0);
- if (pubA_gx == NULL) {
- break;
- }
- pubA_gy = BN_lebin2bn((unsigned char*)p_public_ga->gy, sizeof(sgx_ec256_private_t), 0);
- if (pubA_gy == NULL) {
- break;
- }
-
-
- point_pubA = EC_POINT_new(ec_group);
- if (point_pubA == NULL) {
- ret = SGX_ERROR_OUT_OF_MEMORY;
- break;
- }
-
-
- if (EC_POINT_set_affine_coordinates_GFp(ec_group, point_pubA, pubA_gx, pubA_gy, NULL) != 1) {
- break;
- }
-
-
- if (EC_POINT_is_on_curve(ec_group, point_pubA, NULL) != 1) {
- break;
- }
-
-
- private_key = EC_KEY_new();
- if (private_key == NULL) {
- ret = SGX_ERROR_OUT_OF_MEMORY;
- break;
- }
-
-
- if (EC_KEY_set_group (private_key, ec_group) != 1) {
- break;
- }
-
-
- if (EC_KEY_set_private_key(private_key, BN_dh_privB) != 1) {
- break;
- }
-
-
- size_t shared_key_len = sizeof(sgx_ec256_dh_shared_t);
- shared_key_len = ECDH_compute_key(&(p_shared_key->s), shared_key_len, point_pubA, private_key, NULL);
- if (shared_key_len <= 0) {
- break;
- }
-
-
- tmp = BN_bin2bn((unsigned char*)&(p_shared_key->s), sizeof(sgx_ec256_dh_shared_t), 0);
- if (tmp == NULL) {
- break;
- }
- if (BN_bn2lebinpad(tmp, p_shared_key->s, sizeof(sgx_ec256_dh_shared_t)) == -1) {
- break;
- }
- ret = SGX_SUCCESS;
- } while(0);
- if (ret != SGX_SUCCESS) {
- GET_LAST_OPENSSL_ERROR;
- memset_s(p_shared_key->s, sizeof(p_shared_key->s), 0, sizeof(p_shared_key->s));
- }
-
-
- EC_POINT_clear_free(point_pubA);
- EC_KEY_free(private_key);
- BN_clear_free(BN_dh_privB);
- BN_clear_free(pubA_gx);
- BN_clear_free(pubA_gy);
- BN_clear_free(tmp);
- return ret;
- }
|