30 #include <ccrtp-config.h>
37 #include <commoncpp/config.h>
38 #include <commoncpp/export.h>
45 using_mki(false),mkiLength(0),mki(NULL),
46 roc(0),guessed_roc(0),s_l(0),key_deriv_rate(0),
48 master_key(NULL), master_key_length(0),
49 master_key_srtp_use_nb(0), master_key_srtcp_use_nb(0),
50 master_salt(NULL), master_salt_length(0),
51 n_e(0),k_e(NULL),n_a(0),k_a(NULL),n_s(0),k_s(NULL),
53 ekeyl(0), akeyl(0), skeyl(0),
54 seqNumSet(false), macCtx(NULL), cipher(NULL), f8Cipher(NULL)
64 int32 master_key_length,
66 int32 master_salt_length,
72 ssrcCtx(ssrc),using_mki(false),mkiLength(0),mki(NULL),
73 roc(roc),guessed_roc(0),s_l(0),key_deriv_rate(key_deriv_rate),
75 master_key_srtp_use_nb(0), master_key_srtcp_use_nb(0), seqNumSet(false),
76 macCtx(NULL), cipher(NULL), f8Cipher(NULL)
86 memcpy(this->master_key, master_key, master_key_length);
90 memcpy(this->master_salt, master_salt, master_salt_length);
147 if (master_key_length > 0) {
148 memset(master_key, 0, master_key_length);
149 master_key_length = 0;
152 if (master_salt_length > 0) {
153 memset(master_salt, 0, master_salt_length);
154 master_salt_length = 0;
214 unsigned char iv[16];
215 memcpy( iv,
k_s, 4 );
218 for(i = 4; i < 8; i++ ){
219 iv[i] = ( 0xFF & ( ssrc >> ((7-i)*8) ) ) ^
k_s[i];
221 for(i = 8; i < 14; i++ ){
222 iv[i] = ( 0xFF & (
unsigned char)( index >> ((13-i)*8) ) ) ^
k_s[i];
241 unsigned char iv[16];
242 uint32 *ui32p = (uint32 *)iv;
248 ui32p[3] = htonl(roc);
265 unsigned char temp[20];
266 const unsigned char* chunks[3];
267 unsigned int chunkLength[3];
268 uint32_t beRoc = htonl(roc);
273 chunks[1] = (
unsigned char *)&beRoc;
300 static void computeIv(
unsigned char* iv, uint64 label, uint64 index,
301 int64 kdv,
unsigned char* master_salt)
307 key_id = label << 48;
310 key_id = ((label << 48) | (index / kdv));
323 for(i = 0; i < 7 ; i++ ) {
324 iv[i] = master_salt[i];
327 for(i = 7; i < 14 ; i++ ) {
328 iv[i] = (
unsigned char)(0xFF & (key_id >> (8*(13-i)))) ^
343 cipher->setNewKey(master_key, master_key_length);
344 memset(master_key, 0, master_key_length);
348 computeIv(iv, label, index, key_deriv_rate, master_salt);
353 computeIv(iv, label, index, key_deriv_rate, master_salt);
370 computeIv(iv, label, index, key_deriv_rate, master_salt);
372 memset(master_salt, 0, master_salt_length);
395 if (new_seq_nb -
s_l > 32768) {
403 if (
s_l - 32768 > new_seq_nb) {
430 uint64 guessed_index =
guessIndex( new_seq_nb );
431 uint64 local_index = (((uint64_t)roc) << 16) |
s_l;
433 int64 delta = guessed_index - local_index;
462 int64 delta =
guessIndex(new_seq_nb) - (((uint64)roc) << 16 |
s_l );
474 if( new_seq_nb >
s_l ) {
494 this->master_key_length,
496 this->master_salt_length,
Implments the SRTP encryption modes as defined in RFC3711.
bool isPadded() const
Ask whether the packet contains padding bytes at the end.
A base class for both IncomingRTPPkt and OutgoingRTPPkt.
void srtpAuthenticate(RTPPacket *rtp, uint32 roc, uint8 *tag)
Compute the authentication tag.
#define REPLAY_WINDOW_SIZE
The implementation for a SRTP cryptographic context.
void hmacSha1Ctx(void *ctx, const uint8_t *data, uint32_t data_length, uint8_t *mac, int32_t *mac_length)
Compute SHA1 HMAC.
const int SrtpEncryptionAESF8
void * createSha1HmacContext(uint8_t *key, int32_t key_length)
Create and initialize a SHA1 HMAC context.
void macSkeinCtx(void *ctx, const uint8_t *data, uint32_t data_length, uint8_t *mac)
Compute Skein MAC.
void freeSkeinMacContext(void *ctx)
Free Skein MAC context.
void freeSha1HmacContext(void *ctx)
Free SHA1 HMAC context.
void * createSkeinMacContext(uint8_t *key, int32_t key_length, int32_t mac_length, SkeinSize_t skeinSize)
Create and initialize a Skein MAC context.
const int SrtpEncryptionTWOF8
Functions to compute SHA1 HAMAC.
bool checkReplay(uint16 newSeqNumber)
Check for packet replay.
void srtpEncrypt(RTPPacket *rtp, uint64 index, uint32 ssrc)
Perform SRTP encryption.
const uint8 *const getPayload() const
~CryptoContext()
Destructor.
uint32 master_salt_length
const int SrtpEncryptionTWOCM
CryptoContext * newCryptoContextForSSRC(uint32 ssrc, int roc, int64 keyDerivRate)
Derive a new Crypto Context for use with a new SSRC.
Function that provide Skein MAC support.
uint32 getRawPacketSize() const
Get the raw packet length, including header, extension, payload and padding.
const int SrtpAuthenticationSkeinHmac
const unsigned char *const getRawPacket() const
Get the raw packet as it will be sent through the network.
uint8 getPaddingSize() const
Get the number of octets padding the end of the payload section.
CryptoContext(uint32 ssrc)
Constructor for empty SRTP cryptographic context.
void update(uint16 newSeqNumber)
Update the SRTP packet index.
uint32 getPayloadSize() const
const int SrtpEncryptionNull
int32 getTagLength() const
Get the length of the SRTP authentication tag in bytes.
const int SrtpAuthenticationSha1Hmac
void deriveSrtpKeys(uint64 index)
Perform key derivation according to SRTP specification.
uint64 guessIndex(uint16 newSeqNumber)
Compute (guess) the new SRTP index based on the sequence number of a received RTP packet...
const int SrtpAuthenticationNull
const int SrtpEncryptionAESCM