34 #ifndef SRTPSYMCRYPTO_H
35 #define SRTPSYMCRYPTO_H
48 #ifndef SRTP_BLOCK_SIZE
49 #define SRTP_BLOCK_SIZE 16
105 void encrypt(
const uint8_t* input, uint8_t* output );
119 bool setNewKey(
const uint8_t* key, int32_t keyLength);
156 void ctr_encrypt(
const uint8_t* input, uint32_t inputLen, uint8_t* output, uint8_t* iv );
174 void ctr_encrypt(uint8_t* data, uint32_t data_length, uint8_t* iv );
251 #pragma GCC visibility push(default)
253 #pragma GCC visibility pop
262 #if defined(_MSC_VER) || defined(WIN32) || defined(_WIN32)
265 #include <arpa/inet.h>
270 static void hexdump(
const char* title,
const unsigned char *s,
int l)
274 if (s == NULL)
return;
276 fprintf(stderr,
"%s",title);
277 for( ; n < l ; ++n) {
279 fprintf(stderr,
"\n%04x",n);
280 fprintf(stderr,
" %02x",s[n]);
282 fprintf(stderr,
"\n");
288 static unsigned char salt[] = {0x32, 0xf2, 0x87, 0x0d};
290 static unsigned char iv[] = { 0x00, 0x6e, 0x5c, 0xba, 0x50, 0x68, 0x1d, 0xe5,
291 0x5c, 0x62, 0x15, 0x99, 0xd4, 0x62, 0x56, 0x4a};
293 static unsigned char key[]= { 0x23, 0x48, 0x29, 0x00, 0x84, 0x67, 0xbe, 0x18,
294 0x6c, 0x3d, 0xe1, 0x4a, 0xae, 0x72, 0xd6, 0x2c};
296 static unsigned char payload[] = {
297 0x70, 0x73, 0x65, 0x75, 0x64, 0x6f, 0x72, 0x61,
298 0x6e, 0x64, 0x6f, 0x6d, 0x6e, 0x65, 0x73, 0x73,
299 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20,
300 0x6e, 0x65, 0x78, 0x74, 0x20, 0x62, 0x65, 0x73,
301 0x74, 0x20, 0x74, 0x68, 0x69, 0x6e, 0x67};
303 static unsigned char cipherText[] = {
304 0x01, 0x9c, 0xe7, 0xa2, 0x6e, 0x78, 0x54, 0x01,
305 0x4a, 0x63, 0x66, 0xaa, 0x95, 0xd4, 0xee, 0xfd,
306 0x1a, 0xd4, 0x17, 0x2a, 0x14, 0xf9, 0xfa, 0xf4,
307 0x55, 0xb7, 0xf1, 0xd4, 0xb6, 0x2b, 0xd0, 0x8f,
308 0x56, 0x2c, 0x0e, 0xef, 0x7c, 0x48, 0x02};
314 static unsigned char rtpPacket[] = {
315 0x80, 0x6e, 0x5c, 0xba, 0x50, 0x68, 0x1d, 0xe5,
316 0x5c, 0x62, 0x15, 0x99,
317 0x70, 0x73, 0x65, 0x75, 0x64, 0x6f, 0x72, 0x61,
318 0x6e, 0x64, 0x6f, 0x6d, 0x6e, 0x65, 0x73, 0x73,
319 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20,
320 0x6e, 0x65, 0x78, 0x74, 0x20, 0x62, 0x65, 0x73,
321 0x74, 0x20, 0x74, 0x68, 0x69, 0x6e, 0x67};
322 static uint32_t ROC = 0xd462564a;
339 unsigned char derivedIv[16];
340 uint32_t* ui32p = (uint32_t*)derivedIv;
342 memcpy(derivedIv, rtpPacket, 12);
346 ui32p[3] = htonl(ROC);
350 if (memcmp(iv, derivedIv, 16) != 0) {
351 cerr <<
"Wrong IV constructed" << endl;
352 hexdump(
"derivedIv", derivedIv, 16);
353 hexdump(
"test vector Iv", iv, 16);
357 aesCipher->
f8_deriveForIV(f8AesCipher, key,
sizeof(key), salt,
sizeof(salt));
360 aesCipher->
f8_encrypt(rtpPacket + 12,
sizeof(rtpPacket)-12+pad,
361 derivedIv, f8AesCipher);
364 if (memcmp(rtpPacket+12, cipherText,
sizeof(rtpPacket)-12+pad) != 0) {
365 cerr <<
"cipher data mismatch" << endl;
366 hexdump(
"computed cipher data", rtpPacket+12,
sizeof(rtpPacket)-12+pad);
367 hexdump(
"Test vcetor cipher data", cipherText,
sizeof(cipherText));
372 aesCipher->
f8_encrypt(rtpPacket+12,
sizeof(rtpPacket)-12+pad, derivedIv, f8AesCipher);
375 if (memcmp(rtpPacket+12, payload,
sizeof(rtpPacket)-12+pad) != 0) {
376 cerr <<
"payload data mismatch" << endl;
377 hexdump(
"computed payload data", rtpPacket+12,
sizeof(rtpPacket)-12+pad);
378 hexdump(
"Test vector payload data", payload,
sizeof(payload));
Implments the SRTP encryption modes as defined in RFC3711.
bool setNewKey(const uint8_t *key, int32_t keyLength)
Set new key.
unsigned char * ivAccent
second IV
const int SrtpEncryptionAESF8
void get_ctr_cipher_stream(uint8_t *output, uint32_t length, uint8_t *iv)
Computes the cipher stream for AES CM mode.
struct _f8_ctx F8_CIPHER_CTX
int processBlock(F8_CIPHER_CTX *f8ctx, const uint8_t *in, int32_t length, uint8_t *out)
SrtpSymCrypto(int algo=SrtpEncryptionAESCM)
void ctr_encrypt(const uint8_t *input, uint32_t inputLen, uint8_t *output, uint8_t *iv)
Counter-mode encryption.
void f8_encrypt(const uint8_t *data, uint32_t dataLen, uint8_t *iv, SrtpSymCrypto *f8Cipher)
AES F8 mode encryption, in place.
unsigned char * S
Intermetiade buffer.
void f8_deriveForIV(SrtpSymCrypto *f8Cipher, uint8_t *key, int32_t keyLen, uint8_t *salt, int32_t saltLen)
Derive a AES context to compute the IV'.
const int SrtpEncryptionAESCM
void encrypt(const uint8_t *input, uint8_t *output)
Encrypts the inpout to the output.