6 n__digest_8h
gullradriel edited this page 2026-10-09 00:55:18 +02:00

include/nilorea/n_digest.h

Message digests, HMAC, PBKDF2, and AES-256-GCM AEAD over the OpenSSL EVP API. More...

Detailed Description

Message digests, HMAC, PBKDF2, and AES-256-GCM AEAD over the OpenSSL EVP API.

Author: Castagnier Mickael

Version: 1.0

Source code

/*
 * Nilorea Library
 * Copyright (C) 2005-2026 Castagnier Mickael
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 */


#ifndef __NILOREA_DIGEST_HEADER
#define __NILOREA_DIGEST_HEADER

#ifdef __cplusplus
extern "C" {
#endif


#ifdef HAVE_OPENSSL

#include "nilorea/n_openssl_compat.h"
#include "nilorea/n_str.h"

#include <stddef.h>
#include <stdint.h>

/* Feature tests for the primitives that need a TLS library newer than the
 * OpenSSL 1.0.2 this one still builds against. Each maps to a public entry
 * point that is compiled as a logged failure when the linked library cannot
 * provide it, so a caller (or a test) can skip the call instead of reporting an
 * error. The thresholds live in n_openssl_compat.h because OPENSSL_VERSION_NUMBER
 * alone gets LibreSSL wrong: it is fixed at 0x20000000L there, and LibreSSL has
 * no BLAKE2 at any version. */

#define N_DIGEST_HAVE_BLAKE2B512 N_OSSL_HAVE_BLAKE2B512

#define N_AEAD_HAVE_CHACHA20POLY1305 N_OSSL_HAVE_CHACHA20POLY1305

#define N_KDF_HAVE_EVP_KDF N_OSSL_HAVE_EVP_KDF

#define N_DIGEST_MD5_LEN 16
#define N_DIGEST_SHA1_LEN 20
#define N_DIGEST_SHA256_LEN 32
#define N_DIGEST_SHA384_LEN 48
#define N_DIGEST_SHA512_LEN 64
#define N_DIGEST_BLAKE2B512_LEN 64

int n_digest_md5(const unsigned char* data, size_t len, unsigned char* out);
int n_digest_sha1(const unsigned char* data, size_t len, unsigned char* out);
int n_digest_sha256(const unsigned char* data, size_t len, unsigned char* out);
int n_digest_sha384(const unsigned char* data, size_t len, unsigned char* out);
int n_digest_sha512(const unsigned char* data, size_t len, unsigned char* out);
int n_digest_blake2b512(const unsigned char* data, size_t len, unsigned char* out);

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

int n_hmac_sha1(const unsigned char* key, size_t key_len, const unsigned char* data, size_t data_len, unsigned char* out);
int n_hmac_sha256(const unsigned char* key, size_t key_len, const unsigned char* data, size_t data_len, unsigned char* out);
int n_hmac_sha384(const unsigned char* key, size_t key_len, const unsigned char* data, size_t data_len, unsigned char* out);
int n_hmac_sha512(const unsigned char* key, size_t key_len, const unsigned char* data, size_t data_len, unsigned char* out);

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

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);
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);

#define N_AEAD_AES256GCM_KEY_LEN 32
#define N_AEAD_AES256GCM_TAG_LEN 16

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);
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);

#define N_AEAD_CHACHA20POLY1305_KEY_LEN 32
#define N_AEAD_CHACHA20POLY1305_TAG_LEN 16

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);
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);

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);
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);

#endif /* HAVE_OPENSSL */


#ifdef __cplusplus
}
#endif

#endif /* __NILOREA_DIGEST_HEADER */