Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
DIGEST: message digests and HMAC

Macros

#define N_AEAD_AES256GCM_KEY_LEN   32
 key size in bytes for AES-256-GCM
 
#define N_AEAD_AES256GCM_TAG_LEN   16
 authentication tag size in bytes for AES-256-GCM
 
#define N_DIGEST_MD5_LEN   16
 output size in bytes of an MD5 digest
 
#define N_DIGEST_SHA1_LEN   20
 output size in bytes of a SHA-1 digest
 
#define N_DIGEST_SHA256_LEN   32
 output size in bytes of a SHA-256 digest
 
#define N_DIGEST_SHA384_LEN   48
 output size in bytes of a SHA-384 digest
 
#define N_DIGEST_SHA512_LEN   64
 output size in bytes of a SHA-512 digest
 

Functions

int n_aead_aes256gcm_decrypt (const unsigned char *key, const unsigned char *iv, size_t iv_len, const unsigned char *aad, size_t aad_len, const unsigned char *ct, size_t ct_len, const unsigned char *tag, unsigned char *out_pt)
 AES-256-GCM authenticated decryption.
 
int n_aead_aes256gcm_encrypt (const unsigned char *key, const unsigned char *iv, size_t iv_len, const unsigned char *aad, size_t aad_len, const unsigned char *pt, size_t pt_len, unsigned char *out_ct, unsigned char *out_tag)
 AES-256-GCM authenticated encryption.
 
int n_digest_consttime_equal (const unsigned char *a, const unsigned char *b, size_t len)
 constant-time comparison of two equal-length byte buffers (for verifying MACs/signatures without a timing side channel).
 
int n_digest_md5 (const unsigned char *data, size_t len, unsigned char *out)
 compute the MD5 digest of len bytes of data into out (N_DIGEST_MD5_LEN bytes).
 
int n_digest_sha1 (const unsigned char *data, size_t len, unsigned char *out)
 compute the SHA-1 digest of len bytes of data into out (N_DIGEST_SHA1_LEN bytes).
 
int n_digest_sha256 (const unsigned char *data, size_t len, unsigned char *out)
 compute the SHA-256 digest of len bytes of data into out (N_DIGEST_SHA256_LEN bytes).
 
N_STRn_digest_sha256_hex (const unsigned char *data, size_t len)
 compute the SHA-256 digest of data and return it as a lowercase hex N_STR; free with free_nstr.
 
int n_digest_sha384 (const unsigned char *data, size_t len, unsigned char *out)
 compute the SHA-384 digest of len bytes of data into out (N_DIGEST_SHA384_LEN bytes).
 
int n_digest_sha512 (const unsigned char *data, size_t len, unsigned char *out)
 compute the SHA-512 digest of len bytes of data into out (N_DIGEST_SHA512_LEN bytes).
 
int n_hmac_sha1 (const unsigned char *key, size_t key_len, const unsigned char *data, size_t data_len, unsigned char *out)
 compute HMAC-SHA1 of data under key into out (N_DIGEST_SHA1_LEN bytes).
 
int n_hmac_sha256 (const unsigned char *key, size_t key_len, const unsigned char *data, size_t data_len, unsigned char *out)
 compute HMAC-SHA256 of data under key into out (N_DIGEST_SHA256_LEN bytes).
 
int n_hmac_sha384 (const unsigned char *key, size_t key_len, const unsigned char *data, size_t data_len, unsigned char *out)
 compute HMAC-SHA384 of data under key into out (N_DIGEST_SHA384_LEN bytes).
 
int n_hmac_sha512 (const unsigned char *key, size_t key_len, const unsigned char *data, size_t data_len, unsigned char *out)
 compute HMAC-SHA512 of data under key into out (N_DIGEST_SHA512_LEN bytes).
 
int n_pbkdf2_hmac_sha1 (const unsigned char *pass, size_t pass_len, const unsigned char *salt, size_t salt_len, unsigned int iterations, unsigned char *out, size_t out_len)
 derive out_len key bytes from pass and salt with PBKDF2-HMAC-SHA1 over iterations rounds (RFC 2898).
 
int n_pbkdf2_hmac_sha256 (const unsigned char *pass, size_t pass_len, const unsigned char *salt, size_t salt_len, unsigned int iterations, unsigned char *out, size_t out_len)
 derive out_len key bytes from pass and salt with PBKDF2-HMAC-SHA256 over iterations rounds (RFC 2898).
 

Detailed Description

Macro Definition Documentation

◆ N_AEAD_AES256GCM_KEY_LEN

#define N_AEAD_AES256GCM_KEY_LEN   32

key size in bytes for AES-256-GCM

Examples
ex_digest.c.

Definition at line 88 of file n_digest.h.

