prepare_hmac_sha256.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 "prepare_hmac_sha256.h"
  32. #include <stdlib.h>
  33. PrepareHMACSHA256::PrepareHMACSHA256(const Ipp8u* key, size_t keyLength)
  34. : m_ippstatus(ippStsNoErr), m_pCtx(0)
  35. {
  36. int size;
  37. do
  38. {
  39. if (NULL == key || keyLength < 1)
  40. {
  41. m_ippstatus = ippStsBadArgErr;
  42. break;
  43. }
  44. m_ippstatus = ippsHMAC_GetSize(&size);
  45. if (m_ippstatus != ippStsNoErr)
  46. break;
  47. m_pCtx = (IppsHMACState*) malloc(size);
  48. if (NULL == m_pCtx)
  49. {
  50. m_ippstatus = ippStsNoMemErr;
  51. break;
  52. }
  53. if (keyLength > INT32_MAX)
  54. {
  55. free(m_pCtx);
  56. m_pCtx = NULL;
  57. break;
  58. }
  59. m_ippstatus = ippsHMAC_Init(key, (int)keyLength, m_pCtx, IPP_ALG_HASH_SHA256);
  60. if (m_ippstatus != ippStsNoErr)
  61. {
  62. free(m_pCtx);
  63. m_pCtx = NULL;
  64. break;
  65. }
  66. } while (0);
  67. }
  68. PrepareHMACSHA256::~PrepareHMACSHA256(void)
  69. {
  70. if (m_pCtx) {
  71. free(m_pCtx);
  72. m_pCtx = NULL;
  73. }
  74. }
  75. ae_error_t PrepareHMACSHA256::Update(const void* pData, size_t numBytes)
  76. {
  77. do
  78. {
  79. if (m_ippstatus != ippStsNoErr)
  80. break;
  81. if (NULL == pData || numBytes < 1 || NULL == m_pCtx || numBytes > INT32_MAX)
  82. {
  83. m_ippstatus = ippStsBadArgErr;
  84. break;
  85. }
  86. m_ippstatus = ippsHMAC_Update((const Ipp8u*)pData, (int)numBytes, m_pCtx);
  87. if (m_ippstatus != ippStsNoErr)
  88. break;
  89. } while (0);
  90. ae_error_t ae_status = AE_SUCCESS;
  91. if (m_ippstatus != ippStsNoErr)
  92. {
  93. if (ippStsNoMemErr == m_ippstatus)
  94. ae_status = PSE_PR_INSUFFICIENT_MEMORY_ERROR;
  95. else
  96. ae_status = PSE_PR_HMAC_CALC_ERROR;
  97. }
  98. return ae_status;
  99. }
  100. // pCMAC will contain the computed CMAC if SDS_SUCCESS
  101. ae_error_t PrepareHMACSHA256::Finalize(SIGMA_HMAC *pHMAC)
  102. {
  103. do
  104. {
  105. if (m_ippstatus != ippStsNoErr)
  106. break;
  107. if (NULL == m_pCtx || NULL == pHMAC)
  108. {
  109. m_ippstatus = ippStsBadArgErr;
  110. break;
  111. }
  112. m_ippstatus = ippsHMAC_Final((Ipp8u*)pHMAC, sizeof(SIGMA_HMAC), m_pCtx);
  113. if (m_ippstatus != ippStsNoErr)
  114. break;
  115. } while (0);
  116. ae_error_t ae_status = AE_SUCCESS;
  117. if (m_ippstatus != ippStsNoErr)
  118. {
  119. if (ippStsNoMemErr == m_ippstatus)
  120. ae_status = PSE_PR_INSUFFICIENT_MEMORY_ERROR;
  121. else
  122. ae_status = PSE_PR_HMAC_CALC_ERROR;
  123. }
  124. return ae_status;
  125. }