diff options
author | Nick Mathewson <nickm@torproject.org> | 2018-02-15 20:53:50 -0500 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2018-02-15 20:53:50 -0500 |
commit | 8da6bfa5dedbbb03d12bb81974a265feb88122c1 (patch) | |
tree | 11bd748e1c2d938929c07652ef063503535c57ee /src/common/crypto.c | |
parent | 4d994e7a9c8936c9e33df90b7468e7327f1794e9 (diff) | |
parent | 91c63aae8497bc9de6533daae8f927ca09f96fd2 (diff) | |
download | tor-8da6bfa5dedbbb03d12bb81974a265feb88122c1.tar.gz tor-8da6bfa5dedbbb03d12bb81974a265feb88122c1.zip |
Merge branch 'bug24914'
Diffstat (limited to 'src/common/crypto.c')
-rw-r--r-- | src/common/crypto.c | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/common/crypto.c b/src/common/crypto.c index d85aca4004..69faa0f63d 100644 --- a/src/common/crypto.c +++ b/src/common/crypto.c @@ -970,6 +970,7 @@ crypto_digest_alloc_bytes(digest_algorithm_t alg) /* Gives the length of crypto_digest_t through the end of the field 'd' */ #define END_OF_FIELD(f) (offsetof(crypto_digest_t, f) + \ STRUCT_FIELD_SIZE(crypto_digest_t, f)) + switch (alg) { case DIGEST_SHA1: return END_OF_FIELD(d.sha1); @@ -1155,6 +1156,33 @@ crypto_digest_dup(const crypto_digest_t *digest) return tor_memdup(digest, alloc_bytes); } +/** Temporarily save the state of <b>digest</b> in <b>checkpoint</b>. + * Asserts that <b>digest</b> is a SHA1 digest object. + */ +void +crypto_digest_checkpoint(crypto_digest_checkpoint_t *checkpoint, + const crypto_digest_t *digest) +{ + tor_assert(digest->algorithm == DIGEST_SHA1); + /* The optimizer should turn this into a constant... */ + const size_t bytes = crypto_digest_alloc_bytes(DIGEST_SHA1); + /* ... and remove this assertion entirely. */ + tor_assert(bytes <= sizeof(checkpoint->mem)); + memcpy(checkpoint->mem, digest, bytes); +} + +/** Restore the state of <b>digest</b> from <b>checkpoint</b>. + * Asserts that <b>digest</b> is a SHA1 digest object. Requires that the + * state was previously stored with crypto_digest_checkpoint() */ +void +crypto_digest_restore(crypto_digest_t *digest, + const crypto_digest_checkpoint_t *checkpoint) +{ + tor_assert(digest->algorithm == DIGEST_SHA1); + const size_t bytes = crypto_digest_alloc_bytes(DIGEST_SHA1); + memcpy(digest, checkpoint->mem, bytes); +} + /** Replace the state of the digest object <b>into</b> with the state * of the digest object <b>from</b>. Requires that 'into' and 'from' * have the same digest type. |