mirror of
https://github.com/shadowsocks/shadowsocks-libev.git
synced 2026-11-23 22:43:48 +00:00
Replace nonce cache with a ping-pong bloom filter (#1282)
* Add Ping-Pong bloom filter * Refine bloom filter insertion * Reduce the error rate to 0.00001 * Avoid alignment issue in murmurhash2 * Fix a memory leak * Fix build on non-GPU targets * Detect get_current_dir_name in configure * Update README.md * Remove redudant bfree() * Reduce the memory usage for local client * Fix #1275 * Refine #1275 * Use IP when bypassing SNI domains * Also apply replay detector on UDP traffic * Update deb build script Now build script is able to auto detect system and choose libraries necessary to build. Also update the README accordingly. * Update build script to enable jessie/stretch etc Also include a few cleanup that simplified pkg installation from backports repository.
This commit is contained in:
+22
-20
@@ -33,7 +33,7 @@
|
||||
#include <sodium.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include "cache.h"
|
||||
#include "ppbloom.h"
|
||||
#include "aead.h"
|
||||
#include "utils.h"
|
||||
|
||||
@@ -405,13 +405,11 @@ aead_encrypt_all(buffer_t *plaintext, cipher_t *cipher, size_t capacity)
|
||||
(uint8_t *)plaintext->data, plaintext->len,
|
||||
NULL, 0, cipher_ctx.nonce, cipher_ctx.skey);
|
||||
|
||||
if (err) {
|
||||
bfree(plaintext);
|
||||
aead_ctx_release(&cipher_ctx);
|
||||
return CRYPTO_ERROR;
|
||||
}
|
||||
|
||||
aead_ctx_release(&cipher_ctx);
|
||||
|
||||
if (err)
|
||||
return CRYPTO_ERROR;
|
||||
|
||||
assert(ciphertext->len == clen);
|
||||
|
||||
brealloc(plaintext, salt_len + ciphertext->len, capacity);
|
||||
@@ -444,6 +442,11 @@ aead_decrypt_all(buffer_t *ciphertext, cipher_t *cipher, size_t capacity)
|
||||
uint8_t *salt = cipher_ctx.salt;
|
||||
memcpy(salt, ciphertext->data, salt_len);
|
||||
|
||||
if (ppbloom_check((void *)salt, salt_len) == 1) {
|
||||
LOGE("crypto: AEAD: repeat salt detected");
|
||||
return CRYPTO_ERROR;
|
||||
}
|
||||
|
||||
aead_cipher_ctx_set_key(&cipher_ctx, 0);
|
||||
|
||||
size_t plen = plaintext->len;
|
||||
@@ -453,14 +456,13 @@ aead_decrypt_all(buffer_t *ciphertext, cipher_t *cipher, size_t capacity)
|
||||
ciphertext->len - salt_len, NULL, 0,
|
||||
cipher_ctx.nonce, cipher_ctx.skey);
|
||||
|
||||
if (err) {
|
||||
bfree(ciphertext);
|
||||
aead_ctx_release(&cipher_ctx);
|
||||
return CRYPTO_ERROR;
|
||||
}
|
||||
|
||||
aead_ctx_release(&cipher_ctx);
|
||||
|
||||
if (err)
|
||||
return CRYPTO_ERROR;
|
||||
|
||||
ppbloom_add((void *)salt, salt_len);
|
||||
|
||||
brealloc(ciphertext, plaintext->len, capacity);
|
||||
memcpy(ciphertext->data, plaintext->data, plaintext->len);
|
||||
ciphertext->len = plaintext->len;
|
||||
@@ -488,6 +490,7 @@ aead_chunk_encrypt(cipher_ctx_t *ctx, uint8_t *p, uint8_t *c,
|
||||
NULL, 0, n, ctx->skey);
|
||||
if (err)
|
||||
return CRYPTO_ERROR;
|
||||
|
||||
assert(clen == CHUNK_SIZE_LEN + tlen);
|
||||
|
||||
sodium_increment(n, nlen);
|
||||
@@ -497,6 +500,7 @@ aead_chunk_encrypt(cipher_ctx_t *ctx, uint8_t *p, uint8_t *c,
|
||||
NULL, 0, n, ctx->skey);
|
||||
if (err)
|
||||
return CRYPTO_ERROR;
|
||||
|
||||
assert(clen == plen + tlen);
|
||||
|
||||
sodium_increment(n, nlen);
|
||||
@@ -634,12 +638,9 @@ aead_decrypt(buffer_t *ciphertext, cipher_ctx_t *cipher_ctx, size_t capacity)
|
||||
|
||||
aead_cipher_ctx_set_key(cipher_ctx, 0);
|
||||
|
||||
if (cache_key_exist(nonce_cache, (char *)cipher_ctx->salt, salt_len)) {
|
||||
if (ppbloom_check((void *)cipher_ctx->salt, salt_len) == 1) {
|
||||
LOGE("crypto: AEAD: repeat salt detected");
|
||||
bfree(ciphertext);
|
||||
return CRYPTO_ERROR;
|
||||
} else {
|
||||
cache_insert(nonce_cache, (char *)cipher_ctx->salt, salt_len, NULL);
|
||||
}
|
||||
|
||||
memmove(cipher_ctx->chunk->data, cipher_ctx->chunk->data + salt_len,
|
||||
@@ -647,6 +648,10 @@ aead_decrypt(buffer_t *ciphertext, cipher_ctx_t *cipher_ctx, size_t capacity)
|
||||
cipher_ctx->chunk->len -= salt_len;
|
||||
|
||||
cipher_ctx->init = 1;
|
||||
|
||||
} else if (cipher_ctx->init == 1) {
|
||||
ppbloom_add((void *)cipher_ctx->salt, salt_len);
|
||||
cipher_ctx->init = 2;
|
||||
}
|
||||
|
||||
size_t plen = 0;
|
||||
@@ -685,9 +690,6 @@ aead_key_init(int method, const char *pass, const char *key)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Initialize cache
|
||||
cache_create(&nonce_cache, 1024, NULL);
|
||||
|
||||
cipher_t *cipher = (cipher_t *)ss_malloc(sizeof(cipher_t));
|
||||
memset(cipher, 0, sizeof(cipher_t));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user