sgx_ecc256_common.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 "sgx_ecc256_common.h"
  32. IppStatus sgx_ipp_newBN(const Ipp32u *p_data, int size_in_bytes, IppsBigNumState **p_new_BN)
  33. {
  34. IppsBigNumState *pBN = 0;
  35. int bn_size = 0;
  36. if (p_new_BN == NULL || (size_in_bytes <= 0) || ((size_in_bytes % sizeof(Ipp32u)) != 0))
  37. return ippStsBadArgErr;
  38. // Get the size of the IppsBigNumState context in bytes
  39. IppStatus error_code = ippsBigNumGetSize(size_in_bytes/(int)sizeof(Ipp32u), &bn_size);
  40. if (error_code != ippStsNoErr)
  41. {
  42. *p_new_BN = 0;
  43. return error_code;
  44. }
  45. pBN = (IppsBigNumState *)malloc(bn_size);
  46. if (!pBN)
  47. {
  48. error_code = ippStsMemAllocErr;
  49. *p_new_BN = 0;
  50. return error_code;
  51. }
  52. // Initialize context and partition allocated buffer
  53. error_code = ippsBigNumInit(size_in_bytes/(int)sizeof(Ipp32u), pBN);
  54. if (error_code != ippStsNoErr)
  55. {
  56. free(pBN);
  57. *p_new_BN = 0;
  58. return error_code;
  59. }
  60. if (p_data)
  61. {
  62. error_code = ippsSet_BN(IppsBigNumPOS, size_in_bytes/(int)sizeof(Ipp32u), p_data, pBN);
  63. if (error_code != ippStsNoErr)
  64. {
  65. *p_new_BN = 0;
  66. free(pBN);
  67. return error_code;
  68. }
  69. }
  70. *p_new_BN = pBN;
  71. return error_code;
  72. }
  73. void sgx_ipp_secure_free_BN(IppsBigNumState *pBN, int size_in_bytes)
  74. {
  75. if (pBN == NULL || size_in_bytes <= 0 || ((size_in_bytes % sizeof(Ipp32u)) != 0))
  76. {
  77. if (pBN)
  78. {
  79. free(pBN);
  80. }
  81. return;
  82. }
  83. int bn_size = 0;
  84. // Get the size of the IppsBigNumState context in bytes
  85. // Since we have checked the size_in_bytes before and the &bn_size is not NULL, ippsBigNumGetSize never returns failure
  86. IppStatus error_code = ippsBigNumGetSize(size_in_bytes/(int)sizeof(Ipp32u), &bn_size);
  87. if (error_code != ippStsNoErr)
  88. {
  89. free(pBN);
  90. return;
  91. }
  92. // Clear the buffer before free.
  93. memset_s(pBN, bn_size, 0, bn_size);
  94. free(pBN);
  95. return;
  96. }
  97. IppStatus __STDCALL sgx_ipp_DRNGen(Ipp32u* pRandBNU, int nBits, void* pCtx)
  98. {
  99. sgx_status_t sgx_ret;
  100. UNUSED(pCtx);
  101. if (0 != nBits % 8)
  102. {
  103. // Must be byte aligned
  104. return ippStsSizeErr;
  105. }
  106. if (!pRandBNU)
  107. {
  108. return ippStsNullPtrErr;
  109. }
  110. sgx_ret = sgx_read_rand((uint8_t*)pRandBNU, (uint32_t)nBits / 8);
  111. if (SGX_SUCCESS != sgx_ret)
  112. {
  113. return ippStsErr;
  114. }
  115. return ippStsNoErr;
  116. }