◆ N_AEAD_AES256GCM_TAG_LEN

#define N_AEAD_AES256GCM_TAG_LEN   16

authentication tag size in bytes for AES-256-GCM

Examples
ex_digest.c.

Definition at line 90 of file n_digest.h.

◆ N_DIGEST_MD5_LEN

#define N_DIGEST_MD5_LEN   16

output size in bytes of an MD5 digest

Examples
ex_digest.c.

Definition at line 46 of file n_digest.h.

◆ N_DIGEST_SHA1_LEN

#define N_DIGEST_SHA1_LEN   20

output size in bytes of a SHA-1 digest

Examples
ex_digest.c.

Definition at line 48 of file n_digest.h.

◆ N_DIGEST_SHA256_LEN

#define N_DIGEST_SHA256_LEN   32

output size in bytes of a SHA-256 digest

Examples
ex_digest.c.

Definition at line 50 of file n_digest.h.

◆ N_DIGEST_SHA384_LEN

#define N_DIGEST_SHA384_LEN   48

output size in bytes of a SHA-384 digest

Examples
ex_digest.c.

Definition at line 52 of file n_digest.h.

◆ N_DIGEST_SHA512_LEN

#define N_DIGEST_SHA512_LEN   64

output size in bytes of a SHA-512 digest

Examples
ex_digest.c.

Definition at line 54 of file n_digest.h.

Function Documentation

◆ n_aead_aes256gcm_decrypt()

int n_aead_aes256gcm_decrypt ( const unsigned char *  key,
const unsigned char *  iv,
size_t  iv_len,
const unsigned char *  aad,
size_t  aad_len,
const unsigned char *  ct,
size_t  ct_len,
const unsigned char *  tag,
unsigned char *  out_pt 
)

AES-256-GCM authenticated decryption.

Arguments mirror n_aead_aes256gcm_encrypt; tag is the N_AEAD_AES256GCM_TAG_LEN expected tag. Writes ct_len plaintext bytes to out_pt only when the tag verifies. Returns 0 on success (authentic), -1 on error or a tag mismatch.

Definition at line 195 of file n_digest.c.

References key, LOG_ERR, N_AEAD_AES256GCM_TAG_LEN, and n_log.

Referenced by test_aead().

+ Here is the caller graph for this function:

◆ n_aead_aes256gcm_encrypt()

int n_aead_aes256gcm_encrypt ( const unsigned char *  key,
const unsigned char *  iv,
size_t  iv_len,
const unsigned char *  aad,
size_t  aad_len,
const unsigned char *  pt,
size_t  pt_len,
unsigned char *  out_ct,
unsigned char *  out_tag 
)

AES-256-GCM authenticated encryption.

key is N_AEAD_AES256GCM_KEY_LEN bytes; iv is iv_len bytes (12 recommended); aad may be NULL when aad_len is 0. Writes pt_len ciphertext bytes to out_ct and N_AEAD_AES256GCM_TAG_LEN tag bytes to out_tag. Returns 0 on success, -1 on error.

Definition at line 157 of file n_digest.c.

References key, LOG_ERR, N_AEAD_AES256GCM_TAG_LEN, and n_log.

Referenced by test_aead().

+ Here is the caller graph for this function:

◆ n_digest_consttime_equal()

int n_digest_consttime_equal ( const unsigned char *  a,
const unsigned char *  b,
size_t  len 
)

constant-time comparison of two equal-length byte buffers (for verifying MACs/signatures without a timing side channel).

Returns 1 when equal, 0 otherwise.

Definition at line 124 of file n_digest.c.

Referenced by test_consttime().

+ Here is the caller graph for this function:

◆ n_digest_md5()

int n_digest_md5 ( const unsigned char *  data,
size_t  len,
unsigned char *  out 
)

compute the MD5 digest of len bytes of data into out (N_DIGEST_MD5_LEN bytes).

Returns 0 on success, -1 on error.

Definition at line 58 of file n_digest.c.

References n_digest_oneshot().

Referenced by test_digests().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ n_digest_sha1()

int n_digest_sha1 ( const unsigned char *  data,
size_t  len,
unsigned char *  out 
)

compute the SHA-1 digest of len bytes of data into out (N_DIGEST_SHA1_LEN bytes).

Returns 0 on success, -1 on error.

Definition at line 62 of file n_digest.c.

References n_digest_oneshot().

Referenced by test_digests().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ n_digest_sha256()

int n_digest_sha256 ( const unsigned char *  data,
size_t  len,
unsigned char *  out 
)

compute the SHA-256 digest of len bytes of data into out (N_DIGEST_SHA256_LEN bytes).

Returns 0 on success, -1 on error.

Definition at line 66 of file n_digest.c.

References n_digest_oneshot().

