Skip to content

Instantly share code, notes, and snippets.

@alexandrugrusu
Last active October 17, 2018 13:04
Show Gist options
  • Save alexandrugrusu/6af4b5d3706ccc83c0a40ca63c8928ae to your computer and use it in GitHub Desktop.
Save alexandrugrusu/6af4b5d3706ccc83c0a40ca63c8928ae to your computer and use it in GitHub Desktop.
main.c
rcvr->message.header.seq = snd->message.header.seq;
for(i = 0; read(ixfd, &s, 2) > 0; i++)
{
in_buffer[wr++] = s;
if((i == B_LEN) || (cnt == round(fsize / (B_LEN) / 2) && i == elim))
{
len = i * 2;
my_srtp_encrypt(snd, (char *) in_buffer, &len);
for(j = 0; j < i; j++)
{
if(write(oxfd_enc, &in_buffer[j], 2) != 2)
{
printf("\nThere was an error writing to output file!\n");
}
}
my_srtp_decrypt(rcvr, (char *) in_buffer, &len);
for(j = 0; j < i; j++)
{
if(write(oxfd, &in_buffer[j], 2) != 2)
{
printf("\nThere was an error writing to output file!\n");
}
}
i = 0;
wr = 0;
cnt++;
}
}
void update_snd_seq_ts(rtp_sender_t sender)
{
sender->message.header.seq = ntohs(sender->message.header.seq) + 1;//
sender->message.header.seq = htons(sender->message.header.seq);
sender->message.header.ts = ntohl(sender->message.header.ts) + 1;//
sender->message.header.ts = htonl(sender->message.header.ts);
}
int my_srtp_encrypt(rtp_sender_t sender, char *msg, int *len)
{
srtp_err_status_t stat;
update_snd_seq_ts(sender);
stat = my_srtp_protect(sender->srtp_ctx, &sender->message.header, msg, len);
if(stat)
{
printf("error protect %d\n", stat);
}
return 0;
}
int my_srtp_decrypt(rtp_receiver_t receiver, char *message, int *len)
{
srtp_err_status_t stat;
update_snd_seq_ts((rtp_sender_t) receiver);
stat = my_srtp_unprotect(receiver->srtp_ctx, &receiver->message.header, message, len);
if(stat)
{
printf("error unprotect %d\n", stat);
}
return 0;
}
srtp_err_status_t my_srtp_protect(srtp_ctx_t *ctx, void *rtp_hdr, char *input, int *len)
{
return my_srtp_protect_mki(ctx, rtp_hdr, input, len);
}
srtp_err_status_t my_srtp_protect_mki(srtp_ctx_t *ctx, void *rtp_hdr, char *input, int *len)
{
srtp_hdr_t *hdr = (srtp_hdr_t *)rtp_hdr;
srtp_xtd_seq_num_t est; /* estimated xtd_seq_num_t of *hdr */
int delta; /* delta of local pkt idx and that in hdr */
srtp_err_status_t status;
srtp_stream_ctx_t *stream;
srtp_session_keys_t *session_keys = NULL;
stream = ctx->stream_list;
stream->direction = dir_srtp_sender;
session_keys = srtp_get_session_keys_with_mki_index(stream, 0, 0);
if (session_keys == NULL)
return srtp_err_status_bad_mki;
status = srtp_get_est_pkt_index(hdr, stream, &est, &delta);
status = srtp_rdbx_check(&stream->rtp_rdbx, delta);
if (status) {
if (status != srtp_err_status_replay_fail ||
!stream->allow_repeat_tx)
return status;
}
srtp_rdbx_add_index(&stream->rtp_rdbx, delta);
if (session_keys->rtp_cipher->type->id == SRTP_AES_ICM_128 ||
session_keys->rtp_cipher->type->id == SRTP_AES_ICM_192 ||
session_keys->rtp_cipher->type->id == SRTP_AES_ICM_256) {
v128_t iv;
iv.v32[0] = 0;
iv.v32[1] = hdr->ssrc;
#ifdef NO_64BIT_MATH
iv.v64[1] = be64_to_cpu(
make64((high32(est) << 16) | (low32(est) >> 16), low32(est) << 16));
#else
iv.v64[1] = be64_to_cpu(est << 16);
#endif
status = srtp_cipher_set_iv(session_keys->rtp_cipher, (uint8_t *)&iv,
srtp_direction_encrypt);
if (!status && session_keys->rtp_xtn_hdr_cipher) {
status = srtp_cipher_set_iv(session_keys->rtp_xtn_hdr_cipher,
(uint8_t *)&iv, srtp_direction_encrypt);
}
}
if (status)
return srtp_err_status_cipher_fail;
est = be64_to_cpu(est << 16);
status = srtp_cipher_encrypt(session_keys->rtp_cipher, (uint8_t *)input, (unsigned int *) len);
if (status)
return srtp_err_status_cipher_fail;
return srtp_err_status_ok;
}
srtp_err_status_t my_srtp_unprotect(srtp_ctx_t *ctx, void *srtp_hdr, char *message, int *len)
{
return my_srtp_unprotect_mki(ctx, srtp_hdr, message, len);
}
srtp_err_status_t my_srtp_unprotect_mki(srtp_ctx_t *ctx, void *srtp_hdr, char *input, int *len)
{
srtp_hdr_t *hdr = (srtp_hdr_t *)srtp_hdr;
srtp_xtd_seq_num_t est; /* estimated xtd_seq_num_t of *hdr */
int delta; /* delta of local pkt idx and that in hdr */
v128_t iv;
srtp_err_status_t status;
srtp_stream_ctx_t *stream;
srtp_session_keys_t *session_keys = NULL;
stream = ctx->stream_list;
status = srtp_get_est_pkt_index(hdr, stream, &est, &delta);
status = srtp_rdbx_check(&stream->rtp_rdbx, delta);
if (status)
return status;
session_keys = &stream->session_keys[0];
if (session_keys->rtp_cipher->type->id == SRTP_AES_ICM_128 ||
session_keys->rtp_cipher->type->id == SRTP_AES_ICM_192 ||
session_keys->rtp_cipher->type->id == SRTP_AES_ICM_256) {
/* aes counter mode */
iv.v32[0] = 0;
iv.v32[1] = hdr->ssrc; /* still in network order */
iv.v64[1] = be64_to_cpu(est << 16);
status = srtp_cipher_set_iv(session_keys->rtp_cipher, (uint8_t *)&iv,
srtp_direction_decrypt);
if (!status && session_keys->rtp_xtn_hdr_cipher) {
status = srtp_cipher_set_iv(session_keys->rtp_xtn_hdr_cipher,
(uint8_t *)&iv, srtp_direction_decrypt);
}
}
est = be64_to_cpu(est << 16);
if (input) {
status = srtp_cipher_decrypt(session_keys->rtp_cipher, (uint8_t *) input, (unsigned int *) len);
if (status)
return srtp_err_status_cipher_fail;
}
return srtp_err_status_ok;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment