123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374 |
- #ifdef HAVE_CONFIG_H
- #include <config.h>
- #endif
- #include <mbedtls/aes.h>
- #include "aes_icm_ext.h"
- #include "crypto_types.h"
- #include "err.h"
- #include "alloc.h"
- #include "cipher_types.h"
- #include "cipher_test_cases.h"
- srtp_debug_module_t srtp_mod_aes_icm = {
- 0,
- "aes icm mbedtls"
- };
- static srtp_err_status_t srtp_aes_icm_mbedtls_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_192_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;
- }
- icm->ctx =
- (mbedtls_aes_context *)srtp_crypto_alloc(sizeof(mbedtls_aes_context));
- if (icm->ctx == NULL) {
- srtp_crypto_free(icm);
- srtp_crypto_free(*c);
- *c = NULL;
- return srtp_err_status_alloc_fail;
- }
- mbedtls_aes_init(icm->ctx);
-
- (*c)->state = icm;
-
- switch (key_len) {
- 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 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 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;
- }
-
- (*c)->key_len = key_len;
- return srtp_err_status_ok;
- }
- static srtp_err_status_t srtp_aes_icm_mbedtls_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 != NULL) {
- mbedtls_aes_free(ctx->ctx);
- srtp_crypto_free(ctx->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_mbedtls_context_init(void *cv,
- const uint8_t *key)
- {
- srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
- uint32_t key_size_in_bits = (c->key_size << 3);
- int errcode = 0;
-
- v128_set_to_zero(&c->counter);
- v128_set_to_zero(&c->offset);
- memcpy(&c->counter, key + c->key_size, SRTP_SALT_LEN);
- memcpy(&c->offset, key + c->key_size, SRTP_SALT_LEN);
-
- 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;
- 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));
- switch (c->key_size) {
- case SRTP_AES_256_KEY_LEN:
- case SRTP_AES_192_KEY_LEN:
- case SRTP_AES_128_KEY_LEN:
- break;
- default:
- return srtp_err_status_bad_param;
- break;
- }
- errcode = mbedtls_aes_setkey_enc(c->ctx, key, key_size_in_bits);
- if (errcode != 0) {
- debug_print(srtp_mod_aes_icm, "errCode: %d", errcode);
- }
- return srtp_err_status_ok;
- }
- static srtp_err_status_t srtp_aes_icm_mbedtls_set_iv(
- void *cv,
- uint8_t *iv,
- srtp_cipher_direction_t dir)
- {
- srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
- v128_t nonce;
- (void)dir;
- c->nc_off = 0;
-
- 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));
- return srtp_err_status_ok;
- }
- static srtp_err_status_t srtp_aes_icm_mbedtls_encrypt(void *cv,
- unsigned char *buf,
- unsigned int *enc_len)
- {
- srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
- int errCode = 0;
- debug_print(srtp_mod_aes_icm, "rs0: %s", v128_hex_string(&c->counter));
- errCode =
- mbedtls_aes_crypt_ctr(c->ctx, *enc_len, &(c->nc_off), c->counter.v8,
- c->stream_block.v8, buf, buf);
- if (errCode != 0) {
- debug_print(srtp_mod_aes_icm, "encrypt error: %d", errCode);
- return srtp_err_status_cipher_fail;
- }
- return srtp_err_status_ok;
- }
- static const char srtp_aes_icm_128_mbedtls_description[] =
- "AES-128 counter mode using mbedtls";
- static const char srtp_aes_icm_192_mbedtls_description[] =
- "AES-192 counter mode using mbedtls";
- static const char srtp_aes_icm_256_mbedtls_description[] =
- "AES-256 counter mode using mbedtls";
- const srtp_cipher_type_t srtp_aes_icm_128 = {
- srtp_aes_icm_mbedtls_alloc,
- srtp_aes_icm_mbedtls_dealloc,
- srtp_aes_icm_mbedtls_context_init,
- 0,
- srtp_aes_icm_mbedtls_encrypt,
- srtp_aes_icm_mbedtls_encrypt,
- srtp_aes_icm_mbedtls_set_iv,
- 0,
- srtp_aes_icm_128_mbedtls_description,
- &srtp_aes_icm_128_test_case_0,
- SRTP_AES_ICM_128
- };
- const srtp_cipher_type_t srtp_aes_icm_192 = {
- srtp_aes_icm_mbedtls_alloc,
- srtp_aes_icm_mbedtls_dealloc,
- srtp_aes_icm_mbedtls_context_init,
- 0,
- srtp_aes_icm_mbedtls_encrypt,
- srtp_aes_icm_mbedtls_encrypt,
- srtp_aes_icm_mbedtls_set_iv,
- 0,
- srtp_aes_icm_192_mbedtls_description,
- &srtp_aes_icm_192_test_case_0,
- SRTP_AES_ICM_192
- };
- const srtp_cipher_type_t srtp_aes_icm_256 = {
- srtp_aes_icm_mbedtls_alloc,
- srtp_aes_icm_mbedtls_dealloc,
- srtp_aes_icm_mbedtls_context_init,
- 0,
- srtp_aes_icm_mbedtls_encrypt,
- srtp_aes_icm_mbedtls_encrypt,
- srtp_aes_icm_mbedtls_set_iv,
- 0,
- srtp_aes_icm_256_mbedtls_description,
- &srtp_aes_icm_256_test_case_0,
- SRTP_AES_ICM_256
- };
|