Skip to content

Commit 0e9ff85

Browse files
authored
Merge pull request #224 from ARMmbed/feature-psa-tls-integration-proposed
Update with "Mbed TLS using PSA" mini-release
2 parents dc5f950 + 9edcc3e commit 0e9ff85

31 files changed

+7638
-531
lines changed

ChangeLog

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
mbed TLS ChangeLog (Sorted per branch, date)
22

3+
= mbed TLS 2.xx.x branch released xxxx-xx-xx
4+
5+
Changes
6+
* Add unit tests for AES-GCM when called through mbedtls_cipher_auth_xxx()
7+
from the cipher abstraction layer. Fixes #2198.
8+
39
= mbed TLS 2.14.0 branch released 2018-11-19
410

511
Security

include/mbedtls/cipher.h

Lines changed: 143 additions & 86 deletions
Large diffs are not rendered by default.

include/mbedtls/cipher_internal.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434

3535
#include "cipher.h"
3636

37+
#if defined(MBEDTLS_USE_PSA_CRYPTO)
38+
#include "psa/crypto.h"
39+
#endif /* MBEDTLS_USE_PSA_CRYPTO */
40+
3741
#ifdef __cplusplus
3842
extern "C" {
3943
#endif
@@ -114,6 +118,30 @@ typedef struct
114118
const mbedtls_cipher_info_t *info;
115119
} mbedtls_cipher_definition_t;
116120

121+
#if defined(MBEDTLS_USE_PSA_CRYPTO)
122+
typedef enum
123+
{
124+
MBEDTLS_CIPHER_PSA_KEY_UNSET = 0,
125+
MBEDTLS_CIPHER_PSA_KEY_OWNED, /* Used for PSA-based cipher contexts which */
126+
/* use raw key material internally imported */
127+
/* into a allocated key slot, and which */
128+
/* hence need to destroy that key slot */
129+
/* when they are no longer needed. */
130+
MBEDTLS_CIPHER_PSA_KEY_NOT_OWNED, /* Used for PSA-based cipher contexts */
131+
/* which use a key from a key slot */
132+
/* provided by the user, and which */
133+
/* hence should not be destroyed when */
134+
/* the context is no longer needed. */
135+
} mbedtls_cipher_psa_key_ownership;
136+
137+
typedef struct
138+
{
139+
psa_algorithm_t alg;
140+
psa_key_slot_t slot;
141+
mbedtls_cipher_psa_key_ownership slot_state;
142+
} mbedtls_cipher_context_psa;
143+
#endif /* MBEDTLS_USE_PSA_CRYPTO */
144+
117145
extern const mbedtls_cipher_definition_t mbedtls_cipher_definitions[];
118146

119147
extern int mbedtls_cipher_supported[];

include/mbedtls/ecdsa.h

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,30 @@
3535
#include "ecp.h"
3636
#include "md.h"
3737

38-
/*
39-
* RFC-4492 page 20:
38+
/**
39+
* \brief Maximum ECDSA signature size for a given curve bit size
4040
*
41+
* \param bits Curve size in bits
42+
* \return Maximum signature size in bytes
43+
*
44+
* \note This macro returns a compile-time constant if its argument
45+
* is one. It may evaluate its argument multiple times.
46+
*/
47+
/*
4148
* Ecdsa-Sig-Value ::= SEQUENCE {
4249
* r INTEGER,
4350
* s INTEGER
4451
* }
4552
*
46-
* Size is at most
47-
* 1 (tag) + 1 (len) + 1 (initial 0) + ECP_MAX_BYTES for each of r and s,
48-
* twice that + 1 (tag) + 2 (len) for the sequence
49-
* (assuming ECP_MAX_BYTES is less than 126 for r and s,
50-
* and less than 124 (total len <= 255) for the sequence)
53+
* For each of r and s, the value (V) may include an extra initial "0" bit.
5154
*/
52-
#if MBEDTLS_ECP_MAX_BYTES > 124
53-
#error "MBEDTLS_ECP_MAX_BYTES bigger than expected, please fix MBEDTLS_ECDSA_MAX_LEN"
54-
#endif
55+
#define MBEDTLS_ECDSA_MAX_SIG_LEN( bits ) \
56+
( /*T,L of SEQUENCE*/ ( ( bits ) >= 61 * 8 ? 3 : 2 ) + \
57+
/*T,L of r,s*/ 2 * ( ( ( bits ) >= 127 * 8 ? 3 : 2 ) + \
58+
/*V of r,s*/ ( ( bits ) + 8 ) / 8 ) )
59+
5560
/** The maximal size of an ECDSA signature in Bytes. */
56-
#define MBEDTLS_ECDSA_MAX_LEN ( 3 + 2 * ( 3 + MBEDTLS_ECP_MAX_BYTES ) )
61+
#define MBEDTLS_ECDSA_MAX_LEN MBEDTLS_ECDSA_MAX_SIG_LEN( MBEDTLS_ECP_MAX_BITS )
5762

