sigma_crypto_layer.cpp 13 KB

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