sgx_ecc256_ecdsa.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 "se_tcrypto_common.h"
  32. #include <openssl/sha.h>
  33. #include <openssl/ec.h>
  34. #include <openssl/bn.h>
  35. #include <openssl/err.h>
  36. #include "sgx_tcrypto.h"
  37. /* Computes signature for data based on private key
  38. * Parameters:
  39. * Return: sgx_status_t - SGX_SUCCESS or failure as defined sgx_error.h
  40. * Inputs: sgx_ecc_state_handle_t ecc_handle - Handle to ECC crypto system
  41. * sgx_ec256_private_t *p_private - Pointer to the private key - LITTLE ENDIAN
  42. * sgx_uint8_t *p_data - Pointer to the data to be signed
  43. * uint32_t data_size - Size of the data to be signed
  44. * Output: sgx_ec256_signature_t *p_signature - Pointer to the signature - LITTLE ENDIAN */
  45. sgx_status_t sgx_ecdsa_sign(const uint8_t *p_data,
  46. uint32_t data_size,
  47. sgx_ec256_private_t *p_private,
  48. sgx_ec256_signature_t *p_signature,
  49. sgx_ecc_state_handle_t ecc_handle)
  50. {
  51. if ((ecc_handle == NULL) || (p_private == NULL) || (p_signature == NULL) || (p_data == NULL) || (data_size < 1))
  52. {
  53. return SGX_ERROR_INVALID_PARAMETER;
  54. }
  55. EC_KEY *private_key = NULL;
  56. BIGNUM *bn_priv = NULL;
  57. ECDSA_SIG *ecdsa_sig = NULL;
  58. const BIGNUM *r = NULL;
  59. const BIGNUM *s = NULL;
  60. unsigned char digest[SGX_SHA256_HASH_SIZE] = { 0 };
  61. int written_bytes = 0;
  62. int sig_size = 0;
  63. int max_sig_size = 0;
  64. sgx_status_t retval = SGX_ERROR_UNEXPECTED;
  65. CLEAR_OPENSSL_ERROR_QUEUE;
  66. do {
  67. // converts the r value of private key, represented as positive integer in little-endian into a BIGNUM
  68. //
  69. bn_priv = BN_lebin2bn((unsigned char*)p_private->r, sizeof(p_private->r), 0);
  70. if (NULL == bn_priv) {
  71. break;
  72. }
  73. // create empty ecc key
  74. //
  75. private_key = EC_KEY_new();
  76. if (NULL == private_key) {
  77. retval = SGX_ERROR_OUT_OF_MEMORY;
  78. break;
  79. }
  80. // sets ecc key group (set curve)
  81. //
  82. if (1 != EC_KEY_set_group(private_key, (EC_GROUP*)ecc_handle)) {
  83. break;
  84. }
  85. // uses bn_priv to set the ecc private key
  86. //
  87. if (1 != EC_KEY_set_private_key(private_key, bn_priv)) {
  88. break;
  89. }
  90. /* generates digest of p_data */
  91. if (NULL == SHA256((const unsigned char *)p_data, data_size, (unsigned char *)digest)) {
  92. break;
  93. }
  94. // computes a digital signature of the SGX_SHA256_HASH_SIZE bytes hash value dgst using the private EC key private_key.
  95. // the signature is returned as a newly allocated ECDSA_SIG structure.
  96. //
  97. ecdsa_sig = ECDSA_do_sign(digest, SGX_SHA256_HASH_SIZE, private_key);
  98. if (NULL == ecdsa_sig) {
  99. break;
  100. }
  101. // returns internal pointers the r and s values contained in ecdsa_sig.
  102. ECDSA_SIG_get0(ecdsa_sig, &r, &s);
  103. // converts the r BIGNUM of the signature to little endian buffer, bounded with the len of out buffer
  104. //
  105. written_bytes = BN_bn2lebinpad(r, (unsigned char*)p_signature->x, SGX_ECP256_KEY_SIZE);
  106. if (0 >= written_bytes) {
  107. break;
  108. }
  109. sig_size = written_bytes;
  110. // converts the s BIGNUM of the signature to little endian buffer, bounded with the len of out buffer
  111. //
  112. written_bytes = BN_bn2lebinpad(s, (unsigned char*)p_signature->y, SGX_ECP256_KEY_SIZE);
  113. if (0 >= written_bytes) {
  114. break;
  115. }
  116. sig_size += written_bytes;
  117. // returns the maximum length of a DER encoded ECDSA signature created with the private EC key.
  118. //
  119. max_sig_size = ECDSA_size(private_key);
  120. if (max_sig_size <= 0) {
  121. break;
  122. }
  123. // checks if the signature size not larger than the max len of valid signature
  124. // this check if done for validity, not for overflow.
  125. //
  126. if (sig_size > max_sig_size) {
  127. break;
  128. }
  129. retval = SGX_SUCCESS;
  130. } while(0);
  131. if (SGX_SUCCESS != retval) {
  132. GET_LAST_OPENSSL_ERROR;
  133. }
  134. if (bn_priv)
  135. BN_clear_free(bn_priv);
  136. if (ecdsa_sig)
  137. ECDSA_SIG_free(ecdsa_sig);
  138. if (private_key)
  139. EC_KEY_free(private_key);
  140. return retval;
  141. }
  142. /* Verifies the signature for the given data based on the public key
  143. *
  144. * Parameters:
  145. * Return: sgx_status_t - SGX_SUCCESS or failure as defined sgx_error.h
  146. * Inputs: sgx_ecc_state_handle_t ecc_handle - Handle to ECC crypto system
  147. * sgx_ec256_public_t *p_public - Pointer to the public key - LITTLE ENDIAN
  148. * uint8_t *p_data - Pointer to the data to be signed
  149. * uint32_t data_size - Size of the data to be signed
  150. * sgx_ec256_signature_t *p_signature - Pointer to the signature - LITTLE ENDIAN
  151. * Output: uint8_t *p_result - Pointer to the result of verification check */
  152. sgx_status_t sgx_ecdsa_verify(const uint8_t *p_data,
  153. uint32_t data_size,
  154. const sgx_ec256_public_t *p_public,
  155. sgx_ec256_signature_t *p_signature,
  156. uint8_t *p_result,
  157. sgx_ecc_state_handle_t ecc_handle)
  158. {
  159. if ((ecc_handle == NULL) || (p_public == NULL) || (p_signature == NULL) ||
  160. (p_data == NULL) || (data_size < 1) || (p_result == NULL))
  161. {
  162. return SGX_ERROR_INVALID_PARAMETER;
  163. }
  164. EC_KEY *public_key = NULL;
  165. BIGNUM *bn_pub_x = NULL;
  166. BIGNUM *bn_pub_y = NULL;
  167. BIGNUM *bn_r = NULL;
  168. BIGNUM *bn_s = NULL;
  169. BIGNUM *prev_bn_r = NULL;
  170. BIGNUM *prev_bn_s = NULL;
  171. EC_POINT *public_point = NULL;
  172. ECDSA_SIG *ecdsa_sig = NULL;
  173. unsigned char digest[SGX_SHA256_HASH_SIZE] = { 0 };
  174. sgx_status_t retval = SGX_ERROR_UNEXPECTED;
  175. int valid = 0;
  176. *p_result = SGX_EC_INVALID_SIGNATURE;
  177. CLEAR_OPENSSL_ERROR_QUEUE;
  178. do {
  179. // converts the x value of public key, represented as positive integer in little-endian into a BIGNUM
  180. //
  181. bn_pub_x = BN_lebin2bn((unsigned char*)p_public->gx, sizeof(p_public->gx), 0);
  182. if (NULL == bn_pub_x) {
  183. break;
  184. }
  185. // converts the y value of public key, represented as positive integer in little-endian into a BIGNUM
  186. //
  187. bn_pub_y = BN_lebin2bn((unsigned char*)p_public->gy, sizeof(p_public->gy), 0);
  188. if (NULL == bn_pub_y) {
  189. break;
  190. }
  191. // converts the x value of the signature, represented as positive integer in little-endian into a BIGNUM
  192. //
  193. bn_r = BN_lebin2bn((unsigned char*)p_signature->x, sizeof(p_signature->x), 0);
  194. if (NULL == bn_r) {
  195. break;
  196. }
  197. // converts the y value of the signature, represented as positive integer in little-endian into a BIGNUM
  198. //
  199. bn_s = BN_lebin2bn((unsigned char*)p_signature->y, sizeof(p_signature->y), 0);
  200. if (NULL == bn_s) {
  201. break;
  202. }
  203. // creates new point and assigned the group object that the point relates to
  204. //
  205. public_point = EC_POINT_new((EC_GROUP*)ecc_handle);
  206. if (public_point == NULL) {
  207. retval = SGX_ERROR_OUT_OF_MEMORY;
  208. break;
  209. }
  210. // sets point based on public key's x,y coordinates
  211. //
  212. if (1 != EC_POINT_set_affine_coordinates_GFp((EC_GROUP*)ecc_handle, public_point, bn_pub_x, bn_pub_y, NULL)) {
  213. break;
  214. }
  215. // check point if the point is on curve
  216. //
  217. if (1 != EC_POINT_is_on_curve((EC_GROUP*)ecc_handle, public_point, NULL)) {
  218. break;
  219. }
  220. // create empty ecc key
  221. //
  222. public_key = EC_KEY_new();
  223. if (NULL == public_key) {
  224. retval = SGX_ERROR_OUT_OF_MEMORY;
  225. break;
  226. }
  227. // sets ecc key group (set curve)
  228. //
  229. if (1 != EC_KEY_set_group(public_key, (EC_GROUP*)ecc_handle)) {
  230. break;
  231. }
  232. // uses the created point to set the public key value
  233. //
  234. if (1 != EC_KEY_set_public_key(public_key, public_point)) {
  235. break;
  236. }
  237. /* generates digest of p_data */
  238. if (NULL == SHA256((const unsigned char *)p_data, data_size, (unsigned char *)digest)) {
  239. break;
  240. }
  241. // allocates a new ECDSA_SIG structure (note: this function also allocates the BIGNUMs) and initialize it
  242. //
  243. ecdsa_sig = ECDSA_SIG_new();
  244. if (NULL == ecdsa_sig) {
  245. retval = SGX_ERROR_OUT_OF_MEMORY;
  246. break;
  247. }
  248. // free internal allocated BIGBNUMs
  249. ECDSA_SIG_get0(ecdsa_sig, (const BIGNUM **)&prev_bn_r, (const BIGNUM **)&prev_bn_s);
  250. if (prev_bn_r)
  251. BN_clear_free(prev_bn_r);
  252. if (prev_bn_s)
  253. BN_clear_free(prev_bn_s);
  254. // setes the r and s values of ecdsa_sig
  255. // calling this function transfers the memory management of the values to the ECDSA_SIG object,
  256. // and therefore the values that have been passed in should not be freed directly after this function has been called
  257. //
  258. if (1 != ECDSA_SIG_set0(ecdsa_sig, bn_r, bn_s)) {
  259. ECDSA_SIG_free(ecdsa_sig);
  260. ecdsa_sig = NULL;
  261. break;
  262. }
  263. // verifies that the signature ecdsa_sig is a valid ECDSA signature of the hash value digest of size SGX_SHA256_HASH_SIZE using the public key public_key
  264. //
  265. valid = ECDSA_do_verify(digest, SGX_SHA256_HASH_SIZE, ecdsa_sig, public_key);
  266. if (-1 == valid) {
  267. break;
  268. }
  269. // sets the p_result based on ECDSA_do_verify result
  270. //
  271. if (valid) {
  272. *p_result = SGX_EC_VALID;
  273. }
  274. retval = SGX_SUCCESS;
  275. } while(0);
  276. if (SGX_SUCCESS != retval) {
  277. GET_LAST_OPENSSL_ERROR;
  278. }
  279. if (bn_pub_x)
  280. BN_clear_free(bn_pub_x);
  281. if (bn_pub_y)
  282. BN_clear_free(bn_pub_y);
  283. if (public_point)
  284. EC_POINT_clear_free(public_point);
  285. if (ecdsa_sig) {
  286. ECDSA_SIG_free(ecdsa_sig);
  287. bn_r = NULL;
  288. bn_s = NULL;
  289. }
  290. if (public_key)
  291. EC_KEY_free(public_key);
  292. if (bn_r)
  293. BN_clear_free(bn_r);
  294. if (bn_s)
  295. BN_clear_free(bn_s);
  296. return retval;
  297. }