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

Implement PSA interruptible sign/verify hash #6737

Closed
Show file tree
Hide file tree
Changes from 69 commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
e38925e
First draft of PSA interruptible ECC signing design
paul-elliott-arm Sep 9, 2022
be60b06
Initial empty driver wrapper implementation
paul-elliott-arm Nov 29, 2022
644855f
PSA level initial implementation
paul-elliott-arm Nov 30, 2022
ea28e1a
Make ECDSA restartable sign and verify functions public
paul-elliott-arm Dec 9, 2022
7062653
Add internal implementation
paul-elliott-arm Dec 2, 2022
3acdfad
Fix issues with get_{sign/verify}_num_ops
paul-elliott-arm Dec 15, 2022
70dae44
Basic tests
paul-elliott-arm Dec 7, 2022
34a5454
Add Changelog entry
paul-elliott-arm Dec 9, 2022
a4d6ad4
Negative tests
paul-elliott-arm Dec 16, 2022
57c0c3b
Add interruptible to psa_op_fail tests
paul-elliott-arm Dec 16, 2022
cb46e82
Convert tests to configurable max_ops
paul-elliott-arm Dec 16, 2022
a858e25
Add State tests
paul-elliott-arm Dec 18, 2022
97cb87d
Add {sign/verify}_hash_abort_internal
paul-elliott-arm Dec 18, 2022
aa001a6
Remove #endif from between testcases
davidhorstmann-arm Jan 3, 2023
464693c
Fix ecdsa.h documentation error
paul-elliott-arm Jan 10, 2023
ccf3a95
Ensure max ops gets set regardless of having built-in implementation
paul-elliott-arm Jan 10, 2023
22fa5b4
Clean up structure include guards
paul-elliott-arm Jan 16, 2023
5cb2b8e
Improve indentation of hash start functions
paul-elliott-arm Jan 16, 2023
701ffa5
Remove deterministic alg restriction on sign hash
paul-elliott-arm Jan 16, 2023
97c7284
Change test loops over to do...while
paul-elliott-arm Jan 20, 2023
2127657
Ensure ops are tested on successful 'fail' tests
paul-elliott-arm Jan 20, 2023
3eb6050
Calculate min/max completes rather than passing in to test
paul-elliott-arm Jan 23, 2023
75416e4
Fix broken negative test
paul-elliott-arm Jan 24, 2023
506eea0
Ensure structs are not empty even if ECDSA not supported
paul-elliott-arm Jan 24, 2023
ecaf320
Fix incorrect test dependancies
paul-elliott-arm Jan 25, 2023
1fbafc9
Fix incorrect test dependencies part 2
paul-elliott-arm Jan 26, 2023
0c3c3bf
Fix abort documentation.
paul-elliott-arm Feb 3, 2023
cd6e52f
Fix operation initialisers if no algorithms defined
paul-elliott-arm Feb 3, 2023
58f13a1
Remove unrequired mpis from sign operation struct
paul-elliott-arm Feb 3, 2023
eb6c3fb
Remove obsolete comments
paul-elliott-arm Feb 3, 2023
1513613
Change incorrect define for MAX_OPS_UNLIMITED
paul-elliott-arm Feb 3, 2023
4c26516
Remove unneeded warning from internal headers
paul-elliott-arm Feb 3, 2023
5c4e5eb
Remove incorrect copied comment
paul-elliott-arm Feb 3, 2023
40b5daa
Rename curve_bytes to coordinate_bytes
paul-elliott-arm Feb 5, 2023
0de33b0
Non ECDSA algorithms should return not supported
paul-elliott-arm Feb 5, 2023
7e09389
Replace MBEDTLS_ECP_DP_SECP384R1_ENABLED
paul-elliott-arm Feb 5, 2023
1283223
Store the hash, rather than the pointer
paul-elliott-arm Feb 5, 2023
1abf8c8
Ensure that operation is put into error state if error occurs
paul-elliott-arm Feb 6, 2023
375c151
Move loading of public part of ECP into function
paul-elliott-arm Feb 6, 2023
0d55f61
Move {min|max}_complete choice logic into function
paul-elliott-arm Feb 6, 2023
414fc63
Fix build fails with non ECDSA / restartable builds
paul-elliott-arm Feb 7, 2023
e242074
Move output buffer wiping code to seperate function.
paul-elliott-arm Feb 7, 2023
cb76f1e
Fix {sign|verify}_get_num_ops
paul-elliott-arm Feb 7, 2023
abcc26a
Add documentation comment to internal abort functions
paul-elliott-arm Feb 7, 2023
3110365
Shorten test descriptions.
paul-elliott-arm Feb 7, 2023
da4d28b
Replace allocated hash buffer with array
paul-elliott-arm Feb 7, 2023
837349f
Split state tests into two functions
paul-elliott-arm Feb 7, 2023
32913f1
Add max ops tests
paul-elliott-arm Feb 7, 2023
3e4e133
Add comment to explain lack of driver dispatch
paul-elliott-arm Feb 7, 2023
21c19ed
Fix missing mbedtls_mpi_free() on signing.
paul-elliott-arm Feb 8, 2023
60a3fed
Fix include guards on auxiliary test function.
paul-elliott-arm Feb 9, 2023
c46ce8e
Fix buffer overflow with hashes larger than key size.
paul-elliott-arm Feb 9, 2023
c7394e8
Better formatting of include guard comments
paul-elliott-arm Feb 10, 2023
5fadb30
Fix errors in psa_wipe_output_buffer() doc comment.
paul-elliott-arm Feb 10, 2023
8b60c3e
Switch from nbits to pbits
paul-elliott-arm Feb 10, 2023
70c0ca2
Wipe output buffer even when INCOMPLETE is returned.
paul-elliott-arm Feb 10, 2023
bc631fd
Move num ops update to only point where work can be done.
paul-elliott-arm Feb 10, 2023
ab6f6c4
Move structure init calls as early as possible
paul-elliott-arm Feb 15, 2023
3554b3d
Correct insufficient memory return documentation.
paul-elliott-arm Feb 15, 2023
7615c0c
Run extra complete in failure tests regardless.
paul-elliott-arm Feb 15, 2023
e2455d0
Make mbedtls_sa_ecp_load_public_part return psa_status_t
paul-elliott-arm Feb 15, 2023
8f6970f
Remove spurious incorrect comment
paul-elliott-arm Feb 15, 2023
023f0ee
Move num_ops ECP abstraction fully into internal implementation
paul-elliott-arm Feb 15, 2023
0cab070
Add safety for keys larger than we currently support.
paul-elliott-arm Feb 15, 2023
1105510
Add verify call to max ops tests
paul-elliott-arm Feb 15, 2023
80bda3a
Add reference to mbedtls_ecp_set_max_ops() to docs
paul-elliott-arm Feb 15, 2023
3dd90c5
Add warning to mbedtls_ecp_set_max_ops()
paul-elliott-arm Feb 15, 2023
38944d6
Add comment to indicate non-PSA spec assertion.
paul-elliott-arm Feb 15, 2023
e380115
Remove some abbrevations from test descriptions.
paul-elliott-arm Feb 15, 2023
fb60eaa
Fix formatting issues
paul-elliott-arm Feb 16, 2023
34c61f4
Fix get_num_ops internal code.
paul-elliott-arm Feb 19, 2023
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
5 changes: 5 additions & 0 deletions ChangeLog.d/add_interruptible_sign_hash
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Features
* Add an interruptible version of sign and verify hash to the PSA interface,
backed by internal library support for ECDSA signing and verification.


