SharkSSL C/C++ Reference
TLS, cryptography, certificates, and PSA integration
Password-authenticated key exchange

Detailed Description

The SharkSSL provider implements confirmed SPAKE2+ with P-256, SHA-256, HKDF and HMAC.

See SharkSSL PSA Crypto provider for credential provisioning. Initialize an operation, call psa_pake_setup(), set its role and optional identities/context, then exchange these messages in order. Each sender uses psa_pake_output(); the receiver consumes the same message with psa_pake_input().

Sender Step Bytes
Client PSA_PAKE_STEP_KEY_SHARE 65
Server PSA_PAKE_STEP_KEY_SHARE 65
Server PSA_PAKE_STEP_CONFIRM 32
Client PSA_PAKE_STEP_CONFIRM 32

After both confirmations, psa_pake_get_shared_key() returns a key handle and clears the operation. A failed operation call releases its allocated exchange state and marks it failed; call psa_pake_abort() before reusing it. Abort interrupted exchanges too. Suite accessors only store/read settings; psa_pake_setup() validates the suite. Do not share an active operation between concurrent calls or destroy its credential key before extraction or abort.

Data Structures

struct  psa_pake_cipher_suite_s
 Password-authenticated key-exchange suite, initialized with PSA_PAKE_CIPHER_SUITE_INIT or psa_pake_cipher_suite_init(). More...
 
struct  psa_pake_operation_s
 Opaque PAKE operation storage owned by the caller. More...
 

Typedefs

typedef struct psa_pake_cipher_suite_s psa_pake_cipher_suite_t
 Password-authenticated key-exchange suite, initialized with PSA_PAKE_CIPHER_SUITE_INIT or psa_pake_cipher_suite_init(). More...
 
typedef struct psa_pake_operation_s psa_pake_operation_t
 Opaque PAKE operation storage owned by the caller. More...
 

Functions

psa_pake_operation_t psa_pake_operation_init (void)
 Create an inactive PAKE operation. More...
 
psa_status_t psa_pake_setup (psa_pake_operation_t *operation, psa_key_id_t password_key, const psa_pake_cipher_suite_t *cipher_suite)
 Start a SPAKE2+ exchange using an existing credential key. More...
 
psa_status_t psa_pake_set_role (psa_pake_operation_t *operation, psa_pake_role_t role)
 Select the local SPAKE2+ role once, before exchanging messages. More...
 
psa_status_t psa_pake_set_user (psa_pake_operation_t *operation, const uint8_t *user_id, size_t user_id_len)
 Set the local identity once, before any exchange message. More...
 
psa_status_t psa_pake_set_peer (psa_pake_operation_t *operation, const uint8_t *peer_id, size_t peer_id_len)
 Set the peer identity once, before any exchange message. More...
 
psa_status_t psa_pake_set_context (psa_pake_operation_t *operation, const uint8_t *context, size_t context_length)
 Add an optional context to the exchange transcript before any message. More...
 
psa_status_t psa_pake_input (psa_pake_operation_t *operation, psa_pake_step_t step, const uint8_t *input, size_t input_length)
 Consume the next peer message in the SPAKE2+ exchange. More...
 
psa_status_t psa_pake_output (psa_pake_operation_t *operation, psa_pake_step_t step, uint8_t *output, size_t output_size, size_t *output_length)
 Produce the next local SPAKE2+ message. More...
 
psa_status_t psa_pake_get_shared_key (psa_pake_operation_t *operation, const psa_key_attributes_t *attributes, psa_key_id_t *key)
 Import the confirmed shared secret as a provider key and end the exchange. More...
 
psa_status_t psa_pake_abort (psa_pake_operation_t *operation)
 Release a PAKE operation and reset it to the inactive state. More...
 
psa_pake_cipher_suite_t psa_pake_cipher_suite_init (void)
 Create an unconfigured PAKE suite. More...
 
psa_algorithm_t psa_pake_cs_get_algorithm (const psa_pake_cipher_suite_t *cipher_suite)
 Read a suite's algorithm selector. More...
 
void psa_pake_cs_set_algorithm (psa_pake_cipher_suite_t *cipher_suite, psa_algorithm_t alg)
 Store a suite's algorithm selector without validating provider support. More...
 
