123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437 |
- #ifdef HAVE_CONFIG_H
- #include <config.h>
- #endif
- #define ALIGN_32 0
- #include "aes_icm.h"
- #include "alloc.h"
- #include "cipher_types.h"
- #include "cipher_test_cases.h"
- srtp_debug_module_t srtp_mod_aes_icm = {
- 0,
- "aes icm"
- };
- static srtp_err_status_t srtp_aes_icm_alloc(srtp_cipher_t **c,
- int key_len,
- int tlen)
- {
- srtp_aes_icm_ctx_t *icm;
- (void)tlen;
- debug_print(srtp_mod_aes_icm, "allocating cipher with key length %d",
- key_len);
-
- 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;
- }
-
- *c = (srtp_cipher_t *)srtp_crypto_alloc(sizeof(srtp_cipher_t));
- if (*c == NULL) {
- return srtp_err_status_alloc_fail;
- }
- 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;
- }
-
- (*c)->state = icm;
- 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;
- }
-
- icm->key_size = key_len;
- (*c)->key_len = key_len;
- return srtp_err_status_ok;
- }
- static srtp_err_status_t srtp_aes_icm_dealloc(srtp_cipher_t *c)
- {
- srtp_aes_icm_ctx_t *ctx;
- if (c == NULL) {
- return srtp_err_status_bad_param;
- }
- ctx = (srtp_aes_icm_ctx_t *)c->state;
- if (ctx) {
-
- octet_string_set_to_zero(ctx, sizeof(srtp_aes_icm_ctx_t));
- srtp_crypto_free(ctx);
- }
-
- srtp_crypto_free(c);
- return srtp_err_status_ok;
- }
- 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 (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;
- }
-
- v128_set_to_zero(&c->counter);
- v128_set_to_zero(&c->offset);
- copy_len = c->key_size - base_key_len;
-
- if (copy_len > SRTP_SALT_LEN) {
- copy_len = SRTP_SALT_LEN;
- }
- memcpy(&c->counter, key + base_key_len, copy_len);
- memcpy(&c->offset, key + base_key_len, copy_len);
- 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));
-
- 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;
- }
-
- c->bytes_in_buffer = 0;
- return srtp_err_status_ok;
- }
- 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;
- (void)direction;
-
- v128_copy_octet_string(&nonce, iv);
- debug_print(srtp_mod_aes_icm, "setting iv: %s", v128_hex_string(&nonce));
- v128_xor(&c->counter, &c->offset, &nonce);
- debug_print(srtp_mod_aes_icm, "set_counter: %s",
- v128_hex_string(&c->counter));
-
- c->bytes_in_buffer = 0;
- return srtp_err_status_ok;
- }
- static void srtp_aes_icm_advance(srtp_aes_icm_ctx_t *c)
- {
-
- 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(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 (!++(c->counter.v8[15])) {
- ++(c->counter.v8[14]);
- }
- }
- 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;
-
- unsigned int bytes_of_new_keystream = bytes_to_encr - c->bytes_in_buffer;
- unsigned int blocks_of_new_keystream = (bytes_of_new_keystream + 15) >> 4;
- if ((blocks_of_new_keystream + htons(c->counter.v16[7])) > 0xffff) {
- return srtp_err_status_terminus;
- }
- debug_print(srtp_mod_aes_icm, "block index: %d", htons(c->counter.v16[7]));
- if (bytes_to_encr <= (unsigned int)c->bytes_in_buffer) {
-
- 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];
- }
- c->bytes_in_buffer -= bytes_to_encr;
-
- return srtp_err_status_ok;
- } else {
-
- for (i = (sizeof(v128_t) - c->bytes_in_buffer); i < sizeof(v128_t);
- i++) {
- *buf++ ^= c->keystream_buffer.v8[i];
- }
- bytes_to_encr -= c->bytes_in_buffer;
- c->bytes_in_buffer = 0;
- }
-
- for (i = 0; i < (bytes_to_encr / sizeof(v128_t)); i++) {
-
- srtp_aes_icm_advance(c);
-
- #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 ((((uintptr_t)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 ((bytes_to_encr & 0xf) != 0) {
-
- srtp_aes_icm_advance(c);
- for (i = 0; i < (bytes_to_encr & 0xf); i++) {
- *buf++ ^= c->keystream_buffer.v8[i];
- }
-
- c->bytes_in_buffer = sizeof(v128_t) - i;
- } else {
-
- c->bytes_in_buffer = 0;
- }
- return srtp_err_status_ok;
- }
- 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";
- const srtp_cipher_type_t srtp_aes_icm_128 = {
- srtp_aes_icm_alloc,
- srtp_aes_icm_dealloc,
- srtp_aes_icm_context_init,
- 0,
- srtp_aes_icm_encrypt,
- srtp_aes_icm_encrypt,
- srtp_aes_icm_set_iv,
- 0,
- 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,
- srtp_aes_icm_encrypt,
- srtp_aes_icm_encrypt,
- srtp_aes_icm_set_iv,
- 0,
- srtp_aes_icm_256_description,
- &srtp_aes_icm_256_test_case_0,
- SRTP_AES_ICM_256
- };
|