173 changes: 173 additions & 0 deletions include/mbedtls/ecdsa.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,134 @@ int mbedtls_ecdsa_sign_det_ext( mbedtls_ecp_group *grp, mbedtls_mpi *r,
void *p_rng_blind );
#endif /* MBEDTLS_ECDSA_DETERMINISTIC */

#if !defined(MBEDTLS_ECDSA_SIGN_ALT)
mpg marked this conversation as resolved.
Show resolved Hide resolved
/**
* \brief This function computes the ECDSA signature of a
* previously-hashed message, in a restartable way.
*
* \note The deterministic version implemented in
* mbedtls_ecdsa_sign_det_restartable() is usually
* preferred.
*
* \note This function is like \c mbedtls_ecdsa_sign() but
* it can return early and restart according to the
* limit set with \c mbedtls_ecp_set_max_ops() to
* reduce blocking.
*
* \note If the bitlength of the message hash is larger
* than the bitlength of the group order, then the
* hash is truncated as defined in <em>Standards for
* Efficient Cryptography Group (SECG): SEC1 Elliptic
* Curve Cryptography</em>, section 4.1.3, step 5.
*
* \see ecp.h
*
* \param grp The context for the elliptic curve to use.
* This must be initialized and have group parameters
* set, for example through mbedtls_ecp_group_load().
* \param r The MPI context in which to store the first part
* the signature. This must be initialized.
* \param s The MPI context in which to store the second part
* the signature. This must be initialized.
* \param d The private signing key. This must be initialized
* and setup, for example through
* mbedtls_ecp_gen_privkey().
* \param buf The hashed content to be signed. This must be a readable
* buffer of length \p blen Bytes. It may be \c NULL if
* \p blen is zero.
* \param blen The length of \p buf in Bytes.
* \param f_rng The RNG function. This must not be \c NULL.
* \param p_rng The RNG context to be passed to \p f_rng. This may be
* \c NULL if \p f_rng doesn't need a context parameter.
* \param f_rng_blind The RNG function used for blinding. This must not be
* \c NULL.
* \param p_rng_blind The RNG context to be passed to \p f_rng. This may be
* \c NULL if \p f_rng doesn't need a context parameter.
* \param rs_ctx The restart context to use. This may be \c NULL
* to disable restarting. If it is not \c NULL, it
* must point to an initialized restart context.
*
* \return \c 0 on success.
* \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
* operations was reached: see \c
* mbedtls_ecp_set_max_ops().
* \return Another \c MBEDTLS_ERR_ECP_XXX, \c
* MBEDTLS_ERR_MPI_XXX or \c MBEDTLS_ERR_ASN1_XXX
* error code on failure.
*/
int mbedtls_ecdsa_sign_restartable(
mbedtls_ecp_group *grp,
mbedtls_mpi *r, mbedtls_mpi *s,
const mbedtls_mpi *d,
const unsigned char *buf, size_t blen,
int (*f_rng)( void *, unsigned char *, size_t ),
void *p_rng,
int (*f_rng_blind)( void *, unsigned char *, size_t ),
void *p_rng_blind,
mbedtls_ecdsa_restart_ctx *rs_ctx );

