pse_crypto_helper.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * Copyright (C) 2011-2017 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #include "CertificateProvisioningProtocol.h"
  32. #include <cstddef>
  33. #include "ipp_wrapper.h"
  34. #include "sgx_tcrypto.h"
  35. #include "../../aesm_service/source/aesm/application/aesm_rand.h"
  36. #include <ippcp.h>
  37. #include "sgx_memset_s.h"
  38. #include "se_wrapper.h"
  39. #include "oal/error_report.h"
  40. //#define USE_HARDCODED_PEK
  41. #if defined(USE_HARDCODED_PEK)
  42. public_key_t s_public_key =
  43. {
  44. // little-endian
  45. { // modulus
  46. 0x1B, 0x7D, 0xB4, 0x5C, 0x89, 0x72, 0xFB, 0x51, 0xF3, 0x39, 0xB2, 0x07, 0x1F, 0xF2, 0x8A, 0xD1,
  47. 0xAC, 0x55, 0xE7, 0x38, 0x7D, 0xEA, 0x42, 0xFA, 0x20, 0xD5, 0x31, 0xFA, 0x07, 0xB4, 0x5D, 0x64,
  48. 0xC4, 0xF6, 0x9E, 0xD8, 0x34, 0xE2, 0xE3, 0x8D, 0xEA, 0x1B, 0xA6, 0xD2, 0xB8, 0xD2, 0xA4, 0xD2,
  49. 0xA9, 0xF6, 0xFB, 0xA1, 0xA4, 0x9B, 0xC5, 0x25, 0xCE, 0xF1, 0xD3, 0xAD, 0xB9, 0xBF, 0xD9, 0x92,
  50. 0xDF, 0x04, 0x83, 0xD0, 0x46, 0x9C, 0x66, 0xC0, 0x91, 0x4C, 0xEF, 0x43, 0x19, 0xD8, 0x54, 0xE6,
  51. 0x32, 0xE7, 0x4F, 0x0E, 0xC5, 0x45, 0x74, 0x17, 0x38, 0xE6, 0xA9, 0x01, 0xED, 0xF8, 0xF6, 0x08,
  52. 0xFD, 0x25, 0x0A, 0x1D, 0x8B, 0xED, 0x99, 0xB1, 0xFE, 0x52, 0x13, 0x30, 0x89, 0x5E, 0xE9, 0xD9,
  53. 0x6A, 0xA7, 0x88, 0xD3, 0xBA, 0x1C, 0x78, 0xAD, 0xAB, 0xE1, 0x3C, 0xCB, 0xEF, 0x72, 0x41, 0x0B,
  54. 0xB6, 0x8F, 0xDC, 0x66, 0x59, 0xF7, 0xCB, 0x0D, 0xDB, 0xAA, 0x37, 0x21, 0x8F, 0x80, 0x2D, 0x26,
  55. 0xA2, 0xD0, 0x71, 0x96, 0xC1, 0x13, 0xCE, 0x26, 0xF5, 0x2F, 0x17, 0xCA, 0x99, 0x9B, 0x4B, 0x76,
  56. 0x31, 0x98, 0x82, 0xCD, 0x18, 0x6A, 0x0E, 0x9C, 0xCE, 0x31, 0xA6, 0x71, 0x68, 0x4E, 0xE4, 0x02,
  57. 0x89, 0x1C, 0xB1, 0x67, 0xAA, 0x6A, 0xE2, 0x9D, 0x62, 0xF4, 0x49, 0x2F, 0x86, 0x12, 0xE6, 0x00,
  58. 0x7C, 0xD3, 0xD5, 0xCD, 0xC9, 0x93, 0x49, 0xAB, 0x70, 0x8A, 0xCC, 0xB7, 0xF0, 0x02, 0x21, 0x3F,
  59. 0xA6, 0xE7, 0xB5, 0xFC, 0xB4, 0x94, 0x61, 0x6E, 0xF7, 0x45, 0x35, 0x24, 0xA6, 0x6F, 0x07, 0xBF,
  60. 0x4A, 0x56, 0x81, 0xCD, 0xAF, 0xA1, 0x49, 0x66, 0xCF, 0xCF, 0xDB, 0x63, 0xCB, 0xB4, 0x75, 0x7C,
  61. 0x20, 0xC7, 0x98, 0xA5, 0x02, 0xF6, 0xDD, 0x97, 0xFB, 0xAE, 0x61, 0x7A, 0x3C, 0x11, 0x53, 0x14
  62. },
  63. 0x00000011 // exponent
  64. };
  65. #endif
  66. const public_key_t& CertificateProvisioningProtocol::get_intel_pek()
  67. {
  68. #if defined(USE_HARDCODED_PEK)
  69. return s_public_key;
  70. #else
  71. return m_publicKey;
  72. #endif
  73. }
  74. //Function to get the rsa public key of intel backend server for IPP functions
  75. //The output rsa_pub_key should be released by function free_rsa_key
  76. static IppStatus get_intel_rsa_pub_key_in_ipp_format(const public_key_t& publicKey, IppsRSAPublicKeyState **rsa_pub_key)
  77. {
  78. if (sizeof(publicKey.n) != PVE_RSA_KEY_BYTES)
  79. return ippStsSizeErr;
  80. if (NULL == rsa_pub_key)
  81. return ippStsNullPtrErr;
  82. IppStatus status = create_rsa_pub_key(
  83. sizeof(publicKey.n),
  84. sizeof(publicKey.e),
  85. (const Ipp32u*)publicKey.n,
  86. (const Ipp32u*)&publicKey.e,
  87. rsa_pub_key);
  88. return status;
  89. }
  90. int CertificateProvisioningProtocol::get_intel_pek_cipher_text_size()
  91. {
  92. return sizeof(m_publicKey.n);
  93. }
  94. void CertificateProvisioningProtocol::free_intel_ipp_rsa_pub_key(IppsRSAPublicKeyState* rsa_pub_key)
  95. {
  96. if (NULL == rsa_pub_key)
  97. return;
  98. secure_free_rsa_pub_key(sizeof(m_publicKey.n), sizeof(m_publicKey.e), rsa_pub_key);
  99. }
  100. ae_error_t CertificateProvisioningProtocol::get_random_value(uint32_t size, upse::Buffer& randomValue)
  101. {
  102. ae_error_t status = AE_FAILURE;
  103. do
  104. {
  105. status = randomValue.Alloc(size);
  106. if (AE_FAILED(status))
  107. break;
  108. uint8_t* p;
  109. upse::BufferWriter bw(randomValue);
  110. status = bw.reserve(size, &p);
  111. if (AE_FAILED(status))
  112. break;
  113. status = aesm_read_rand(p, size);
  114. } while (0);
  115. return status;
  116. }
  117. ae_error_t CertificateProvisioningProtocol::aesGCMEncrypt(const upse::Buffer& iv, const upse::Buffer& key, const upse::Buffer& plainText,
  118. const upse::Buffer& aad, upse::Buffer& encryptedText, upse::Buffer& mac)
  119. {
  120. ae_error_t status = AE_FAILURE;
  121. do
  122. {
  123. if (key.getSize() != sizeof(sgx_aes_gcm_128bit_key_t))
  124. break;
  125. status = encryptedText.Alloc(plainText.getSize());
  126. if (AE_FAILED(status))
  127. break;
  128. uint8_t* pEncryptedText;
  129. status = upse::BufferWriter(encryptedText).reserve(encryptedText.getSize(), &pEncryptedText);
  130. if (AE_FAILED(status))
  131. break;
  132. status = mac.Alloc(sizeof(sgx_aes_gcm_128bit_tag_t));
  133. if (AE_FAILED(status))
  134. break;
  135. uint8_t* pMAC;
  136. status = upse::BufferWriter(mac).reserve(mac.getSize(), &pMAC);
  137. if (AE_FAILED(status))
  138. break;
  139. sgx_status_t sgx_status;
  140. sgx_status = sgx_rijndael128GCM_encrypt(reinterpret_cast<const sgx_aes_gcm_128bit_key_t *>(key.getData()),
  141. plainText.getData(), plainText.getSize(), pEncryptedText, iv.getData(), IV_SIZE, aad.getData(), aad.getSize(),
  142. reinterpret_cast<sgx_aes_gcm_128bit_tag_t *>(pMAC));
  143. if (SGX_SUCCESS != sgx_status)
  144. {
  145. status = AE_FAILURE;
  146. break;
  147. }
  148. status = AE_SUCCESS;
  149. } while (0);
  150. return status;
  151. }
  152. ae_error_t CertificateProvisioningProtocol::aesGCMDecrypt(const upse::Buffer& iv, const upse::Buffer& key, const upse::Buffer& cipherText,
  153. const upse::Buffer& aad, const upse::Buffer& mac, upse::Buffer& plainText)
  154. {
  155. ae_error_t status = AE_FAILURE;
  156. do
  157. {
  158. if (key.getSize() != sizeof(sgx_aes_gcm_128bit_key_t))
  159. break;
  160. status = plainText.Alloc(cipherText.getSize());
  161. if (AE_FAILED(status))
  162. break;
  163. uint8_t* pPlainText = NULL;
  164. status = upse::BufferWriter(plainText).reserve(plainText.getSize(), &pPlainText);
  165. if (AE_FAILED(status))
  166. break;
  167. sgx_status_t sgx_status;
  168. sgx_status = sgx_rijndael128GCM_decrypt(reinterpret_cast<const sgx_aes_gcm_128bit_key_t *>(key.getData()),
  169. cipherText.getData(), cipherText.getSize(), pPlainText, iv.getData(), IV_SIZE, aad.getData(), aad.getSize(),
  170. reinterpret_cast<const sgx_aes_gcm_128bit_tag_t *>(mac.getData()));
  171. if (SGX_SUCCESS != sgx_status)
  172. {
  173. AESM_LOG_ERROR("%s", g_event_string_table[SGX_EVENT_PSE_CERT_PROV_INTEGRITY_ERROR]);
  174. status = AE_FAILURE;
  175. break;
  176. }
  177. status = AE_SUCCESS;
  178. } while (0);
  179. return status;
  180. }
  181. ae_error_t CertificateProvisioningProtocol::aesCMAC(const upse::Buffer& key, const upse::Buffer& message, upse::Buffer& cmac)
  182. {
  183. ae_error_t status = AE_FAILURE;
  184. do
  185. {
  186. if (key.getSize() != sizeof(sgx_aes_gcm_128bit_key_t))
  187. break;
  188. status = cmac.Alloc(sizeof(sgx_cmac_128bit_tag_t));
  189. if (AE_FAILED(status))
  190. break;
  191. uint8_t* pCMAC;
  192. status = upse::BufferWriter(cmac).reserve(cmac.getSize(), &pCMAC);
  193. if (AE_FAILED(status))
  194. break;
  195. sgx_status_t sgx_status;
  196. sgx_status = sgx_rijndael128_cmac_msg(reinterpret_cast<const sgx_aes_gcm_128bit_key_t *>(key.getData()),
  197. message.getData(), message.getSize(), reinterpret_cast<sgx_cmac_128bit_tag_t *>(pCMAC));
  198. if (SGX_SUCCESS != sgx_status)
  199. {
  200. status = AE_FAILURE;
  201. break;
  202. }
  203. status = AE_SUCCESS;
  204. } while (0);
  205. return status;
  206. }
  207. ae_error_t CertificateProvisioningProtocol::encryptRSA_OAEP_SHA256(const public_key_t& publicKey, upse::BufferReader& plainTextReader, upse::Buffer& cipherText)
  208. {
  209. ae_error_t status = AE_FAILURE;
  210. IppStatus ippReturnStatus;
  211. IppsRSAPublicKeyState* rsa_pub_key = NULL;
  212. uint8_t* pub_key_buffer = NULL;
  213. do
  214. {
  215. ippReturnStatus = get_intel_rsa_pub_key_in_ipp_format(publicKey, &rsa_pub_key);
  216. if (ippStsNoErr != ippReturnStatus)
  217. break;
  218. uint8_t seed[IPP_SHA256_DIGEST_BITSIZE / 8];
  219. ae_error_t rand_status = aesm_read_rand(seed, sizeof(seed));
  220. BREAK_IF_TRUE(AE_FAILED(rand_status), status, AE_FAILURE);
  221. int pub_key_size;
  222. ippReturnStatus = ippsRSA_GetBufferSizePublicKey(&pub_key_size, rsa_pub_key);
  223. if (ippStsNoErr != ippReturnStatus)
  224. break;
  225. pub_key_buffer = (uint8_t*)malloc(pub_key_size);
  226. if (NULL == pub_key_buffer)
  227. break;
  228. int plainTextSize = plainTextReader.getRemainingSize();
  229. const uint8_t* pPlainText = NULL;
  230. if (AE_FAILED(plainTextReader.readRaw(&pPlainText)))
  231. break;
  232. int cipherTextSize = get_intel_pek_cipher_text_size();
  233. if (AE_FAILED(cipherText.Alloc(cipherTextSize)))
  234. break;
  235. upse::BufferWriter cipherTextWriter(cipherText);
  236. uint8_t* pCipherText;
  237. if (AE_FAILED(cipherTextWriter.reserve(cipherText.getSize(), &pCipherText)))
  238. break;
  239. ippReturnStatus = ippsRSAEncrypt_OAEP(pPlainText, plainTextSize,
  240. NULL, 0, seed,
  241. pCipherText,
  242. rsa_pub_key, IPP_ALG_HASH_SHA256,
  243. pub_key_buffer);
  244. if (ippStsNoErr != ippReturnStatus)
  245. break;
  246. status = AE_SUCCESS;
  247. } while (0);
  248. if (NULL != pub_key_buffer)
  249. free(pub_key_buffer);
  250. free_intel_ipp_rsa_pub_key(rsa_pub_key);
  251. return status;
  252. }