From 87ee4d6fa7a29c4daa866f014787d0f8ed6935be Mon Sep 17 00:00:00 2001 From: Logan007 Date: Tue, 1 Sep 2020 22:50:52 +0545 Subject: [PATCH] reworked cc20 --- src/cc20.c | 36 +++++++++++++++++++++++------------- src/edge.c | 1 - tools/benchmark.c | 3 +-- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/cc20.c b/src/cc20.c index 2372db4..8d13ffc 100644 --- a/src/cc20.c +++ b/src/cc20.c @@ -92,11 +92,20 @@ static void chacha20_init_block(cc20_context_t *ctx, const uint8_t nonce[]) { } #define ROL32(x,r) (((x)<<(r))|((x)>>(32-(r)))) -#define CHACHA20_QUARTERROUND(x, a, b, c, d) \ +#define CHACHA20_QUARTERROUND(x, a, b, c, d) \ x[a] += x[b]; x[d] = ROL32(x[d] ^ x[a], 16); \ x[c] += x[d]; x[b] = ROL32(x[b] ^ x[c], 12); \ - x[a] += x[b]; x[d] = ROL32(x[d] ^ x[a], 8); \ - x[c] += x[d]; x[b] = ROL32(x[b] ^ x[c], 7); + x[a] += x[b]; x[d] = ROL32(x[d] ^ x[a], 8); \ + x[c] += x[d]; x[b] = ROL32(x[b] ^ x[c], 7) +#define CHACHA20_DOUBLE_ROUND \ + CHACHA20_QUARTERROUND(ctx->keystream32, 0, 4, 8, 12); \ + CHACHA20_QUARTERROUND(ctx->keystream32, 1, 5, 9, 13); \ + CHACHA20_QUARTERROUND(ctx->keystream32, 2, 6, 10, 14); \ + CHACHA20_QUARTERROUND(ctx->keystream32, 3, 7, 11, 15); \ + CHACHA20_QUARTERROUND(ctx->keystream32, 0, 5, 10, 15); \ + CHACHA20_QUARTERROUND(ctx->keystream32, 1, 6, 11, 12); \ + CHACHA20_QUARTERROUND(ctx->keystream32, 2, 7, 8, 13); \ + CHACHA20_QUARTERROUND(ctx->keystream32, 3, 4, 9, 14) static void chacha20_block_next(cc20_context_t *ctx) { @@ -105,16 +114,17 @@ static void chacha20_block_next(cc20_context_t *ctx) { for(i = 0; i < 16; i++) ctx->keystream32[i] = ctx->state[i]; - for(i = 0; i < 10; i++) { - CHACHA20_QUARTERROUND(ctx->keystream32, 0, 4, 8, 12) - CHACHA20_QUARTERROUND(ctx->keystream32, 1, 5, 9, 13) - CHACHA20_QUARTERROUND(ctx->keystream32, 2, 6, 10, 14) - CHACHA20_QUARTERROUND(ctx->keystream32, 3, 7, 11, 15) - CHACHA20_QUARTERROUND(ctx->keystream32, 0, 5, 10, 15) - CHACHA20_QUARTERROUND(ctx->keystream32, 1, 6, 11, 12) - CHACHA20_QUARTERROUND(ctx->keystream32, 2, 7, 8, 13) - CHACHA20_QUARTERROUND(ctx->keystream32, 3, 4, 9, 14) - } + // 10 double rounds + CHACHA20_DOUBLE_ROUND; + CHACHA20_DOUBLE_ROUND; + CHACHA20_DOUBLE_ROUND; + CHACHA20_DOUBLE_ROUND; + CHACHA20_DOUBLE_ROUND; + CHACHA20_DOUBLE_ROUND; + CHACHA20_DOUBLE_ROUND; + CHACHA20_DOUBLE_ROUND; + CHACHA20_DOUBLE_ROUND; + CHACHA20_DOUBLE_ROUND; for(i = 0; i < 16; i++) ctx->keystream32[i] += ctx->state[i]; diff --git a/src/edge.c b/src/edge.c index 7e5ace5..22c8ff8 100644 --- a/src/edge.c +++ b/src/edge.c @@ -164,7 +164,6 @@ static void help() { printf("-r | Enable packet forwarding through n2n community.\n"); printf("-A1 | Disable payload encryption. Do not use with key (defaulting to Twofish then).\n"); printf("-A2 ... -A5 or -A | Choose a cipher for payload encryption, requires a key: -A2 = Twofish (default),\n"); - printf(" | " printf(" | -A3 or -A (deprecated) = AES, " "-A4 = ChaCha20, " "-A5 = Speck-CTR.\n"); diff --git a/tools/benchmark.c b/tools/benchmark.c index 8b631a4..62c7ca8 100644 --- a/tools/benchmark.c +++ b/tools/benchmark.c @@ -63,7 +63,6 @@ static void parseArgs(int argc, char * argv[]) { int main(int argc, char * argv[]) { uint8_t pktbuf[N2N_PKT_BUF_SIZE]; n2n_trans_op_t transop_null, transop_tf; - n2n_trans_op_t transop_aes_cbc; n2n_trans_op_t transop_aes; n2n_trans_op_t transop_cc20; @@ -80,7 +79,7 @@ int main(int argc, char * argv[]) { /* Init transopts */ n2n_transop_null_init(&conf, &transop_null); n2n_transop_tf_init(&conf, &transop_tf); - n2n_transop_aes_cbc_init(&conf, &transop_aes_cbc); + n2n_transop_aes_init(&conf, &transop_aes); n2n_transop_aes_init(&conf, &transop_aes); n2n_transop_cc20_init(&conf, &transop_cc20); n2n_transop_speck_init(&conf, &transop_speck);