Referenced by n_digest_sha256_hex(), test_digests(), and test_errors().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ n_digest_sha256_hex()

N_STR * n_digest_sha256_hex ( const unsigned char *  data,
size_t  len 
)

compute the SHA-256 digest of data and return it as a lowercase hex N_STR; free with free_nstr.

Returns NULL on error.

Definition at line 78 of file n_digest.c.

References n_digest_sha256(), N_DIGEST_SHA256_LEN, and n_hex_encode().

Referenced by test_sha256_hex().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ n_digest_sha384()

int n_digest_sha384 ( const unsigned char *  data,
size_t  len,
unsigned char *  out 
)

compute the SHA-384 digest of len bytes of data into out (N_DIGEST_SHA384_LEN bytes).

Returns 0 on success, -1 on error.

Definition at line 70 of file n_digest.c.

References n_digest_oneshot().

Referenced by test_digests().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ n_digest_sha512()

int n_digest_sha512 ( const unsigned char *  data,
size_t  len,
unsigned char *  out 
)

compute the SHA-512 digest of len bytes of data into out (N_DIGEST_SHA512_LEN bytes).

Returns 0 on success, -1 on error.

Definition at line 74 of file n_digest.c.

References n_digest_oneshot().

Referenced by test_digests().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ n_hmac_sha1()

int n_hmac_sha1 ( const unsigned char *  key,
size_t  key_len,
const unsigned char *  data,
size_t  data_len,
unsigned char *  out 
)

compute HMAC-SHA1 of data under key into out (N_DIGEST_SHA1_LEN bytes).

A NULL key or data is treated as empty when its length is 0. Returns 0 on success, -1 on error.

Definition at line 108 of file n_digest.c.

References key, N_DIGEST_SHA1_LEN, and n_hmac_oneshot().

Referenced by test_hmac_variants().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ n_hmac_sha256()

int n_hmac_sha256 ( const unsigned char *  key,
size_t  key_len,
const unsigned char *  data,
size_t  data_len,
unsigned char *  out 
)

compute HMAC-SHA256 of data under key into out (N_DIGEST_SHA256_LEN bytes).

A NULL key or data is treated as empty when its length is 0. Returns 0 on success, -1 on error.

Definition at line 112 of file n_digest.c.

References key, N_DIGEST_SHA256_LEN, and n_hmac_oneshot().

Referenced by test_errors(), and test_hmac().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ n_hmac_sha384()

int n_hmac_sha384 ( const unsigned char *  key,
size_t  key_len,
const unsigned char *  data,
size_t  data_len,
unsigned char *  out 
)

compute HMAC-SHA384 of data under key into out (N_DIGEST_SHA384_LEN bytes).

A NULL key or data is treated as empty when its length is 0. Returns 0 on success, -1 on error.

Definition at line 116 of file n_digest.c.

References key, N_DIGEST_SHA384_LEN, and n_hmac_oneshot().

Referenced by test_hmac_variants().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ n_hmac_sha512()

int n_hmac_sha512 ( const unsigned char *  key,
size_t  key_len,
const unsigned char *  data,
size_t  data_len,
unsigned char *  out 
)

compute HMAC-SHA512 of data under key into out (N_DIGEST_SHA512_LEN bytes).

A NULL key or data is treated as empty when its length is 0. Returns 0 on success, -1 on error.

Definition at line 120 of file n_digest.c.

References key, N_DIGEST_SHA512_LEN, and n_hmac_oneshot().

Referenced by test_hmac_variants().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ n_pbkdf2_hmac_sha1()

int n_pbkdf2_hmac_sha1 ( const unsigned char *  pass,
size_t  pass_len,
const unsigned char *  salt,
size_t  salt_len,
unsigned int  iterations,
unsigned char *  out,
size_t  out_len 
)

derive out_len key bytes from pass and salt with PBKDF2-HMAC-SHA1 over iterations rounds (RFC 2898).

A NULL pass or salt is treated as empty when its length is 0. Returns 0 on success, -1 on error.

Definition at line 149 of file n_digest.c.

References n_pbkdf2_oneshot().

Referenced by test_pbkdf2().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ n_pbkdf2_hmac_sha256()

int n_pbkdf2_hmac_sha256 ( const unsigned char *  pass,
size_t  pass_len,
const unsigned char *  salt,
size_t  salt_len,
unsigned int  iterations,
unsigned char *  out,
size_t  out_len 
)

derive out_len key bytes from pass and salt with PBKDF2-HMAC-SHA256 over iterations rounds (RFC 2898).

A NULL pass or salt is treated as empty when its length is 0. Returns 0 on success, -1 on error.

Definition at line 153 of file n_digest.c.

References n_pbkdf2_oneshot().

Referenced by test_pbkdf2().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: