Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use size_t for all memory length variables #675

Merged
merged 2 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crypto/cipher/aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1503,7 +1503,7 @@ static void aes_256_expand_encryption_key(const unsigned char *key,

srtp_err_status_t srtp_aes_expand_encryption_key(
const uint8_t *key,
int key_len,
size_t key_len,
srtp_aes_expanded_key_t *expanded_key)
{
if (key_len == 16) {
Expand All @@ -1522,7 +1522,7 @@ srtp_err_status_t srtp_aes_expand_encryption_key(

srtp_err_status_t srtp_aes_expand_decryption_key(
const uint8_t *key,
int key_len,
size_t key_len,
srtp_aes_expanded_key_t *expanded_key)
{
int i;
Expand Down
19 changes: 10 additions & 9 deletions crypto/cipher/aes_gcm_mbedtls.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,16 @@ srtp_debug_module_t srtp_mod_aes_gcm = {
* initializing the KDF.
*/
static srtp_err_status_t srtp_aes_gcm_mbedtls_alloc(srtp_cipher_t **c,
int key_len,
int tlen)
size_t key_len,
size_t tlen)
{
FUNC_ENTRY();
srtp_aes_gcm_ctx_t *gcm;

debug_print(srtp_mod_aes_gcm, "allocating cipher with key length %d",
debug_print(srtp_mod_aes_gcm, "allocating cipher with key length %zu",
key_len);
debug_print(srtp_mod_aes_gcm, "allocating cipher with tag length %d", tlen);
debug_print(srtp_mod_aes_gcm, "allocating cipher with tag length %zu",
tlen);

/*
* Verify the key_len is valid for one of: AES-128/256
Expand Down Expand Up @@ -253,7 +254,7 @@ static srtp_err_status_t srtp_aes_gcm_mbedtls_set_iv(
*/
static srtp_err_status_t srtp_aes_gcm_mbedtls_set_aad(void *cv,
const uint8_t *aad,
uint32_t aad_len)
size_t aad_len)
{
FUNC_ENTRY();
srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
Expand Down Expand Up @@ -281,7 +282,7 @@ static srtp_err_status_t srtp_aes_gcm_mbedtls_set_aad(void *cv,
*/
static srtp_err_status_t srtp_aes_gcm_mbedtls_encrypt(void *cv,
unsigned char *buf,
unsigned int *enc_len)
size_t *enc_len)
{
FUNC_ENTRY();
srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
Expand Down Expand Up @@ -317,11 +318,11 @@ static srtp_err_status_t srtp_aes_gcm_mbedtls_encrypt(void *cv,
*/
static srtp_err_status_t srtp_aes_gcm_mbedtls_get_tag(void *cv,
uint8_t *buf,
uint32_t *len)
size_t *len)
{
FUNC_ENTRY();
srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
debug_print(srtp_mod_aes_gcm, "appended tag size: %d", c->tag_len);
debug_print(srtp_mod_aes_gcm, "appended tag size: %zu", c->tag_len);
*len = c->tag_len;
memcpy(buf, c->tag, c->tag_len);
return (srtp_err_status_ok);
Expand All @@ -337,7 +338,7 @@ static srtp_err_status_t srtp_aes_gcm_mbedtls_get_tag(void *cv,
*/
static srtp_err_status_t srtp_aes_gcm_mbedtls_decrypt(void *cv,
unsigned char *buf,
unsigned int *enc_len)
size_t *enc_len)
{
FUNC_ENTRY();
srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
Expand Down
26 changes: 14 additions & 12 deletions crypto/cipher/aes_gcm_nss.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,16 @@ srtp_debug_module_t srtp_mod_aes_gcm = {
* initializing the KDF.
*/
static srtp_err_status_t srtp_aes_gcm_nss_alloc(srtp_cipher_t **c,
int key_len,
int tlen)
size_t key_len,
size_t tlen)
{
srtp_aes_gcm_ctx_t *gcm;
NSSInitContext *nss;

debug_print(srtp_mod_aes_gcm, "allocating cipher with key length %d",
debug_print(srtp_mod_aes_gcm, "allocating cipher with key length %zu",
key_len);
debug_print(srtp_mod_aes_gcm, "allocating cipher with tag length %d", tlen);
debug_print(srtp_mod_aes_gcm, "allocating cipher with tag length %zu",
tlen);

/*
* Verify the key_len is valid for one of: AES-128/256
Expand Down Expand Up @@ -261,7 +262,7 @@ static srtp_err_status_t srtp_aes_gcm_nss_set_iv(
*/
static srtp_err_status_t srtp_aes_gcm_nss_set_aad(void *cv,
const uint8_t *aad,
uint32_t aad_len)
size_t aad_len)
{
srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;

Expand All @@ -281,7 +282,7 @@ static srtp_err_status_t srtp_aes_gcm_nss_set_aad(void *cv,
static srtp_err_status_t srtp_aes_gcm_nss_do_crypto(void *cv,
int encrypt,
unsigned char *buf,
unsigned int *enc_len)
size_t *enc_len)
{
srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;

Expand All @@ -293,17 +294,18 @@ static srtp_err_status_t srtp_aes_gcm_nss_do_crypto(void *cv,
// Reset AAD
c->aad_size = 0;

unsigned int out_len = 0;
int rv;
SECItem param = { siBuffer, (unsigned char *)&c->params,
sizeof(CK_GCM_PARAMS) };
if (encrypt) {
rv = PK11_Encrypt(c->key, CKM_AES_GCM, &param, buf, enc_len,
rv = PK11_Encrypt(c->key, CKM_AES_GCM, &param, buf, &out_len,
*enc_len + 16, buf, *enc_len);
} else {
rv = PK11_Decrypt(c->key, CKM_AES_GCM, &param, buf, enc_len, *enc_len,
rv = PK11_Decrypt(c->key, CKM_AES_GCM, &param, buf, &out_len, *enc_len,
buf, *enc_len);
}

*enc_len = out_len;
srtp_err_status_t status = (srtp_err_status_ok);
if (rv != SECSuccess) {
status = (srtp_err_status_cipher_fail);
Expand All @@ -327,7 +329,7 @@ static srtp_err_status_t srtp_aes_gcm_nss_do_crypto(void *cv,
*/
static srtp_err_status_t srtp_aes_gcm_nss_encrypt(void *cv,
unsigned char *buf,
unsigned int *enc_len)
size_t *enc_len)
{
srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;

Expand Down Expand Up @@ -368,7 +370,7 @@ static srtp_err_status_t srtp_aes_gcm_nss_encrypt(void *cv,
*/
static srtp_err_status_t srtp_aes_gcm_nss_get_tag(void *cv,
uint8_t *buf,
uint32_t *len)
size_t *len)
{
srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
*len = c->tag_size;
Expand All @@ -386,7 +388,7 @@ static srtp_err_status_t srtp_aes_gcm_nss_get_tag(void *cv,
*/
static srtp_err_status_t srtp_aes_gcm_nss_decrypt(void *cv,
unsigned char *buf,
unsigned int *enc_len)
size_t *enc_len)
{
srtp_err_status_t status = srtp_aes_gcm_nss_do_crypto(cv, 0, buf, enc_len);
if (status != srtp_err_status_ok) {
Expand Down
17 changes: 9 additions & 8 deletions crypto/cipher/aes_gcm_ossl.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,15 @@ srtp_debug_module_t srtp_mod_aes_gcm = {
* initializing the KDF.
*/
static srtp_err_status_t srtp_aes_gcm_openssl_alloc(srtp_cipher_t **c,
int key_len,
int tlen)
size_t key_len,
size_t tlen)
{
srtp_aes_gcm_ctx_t *gcm;

debug_print(srtp_mod_aes_gcm, "allocating cipher with key length %d",
debug_print(srtp_mod_aes_gcm, "allocating cipher with key length %zu",
key_len);
debug_print(srtp_mod_aes_gcm, "allocating cipher with tag length %d", tlen);
debug_print(srtp_mod_aes_gcm, "allocating cipher with tag length %zu",
tlen);

/*
* Verify the key_len is valid for one of: AES-128/256
Expand Down Expand Up @@ -244,7 +245,7 @@ static srtp_err_status_t srtp_aes_gcm_openssl_set_iv(
*/
static srtp_err_status_t srtp_aes_gcm_openssl_set_aad(void *cv,
const uint8_t *aad,
uint32_t aad_len)
size_t aad_len)
{
srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
int rv;
Expand Down Expand Up @@ -293,7 +294,7 @@ static srtp_err_status_t srtp_aes_gcm_openssl_set_aad(void *cv,
*/
static srtp_err_status_t srtp_aes_gcm_openssl_encrypt(void *cv,
unsigned char *buf,
unsigned int *enc_len)
size_t *enc_len)
{
srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
if (c->dir != srtp_direction_encrypt && c->dir != srtp_direction_decrypt) {
Expand Down Expand Up @@ -321,7 +322,7 @@ static srtp_err_status_t srtp_aes_gcm_openssl_encrypt(void *cv,
*/
static srtp_err_status_t srtp_aes_gcm_openssl_get_tag(void *cv,
uint8_t *buf,
uint32_t *len)
size_t *len)
{
srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
/*
Expand Down Expand Up @@ -354,7 +355,7 @@ static srtp_err_status_t srtp_aes_gcm_openssl_get_tag(void *cv,
*/
static srtp_err_status_t srtp_aes_gcm_openssl_decrypt(void *cv,
unsigned char *buf,
unsigned int *enc_len)
size_t *enc_len)
{
srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
if (c->dir != srtp_direction_encrypt && c->dir != srtp_direction_decrypt) {
Expand Down
12 changes: 6 additions & 6 deletions crypto/cipher/aes_icm.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ srtp_debug_module_t srtp_mod_aes_icm = {
*/

static srtp_err_status_t srtp_aes_icm_alloc(srtp_cipher_t **c,
int key_len,
int tlen)
size_t key_len,
size_t tlen)
{
srtp_aes_icm_ctx_t *icm;
(void)tlen;

debug_print(srtp_mod_aes_icm, "allocating cipher with key length %d",
debug_print(srtp_mod_aes_icm, "allocating cipher with key length %zu",
key_len);

/*
Expand Down Expand Up @@ -183,7 +183,7 @@ 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;
size_t 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) {
Expand Down Expand Up @@ -296,10 +296,10 @@ static void srtp_aes_icm_advance(srtp_aes_icm_ctx_t *c)

static srtp_err_status_t srtp_aes_icm_encrypt(void *cv,
unsigned char *buf,
unsigned int *enc_len)
size_t *enc_len)
{
srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
unsigned int bytes_to_encr = *enc_len;
unsigned int bytes_to_encr = (unsigned int)*enc_len;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this one not also size_t?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because changing it led to a lot of other changes in in the aes code that I think require more understanding. Should maybe do it as it's own commit.

unsigned int i;
uint32_t *b;

Expand Down
8 changes: 4 additions & 4 deletions crypto/cipher/aes_icm_mbedtls.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ srtp_debug_module_t srtp_mod_aes_icm = {
* isn't used in counter mode.
*/
static srtp_err_status_t srtp_aes_icm_mbedtls_alloc(srtp_cipher_t **c,
int key_len,
int tlen)
size_t key_len,
size_t tlen)
{
srtp_aes_icm_ctx_t *icm;
(void)tlen;

debug_print(srtp_mod_aes_icm, "allocating cipher with key length %d",
debug_print(srtp_mod_aes_icm, "allocating cipher with key length %zu",
key_len);

/*
Expand Down Expand Up @@ -291,7 +291,7 @@ static srtp_err_status_t srtp_aes_icm_mbedtls_set_iv(
*/
static srtp_err_status_t srtp_aes_icm_mbedtls_encrypt(void *cv,
unsigned char *buf,
unsigned int *enc_len)
size_t *enc_len)
{
srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;

Expand Down
14 changes: 7 additions & 7 deletions crypto/cipher/aes_icm_nss.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,14 @@ srtp_debug_module_t srtp_mod_aes_icm = {
* isn't used in counter mode.
*/
static srtp_err_status_t srtp_aes_icm_nss_alloc(srtp_cipher_t **c,
int key_len,
int tlen)
size_t key_len,
size_t tlen)
{
srtp_aes_icm_ctx_t *icm;
NSSInitContext *nss;
(void)tlen;

debug_print(srtp_mod_aes_icm, "allocating cipher with key length %d",
debug_print(srtp_mod_aes_icm, "allocating cipher with key length %zu",
key_len);

/*
Expand Down Expand Up @@ -324,17 +324,17 @@ static srtp_err_status_t srtp_aes_icm_nss_set_iv(void *cv,
*/
static srtp_err_status_t srtp_aes_icm_nss_encrypt(void *cv,
unsigned char *buf,
unsigned int *enc_len)
size_t *enc_len)
{
srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;

if (!c->ctx) {
return srtp_err_status_bad_param;
}

int rv =
PK11_CipherOp(c->ctx, buf, (int *)enc_len, *enc_len, buf, *enc_len);

int out_len = 0;
int rv = PK11_CipherOp(c->ctx, buf, &out_len, *enc_len, buf, *enc_len);
*enc_len = out_len;
srtp_err_status_t status = (srtp_err_status_ok);
if (rv != SECSuccess) {
status = (srtp_err_status_cipher_fail);
Expand Down
8 changes: 4 additions & 4 deletions crypto/cipher/aes_icm_ossl.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,13 @@ srtp_debug_module_t srtp_mod_aes_icm = {
* isn't used in counter mode.
*/
static srtp_err_status_t srtp_aes_icm_openssl_alloc(srtp_cipher_t **c,
int key_len,
int tlen)
size_t key_len,
size_t tlen)
{
srtp_aes_icm_ctx_t *icm;
(void)tlen;

debug_print(srtp_mod_aes_icm, "allocating cipher with key length %d",
debug_print(srtp_mod_aes_icm, "allocating cipher with key length %zu",
key_len);

/*
Expand Down Expand Up @@ -299,7 +299,7 @@ static srtp_err_status_t srtp_aes_icm_openssl_set_iv(
*/
static srtp_err_status_t srtp_aes_icm_openssl_encrypt(void *cv,
unsigned char *buf,
unsigned int *enc_len)
size_t *enc_len)
{
srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
int len = 0;
Expand Down
Loading
Loading