sgx_ecc256.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 "string.h"
  32. #include "se_tcrypto_common.h"
  33. #include <openssl/evp.h>
  34. #include <openssl/ec.h>
  35. #include <openssl/err.h>
  36. #include "sgx_tcrypto.h"
  37. #define POINT_NOT_ON_CURVE 0x1007c06b
  38. /*
  39. * Elliptic Curve Cryptography - Based on GF(p), 256 bit
  40. */
  41. /* Allocates and initializes ecc context
  42. * Parameters:
  43. * Return: sgx_status_t - SGX_SUCCESS or failure as defined sgx_error.h
  44. * Output: sgx_ecc_state_handle_t *p_ecc_handle - Pointer to the handle of ECC crypto system */
  45. sgx_status_t sgx_ecc256_open_context(sgx_ecc_state_handle_t* p_ecc_handle)
  46. {
  47. if (p_ecc_handle == NULL) {
  48. return SGX_ERROR_INVALID_PARAMETER;
  49. }
  50. sgx_status_t retval = SGX_SUCCESS;
  51. CLEAR_OPENSSL_ERROR_QUEUE;
  52. /* construct a curve p-256 */
  53. EC_GROUP* ec_group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
  54. if (NULL == ec_group) {
  55. GET_LAST_OPENSSL_ERROR;
  56. retval = SGX_ERROR_UNEXPECTED;
  57. } else {
  58. *p_ecc_handle = (void*)ec_group;
  59. }
  60. return retval;
  61. }
  62. /* Cleans up ecc context
  63. * Parameters:
  64. * Return: sgx_status_t - SGX_SUCCESS or failure as defined sgx_error.h
  65. * Output: sgx_ecc_state_handle_t ecc_handle - Handle to ECC crypto system */
  66. sgx_status_t sgx_ecc256_close_context(sgx_ecc_state_handle_t ecc_handle)
  67. {
  68. if (ecc_handle == NULL) {
  69. return SGX_ERROR_INVALID_PARAMETER;
  70. }
  71. EC_GROUP_free((EC_GROUP*)ecc_handle);
  72. return SGX_SUCCESS;
  73. }
  74. /* Populates private/public key pair - caller code allocates memory
  75. * Parameters:
  76. * Return: sgx_status_t - SGX_SUCCESS or failure as defined sgx_error.h
  77. * Inputs: sgx_ecc_state_handle_t ecc_handle - Handle to ECC crypto system
  78. * Outputs: sgx_ec256_private_t *p_private - Pointer to the private key
  79. * sgx_ec256_public_t *p_public - Pointer to the public key */
  80. sgx_status_t sgx_ecc256_create_key_pair(sgx_ec256_private_t *p_private,
  81. sgx_ec256_public_t *p_public,
  82. sgx_ecc_state_handle_t ecc_handle)
  83. {
  84. if ((ecc_handle == NULL) || (p_private == NULL) || (p_public == NULL)) {
  85. return SGX_ERROR_INVALID_PARAMETER;
  86. }
  87. EC_GROUP *ec_group = (EC_GROUP*) ecc_handle;
  88. EC_KEY *ec_key = NULL;
  89. BIGNUM *pub_k_x = NULL;
  90. BIGNUM *pub_k_y = NULL;
  91. const EC_POINT *public_k = NULL;
  92. const BIGNUM *private_k = NULL;
  93. sgx_status_t ret = SGX_ERROR_UNEXPECTED;
  94. CLEAR_OPENSSL_ERROR_QUEUE;
  95. do {
  96. // create new EC key
  97. //
  98. ec_key = EC_KEY_new();
  99. if (NULL == ec_key) {
  100. ret = SGX_ERROR_OUT_OF_MEMORY;
  101. break;
  102. }
  103. // set key's group (curve)
  104. //
  105. if (0 == EC_KEY_set_group (ec_key, ec_group)) {
  106. break;
  107. }
  108. // generate key pair, based on the curve set
  109. //
  110. if (0 == EC_KEY_generate_key(ec_key)) {
  111. break;
  112. }
  113. pub_k_x = BN_new();
  114. pub_k_y = BN_new();
  115. if (NULL == pub_k_x || NULL == pub_k_y) {
  116. ret = SGX_ERROR_OUT_OF_MEMORY;
  117. break;
  118. }
  119. // This OPENSSL API doesn't validate user's parameters
  120. // get public and private keys
  121. //
  122. public_k = EC_KEY_get0_public_key(ec_key);
  123. if (NULL == ec_key) {
  124. break;
  125. }
  126. private_k = EC_KEY_get0_private_key(ec_key);
  127. if (NULL == ec_key) {
  128. break;
  129. }
  130. // extract two BNs representing the public key
  131. //
  132. if (!EC_POINT_get_affine_coordinates_GFp(ec_group, public_k, pub_k_x, pub_k_y, NULL)) {
  133. break;
  134. }
  135. // convert private key BN to little-endian unsigned char form
  136. //
  137. if (-1 == BN_bn2lebinpad(private_k, (unsigned char*)p_private, SGX_ECP256_KEY_SIZE)) {
  138. break;
  139. }
  140. // convert public key BN to little-endian unsigned char form
  141. //
  142. if (-1 == BN_bn2lebinpad(pub_k_x, (unsigned char*)p_public->gx, SGX_ECP256_KEY_SIZE)) {
  143. break;
  144. }
  145. // convert public key BN to little-endian unsigned char form
  146. //
  147. if (-1 == BN_bn2lebinpad(pub_k_y, (unsigned char*)p_public->gy, SGX_ECP256_KEY_SIZE)) {
  148. break;
  149. }
  150. ret = SGX_SUCCESS;
  151. } while(0);
  152. if (SGX_SUCCESS != ret) {
  153. GET_LAST_OPENSSL_ERROR;
  154. // in case of error, clear output buffers
  155. //
  156. memset_s(p_private, sizeof(p_private), 0, sizeof(p_private));
  157. memset_s(p_public->gx, sizeof(p_public->gx), 0, sizeof(p_public->gx));
  158. memset_s(p_public->gy, sizeof(p_public->gy), 0, sizeof(p_public->gy));
  159. }
  160. //free temp data
  161. //
  162. EC_KEY_free(ec_key);
  163. BN_clear_free(pub_k_x);
  164. BN_clear_free(pub_k_y);
  165. return ret;
  166. }
  167. /* Checks whether the input point is a valid point on the given elliptic curve
  168. * Parameters:
  169. * Return: sgx_status_t - SGX_SUCCESS or failure as defined sgx_error.h
  170. * Inputs: sgx_ecc_state_handle_t ecc_handle - Handle to ECC crypto system
  171. * sgx_ec256_public_t *p_point - Pointer to perform validity check on - LITTLE ENDIAN
  172. * Output: int *p_valid - Return 0 if the point is an invalid point on ECC curve */
  173. sgx_status_t sgx_ecc256_check_point(const sgx_ec256_public_t *p_point,
  174. const sgx_ecc_state_handle_t ecc_handle,
  175. int *p_valid)
  176. {
  177. if ((ecc_handle == NULL) || (p_point == NULL) || (p_valid == NULL)) {
  178. return SGX_ERROR_INVALID_PARAMETER;
  179. }
  180. sgx_status_t retval = SGX_ERROR_UNEXPECTED;
  181. EC_POINT *ec_point = NULL;
  182. BIGNUM *b_x = NULL;
  183. BIGNUM *b_y = NULL;
  184. int ret_point_on_curve = 0;
  185. unsigned long internal_openssl_error = 0;
  186. CLEAR_OPENSSL_ERROR_QUEUE;
  187. do {
  188. // converts the x value of the point, represented as positive integer in little-endian into a BIGNUM
  189. //
  190. b_x = BN_lebin2bn(p_point->gx, SGX_ECP256_KEY_SIZE, NULL);
  191. if (NULL == b_x) {
  192. break;
  193. }
  194. // converts the y value of the point, represented as positive integer in little-endian into a BIGNUM
  195. //
  196. b_y = BN_lebin2bn(p_point->gy, SGX_ECP256_KEY_SIZE, NULL);
  197. if (NULL == b_y) {
  198. break;
  199. }
  200. // creates new point and assigned the group object that the point relates to
  201. //
  202. ec_point = EC_POINT_new((const EC_GROUP*)ecc_handle);
  203. if (NULL == ec_point) {
  204. retval = SGX_ERROR_OUT_OF_MEMORY;
  205. break;
  206. }
  207. // sets point based on x,y coordinates
  208. //
  209. if (1 != EC_POINT_set_affine_coordinates_GFp((const EC_GROUP*)ecc_handle, ec_point, b_x, b_y, NULL)) {
  210. internal_openssl_error = ERR_get_error();
  211. if (internal_openssl_error == POINT_NOT_ON_CURVE) {
  212. /* fails if point not on curve */
  213. *p_valid = 0;
  214. retval = SGX_SUCCESS;
  215. } else {
  216. #ifdef DEBUG
  217. openssl_last_err = internal_openssl_error;
  218. #endif /* DEBUG */
  219. }
  220. break;
  221. }
  222. // checks if point is on curve
  223. //
  224. ret_point_on_curve = EC_POINT_is_on_curve((const EC_GROUP*)ecc_handle, ec_point, NULL);
  225. if (-1 == ret_point_on_curve) {
  226. break;
  227. }
  228. *p_valid = ret_point_on_curve;
  229. retval = SGX_SUCCESS;
  230. } while(0);
  231. #ifdef DEBUG
  232. if (SGX_SUCCESS != retval && 0 != openssl_last_err) {
  233. GET_LAST_OPENSSL_ERROR;
  234. }
  235. #endif /* DEBUG */
  236. if (ec_point)
  237. EC_POINT_clear_free(ec_point);
  238. if (b_x)
  239. BN_clear_free(b_x);
  240. if (b_y)
  241. BN_clear_free(b_y);
  242. return retval;
  243. }
  244. /* Computes DH shared key based on private B key (local) and remote public Ga Key
  245. * Parameters:
  246. * Return: sgx_status_t - SGX_SUCCESS or failure as defined sgx_error.h
  247. * Inputs: sgx_ecc_state_handle_t ecc_handle - Handle to ECC crypto system
  248. * sgx_ec256_private_t *p_private_b - Pointer to the local private key - LITTLE ENDIAN
  249. * sgx_ec256_public_t *p_public_ga - Pointer to the remote public key - LITTLE ENDIAN
  250. * Output: sgx_ec256_dh_shared_t *p_shared_key - Pointer to the shared DH key - LITTLE ENDIAN
  251. x-coordinate of (privKeyB - pubKeyA) */
  252. sgx_status_t sgx_ecc256_compute_shared_dhkey(sgx_ec256_private_t *p_private_b,
  253. sgx_ec256_public_t *p_public_ga,
  254. sgx_ec256_dh_shared_t *p_shared_key,
  255. sgx_ecc_state_handle_t ecc_handle)
  256. {
  257. if ((ecc_handle == NULL) || (p_private_b == NULL) || (p_public_ga == NULL) || (p_shared_key == NULL)) {
  258. return SGX_ERROR_INVALID_PARAMETER;
  259. }
  260. sgx_status_t ret = SGX_ERROR_UNEXPECTED;
  261. EC_GROUP *ec_group = (EC_GROUP*) ecc_handle;
  262. EC_POINT *point_pubA = NULL;
  263. EC_KEY* private_key = NULL;
  264. BIGNUM *BN_dh_privB = NULL;
  265. BIGNUM *pubA_gx = NULL;
  266. BIGNUM *pubA_gy = NULL;
  267. BIGNUM *tmp = NULL;
  268. CLEAR_OPENSSL_ERROR_QUEUE;
  269. do {
  270. // get BN from public key and private key
  271. //
  272. BN_dh_privB = BN_lebin2bn((unsigned char*)p_private_b->r, sizeof(sgx_ec256_private_t), 0);
  273. if (BN_dh_privB == NULL) {
  274. break;
  275. }
  276. pubA_gx = BN_lebin2bn((unsigned char*)p_public_ga->gx, sizeof(sgx_ec256_private_t), 0);
  277. if (pubA_gx == NULL) {
  278. break;
  279. }
  280. pubA_gy = BN_lebin2bn((unsigned char*)p_public_ga->gy, sizeof(sgx_ec256_private_t), 0);
  281. if (pubA_gy == NULL) {
  282. break;
  283. }
  284. // set point based on pub key x and y
  285. //
  286. point_pubA = EC_POINT_new(ec_group);
  287. if (point_pubA == NULL) {
  288. ret = SGX_ERROR_OUT_OF_MEMORY;
  289. break;
  290. }
  291. // create point (public key) based on public key's x,y coordinates
  292. //
  293. if (EC_POINT_set_affine_coordinates_GFp(ec_group, point_pubA, pubA_gx, pubA_gy, NULL) != 1) {
  294. break;
  295. }
  296. // check point if valid, point is on curve
  297. //
  298. if (EC_POINT_is_on_curve(ec_group, point_pubA, NULL) != 1) {
  299. break;
  300. }
  301. // create empty shared key BN
  302. //
  303. private_key = EC_KEY_new();
  304. if (private_key == NULL) {
  305. ret = SGX_ERROR_OUT_OF_MEMORY;
  306. break;
  307. }
  308. // init private key group (set curve)
  309. //
  310. if (EC_KEY_set_group (private_key, ec_group) != 1) {
  311. break;
  312. }
  313. // init private key with BN value
  314. //
  315. if (EC_KEY_set_private_key(private_key, BN_dh_privB) != 1) {
  316. break;
  317. }
  318. // calculate shared dh key
  319. //
  320. size_t shared_key_len = sizeof(sgx_ec256_dh_shared_t);
  321. shared_key_len = ECDH_compute_key(&(p_shared_key->s), shared_key_len, point_pubA, private_key, NULL);
  322. if (shared_key_len <= 0) {
  323. break;
  324. }
  325. // convert big endian to little endian
  326. //
  327. tmp = BN_bin2bn((unsigned char*)&(p_shared_key->s), sizeof(sgx_ec256_dh_shared_t), 0);
  328. if (tmp == NULL) {
  329. break;
  330. }
  331. if (BN_bn2lebinpad(tmp, p_shared_key->s, sizeof(sgx_ec256_dh_shared_t)) == -1) {
  332. break;
  333. }
  334. ret = SGX_SUCCESS;
  335. } while(0);
  336. if (ret != SGX_SUCCESS) {
  337. GET_LAST_OPENSSL_ERROR;
  338. memset_s(p_shared_key->s, sizeof(p_shared_key->s), 0, sizeof(p_shared_key->s));
  339. }
  340. // clear and free memory
  341. //
  342. EC_POINT_clear_free(point_pubA);
  343. EC_KEY_free(private_key);
  344. BN_clear_free(BN_dh_privB);
  345. BN_clear_free(pubA_gx);
  346. BN_clear_free(pubA_gy);
  347. BN_clear_free(tmp);
  348. return ret;
  349. }