Tor 0.4.9.2-alpha-dev
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
context.c
1/* Copyright (c) 2020 tevador <tevador@gmail.com> */
2/* See LICENSE for licensing information */
3
4#include <stdlib.h>
5#include <string.h>
6
7#include <hashx.h>
8#include "context.h"
9#include "compiler.h"
10#include "program.h"
11
12#define STRINGIZE_INNER(x) #x
13#define STRINGIZE(x) STRINGIZE_INNER(x)
14
15/* Salt used when generating hash functions. Useful for domain separation. */
16#ifndef HASHX_SALT
17#define HASHX_SALT HashX v1
18#endif
19
20/* Blake2b params used to generate program keys */
21const blake2b_param hashx_blake2_params = {
22 .digest_length = 64,
23 .key_length = 0,
24 .fanout = 1,
25 .depth = 1,
26 .leaf_length = 0,
27 .node_offset = 0,
28 .node_depth = 0,
29 .inner_length = 0,
30 .reserved = { 0 },
31 .salt = STRINGIZE(HASHX_SALT),
32 .personal = { 0 }
33};
34
35hashx_ctx* hashx_alloc(hashx_type type) {
36 hashx_ctx* ctx = malloc(sizeof(hashx_ctx));
37 if (ctx == NULL)
38 return NULL;
39
40 memset(ctx, 0, sizeof *ctx);
41 ctx->ctx_type = type;
42 if (type == HASHX_TYPE_COMPILED || type == HASHX_TRY_COMPILE) {
43 hashx_compiler_init(ctx);
44 }
45
46#ifdef HASHX_BLOCK_MODE
47 memcpy(&ctx->params, &hashx_blake2_params, 32);
48#endif
49 return ctx;
50}
51
52void hashx_free(hashx_ctx* ctx) {
53 if (ctx != NULL) {
54 hashx_compiler_destroy(ctx);
55 free(ctx);
56 }
57}
58
59#ifdef HASHX_RNG_CALLBACK
60void hashx_rng_callback(hashx_ctx* ctx,
61 void (*callback)(uint64_t*, void*),
62 void* callback_user_data)
63{
64 ctx->program.rng_callback = callback;
65 ctx->program.rng_callback_user_data = callback_user_data;
66}
67#endif