|
ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
|
Common handshake functions: initialization, cleanup, encryption, rekeying. More...
Go to the source code of this file.
Functions | |
| asciichat_error_t | crypto_handshake_init (crypto_handshake_context_t *ctx, bool is_server) |
| Initialize crypto handshake context. | |
| asciichat_error_t | crypto_handshake_set_parameters (crypto_handshake_context_t *ctx, const crypto_parameters_packet_t *params) |
| Set crypto parameters from crypto_parameters_packet_t. | |
| asciichat_error_t | crypto_handshake_validate_packet_size (const crypto_handshake_context_t *ctx, uint16_t packet_type, size_t packet_size) |
| Validate crypto packet size based on session parameters. | |
| asciichat_error_t | crypto_handshake_init_with_password (crypto_handshake_context_t *ctx, bool is_server, const char *password) |
| Initialize crypto handshake context with password authentication. | |
| void | crypto_handshake_cleanup (crypto_handshake_context_t *ctx) |
| Cleanup crypto handshake context with secure memory wiping. | |
| bool | crypto_handshake_is_ready (const crypto_handshake_context_t *ctx) |
| Check if handshake is complete and encryption is ready. | |
| const crypto_context_t * | crypto_handshake_get_context (const crypto_handshake_context_t *ctx) |
| Get the crypto context for encryption/decryption. | |
| asciichat_error_t | crypto_handshake_encrypt_packet (const crypto_handshake_context_t *ctx, const uint8_t *plaintext, size_t plaintext_len, uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_len) |
| Encrypt a packet using the established crypto context. | |
| asciichat_error_t | crypto_handshake_decrypt_packet (const crypto_handshake_context_t *ctx, const uint8_t *ciphertext, size_t ciphertext_len, uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_len) |
| Decrypt a packet using the established crypto context. | |
| asciichat_error_t | crypto_encrypt_packet_or_passthrough (const crypto_handshake_context_t *ctx, bool crypto_ready, const uint8_t *plaintext, size_t plaintext_len, uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_len) |
| Encrypt with automatic passthrough if crypto not ready. | |
| asciichat_error_t | crypto_decrypt_packet_or_passthrough (const crypto_handshake_context_t *ctx, bool crypto_ready, const uint8_t *ciphertext, size_t ciphertext_len, uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_len) |
| Decrypt with automatic passthrough if crypto not ready. | |
| asciichat_error_t | crypto_handshake_rekey_request (crypto_handshake_context_t *ctx, socket_t socket) |
| Send REKEY_REQUEST packet (initiator side) | |
| asciichat_error_t | crypto_handshake_rekey_response (crypto_handshake_context_t *ctx, socket_t socket) |
| Send REKEY_RESPONSE packet (responder side) | |
| asciichat_error_t | crypto_handshake_rekey_complete (crypto_handshake_context_t *ctx, socket_t socket) |
| Send REKEY_COMPLETE packet (initiator side) | |
| asciichat_error_t | crypto_handshake_process_rekey_request (crypto_handshake_context_t *ctx, const uint8_t *packet, size_t packet_len) |
| Process received REKEY_REQUEST packet (responder side) | |
| asciichat_error_t | crypto_handshake_process_rekey_response (crypto_handshake_context_t *ctx, const uint8_t *packet, size_t packet_len) |
| Process received REKEY_RESPONSE packet (initiator side) | |
| asciichat_error_t | crypto_handshake_process_rekey_complete (crypto_handshake_context_t *ctx, const uint8_t *packet, size_t packet_len) |
| Process received REKEY_COMPLETE packet (responder side) | |
| bool | crypto_handshake_should_rekey (const crypto_handshake_context_t *ctx) |
| Check if rekeying should be triggered for this handshake context. | |
Common handshake functions: initialization, cleanup, encryption, rekeying.
Definition in file crypto/handshake/common.c.
| asciichat_error_t crypto_decrypt_packet_or_passthrough | ( | const crypto_handshake_context_t * | ctx, |
| bool | crypto_ready, | ||
| const uint8_t * | ciphertext, | ||
| size_t | ciphertext_len, | ||
| uint8_t * | plaintext, | ||
| size_t | plaintext_size, | ||
| size_t * | plaintext_len | ||
| ) |
Decrypt with automatic passthrough if crypto not ready.
| ctx | Handshake context |
| crypto_ready | True if crypto is ready, false to passthrough |
| ciphertext | Ciphertext or plaintext data to decrypt |
| ciphertext_len | Length of ciphertext/plaintext data |
| plaintext | Output buffer for plaintext |
| plaintext_size | Size of output buffer |
| plaintext_len | Output parameter for actual plaintext length |
Definition at line 340 of file crypto/handshake/common.c.
References ASCIICHAT_OK, crypto_handshake_decrypt_packet(), ERROR_BUFFER, and SET_ERRNO.
Referenced by crypto_client_decrypt_packet(), and crypto_server_decrypt_packet().
| asciichat_error_t crypto_encrypt_packet_or_passthrough | ( | const crypto_handshake_context_t * | ctx, |
| bool | crypto_ready, | ||
| const uint8_t * | plaintext, | ||
| size_t | plaintext_len, | ||
| uint8_t * | ciphertext, | ||
| size_t | ciphertext_size, | ||
| size_t * | ciphertext_len | ||
| ) |
Encrypt with automatic passthrough if crypto not ready.
| ctx | Handshake context |
| crypto_ready | True if crypto is ready, false to passthrough |
| plaintext | Plaintext data to encrypt |
| plaintext_len | Length of plaintext data |
| ciphertext | Output buffer for ciphertext or plaintext (if passthrough) |
| ciphertext_size | Size of output buffer |
| ciphertext_len | Output parameter for actual length |
Definition at line 321 of file crypto/handshake/common.c.
References ASCIICHAT_OK, crypto_handshake_encrypt_packet(), ERROR_BUFFER, and SET_ERRNO.
Referenced by crypto_client_encrypt_packet(), and crypto_server_encrypt_packet().
| void crypto_handshake_cleanup | ( | crypto_handshake_context_t * | ctx | ) |
Cleanup crypto handshake context with secure memory wiping.
| ctx | Handshake context to cleanup |
Definition at line 259 of file crypto/handshake/common.c.
References crypto_cleanup(), and crypto_handshake_context_t::crypto_ctx.
Referenced by client_crypto_init(), crypto_client_cleanup(), crypto_server_cleanup_client(), and server_connection_close().
| asciichat_error_t crypto_handshake_decrypt_packet | ( | const crypto_handshake_context_t * | ctx, |
| const uint8_t * | ciphertext, | ||
| size_t | ciphertext_len, | ||
| uint8_t * | plaintext, | ||
| size_t | plaintext_size, | ||
| size_t * | plaintext_len | ||
| ) |
Decrypt a packet using the established crypto context.
| ctx | Handshake context (must be ready) |
| ciphertext | Ciphertext data to decrypt |
| ciphertext_len | Length of ciphertext data |
| plaintext | Output buffer for plaintext |
| plaintext_size | Size of output buffer |
| plaintext_len | Output parameter for actual plaintext length |
Definition at line 303 of file crypto/handshake/common.c.
References ASCIICHAT_OK, crypto_handshake_context_t::crypto_ctx, crypto_decrypt(), crypto_handshake_is_ready(), CRYPTO_OK, crypto_result_to_string(), ERROR_INVALID_STATE, ERROR_NETWORK, and SET_ERRNO.
Referenced by crypto_decrypt_packet_or_passthrough().
| asciichat_error_t crypto_handshake_encrypt_packet | ( | const crypto_handshake_context_t * | ctx, |
| const uint8_t * | plaintext, | ||
| size_t | plaintext_len, | ||
| uint8_t * | ciphertext, | ||
| size_t | ciphertext_size, | ||
| size_t * | ciphertext_len | ||
| ) |
Encrypt a packet using the established crypto context.
| ctx | Handshake context (must be ready) |
| plaintext | Plaintext data to encrypt |
| plaintext_len | Length of plaintext data |
| ciphertext | Output buffer for ciphertext |
| ciphertext_size | Size of output buffer |
| ciphertext_len | Output parameter for actual ciphertext length |
Definition at line 285 of file crypto/handshake/common.c.
References ASCIICHAT_OK, crypto_handshake_context_t::crypto_ctx, crypto_encrypt(), crypto_handshake_is_ready(), CRYPTO_OK, crypto_result_to_string(), ERROR_INVALID_STATE, ERROR_NETWORK, and SET_ERRNO.
Referenced by crypto_encrypt_packet_or_passthrough().
| const crypto_context_t * crypto_handshake_get_context | ( | const crypto_handshake_context_t * | ctx | ) |
Get the crypto context for encryption/decryption.
| ctx | Handshake context |
Definition at line 278 of file crypto/handshake/common.c.
References crypto_handshake_context_t::crypto_ctx, and crypto_handshake_is_ready().
Referenced by broadcast_server_state_to_all_clients(), client_send_thread_func(), crypto_client_get_context(), crypto_server_get_context(), disconnect_client_for_bad_data(), tcp_client_send_audio_batch(), tcp_client_send_audio_opus(), tcp_client_send_audio_opus_batch(), tcp_client_send_join(), and tcp_client_send_packet().
| asciichat_error_t crypto_handshake_init | ( | crypto_handshake_context_t * | ctx, |
| bool | is_server | ||
| ) |
Initialize crypto handshake context.
| ctx | Handshake context to initialize (must not be NULL) |
| is_server | True if this is the server side, false for client |
Definition at line 17 of file crypto/handshake/common.c.
References ASCIICHAT_OK, crypto_handshake_context_t::crypto_ctx, CRYPTO_HANDSHAKE_INIT, crypto_init(), CRYPTO_OK, crypto_result_to_string(), ERROR_CRYPTO, ERROR_INVALID_PARAM, crypto_handshake_context_t::is_server, log_info, crypto_handshake_context_t::require_client_auth, crypto_handshake_context_t::server_uses_client_auth, SET_ERRNO, crypto_handshake_context_t::state, and crypto_handshake_context_t::verify_server_key.
Referenced by client_crypto_init(), and server_crypto_handshake().
| asciichat_error_t crypto_handshake_init_with_password | ( | crypto_handshake_context_t * | ctx, |
| bool | is_server, | ||
| const char * | password | ||
| ) |
Initialize crypto handshake context with password authentication.
| ctx | Handshake context to initialize (must not be NULL) |
| is_server | True if this is the server side, false for client |
| password | Password for authentication (must meet length requirements) |
Definition at line 229 of file crypto/handshake/common.c.
References ASCIICHAT_OK, crypto_handshake_context_t::crypto_ctx, CRYPTO_HANDSHAKE_INIT, crypto_init_with_password(), CRYPTO_OK, crypto_result_to_string(), ERROR_CRYPTO, ERROR_INVALID_PARAM, crypto_handshake_context_t::has_password, crypto_handshake_context_t::is_server, crypto_handshake_context_t::password, crypto_handshake_context_t::require_client_auth, SAFE_STRNCPY, crypto_handshake_context_t::server_uses_client_auth, SET_ERRNO, crypto_handshake_context_t::state, and crypto_handshake_context_t::verify_server_key.
Referenced by client_crypto_init(), and server_crypto_handshake().
| bool crypto_handshake_is_ready | ( | const crypto_handshake_context_t * | ctx | ) |
Check if handshake is complete and encryption is ready.
| ctx | Handshake context |
Definition at line 271 of file crypto/handshake/common.c.
References crypto_handshake_context_t::crypto_ctx, CRYPTO_HANDSHAKE_READY, crypto_is_ready(), and crypto_handshake_context_t::state.
Referenced by client_send_thread_func(), crypto_client_is_ready(), crypto_handshake_decrypt_packet(), crypto_handshake_encrypt_packet(), crypto_handshake_get_context(), crypto_handshake_process_rekey_complete(), crypto_handshake_process_rekey_request(), crypto_handshake_process_rekey_response(), crypto_handshake_rekey_complete(), crypto_handshake_rekey_request(), crypto_handshake_rekey_response(), crypto_handshake_should_rekey(), crypto_server_is_ready(), tcp_client_send_audio_batch(), tcp_client_send_audio_opus(), tcp_client_send_audio_opus_batch(), tcp_client_send_join(), and tcp_client_send_packet().
| asciichat_error_t crypto_handshake_process_rekey_complete | ( | crypto_handshake_context_t * | ctx, |
| const uint8_t * | packet, | ||
| size_t | packet_len | ||
| ) |
Process received REKEY_COMPLETE packet (responder side)
Process received REKEY_COMPLETE packet (responder side). Verifies that the packet decrypts with the new shared secret. If successful, commits to the new key.
Definition at line 563 of file crypto/handshake/common.c.
References ASCIICHAT_OK, crypto_handshake_context_t::crypto_ctx, crypto_decrypt(), crypto_handshake_is_ready(), CRYPTO_OK, crypto_rekey_abort(), crypto_rekey_commit(), crypto_result_to_string(), CRYPTO_SHARED_KEY_SIZE, ERROR_CRYPTO, ERROR_INVALID_STATE, crypto_context_t::has_temp_key, log_info, crypto_context_t::rekey_in_progress, SET_ERRNO, crypto_context_t::shared_key, and crypto_context_t::temp_shared_key.
| asciichat_error_t crypto_handshake_process_rekey_request | ( | crypto_handshake_context_t * | ctx, |
| const uint8_t * | packet, | ||
| size_t | packet_len | ||
| ) |
Process received REKEY_REQUEST packet (responder side)
Process received REKEY_REQUEST packet (responder side). Extracts peer's new ephemeral public key and computes new shared secret.
Definition at line 478 of file crypto/handshake/common.c.
References ASCIICHAT_OK, crypto_handshake_context_t::crypto_ctx, crypto_handshake_is_ready(), CRYPTO_OK, CRYPTO_PUBLIC_KEY_SIZE, crypto_rekey_abort(), crypto_rekey_init(), crypto_rekey_process_request(), crypto_result_to_string(), ERROR_CRYPTO, ERROR_INVALID_PARAM, ERROR_INVALID_STATE, log_debug, log_info, crypto_context_t::rekey_last_request_time, REKEY_MIN_REQUEST_INTERVAL, and SET_ERRNO.
Referenced by crypto_client_process_rekey_request().
| asciichat_error_t crypto_handshake_process_rekey_response | ( | crypto_handshake_context_t * | ctx, |
| const uint8_t * | packet, | ||
| size_t | packet_len | ||
| ) |
Process received REKEY_RESPONSE packet (initiator side)
Process received REKEY_RESPONSE packet (initiator side). Extracts peer's new ephemeral public key and computes new shared secret.
Definition at line 528 of file crypto/handshake/common.c.
References ASCIICHAT_OK, crypto_handshake_context_t::crypto_ctx, crypto_handshake_is_ready(), CRYPTO_OK, CRYPTO_PUBLIC_KEY_SIZE, crypto_rekey_abort(), crypto_rekey_process_response(), crypto_result_to_string(), ERROR_CRYPTO, ERROR_INVALID_PARAM, ERROR_INVALID_STATE, crypto_context_t::has_temp_key, log_debug, log_info, crypto_context_t::rekey_in_progress, and SET_ERRNO.
Referenced by crypto_client_process_rekey_response().
| asciichat_error_t crypto_handshake_rekey_complete | ( | crypto_handshake_context_t * | ctx, |
| socket_t | socket | ||
| ) |
Send REKEY_COMPLETE packet (initiator side)
Send REKEY_COMPLETE packet (initiator side). CRITICAL: This packet is encrypted with the NEW shared secret. It proves that both sides have computed the same shared secret.
Definition at line 423 of file crypto/handshake/common.c.
References ASCIICHAT_OK, crypto_handshake_context_t::crypto_ctx, crypto_encrypt(), crypto_handshake_is_ready(), CRYPTO_OK, crypto_rekey_abort(), crypto_rekey_commit(), crypto_result_to_string(), CRYPTO_SHARED_KEY_SIZE, ERROR_CRYPTO, ERROR_INVALID_STATE, ERROR_NETWORK, crypto_context_t::has_temp_key, log_info, PACKET_TYPE_CRYPTO_REKEY_COMPLETE, crypto_context_t::rekey_in_progress, send_packet(), SET_ERRNO, crypto_context_t::shared_key, and crypto_context_t::temp_shared_key.
Referenced by crypto_client_send_rekey_complete().
| asciichat_error_t crypto_handshake_rekey_request | ( | crypto_handshake_context_t * | ctx, |
| socket_t | socket | ||
| ) |
Send REKEY_REQUEST packet (initiator side)
Send REKEY_REQUEST packet (initiator side). Sends the initiator's new ephemeral public key to the peer.
Definition at line 366 of file crypto/handshake/common.c.
References ASCIICHAT_OK, crypto_handshake_context_t::crypto_ctx, crypto_handshake_is_ready(), CRYPTO_OK, CRYPTO_PUBLIC_KEY_SIZE, crypto_rekey_abort(), crypto_rekey_init(), crypto_result_to_string(), ERROR_CRYPTO, ERROR_INVALID_STATE, ERROR_NETWORK, log_debug, log_info, PACKET_TYPE_CRYPTO_REKEY_REQUEST, send_packet(), SET_ERRNO, and crypto_context_t::temp_public_key.
Referenced by client_send_thread_func(), and crypto_client_initiate_rekey().
| asciichat_error_t crypto_handshake_rekey_response | ( | crypto_handshake_context_t * | ctx, |
| socket_t | socket | ||
| ) |
Send REKEY_RESPONSE packet (responder side)
Send REKEY_RESPONSE packet (responder side). Sends the responder's new ephemeral public key to the peer.
Definition at line 395 of file crypto/handshake/common.c.
References ASCIICHAT_OK, crypto_handshake_context_t::crypto_ctx, crypto_handshake_is_ready(), CRYPTO_PUBLIC_KEY_SIZE, crypto_rekey_abort(), ERROR_INVALID_STATE, ERROR_NETWORK, crypto_context_t::has_temp_key, log_debug, log_info, PACKET_TYPE_CRYPTO_REKEY_RESPONSE, crypto_context_t::rekey_in_progress, send_packet(), SET_ERRNO, and crypto_context_t::temp_public_key.
Referenced by crypto_client_send_rekey_response().
| asciichat_error_t crypto_handshake_set_parameters | ( | crypto_handshake_context_t * | ctx, |
| const crypto_parameters_packet_t * | params | ||
| ) |
Set crypto parameters from crypto_parameters_packet_t.
| ctx | Handshake context |
| params | Negotiated crypto parameters (from capabilities negotiation) |
Definition at line 48 of file crypto/handshake/common.c.
References ARGON2ID_SALT_SIZE, ASCIICHAT_OK, AUTH_CHALLENGE_SIZE, crypto_context_t::auth_challenge_size, crypto_context_t::auth_public_key_size, crypto_parameters_packet_t::auth_public_key_size, crypto_handshake_context_t::crypto_ctx, crypto_context_t::encryption_key_size, ERROR_INVALID_PARAM, crypto_context_t::hmac_size, crypto_parameters_packet_t::hmac_size, crypto_handshake_context_t::is_server, crypto_parameters_packet_t::kex_public_key_size, log_debug, crypto_context_t::mac_size, crypto_parameters_packet_t::mac_size, NET_TO_HOST_U16, crypto_context_t::nonce_size, crypto_parameters_packet_t::nonce_size, crypto_context_t::private_key_size, crypto_context_t::public_key_size, crypto_context_t::salt_size, SET_ERRNO, crypto_context_t::shared_key_size, crypto_parameters_packet_t::shared_secret_size, crypto_context_t::signature_size, and crypto_parameters_packet_t::signature_size.
Referenced by client_crypto_handshake(), and server_crypto_handshake().
| bool crypto_handshake_should_rekey | ( | const crypto_handshake_context_t * | ctx | ) |
Check if rekeying should be triggered for this handshake context.
Check if rekeying should be triggered for this handshake context. Wrapper around crypto_should_rekey() for handshake context.
Definition at line 613 of file crypto/handshake/common.c.
References crypto_handshake_context_t::crypto_ctx, crypto_handshake_is_ready(), and crypto_should_rekey().
Referenced by client_send_thread_func(), and crypto_client_should_rekey().
| asciichat_error_t crypto_handshake_validate_packet_size | ( | const crypto_handshake_context_t * | ctx, |
| uint16_t | packet_type, | ||
| size_t | packet_size | ||
| ) |
Validate crypto packet size based on session parameters.
| ctx | Handshake context (must have parameters set) |
| packet_type | Packet type to validate |
| packet_size | Actual packet size received |
Definition at line 92 of file crypto/handshake/common.c.
References ASCIICHAT_OK, AUTH_CHALLENGE_FLAGS_SIZE, crypto_context_t::auth_challenge_size, crypto_context_t::auth_public_key_size, crypto_handshake_context_t::crypto_ctx, ED25519_PUBLIC_KEY_SIZE, ED25519_SIGNATURE_SIZE, ERROR_INVALID_PARAM, ERROR_NETWORK_PROTOCOL, crypto_context_t::hmac_size, MAX_AUTH_FAILED_PACKET_SIZE, MAX_ENCRYPTED_PACKET_SIZE, PACKET_TYPE_CRYPTO_AUTH_CHALLENGE, PACKET_TYPE_CRYPTO_AUTH_FAILED, PACKET_TYPE_CRYPTO_AUTH_RESPONSE, PACKET_TYPE_CRYPTO_CAPABILITIES, PACKET_TYPE_CRYPTO_HANDSHAKE_COMPLETE, PACKET_TYPE_CRYPTO_KEY_EXCHANGE_INIT, PACKET_TYPE_CRYPTO_KEY_EXCHANGE_RESP, PACKET_TYPE_CRYPTO_NO_ENCRYPTION, PACKET_TYPE_CRYPTO_PARAMETERS, PACKET_TYPE_CRYPTO_SERVER_AUTH_RESP, PACKET_TYPE_ENCRYPTED, crypto_context_t::public_key_size, SET_ERRNO, and crypto_context_t::signature_size.
Referenced by crypto_handshake_client_auth_response(), crypto_handshake_client_key_exchange(), crypto_handshake_server_auth_challenge(), and crypto_handshake_server_complete().