nostrdb: port everything over to be in as sync as possible

for now
This commit is contained in:
William Casarin
2024-01-25 14:33:44 -08:00
committed by Daniel D’Aquino
parent 954f48b23d
commit 1fb88a912a
36 changed files with 4278 additions and 417 deletions

View File

@@ -1,7 +1,14 @@
#ifndef CCAN_CRYPTO_SHA256_H
#define CCAN_CRYPTO_SHA256_H
/** Output length for `wally_sha256` */
#define SHA256_LEN 32
/* BSD-MIT - see LICENSE file for details */
#include "config.h"
/* #include "config.h" */
#include <stdint.h>
#include <stdlib.h>
@@ -20,17 +27,17 @@
* Other fields may be added to the union in future.
*/
struct sha256 {
union {
uint32_t u32[8];
unsigned char u8[32];
} u;
union {
uint32_t u32[8];
unsigned char u8[32];
} u;
};
/**
* sha256 - return sha256 of an object.
* @sha256: the sha256 to fill in
* @p: pointer to memory,
* @size: the number of bytes pointed to by
* @size: the number of bytes pointed to by @p
*
* The bytes pointed to by @p is SHA256 hashed into @sha256. This is
* equivalent to sha256_init(), sha256_update() then sha256_done().
@@ -42,14 +49,14 @@ void sha256(struct sha256 *sha, const void *p, size_t size);
*/
struct sha256_ctx {
#ifdef CCAN_CRYPTO_SHA256_USE_OPENSSL
SHA256_CTX c;
SHA256_CTX c;
#else
uint32_t s[8];
union {
uint32_t u32[16];
unsigned char u8[64];
} buf;
size_t bytes;
uint32_t s[8];
union {
uint32_t u32[16];
unsigned char u8[64];
} buf;
size_t bytes;
#endif
};
@@ -66,13 +73,13 @@ struct sha256_ctx {
* Example:
* static void hash_all(const char **arr, struct sha256 *hash)
* {
* size_t i;
* struct sha256_ctx ctx;
* size_t i;
* struct sha256_ctx ctx;
*
* sha256_init(&ctx);
* for (i = 0; arr[i]; i++)
* sha256_update(&ctx, arr[i], strlen(arr[i]));
* sha256_done(&ctx, hash);
* sha256_init(&ctx);
* for (i = 0; arr[i]; i++)
* sha256_update(&ctx, arr[i], strlen(arr[i]));
* sha256_done(&ctx, hash);
* }
*/
void sha256_init(struct sha256_ctx *ctx);
@@ -86,33 +93,33 @@ void sha256_init(struct sha256_ctx *ctx);
* Example:
* static void hash_all(const char **arr, struct sha256 *hash)
* {
* size_t i;
* struct sha256_ctx ctx = SHA256_INIT;
* size_t i;
* struct sha256_ctx ctx = SHA256_INIT;
*
* for (i = 0; arr[i]; i++)
* sha256_update(&ctx, arr[i], strlen(arr[i]));
* sha256_done(&ctx, hash);
* for (i = 0; arr[i]; i++)
* sha256_update(&ctx, arr[i], strlen(arr[i]));
* sha256_done(&ctx, hash);
* }
*/
#ifdef CCAN_CRYPTO_SHA256_USE_OPENSSL
#define SHA256_INIT \
{ { { 0x6a09e667ul, 0xbb67ae85ul, 0x3c6ef372ul, 0xa54ff53aul, \
0x510e527ful, 0x9b05688cul, 0x1f83d9abul, 0x5be0cd19ul }, \
0x0, 0x0, \
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \
0x0, 0x20 } }
#define SHA256_INIT \
{ { { 0x6a09e667ul, 0xbb67ae85ul, 0x3c6ef372ul, 0xa54ff53aul, \
0x510e527ful, 0x9b05688cul, 0x1f83d9abul, 0x5be0cd19ul }, \
0x0, 0x0, \
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \
0x0, 0x20 } }
#else
#define SHA256_INIT \
{ { 0x6a09e667ul, 0xbb67ae85ul, 0x3c6ef372ul, 0xa54ff53aul, \
0x510e527ful, 0x9b05688cul, 0x1f83d9abul, 0x5be0cd19ul }, \
{ { 0 } }, 0 }
#define SHA256_INIT \
{ { 0x6a09e667ul, 0xbb67ae85ul, 0x3c6ef372ul, 0xa54ff53aul, \
0x510e527ful, 0x9b05688cul, 0x1f83d9abul, 0x5be0cd19ul }, \
{ { 0 } }, 0 }
#endif
/**
* sha256_update - include some memory in the hash.
* @ctx: the sha256_ctx to use
* @p: pointer to memory,
* @size: the number of bytes pointed to by
* @size: the number of bytes pointed to by @p
*
* You can call this multiple times to hash more data, before calling
* sha256_done().
@@ -144,4 +151,5 @@ void sha256_le64(struct sha256_ctx *ctx, uint64_t v);
void sha256_be16(struct sha256_ctx *ctx, uint16_t v);
void sha256_be32(struct sha256_ctx *ctx, uint32_t v);
void sha256_be64(struct sha256_ctx *ctx, uint64_t v);
#endif /* CCAN_CRYPTO_SHA256_H */