ipp_bn.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. /**
  32. * File:
  33. * ipp_bn.cpp
  34. *Description:
  35. * Wrappers for Big number generation and free functions
  36. *
  37. */
  38. #include "ipp_wrapper.h"
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #ifndef _TLIBC_CDECL_
  42. extern "C" int memset_s(void *s, size_t smax, int c, size_t n);
  43. #endif
  44. extern "C" IppStatus
  45. newBN(const Ipp32u *data, int size_in_bytes, IppsBigNumState **p_new_BN)
  46. {
  47. IppsBigNumState *pBN=0;
  48. int bn_size = 0;
  49. if(p_new_BN == NULL || size_in_bytes <= 0 || size_in_bytes % sizeof(Ipp32u))
  50. return ippStsBadArgErr;
  51. /* Get the size of the IppsBigNumState context in bytes */
  52. IppStatus error_code = ippsBigNumGetSize(size_in_bytes/(int)sizeof(Ipp32u), &bn_size);
  53. if(error_code != ippStsNoErr)
  54. {
  55. *p_new_BN = 0;
  56. return error_code;
  57. }
  58. pBN = (IppsBigNumState *) malloc(bn_size);
  59. if(!pBN)
  60. {
  61. error_code = ippStsMemAllocErr;
  62. *p_new_BN = 0;
  63. return error_code;
  64. }
  65. /* Initializes context and partitions allocated buffer */
  66. error_code = ippsBigNumInit(size_in_bytes/(int)sizeof(Ipp32u), pBN);
  67. if(error_code != ippStsNoErr)
  68. {
  69. SAFE_FREE_MM(pBN);
  70. *p_new_BN = 0;
  71. return error_code;
  72. }
  73. if(data)
  74. {
  75. error_code = ippsSet_BN(IppsBigNumPOS, size_in_bytes/(int)sizeof(Ipp32u), data, pBN);
  76. if(error_code != ippStsNoErr)
  77. {
  78. SAFE_FREE_MM(pBN);
  79. *p_new_BN = 0;
  80. return error_code;
  81. }
  82. }
  83. *p_new_BN = pBN;
  84. return error_code;
  85. }
  86. extern "C" void secure_free_BN(IppsBigNumState *pBN, int size_in_bytes)
  87. {
  88. if(pBN == NULL || size_in_bytes <= 0 || size_in_bytes % sizeof(Ipp32u))
  89. {
  90. if(pBN)
  91. {
  92. free(pBN);
  93. }
  94. return;
  95. }
  96. int bn_size = 0;
  97. /* Get the size of the IppsBigNumState context in bytes
  98. * Since we have checked the size_in_bytes before and the &bn_size is not NULL,
  99. * ippsBigNumGetSize never returns failure
  100. */
  101. if(ippsBigNumGetSize(size_in_bytes/(int)sizeof(Ipp32u), &bn_size) != ippStsNoErr)
  102. {
  103. free(pBN);
  104. return;
  105. }
  106. /* Clear the buffer before free. */
  107. memset_s(pBN, bn_size, 0, bn_size);
  108. free(pBN);
  109. return;
  110. }