psa_pake_primitive_t psa_pake_cs_get_primitive (const psa_pake_cipher_suite_t *cipher_suite)
 Read a suite's encoded primitive. More...
 
void psa_pake_cs_set_primitive (psa_pake_cipher_suite_t *cipher_suite, psa_pake_primitive_t primitive)
 Store a suite's primitive without validating provider support. More...
 
uint32_t psa_pake_cs_get_key_confirmation (const psa_pake_cipher_suite_t *cipher_suite)
 Read a suite's key-confirmation mode. More...
 
void psa_pake_cs_set_key_confirmation (psa_pake_cipher_suite_t *cipher_suite, uint32_t key_confirmation)
 Store a suite's key-confirmation mode without validation. More...
 

Typedef Documentation

◆ psa_pake_cipher_suite_t

Password-authenticated key-exchange suite, initialized with PSA_PAKE_CIPHER_SUITE_INIT or psa_pake_cipher_suite_init().

Use the suite accessors to configure it before psa_pake_setup().

◆ psa_pake_operation_t

Opaque PAKE operation storage owned by the caller.

Initialize with PSA_PAKE_OPERATION_INIT or psa_pake_operation_init(). Do not inspect members, copy, or move an active operation. The provider owns its allocated context and retains a dependency on the credential key. Keep the key available until successful shared-key extraction or psa_pake_abort().

Function Documentation

◆ psa_pake_abort()

psa_status_t psa_pake_abort ( psa_pake_operation_t *  operation)

Release a PAKE operation and reset it to the inactive state.

Frees allocated state, clears exchange secrets, and releases the credential dependency. Does not destroy the credential key. Safe after success, failure, or a previous abort.

Parameters
operationpsa_pake_operation_t *; required initialized operation.
Returns
psa_status_t; PSA_SUCCESS, or PSA_ERROR_INVALID_ARGUMENT for NULL.

◆ psa_pake_cipher_suite_init()

psa_pake_cipher_suite_t psa_pake_cipher_suite_init ( void  )

Create an unconfigured PAKE suite.

Returns
psa_pake_cipher_suite_t; algorithm PSA_ALG_NONE, primitive 0, and PSA_PAKE_CONFIRMED_KEY. Set algorithm and primitive before setup.

◆ psa_pake_cs_get_algorithm()

psa_algorithm_t psa_pake_cs_get_algorithm ( const psa_pake_cipher_suite_t *  cipher_suite)

Read a suite's algorithm selector.

Parameters
cipher_suiteconst psa_pake_cipher_suite_t *; required initialized suite.
Returns
psa_algorithm_t; stored selector, initially PSA_ALG_NONE.

◆ psa_pake_cs_get_key_confirmation()

uint32_t psa_pake_cs_get_key_confirmation ( const psa_pake_cipher_suite_t *  cipher_suite)

Read a suite's key-confirmation mode.

Parameters
cipher_suiteconst psa_pake_cipher_suite_t *; required initialized suite.
Returns
uint32_t; stored mode, initially PSA_PAKE_CONFIRMED_KEY.

◆ psa_pake_cs_get_primitive()

psa_pake_primitive_t psa_pake_cs_get_primitive ( const psa_pake_cipher_suite_t *  cipher_suite)

Read a suite's encoded primitive.

Parameters
cipher_suiteconst psa_pake_cipher_suite_t *; required initialized suite.
Returns
psa_pake_primitive_t; stored primitive, initially 0.

◆ psa_pake_cs_set_algorithm()

void psa_pake_cs_set_algorithm ( psa_pake_cipher_suite_t *  cipher_suite,
psa_algorithm_t  alg 
)

Store a suite's algorithm selector without validating provider support.

Parameters
cipher_suitepsa_pake_cipher_suite_t *; required writable suite.
algpsa_algorithm_t; algorithm selector; this provider supports PSA_ALG_SPAKE2P_HMAC(PSA_ALG_SHA_256). Validation occurs at setup.
Note
This function returns no status.

◆ psa_pake_cs_set_key_confirmation()

void psa_pake_cs_set_key_confirmation ( psa_pake_cipher_suite_t *  cipher_suite,
uint32_t  key_confirmation 
)

