octstr-testhelper.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*############################################################################
  2. # Copyright 2016-2017 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 "epid/common/types.h"
  25. #include "ext/ipp/include/ippcp.h"
  26. typedef Ipp8u* IppOctStr;
  27. /// Internal function to delete BigNum
  28. void delete_BigNum(IppsBigNumState** bn) {
  29. if (*bn) {
  30. SAFE_FREE(*bn);
  31. }
  32. }
  33. /// Internal function to create BigNum from an OctStr256
  34. EpidStatus create_BigNum(IppsBigNumState** bn, const OctStr256* str) {
  35. EpidStatus result = kEpidErr;
  36. IppsBigNumState* ipp_bn_ctx = nullptr;
  37. do {
  38. IppStatus sts = ippStsNoErr;
  39. unsigned int byte_size = sizeof(OctStr256);
  40. unsigned int word_size =
  41. (unsigned int)((byte_size + sizeof(Ipp32u) - 1) / sizeof(Ipp32u));
  42. int bignum_ctx_size = 0;
  43. if (!bn || !str) {
  44. return kEpidBadArgErr;
  45. }
  46. sts = ippsBigNumGetSize(word_size, &bignum_ctx_size);
  47. if (ippStsNoErr != sts) {
  48. if (ippStsLengthErr == sts) {
  49. result = kEpidBadArgErr;
  50. } else {
  51. result = kEpidMathErr;
  52. }
  53. break;
  54. }
  55. // Allocate space for ipp bignum context
  56. ipp_bn_ctx = (IppsBigNumState*)SAFE_ALLOC(bignum_ctx_size);
  57. if (!ipp_bn_ctx) {
  58. result = kEpidMemAllocErr;
  59. break;
  60. }
  61. // Initialize ipp bignum context
  62. sts = ippsBigNumInit(word_size, ipp_bn_ctx);
  63. if (sts != ippStsNoErr) {
  64. if (sts == ippStsLengthErr) {
  65. result = kEpidBadArgErr;
  66. } else {
  67. result = kEpidMathErr;
  68. }
  69. break;
  70. }
  71. sts = ippsSetOctString_BN((IppOctStr)str, byte_size, ipp_bn_ctx);
  72. if (sts != ippStsNoErr) {
  73. if (sts == ippStsLengthErr) {
  74. result = kEpidBadArgErr;
  75. } else {
  76. result = kEpidMathErr;
  77. }
  78. break;
  79. }
  80. *bn = ipp_bn_ctx;
  81. result = kEpidNoErr;
  82. } while (0);
  83. if (result != kEpidNoErr) {
  84. SAFE_FREE(ipp_bn_ctx);
  85. }
  86. return result;
  87. }
  88. EpidStatus Cmp_OctStr256(const OctStr256* pA, const OctStr256* pB,
  89. unsigned int* pResult) {
  90. EpidStatus result = kEpidErr;
  91. IppsBigNumState* ipp_a_ctx = nullptr;
  92. IppsBigNumState* ipp_b_ctx = nullptr;
  93. do {
  94. IppStatus sts = ippStsNoErr;
  95. if (!pA || !pB || !pResult) {
  96. return kEpidBadArgErr;
  97. }
  98. result = create_BigNum(&ipp_a_ctx, pA);
  99. if (kEpidNoErr != result) {
  100. break;
  101. }
  102. result = create_BigNum(&ipp_b_ctx, pB);
  103. if (kEpidNoErr != result) {
  104. break;
  105. }
  106. sts = ippsCmp_BN(ipp_a_ctx, ipp_b_ctx, pResult);
  107. if (ippStsNoErr != sts) {
  108. if (ippStsContextMatchErr == sts || ippStsRangeErr == sts ||
  109. ippStsLengthErr == sts || ippStsOutOfRangeErr == sts) {
  110. result = kEpidBadArgErr;
  111. } else {
  112. result = kEpidMathErr;
  113. }
  114. }
  115. } while (0);
  116. delete_BigNum(&ipp_a_ctx);
  117. delete_BigNum(&ipp_b_ctx);
  118. return result;
  119. }