sgx_ecc256_internal.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (C) 2011-2018 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 "string.h"
  32. #include "se_tcrypto_common.h"
  33. #include "sgx_ecc256_internal.h"
  34. #include <openssl/evp.h>
  35. #include <openssl/ec.h>
  36. #include <openssl/err.h>
  37. #include "sgx_tcrypto.h"
  38. /* Computes a point with scalar multiplication based on private B key (local) and remote public Ga Key
  39. * Parameters:
  40. * Return: sgx_status_t - SGX_SUCCESS or failure as defined sgx_error.h
  41. * Inputs: sgx_ecc_state_handle_t ecc_handle - Handle to ECC crypto system
  42. * sgx_ec256_private_t *p_private_b - Pointer to the local private key - LITTLE ENDIAN
  43. * sgx_ec256_public_t *p_public_ga - Pointer to the remote public key - LITTLE ENDIAN
  44. * Output: sgx_ec256_shared_point_t *p_shared_key - Pointer to the target shared point - LITTLE ENDIAN
  45. x-coordinate of (privKeyB - pubKeyA) */
  46. sgx_status_t sgx_ecc256_compute_shared_point(sgx_ec256_private_t *p_private_b,
  47. sgx_ec256_public_t *p_public_ga,
  48. sgx_ec256_shared_point_t *p_shared_key,
  49. sgx_ecc_state_handle_t ecc_handle)
  50. {
  51. if ((ecc_handle == NULL) || (p_private_b == NULL) || (p_public_ga == NULL) || (p_shared_key == NULL))
  52. {
  53. return SGX_ERROR_INVALID_PARAMETER;
  54. }
  55. sgx_status_t ret = SGX_ERROR_UNEXPECTED;
  56. EC_GROUP *ec_group = (EC_GROUP*) ecc_handle;
  57. EC_POINT *point_pubA = NULL;
  58. EC_POINT *point_R = NULL;
  59. BIGNUM *BN_dh_privB = NULL;
  60. BIGNUM *pubA_gx = NULL;
  61. BIGNUM *pubA_gy = NULL;
  62. BIGNUM *BN_dh_shared_x = NULL;
  63. BIGNUM *BN_dh_shared_y = NULL;
  64. CLEAR_OPENSSL_ERROR_QUEUE;
  65. do {
  66. //get BN from public key and private key
  67. //
  68. BN_dh_privB = BN_lebin2bn((unsigned char*)p_private_b->r, sizeof(sgx_ec256_private_t), 0);
  69. if (BN_dh_privB == NULL) {
  70. break;
  71. }
  72. pubA_gx = BN_lebin2bn((unsigned char*)p_public_ga->gx, sizeof(p_public_ga->gx), 0);
  73. if (pubA_gx == NULL) {
  74. break;
  75. }
  76. pubA_gy = BN_lebin2bn((unsigned char*)p_public_ga->gy, sizeof(p_public_ga->gy), 0);
  77. if (pubA_gy == NULL) {
  78. break;
  79. }
  80. //set point based on pub key x and y
  81. //
  82. point_pubA = EC_POINT_new(ec_group);
  83. if (point_pubA == NULL) {
  84. ret = SGX_ERROR_OUT_OF_MEMORY;
  85. break;
  86. }
  87. //create point (public key) based on public key's x,y coordinates
  88. //
  89. if (EC_POINT_set_affine_coordinates_GFp(ec_group, point_pubA, pubA_gx, pubA_gy, NULL) != 1) {
  90. break;
  91. }
  92. //check point if valid, point is on curve
  93. //
  94. if (EC_POINT_is_on_curve(ec_group, point_pubA, NULL) != 1) {
  95. break;
  96. }
  97. //create new point R
  98. //
  99. point_R = EC_POINT_new(ec_group);
  100. if (point_R == NULL) {
  101. ret = SGX_ERROR_OUT_OF_MEMORY;
  102. break;
  103. }
  104. //multiply pointA with privateKey BN scalar, to get point R.
  105. //after, R's x and y are the shred d key
  106. //
  107. if (EC_POINT_mul(ec_group, point_R, NULL, point_pubA, BN_dh_privB, NULL) != 1) {
  108. break;
  109. }
  110. //check point if valid, point is on curve
  111. //
  112. if (EC_POINT_is_on_curve(ec_group, point_R, NULL) != 1) {
  113. break;
  114. }
  115. BN_dh_shared_x = BN_new();
  116. if (BN_dh_shared_x == NULL) {
  117. ret = SGX_ERROR_OUT_OF_MEMORY;
  118. break;
  119. }
  120. BN_dh_shared_y = BN_new();
  121. if (BN_dh_shared_y == NULL) {
  122. ret = SGX_ERROR_OUT_OF_MEMORY;
  123. break;
  124. }
  125. if (EC_POINT_get_affine_coordinates_GFp(ec_group, point_R, BN_dh_shared_x, BN_dh_shared_y, NULL) != 1) {
  126. break;
  127. }
  128. if (BN_bn2lebinpad(BN_dh_shared_x, (unsigned char*)p_shared_key->x, sizeof(p_shared_key->x)) == -1) {
  129. break;
  130. }
  131. if (BN_bn2lebinpad(BN_dh_shared_y, (unsigned char*)p_shared_key->y, sizeof(p_shared_key->y)) == -1) {
  132. break;
  133. }
  134. ret = SGX_SUCCESS;
  135. } while(0);
  136. if (ret != SGX_SUCCESS) {
  137. GET_LAST_OPENSSL_ERROR;
  138. memset_s(p_shared_key->x, sizeof(p_shared_key->x), 0, sizeof(p_shared_key->x));
  139. memset_s(p_shared_key->y, sizeof(p_shared_key->y), 0, sizeof(p_shared_key->y));
  140. }
  141. //Free and clean all memory
  142. //
  143. EC_POINT_clear_free(point_pubA);
  144. EC_POINT_clear_free(point_R);
  145. BN_clear_free(BN_dh_shared_x);
  146. BN_clear_free(BN_dh_shared_y);
  147. BN_clear_free(BN_dh_privB);
  148. BN_clear_free(pubA_gx);
  149. BN_clear_free(pubA_gy);
  150. return ret;
  151. }