Store a suite's key-confirmation mode without validation.

Parameters
cipher_suitepsa_pake_cipher_suite_t *; required writable suite.
key_confirmationuint32_t; mode selector. This provider supports PSA_PAKE_CONFIRMED_KEY only; other values are rejected at setup.
Note
This function returns no status.

◆ psa_pake_cs_set_primitive()

void psa_pake_cs_set_primitive ( psa_pake_cipher_suite_t *  cipher_suite,
psa_pake_primitive_t  primitive 
)

Store a suite's primitive without validating provider support.

Parameters
cipher_suitepsa_pake_cipher_suite_t *; required writable suite.
primitivepsa_pake_primitive_t; encoded primitive; use SHARKSSL_PSA_SPAKE2P_PRIMITIVE for this provider's P-256 suite.
Note
This function returns no status. Validation occurs at setup.

◆ psa_pake_get_shared_key()

psa_status_t psa_pake_get_shared_key ( psa_pake_operation_t *  operation,
const psa_key_attributes_t *  attributes,
psa_key_id_t *  key 
)

Import the confirmed shared secret as a provider key and end the exchange.

Both confirmation steps must have completed. Success clears the operation and releases its credential dependency. The returned key remains live until the application destroys it with psa_destroy_key().

Parameters
operationpsa_pake_operation_t *; required completed operation.
attributesconst psa_key_attributes_t *; required output-key policy, borrowed for this call. Leave bits at 0; the imported secret is 32 bytes. Supported types are PSA_KEY_TYPE_DERIVE, PSA_KEY_TYPE_HMAC, PSA_KEY_TYPE_AES, and PSA_KEY_TYPE_CHACHA20, subject to the provider's import policy.
keypsa_key_id_t *; required handle output; set to PSA_KEY_ID_NULL on failure.
Returns
psa_status_t; PSA_SUCCESS, or a negative argument/state/key-import error. On failure, abort the operation before reusing it.

◆ psa_pake_input()

psa_status_t psa_pake_input ( psa_pake_operation_t *  operation,
psa_pake_step_t  step,
const uint8_t *  input,
size_t  input_length 
)

Consume the next peer message in the SPAKE2+ exchange.

Follow the order in Password-authenticated key exchange. Input is borrowed for this call.

Parameters
operationpsa_pake_operation_t *; required active operation with its role set.
steppsa_pake_step_t; PSA_PAKE_STEP_KEY_SHARE or PSA_PAKE_STEP_CONFIRM as required by the next peer message.
inputconst uint8_t *; required message buffer, disjoint from operation.
input_lengthsize_t; exactly 65 bytes for a P-256 share or 32 for confirmation.
Returns
psa_status_t; PSA_SUCCESS, PSA_ERROR_INVALID_SIGNATURE for a bad confirmation, or another negative error for malformed input, incorrect call order, unavailable key, or cryptographic failure. Errors require abort before reuse.

◆ psa_pake_operation_init()

psa_pake_operation_t psa_pake_operation_init ( void  )

Create an inactive PAKE operation.

Do not overwrite an active operation with this value; abort it first.

Returns
psa_pake_operation_t; inactive storage equivalent to PSA_PAKE_OPERATION_INIT.

◆ psa_pake_output()

psa_status_t psa_pake_output ( psa_pake_operation_t *  operation,
psa_pake_step_t  step,
uint8_t *  output,
size_t  output_size,
size_t *  output_length 
)

Produce the next local SPAKE2+ message.

Follow the order in Password-authenticated key exchange. Even a buffer-capacity error terminates this exchange; it cannot be retried on the same active state.

Parameters
operationpsa_pake_operation_t *; required active operation with its role set.
steppsa_pake_step_t; PSA_PAKE_STEP_KEY_SHARE or PSA_PAKE_STEP_CONFIRM as required by the next local message.
outputuint8_t *; required caller-owned buffer, disjoint from operation.
output_sizesize_t; buffer capacity in bytes, at least 65 for a share or 32 for confirmation; PSA_PAKE_OUTPUT_SIZE supplies the required size.
output_lengthsize_t *; required byte-count output, set to 0 on failure.
Returns
psa_status_t; PSA_SUCCESS, PSA_ERROR_BUFFER_TOO_SMALL, or another negative argument/state/key/RNG/cryptographic error. Output is usable only on success.

