1
0
mirror of https://github.com/Mbed-TLS/mbedtls.git synced 2025-07-28 00:21:48 +03:00

Add mbedtls_ecp_read_key

The private keys used in ECDH differ in the case of Weierstrass and
Montgomery curves. They have different constraints, the former is based
on big endian, the latter little endian byte order. The fundamental
approach is different too:
- Weierstrass keys have to be in the right interval, otherwise they are
  rejected.
- Any byte array of the right size is a valid Montgomery key and it
  needs to be masked before interpreting it as a number.

Historically it was sufficient to use mbedtls_mpi_read_binary() to read
private keys, but as a preparation to improve support for Montgomery
curves we add mbedtls_ecp_read_key() to enable uniform treatment of EC
keys.

For the masking the `mbedtls_mpi_set_bit()` function is used. This is
suboptimal but seems to provide the best trade-off at this time.
Alternatives considered:
- Making a copy of the input buffer (less efficient)
- removing the `const` constraint from the input buffer (breaks the api
and makes it less user friendly)
- applying the mask directly to the limbs (violates the api between the
modules and creates and unwanted dependency)
This commit is contained in:
Janos Follath
2019-02-15 16:17:45 +00:00
parent 59b813c7be
commit 171a7efd02
6 changed files with 160 additions and 4 deletions

View File

@ -838,6 +838,11 @@ int mbedtls_mpi_read_binary_le( mbedtls_mpi *X,
cleanup:
/*
* This function is also used to import keys. However, wiping the buffers
* upon failure is not necessary because failure only can happen before any
* input is copied.
*/
return( ret );
}
@ -875,6 +880,11 @@ int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t bu
cleanup:
/*
* This function is also used to import keys. However, wiping the buffers
* upon failure is not necessary because failure only can happen before any
* input is copied.
*/
return( ret );
}