sample_libcrypto.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright (C) 2011-2016 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: sample_libcrypto.h
  33. * Description:
  34. * Interface for generic crypto library APIs. This library does NOT have
  35. * production quality crypto implementations.
  36. * Some methods have been constructed to facilitate reproduceable results to
  37. * aid in the debugging of the sample application.
  38. */
  39. #ifndef SAMPLE_LIBCRYPTO_H
  40. #define SAMPLE_LIBCRYPTO_H
  41. #include <stdint.h>
  42. typedef enum sample_status_t
  43. {
  44. SAMPLE_SUCCESS = 0,
  45. SAMPLE_ERROR_UNEXPECTED , // Unexpected error
  46. SAMPLE_ERROR_INVALID_PARAMETER , // The parameter is incorrect
  47. SAMPLE_ERROR_OUT_OF_MEMORY , // Not enough memory is available to complete this operation
  48. } sample_status_t;
  49. #define SAMPLE_SHA256_HASH_SIZE 32
  50. #define SAMPLE_ECP256_KEY_SIZE 32
  51. #define SAMPLE_NISTP_ECP256_KEY_SIZE (SAMPLE_ECP256_KEY_SIZE/sizeof(uint32_t))
  52. #define SAMPLE_AESGCM_IV_SIZE 12
  53. #define SAMPLE_AESGCM_KEY_SIZE 16
  54. #define SAMPLE_AESGCM_MAC_SIZE 16
  55. #define SAMPLE_CMAC_KEY_SIZE 16
  56. #define SAMPLE_CMAC_MAC_SIZE 16
  57. #define SAMPLE_AESCTR_KEY_SIZE 16
  58. typedef struct sample_ec256_dh_shared_t
  59. {
  60. uint8_t s[SAMPLE_ECP256_KEY_SIZE];
  61. } sample_ec256_dh_shared_t;
  62. typedef struct sample_ec256_private_t
  63. {
  64. uint8_t r[SAMPLE_ECP256_KEY_SIZE];
  65. } sample_ec256_private_t;
  66. typedef struct sample_ec256_public_t
  67. {
  68. uint8_t gx[SAMPLE_ECP256_KEY_SIZE];
  69. uint8_t gy[SAMPLE_ECP256_KEY_SIZE];
  70. } sample_ec256_public_t;
  71. typedef struct sample_ec256_signature_t
  72. {
  73. uint32_t x[SAMPLE_NISTP_ECP256_KEY_SIZE];
  74. uint32_t y[SAMPLE_NISTP_ECP256_KEY_SIZE];
  75. } sample_ec256_signature_t;
  76. typedef void* sample_sha_state_handle_t;
  77. typedef void* sample_cmac_state_handle_t;
  78. typedef void* sample_ecc_state_handle_t;
  79. typedef uint8_t sample_sha256_hash_t[SAMPLE_SHA256_HASH_SIZE];
  80. typedef uint8_t sample_aes_gcm_128bit_key_t[SAMPLE_AESGCM_KEY_SIZE];
  81. typedef uint8_t sample_aes_gcm_128bit_tag_t[SAMPLE_AESGCM_MAC_SIZE];
  82. typedef uint8_t sample_cmac_128bit_key_t[SAMPLE_CMAC_KEY_SIZE];
  83. typedef uint8_t sample_cmac_128bit_tag_t[SAMPLE_CMAC_MAC_SIZE];
  84. typedef uint8_t sample_aes_ctr_128bit_key_t[SAMPLE_AESCTR_KEY_SIZE];
  85. #ifdef __cplusplus
  86. #define EXTERN_C extern "C"
  87. #else
  88. #define EXTERN_C
  89. #endif
  90. #define SAMPLE_LIBCRYPTO_API EXTERN_C
  91. /* Rijndael AES-GCM
  92. * Parameters:
  93. * Return: sample_status_t - SAMPLE_SUCCESS on success, error code otherwise.
  94. * Inputs: sample_aes_gcm_128bit_key_t *p_key - Pointer to key used in encryption/decryption operation
  95. * uint8_t *p_src - Pointer to input stream to be encrypted/decrypted
  96. * uint32_t src_len - Length of input stream to be encrypted/decrypted
  97. * uint8_t *p_iv - Pointer to initialization vector to use
  98. * uint32_t iv_len - Length of initialization vector
  99. * uint8_t *p_aad - Pointer to input stream of additional authentication data
  100. * uint32_t aad_len - Length of additional authentication data stream
  101. * sample_aes_gcm_128bit_tag_t *p_in_mac - Pointer to expected MAC in decryption process
  102. * Output: uint8_t *p_dst - Pointer to cipher text. Size of buffer should be >= src_len.
  103. * sample_aes_gcm_128bit_tag_t *p_out_mac - Pointer to MAC generated from encryption process
  104. * NOTE: Wrapper is responsible for confirming decryption tag matches encryption tag */
  105. SAMPLE_LIBCRYPTO_API sample_status_t sample_rijndael128GCM_encrypt(const sample_aes_gcm_128bit_key_t *p_key, const uint8_t *p_src, uint32_t src_len,
  106. uint8_t *p_dst, const uint8_t *p_iv, uint32_t iv_len, const uint8_t *p_aad, uint32_t aad_len,
  107. sample_aes_gcm_128bit_tag_t *p_out_mac);
  108. /* Message Authentication - Rijndael 128 CMAC
  109. * Parameters:
  110. * Return: sample_status_t - SAMPLE_SUCCESS on success, error code otherwise.
  111. * Inputs: sample_cmac_128bit_key_t *p_key - Pointer to key used in encryption/decryption operation
  112. * uint8_t *p_src - Pointer to input stream to be MAC
  113. * uint32_t src_len - Length of input stream to be MAC
  114. * Output: sample_cmac_gcm_128bit_tag_t *p_mac - Pointer to resultant MAC */
  115. SAMPLE_LIBCRYPTO_API sample_status_t sample_rijndael128_cmac_msg(const sample_cmac_128bit_key_t *p_key, const uint8_t *p_src,
  116. uint32_t src_len, sample_cmac_128bit_tag_t *p_mac);
  117. /*
  118. * Elliptic Curve Crytpography - Based on GF(p), 256 bit
  119. */
  120. /* Allocates and initializes ecc context
  121. * Parameters:
  122. * Return: sample_status_t - SAMPLE_SUCCESS or failure as defined SAMPLE_Error.h.
  123. * Output: sample_ecc_state_handle_t ecc_handle - Handle to ECC crypto system */
  124. SAMPLE_LIBCRYPTO_API sample_status_t sample_ecc256_open_context(sample_ecc_state_handle_t* ecc_handle);
  125. /* Cleans up ecc context
  126. * Parameters:
  127. * Return: sample_status_t - SAMPLE_SUCCESS or failure as defined SAMPLE_Error.h.
  128. * Output: sample_ecc_state_handle_t ecc_handle - Handle to ECC crypto system */
  129. SAMPLE_LIBCRYPTO_API sample_status_t sample_ecc256_close_context(sample_ecc_state_handle_t ecc_handle);
  130. /* Populates private/public key pair - caller code allocates memory
  131. * Parameters:
  132. * Return: sample_status_t - SAMPLE_SUCCESS on success, error code otherwise.
  133. * Inputs: sample_ecc_state_handle_t ecc_handle - Handle to ECC crypto system
  134. * Outputs: sample_ec256_private_t *p_private - Pointer to the private key
  135. * sample_ec256_public_t *p_public - Pointer to the public key */
  136. SAMPLE_LIBCRYPTO_API sample_status_t sample_ecc256_create_key_pair(sample_ec256_private_t *p_private,
  137. sample_ec256_public_t *p_public,
  138. sample_ecc_state_handle_t ecc_handle);
  139. /* Computes DH shared key based on private B key (local) and remote public Ga Key
  140. * Parameters:
  141. * Return: sample_status_t - SAMPLE_SUCCESS on success, error code otherwise.
  142. * Inputs: sample_ecc_state_handle_t ecc_handle - Handle to ECC crypto system
  143. * sample_ec256_private_t *p_private_b - Pointer to the local private key - LITTLE ENDIAN
  144. * sample_ec256_public_t *p_public_ga - Pointer to the remote public key - LITTLE ENDIAN
  145. * Output: sample_ec256_dh_shared_t *p_shared_key - Pointer to the shared DH key - LITTLE ENDIAN
  146. x-coordinate of (privKeyB - pubKeyA) */
  147. SAMPLE_LIBCRYPTO_API sample_status_t sample_ecc256_compute_shared_dhkey(sample_ec256_private_t *p_private_b,
  148. sample_ec256_public_t *p_public_ga,
  149. sample_ec256_dh_shared_t *p_shared_key,
  150. sample_ecc_state_handle_t ecc_handle);
  151. /* Computes signature for data based on private key
  152. *
  153. * A message digest is a fixed size number derived from the original message with
  154. * an applied hash function over the binary code of the message. (SHA256 in this case)
  155. * The signer's private key and the message digest are used to create a signature.
  156. *
  157. * A digital signature over a message consists of a pair of large numbers, 256-bits each,
  158. * which the given function computes.
  159. *
  160. * The scheme used for computing a digital signature is of the ECDSA scheme,
  161. * an elliptic curve of the DSA scheme.
  162. *
  163. * The keys can be generated and set up by the function: sgx_ecc256_create_key_pair.
  164. *
  165. * The elliptic curve domain parameters must be created by function:
  166. * sample_ecc256_open_context
  167. *
  168. * Return: If context, private key, signature or data pointer is NULL,
  169. * SAMPLE_ERROR_INVALID_PARAMETER is returned.
  170. * If the signature creation process fails then SAMPLE_ERROR_UNEXPECTED is returned.
  171. *
  172. * Parameters:
  173. * Return: sample_status_t - SAMPLE_SUCCESS, success, error code otherwise.
  174. * Inputs: sample_ecc_state_handle_t ecc_handle - Handle to the ECC crypto system
  175. * sample_ec256_private_t *p_private - Pointer to the private key - LITTLE ENDIAN
  176. * uint8_t *p_data - Pointer to the data to be signed
  177. * uint32_t data_size - Size of the data to be signed
  178. * Output: ec256_signature_t *p_signature - Pointer to the signature - LITTLE ENDIAN */
  179. SAMPLE_LIBCRYPTO_API sample_status_t sample_ecdsa_sign(const uint8_t *p_data,
  180. uint32_t data_size,
  181. sample_ec256_private_t *p_private,
  182. sample_ec256_signature_t *p_signature,
  183. sample_ecc_state_handle_t ecc_handle);
  184. /* Allocates and initializes sha256 state
  185. * Parameters:
  186. * Return: sample_status_t - SAMPLE_SUCCESS on success, error code otherwise.
  187. * Output: sample_sha_state_handle_t sha_handle - Handle to the SHA256 state */
  188. SAMPLE_LIBCRYPTO_API sample_status_t sample_sha256_init(sample_sha_state_handle_t* p_sha_handle);
  189. /* Updates sha256 has calculation based on the input message
  190. * Parameters:
  191. * Return: sample_status_t - SAMPLE_SUCCESS or failure.
  192. * Input: sample_sha_state_handle_t sha_handle - Handle to the SHA256 state
  193. * uint8_t *p_src - Pointer to the input stream to be hashed
  194. * uint32_t src_len - Length of the input stream to be hashed */
  195. SAMPLE_LIBCRYPTO_API sample_status_t sample_sha256_update(const uint8_t *p_src, uint32_t src_len, sample_sha_state_handle_t sha_handle);
  196. /* Returns Hash calculation
  197. * Parameters:
  198. * Return: sample_status_t - SAMPLE_SUCCESS on success, error code otherwise.
  199. * Input: sample_sha_state_handle_t sha_handle - Handle to the SHA256 state
  200. * Output: sample_sha256_hash_t *p_hash - Resultant hash from operation */
  201. SAMPLE_LIBCRYPTO_API sample_status_t sample_sha256_get_hash(sample_sha_state_handle_t sha_handle, sample_sha256_hash_t *p_hash);
  202. /* Cleans up sha state
  203. * Parameters:
  204. * Return: sample_status_t - SAMPLE_SUCCESS on success, error code otherwise.
  205. * Input: sample_sha_state_handle_t sha_handle - Handle to the SHA256 state */
  206. SAMPLE_LIBCRYPTO_API sample_status_t sample_sha256_close(sample_sha_state_handle_t sha_handle);
  207. #endif