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

Added function to return a string representation of the next handshake action #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion include/noise/protocol/handshakestate.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ int noise_handshakestate_split
(NoiseHandshakeState *state, NoiseCipherState **send, NoiseCipherState **receive);
int noise_handshakestate_get_handshake_hash
(const NoiseHandshakeState *state, uint8_t *hash, size_t max_len);

int noise_handshakestate_get_action_pattern
(const NoiseHandshakeState *state, char *pattern, size_t max_len);
#ifdef __cplusplus
};
#endif
Expand Down
95 changes: 95 additions & 0 deletions src/protocol/handshakestate.c
Original file line number Diff line number Diff line change
Expand Up @@ -1779,4 +1779,99 @@ int noise_handshakestate_get_handshake_hash
return NOISE_ERROR_NONE;
}


int noise_handshakestate_get_action_pattern
(const NoiseHandshakeState *state, char *pattern, size_t max_len)
{
uint8_t token = 0;
int err = NOISE_ERROR_NONE;
int p = 0;
int t = 0;

for (;;) {
token = *(state->tokens + t);
if (token == NOISE_TOKEN_END) {
break;
} else if (token == NOISE_TOKEN_FLIP_DIR) {
break;
}
if (p != 0) {
if (max_len - p > 1) {
pattern[p++] = ',';
}
}

/* Process the token */
switch (token) {
case NOISE_TOKEN_E:
if (max_len - p > 1) {
pattern[p++] = 'e';
}
break;
case NOISE_TOKEN_S:
if (max_len - p > 1) {
pattern[p++] = 's';
}
break;
case NOISE_TOKEN_EE:
if (max_len - p > 1) {
pattern[p++] = 'e';
}
if (max_len - p > 1) {
pattern[p++] = 'e';
}
break;
case NOISE_TOKEN_ES:
if (max_len - p > 1) {
pattern[p++] = 'e';
}
if (max_len - p > 1) {
pattern[p++] = 's';
}
break;
case NOISE_TOKEN_SE:
if (max_len - p > 1) {
pattern[p++] = 's';
}
if (max_len - p > 1) {
pattern[p++] = 'e';
}
break;
case NOISE_TOKEN_SS:
if (max_len - p > 1) {
pattern[p++] = 's';
}
if (max_len - p > 1) {
pattern[p++] = 's';
}
break;
case NOISE_TOKEN_F:
if (max_len - p > 1) {
pattern[p++] = 'f';
}
break;
case NOISE_TOKEN_FF:
if (max_len - p > 1) {
pattern[p++] = 'f';
}
if (max_len - p > 1) {
pattern[p++] = 'f';
}
break;
default:
err = NOISE_ERROR_INVALID_STATE;
break;
}
if (err != NOISE_ERROR_NONE) {
return err;
}
t++;
}

if (max_len - p > 0) {
pattern[p++] = '\0';
}
return err;
}

/**@}*/