prepare_hmac_sha256.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (C) 2011-2017 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. break;
  55. m_ippstatus = ippsHMAC_Init(key, (int)keyLength, m_pCtx, IPP_ALG_HASH_SHA256);
  56. if (m_ippstatus != ippStsNoErr)
  57. {
  58. free(m_pCtx);
  59. m_pCtx = NULL;
  60. break;
  61. }
  62. } while (0);
  63. }
  64. PrepareHMACSHA256::~PrepareHMACSHA256(void)
  65. {
  66. if (m_pCtx) {
  67. free(m_pCtx);
  68. m_pCtx = NULL;
  69. }
  70. }
  71. ae_error_t PrepareHMACSHA256::Update(const void* pData, size_t numBytes)
  72. {
  73. do
  74. {
  75. if (m_ippstatus != ippStsNoErr)
  76. break;
  77. if (NULL == pData || numBytes < 1 || NULL == m_pCtx || numBytes > INT32_MAX)
  78. {
  79. m_ippstatus = ippStsBadArgErr;
  80. break;
  81. }
  82. m_ippstatus = ippsHMAC_Update((const Ipp8u*)pData, (int)numBytes, m_pCtx);
  83. if (m_ippstatus != ippStsNoErr)
  84. break;
  85. } while (0);
  86. ae_error_t ae_status = AE_SUCCESS;
  87. if (m_ippstatus != ippStsNoErr)
  88. {
  89. if (ippStsNoMemErr == m_ippstatus)
  90. ae_status = PSE_PR_INSUFFICIENT_MEMORY_ERROR;
  91. else
  92. ae_status = PSE_PR_HMAC_CALC_ERROR;
  93. }
  94. return ae_status;
  95. }
  96. // pCMAC will contain the computed CMAC if SDS_SUCCESS
  97. ae_error_t PrepareHMACSHA256::Finalize(SIGMA_HMAC *pHMAC)
  98. {
  99. do
  100. {
  101. if (m_ippstatus != ippStsNoErr)
  102. break;
  103. if (NULL == m_pCtx || NULL == pHMAC)
  104. {
  105. m_ippstatus = ippStsBadArgErr;
  106. break;
  107. }
  108. m_ippstatus = ippsHMAC_Final((Ipp8u*)pHMAC, sizeof(SIGMA_HMAC), m_pCtx);
  109. if (m_ippstatus != ippStsNoErr)
  110. break;
  111. } while (0);
  112. ae_error_t ae_status = AE_SUCCESS;
  113. if (m_ippstatus != ippStsNoErr)
  114. {
  115. if (ippStsNoMemErr == m_ippstatus)
  116. ae_status = PSE_PR_INSUFFICIENT_MEMORY_ERROR;
  117. else
  118. ae_status = PSE_PR_HMAC_CALC_ERROR;
  119. }
  120. return ae_status;
  121. }