Noise-C
|
Typedefs | |
typedef struct NoiseHandshakeState_s | NoiseHandshakeState |
Opaque object that represents a HandshakeState. More... | |
Functions | |
int | noise_handshakestate_fallback (NoiseHandshakeState *state) |
Falls back to the "XXfallback" handshake pattern. More... | |
int | noise_handshakestate_fallback_to (NoiseHandshakeState *state, int pattern_id) |
Falls back to another handshake pattern. More... | |
int | noise_handshakestate_free (NoiseHandshakeState *state) |
Frees a HandshakeState object after destroying all sensitive material. More... | |
int | noise_handshakestate_get_action (const NoiseHandshakeState *state) |
Gets the next action the application should perform for the handshake phase of the protocol. More... | |
NoiseDHState * | noise_handshakestate_get_fixed_ephemeral_dh (NoiseHandshakeState *state) |
Gets the DHState object that contains the local ephemeral keypair. More... | |
NoiseDHState * | noise_handshakestate_get_fixed_hybrid_dh (NoiseHandshakeState *state) |
Gets the DHState object that contains the local additional hybrid secrecy keypair. More... | |
int | noise_handshakestate_get_handshake_hash (const NoiseHandshakeState *state, uint8_t *hash, size_t max_len) |
Gets the handshake hash value once the handshake ends. More... | |
NoiseDHState * | noise_handshakestate_get_local_keypair_dh (const NoiseHandshakeState *state) |
Gets the DHState object that contains the local static keypair. More... | |
int | noise_handshakestate_get_protocol_id (const NoiseHandshakeState *state, NoiseProtocolId *id) |
Gets the protocol identifier associated with a HandshakeState object. More... | |
NoiseDHState * | noise_handshakestate_get_remote_public_key_dh (const NoiseHandshakeState *state) |
Gets the DHState object that contains the remote static public key. More... | |
int | noise_handshakestate_get_role (const NoiseHandshakeState *state) |
Gets the role that a HandshakeState object is playing. More... | |
int | noise_handshakestate_has_local_keypair (const NoiseHandshakeState *state) |
Determine if a HandshakeState has been configured with a local keypair. More... | |
int | noise_handshakestate_has_pre_shared_key (const NoiseHandshakeState *state) |
Determine if a HandshakeState object has already been configured with a pre shared key. More... | |
int | noise_handshakestate_has_remote_public_key (const NoiseHandshakeState *state) |
Determine if a HandshakeState has a remote public key. More... | |
int | noise_handshakestate_needs_local_keypair (const NoiseHandshakeState *state) |
Determine if a HandshakeState still needs to be configured with a local keypair. More... | |
int | noise_handshakestate_needs_pre_shared_key (const NoiseHandshakeState *state) |
Determine if a HandshakeState object requires a pre shared key. More... | |
int | noise_handshakestate_needs_remote_public_key (const NoiseHandshakeState *state) |
Determine if a HandshakeState still needs to be configured with a remote public key before the protocol can start. More... | |
int | noise_handshakestate_new_by_id (NoiseHandshakeState **state, const NoiseProtocolId *protocol_id, int role) |
Creates a new HandshakeState object by protocol identifier. More... | |
int | noise_handshakestate_new_by_name (NoiseHandshakeState **state, const char *protocol_name, int role) |
Creates a new HandshakeState object by protocol name. More... | |
int | noise_handshakestate_read_message (NoiseHandshakeState *state, NoiseBuffer *message, NoiseBuffer *payload) |
Reads a message payload using a HandshakeState. More... | |
int | noise_handshakestate_set_pre_shared_key (NoiseHandshakeState *state, const uint8_t *key, size_t key_len) |
Sets the pre shared key for a HandshakeState. More... | |
int | noise_handshakestate_set_prologue (NoiseHandshakeState *state, const void *prologue, size_t prologue_len) |
Sets the prologue for a HandshakeState. More... | |
int | noise_handshakestate_split (NoiseHandshakeState *state, NoiseCipherState **send, NoiseCipherState **receive) |
Splits the transport encryption CipherState objects out of this HandshakeState object. More... | |
int | noise_handshakestate_start (NoiseHandshakeState *state) |
Starts the handshake on a HandshakeState object. More... | |
int | noise_handshakestate_write_message (NoiseHandshakeState *state, NoiseBuffer *message, const NoiseBuffer *payload) |
Writes a message payload using a HandshakeState. More... | |
See the echo example for an overview of how to use this API.
Opaque object that represents a HandshakeState.
Definition at line 33 of file handshakestate.h.
int noise_handshakestate_fallback | ( | NoiseHandshakeState * | state | ) |
Falls back to the "XXfallback" handshake pattern.
state | The HandshakeState object. |
This function is intended used to help implement the "Noise Pipes" protocol. It resets a HandshakeState object with the original handshake pattern (usually "IK"), converting it into an object with the handshake pattern "XXfallback". Information from the previous session such as the local keypair, the initiator's ephemeral key, the prologue value, and the pre-shared key, are passed to the new session.
Once the fallback has been initiated, the application can set new values for the handshake parameters if the values from the previous session do not apply. For example, the application may use a different prologue for the fallback than for the original session.
After setting any new parameters, the application calls noise_handshakestate_start() again to restart the handshake from where it left off before the fallback.
Definition at line 921 of file handshakestate.c.
int noise_handshakestate_fallback_to | ( | NoiseHandshakeState * | state, |
int | pattern_id | ||
) |
Falls back to another handshake pattern.
state | The HandshakeState object. |
pattern_id | The identifier for the pattern to fallback to; e.g. NOISE_PATTERN_XX_FALLBACK, NOISE_PATTERN_NX_FALLBACK, etc. |
This function is a generalization of the "Noise Pipes" protocol, allowing for other combinations of patterns to be used for the full handshake, abbreviated handshake, and fallback handshake. For example, "NX/NK/NXfallback", "XX/XK/XXfallback", etc.
This function resets a HandshakeState object with the original handshake pattern, and converts it into an object with the new handshake pattern_id. Information from the previous session such as the local keypair, the initiator's ephemeral key, the prologue value, and the pre-shared key, are passed to the new session.
Once the fallback has been initiated, the application can set new values for the handshake parameters if the values from the previous session do not apply. For example, the application may use a different prologue for the fallback than for the original session.
After setting any new parameters, the application calls noise_handshakestate_start() again to restart the handshake from where it left off before the fallback.
The new pattern may have greater key requirements than the original; for example changing from "NK" from "XXfallback" requires that the initiator's static public key be set. The application is responsible for setting any extra keys before calling noise_handshakestate_start().
Definition at line 973 of file handshakestate.c.
int noise_handshakestate_free | ( | NoiseHandshakeState * | state | ) |
Frees a HandshakeState object after destroying all sensitive material.
state | The HandshakeState object to free. |
Definition at line 336 of file handshakestate.c.
int noise_handshakestate_get_action | ( | const NoiseHandshakeState * | state | ) |
Gets the next action the application should perform for the handshake phase of the protocol.
state | The HandshakeState object. |
Definition at line 1112 of file handshakestate.c.
NoiseDHState* noise_handshakestate_get_fixed_ephemeral_dh | ( | NoiseHandshakeState * | state | ) |
Gets the DHState object that contains the local ephemeral keypair.
state | The HandshakeState object. |
Definition at line 459 of file handshakestate.c.
NoiseDHState* noise_handshakestate_get_fixed_hybrid_dh | ( | NoiseHandshakeState * | state | ) |
Gets the DHState object that contains the local additional hybrid secrecy keypair.
state | The HandshakeState object. |
Definition at line 495 of file handshakestate.c.
int noise_handshakestate_get_handshake_hash | ( | const NoiseHandshakeState * | state, |
uint8_t * | hash, | ||
size_t | max_len | ||
) |
Gets the handshake hash value once the handshake ends.
state | The HandshakeState object. |
hash | The buffer to receive the handshake hash value. |
max_len | The maximum length of the hash buffer. |
If max_len is greater than the length of the handshake hash, then the extra bytes will be filled with zeroes. If max_len is less than the length of the handshake hash, then the value will be truncated to the first max_len bytes. Handshake hashes are typically 32 or 64 bytes in length, depending upon the hash algorithm that was used during the protocol.
The handshake hash can be used to implement "channel binding". The value will be a unique identifier for the session.
Definition at line 1760 of file handshakestate.c.
NoiseDHState* noise_handshakestate_get_local_keypair_dh | ( | const NoiseHandshakeState * | state | ) |
Gets the DHState object that contains the local static keypair.
state | The HandshakeState object. |
The application uses the returned object to set the static keypair for the local end of the handshake if one is required.
Definition at line 418 of file handshakestate.c.
int noise_handshakestate_get_protocol_id | ( | const NoiseHandshakeState * | state, |
NoiseProtocolId * | id | ||
) |
Gets the protocol identifier associated with a HandshakeState object.
state | The HandshakeState object. |
id | Return buffer for the protocol identifier, which consists of fields that identify the cipher algorithm, hash algorith, handshake pattern, etc. |
Definition at line 393 of file handshakestate.c.
NoiseDHState* noise_handshakestate_get_remote_public_key_dh | ( | const NoiseHandshakeState * | state | ) |
Gets the DHState object that contains the remote static public key.
state | The HandshakeState object. |
The application uses the returned object to set the public key for the remote end of the handshake if the key must be provided prior to the handshake. The returned object can also be used to obtain the public key value that was transmitted by the remote party during the handshake.
Definition at line 439 of file handshakestate.c.
int noise_handshakestate_get_role | ( | const NoiseHandshakeState * | state | ) |
Gets the role that a HandshakeState object is playing.
state | The HandshakeState object. |
Definition at line 376 of file handshakestate.c.
int noise_handshakestate_has_local_keypair | ( | const NoiseHandshakeState * | state | ) |
Determine if a HandshakeState has been configured with a local keypair.
state | The HandshakeState object. |
Definition at line 685 of file handshakestate.c.
int noise_handshakestate_has_pre_shared_key | ( | const NoiseHandshakeState * | state | ) |
Determine if a HandshakeState object has already been configured with a pre shared key.
state | The HandshakeState object. |
Definition at line 550 of file handshakestate.c.
int noise_handshakestate_has_remote_public_key | ( | const NoiseHandshakeState * | state | ) |
Determine if a HandshakeState has a remote public key.
state | The HandshakeState object. |
A remote public key may either be provided ahead of time on the noise_handshakestate_get_remote_public_key_dh() object, or it may be provided by the remote party during the handshake.
Definition at line 735 of file handshakestate.c.
int noise_handshakestate_needs_local_keypair | ( | const NoiseHandshakeState * | state | ) |
Determine if a HandshakeState still needs to be configured with a local keypair.
state | The HandshakeState object. |
The application configures the local keypair on the object returned by noise_handshakestate_get_local_keypair_dh().
Definition at line 663 of file handshakestate.c.
int noise_handshakestate_needs_pre_shared_key | ( | const NoiseHandshakeState * | state | ) |
Determine if a HandshakeState object requires a pre shared key.
state | The HandshakeState object. |
Definition at line 531 of file handshakestate.c.
int noise_handshakestate_needs_remote_public_key | ( | const NoiseHandshakeState * | state | ) |
Determine if a HandshakeState still needs to be configured with a remote public key before the protocol can start.
state | The HandshakeState object. |
This function indicates that a remote public key must be supplied before the protocol starts. If it is possible for the remote public key to be provided by the remote party during the session, then the remote public key can be obtained at the end of the handshake using the noise_handshakestate_get_remote_public_key_dh() object.
Definition at line 711 of file handshakestate.c.
int noise_handshakestate_new_by_id | ( | NoiseHandshakeState ** | state, |
const NoiseProtocolId * | protocol_id, | ||
int | role | ||
) |
Creates a new HandshakeState object by protocol identifier.
state | Points to the variable where to store the pointer to the new HandshakeState object. |
protocol_id | The protocol identifier as a set of algorithm identifiers. |
role | The role for the new object, either NOISE_ROLE_INITIATOR or NOISE_ROLE_RESPONDER. |
Definition at line 257 of file handshakestate.c.
int noise_handshakestate_new_by_name | ( | NoiseHandshakeState ** | state, |
const char * | protocol_name, | ||
int | role | ||
) |
Creates a new HandshakeState object by protocol name.
state | Points to the variable where to store the pointer to the new HandshakeState object. |
protocol_name | The name of the Noise protocol to use. This string must be NUL-terminated. |
role | The role for the new object, either NOISE_ROLE_INITIATOR or NOISE_ROLE_RESPONDER. |
Definition at line 303 of file handshakestate.c.
int noise_handshakestate_read_message | ( | NoiseHandshakeState * | state, |
NoiseBuffer * | message, | ||
NoiseBuffer * | payload | ||
) |
Reads a message payload using a HandshakeState.
state | The HandshakeState object. |
message | Points to the incoming handshake message to be unpacked. |
payload | Points to the buffer to fill with the message payload. This can be NULL if the application does not need the message payload. |
If payload is NULL, then the message payload will be authenticated and then discarded, regardless of its length. If the application was expecting an empty payload and wants to verify that, then payload should point to a non-NULL zero-length buffer.
The mesaage and payload buffers must not overlap in memory.
The message buffer will be modified by this function to decrypt sub-components while it is being processed. The contents will be cleared just before the function exits to avoid leaking decrypted message data other than the payload.
Definition at line 1640 of file handshakestate.c.
int noise_handshakestate_set_pre_shared_key | ( | NoiseHandshakeState * | state, |
const uint8_t * | key, | ||
size_t | key_len | ||
) |
Sets the pre shared key for a HandshakeState.
state | The HandshakeState object. |
key | Points to the pre shared key. |
key_len | The length of the key in bytes. This must be 32 to comply with the requirements from the Noise protocol specification. |
Definition at line 579 of file handshakestate.c.
int noise_handshakestate_set_prologue | ( | NoiseHandshakeState * | state, |
const void * | prologue, | ||
size_t | prologue_len | ||
) |
Sets the prologue for a HandshakeState.
state | The HandshakeState object. |
prologue | Points to the prologue value. |
prologue_len | The length of the prologue value in bytes. |
This function should be called immediately after noise_handshakestate_new_by_id() or noise_handshakestate_new_by_name() if there is a prologue for the session. If the function is not called, then the prologue will be assumed to be empty when the protocol starts.
Definition at line 619 of file handshakestate.c.
int noise_handshakestate_split | ( | NoiseHandshakeState * | state, |
NoiseCipherState ** | send, | ||
NoiseCipherState ** | receive | ||
) |
Splits the transport encryption CipherState objects out of this HandshakeState object.
state | The HandshakeState object. |
send | Points to the variable where to place the pointer to the CipherState object to use to send packets from local to remote. This can be NULL if the application is using a one-way handshake pattern. |
receive | Points to the variable where to place the pointer to the CipherState object to use to receive packets from the remote to local. This can be NULL if the application is using a one-way handshake pattern. |
Once a HandshakeState has been split, it is effectively finished and cannot be used for future handshake operations. If those operations are invoked, the relevant functions will return NOISE_ERROR_INVALID_STATE.
The send object should be used to protect messages from the local side to the remote side, and the receive object should be used to protect messages from the remote side to the local side.
If the handshake pattern is one-way, then the application should call noise_cipherstate_free() on the object that is not needed. Alternatively, the application can pass NULL to noise_handshakestate_split() as the send or receive argument and the second CipherState will not be created at all.
Definition at line 1702 of file handshakestate.c.
int noise_handshakestate_start | ( | NoiseHandshakeState * | state | ) |
Starts the handshake on a HandshakeState object.
state | The HandshakeState object. |
This function is called after all of the handshake parameters have been provided to the HandshakeState object. This function should be followed by calls to noise_handshake_write_message() or noise_handshake_read_message() to process the handshake messages. The noise_handshakestate_get_action() function indicates the action to take next.
Definition at line 800 of file handshakestate.c.
int noise_handshakestate_write_message | ( | NoiseHandshakeState * | state, |
NoiseBuffer * | message, | ||
const NoiseBuffer * | payload | ||
) |
Writes a message payload using a HandshakeState.
state | The HandshakeState object. |
message | Points to the message buffer to be populated with handshake details and the message payload. |
payload | Points to the message payload to be sent, which can be NULL if no payload is required. |
The message and payload buffers must not overlap in memory.
The following example demonstrates how to write a handshake message into the application's msgbuf
array:
Definition at line 1380 of file handshakestate.c.