tor  0.4.0.0-alpha-dev
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-2018, 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 "lib/cc/torint.h"
17 #include "lib/defs/digest_sizes.h"
18 #include "lib/malloc/malloc.h"
20 
23 #define BASE32_DIGEST_LEN 32
24 
26 #define BASE64_DIGEST_LEN 27
27 
29 #define BASE64_DIGEST256_LEN 43
30 
32 #define BASE64_DIGEST512_LEN 86
33 
35 #define HEX_DIGEST_LEN 40
36 
37 #define HEX_DIGEST256_LEN 64
38 
39 #define HEX_DIGEST512_LEN 128
40 
41 typedef enum {
42  DIGEST_SHA1 = 0,
43  DIGEST_SHA256 = 1,
44  DIGEST_SHA512 = 2,
45  DIGEST_SHA3_256 = 3,
46  DIGEST_SHA3_512 = 4,
47 } digest_algorithm_t;
48 #define N_DIGEST_ALGORITHMS (DIGEST_SHA3_512+1)
49 #define N_COMMON_DIGEST_ALGORITHMS (DIGEST_SHA256+1)
50 
51 #define DIGEST_CHECKPOINT_BYTES (SIZEOF_VOID_P + 512)
52 
55 #ifdef ENABLE_NSS
56  unsigned int bytes_used;
57 #endif
58  uint8_t mem[DIGEST_CHECKPOINT_BYTES];
60 
69 typedef struct {
70  char d[N_COMMON_DIGEST_ALGORITHMS][DIGEST256_LEN];
72 
73 typedef struct crypto_digest_t crypto_digest_t;
74 typedef struct crypto_xof_t crypto_xof_t;
75 
76 struct smartlist_t;
77 
78 /* SHA-1 and other digests */
79 MOCK_DECL(int, crypto_digest,(char *digest, const char *m, size_t len));
80 int crypto_digest256(char *digest, const char *m, size_t len,
81  digest_algorithm_t algorithm);
82 int crypto_digest512(char *digest, const char *m, size_t len,
83  digest_algorithm_t algorithm);
84 int crypto_common_digests(common_digests_t *ds_out, const char *m, size_t len);
85 void crypto_digest_smartlist_prefix(char *digest_out, size_t len_out,
86  const char *prepend,
87  const struct smartlist_t *lst,
88  const char *append,
89  digest_algorithm_t alg);
90 void crypto_digest_smartlist(char *digest_out, size_t len_out,
91  const struct smartlist_t *lst, const char *append,
92  digest_algorithm_t alg);
93 const char *crypto_digest_algorithm_get_name(digest_algorithm_t alg);
94 size_t crypto_digest_algorithm_get_length(digest_algorithm_t alg);
95 int crypto_digest_algorithm_parse_name(const char *name);
97 crypto_digest_t *crypto_digest256_new(digest_algorithm_t algorithm);
98 crypto_digest_t *crypto_digest512_new(digest_algorithm_t algorithm);
100 #define crypto_digest_free(d) \
101  FREE_AND_NULL(crypto_digest_t, crypto_digest_free_, (d))
102 void crypto_digest_add_bytes(crypto_digest_t *digest, const char *data,
103  size_t len);
105  char *out, size_t out_len);
108  const crypto_digest_t *digest);
110  const crypto_digest_checkpoint_t *checkpoint);
112  const crypto_digest_t *from);
113 void crypto_hmac_sha256(char *hmac_out,
114  const char *key, size_t key_len,
115  const char *msg, size_t msg_len);
116 void crypto_mac_sha3_256(uint8_t *mac_out, size_t len_out,
117  const uint8_t *key, size_t key_len,
118  const uint8_t *msg, size_t msg_len);
119 
120 /* xof functions*/
122 void crypto_xof_add_bytes(crypto_xof_t *xof, const uint8_t *data, size_t len);
123 void crypto_xof_squeeze_bytes(crypto_xof_t *xof, uint8_t *out, size_t len);
124 void crypto_xof_free_(crypto_xof_t *xof);
125 #define crypto_xof_free(xof) \
126  FREE_AND_NULL(crypto_xof_t, crypto_xof_free_, (xof))
127 
128 #ifdef TOR_UNIT_TESTS
129 digest_algorithm_t crypto_digest_get_algorithm(crypto_digest_t *digest);
130 #endif
131 
132 #endif /* !defined(TOR_CRYPTO_DIGEST_H) */
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)
crypto_digest_t * crypto_digest_dup(const crypto_digest_t *digest)
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)
void crypto_digest_get_digest(crypto_digest_t *digest, char *out, size_t out_len)
void crypto_digest_smartlist(char *digest_out, size_t len_out, const smartlist_t *lst, const char *append, digest_algorithm_t alg)
void crypto_xof_free_(crypto_xof_t *xof)
void crypto_digest_checkpoint(crypto_digest_checkpoint_t *checkpoint, const crypto_digest_t *digest)
crypto_digest_t * crypto_digest256_new(digest_algorithm_t algorithm)
void crypto_digest_add_bytes(crypto_digest_t *digest, const char *data, size_t len)
Integer definitions used throughout Tor.
Headers for util_malloc.c.
crypto_xof_t * crypto_xof_new(void)
void crypto_digest_free_(crypto_digest_t *digest)
#define DIGEST256_LEN
Definition: digest_sizes.h:23
void crypto_xof_add_bytes(crypto_xof_t *xof, const uint8_t *data, size_t len)
int crypto_digest_algorithm_parse_name(const char *name)
const char * crypto_digest_algorithm_get_name(digest_algorithm_t alg)
struct crypto_digest_checkpoint_t crypto_digest_checkpoint_t
void crypto_hmac_sha256(char *hmac_out, const char *key, size_t key_len, const char *msg, size_t msg_len)
void crypto_digest_restore(crypto_digest_t *digest, const crypto_digest_checkpoint_t *checkpoint)
size_t crypto_digest_algorithm_get_length(digest_algorithm_t alg)
void crypto_xof_squeeze_bytes(crypto_xof_t *xof, uint8_t *out, size_t len)
Macros to implement mocking and selective exposure for the test code.
Definitions for common sizes of cryptographic digests.
#define MOCK_DECL(rv, funcname, arglist)
Definition: testsupport.h:94
int crypto_common_digests(common_digests_t *ds_out, const char *m, size_t len)
void crypto_digest_assign(crypto_digest_t *into, const crypto_digest_t *from)
crypto_digest_t * crypto_digest512_new(digest_algorithm_t algorithm)
int crypto_digest256(char *digest, const char *m, size_t len, digest_algorithm_t algorithm)
crypto_digest_t * crypto_digest_new(void)
int crypto_digest512(char *digest, const char *m, size_t len, digest_algorithm_t algorithm)