2020-05-27 23:32:21 +02:00
|
|
|
/**
|
|
|
|
* (C) 2007-20 - ntop.org and contributors
|
|
|
|
*
|
|
|
|
* 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-08-29 17:08:37 +02:00
|
|
|
#include "n2n.h"
|
2020-05-27 23:32:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* Speck plaintext preamble */
|
2020-08-29 17:08:37 +02:00
|
|
|
#define TRANSOP_SPECK_PREAMBLE_SIZE (N2N_SPECK_IVEC_SIZE)
|
2020-05-27 23:32:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
typedef struct transop_speck {
|
2020-08-29 17:08:37 +02:00
|
|
|
speck_context_t *ctx; /* the round keys for payload encryption & decryption */
|
2020-05-27 23:32:21 +02:00
|
|
|
} transop_speck_t;
|
|
|
|
|
|
|
|
/* ****************************************************** */
|
|
|
|
|
|
|
|
static int transop_deinit_speck(n2n_trans_op_t *arg) {
|
|
|
|
transop_speck_t *priv = (transop_speck_t *)arg->priv;
|
|
|
|
|
2020-08-29 17:08:37 +02:00
|
|
|
if(priv->ctx) speck_deinit(priv->ctx);
|
2020-05-27 23:32:21 +02:00
|
|
|
|
2020-08-29 17:08:37 +02:00
|
|
|
if(priv) free (priv);
|
2020-05-27 23:32:21 +02:00
|
|
|
|
2020-08-29 17:08:37 +02:00
|
|
|
return 0;
|
2020-05-27 23:32:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ****************************************************** */
|
|
|
|
|
|
|
|
/** The Speck packet format consists of:
|
|
|
|
*
|
|
|
|
* - a 128-bit random IV
|
|
|
|
* - encrypted payload.
|
|
|
|
*
|
2020-08-15 17:29:24 +02:00
|
|
|
* [IIII|DDDDDDDDDDDDDDDDDDDDD]
|
|
|
|
* |<---- encrypted ---->|
|
2020-05-27 23:32:21 +02:00
|
|
|
*/
|
|
|
|
static int transop_encode_speck(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-08-29 17:08:37 +02:00
|
|
|
|
2020-05-27 23:32:21 +02:00
|
|
|
int len=-1;
|
|
|
|
transop_speck_t * priv = (transop_speck_t *)arg->priv;
|
|
|
|
|
|
|
|
if(in_len <= N2N_PKT_BUF_SIZE) {
|
|
|
|
if((in_len + TRANSOP_SPECK_PREAMBLE_SIZE) <= out_len) {
|
|
|
|
size_t idx=0;
|
|
|
|
|
|
|
|
traceEvent(TRACE_DEBUG, "encode_speck %lu bytes", in_len);
|
|
|
|
|
|
|
|
/* Generate and encode the IV. */
|
2020-08-29 17:08:37 +02:00
|
|
|
encode_uint64(outbuf, &idx, n2n_rand());
|
|
|
|
encode_uint64(outbuf, &idx, n2n_rand());
|
2020-05-27 23:32:21 +02:00
|
|
|
|
|
|
|
/* Encrypt the payload and write the ciphertext after the iv. */
|
|
|
|
/* len is set to the length of the cipher plain text to be encrpyted
|
|
|
|
which is (in this case) identical to original packet lentgh */
|
|
|
|
len = in_len;
|
2020-08-29 17:08:37 +02:00
|
|
|
speck_ctr (outbuf + TRANSOP_SPECK_PREAMBLE_SIZE, // output starts right after the iv
|
|
|
|
inbuf, // input
|
|
|
|
in_len, // len
|
|
|
|
outbuf, // iv (already encoded in outbuf, speck does not change it)
|
|
|
|
priv->ctx); // ctx already setup with round keys
|
2020-05-27 23:32:21 +02:00
|
|
|
|
|
|
|
traceEvent(TRACE_DEBUG, "encode_speck: encrypted %u bytes.\n", in_len);
|
|
|
|
|
|
|
|
len += TRANSOP_SPECK_PREAMBLE_SIZE; /* size of data carried in UDP. */
|
|
|
|
} else
|
|
|
|
traceEvent(TRACE_ERROR, "encode_speck outbuf too small.");
|
|
|
|
} else
|
|
|
|
traceEvent(TRACE_ERROR, "encode_speck inbuf too big to encrypt.");
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ****************************************************** */
|
|
|
|
|
|
|
|
/* See transop_encode_speck for packet format */
|
|
|
|
static int transop_decode_speck(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-08-29 17:08:37 +02:00
|
|
|
|
2020-05-27 23:32:21 +02:00
|
|
|
int len=0;
|
|
|
|
transop_speck_t * priv = (transop_speck_t *)arg->priv;
|
|
|
|
|
|
|
|
if(((in_len - TRANSOP_SPECK_PREAMBLE_SIZE) <= N2N_PKT_BUF_SIZE) /* Cipher text fits in buffer */
|
2020-08-15 17:29:24 +02:00
|
|
|
&& (in_len >= TRANSOP_SPECK_PREAMBLE_SIZE) /* Has at least iv */
|
2020-05-27 23:32:21 +02:00
|
|
|
)
|
|
|
|
{
|
|
|
|
size_t rem=in_len;
|
|
|
|
size_t idx=0;
|
|
|
|
|
|
|
|
traceEvent(TRACE_DEBUG, "decode_speck %lu bytes", in_len);
|
|
|
|
|
2020-08-29 17:08:37 +02:00
|
|
|
len = (in_len - TRANSOP_SPECK_PREAMBLE_SIZE);
|
|
|
|
speck_ctr (outbuf, // output
|
|
|
|
inbuf + TRANSOP_SPECK_PREAMBLE_SIZE, // encrypted data starts right after preamble (IV)
|
|
|
|
len, // len
|
|
|
|
inbuf, // IV can be found at input's beginning
|
|
|
|
priv->ctx); // ctx already setup with round keys
|
2020-05-27 23:32:21 +02:00
|
|
|
|
2020-08-29 17:08:37 +02:00
|
|
|
traceEvent(TRACE_DEBUG, "decode_speck decrypted %u bytes.\n", len);
|
2020-05-27 23:32:21 +02:00
|
|
|
|
|
|
|
} else
|
|
|
|
traceEvent(TRACE_ERROR, "decode_speck inbuf wrong size (%ul) to decrypt.", in_len);
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ****************************************************** */
|
|
|
|
|
|
|
|
static int setup_speck_key(transop_speck_t *priv, const uint8_t *key, ssize_t key_size) {
|
|
|
|
|
2020-08-29 17:08:37 +02:00
|
|
|
uint8_t key_mat_buf[32];
|
2020-05-27 23:32:21 +02:00
|
|
|
|
2020-06-13 15:40:39 +02:00
|
|
|
/* the input key always gets hashed to make a more unpredictable and more complete use of the key space */
|
2020-08-29 17:08:37 +02:00
|
|
|
pearson_hash_256(key_mat_buf, key, key_size);
|
2020-06-13 15:40:39 +02:00
|
|
|
|
|
|
|
/* expand the key material to the context (= round keys) */
|
2020-08-29 17:08:37 +02:00
|
|
|
speck_init(key_mat_buf, &(priv->ctx));
|
2020-06-04 21:58:57 +02:00
|
|
|
|
2020-08-29 17:08:37 +02:00
|
|
|
traceEvent(TRACE_DEBUG, "setup_speck_key completed\n");
|
2020-05-27 23:32:21 +02:00
|
|
|
|
2020-08-29 17:08:37 +02:00
|
|
|
return 0;
|
2020-05-27 23:32:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ****************************************************** */
|
|
|
|
|
|
|
|
static void transop_tick_speck(n2n_trans_op_t * arg, time_t now) { ; }
|
|
|
|
|
|
|
|
/* ****************************************************** */
|
2020-08-29 17:08:37 +02:00
|
|
|
|
2020-05-27 23:32:21 +02:00
|
|
|
/* Speck initialization function */
|
|
|
|
int n2n_transop_speck_init(const n2n_edge_conf_t *conf, n2n_trans_op_t *ttt) {
|
2020-08-29 17:08:37 +02:00
|
|
|
|
2020-05-27 23:32:21 +02:00
|
|
|
transop_speck_t *priv;
|
|
|
|
const u_char *encrypt_key = (const u_char *)conf->encrypt_key;
|
|
|
|
size_t encrypt_key_len = strlen(conf->encrypt_key);
|
|
|
|
|
|
|
|
memset(ttt, 0, sizeof(*ttt));
|
|
|
|
ttt->transform_id = N2N_TRANSFORM_ID_SPECK;
|
|
|
|
|
|
|
|
ttt->tick = transop_tick_speck;
|
|
|
|
ttt->deinit = transop_deinit_speck;
|
|
|
|
ttt->fwd = transop_encode_speck;
|
|
|
|
ttt->rev = transop_decode_speck;
|
2020-08-29 17:08:37 +02:00
|
|
|
|
|
|
|
priv = (transop_speck_t*) calloc(1, sizeof(transop_speck_t));
|
2020-05-27 23:32:21 +02:00
|
|
|
if(!priv) {
|
2020-08-29 17:08:37 +02:00
|
|
|
traceEvent(TRACE_ERROR, "n2n_transop_speck_init cannot allocate transop_speck_t memory");
|
2020-05-27 23:32:21 +02:00
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
ttt->priv = priv;
|
|
|
|
|
|
|
|
/* Setup the cipher and key */
|
2020-08-29 17:08:37 +02:00
|
|
|
return setup_speck_key(priv, encrypt_key, encrypt_key_len);
|
2020-05-27 23:32:21 +02:00
|
|
|
}
|