pve_qe_common.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 "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. #include "isk_pub.hh"
  38. /*
  39. * Function used to get ECP context.
  40. *
  41. * @param pp_new_ecp[out] Used to get the pointer of ECP context.
  42. * @return IppStatus ippStsNoErr or other error cases.
  43. */
  44. IppStatus new_std_256_ecp(IppsECCPState **pp_new_ecp)
  45. {
  46. int ctx_size = 0;
  47. IppStatus ipp_ret = ippStsNoErr;
  48. IppsECCPState* p_ctx = NULL;
  49. ipp_ret = ippsECCPGetSize(256, &ctx_size);
  50. if(ippStsNoErr != ipp_ret)
  51. return ipp_ret;
  52. p_ctx = (IppsECCPState*)malloc(ctx_size);
  53. if(NULL == p_ctx)
  54. return ippStsNoMemErr;
  55. ipp_ret = ippsECCPInit(256, p_ctx);
  56. if(ippStsNoErr != ipp_ret)
  57. {
  58. free(p_ctx);
  59. return ipp_ret;
  60. }
  61. ipp_ret = ippsECCPSetStd(IppECCPStd256r1, p_ctx);
  62. if(ippStsNoErr != ipp_ret)
  63. {
  64. free(p_ctx);
  65. return ipp_ret;
  66. }
  67. *pp_new_ecp = p_ctx;
  68. return ipp_ret;
  69. }
  70. /*
  71. * Function used to free ECP context.
  72. *
  73. * @param p_ecp[out] Pointer of ECP context.
  74. */
  75. void secure_free_std_256_ecp(IppsECCPState *p_ecp)
  76. {
  77. int ctx_size = 0;
  78. IppStatus ipp_ret = ippStsNoErr;
  79. if(p_ecp)
  80. {
  81. ipp_ret = ippsECCPGetSize(256, &ctx_size);
  82. if(ippStsNoErr == ipp_ret)
  83. memset_s(p_ecp, ctx_size, 0, ctx_size);
  84. free(p_ecp);
  85. }
  86. return;
  87. }
  88. int __STDCALL epid_random_func(
  89. unsigned int *p_random_data,
  90. int bits,
  91. void* p_user_data)
  92. {
  93. UNUSED(p_user_data);
  94. assert(!(bits % 8));
  95. if(SGX_SUCCESS != sgx_read_rand((uint8_t *)p_random_data,
  96. ROUND_TO(bits, 8) / 8))
  97. return ippStsErr;
  98. return ippStsNoErr;
  99. }