Skip to content

Commit

Permalink
introduce xxx2 api
Browse files Browse the repository at this point in the history
  • Loading branch information
pabuhler committed Nov 30, 2023
1 parent 8d24f9c commit d042fec
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 38 deletions.
30 changes: 30 additions & 0 deletions include/srtp.h
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,14 @@ srtp_err_status_t srtp_protect_mki(srtp_ctx_t *ctx,
unsigned int use_mki,
unsigned int mki_index);

srtp_err_status_t srtp_protect2(srtp_t ctx,
const void *rtp,
int rtp_len,
void *srtp,
int *srtp_len,
unsigned int use_mki,
unsigned int mki_index);

/**
* @brief srtp_unprotect() is the Secure RTP receiver-side packet
* processing function.
Expand Down Expand Up @@ -571,6 +579,13 @@ srtp_err_status_t srtp_unprotect_mki(srtp_t ctx,
int *len_ptr,
unsigned int use_mki);

srtp_err_status_t srtp_unprotect2(srtp_t ctx,
const void *srtp,
int srtp_len,
void *rtp,
int *rtp_len,
unsigned int use_mki);

/**
* @brief srtp_create() allocates and initializes an SRTP session.
Expand Down Expand Up @@ -1382,6 +1397,14 @@ srtp_err_status_t srtp_protect_rtcp_mki(srtp_t ctx,
unsigned int use_mki,
unsigned int mki_index);

srtp_err_status_t srtp_protect_rtcp2(srtp_t ctx,
const void *rtcp,
int rtcp_len,
void *srtcp,
int *srtcp_len,
unsigned int use_mki,
unsigned int mki_index);

/**
* @brief srtp_unprotect_rtcp() is the Secure RTCP receiver-side packet
* processing function.
Expand Down Expand Up @@ -1474,6 +1497,13 @@ srtp_err_status_t srtp_unprotect_rtcp_mki(srtp_t ctx,
int *pkt_octet_len,
unsigned int use_mki);

srtp_err_status_t srtp_unprotect_rtcp2(srtp_t ctx,
const void *srtcp,
int srtcp_len,
void *rtcp,
int *rtcp_len,
unsigned int use_mki);

/**
* @}
*/
Expand Down
64 changes: 64 additions & 0 deletions srtp/srtp.c
Original file line number Diff line number Diff line change
Expand Up @@ -2142,6 +2142,22 @@ srtp_err_status_t srtp_protect(srtp_ctx_t *ctx,
return srtp_protect_mki(ctx, rtp_hdr, pkt_octet_len, 0, 0);
}

srtp_err_status_t srtp_protect2(srtp_t ctx,
const void *rtp,
int rtp_len,
void *srtp,
int *srtp_len,
unsigned int use_mki,
unsigned int mki_index)
{
if (*srtp_len < rtp_len) {
return srtp_err_status_bad_param;
}
memcpy(srtp, rtp, rtp_len);
*srtp_len = rtp_len;
return srtp_protect_mki(ctx, srtp, srtp_len, use_mki, mki_index);
}

