112 lines
2.7 KiB
C
112 lines
2.7 KiB
C
#ifndef BLUELIB_CORE_HASH_H_
|
|
#define BLUELIB_CORE_HASH_H_
|
|
|
|
#include <blue/core/misc.h>
|
|
#include <blue/core/status.h>
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
|
|
#define B_DIGEST_LENGTH_128 16
|
|
#define B_DIGEST_LENGTH_160 20
|
|
#define B_DIGEST_LENGTH_192 24
|
|
#define B_DIGEST_LENGTH_224 28
|
|
#define B_DIGEST_LENGTH_256 32
|
|
#define B_DIGEST_LENGTH_384 48
|
|
#define B_DIGEST_LENGTH_512 64
|
|
|
|
#define B_DIGEST_LENGTH_MD4 B_DIGEST_LENGTH_128
|
|
#define B_DIGEST_LENGTH_MD5 B_DIGEST_LENGTH_128
|
|
#define B_DIGEST_LENGTH_SHA1 B_DIGEST_LENGTH_160
|
|
#define B_DIGEST_LENGTH_SHA2_224 B_DIGEST_LENGTH_224
|
|
#define B_DIGEST_LENGTH_SHA2_256 B_DIGEST_LENGTH_256
|
|
#define B_DIGEST_LENGTH_SHA2_384 B_DIGEST_LENGTH_384
|
|
#define B_DIGEST_LENGTH_SHA2_512 B_DIGEST_LENGTH_512
|
|
#define B_DIGEST_LENGTH_SHA3_224 B_DIGEST_LENGTH_224
|
|
#define B_DIGEST_LENGTH_SHA3_256 B_DIGEST_LENGTH_256
|
|
#define B_DIGEST_LENGTH_SHA3_384 B_DIGEST_LENGTH_384
|
|
#define B_DIGEST_LENGTH_SHA3_512 B_DIGEST_LENGTH_512
|
|
#define B_DIGEST_LENGTH_SHAKE128 B_DIGEST_LENGTH_128
|
|
#define B_DIGEST_LENGTH_SHAKE256 B_DIGEST_LENGTH_256
|
|
|
|
struct b_hash_function_ops;
|
|
struct b_rope;
|
|
|
|
typedef enum b_hash_function {
|
|
B_HASH_NONE = 0,
|
|
B_HASH_MD4,
|
|
B_HASH_MD5,
|
|
B_HASH_SHA1,
|
|
B_HASH_SHA2_224,
|
|
B_HASH_SHA2_256,
|
|
B_HASH_SHA2_384,
|
|
B_HASH_SHA2_512,
|
|
B_HASH_SHA3_224,
|
|
B_HASH_SHA3_256,
|
|
B_HASH_SHA3_384,
|
|
B_HASH_SHA3_512,
|
|
B_HASH_SHAKE128,
|
|
B_HASH_SHAKE256,
|
|
} b_hash_function;
|
|
|
|
typedef struct b_hash_ctx {
|
|
b_hash_function ctx_func;
|
|
const struct b_hash_function_ops *ctx_ops;
|
|
|
|
union {
|
|
struct {
|
|
uint32_t lo, hi;
|
|
uint32_t a, b, c, d;
|
|
uint32_t block[16];
|
|
unsigned char buffer[64];
|
|
} md4;
|
|
|
|
struct {
|
|
unsigned int count[2];
|
|
unsigned int a, b, c, d;
|
|
unsigned int block[16];
|
|
unsigned char input[64];
|
|
} md5;
|
|
|
|
struct {
|
|
uint32_t state[5];
|
|
uint32_t count[2];
|
|
unsigned char buffer[64];
|
|
} sha1;
|
|
|
|
struct {
|
|
uint64_t curlen;
|
|
uint64_t length;
|
|
unsigned char buf[128];
|
|
uint32_t state[8];
|
|
} sha2_256;
|
|
|
|
struct {
|
|
uint64_t curlen;
|
|
uint64_t length;
|
|
unsigned char block[256];
|
|
uint64_t state[8];
|
|
} sha2_512;
|
|
|
|
struct {
|
|
union {
|
|
uint8_t b[200];
|
|
uint64_t q[25];
|
|
} st;
|
|
|
|
int pt, rsiz, mdlen;
|
|
} sha3;
|
|
} ctx_state;
|
|
} b_hash_ctx;
|
|
|
|
BLUE_API uint64_t b_hash_cstr(const char *s);
|
|
BLUE_API uint64_t b_hash_cstr_ex(const char *s, size_t *len);
|
|
|
|
BLUE_API b_status b_hash_ctx_init(b_hash_ctx *ctx, b_hash_function func);
|
|
BLUE_API b_status b_hash_ctx_reset(b_hash_ctx *ctx);
|
|
BLUE_API b_status b_hash_ctx_update(b_hash_ctx *ctx, const void *p, size_t len);
|
|
BLUE_API b_status b_hash_ctx_update_rope(b_hash_ctx *ctx, const struct b_rope *rope);
|
|
BLUE_API b_status b_hash_ctx_finish(
|
|
b_hash_ctx *ctx, void *out_digest, size_t out_max);
|
|
|
|
#endif
|