#if defined(MBEDTLS_ECDSA_DETERMINISTIC)

/**
* \brief This function computes the ECDSA signature of a
* previously-hashed message, in a restartable way.
*
* \note This function is like \c
* mbedtls_ecdsa_sign_det_ext() but it can return
* early and restart according to the limit set with
* \c mbedtls_ecp_set_max_ops() to reduce blocking.
*
* \note If the bitlength of the message hash is larger
* than the bitlength of the group order, then the
* hash is truncated as defined in <em>Standards for
* Efficient Cryptography Group (SECG): SEC1 Elliptic
* Curve Cryptography</em>, section 4.1.3, step 5.
*
* \see ecp.h
*
* \param grp The context for the elliptic curve to use.
* This must be initialized and have group parameters
* set, for example through mbedtls_ecp_group_load().
* \param r The MPI context in which to store the first part
* the signature. This must be initialized.
* \param s The MPI context in which to store the second part
* the signature. This must be initialized.
* \param d The private signing key. This must be initialized
* and setup, for example through
* mbedtls_ecp_gen_privkey().
* \param buf The hashed content to be signed. This must be a readable
* buffer of length \p blen Bytes. It may be \c NULL if
* \p blen is zero.
* \param blen The length of \p buf in Bytes.
* \param f_rng_blind The RNG function used for blinding. This must not be
* \c NULL.
* \param p_rng_blind The RNG context to be passed to \p f_rng. This may be
* \c NULL if \p f_rng doesn't need a context parameter.
* \param rs_ctx The restart context to use. This may be \c NULL
* to disable restarting. If it is not \c NULL, it
* must point to an initialized restart context.
*
* \return \c 0 on success.
* \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
* operations was reached: see \c
* mbedtls_ecp_set_max_ops().
* \return Another \c MBEDTLS_ERR_ECP_XXX, \c
* MBEDTLS_ERR_MPI_XXX or \c MBEDTLS_ERR_ASN1_XXX
* error code on failure.
*/
int mbedtls_ecdsa_sign_det_restartable(
mbedtls_ecp_group *grp,
mbedtls_mpi *r, mbedtls_mpi *s,
const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
mbedtls_md_type_t md_alg,
int (*f_rng_blind)( void *, unsigned char *, size_t ),
void *p_rng_blind,
mbedtls_ecdsa_restart_ctx *rs_ctx );