srtp_err_status_t srtp_protect_mki(srtp_ctx_t *ctx,
void *rtp_hdr,
int *pkt_octet_len,
Expand Down Expand Up @@ -2470,6 +2486,22 @@ srtp_err_status_t srtp_unprotect(srtp_ctx_t *ctx,
return srtp_unprotect_mki(ctx, srtp_hdr, pkt_octet_len, 0);
}

srtp_err_status_t srtp_unprotect2(srtp_t ctx,
const void *srtp,
int srtp_len,
void *rtp,
int *rtp_len,
unsigned int use_mki)
{
if (*rtp_len < srtp_len) {
// this is actually expected but for the tests this should not happen
return srtp_err_status_bad_param;
}
memcpy(rtp, srtp, srtp_len);
*rtp_len = srtp_len;
return srtp_unprotect_mki(ctx, rtp, rtp_len, use_mki);
}

srtp_err_status_t srtp_unprotect_mki(srtp_ctx_t *ctx,
void *srtp_hdr,
int *pkt_octet_len,
Expand Down Expand Up @@ -3987,6 +4019,22 @@ srtp_err_status_t srtp_protect_rtcp(srtp_t ctx,
return srtp_protect_rtcp_mki(ctx, rtcp_hdr, pkt_octet_len, 0, 0);
}

srtp_err_status_t srtp_protect_rtcp2(srtp_t ctx,
const void *rtcp,
int rtcp_len,
void *srtcp,
int *srtcp_len,
unsigned int use_mki,
unsigned int mki_index)
{
if (*srtcp_len < rtcp_len) {
return srtp_err_status_bad_param;
}
memcpy(srtcp, rtcp, rtcp_len);
*srtcp_len = rtcp_len;
return srtp_protect_rtcp_mki(ctx, srtcp, srtcp_len, use_mki, mki_index);
}

srtp_err_status_t srtp_protect_rtcp_mki(srtp_t ctx,
void *rtcp_hdr,
int *pkt_octet_len,
Expand Down Expand Up @@ -4219,6 +4267,22 @@ srtp_err_status_t srtp_unprotect_rtcp(srtp_t ctx,
return srtp_unprotect_rtcp_mki(ctx, srtcp_hdr, pkt_octet_len, 0);
}

srtp_err_status_t srtp_unprotect_rtcp2(srtp_t ctx,
const void *srtcp,
int srtcp_len,
void *rtcp,
int *rtcp_len,
unsigned int use_mki)
{
if (*rtcp_len < srtcp_len) {
// this is actually expected but for the tests this should not happen
return srtp_err_status_bad_param;
}
memcpy(rtcp, srtcp, srtcp_len);
*rtcp_len = srtcp_len;
return srtp_unprotect_rtcp_mki(ctx, rtcp, rtcp_len, use_mki);
}

srtp_err_status_t srtp_unprotect_rtcp_mki(srtp_t ctx,
void *srtcp_hdr,
int *pkt_octet_len,
Expand Down
127 changes: 89 additions & 38 deletions test/srtp_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,80 +155,131 @@ srtp_master_key_t *test_keys[2] = {
};
// clang-format on

srtp_err_status_t __srtp_protect_mki(srtp_t srtp_sender,
srtp_hdr_t *hdr,
int *len,
srtp_err_status_t __srtp_protect_mki(srtp_ctx_t *ctx,
void *rtp,
int *rtp_len,
int mki_index)
{
if (mki_index == -1) {
return srtp_protect(srtp_sender, hdr, len);
srtp_err_status_t status = srtp_err_status_fail;
int use_mki = mki_index >= 0 ? 1 : 0;
if (1) {
uint8_t out_buf[4048];
int out_len = sizeof(out_buf);
status = srtp_protect2(ctx, rtp, *rtp_len, out_buf, &out_len, use_mki,
mki_index);
if (status == srtp_err_status_ok) {
memcpy(rtp, out_buf, out_len);
*rtp_len = out_len;
}
} else {
return srtp_protect_mki(srtp_sender, hdr, len, 1, mki_index);
if (use_mki == 0) {
status = srtp_protect(ctx, rtp, rtp_len);
} else {
status = srtp_protect_mki(ctx, rtp, rtp_len, use_mki, mki_index);
}
}
return status;
}

srtp_err_status_t __srtp_protect(srtp_ctx_t *ctx,
void *rtp_hdr,
int *pkt_octet_len)
srtp_err_status_t __srtp_protect(srtp_ctx_t *ctx, void *rtp, int *rtp_len)
{
return __srtp_protect_mki(ctx, rtp_hdr, pkt_octet_len, -1);
return __srtp_protect_mki(ctx, rtp, rtp_len, -1);
}

srtp_err_status_t __srtp_unprotect_mki(srtp_t srtp_sender,
srtp_hdr_t *hdr,
int *len,
srtp_err_status_t __srtp_unprotect_mki(srtp_ctx_t *ctx,
void *srtp,
int *srtp_len,
int use_mki)
{
if (use_mki == 0) {
return srtp_unprotect(srtp_sender, hdr, len);
srtp_err_status_t status = srtp_err_status_fail;
if (1) {
uint8_t out_buf[4048];
int out_len = sizeof(out_buf);
status =
srtp_unprotect2(ctx, srtp, *srtp_len, out_buf, &out_len, use_mki);
if (status == srtp_err_status_ok) {
memcpy(srtp, out_buf, out_len);
*srtp_len = out_len;
}
} else {
return srtp_unprotect_mki(srtp_sender, hdr, len, use_mki);
if (use_mki == 0) {
status = srtp_unprotect(ctx, srtp, srtp_len);
} else {
status = srtp_unprotect_mki(ctx, srtp, srtp_len, use_mki);
}
}
return status;
}

srtp_err_status_t __srtp_unprotect(srtp_ctx_t *ctx,
void *srtp_hdr,
int *pkt_octet_len)
srtp_err_status_t __srtp_unprotect(srtp_ctx_t *ctx, void *srtp, int *srtp_len)
{
return __srtp_unprotect_mki(ctx, srtp_hdr, pkt_octet_len, 0);
return __srtp_unprotect_mki(ctx, srtp, srtp_len, 0);
}

srtp_err_status_t __srtp_protect_rtcp_mki(srtp_t srtp_sender,
srtcp_hdr_t *hdr,
int *len,
srtp_err_status_t __srtp_protect_rtcp_mki(srtp_ctx_t *ctx,
void *rtcp,
int *rtcp_len,
int mki_index)
{
if (mki_index == -1) {
return srtp_protect_rtcp(srtp_sender, hdr, len);
srtp_err_status_t status = srtp_err_status_fail;
int use_mki = mki_index >= 0 ? 1 : 0;
if (1) {
uint8_t out_buf[4048];
int out_len = sizeof(out_buf);
status = srtp_protect_rtcp2(ctx, rtcp, *rtcp_len, out_buf, &out_len,
use_mki, mki_index);
if (status == srtp_err_status_ok) {
memcpy(rtcp, out_buf, out_len);
*rtcp_len = out_len;
}
} else {
return srtp_protect_rtcp_mki(srtp_sender, hdr, len, 1, mki_index);
if (use_mki == 0) {
status = srtp_protect_rtcp(ctx, rtcp, rtcp_len);
} else {
status =
srtp_protect_rtcp_mki(ctx, rtcp, rtcp_len, use_mki, mki_index);
}
}
return status;
}

srtp_err_status_t __srtp_protect_rtcp(srtp_ctx_t *ctx,
void *rtcp_hdr,
int *pkt_octet_len)
void *rtcp,
int *rtcp_len)
{
return __srtp_protect_rtcp_mki(ctx, rtcp_hdr, pkt_octet_len, -1);
return __srtp_protect_rtcp_mki(ctx, rtcp, rtcp_len, -1);
}

srtp_err_status_t __srtp_unprotect_rtcp_mki(srtp_t srtp_sender,
srtcp_hdr_t *hdr,
int *len,
srtp_err_status_t __srtp_unprotect_rtcp_mki(srtp_ctx_t *ctx,
void *srtcp,
int *srtcp_len,
int use_mki)
{
if (use_mki == 0) {
return srtp_unprotect_rtcp(srtp_sender, hdr, len);
srtp_err_status_t status = srtp_err_status_fail;
if (1) {
uint8_t out_buf[4048];
int out_len = sizeof(out_buf);
status = srtp_unprotect_rtcp2(ctx, srtcp, *srtcp_len, out_buf, &out_len,
use_mki);
if (status == srtp_err_status_ok) {
memcpy(srtcp, out_buf, out_len);
*srtcp_len = out_len;
}
} else {
return srtp_unprotect_rtcp_mki(srtp_sender, hdr, len, use_mki);
if (use_mki == 0) {
status = srtp_unprotect_rtcp(ctx, srtcp, srtcp_len);
} else {
status = srtp_unprotect_rtcp_mki(ctx, srtcp, srtcp_len, use_mki);
}
}
return status;
}

srtp_err_status_t __srtp_unprotect_rtcp(srtp_ctx_t *ctx,
void *srtcp_hdr,
int *pkt_octet_len)
void *srtcp,
int *srtcp_len)
{
return __srtp_unprotect_rtcp_mki(ctx, srtcp_hdr, pkt_octet_len, 0);
return __srtp_unprotect_rtcp_mki(ctx, srtcp, srtcp_len, 0);
}

void usage(char *prog_name)
Expand Down

0 comments on commit d042fec

Please sign in to comment.