60 static int noise_handshakestate_requirements
61 (NoisePatternFlags_t flags,
int prefix_id,
int role,
int is_fallback)
102 static int noise_handshakestate_new
105 const uint8_t *pattern;
108 NoisePatternFlags_t flags;
120 flags = ((NoisePatternFlags_t)(pattern[0])) |
121 (((NoisePatternFlags_t)(pattern[1])) << 8);
122 if ((flags & NOISE_PAT_FLAG_REMOTE_REQUIRED) != 0)
158 (*state)->requirements = extra_reqs | noise_handshakestate_requirements
161 (*state)->tokens = pattern + 2;
162 (*state)->role =
role;
163 (*state)->symmetric = symmetric;
169 if ((flags & NOISE_PAT_FLAG_LOCAL_STATIC) != 0) {
211 if ((*state)->dh_local_static && (*state)->dh_local_static->ephemeral_only)
213 if ((*state)->dh_remote_static && (*state)->dh_remote_static->ephemeral_only)
215 if ((*state)->dh_local_ephemeral && (*state)->dh_local_ephemeral->ephemeral_only) {
216 if (!((*state)->dh_remote_ephemeral))
218 }
else if ((*state)->dh_remote_ephemeral && (*state)->dh_remote_ephemeral->ephemeral_only) {
277 return noise_handshakestate_new(state, symmetric, role);
323 return noise_handshakestate_new(state, symmetric, role);
378 return state ? state->
role : 0;
622 if (!state || !prologue)
629 memcpy(state->
prologue, prologue, prologue_len);
633 state->
prologue = (uint8_t *)malloc(prologue_len);
638 memcpy(state->
prologue, prologue, prologue_len);
748 static void noise_handshakestate_mix_public_key
763 static void noise_handshakestate_mix_chaining_key
851 noise_handshakestate_mix_public_key
855 noise_handshakestate_mix_chaining_key
867 noise_handshakestate_mix_public_key
871 noise_handshakestate_mix_chaining_key
979 const uint8_t *pattern;
980 NoisePatternFlags_t flags;
996 flags = ((NoisePatternFlags_t)(pattern[0])) |
997 (((NoisePatternFlags_t)(pattern[1])) << 8);
998 if ((flags & NOISE_PAT_FLAG_REMOTE_EPHEM_REQ) == 0)
1043 if (!(flags & NOISE_PAT_FLAG_REMOTE_REQUIRED))
1049 state->
tokens = pattern + 2;
1056 state->
requirements = noise_handshakestate_requirements
1057 (flags,
id.prefix_id, state->
role, 1);
1061 name_len = strlen(name);
1063 if (name_len <= hash_len) {
1065 memset(state->
symmetric->
h + name_len, 0, hash_len - name_len);
1128 static int noise_handshake_mix_dh
1133 uint8_t *shared = alloca(len);
1151 static int noise_handshakestate_write
1162 token = *(state->
tokens);
1226 if (rest.
max_size < (len + mac_len))
1236 err = noise_handshake_mix_dh
1242 err = noise_handshake_mix_dh
1245 err = noise_handshake_mix_dh
1252 err = noise_handshake_mix_dh
1255 err = noise_handshake_mix_dh
1261 err = noise_handshake_mix_dh
1293 if (rest.
max_size < (len + mac_len))
1303 err = noise_handshake_mix_dh
1388 if (!state || !(message->
data))
1390 if (payload && !(payload->
data))
1396 err = noise_handshakestate_write(state, message, payload);
1419 static int noise_handshakestate_read
1435 token = *(state->
tokens);
1508 err = noise_handshake_mix_dh
1514 err = noise_handshake_mix_dh
1517 err = noise_handshake_mix_dh
1524 err = noise_handshake_mix_dh
1527 err = noise_handshake_mix_dh
1533 err = noise_handshake_mix_dh
1576 err = noise_handshake_mix_dh
1646 if (!(payload->
data))
1650 if (!state || !message || !(message->
data))
1658 err = noise_handshakestate_read(state, message, payload);
1710 if (!send && !receive)
1765 if (!state || !hash)
1773 if (hash_len <= max_len) {
1775 memset(hash + hash_len, 0, max_len - hash_len);
#define NOISE_PSK_LEN
Standard length for pre-shared keys.
#define NOISE_PAT_FLAG_LOCAL_EPHEMERAL
Noise protocol name broken out into separate identifier fields.
int noise_handshakestate_has_pre_shared_key(const NoiseHandshakeState *state)
Determine if a HandshakeState object has already been configured with a pre shared key...
#define NOISE_ERROR_UNKNOWN_ID
Algorithm identifier is unknown.
uint16_t hash_len
Length of the output from this hash algorithm.
#define NOISE_ACTION_NONE
No action is required of the application yet because the handshake has not started.
int noise_dhstate_clear_key(NoiseDHState *state)
Clears the key in a DHState object.
void noise_clean(void *data, size_t size)
Cleans a block of memory to destroy its contents.
#define NOISE_ERROR_INVALID_STATE
Operation cannot be performed in the current state.
uint8_t ck[NOISE_MAX_HASHLEN]
Current value of the chaining key for the handshake.
NoiseHashState * hash
Points to the HashState object for this SymmetricState.
#define NOISE_ERROR_INVALID_PARAM
Invalid parameter to function; e.g. a NULL value.
uint8_t * public_key
Points to the public key in the subclass state.
Internal structure of the NoiseHashState type.
NoiseDHState * dh_fixed_ephemeral
Points to the object for the fixed ephemeral test key.
#define NOISE_ACTION_COMPLETE
The HandshakeState has flagged that the handshake is over and and that noise_handshake_split() has be...
NoiseProtocolId id
Algorithm identifiers for the components of the protocol.
#define NOISE_PAT_FLAG_LOCAL_REQUIRED
#define NOISE_ERROR_NONE
Success, no error.
#define NOISE_KEY_TYPE_NO_KEY
int noise_handshakestate_get_role(const NoiseHandshakeState *state)
Gets the role that a HandshakeState object is playing.
int noise_handshakestate_get_protocol_id(const NoiseHandshakeState *state, NoiseProtocolId *id)
Gets the protocol identifier associated with a HandshakeState object.
const uint8_t * tokens
Points to the next message pattern tokens to be processed.
uint8_t has_key
Non-zero if the key has been set on this cipher.
size_t prologue_len
Length of the prologue value in bytes.
size_t size
Total size of the structure, including DH key storage.
int noise_handshakestate_read_message(NoiseHandshakeState *state, NoiseBuffer *message, NoiseBuffer *payload)
Reads a message payload using a HandshakeState.
#define NOISE_REQ_REMOTE_PREMSG
int noise_dhstate_get_role(const NoiseDHState *state)
Gets the role that a DHState object will play in a handshake.
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.
int noise_handshakestate_fallback(NoiseHandshakeState *state)
Falls back to the "XXfallback" handshake pattern.
int noise_handshakestate_free(NoiseHandshakeState *state)
Frees a HandshakeState object after destroying all sensitive material.
int noise_handshakestate_new_by_id(NoiseHandshakeState **state, const NoiseProtocolId *protocol_id, int role)
Creates a new HandshakeState object by protocol identifier.
#define NOISE_REQ_LOCAL_PREMSG
int noise_handshakestate_needs_pre_shared_key(const NoiseHandshakeState *state)
Determine if a HandshakeState object requires a pre shared key.
int noise_symmetricstate_mix_key(NoiseSymmetricState *state, const uint8_t *input, size_t size)
Mixes new input data into the chaining key.
int noise_dhstate_new_by_id(NoiseDHState **state, int id)
Creates a new DHState object by its algorithm identifier.
NoiseDHState * dh_remote_ephemeral
Points to the DHState object for remote ephemeral key.
NoiseDHState * dh_local_ephemeral
Points to the DHState object for local ephemeral key.
uint8_t h[NOISE_MAX_HASHLEN]
Current value of the handshake hash.
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.
#define NOISE_MAX_HASHLEN
Maximum hash length over all supported hash algorithms.
#define NOISE_PAT_FLAG_LOCAL_STATIC
int noise_handshakestate_write_message(NoiseHandshakeState *state, NoiseBuffer *message, const NoiseBuffer *payload)
Writes a message payload using a HandshakeState.
int noise_hashstate_hash_one(NoiseHashState *state, const uint8_t *data, size_t data_len, uint8_t *hash, size_t hash_len)
Hashes a single data buffer and returns the hash value.
#define NOISE_PAT_FLAG_REMOTE_STATIC
NoiseSymmetricState * symmetric
Points to the SymmetricState object for this HandshakeState.
uint16_t shared_key_len
Length of the shared key for this algorithm in bytes.
#define NOISE_ACTION_SPLIT
The HandshakeState has flagged that the handshake is over and that the application is expected to cal...
#define NOISE_PAT_FLAG_REMOTE_HYBRID
#define NOISE_ERROR_INVALID_PUBLIC_KEY
Invalid public key value.
uint8_t pre_shared_key[NOISE_PSK_LEN]
Pre-shared key value.
Internal structure of the NoiseDHState type.
NoiseDHState * dh_remote_hybrid
Points to the DHState object for remote hybrid forward secrecy key.
#define NOISE_PAT_FLAG_REMOTE_EPHEMERAL
NoiseDHState * noise_handshakestate_get_fixed_ephemeral_dh(NoiseHandshakeState *state)
Gets the DHState object that contains the local ephemeral keypair.
#define NOISE_ERROR_NO_MEMORY
Insufficient memory to complete the operation.
#define NOISE_PAT_FLAG_REMOTE_EPHEM_REQ
NoiseDHState * noise_handshakestate_get_local_keypair_dh(const NoiseHandshakeState *state)
Gets the DHState object that contains the local static keypair.
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 protoc...
#define NOISE_PAT_FLAG_REMOTE_REQUIRED
#define NOISE_MAX_PROTOCOL_NAME
Maximum length of a protocol name string.
int action
Next action to be taken by the application.
uint64_t n
The nonce value for the next packet.
#define NOISE_ERROR_INVALID_LENGTH
Invalid length specified for a key, packet, etc.
int noise_handshakestate_set_prologue(NoiseHandshakeState *state, const void *prologue, size_t prologue_len)
Sets the prologue for a HandshakeState.
uint8_t key_type
The type of key stored within this DHState object.
#define NOISE_ACTION_FAILED
The HandshakeState has flagged that the handshake has failed due to some kind of error.
int noise_handshakestate_get_action(const NoiseHandshakeState *state)
Gets the next action the application should perform for the handshake phase of the protocol...
NoiseCipherState * cipher
Points to the CipherState object for this SymmetricState.
int noise_symmetricstate_free(NoiseSymmetricState *state)
Frees a SymmetricState object after destroying all sensitive material.
int noise_handshakestate_split(NoiseHandshakeState *state, NoiseCipherState **send, NoiseCipherState **receive)
Splits the transport encryption CipherState objects out of this HandshakeState object.
int noise_dhstate_has_keypair(const NoiseDHState *state)
Determine if a DHState object contains a keypair.
#define NOISE_ROLE_INITIATOR
This end of the communications is the initiator.
int role
The role of this object, initiator or responder.
#define NOISE_TOKEN_FLIP_DIR
int noise_dhstate_calculate(const NoiseDHState *private_key_state, const NoiseDHState *public_key_state, uint8_t *shared_key, size_t shared_key_len)
Performs a Diffie-Hellman calculation.
#define NOISE_ERROR_LOCAL_KEY_REQUIRED
A local keypair is required for the selected protocol, but one was not supplied.
Internal structure of the NoiseSymmetricState type.
int noise_hashstate_hkdf(NoiseHashState *state, const uint8_t *key, size_t key_len, const uint8_t *data, size_t data_len, uint8_t *output1, size_t output1_len, uint8_t *output2, size_t output2_len)
Hashes input data with a key to generate two output values.
size_t noise_symmetricstate_get_mac_length(const NoiseSymmetricState *state)
Gets the current length of packet MAC values for a SymmetricState object.
int noise_symmetricstate_new_by_id(NoiseSymmetricState **state, const NoiseProtocolId *id)
Creates a new SymmetricState object from a protocol identifier.
NoiseDHState * noise_handshakestate_get_remote_public_key_dh(const NoiseHandshakeState *state)
Gets the DHState object that contains the remote static public key.
int noise_symmetricstate_mix_hash(NoiseSymmetricState *state, const uint8_t *input, size_t size)
Mixes new input data into the handshake hash.
#define NOISE_ERROR_NOT_APPLICABLE
An option was supplied that was not applicable to the selected protocol.
int requirements
Requirements that are yet to be satisfied.
uint8_t * prologue
Points to the prologue value.
Internal definitions for the library.
#define NOISE_ROLE_RESPONDER
This end of the communications is the responder.
int noise_dhstate_free(NoiseDHState *state)
Frees a DHState object after destroying all sensitive material.
int noise_symmetricstate_encrypt_and_hash(NoiseSymmetricState *state, NoiseBuffer *buffer)
Encrypts a block of data with this SymmetricState object and adds the ciphertext to the handshake has...
#define NOISE_REQ_FALLBACK_POSSIBLE
void noise_free(void *ptr, size_t size)
Destroys the contents of a block of memory and free it.
int noise_handshakestate_needs_local_keypair(const NoiseHandshakeState *state)
Determine if a HandshakeState still needs to be configured with a local keypair.
#define NOISE_DH_NONE
Diffie-Hellman identifier that indicates "no DH".
size_t pre_shared_key_len
Length of the pre-shared key value: zero or NOISE_PSK_LEN only.
Internal structure of the NoiseCipherState type.
#define noise_new(type)
Allocates an object from the system and initializes it.
#define NOISE_ERROR_REMOTE_KEY_REQUIRED
A remote static public key is required for the selected protocol, but one was not supplied...
#define NOISE_PATTERN_NN
Handshake pattern identifier for "NN".
#define NOISE_REQ_REMOTE_REQUIRED
NoisePatternFlags_t noise_pattern_reverse_flags(NoisePatternFlags_t flags)
Reverses the local and remote flags for a pattern.
int(* copy)(NoiseDHState *state, const NoiseDHState *from, const NoiseDHState *other)
Copies another key into this object.
int noise_dhstate_has_public_key(const NoiseDHState *state)
Determine if a DHState object contains a public key.
int noise_handshakestate_fallback_to(NoiseHandshakeState *state, int pattern_id)
Falls back to another handshake pattern.
NoiseDHState * noise_handshakestate_get_fixed_hybrid_dh(NoiseHandshakeState *state)
Gets the DHState object that contains the local additional hybrid secrecy keypair.
#define NOISE_PATTERN_XX_FALLBACK
Handshake pattern identifier for "XXfallback".
const uint8_t * noise_pattern_lookup(int id)
Looks up a specific handshake pattern.
int noise_handshakestate_has_remote_public_key(const NoiseHandshakeState *state)
Determine if a HandshakeState has a remote public key.
int noise_dhstate_is_null_public_key(const NoiseDHState *state)
Determine if the public key in a DHState object has the special null value.
int noise_handshakestate_new_by_name(NoiseHandshakeState **state, const char *protocol_name, int role)
Creates a new HandshakeState object by protocol name.
#define NOISE_ACTION_READ_MESSAGE
The HandshakeState expects the application to read the next message payload from the handshake...
size_t noise_hashstate_get_hash_length(const NoiseHashState *state)
Gets the length of the hash output for a HashState object.
#define NOISE_PREFIX_PSK
Protocol prefix identifier for "NoisePSK".
#define NOISE_REQ_FALLBACK_PREMSG
#define NOISE_PAT_FLAG_LOCAL_HYBRID
int noise_protocol_id_to_name(char *name, size_t name_len, const NoiseProtocolId *id)
Formats a protocol name from a set of identifiers for the algorithms that make up the name...
int noise_handshakestate_start(NoiseHandshakeState *state)
Starts the handshake on a HandshakeState object.
#define NOISE_REQ_LOCAL_REQUIRED
NoiseDHState * dh_local_hybrid
Points to the DHState object for local hybrid forward secrecy key.
short dh_id
Algorithm identifier for the Diffie-Hellman operation.
int noise_dhstate_set_public_key(NoiseDHState *state, const uint8_t *public_key, size_t public_key_len)
Sets the public key in a DHState object.
int noise_dhstate_set_role(NoiseDHState *state, int role)
Sets the role that a DHState object will play in a handshake.
#define NOISE_PAT_FLAG_LOCAL_EPHEM_REQ
#define NOISE_ERROR_PSK_REQUIRED
A pre shared key is required for the selected protocol, but one was not supplied. ...
Internal structure of the NoiseHandshakeState type.
NoiseDHState * dh_remote_static
Points to the DHState object for remote static key.
int noise_symmetricstate_split(NoiseSymmetricState *state, NoiseCipherState **c1, NoiseCipherState **c2)
Splits the transport encryption CipherState objects out of this SymmetricState object.
int noise_dhstate_generate_dependent_keypair(NoiseDHState *state, const NoiseDHState *other)
Generates a new key pair within a DHState object that may depend upon the parameters from another DHS...
int noise_symmetricstate_new_by_name(NoiseSymmetricState **state, const char *name)
Creates a new SymmetricState object from a protocol name.
uint8_t * private_key
Points to the private key in the subclass state.
short role
The role; either initiator or responder or zero.
int noise_symmetricstate_decrypt_and_hash(NoiseSymmetricState *state, NoiseBuffer *buffer)
Decrypts a block of data with this SymmetricState object and adds the ciphertext to the handshake has...
Type that defines a region of memory for a data buffer.
NoiseDHState * dh_local_static
Points to the DHState object for local static key.
int noise_handshakestate_has_local_keypair(const NoiseHandshakeState *state)
Determine if a HandshakeState has been configured with a local keypair.
NoiseDHState * dh_fixed_hybrid
Points to the object for the fixed hybrid forward secrecy test key.
uint16_t public_key_len
Length of the public key for this algorithm in bytes.
#define NOISE_ACTION_WRITE_MESSAGE
The HandshakeState expects the application to write the next message payload for the handshake...