sgx_tcrypto.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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. /**
  32. * File: sgx_tcrypto.h
  33. * Description:
  34. * Interface for generic crypto library APIs required in SDK implementation.
  35. */
  36. #ifndef _SGX_TCRYPTO_H_
  37. #define _SGX_TCRYPTO_H_
  38. #include "sgx.h"
  39. #include "sgx_defs.h"
  40. #define SGX_SHA256_HASH_SIZE 32
  41. #define SGX_ECP256_KEY_SIZE 32
  42. #define SGX_NISTP_ECP256_KEY_SIZE (SGX_ECP256_KEY_SIZE/sizeof(uint32_t))
  43. #define SGX_AESGCM_IV_SIZE 12
  44. #define SGX_AESGCM_KEY_SIZE 16
  45. #define SGX_AESGCM_MAC_SIZE 16
  46. #define SGX_CMAC_KEY_SIZE 16
  47. #define SGX_CMAC_MAC_SIZE 16
  48. #define SGX_AESCTR_KEY_SIZE 16
  49. typedef struct _sgx_ec256_dh_shared_t
  50. {
  51. uint8_t s[SGX_ECP256_KEY_SIZE];
  52. } sgx_ec256_dh_shared_t;
  53. typedef struct _sgx_ec256_dh_shared512_t
  54. {
  55. uint8_t x[SGX_ECP256_KEY_SIZE];
  56. uint8_t y[SGX_ECP256_KEY_SIZE];
  57. } sgx_ec256_dh_shared512_t;
  58. typedef struct _sgx_ec256_private_t
  59. {
  60. uint8_t r[SGX_ECP256_KEY_SIZE];
  61. } sgx_ec256_private_t;
  62. typedef struct _sgx_ec256_public_t
  63. {
  64. uint8_t gx[SGX_ECP256_KEY_SIZE];
  65. uint8_t gy[SGX_ECP256_KEY_SIZE];
  66. } sgx_ec256_public_t;
  67. typedef struct _sgx_ec256_signature_t
  68. {
  69. uint32_t x[SGX_NISTP_ECP256_KEY_SIZE];
  70. uint32_t y[SGX_NISTP_ECP256_KEY_SIZE];
  71. } sgx_ec256_signature_t;
  72. typedef void* sgx_sha_state_handle_t;
  73. typedef void* sgx_cmac_state_handle_t;
  74. typedef void* sgx_ecc_state_handle_t;
  75. typedef uint8_t sgx_sha256_hash_t[SGX_SHA256_HASH_SIZE];
  76. typedef uint8_t sgx_aes_gcm_128bit_key_t[SGX_AESGCM_KEY_SIZE];
  77. typedef uint8_t sgx_aes_gcm_128bit_tag_t[SGX_AESGCM_MAC_SIZE];
  78. typedef uint8_t sgx_cmac_128bit_key_t[SGX_CMAC_KEY_SIZE];
  79. typedef uint8_t sgx_cmac_128bit_tag_t[SGX_CMAC_MAC_SIZE];
  80. typedef uint8_t sgx_aes_ctr_128bit_key_t[SGX_AESCTR_KEY_SIZE];
  81. typedef enum {
  82. SGX_EC_VALID, /* validation pass successfully */
  83. SGX_EC_COMPOSITE_BASE, /* field based on composite */
  84. SGX_EC_COMPLICATED_BASE, /* number of non-zero terms in the polynomial (> PRIME_ARR_MAX) */
  85. SGX_EC_IS_ZERO_DISCRIMINANT,/* zero discriminant */
  86. SGX_EC_COMPOSITE_ORDER, /* composite order of base point */
  87. SGX_EC_INVALID_ORDER, /* invalid base point order */
  88. SGX_EC_IS_WEAK_MOV, /* weak Meneze-Okamoto-Vanstone reduction attack */
  89. SGX_EC_IS_WEAK_SSA, /* weak Semaev-Smart,Satoh-Araki reduction attack */
  90. SGX_EC_IS_SUPER_SINGULAR, /* supersingular curve */
  91. SGX_EC_INVALID_PRIVATE_KEY, /* !(0 < Private < order) */
  92. SGX_EC_INVALID_PUBLIC_KEY, /* (order*PublicKey != Infinity) */
  93. SGX_EC_INVALID_KEY_PAIR, /* (Private*BasePoint != PublicKey) */
  94. SGX_EC_POINT_OUT_OF_GROUP, /* out of group (order*P != Infinity) */
  95. SGX_EC_POINT_IS_AT_INFINITY,/* point (P=(Px,Py)) at Infinity */
  96. SGX_EC_POINT_IS_NOT_VALID, /* point (P=(Px,Py)) out-of EC */
  97. SGX_EC_POINT_IS_EQUAL, /* compared points are equal */
  98. SGX_EC_POINT_IS_NOT_EQUAL, /* compared points are different */
  99. SGX_EC_INVALID_SIGNATURE /* invalid signature */
  100. } sgx_generic_ecresult_t;
  101. #ifdef __cplusplus
  102. extern "C" {
  103. #endif
  104. /** SHA Hashing functions - NOTE: ONLY 256-bit is supported.
  105. *
  106. * NOTE: Use sgx_sha256_msg if the src pointer contains the complete msg to perform hash (Option 1)
  107. * Else use the Init, Update, Update, ..., Final procedure (Option 2)
  108. * Option 1: If the complete dataset is available for hashing, sgx_sha256_msg
  109. * is a single API call for generating the 256bit hash for the given dataset.
  110. * Return: If source pointer or hash pointer are NULL, SGX_ERROR_INVALID_PARAMETER is returned.
  111. * If hash function fails then SGX_ERROR_UNEXPECTED is returned.
  112. * Option 2: If the hash is to be performed over multiple data sets, then use:
  113. * A. sgx_sha256_init - to create the context - context memory is allocated by this function.
  114. * Return: If out of enclave memory, SGX_ERROR_OUT_OF_MEMORY is returned.
  115. * If context creation fails then SGX_ERROR_UNEXPECTED is returned.
  116. * B. sgx_sha256_update - updates hash based on input source data
  117. * This function should be called for each chunk of data to be
  118. * included in the hash including the 1st and final chunks.
  119. * Return: If source pointer or context pointer are NULL, SGX_ERROR_INVALID_PARAMETER is returned.
  120. * If hash function fails then SGX_ERROR_UNEXPECTED is returned.
  121. * C. sgx_sha256_get_hash - function obtains the hash value
  122. * Return: If hash pointer or context pointer are NULL, SGX_ERROR_INVALID_PARAMETER is returned.
  123. * If the function fails then SGX_ERROR_UNEXPECTED is returned.
  124. * D. sgx_sha256_close - SHOULD BE CALLED to FREE context memory
  125. * Upon completing the process of computing a hash over a set of data
  126. * or sets of data, this function is used to free the context.
  127. * Return: If context pointer is NULL, SGX_ERROR_INVALID_PARAMETER is returned.
  128. *
  129. * Parameters:
  130. * Return: sgx_status_t - SGX_SUCCESS or failure as defined in sgx_error.h
  131. * Inputs: uint8_t *p_src - Pointer to the input stream to be hashed
  132. * uint32_t src_len - Length of the input stream to be hashed
  133. * Output: sgx_sha256_hash_t *p_hash - Resultant hash from operation
  134. */
  135. sgx_status_t SGXAPI sgx_sha256_msg(const uint8_t *p_src, uint32_t src_len, sgx_sha256_hash_t *p_hash);
  136. /** Allocates and initializes sha256 state
  137. *
  138. * Parameters:
  139. * Return: sgx_status_t - SGX_SUCCESS or failure as defined in sgx_error.h
  140. * Output: sgx_sha_state_handle_t *p_sha_handle - Pointer to the handle of the SHA256 state
  141. */
  142. sgx_status_t SGXAPI sgx_sha256_init(sgx_sha_state_handle_t* p_sha_handle);
  143. /** Updates sha256 has calculation based on the input message
  144. *
  145. * Parameters:
  146. * Return: sgx_status_t - SGX_SUCCESS or failure as defined in sgx_error.h
  147. * Input: sgx_sha_state_handle_t sha_handle - Handle to the SHA256 state
  148. * uint8_t *p_src - Pointer to the input stream to be hashed
  149. * uint32_t src_len - Length of the input stream to be hashed
  150. */
  151. sgx_status_t SGXAPI sgx_sha256_update(const uint8_t *p_src, uint32_t src_len, sgx_sha_state_handle_t sha_handle);
  152. /** Returns Hash calculation
  153. *
  154. * Parameters:
  155. * Return: sgx_status_t - SGX_SUCCESS or failure as defined in sgx_error.h
  156. * Input: sgx_sha_state_handle_t sha_handle - Handle to the SHA256 state
  157. * Output: sgx_sha256_hash_t *p_hash - Resultant hash from operation
  158. */
  159. sgx_status_t SGXAPI sgx_sha256_get_hash(sgx_sha_state_handle_t sha_handle, sgx_sha256_hash_t *p_hash);
  160. /** Cleans up SHA state
  161. *
  162. * Parameters:
  163. * Return: sgx_status_t - SGX_SUCCESS or failure as defined in sgx_error.h
  164. * Input: sgx_sha_state_handle_t sha_handle - Handle to the SHA256 state
  165. */
  166. sgx_status_t SGXAPI sgx_sha256_close(sgx_sha_state_handle_t sha_handle);
  167. /**Rijndael AES-GCM - Only 128-bit key AES-GCM Encryption/Decryption is supported
  168. *
  169. * The Galois/Counter Mode (GCM) is a mode of operation of the AES algorithm.
  170. * GCM [NIST SP 800-38D] uses a variation of the Counter mode of operation for encryption.
  171. * GCM assures authenticity of the confidential data (of up to about 64 GB per invocation)
  172. * using a universal hash function defined over a binary finite field (the Galois field).
  173. *
  174. * GCM can also provide authentication assurance for additional data
  175. * (of practically unlimited length per invocation) that is not encrypted.
  176. * GCM provides stronger authentication assurance than a (non-cryptographic) checksum or
  177. * error detecting code. In particular, GCM can detect both accidental modifications of
  178. * the data and intentional, unauthorized modifications.
  179. *
  180. * sgx_rijndael128GCM_encrypt:
  181. * Return: If key, source, destination, MAC, or IV pointer is NULL, SGX_ERROR_INVALID_PARAMETER is returned.
  182. * If AAD size is > 0 and the AAD pointer is NULL, SGX_ERROR_INVALID_PARAMETER is returned.
  183. * If the Source Length is < 1, SGX_ERROR_INVALID_PARAMETER is returned.
  184. * IV Length must = 12 (bytes) or SGX_ERROR_INVALID_PARAMETER is returned.
  185. * If out of enclave memory then SGX_ERROR_OUT_OF_MEMORY is returned.
  186. * If the encryption process fails then SGX_ERROR_UNEXPECTED is returned.
  187. *
  188. * sgx_rijndael128GCM_decrypt:
  189. * Return: If key, source, destination, MAC, or IV pointer is NULL, SGX_ERROR_INVALID_PARAMETER is returned.
  190. * If AAD size is > 0 and the AAD pointer is NULL, SGX_ERROR_INVALID_PARAMETER is returned.
  191. * If the Source Length is < 1, SGX_ERROR_INVALID_PARAMETER is returned.
  192. * IV Length must = 12 (bytes) or SGX_ERROR_INVALID_PARAMETER is returned.
  193. * If the decryption process fails then SGX_ERROR_UNEXPECTED is returned.
  194. * If the input MAC does not match the calculated MAC, SGX_ERROR_MAC_MISMATCH is returned.
  195. *
  196. * Parameters:
  197. * Return: sgx_status_t - SGX_SUCCESS or failure as defined in sgx_error.h
  198. * Inputs: sgx_aes_gcm_128bit_key_t *p_key - Pointer to the key used in encryption/decryption operation
  199. * Size MUST BE 128-bits
  200. * uint8_t *p_src - Pointer to the input stream to be encrypted/decrypted
  201. * uint32_t src_len - Length of the input stream to be encrypted/decrypted
  202. * uint8_t *p_iv - Pointer to the initialization vector
  203. * uint32_t iv_len - Length of the initialization vector - MUST BE 12 (bytes)
  204. * NIST AES-GCM recommended IV size = 96 bits
  205. * uint8_t *p_aad - Pointer to the input stream of additional authentication data
  206. * uint32_t aad_len - Length of the additional authentication data stream
  207. * sgx_aes_gcm_128bit_tag_t *p_in_mac - Pointer to the expected MAC in decryption process
  208. * Output: uint8_t *p_dst - Pointer to the cipher text for encryption or clear text for decryption. Size of buffer should be >= src_len.
  209. * sgx_aes_gcm_128bit_tag_t *p_out_mac - Pointer to the MAC generated from encryption process
  210. * NOTE: Wrapper is responsible for confirming decryption tag matches encryption tag
  211. */
  212. sgx_status_t SGXAPI sgx_rijndael128GCM_encrypt(const sgx_aes_gcm_128bit_key_t *p_key,
  213. const uint8_t *p_src,
  214. uint32_t src_len,
  215. uint8_t *p_dst,
  216. const uint8_t *p_iv,
  217. uint32_t iv_len,
  218. const uint8_t *p_aad,
  219. uint32_t aad_len,
  220. sgx_aes_gcm_128bit_tag_t *p_out_mac);
  221. sgx_status_t SGXAPI sgx_rijndael128GCM_decrypt(const sgx_aes_gcm_128bit_key_t *p_key,
  222. const uint8_t *p_src,
  223. uint32_t src_len,
  224. uint8_t *p_dst,
  225. const uint8_t *p_iv,
  226. uint32_t iv_len,
  227. const uint8_t *p_aad,
  228. uint32_t aad_len,
  229. const sgx_aes_gcm_128bit_tag_t *p_in_mac);
  230. /** Message Authentication Rijndael 128 CMAC - Only 128-bit key size is supported.
  231. * NOTE: Use sgx_rijndael128_cmac_msg if the src ptr contains the complete msg to perform hash (Option 1)
  232. * Else use the Init, Update, Update, ..., Final, Close procedure (Option 2)
  233. * Option 1: If the complete dataset is available for hashing, sgx_rijndael128_cmac_msg
  234. * is a single API call for generating the 128-bit hash for the given dataset.
  235. * Return: If source, key, or MAC pointer is NULL, SGX_ERROR_INVALID_PARAMETER is returned.
  236. * If out of enclave memory, SGX_ERROR_OUT_OF_MEMORY is returned.
  237. * If hash function fails then SGX_ERROR_UNEXPECTED is returned.
  238. * Option 2: If the hash is to be performed over multiple data sets, then use:
  239. * A. sgx_cmac128_init - to create the context - context memory is allocated by this function.
  240. * Return: If key pointer is NULL, SGX_ERROR_INVALID_PARAMETER is returned.
  241. * If out of enclave memory, SGX_ERROR_OUT_OF_MEMORY is returned.
  242. * If context creation fails then SGX_ERROR_UNEXPECTED is returned.
  243. * B. sgx_cmac128_update - updates hash based on input source data
  244. * This function should be called for each chunk of data to be
  245. * included in the hash including the 1st and final chunks.
  246. * Return: If source pointer or context pointer are NULL, SGX_ERROR_INVALID_PARAMETER is returned.
  247. * If hash function fails then SGX_ERROR_UNEXPECTED is returned.
  248. * C. sgx_cmac128_final - function obtains the hash value
  249. * Upon completing the process of computing a hash over a set of data or sets of data,
  250. * this function populates the hash value.
  251. * Return: If hash pointer or context pointer are NULL, SGX_ERROR_INVALID_PARAMETER is returned.
  252. * If the function fails then SGX_ERROR_UNEXPECTED is returned.
  253. * D. sgx_cmac128_close - SHOULD BE CALLED to clean up the CMAC state
  254. * Upon populating the hash value over a set of data or sets of data,
  255. * this function is used to free the CMAC state.
  256. * Return: If CMAC state pointer is NULL, SGX_ERROR_INVALID_PARAMETER is returned.
  257. *
  258. * Parameters:
  259. * Return: sgx_status_t - SGX_SUCCESS or failure as defined in sgx_error.h
  260. * Inputs: sgx_cmac_128bit_key_t *p_key - Pointer to the key used in encryption/decryption operation
  261. * uint8_t *p_src - Pointer to the input stream to be MAC’d
  262. * uint32_t src_len - Length of the input stream to be MAC’d
  263. * Output: sgx_cmac_gcm_128bit_tag_t *p_mac - Pointer to the resultant MAC
  264. */
  265. sgx_status_t SGXAPI sgx_rijndael128_cmac_msg(const sgx_cmac_128bit_key_t *p_key,
  266. const uint8_t *p_src,
  267. uint32_t src_len,
  268. sgx_cmac_128bit_tag_t *p_mac);
  269. /** Allocates and initializes CMAC state.
  270. *
  271. * Parameters:
  272. * Return: sgx_status_t - SGX_SUCCESS or failure as defined in sgx_error.h
  273. * Inputs: sgx_cmac_128bit_key_t *p_key - Pointer to the key used in encryption/decryption operation
  274. * Output: sgx_cmac_state_handle_t *p_cmac_handle - Pointer to the handle of the CMAC state
  275. */
  276. sgx_status_t SGXAPI sgx_cmac128_init(const sgx_cmac_128bit_key_t *p_key, sgx_cmac_state_handle_t* p_cmac_handle);
  277. /** Updates CMAC has calculation based on the input message.
  278. *
  279. * Parameters:
  280. * Return: sgx_status_t - SGX_SUCCESS or failure as defined in sgx_error.h
  281. * Input: sgx_cmac_state_handle_t cmac_handle - Handle to the CMAC state
  282. * uint8_t *p_src - Pointer to the input stream to be hashed
  283. * uint32_t src_len - Length of the input stream to be hashed
  284. */
  285. sgx_status_t SGXAPI sgx_cmac128_update(const uint8_t *p_src, uint32_t src_len, sgx_cmac_state_handle_t cmac_handle);
  286. /** Returns Hash calculation and clean up CMAC state.
  287. *
  288. * Parameters:
  289. * Return: sgx_status_t - SGX_SUCCESS or failure as defined in sgx_error.h
  290. * Input: sgx_cmac_state_handle_t cmac_handle - Handle to the CMAC state
  291. * Output: sgx_cmac_128bit_tag_t *p_hash - Resultant hash from operation
  292. */
  293. sgx_status_t SGXAPI sgx_cmac128_final(sgx_cmac_state_handle_t cmac_handle, sgx_cmac_128bit_tag_t *p_hash);
  294. /** Clean up the CMAC state
  295. *
  296. * Parameters:
  297. * Return: sgx_status_t - SGX_SUCCESS or failure as defined in sgx_error.h
  298. * Input: sgx_cmac_state_handle_t cmac_handle - Handle to the CMAC state
  299. */
  300. sgx_status_t SGXAPI sgx_cmac128_close(sgx_cmac_state_handle_t cmac_handle);
  301. /** AES-CTR 128-bit - Only 128-bit key size is supported.
  302. *
  303. * These functions encrypt/decrypt the input data stream of a variable length according
  304. * to the CTR mode as specified in [NIST SP 800-38A]. The counter can be thought of as
  305. * an IV which increments on successive encryption or decrytion calls. For a given
  306. * dataset or data stream the incremented counter block should be used on successive
  307. * calls of the encryption/decryption process for that given stream. However for
  308. * new or different datasets/streams, the same counter should not be reused, instead
  309. * intialize the counter for the new data set.
  310. *
  311. * sgx_aes_ctr_encrypt
  312. * Return: If source, key, counter, or destination pointer is NULL,
  313. * SGX_ERROR_INVALID_PARAMETER is returned.
  314. * If out of enclave memory, SGX_ERROR_OUT_OF_MEMORY is returned.
  315. * If the encryption process fails then SGX_ERROR_UNEXPECTED is returned.
  316. * sgx_aes_ctr_decrypt
  317. * Return: If source, key, counter, or destination pointer is NULL,
  318. * SGX_ERROR_INVALID_PARAMETER is returned.
  319. * If out of enclave memory, SGX_ERROR_OUT_OF_MEMORY is returned.
  320. * If the decryption process fails then SGX_ERROR_UNEXPECTED is returned.
  321. *
  322. * Parameters:
  323. * Return:
  324. * sgx_status_t - SGX_SUCCESS or failure as defined
  325. * in sgx_error.h
  326. * Inputs:
  327. * sgx_aes_128bit_key_t *p_key - Pointer to the key used in
  328. * encryption/decryption operation
  329. * uint8_t *p_src - Pointer to the input stream to be
  330. * encrypted/decrypted
  331. * uint32_t src_len - Length of the input stream to be
  332. * encrypted/decrypted
  333. * uint8_t *p_ctr - Pointer to the counter block
  334. * uint32_t ctr_inc_bits - Number of bits in counter to be
  335. * incremented
  336. * Output:
  337. * uint8_t *p_dst - Pointer to the cipher text.
  338. * Size of buffer should be >= src_len.
  339. */
  340. sgx_status_t SGXAPI sgx_aes_ctr_encrypt(
  341. const sgx_aes_ctr_128bit_key_t *p_key,
  342. const uint8_t *p_src,
  343. const uint32_t src_len,
  344. uint8_t *p_ctr,
  345. const uint32_t ctr_inc_bits,
  346. uint8_t *p_dst);
  347. sgx_status_t SGXAPI sgx_aes_ctr_decrypt(
  348. const sgx_aes_ctr_128bit_key_t *p_key,
  349. const uint8_t *p_src,
  350. const uint32_t src_len,
  351. uint8_t *p_ctr,
  352. const uint32_t ctr_inc_bits,
  353. uint8_t *p_dst);
  354. /**
  355. * Elliptic Curve Cryptography based on GF(p), 256 bit.
  356. *
  357. * Elliptic curve cryptosystems (ECCs) implement a different way of creating public keys.
  358. * Because elliptic curve calculation is based on the addition of the rational points in
  359. * the (x,y) plane and it is difficult to solve a discrete logarithm from these points,
  360. * a higher level of security is achieved through the cryptographic schemes that use the
  361. * elliptic curves. The cryptographic systems that encrypt messages by using the properties
  362. * of elliptic curves are hard to attack due to the extreme complexity of deciphering the
  363. * private key.
  364. *
  365. * Use of elliptic curves allows for shorter public key length and encourage cryptographers
  366. * to create cryptosystems with the same or higher encryption strength as the RSA or DSA
  367. * cryptosystems. Because of the relatively short key length, ECCs do encryption and decryption
  368. * faster on the hardware that requires less computation processing volumes. For example, with
  369. * a key length of 150-350 bits, ECCs provide the same encryption strength as the cryptosystems
  370. * who have to use 600 -1400 bits.
  371. *
  372. * ECCP stands for Elliptic Curve Cryptography Prime and these functions include operations
  373. * over a prime finite field GF(p).
  374. *
  375. */
  376. /** Allocates and initializes ecc context.
  377. * The function initializes the context of the elliptic curve cryptosystem over the
  378. * prime finite field GF(p). This function allocates and initializes the ecc context.
  379. * Return: If out of enclave memory, SGX_ERROR_OUT_OF_MEMORY is returned.
  380. * If context creation fails then SGX_ERROR_UNEXPECTED is returned.
  381. * Parameters:
  382. * Return: sgx_status_t - SGX_SUCCESS or failure as defined in sgx_error.h
  383. * Output: sgx_ecc_state_handle_t *p_ecc_handle - Pointer to the handle of the ECC crypto system
  384. */
  385. sgx_status_t SGXAPI sgx_ecc256_open_context(sgx_ecc_state_handle_t* p_ecc_handle);
  386. /** Cleans up ecc context.
  387. * Return: If context pointer is NULL, SGX_ERROR_INVALID_PARAMETER is returned.
  388. * Parameters:
  389. * Return: sgx_status_t - SGX_SUCCESS or failure as defined in sgx_error.h
  390. * Output: sgx_ecc_state_handle_t ecc_handle - Handle to the ECC crypto system
  391. */
  392. sgx_status_t SGXAPI sgx_ecc256_close_context(sgx_ecc_state_handle_t ecc_handle);
  393. /** Populates private/public key pair.
  394. * NOTE: Caller code allocates memory for Private & Public key pointers to be populated
  395. *
  396. * The function generates a private key p_private and computes a public key p_public of the
  397. * elliptic cryptosystem over a finite field GF(p).
  398. *
  399. * The private key p_private is a number that lies in the range of [1, n-1] where n is
  400. * the order of the elliptic curve base point.
  401. *
  402. * The public key p_public is an elliptic curve point such that p_public = p_private ?G,
  403. * where G is the base point of the elliptic curve.
  404. *
  405. * The context of the point p_public as an elliptic curve point must be created by using
  406. * the function sgx_ecc256_open_context.
  407. *
  408. * Return: If context, public key, or private key pointer is NULL,
  409. * SGX_ERROR_INVALID_PARAMETER is returned.
  410. * If the key creation process fails then SGX_ERROR_UNEXPECTED is returned.
  411. * Parameters:
  412. * Return: sgx_status_t - SGX_SUCCESS or failure as defined in sgx_error.h
  413. * Inputs: sgx_ecc_state_handle_t ecc_handle - Handle to the ECC crypto system
  414. * Outputs: sgx_ec256_private_t *p_private - Pointer to the private key - LITTLE ENDIAN
  415. * sgx_ec256_public_t *p_public - Pointer to the public key - LITTLE ENDIAN
  416. */
  417. sgx_status_t SGXAPI sgx_ecc256_create_key_pair(sgx_ec256_private_t *p_private,
  418. sgx_ec256_public_t *p_public,
  419. sgx_ecc_state_handle_t ecc_handle);
  420. /** Checks whether the input point is a valid point on the given elliptic curve.
  421. * Parameters:
  422. * Return: sgx_status_t - SGX_SUCCESS or failure as defined sgx_error.h
  423. * Inputs: sgx_ecc_state_handle_t ecc_handle - Handle to ECC crypto system
  424. * sgx_ec256_public_t *p_point - Pointer to perform validity check on - LITTLE ENDIAN
  425. * Output: int *p_valid - Return 0 if the point is an invalid point on ECC curve
  426. */
  427. sgx_status_t SGXAPI sgx_ecc256_check_point(const sgx_ec256_public_t *p_point,
  428. const sgx_ecc_state_handle_t ecc_handle,
  429. int *p_valid);
  430. /** Computes DH shared key based on own (local) private key and remote public Ga Key.
  431. * NOTE: Caller code allocates memory for Shared key pointer to be populated
  432. *
  433. * The function computes a secret number bnShare, which is a secret key shared between
  434. * two participants of the cryptosystem.
  435. *
  436. * In cryptography, metasyntactic names such as Alice as Bob are normally used as examples
  437. * and in discussions and stand for participant A and participant B.
  438. *
  439. * Both participants (Alice and Bob) use the cryptosystem for receiving a common secret point
  440. * on the elliptic curve called a secret key. To receive a secret key, participants apply the
  441. * Diffie-Hellman key-agreement scheme involving public key exchange. The value of the secret
  442. * key entirely depends on participants.
  443. *
  444. * According to the scheme, Alice and Bob perform the following operations:
  445. * 1. Alice calculates her own public key pubKeyA by using her private key
  446. * privKeyA: pubKeyA = privKeyA ?G, where G is the base point of the elliptic curve.
  447. * 2. Alice passes the public key to Bob.
  448. * 3. Bob calculates his own public key pubKeyB by using his private key
  449. * privKeyB: pubKeyB = privKeyB ?G, where G is a base point of the elliptic curve.
  450. * 4. Bob passes the public key to Alice.
  451. * 5. Alice gets Bob's public key and calculates the secret point shareA. When calculating,
  452. * she uses her own private key and Bob's public key and applies the following formula:
  453. * shareA = privKeyA ?pubKeyB = privKeyA ?privKeyB ?G.
  454. * 6. Bob gets Alice's public key and calculates the secret point shareB. When calculating,
  455. * he uses his own private key and Alice's public key and applies the following formula:
  456. * shareB = privKeyB ?pubKeyA = privKeyB ?privKeyA ?G.
  457. *
  458. * Because the following equation is true privKeyA ?privKeyB ?G = privKeyB ?privKeyA ?G,
  459. * the result of both calculations is the same, that is, the equation shareA = shareB is true.
  460. * The secret point serves as a secret key.
  461. *
  462. * Shared secret bnShare is an x-coordinate of the secret point on the elliptic curve. The elliptic
  463. * curve domain parameters must be hitherto defined by the function: sgx_ecc256_open_context.
  464. *
  465. * Return: If context, public key, private key, or shared key pointer is NULL,
  466. * SGX_ERROR_INVALID_PARAMETER is returned.
  467. * If the remote public key is not a valid point on the elliptic curve,
  468. * SGX_ERROR_INVALID_PARAMETER is returned.
  469. * If the key creation process fails then SGX_ERROR_UNEXPECTED is returned.
  470. *
  471. * Parameters:
  472. * Return: sgx_status_t - SGX_SUCCESS or failure as defined in sgx_error.h
  473. * Inputs: sgx_ecc_state_handle_t ecc_handle - Handle to the ECC crypto system
  474. * sgx_ec256_private_t *p_private_b - Pointer to the local private key - LITTLE ENDIAN
  475. * sgx_ec256_public_t *p_public_ga - Pointer to the remote public key - LITTLE ENDIAN
  476. * Output: sgx_ec256_dh_shared_t *p_shared_key - Pointer to the shared DH key - LITTLE ENDIAN
  477. */
  478. sgx_status_t SGXAPI sgx_ecc256_compute_shared_dhkey(sgx_ec256_private_t *p_private_b,
  479. sgx_ec256_public_t *p_public_ga,
  480. sgx_ec256_dh_shared_t *p_shared_key,
  481. sgx_ecc_state_handle_t ecc_handle);
  482. /* Computes 512-bit DH shared key based on private B key (local) and remote public Ga Key
  483. * Parameters:
  484. * Return: sgx_status_t - SGX_SUCCESS or failure as defined in sgx_error.h
  485. * Inputs: sgx_ecc_state_handle_t ecc_handle - Handle to the ECC crypto system
  486. * sgx_ec256_private_t *p_private_b - Pointer to the local private key
  487. * sgx_ec256_public_t *p_public_ga - Pointer to the remote public key
  488. * Output: sgx_ec256_dh_shared512_t *p_shared_key - Pointer to the 512-bit shared DH key
  489. */
  490. sgx_status_t SGXAPI sgx_ecc256_compute_shared_dhkey512(sgx_ec256_private_t *p_private_b,
  491. sgx_ec256_public_t *p_public_ga,
  492. sgx_ec256_dh_shared512_t *p_shared_key,
  493. sgx_ecc_state_handle_t ecc_handle);
  494. /** Computes signature for data based on private key.
  495. *
  496. * A message digest is a fixed size number derived from the original message with
  497. * an applied hash function over the binary code of the message. (SHA256 in this case)
  498. * The signer's private key and the message digest are used to create a signature.
  499. *
  500. * A digital signature over a message consists of a pair of large numbers, 256-bits each,
  501. * which the given function computes.
  502. *
  503. * The scheme used for computing a digital signature is of the ECDSA scheme,
  504. * an elliptic curve of the DSA scheme.
  505. *
  506. * The keys can be generated and set up by the function: sgx_ecc256_create_key_pair.
  507. *
  508. * The elliptic curve domain parameters must be created by function:
  509. * sgx_ecc256_open_context
  510. *
  511. * Return: If context, private key, signature or data pointer is NULL,
  512. * SGX_ERROR_INVALID_PARAMETER is returned.
  513. * If the signature creation process fails then SGX_ERROR_UNEXPECTED is returned.
  514. *
  515. * Parameters:
  516. * Return: sgx_status_t - SGX_SUCCESS or failure as defined in sgx_error.h
  517. * Inputs: sgx_ecc_state_handle_t ecc_handle - Handle to the ECC crypto system
  518. * sgx_ec256_private_t *p_private - Pointer to the private key - LITTLE ENDIAN
  519. * uint8_t *p_data - Pointer to the data to be signed
  520. * uint32_t data_size - Size of the data to be signed
  521. * Output: ec256_signature_t *p_signature - Pointer to the signature - LITTLE ENDIAN
  522. */
  523. sgx_status_t SGXAPI sgx_ecdsa_sign(const uint8_t *p_data,
  524. uint32_t data_size,
  525. sgx_ec256_private_t *p_private,
  526. sgx_ec256_signature_t *p_signature,
  527. sgx_ecc_state_handle_t ecc_handle);
  528. /** Verifies the signature for the given data based on the public key.
  529. *
  530. * A digital signature over a message consists of a pair of large numbers, 256-bits each,
  531. * which could be created by function: sgx_ecdsa_sign. The scheme used for computing a
  532. * digital signature is of the ECDSA scheme, an elliptic curve of the DSA scheme.
  533. *
  534. * The typical result of the digital signature verification is one of the two values:
  535. * SGX_Generic_ECValid - Digital signature is valid
  536. * SGX_Generic_ECInvalidSignature - Digital signature is not valid
  537. *
  538. * The elliptic curve domain parameters must be created by function:
  539. * sgx_ecc256_open_context
  540. *
  541. * Return: If context, public key, signature, result or data pointer is NULL,
  542. * SGX_ERROR_INVALID_PARAMETER is returned.
  543. * If the verification process fails then SGX_ERROR_UNEXPECTED is returned.
  544. * Parameters:
  545. * Return: sgx_status_t - SGX_SUCCESS or failure as defined in sgx_error.h
  546. * Inputs: sgx_ecc_state_handle_t ecc_handle - Handle to the ECC crypto system
  547. * sgx_ec256_public_t *p_public - Pointer to the public key
  548. * uint8_t *p_data - Pointer to the data to be signed
  549. * uint32_t data_size - Size of the data to be signed
  550. * sgx_ec256_signature_t *p_signature - Pointer to the signature
  551. * Output: uint8_t *p_result - Pointer to the result of verification check
  552. */
  553. sgx_status_t SGXAPI sgx_ecdsa_verify(const uint8_t *p_data,
  554. uint32_t data_size,
  555. const sgx_ec256_public_t *p_public,
  556. sgx_ec256_signature_t *p_signature,
  557. uint8_t *p_result,
  558. sgx_ecc_state_handle_t ecc_handle);
  559. #ifdef __cplusplus
  560. }
  561. #endif
  562. #endif