123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484 |
- #include "sgx_ecc256_common.h"
- sgx_status_t sgx_ecc256_open_context(sgx_ecc_state_handle_t* p_ecc_handle)
- {
- IppStatus ipp_ret = ippStsNoErr;
- IppsECCPState* p_ecc_state = NULL;
-
- int ctx_size = 0;
- if (p_ecc_handle == NULL)
- return SGX_ERROR_INVALID_PARAMETER;
- ipp_ret = ippsECCPGetSize(256, &ctx_size);
- if (ipp_ret != ippStsNoErr)
- return SGX_ERROR_UNEXPECTED;
- p_ecc_state = (IppsECCPState*)(malloc(ctx_size));
- if (p_ecc_state == NULL)
- return SGX_ERROR_OUT_OF_MEMORY;
- ipp_ret = ippsECCPInit(256, p_ecc_state);
- if (ipp_ret != ippStsNoErr)
- {
- SAFE_FREE(p_ecc_state);
- *p_ecc_handle = NULL;
- return SGX_ERROR_UNEXPECTED;
- }
- ipp_ret = ippsECCPSetStd(IppECCPStd256r1, p_ecc_state);
- if (ipp_ret != ippStsNoErr)
- {
- SAFE_FREE(p_ecc_state);
- *p_ecc_handle = NULL;
- return SGX_ERROR_UNEXPECTED;
- }
- *p_ecc_handle = p_ecc_state;
- return SGX_SUCCESS;
- }
- sgx_status_t sgx_ecc256_close_context(sgx_ecc_state_handle_t ecc_handle)
- {
- if (ecc_handle == NULL)
- {
- return SGX_ERROR_INVALID_PARAMETER;
- }
- IppsECCPState* p_ecc_state = (IppsECCPState*)ecc_handle;
- int ctx_size = 0;
- IppStatus ipp_ret = ippsECCPGetSize(256, &ctx_size);
- if (ipp_ret != ippStsNoErr)
- {
- free(p_ecc_state);
- return SGX_SUCCESS;
- }
- memset_s(p_ecc_state, ctx_size, 0, ctx_size);
- free(p_ecc_state);
- 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;
- }
- IppsBigNumState* dh_priv_BN = NULL;
- IppsECCPPointState* point_pub = NULL;
- IppsBigNumState* pub_gx = NULL;
- IppsBigNumState* pub_gy = NULL;
- IppStatus ipp_ret = ippStsNoErr;
- int ecPointSize = 0;
- IppsECCPState* p_ecc_state = (IppsECCPState*)ecc_handle;
- do
- {
-
- ipp_ret = ippsECCPPointGetSize(256, &ecPointSize);
- ERROR_BREAK(ipp_ret);
- point_pub = (IppsECCPPointState*)(malloc(ecPointSize));
- if (!point_pub)
- {
- ipp_ret = ippStsNoMemErr;
- break;
- }
- ipp_ret = ippsECCPPointInit(256, point_pub);
- ERROR_BREAK(ipp_ret);
- ipp_ret = sgx_ipp_newBN(NULL, SGX_ECP256_KEY_SIZE, &dh_priv_BN);
- ERROR_BREAK(ipp_ret);
-
-
- ipp_ret = ippsECCPGenKeyPair(dh_priv_BN, point_pub, p_ecc_state, (IppBitSupplier)sgx_ipp_DRNGen, NULL);
- ERROR_BREAK(ipp_ret);
-
- ipp_ret = sgx_ipp_newBN(NULL, SGX_ECP256_KEY_SIZE, &pub_gx);
- ERROR_BREAK(ipp_ret);
- ipp_ret = sgx_ipp_newBN(NULL, SGX_ECP256_KEY_SIZE, &pub_gy);
- ERROR_BREAK(ipp_ret);
- ipp_ret = ippsECCPGetPoint(pub_gx, pub_gy, point_pub, p_ecc_state);
- ERROR_BREAK(ipp_ret);
- IppsBigNumSGN sgn = IppsBigNumPOS;
- Ipp32u *pdata = NULL;
-
- int length = 0;
- ipp_ret = ippsRef_BN(&sgn, &length, &pdata, pub_gx);
- ERROR_BREAK(ipp_ret);
- memset(p_public->gx, 0, sizeof(p_public->gx));
- ipp_ret = check_copy_size(sizeof(p_public->gx), ROUND_TO(length, 8) / 8);
- ERROR_BREAK(ipp_ret);
- memcpy(p_public->gx, pdata, ROUND_TO(length, 8) / 8);
- ipp_ret = ippsRef_BN(&sgn, &length, &pdata, pub_gy);
- ERROR_BREAK(ipp_ret);
- memset(p_public->gy, 0, sizeof(p_public->gy));
- ipp_ret = check_copy_size(sizeof(p_public->gy), ROUND_TO(length, 8) / 8);
- ERROR_BREAK(ipp_ret);
- memcpy(p_public->gy, pdata, ROUND_TO(length, 8) / 8);
- ipp_ret = ippsRef_BN(&sgn, &length, &pdata, dh_priv_BN);
- ERROR_BREAK(ipp_ret);
- memset(p_private->r, 0, sizeof(p_private->r));
- ipp_ret = check_copy_size(sizeof(p_private->r), ROUND_TO(length, 8) / 8);
- ERROR_BREAK(ipp_ret);
- memcpy(p_private->r, pdata, ROUND_TO(length, 8) / 8);
- } while (0);
-
- if (point_pub) memset_s(point_pub, ecPointSize, 0, ecPointSize);
- SAFE_FREE(point_pub);
- sgx_ipp_secure_free_BN(pub_gx, SGX_ECP256_KEY_SIZE);
- sgx_ipp_secure_free_BN(pub_gy, SGX_ECP256_KEY_SIZE);
- sgx_ipp_secure_free_BN(dh_priv_BN, SGX_ECP256_KEY_SIZE);
- switch (ipp_ret)
- {
- case ippStsNoErr: return SGX_SUCCESS;
- case ippStsNoMemErr:
- case ippStsMemAllocErr: return SGX_ERROR_OUT_OF_MEMORY;
- case ippStsNullPtrErr:
- case ippStsLengthErr:
- case ippStsOutOfRangeErr:
- case ippStsSizeErr:
- case ippStsBadArgErr: return SGX_ERROR_INVALID_PARAMETER;
- default: return SGX_ERROR_UNEXPECTED;
- }
- }
- 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;
- }
- IppsECCPPointState* point2check = NULL;
- IppStatus ipp_ret = ippStsNoErr;
- IppsECCPState* p_ecc_state = (IppsECCPState*)ecc_handle;
- IppECResult ipp_result = ippECValid;
- int ecPointSize = 0;
- IppsBigNumState* BN_gx = NULL;
- IppsBigNumState* BN_gy = NULL;
-
- *p_valid = 0;
- do
- {
- ipp_ret = ippsECCPPointGetSize(256, &ecPointSize);
- ERROR_BREAK(ipp_ret);
- point2check = (IppsECCPPointState*)malloc(ecPointSize);
- if (!point2check)
- {
- ipp_ret = ippStsNoMemErr;
- break;
- }
- ipp_ret = ippsECCPPointInit(256, point2check);
- ERROR_BREAK(ipp_ret);
- ipp_ret = sgx_ipp_newBN((const Ipp32u *)p_point->gx, sizeof(p_point->gx), &BN_gx);
- ERROR_BREAK(ipp_ret);
- ipp_ret = sgx_ipp_newBN((const Ipp32u *)p_point->gy, sizeof(p_point->gy), &BN_gy);
- ERROR_BREAK(ipp_ret);
- ipp_ret = ippsECCPSetPoint(BN_gx, BN_gy, point2check, p_ecc_state);
- ERROR_BREAK(ipp_ret);
-
- ipp_ret = ippsECCPCheckPoint(point2check, &ipp_result, p_ecc_state);
- ERROR_BREAK(ipp_ret);
- if (ipp_result == ippECValid)
- {
- *p_valid = 1;
- }
- } while (0);
-
- if (point2check)
- memset_s(point2check, ecPointSize, 0, ecPointSize);
- SAFE_FREE(point2check);
- sgx_ipp_secure_free_BN(BN_gx, sizeof(p_point->gx));
- sgx_ipp_secure_free_BN(BN_gy, sizeof(p_point->gy));
- switch (ipp_ret)
- {
- case ippStsNoErr: return SGX_SUCCESS;
- case ippStsNoMemErr:
- case ippStsMemAllocErr: return SGX_ERROR_OUT_OF_MEMORY;
- case ippStsNullPtrErr:
- case ippStsLengthErr:
- case ippStsOutOfRangeErr:
- case ippStsSizeErr:
- case ippStsBadArgErr: return SGX_ERROR_INVALID_PARAMETER;
- default: return SGX_ERROR_UNEXPECTED;
- }
- }
- 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;
- }
- IppsBigNumState* BN_dh_privB = NULL;
- IppsBigNumState* BN_dh_share = NULL;
- IppsBigNumState* pubA_gx = NULL;
- IppsBigNumState* pubA_gy = NULL;
- IppsECCPPointState* point_pubA = NULL;
- IppStatus ipp_ret = ippStsNoErr;
- int ecPointSize = 0;
- IppsECCPState* p_ecc_state = (IppsECCPState*)ecc_handle;
- IppECResult ipp_result = ippECValid;
- do
- {
- ipp_ret = sgx_ipp_newBN((Ipp32u*)p_private_b->r, sizeof(sgx_ec256_private_t), &BN_dh_privB);
- ERROR_BREAK(ipp_ret);
- ipp_ret = sgx_ipp_newBN((uint32_t*)p_public_ga->gx, sizeof(p_public_ga->gx), &pubA_gx);
- ERROR_BREAK(ipp_ret);
- ipp_ret = sgx_ipp_newBN((uint32_t*)p_public_ga->gy, sizeof(p_public_ga->gy), &pubA_gy);
- ERROR_BREAK(ipp_ret);
- ipp_ret = ippsECCPPointGetSize(256, &ecPointSize);
- ERROR_BREAK(ipp_ret);
- point_pubA = (IppsECCPPointState*)(malloc(ecPointSize));
- if (!point_pubA)
- {
- ipp_ret = ippStsNoMemErr;
- break;
- }
- ipp_ret = ippsECCPPointInit(256, point_pubA);
- ERROR_BREAK(ipp_ret);
- ipp_ret = ippsECCPSetPoint(pubA_gx, pubA_gy, point_pubA, p_ecc_state);
- ERROR_BREAK(ipp_ret);
-
- ipp_ret = ippsECCPCheckPoint(point_pubA, &ipp_result, p_ecc_state);
- if (ipp_result != ippECValid)
- {
- break;
- }
- ERROR_BREAK(ipp_ret);
- ipp_ret = sgx_ipp_newBN(NULL, sizeof(sgx_ec256_dh_shared_t), &BN_dh_share);
- ERROR_BREAK(ipp_ret);
-
- ipp_ret = ippsECCPSharedSecretDH(BN_dh_privB, point_pubA, BN_dh_share, p_ecc_state);
- ERROR_BREAK(ipp_ret);
- IppsBigNumSGN sgn = IppsBigNumPOS;
- int length = 0;
- Ipp32u * pdata = NULL;
- ipp_ret = ippsRef_BN(&sgn, &length, &pdata, BN_dh_share);
- ERROR_BREAK(ipp_ret);
- memset(p_shared_key->s, 0, sizeof(p_shared_key->s));
- ipp_ret = check_copy_size(sizeof(p_shared_key->s), ROUND_TO(length, 8) / 8);
- ERROR_BREAK(ipp_ret);
- memcpy(p_shared_key->s, pdata, ROUND_TO(length, 8) / 8);
- } while (0);
-
- if (point_pubA) memset_s(point_pubA, ecPointSize, 0, ecPointSize);
- SAFE_FREE(point_pubA);
- sgx_ipp_secure_free_BN(pubA_gx, sizeof(p_public_ga->gx));
- sgx_ipp_secure_free_BN(pubA_gy, sizeof(p_public_ga->gy));
- sgx_ipp_secure_free_BN(BN_dh_privB, sizeof(sgx_ec256_private_t));
- sgx_ipp_secure_free_BN(BN_dh_share, sizeof(sgx_ec256_dh_shared_t));
- if (ipp_result != ippECValid)
- {
- return SGX_ERROR_INVALID_PARAMETER;
- }
- switch (ipp_ret)
- {
- case ippStsNoErr: return SGX_SUCCESS;
- case ippStsNoMemErr:
- case ippStsMemAllocErr: return SGX_ERROR_OUT_OF_MEMORY;
- case ippStsNullPtrErr:
- case ippStsLengthErr:
- case ippStsOutOfRangeErr:
- case ippStsSizeErr:
- case ippStsBadArgErr: return SGX_ERROR_INVALID_PARAMETER;
- default: return SGX_ERROR_UNEXPECTED;
- }
- }
- sgx_status_t sgx_ecc256_compute_shared_dhkey512(sgx_ec256_private_t *p_private_b,
- sgx_ec256_public_t *p_public_ga,
- sgx_ec256_dh_shared512_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;
- }
- IppsBigNumState* BN_dh_privB = NULL;
- IppsBigNumState* BN_dh_share512_x = NULL;
- IppsBigNumState* BN_dh_share512_y = NULL;
- IppsBigNumState* pubA_gx = NULL;
- IppsBigNumState* pubA_gy = NULL;
- IppsECCPPointState* point_pubA = NULL;
- IppsECCPPointState* point_R = NULL;
- IppStatus ipp_ret = ippStsNoErr;
- int ecPointSize = 0;
- IppsECCPState* p_ecc_state = (IppsECCPState*)ecc_handle;
- do
- {
- ipp_ret = sgx_ipp_newBN((Ipp32u*)p_private_b->r, sizeof(sgx_ec256_private_t), &BN_dh_privB);
- ERROR_BREAK(ipp_ret);
- ipp_ret = sgx_ipp_newBN((uint32_t*)p_public_ga->gx, sizeof(p_public_ga->gx), &pubA_gx);
- ERROR_BREAK(ipp_ret);
- ipp_ret = sgx_ipp_newBN((uint32_t*)p_public_ga->gy, sizeof(p_public_ga->gy), &pubA_gy);
- ERROR_BREAK(ipp_ret);
- ipp_ret = ippsECCPPointGetSize(256, &ecPointSize);
- ERROR_BREAK(ipp_ret);
- point_pubA = (IppsECCPPointState*)( malloc(ecPointSize) );
- if(!point_pubA)
- {
- ipp_ret = ippStsNoMemErr;
- break;
- }
- ipp_ret = ippsECCPPointInit(256, point_pubA);
- ERROR_BREAK(ipp_ret);
- ipp_ret = ippsECCPSetPoint(pubA_gx, pubA_gy, point_pubA, p_ecc_state);
- ERROR_BREAK(ipp_ret);
- point_R = (IppsECCPPointState*)( malloc(ecPointSize) );
- if(!point_R)
- {
- ipp_ret = ippStsNoMemErr;
- break;
- }
- ipp_ret = ippsECCPPointInit(256, point_R);
- ERROR_BREAK(ipp_ret);
- ipp_ret = sgx_ipp_newBN(NULL, sizeof(sgx_ec256_dh_shared_t), &BN_dh_share512_x);
- ERROR_BREAK(ipp_ret);
- ipp_ret = sgx_ipp_newBN(NULL, sizeof(sgx_ec256_dh_shared_t), &BN_dh_share512_y);
- ERROR_BREAK(ipp_ret);
- ipp_ret = ippsECCPMulPointScalar(point_pubA, BN_dh_privB, point_R, p_ecc_state);
- ERROR_BREAK(ipp_ret);
- ipp_ret = ippsECCPGetPoint(BN_dh_share512_x, BN_dh_share512_y, point_R, p_ecc_state);
- ERROR_BREAK(ipp_ret);
- IppsBigNumSGN sgn = IppsBigNumPOS;
- int length = 0;
- Ipp32u *pdata = NULL;
- ipp_ret = ippsRef_BN(&sgn, &length, &pdata, BN_dh_share512_x);
- ERROR_BREAK(ipp_ret);
- memset(p_shared_key->x, 0, sizeof(p_shared_key->x));
- memcpy(p_shared_key->x, pdata, ROUND_TO(length, 8)/8);
-
- memset_s(pdata, sizeof(p_shared_key->x), 0, ROUND_TO(length, 8)/8);
- ipp_ret = ippsRef_BN(&sgn, &length, &pdata, BN_dh_share512_y);
- ERROR_BREAK(ipp_ret);
- memset(p_shared_key->y, 0, sizeof(p_shared_key->y));
- memcpy(p_shared_key->y, pdata, ROUND_TO(length, 8)/8);
-
- memset_s(pdata, sizeof(p_shared_key->x), 0, ROUND_TO(length, 8)/8);
- }while(0);
-
- if (point_pubA) memset_s(point_pubA, ecPointSize, 0, ecPointSize);
- SAFE_FREE(point_pubA);
- if (point_R) memset_s(point_R, ecPointSize, 0, ecPointSize);
- SAFE_FREE(point_R);
- sgx_ipp_secure_free_BN(pubA_gx, sizeof(p_public_ga->gx));
- sgx_ipp_secure_free_BN(pubA_gy, sizeof(p_public_ga->gy));
- sgx_ipp_secure_free_BN(BN_dh_privB, sizeof(sgx_ec256_private_t));
- sgx_ipp_secure_free_BN(BN_dh_share512_x, sizeof(sgx_ec256_dh_shared_t));
- sgx_ipp_secure_free_BN(BN_dh_share512_y, sizeof(sgx_ec256_dh_shared_t));
- switch (ipp_ret)
- {
- case ippStsNoErr: return SGX_SUCCESS;
- case ippStsNoMemErr:
- case ippStsMemAllocErr: return SGX_ERROR_OUT_OF_MEMORY;
- case ippStsNullPtrErr:
- case ippStsLengthErr:
- case ippStsOutOfRangeErr:
- case ippStsSizeErr:
- case ippStsBadArgErr: return SGX_ERROR_INVALID_PARAMETER;
- default: return SGX_ERROR_UNEXPECTED;
- }
- }
|