17 #include <boost/noncopyable.hpp>
18 #include <boost/scoped_ptr.hpp>
53 class NSEC3HashRFC5155 : boost::noncopyable,
public NSEC3Hash {
56 static const uint8_t NSEC3_HASH_SHA1 = 1;
58 static const size_t DEFAULT_DIGEST_LENGTH = 32;
61 NSEC3HashRFC5155(uint8_t algorithm, uint16_t iterations,
62 const uint8_t* salt_data,
size_t salt_length) :
63 algorithm_(algorithm), iterations_(iterations),
64 salt_data_(NULL), salt_length_(salt_length),
65 digest_(DEFAULT_DIGEST_LENGTH), obuf_(
Name::MAX_WIRE)
67 if (algorithm_ != NSEC3_HASH_SHA1) {
69 static_cast<unsigned int>(algorithm_));
72 if (salt_length > 0) {
73 salt_data_ =
static_cast<uint8_t*
>(std::malloc(salt_length));
74 if (salt_data_ == NULL) {
75 throw std::bad_alloc();
77 std::memcpy(salt_data_, salt_data, salt_length);
81 virtual ~NSEC3HashRFC5155() {
82 std::free(salt_data_);
85 virtual std::string calculate(
const Name& name)
const;
90 bool match(uint8_t algorithm, uint16_t iterations,
91 const vector<uint8_t>& salt)
const;
94 std::string calculateForWiredata(
const uint8_t* data,
size_t length)
const;
96 const uint8_t algorithm_;
97 const uint16_t iterations_;
99 const size_t salt_length_;
105 mutable vector<uint8_t> vdigest_;
110 iterateSHA1(
const uint8_t* input,
size_t inlength,
111 const uint8_t* salt,
size_t saltlen,
114 boost::scoped_ptr<Hash> hash(CryptoLink::getCryptoLink().createHash(
SHA1));
115 hash->update(input, inlength);
116 hash->update(salt, saltlen);
117 hash->final(output, hash->getOutputLength());
121 NSEC3HashRFC5155::calculateForWiredata(
const uint8_t* data,
127 uint8_t name_buf[256];
128 assert(length <
sizeof (name_buf));
130 const uint8_t *p1 = data;
131 uint8_t *p2 = name_buf;
144 iterateSHA1(name_buf, length,
145 salt_data_, salt_length_, digest_);
146 const uint8_t* dgst_data =
static_cast<const uint8_t*
>(digest_.getData());
147 size_t dgst_len = digest_.getLength();
148 for (
unsigned int n = 0; n < iterations_; ++n) {
150 iterateSHA1(dgst_data, dgst_len, salt_data_, salt_length_, digest_);
153 vdigest_.resize(dgst_len);
154 std::memcpy(&vdigest_[0], dgst_data, dgst_len);
159 NSEC3HashRFC5155::calculate(
const Name& name)
const {
163 return (calculateForWiredata(static_cast<const uint8_t*>(obuf_.getData()),
172 const uint8_t* data = ls.
getData(&length);
174 return (calculateForWiredata(data, length));
178 NSEC3HashRFC5155::match(uint8_t algorithm, uint16_t iterations,
179 const vector<uint8_t>& salt)
const
181 return (algorithm_ == algorithm && iterations_ == iterations &&
182 salt_length_ == salt.size() &&
183 ((salt_length_ == 0) ||
184 memcmp(salt_data_, &salt[0], salt_length_) == 0));
207 getNSEC3HashCreator() {
209 if (creator == NULL) {
210 creator = &default_creator;
222 return (getNSEC3HashCreator()->create(param));
227 return (getNSEC3HashCreator()->create(nsec3));
231 NSEC3Hash::create(uint8_t algorithm, uint16_t iterations,
232 const uint8_t* salt_data,
size_t salt_length) {
233 return (getNSEC3HashCreator()->create(algorithm, iterations,
234 salt_data, salt_length));
239 const vector<uint8_t>& salt = param.
getSalt();
241 salt.empty() ? NULL : &salt[0],
247 const vector<uint8_t>& salt = nsec3.
getSalt();
249 salt.empty() ? NULL : &salt[0],
254 DefaultNSEC3HashCreator::create(uint8_t algorithm, uint16_t iterations,
255 const uint8_t* salt_data,
256 size_t salt_length)
const
258 return (
new NSEC3HashRFC5155(algorithm, iterations,
259 salt_data, salt_length));
264 creator = new_creator;
The Name class encapsulates DNS names.
void setNSEC3HashCreator(const NSEC3HashCreator *new_creator)
The registrar of NSEC3HashCreator.
A calculator of NSEC3 hashes.
uint16_t getIterations() const
An exception that is thrown for when an NSEC3Hash object is constructed with an unknown hash algorith...
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
uint8_t getHashalg() const
Specialized methods.
Factory class of NSEC3Hash.
uint16_t getIterations() const
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Defines the logger used by the top-level component of kea-dhcp-ddns.
const uint8_t * getData(size_t *len) const
Return the wire-format data for this LabelSequence.
bool isAbsolute() const
Checks whether the label sequence is absolute.
The default NSEC3Hash creator.
std::string encodeBase32Hex(const std::vector< uint8_t > &binary)
Encode binary data in the base32hex format.
uint8_t getHashalg() const
void toWire(AbstractMessageRenderer &renderer) const
Render the Name in the wire format with compression.
const std::vector< uint8_t > & getSalt() const
Light-weight Accessor to Name data.
const std::vector< uint8_t > & getSalt() const
const uint8_t maptolower[]