Commit [1d83e2] Maximize Restore History

Modify ccrtp/crypto sources to match the master sources.

The sources in ccrtp/crypto are now in sync with the master sources in libzrtpcpp/srtp/crypto (see
my libzrtpcpp project at GitHub). The modifications als required some small changes in the build
files (cmake and automake). Currently only the realy SRTP crypto contexts are not yet in sync
and must be handled separately in necessary.

wernerd wernerd 2012-03-08

1 2 > >> (Page 1 of 2)
changed demo/Makefile.am
changed demo/ccsrtptest.cpp
changed src/ccrtp/crypto/gcrypt/InitializeGcrypt.cpp
changed src/ccrtp/crypto/gcrypt/gcrypthmac.cpp
changed src/ccrtp/crypto/openssl/hmac.cpp
changed src/ccrtp/crypto/Makefile.am
changed src/ccrtp/crypto/hmac.h
changed src/ccrtp/crypto/macSkein.cpp
changed src/ccrtp/crypto/macSkein.h
changed src/ccrtp/crypto/skein.c
changed src/ccrtp/crypto/skein.h
changed src/ccrtp/crypto/skeinApi.c
changed src/ccrtp/crypto/skeinApi.h
changed src/ccrtp/crypto/skein_block.c
changed src/ccrtp/crypto/skein_iv.h
changed src/ccrtp/crypto/skein_port.h
changed src/ccrtp/CryptoContext.h
changed src/ccrtp/CryptoContextCtrl.h
changed src/ccrtp/Makefile.am
copied src/ccrtp/crypto/gcrypt/gcryptAesSrtp.cpp -> src/ccrtp/crypto/gcrypt/gcryptSrtpSymCrypto.cpp
copied src/ccrtp/crypto/openssl/AesSrtp.cpp -> src/ccrtp/crypto/openssl/SrtpSymCrypto.cpp
copied src/ccrtp/crypto/AesSrtp.h -> src/ccrtp/crypto/SrtpSymCrypto.h
demo/Makefile.am Diff Switch to side-by-side view
Loading...
demo/ccsrtptest.cpp Diff Switch to side-by-side view
Loading...
src/ccrtp/crypto/gcrypt/InitializeGcrypt.cpp Diff Switch to side-by-side view
Loading...
src/ccrtp/crypto/gcrypt/gcrypthmac.cpp Diff Switch to side-by-side view
Loading...
src/ccrtp/crypto/openssl/hmac.cpp Diff Switch to side-by-side view
Loading...
src/ccrtp/crypto/Makefile.am Diff Switch to side-by-side view
Loading...
src/ccrtp/crypto/hmac.h Diff Switch to side-by-side view
Loading...
src/ccrtp/crypto/macSkein.cpp Diff Switch to side-by-side view
Loading...
src/ccrtp/crypto/macSkein.h Diff Switch to side-by-side view
Loading...
src/ccrtp/crypto/skein.c Diff Switch to side-by-side view
Loading...
src/ccrtp/crypto/skein.h Diff Switch to side-by-side view
Loading...
src/ccrtp/crypto/skeinApi.c Diff Switch to side-by-side view
Loading...
src/ccrtp/crypto/skeinApi.h Diff Switch to side-by-side view
Loading...
src/ccrtp/crypto/skein_block.c Diff Switch to side-by-side view
Loading...
src/ccrtp/crypto/skein_iv.h Diff Switch to side-by-side view
Loading...
src/ccrtp/crypto/skein_port.h Diff Switch to side-by-side view
Loading...
src/ccrtp/CryptoContext.h Diff Switch to side-by-side view
Loading...
src/ccrtp/CryptoContextCtrl.h Diff Switch to side-by-side view
Loading...
src/ccrtp/Makefile.am Diff Switch to side-by-side view
Loading...
src/ccrtp/crypto/gcrypt/gcryptAesSrtp.cpp to src/ccrtp/crypto/gcrypt/gcryptSrtpSymCrypto.cpp
--- a/src/ccrtp/crypto/gcrypt/gcryptAesSrtp.cpp
+++ b/src/ccrtp/crypto/gcrypt/gcryptSrtpSymCrypto.cpp
@@ -37,31 +37,32 @@
 
 extern void initializeGcrypt();
 
+#define MAKE_F8_TEST
+
 #include <gcrypt.h>            // the include of gcrypt
 #include <stdlib.h>
-#include <ccrtp/crypto/AesSrtp.h>
-#include <ccrtp/crypto/twofish.h>
+#include <crypto/SrtpSymCrypto.h>
+#include <crypto/twofish.h>
 
 #include <stdio.h>
