sigma_crypto_layer.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. #include "epid/verifier/1.1/api.h"
  43. #include "epid/common/1.1/types.h"
  44. #include "sgx_trts.h"
  45. #include "ae_ipp.h"
  46. #include "util.h"
  47. #include "Keys.h"
  48. #include "pairing_blob.h"
  49. static ae_error_t MapEpidResultToAEError(EpidStatus epid_result)
  50. {
  51. ae_error_t status = PSE_PR_PCH_EPID_UNKNOWN_ERROR;
  52. switch (epid_result)
  53. {
  54. case kEpidNoErr: status = AE_SUCCESS; break;
  55. case kEpidSigInvalid: status = PSE_PR_PCH_EPID_SIG_INVALID; break;
  56. case kEpidSigRevokedInGroupRl: status = PSE_PR_PCH_EPID_SIG_REVOKED_IN_GROUPRL; break;
  57. case kEpidSigRevokedInPrivRl: status = PSE_PR_PCH_EPID_SIG_REVOKED_IN_PRIVRL; break;
  58. case kEpidSigRevokedInSigRl: status = PSE_PR_PCH_EPID_SIG_REVOKED_IN_SIGRL; break;
  59. case kEpidSigRevokedInVerifierRl: status = PSE_PR_PCH_EPID_SIG_REVOKED_IN_VERIFIERRL; break;
  60. case kEpidErr: status = PSE_PR_PCH_EPID_UNKNOWN_ERROR; break;
  61. case kEpidNotImpl: status = PSE_PR_PCH_EPID_NOT_IMPLEMENTED; break;
  62. case kEpidBadArgErr: status = PSE_PR_PCH_EPID_BAD_ARG_ERR; break;
  63. case kEpidNoMemErr: status = PSE_PR_PCH_EPID_NO_MEMORY_ERR; break;
  64. case kEpidMemAllocErr: status = PSE_PR_PCH_EPID_NO_MEMORY_ERR; break;
  65. case kEpidMathErr: status = PSE_PR_PCH_EPID_MATH_ERR; break;
  66. case kEpidDivByZeroErr: status = PSE_PR_PCH_EPID_DIVIDED_BY_ZERO_ERR; break;
  67. case kEpidUnderflowErr: status = PSE_PR_PCH_EPID_UNDERFLOW_ERR; break;
  68. case kEpidHashAlgorithmNotSupported: status = PSE_PR_PCH_EPID_HASH_ALGORITHM_NOT_SUPPORTED; break;
  69. case kEpidRandMaxIterErr: status = PSE_PR_PCH_EPID_RAND_MAX_ITER_ERR; break;
  70. case kEpidDuplicateErr: status = PSE_PR_PCH_EPID_DUPLICATE_ERR; break;
  71. case kEpidInconsistentBasenameSetErr: status = PSE_PR_PCH_EPID_INCONSISTENT_BASENAME_SET_ERR; break;
  72. case kEpidMathQuadraticNonResidueError: status = PSE_PR_PCH_EPID_MATH_ERR; break;
  73. default: status = PSE_PR_PCH_EPID_UNKNOWN_ERROR; break;
  74. }
  75. return status;
  76. }
  77. SigmaCryptoLayer::SigmaCryptoLayer()
  78. {
  79. }
  80. SigmaCryptoLayer::~SigmaCryptoLayer(void)
  81. {
  82. memset_s(m_local_private_key_b_little_endian, SIGMA_SESSION_PRIVKEY_LENGTH, 0, SIGMA_SESSION_PRIVKEY_LENGTH);
  83. memset_s(m_SMK, SIGMA_SMK_LENGTH, 0, SIGMA_SMK_LENGTH);
  84. memset_s(m_SK, sizeof(m_SK), 0, sizeof(m_SK));
  85. memset_s(m_MK, sizeof(m_MK), 0, sizeof(m_MK));
  86. }
  87. ae_error_t SigmaCryptoLayer::DeriveSkMk(/* In */ sgx_ecc_state_handle_t ecc_handle)
  88. {
  89. ae_error_t ae_status = PSE_PR_DERIVE_SMK_ERROR;
  90. IppStatus Status;
  91. Ipp8u Gab[SGX_ECP256_KEY_SIZE*2] = {0};
  92. Ipp8u Gab_Wth_00[SGX_ECP256_KEY_SIZE*2+1] = {0};
  93. Ipp8u Gab_Wth_01[SGX_ECP256_KEY_SIZE*2+1] = {0};
  94. Ipp8u GabHMACSha256[SGX_SHA256_HASH_SIZE] = { 0 };
  95. /* convert m_remotePublicKey_ga_big_endian to little endian format */
  96. uint8_t public_key_little_endian[SIGMA_SESSION_PUBKEY_LENGTH];
  97. memcpy(public_key_little_endian, m_remote_public_key_ga_big_endian, SIGMA_SESSION_PUBKEY_LENGTH);
  98. SwapEndian_32B(&(public_key_little_endian[0]));
  99. SwapEndian_32B(&(public_key_little_endian[32]));
  100. do
  101. {
  102. // Watch for null pointers
  103. if (ecc_handle == NULL)
  104. {
  105. ae_status = PSE_PR_PARAMETER_ERROR;
  106. break;
  107. }
  108. sgx_status_t sgx_status = sgx_ecc256_compute_shared_point((sgx_ec256_private_t *)m_local_private_key_b_little_endian,
  109. (sgx_ec256_public_t *)public_key_little_endian,
  110. (sgx_ec256_shared_point_t *)Gab,
  111. ecc_handle);
  112. if (SGX_SUCCESS != sgx_status)
  113. {
  114. if (SGX_ERROR_OUT_OF_MEMORY == sgx_status)
  115. ae_status = PSE_PR_INSUFFICIENT_MEMORY_ERROR;
  116. break;
  117. }
  118. //Initialize Variables required to get SK, SMK, MK
  119. memcpy(Gab_Wth_00, Gab, sizeof(Gab));
  120. Gab_Wth_00[sizeof(Gab)] = 0;
  121. memcpy(Gab_Wth_01, Gab, sizeof(Gab));
  122. Gab_Wth_01[sizeof(Gab)] = 1;
  123. Ipp8u HMAC_Key[SIGMA_HMAC_LENGTH] = {0};
  124. //Compute SMK
  125. Status = ippsHMAC_Message(Gab_Wth_00, sizeof(Gab_Wth_00), HMAC_Key, sizeof(HMAC_Key),
  126. m_SMK, sizeof(m_SMK), IPP_ALG_HASH_SHA256);
  127. if (Status != ippStsNoErr)
  128. {
  129. if (Status == ippStsNoMemErr || Status == ippStsMemAllocErr)
  130. ae_status = PSE_PR_INSUFFICIENT_MEMORY_ERROR;
  131. break;
  132. }
  133. // Compute SK and MK
  134. Status = ippsHMAC_Message(Gab_Wth_01, sizeof(Gab_Wth_01), HMAC_Key, sizeof(HMAC_Key),
  135. GabHMACSha256, sizeof(GabHMACSha256), IPP_ALG_HASH_SHA256);
  136. if (Status != ippStsNoErr)
  137. {
  138. if (Status == ippStsNoMemErr || Status == ippStsMemAllocErr)
  139. ae_status = PSE_PR_INSUFFICIENT_MEMORY_ERROR;
  140. break;
  141. }
  142. // Derive SK and MK from SHA256(g^ab)
  143. memcpy(m_SK, (GabHMACSha256), SIGMA_SK_LENGTH); // SK: bits 0-127
  144. memcpy(m_MK, (GabHMACSha256 + SIGMA_SK_LENGTH), SIGMA_MK_LENGTH); // MK: bits 128-255
  145. ae_status = AE_SUCCESS;
  146. } while (false);
  147. // Defense-in-depth: clear secrets in stack before return
  148. memset_s(Gab, sizeof(Gab), 0, sizeof(Gab));
  149. memset_s(Gab_Wth_00, sizeof(Gab_Wth_00), 0, sizeof(Gab_Wth_00));
  150. memset_s(Gab_Wth_01, sizeof(Gab_Wth_00), 0, sizeof(Gab_Wth_00));
  151. memset_s(GabHMACSha256, sizeof(GabHMACSha256), 0, sizeof(GabHMACSha256));
  152. return ae_status;
  153. }
  154. ae_error_t SigmaCryptoLayer::calc_s2_hmac(
  155. SIGMA_HMAC* hmac, const SIGMA_S2_MESSAGE* s2, size_t nS2VLDataLen)
  156. {
  157. PrepareHMACSHA256 p(m_SMK, sizeof(m_SMK));
  158. p.Update(s2->Gb, sizeof(s2->Gb));
  159. p.Update(s2->Basename, sizeof(s2->Basename));
  160. p.Update(&s2->OcspReq, sizeof(s2->OcspReq));
  161. p.Update(s2->Data, nS2VLDataLen);
  162. //NRG: SIGMA_HMAC - HMAC_SHA256 of [Gb || Basename || OCSP Req ||
  163. // Verifier Cert || Sig-RL List ], using SMK
  164. return p.Finalize(hmac);
  165. }
  166. ae_error_t SigmaCryptoLayer::calc_s3_hmac(
  167. SIGMA_HMAC* hmac, const SIGMA_S3_MESSAGE* s3, size_t nS3VLDataLen)
  168. {
  169. PrepareHMACSHA256 p(m_SMK, sizeof(m_SMK));
  170. p.Update(&s3->TaskInfo, sizeof(s3->TaskInfo));
  171. p.Update(s3->Ga, sizeof(s3->Ga));
  172. p.Update(s3->Data, nS3VLDataLen);
  173. //NRG: SIGMA_HMAC -- HMAC_SHA256 of [TaskInfo || g^a ||
  174. // EPIDCertprvr || EPIDSig(g^a || g^b)], using SMK
  175. return p.Finalize(hmac);
  176. }
  177. ae_error_t SigmaCryptoLayer::ComputePR(SIGMA_SECRET_KEY* oldSK, Ipp8u byteToAdd, SIGMA_HMAC* hmac)
  178. {
  179. Ipp8u Sk_Wth_Added_Byte[sizeof(SIGMA_SIGN_KEY)+1];
  180. ae_error_t ae_status = PSE_PR_PR_CALC_ERROR;
  181. memset(hmac, 0, sizeof(*hmac));
  182. do
  183. {
  184. memcpy(Sk_Wth_Added_Byte, oldSK, SIGMA_SK_LENGTH);
  185. Sk_Wth_Added_Byte[SIGMA_SK_LENGTH] = byteToAdd;
  186. //Compute hmac
  187. IppStatus ippstatus = ippsHMAC_Message(Sk_Wth_Added_Byte,
  188. SIGMA_SK_LENGTH+1, (Ipp8u*)m_MK, SIGMA_MK_LENGTH,
  189. (Ipp8u*)hmac, SIGMA_HMAC_LENGTH, IPP_ALG_HASH_SHA256);
  190. // defense-in-depth, clear secret data
  191. memset_s(Sk_Wth_Added_Byte, sizeof(Sk_Wth_Added_Byte), 0, sizeof(Sk_Wth_Added_Byte));
  192. if (ippStsNoErr != ippstatus)
  193. {
  194. if (ippStsNoMemErr == ippstatus || ippStsMemAllocErr == ippstatus)
  195. ae_status = PSE_PR_INSUFFICIENT_MEMORY_ERROR;
  196. break;
  197. }
  198. ae_status = AE_SUCCESS;
  199. } while (0);
  200. return ae_status;
  201. }
  202. ae_error_t SigmaCryptoLayer::ComputeId(Ipp8u byteToAdd,
  203. SHA256_HASH* hash)
  204. {
  205. memset(hash, 0, sizeof(*hash));
  206. PrepareHashSHA256 p;
  207. p.Update(m_SK, sizeof(SIGMA_SIGN_KEY));
  208. p.Update(m_MK, sizeof(SIGMA_MAC_KEY));
  209. p.Update(&byteToAdd, sizeof(Ipp8u));
  210. return p.Finalize(hash);
  211. }
  212. ae_error_t SigmaCryptoLayer::MsgVerifyPch(Ipp8u* PubKeyPch, int PubKeyPchLen,
  213. Ipp8u* EpidParamsCert, Ipp8u* Msg, int MsgLen,
  214. Ipp8u* Bsn, int BsnLen, Ipp8u* Signature,
  215. int SignatureLen,
  216. Ipp8u* PrivRevList, int PrivRL_Len, Ipp8u* SigRevList, int SigRL_Len,
  217. Ipp8u* GrpRevList, int GrpRL_Len)
  218. {
  219. ae_error_t status = AE_FAILURE;
  220. EpidStatus SafeIdRes = kEpidNoErr;
  221. Epid11Signature Epid11Sig;
  222. Epid11Signature *SigPointer = NULL;
  223. memset_s(&Epid11Sig, sizeof(Epid11Sig), 0, sizeof(Epid11Sig));
  224. UNUSED(EpidParamsCert);
  225. UNUSED(Bsn);
  226. UNUSED(BsnLen);
  227. Epid11VerifierCtx* ctx = NULL;
  228. do
  229. {
  230. // Watch for null pointers
  231. if ((PubKeyPch == NULL) || (Msg == NULL) || (Signature == NULL))
  232. {
  233. status = PSE_PR_PARAMETER_ERROR;
  234. break;
  235. }
  236. // Verify the length of public key and signature buffers
  237. if (((size_t)PubKeyPchLen < (SAFEID_CERT_LEN - ECDSA_SIGNATURE_LEN)) ||
  238. (SignatureLen < SAFEID_SIG_LEN))
  239. {
  240. status = PSE_PR_PARAMETER_ERROR;
  241. break;
  242. }
  243. SafeIdRes = Epid11VerifierCreate(
  244. (Epid11GroupPubKey* )(PubKeyPch),
  245. NULL, &ctx);
  246. status = MapEpidResultToAEError(SafeIdRes);
  247. if (AE_FAILED(status)){
  248. break;
  249. }
  250. if(PrivRevList != NULL){
  251. SafeIdRes = Epid11VerifierSetPrivRl(ctx, (Epid11PrivRl *)(PrivRevList), PrivRL_Len);
  252. status = MapEpidResultToAEError(SafeIdRes);
  253. if(AE_FAILED(status)) {break;}
  254. }
  255. if(SigRevList != NULL){
  256. SafeIdRes = Epid11VerifierSetSigRl(ctx, (Epid11SigRl *)(SigRevList), SigRL_Len);
  257. status = MapEpidResultToAEError(SafeIdRes);
  258. if(AE_FAILED(status)) {break;}
  259. }
  260. if(GrpRevList != NULL){
  261. SafeIdRes = Epid11VerifierSetGroupRl(ctx, (Epid11GroupRl *)(GrpRevList), GrpRL_Len);
  262. status = MapEpidResultToAEError(SafeIdRes);
  263. if(AE_FAILED(status)) {break;}
  264. }
  265. //verify signature with Pub Key in ctx
  266. //For epid-sdk-3.0, when the sigRL is null, the signature size includes "rl_ver" and "n2" fields
  267. //(See structure definition of Epid11Signature)
  268. //So we must use bigger buffer add 8 bytes to the length
  269. if(SignatureLen == sizeof(Epid11BasicSignature)){
  270. memcpy(&Epid11Sig, Signature, SignatureLen);
  271. SignatureLen = static_cast<int>(SignatureLen + sizeof(Epid11Sig.rl_ver) + sizeof(Epid11Sig.n2));
  272. SigPointer = &Epid11Sig;
  273. }
  274. else
  275. {
  276. SigPointer = (Epid11Signature *)Signature;
  277. }
  278. SafeIdRes = Epid11Verify(ctx,
  279. SigPointer, SignatureLen,
  280. Msg, MsgLen);
  281. status = MapEpidResultToAEError(SafeIdRes);
  282. if (AE_FAILED(status)){
  283. break;
  284. }
  285. status = AE_SUCCESS;
  286. } while (false);
  287. if (NULL != ctx)
  288. {
  289. Epid11VerifierDelete(&ctx);
  290. }
  291. return status;
  292. }