From eecc44dab8ad98246b2c4dbedf977113f1874f77 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Thu, 7 Feb 2008 16:10:33 +0000 Subject: r17963@catbus: nickm | 2008-02-07 10:14:25 -0500 Be more thorough about memory poisoning and clearing. Add an in-place version of aes_crypt in order to remove a memcpy from relay_crypt_one_payload. svn:r13414 --- src/common/aes.c | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) (limited to 'src/common/aes.c') diff --git a/src/common/aes.c b/src/common/aes.c index 6dc4896988..cd8317dd11 100644 --- a/src/common/aes.c +++ b/src/common/aes.c @@ -289,7 +289,7 @@ aes_crypt(aes_cnt_cipher_t *cipher, const char *input, size_t len, * of alignmement, using a bigger buffer, and so on. Not till after 0.1.2.x, * though. */ int c = cipher->pos; - if (!len) return; + if (PREDICT_UNLIKELY(!len)) return; while (1) { do { @@ -312,6 +312,42 @@ aes_crypt(aes_cnt_cipher_t *cipher, const char *input, size_t len, } } +/** Encrypt len bytes from input, storing the results in place. + * Uses the key in cipher, and advances the counter by len bytes + * as it encrypts. + */ +void +aes_crypt_inplace(aes_cnt_cipher_t *cipher, char *data, size_t len) +{ + + /* XXXX This function is up to 5% of our runtime in some profiles; + * we should look into unrolling some of the loops; taking advantage + * of alignmement, using a bigger buffer, and so on. Not till after 0.1.2.x, + * though. */ + int c = cipher->pos; + if (PREDICT_UNLIKELY(!len)) return; + + while (1) { + do { + if (len-- == 0) { cipher->pos = c; return; } + *(data++) ^= cipher->buf[c]; + } while (++c != 16); + cipher->pos = c = 0; + if (PREDICT_UNLIKELY(! ++COUNTER(cipher, 0))) { + if (PREDICT_UNLIKELY(! ++COUNTER(cipher, 1))) { + if (PREDICT_UNLIKELY(! ++COUNTER(cipher, 2))) { + ++COUNTER(cipher, 3); + UPDATE_CTR_BUF(cipher, 3); + } + UPDATE_CTR_BUF(cipher, 2); + } + UPDATE_CTR_BUF(cipher, 1); + } + UPDATE_CTR_BUF(cipher, 0); + _aes_fill_buf(cipher); + } +} + /** Reset the 128-bit counter of cipher to the 16-bit big-endian value * in iv. */ void -- cgit v1.2.3-54-g00ecf