pve_qe_common.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 "stdlib.h"
  32. #include "string.h"
  33. #include "pve_qe_common.h"
  34. #include "assert.h"
  35. #include "util.h"
  36. #include "sgx_trts.h"
  37. #ifdef __cplusplus
  38. extern "C" {
  39. #endif
  40. #include "epid/common/src/memory.h"
  41. #include "epid/member/software_member.h"
  42. #include "epid/member/src/write_precomp.h"
  43. #include "epid/member/src/signbasic.h"
  44. #include "epid/member/src/nrprove.h"
  45. #ifdef __cplusplus
  46. }
  47. #endif
  48. /*
  49. * Function used to get ECP context.
  50. *
  51. * @param pp_new_ecp[out] Used to get the pointer of ECP context.
  52. * @return IppStatus ippStsNoErr or other error cases.
  53. */
  54. IppStatus new_std_256_ecp(IppsECCPState **pp_new_ecp)
  55. {
  56. int ctx_size = 0;
  57. IppStatus ipp_ret = ippStsNoErr;
  58. IppsECCPState* p_ctx = NULL;
  59. ipp_ret = ippsECCPGetSize(256, &ctx_size);
  60. if(ippStsNoErr != ipp_ret)
  61. return ipp_ret;
  62. p_ctx = (IppsECCPState*)malloc(ctx_size);
  63. if(NULL == p_ctx)
  64. return ippStsNoMemErr;
  65. ipp_ret = ippsECCPInit(256, p_ctx);
  66. if(ippStsNoErr != ipp_ret)
  67. {
  68. free(p_ctx);
  69. return ipp_ret;
  70. }
  71. ipp_ret = ippsECCPSetStd256r1(p_ctx);
  72. if(ippStsNoErr != ipp_ret)
  73. {
  74. free(p_ctx);
  75. return ipp_ret;
  76. }
  77. *pp_new_ecp = p_ctx;
  78. return ipp_ret;
  79. }
  80. /*
  81. * Function used to free ECP context.
  82. *
  83. * @param p_ecp[out] Pointer of ECP context.
  84. */
  85. void secure_free_std_256_ecp(IppsECCPState *p_ecp)
  86. {
  87. int ctx_size = 0;
  88. IppStatus ipp_ret = ippStsNoErr;
  89. if(p_ecp)
  90. {
  91. ipp_ret = ippsECCPGetSize(256, &ctx_size);
  92. if(ippStsNoErr == ipp_ret)
  93. memset_s(p_ecp, ctx_size, 0, ctx_size);
  94. free(p_ecp);
  95. }
  96. return;
  97. }
  98. int IPP_STDCALL epid_random_func(
  99. unsigned int *p_random_data,
  100. int bits,
  101. void* p_user_data)
  102. {
  103. UNUSED(p_user_data);
  104. assert(!(bits % 8));
  105. if(SGX_SUCCESS != sgx_read_rand((uint8_t *)p_random_data,
  106. ROUND_TO(bits, 8) / 8))
  107. return ippStsErr;
  108. return ippStsNoErr;
  109. }
  110. EpidStatus epid_member_create(BitSupplier rnd_func, void* rnd_param, FpElemStr* f, MemberCtx** ctx)
  111. {
  112. MemberParams params;
  113. size_t member_size = 0;
  114. EpidStatus epid_ret = kEpidNoErr;
  115. memset(&params, 0, sizeof(params));
  116. params.rnd_func = rnd_func;
  117. params.rnd_param = rnd_param;
  118. params.f = f;
  119. epid_ret = EpidMemberGetSize(&params, &member_size);
  120. if (kEpidNoErr != epid_ret) {
  121. return epid_ret;
  122. }
  123. *ctx = (MemberCtx*)SAFE_ALLOC(member_size);
  124. if (!(*ctx)) {
  125. return kEpidNoMemErr;
  126. }
  127. epid_ret = EpidMemberInit(&params, *ctx);
  128. if (kEpidNoErr != epid_ret) {
  129. SAFE_FREE(*ctx);
  130. *ctx = NULL;
  131. return epid_ret;
  132. }
  133. epid_ret = EpidMemberSetHashAlg(*ctx, kSha256);
  134. if (kEpidNoErr != epid_ret) {
  135. goto ret_point;
  136. }
  137. ret_point:
  138. if (kEpidNoErr != epid_ret) {
  139. epid_member_delete(ctx);
  140. }
  141. return epid_ret;
  142. }
  143. void epid_member_delete(MemberCtx** ctx)
  144. {
  145. if (!ctx) {
  146. return;
  147. }
  148. EpidMemberDeinit(*ctx);
  149. SAFE_FREE(*ctx);
  150. *ctx = NULL;
  151. }