sigma_crypto_layer.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * Copyright (C) 2011-2018 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #include "sigma_crypto_layer.h"
  32. #include "sgx_ecc256_internal.h"
  33. #include "pse_pr_inc.h"
  34. #include "pse_pr_types.h"
  35. #include "safe_id.h"
  36. #include <stddef.h>
  37. #include <time.h>
  38. #include <cstring>
  39. #include "le2be_macros.h"
  40. #include "prepare_hmac_sha256.h"
  41. #include "prepare_hash_sha256.h"
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif
  45. #include "epid/verifier/1.1/api.h"
  46. #include "epid/common/1.1/types.h"
  47. #ifdef __cplusplus
  48. }
  49. #endif
  50. #include "sgx_trts.h"
  51. #include "ae_ipp.h"
  52. #include "util.h"
  53. #include "Keys.h"
  54. #include "pairing_blob.h"
  55. static ae_error_t MapEpidResultToAEError(EpidStatus epid_result)
  56. {
  57. ae_error_t status = PSE_PR_PCH_EPID_UNKNOWN_ERROR;
  58. switch (epid_result)
  59. {
  60. case kEpidNoErr: status = AE_SUCCESS; break;
  61. case kEpidSigInvalid: status = PSE_PR_PCH_EPID_SIG_INVALID; break;
  62. case kEpidSigRevokedInGroupRl: status = PSE_PR_PCH_EPID_SIG_REVOKED_IN_GROUPRL; break;
  63. case kEpidSigRevokedInPrivRl: status = PSE_PR_PCH_EPID_SIG_REVOKED_IN_PRIVRL; break;
  64. case kEpidSigRevokedInSigRl: status = PSE_PR_PCH_EPID_SIG_REVOKED_IN_SIGRL; break;
  65. case kEpidSigRevokedInVerifierRl: status = PSE_PR_PCH_EPID_SIG_REVOKED_IN_VERIFIERRL; break;
  66. case kEpidErr: status = PSE_PR_PCH_EPID_UNKNOWN_ERROR; break;
  67. case kEpidNotImpl: status = PSE_PR_PCH_EPID_NOT_IMPLEMENTED; break;
  68. case kEpidBadArgErr: status = PSE_PR_PCH_EPID_BAD_ARG_ERR; break;
  69. case kEpidNoMemErr: status = PSE_PR_PCH_EPID_NO_MEMORY_ERR; break;
  70. case kEpidMemAllocErr: status = PSE_PR_PCH_EPID_NO_MEMORY_ERR; break;
  71. case kEpidMathErr: status = PSE_PR_PCH_EPID_MATH_ERR; break;
  72. case kEpidDivByZeroErr: status = PSE_PR_PCH_EPID_DIVIDED_BY_ZERO_ERR; break;
  73. case kEpidUnderflowErr: status = PSE_PR_PCH_EPID_UNDERFLOW_ERR; break;
  74. case kEpidHashAlgorithmNotSupported: status = PSE_PR_PCH_EPID_HASH_ALGORITHM_NOT_SUPPORTED; break;
  75. case kEpidRandMaxIterErr: status = PSE_PR_PCH_EPID_RAND_MAX_ITER_ERR; break;
  76. case kEpidDuplicateErr: status = PSE_PR_PCH_EPID_DUPLICATE_ERR; break;
  77. case kEpidInconsistentBasenameSetErr: status = PSE_PR_PCH_EPID_INCONSISTENT_BASENAME_SET_ERR; break;
  78. case kEpidMathQuadraticNonResidueError: status = PSE_PR_PCH_EPID_MATH_ERR; break;
  79. default: status = PSE_PR_PCH_EPID_UNKNOWN_ERROR; break;
  80. }
  81. return status;
  82. }
  83. SigmaCryptoLayer::SigmaCryptoLayer()
  84. {
  85. }
  86. SigmaCryptoLayer::~SigmaCryptoLayer(void)
  87. {
  88. memset_s(m_local_private_key_b_little_endian, SIGMA_SESSION_PRIVKEY_LENGTH, 0, SIGMA_SESSION_PRIVKEY_LENGTH);
  89. memset_s(m_SMK, SIGMA_SMK_LENGTH, 0, SIGMA_SMK_LENGTH);
  90. memset_s(m_SK, sizeof(m_SK), 0, sizeof(m_SK));
  91. memset_s(m_MK, sizeof(m_MK), 0, sizeof(m_MK));
  92. }
  93. ae_error_t SigmaCryptoLayer::DeriveSkMk(/* In */ sgx_ecc_state_handle_t ecc_handle)
  94. {
  95. ae_error_t ae_status = PSE_PR_DERIVE_SMK_ERROR;
  96. IppStatus Status;
  97. Ipp8u Gab[SGX_ECP256_KEY_SIZE*2] = {0};
  98. Ipp8u Gab_Wth_00[SGX_ECP256_KEY_SIZE*2+1] = {0};
  99. Ipp8u Gab_Wth_01[SGX_ECP256_KEY_SIZE*2+1] = {0};
  100. Ipp8u GabHMACSha256[SGX_SHA256_HASH_SIZE] = { 0 };
  101. /* convert m_remotePublicKey_ga_big_endian to little endian format */
  102. uint8_t public_key_little_endian[SIGMA_SESSION_PUBKEY_LENGTH];
  103. memcpy(public_key_little_endian, m_remote_public_key_ga_big_endian, SIGMA_SESSION_PUBKEY_LENGTH);
  104. SwapEndian_32B(&(public_key_little_endian[0]));
  105. SwapEndian_32B(&(public_key_little_endian[32]));
  106. do
  107. {
  108. // Watch for null pointers
  109. if (ecc_handle == NULL)
  110. {
  111. ae_status = PSE_PR_PARAMETER_ERROR;
  112. break;
  113. }
  114. sgx_status_t sgx_status = sgx_ecc256_compute_shared_point((sgx_ec256_private_t *)m_local_private_key_b_little_endian,
  115. (sgx_ec256_public_t *)public_key_little_endian,
  116. (sgx_ec256_shared_point_t *)Gab,
  117. ecc_handle);
  118. if (SGX_SUCCESS != sgx_status)
  119. {
  120. if (SGX_ERROR_OUT_OF_MEMORY == sgx_status)
  121. ae_status = PSE_PR_INSUFFICIENT_MEMORY_ERROR;
  122. break;
  123. }
  124. //Initialize Variables required to get SK, SMK, MK
  125. memcpy(Gab_Wth_00, Gab, sizeof(Gab));
  126. Gab_Wth_00[sizeof(Gab)] = 0;
  127. memcpy(Gab_Wth_01, Gab, sizeof(Gab));
  128. Gab_Wth_01[sizeof(Gab)] = 1;
  129. Ipp8u HMAC_Key[SIGMA_HMAC_LENGTH] = {0};
  130. //Compute SMK
  131. Status = ippsHMAC_Message(Gab_Wth_00, sizeof(Gab_Wth_00), HMAC_Key, sizeof(HMAC_Key),
  132. m_SMK, sizeof(m_SMK), IPP_ALG_HASH_SHA256);
  133. if (Status != ippStsNoErr)
  134. {
  135. if (Status == ippStsNoMemErr || Status == ippStsMemAllocErr)
  136. ae_status = PSE_PR_INSUFFICIENT_MEMORY_ERROR;
  137. break;
  138. }
  139. // Compute SK and MK
  140. Status = ippsHMAC_Message(Gab_Wth_01, sizeof(Gab_Wth_01), HMAC_Key, sizeof(HMAC_Key),
  141. GabHMACSha256, sizeof(GabHMACSha256), IPP_ALG_HASH_SHA256);
  142. if (Status != ippStsNoErr)
  143. {
  144. if (Status == ippStsNoMemErr || Status == ippStsMemAllocErr)
  145. ae_status = PSE_PR_INSUFFICIENT_MEMORY_ERROR;
  146. break;
  147. }
  148. // Derive SK and MK from SHA256(g^ab)
  149. memcpy(m_SK, (GabHMACSha256), SIGMA_SK_LENGTH); // SK: bits 0-127
  150. memcpy(m_MK, (GabHMACSha256 + SIGMA_SK_LENGTH), SIGMA_MK_LENGTH); // MK: bits 128-255
  151. ae_status = AE_SUCCESS;
  152. } while (false);
  153. // Defense-in-depth: clear secrets in stack before return
  154. memset_s(Gab, sizeof(Gab), 0, sizeof(Gab));
  155. memset_s(Gab_Wth_00, sizeof(Gab_Wth_00), 0, sizeof(Gab_Wth_00));
  156. memset_s(Gab_Wth_01, sizeof(Gab_Wth_00), 0, sizeof(Gab_Wth_00));
  157. memset_s(GabHMACSha256, sizeof(GabHMACSha256), 0, sizeof(GabHMACSha256));
  158. return ae_status;
  159. }
  160. ae_error_t SigmaCryptoLayer::calc_s2_hmac(
  161. SIGMA_HMAC* hmac, const SIGMA_S2_MESSAGE* s2, size_t nS2VLDataLen)
  162. {
  163. PrepareHMACSHA256 p(m_SMK, sizeof(m_SMK));
  164. p.Update(s2->Gb, sizeof(s2->Gb));
  165. p.Update(s2->Basename, sizeof(s2->Basename));
  166. p.Update(&s2->OcspReq, sizeof(s2->OcspReq));
  167. p.Update(s2->Data, nS2VLDataLen);
  168. //NRG: SIGMA_HMAC - HMAC_SHA256 of [Gb || Basename || OCSP Req ||
  169. // Verifier Cert || Sig-RL List ], using SMK
  170. return p.Finalize(hmac);
  171. }
  172. ae_error_t SigmaCryptoLayer::calc_s3_hmac(
  173. SIGMA_HMAC* hmac, const SIGMA_S3_MESSAGE* s3, size_t nS3VLDataLen)
  174. {
  175. PrepareHMACSHA256 p(m_SMK, sizeof(m_SMK));
  176. p.Update(&s3->TaskInfo, sizeof(s3->TaskInfo));
  177. p.Update(s3->Ga, sizeof(s3->Ga));
  178. p.Update(s3->Data, nS3VLDataLen);
  179. //NRG: SIGMA_HMAC -- HMAC_SHA256 of [TaskInfo || g^a ||
  180. // EPIDCertprvr || EPIDSig(g^a || g^b)], using SMK
  181. return p.Finalize(hmac);
  182. }
  183. ae_error_t SigmaCryptoLayer::ComputePR(SIGMA_SECRET_KEY* oldSK, Ipp8u byteToAdd, SIGMA_HMAC* hmac)
  184. {
  185. Ipp8u Sk_Wth_Added_Byte[sizeof(SIGMA_SIGN_KEY)+1];
  186. ae_error_t ae_status = PSE_PR_PR_CALC_ERROR;
  187. memset(hmac, 0, sizeof(*hmac));
  188. do
  189. {
  190. memcpy(Sk_Wth_Added_Byte, oldSK, SIGMA_SK_LENGTH);
  191. Sk_Wth_Added_Byte[SIGMA_SK_LENGTH] = byteToAdd;
  192. //Compute hmac
  193. IppStatus ippstatus = ippsHMAC_Message(Sk_Wth_Added_Byte,
  194. SIGMA_SK_LENGTH+1, (Ipp8u*)m_MK, SIGMA_MK_LENGTH,
  195. (Ipp8u*)hmac, SIGMA_HMAC_LENGTH, IPP_ALG_HASH_SHA256);
  196. // defense-in-depth, clear secret data
  197. memset_s(Sk_Wth_Added_Byte, sizeof(Sk_Wth_Added_Byte), 0, sizeof(Sk_Wth_Added_Byte));
  198. if (ippStsNoErr != ippstatus)
  199. {
  200. if (ippStsNoMemErr == ippstatus || ippStsMemAllocErr == ippstatus)
  201. ae_status = PSE_PR_INSUFFICIENT_MEMORY_ERROR;
  202. break;
  203. }
  204. ae_status = AE_SUCCESS;
  205. } while (0);
  206. return ae_status;
  207. }
  208. ae_error_t SigmaCryptoLayer::ComputeId(Ipp8u byteToAdd,
  209. SHA256_HASH* hash)
  210. {
  211. memset(hash, 0, sizeof(*hash));
  212. PrepareHashSHA256 p;
  213. p.Update(m_SK, sizeof(SIGMA_SIGN_KEY));
  214. p.Update(m_MK, sizeof(SIGMA_MAC_KEY));
  215. p.Update(&byteToAdd, sizeof(Ipp8u));
  216. return p.Finalize(hash);
  217. }
  218. ae_error_t SigmaCryptoLayer::MsgVerifyPch(Ipp8u* PubKeyPch, int PubKeyPchLen,
  219. Ipp8u* EpidParamsCert, Ipp8u* Msg, int MsgLen,
  220. Ipp8u* Bsn, int BsnLen, Ipp8u* Signature,
  221. int SignatureLen,
  222. Ipp8u* PrivRevList, int PrivRL_Len, Ipp8u* SigRevList, int SigRL_Len,
  223. Ipp8u* GrpRevList, int GrpRL_Len)
  224. {
  225. ae_error_t status = AE_FAILURE;
  226. EpidStatus SafeIdRes = kEpidNoErr;
  227. Epid11Signature Epid11Sig;
  228. Epid11Signature *SigPointer = NULL;
  229. memset_s(&Epid11Sig, sizeof(Epid11Sig), 0, sizeof(Epid11Sig));
  230. UNUSED(EpidParamsCert);
  231. UNUSED(Bsn);
  232. UNUSED(BsnLen);
  233. Epid11VerifierCtx* ctx = NULL;
  234. do
  235. {
  236. // Watch for null pointers
  237. if ((PubKeyPch == NULL) || (Msg == NULL) || (Signature == NULL))
  238. {
  239. status = PSE_PR_PARAMETER_ERROR;
  240. break;
  241. }
  242. // Verify the length of public key and signature buffers
  243. if (((size_t)PubKeyPchLen < (SAFEID_CERT_LEN - ECDSA_SIGNATURE_LEN)) ||
  244. (SignatureLen < SAFEID_SIG_LEN))
  245. {
  246. status = PSE_PR_PARAMETER_ERROR;
  247. break;
  248. }
  249. SafeIdRes = Epid11VerifierCreate(
  250. (Epid11GroupPubKey* )(PubKeyPch),
  251. NULL, &ctx);
  252. status = MapEpidResultToAEError(SafeIdRes);
  253. if (AE_FAILED(status)){
  254. break;
  255. }
  256. if(PrivRevList != NULL){
  257. SafeIdRes = Epid11VerifierSetPrivRl(ctx, (Epid11PrivRl *)(PrivRevList), PrivRL_Len);
  258. status = MapEpidResultToAEError(SafeIdRes);
  259. if(AE_FAILED(status)) {break;}
  260. }
  261. if(SigRevList != NULL){
  262. SafeIdRes = Epid11VerifierSetSigRl(ctx, (Epid11SigRl *)(SigRevList), SigRL_Len);
  263. status = MapEpidResultToAEError(SafeIdRes);
  264. if(AE_FAILED(status)) {break;}
  265. }
  266. if(GrpRevList != NULL){
  267. SafeIdRes = Epid11VerifierSetGroupRl(ctx, (Epid11GroupRl *)(GrpRevList), GrpRL_Len);
  268. status = MapEpidResultToAEError(SafeIdRes);
  269. if(AE_FAILED(status)) {break;}
  270. }
  271. //verify signature with Pub Key in ctx
  272. //For epid-sdk-3.0, when the sigRL is null, the signature size includes "rl_ver" and "n2" fields
  273. //(See structure definition of Epid11Signature)
  274. //So we must use bigger buffer add 8 bytes to the length
  275. if(SignatureLen == sizeof(Epid11BasicSignature)){
  276. memcpy(&Epid11Sig, Signature, SignatureLen);
  277. SignatureLen = static_cast<int>(SignatureLen + sizeof(Epid11Sig.rl_ver) + sizeof(Epid11Sig.n2));
  278. SigPointer = &Epid11Sig;
  279. }
  280. else
  281. {
  282. SigPointer = (Epid11Signature *)Signature;
  283. }
  284. SafeIdRes = Epid11Verify(ctx,
  285. SigPointer, SignatureLen,
  286. Msg, MsgLen);
  287. status = MapEpidResultToAEError(SafeIdRes);
  288. if (AE_FAILED(status)){
  289. break;
  290. }
  291. status = AE_SUCCESS;
  292. } while (false);
  293. if (NULL != ctx)
  294. {
  295. Epid11VerifierDelete(&ctx);
  296. }
  297. return status;
  298. }