HMAC.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. * SPDX-License-Identifier: Apache-2.0.
  4. */
  5. #include <aws/crt/crypto/HMAC.h>
  6. #include <aws/cal/hmac.h>
  7. namespace Aws
  8. {
  9. namespace Crt
  10. {
  11. namespace Crypto
  12. {
  13. bool ComputeSHA256HMAC(
  14. Allocator *allocator,
  15. const ByteCursor &secret,
  16. const ByteCursor &input,
  17. ByteBuf &output,
  18. size_t truncateTo) noexcept
  19. {
  20. return aws_sha256_hmac_compute(allocator, &secret, &input, &output, truncateTo) == AWS_OP_SUCCESS;
  21. }
  22. bool ComputeSHA256HMAC(
  23. const ByteCursor &secret,
  24. const ByteCursor &input,
  25. ByteBuf &output,
  26. size_t truncateTo) noexcept
  27. {
  28. return aws_sha256_hmac_compute(ApiAllocator(), &secret, &input, &output, truncateTo) == AWS_OP_SUCCESS;
  29. }
  30. HMAC::HMAC(aws_hmac *hmac) noexcept : m_hmac(hmac), m_good(false), m_lastError(0)
  31. {
  32. if (hmac)
  33. {
  34. m_good = true;
  35. }
  36. else
  37. {
  38. m_lastError = aws_last_error();
  39. }
  40. }
  41. HMAC::~HMAC()
  42. {
  43. if (m_hmac)
  44. {
  45. aws_hmac_destroy(m_hmac);
  46. m_hmac = nullptr;
  47. }
  48. }
  49. HMAC::HMAC(HMAC &&toMove) : m_hmac(toMove.m_hmac), m_good(toMove.m_good), m_lastError(toMove.m_lastError)
  50. {
  51. toMove.m_hmac = nullptr;
  52. toMove.m_good = false;
  53. }
  54. HMAC &HMAC::operator=(HMAC &&toMove)
  55. {
  56. if (&toMove != this)
  57. {
  58. *this = HMAC(std::move(toMove));
  59. }
  60. return *this;
  61. }
  62. HMAC HMAC::CreateSHA256HMAC(Allocator *allocator, const ByteCursor &secret) noexcept
  63. {
  64. return HMAC(aws_sha256_hmac_new(allocator, &secret));
  65. }
  66. HMAC HMAC::CreateSHA256HMAC(const ByteCursor &secret) noexcept
  67. {
  68. return HMAC(aws_sha256_hmac_new(ApiAllocator(), &secret));
  69. }
  70. bool HMAC::Update(const ByteCursor &toHMAC) noexcept
  71. {
  72. if (*this)
  73. {
  74. if (aws_hmac_update(m_hmac, &toHMAC))
  75. {
  76. m_lastError = aws_last_error();
  77. m_good = false;
  78. return false;
  79. }
  80. return true;
  81. }
  82. return false;
  83. }
  84. bool HMAC::Digest(ByteBuf &output, size_t truncateTo) noexcept
  85. {
  86. if (*this)
  87. {
  88. m_good = false;
  89. if (aws_hmac_finalize(m_hmac, &output, truncateTo))
  90. {
  91. m_lastError = aws_last_error();
  92. return false;
  93. }
  94. return true;
  95. }
  96. return false;
  97. }
  98. aws_hmac_vtable ByoHMAC::s_Vtable = {
  99. "aws-crt-cpp-byo-crypto-hmac",
  100. "aws-crt-cpp-byo-crypto",
  101. ByoHMAC::s_Destroy,
  102. ByoHMAC::s_Update,
  103. ByoHMAC::s_Finalize,
  104. };
  105. ByoHMAC::ByoHMAC(size_t digestSize, const ByteCursor &, Allocator *allocator)
  106. {
  107. AWS_ZERO_STRUCT(m_hmacValue);
  108. m_hmacValue.impl = reinterpret_cast<void *>(this);
  109. m_hmacValue.digest_size = digestSize;
  110. m_hmacValue.allocator = allocator;
  111. m_hmacValue.good = true;
  112. m_hmacValue.vtable = &s_Vtable;
  113. }
  114. aws_hmac *ByoHMAC::SeatForCInterop(const std::shared_ptr<ByoHMAC> &selfRef)
  115. {
  116. AWS_FATAL_ASSERT(this == selfRef.get());
  117. m_selfReference = selfRef;
  118. return &m_hmacValue;
  119. }
  120. void ByoHMAC::s_Destroy(struct aws_hmac *hmac)
  121. {
  122. auto *byoHash = reinterpret_cast<ByoHMAC *>(hmac->impl);
  123. byoHash->m_selfReference = nullptr;
  124. }
  125. int ByoHMAC::s_Update(struct aws_hmac *hmac, const struct aws_byte_cursor *buf)
  126. {
  127. auto *byoHmac = reinterpret_cast<ByoHMAC *>(hmac->impl);
  128. if (!byoHmac->m_hmacValue.good)
  129. {
  130. return aws_raise_error(AWS_ERROR_INVALID_STATE);
  131. }
  132. if (!byoHmac->UpdateInternal(*buf))
  133. {
  134. byoHmac->m_hmacValue.good = false;
  135. return AWS_OP_ERR;
  136. }
  137. return AWS_OP_SUCCESS;
  138. }
  139. int ByoHMAC::s_Finalize(struct aws_hmac *hmac, struct aws_byte_buf *out)
  140. {
  141. auto *byoHmac = reinterpret_cast<ByoHMAC *>(hmac->impl);
  142. if (!byoHmac->m_hmacValue.good)
  143. {
  144. return aws_raise_error(AWS_ERROR_INVALID_STATE);
  145. }
  146. bool success = byoHmac->DigestInternal(*out);
  147. byoHmac->m_hmacValue.good = false;
  148. return success ? AWS_OP_SUCCESS : AWS_OP_ERR;
  149. }
  150. } // namespace Crypto
  151. } // namespace Crt
  152. } // namespace Aws