Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_digest.h
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
26
27#ifndef __NILOREA_DIGEST_HEADER
28#define __NILOREA_DIGEST_HEADER
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
38
39#ifdef HAVE_OPENSSL
40
42#include "nilorea/n_str.h"
43
44#include <stddef.h>
45#include <stdint.h>
46
47/* Feature tests for the primitives that need a TLS library newer than the
48 * OpenSSL 1.0.2 this one still builds against. Each maps to a public entry
49 * point that is compiled as a logged failure when the linked library cannot
50 * provide it, so a caller (or a test) can skip the call instead of reporting an
51 * error. The thresholds live in n_openssl_compat.h because OPENSSL_VERSION_NUMBER
52 * alone gets LibreSSL wrong: it is fixed at 0x20000000L there, and LibreSSL has
53 * no BLAKE2 at any version. */
54
56#define N_DIGEST_HAVE_BLAKE2B512 N_OSSL_HAVE_BLAKE2B512
57
59#define N_AEAD_HAVE_CHACHA20POLY1305 N_OSSL_HAVE_CHACHA20POLY1305
60
62#define N_KDF_HAVE_EVP_KDF N_OSSL_HAVE_EVP_KDF
63
65#define N_DIGEST_MD5_LEN 16
67#define N_DIGEST_SHA1_LEN 20
69#define N_DIGEST_SHA256_LEN 32
71#define N_DIGEST_SHA384_LEN 48
73#define N_DIGEST_SHA512_LEN 64
75#define N_DIGEST_BLAKE2B512_LEN 64
76
78int n_digest_md5(const unsigned char* data, size_t len, unsigned char* out);
80int n_digest_sha1(const unsigned char* data, size_t len, unsigned char* out);
82int n_digest_sha256(const unsigned char* data, size_t len, unsigned char* out);
84int n_digest_sha384(const unsigned char* data, size_t len, unsigned char* out);
86int n_digest_sha512(const unsigned char* data, size_t len, unsigned char* out);
88int n_digest_blake2b512(const unsigned char* data, size_t len, unsigned char* out);
89
91N_STR* n_digest_sha256_hex(const unsigned char* data, size_t len);
92
94int n_hmac_sha1(const unsigned char* key, size_t key_len, const unsigned char* data, size_t data_len, unsigned char* out);
96int n_hmac_sha256(const unsigned char* key, size_t key_len, const unsigned char* data, size_t data_len, unsigned char* out);
98int n_hmac_sha384(const unsigned char* key, size_t key_len, const unsigned char* data, size_t data_len, unsigned char* out);
100int n_hmac_sha512(const unsigned char* key, size_t key_len, const unsigned char* data, size_t data_len, unsigned char* out);
101
103int n_digest_consttime_equal(const unsigned char* a, const unsigned char* b, size_t len);
104
106int 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);
108int 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);
109
111#define N_AEAD_AES256GCM_KEY_LEN 32
113#define N_AEAD_AES256GCM_TAG_LEN 16
114
116int 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);
118int 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);
119
121#define N_AEAD_CHACHA20POLY1305_KEY_LEN 32
123#define N_AEAD_CHACHA20POLY1305_TAG_LEN 16
124
126int n_aead_chacha20poly1305_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);
128int n_aead_chacha20poly1305_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);
129
131int n_kdf_hkdf_sha256(const unsigned char* ikm, size_t ikm_len, const unsigned char* salt, size_t salt_len, const unsigned char* info, size_t info_len, unsigned char* out, size_t out_len);
133int n_kdf_scrypt(const unsigned char* pass, size_t pass_len, const unsigned char* salt, size_t salt_len, uint64_t n, uint32_t r, uint32_t p, uint64_t maxmem, unsigned char* out, size_t out_len);
134
135#endif /* HAVE_OPENSSL */
136
140
141#ifdef __cplusplus
142}
143#endif
144
145#endif /* __NILOREA_DIGEST_HEADER */
volatile char key[227]
TRUE while the key is held.
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).
Definition n_digest.c:66
int n_kdf_scrypt(const unsigned char *pass, size_t pass_len, const unsigned char *salt, size_t salt_len, uint64_t n, uint32_t r, uint32_t p, uint64_t maxmem, unsigned char *out, size_t out_len)
scrypt (RFC 7914) of pass with salt and cost parameters n (CPU/memory, a power of two),...
Definition n_digest.c:452
int n_aead_chacha20poly1305_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)
ChaCha20-Poly1305 (RFC 8439) authenticated encryption.
Definition n_digest.c:339
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).
Definition n_digest.c:74
int n_kdf_hkdf_sha256(const unsigned char *ikm, size_t ikm_len, const unsigned char *salt, size_t salt_len, const unsigned char *info, size_t info_len, unsigned char *out, size_t out_len)
HKDF-SHA256 (RFC 5869 extract-then-expand) of ikm with the given salt and info into out (out_len byte...
Definition n_digest.c:440
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).
Definition n_digest.c:166
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).
Definition n_digest.c:129
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.
Definition n_digest.c:174
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).
Definition n_digest.c:125
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).
Definition n_digest.c:62
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)...
Definition n_digest.c:170
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 ti...
Definition n_digest.c:141
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).
Definition n_digest.c:137
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).
Definition n_digest.c:70
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).
Definition n_digest.c:133
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.
Definition n_digest.c:212
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).
Definition n_digest.c:58
int n_aead_chacha20poly1305_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)
ChaCha20-Poly1305 authenticated decryption.
Definition n_digest.c:353
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.
Definition n_digest.c:95
int n_digest_blake2b512(const unsigned char *data, size_t len, unsigned char *out)
compute the BLAKE2b-512 digest of len bytes of data into out (N_DIGEST_BLAKE2B512_LEN bytes).
Definition n_digest.c:86
A box including a string and his lenght.
Definition n_str.h:61
One place deciding which OpenSSL/LibreSSL features the build can use.
N_STR and string function declaration.