esp8266ndn
NDN Arduino library for ESP8266 and more
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
digest.hpp
Go to the documentation of this file.
1 #ifndef NDNPH_KEYCHAIN_DIGEST_HPP
2 #define NDNPH_KEYCHAIN_DIGEST_HPP
3 
4 #include "../port/timingsafe/port.hpp"
5 #include "helper.hpp"
6 #include "private-key.hpp"
7 #include "public-key.hpp"
8 
9 namespace ndnph {
10 
12 class DigestKey
13  : public PrivateKey
14  , public PublicKey {
15 public:
16  static const DigestKey& get() {
17  static DigestKey instance;
18  return instance;
19  }
20 
21  size_t getMaxSigLen() const final {
22  return NDNPH_SHA256_LEN;
23  }
24 
25  void updateSigInfo(SigInfo& sigInfo) const final {
26  sigInfo.sigType = SigType::Sha256;
27  sigInfo.name = Name();
28  }
29 
30  ssize_t sign(std::initializer_list<tlv::Value> chunks, uint8_t* sig) const final {
31  bool ok = detail::computeDigest(chunks, sig);
32  return ok ? NDNPH_SHA256_LEN : -1;
33  }
34 
35  bool matchSigInfo(const SigInfo& sigInfo) const final {
36  return sigInfo.sigType == SigType::Sha256;
37  }
38 
39  bool verify(std::initializer_list<tlv::Value> chunks, const uint8_t* sig,
40  size_t sigLen) const final {
41  uint8_t digest[NDNPH_SHA256_LEN];
42  return detail::computeDigest(chunks, digest) &&
43  port::TimingSafeEqual()(digest, NDNPH_SHA256_LEN, sig, sigLen);
44  }
45 };
46 
47 } // namespace ndnph
48 
49 #endif // NDNPH_KEYCHAIN_DIGEST_HPP
DigestSha256 signing and verification.
Definition: digest.hpp:14
bool matchSigInfo(const SigInfo &sigInfo) const final
Determine whether packet was signed by corresponding private key.
Definition: digest.hpp:35
static const DigestKey & get()
Definition: digest.hpp:16
ssize_t sign(std::initializer_list< tlv::Value > chunks, uint8_t *sig) const final
Perform signing.
Definition: digest.hpp:30
void updateSigInfo(SigInfo &sigInfo) const final
Write SigType and KeyLocator.
Definition: digest.hpp:25
bool verify(std::initializer_list< tlv::Value > chunks, const uint8_t *sig, size_t sigLen) const final
Perform verification.
Definition: digest.hpp:39
size_t getMaxSigLen() const final
Definition: digest.hpp:21
Private key.
Definition: private-key.hpp:9
Public key.
Definition: public-key.hpp:9
SignatureInfo.
Definition: sig-info.hpp:12
#define NDNPH_SHA256_LEN
SHA256 digest length.
Definition: common.hpp:34
@ Sha256
Definition: an.hpp:77
@ Name
Definition: an.hpp:19
bool computeDigest(std::initializer_list< tlv::Value > chunks, uint8_t digest[NDNPH_SHA256_LEN])
Definition: helper.hpp:15
Definition: fs.hpp:33