diff --git a/lib/libDtChat/Makefile.am b/lib/libDtChat/Makefile.am index b3fe2d7b..92b13709 100644 --- a/lib/libDtChat/Makefile.am +++ b/lib/libDtChat/Makefile.am @@ -12,7 +12,7 @@ libDtChatIpc_la_LDFLAGS = -version-info 1:0:0 libDtChat_la_CPPFLAGS = -I$(top_srcdir)/include libDtChat_la_SOURCES = core.c loop.c store.c conv.c text.c adapter.c instance.c \ irc.c identity.c peers.c local.c filexfer.c pair.c i18n.c media_audio.c \ - dc_internal.h + media_packet.c dc_internal.h libDtChat_la_LIBADD = libDtChatIpc.la $(CRYPTOLIB) $(LIBTLS) $(OPUSLIB) libDtChat_la_LDFLAGS = -version-info 1:0:0 @@ -20,7 +20,8 @@ libDtChat_la_LDFLAGS = -version-info 1:0:0 # build/mcde-host; make check still builds and runs them. noinst_PROGRAMS = test_json test_frame test_loop test_store test_conv test_text test_irc \ test_identity test_peers test_local test_filexfer test_pair test_fuzz \ - test_i18n test_audio_opus test_adapter test_instance fake_adapter + test_i18n test_audio_opus test_media_packet test_adapter test_instance \ + fake_adapter test_json_SOURCES = test_json.c test_json_CPPFLAGS = -I$(top_srcdir)/include test_json_LDADD = libDtChatIpc.la @@ -76,6 +77,9 @@ test_i18n_LDADD = libDtChat.la libDtChatIpc.la test_audio_opus_SOURCES = test_audio_opus.c test_audio_opus_CPPFLAGS = -I$(top_srcdir)/include -I$(srcdir) test_audio_opus_LDADD = libDtChat.la libDtChatIpc.la $(OPUSLIB) -lm +test_media_packet_SOURCES = test_media_packet.c +test_media_packet_CPPFLAGS = -I$(top_srcdir)/include -I$(srcdir) +test_media_packet_LDADD = libDtChat.la libDtChatIpc.la $(CRYPTOLIB) TESTS = test_json test_frame test_loop test_store test_conv test_text test_irc \ test_identity test_peers test_local test_filexfer test_pair test_fuzz \ - test_i18n test_audio_opus test_adapter test_instance + test_i18n test_audio_opus test_media_packet test_adapter test_instance diff --git a/lib/libDtChat/dc_internal.h b/lib/libDtChat/dc_internal.h index 95782def..8d0427ee 100644 --- a/lib/libDtChat/dc_internal.h +++ b/lib/libDtChat/dc_internal.h @@ -352,6 +352,62 @@ int dc_opus_dec_frame(DcOpusDec *dec, const unsigned char *packet, size_t len, int16_t *pcm); void dc_opus_dec_free(DcOpusDec *dec); +/* + * Media packets (T6.3). X25519 + HKDF-SHA256 give two 32-byte keys per + * session (send and recv, mirrored by the two sides); ChaCha20-Poly1305 + * seals the payload, the 12-byte header is the additional data. The replay + * window is the 64-packet slider of RFC 3711. + */ +#define DC_MEDIA_VERSION 1 +#define DC_MEDIA_KEY_LEN 32 +#define DC_MEDIA_TAG_LEN 16 +#define DC_MEDIA_MAX_PAYLOAD 1200 + +enum { + DC_MEDIA_AUDIO = 1, + DC_MEDIA_VIDEO = 2, + DC_MEDIA_PING = 3, + DC_MEDIA_BYE = 4 +}; + +typedef struct { + unsigned char send[DC_MEDIA_KEY_LEN]; + unsigned char recv[DC_MEDIA_KEY_LEN]; +} DcMediaKeys; + +typedef struct { + unsigned char key[DC_MEDIA_KEY_LEN]; + uint64_t seq; +} DcMediaSend; + +typedef struct { + unsigned char key[DC_MEDIA_KEY_LEN]; + uint64_t highest; + uint64_t mask; + int started; +} DcMediaRecv; + +/* a new X25519 key pair: public in PUB, the secret in PRIV */ +int dc_media_keypair(unsigned char pub[DC_MEDIA_KEY_LEN], + unsigned char priv[DC_MEDIA_KEY_LEN]); +/* the two directions of a session; INITIATOR 1 on the side that started */ +int dc_media_keys_agree(const unsigned char priv[DC_MEDIA_KEY_LEN], + const unsigned char peer_pub[DC_MEDIA_KEY_LEN], int initiator, + DcMediaKeys *keys); +void dc_media_send_init(DcMediaSend *send, + const unsigned char key[DC_MEDIA_KEY_LEN]); +void dc_media_recv_init(DcMediaRecv *recv, + const unsigned char key[DC_MEDIA_KEY_LEN]); +/* the packet length, or 0 (too small a buffer, bad type, too much data) */ +size_t dc_media_seal(DcMediaSend *send, int type, uint32_t ts, + const void *plain, size_t len, unsigned char *out, size_t outlen); +/* 1 and the payload/type/timestamp, or -1 (changed header or payload, + * wrong key, replay, too short) */ +int dc_media_open(DcMediaRecv *recv, const unsigned char *packet, size_t len, + int *type, uint32_t *ts, void *plain, size_t *plainlen); +/* 1 when SEQ is new and inside the window (it is marked seen), else 0 */ +int dc_media_replay_ok(DcMediaRecv *recv, uint32_t seq); + /* * IRC lines (RFC 1459/2812 + IRCv3 tags). dc_irc_parse() copies the tags, * the prefix, the command, up to 15 parameters and the trailing text; diff --git a/lib/libDtChat/media_packet.c b/lib/libDtChat/media_packet.c new file mode 100644 index 00000000..8c33fb05 --- /dev/null +++ b/lib/libDtChat/media_packet.c @@ -0,0 +1,347 @@ +/* + * media_packet.c -- dtchat's media packets: X25519, HKDF, ChaCha20-Poly1305 + * + * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. + * + * One session derives two 32-byte keys from an X25519 exchange and HKDF- + * SHA256; the initiator sends with the first and receives with the second, + * the responder the other way round. A packet is a 12-byte header in the + * clear (version, type, flags, sequence, timestamp), the encrypted payload + * and a 16-byte tag; the header is the AEAD's additional data, so a changed + * sequence or timestamp is refused. The nonce is the 64-bit sequence in + * big-endian after four zero bytes: keys are fresh per session, so no + * nonce is ever reused. + * + * byte 0 version (1) + * byte 1 type (DC_MEDIA_AUDIO, _VIDEO, _PING, _BYE) + * byte 2 flags (0 for now) + * byte 3 reserved (0) + * bytes 4..7 sequence, big-endian + * bytes 8..11 timestamp in ms, big-endian, 32 bit + * payload ..., 16-byte tag + * + * The replay window is the 64-packet sliding window of RFC 3711: a packet + * inside the window that was seen before is refused, an old one outside it + * too, and the window follows the highest sequence seen. + */ +#include "dc_internal.h" + +#include +#include +#include + +#include +#include +#include + +#define HDR_LEN 12 + +static void +put_u32(unsigned char *p, uint32_t v) +{ + p[0] = (unsigned char)(v >> 24); + p[1] = (unsigned char)(v >> 16); + p[2] = (unsigned char)(v >> 8); + p[3] = (unsigned char)v; +} + +static uint32_t +get_u32(const unsigned char *p) +{ + return ((uint32_t)p[0] << 24) | ((uint32_t)p[1] << 16) | + ((uint32_t)p[2] << 8) | (uint32_t)p[3]; +} + +static void +put_u64(unsigned char *p, uint64_t v) +{ + int i; + + for (i = 7; i >= 0; i--) { + p[i] = (unsigned char)(v & 0xff); + v >>= 8; + } +} + +int +dc_media_keypair(unsigned char pub[DC_MEDIA_KEY_LEN], + unsigned char priv[DC_MEDIA_KEY_LEN]) +{ + EVP_PKEY *key; + size_t publen = DC_MEDIA_KEY_LEN; + size_t privlen = DC_MEDIA_KEY_LEN; + + if (pub == NULL || priv == NULL) + return -1; + if (RAND_bytes(priv, DC_MEDIA_KEY_LEN) != 1) + return -1; + key = EVP_PKEY_new_raw_private_key(EVP_PKEY_X25519, NULL, priv, + DC_MEDIA_KEY_LEN); + if (key == NULL) + return -1; + if (EVP_PKEY_get_raw_public_key(key, pub, &publen) != 1 || + publen != DC_MEDIA_KEY_LEN) { + EVP_PKEY_free(key); + return -1; + } + EVP_PKEY_free(key); + (void)privlen; + return 0; +} + +/* HKDF-SHA256, 64 bytes out: the two directions of one session */ +static int +derive(const unsigned char secret[DC_MEDIA_KEY_LEN], + const unsigned char pub_a[DC_MEDIA_KEY_LEN], + const unsigned char pub_b[DC_MEDIA_KEY_LEN], unsigned char out[64]) +{ + static const char info[] = "dtchat media v1"; + unsigned char salt[DC_MEDIA_KEY_LEN * 2]; + unsigned char *p = salt; + EVP_PKEY_CTX *ctx; + size_t outlen = 64; + int rc = -1; + + /* the public keys, sorted, make the salt of the session */ + if (memcmp(pub_a, pub_b, DC_MEDIA_KEY_LEN) <= 0) { + memcpy(p, pub_a, DC_MEDIA_KEY_LEN); + memcpy(p + DC_MEDIA_KEY_LEN, pub_b, DC_MEDIA_KEY_LEN); + } else { + memcpy(p, pub_b, DC_MEDIA_KEY_LEN); + memcpy(p + DC_MEDIA_KEY_LEN, pub_a, DC_MEDIA_KEY_LEN); + } + ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL); + if (ctx == NULL) + return -1; + if (EVP_PKEY_derive_init(ctx) == 1 && + EVP_PKEY_CTX_set_hkdf_md(ctx, EVP_sha256()) == 1 && + EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, sizeof(salt)) == 1 && + EVP_PKEY_CTX_set1_hkdf_key(ctx, secret, DC_MEDIA_KEY_LEN) == 1 && + EVP_PKEY_CTX_add1_hkdf_info(ctx, (const unsigned char *)info, + sizeof(info) - 1) == 1 && + EVP_PKEY_derive(ctx, out, &outlen) == 1 && outlen == 64) + rc = 0; + EVP_PKEY_CTX_free(ctx); + return rc; +} + +int +dc_media_keys_agree(const unsigned char priv[DC_MEDIA_KEY_LEN], + const unsigned char peer_pub[DC_MEDIA_KEY_LEN], + int initiator, DcMediaKeys *keys) +{ + EVP_PKEY *mine = NULL; + EVP_PKEY *theirs = NULL; + EVP_PKEY_CTX *ctx = NULL; + unsigned char secret[DC_MEDIA_KEY_LEN]; + unsigned char my_pub[DC_MEDIA_KEY_LEN]; + unsigned char both[64]; + size_t seclen = sizeof(secret); + size_t mylen = sizeof(my_pub); + int rc = -1; + + if (priv == NULL || peer_pub == NULL || keys == NULL) + return -1; + memset(keys, 0, sizeof(*keys)); + mine = EVP_PKEY_new_raw_private_key(EVP_PKEY_X25519, NULL, priv, + DC_MEDIA_KEY_LEN); + theirs = EVP_PKEY_new_raw_public_key(EVP_PKEY_X25519, NULL, peer_pub, + DC_MEDIA_KEY_LEN); + if (mine == NULL || theirs == NULL) + goto out; + if (EVP_PKEY_get_raw_public_key(mine, my_pub, &mylen) != 1 || + mylen != DC_MEDIA_KEY_LEN) + goto out; + ctx = EVP_PKEY_CTX_new(mine, NULL); + if (ctx == NULL || EVP_PKEY_derive_init(ctx) != 1 || + EVP_PKEY_derive_set_peer(ctx, theirs) != 1 || + EVP_PKEY_derive(ctx, secret, &seclen) != 1 || + seclen != DC_MEDIA_KEY_LEN) + goto out; + if (derive(secret, my_pub, peer_pub, both) != 0) + goto out; + if (initiator) { + memcpy(keys->send, both, DC_MEDIA_KEY_LEN); + memcpy(keys->recv, both + DC_MEDIA_KEY_LEN, DC_MEDIA_KEY_LEN); + } else { + memcpy(keys->recv, both, DC_MEDIA_KEY_LEN); + memcpy(keys->send, both + DC_MEDIA_KEY_LEN, DC_MEDIA_KEY_LEN); + } + rc = 0; +out: + OPENSSL_cleanse(secret, sizeof(secret)); + OPENSSL_cleanse(both, sizeof(both)); + EVP_PKEY_CTX_free(ctx); + EVP_PKEY_free(mine); + EVP_PKEY_free(theirs); + return rc; +} + +void +dc_media_send_init(DcMediaSend *send, const unsigned char key[DC_MEDIA_KEY_LEN]) +{ + if (send == NULL) + return; + memset(send, 0, sizeof(*send)); + if (key != NULL) + memcpy(send->key, key, DC_MEDIA_KEY_LEN); +} + +void +dc_media_recv_init(DcMediaRecv *recv, const unsigned char key[DC_MEDIA_KEY_LEN]) +{ + if (recv == NULL) + return; + memset(recv, 0, sizeof(*recv)); + if (key != NULL) + memcpy(recv->key, key, DC_MEDIA_KEY_LEN); +} + +size_t +dc_media_seal(DcMediaSend *send, int type, uint32_t ts, const void *plain, + size_t len, unsigned char *out, size_t outlen) +{ + EVP_CIPHER_CTX *ctx = NULL; + unsigned char nonce[12]; + unsigned char *payload; + int n = 0; + int total = 0; + size_t need; + + if (send == NULL || out == NULL || + (len > 0 && plain == NULL) || len > DC_MEDIA_MAX_PAYLOAD) + return 0; + if (type < DC_MEDIA_AUDIO || type > DC_MEDIA_BYE) + return 0; + need = HDR_LEN + len + DC_MEDIA_TAG_LEN; + if (outlen < need) + return 0; + out[0] = DC_MEDIA_VERSION; + out[1] = (unsigned char)type; + out[2] = 0; + out[3] = 0; + put_u32(out + 4, (uint32_t)send->seq); + put_u32(out + 8, ts); + payload = out + HDR_LEN; + + memset(nonce, 0, sizeof(nonce)); + put_u64(nonce + 4, send->seq); + ctx = EVP_CIPHER_CTX_new(); + if (ctx == NULL) + return 0; + if (EVP_EncryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL, + NULL) != 1 || + EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, sizeof(nonce), + NULL) != 1 || + EVP_EncryptInit_ex(ctx, NULL, NULL, send->key, nonce) != 1 || + EVP_EncryptUpdate(ctx, NULL, &n, out, HDR_LEN) != 1 || + EVP_EncryptUpdate(ctx, payload, &n, plain != NULL ? plain : + (const unsigned char *)"", (int)len) != 1) { + EVP_CIPHER_CTX_free(ctx); + return 0; + } + total = n; + if (EVP_EncryptFinal_ex(ctx, payload + total, &n) != 1 || + EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, DC_MEDIA_TAG_LEN, + payload + len) != 1) { + EVP_CIPHER_CTX_free(ctx); + return 0; + } + EVP_CIPHER_CTX_free(ctx); + send->seq++; + return need; +} + +int +dc_media_open(DcMediaRecv *recv, const unsigned char *packet, size_t len, + int *type, uint32_t *ts, void *plain, size_t *plainlen) +{ + EVP_CIPHER_CTX *ctx = NULL; + unsigned char nonce[12]; + unsigned char *payload; + uint32_t seq; + size_t paylen; + int n = 0; + int total = 0; + int ok = 0; + + if (recv == NULL || packet == NULL || plain == NULL || plainlen == NULL) + return -1; + if (len < HDR_LEN + DC_MEDIA_TAG_LEN) + return -1; + if (packet[0] != DC_MEDIA_VERSION || packet[2] != 0 || packet[3] != 0) + return -1; + if (packet[1] < DC_MEDIA_AUDIO || packet[1] > DC_MEDIA_BYE) + return -1; + seq = get_u32(packet + 4); + paylen = len - HDR_LEN - DC_MEDIA_TAG_LEN; + if (paylen > DC_MEDIA_MAX_PAYLOAD) + return -1; + payload = (unsigned char *)packet + HDR_LEN; + + memset(nonce, 0, sizeof(nonce)); + put_u64(nonce + 4, seq); + ctx = EVP_CIPHER_CTX_new(); + if (ctx == NULL) + return -1; + if (EVP_DecryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL, + NULL) != 1 || + EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, sizeof(nonce), + NULL) != 1 || + EVP_DecryptInit_ex(ctx, NULL, NULL, recv->key, nonce) != 1 || + EVP_DecryptUpdate(ctx, NULL, &n, packet, HDR_LEN) != 1 || + EVP_DecryptUpdate(ctx, (unsigned char *)plain, &n, payload, + (int)paylen) != 1) { + EVP_CIPHER_CTX_free(ctx); + return -1; + } + total = n; + if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, DC_MEDIA_TAG_LEN, + payload + paylen) != 1 || + EVP_DecryptFinal_ex(ctx, (unsigned char *)plain + total, &n) != 1) + goto out; + total += n; + if (dc_media_replay_ok(recv, seq) != 1) + goto out; + if (type != NULL) + *type = packet[1]; + if (ts != NULL) + *ts = get_u32(packet + 8); + *plainlen = (size_t)total; + ok = 1; +out: + EVP_CIPHER_CTX_free(ctx); + return ok ? 1 : -1; +} + +int +dc_media_replay_ok(DcMediaRecv *recv, uint32_t seq) +{ + uint64_t s = seq; + uint64_t diff; + + if (recv == NULL) + return 0; + if (!recv->started) { + recv->started = 1; + recv->highest = s; + recv->mask = 1; + return 1; + } + if (s > recv->highest) { + diff = s - recv->highest; + if (diff >= 64) + recv->mask = 1; + else + recv->mask = (recv->mask << diff) | 1; + recv->highest = s; + return 1; + } + diff = recv->highest - s; + if (diff >= 64) + return 0; + if (recv->mask & (1ULL << diff)) + return 0; + recv->mask |= 1ULL << diff; + return 1; +} diff --git a/lib/libDtChat/test_media_packet.c b/lib/libDtChat/test_media_packet.c new file mode 100644 index 00000000..cb82473e --- /dev/null +++ b/lib/libDtChat/test_media_packet.c @@ -0,0 +1,296 @@ +/* + * test_media_packet.c -- tests for the media packets (T6.3) + * + * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. + */ +#include "dc_internal.h" + +#include +#include +#include + +static int fails; + +#define CHECK(cond) do { \ + if (!(cond)) { \ + fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, \ + #cond); \ + fails++; \ + } \ +} while (0) + +struct Session { + DcMediaKeys initiator; + DcMediaKeys responder; + DcMediaSend snd; + DcMediaRecv rcv; +}; + +static int +session_new(struct Session *s) +{ + unsigned char pub_a[DC_MEDIA_KEY_LEN], priv_a[DC_MEDIA_KEY_LEN]; + unsigned char pub_b[DC_MEDIA_KEY_LEN], priv_b[DC_MEDIA_KEY_LEN]; + + CHECK(dc_media_keypair(pub_a, priv_a) == 0); + CHECK(dc_media_keypair(pub_b, priv_b) == 0); + CHECK(dc_media_keys_agree(priv_a, pub_b, 1, &s->initiator) == 0); + CHECK(dc_media_keys_agree(priv_b, pub_a, 0, &s->responder) == 0); + /* the initiator's send key is the responder's receive key */ + CHECK(memcmp(s->initiator.send, s->responder.recv, + DC_MEDIA_KEY_LEN) == 0); + CHECK(memcmp(s->initiator.recv, s->responder.send, + DC_MEDIA_KEY_LEN) == 0); + CHECK(memcmp(s->initiator.send, s->initiator.recv, + DC_MEDIA_KEY_LEN) != 0); + dc_media_send_init(&s->snd, s->initiator.send); + dc_media_recv_init(&s->rcv, s->responder.recv); + return 1; +} + +static void +test_roundtrip(void) +{ + struct Session s; + unsigned char packet[DC_MEDIA_MAX_PAYLOAD + 64]; + unsigned char plain[DC_MEDIA_MAX_PAYLOAD]; + unsigned char got[DC_MEDIA_MAX_PAYLOAD]; + size_t len; + size_t gotlen = 0; + int type = 0; + uint32_t ts = 0; + int i; + + if (!session_new(&s)) + return; + for (i = 0; i < (int)sizeof(plain); i++) + plain[i] = (unsigned char)(i * 7 + 3); + len = dc_media_seal(&s.snd, DC_MEDIA_AUDIO, 1234, plain, 160, packet, + sizeof(packet)); + CHECK(len == 12 + 160 + DC_MEDIA_TAG_LEN); + CHECK(dc_media_open(&s.rcv, packet, len, &type, &ts, got, &gotlen) + == 1); + CHECK(type == DC_MEDIA_AUDIO); + CHECK(ts == 1234); + CHECK(gotlen == 160); + CHECK(memcmp(plain, got, gotlen) == 0); + + /* zero length works too */ + len = dc_media_seal(&s.snd, DC_MEDIA_PING, 1, NULL, 0, packet, + sizeof(packet)); + CHECK(len == 12 + DC_MEDIA_TAG_LEN); + CHECK(dc_media_open(&s.rcv, packet, len, &type, &ts, got, &gotlen) + == 1); + CHECK(type == DC_MEDIA_PING); + CHECK(gotlen == 0); +} + +static void +test_tamper(void) +{ + struct Session s; + unsigned char packet[512]; + unsigned char got[512]; + unsigned char save; + size_t len; + size_t gotlen; + int type; + + if (!session_new(&s)) + return; + len = dc_media_seal(&s.snd, DC_MEDIA_VIDEO, 99, "payload", 7, packet, + sizeof(packet)); + CHECK(len > 0); + + /* a changed header (the sequence is additional data) */ + save = packet[7]; + packet[7] ^= 1; + CHECK(dc_media_open(&s.rcv, packet, len, &type, NULL, got, &gotlen) + == -1); + packet[7] = save; + + /* a changed payload */ + save = packet[12]; + packet[12] ^= 0x80; + CHECK(dc_media_open(&s.rcv, packet, len, &type, NULL, got, &gotlen) + == -1); + packet[12] = save; + + /* a changed tag */ + save = packet[len - 1]; + packet[len - 1] ^= 1; + CHECK(dc_media_open(&s.rcv, packet, len, &type, NULL, got, &gotlen) + == -1); + packet[len - 1] = save; + + /* truncated */ + CHECK(dc_media_open(&s.rcv, packet, len - 1, &type, NULL, got, + &gotlen) == -1); + CHECK(dc_media_open(&s.rcv, packet, 10, &type, NULL, got, &gotlen) + == -1); + + /* the unchanged packet still opens */ + CHECK(dc_media_open(&s.rcv, packet, len, &type, NULL, got, &gotlen) + == 1); + + /* a wrong key is refused */ + { + DcMediaRecv wrong; + unsigned char key[DC_MEDIA_KEY_LEN]; + + memset(key, 0x42, sizeof(key)); + dc_media_recv_init(&wrong, key); + len = dc_media_seal(&s.snd, DC_MEDIA_AUDIO, 1, "x", 1, packet, + sizeof(packet)); + CHECK(dc_media_open(&wrong, packet, len, &type, NULL, got, + &gotlen) == -1); + } +} + +static void +test_replay(void) +{ + struct Session s; + unsigned char got[256]; + size_t gotlen; + int i; + int type; + uint32_t ts; + + if (!session_new(&s)) + return; + /* seal five, deliver them out of order */ + { + unsigned char packets[5][256]; + size_t lens[5]; + + for (i = 0; i < 5; i++) { + lens[i] = dc_media_seal(&s.snd, DC_MEDIA_AUDIO, + (uint32_t)i, &i, sizeof i, packets[i], + sizeof packets[i]); + CHECK(lens[i] > 0); + } + CHECK(dc_media_open(&s.rcv, packets[4], lens[4], &type, &ts, + got, &gotlen) == 1); + CHECK(dc_media_open(&s.rcv, packets[3], lens[3], &type, &ts, + got, &gotlen) == 1); + CHECK(dc_media_open(&s.rcv, packets[1], lens[1], &type, &ts, + got, &gotlen) == 1); + /* the same packet twice is a replay */ + CHECK(dc_media_open(&s.rcv, packets[3], lens[3], &type, &ts, + got, &gotlen) == -1); + CHECK(dc_media_open(&s.rcv, packets[1], lens[1], &type, &ts, + got, &gotlen) == -1); + CHECK(dc_media_open(&s.rcv, packets[2], lens[2], &type, &ts, + got, &gotlen) == 1); + CHECK(dc_media_open(&s.rcv, packets[0], lens[0], &type, &ts, + got, &gotlen) == 1); + } + + /* the window: 100 packets, then an old one outside it */ + { + DcMediaRecv win; + unsigned char key[DC_MEDIA_KEY_LEN]; + uint64_t base = 1000; + + memset(key, 9, sizeof(key)); + dc_media_recv_init(&win, key); + CHECK(dc_media_replay_ok(&win, (uint32_t)base) == 1); + (void)base; + CHECK(dc_media_replay_ok(&win, 1000 + 64) == 1); + CHECK(dc_media_replay_ok(&win, 1000 + 64 - 63) == 1); + CHECK(dc_media_replay_ok(&win, 1000 + 64 - 63) == 0); + /* 64 packets behind the highest is outside the window */ + CHECK(dc_media_replay_ok(&win, 1000 + 64 - 64) == 0); + CHECK(dc_media_replay_ok(&win, 1000 - 1) == 0); + CHECK(dc_media_replay_ok(&win, 1000 + 65) == 1); + /* the move kept 1064, dropped everything at or below 1001 */ + CHECK(dc_media_replay_ok(&win, 1000 + 64) == 0); + CHECK(dc_media_replay_ok(&win, 1001) == 0); + CHECK(dc_media_replay_ok(&win, 1000 + 63) == 1); + } +} + +static void +test_limits(void) +{ + struct Session s; + unsigned char packet[DC_MEDIA_MAX_PAYLOAD + 64]; + unsigned char big[DC_MEDIA_MAX_PAYLOAD + 1]; + size_t len; + + if (!session_new(&s)) + return; + memset(big, 'a', sizeof(big)); + /* too much for the buffer */ + len = dc_media_seal(&s.snd, DC_MEDIA_AUDIO, 0, big, 100, packet, 50); + CHECK(len == 0); + /* more than a packet carries */ + len = dc_media_seal(&s.snd, DC_MEDIA_AUDIO, 0, big, sizeof(big), + packet, sizeof(packet)); + CHECK(len == 0); + /* a bad type */ + len = dc_media_seal(&s.snd, 0, 0, big, 10, packet, sizeof(packet)); + CHECK(len == 0); + len = dc_media_seal(&s.snd, 99, 0, big, 10, packet, sizeof(packet)); + CHECK(len == 0); + /* exactly the maximum */ + len = dc_media_seal(&s.snd, DC_MEDIA_VIDEO, 0, big, + DC_MEDIA_MAX_PAYLOAD, packet, sizeof(packet)); + CHECK(len == 12 + DC_MEDIA_MAX_PAYLOAD + DC_MEDIA_TAG_LEN); +} + +static void +test_bulk(void) +{ + struct Session s; + unsigned char packet[DC_MEDIA_MAX_PAYLOAD + 64]; + unsigned char plain[DC_MEDIA_MAX_PAYLOAD]; + unsigned char got[DC_MEDIA_MAX_PAYLOAD]; + unsigned int x = 7; + size_t i; + int n; + + if (!session_new(&s)) + return; + for (n = 0; n < 1000; n++) { + size_t len = (size_t)(n * 37 % (DC_MEDIA_MAX_PAYLOAD + 1)); + size_t gotlen = 0; + int type = 0; + + x = x * 1103515245u + 12345u; + for (i = 0; i < len; i++) { + x = x * 1103515245u + 12345u; + plain[i] = (unsigned char)(x >> 16); + } + if (dc_media_seal(&s.snd, DC_MEDIA_AUDIO, (uint32_t)n, plain, + len, packet, sizeof(packet)) != 12 + len + + DC_MEDIA_TAG_LEN) { + CHECK(0); + return; + } + if (dc_media_open(&s.rcv, packet, 12 + len + + DC_MEDIA_TAG_LEN, &type, NULL, got, &gotlen) != 1 || + gotlen != len || memcmp(plain, got, len) != 0) { + CHECK(0); + return; + } + } + CHECK(1); +} + +int +main(void) +{ + test_roundtrip(); + test_tamper(); + test_replay(); + test_limits(); + test_bulk(); + if (fails != 0) { + fprintf(stderr, "%d test(s) failed\n", fails); + return 1; + } + printf("test_media_packet: all ok\n"); + return 0; +}