Created
March 16, 2018 11:44
-
-
Save alfredh/792759cccdd88d058bfd4a2a2d689922 to your computer and use it in GitHub Desktop.
Simple test program for SRTP Double Encryption
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Simple test program for SRTP Double Encryption | |
* | |
* https://tools.ietf.org/html/draft-ietf-perc-double-08 | |
* | |
* | |
* To build and run the program, fetch libre from github | |
* and put this file as test.c | |
* | |
* git clone https://github.com/creytiv/re.git | |
* cd re | |
* make test | |
* ./test | |
*/ | |
#include <string.h> | |
#include <re.h> | |
struct double_srtp { | |
struct srtp *srtp_inner; | |
struct srtp *srtp_outer; | |
}; | |
static int write_dummy_rtp_packet(struct mbuf *mb) | |
{ | |
#define PAYLOAD_LEN 8 | |
static const struct rtp_header hdr = { | |
.ver = RTP_VERSION, | |
.pt = 8, | |
.seq = 1, | |
.ts = 160, | |
.ssrc = 0x5c5c5c5c | |
}; | |
static const uint8_t dummy_payload[PAYLOAD_LEN] = {1,2,3,4,5,6,7,8}; | |
int err; | |
err = rtp_hdr_encode(mb, &hdr); | |
if (err) | |
return err; | |
/* Dummy RTP payload */ | |
err = mbuf_write_mem(mb, dummy_payload, sizeof(dummy_payload)); | |
if (err) | |
return err; | |
return 0; | |
} | |
static int double_encrypt_packet(struct double_srtp *dsrtp, struct mbuf *mb) | |
{ | |
size_t start; | |
int err = 0; | |
re_printf("RTP packet: [%zu bytes] %w\n", | |
mb->end, mb->buf, mb->end); | |
start = mb->pos; | |
/* 4. Apply the inner cryptographic algorithm to the RTP packet. */ | |
err = srtp_encrypt(dsrtp->srtp_inner, mb); | |
if (err) { | |
re_printf("encrypt: could not encrypt inner SRTP (%m)\n", | |
err); | |
return err; | |
} | |
re_printf("1xSRTP packet: [%zu bytes] %w\n", | |
mb->end, mb->buf, mb->end); | |
mb->pos = mb->end; | |
/* 5. Append and empty OHB */ | |
err = mbuf_write_u8(mb, 0x00); | |
if (err) | |
return err; | |
mb->pos = start; | |
/* 6. Apply the outer cryptographic algorithm to the RTP packet. */ | |
err = srtp_encrypt(dsrtp->srtp_outer, mb); | |
if (err) { | |
re_printf("encrypt: could not encrypt outer SRTP (%m)\n", | |
err); | |
return err; | |
} | |
re_printf("2xSRTP packet: [%zu bytes] %w\n", | |
mb->end, mb->buf, mb->end); | |
return 0; | |
} | |
static int test(struct double_srtp *dsrtp) | |
{ | |
struct mbuf *mb; | |
int err; | |
mb = mbuf_alloc(1024); | |
err = write_dummy_rtp_packet(mb); | |
if (err) | |
goto out; | |
mb->pos = 0; | |
err = double_encrypt_packet(dsrtp, mb); | |
if (err) | |
goto out; | |
out: | |
return err; | |
} | |
int main(void) | |
{ | |
struct double_srtp dsrtp; | |
const enum srtp_suite suite = SRTP_AES_128_GCM; | |
int err; | |
#define KEY_SIZE (16+12) | |
static const uint8_t master_key[KEY_SIZE] = { | |
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* key */ | |
2,2,2,2,2,2,2,2,2,2,2,2 /* salt */ | |
}; | |
memset(&dsrtp, 0, sizeof(dsrtp)); | |
/* Initialize re library */ | |
err = libre_init(); | |
if (err) | |
goto out; | |
/* | |
* Create two SRTP contexts, one for inner encryption and | |
* one for outer encryption. | |
* | |
* NOTE: The two SRTP contexts are on purpose using the | |
* same master key and salt. | |
*/ | |
err = srtp_alloc(&dsrtp.srtp_inner, suite, master_key, KEY_SIZE, 0); | |
if (err) { | |
re_printf("could not create inner SRTP (%m)\n", err); | |
goto out; | |
} | |
err = srtp_alloc(&dsrtp.srtp_outer, suite, master_key, KEY_SIZE, 0); | |
if (err) { | |
re_printf("could not create outer SRTP (%m)\n", err); | |
goto out; | |
} | |
/* | |
* Start the test | |
*/ | |
err = test(&dsrtp); | |
if (err) | |
goto out; | |
out: | |
if (err) | |
re_printf("program failed with error code (%m)\n", err); | |
else | |
re_printf("program terminated successfully.\n"); | |
return err; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment