nsec3hash.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright (C) 2012 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 <stdint.h>
  15. #include <cassert>
  16. #include <string>
  17. #include <vector>
  18. #include <boost/noncopyable.hpp>
  19. #include <exceptions/exceptions.h>
  20. #include <util/buffer.h>
  21. #include <util/encode/base32hex.h>
  22. #include <util/hash/sha1.h>
  23. #include <dns/name.h>
  24. #include <dns/nsec3hash.h>
  25. #include <dns/rdataclass.h>
  26. using namespace std;
  27. using namespace isc::util;
  28. using namespace isc::util::encode;
  29. using namespace isc::util::hash;
  30. using namespace isc::dns;
  31. using namespace isc::dns::rdata;
  32. namespace {
  33. /// \brief A derived class of \c NSEC3Hash that implements the standard hash
  34. /// calculation specified in RFC5155.
  35. ///
  36. /// Currently the only pre-defined algorithm in the RFC is SHA1. So we don't
  37. /// over-generalize it at the moment, and rather hardocde it and assume that
  38. /// specific algorithm.
  39. ///
  40. /// The implementation details are only open within this file, but to avoid
  41. /// an accidental error in this implementation we explicitly make it non
  42. /// copyable.
  43. class NSEC3HashRFC5155 : boost::noncopyable, public NSEC3Hash {
  44. private:
  45. // This is the algorithm number for SHA1/NSEC3 as defined in RFC5155.
  46. static const uint8_t NSEC3_HASH_SHA1 = 1;
  47. public:
  48. NSEC3HashRFC5155(const generic::NSEC3PARAM& param) :
  49. algorithm_(param.getHashalg()),
  50. iterations_(param.getIterations()),
  51. salt_(param.getSalt()), digest_(SHA1_HASHSIZE), obuf_(Name::MAX_WIRE)
  52. {
  53. if (algorithm_ != NSEC3_HASH_SHA1) {
  54. isc_throw(UnknownNSEC3HashAlgorithm, "Unknown NSEC3 algorithm: " <<
  55. static_cast<unsigned int>(algorithm_));
  56. }
  57. SHA1Reset(&sha1_ctx_);
  58. }
  59. virtual std::string calculate(const Name& name) const;
  60. private:
  61. const uint8_t algorithm_;
  62. const uint16_t iterations_;
  63. const vector<uint8_t> salt_;
  64. // The following members are placeholder of work place and don't hold
  65. // any state over multiple calls so can be mutable without breaking
  66. // constness.
  67. mutable SHA1Context sha1_ctx_;
  68. mutable vector<uint8_t> digest_;
  69. mutable OutputBuffer obuf_;
  70. };
  71. inline void
  72. iterateSHA1(SHA1Context* ctx, const uint8_t* input, size_t inlength,
  73. const uint8_t* salt, size_t saltlen,
  74. uint8_t output[SHA1_HASHSIZE])
  75. {
  76. SHA1Reset(ctx);
  77. SHA1Input(ctx, input, inlength);
  78. SHA1Input(ctx, salt, saltlen); // this works whether saltlen == or > 0
  79. SHA1Result(ctx, output);
  80. }
  81. string
  82. NSEC3HashRFC5155::calculate(const Name& name) const {
  83. // We first need to normalize the name by converting all upper case
  84. // characters in the labels to lower ones.
  85. obuf_.clear();
  86. Name name_copy(name);
  87. name_copy.downcase();
  88. name_copy.toWire(obuf_);
  89. const uint8_t saltlen = salt_.size();
  90. const uint8_t* const salt = (saltlen > 0) ? &salt_[0] : NULL;
  91. uint8_t* const digest = &digest_[0];
  92. assert(digest_.size() == SHA1_HASHSIZE);
  93. iterateSHA1(&sha1_ctx_, static_cast<const uint8_t*>(obuf_.getData()),
  94. obuf_.getLength(), salt, saltlen, digest);
  95. for (unsigned int n = 0; n < iterations_; ++n) {
  96. iterateSHA1(&sha1_ctx_, digest, SHA1_HASHSIZE, salt, saltlen, digest);
  97. }
  98. return (encodeBase32Hex(digest_));
  99. }
  100. } // end of unnamed namespace
  101. namespace isc {
  102. namespace dns {
  103. NSEC3Hash*
  104. NSEC3Hash::create(const generic::NSEC3PARAM& param) {
  105. return (new NSEC3HashRFC5155(param));
  106. }
  107. } // namespace dns
  108. } // namespace isc