sgx_rsa_internal.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 "ipp_wrapper.h"
  34. extern "C" IppStatus newBN(const Ipp32u *data, int size_in_bytes, IppsBigNumState **p_new_BN)
  35. {
  36. IppsBigNumState *pBN = 0;
  37. int bn_size = 0;
  38. if (p_new_BN == NULL || size_in_bytes <= 0 || size_in_bytes % sizeof(Ipp32u))
  39. return ippStsBadArgErr;
  40. /* Get the size of the IppsBigNumState context in bytes */
  41. IppStatus error_code = ippsBigNumGetSize(size_in_bytes / (int)sizeof(Ipp32u), &bn_size);
  42. if (error_code != ippStsNoErr) {
  43. *p_new_BN = 0;
  44. return error_code;
  45. }
  46. pBN = (IppsBigNumState *)malloc(bn_size);
  47. if (!pBN) {
  48. error_code = ippStsMemAllocErr;
  49. *p_new_BN = 0;
  50. return error_code;
  51. }
  52. /* Initializes context and partitions allocated buffer */
  53. error_code = ippsBigNumInit(size_in_bytes / (int)sizeof(Ipp32u), pBN);
  54. if (error_code != ippStsNoErr) {
  55. SAFE_FREE_MM(pBN);
  56. *p_new_BN = 0;
  57. return error_code;
  58. }
  59. if (data) {
  60. error_code = ippsSet_BN(IppsBigNumPOS, size_in_bytes / (int)sizeof(Ipp32u), data, pBN);
  61. if (error_code != ippStsNoErr) {
  62. SAFE_FREE_MM(pBN);
  63. *p_new_BN = 0;
  64. return error_code;
  65. }
  66. }
  67. *p_new_BN = pBN;
  68. return error_code;
  69. }
  70. extern "C" void secure_free_BN(IppsBigNumState *pBN, int size_in_bytes)
  71. {
  72. if (pBN == NULL || size_in_bytes <= 0 || size_in_bytes % sizeof(Ipp32u)) {
  73. if (pBN) {
  74. free(pBN);
  75. }
  76. return;
  77. }
  78. int bn_size = 0;
  79. /* Get the size of the IppsBigNumState context in bytes
  80. * Since we have checked the size_in_bytes before and the &bn_size is not NULL,
  81. * ippsBigNumGetSize never returns failure
  82. */
  83. if (ippsBigNumGetSize(size_in_bytes / (int)sizeof(Ipp32u), &bn_size) != ippStsNoErr) {
  84. free(pBN);
  85. return;
  86. }
  87. /* Clear the buffer before free. */
  88. memset_s(pBN, bn_size, 0, bn_size);
  89. free(pBN);
  90. return;
  91. }
  92. extern "C" void secure_free_rsa_pri2_key(int p_byte_size, IppsRSAPrivateKeyState *pri_key2)
  93. {
  94. if (p_byte_size <= 0 || pri_key2 == NULL) {
  95. if (pri_key2)
  96. free(pri_key2);
  97. return;
  98. }
  99. int rsa2_size = 0;
  100. if (ippsRSA_GetSizePrivateKeyType2(p_byte_size / 2 * 8, p_byte_size / 2 * 8, &rsa2_size) != ippStsNoErr) {
  101. free(pri_key2);
  102. return;
  103. }
  104. /* Clear the buffer before free. */
  105. memset_s(pri_key2, rsa2_size, 0, rsa2_size);
  106. free(pri_key2);
  107. return;
  108. }
  109. extern "C" void secure_free_rsa_pub_key(int n_byte_size, int e_byte_size, IppsRSAPublicKeyState *pub_key)
  110. {
  111. if (n_byte_size <= 0 || e_byte_size <= 0 || pub_key == NULL) {
  112. if (pub_key)
  113. free(pub_key);
  114. return;
  115. }
  116. int rsa_size = 0;
  117. if (ippsRSA_GetSizePublicKey(n_byte_size * 8, e_byte_size * 8, &rsa_size) != ippStsNoErr) {
  118. free(pub_key);
  119. return;
  120. }
  121. /* Clear the buffer before free. */
  122. memset_s(pub_key, rsa_size, 0, rsa_size);
  123. free(pub_key);
  124. return;
  125. }