sgx_tcrypto.h 35 KB

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