sgx_ecc512.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. /* Computes 512-bit DH shared key based on private B key (local) and remote public Ga Key
  33. * Parameters:
  34. * Return: sgx_status_t - SGX_SUCCESS or failure as defined sgx_error.h
  35. * Inputs: sgx_ecc_state_handle_t ecc_handle ?Handle to ECC crypto system
  36. * sgx_ec256_private_t *p_private_b - Pointer to the local private key - LITTLE ENDIAN
  37. * sgx_ec256_public_t *p_public_ga - Pointer to the remote public key - LITTLE ENDIAN
  38. * Output: sgx_ec256_dh_shared_t *p_shared_key ?Pointer to the shared DH key - LITTLE ENDIAN
  39. x-coordinate of (privKeyB ?pubKeyA) */
  40. sgx_status_t sgx_ecc256_compute_shared_dhkey512(sgx_ec256_private_t *p_private_b,
  41. sgx_ec256_public_t *p_public_ga,
  42. sgx_ec256_dh_shared512_t *p_shared_key,
  43. sgx_ecc_state_handle_t ecc_handle)
  44. {
  45. if ((ecc_handle == NULL) || (p_private_b == NULL) || (p_public_ga == NULL) || (p_shared_key == NULL))
  46. {
  47. return SGX_ERROR_INVALID_PARAMETER;
  48. }
  49. IppsBigNumState* BN_dh_privB = NULL;
  50. IppsBigNumState* BN_dh_share512_x = NULL;
  51. IppsBigNumState* BN_dh_share512_y = NULL;
  52. IppsBigNumState* pubA_gx = NULL;
  53. IppsBigNumState* pubA_gy = NULL;
  54. IppsECCPPointState* point_pubA = NULL;
  55. IppsECCPPointState* point_R = NULL;
  56. IppStatus ipp_ret = ippStsNoErr;
  57. int ecPointSize = 0;
  58. IppECResult ipp_result = ippECValid;
  59. IppsECCPState* p_ecc_state = (IppsECCPState*)ecc_handle;
  60. do
  61. {
  62. ipp_ret = sgx_ipp_newBN((Ipp32u*)p_private_b->r, sizeof(sgx_ec256_private_t), &BN_dh_privB);
  63. ERROR_BREAK(ipp_ret);
  64. ipp_ret = sgx_ipp_newBN((uint32_t*)p_public_ga->gx, sizeof(p_public_ga->gx), &pubA_gx);
  65. ERROR_BREAK(ipp_ret);
  66. ipp_ret = sgx_ipp_newBN((uint32_t*)p_public_ga->gy, sizeof(p_public_ga->gy), &pubA_gy);
  67. ERROR_BREAK(ipp_ret);
  68. ipp_ret = ippsECCPPointGetSize(256, &ecPointSize);
  69. ERROR_BREAK(ipp_ret);
  70. point_pubA = (IppsECCPPointState*)( malloc(ecPointSize) );
  71. if(!point_pubA)
  72. {
  73. ipp_ret = ippStsNoMemErr;
  74. break;
  75. }
  76. ipp_ret = ippsECCPPointInit(256, point_pubA);
  77. ERROR_BREAK(ipp_ret);
  78. ipp_ret = ippsECCPSetPoint(pubA_gx, pubA_gy, point_pubA, p_ecc_state);
  79. ERROR_BREAK(ipp_ret);
  80. //defense in depth to verify that input publick key in ECC group
  81. //a return value of ippECValid indicates the point is on the elliptic curve
  82. //and is not the point at infinity
  83. ipp_ret = ippsECCPCheckPoint(point_pubA, &ipp_result, p_ecc_state);
  84. ERROR_BREAK(ipp_ret);
  85. if (ipp_result != ippECValid )
  86. {
  87. ipp_ret = ippStsIvalidPublicKey;
  88. break;
  89. }
  90. point_R = (IppsECCPPointState*)( malloc(ecPointSize) );
  91. if(!point_R)
  92. {
  93. ipp_ret = ippStsNoMemErr;
  94. break;
  95. }
  96. ipp_ret = ippsECCPPointInit(256, point_R);
  97. ERROR_BREAK(ipp_ret);
  98. ipp_ret = sgx_ipp_newBN(NULL, sizeof(sgx_ec256_dh_shared_t), &BN_dh_share512_x);
  99. ERROR_BREAK(ipp_ret);
  100. ipp_ret = sgx_ipp_newBN(NULL, sizeof(sgx_ec256_dh_shared_t), &BN_dh_share512_y);
  101. ERROR_BREAK(ipp_ret);
  102. ipp_ret = ippsECCPMulPointScalar(point_pubA, BN_dh_privB, point_R, p_ecc_state);
  103. ERROR_BREAK(ipp_ret);
  104. //defense in depth to verify that point_R in ECC group
  105. //a return value of ippECValid indicates the point is on the elliptic curve
  106. //and is not the point at infinity
  107. ipp_ret = ippsECCPCheckPoint(point_R, &ipp_result, p_ecc_state);
  108. ERROR_BREAK(ipp_ret);
  109. if (ipp_result != ippECValid)
  110. {
  111. ipp_ret = ippStsIvalidPublicKey;
  112. break;
  113. }
  114. ipp_ret = ippsECCPGetPoint(BN_dh_share512_x, BN_dh_share512_y, point_R, p_ecc_state);
  115. ERROR_BREAK(ipp_ret);
  116. IppsBigNumSGN sgn = IppsBigNumPOS;
  117. int length = 0;
  118. Ipp32u *pdata = NULL;
  119. ipp_ret = ippsRef_BN(&sgn, &length, &pdata, BN_dh_share512_x);
  120. ERROR_BREAK(ipp_ret);
  121. memset(p_shared_key->x, 0, sizeof(p_shared_key->x));
  122. memcpy(p_shared_key->x, pdata, ROUND_TO(length, 8)/8);
  123. // Clear memory securely
  124. memset_s(pdata, sizeof(p_shared_key->x), 0, ROUND_TO(length, 8)/8);
  125. ipp_ret = ippsRef_BN(&sgn, &length, &pdata, BN_dh_share512_y);
  126. ERROR_BREAK(ipp_ret);
  127. memset(p_shared_key->y, 0, sizeof(p_shared_key->y));
  128. memcpy(p_shared_key->y, pdata, ROUND_TO(length, 8)/8);
  129. // Clear memory securely
  130. memset_s(pdata, sizeof(p_shared_key->x), 0, ROUND_TO(length, 8)/8);
  131. }while(0);
  132. SAFE_FREE(point_pubA);
  133. if (point_R) memset_s(point_R, ecPointSize, 0, ecPointSize);
  134. SAFE_FREE(point_R);
  135. sgx_ipp_secure_free_BN(pubA_gx, sizeof(p_public_ga->gx));
  136. sgx_ipp_secure_free_BN(pubA_gy, sizeof(p_public_ga->gy));
  137. sgx_ipp_secure_free_BN(BN_dh_privB, sizeof(sgx_ec256_private_t));
  138. sgx_ipp_secure_free_BN(BN_dh_share512_x, sizeof(sgx_ec256_dh_shared_t));
  139. sgx_ipp_secure_free_BN(BN_dh_share512_y, sizeof(sgx_ec256_dh_shared_t));
  140. if (ipp_ret == ippStsNoMemErr || ipp_ret == ippStsMemAllocErr)
  141. {
  142. return SGX_ERROR_OUT_OF_MEMORY;
  143. }
  144. if (ipp_ret != ippStsNoErr)
  145. {
  146. return SGX_ERROR_UNEXPECTED;
  147. }
  148. return SGX_SUCCESS;
  149. }