41 #include <openssl/aes.h>
47 #if defined(_MSC_VER) || defined(WIN32) || defined(_WIN32)
50 #include <arpa/inet.h>
57 key(NULL), algorithm(algo) {
59 setNewKey(k, keyLength);
65 memset(
key, 0,
sizeof(AES_KEY) );
70 delete[] (uint8_t*)
key;
75 static int twoFishInit = 0;
80 delete[] (uint8_t*)
key;
82 if (!(keyLength == 16 || keyLength == 32)) {
86 key =
new uint8_t[
sizeof(AES_KEY)];
87 memset(
key, 0,
sizeof(AES_KEY) );
88 AES_set_encrypt_key(k, keyLength*8, (AES_KEY *)
key);
108 AES_encrypt(input, output, (AES_KEY *)
key);
123 iv[14] = (uint8_t)((ctr & 0xFF00) >> 8);
124 iv[15] = (uint8_t)((ctr & 0x00FF));
130 iv[14] = (uint8_t)((ctr & 0xFF00) >> 8);
131 iv[15] = (uint8_t)((ctr & 0x00FF));
134 memcpy(&output[ctr*SRTP_BLOCK_SIZE], temp, length % SRTP_BLOCK_SIZE );
139 uint8_t* output, uint8_t* iv ) {
148 for (ctr = 0; ctr < l; ctr++ ) {
149 iv[14] = (uint8_t)((ctr & 0xFF00) >> 8);
150 iv[15] = (uint8_t)((ctr & 0x00FF));
154 *output++ = temp[i] ^ *input++;
161 iv[14] = (uint8_t)((ctr & 0xFF00) >> 8);
162 iv[15] = (uint8_t)((ctr & 0x00FF));
165 for (
int i = 0; i < l; i++ ) {
166 *output++ = temp[i] ^ *input++;
180 for (ctr = 0; ctr < l; ctr++ ) {
181 iv[14] = (uint8_t)((ctr & 0xFF00) >> 8);
182 iv[15] = (uint8_t)((ctr & 0x00FF));
193 iv[14] = (uint8_t)((ctr & 0xFF00) >> 8);
194 iv[15] = (uint8_t)((ctr & 0x00FF));
197 for (
int i = 0; i < l; i++ ) {
206 f8_encrypt(data, data_length, const_cast<uint8_t*>(data), iv, f8Cipher);
209 #define MAX_KEYLEN 32
212 uint8_t* salt, int32_t saltLen) {
214 unsigned char *cp_in, *cp_in1, *cp_out;
222 if (saltLen > keyLen)
228 memcpy(saltMask, salt, saltLen);
229 memset(saltMask+saltLen, 0x55, keyLen-saltLen);
238 for (
int i = 0; i < keyLen; i++) {
239 *cp_out++ = *cp_in++ ^ *cp_in1++;
272 memset(f8ctx.
S, 0, SRTP_BLOCK_SIZE);
274 while (in_length >= SRTP_BLOCK_SIZE) {
275 processBlock(&f8ctx, in+offset, SRTP_BLOCK_SIZE, out+offset);
287 const uint8_t *cp_in;
288 uint8_t* cp_in1, *cp_out;
298 *cp_out++ ^= *cp_in++;
303 ui32p = (uint32_t *)f8ctx->
S;
304 ui32p[3] ^= htonl(f8ctx->
J);
317 for (i = 0; i < length; i++) {
318 *cp_out++ = *cp_in++ ^ *cp_in1++;
Implments the SRTP encryption modes as defined in RFC3711.
bool setNewKey(const uint8_t *key, int32_t keyLength)
Set new key.
Structure that contains a prepared Twofish key.
unsigned char Twofish_Byte
A Twofish_Byte must be an unsigned 8-bit integer.
int Twofish_initialise()
Initialise and test the Twofish implementation.
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.
const int SrtpEncryptionTWOF8
int processBlock(F8_CIPHER_CTX *f8ctx, const uint8_t *in, int32_t length, uint8_t *out)
const int SrtpEncryptionTWOCM
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.
Class which implements SRTP AES cryptographic functions.
void Twofish_encrypt(Twofish_key *xkey, Twofish_Byte p[16], Twofish_Byte c[16])
Encrypt a single block of data.
int Twofish_prepare_key(Twofish_Byte key[], int key_len, Twofish_key *xkey)
Convert a cipher key to the internal form used for encryption and decryption.
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.