n2n/src/transform_cc20.c

171 lines
4.8 KiB
C
Raw Normal View History

2020-04-26 15:46:41 +02:00
/**
2021-02-08 11:26:06 +01:00
* (C) 2007-21 - ntop.org and contributors
2020-04-26 15:46:41 +02:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not see see <http://www.gnu.org/licenses/>
*
*/
2020-09-01 16:24:29 +02:00
2020-04-26 15:46:41 +02:00
#include "n2n.h"
2020-12-19 12:28:33 +01:00
// ChaCha20 plaintext preamble
2020-09-01 11:28:25 +02:00
#define CC20_PREAMBLE_SIZE (CC20_IV_SIZE)
2020-04-26 15:46:41 +02:00
2020-12-19 12:28:33 +01:00
2020-04-26 15:46:41 +02:00
typedef struct transop_cc20 {
2020-12-19 12:28:33 +01:00
cc20_context_t *ctx;
2020-04-26 15:46:41 +02:00
} transop_cc20_t;
2020-12-19 12:28:33 +01:00
static int transop_deinit_cc20 (n2n_trans_op_t *arg) {
2020-09-01 11:28:25 +02:00
2020-12-19 12:28:33 +01:00
transop_cc20_t *priv = (transop_cc20_t *)arg->priv;
2020-04-26 15:46:41 +02:00
2020-12-19 12:28:33 +01:00
if(priv->ctx)
cc20_deinit(priv->ctx);
2020-04-26 15:46:41 +02:00
2020-12-19 12:28:33 +01:00
if(priv)
free(priv);
2020-04-26 15:46:41 +02:00
2020-12-19 12:28:33 +01:00
return 0;
2020-04-26 15:46:41 +02:00
}
2020-12-19 12:28:33 +01:00
// the ChaCha20 packet format consists of
//
// - a 128-bit random iv
// - encrypted payload
//
// [IIII|DDDDDDDDDDDDDDDDDDDDD]
// |<---- encrypted ---->|
//
static int transop_encode_cc20 (n2n_trans_op_t *arg,
uint8_t *outbuf,
size_t out_len,
const uint8_t *inbuf,
size_t in_len,
const uint8_t *peer_mac) {
int len = -1;
transop_cc20_t *priv = (transop_cc20_t *)arg->priv;
if(in_len <= N2N_PKT_BUF_SIZE) {
if((in_len + CC20_PREAMBLE_SIZE) <= out_len) {
size_t idx = 0;
traceEvent(TRACE_DEBUG, "encode_cc20 %lu bytes", in_len);
// full iv sized random value (128 bit)
encode_uint64(outbuf, &idx, n2n_rand());
encode_uint64(outbuf, &idx, n2n_rand());
len = in_len;
cc20_crypt(outbuf + CC20_PREAMBLE_SIZE,
inbuf,
in_len,
outbuf, /* iv */
priv->ctx);
// size of datacarried in UDP
len += CC20_PREAMBLE_SIZE;
} else
traceEvent(TRACE_ERROR, "encode_cc20 outbuf too small.");
2020-04-26 15:46:41 +02:00
} else
2020-12-19 12:28:33 +01:00
traceEvent(TRACE_ERROR, "encode_cc20 inbuf too big to encrypt.");
2020-04-26 15:46:41 +02:00
2020-12-19 12:28:33 +01:00
return len;
2020-04-26 15:46:41 +02:00
}
2020-09-01 11:28:25 +02:00
2020-12-19 12:28:33 +01:00
// see transop_encode_cc20 for packet format
static int transop_decode_cc20 (n2n_trans_op_t *arg,
uint8_t *outbuf,
size_t out_len,
const uint8_t *inbuf,
size_t in_len,
const uint8_t *peer_mac) {
2020-04-26 15:46:41 +02:00
2020-12-19 12:28:33 +01:00
int len = 0;
transop_cc20_t *priv = (transop_cc20_t *)arg->priv;
2020-09-01 11:28:25 +02:00
2020-12-19 12:28:33 +01:00
if(((in_len - CC20_PREAMBLE_SIZE) <= N2N_PKT_BUF_SIZE) /* cipher text fits in assembly */
Basic C Code lint checker and shell checker (#859) * Factor build packages out into a more maintainable list * Create a location for scripts to live * Provide a make target to return the source dir as close as reasonable to the original distributed state * Add a code lint step, checking the coding style * Change test harness as recommended by shellcheck * Ensure we actually have the linter tool installed * Use the correct directory for cmake to run the tests * Adjust for the older uncrustify in the current github ubuntu-latest * Make one file pass the linter * Integrate the lint with the existing test workflow * Add files with minimal changes needed to the linter * Add more files with minimal changes needed to the linter * Dont build binaries if we fail the lint test * Update the phony targets with the lint steps * Ensure the flake8 package is installed in the new lint workflow job * Use the makefile to drive the packages needed to install for linting * No need to add dependancies on lint, just rely on the workflow status to show failure * Update the scripts dir README to reflect current assumptions * Rename and briefly document the indent.sh script * Fix the ignore to ignore the right Makefile * Rename the test_harness script to make it clear it is a shell script * Provide a master lint make target and add a shell script lint tool * Elminate stray tabs * Drop include/auth.h from linter - there are inconsistant results with function definitions when using the current uncrustify rules
2021-10-23 21:36:18 +02:00
&& (in_len >= CC20_PREAMBLE_SIZE)) { /* has at least iv */
2020-09-01 11:28:25 +02:00
2020-12-19 12:28:33 +01:00
traceEvent(TRACE_DEBUG, "decode_cc20 %lu bytes", in_len);
2020-09-01 11:28:25 +02:00
2020-12-19 12:28:33 +01:00
len = (in_len - CC20_PREAMBLE_SIZE);
2020-09-01 11:28:25 +02:00
2020-12-19 12:28:33 +01:00
cc20_crypt(outbuf,
inbuf + CC20_PREAMBLE_SIZE,
in_len,
inbuf, /* iv */
priv->ctx);
} else
traceEvent(TRACE_ERROR, "decode_cc20 inbuf wrong size (%ul) to decrypt.", in_len);
2020-04-26 15:46:41 +02:00
2020-12-19 12:28:33 +01:00
return len;
2020-04-26 15:46:41 +02:00
}
2020-12-19 12:28:33 +01:00
static int setup_cc20_key (transop_cc20_t *priv, const uint8_t *password, ssize_t password_len) {
2020-04-26 15:46:41 +02:00
2020-12-19 12:28:33 +01:00
uint8_t key_mat[CC20_KEY_BYTES];
2020-04-26 15:46:41 +02:00
2020-12-19 12:28:33 +01:00
// the input key always gets hashed to make a more unpredictable and more complete use of the key space
pearson_hash_256(key_mat, password, password_len);
2020-09-01 11:28:25 +02:00
2020-12-19 12:28:33 +01:00
if(cc20_init(key_mat, &(priv->ctx))) {
traceEvent(TRACE_ERROR, "setup_cc20_key setup unsuccessful");
return -1;
}
2020-04-26 15:46:41 +02:00
2020-12-19 12:28:33 +01:00
traceEvent(TRACE_DEBUG, "setup_cc20_key completed");
2020-04-26 15:46:41 +02:00
2020-12-19 12:28:33 +01:00
return 0;
2020-04-26 15:46:41 +02:00
}
2020-12-19 12:28:33 +01:00
static void transop_tick_cc20 (n2n_trans_op_t *arg, time_t now) {
// no tick action
}
2020-04-26 15:46:41 +02:00
2020-12-19 12:28:33 +01:00
// ChaCha20 initialization function
int n2n_transop_cc20_init (const n2n_edge_conf_t *conf, n2n_trans_op_t *ttt) {
2020-09-01 11:28:25 +02:00
2020-12-19 12:28:33 +01:00
transop_cc20_t *priv;
const u_char *encrypt_key = (const u_char *)conf->encrypt_key;
size_t encrypt_key_len = strlen(conf->encrypt_key);
2020-04-26 15:46:41 +02:00
2020-12-19 12:28:33 +01:00
memset(ttt, 0, sizeof(*ttt));
ttt->transform_id = N2N_TRANSFORM_ID_CHACHA20;
2020-04-26 15:46:41 +02:00
2020-12-19 12:28:33 +01:00
ttt->tick = transop_tick_cc20;
ttt->deinit = transop_deinit_cc20;
ttt->fwd = transop_encode_cc20;
ttt->rev = transop_decode_cc20;
2020-04-26 15:46:41 +02:00
2020-12-19 12:28:33 +01:00
priv = (transop_cc20_t*)calloc(1, sizeof(transop_cc20_t));
if(!priv) {
traceEvent(TRACE_ERROR, "cannot allocate transop_cc20_t memory");
return -1;
}
ttt->priv = priv;
2020-04-26 15:46:41 +02:00
2020-12-19 12:28:33 +01:00
// setup the cipher and key
return setup_cc20_key(priv, encrypt_key, encrypt_key_len);
2020-04-26 15:46:41 +02:00
}