sgx_ecc256_internal.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright (C) 2011-2017 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #include "sgx_ecc256_common.h"
  32. #include "sgx_ecc256_internal.h"
  33. /* Computes a point with scalar multiplication based on private B key (local) and remote public Ga Key
  34. * Parameters:
  35. * Return: sgx_status_t - SGX_SUCCESS or failure as defined sgx_error.h
  36. * Inputs: sgx_ecc_state_handle_t ecc_handle - Handle to ECC crypto system
  37. * sgx_ec256_private_t *p_private_b - Pointer to the local private key - LITTLE ENDIAN
  38. * sgx_ec256_public_t *p_public_ga - Pointer to the remote public key - LITTLE ENDIAN
  39. * Output: sgx_ec256_shared_point_t *p_shared_key - Pointer to the target shared point - LITTLE ENDIAN
  40. x-coordinate of (privKeyB - pubKeyA) */
  41. sgx_status_t sgx_ecc256_compute_shared_point(sgx_ec256_private_t *p_private_b,
  42. sgx_ec256_public_t *p_public_ga,
  43. sgx_ec256_shared_point_t *p_shared_key,
  44. sgx_ecc_state_handle_t ecc_handle)
  45. {
  46. if ((ecc_handle == NULL) || (p_private_b == NULL) || (p_public_ga == NULL) || (p_shared_key == NULL))
  47. {
  48. return SGX_ERROR_INVALID_PARAMETER;
  49. }
  50. IppsBigNumState* BN_dh_privB = NULL;
  51. IppsBigNumState* BN_dh_shared_x = NULL;
  52. IppsBigNumState* BN_dh_shared_y = NULL;
  53. IppsBigNumState* pubA_gx = NULL;
  54. IppsBigNumState* pubA_gy = NULL;
  55. IppsECCPPointState* point_pubA = NULL;
  56. IppsECCPPointState* point_R = NULL;
  57. IppStatus ipp_ret = ippStsNoErr;
  58. int ecPointSize = 0;
  59. IppECResult ipp_result = ippECValid;
  60. IppsECCPState* p_ecc_state = (IppsECCPState*)ecc_handle;
  61. do
  62. {
  63. ipp_ret = sgx_ipp_newBN((Ipp32u*)p_private_b->r, sizeof(sgx_ec256_private_t), &BN_dh_privB);
  64. ERROR_BREAK(ipp_ret);
  65. ipp_ret = sgx_ipp_newBN((uint32_t*)p_public_ga->gx, sizeof(p_public_ga->gx), &pubA_gx);
  66. ERROR_BREAK(ipp_ret);
  67. ipp_ret = sgx_ipp_newBN((uint32_t*)p_public_ga->gy, sizeof(p_public_ga->gy), &pubA_gy);
  68. ERROR_BREAK(ipp_ret);
  69. ipp_ret = ippsECCPPointGetSize(256, &ecPointSize);
  70. ERROR_BREAK(ipp_ret);
  71. point_pubA = (IppsECCPPointState*)( malloc(ecPointSize) );
  72. if(!point_pubA)
  73. {
  74. ipp_ret = ippStsNoMemErr;
  75. break;
  76. }
  77. ipp_ret = ippsECCPPointInit(256, point_pubA);
  78. ERROR_BREAK(ipp_ret);
  79. ipp_ret = ippsECCPSetPoint(pubA_gx, pubA_gy, point_pubA, p_ecc_state);
  80. ERROR_BREAK(ipp_ret);
  81. //defense in depth to verify that input public key in ECC group
  82. //a return value of ippECValid indicates the point is on the elliptic curve
  83. //and is not the point at infinity
  84. ipp_ret = ippsECCPCheckPoint(point_pubA, &ipp_result, p_ecc_state);
  85. ERROR_BREAK(ipp_ret);
  86. if (ipp_result != ippECValid )
  87. {
  88. ipp_ret = ippStsIvalidPublicKey;
  89. break;
  90. }
  91. point_R = (IppsECCPPointState*)( malloc(ecPointSize) );
  92. if(!point_R)
  93. {
  94. ipp_ret = ippStsNoMemErr;
  95. break;
  96. }
  97. ipp_ret = ippsECCPPointInit(256, point_R);
  98. ERROR_BREAK(ipp_ret);
  99. ipp_ret = sgx_ipp_newBN(NULL, sizeof(sgx_ec256_dh_shared_t), &BN_dh_shared_x);
  100. ERROR_BREAK(ipp_ret);
  101. ipp_ret = sgx_ipp_newBN(NULL, sizeof(sgx_ec256_dh_shared_t), &BN_dh_shared_y);
  102. ERROR_BREAK(ipp_ret);
  103. ipp_ret = ippsECCPMulPointScalar(point_pubA, BN_dh_privB, point_R, p_ecc_state);
  104. ERROR_BREAK(ipp_ret);
  105. //defense in depth to verify that point_R in ECC group
  106. //a return value of ippECValid indicates the point is on the elliptic curve
  107. //and is not the point at infinity
  108. ipp_ret = ippsECCPCheckPoint(point_R, &ipp_result, p_ecc_state);
  109. ERROR_BREAK(ipp_ret);
  110. if (ipp_result != ippECValid)
  111. {
  112. ipp_ret = ippStsIvalidPublicKey;
  113. break;
  114. }
  115. ipp_ret = ippsECCPGetPoint(BN_dh_shared_x, BN_dh_shared_y, point_R, p_ecc_state);
  116. ERROR_BREAK(ipp_ret);
  117. IppsBigNumSGN sgn = IppsBigNumPOS;
  118. int length = 0;
  119. Ipp32u *pdata = NULL;
  120. ipp_ret = ippsRef_BN(&sgn, &length, &pdata, BN_dh_shared_x);
  121. ERROR_BREAK(ipp_ret);
  122. memset(p_shared_key->x, 0, sizeof(p_shared_key->x));
  123. memcpy(p_shared_key->x, pdata, ROUND_TO(length, 8)/8);
  124. // Clear memory securely
  125. memset_s(pdata, sizeof(p_shared_key->x), 0, ROUND_TO(length, 8)/8);
  126. ipp_ret = ippsRef_BN(&sgn, &length, &pdata, BN_dh_shared_y);
  127. ERROR_BREAK(ipp_ret);
  128. memset(p_shared_key->y, 0, sizeof(p_shared_key->y));
  129. memcpy(p_shared_key->y, pdata, ROUND_TO(length, 8)/8);
  130. // Clear memory securely
  131. memset_s(pdata, sizeof(p_shared_key->x), 0, ROUND_TO(length, 8)/8);
  132. }while(0);
  133. SAFE_FREE(point_pubA);
  134. if (point_R) memset_s(point_R, ecPointSize, 0, ecPointSize);
  135. SAFE_FREE(point_R);
  136. sgx_ipp_secure_free_BN(pubA_gx, sizeof(p_public_ga->gx));
  137. sgx_ipp_secure_free_BN(pubA_gy, sizeof(p_public_ga->gy));
  138. sgx_ipp_secure_free_BN(BN_dh_privB, sizeof(sgx_ec256_private_t));
  139. sgx_ipp_secure_free_BN(BN_dh_shared_x, sizeof(sgx_ec256_dh_shared_t));
  140. sgx_ipp_secure_free_BN(BN_dh_shared_y, sizeof(sgx_ec256_dh_shared_t));
  141. if (ipp_ret == ippStsNoMemErr || ipp_ret == ippStsMemAllocErr)
  142. {
  143. return SGX_ERROR_OUT_OF_MEMORY;
  144. }
  145. if (ipp_ret != ippStsNoErr)
  146. {
  147. return SGX_ERROR_UNEXPECTED;
  148. }
  149. return SGX_SUCCESS;
  150. }