5863
#ifdef __cplusplus
5964
extern "C" {

include/mbedtls/pk.h

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@
4545
#include "ecdsa.h"
4646
#endif
4747

48+
#if defined(MBEDTLS_USE_PSA_CRYPTO)
49+
#include "psa/crypto.h"
50+
#endif
51+
4852
#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
4953
!defined(inline) && !defined(__cplusplus)
5054
#define inline __inline
@@ -83,6 +87,7 @@ typedef enum {
8387
MBEDTLS_PK_ECDSA,
8488
MBEDTLS_PK_RSA_ALT,
8589
MBEDTLS_PK_RSASSA_PSS,
90+
MBEDTLS_PK_OPAQUE,
8691
} mbedtls_pk_type_t;
8792

8893
/**
@@ -203,6 +208,11 @@ void mbedtls_pk_init( mbedtls_pk_context *ctx );
203208

204209
/**
205210
* \brief Free a mbedtls_pk_context
211+
*
212+
* \note For contexts that have been set up with
213+
* mbedtls_pk_setup_opaque(), this does not free the underlying
214+
* key slot and you still need to call psa_destroy_key()
215+
* independently if you want to destroy that key.
206216
*/
207217
void mbedtls_pk_free( mbedtls_pk_context *ctx );
208218

@@ -234,6 +244,38 @@ void mbedtls_pk_restart_free( mbedtls_pk_restart_ctx *ctx );
234244
*/
235245
int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info );
236246

247+
#if defined(MBEDTLS_USE_PSA_CRYPTO)
248+
/**
249+
* \brief Initialize a PK context to wrap a PSA key slot.
250+
*
251+
* \note This function replaces mbedtls_pk_setup() for contexts
252+
* that wrap a (possibly opaque) PSA key slot instead of
253+
* storing and manipulating the key material directly.
254+
*
255+
* \param ctx The context to initialize. It must be empty (type NONE).
256+
* \param key The PSA key slot to wrap, which must hold an ECC key pair
257+
* (see notes below).
258+
*
259+
* \note The wrapped key slot must remain valid as long as the
260+
* wrapping PK context is in use, that is at least between
261+
* the point this function is called and the point
262+
* mbedtls_pk_free() is called on this context. The wrapped
263+
* key slot might then be independently used or destroyed.
264+
*
265+
* \note This function is currently only available for ECC key
266+
* pairs (that is, ECC keys containing private key material).
267+
* Support for other key types may be added later.
268+
*
269+
* \return \c 0 on success.
270+
* \return #MBEDTLS_ERR_PK_BAD_INPUT_DATA on invalid input
271+
* (context already used, invalid key slot).
272+
* \return #MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE if the key is not an
273+
* ECC key pair.
274+
* \return #MBEDTLS_ERR_PK_ALLOC_FAILED on allocation failure.
275+
*/
276+
int mbedtls_pk_setup_opaque( mbedtls_pk_context *ctx, const psa_key_slot_t key );
277+
#endif /* MBEDTLS_USE_PSA_CRYPTO */
278+
237279
#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
238280
/**
239281
* \brief Initialize an RSA-alt context
@@ -480,7 +522,11 @@ int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
480522
* \param pub Context holding a public key.
481523
* \param prv Context holding a private (and public) key.
482524
*
483-
* \return 0 on success or MBEDTLS_ERR_PK_BAD_INPUT_DATA
525+
* \return \c 0 on success (keys were checked and match each other).
526+
* \return #MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE if the keys could not
527+
* be checked - in that case they may or may not match.
528+
* \return #MBEDTLS_ERR_PK_BAD_INPUT_DATA if a context is invalid.
529+
* \return Another non-zero value if the keys do not match.
484530
*/
485531
int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv );
486532

@@ -694,6 +740,31 @@ int mbedtls_pk_write_pubkey( unsigned char **p, unsigned char *start,
694740
int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n );
695741
#endif
696742

743+
#if defined(MBEDTLS_USE_PSA_CRYPTO)
744+
/**
745+
* \brief Turn an EC key into an Opaque one
746+
*
747+
* \warning This is a temporary utility function for tests. It might
748+
* change or be removed at any time without notice.
749+
*
750+
* \note Only ECDSA keys are supported so far. Signing with the
751+
* specified hash is the only allowed use of that key.
752+
*
753+
* \param pk Input: the EC key to transfer to a PSA key slot.
754+
* Output: a PK context wrapping that PSA key slot.
755+
* \param slot Output: the chosen slot for storing the key.
756+
* It's the caller's responsibility to destroy that slot
757+
* after calling mbedtls_pk_free() on the PK context.
758+
* \param hash_alg The hash algorithm to allow for use with that key.
759+
*
760+
* \return \c 0 if successful.
761+
* \return An Mbed TLS error code otherwise.
762+
*/
763+
int mbedtls_pk_wrap_as_opaque( mbedtls_pk_context *pk,
764+
psa_key_slot_t *slot,
765+
psa_algorithm_t hash_alg );
766+
#endif /* MBEDTLS_USE_PSA_CRYPTO */
767+
697768
#ifdef __cplusplus
698769
}
699770
#endif

include/mbedtls/pk_internal.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,8 @@ extern const mbedtls_pk_info_t mbedtls_ecdsa_info;
135135
extern const mbedtls_pk_info_t mbedtls_rsa_alt_info;
136136
#endif
137137

138+
#if defined(MBEDTLS_USE_PSA_CRYPTO)
139+
extern const mbedtls_pk_info_t mbedtls_pk_opaque_info;
140+
#endif
141+
138142
#endif /* MBEDTLS_PK_WRAP_H */

0 commit comments

Comments
 (0)