-#include <libzrtpcpp/ZrtpCallback.h>
-
-AesSrtp::AesSrtp(int algo) : key(NULL), algorithm(algo) {
+
+SrtpSymCrypto::SrtpSymCrypto(int algo) : key(NULL), algorithm(algo) {
     initializeGcrypt();
 }
 
-AesSrtp::AesSrtp( uint8* k, int32 keyLength, int algo) :
+SrtpSymCrypto::SrtpSymCrypto( uint8_t* k, int32_t keyLength, int algo) :
     key(NULL),  algorithm(algo) {
 
     initializeGcrypt();
     setNewKey(k, keyLength);
 }
 
-AesSrtp::~AesSrtp() {
+SrtpSymCrypto::~SrtpSymCrypto() {
     if (key) {
         if (algorithm == SrtpEncryptionAESCM)
             gcry_cipher_close(static_cast<gcry_cipher_hd_t>(key));
         else if (algorithm == SrtpEncryptionTWOCM)
-            delete[] (uint8*)key;
+            delete[] (uint8_t*)key;
 
         key = NULL;
     }
@@ -69,10 +70,10 @@
 
 static int twoFishInit = 0;
 
-bool AesSrtp::setNewKey(const uint8* k, int32 keyLength) {
+bool SrtpSymCrypto::setNewKey(const uint8_t* k, int32_t keyLength) {
 
     // release an existing key before setting a new one
-    if (algorithm == SrtpEncryptionAESCM) {
+    if (algorithm == SrtpEncryptionAESCM || algorithm == SrtpEncryptionAESF8) {
         if (key != NULL) {
             gcry_cipher_close(static_cast<gcry_cipher_hd_t>(key));
             key = NULL;
@@ -93,15 +94,15 @@
         key = tmp;
         gcry_cipher_setkey(static_cast<gcry_cipher_hd_t>(key), k, keyLength);
     }
-    else if (algorithm == SrtpEncryptionTWOCM) {
+    else if (algorithm == SrtpEncryptionTWOCM || algorithm == SrtpEncryptionTWOF8) {
         if (!twoFishInit) {
             Twofish_initialise();
             twoFishInit = 1;
         }
         if (key != NULL)
-            delete[] (uint8*)key;
-
-        key = new uint8[sizeof(Twofish_key)];
+            delete[] (uint8_t*)key;
+
+        key = new uint8_t[sizeof(Twofish_key)];
         memset(key, 0, sizeof(Twofish_key));
         Twofish_prepare_key((Twofish_Byte*)k, keyLength,  (Twofish_key*)key);
     }
@@ -112,7 +113,7 @@
 }
 
 
-void AesSrtp::encrypt(const uint8* input, uint8* output) {
+void SrtpSymCrypto::encrypt(const uint8_t* input, uint8_t* output) {
     if (key != NULL) {
         if (algorithm == SrtpEncryptionAESCM)
             gcry_cipher_encrypt (static_cast<gcry_cipher_hd_t>(key),
@@ -123,42 +124,42 @@
         }
 }
 
-void AesSrtp::get_ctr_cipher_stream( uint8* output, uint32 length,
-                                     uint8* iv ) {
-    uint16 ctr = 0;
+void SrtpSymCrypto::get_ctr_cipher_stream( uint8_t* output, uint32_t length,
+                                     uint8_t* iv ) {
+    uint16_t ctr = 0;
 
     unsigned char temp[SRTP_BLOCK_SIZE];
 
     for(ctr = 0; ctr < length/SRTP_BLOCK_SIZE; ctr++ ){
         //compute the cipher stream
-        iv[14] = (uint8)((ctr & 0xFF00) >>  8);
-        iv[15] = (uint8)((ctr & 0x00FF));
+        iv[14] = (uint8_t)((ctr & 0xFF00) >>  8);
+        iv[15] = (uint8_t)((ctr & 0x00FF));
 
         encrypt(iv, &output[ctr*SRTP_BLOCK_SIZE]);
     }
     if ((length % SRTP_BLOCK_SIZE) > 0) {
         // Treat the last bytes:
-        iv[14] = (uint8)((ctr & 0xFF00) >>  8);
-        iv[15] = (uint8)((ctr & 0x00FF));
+        iv[14] = (uint8_t)((ctr & 0xFF00) >>  8);
+        iv[15] = (uint8_t)((ctr & 0x00FF));
 
         encrypt(iv, temp);
         memcpy(&output[ctr*SRTP_BLOCK_SIZE], temp, length % SRTP_BLOCK_SIZE);
     }
 }
 
-void AesSrtp::ctr_encrypt( const uint8* input, uint32 input_length,
-               uint8* output, uint8* iv ) {
+void SrtpSymCrypto::ctr_encrypt( const uint8_t* input, uint32_t input_length,
+			   uint8_t* output, uint8_t* iv ) {
 
     if (key == NULL)
         return;
 
-    uint16 ctr = 0;
+    uint16_t ctr = 0;
     unsigned char temp[SRTP_BLOCK_SIZE];
 
     int l = input_length/SRTP_BLOCK_SIZE;
     for ( ctr = 0; ctr < l; ctr++ ) {
-        iv[14] = (uint8)((ctr & 0xFF00) >>  8);
-        iv[15] = (uint8)((ctr & 0x00FF));
+        iv[14] = (uint8_t)((ctr & 0xFF00) >>  8);
+        iv[15] = (uint8_t)((ctr & 0x00FF));
 
         encrypt(iv, temp);
         for (int i = 0; i < SRTP_BLOCK_SIZE; i++ ) {
@@ -169,8 +170,8 @@
     l = input_length % SRTP_BLOCK_SIZE;
     if (l > 0) {
         // Treat the last bytes:
-        iv[14] = (uint8)((ctr & 0xFF00) >>  8);
-        iv[15] = (uint8)((ctr & 0x00FF));
+        iv[14] = (uint8_t)((ctr & 0xFF00) >>  8);
+        iv[15] = (uint8_t)((ctr & 0x00FF));
 
         encrypt(iv, temp);
         for (int i = 0; i < l; i++ ) {
@@ -179,18 +180,18 @@
     }
 }
 
-void AesSrtp::ctr_encrypt( uint8* data, uint32 data_length, uint8* iv ) {
+void SrtpSymCrypto::ctr_encrypt( uint8_t* data, uint32_t data_length, uint8_t* iv ) {
 
     if (key == NULL)
         return;
-
-    uint16 ctr = 0;
+    
+    uint16_t ctr = 0;
     unsigned char temp[SRTP_BLOCK_SIZE];
 
     int l = data_length/SRTP_BLOCK_SIZE;
     for (ctr = 0; ctr < l; ctr++ ) {
-        iv[14] = (uint8)((ctr & 0xFF00) >>  8);
-        iv[15] = (uint8)((ctr & 0x00FF));
+        iv[14] = (uint8_t)((ctr & 0xFF00) >>  8);
+        iv[15] = (uint8_t)((ctr & 0x00FF));
 
         encrypt(iv, temp);
         for (int i = 0; i < SRTP_BLOCK_SIZE; i++ ) {
@@ -201,8 +202,8 @@
     l = data_length % SRTP_BLOCK_SIZE;
     if (l > 0) {
         // Treat the last bytes:
-        iv[14] = (uint8)((ctr & 0xFF00) >>  8);
-        iv[15] = (uint8)((ctr & 0x00FF));
+        iv[14] = (uint8_t)((ctr & 0xFF00) >>  8);
+        iv[15] = (uint8_t)((ctr & 0x00FF));
 
         encrypt(iv, temp);
         for (int i = 0; i < l; i++ ) {
@@ -212,15 +213,15 @@
 
 }
 
-void AesSrtp::f8_encrypt(const uint8* data, uint32 data_length, uint8* iv, AesSrtp* f8Cipher ) {
-
-    f8_encrypt(data, data_length, const_cast<uint8*>(data), iv, f8Cipher);
+void SrtpSymCrypto::f8_encrypt(const uint8_t* data, uint32_t data_length, uint8_t* iv, SrtpSymCrypto* f8Cipher ) {
+
+    f8_encrypt(data, data_length, const_cast<uint8_t*>(data), iv, f8Cipher);
 }
 
 #define MAX_KEYLEN 32
 
-void AesSrtp::f8_deriveForIV(AesSrtp* f8Cipher, uint8* key, int32 keyLen,
-             uint8* salt, int32 saltLen) {
+void SrtpSymCrypto::f8_deriveForIV(SrtpSymCrypto* f8Cipher, uint8_t* key, int32_t keyLen,
+             uint8_t* salt, int32_t saltLen) {
 
     unsigned char *cp_in, *cp_in1, *cp_out;
 
@@ -255,8 +256,8 @@
     f8Cipher->setNewKey(maskedKey, keyLen);
 }
 
-void AesSrtp::f8_encrypt(const uint8* in, uint32 in_length, uint8* out,
-                         uint8* iv, AesSrtp* f8Cipher ) {
+void SrtpSymCrypto::f8_encrypt(const uint8_t* in, uint32_t in_length, uint8_t* out,
+                         uint8_t* iv, SrtpSymCrypto* f8Cipher ) {
 
     int offset = 0;
 
@@ -292,11 +293,11 @@
     }
 }
 
-int AesSrtp::processBlock(F8_CIPHER_CTX *f8ctx, const uint8* in, int32 length, uint8* out) {
+int SrtpSymCrypto::processBlock(F8_CIPHER_CTX *f8ctx, const uint8_t* in, int32_t length, uint8_t* out) {
 
     int i;
-    const uint8 *cp_in;
-    uint8* cp_in1, *cp_out;
+    const uint8_t *cp_in;
+    uint8_t* cp_in1, *cp_out;
     uint32_t *ui32p;
 
     /*
src/ccrtp/crypto/openssl/AesSrtp.cpp to src/ccrtp/crypto/openssl/SrtpSymCrypto.cpp
--- a/src/ccrtp/crypto/openssl/AesSrtp.cpp
+++ b/src/ccrtp/crypto/openssl/SrtpSymCrypto.cpp
@@ -35,48 +35,51 @@
  * @author Werner Dittmann <Werner.Dittmann@t-online.de>
  */
 
+#define MAKE_F8_TEST
+
 #include <stdlib.h>
 #include <openssl/aes.h>                // the include of openSSL
-#include <ccrtp/crypto/AesSrtp.h>
-#include <ccrtp/crypto/twofish.h>
+#include <crypto/SrtpSymCrypto.h>
+#include <crypto/twofish.h>
 #include <string.h>
 #include <stdio.h>
-
-AesSrtp::AesSrtp(int algo):key(NULL), algorithm(algo) {
-}
-
-AesSrtp::AesSrtp( uint8* k, int32 keyLength, int algo ):
+#include <arpa/inet.h>
+
+SrtpSymCrypto::SrtpSymCrypto(int algo):key(NULL), algorithm(algo) {
+}
+
+SrtpSymCrypto::SrtpSymCrypto( uint8_t* k, int32_t keyLength, int algo ):
     key(NULL), algorithm(algo) {
 
     setNewKey(k, keyLength);
 }
 
-AesSrtp::~AesSrtp() {
+SrtpSymCrypto::~SrtpSymCrypto() {
     if (key != NULL)
-        delete[] (uint8*)key;
+        delete[] (uint8_t*)key;
 }
 
 static int twoFishInit = 0;
 
-bool AesSrtp::setNewKey(const uint8* k, int32 keyLength) {
+bool SrtpSymCrypto::setNewKey(const uint8_t* k, int32_t keyLength) {
     // release an existing key before setting a new one
     if (key != NULL)
-        delete[] (uint8*)key;
+        delete[] (uint8_t*)key;
 
     if (!(keyLength == 16 || keyLength == 32)) {
         return false;
     }
-    if (algorithm == SrtpEncryptionAESCM) {
-        key = new uint8[sizeof(AES_KEY)];
+    if (algorithm == SrtpEncryptionAESCM || algorithm == SrtpEncryptionAESF8) {
+        key = new uint8_t[sizeof(AES_KEY)];
         memset(key, 0, sizeof(AES_KEY) );
         AES_set_encrypt_key(k, keyLength*8, (AES_KEY *)key);
     }
-    else if (algorithm == SrtpEncryptionTWOCM) {
+    else if (algorithm == SrtpEncryptionTWOCM || algorithm == SrtpEncryptionTWOF8) {
         if (!twoFishInit) {
             Twofish_initialise();
             twoFishInit = 1;
         }
-        key = new uint8[sizeof(Twofish_key)];
+        key = new uint8_t[sizeof(Twofish_key)];
         memset(key, 0, sizeof(Twofish_key));
         Twofish_prepare_key((Twofish_Byte*)k, keyLength,  (Twofish_key*)key);
     }
@@ -87,7 +90,7 @@
 }
 
 
-void AesSrtp::encrypt(const uint8* input, uint8* output ) {
+void SrtpSymCrypto::encrypt(const uint8_t* input, uint8_t* output ) {
     if (algorithm == SrtpEncryptionAESCM) {
         AES_encrypt(input, output, (AES_KEY *)key);
     }
@@ -97,41 +100,41 @@
     }
 }
 
-void AesSrtp::get_ctr_cipher_stream(uint8* output, uint32 length,
-                                    uint8* iv ) {
-    uint16 ctr = 0;
+void SrtpSymCrypto::get_ctr_cipher_stream(uint8_t* output, uint32_t length,
+                                    uint8_t* iv ) {
+    uint16_t ctr = 0;
     unsigned char temp[SRTP_BLOCK_SIZE];
 
     for(ctr = 0; ctr < length/SRTP_BLOCK_SIZE; ctr++) {
         //compute the cipher stream
-        iv[14] = (uint8)((ctr & 0xFF00) >>  8);
-        iv[15] = (uint8)((ctr & 0x00FF));
+        iv[14] = (uint8_t)((ctr & 0xFF00) >>  8);
+        iv[15] = (uint8_t)((ctr & 0x00FF));
 
         encrypt(iv, &output[ctr*SRTP_BLOCK_SIZE]);
     }
     if ((length % SRTP_BLOCK_SIZE) > 0) {
         // Treat the last bytes:
-        iv[14] = (uint8)((ctr & 0xFF00) >>  8);
-        iv[15] = (uint8)((ctr & 0x00FF));
+        iv[14] = (uint8_t)((ctr & 0xFF00) >>  8);
+        iv[15] = (uint8_t)((ctr & 0x00FF));
 
         encrypt(iv, temp);
         memcpy(&output[ctr*SRTP_BLOCK_SIZE], temp, length % SRTP_BLOCK_SIZE );
     }
 }
 
-void AesSrtp::ctr_encrypt(const uint8* input, uint32 input_length,
-                           uint8* output, uint8* iv ) {
+void SrtpSymCrypto::ctr_encrypt(const uint8_t* input, uint32_t input_length,
+                           uint8_t* output, uint8_t* iv ) {
 
     if (key == NULL)
         return;
 
-    uint16 ctr = 0;
+    uint16_t ctr = 0;
     unsigned char temp[SRTP_BLOCK_SIZE];
 
     int l = input_length/SRTP_BLOCK_SIZE;
     for (ctr = 0; ctr < l; ctr++ ) {
-        iv[14] = (uint8)((ctr & 0xFF00) >>  8);
-        iv[15] = (uint8)((ctr & 0x00FF));
+        iv[14] = (uint8_t)((ctr & 0xFF00) >>  8);
+        iv[15] = (uint8_t)((ctr & 0x00FF));
 
         encrypt(iv, temp);
         for (int i = 0; i < SRTP_BLOCK_SIZE; i++ ) {
@@ -142,8 +145,8 @@
     l = input_length % SRTP_BLOCK_SIZE;
     if (l > 0) {
         // Treat the last bytes:
-        iv[14] = (uint8)((ctr & 0xFF00) >>  8);
-        iv[15] = (uint8)((ctr & 0x00FF));
+        iv[14] = (uint8_t)((ctr & 0xFF00) >>  8);
+        iv[15] = (uint8_t)((ctr & 0x00FF));
 
         encrypt(iv, temp);
         for (int i = 0; i < l; i++ ) {
@@ -152,18 +155,18 @@
     }
 }
 
-void AesSrtp::ctr_encrypt( uint8* data, uint32 data_length, uint8* iv ) {
+void SrtpSymCrypto::ctr_encrypt( uint8_t* data, uint32_t data_length, uint8_t* iv ) {
 
     if (key == NULL)
         return;
 
-    uint16 ctr = 0;
+    uint16_t ctr = 0;
     unsigned char temp[SRTP_BLOCK_SIZE];
 
     int l = data_length/SRTP_BLOCK_SIZE;
     for (ctr = 0; ctr < l; ctr++ ) {
-        iv[14] = (uint8)((ctr & 0xFF00) >>  8);
-        iv[15] = (uint8)((ctr & 0x00FF));
+        iv[14] = (uint8_t)((ctr & 0xFF00) >>  8);
+        iv[15] = (uint8_t)((ctr & 0x00FF));
 
         encrypt(iv, temp);
         for (int i = 0; i < SRTP_BLOCK_SIZE; i++ ) {
@@ -174,8 +177,8 @@
     l = data_length % SRTP_BLOCK_SIZE;
     if (l > 0) {
         // Treat the last bytes:
-        iv[14] = (uint8)((ctr & 0xFF00) >>  8);
-        iv[15] = (uint8)((ctr & 0x00FF));
+        iv[14] = (uint8_t)((ctr & 0xFF00) >>  8);
+        iv[15] = (uint8_t)((ctr & 0x00FF));
 
         encrypt(iv, temp);
         for (int i = 0; i < l; i++ ) {
@@ -184,16 +187,16 @@
     }
 }
 
-void AesSrtp::f8_encrypt(const uint8* data, uint32 data_length,
-                         uint8* iv, AesSrtp* f8Cipher ) {
-
-    f8_encrypt(data, data_length, const_cast<uint8*>(data), iv, f8Cipher);
+void SrtpSymCrypto::f8_encrypt(const uint8_t* data, uint32_t data_length,
+                         uint8_t* iv, SrtpSymCrypto* f8Cipher ) {
+
+    f8_encrypt(data, data_length, const_cast<uint8_t*>(data), iv, f8Cipher);
 }
 
 #define MAX_KEYLEN 32
 
-void AesSrtp::f8_deriveForIV(AesSrtp* f8Cipher, uint8* key, int32 keyLen,
-             uint8* salt, int32 saltLen) {
+void SrtpSymCrypto::f8_deriveForIV(SrtpSymCrypto* f8Cipher, uint8_t* key, int32_t keyLen,
+             uint8_t* salt, int32_t saltLen) {
 
     unsigned char *cp_in, *cp_in1, *cp_out;
 
@@ -228,8 +231,8 @@
     f8Cipher->setNewKey(maskedKey, keyLen);
 }
 
-void AesSrtp::f8_encrypt(const uint8* in, uint32 in_length, uint8* out,
-                         uint8* iv, AesSrtp* f8Cipher ) {
+void SrtpSymCrypto::f8_encrypt(const uint8_t* in, uint32_t in_length, uint8_t* out,
+                         uint8_t* iv, SrtpSymCrypto* f8Cipher ) {
 
 
     int offset = 0;
@@ -251,7 +254,7 @@
     f8Cipher->encrypt(iv, f8ctx.ivAccent);
 
     f8ctx.J = 0;                       // initialize the counter
-    f8ctx.S = S;		       // get the key stream buffer
+    f8ctx.S = S;               // get the key stream buffer
 
     memset(f8ctx.S, 0, SRTP_BLOCK_SIZE); // initial value for key stream
 
@@ -265,11 +268,11 @@
     }
 }
 
-int AesSrtp::processBlock(F8_CIPHER_CTX *f8ctx, const uint8* in, int32 length, uint8* out) {
+int SrtpSymCrypto::processBlock(F8_CIPHER_CTX *f8ctx, const uint8_t* in, int32_t length, uint8_t* out) {
 
     int i;
-    const uint8 *cp_in;
-    uint8* cp_in1, *cp_out;
+    const uint8_t *cp_in;
+    uint8_t* cp_in1, *cp_out;
     uint32_t *ui32p;
 
     /*
src/ccrtp/crypto/AesSrtp.h to src/ccrtp/crypto/SrtpSymCrypto.h
--- a/src/ccrtp/crypto/AesSrtp.h
+++ b/src/ccrtp/crypto/SrtpSymCrypto.h
@@ -1,5 +1,5 @@
 /*
-  Copyright (C) 2005, 2004 Erik Eliasson, Johan Bilien
+  Copyright (C) 2005, 2004, 2010, 2012 Erik Eliasson, Johan Bilien, Werner Dittmann
 
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
@@ -29,6 +29,32 @@
  * files in the program, then also delete it here.
 */
 
+
+
+#ifndef SRTPSYMCRYPTO_H
+#define SRTPSYMCRYPTO_H
+
+/**
+ * @file SrtpSymCrypto.h
+ * @brief Class which implements SRTP AES cryptographic functions
+ * 
+ * @ingroup GNU_ZRTP
+ * @{
+ */
+
+#include <stdint.h>
+#include <CryptoContext.h>
+
+#ifndef SRTP_BLOCK_SIZE
+#define SRTP_BLOCK_SIZE 16
+#endif
+
+typedef struct _f8_ctx {
+    unsigned char *S;           ///< Intermetiade buffer
+    unsigned char *ivAccent;    ///< second IV
+    uint32_t J;                 ///< Counter
+} F8_CIPHER_CTX;
+
 /**
  * Implments the SRTP encryption modes as defined in RFC3711
  *
@@ -39,7 +65,7 @@
  * Both modes are desinged to encrypt/decrypt data of arbitrary length
  * (with a specified upper limit, refer to RFC 3711). These modes do
  * <em>not</em> require that the amount of data to encrypt is a multiple
- * of the AES blocksize (128byte), no padding is necessary.
+ * of the AES blocksize (16 bytes), no padding is necessary.
  *
  * The implementation uses the openSSL library as its cryptographic
  * backend.
@@ -48,31 +74,21 @@
  * @author Johan Bilien <jobi@via.ecp.fr>
  * @author Werner Dittmann <Werner.Dittmann@t-online.de>
  */
-
-
-#ifndef AESSRTP_H
-#define AESSRTP_H
-
-#include <commoncpp/config.h>
-
-#include <ccrtp/base.h>
-
-#ifndef SRTP_BLOCK_SIZE
-#define SRTP_BLOCK_SIZE 16
-#endif
-
-typedef struct _f8_ctx {
-    unsigned char *S;
-    unsigned char *ivAccent;
-    uint32_t J;
-} F8_CIPHER_CTX;
-
-
-class __EXPORT AesSrtp {
+class SrtpSymCrypto {
 public:
-    AesSrtp(int algo = SrtpEncryptionAESCM);
-    AesSrtp(uint8* key, int32 key_length, int algo = SrtpEncryptionAESCM);
-    ~AesSrtp();
+    SrtpSymCrypto(int algo = SrtpEncryptionAESCM);
+
+    /**
+     * Constructor that initializes key data
+     * 
+     * @param key
+     *     Pointer to key bytes.
+     * @param key_length
+     *     Number of key bytes.
+     */
+    SrtpSymCrypto(uint8_t* key, int32_t key_length, int algo = SrtpEncryptionAESCM);
+
+    ~SrtpSymCrypto();
 
     /**
      * Encrypts the inpout to the output.
@@ -86,13 +102,13 @@
      * @param output
      *    Pointer to output block, must be 16 bytes
      */
-    void encrypt( const uint8* input, uint8* output );
+    void encrypt( const uint8_t* input, uint8_t* output );
 
     /**
      * Set new key
      *
      * @param key
-     *   Pointer to key data, must have at least a size of keyLength
+     *   Pointer to key data, must have at least a size of keyLength 
      *
      * @param keyLength
      *   Length of the key in bytes, must be 16, 24, or 32
@@ -100,7 +116,7 @@
      * @return
      *   false if key could not set.
      */
-    bool setNewKey(const uint8* key, int32 keyLength);
+    bool setNewKey(const uint8_t* key, int32_t keyLength);
 
     /**
      * Computes the cipher stream for AES CM mode.
@@ -117,7 +133,7 @@
      *    The initialization vector as input to create the cipher stream.
      *    Refer to chapter 4.1.1 in RFC 3711.
      */
-    void get_ctr_cipher_stream(uint8* output, uint32 length, uint8* iv);
+    void get_ctr_cipher_stream(uint8_t* output, uint32_t length, uint8_t* iv);
 
     /**
      * Counter-mode encryption.
@@ -137,9 +153,7 @@
      *    The initialization vector as input to create the cipher stream.
      *    Refer to chapter 4.1.1 in RFC 3711.
      */
-    void ctr_encrypt( const uint8* input,
-              uint32 inputLen,
-              uint8* output, uint8* iv );
+    void ctr_encrypt(const uint8_t* input, uint32_t inputLen, uint8_t* output, uint8_t* iv );
 
     /**
      * Counter-mode encryption, in place.
@@ -150,16 +164,14 @@
      *    Pointer to input and output block, must be <code>dataLen</code>
      *    bytes.
      *
-     * @param dataLen
+     * @param data_length
      *    Number of bytes to process.
      *
      * @param iv
      *    The initialization vector as input to create the cipher stream.
      *    Refer to chapter 4.1.1 in RFC 3711.
      */
-    void ctr_encrypt( uint8* data,
-              uint32 data_length,
-              uint8* iv );
+    void ctr_encrypt(uint8_t* data, uint32_t data_length, uint8_t* iv );
 
     /**
      * Derive a AES context to compute the IV'.
@@ -181,7 +193,7 @@
      * @param saltLen
      *   length of master salt.
      */
-    void f8_deriveForIV(AesSrtp* f8Cipher, uint8* key, int32 keyLen, uint8* salt, int32 saltLen);
+    void f8_deriveForIV(SrtpSymCrypto* f8Cipher, uint8_t* key, int32_t keyLen, uint8_t* salt, int32_t saltLen);
 
     /**
      * AES F8 mode encryption, in place.
@@ -203,7 +215,7 @@
      * @param f8Cipher
      *   An AES cipher context used to encrypt IV to IV'.
      */
-    void f8_encrypt(const uint8* data, uint32 dataLen, uint8* iv, AesSrtp* f8Cipher);
+    void f8_encrypt(const uint8_t* data, uint32_t dataLen, uint8_t* iv, SrtpSymCrypto* f8Cipher);
 
     /**
      * AES F8 mode encryption.
@@ -228,17 +240,146 @@
      * @param f8Cipher
      *   An AES cipher context used to encrypt IV to IV'.
      */
-    void f8_encrypt(const uint8* data, uint32 dataLen, uint8* out, uint8* iv, AesSrtp* f8Cipher);
-
+    void f8_encrypt(const uint8_t* data, uint32_t dataLen, uint8_t* out, uint8_t* iv, SrtpSymCrypto* f8Cipher);
 
 private:
-    int processBlock(F8_CIPHER_CTX *f8ctx,
-             const uint8* in,
-             int32 length,
-             uint8* out);
+    int processBlock(F8_CIPHER_CTX* f8ctx, const uint8_t* in, int32_t length, uint8_t* out);
     void* key;
     int32_t algorithm;
 };
+
+#pragma GCC visibility push(default)
+int testF8();
+#pragma GCC visibility pop
+
+/* Only SrtpSymCrypto functions define the MAKE_F8_TEST */
+#ifdef MAKE_F8_TEST
+
+#include <cstring>
+#include <iostream>
+#include <cstdio>
+#include <arpa/inet.h>
+
+using namespace std;
+
+static void hexdump(const char* title, const unsigned char *s, int l)
+{
+    int n=0;
+
+    if (s == NULL) return;
+
+    fprintf(stderr, "%s",title);
+    for( ; n < l ; ++n) {
+        if((n%16) == 0)
+            fprintf(stderr, "\n%04x",n);
+        fprintf(stderr, " %02x",s[n]);
+    }
+    fprintf(stderr, "\n");
+}
+
+/*
+ * The F8 test vectors according to RFC3711
+ */
+static unsigned char salt[] = {0x32, 0xf2, 0x87, 0x0d};
+
+static unsigned char iv[] = {  0x00, 0x6e, 0x5c, 0xba, 0x50, 0x68, 0x1d, 0xe5,
+                        0x5c, 0x62, 0x15, 0x99, 0xd4, 0x62, 0x56, 0x4a};
+
+static unsigned char key[]= {  0x23, 0x48, 0x29, 0x00, 0x84, 0x67, 0xbe, 0x18,
+                        0x6c, 0x3d, 0xe1, 0x4a, 0xae, 0x72, 0xd6, 0x2c};
+
+static unsigned char payload[] = {
+                        0x70, 0x73, 0x65, 0x75, 0x64, 0x6f, 0x72, 0x61,
+                        0x6e, 0x64, 0x6f, 0x6d, 0x6e, 0x65, 0x73, 0x73,
+                        0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20,
+                        0x6e, 0x65, 0x78, 0x74, 0x20, 0x62, 0x65, 0x73,
+                        0x74, 0x20, 0x74, 0x68, 0x69, 0x6e, 0x67};  // 39 bytes
+
+static unsigned char cipherText[] = {
+                        0x01, 0x9c, 0xe7, 0xa2, 0x6e, 0x78, 0x54, 0x01,
+                        0x4a, 0x63, 0x66, 0xaa, 0x95, 0xd4, 0xee, 0xfd,
+                        0x1a, 0xd4, 0x17, 0x2a, 0x14, 0xf9, 0xfa, 0xf4,
+                        0x55, 0xb7, 0xf1, 0xd4, 0xb6, 0x2b, 0xd0, 0x8f,
+                        0x56, 0x2c, 0x0e, 0xef, 0x7c, 0x48, 0x02}; // 39 bytes
+
+// static unsigned char rtpPacketHeader[] = {
+//                         0x80, 0x6e, 0x5c, 0xba, 0x50, 0x68, 0x1d, 0xe5,
+//                         0x5c, 0x62, 0x15, 0x99};
+
+static unsigned char rtpPacket[] = {
+                    0x80, 0x6e, 0x5c, 0xba, 0x50, 0x68, 0x1d, 0xe5,
+                    0x5c, 0x62, 0x15, 0x99,                        // header
+                    0x70, 0x73, 0x65, 0x75, 0x64, 0x6f, 0x72, 0x61, // payload
+                    0x6e, 0x64, 0x6f, 0x6d, 0x6e, 0x65, 0x73, 0x73,
+                    0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20,
+                    0x6e, 0x65, 0x78, 0x74, 0x20, 0x62, 0x65, 0x73,
+                    0x74, 0x20, 0x74, 0x68, 0x69, 0x6e, 0x67};
+static uint32_t ROC = 0xd462564a;
+
+int testF8()
+{
+    SrtpSymCrypto* aesCipher = new SrtpSymCrypto();
+    SrtpSymCrypto* f8AesCipher = new SrtpSymCrypto();
+
+    aesCipher->setNewKey(key, sizeof(key));
+
+    /* Create the F8 IV (refer to chapter 4.1.2.2 in RFC 3711):
+     *
+     * IV = 0x00 || M || PT || SEQ  ||      TS    ||    SSRC   ||    ROC
+     *      8Bit  1bit  7bit  16bit       32bit        32bit        32bit
+     * ------------\     /--------------------------------------------------
+     *       XX       XX      XX XX   XX XX XX XX   XX XX XX XX  XX XX XX XX
+     */
+
+    unsigned char derivedIv[16];
+    uint32_t* ui32p = (uint32_t*)derivedIv;
+
+    memcpy(derivedIv, rtpPacket, 12);
+    derivedIv[0] = 0;
+
+    // set ROC in network order into IV
+    ui32p[3] = htonl(ROC);
+
+    int32_t pad = 0;
+
+    if (memcmp(iv, derivedIv, 16) != 0) {
+        cerr << "Wrong IV constructed" << endl;
+        hexdump("derivedIv", derivedIv, 16);
+        hexdump("test vector Iv", iv, 16);
+        return -1;
+    }
+
+    aesCipher->f8_deriveForIV(f8AesCipher, key, sizeof(key), salt, sizeof(salt));
+
+    // now encrypt the RTP payload data
+    aesCipher->f8_encrypt(rtpPacket + 12, sizeof(rtpPacket)-12+pad,
+        derivedIv, f8AesCipher);
+
+    // compare with test vector cipher data
+    if (memcmp(rtpPacket+12, cipherText, sizeof(rtpPacket)-12+pad) != 0) {
+        cerr << "cipher data mismatch" << endl;
+        hexdump("computed cipher data", rtpPacket+12, sizeof(rtpPacket)-12+pad);
+        hexdump("Test vcetor cipher data", cipherText, sizeof(cipherText));
+        return -1;
+    }
+
+    // Now decrypt the data to get the payload data again
+    aesCipher->f8_encrypt(rtpPacket+12, sizeof(rtpPacket)-12+pad, derivedIv, f8AesCipher);
+
+    // compare decrypted data with test vector payload data
+    if (memcmp(rtpPacket+12, payload, sizeof(rtpPacket)-12+pad) != 0) {
+        cerr << "payload data mismatch" << endl;
+        hexdump("computed payload data", rtpPacket+12, sizeof(rtpPacket)-12+pad);
+        hexdump("Test vector payload data", payload, sizeof(payload));
+        return -1;
+    }
+    return 0;
+}
+#endif
+
+/**
+ * @}
+ */
 
 #endif
 
1 2 > >> (Page 1 of 2)