mirror of
https://github.com/signalwire/freeswitch.git
synced 2026-11-17 06:48:33 +00:00
FS-9785: upgrade libsrtp to 2.1
This commit is contained in:
1 parent
19e8621682
commit
3bcf5b7ff8
129 files changed
+21084
-21652
No files matched your search
+1854
-1765
File diff suppressed because it is too large.
Load diff
@@ -1,561 +0,0 @@
|
||||
/*
|
||||
* aes_cbc.c
|
||||
*
|
||||
* AES Cipher Block Chaining Mode
|
||||
*
|
||||
* David A. McGrew
|
||||
* Cisco Systems, Inc.
|
||||
*/
|
||||
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2001-2006, Cisco Systems, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* Redistributions in binary form must reproduce the above
|
||||
* copyright notice, this list of conditions and the following
|
||||
* disclaimer in the documentation and/or other materials provided
|
||||
* with the distribution.
|
||||
*
|
||||
* Neither the name of the Cisco Systems, Inc. nor the names of its
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include "aes_cbc.h"
|
||||
#include "alloc.h"
|
||||
|
||||
debug_module_t mod_aes_cbc = {
|
||||
0, /* debugging is off by default */
|
||||
"aes cbc" /* printable module name */
|
||||
};
|
||||
|
||||
|
||||
|
||||
err_status_t
|
||||
aes_cbc_alloc(cipher_t **c, int key_len, int tlen) {
|
||||
extern cipher_type_t aes_cbc;
|
||||
uint8_t *pointer;
|
||||
int tmp;
|
||||
|
||||
debug_print(mod_aes_cbc,
|
||||
"allocating cipher with key length %d", key_len);
|
||||
|
||||
if (key_len != 16 && key_len != 24 && key_len != 32)
|
||||
return err_status_bad_param;
|
||||
|
||||
/* allocate memory a cipher of type aes_cbc */
|
||||
tmp = (sizeof(aes_cbc_ctx_t) + sizeof(cipher_t));
|
||||
pointer = (uint8_t*)crypto_alloc(tmp);
|
||||
if (pointer == NULL)
|
||||
return err_status_alloc_fail;
|
||||
|
||||
/* set pointers */
|
||||
*c = (cipher_t *)pointer;
|
||||
(*c)->type = &aes_cbc;
|
||||
(*c)->state = pointer + sizeof(cipher_t);
|
||||
|
||||
/* increment ref_count */
|
||||
aes_cbc.ref_count++;
|
||||
|
||||
/* set key size */
|
||||
(*c)->key_len = key_len;
|
||||
|
||||
return err_status_ok;
|
||||
}
|
||||
|
||||
err_status_t
|
||||
aes_cbc_dealloc(cipher_t *c) {
|
||||
extern cipher_type_t aes_cbc;
|
||||
|
||||
/* zeroize entire state*/
|
||||
octet_string_set_to_zero((uint8_t *)c,
|
||||
sizeof(aes_cbc_ctx_t) + sizeof(cipher_t));
|
||||
|
||||
/* free memory */
|
||||
crypto_free(c);
|
||||
|
||||
/* decrement ref_count */
|
||||
aes_cbc.ref_count--;
|
||||
|
||||
return err_status_ok;
|
||||
}
|
||||
|
||||
err_status_t
|
||||
aes_cbc_context_init(aes_cbc_ctx_t *c, const uint8_t *key, int key_len) {
|
||||
|
||||
debug_print(mod_aes_cbc,
|
||||
"key: %s", octet_string_hex_string(key, key_len));
|
||||
|
||||
/*
|
||||
* Save the key until we have the IV later. We don't
|
||||
* know the direction until the IV is set.
|
||||
*/
|
||||
c->key_len = (key_len <= 32 ? key_len : 32);
|
||||
memcpy(c->key, key, c->key_len);
|
||||
|
||||
return err_status_ok;
|
||||
}
|
||||
|
||||
|
||||
err_status_t
|
||||
aes_cbc_set_iv(aes_cbc_ctx_t *c, void *iv, int direction) {
|
||||
err_status_t status;
|
||||
int i;
|
||||
/* v128_t *input = iv; */
|
||||
uint8_t *input = (uint8_t*) iv;
|
||||
|
||||
/* set state and 'previous' block to iv */
|
||||
for (i=0; i < 16; i++)
|
||||
c->previous.v8[i] = c->state.v8[i] = input[i];
|
||||
|
||||
debug_print(mod_aes_cbc, "setting iv: %s", v128_hex_string(&c->state));
|
||||
|
||||
/* expand key for the appropriate direction */
|
||||
switch (direction) {
|
||||
case (direction_encrypt):
|
||||
status = aes_expand_encryption_key(c->key, c->key_len, &c->expanded_key);
|
||||
memset(c->key, 0, 32);
|
||||
if (status)
|
||||
return status;
|
||||
break;
|
||||
case (direction_decrypt):
|
||||
status = aes_expand_decryption_key(c->key, c->key_len, &c->expanded_key);
|
||||
memset(c->key, 0, 32);
|
||||
if (status)
|
||||
return status;
|
||||
break;
|
||||
default:
|
||||
return err_status_bad_param;
|
||||
}
|
||||
|
||||
return err_status_ok;
|
||||
}
|
||||
|
||||
err_status_t
|
||||
aes_cbc_encrypt(aes_cbc_ctx_t *c,
|
||||
unsigned char *data,
|
||||
unsigned int *bytes_in_data) {
|
||||
int i;
|
||||
unsigned char *input = data; /* pointer to data being read */
|
||||
unsigned char *output = data; /* pointer to data being written */
|
||||
int bytes_to_encr = *bytes_in_data;
|
||||
|
||||
/*
|
||||
* verify that we're 16-octet aligned
|
||||
*/
|
||||
if (*bytes_in_data & 0xf)
|
||||
return err_status_bad_param;
|
||||
|
||||
/*
|
||||
* note that we assume that the initialization vector has already
|
||||
* been set, e.g. by calling aes_cbc_set_iv()
|
||||
*/
|
||||
debug_print(mod_aes_cbc, "iv: %s",
|
||||
v128_hex_string(&c->state));
|
||||
|
||||
/*
|
||||
* loop over plaintext blocks, exoring state into plaintext then
|
||||
* encrypting and writing to output
|
||||
*/
|
||||
while (bytes_to_encr > 0) {
|
||||
|
||||
/* exor plaintext into state */
|
||||
for (i=0; i < 16; i++)
|
||||
c->state.v8[i] ^= *input++;
|
||||
|
||||
debug_print(mod_aes_cbc, "inblock: %s",
|
||||
v128_hex_string(&c->state));
|
||||
|
||||
aes_encrypt(&c->state, &c->expanded_key);
|
||||
|
||||
debug_print(mod_aes_cbc, "outblock: %s",
|
||||
v128_hex_string(&c->state));
|
||||
|
||||
/* copy ciphertext to output */
|
||||
for (i=0; i < 16; i++)
|
||||
*output++ = c->state.v8[i];
|
||||
|
||||
bytes_to_encr -= 16;
|
||||
}
|
||||
|
||||
return err_status_ok;
|
||||
}
|
||||
|
||||
err_status_t
|
||||
aes_cbc_decrypt(aes_cbc_ctx_t *c,
|
||||
unsigned char *data,
|
||||
unsigned int *bytes_in_data) {
|
||||
int i;
|
||||
v128_t state, previous;
|
||||
unsigned char *input = data; /* pointer to data being read */
|
||||
unsigned char *output = data; /* pointer to data being written */
|
||||
int bytes_to_encr = *bytes_in_data;
|
||||
uint8_t tmp;
|
||||
|
||||
/*
|
||||
* verify that we're 16-octet aligned
|
||||
*/
|
||||
if (*bytes_in_data & 0x0f)
|
||||
return err_status_bad_param;
|
||||
|
||||
/* set 'previous' block to iv*/
|
||||
for (i=0; i < 16; i++) {
|
||||
previous.v8[i] = c->previous.v8[i];
|
||||
}
|
||||
|
||||
debug_print(mod_aes_cbc, "iv: %s",
|
||||
v128_hex_string(&previous));
|
||||
|
||||
/*
|
||||
* loop over ciphertext blocks, decrypting then exoring with state
|
||||
* then writing plaintext to output
|
||||
*/
|
||||
while (bytes_to_encr > 0) {
|
||||
|
||||
/* set state to ciphertext input block */
|
||||
for (i=0; i < 16; i++) {
|
||||
state.v8[i] = *input++;
|
||||
}
|
||||
|
||||
debug_print(mod_aes_cbc, "inblock: %s",
|
||||
v128_hex_string(&state));
|
||||
|
||||
/* decrypt state */
|
||||
aes_decrypt(&state, &c->expanded_key);
|
||||
|
||||
debug_print(mod_aes_cbc, "outblock: %s",
|
||||
v128_hex_string(&state));
|
||||
|
||||
/*
|
||||
* exor previous ciphertext block out of plaintext, and write new
|
||||
* plaintext block to output, while copying old ciphertext block
|
||||
* to the 'previous' block
|
||||
*/
|
||||
for (i=0; i < 16; i++) {
|
||||
tmp = *output;
|
||||
*output++ = state.v8[i] ^ previous.v8[i];
|
||||
previous.v8[i] = tmp;
|
||||
}
|
||||
|
||||
bytes_to_encr -= 16;
|
||||
}
|
||||
|
||||
return err_status_ok;
|
||||
}
|
||||
|
||||
|
||||
err_status_t
|
||||
aes_cbc_nist_encrypt(aes_cbc_ctx_t *c,
|
||||
unsigned char *data,
|
||||
unsigned int *bytes_in_data) {
|
||||
int i;
|
||||
unsigned char *pad_start;
|
||||
int num_pad_bytes;
|
||||
err_status_t status;
|
||||
|
||||
/*
|
||||
* determine the number of padding bytes that we need to add -
|
||||
* this value is always between 1 and 16, inclusive.
|
||||
*/
|
||||
num_pad_bytes = 16 - (*bytes_in_data & 0xf);
|
||||
pad_start = data;
|
||||
pad_start += *bytes_in_data;
|
||||
*pad_start++ = 0xa0;
|
||||
for (i=0; i < num_pad_bytes; i++)
|
||||
*pad_start++ = 0x00;
|
||||
|
||||
/*
|
||||
* increment the data size
|
||||
*/
|
||||
*bytes_in_data += num_pad_bytes;
|
||||
|
||||
/*
|
||||
* now cbc encrypt the padded data
|
||||
*/
|
||||
status = aes_cbc_encrypt(c, data, bytes_in_data);
|
||||
if (status)
|
||||
return status;
|
||||
|
||||
return err_status_ok;
|
||||
}
|
||||
|
||||
|
||||
err_status_t
|
||||
aes_cbc_nist_decrypt(aes_cbc_ctx_t *c,
|
||||
unsigned char *data,
|
||||
unsigned int *bytes_in_data) {
|
||||
unsigned char *pad_end;
|
||||
int num_pad_bytes;
|
||||
err_status_t status;
|
||||
|
||||
/*
|
||||
* cbc decrypt the padded data
|
||||
*/
|
||||
status = aes_cbc_decrypt(c, data, bytes_in_data);
|
||||
if (status)
|
||||
return status;
|
||||
|
||||
/*
|
||||
* determine the number of padding bytes in the decrypted plaintext
|
||||
* - this value is always between 1 and 16, inclusive.
|
||||
*/
|
||||
num_pad_bytes = 1;
|
||||
pad_end = data + (*bytes_in_data - 1);
|
||||
while (*pad_end != 0xa0) { /* note: should check padding correctness */
|
||||
pad_end--;
|
||||
num_pad_bytes++;
|
||||
}
|
||||
|
||||
/* decrement data size */
|
||||
*bytes_in_data -= num_pad_bytes;
|
||||
|
||||
return err_status_ok;
|
||||
}
|
||||
|
||||
|
||||
char
|
||||
aes_cbc_description[] = "aes cipher block chaining (cbc) mode";
|
||||
|
||||
/*
|
||||
* Test case 0 is derived from FIPS 197 Appendix C; it uses an
|
||||
* all-zero IV, so that the first block encryption matches the test
|
||||
* case in that appendix. This property provides a check of the base
|
||||
* AES encryption and decryption algorithms; if CBC fails on some
|
||||
* particular platform, then you should print out AES intermediate
|
||||
* data and compare with the detailed info provided in that appendix.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
uint8_t aes_cbc_test_case_0_key[16] = {
|
||||
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
||||
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
|
||||
};
|
||||
|
||||
uint8_t aes_cbc_test_case_0_plaintext[64] = {
|
||||
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
|
||||
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
|
||||
};
|
||||
|
||||
uint8_t aes_cbc_test_case_0_ciphertext[80] = {
|
||||
0x69, 0xc4, 0xe0, 0xd8, 0x6a, 0x7b, 0x04, 0x30,
|
||||
0xd8, 0xcd, 0xb7, 0x80, 0x70, 0xb4, 0xc5, 0x5a,
|
||||
0x03, 0x35, 0xed, 0x27, 0x67, 0xf2, 0x6d, 0xf1,
|
||||
0x64, 0x83, 0x2e, 0x23, 0x44, 0x38, 0x70, 0x8b
|
||||
|
||||
};
|
||||
|
||||
uint8_t aes_cbc_test_case_0_iv[16] = {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||
};
|
||||
|
||||
|
||||
cipher_test_case_t aes_cbc_test_case_0 = {
|
||||
16, /* octets in key */
|
||||
aes_cbc_test_case_0_key, /* key */
|
||||
aes_cbc_test_case_0_iv, /* initialization vector */
|
||||
16, /* octets in plaintext */
|
||||
aes_cbc_test_case_0_plaintext, /* plaintext */
|
||||
32, /* octets in ciphertext */
|
||||
aes_cbc_test_case_0_ciphertext, /* ciphertext */
|
||||
0,
|
||||
NULL,
|
||||
0,
|
||||
NULL /* pointer to next testcase */
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
* this test case is taken directly from Appendix F.2 of NIST Special
|
||||
* Publication SP 800-38A
|
||||
*/
|
||||
|
||||
uint8_t aes_cbc_test_case_1_key[16] = {
|
||||
0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
|
||||
0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c,
|
||||
};
|
||||
|
||||
uint8_t aes_cbc_test_case_1_plaintext[64] = {
|
||||
0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
|
||||
0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
|
||||
0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
|
||||
0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
|
||||
0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
|
||||
0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
|
||||
0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
|
||||
0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10
|
||||
};
|
||||
|
||||
uint8_t aes_cbc_test_case_1_ciphertext[80] = {
|
||||
0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46,
|
||||
0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
|
||||
0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee,
|
||||
0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2,
|
||||
0x73, 0xbe, 0xd6, 0xb8, 0xe3, 0xc1, 0x74, 0x3b,
|
||||
0x71, 0x16, 0xe6, 0x9e, 0x22, 0x22, 0x95, 0x16,
|
||||
0x3f, 0xf1, 0xca, 0xa1, 0x68, 0x1f, 0xac, 0x09,
|
||||
0x12, 0x0e, 0xca, 0x30, 0x75, 0x86, 0xe1, 0xa7,
|
||||
0x39, 0x34, 0x07, 0x03, 0x36, 0xd0, 0x77, 0x99,
|
||||
0xe0, 0xc4, 0x2f, 0xdd, 0xa8, 0xdf, 0x4c, 0xa3
|
||||
};
|
||||
|
||||
uint8_t aes_cbc_test_case_1_iv[16] = {
|
||||
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
||||
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
|
||||
};
|
||||
|
||||
cipher_test_case_t aes_cbc_test_case_1 = {
|
||||
16, /* octets in key */
|
||||
aes_cbc_test_case_1_key, /* key */
|
||||
aes_cbc_test_case_1_iv, /* initialization vector */
|
||||
64, /* octets in plaintext */
|
||||
aes_cbc_test_case_1_plaintext, /* plaintext */
|
||||
80, /* octets in ciphertext */
|
||||
aes_cbc_test_case_1_ciphertext, /* ciphertext */
|
||||
0,
|
||||
NULL,
|
||||
0,
|
||||
&aes_cbc_test_case_0 /* pointer to next testcase */
|
||||
};
|
||||
|
||||
/*
|
||||
* Test case 2 is like test case 0, but for 256-bit keys. (FIPS 197
|
||||
* appendix C.3).
|
||||
*/
|
||||
|
||||
|
||||
uint8_t aes_cbc_test_case_2_key[32] = {
|
||||
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
||||
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
|
||||
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
|
||||
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
|
||||
};
|
||||
|
||||
uint8_t aes_cbc_test_case_2_plaintext[64] = {
|
||||
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
|
||||
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
|
||||
};
|
||||
|
||||
uint8_t aes_cbc_test_case_2_ciphertext[80] = {
|
||||
0x8e, 0xa2, 0xb7, 0xca, 0x51, 0x67, 0x45, 0xbf,
|
||||
0xea, 0xfc, 0x49, 0x90, 0x4b, 0x49, 0x60, 0x89,
|
||||
0x72, 0x72, 0x6e, 0xe7, 0x71, 0x39, 0xbf, 0x11,
|
||||
0xe5, 0x40, 0xe2, 0x7c, 0x54, 0x65, 0x1d, 0xee
|
||||
};
|
||||
|
||||
uint8_t aes_cbc_test_case_2_iv[16] = {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||
};
|
||||
|
||||
cipher_test_case_t aes_cbc_test_case_2 = {
|
||||
32, /* octets in key */
|
||||
aes_cbc_test_case_2_key, /* key */
|
||||
aes_cbc_test_case_2_iv, /* initialization vector */
|
||||
16, /* octets in plaintext */
|
||||
aes_cbc_test_case_2_plaintext, /* plaintext */
|
||||
32, /* octets in ciphertext */
|
||||
aes_cbc_test_case_2_ciphertext, /* ciphertext */
|
||||
0,
|
||||
NULL,
|
||||
0,
|
||||
&aes_cbc_test_case_1 /* pointer to next testcase */
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
* this test case is taken directly from Appendix F.2 of NIST Special
|
||||
* Publication SP 800-38A
|
||||
*/
|
||||
|
||||
uint8_t aes_cbc_test_case_3_key[32] = {
|
||||
0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
|
||||
0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
|
||||
0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
|
||||
0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
|
||||
};
|
||||
|
||||
uint8_t aes_cbc_test_case_3_plaintext[64] = {
|
||||
0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
|
||||
0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
|
||||
0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
|
||||
0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
|
||||
0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
|
||||
0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
|
||||
0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
|
||||
0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10
|
||||
};
|
||||
|
||||
uint8_t aes_cbc_test_case_3_ciphertext[80] = {
|
||||
0xf5, 0x8c, 0x4c, 0x04, 0xd6, 0xe5, 0xf1, 0xba,
|
||||
0x77, 0x9e, 0xab, 0xfb, 0x5f, 0x7b, 0xfb, 0xd6,
|
||||
0x9c, 0xfc, 0x4e, 0x96, 0x7e, 0xdb, 0x80, 0x8d,
|
||||
0x67, 0x9f, 0x77, 0x7b, 0xc6, 0x70, 0x2c, 0x7d,
|
||||
0x39, 0xf2, 0x33, 0x69, 0xa9, 0xd9, 0xba, 0xcf,
|
||||
0xa5, 0x30, 0xe2, 0x63, 0x04, 0x23, 0x14, 0x61,
|
||||
0xb2, 0xeb, 0x05, 0xe2, 0xc3, 0x9b, 0xe9, 0xfc,
|
||||
0xda, 0x6c, 0x19, 0x07, 0x8c, 0x6a, 0x9d, 0x1b,
|
||||
0xfb, 0x98, 0x20, 0x2c, 0x45, 0xb2, 0xe4, 0xa0,
|
||||
0x63, 0xc4, 0x68, 0xba, 0x84, 0x39, 0x16, 0x5a
|
||||
};
|
||||
|
||||
uint8_t aes_cbc_test_case_3_iv[16] = {
|
||||
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
||||
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
|
||||
};
|
||||
|
||||
cipher_test_case_t aes_cbc_test_case_3 = {
|
||||
32, /* octets in key */
|
||||
aes_cbc_test_case_3_key, /* key */
|
||||
aes_cbc_test_case_3_iv, /* initialization vector */
|
||||
64, /* octets in plaintext */
|
||||
aes_cbc_test_case_3_plaintext, /* plaintext */
|
||||
80, /* octets in ciphertext */
|
||||
aes_cbc_test_case_3_ciphertext, /* ciphertext */
|
||||
0,
|
||||
NULL,
|
||||
0,
|
||||
&aes_cbc_test_case_2 /* pointer to next testcase */
|
||||
};
|
||||
|
||||
cipher_type_t aes_cbc = {
|
||||
(cipher_alloc_func_t) aes_cbc_alloc,
|
||||
(cipher_dealloc_func_t) aes_cbc_dealloc,
|
||||
(cipher_init_func_t) aes_cbc_context_init,
|
||||
(cipher_set_aad_func_t) 0,
|
||||
(cipher_encrypt_func_t) aes_cbc_nist_encrypt,
|
||||
(cipher_decrypt_func_t) aes_cbc_nist_decrypt,
|
||||
(cipher_set_iv_func_t) aes_cbc_set_iv,
|
||||
(cipher_get_tag_func_t) 0,
|
||||
(char *) aes_cbc_description,
|
||||
(int) 0, /* instance count */
|
||||
(cipher_test_case_t *) &aes_cbc_test_case_3,
|
||||
(debug_module_t *) &mod_aes_cbc,
|
||||
(cipher_type_id_t) AES_CBC
|
||||
};
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2013, Cisco Systems, Inc.
|
||||
* Copyright (c) 2013-2017, Cisco Systems, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -44,14 +44,19 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#include <openssl/evp.h>
|
||||
#include "aes_icm_ossl.h"
|
||||
#include "aes_gcm_ossl.h"
|
||||
#include "alloc.h"
|
||||
#include "err.h" /* for srtp_debug */
|
||||
#include "crypto_types.h"
|
||||
|
||||
|
||||
debug_module_t mod_aes_gcm = {
|
||||
srtp_debug_module_t srtp_mod_aes_gcm = {
|
||||
0, /* debugging is off by default */
|
||||
"aes gcm" /* printable module name */
|
||||
};
|
||||
@@ -60,12 +65,12 @@ debug_module_t mod_aes_gcm = {
|
||||
* The following are the global singleton instances for the
|
||||
* 128-bit and 256-bit GCM ciphers.
|
||||
*/
|
||||
extern cipher_type_t aes_gcm_128_openssl;
|
||||
extern cipher_type_t aes_gcm_256_openssl;
|
||||
extern const srtp_cipher_type_t srtp_aes_gcm_128_openssl;
|
||||
extern const srtp_cipher_type_t srtp_aes_gcm_256_openssl;
|
||||
|
||||
/*
|
||||
* For now we only support 8 and 16 octet tags. The spec allows for
|
||||
* optional 12 byte tag, which may be supported in the future.
|
||||
* optional 12 byte tag, which may be supported in the future.
|
||||
*/
|
||||
#define GCM_AUTH_TAG_LEN 16
|
||||
#define GCM_AUTH_TAG_LEN_8 8
|
||||
@@ -74,101 +79,98 @@ extern cipher_type_t aes_gcm_256_openssl;
|
||||
/*
|
||||
* This function allocates a new instance of this crypto engine.
|
||||
* The key_len parameter should be one of 28 or 44 for
|
||||
* AES-128-GCM or AES-256-GCM respectively. Note that the
|
||||
* AES-128-GCM or AES-256-GCM respectively. Note that the
|
||||
* key length includes the 14 byte salt value that is used when
|
||||
* initializing the KDF.
|
||||
*/
|
||||
err_status_t aes_gcm_openssl_alloc (cipher_t **c, int key_len, int tlen)
|
||||
static srtp_err_status_t srtp_aes_gcm_openssl_alloc (srtp_cipher_t **c, int key_len, int tlen)
|
||||
{
|
||||
aes_gcm_ctx_t *gcm;
|
||||
int tmp;
|
||||
uint8_t *allptr;
|
||||
srtp_aes_gcm_ctx_t *gcm;
|
||||
|
||||
debug_print(mod_aes_gcm, "allocating cipher with key length %d", key_len);
|
||||
debug_print(mod_aes_gcm, "allocating cipher with tag length %d", tlen);
|
||||
debug_print(srtp_mod_aes_gcm, "allocating cipher with key length %d", key_len);
|
||||
debug_print(srtp_mod_aes_gcm, "allocating cipher with tag length %d", tlen);
|
||||
|
||||
/*
|
||||
* Verify the key_len is valid for one of: AES-128/256
|
||||
*/
|
||||
if (key_len != AES_128_GCM_KEYSIZE_WSALT &&
|
||||
key_len != AES_256_GCM_KEYSIZE_WSALT) {
|
||||
return (err_status_bad_param);
|
||||
if (key_len != SRTP_AES_GCM_128_KEY_LEN_WSALT &&
|
||||
key_len != SRTP_AES_GCM_256_KEY_LEN_WSALT) {
|
||||
return (srtp_err_status_bad_param);
|
||||
}
|
||||
|
||||
if (tlen != GCM_AUTH_TAG_LEN &&
|
||||
tlen != GCM_AUTH_TAG_LEN_8) {
|
||||
return (err_status_bad_param);
|
||||
tlen != GCM_AUTH_TAG_LEN_8) {
|
||||
return (srtp_err_status_bad_param);
|
||||
}
|
||||
|
||||
/* allocate memory a cipher of type aes_gcm */
|
||||
tmp = sizeof(cipher_t) + sizeof(aes_gcm_ctx_t);
|
||||
allptr = crypto_alloc(tmp);
|
||||
if (allptr == NULL) {
|
||||
return (err_status_alloc_fail);
|
||||
*c = (srtp_cipher_t *)srtp_crypto_alloc(sizeof(srtp_cipher_t));
|
||||
if (*c == NULL) {
|
||||
return (srtp_err_status_alloc_fail);
|
||||
}
|
||||
memset(*c, 0x0, sizeof(srtp_cipher_t));
|
||||
|
||||
gcm = (srtp_aes_gcm_ctx_t *)srtp_crypto_alloc(sizeof(srtp_aes_gcm_ctx_t));
|
||||
if (gcm == NULL) {
|
||||
srtp_crypto_free(*c);
|
||||
*c = NULL;
|
||||
return (srtp_err_status_alloc_fail);
|
||||
}
|
||||
memset(gcm, 0x0, sizeof(srtp_aes_gcm_ctx_t));
|
||||
|
||||
gcm->ctx = EVP_CIPHER_CTX_new();
|
||||
if (gcm->ctx == NULL) {
|
||||
srtp_crypto_free(gcm);
|
||||
srtp_crypto_free(*c);
|
||||
*c = NULL;
|
||||
return srtp_err_status_alloc_fail;
|
||||
}
|
||||
|
||||
/* set pointers */
|
||||
*c = (cipher_t*)allptr;
|
||||
(*c)->state = allptr + sizeof(cipher_t);
|
||||
gcm = (aes_gcm_ctx_t *)(*c)->state;
|
||||
(*c)->state = gcm;
|
||||
|
||||
/* increment ref_count */
|
||||
/* setup cipher attributes */
|
||||
switch (key_len) {
|
||||
case AES_128_GCM_KEYSIZE_WSALT:
|
||||
(*c)->type = &aes_gcm_128_openssl;
|
||||
(*c)->algorithm = AES_128_GCM;
|
||||
aes_gcm_128_openssl.ref_count++;
|
||||
((aes_gcm_ctx_t*)(*c)->state)->key_size = AES_128_KEYSIZE;
|
||||
((aes_gcm_ctx_t*)(*c)->state)->tag_len = tlen;
|
||||
case SRTP_AES_GCM_128_KEY_LEN_WSALT:
|
||||
(*c)->type = &srtp_aes_gcm_128_openssl;
|
||||
(*c)->algorithm = SRTP_AES_GCM_128;
|
||||
gcm->key_size = SRTP_AES_128_KEY_LEN;
|
||||
gcm->tag_len = tlen;
|
||||
break;
|
||||
case AES_256_GCM_KEYSIZE_WSALT:
|
||||
(*c)->type = &aes_gcm_256_openssl;
|
||||
(*c)->algorithm = AES_256_GCM;
|
||||
aes_gcm_256_openssl.ref_count++;
|
||||
((aes_gcm_ctx_t*)(*c)->state)->key_size = AES_256_KEYSIZE;
|
||||
((aes_gcm_ctx_t*)(*c)->state)->tag_len = tlen;
|
||||
case SRTP_AES_GCM_256_KEY_LEN_WSALT:
|
||||
(*c)->type = &srtp_aes_gcm_256_openssl;
|
||||
(*c)->algorithm = SRTP_AES_GCM_256;
|
||||
gcm->key_size = SRTP_AES_256_KEY_LEN;
|
||||
gcm->tag_len = tlen;
|
||||
break;
|
||||
}
|
||||
|
||||
/* set key size */
|
||||
(*c)->key_len = key_len;
|
||||
EVP_CIPHER_CTX_init(&gcm->ctx);
|
||||
|
||||
return (err_status_ok);
|
||||
return (srtp_err_status_ok);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* This function deallocates a GCM session
|
||||
* This function deallocates a GCM session
|
||||
*/
|
||||
err_status_t aes_gcm_openssl_dealloc (cipher_t *c)
|
||||
static srtp_err_status_t srtp_aes_gcm_openssl_dealloc (srtp_cipher_t *c)
|
||||
{
|
||||
aes_gcm_ctx_t *ctx;
|
||||
srtp_aes_gcm_ctx_t *ctx;
|
||||
|
||||
ctx = (aes_gcm_ctx_t*)c->state;
|
||||
ctx = (srtp_aes_gcm_ctx_t*)c->state;
|
||||
if (ctx) {
|
||||
EVP_CIPHER_CTX_cleanup(&ctx->ctx);
|
||||
/* decrement ref_count for the appropriate engine */
|
||||
switch (ctx->key_size) {
|
||||
case AES_256_KEYSIZE:
|
||||
aes_gcm_256_openssl.ref_count--;
|
||||
break;
|
||||
case AES_128_KEYSIZE:
|
||||
aes_gcm_128_openssl.ref_count--;
|
||||
break;
|
||||
default:
|
||||
return (err_status_dealloc_fail);
|
||||
break;
|
||||
}
|
||||
EVP_CIPHER_CTX_free(ctx->ctx);
|
||||
/* zeroize the key material */
|
||||
octet_string_set_to_zero(ctx, sizeof(srtp_aes_gcm_ctx_t));
|
||||
srtp_crypto_free(ctx);
|
||||
}
|
||||
|
||||
/* zeroize entire state*/
|
||||
octet_string_set_to_zero((uint8_t*)c, sizeof(cipher_t) + sizeof(aes_gcm_ctx_t));
|
||||
|
||||
/* free memory */
|
||||
crypto_free(c);
|
||||
srtp_crypto_free(c);
|
||||
|
||||
return (err_status_ok);
|
||||
return (srtp_err_status_ok);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -177,25 +179,32 @@ err_status_t aes_gcm_openssl_dealloc (cipher_t *c)
|
||||
*
|
||||
* the key is the secret key
|
||||
*/
|
||||
err_status_t aes_gcm_openssl_context_init (aes_gcm_ctx_t *c, const uint8_t *key)
|
||||
static srtp_err_status_t srtp_aes_gcm_openssl_context_init (void* cv, const uint8_t *key)
|
||||
{
|
||||
c->dir = direction_any;
|
||||
srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
|
||||
const EVP_CIPHER *evp;
|
||||
|
||||
/* copy key to be used later when CiscoSSL crypto context is created */
|
||||
v128_copy_octet_string((v128_t*)&c->key, key);
|
||||
c->dir = srtp_direction_any;
|
||||
|
||||
if (c->key_size == AES_256_KEYSIZE) {
|
||||
debug_print(mod_aes_gcm, "Copying last 16 bytes of key: %s",
|
||||
v128_hex_string((v128_t*)(key + AES_128_KEYSIZE)));
|
||||
v128_copy_octet_string(((v128_t*)(&c->key.v8)) + 1,
|
||||
key + AES_128_KEYSIZE);
|
||||
debug_print(srtp_mod_aes_gcm, "key: %s", srtp_octet_string_hex_string(key, c->key_size));
|
||||
|
||||
switch (c->key_size) {
|
||||
case SRTP_AES_256_KEY_LEN:
|
||||
evp = EVP_aes_256_gcm();
|
||||
break;
|
||||
case SRTP_AES_128_KEY_LEN:
|
||||
evp = EVP_aes_128_gcm();
|
||||
break;
|
||||
default:
|
||||
return (srtp_err_status_bad_param);
|
||||
break;
|
||||
}
|
||||
|
||||
debug_print(mod_aes_gcm, "key: %s", v128_hex_string((v128_t*)&c->key));
|
||||
if (!EVP_CipherInit_ex(c->ctx, evp, NULL, key, NULL, 0)) {
|
||||
return (srtp_err_status_init_fail);
|
||||
}
|
||||
|
||||
EVP_CIPHER_CTX_cleanup(&c->ctx);
|
||||
|
||||
return (err_status_ok);
|
||||
return (srtp_err_status_ok);
|
||||
}
|
||||
|
||||
|
||||
@@ -203,48 +212,34 @@ err_status_t aes_gcm_openssl_context_init (aes_gcm_ctx_t *c, const uint8_t *key)
|
||||
* aes_gcm_openssl_set_iv(c, iv) sets the counter value to the exor of iv with
|
||||
* the offset
|
||||
*/
|
||||
err_status_t aes_gcm_openssl_set_iv (aes_gcm_ctx_t *c, void *iv,
|
||||
int direction)
|
||||
static srtp_err_status_t srtp_aes_gcm_openssl_set_iv (void *cv, uint8_t *iv, srtp_cipher_direction_t direction)
|
||||
{
|
||||
const EVP_CIPHER *evp;
|
||||
v128_t *nonce = iv;
|
||||
srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
|
||||
|
||||
if (direction != direction_encrypt && direction != direction_decrypt) {
|
||||
return (err_status_bad_param);
|
||||
if (direction != srtp_direction_encrypt && direction != srtp_direction_decrypt) {
|
||||
return (srtp_err_status_bad_param);
|
||||
}
|
||||
c->dir = direction;
|
||||
|
||||
debug_print(mod_aes_gcm, "setting iv: %s", v128_hex_string(nonce));
|
||||
debug_print(srtp_mod_aes_gcm, "setting iv: %s", v128_hex_string((v128_t*)iv));
|
||||
|
||||
switch (c->key_size) {
|
||||
case AES_256_KEYSIZE:
|
||||
evp = EVP_aes_256_gcm();
|
||||
break;
|
||||
case AES_128_KEYSIZE:
|
||||
evp = EVP_aes_128_gcm();
|
||||
break;
|
||||
default:
|
||||
return (err_status_bad_param);
|
||||
break;
|
||||
}
|
||||
|
||||
if (!EVP_CipherInit_ex(&c->ctx, evp, NULL, (const unsigned char*)&c->key.v8,
|
||||
NULL, (c->dir == direction_encrypt ? 1 : 0))) {
|
||||
return (err_status_init_fail);
|
||||
if (!EVP_CipherInit_ex(c->ctx, NULL, NULL, NULL,
|
||||
NULL, (c->dir == srtp_direction_encrypt ? 1 : 0))) {
|
||||
return (srtp_err_status_init_fail);
|
||||
}
|
||||
|
||||
/* set IV len and the IV value, the followiong 3 calls are required */
|
||||
if (!EVP_CIPHER_CTX_ctrl(&c->ctx, EVP_CTRL_GCM_SET_IVLEN, 12, 0)) {
|
||||
return (err_status_init_fail);
|
||||
if (!EVP_CIPHER_CTX_ctrl(c->ctx, EVP_CTRL_GCM_SET_IVLEN, 12, 0)) {
|
||||
return (srtp_err_status_init_fail);
|
||||
}
|
||||
if (!EVP_CIPHER_CTX_ctrl(&c->ctx, EVP_CTRL_GCM_SET_IV_FIXED, -1, iv)) {
|
||||
return (err_status_init_fail);
|
||||
if (!EVP_CIPHER_CTX_ctrl(c->ctx, EVP_CTRL_GCM_SET_IV_FIXED, -1, (void*)iv)) {
|
||||
return (srtp_err_status_init_fail);
|
||||
}
|
||||
if (!EVP_CIPHER_CTX_ctrl(&c->ctx, EVP_CTRL_GCM_IV_GEN, 0, iv)) {
|
||||
return (err_status_init_fail);
|
||||
if (!EVP_CIPHER_CTX_ctrl(c->ctx, EVP_CTRL_GCM_IV_GEN, 0, (void*)iv)) {
|
||||
return (srtp_err_status_init_fail);
|
||||
}
|
||||
|
||||
return (err_status_ok);
|
||||
return (srtp_err_status_ok);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -255,22 +250,31 @@ err_status_t aes_gcm_openssl_set_iv (aes_gcm_ctx_t *c, void *iv,
|
||||
* aad Additional data to process for AEAD cipher suites
|
||||
* aad_len length of aad buffer
|
||||
*/
|
||||
err_status_t aes_gcm_openssl_set_aad (aes_gcm_ctx_t *c, unsigned char *aad,
|
||||
unsigned int aad_len)
|
||||
static srtp_err_status_t srtp_aes_gcm_openssl_set_aad (void *cv, const uint8_t *aad, uint32_t aad_len)
|
||||
{
|
||||
srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
|
||||
int rv;
|
||||
|
||||
/*
|
||||
* Set dummy tag, OpenSSL requires the Tag to be set before
|
||||
* processing AAD
|
||||
*/
|
||||
EVP_CIPHER_CTX_ctrl(&c->ctx, EVP_CTRL_GCM_SET_TAG, c->tag_len, aad);
|
||||
|
||||
rv = EVP_Cipher(&c->ctx, NULL, aad, aad_len);
|
||||
/*
|
||||
* OpenSSL never write to address pointed by the last parameter of
|
||||
* EVP_CIPHER_CTX_ctrl while EVP_CTRL_GCM_SET_TAG (in reality,
|
||||
* OpenSSL copy its content to the context), so we can make
|
||||
* aad read-only in this function and all its wrappers.
|
||||
*/
|
||||
unsigned char dummy_tag[GCM_AUTH_TAG_LEN];
|
||||
memset(dummy_tag, 0x0, GCM_AUTH_TAG_LEN);
|
||||
EVP_CIPHER_CTX_ctrl(c->ctx, EVP_CTRL_GCM_SET_TAG, c->tag_len, &dummy_tag);
|
||||
|
||||
rv = EVP_Cipher(c->ctx, NULL, aad, aad_len);
|
||||
if (rv != aad_len) {
|
||||
return (err_status_algo_fail);
|
||||
return (srtp_err_status_algo_fail);
|
||||
} else {
|
||||
return (err_status_ok);
|
||||
return (srtp_err_status_ok);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -282,24 +286,24 @@ err_status_t aes_gcm_openssl_set_aad (aes_gcm_ctx_t *c, unsigned char *aad,
|
||||
* buf data to encrypt
|
||||
* enc_len length of encrypt buffer
|
||||
*/
|
||||
err_status_t aes_gcm_openssl_encrypt (aes_gcm_ctx_t *c, unsigned char *buf,
|
||||
unsigned int *enc_len)
|
||||
static srtp_err_status_t srtp_aes_gcm_openssl_encrypt (void *cv, unsigned char *buf, unsigned int *enc_len)
|
||||
{
|
||||
if (c->dir != direction_encrypt && c->dir != direction_decrypt) {
|
||||
return (err_status_bad_param);
|
||||
srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
|
||||
if (c->dir != srtp_direction_encrypt && c->dir != srtp_direction_decrypt) {
|
||||
return (srtp_err_status_bad_param);
|
||||
}
|
||||
|
||||
/*
|
||||
* Encrypt the data
|
||||
*/
|
||||
EVP_Cipher(&c->ctx, buf, buf, *enc_len);
|
||||
EVP_Cipher(c->ctx, buf, buf, *enc_len);
|
||||
|
||||
return (err_status_ok);
|
||||
return (srtp_err_status_ok);
|
||||
}
|
||||
|
||||
/*
|
||||
* This function calculates and returns the GCM tag for a given context.
|
||||
* This should be called after encrypting the data. The *len value
|
||||
* This should be called after encrypting the data. The *len value
|
||||
* is increased by the tag size. The caller must ensure that *buf has
|
||||
* enough room to accept the appended tag.
|
||||
*
|
||||
@@ -308,25 +312,25 @@ err_status_t aes_gcm_openssl_encrypt (aes_gcm_ctx_t *c, unsigned char *buf,
|
||||
* buf data to encrypt
|
||||
* len length of encrypt buffer
|
||||
*/
|
||||
err_status_t aes_gcm_openssl_get_tag (aes_gcm_ctx_t *c, unsigned char *buf,
|
||||
int *len)
|
||||
static srtp_err_status_t srtp_aes_gcm_openssl_get_tag (void *cv, uint8_t *buf, uint32_t *len)
|
||||
{
|
||||
srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
|
||||
/*
|
||||
* Calculate the tag
|
||||
*/
|
||||
EVP_Cipher(&c->ctx, NULL, NULL, 0);
|
||||
EVP_Cipher(c->ctx, NULL, NULL, 0);
|
||||
|
||||
/*
|
||||
* Retreive the tag
|
||||
*/
|
||||
EVP_CIPHER_CTX_ctrl(&c->ctx, EVP_CTRL_GCM_GET_TAG, c->tag_len, buf);
|
||||
EVP_CIPHER_CTX_ctrl(c->ctx, EVP_CTRL_GCM_GET_TAG, c->tag_len, buf);
|
||||
|
||||
/*
|
||||
* Increase encryption length by desired tag size
|
||||
*/
|
||||
*len = c->tag_len;
|
||||
|
||||
return (err_status_ok);
|
||||
return (srtp_err_status_ok);
|
||||
}
|
||||
|
||||
|
||||
@@ -338,25 +342,25 @@ err_status_t aes_gcm_openssl_get_tag (aes_gcm_ctx_t *c, unsigned char *buf,
|
||||
* buf data to encrypt
|
||||
* enc_len length of encrypt buffer
|
||||
*/
|
||||
err_status_t aes_gcm_openssl_decrypt (aes_gcm_ctx_t *c, unsigned char *buf,
|
||||
unsigned int *enc_len)
|
||||
static srtp_err_status_t srtp_aes_gcm_openssl_decrypt (void *cv, unsigned char *buf, unsigned int *enc_len)
|
||||
{
|
||||
if (c->dir != direction_encrypt && c->dir != direction_decrypt) {
|
||||
return (err_status_bad_param);
|
||||
srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
|
||||
if (c->dir != srtp_direction_encrypt && c->dir != srtp_direction_decrypt) {
|
||||
return (srtp_err_status_bad_param);
|
||||
}
|
||||
|
||||
/*
|
||||
* Set the tag before decrypting
|
||||
*/
|
||||
EVP_CIPHER_CTX_ctrl(&c->ctx, EVP_CTRL_GCM_SET_TAG, c->tag_len,
|
||||
buf + (*enc_len - c->tag_len));
|
||||
EVP_Cipher(&c->ctx, buf, buf, *enc_len - c->tag_len);
|
||||
EVP_CIPHER_CTX_ctrl(c->ctx, EVP_CTRL_GCM_SET_TAG, c->tag_len,
|
||||
buf + (*enc_len - c->tag_len));
|
||||
EVP_Cipher(c->ctx, buf, buf, *enc_len - c->tag_len);
|
||||
|
||||
/*
|
||||
* Check the tag
|
||||
*/
|
||||
if (EVP_Cipher(&c->ctx, NULL, NULL, 0)) {
|
||||
return (err_status_auth_fail);
|
||||
if (EVP_Cipher(c->ctx, NULL, NULL, 0)) {
|
||||
return (srtp_err_status_auth_fail);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -365,7 +369,7 @@ err_status_t aes_gcm_openssl_decrypt (aes_gcm_ctx_t *c, unsigned char *buf,
|
||||
*/
|
||||
*enc_len -= c->tag_len;
|
||||
|
||||
return (err_status_ok);
|
||||
return (srtp_err_status_ok);
|
||||
}
|
||||
|
||||
|
||||
@@ -373,28 +377,28 @@ err_status_t aes_gcm_openssl_decrypt (aes_gcm_ctx_t *c, unsigned char *buf,
|
||||
/*
|
||||
* Name of this crypto engine
|
||||
*/
|
||||
char aes_gcm_128_openssl_description[] = "AES-128 GCM using openssl";
|
||||
char aes_gcm_256_openssl_description[] = "AES-256 GCM using openssl";
|
||||
static const char srtp_aes_gcm_128_openssl_description[] = "AES-128 GCM using openssl";
|
||||
static const char srtp_aes_gcm_256_openssl_description[] = "AES-256 GCM using openssl";
|
||||
|
||||
|
||||
/*
|
||||
* KAT values for AES self-test. These
|
||||
* values we're derived from independent test code
|
||||
* values we're derived from independent test code
|
||||
* using OpenSSL.
|
||||
*/
|
||||
uint8_t aes_gcm_test_case_0_key[AES_128_GCM_KEYSIZE_WSALT] = {
|
||||
static const uint8_t srtp_aes_gcm_test_case_0_key[SRTP_AES_GCM_128_KEY_LEN_WSALT] = {
|
||||
0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
|
||||
0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
|
||||
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
|
||||
0x09, 0x0a, 0x0b, 0x0c,
|
||||
0x09, 0x0a, 0x0b, 0x0c,
|
||||
};
|
||||
|
||||
uint8_t aes_gcm_test_case_0_iv[12] = {
|
||||
0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
|
||||
static uint8_t srtp_aes_gcm_test_case_0_iv[12] = {
|
||||
0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
|
||||
0xde, 0xca, 0xf8, 0x88
|
||||
};
|
||||
|
||||
uint8_t aes_gcm_test_case_0_plaintext[60] = {
|
||||
static const uint8_t srtp_aes_gcm_test_case_0_plaintext[60] = {
|
||||
0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
|
||||
0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
|
||||
0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
|
||||
@@ -405,13 +409,13 @@ uint8_t aes_gcm_test_case_0_plaintext[60] = {
|
||||
0xba, 0x63, 0x7b, 0x39
|
||||
};
|
||||
|
||||
uint8_t aes_gcm_test_case_0_aad[20] = {
|
||||
static const uint8_t srtp_aes_gcm_test_case_0_aad[20] = {
|
||||
0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
|
||||
0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
|
||||
0xab, 0xad, 0xda, 0xd2
|
||||
};
|
||||
|
||||
uint8_t aes_gcm_test_case_0_ciphertext[76] = {
|
||||
static const uint8_t srtp_aes_gcm_test_case_0_ciphertext[76] = {
|
||||
0x42, 0x83, 0x1e, 0xc2, 0x21, 0x77, 0x74, 0x24,
|
||||
0x4b, 0x72, 0x21, 0xb7, 0x84, 0xd0, 0xd4, 0x9c,
|
||||
0xe3, 0xaa, 0x21, 0x2f, 0x2c, 0x02, 0xa4, 0xe0,
|
||||
@@ -425,50 +429,50 @@ uint8_t aes_gcm_test_case_0_ciphertext[76] = {
|
||||
0x94, 0xfa, 0xe9, 0x5a, 0xe7, 0x12, 0x1a, 0x47,
|
||||
};
|
||||
|
||||
cipher_test_case_t aes_gcm_test_case_0a = {
|
||||
AES_128_GCM_KEYSIZE_WSALT, /* octets in key */
|
||||
aes_gcm_test_case_0_key, /* key */
|
||||
aes_gcm_test_case_0_iv, /* packet index */
|
||||
60, /* octets in plaintext */
|
||||
aes_gcm_test_case_0_plaintext, /* plaintext */
|
||||
68, /* octets in ciphertext */
|
||||
aes_gcm_test_case_0_ciphertext, /* ciphertext + tag */
|
||||
20, /* octets in AAD */
|
||||
aes_gcm_test_case_0_aad, /* AAD */
|
||||
static const srtp_cipher_test_case_t srtp_aes_gcm_test_case_0a = {
|
||||
SRTP_AES_GCM_128_KEY_LEN_WSALT, /* octets in key */
|
||||
srtp_aes_gcm_test_case_0_key, /* key */
|
||||
srtp_aes_gcm_test_case_0_iv, /* packet index */
|
||||
60, /* octets in plaintext */
|
||||
srtp_aes_gcm_test_case_0_plaintext, /* plaintext */
|
||||
68, /* octets in ciphertext */
|
||||
srtp_aes_gcm_test_case_0_ciphertext, /* ciphertext + tag */
|
||||
20, /* octets in AAD */
|
||||
srtp_aes_gcm_test_case_0_aad, /* AAD */
|
||||
GCM_AUTH_TAG_LEN_8,
|
||||
NULL /* pointer to next testcase */
|
||||
NULL /* pointer to next testcase */
|
||||
};
|
||||
|
||||
cipher_test_case_t aes_gcm_test_case_0 = {
|
||||
AES_128_GCM_KEYSIZE_WSALT, /* octets in key */
|
||||
aes_gcm_test_case_0_key, /* key */
|
||||
aes_gcm_test_case_0_iv, /* packet index */
|
||||
60, /* octets in plaintext */
|
||||
aes_gcm_test_case_0_plaintext, /* plaintext */
|
||||
76, /* octets in ciphertext */
|
||||
aes_gcm_test_case_0_ciphertext, /* ciphertext + tag */
|
||||
20, /* octets in AAD */
|
||||
aes_gcm_test_case_0_aad, /* AAD */
|
||||
static const srtp_cipher_test_case_t srtp_aes_gcm_test_case_0 = {
|
||||
SRTP_AES_GCM_128_KEY_LEN_WSALT, /* octets in key */
|
||||
srtp_aes_gcm_test_case_0_key, /* key */
|
||||
srtp_aes_gcm_test_case_0_iv, /* packet index */
|
||||
60, /* octets in plaintext */
|
||||
srtp_aes_gcm_test_case_0_plaintext, /* plaintext */
|
||||
76, /* octets in ciphertext */
|
||||
srtp_aes_gcm_test_case_0_ciphertext, /* ciphertext + tag */
|
||||
20, /* octets in AAD */
|
||||
srtp_aes_gcm_test_case_0_aad, /* AAD */
|
||||
GCM_AUTH_TAG_LEN,
|
||||
&aes_gcm_test_case_0a /* pointer to next testcase */
|
||||
&srtp_aes_gcm_test_case_0a /* pointer to next testcase */
|
||||
};
|
||||
|
||||
uint8_t aes_gcm_test_case_1_key[AES_256_GCM_KEYSIZE_WSALT] = {
|
||||
static const uint8_t srtp_aes_gcm_test_case_1_key[SRTP_AES_GCM_256_KEY_LEN_WSALT] = {
|
||||
0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
|
||||
0xa5, 0x59, 0x09, 0xc5, 0x54, 0x66, 0x93, 0x1c,
|
||||
0xaf, 0xf5, 0x26, 0x9a, 0x21, 0xd5, 0x14, 0xb2,
|
||||
0xaf, 0xf5, 0x26, 0x9a, 0x21, 0xd5, 0x14, 0xb2,
|
||||
0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
|
||||
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
|
||||
0x09, 0x0a, 0x0b, 0x0c,
|
||||
0x09, 0x0a, 0x0b, 0x0c,
|
||||
|
||||
};
|
||||
|
||||
uint8_t aes_gcm_test_case_1_iv[12] = {
|
||||
0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
|
||||
static uint8_t srtp_aes_gcm_test_case_1_iv[12] = {
|
||||
0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
|
||||
0xde, 0xca, 0xf8, 0x88
|
||||
};
|
||||
|
||||
uint8_t aes_gcm_test_case_1_plaintext[60] = {
|
||||
static const uint8_t srtp_aes_gcm_test_case_1_plaintext[60] = {
|
||||
0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
|
||||
0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
|
||||
0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
|
||||
@@ -479,89 +483,85 @@ uint8_t aes_gcm_test_case_1_plaintext[60] = {
|
||||
0xba, 0x63, 0x7b, 0x39
|
||||
};
|
||||
|
||||
uint8_t aes_gcm_test_case_1_aad[20] = {
|
||||
static const uint8_t srtp_aes_gcm_test_case_1_aad[20] = {
|
||||
0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
|
||||
0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
|
||||
0xab, 0xad, 0xda, 0xd2
|
||||
};
|
||||
|
||||
uint8_t aes_gcm_test_case_1_ciphertext[76] = {
|
||||
0x0b, 0x11, 0xcf, 0xaf, 0x68, 0x4d, 0xae, 0x46,
|
||||
0xc7, 0x90, 0xb8, 0x8e, 0xb7, 0x6a, 0x76, 0x2a,
|
||||
0x94, 0x82, 0xca, 0xab, 0x3e, 0x39, 0xd7, 0x86,
|
||||
0x1b, 0xc7, 0x93, 0xed, 0x75, 0x7f, 0x23, 0x5a,
|
||||
0xda, 0xfd, 0xd3, 0xe2, 0x0e, 0x80, 0x87, 0xa9,
|
||||
0x6d, 0xd7, 0xe2, 0x6a, 0x7d, 0x5f, 0xb4, 0x80,
|
||||
0xef, 0xef, 0xc5, 0x29, 0x12, 0xd1, 0xaa, 0x10,
|
||||
0x09, 0xc9, 0x86, 0xc1,
|
||||
static const uint8_t srtp_aes_gcm_test_case_1_ciphertext[76] = {
|
||||
0x0b, 0x11, 0xcf, 0xaf, 0x68, 0x4d, 0xae, 0x46,
|
||||
0xc7, 0x90, 0xb8, 0x8e, 0xb7, 0x6a, 0x76, 0x2a,
|
||||
0x94, 0x82, 0xca, 0xab, 0x3e, 0x39, 0xd7, 0x86,
|
||||
0x1b, 0xc7, 0x93, 0xed, 0x75, 0x7f, 0x23, 0x5a,
|
||||
0xda, 0xfd, 0xd3, 0xe2, 0x0e, 0x80, 0x87, 0xa9,
|
||||
0x6d, 0xd7, 0xe2, 0x6a, 0x7d, 0x5f, 0xb4, 0x80,
|
||||
0xef, 0xef, 0xc5, 0x29, 0x12, 0xd1, 0xaa, 0x10,
|
||||
0x09, 0xc9, 0x86, 0xc1,
|
||||
/* the last 16 bytes are the tag */
|
||||
0x45, 0xbc, 0x03, 0xe6, 0xe1, 0xac, 0x0a, 0x9f,
|
||||
0x45, 0xbc, 0x03, 0xe6, 0xe1, 0xac, 0x0a, 0x9f,
|
||||
0x81, 0xcb, 0x8e, 0x5b, 0x46, 0x65, 0x63, 0x1d,
|
||||
};
|
||||
|
||||
cipher_test_case_t aes_gcm_test_case_1a = {
|
||||
AES_256_GCM_KEYSIZE_WSALT, /* octets in key */
|
||||
aes_gcm_test_case_1_key, /* key */
|
||||
aes_gcm_test_case_1_iv, /* packet index */
|
||||
60, /* octets in plaintext */
|
||||
aes_gcm_test_case_1_plaintext, /* plaintext */
|
||||
68, /* octets in ciphertext */
|
||||
aes_gcm_test_case_1_ciphertext, /* ciphertext + tag */
|
||||
20, /* octets in AAD */
|
||||
aes_gcm_test_case_1_aad, /* AAD */
|
||||
static const srtp_cipher_test_case_t srtp_aes_gcm_test_case_1a = {
|
||||
SRTP_AES_GCM_256_KEY_LEN_WSALT, /* octets in key */
|
||||
srtp_aes_gcm_test_case_1_key, /* key */
|
||||
srtp_aes_gcm_test_case_1_iv, /* packet index */
|
||||
60, /* octets in plaintext */
|
||||
srtp_aes_gcm_test_case_1_plaintext, /* plaintext */
|
||||
68, /* octets in ciphertext */
|
||||
srtp_aes_gcm_test_case_1_ciphertext, /* ciphertext + tag */
|
||||
20, /* octets in AAD */
|
||||
srtp_aes_gcm_test_case_1_aad, /* AAD */
|
||||
GCM_AUTH_TAG_LEN_8,
|
||||
NULL /* pointer to next testcase */
|
||||
NULL /* pointer to next testcase */
|
||||
};
|
||||
|
||||
cipher_test_case_t aes_gcm_test_case_1 = {
|
||||
AES_256_GCM_KEYSIZE_WSALT, /* octets in key */
|
||||
aes_gcm_test_case_1_key, /* key */
|
||||
aes_gcm_test_case_1_iv, /* packet index */
|
||||
60, /* octets in plaintext */
|
||||
aes_gcm_test_case_1_plaintext, /* plaintext */
|
||||
76, /* octets in ciphertext */
|
||||
aes_gcm_test_case_1_ciphertext, /* ciphertext + tag */
|
||||
20, /* octets in AAD */
|
||||
aes_gcm_test_case_1_aad, /* AAD */
|
||||
static const srtp_cipher_test_case_t srtp_aes_gcm_test_case_1 = {
|
||||
SRTP_AES_GCM_256_KEY_LEN_WSALT, /* octets in key */
|
||||
srtp_aes_gcm_test_case_1_key, /* key */
|
||||
srtp_aes_gcm_test_case_1_iv, /* packet index */
|
||||
60, /* octets in plaintext */
|
||||
srtp_aes_gcm_test_case_1_plaintext, /* plaintext */
|
||||
76, /* octets in ciphertext */
|
||||
srtp_aes_gcm_test_case_1_ciphertext, /* ciphertext + tag */
|
||||
20, /* octets in AAD */
|
||||
srtp_aes_gcm_test_case_1_aad, /* AAD */
|
||||
GCM_AUTH_TAG_LEN,
|
||||
&aes_gcm_test_case_1a /* pointer to next testcase */
|
||||
&srtp_aes_gcm_test_case_1a /* pointer to next testcase */
|
||||
};
|
||||
|
||||
/*
|
||||
* This is the vector function table for this crypto engine.
|
||||
*/
|
||||
cipher_type_t aes_gcm_128_openssl = {
|
||||
(cipher_alloc_func_t) aes_gcm_openssl_alloc,
|
||||
(cipher_dealloc_func_t) aes_gcm_openssl_dealloc,
|
||||
(cipher_init_func_t) aes_gcm_openssl_context_init,
|
||||
(cipher_set_aad_func_t) aes_gcm_openssl_set_aad,
|
||||
(cipher_encrypt_func_t) aes_gcm_openssl_encrypt,
|
||||
(cipher_decrypt_func_t) aes_gcm_openssl_decrypt,
|
||||
(cipher_set_iv_func_t) aes_gcm_openssl_set_iv,
|
||||
(cipher_get_tag_func_t) aes_gcm_openssl_get_tag,
|
||||
(char*) aes_gcm_128_openssl_description,
|
||||
(int) 0, /* instance count */
|
||||
(cipher_test_case_t*) &aes_gcm_test_case_0,
|
||||
(debug_module_t*) &mod_aes_gcm,
|
||||
(cipher_type_id_t) AES_128_GCM
|
||||
const srtp_cipher_type_t srtp_aes_gcm_128_openssl = {
|
||||
srtp_aes_gcm_openssl_alloc,
|
||||
srtp_aes_gcm_openssl_dealloc,
|
||||
srtp_aes_gcm_openssl_context_init,
|
||||
srtp_aes_gcm_openssl_set_aad,
|
||||
srtp_aes_gcm_openssl_encrypt,
|
||||
srtp_aes_gcm_openssl_decrypt,
|
||||
srtp_aes_gcm_openssl_set_iv,
|
||||
srtp_aes_gcm_openssl_get_tag,
|
||||
srtp_aes_gcm_128_openssl_description,
|
||||
&srtp_aes_gcm_test_case_0,
|
||||
SRTP_AES_GCM_128
|
||||
};
|
||||
|
||||
/*
|
||||
* This is the vector function table for this crypto engine.
|
||||
*/
|
||||
cipher_type_t aes_gcm_256_openssl = {
|
||||
(cipher_alloc_func_t) aes_gcm_openssl_alloc,
|
||||
(cipher_dealloc_func_t) aes_gcm_openssl_dealloc,
|
||||
(cipher_init_func_t) aes_gcm_openssl_context_init,
|
||||
(cipher_set_aad_func_t) aes_gcm_openssl_set_aad,
|
||||
(cipher_encrypt_func_t) aes_gcm_openssl_encrypt,
|
||||
(cipher_decrypt_func_t) aes_gcm_openssl_decrypt,
|
||||
(cipher_set_iv_func_t) aes_gcm_openssl_set_iv,
|
||||
(cipher_get_tag_func_t) aes_gcm_openssl_get_tag,
|
||||
(char*) aes_gcm_256_openssl_description,
|
||||
(int) 0, /* instance count */
|
||||
(cipher_test_case_t*) &aes_gcm_test_case_1,
|
||||
(debug_module_t*) &mod_aes_gcm,
|
||||
(cipher_type_id_t) AES_256_GCM
|
||||
const srtp_cipher_type_t srtp_aes_gcm_256_openssl = {
|
||||
srtp_aes_gcm_openssl_alloc,
|
||||
srtp_aes_gcm_openssl_dealloc,
|
||||
srtp_aes_gcm_openssl_context_init,
|
||||
srtp_aes_gcm_openssl_set_aad,
|
||||
srtp_aes_gcm_openssl_encrypt,
|
||||
srtp_aes_gcm_openssl_decrypt,
|
||||
srtp_aes_gcm_openssl_set_iv,
|
||||
srtp_aes_gcm_openssl_get_tag,
|
||||
srtp_aes_gcm_256_openssl_description,
|
||||
&srtp_aes_gcm_test_case_1,
|
||||
SRTP_AES_GCM_256
|
||||
};
|
||||
|
||||
+325
-397
@@ -8,26 +8,26 @@
|
||||
*/
|
||||
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2001-2006,2013 Cisco Systems, Inc.
|
||||
*
|
||||
* Copyright (c) 2001-2017 Cisco Systems, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
*
|
||||
* Redistributions in binary form must reproduce the above
|
||||
* copyright notice, this list of conditions and the following
|
||||
* disclaimer in the documentation and/or other materials provided
|
||||
* with the distribution.
|
||||
*
|
||||
*
|
||||
* Neither the name of the Cisco Systems, Inc. nor the names of its
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
@@ -43,6 +43,9 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#define ALIGN_32 0
|
||||
|
||||
@@ -50,17 +53,19 @@
|
||||
#include "alloc.h"
|
||||
|
||||
|
||||
debug_module_t mod_aes_icm = {
|
||||
0, /* debugging is off by default */
|
||||
"aes icm" /* printable module name */
|
||||
srtp_debug_module_t srtp_mod_aes_icm = {
|
||||
0, /* debugging is off by default */
|
||||
"aes icm" /* printable module name */
|
||||
};
|
||||
extern const srtp_cipher_type_t srtp_aes_icm_128;
|
||||
extern const srtp_cipher_type_t srtp_aes_icm_256;
|
||||
|
||||
/*
|
||||
* integer counter mode works as follows:
|
||||
*
|
||||
* 16 bits
|
||||
* <----->
|
||||
* +------+------+------+------+------+------+------+------+
|
||||
* +------+------+------+------+------+------+------+------+
|
||||
* | nonce | pakcet index | ctr |---+
|
||||
* +------+------+------+------+------+------+------+------+ |
|
||||
* |
|
||||
@@ -71,16 +76,16 @@ debug_module_t mod_aes_icm = {
|
||||
* +---------+
|
||||
* | encrypt |
|
||||
* +---------+
|
||||
* |
|
||||
* |
|
||||
* +------+------+------+------+------+------+------+------+ |
|
||||
* | keystream block |<--+
|
||||
* +------+------+------+------+------+------+------+------+
|
||||
* | keystream block |<--+
|
||||
* +------+------+------+------+------+------+------+------+
|
||||
*
|
||||
* All fields are big-endian
|
||||
*
|
||||
* ctr is the block counter, which increments from zero for
|
||||
* each packet (16 bits wide)
|
||||
*
|
||||
*
|
||||
* packet index is distinct for each packet (48 bits wide)
|
||||
*
|
||||
* nonce can be distinct across many uses of the same key, or
|
||||
@@ -89,77 +94,77 @@ debug_module_t mod_aes_icm = {
|
||||
*
|
||||
*/
|
||||
|
||||
err_status_t
|
||||
aes_icm_alloc_ismacryp(cipher_t **c, int key_len, int forIsmacryp) {
|
||||
extern cipher_type_t aes_icm;
|
||||
uint8_t *pointer;
|
||||
int tmp;
|
||||
static srtp_err_status_t srtp_aes_icm_alloc (srtp_cipher_t **c, int key_len, int tlen)
|
||||
{
|
||||
srtp_aes_icm_ctx_t *icm;
|
||||
|
||||
debug_print(mod_aes_icm,
|
||||
"allocating cipher with key length %d", key_len);
|
||||
debug_print(srtp_mod_aes_icm,
|
||||
"allocating cipher with key length %d", key_len);
|
||||
|
||||
/*
|
||||
* Ismacryp, for example, uses 16 byte key + 8 byte
|
||||
* salt so this function is called with key_len = 24.
|
||||
* The check for key_len = 30/38/46 does not apply. Our usage
|
||||
* of aes functions with key_len = values other than 30
|
||||
* has not broken anything. Don't know what would be the
|
||||
* effect of skipping this check for srtp in general.
|
||||
*/
|
||||
if (!(forIsmacryp && key_len > 16 && key_len < 30) &&
|
||||
key_len != 30 && key_len != 38 && key_len != 46)
|
||||
return err_status_bad_param;
|
||||
/*
|
||||
* The check for key_len = 30/46 does not apply. Our usage
|
||||
* of aes functions with key_len = values other than 30
|
||||
* has not broken anything. Don't know what would be the
|
||||
* effect of skipping this check for srtp in general.
|
||||
*/
|
||||
if (key_len != SRTP_AES_ICM_128_KEY_LEN_WSALT && key_len != SRTP_AES_ICM_256_KEY_LEN_WSALT) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
/* allocate memory a cipher of type aes_icm */
|
||||
tmp = (sizeof(aes_icm_ctx_t) + sizeof(cipher_t));
|
||||
pointer = (uint8_t*)crypto_alloc(tmp);
|
||||
if (pointer == NULL)
|
||||
return err_status_alloc_fail;
|
||||
/* allocate memory a cipher of type aes_icm */
|
||||
*c = (srtp_cipher_t *)srtp_crypto_alloc(sizeof(srtp_cipher_t));
|
||||
if (*c == NULL) {
|
||||
return srtp_err_status_alloc_fail;
|
||||
}
|
||||
memset(*c, 0x0, sizeof(srtp_cipher_t));
|
||||
|
||||
/* set pointers */
|
||||
*c = (cipher_t *)pointer;
|
||||
switch (key_len) {
|
||||
case 46:
|
||||
(*c)->algorithm = AES_256_ICM;
|
||||
break;
|
||||
case 38:
|
||||
(*c)->algorithm = AES_192_ICM;
|
||||
break;
|
||||
default:
|
||||
(*c)->algorithm = AES_128_ICM;
|
||||
break;
|
||||
}
|
||||
(*c)->type = &aes_icm;
|
||||
(*c)->state = pointer + sizeof(cipher_t);
|
||||
icm = (srtp_aes_icm_ctx_t *)srtp_crypto_alloc(sizeof(srtp_aes_icm_ctx_t));
|
||||
if (icm == NULL) {
|
||||
srtp_crypto_free(*c);
|
||||
return srtp_err_status_alloc_fail;
|
||||
}
|
||||
memset(icm, 0x0, sizeof(srtp_aes_icm_ctx_t));
|
||||
|
||||
/* increment ref_count */
|
||||
aes_icm.ref_count++;
|
||||
/* set pointers */
|
||||
(*c)->state = icm;
|
||||
|
||||
/* set key size */
|
||||
(*c)->key_len = key_len;
|
||||
switch (key_len) {
|
||||
case SRTP_AES_ICM_256_KEY_LEN_WSALT:
|
||||
(*c)->algorithm = SRTP_AES_ICM_256;
|
||||
(*c)->type = &srtp_aes_icm_256;
|
||||
break;
|
||||
default:
|
||||
(*c)->algorithm = SRTP_AES_ICM_128;
|
||||
(*c)->type = &srtp_aes_icm_128;
|
||||
break;
|
||||
}
|
||||
|
||||
return err_status_ok;
|
||||
/* set key size */
|
||||
icm->key_size = key_len;
|
||||
(*c)->key_len = key_len;
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
err_status_t aes_icm_alloc(cipher_t **c, int key_len, int forIsmacryp) {
|
||||
return aes_icm_alloc_ismacryp(c, key_len, 0);
|
||||
}
|
||||
static srtp_err_status_t srtp_aes_icm_dealloc (srtp_cipher_t *c)
|
||||
{
|
||||
srtp_aes_icm_ctx_t *ctx;
|
||||
|
||||
err_status_t
|
||||
aes_icm_dealloc(cipher_t *c) {
|
||||
extern cipher_type_t aes_icm;
|
||||
if (c == NULL) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
/* zeroize entire state*/
|
||||
octet_string_set_to_zero((uint8_t *)c,
|
||||
sizeof(aes_icm_ctx_t) + sizeof(cipher_t));
|
||||
ctx = (srtp_aes_icm_ctx_t *)c->state;
|
||||
if (ctx) {
|
||||
/* zeroize the key material */
|
||||
octet_string_set_to_zero(ctx, sizeof(srtp_aes_icm_ctx_t));
|
||||
srtp_crypto_free(ctx);
|
||||
}
|
||||
|
||||
/* free memory */
|
||||
crypto_free(c);
|
||||
/* free the cipher context */
|
||||
srtp_crypto_free(c);
|
||||
|
||||
/* decrement ref_count */
|
||||
aes_icm.ref_count--;
|
||||
|
||||
return err_status_ok;
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
|
||||
@@ -167,114 +172,57 @@ aes_icm_dealloc(cipher_t *c) {
|
||||
* aes_icm_context_init(...) initializes the aes_icm_context
|
||||
* using the value in key[].
|
||||
*
|
||||
* the key is the secret key
|
||||
* the key is the secret key
|
||||
*
|
||||
* the salt is unpredictable (but not necessarily secret) data which
|
||||
* randomizes the starting point in the keystream
|
||||
*/
|
||||
|
||||
err_status_t
|
||||
aes_icm_context_init(aes_icm_ctx_t *c, const uint8_t *key, int key_len) {
|
||||
err_status_t status;
|
||||
int base_key_len, copy_len;
|
||||
static srtp_err_status_t srtp_aes_icm_context_init (void *cv, const uint8_t *key)
|
||||
{
|
||||
srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
|
||||
srtp_err_status_t status;
|
||||
int base_key_len, copy_len;
|
||||
|
||||
if (key_len > 16 && key_len < 30) /* Ismacryp */
|
||||
base_key_len = 16;
|
||||
else if (key_len == 30 || key_len == 38 || key_len == 46)
|
||||
base_key_len = key_len - 14;
|
||||
else
|
||||
return err_status_bad_param;
|
||||
if (c->key_size == SRTP_AES_ICM_128_KEY_LEN_WSALT || c->key_size == SRTP_AES_ICM_256_KEY_LEN_WSALT) {
|
||||
base_key_len = c->key_size - SRTP_SALT_LEN;
|
||||
} else{
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
/*
|
||||
* set counter and initial values to 'offset' value, being careful not to
|
||||
* go past the end of the key buffer
|
||||
*/
|
||||
v128_set_to_zero(&c->counter);
|
||||
v128_set_to_zero(&c->offset);
|
||||
|
||||
copy_len = key_len - base_key_len;
|
||||
/* force last two octets of the offset to be left zero (for srtp compatibility) */
|
||||
if (copy_len > 14)
|
||||
copy_len = 14;
|
||||
|
||||
memcpy(&c->counter, key + base_key_len, copy_len);
|
||||
memcpy(&c->offset, key + base_key_len, copy_len);
|
||||
|
||||
debug_print(mod_aes_icm,
|
||||
"key: %s", octet_string_hex_string(key, base_key_len));
|
||||
debug_print(mod_aes_icm,
|
||||
"offset: %s", v128_hex_string(&c->offset));
|
||||
|
||||
/* expand key */
|
||||
status = aes_expand_encryption_key(key, base_key_len, &c->expanded_key);
|
||||
if (status) {
|
||||
/*
|
||||
* set counter and initial values to 'offset' value, being careful not to
|
||||
* go past the end of the key buffer
|
||||
*/
|
||||
v128_set_to_zero(&c->counter);
|
||||
v128_set_to_zero(&c->offset);
|
||||
return status;
|
||||
}
|
||||
|
||||
/* indicate that the keystream_buffer is empty */
|
||||
c->bytes_in_buffer = 0;
|
||||
copy_len = c->key_size - base_key_len;
|
||||
/* force last two octets of the offset to be left zero (for srtp compatibility) */
|
||||
if (copy_len > SRTP_SALT_LEN) {
|
||||
copy_len = SRTP_SALT_LEN;
|
||||
}
|
||||
|
||||
return err_status_ok;
|
||||
}
|
||||
memcpy(&c->counter, key + base_key_len, copy_len);
|
||||
memcpy(&c->offset, key + base_key_len, copy_len);
|
||||
|
||||
/*
|
||||
* aes_icm_set_octet(c, i) sets the counter of the context which it is
|
||||
* passed so that the next octet of keystream that will be generated
|
||||
* is the ith octet
|
||||
*/
|
||||
debug_print(srtp_mod_aes_icm,
|
||||
"key: %s", srtp_octet_string_hex_string(key, base_key_len));
|
||||
debug_print(srtp_mod_aes_icm,
|
||||
"offset: %s", v128_hex_string(&c->offset));
|
||||
|
||||
err_status_t
|
||||
aes_icm_set_octet(aes_icm_ctx_t *c,
|
||||
uint64_t octet_num) {
|
||||
/* expand key */
|
||||
status = srtp_aes_expand_encryption_key(key, base_key_len, &c->expanded_key);
|
||||
if (status) {
|
||||
v128_set_to_zero(&c->counter);
|
||||
v128_set_to_zero(&c->offset);
|
||||
return status;
|
||||
}
|
||||
|
||||
#ifdef NO_64BIT_MATH
|
||||
int tail_num = low32(octet_num) & 0x0f;
|
||||
/* 64-bit right-shift 4 */
|
||||
uint64_t block_num = make64(high32(octet_num) >> 4,
|
||||
((high32(octet_num) & 0x0f)<<(32-4)) |
|
||||
(low32(octet_num) >> 4));
|
||||
#else
|
||||
int tail_num = (int)(octet_num % 16);
|
||||
uint64_t block_num = octet_num / 16;
|
||||
#endif
|
||||
|
||||
|
||||
/* set counter value */
|
||||
/* FIX - There's no way this is correct */
|
||||
c->counter.v64[0] = c->offset.v64[0];
|
||||
#ifdef NO_64BIT_MATH
|
||||
c->counter.v64[0] = make64(high32(c->offset.v64[0]) ^ high32(block_num),
|
||||
low32(c->offset.v64[0]) ^ low32(block_num));
|
||||
#else
|
||||
c->counter.v64[0] = c->offset.v64[0] ^ block_num;
|
||||
#endif
|
||||
|
||||
debug_print(mod_aes_icm,
|
||||
"set_octet: %s", v128_hex_string(&c->counter));
|
||||
|
||||
/* fill keystream buffer, if needed */
|
||||
if (tail_num) {
|
||||
v128_copy(&c->keystream_buffer, &c->counter);
|
||||
aes_encrypt(&c->keystream_buffer, &c->expanded_key);
|
||||
c->bytes_in_buffer = sizeof(v128_t);
|
||||
|
||||
debug_print(mod_aes_icm, "counter: %s",
|
||||
v128_hex_string(&c->counter));
|
||||
debug_print(mod_aes_icm, "ciphertext: %s",
|
||||
v128_hex_string(&c->keystream_buffer));
|
||||
|
||||
/* indicate number of bytes in keystream_buffer */
|
||||
c->bytes_in_buffer = sizeof(v128_t) - tail_num;
|
||||
|
||||
} else {
|
||||
|
||||
/* indicate that keystream_buffer is empty */
|
||||
/* indicate that the keystream_buffer is empty */
|
||||
c->bytes_in_buffer = 0;
|
||||
}
|
||||
|
||||
return err_status_ok;
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -282,25 +230,26 @@ aes_icm_set_octet(aes_icm_ctx_t *c,
|
||||
* the offset
|
||||
*/
|
||||
|
||||
err_status_t
|
||||
aes_icm_set_iv(aes_icm_ctx_t *c, void *iv, int direction) {
|
||||
v128_t nonce;
|
||||
static srtp_err_status_t srtp_aes_icm_set_iv (void *cv, uint8_t *iv, srtp_cipher_direction_t direction)
|
||||
{
|
||||
srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
|
||||
v128_t nonce;
|
||||
|
||||
/* set nonce (for alignment) */
|
||||
v128_copy_octet_string(&nonce, iv);
|
||||
/* set nonce (for alignment) */
|
||||
v128_copy_octet_string(&nonce, iv);
|
||||
|
||||
debug_print(mod_aes_icm,
|
||||
"setting iv: %s", v128_hex_string(&nonce));
|
||||
|
||||
v128_xor(&c->counter, &c->offset, &nonce);
|
||||
|
||||
debug_print(mod_aes_icm,
|
||||
"set_counter: %s", v128_hex_string(&c->counter));
|
||||
debug_print(srtp_mod_aes_icm,
|
||||
"setting iv: %s", v128_hex_string(&nonce));
|
||||
|
||||
/* indicate that the keystream_buffer is empty */
|
||||
c->bytes_in_buffer = 0;
|
||||
v128_xor(&c->counter, &c->offset, &nonce);
|
||||
|
||||
return err_status_ok;
|
||||
debug_print(srtp_mod_aes_icm,
|
||||
"set_counter: %s", v128_hex_string(&c->counter));
|
||||
|
||||
/* indicate that the keystream_buffer is empty */
|
||||
c->bytes_in_buffer = 0;
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
|
||||
@@ -311,37 +260,24 @@ aes_icm_set_iv(aes_icm_ctx_t *c, void *iv, int direction) {
|
||||
*
|
||||
* this is an internal, hopefully inlined function
|
||||
*/
|
||||
|
||||
static inline void
|
||||
aes_icm_advance_ismacryp(aes_icm_ctx_t *c, uint8_t forIsmacryp) {
|
||||
/* fill buffer with new keystream */
|
||||
v128_copy(&c->keystream_buffer, &c->counter);
|
||||
aes_encrypt(&c->keystream_buffer, &c->expanded_key);
|
||||
c->bytes_in_buffer = sizeof(v128_t);
|
||||
static void srtp_aes_icm_advance (srtp_aes_icm_ctx_t *c)
|
||||
{
|
||||
/* fill buffer with new keystream */
|
||||
v128_copy(&c->keystream_buffer, &c->counter);
|
||||
srtp_aes_encrypt(&c->keystream_buffer, &c->expanded_key);
|
||||
c->bytes_in_buffer = sizeof(v128_t);
|
||||
|
||||
debug_print(mod_aes_icm, "counter: %s",
|
||||
v128_hex_string(&c->counter));
|
||||
debug_print(mod_aes_icm, "ciphertext: %s",
|
||||
v128_hex_string(&c->keystream_buffer));
|
||||
|
||||
/* clock counter forward */
|
||||
debug_print(srtp_mod_aes_icm, "counter: %s",
|
||||
v128_hex_string(&c->counter));
|
||||
debug_print(srtp_mod_aes_icm, "ciphertext: %s",
|
||||
v128_hex_string(&c->keystream_buffer));
|
||||
|
||||
if (forIsmacryp) {
|
||||
uint32_t temp;
|
||||
//alex's clock counter forward
|
||||
temp = ntohl(c->counter.v32[3]);
|
||||
c->counter.v32[3] = htonl(++temp);
|
||||
} else {
|
||||
if (!++(c->counter.v8[15]))
|
||||
++(c->counter.v8[14]);
|
||||
}
|
||||
/* clock counter forward */
|
||||
if (!++(c->counter.v8[15])) {
|
||||
++(c->counter.v8[14]);
|
||||
}
|
||||
}
|
||||
|
||||
static inline void aes_icm_advance(aes_icm_ctx_t *c) {
|
||||
aes_icm_advance_ismacryp(c, 0);
|
||||
}
|
||||
|
||||
|
||||
/*e
|
||||
* icm_encrypt deals with the following cases:
|
||||
*
|
||||
@@ -352,221 +288,202 @@ static inline void aes_icm_advance(aes_icm_ctx_t *c) {
|
||||
* - add keystream into data until keystream_buffer is depleted
|
||||
* - loop over blocks, filling keystream_buffer and then
|
||||
* adding keystream into data
|
||||
* - fill buffer then add in remaining (< 16) bytes of keystream
|
||||
* - fill buffer then add in remaining (< 16) bytes of keystream
|
||||
*/
|
||||
|
||||
err_status_t
|
||||
aes_icm_encrypt_ismacryp(aes_icm_ctx_t *c,
|
||||
unsigned char *buf, unsigned int *enc_len,
|
||||
int forIsmacryp) {
|
||||
unsigned int bytes_to_encr = *enc_len;
|
||||
unsigned int i;
|
||||
uint32_t *b;
|
||||
static srtp_err_status_t srtp_aes_icm_encrypt (void *cv,
|
||||
unsigned char *buf, unsigned int *enc_len)
|
||||
{
|
||||
srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t*)cv;
|
||||
unsigned int bytes_to_encr = *enc_len;
|
||||
unsigned int i;
|
||||
uint32_t *b;
|
||||
|
||||
/* check that there's enough segment left but not for ismacryp*/
|
||||
if (!forIsmacryp && (bytes_to_encr + htons(c->counter.v16[7])) > 0xffff)
|
||||
return err_status_terminus;
|
||||
/* check that there's enough segment left*/
|
||||
if ((bytes_to_encr + htons(c->counter.v16[7])) > 0xffff) {
|
||||
return srtp_err_status_terminus;
|
||||
}
|
||||
|
||||
debug_print(mod_aes_icm, "block index: %d",
|
||||
htons(c->counter.v16[7]));
|
||||
if (bytes_to_encr <= (unsigned int)c->bytes_in_buffer) {
|
||||
|
||||
/* deal with odd case of small bytes_to_encr */
|
||||
for (i = (sizeof(v128_t) - c->bytes_in_buffer);
|
||||
i < (sizeof(v128_t) - c->bytes_in_buffer + bytes_to_encr); i++)
|
||||
{
|
||||
*buf++ ^= c->keystream_buffer.v8[i];
|
||||
}
|
||||
debug_print(srtp_mod_aes_icm, "block index: %d",
|
||||
htons(c->counter.v16[7]));
|
||||
if (bytes_to_encr <= (unsigned int)c->bytes_in_buffer) {
|
||||
|
||||
c->bytes_in_buffer -= bytes_to_encr;
|
||||
/* deal with odd case of small bytes_to_encr */
|
||||
for (i = (sizeof(v128_t) - c->bytes_in_buffer);
|
||||
i < (sizeof(v128_t) - c->bytes_in_buffer + bytes_to_encr); i++) {
|
||||
*buf++ ^= c->keystream_buffer.v8[i];
|
||||
}
|
||||
|
||||
/* return now to avoid the main loop */
|
||||
return err_status_ok;
|
||||
c->bytes_in_buffer -= bytes_to_encr;
|
||||
|
||||
} else {
|
||||
|
||||
/* encrypt bytes until the remaining data is 16-byte aligned */
|
||||
for (i=(sizeof(v128_t) - c->bytes_in_buffer); i < sizeof(v128_t); i++)
|
||||
*buf++ ^= c->keystream_buffer.v8[i];
|
||||
/* return now to avoid the main loop */
|
||||
return srtp_err_status_ok;
|
||||
|
||||
bytes_to_encr -= c->bytes_in_buffer;
|
||||
c->bytes_in_buffer = 0;
|
||||
} else {
|
||||
|
||||
}
|
||||
|
||||
/* now loop over entire 16-byte blocks of keystream */
|
||||
for (i=0; i < (bytes_to_encr/sizeof(v128_t)); i++) {
|
||||
/* encrypt bytes until the remaining data is 16-byte aligned */
|
||||
for (i = (sizeof(v128_t) - c->bytes_in_buffer); i < sizeof(v128_t); i++) {
|
||||
*buf++ ^= c->keystream_buffer.v8[i];
|
||||
}
|
||||
|
||||
/* fill buffer with new keystream */
|
||||
aes_icm_advance_ismacryp(c, forIsmacryp);
|
||||
bytes_to_encr -= c->bytes_in_buffer;
|
||||
c->bytes_in_buffer = 0;
|
||||
|
||||
/*
|
||||
* add keystream into the data buffer (this would be a lot faster
|
||||
* if we could assume 32-bit alignment!)
|
||||
*/
|
||||
}
|
||||
|
||||
/* now loop over entire 16-byte blocks of keystream */
|
||||
for (i = 0; i < (bytes_to_encr / sizeof(v128_t)); i++) {
|
||||
|
||||
/* fill buffer with new keystream */
|
||||
srtp_aes_icm_advance(c);
|
||||
|
||||
/*
|
||||
* add keystream into the data buffer (this would be a lot faster
|
||||
* if we could assume 32-bit alignment!)
|
||||
*/
|
||||
|
||||
#if ALIGN_32
|
||||
b = (uint32_t *)buf;
|
||||
*b++ ^= c->keystream_buffer.v32[0];
|
||||
*b++ ^= c->keystream_buffer.v32[1];
|
||||
*b++ ^= c->keystream_buffer.v32[2];
|
||||
*b++ ^= c->keystream_buffer.v32[3];
|
||||
buf = (uint8_t *)b;
|
||||
#else
|
||||
if ((((unsigned long) buf) & 0x03) != 0) {
|
||||
*buf++ ^= c->keystream_buffer.v8[0];
|
||||
*buf++ ^= c->keystream_buffer.v8[1];
|
||||
*buf++ ^= c->keystream_buffer.v8[2];
|
||||
*buf++ ^= c->keystream_buffer.v8[3];
|
||||
*buf++ ^= c->keystream_buffer.v8[4];
|
||||
*buf++ ^= c->keystream_buffer.v8[5];
|
||||
*buf++ ^= c->keystream_buffer.v8[6];
|
||||
*buf++ ^= c->keystream_buffer.v8[7];
|
||||
*buf++ ^= c->keystream_buffer.v8[8];
|
||||
*buf++ ^= c->keystream_buffer.v8[9];
|
||||
*buf++ ^= c->keystream_buffer.v8[10];
|
||||
*buf++ ^= c->keystream_buffer.v8[11];
|
||||
*buf++ ^= c->keystream_buffer.v8[12];
|
||||
*buf++ ^= c->keystream_buffer.v8[13];
|
||||
*buf++ ^= c->keystream_buffer.v8[14];
|
||||
*buf++ ^= c->keystream_buffer.v8[15];
|
||||
} else {
|
||||
b = (uint32_t *)buf;
|
||||
*b++ ^= c->keystream_buffer.v32[0];
|
||||
*b++ ^= c->keystream_buffer.v32[1];
|
||||
*b++ ^= c->keystream_buffer.v32[2];
|
||||
*b++ ^= c->keystream_buffer.v32[3];
|
||||
buf = (uint8_t *)b;
|
||||
b = (uint32_t*)buf;
|
||||
*b++ ^= c->keystream_buffer.v32[0];
|
||||
*b++ ^= c->keystream_buffer.v32[1];
|
||||
*b++ ^= c->keystream_buffer.v32[2];
|
||||
*b++ ^= c->keystream_buffer.v32[3];
|
||||
buf = (uint8_t*)b;
|
||||
#else
|
||||
if ((((unsigned long)buf) & 0x03) != 0) {
|
||||
*buf++ ^= c->keystream_buffer.v8[0];
|
||||
*buf++ ^= c->keystream_buffer.v8[1];
|
||||
*buf++ ^= c->keystream_buffer.v8[2];
|
||||
*buf++ ^= c->keystream_buffer.v8[3];
|
||||
*buf++ ^= c->keystream_buffer.v8[4];
|
||||
*buf++ ^= c->keystream_buffer.v8[5];
|
||||
*buf++ ^= c->keystream_buffer.v8[6];
|
||||
*buf++ ^= c->keystream_buffer.v8[7];
|
||||
*buf++ ^= c->keystream_buffer.v8[8];
|
||||
*buf++ ^= c->keystream_buffer.v8[9];
|
||||
*buf++ ^= c->keystream_buffer.v8[10];
|
||||
*buf++ ^= c->keystream_buffer.v8[11];
|
||||
*buf++ ^= c->keystream_buffer.v8[12];
|
||||
*buf++ ^= c->keystream_buffer.v8[13];
|
||||
*buf++ ^= c->keystream_buffer.v8[14];
|
||||
*buf++ ^= c->keystream_buffer.v8[15];
|
||||
} else {
|
||||
b = (uint32_t*)buf;
|
||||
*b++ ^= c->keystream_buffer.v32[0];
|
||||
*b++ ^= c->keystream_buffer.v32[1];
|
||||
*b++ ^= c->keystream_buffer.v32[2];
|
||||
*b++ ^= c->keystream_buffer.v32[3];
|
||||
buf = (uint8_t*)b;
|
||||
}
|
||||
#endif /* #if ALIGN_32 */
|
||||
|
||||
}
|
||||
#endif /* #if ALIGN_32 */
|
||||
|
||||
}
|
||||
|
||||
/* if there is a tail end of the data, process it */
|
||||
if ((bytes_to_encr & 0xf) != 0) {
|
||||
|
||||
/* fill buffer with new keystream */
|
||||
aes_icm_advance_ismacryp(c, forIsmacryp);
|
||||
|
||||
for (i=0; i < (bytes_to_encr & 0xf); i++)
|
||||
*buf++ ^= c->keystream_buffer.v8[i];
|
||||
|
||||
/* reset the keystream buffer size to right value */
|
||||
c->bytes_in_buffer = sizeof(v128_t) - i;
|
||||
} else {
|
||||
/* if there is a tail end of the data, process it */
|
||||
if ((bytes_to_encr & 0xf) != 0) {
|
||||
|
||||
/* no tail, so just reset the keystream buffer size to zero */
|
||||
c->bytes_in_buffer = 0;
|
||||
/* fill buffer with new keystream */
|
||||
srtp_aes_icm_advance(c);
|
||||
|
||||
}
|
||||
for (i = 0; i < (bytes_to_encr & 0xf); i++) {
|
||||
*buf++ ^= c->keystream_buffer.v8[i];
|
||||
}
|
||||
|
||||
return err_status_ok;
|
||||
/* reset the keystream buffer size to right value */
|
||||
c->bytes_in_buffer = sizeof(v128_t) - i;
|
||||
} else {
|
||||
|
||||
/* no tail, so just reset the keystream buffer size to zero */
|
||||
c->bytes_in_buffer = 0;
|
||||
|
||||
}
|
||||
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
err_status_t
|
||||
aes_icm_encrypt(aes_icm_ctx_t *c, unsigned char *buf, unsigned int *enc_len) {
|
||||
return aes_icm_encrypt_ismacryp(c, buf, enc_len, 0);
|
||||
}
|
||||
static const char srtp_aes_icm_128_description[] = "AES-128 integer counter mode";
|
||||
static const char srtp_aes_icm_256_description[] = "AES-256 integer counter mode";
|
||||
|
||||
err_status_t
|
||||
aes_icm_output(aes_icm_ctx_t *c, uint8_t *buffer, int num_octets_to_output) {
|
||||
unsigned int len = num_octets_to_output;
|
||||
|
||||
/* zeroize the buffer */
|
||||
octet_string_set_to_zero(buffer, num_octets_to_output);
|
||||
|
||||
/* exor keystream into buffer */
|
||||
return aes_icm_encrypt(c, buffer, &len);
|
||||
}
|
||||
|
||||
uint16_t
|
||||
aes_icm_bytes_encrypted(aes_icm_ctx_t *c) {
|
||||
return htons(c->counter.v16[7]);
|
||||
}
|
||||
|
||||
char
|
||||
aes_icm_description[] = "aes integer counter mode";
|
||||
|
||||
uint8_t aes_icm_test_case_0_key[30] = {
|
||||
0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
|
||||
0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c,
|
||||
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
|
||||
0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd
|
||||
static const uint8_t srtp_aes_icm_128_test_case_0_key[SRTP_AES_ICM_128_KEY_LEN_WSALT] = {
|
||||
0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
|
||||
0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c,
|
||||
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
|
||||
0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd
|
||||
};
|
||||
|
||||
uint8_t aes_icm_test_case_0_nonce[16] = {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||
static uint8_t srtp_aes_icm_128_test_case_0_nonce[16] = {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||
};
|
||||
|
||||
uint8_t aes_icm_test_case_0_plaintext[32] = {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
static const uint8_t srtp_aes_icm_128_test_case_0_plaintext[32] = {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
};
|
||||
|
||||
uint8_t aes_icm_test_case_0_ciphertext[32] = {
|
||||
0xe0, 0x3e, 0xad, 0x09, 0x35, 0xc9, 0x5e, 0x80,
|
||||
0xe1, 0x66, 0xb1, 0x6d, 0xd9, 0x2b, 0x4e, 0xb4,
|
||||
0xd2, 0x35, 0x13, 0x16, 0x2b, 0x02, 0xd0, 0xf7,
|
||||
0x2a, 0x43, 0xa2, 0xfe, 0x4a, 0x5f, 0x97, 0xab
|
||||
static const uint8_t srtp_aes_icm_128_test_case_0_ciphertext[32] = {
|
||||
0xe0, 0x3e, 0xad, 0x09, 0x35, 0xc9, 0x5e, 0x80,
|
||||
0xe1, 0x66, 0xb1, 0x6d, 0xd9, 0x2b, 0x4e, 0xb4,
|
||||
0xd2, 0x35, 0x13, 0x16, 0x2b, 0x02, 0xd0, 0xf7,
|
||||
0x2a, 0x43, 0xa2, 0xfe, 0x4a, 0x5f, 0x97, 0xab
|
||||
};
|
||||
|
||||
cipher_test_case_t aes_icm_test_case_0 = {
|
||||
30, /* octets in key */
|
||||
aes_icm_test_case_0_key, /* key */
|
||||
aes_icm_test_case_0_nonce, /* packet index */
|
||||
32, /* octets in plaintext */
|
||||
aes_icm_test_case_0_plaintext, /* plaintext */
|
||||
32, /* octets in ciphertext */
|
||||
aes_icm_test_case_0_ciphertext, /* ciphertext */
|
||||
0,
|
||||
NULL,
|
||||
0,
|
||||
NULL /* pointer to next testcase */
|
||||
static const srtp_cipher_test_case_t srtp_aes_icm_128_test_case_0 = {
|
||||
SRTP_AES_ICM_128_KEY_LEN_WSALT, /* octets in key */
|
||||
srtp_aes_icm_128_test_case_0_key, /* key */
|
||||
srtp_aes_icm_128_test_case_0_nonce, /* packet index */
|
||||
32, /* octets in plaintext */
|
||||
srtp_aes_icm_128_test_case_0_plaintext, /* plaintext */
|
||||
32, /* octets in ciphertext */
|
||||
srtp_aes_icm_128_test_case_0_ciphertext, /* ciphertext */
|
||||
0,
|
||||
NULL,
|
||||
0,
|
||||
NULL /* pointer to next testcase */
|
||||
};
|
||||
|
||||
uint8_t aes_icm_test_case_1_key[46] = {
|
||||
0x57, 0xf8, 0x2f, 0xe3, 0x61, 0x3f, 0xd1, 0x70,
|
||||
0xa8, 0x5e, 0xc9, 0x3c, 0x40, 0xb1, 0xf0, 0x92,
|
||||
0x2e, 0xc4, 0xcb, 0x0d, 0xc0, 0x25, 0xb5, 0x82,
|
||||
0x72, 0x14, 0x7c, 0xc4, 0x38, 0x94, 0x4a, 0x98,
|
||||
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
|
||||
0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd
|
||||
static const uint8_t srtp_aes_icm_256_test_case_0_key[SRTP_AES_ICM_256_KEY_LEN_WSALT] = {
|
||||
0x57, 0xf8, 0x2f, 0xe3, 0x61, 0x3f, 0xd1, 0x70,
|
||||
0xa8, 0x5e, 0xc9, 0x3c, 0x40, 0xb1, 0xf0, 0x92,
|
||||
0x2e, 0xc4, 0xcb, 0x0d, 0xc0, 0x25, 0xb5, 0x82,
|
||||
0x72, 0x14, 0x7c, 0xc4, 0x38, 0x94, 0x4a, 0x98,
|
||||
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
|
||||
0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd
|
||||
};
|
||||
|
||||
uint8_t aes_icm_test_case_1_nonce[16] = {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||
static uint8_t srtp_aes_icm_256_test_case_0_nonce[16] = {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||
};
|
||||
|
||||
uint8_t aes_icm_test_case_1_plaintext[32] = {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
static const uint8_t srtp_aes_icm_256_test_case_0_plaintext[32] = {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
};
|
||||
|
||||
uint8_t aes_icm_test_case_1_ciphertext[32] = {
|
||||
0x92, 0xbd, 0xd2, 0x8a, 0x93, 0xc3, 0xf5, 0x25,
|
||||
0x11, 0xc6, 0x77, 0xd0, 0x8b, 0x55, 0x15, 0xa4,
|
||||
0x9d, 0xa7, 0x1b, 0x23, 0x78, 0xa8, 0x54, 0xf6,
|
||||
0x70, 0x50, 0x75, 0x6d, 0xed, 0x16, 0x5b, 0xac
|
||||
static const uint8_t srtp_aes_icm_256_test_case_0_ciphertext[32] = {
|
||||
0x92, 0xbd, 0xd2, 0x8a, 0x93, 0xc3, 0xf5, 0x25,
|
||||
0x11, 0xc6, 0x77, 0xd0, 0x8b, 0x55, 0x15, 0xa4,
|
||||
0x9d, 0xa7, 0x1b, 0x23, 0x78, 0xa8, 0x54, 0xf6,
|
||||
0x70, 0x50, 0x75, 0x6d, 0xed, 0x16, 0x5b, 0xac
|
||||
};
|
||||
|
||||
cipher_test_case_t aes_icm_test_case_1 = {
|
||||
46, /* octets in key */
|
||||
aes_icm_test_case_1_key, /* key */
|
||||
aes_icm_test_case_1_nonce, /* packet index */
|
||||
32, /* octets in plaintext */
|
||||
aes_icm_test_case_1_plaintext, /* plaintext */
|
||||
32, /* octets in ciphertext */
|
||||
aes_icm_test_case_1_ciphertext, /* ciphertext */
|
||||
0,
|
||||
NULL,
|
||||
0,
|
||||
&aes_icm_test_case_0 /* pointer to next testcase */
|
||||
static const srtp_cipher_test_case_t srtp_aes_icm_256_test_case_0 = {
|
||||
SRTP_AES_ICM_256_KEY_LEN_WSALT, /* octets in key */
|
||||
srtp_aes_icm_256_test_case_0_key, /* key */
|
||||
srtp_aes_icm_256_test_case_0_nonce, /* packet index */
|
||||
32, /* octets in plaintext */
|
||||
srtp_aes_icm_256_test_case_0_plaintext, /* plaintext */
|
||||
32, /* octets in ciphertext */
|
||||
srtp_aes_icm_256_test_case_0_ciphertext, /* ciphertext */
|
||||
0,
|
||||
NULL,
|
||||
0,
|
||||
NULL, /* pointer to next testcase */
|
||||
};
|
||||
|
||||
|
||||
@@ -575,19 +492,30 @@ cipher_test_case_t aes_icm_test_case_1 = {
|
||||
* note: the encrypt function is identical to the decrypt function
|
||||
*/
|
||||
|
||||
cipher_type_t aes_icm = {
|
||||
(cipher_alloc_func_t) aes_icm_alloc,
|
||||
(cipher_dealloc_func_t) aes_icm_dealloc,
|
||||
(cipher_init_func_t) aes_icm_context_init,
|
||||
(cipher_set_aad_func_t) 0,
|
||||
(cipher_encrypt_func_t) aes_icm_encrypt,
|
||||
(cipher_decrypt_func_t) aes_icm_encrypt,
|
||||
(cipher_set_iv_func_t) aes_icm_set_iv,
|
||||
(cipher_get_tag_func_t) 0,
|
||||
(char *) aes_icm_description,
|
||||
(int) 0, /* instance count */
|
||||
(cipher_test_case_t *) &aes_icm_test_case_1,
|
||||
(debug_module_t *) &mod_aes_icm,
|
||||
(cipher_type_id_t) AES_ICM
|
||||
const srtp_cipher_type_t srtp_aes_icm_128 = {
|
||||
srtp_aes_icm_alloc,
|
||||
srtp_aes_icm_dealloc,
|
||||
srtp_aes_icm_context_init,
|
||||
0, /* set_aad */
|
||||
srtp_aes_icm_encrypt,
|
||||
srtp_aes_icm_encrypt,
|
||||
srtp_aes_icm_set_iv,
|
||||
0, /* get_tag */
|
||||
srtp_aes_icm_128_description,
|
||||
&srtp_aes_icm_128_test_case_0,
|
||||
SRTP_AES_ICM_128
|
||||
};
|
||||
|
||||
const srtp_cipher_type_t srtp_aes_icm_256 = {
|
||||
srtp_aes_icm_alloc,
|
||||
srtp_aes_icm_dealloc,
|
||||
srtp_aes_icm_context_init,
|
||||
0, /* set_aad */
|
||||
srtp_aes_icm_encrypt,
|
||||
srtp_aes_icm_encrypt,
|
||||
srtp_aes_icm_set_iv,
|
||||
0, /* get_tag */
|
||||
srtp_aes_icm_256_description,
|
||||
&srtp_aes_icm_256_test_case_0,
|
||||
SRTP_AES_ICM_256
|
||||
};
|
||||
@@ -14,7 +14,7 @@
|
||||
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2013, Cisco Systems, Inc.
|
||||
* Copyright (c) 2013-2017, Cisco Systems, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -48,21 +48,24 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#include <openssl/evp.h>
|
||||
#include "aes_icm_ossl.h"
|
||||
#include "crypto_types.h"
|
||||
#include "err.h" /* for srtp_debug */
|
||||
#include "alloc.h"
|
||||
#include "crypto_types.h"
|
||||
|
||||
|
||||
debug_module_t mod_aes_icm = {
|
||||
srtp_debug_module_t srtp_mod_aes_icm = {
|
||||
0, /* debugging is off by default */
|
||||
"aes icm ossl" /* printable module name */
|
||||
};
|
||||
extern cipher_test_case_t aes_icm_test_case_0;
|
||||
extern cipher_type_t aes_icm;
|
||||
extern cipher_type_t aes_icm_192;
|
||||
extern cipher_type_t aes_icm_256;
|
||||
extern const srtp_cipher_type_t srtp_aes_icm_128;
|
||||
extern const srtp_cipher_type_t srtp_aes_icm_192;
|
||||
extern const srtp_cipher_type_t srtp_aes_icm_256;
|
||||
|
||||
/*
|
||||
* integer counter mode works as follows:
|
||||
@@ -70,7 +73,7 @@ extern cipher_type_t aes_icm_256;
|
||||
* 16 bits
|
||||
* <----->
|
||||
* +------+------+------+------+------+------+------+------+
|
||||
* | nonce | pakcet index | ctr |---+
|
||||
* | nonce | packet index | ctr |---+
|
||||
* +------+------+------+------+------+------+------+------+ |
|
||||
* |
|
||||
* +------+------+------+------+------+------+------+------+ v
|
||||
@@ -106,106 +109,98 @@ extern cipher_type_t aes_icm_256;
|
||||
* value. The tlen argument is for the AEAD tag length, which
|
||||
* isn't used in counter mode.
|
||||
*/
|
||||
err_status_t aes_icm_openssl_alloc (cipher_t **c, int key_len, int tlen)
|
||||
static srtp_err_status_t srtp_aes_icm_openssl_alloc (srtp_cipher_t **c, int key_len, int tlen)
|
||||
{
|
||||
aes_icm_ctx_t *icm;
|
||||
int tmp;
|
||||
uint8_t *allptr;
|
||||
srtp_aes_icm_ctx_t *icm;
|
||||
|
||||
debug_print(mod_aes_icm, "allocating cipher with key length %d", key_len);
|
||||
debug_print(srtp_mod_aes_icm, "allocating cipher with key length %d", key_len);
|
||||
|
||||
/*
|
||||
* Verify the key_len is valid for one of: AES-128/192/256
|
||||
*/
|
||||
if (key_len != AES_128_KEYSIZE_WSALT && key_len != AES_192_KEYSIZE_WSALT &&
|
||||
key_len != AES_256_KEYSIZE_WSALT) {
|
||||
return err_status_bad_param;
|
||||
if (key_len != SRTP_AES_ICM_128_KEY_LEN_WSALT && key_len != SRTP_AES_ICM_192_KEY_LEN_WSALT &&
|
||||
key_len != SRTP_AES_ICM_256_KEY_LEN_WSALT) {
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
/* allocate memory a cipher of type aes_icm */
|
||||
tmp = sizeof(cipher_t) + sizeof(aes_icm_ctx_t);
|
||||
allptr = (uint8_t*)crypto_alloc(tmp);
|
||||
if (allptr == NULL) {
|
||||
return err_status_alloc_fail;
|
||||
*c = (srtp_cipher_t *)srtp_crypto_alloc(sizeof(srtp_cipher_t));
|
||||
if (*c == NULL) {
|
||||
return srtp_err_status_alloc_fail;
|
||||
}
|
||||
memset(*c, 0x0, sizeof(srtp_cipher_t));
|
||||
|
||||
icm = (srtp_aes_icm_ctx_t *)srtp_crypto_alloc(sizeof(srtp_aes_icm_ctx_t));
|
||||
if (icm == NULL) {
|
||||
srtp_crypto_free(*c);
|
||||
*c = NULL;
|
||||
return srtp_err_status_alloc_fail;
|
||||
}
|
||||
memset(icm, 0x0, sizeof(srtp_aes_icm_ctx_t));
|
||||
|
||||
icm->ctx = EVP_CIPHER_CTX_new();
|
||||
if (icm->ctx == NULL) {
|
||||
srtp_crypto_free(icm);
|
||||
srtp_crypto_free(*c);
|
||||
*c = NULL;
|
||||
return srtp_err_status_alloc_fail;
|
||||
}
|
||||
|
||||
/* set pointers */
|
||||
*c = (cipher_t*)allptr;
|
||||
(*c)->state = allptr + sizeof(cipher_t);
|
||||
icm = (aes_icm_ctx_t*)(*c)->state;
|
||||
(*c)->state = icm;
|
||||
|
||||
/* increment ref_count */
|
||||
/* setup cipher parameters */
|
||||
switch (key_len) {
|
||||
case AES_128_KEYSIZE_WSALT:
|
||||
(*c)->algorithm = AES_128_ICM;
|
||||
(*c)->type = &aes_icm;
|
||||
aes_icm.ref_count++;
|
||||
((aes_icm_ctx_t*)(*c)->state)->key_size = AES_128_KEYSIZE;
|
||||
case SRTP_AES_ICM_128_KEY_LEN_WSALT:
|
||||
(*c)->algorithm = SRTP_AES_ICM_128;
|
||||
(*c)->type = &srtp_aes_icm_128;
|
||||
icm->key_size = SRTP_AES_128_KEY_LEN;
|
||||
break;
|
||||
case AES_192_KEYSIZE_WSALT:
|
||||
(*c)->algorithm = AES_192_ICM;
|
||||
(*c)->type = &aes_icm_192;
|
||||
aes_icm_192.ref_count++;
|
||||
((aes_icm_ctx_t*)(*c)->state)->key_size = AES_192_KEYSIZE;
|
||||
case SRTP_AES_ICM_192_KEY_LEN_WSALT:
|
||||
(*c)->algorithm = SRTP_AES_ICM_192;
|
||||
(*c)->type = &srtp_aes_icm_192;
|
||||
icm->key_size = SRTP_AES_192_KEY_LEN;
|
||||
break;
|
||||
case AES_256_KEYSIZE_WSALT:
|
||||
(*c)->algorithm = AES_256_ICM;
|
||||
(*c)->type = &aes_icm_256;
|
||||
aes_icm_256.ref_count++;
|
||||
((aes_icm_ctx_t*)(*c)->state)->key_size = AES_256_KEYSIZE;
|
||||
case SRTP_AES_ICM_256_KEY_LEN_WSALT:
|
||||
(*c)->algorithm = SRTP_AES_ICM_256;
|
||||
(*c)->type = &srtp_aes_icm_256;
|
||||
icm->key_size = SRTP_AES_256_KEY_LEN;
|
||||
break;
|
||||
}
|
||||
|
||||
/* set key size */
|
||||
(*c)->key_len = key_len;
|
||||
EVP_CIPHER_CTX_init(&icm->ctx);
|
||||
|
||||
return err_status_ok;
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* This function deallocates an instance of this engine
|
||||
*/
|
||||
err_status_t aes_icm_openssl_dealloc (cipher_t *c)
|
||||
static srtp_err_status_t srtp_aes_icm_openssl_dealloc (srtp_cipher_t *c)
|
||||
{
|
||||
aes_icm_ctx_t *ctx;
|
||||
srtp_aes_icm_ctx_t *ctx;
|
||||
|
||||
if (c == NULL) {
|
||||
return err_status_bad_param;
|
||||
return srtp_err_status_bad_param;
|
||||
}
|
||||
|
||||
/*
|
||||
* Free the EVP context
|
||||
*/
|
||||
ctx = (aes_icm_ctx_t*)c->state;
|
||||
ctx = (srtp_aes_icm_ctx_t*)c->state;
|
||||
if (ctx != NULL) {
|
||||
EVP_CIPHER_CTX_cleanup(&ctx->ctx);
|
||||
/* decrement ref_count for the appropriate engine */
|
||||
switch (ctx->key_size) {
|
||||
case AES_256_KEYSIZE:
|
||||
aes_icm_256.ref_count--;
|
||||
break;
|
||||
case AES_192_KEYSIZE:
|
||||
aes_icm_192.ref_count--;
|
||||
break;
|
||||
case AES_128_KEYSIZE:
|
||||
aes_icm.ref_count--;
|
||||
break;
|
||||
default:
|
||||
return err_status_dealloc_fail;
|
||||
break;
|
||||
}
|
||||
EVP_CIPHER_CTX_free(ctx->ctx);
|
||||
/* zeroize the key material */
|
||||
octet_string_set_to_zero(ctx, sizeof(srtp_aes_icm_ctx_t));
|
||||
srtp_crypto_free(ctx);
|
||||
}
|
||||
|
||||
/* zeroize entire state*/
|
||||
octet_string_set_to_zero((uint8_t*)c,
|
||||
sizeof(cipher_t) + sizeof(aes_icm_ctx_t));
|
||||
|
||||
/* free memory */
|
||||
crypto_free(c);
|
||||
srtp_crypto_free(c);
|
||||
|
||||
return err_status_ok;
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -217,42 +212,50 @@ err_status_t aes_icm_openssl_dealloc (cipher_t *c)
|
||||
* the salt is unpredictable (but not necessarily secret) data which
|
||||
* randomizes the starting point in the keystream
|
||||
*/
|
||||
err_status_t aes_icm_openssl_context_init (aes_icm_ctx_t *c, const uint8_t *key)
|
||||
static srtp_err_status_t srtp_aes_icm_openssl_context_init (void* cv, const uint8_t *key)
|
||||
{
|
||||
srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
|
||||
const EVP_CIPHER *evp;
|
||||
|
||||
/*
|
||||
* set counter and initial values to 'offset' value, being careful not to
|
||||
* go past the end of the key buffer
|
||||
*/
|
||||
v128_set_to_zero(&c->counter);
|
||||
v128_set_to_zero(&c->offset);
|
||||
memcpy(&c->counter, key + c->key_size, SALT_SIZE);
|
||||
memcpy(&c->offset, key + c->key_size, SALT_SIZE);
|
||||
memcpy(&c->counter, key + c->key_size, SRTP_SALT_LEN);
|
||||
memcpy(&c->offset, key + c->key_size, SRTP_SALT_LEN);
|
||||
|
||||
/* force last two octets of the offset to zero (for srtp compatibility) */
|
||||
c->offset.v8[SALT_SIZE] = c->offset.v8[SALT_SIZE + 1] = 0;
|
||||
c->counter.v8[SALT_SIZE] = c->counter.v8[SALT_SIZE + 1] = 0;
|
||||
c->offset.v8[SRTP_SALT_LEN] = c->offset.v8[SRTP_SALT_LEN + 1] = 0;
|
||||
c->counter.v8[SRTP_SALT_LEN] = c->counter.v8[SRTP_SALT_LEN + 1] = 0;
|
||||
|
||||
/* copy key to be used later when CiscoSSL crypto context is created */
|
||||
v128_copy_octet_string((v128_t*)&c->key, key);
|
||||
debug_print(srtp_mod_aes_icm, "key: %s", srtp_octet_string_hex_string(key, c->key_size));
|
||||
debug_print(srtp_mod_aes_icm, "offset: %s", v128_hex_string(&c->offset));
|
||||
|
||||
/* if the key is greater than 16 bytes, copy the second
|
||||
* half. Note, we treat AES-192 and AES-256 the same here
|
||||
* for simplicity. The storage location receiving the
|
||||
* key is statically allocated to handle a full 32 byte key
|
||||
* regardless of the cipher in use.
|
||||
*/
|
||||
if (c->key_size == AES_256_KEYSIZE || c->key_size == AES_192_KEYSIZE) {
|
||||
debug_print(mod_aes_icm, "Copying last 16 bytes of key: %s",
|
||||
v128_hex_string((v128_t*)(key + AES_128_KEYSIZE)));
|
||||
v128_copy_octet_string(((v128_t*)(&c->key.v8)) + 1, key + AES_128_KEYSIZE);
|
||||
switch (c->key_size) {
|
||||
case SRTP_AES_256_KEY_LEN:
|
||||
evp = EVP_aes_256_ctr();
|
||||
break;
|
||||
case SRTP_AES_192_KEY_LEN:
|
||||
evp = EVP_aes_192_ctr();
|
||||
break;
|
||||
case SRTP_AES_128_KEY_LEN:
|
||||
evp = EVP_aes_128_ctr();
|
||||
break;
|
||||
default:
|
||||
return srtp_err_status_bad_param;
|
||||
break;
|
||||
}
|
||||
|
||||
debug_print(mod_aes_icm, "key: %s", v128_hex_string((v128_t*)&c->key));
|
||||
debug_print(mod_aes_icm, "offset: %s", v128_hex_string(&c->offset));
|
||||
if (!EVP_EncryptInit_ex(c->ctx, evp,
|
||||
NULL, key, NULL)) {
|
||||
return srtp_err_status_fail;
|
||||
} else {
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
EVP_CIPHER_CTX_cleanup(&c->ctx);
|
||||
|
||||
return err_status_ok;
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
|
||||
@@ -260,40 +263,25 @@ err_status_t aes_icm_openssl_context_init (aes_icm_ctx_t *c, const uint8_t *key)
|
||||
* aes_icm_set_iv(c, iv) sets the counter value to the exor of iv with
|
||||
* the offset
|
||||
*/
|
||||
err_status_t aes_icm_openssl_set_iv (aes_icm_ctx_t *c, void *iv, int dir)
|
||||
static srtp_err_status_t srtp_aes_icm_openssl_set_iv (void *cv, uint8_t *iv, srtp_cipher_direction_t dir)
|
||||
{
|
||||
const EVP_CIPHER *evp;
|
||||
srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
|
||||
v128_t nonce;
|
||||
|
||||
/* set nonce (for alignment) */
|
||||
v128_copy_octet_string(&nonce, iv);
|
||||
|
||||
debug_print(mod_aes_icm, "setting iv: %s", v128_hex_string(&nonce));
|
||||
debug_print(srtp_mod_aes_icm, "setting iv: %s", v128_hex_string(&nonce));
|
||||
|
||||
v128_xor(&c->counter, &c->offset, &nonce);
|
||||
|
||||
debug_print(mod_aes_icm, "set_counter: %s", v128_hex_string(&c->counter));
|
||||
debug_print(srtp_mod_aes_icm, "set_counter: %s", v128_hex_string(&c->counter));
|
||||
|
||||
switch (c->key_size) {
|
||||
case AES_256_KEYSIZE:
|
||||
evp = EVP_aes_256_ctr();
|
||||
break;
|
||||
case AES_192_KEYSIZE:
|
||||
evp = EVP_aes_192_ctr();
|
||||
break;
|
||||
case AES_128_KEYSIZE:
|
||||
evp = EVP_aes_128_ctr();
|
||||
break;
|
||||
default:
|
||||
return err_status_bad_param;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!EVP_EncryptInit_ex(&c->ctx, evp,
|
||||
NULL, c->key.v8, c->counter.v8)) {
|
||||
return err_status_fail;
|
||||
if (!EVP_EncryptInit_ex(c->ctx, NULL,
|
||||
NULL, NULL, c->counter.v8)) {
|
||||
return srtp_err_status_fail;
|
||||
} else {
|
||||
return err_status_ok;
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -305,85 +293,72 @@ err_status_t aes_icm_openssl_set_iv (aes_icm_ctx_t *c, void *iv, int dir)
|
||||
* buf data to encrypt
|
||||
* enc_len length of encrypt buffer
|
||||
*/
|
||||
err_status_t aes_icm_openssl_encrypt (aes_icm_ctx_t *c, unsigned char *buf, unsigned int *enc_len)
|
||||
static srtp_err_status_t srtp_aes_icm_openssl_encrypt (void *cv, unsigned char *buf, unsigned int *enc_len)
|
||||
{
|
||||
srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
|
||||
int len = 0;
|
||||
|
||||
debug_print(mod_aes_icm, "rs0: %s", v128_hex_string(&c->counter));
|
||||
debug_print(srtp_mod_aes_icm, "rs0: %s", v128_hex_string(&c->counter));
|
||||
|
||||
if (!EVP_EncryptUpdate(&c->ctx, buf, &len, buf, *enc_len)) {
|
||||
return err_status_cipher_fail;
|
||||
if (!EVP_EncryptUpdate(c->ctx, buf, &len, buf, *enc_len)) {
|
||||
return srtp_err_status_cipher_fail;
|
||||
}
|
||||
*enc_len = len;
|
||||
|
||||
if (!EVP_EncryptFinal_ex(&c->ctx, buf, (int*)&len)) {
|
||||
return err_status_cipher_fail;
|
||||
if (!EVP_EncryptFinal_ex(c->ctx, buf, &len)) {
|
||||
return srtp_err_status_cipher_fail;
|
||||
}
|
||||
*enc_len += len;
|
||||
|
||||
return err_status_ok;
|
||||
}
|
||||
|
||||
/*
|
||||
* Abstraction layer for encrypt.
|
||||
*/
|
||||
err_status_t aes_icm_output (aes_icm_ctx_t *c, uint8_t *buffer, int num_octets_to_output)
|
||||
{
|
||||
unsigned int len = num_octets_to_output;
|
||||
|
||||
/* zeroize the buffer */
|
||||
octet_string_set_to_zero(buffer, num_octets_to_output);
|
||||
|
||||
/* exor keystream into buffer */
|
||||
return aes_icm_openssl_encrypt(c, buffer, &len);
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
/*
|
||||
* Name of this crypto engine
|
||||
*/
|
||||
char aes_icm_openssl_description[] = "AES-128 counter mode using openssl";
|
||||
char aes_icm_192_openssl_description[] = "AES-192 counter mode using openssl";
|
||||
char aes_icm_256_openssl_description[] = "AES-256 counter mode using openssl";
|
||||
static const char srtp_aes_icm_128_openssl_description[] = "AES-128 counter mode using openssl";
|
||||
static const char srtp_aes_icm_192_openssl_description[] = "AES-192 counter mode using openssl";
|
||||
static const char srtp_aes_icm_256_openssl_description[] = "AES-256 counter mode using openssl";
|
||||
|
||||
|
||||
/*
|
||||
* KAT values for AES self-test. These
|
||||
* values came from the legacy libsrtp code.
|
||||
*/
|
||||
uint8_t aes_icm_test_case_0_key[AES_128_KEYSIZE_WSALT] = {
|
||||
static const uint8_t srtp_aes_icm_128_test_case_0_key[SRTP_AES_ICM_128_KEY_LEN_WSALT] = {
|
||||
0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
|
||||
0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c,
|
||||
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
|
||||
0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd
|
||||
};
|
||||
|
||||
uint8_t aes_icm_test_case_0_nonce[16] = {
|
||||
static uint8_t srtp_aes_icm_128_test_case_0_nonce[16] = {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||
};
|
||||
|
||||
uint8_t aes_icm_test_case_0_plaintext[32] = {
|
||||
static const uint8_t srtp_aes_icm_128_test_case_0_plaintext[32] = {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
};
|
||||
|
||||
uint8_t aes_icm_test_case_0_ciphertext[32] = {
|
||||
static const uint8_t srtp_aes_icm_128_test_case_0_ciphertext[32] = {
|
||||
0xe0, 0x3e, 0xad, 0x09, 0x35, 0xc9, 0x5e, 0x80,
|
||||
0xe1, 0x66, 0xb1, 0x6d, 0xd9, 0x2b, 0x4e, 0xb4,
|
||||
0xd2, 0x35, 0x13, 0x16, 0x2b, 0x02, 0xd0, 0xf7,
|
||||
0x2a, 0x43, 0xa2, 0xfe, 0x4a, 0x5f, 0x97, 0xab
|
||||
};
|
||||
|
||||
cipher_test_case_t aes_icm_test_case_0 = {
|
||||
AES_128_KEYSIZE_WSALT, /* octets in key */
|
||||
aes_icm_test_case_0_key, /* key */
|
||||
aes_icm_test_case_0_nonce, /* packet index */
|
||||
static const srtp_cipher_test_case_t srtp_aes_icm_128_test_case_0 = {
|
||||
SRTP_AES_ICM_128_KEY_LEN_WSALT, /* octets in key */
|
||||
srtp_aes_icm_128_test_case_0_key, /* key */
|
||||
srtp_aes_icm_128_test_case_0_nonce, /* packet index */
|
||||
32, /* octets in plaintext */
|
||||
aes_icm_test_case_0_plaintext, /* plaintext */
|
||||
srtp_aes_icm_128_test_case_0_plaintext, /* plaintext */
|
||||
32, /* octets in ciphertext */
|
||||
aes_icm_test_case_0_ciphertext, /* ciphertext */
|
||||
srtp_aes_icm_128_test_case_0_ciphertext, /* ciphertext */
|
||||
0,
|
||||
NULL,
|
||||
0,
|
||||
@@ -394,7 +369,7 @@ cipher_test_case_t aes_icm_test_case_0 = {
|
||||
* KAT values for AES-192-CTR self-test. These
|
||||
* values came from section 7 of RFC 6188.
|
||||
*/
|
||||
uint8_t aes_icm_192_test_case_1_key[AES_192_KEYSIZE_WSALT] = {
|
||||
static const uint8_t srtp_aes_icm_192_test_case_0_key[SRTP_AES_ICM_192_KEY_LEN_WSALT] = {
|
||||
0xea, 0xb2, 0x34, 0x76, 0x4e, 0x51, 0x7b, 0x2d,
|
||||
0x3d, 0x16, 0x0d, 0x58, 0x7d, 0x8c, 0x86, 0x21,
|
||||
0x97, 0x40, 0xf6, 0x5f, 0x99, 0xb6, 0xbc, 0xf7,
|
||||
@@ -402,45 +377,44 @@ uint8_t aes_icm_192_test_case_1_key[AES_192_KEYSIZE_WSALT] = {
|
||||
0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd
|
||||
};
|
||||
|
||||
uint8_t aes_icm_192_test_case_1_nonce[16] = {
|
||||
static uint8_t srtp_aes_icm_192_test_case_0_nonce[16] = {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||
};
|
||||
|
||||
uint8_t aes_icm_192_test_case_1_plaintext[32] = {
|
||||
static const uint8_t srtp_aes_icm_192_test_case_0_plaintext[32] = {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
};
|
||||
|
||||
uint8_t aes_icm_192_test_case_1_ciphertext[32] = {
|
||||
static const uint8_t srtp_aes_icm_192_test_case_0_ciphertext[32] = {
|
||||
0x35, 0x09, 0x6c, 0xba, 0x46, 0x10, 0x02, 0x8d,
|
||||
0xc1, 0xb5, 0x75, 0x03, 0x80, 0x4c, 0xe3, 0x7c,
|
||||
0x5d, 0xe9, 0x86, 0x29, 0x1d, 0xcc, 0xe1, 0x61,
|
||||
0xd5, 0x16, 0x5e, 0xc4, 0x56, 0x8f, 0x5c, 0x9a
|
||||
};
|
||||
|
||||
cipher_test_case_t aes_icm_192_test_case_1 = {
|
||||
AES_192_KEYSIZE_WSALT, /* octets in key */
|
||||
aes_icm_192_test_case_1_key, /* key */
|
||||
aes_icm_192_test_case_1_nonce, /* packet index */
|
||||
static const srtp_cipher_test_case_t srtp_aes_icm_192_test_case_0 = {
|
||||
SRTP_AES_ICM_192_KEY_LEN_WSALT, /* octets in key */
|
||||
srtp_aes_icm_192_test_case_0_key, /* key */
|
||||
srtp_aes_icm_192_test_case_0_nonce, /* packet index */
|
||||
32, /* octets in plaintext */
|
||||
aes_icm_192_test_case_1_plaintext, /* plaintext */
|
||||
srtp_aes_icm_192_test_case_0_plaintext, /* plaintext */
|
||||
32, /* octets in ciphertext */
|
||||
aes_icm_192_test_case_1_ciphertext, /* ciphertext */
|
||||
srtp_aes_icm_192_test_case_0_ciphertext, /* ciphertext */
|
||||
0,
|
||||
NULL,
|
||||
0,
|
||||
NULL /* pointer to next testcase */
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
* KAT values for AES-256-CTR self-test. These
|
||||
* values came from section 7 of RFC 6188.
|
||||
*/
|
||||
uint8_t aes_icm_256_test_case_2_key[AES_256_KEYSIZE_WSALT] = {
|
||||
static const uint8_t srtp_aes_icm_256_test_case_0_key[SRTP_AES_ICM_256_KEY_LEN_WSALT] = {
|
||||
0x57, 0xf8, 0x2f, 0xe3, 0x61, 0x3f, 0xd1, 0x70,
|
||||
0xa8, 0x5e, 0xc9, 0x3c, 0x40, 0xb1, 0xf0, 0x92,
|
||||
0x2e, 0xc4, 0xcb, 0x0d, 0xc0, 0x25, 0xb5, 0x82,
|
||||
@@ -449,33 +423,33 @@ uint8_t aes_icm_256_test_case_2_key[AES_256_KEYSIZE_WSALT] = {
|
||||
0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd
|
||||
};
|
||||
|
||||
uint8_t aes_icm_256_test_case_2_nonce[16] = {
|
||||
static uint8_t srtp_aes_icm_256_test_case_0_nonce[16] = {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||
};
|
||||
|
||||
uint8_t aes_icm_256_test_case_2_plaintext[32] = {
|
||||
static const uint8_t srtp_aes_icm_256_test_case_0_plaintext[32] = {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
};
|
||||
|
||||
uint8_t aes_icm_256_test_case_2_ciphertext[32] = {
|
||||
static const uint8_t srtp_aes_icm_256_test_case_0_ciphertext[32] = {
|
||||
0x92, 0xbd, 0xd2, 0x8a, 0x93, 0xc3, 0xf5, 0x25,
|
||||
0x11, 0xc6, 0x77, 0xd0, 0x8b, 0x55, 0x15, 0xa4,
|
||||
0x9d, 0xa7, 0x1b, 0x23, 0x78, 0xa8, 0x54, 0xf6,
|
||||
0x70, 0x50, 0x75, 0x6d, 0xed, 0x16, 0x5b, 0xac
|
||||
};
|
||||
|
||||
cipher_test_case_t aes_icm_256_test_case_2 = {
|
||||
AES_256_KEYSIZE_WSALT, /* octets in key */
|
||||
aes_icm_256_test_case_2_key, /* key */
|
||||
aes_icm_256_test_case_2_nonce, /* packet index */
|
||||
static const srtp_cipher_test_case_t srtp_aes_icm_256_test_case_0 = {
|
||||
SRTP_AES_ICM_256_KEY_LEN_WSALT, /* octets in key */
|
||||
srtp_aes_icm_256_test_case_0_key, /* key */
|
||||
srtp_aes_icm_256_test_case_0_nonce, /* packet index */
|
||||
32, /* octets in plaintext */
|
||||
aes_icm_256_test_case_2_plaintext, /* plaintext */
|
||||
srtp_aes_icm_256_test_case_0_plaintext, /* plaintext */
|
||||
32, /* octets in ciphertext */
|
||||
aes_icm_256_test_case_2_ciphertext, /* ciphertext */
|
||||
srtp_aes_icm_256_test_case_0_ciphertext, /* ciphertext */
|
||||
0,
|
||||
NULL,
|
||||
0,
|
||||
@@ -486,59 +460,53 @@ cipher_test_case_t aes_icm_256_test_case_2 = {
|
||||
* This is the function table for this crypto engine.
|
||||
* note: the encrypt function is identical to the decrypt function
|
||||
*/
|
||||
cipher_type_t aes_icm = {
|
||||
(cipher_alloc_func_t) aes_icm_openssl_alloc,
|
||||
(cipher_dealloc_func_t) aes_icm_openssl_dealloc,
|
||||
(cipher_init_func_t) aes_icm_openssl_context_init,
|
||||
(cipher_set_aad_func_t) 0,
|
||||
(cipher_encrypt_func_t) aes_icm_openssl_encrypt,
|
||||
(cipher_decrypt_func_t) aes_icm_openssl_encrypt,
|
||||
(cipher_set_iv_func_t) aes_icm_openssl_set_iv,
|
||||
(cipher_get_tag_func_t) 0,
|
||||
(char*) aes_icm_openssl_description,
|
||||
(int) 0, /* instance count */
|
||||
(cipher_test_case_t*) &aes_icm_test_case_0,
|
||||
(debug_module_t*) &mod_aes_icm,
|
||||
(cipher_type_id_t) AES_ICM
|
||||
const srtp_cipher_type_t srtp_aes_icm_128 = {
|
||||
srtp_aes_icm_openssl_alloc,
|
||||
srtp_aes_icm_openssl_dealloc,
|
||||
srtp_aes_icm_openssl_context_init,
|
||||
0, /* set_aad */
|
||||
srtp_aes_icm_openssl_encrypt,
|
||||
srtp_aes_icm_openssl_encrypt,
|
||||
srtp_aes_icm_openssl_set_iv,
|
||||
0, /* get_tag */
|
||||
srtp_aes_icm_128_openssl_description,
|
||||
&srtp_aes_icm_128_test_case_0,
|
||||
SRTP_AES_ICM_128
|
||||
};
|
||||
|
||||
/*
|
||||
* This is the function table for this crypto engine.
|
||||
* note: the encrypt function is identical to the decrypt function
|
||||
*/
|
||||
cipher_type_t aes_icm_192 = {
|
||||
(cipher_alloc_func_t) aes_icm_openssl_alloc,
|
||||
(cipher_dealloc_func_t) aes_icm_openssl_dealloc,
|
||||
(cipher_init_func_t) aes_icm_openssl_context_init,
|
||||
(cipher_set_aad_func_t) 0,
|
||||
(cipher_encrypt_func_t) aes_icm_openssl_encrypt,
|
||||
(cipher_decrypt_func_t) aes_icm_openssl_encrypt,
|
||||
(cipher_set_iv_func_t) aes_icm_openssl_set_iv,
|
||||
(cipher_get_tag_func_t) 0,
|
||||
(char*) aes_icm_192_openssl_description,
|
||||
(int) 0, /* instance count */
|
||||
(cipher_test_case_t*) &aes_icm_192_test_case_1,
|
||||
(debug_module_t*) &mod_aes_icm,
|
||||
(cipher_type_id_t) AES_192_ICM
|
||||
const srtp_cipher_type_t srtp_aes_icm_192 = {
|
||||
srtp_aes_icm_openssl_alloc,
|
||||
srtp_aes_icm_openssl_dealloc,
|
||||
srtp_aes_icm_openssl_context_init,
|
||||
0, /* set_aad */
|
||||
srtp_aes_icm_openssl_encrypt,
|
||||
srtp_aes_icm_openssl_encrypt,
|
||||
srtp_aes_icm_openssl_set_iv,
|
||||
0, /* get_tag */
|
||||
srtp_aes_icm_192_openssl_description,
|
||||
&srtp_aes_icm_192_test_case_0,
|
||||
SRTP_AES_ICM_192
|
||||
};
|
||||
|
||||
/*
|
||||
* This is the function table for this crypto engine.
|
||||
* note: the encrypt function is identical to the decrypt function
|
||||
*/
|
||||
cipher_type_t aes_icm_256 = {
|
||||
(cipher_alloc_func_t) aes_icm_openssl_alloc,
|
||||
(cipher_dealloc_func_t) aes_icm_openssl_dealloc,
|
||||
(cipher_init_func_t) aes_icm_openssl_context_init,
|
||||
(cipher_set_aad_func_t) 0,
|
||||
(cipher_encrypt_func_t) aes_icm_openssl_encrypt,
|
||||
(cipher_decrypt_func_t) aes_icm_openssl_encrypt,
|
||||
(cipher_set_iv_func_t) aes_icm_openssl_set_iv,
|
||||
(cipher_get_tag_func_t) 0,
|
||||
(char*) aes_icm_256_openssl_description,
|
||||
(int) 0, /* instance count */
|
||||
(cipher_test_case_t*) &aes_icm_256_test_case_2,
|
||||
(debug_module_t*) &mod_aes_icm,
|
||||
(cipher_type_id_t) AES_256_ICM
|
||||
const srtp_cipher_type_t srtp_aes_icm_256 = {
|
||||
srtp_aes_icm_openssl_alloc,
|
||||
srtp_aes_icm_openssl_dealloc,
|
||||
srtp_aes_icm_openssl_context_init,
|
||||
0, /* set_aad */
|
||||
srtp_aes_icm_openssl_encrypt,
|
||||
srtp_aes_icm_openssl_encrypt,
|
||||
srtp_aes_icm_openssl_set_iv,
|
||||
0, /* get_tag */
|
||||
srtp_aes_icm_256_openssl_description,
|
||||
&srtp_aes_icm_256_test_case_0,
|
||||
SRTP_AES_ICM_256
|
||||
};
|
||||
|
||||
+551
-410
File diff suppressed because it is too large.
Load diff
@@ -9,26 +9,26 @@
|
||||
*/
|
||||
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2001-2006,2013 Cisco Systems, Inc.
|
||||
*
|
||||
* Copyright (c) 2001-2017 Cisco Systems, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
*
|
||||
* Redistributions in binary form must reproduce the above
|
||||
* copyright notice, this list of conditions and the following
|
||||
* disclaimer in the documentation and/or other materials provided
|
||||
* with the distribution.
|
||||
*
|
||||
*
|
||||
* Neither the name of the Cisco Systems, Inc. nor the names of its
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
@@ -44,96 +44,95 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#include "datatypes.h"
|
||||
#include "null_cipher.h"
|
||||
#include "err.h" /* for srtp_debug */
|
||||
#include "alloc.h"
|
||||
|
||||
/* the null_cipher uses the cipher debug module */
|
||||
|
||||
extern debug_module_t mod_cipher;
|
||||
extern srtp_debug_module_t srtp_mod_cipher;
|
||||
|
||||
err_status_t
|
||||
null_cipher_alloc(cipher_t **c, int key_len, int tlen) {
|
||||
extern cipher_type_t null_cipher;
|
||||
uint8_t *pointer;
|
||||
|
||||
debug_print(mod_cipher,
|
||||
"allocating cipher with key length %d", key_len);
|
||||
static srtp_err_status_t srtp_null_cipher_alloc (srtp_cipher_t **c, int key_len, int tlen)
|
||||
{
|
||||
extern const srtp_cipher_type_t srtp_null_cipher;
|
||||
|
||||
/* allocate memory a cipher of type null_cipher */
|
||||
pointer = (uint8_t*)crypto_alloc(sizeof(null_cipher_ctx_t) + sizeof(cipher_t));
|
||||
if (pointer == NULL)
|
||||
return err_status_alloc_fail;
|
||||
debug_print(srtp_mod_cipher,
|
||||
"allocating cipher with key length %d", key_len);
|
||||
|
||||
/* set pointers */
|
||||
*c = (cipher_t *)pointer;
|
||||
(*c)->algorithm = NULL_CIPHER;
|
||||
(*c)->type = &null_cipher;
|
||||
(*c)->state = pointer + sizeof(cipher_t);
|
||||
/* allocate memory a cipher of type null_cipher */
|
||||
*c = (srtp_cipher_t *)srtp_crypto_alloc(sizeof(srtp_cipher_t));
|
||||
if (*c == NULL) {
|
||||
return srtp_err_status_alloc_fail;
|
||||
}
|
||||
memset(*c, 0x0, sizeof(srtp_cipher_t));
|
||||
|
||||
/* set key size */
|
||||
(*c)->key_len = key_len;
|
||||
/* set pointers */
|
||||
(*c)->algorithm = SRTP_NULL_CIPHER;
|
||||
(*c)->type = &srtp_null_cipher;
|
||||
(*c)->state = (void *) 0x1; /* The null cipher does not maintain state */
|
||||
|
||||
/* set key size */
|
||||
(*c)->key_len = key_len;
|
||||
|
||||
return srtp_err_status_ok;
|
||||
|
||||
/* increment ref_count */
|
||||
null_cipher.ref_count++;
|
||||
|
||||
return err_status_ok;
|
||||
|
||||
}
|
||||
|
||||
err_status_t
|
||||
null_cipher_dealloc(cipher_t *c) {
|
||||
extern cipher_type_t null_cipher;
|
||||
static srtp_err_status_t srtp_null_cipher_dealloc (srtp_cipher_t *c)
|
||||
{
|
||||
extern const srtp_cipher_type_t srtp_null_cipher;
|
||||
|
||||
/* zeroize entire state*/
|
||||
octet_string_set_to_zero((uint8_t *)c,
|
||||
sizeof(null_cipher_ctx_t) + sizeof(cipher_t));
|
||||
/* zeroize entire state*/
|
||||
octet_string_set_to_zero(c, sizeof(srtp_cipher_t));
|
||||
|
||||
/* free memory of type null_cipher */
|
||||
crypto_free(c);
|
||||
/* free memory of type null_cipher */
|
||||
srtp_crypto_free(c);
|
||||
|
||||
return srtp_err_status_ok;
|
||||
|
||||
/* decrement reference count */
|
||||
null_cipher.ref_count--;
|
||||
|
||||
return err_status_ok;
|
||||
|
||||
}
|
||||
|
||||
err_status_t
|
||||
null_cipher_init(null_cipher_ctx_t *ctx, const uint8_t *key, int key_len) {
|
||||
static srtp_err_status_t srtp_null_cipher_init (void *cv, const uint8_t *key)
|
||||
{
|
||||
/* srtp_null_cipher_ctx_t *c = (srtp_null_cipher_ctx_t *)cv; */
|
||||
|
||||
debug_print(mod_cipher, "initializing null cipher", NULL);
|
||||
debug_print(srtp_mod_cipher, "initializing null cipher", NULL);
|
||||
|
||||
return err_status_ok;
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
err_status_t
|
||||
null_cipher_set_iv(null_cipher_ctx_t *c, void *iv) {
|
||||
return err_status_ok;
|
||||
static srtp_err_status_t srtp_null_cipher_set_iv (void *cv, uint8_t *iv, srtp_cipher_direction_t dir)
|
||||
{
|
||||
/* srtp_null_cipher_ctx_t *c = (srtp_null_cipher_ctx_t *)cv; */
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
err_status_t
|
||||
null_cipher_encrypt(null_cipher_ctx_t *c,
|
||||
unsigned char *buf, unsigned int *bytes_to_encr) {
|
||||
return err_status_ok;
|
||||
static srtp_err_status_t srtp_null_cipher_encrypt (void *cv,
|
||||
unsigned char *buf, unsigned int *bytes_to_encr)
|
||||
{
|
||||
/* srtp_null_cipher_ctx_t *c = (srtp_null_cipher_ctx_t *)cv; */
|
||||
return srtp_err_status_ok;
|
||||
}
|
||||
|
||||
char
|
||||
null_cipher_description[] = "null cipher";
|
||||
static const char srtp_null_cipher_description[] = "null cipher";
|
||||
|
||||
cipher_test_case_t
|
||||
null_cipher_test_0 = {
|
||||
0, /* octets in key */
|
||||
NULL, /* key */
|
||||
0, /* packet index */
|
||||
0, /* octets in plaintext */
|
||||
NULL, /* plaintext */
|
||||
0, /* octets in plaintext */
|
||||
NULL, /* ciphertext */
|
||||
0,
|
||||
NULL,
|
||||
0,
|
||||
NULL /* pointer to next testcase */
|
||||
static const srtp_cipher_test_case_t srtp_null_cipher_test_0 = {
|
||||
0, /* octets in key */
|
||||
NULL, /* key */
|
||||
0, /* packet index */
|
||||
0, /* octets in plaintext */
|
||||
NULL, /* plaintext */
|
||||
0, /* octets in plaintext */
|
||||
NULL, /* ciphertext */
|
||||
0,
|
||||
NULL,
|
||||
0,
|
||||
NULL /* pointer to next testcase */
|
||||
};
|
||||
|
||||
|
||||
@@ -141,19 +140,17 @@ null_cipher_test_0 = {
|
||||
* note: the decrypt function is idential to the encrypt function
|
||||
*/
|
||||
|
||||
cipher_type_t null_cipher = {
|
||||
(cipher_alloc_func_t) null_cipher_alloc,
|
||||
(cipher_dealloc_func_t) null_cipher_dealloc,
|
||||
(cipher_init_func_t) null_cipher_init,
|
||||
(cipher_set_aad_func_t) 0,
|
||||
(cipher_encrypt_func_t) null_cipher_encrypt,
|
||||
(cipher_decrypt_func_t) null_cipher_encrypt,
|
||||
(cipher_set_iv_func_t) null_cipher_set_iv,
|
||||
(cipher_get_tag_func_t) 0,
|
||||
(char *) null_cipher_description,
|
||||
(int) 0,
|
||||
(cipher_test_case_t *) &null_cipher_test_0,
|
||||
(debug_module_t *) NULL,
|
||||
(cipher_type_id_t) NULL_CIPHER
|
||||
const srtp_cipher_type_t srtp_null_cipher = {
|
||||
srtp_null_cipher_alloc,
|
||||
srtp_null_cipher_dealloc,
|
||||
srtp_null_cipher_init,
|
||||
0, /* set_aad */
|
||||
srtp_null_cipher_encrypt,
|
||||
srtp_null_cipher_encrypt,
|
||||
srtp_null_cipher_set_iv,
|
||||
0, /* get_tag */
|
||||
srtp_null_cipher_description,
|
||||
&srtp_null_cipher_test_0,
|
||||
SRTP_NULL_CIPHER
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user