tor  master
crypto_digest.h
Go to the documentation of this file.
1 /* Copyright (c) 2001, Matej Pfajfar.
2  * Copyright (c) 2001-2004, Roger Dingledine.
3  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4  * Copyright (c) 2007-2017, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
6 
13 #ifndef TOR_CRYPTO_DIGEST_H
14 #define TOR_CRYPTO_DIGEST_H
15 
16 #include <stdio.h>
17 
18 #include "container.h"
19 #include "torint.h"
20 
22 #define DIGEST_LEN 20
23 
25 #define DIGEST256_LEN 32
26 
27 #define DIGEST512_LEN 64
28 
31 #define BASE32_DIGEST_LEN 32
32 
34 #define BASE64_DIGEST_LEN 27
35 
37 #define BASE64_DIGEST256_LEN 43
38 
40 #define BASE64_DIGEST512_LEN 86
41 
43 #define HEX_DIGEST_LEN 40
44 
45 #define HEX_DIGEST256_LEN 64
46 
47 #define HEX_DIGEST512_LEN 128
48 
49 typedef enum {
50  DIGEST_SHA1 = 0,
51  DIGEST_SHA256 = 1,
52  DIGEST_SHA512 = 2,
53  DIGEST_SHA3_256 = 3,
54  DIGEST_SHA3_512 = 4,
55 } digest_algorithm_t;
56 #define N_DIGEST_ALGORITHMS (DIGEST_SHA3_512+1)
57 #define N_COMMON_DIGEST_ALGORITHMS (DIGEST_SHA256+1)
58 
59 #define DIGEST_CHECKPOINT_BYTES (SIZEOF_VOID_P + 512)
60 
63  uint8_t mem[DIGEST_CHECKPOINT_BYTES];
65 
74 typedef struct {
75  char d[N_COMMON_DIGEST_ALGORITHMS][DIGEST256_LEN];
77 
78 typedef struct crypto_digest_t crypto_digest_t;
79 typedef struct crypto_xof_t crypto_xof_t;
80 
81 /* SHA-1 and other digests */
82 int crypto_digest(char *digest, const char *m, size_t len);
83 int crypto_digest256(char *digest, const char *m, size_t len,
84  digest_algorithm_t algorithm);
85 int crypto_digest512(char *digest, const char *m, size_t len,
86  digest_algorithm_t algorithm);
87 int crypto_common_digests(common_digests_t *ds_out, const char *m, size_t len);
88 void crypto_digest_smartlist_prefix(char *digest_out, size_t len_out,
89  const char *prepend,
90  const struct smartlist_t *lst,
91  const char *append,
92  digest_algorithm_t alg);
93 void crypto_digest_smartlist(char *digest_out, size_t len_out,
94  const struct smartlist_t *lst, const char *append,
95  digest_algorithm_t alg);
96 const char *crypto_digest_algorithm_get_name(digest_algorithm_t alg);
97 size_t crypto_digest_algorithm_get_length(digest_algorithm_t alg);
98 int crypto_digest_algorithm_parse_name(const char *name);
99 crypto_digest_t *crypto_digest_new(void);
100 crypto_digest_t *crypto_digest256_new(digest_algorithm_t algorithm);
101 crypto_digest_t *crypto_digest512_new(digest_algorithm_t algorithm);
102 void crypto_digest_free_(crypto_digest_t *digest);
103 #define crypto_digest_free(d) \
104  FREE_AND_NULL(crypto_digest_t, crypto_digest_free_, (d))
105 void crypto_digest_add_bytes(crypto_digest_t *digest, const char *data,
106  size_t len);
107 void crypto_digest_get_digest(crypto_digest_t *digest,
108  char *out, size_t out_len);
109 crypto_digest_t *crypto_digest_dup(const crypto_digest_t *digest);
111  const crypto_digest_t *digest);
112 void crypto_digest_restore(crypto_digest_t *digest,
113  const crypto_digest_checkpoint_t *checkpoint);
114 void crypto_digest_assign(crypto_digest_t *into,
115  const crypto_digest_t *from);
116 void crypto_hmac_sha256(char *hmac_out,
117  const char *key, size_t key_len,
118  const char *msg, size_t msg_len);
119 void crypto_mac_sha3_256(uint8_t *mac_out, size_t len_out,
120  const uint8_t *key, size_t key_len,
121  const uint8_t *msg, size_t msg_len);
122 
123 /* xof functions*/
124 crypto_xof_t *crypto_xof_new(void);
125 void crypto_xof_add_bytes(crypto_xof_t *xof, const uint8_t *data, size_t len);
126 void crypto_xof_squeeze_bytes(crypto_xof_t *xof, uint8_t *out, size_t len);
127 void crypto_xof_free_(crypto_xof_t *xof);
128 #define crypto_xof_free(xof) \
129  FREE_AND_NULL(crypto_xof_t, crypto_xof_free_, (xof))
130 
131 #ifdef TOR_UNIT_TESTS
132 digest_algorithm_t crypto_digest_get_algorithm(crypto_digest_t *digest);
133 #endif
134 
135 #endif /* !defined(TOR_CRYPTO_DIGEST_H) */
136 
void crypto_mac_sha3_256(uint8_t *mac_out, size_t len_out, const uint8_t *key, size_t key_len, const uint8_t *msg, size_t msg_len)
Definition: crypto_digest.c:508
crypto_digest_t * crypto_digest_dup(const crypto_digest_t *digest)
Definition: crypto_digest.c:401
void crypto_digest_smartlist_prefix(char *digest_out, size_t len_out, const char *prepend, const smartlist_t *lst, const char *append, digest_algorithm_t alg)
Definition: crypto_digest.c:467
Definition: crypto_digest.h:74
void crypto_digest_get_digest(crypto_digest_t *digest, char *out, size_t out_len)
Definition: crypto_digest.c:350
int crypto_digest(char *digest, const char *m, size_t len)
Definition: crypto_digest.c:35
void crypto_digest_smartlist(char *digest_out, size_t len_out, const smartlist_t *lst, const char *append, digest_algorithm_t alg)
Definition: crypto_digest.c:452
void crypto_xof_free_(crypto_xof_t *xof)
Definition: crypto_digest.c:576
void crypto_digest_checkpoint(crypto_digest_checkpoint_t *checkpoint, const crypto_digest_t *digest)
Definition: crypto_digest.c:412
crypto_digest_t * crypto_digest256_new(digest_algorithm_t algorithm)
Definition: crypto_digest.c:277
void crypto_digest_add_bytes(crypto_digest_t *digest, const char *data, size_t len)
Definition: crypto_digest.c:310
Definition: container.h:18
Header file to define uint32_t and friends.
Definition: d.py:1
crypto_xof_t * crypto_xof_new(void)
Definition: crypto_digest.c:545
void crypto_digest_free_(crypto_digest_t *digest)
Definition: crypto_digest.c:295
void crypto_xof_add_bytes(crypto_xof_t *xof, const uint8_t *data, size_t len)
Definition: crypto_digest.c:558
int crypto_digest_algorithm_parse_name(const char *name)
Definition: crypto_digest.c:133
const char * crypto_digest_algorithm_get_name(digest_algorithm_t alg)
Definition: crypto_digest.c:109
struct crypto_digest_checkpoint_t crypto_digest_checkpoint_t
Definition: crypto_digest.c:535
void crypto_hmac_sha256(char *hmac_out, const char *key, size_t key_len, const char *msg, size_t msg_len)
Definition: crypto_digest.c:489
void crypto_digest_restore(crypto_digest_t *digest, const crypto_digest_checkpoint_t *checkpoint)
Definition: crypto_digest.c:424
#define DIGEST256_LEN
Definition: crypto_digest.h:25
Definition: crypto_digest.h:62
size_t crypto_digest_algorithm_get_length(digest_algorithm_t alg)
Definition: crypto_digest.c:151
void crypto_xof_squeeze_bytes(crypto_xof_t *xof, uint8_t *out, size_t len)
Definition: crypto_digest.c:568
int crypto_common_digests(common_digests_t *ds_out, const char *m, size_t len)
Definition: crypto_digest.c:95
void crypto_digest_assign(crypto_digest_t *into, const crypto_digest_t *from)
Definition: crypto_digest.c:436
crypto_digest_t * crypto_digest512_new(digest_algorithm_t algorithm)
Definition: crypto_digest.c:286
int crypto_digest256(char *digest, const char *m, size_t len, digest_algorithm_t algorithm)
Definition: crypto_digest.c:48
crypto_digest_t * crypto_digest_new(void)
Definition: crypto_digest.c:265
Definition: crypto_digest.c:171
int crypto_digest512(char *digest, const char *m, size_t len, digest_algorithm_t algorithm)
Definition: crypto_digest.c:71