123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- #include "string.h"
- #include "se_tcrypto_common.h"
- #include "sgx_ecc256_internal.h"
- #include <openssl/evp.h>
- #include <openssl/ec.h>
- #include <openssl/err.h>
- #include "sgx_tcrypto.h"
- sgx_status_t sgx_ecc256_compute_shared_point(sgx_ec256_private_t *p_private_b,
- sgx_ec256_public_t *p_public_ga,
- sgx_ec256_shared_point_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_POINT *point_R = NULL;
- BIGNUM *BN_dh_privB = NULL;
- BIGNUM *pubA_gx = NULL;
- BIGNUM *pubA_gy = NULL;
- BIGNUM *BN_dh_shared_x = NULL;
- BIGNUM *BN_dh_shared_y = 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(p_public_ga->gx), 0);
- if (pubA_gx == NULL) {
- break;
- }
- pubA_gy = BN_lebin2bn((unsigned char*)p_public_ga->gy, sizeof(p_public_ga->gy), 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;
- }
-
-
- point_R = EC_POINT_new(ec_group);
- if (point_R == NULL) {
- ret = SGX_ERROR_OUT_OF_MEMORY;
- break;
- }
-
-
-
- if (EC_POINT_mul(ec_group, point_R, NULL, point_pubA, BN_dh_privB, NULL) != 1) {
- break;
- }
-
-
- if (EC_POINT_is_on_curve(ec_group, point_R, NULL) != 1) {
- break;
- }
- BN_dh_shared_x = BN_new();
- if (BN_dh_shared_x == NULL) {
- ret = SGX_ERROR_OUT_OF_MEMORY;
- break;
- }
- BN_dh_shared_y = BN_new();
- if (BN_dh_shared_y == NULL) {
- ret = SGX_ERROR_OUT_OF_MEMORY;
- break;
- }
- if (EC_POINT_get_affine_coordinates_GFp(ec_group, point_R, BN_dh_shared_x, BN_dh_shared_y, NULL) != 1) {
- break;
- }
-
- if (BN_bn2lebinpad(BN_dh_shared_x, (unsigned char*)p_shared_key->x, sizeof(p_shared_key->x)) == -1) {
- break;
- }
- if (BN_bn2lebinpad(BN_dh_shared_y, (unsigned char*)p_shared_key->y, sizeof(p_shared_key->y)) == -1) {
- break;
- }
- ret = SGX_SUCCESS;
- } while(0);
- if (ret != SGX_SUCCESS) {
- GET_LAST_OPENSSL_ERROR;
- memset_s(p_shared_key->x, sizeof(p_shared_key->x), 0, sizeof(p_shared_key->x));
- memset_s(p_shared_key->y, sizeof(p_shared_key->y), 0, sizeof(p_shared_key->y));
- }
-
-
- EC_POINT_clear_free(point_pubA);
- EC_POINT_clear_free(point_R);
- BN_clear_free(BN_dh_shared_x);
- BN_clear_free(BN_dh_shared_y);
- BN_clear_free(BN_dh_privB);
- BN_clear_free(pubA_gx);
- BN_clear_free(pubA_gy);
- return ret;
- }
|