git-trace

romele / git-trace public

62 commits · 1 branch · 1 tag · 1 contributor (top: Romele) · first commit 3d ago · last push 11m ago

src/crypto.h

#ifndef GT_CRYPTO_H
#define GT_CRYPTO_H

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

void sha256(const void *data, size_t len, uint8_t out[32]);

/* HMAC-SHA256 — used by webhook payload signatures */
void gt_hmac_sha256(const uint8_t *key, size_t klen,
                    const uint8_t *msg, size_t mlen, uint8_t out[32]);

/* PBKDF2-HMAC-SHA256, fixed 32-byte output (single block) */
void pbkdf2_sha256(const char *password, const uint8_t *salt, size_t saltlen,
                   unsigned iters, uint8_t out[32]);

/* scrypt (RFC 7914): N must be a power of two; returns -1 on bad
 * params or allocation failure */
int  scrypt_kdf(const uint8_t *pw, size_t pwlen,
                const uint8_t *salt, size_t saltlen,
                uint64_t N, unsigned r, unsigned p,
                uint8_t *out, size_t outlen);

/* stored formats: scrypt$<N>$<r>$<p>$<salt>$<hash> (current),
 * pbkdf2$<iters>$<salt>$<hash> (legacy, still verifies) */
int  pw_hash(const char *password, char *out, size_t outlen);
int  pw_verify(const char *password, const char *stored);
int  pw_needs_rehash(const char *stored);      /* legacy scheme? */
const char *pw_scheme(const char *stored);     /* pbkdf2|scrypt|unknown */

/* memory wipe the compiler cannot elide */
void gt_bzero(void *p, size_t n);

/* getrandom() with /dev/urandom fallback; dies on failure */
void rand_bytes(void *buf, size_t n);
/* 2*n+1 lowercase hex chars, NUL-terminated */
void rand_hex(char *out, size_t n);

int  consteq(const void *a, const void *b, size_t n);
/* returns decoded length, -1 on malformed input */
long b64_decode(const char *in, size_t inlen, void *out, size_t outcap);
/* base64-encode into out; strip_padding removes '=' (SSH fingerprints) */
void b64_encode(const void *in, size_t inlen, char *out, size_t outcap,
                int strip_padding);

void to_hex(const uint8_t *in, size_t n, char *out);
int  from_hex(const char *hex, uint8_t *out, size_t outlen);

#endif