From a3904932bf56e1326b3b90061011a5030e5ce27d Mon Sep 17 00:00:00 2001 From: irungentoo Date: Wed, 19 Feb 2014 07:34:44 -0500 Subject: [PATCH] crypto_secretbox_NONCEBYTES is the one to use for the symmetric encryption. Not currently a big deal since they are the same size, but... --- toxav/msi.c | 16 ++++++++-------- toxav/rtp.c | 37 +++++++++++++++++++------------------ 2 files changed, 27 insertions(+), 26 deletions(-) diff --git a/toxav/msi.c b/toxav/msi.c index 8f69d942..6cc9cf12 100644 --- a/toxav/msi.c +++ b/toxav/msi.c @@ -893,8 +893,8 @@ int handle_recv_start ( MSISession *session, MSIMessage *msg ) session->call->key_peer = calloc ( sizeof ( uint8_t ), crypto_secretbox_KEYBYTES ); memcpy ( session->call->key_peer, msg->cryptokey.header_value, crypto_secretbox_KEYBYTES ); - session->call->nonce_peer = calloc ( sizeof ( uint8_t ), crypto_box_NONCEBYTES ); - memcpy ( session->call->nonce_peer, msg->nonce.header_value, crypto_box_NONCEBYTES ); + session->call->nonce_peer = calloc ( sizeof ( uint8_t ), crypto_secretbox_NONCEBYTES ); + memcpy ( session->call->nonce_peer, msg->nonce.header_value, crypto_secretbox_NONCEBYTES ); flush_peer_type ( session, msg, 0 ); @@ -981,21 +981,21 @@ int handle_recv_starting ( MSISession *session, MSIMessage *msg ) session->call->key_local = calloc ( sizeof ( uint8_t ), crypto_secretbox_KEYBYTES ); new_symmetric_key ( session->call->key_local ); - session->call->nonce_local = calloc ( sizeof ( uint8_t ), crypto_box_NONCEBYTES ); + session->call->nonce_local = calloc ( sizeof ( uint8_t ), crypto_secretbox_NONCEBYTES ); new_nonce ( session->call->nonce_local ); /* Save peer key/nonce */ session->call->key_peer = calloc ( sizeof ( uint8_t ), crypto_secretbox_KEYBYTES ); memcpy ( session->call->key_peer, msg->cryptokey.header_value, crypto_secretbox_KEYBYTES ); - session->call->nonce_peer = calloc ( sizeof ( uint8_t ), crypto_box_NONCEBYTES ); - memcpy ( session->call->nonce_peer, msg->nonce.header_value, crypto_box_NONCEBYTES ); + session->call->nonce_peer = calloc ( sizeof ( uint8_t ), crypto_secretbox_NONCEBYTES ); + memcpy ( session->call->nonce_peer, msg->nonce.header_value, crypto_secretbox_NONCEBYTES ); session->call->state = call_active; MSIMessage *_msg_start = msi_new_message ( TYPE_REQUEST, stringify_request ( start ) ); msi_msg_set_cryptokey ( _msg_start, session->call->key_local, crypto_secretbox_KEYBYTES ); - msi_msg_set_nonce ( _msg_start, session->call->nonce_local, crypto_box_NONCEBYTES ); + msi_msg_set_nonce ( _msg_start, session->call->nonce_local, crypto_secretbox_NONCEBYTES ); send_message ( session, _msg_start, msg->friend_id ); free_message ( _msg_start ); @@ -1341,11 +1341,11 @@ int msi_answer ( MSISession *session, MSICallType call_type ) session->call->key_local = calloc ( sizeof ( uint8_t ), crypto_secretbox_KEYBYTES ); new_symmetric_key ( session->call->key_local ); - session->call->nonce_local = calloc ( sizeof ( uint8_t ), crypto_box_NONCEBYTES ); + session->call->nonce_local = calloc ( sizeof ( uint8_t ), crypto_secretbox_NONCEBYTES ); new_nonce ( session->call->nonce_local ); msi_msg_set_cryptokey ( _msg_starting, session->call->key_local, crypto_secretbox_KEYBYTES ); - msi_msg_set_nonce ( _msg_starting, session->call->nonce_local, crypto_box_NONCEBYTES ); + msi_msg_set_nonce ( _msg_starting, session->call->nonce_local, crypto_secretbox_NONCEBYTES ); send_message ( session, _msg_starting, session->call->peers[session->call->peer_count - 1] ); free_message ( _msg_starting ); diff --git a/toxav/rtp.c b/toxav/rtp.c index dbaecbb6..8f6f0be7 100644 --- a/toxav/rtp.c +++ b/toxav/rtp.c @@ -168,8 +168,8 @@ inline__ void increase_nonce(uint8_t *nonce, uint16_t target) uint16_t _nonce_counter; uint8_t _reverse_bytes[2]; - _reverse_bytes[0] = nonce[crypto_box_NONCEBYTES - 1]; - _reverse_bytes[1] = nonce[crypto_box_NONCEBYTES - 2]; + _reverse_bytes[0] = nonce[crypto_secretbox_NONCEBYTES - 1]; + _reverse_bytes[1] = nonce[crypto_secretbox_NONCEBYTES - 2]; bytes_to_U16(&_nonce_counter, _reverse_bytes ); @@ -177,7 +177,8 @@ inline__ void increase_nonce(uint8_t *nonce, uint16_t target) if (_nonce_counter > UINT16_MAX - target ) { /* 2 bytes are not long enough */ uint8_t _it = 3; - while ( _it <= crypto_box_NONCEBYTES ) _it += ++nonce[crypto_box_NONCEBYTES - _it] ? crypto_box_NONCEBYTES : 1; + while ( _it <= crypto_secretbox_NONCEBYTES ) _it += ++nonce[crypto_secretbox_NONCEBYTES - _it] ? + crypto_secretbox_NONCEBYTES : 1; _nonce_counter = _nonce_counter - (UINT16_MAX - target ); /* Assign the rest of it */ } else { /* Increase nonce */ @@ -188,8 +189,8 @@ inline__ void increase_nonce(uint8_t *nonce, uint16_t target) /* Assign the last bytes */ U16_to_bytes( _reverse_bytes, _nonce_counter); - nonce [crypto_box_NONCEBYTES - 1] = _reverse_bytes[0]; - nonce [crypto_box_NONCEBYTES - 2] = _reverse_bytes[1]; + nonce [crypto_secretbox_NONCEBYTES - 1] = _reverse_bytes[0]; + nonce [crypto_secretbox_NONCEBYTES - 2] = _reverse_bytes[1]; } @@ -525,8 +526,8 @@ int rtp_handle_packet ( void *object, IP_Port ip_port, uint8_t *data, uint32_t l bytes_to_U16(&_sequnum, data + 1); /* Clculate the right nonce */ - uint8_t _calculated[crypto_box_NONCEBYTES]; - memcpy(_calculated, _session->decrypt_nonce, crypto_box_NONCEBYTES); + uint8_t _calculated[crypto_secretbox_NONCEBYTES]; + memcpy(_calculated, _session->decrypt_nonce, crypto_secretbox_NONCEBYTES); increase_nonce ( _calculated, _sequnum ); /* Decrypt message */ @@ -556,8 +557,8 @@ int rtp_handle_packet ( void *object, IP_Port ip_port, uint8_t *data, uint32_t l if ( !_decrypted_length ) return -1; /* This is just an error */ /* A new cycle setting. */ - memcpy(_session->nonce_cycle, _session->decrypt_nonce, crypto_box_NONCEBYTES); - memcpy(_session->decrypt_nonce, _calculated, crypto_box_NONCEBYTES); + memcpy(_session->nonce_cycle, _session->decrypt_nonce, crypto_secretbox_NONCEBYTES); + memcpy(_session->decrypt_nonce, _calculated, crypto_secretbox_NONCEBYTES); } } @@ -755,8 +756,8 @@ int rtp_send_msg ( RTPSession *session, Messenger *messenger, const uint8_t *dat _send_data[0] = session->prefix; /* Generate the right nonce */ - uint8_t _calculated[crypto_box_NONCEBYTES]; - memcpy(_calculated, session->encrypt_nonce, crypto_box_NONCEBYTES); + uint8_t _calculated[crypto_secretbox_NONCEBYTES]; + memcpy(_calculated, session->encrypt_nonce, crypto_secretbox_NONCEBYTES); increase_nonce ( _calculated, msg->header->sequnum ); /* Need to skip 2 bytes that are for sequnum */ @@ -779,7 +780,7 @@ int rtp_send_msg ( RTPSession *session, Messenger *messenger, const uint8_t *dat /* Set sequ number */ if ( session->sequnum >= MAX_SEQU_NUM ) { session->sequnum = 0; - memcpy(session->encrypt_nonce, _calculated, crypto_box_NONCEBYTES); + memcpy(session->encrypt_nonce, _calculated, crypto_secretbox_NONCEBYTES); } else { session->sequnum++; } @@ -874,16 +875,16 @@ RTPSession *rtp_init_session ( int payload_type, _retu->decrypt_key = decrypt_key; /* Need to allocate new memory */ - _retu->encrypt_nonce = calloc ( crypto_box_NONCEBYTES, sizeof (uint8_t) ); + _retu->encrypt_nonce = calloc ( crypto_secretbox_NONCEBYTES, sizeof (uint8_t) ); assert(_retu->encrypt_nonce); - _retu->decrypt_nonce = calloc ( crypto_box_NONCEBYTES, sizeof (uint8_t) ); + _retu->decrypt_nonce = calloc ( crypto_secretbox_NONCEBYTES, sizeof (uint8_t) ); assert(_retu->decrypt_nonce); - _retu->nonce_cycle = calloc ( crypto_box_NONCEBYTES, sizeof (uint8_t) ); + _retu->nonce_cycle = calloc ( crypto_secretbox_NONCEBYTES, sizeof (uint8_t) ); assert(_retu->nonce_cycle); - memcpy(_retu->encrypt_nonce, encrypt_nonce, crypto_box_NONCEBYTES); - memcpy(_retu->decrypt_nonce, decrypt_nonce, crypto_box_NONCEBYTES); - memcpy(_retu->nonce_cycle , decrypt_nonce, crypto_box_NONCEBYTES); + memcpy(_retu->encrypt_nonce, encrypt_nonce, crypto_secretbox_NONCEBYTES); + memcpy(_retu->decrypt_nonce, decrypt_nonce, crypto_secretbox_NONCEBYTES); + memcpy(_retu->nonce_cycle , decrypt_nonce, crypto_secretbox_NONCEBYTES); _retu->csrc = calloc(1, sizeof (uint32_t)); assert(_retu->csrc);