#endif /* MBEDTLS_ECDSA_DETERMINISTIC */

#endif /* !MBEDTLS_ECDSA_SIGN_ALT */

/**
* \brief This function verifies the ECDSA signature of a
* previously-hashed message.
Expand Down Expand Up @@ -253,6 +381,51 @@ int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp,
const mbedtls_ecp_point *Q, const mbedtls_mpi *r,
const mbedtls_mpi *s);

#if !defined(MBEDTLS_ECDSA_VERIFY_ALT)
mpg marked this conversation as resolved.
Show resolved Hide resolved
/**
* \brief This function verifies the ECDSA signature of a
* previously-hashed message, in a restartable manner
*
* \note If the bitlength of the message hash is larger than the
* bitlength of the group order, then the hash is truncated as
* defined in <em>Standards for Efficient Cryptography Group
* (SECG): SEC1 Elliptic Curve Cryptography</em>, section
* 4.1.4, step 3.
*
* \see ecp.h
*
* \param grp The ECP group to use.
* This must be initialized and have group parameters
* set, for example through mbedtls_ecp_group_load().
* \param buf The hashed content that was signed. This must be a readable
* buffer of length \p blen Bytes. It may be \c NULL if
* \p blen is zero.
* \param blen The length of \p buf in Bytes.
* \param Q The public key to use for verification. This must be
* initialized and setup.
* \param r The first integer of the signature.
* This must be initialized.
* \param s The second integer of the signature.
* This must be initialized.
* \param rs_ctx The restart context to use. This may be \c NULL to disable
* restarting. If it is not \c NULL, it must point to an
* initialized restart context.
*
* \return \c 0 on success.
* \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
* operations was reached: see \c mbedtls_ecp_set_max_ops().
* \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
* error code on failure.
*/
int mbedtls_ecdsa_verify_restartable( mbedtls_ecp_group *grp,
const unsigned char *buf, size_t blen,
const mbedtls_ecp_point *Q,
const mbedtls_mpi *r,
const mbedtls_mpi *s,
mbedtls_ecdsa_restart_ctx *rs_ctx );

#endif /* !MBEDTLS_ECDSA_VERIFY_ALT */

/**
* \brief This function computes the ECDSA signature and writes it
* to a buffer, serialized as defined in <em>RFC-4492:
Expand Down
6 changes: 6 additions & 0 deletions include/mbedtls/ecp.h
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,12 @@ mbedtls_ecp_keypair;
* only enabled for specific sides and key exchanges
* (currently only for clients and ECDHE-ECDSA).
*
* \warning Using the PSA interruptible interfaces with keys in local
* storage and no accelerator driver will also call this
* function to set the values specified via those interfaces,
* overwriting values previously set. Care should be taken if
* mixing these two interfaces.
*
* \param max_ops Maximum number of basic operations done in a row.
* Default: 0 (unlimited).
* Lower (non-zero) values mean ECC functions will block for
Expand Down
Loading