crypto_hmac.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <dns/buffer.h>
  15. #include <boost/noncopyable.hpp>
  16. #include <cryptolink/cryptolink.h>
  17. #ifndef _ISC_CRYPTO_HMAC_H
  18. #define _ISC_CRYPTO_HMAC_H
  19. namespace isc {
  20. namespace cryptolink {
  21. /// Forward declaration, pimpl style
  22. class HMACImpl;
  23. /// \brief HMAC support
  24. ///
  25. /// This class is used to create and verify HMAC signatures. Instances
  26. /// can be created with CryptoLink::createHMAC()
  27. ///
  28. class HMAC : private boost::noncopyable {
  29. private:
  30. /// \brief Constructor from a secret and a hash algorithm
  31. ///
  32. /// \exception UnsupportedAlgorithmException if the given algorithm
  33. /// is unknown or not supported by the underlying library
  34. /// \exception InvalidKeyLength if the given key secret_len is bad
  35. /// \exception LibraryError if there was any unexpected exception
  36. /// in the underlying library
  37. ///
  38. /// Notes: if the secret is longer than the block size of its
  39. /// algorithm, the constructor will run it through the hash
  40. /// algorithm, and use the digest as the secret for this HMAC
  41. /// operation
  42. ///
  43. /// \param secret The secret to sign with
  44. /// \param len The length of the secret
  45. /// \param hash_algorithm The hash algorithm
  46. HMAC(const void* secret, size_t secret_len,
  47. const HashAlgorithm hash_algorithm);
  48. friend HMAC* CryptoLink::createHMAC(const void*, size_t,
  49. const HashAlgorithm);
  50. public:
  51. /// \brief Destructor
  52. ~HMAC();
  53. /// \brief Returns the output size of the digest
  54. ///
  55. /// \return output size of the digest
  56. size_t getOutputLength() const;
  57. /// \brief Add data to digest
  58. ///
  59. /// \exception LibraryError if there was any unexpected exception
  60. /// in the underlying library
  61. ///
  62. /// \param data The data to add
  63. /// \param len The size of the data
  64. void update(const void* data, const size_t len);
  65. /// \brief Calculate the final signature
  66. ///
  67. /// The result will be appended to the given outputbuffer
  68. ///
  69. /// \exception LibraryError if there was any unexpected exception
  70. /// in the underlying library
  71. ///
  72. /// \param result The OutputBuffer to append the result to
  73. /// \param len The number of bytes from the result to copy. If this
  74. /// value is smaller than the algorithms output size, the
  75. /// result will be truncated. If this value is larger, or 0
  76. /// (the default), it will be ignored
  77. void sign(isc::dns::OutputBuffer& result, size_t len = 0);
  78. /// \brief Calculate the final signature
  79. ///
  80. /// len bytes of data from the result will be copied to *result
  81. /// If len is larger than the output size, only output_size bytes
  82. /// will be copied. If it is smaller, the output will be truncated
  83. ///
  84. /// \exception LibraryError if there was any unexpected exception
  85. /// in the underlying library
  86. ///
  87. /// At least len bytes of data must be available for writing at
  88. /// result
  89. void sign(void* result, size_t len);
  90. /// \brief Calculate the final signatre
  91. ///
  92. /// The result will be returned as a std::vector<uint8_t>
  93. ///
  94. /// \exception LibraryError if there was any unexpected exception
  95. /// in the underlying library
  96. ///
  97. /// \param len The number of bytes from the result to copy. If this
  98. /// value is smaller than the algorithms output size, the
  99. /// result will be truncated. If this value is larger, or 0
  100. /// (the default), it will be ignored
  101. /// \return a vector containing the signature
  102. std::vector<uint8_t> sign(size_t len = 0);
  103. /// \brief Verify an existing signature
  104. ///
  105. /// \exception LibraryError if there was any unexpected exception
  106. /// in the underlying library
  107. ///
  108. /// \param sig The signature to verify
  109. /// \param len The length of the signature. If this is non-zero,
  110. /// and smaller than the output length of the algorithm,
  111. /// only len bytes will be checked
  112. /// \return true if the signature is correct, false otherwise
  113. bool verify(const void* sig, size_t len);
  114. private:
  115. HMACImpl* impl_;
  116. };
  117. /// \brief Create an HMAC signature for the given data
  118. ///
  119. /// This is a convenience function that calculates the hmac signature,
  120. /// given a fixed amount of data. Internally it does the same as
  121. /// creating an HMAC object, feeding it the data, and calculating the
  122. /// resulting signature.
  123. ///
  124. /// \exception UnsupportedAlgorithm if the given algorithm is unknown
  125. /// or not supported by the underlying library
  126. /// \exception BadKey if the given key secret_len is bad
  127. /// \exception LibraryError if there was any unexpected exception
  128. /// in the underlying library
  129. ///
  130. /// Notes: if the secret is longer than the block size of its
  131. /// algorithm, the constructor will run it through the hash
  132. /// algorithm, and use the digest as the secret for this HMAC
  133. /// operation
  134. ///
  135. /// \param data The data to sign
  136. /// \param data_len The length of the data
  137. /// \param secret The secret to sign with
  138. /// \param secret_len The length of the secret
  139. /// \param hash_algorithm The hash algorithm
  140. /// \param result The signature will be appended to this buffer
  141. /// \param len If this is non-zero and less than the output size,
  142. /// the result will be truncated to len bytes
  143. void signHMAC(const void* data,
  144. const size_t data_len,
  145. const void* secret,
  146. size_t secret_len,
  147. const HashAlgorithm hash_algorithm,
  148. isc::dns::OutputBuffer& result,
  149. size_t len = 0);
  150. /// \brief Verify an HMAC signature for the given data
  151. ///
  152. /// This is a convenience function that verifies an hmac signature,
  153. /// given a fixed amount of data. Internally it does the same as
  154. /// creating an HMAC object, feeding it the data, and checking the
  155. /// resulting signature.
  156. ///
  157. /// \exception UnsupportedAlgorithm if the given algorithm is unknown
  158. /// or not supported by the underlying library
  159. /// \exception BadKey if the given key secret_len is bad
  160. /// \exception LibraryError if there was any unexpected exception
  161. /// in the underlying library
  162. ///
  163. /// Notes: if the secret is longer than the block size of its
  164. /// algorithm, the constructor will run it through the hash
  165. /// algorithm, and use the digest as the secret for this HMAC
  166. /// operation
  167. ///
  168. /// \param data The data to verify
  169. /// \param data_len The length of the data
  170. /// \param secret The secret to sign with
  171. /// \param secret_len The length of the secret
  172. /// \param hash_algorithm The hash algorithm
  173. /// \param sig The signature to verify
  174. /// \param sig_len The length of the signature
  175. /// \return True if the signature verifies, false if not
  176. bool verifyHMAC(const void* data,
  177. const size_t data_len,
  178. const void* secret,
  179. size_t secret_len,
  180. const HashAlgorithm hash_algorithm,
  181. const void* sig,
  182. const size_t sig_len);
  183. /// \brief Delete an HMAC object
  184. void deleteHMAC(HMAC* hmac);
  185. } // namespace cryptolink
  186. } // namespace isc
  187. #endif // __ISC_CRYPTO_HMAC