ndn-embeds 0.1.0
Lightweight NDN protocol stack for embedded systems
Loading...
Searching...
No Matches
crypto.hpp File Reference

NDN cryptographic utilities. More...

#include "ndn/common.hpp"

Go to the source code of this file.

Functions

Error ndn::crypto::sha256 (const uint8_t *data, size_t len, uint8_t *out)
 Compute SHA-256 hash.
 
Error ndn::crypto::hmacSha256 (const uint8_t *key, size_t keyLen, const uint8_t *data, size_t dataLen, uint8_t *out)
 Compute HMAC-SHA256.
 
bool ndn::crypto::constantTimeCompare (const uint8_t *lhs, const uint8_t *rhs, size_t len)
 Compare two buffers in constant time.
 
Error ndn::crypto::ecdsaP256GenerateKeyPair (uint8_t *privKey, uint8_t *pubKey)
 Generate an ECDSA P-256 key pair.
 
Error ndn::crypto::ecdsaP256Sign (const uint8_t *privKey, const uint8_t *data, size_t dataLen, uint8_t *sig, size_t *sigLen)
 Sign with ECDSA P-256.
 
bool ndn::crypto::ecdsaP256Verify (const uint8_t *pubKey, const uint8_t *data, size_t dataLen, const uint8_t *sig, size_t sigLen)
 Verify an ECDSA P-256 signature.
 

Detailed Description

NDN cryptographic utilities.

Provides utility functions for SHA-256 hash and HMAC-SHA256 computation. Uses the mbedtls library from ESP-IDF.

Definition in file crypto.hpp.

Function Documentation

◆ constantTimeCompare()

bool ndn::crypto::constantTimeCompare ( const uint8_t *  lhs,
const uint8_t *  rhs,
size_t  len 
)

Compare two buffers in constant time.

Always compares all bytes to prevent timing attacks.

Parameters
lhsFirst buffer
rhsSecond buffer
lenNumber of bytes to compare
Returns
true if buffers match

Definition at line 60 of file crypto.cpp.

References ndn::crypto::constantTimeCompare().

Referenced by ndn::crypto::constantTimeCompare().

◆ ecdsaP256GenerateKeyPair()

Error ndn::crypto::ecdsaP256GenerateKeyPair ( uint8_t *  privKey,
uint8_t *  pubKey 
)

Generate an ECDSA P-256 key pair.

Parameters
privKeyPrivate key output buffer (32 bytes)
pubKeyPublic key output buffer (65 bytes, uncompressed form 0x04 || X || Y)
Returns
Error::Success on success

Definition at line 72 of file crypto.cpp.

References ndn::ECDSA_P256_PRIVKEY_SIZE, ndn::ECDSA_P256_PUBKEY_SIZE, and ndn::crypto::ecdsaP256GenerateKeyPair().

Referenced by ndn::crypto::ecdsaP256GenerateKeyPair().

◆ ecdsaP256Sign()

Error ndn::crypto::ecdsaP256Sign ( const uint8_t *  privKey,
const uint8_t *  data,
size_t  dataLen,
uint8_t *  sig,
size_t *  sigLen 
)

Sign with ECDSA P-256.

Computes a SHA-256 hash and generates an ECDSA signature. The signature is encoded in DER format.

Parameters
privKeyPrivate key (32 bytes)
dataData to sign
dataLenData length
sigSignature output buffer (max 72 bytes)
sigLenStores the actual signature size
Returns
Error::Success on success

Definition at line 126 of file crypto.cpp.

References ndn::ECDSA_P256_PRIVKEY_SIZE, ndn::ECDSA_P256_SIG_MAX_SIZE, ndn::crypto::ecdsaP256Sign(), ndn::crypto::sha256(), and ndn::SHA256_DIGEST_SIZE.

Referenced by ndn::crypto::ecdsaP256Sign().

◆ ecdsaP256Verify()

bool ndn::crypto::ecdsaP256Verify ( const uint8_t *  pubKey,
const uint8_t *  data,
size_t  dataLen,
const uint8_t *  sig,
size_t  sigLen 
)

Verify an ECDSA P-256 signature.

Parameters
pubKeyPublic key (65 bytes, uncompressed form)
dataData that was signed
dataLenData length
sigSignature (DER format)
sigLenSignature length
Returns
true if the signature is valid

Definition at line 181 of file crypto.cpp.

References ndn::ECDSA_P256_PUBKEY_SIZE, ndn::crypto::ecdsaP256Verify(), ndn::crypto::sha256(), and ndn::SHA256_DIGEST_SIZE.

Referenced by ndn::crypto::ecdsaP256Verify().

◆ hmacSha256()

Error ndn::crypto::hmacSha256 ( const uint8_t *  key,
size_t  keyLen,
const uint8_t *  data,
size_t  dataLen,
uint8_t *  out 
)

Compute HMAC-SHA256.

Parameters
keyPointer to key data
keyLenKey length (bytes)
dataPointer to input data
dataLenInput data length (bytes)
outOutput buffer (must be at least 32 bytes)
Returns
Error::Success on success

Definition at line 45 of file crypto.cpp.

References ndn::crypto::hmacSha256().

Referenced by ndn::crypto::hmacSha256().

◆ sha256()

Error ndn::crypto::sha256 ( const uint8_t *  data,
size_t  len,
uint8_t *  out 
)

Compute SHA-256 hash.

Parameters
dataPointer to input data
lenInput data length (bytes)
outOutput buffer (must be at least 32 bytes)
Returns
Error::Success on success

Definition at line 19 of file crypto.cpp.

References ndn::crypto::sha256().

Referenced by ndn::crypto::ecdsaP256Sign(), ndn::crypto::ecdsaP256Verify(), and ndn::crypto::sha256().