◆ psa_pake_set_context()

psa_status_t psa_pake_set_context ( psa_pake_operation_t *  operation,
const uint8_t *  context,
size_t  context_length 
)

Add an optional context to the exchange transcript before any message.

May be called once. An explicit empty context adds a zero-length transcript field; omitting this call adds no field. Both peers must make the same choice.

Parameters
operationpsa_pake_operation_t *; required operation after setup.
contextconst uint8_t *; bytes borrowed for this call; NULL only if context_length is 0.
context_lengthsize_t; context length in bytes; 0 is allowed.
Returns
psa_status_t; PSA_SUCCESS, or a negative error for invalid arguments, operation state, an unavailable key, or transcript hashing failure.

◆ psa_pake_set_peer()

psa_status_t psa_pake_set_peer ( psa_pake_operation_t *  operation,
const uint8_t *  peer_id,
size_t  peer_id_len 
)

Set the peer identity once, before any exchange message.

The provider copies the bytes. Match the peer's local identity; an omitted identity is empty.

Parameters
operationpsa_pake_operation_t *; required operation after setup.
peer_idconst uint8_t *; borrowed identity bytes; NULL only if peer_id_len is 0.
peer_id_lensize_t; identity length in bytes; 0 is allowed.
Returns
psa_status_t; PSA_SUCCESS, or a negative error for invalid arguments, allocation failure, an unavailable key, repeated setting, or an exchange already started.

◆ psa_pake_set_role()

psa_status_t psa_pake_set_role ( psa_pake_operation_t *  operation,
psa_pake_role_t  role 
)

Select the local SPAKE2+ role once, before exchanging messages.

Parameters
operationpsa_pake_operation_t *; required operation after successful setup.
rolepsa_pake_role_t; PSA_PAKE_ROLE_CLIENT or PSA_PAKE_ROLE_SERVER. A public-key credential cannot be used for the client role.
Returns
psa_status_t; PSA_SUCCESS, or a negative error for invalid arguments, repeated role selection, invalid operation state, or an unavailable credential key.

◆ psa_pake_set_user()

psa_status_t psa_pake_set_user ( psa_pake_operation_t *  operation,
const uint8_t *  user_id,
size_t  user_id_len 
)

Set the local identity once, before any exchange message.

The provider copies the bytes. An omitted identity is empty.

Parameters
operationpsa_pake_operation_t *; required operation after setup.
user_idconst uint8_t *; borrowed identity bytes; NULL only if user_id_len is 0.
user_id_lensize_t; identity length in bytes; 0 is allowed.
Returns
psa_status_t; PSA_SUCCESS, or a negative error for invalid arguments, allocation failure, an unavailable key, repeated setting, or an exchange already started.

◆ psa_pake_setup()

psa_status_t psa_pake_setup ( psa_pake_operation_t *  operation,
psa_key_id_t  password_key,
const psa_pake_cipher_suite_t *  cipher_suite 
)

Start a SPAKE2+ exchange using an existing credential key.

This provider supports confirmed P-256 with PSA_ALG_SPAKE2P_HMAC(PSA_ALG_SHA_256). It copies the suite and credential data and retains a key dependency until extraction or abort. Set the role, optional identities and context next; see Password-authenticated key exchange for the message order and error cleanup.

Parameters
operationpsa_pake_operation_t *; required initialized, inactive object.
password_keypsa_key_id_t; live 256-bit SPAKE2+ SECP_R1 key-pair or public-key handle, with PSA_KEY_USAGE_DERIVE and the suite's algorithm policy. The client requires a key pair; the server accepts either credential type.
cipher_suiteconst psa_pake_cipher_suite_t *; required suite, borrowed for this call; use SHARKSSL_PSA_SPAKE2P_PRIMITIVE and PSA_PAKE_CONFIRMED_KEY.
Returns
psa_status_t; PSA_SUCCESS, or a negative error for invalid arguments, active/failed state, unsupported suite, key access/policy, allocation, or hash setup.