octstr-testhelper.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*############################################################################
  2. # Copyright 2016 Intel Corporation
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. ############################################################################*/
  16. /*!
  17. * \file
  18. * \brief OctString handling utility implementation.
  19. */
  20. #include "epid/common-testhelper/octstr-testhelper.h"
  21. extern "C" {
  22. #include "epid/common/src/memory.h"
  23. }
  24. #include "ext/ipp/include/ippcp.h"
  25. /// Internal function to delete BigNum
  26. void delete_BigNum(IppsBigNumState** bn) {
  27. if (*bn) {
  28. SAFE_FREE(*bn);
  29. }
  30. }
  31. /// Internal function to create BigNum from an OctStr256
  32. EpidStatus create_BigNum(IppsBigNumState** bn, const OctStr256* str) {
  33. EpidStatus result = kEpidErr;
  34. IppsBigNumState* ipp_bn_ctx = nullptr;
  35. do {
  36. IppStatus sts = ippStsNoErr;
  37. unsigned int byte_size = sizeof(OctStr256);
  38. unsigned int word_size =
  39. (unsigned int)((byte_size + sizeof(Ipp32u) - 1) / sizeof(Ipp32u));
  40. int bignum_ctx_size = 0;
  41. if (!bn || !str) {
  42. return kEpidBadArgErr;
  43. }
  44. sts = ippsBigNumGetSize(word_size, &bignum_ctx_size);
  45. if (ippStsNoErr != sts) {
  46. if (ippStsLengthErr == sts) {
  47. result = kEpidBadArgErr;
  48. } else {
  49. result = kEpidMathErr;
  50. }
  51. break;
  52. }
  53. // Allocate space for ipp bignum context
  54. ipp_bn_ctx = (IppsBigNumState*)SAFE_ALLOC(bignum_ctx_size);
  55. if (!ipp_bn_ctx) {
  56. result = kEpidMemAllocErr;
  57. break;
  58. }
  59. // Initialize ipp bignum context
  60. sts = ippsBigNumInit(word_size, ipp_bn_ctx);
  61. if (sts != ippStsNoErr) {
  62. if (sts == ippStsLengthErr) {
  63. result = kEpidBadArgErr;
  64. } else {
  65. result = kEpidMathErr;
  66. }
  67. break;
  68. }
  69. sts = ippsSetOctString_BN((Ipp8u*)str, byte_size, ipp_bn_ctx);
  70. if (sts != ippStsNoErr) {
  71. if (sts == ippStsLengthErr) {
  72. result = kEpidBadArgErr;
  73. } else {
  74. result = kEpidMathErr;
  75. }
  76. break;
  77. }
  78. *bn = ipp_bn_ctx;
  79. result = kEpidNoErr;
  80. } while (0);
  81. if (result != kEpidNoErr) {
  82. SAFE_FREE(ipp_bn_ctx);
  83. }
  84. return result;
  85. }
  86. EpidStatus Cmp_OctStr256(const OctStr256* pA, const OctStr256* pB,
  87. unsigned int* pResult) {
  88. EpidStatus result = kEpidErr;
  89. IppsBigNumState* ipp_a_ctx = nullptr;
  90. IppsBigNumState* ipp_b_ctx = nullptr;
  91. do {
  92. IppStatus sts = ippStsNoErr;
  93. if (!pA || !pB || !pResult) {
  94. return kEpidBadArgErr;
  95. }
  96. result = create_BigNum(&ipp_a_ctx, pA);
  97. if (kEpidNoErr != result) {
  98. break;
  99. }
  100. result = create_BigNum(&ipp_b_ctx, pB);
  101. if (kEpidNoErr != result) {
  102. break;
  103. }
  104. sts = ippsCmp_BN(ipp_a_ctx, ipp_b_ctx, pResult);
  105. if (ippStsNoErr != sts) {
  106. if (ippStsContextMatchErr == sts || ippStsRangeErr == sts ||
  107. ippStsLengthErr == sts || ippStsOutOfRangeErr == sts) {
  108. result = kEpidBadArgErr;
  109. } else {
  110. result = kEpidMathErr;
  111. }
  112. }
  113. } while (0);
  114. delete_BigNum(&ipp_a_ctx);
  115. delete_BigNum(&ipp_b_ctx);
  116. return result;
  117. }