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-2021, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
8 * \file crypto_digest.c
9 * \brief Block of functions related with digest and xof utilities and
13 #include "lib/container/smartlist.h"
14 #include "lib/crypt_ops/crypto_digest.h"
15 #include "lib/crypt_ops/crypto_util.h"
16 #include "lib/log/log.h"
17 #include "lib/log/util_bug.h"
19 #include "keccak-tiny/keccak-tiny.h"
24 #include "lib/arch/bytes.h"
26 /** Set the common_digests_t in <b>ds_out</b> to contain every digest on the
27 * <b>len</b> bytes in <b>m</b> that we know how to compute. Return 0 on
28 * success, -1 on failure. */
30 crypto_common_digests(common_digests_t
*ds_out
, const char *m
, size_t len
)
33 memset(ds_out
, 0, sizeof(*ds_out
));
34 if (crypto_digest(ds_out
->d
[DIGEST_SHA1
], m
, len
) < 0)
36 if (crypto_digest256(ds_out
->d
[DIGEST_SHA256
], m
, len
, DIGEST_SHA256
) < 0)
42 /** Return the name of an algorithm, as used in directory documents. */
44 crypto_digest_algorithm_get_name(digest_algorithm_t alg
)
60 return "??unknown_digest??";
65 /** Given the name of a digest algorithm, return its integer value, or -1 if
66 * the name is not recognized. */
68 crypto_digest_algorithm_parse_name(const char *name
)
70 if (!strcmp(name
, "sha1"))
72 else if (!strcmp(name
, "sha256"))
74 else if (!strcmp(name
, "sha512"))
76 else if (!strcmp(name
, "sha3-256"))
77 return DIGEST_SHA3_256
;
78 else if (!strcmp(name
, "sha3-512"))
79 return DIGEST_SHA3_512
;
84 /** Given an algorithm, return the digest length in bytes. */
86 crypto_digest_algorithm_get_length(digest_algorithm_t alg
)
100 tor_assert(0); // LCOV_EXCL_LINE
101 return 0; /* Unreachable */ // LCOV_EXCL_LINE
105 /** Compute a MAC using SHA3-256 of <b>msg_len</b> bytes in <b>msg</b> using a
106 * <b>key</b> of length <b>key_len</b> and a <b>salt</b> of length
107 * <b>salt_len</b>. Store the result of <b>len_out</b> bytes in in
108 * <b>mac_out</b>. This function can't fail. */
110 crypto_mac_sha3_256(uint8_t *mac_out
, size_t len_out
,
111 const uint8_t *key
, size_t key_len
,
112 const uint8_t *msg
, size_t msg_len
)
114 crypto_digest_t
*digest
;
116 const uint64_t key_len_netorder
= tor_htonll(key_len
);
122 digest
= crypto_digest256_new(DIGEST_SHA3_256
);
124 /* Order matters here that is any subsystem using this function should
125 * expect this very precise ordering in the MAC construction. */
126 crypto_digest_add_bytes(digest
, (const char *) &key_len_netorder
,
127 sizeof(key_len_netorder
));
128 crypto_digest_add_bytes(digest
, (const char *) key
, key_len
);
129 crypto_digest_add_bytes(digest
, (const char *) msg
, msg_len
);
130 crypto_digest_get_digest(digest
, (char *) mac_out
, len_out
);
131 crypto_digest_free(digest
);
136 /** Internal state for a eXtendable-Output Function (XOF). */
137 struct crypto_xof_t
{
138 #ifdef OPENSSL_HAS_SHAKE3_EVP
139 /* XXXX We can't enable this yet, because OpenSSL's
140 * DigestFinalXOF function can't be called repeatedly on the same
143 * We could in theory use the undocumented SHA3_absorb and SHA3_squeeze
144 * functions, but let's not mess with undocumented OpenSSL internals any
145 * more than we have to.
147 * We could also revise our XOF code so that it only allows a single
148 * squeeze operation; we don't require streaming squeeze operations
149 * outside the tests yet.
152 #else /* !defined(OPENSSL_HAS_SHAKE3_EVP) */
154 * State of the Keccak sponge for the SHAKE-256 computation.
157 #endif /* defined(OPENSSL_HAS_SHAKE3_EVP) */
160 /** Allocate a new XOF object backed by SHAKE-256. The security level
161 * provided is a function of the length of the output used. Read and
162 * understand FIPS-202 A.2 "Additional Consideration for Extendable-Output
163 * Functions" before using this construct.
169 xof
= tor_malloc(sizeof(crypto_xof_t
));
170 #ifdef OPENSSL_HAS_SHAKE256
171 xof
->ctx
= EVP_MD_CTX_new();
172 tor_assert(xof
->ctx
);
173 int r
= EVP_DigestInit(xof
->ctx
, EVP_shake256());
175 #else /* !defined(OPENSSL_HAS_SHAKE256) */
176 keccak_xof_init(&xof
->s
, 256);
177 #endif /* defined(OPENSSL_HAS_SHAKE256) */
181 /** Absorb bytes into a XOF object. Must not be called after a call to
182 * crypto_xof_squeeze_bytes() for the same instance, and will assert
186 crypto_xof_add_bytes(crypto_xof_t
*xof
, const uint8_t *data
, size_t len
)
188 #ifdef OPENSSL_HAS_SHAKE256
189 int r
= EVP_DigestUpdate(xof
->ctx
, data
, len
);
192 int i
= keccak_xof_absorb(&xof
->s
, data
, len
);
194 #endif /* defined(OPENSSL_HAS_SHAKE256) */
197 /** Squeeze bytes out of a XOF object. Calling this routine will render
198 * the XOF instance ineligible to absorb further data.
201 crypto_xof_squeeze_bytes(crypto_xof_t
*xof
, uint8_t *out
, size_t len
)
203 #ifdef OPENSSL_HAS_SHAKE256
204 int r
= EVP_DigestFinalXOF(xof
->ctx
, out
, len
);
207 int i
= keccak_xof_squeeze(&xof
->s
, out
, len
);
209 #endif /* defined(OPENSSL_HAS_SHAKE256) */
212 /** Cleanse and deallocate a XOF object. */
214 crypto_xof_free_(crypto_xof_t
*xof
)
218 #ifdef OPENSSL_HAS_SHAKE256
220 EVP_MD_CTX_free(xof
->ctx
);
222 memwipe(xof
, 0, sizeof(crypto_xof_t
));
226 /** Compute the XOF (SHAKE256) of a <b>input_len</b> bytes at <b>input</b>,
227 * putting <b>output_len</b> bytes at <b>output</b>. */
229 crypto_xof(uint8_t *output
, size_t output_len
,
230 const uint8_t *input
, size_t input_len
)
232 #ifdef OPENSSL_HAS_SHA3
233 EVP_MD_CTX
*ctx
= EVP_MD_CTX_new();
235 int r
= EVP_DigestInit(ctx
, EVP_shake256());
237 r
= EVP_DigestUpdate(ctx
, input
, input_len
);
239 r
= EVP_DigestFinalXOF(ctx
, output
, output_len
);
241 EVP_MD_CTX_free(ctx
);
242 #else /* !defined(OPENSSL_HAS_SHA3) */
243 crypto_xof_t
*xof
= crypto_xof_new();
244 crypto_xof_add_bytes(xof
, input
, input_len
);
245 crypto_xof_squeeze_bytes(xof
, output
, output_len
);
246 crypto_xof_free(xof
);
247 #endif /* defined(OPENSSL_HAS_SHA3) */