Skip to content

PSA unified driver prototype #3313

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
d1e09d3
Define some structure for lifetime values
gilles-peskine-arm Feb 5, 2020
27bf930
Shorten type and value names for lifetime parts
gilles-peskine-arm May 4, 2020
5936e78
PSA_KEY_LIFETIME_PERSISTENT is a lifetime, not just a storage area
gilles-peskine-arm May 4, 2020
8797745
Rename and clarify the default persistent location and persistence
gilles-peskine-arm May 4, 2020
8d0755f
Document PSA_KEY_PERSISTENCE_xxx and PSA_KEY_LOCATION_xxx
gilles-peskine-arm May 4, 2020
546dc5a
Define a macro to construct a lifetime from persistence and location
gilles-peskine-arm May 4, 2020
4665ae6
Start a framework for a unified cryptoprocessor driver interface
gilles-peskine-arm May 4, 2020
4a5be61
Add experimental option MBEDTLS_PSA_CRYPTO_DRIVERS
gilles-peskine-arm May 6, 2020
0f45080
Fix macros
gilles-peskine-arm May 6, 2020
d1e919d
New internal function psa_get_key_slot_attributes
gilles-peskine-arm May 6, 2020
e7e77c7
PSA crypto driver description parser and header generator
gilles-peskine-arm May 6, 2020
1f9f004
PSA crypto driver wrapper generator
gilles-peskine-arm May 6, 2020
3817cff
Support dispatching psa_sign_hash to a driver
gilles-peskine-arm May 6, 2020
97b9253
New test driver implementing sign_hash with a transparent key
gilles-peskine-arm May 6, 2020
0f07c6d
Opaque driver declaring import/destroy key and sign_hash
gilles-peskine-arm May 6, 2020
99727e3
Smoke test for psa_sign_hash through a transparent driver
gilles-peskine-arm May 6, 2020
6c5adaa
Support library/psa_crypto_driver_wrappers.c in Makefile
gilles-peskine-arm May 6, 2020
7bc4091
Add all.sh component to run the driver wrapper smoke test
gilles-peskine-arm May 6, 2020
e225cfa
PSA driver interface document
gilles-peskine-arm May 7, 2020
83405b8
Maximum asymmetric key export size
gilles-peskine-arm May 12, 2020
d6043ee
Test the maximum asymmetric key export size macros
gilles-peskine-arm May 12, 2020
2299c4e
Transparent drivers: pass the key in export format
gilles-peskine-arm May 12, 2020
417f6fc
Support linking tests with driver code in tests/drivers/
gilles-peskine-arm May 12, 2020
c451653
Move the signature driver from psa_crypto_helpers.h into its own file
gilles-peskine-arm May 12, 2020
8d3dff4
Major rewrite of the architecture document
gilles-peskine-arm May 18, 2020
644bbe1
Re-unify remote and opaque drivers
gilles-peskine-arm May 21, 2020
62ad281
Reorganize open questions section
gilles-peskine-arm May 22, 2020
29a623a
Don't treat key pairs specially
gilles-peskine-arm May 22, 2020
b970bc8
Note that substitution point or core callbacks should be added
gilles-peskine-arm May 22, 2020
990b84a
Write down some high-level requirements
gilles-peskine-arm May 26, 2020
e1fe2fa
Key context size calculation
gilles-peskine-arm May 26, 2020
10470ec
Add a time stamp
gilles-peskine-arm May 26, 2020
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ programs: lib
$(MAKE) -C programs

lib:
$(MAKE) -C library
$(MAKE) -C library "PSA_DRIVERS=$(abspath $(PSA_DRIVERS))"

tests: lib
$(MAKE) -C tests
Expand Down
1 change: 1 addition & 0 deletions docs/architecture/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ default: all

all_markdown = \
mbed-crypto-storage-specification.md \
psa-driver-interface.md \
testing/driver-interface-test-strategy.md \
testing/test-framework.md \
# This line is intentionally left blank
Expand Down
561 changes: 561 additions & 0 deletions docs/architecture/psa-driver-interface.md

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions include/mbedtls/check_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,10 @@
#error "MBEDTLS_PSA_CRYPTO_SE_C defined, but not all prerequisites"
#endif

#if defined(MBEDTLS_PSA_CRYPTO_DRIVERS) && ! defined(MBEDTLS_PSA_CRYPTO_C)
#error "MBEDTLS_PSA_CRYPTO_SE_C defined, but not all prerequisites"
#endif

#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) && \
! defined(MBEDTLS_PSA_CRYPTO_C)
#error "MBEDTLS_PSA_CRYPTO_STORAGE_C defined, but not all prerequisites"
Expand Down
11 changes: 11 additions & 0 deletions include/mbedtls/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -1307,6 +1307,17 @@
*/
#define MBEDTLS_PKCS1_V21

/** \def MBEDTLS_PSA_CRYPTO_DRIVERS
*
* Enable support for the experimental PSA crypto driver interface.
*
* Requires: MBEDTLS_PSA_CRYPTO_C.
*
* \warning This interface is experimental and may change or be removed
* without notice.
*/
//#define MBEDTLS_PSA_CRYPTO_DRIVERS

/**
* \def MBEDTLS_PSA_CRYPTO_SPM
*
Expand Down
52 changes: 52 additions & 0 deletions include/psa/crypto_driver.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* \file psa/crypto_driver.h
* \brief PSA cryptoprocessor driver interface
*
* This header declares types and function signatures for cryptoprocessor
* drivers.
*
* This file is part of the PSA Crypto Driver Model, containing functions for
* driver developers to implement to enable hardware to be called in a
* standardized way by a PSA Cryptographic API implementation. The functions
* comprising the driver model, which driver authors implement, are not
* intended to be called by application developers.
*/

/*
* Copyright (C) 2020, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef PSA_CRYPTO_DRIVER_H
#define PSA_CRYPTO_DRIVER_H

#include <stddef.h>
#include <stdint.h>

/* Include type definitions (psa_status_t, psa_algorithm_t,
* psa_key_type_t, etc.) and macros to build and analyze values
* of these types. */
#include "crypto_types.h"
#include "crypto_values.h"

#ifdef __cplusplus
extern "C" {
#endif


#ifdef __cplusplus
}
#endif

#endif /* PSA_CRYPTO_DRIVER_H */
37 changes: 37 additions & 0 deletions include/psa/crypto_sizes.h
Original file line number Diff line number Diff line change
Expand Up @@ -659,4 +659,41 @@
PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \
0)

#define PSA_VENDOR_RSA_MAX_PUBLIC_KEY_SIZE \
PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS)
#define PSA_VENDOR_RSA_MAX_KEY_PAIR_SIZE \
PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS)
#define PSA_VENDOR_ECC_MAX_PUBLIC_KEY_SIZE \
PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
#define PSA_VENDOR_ECC_MAX_KEY_PAIR_SIZE \
PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS)

/** Sufficient output buffer size for psa_export_key() on a public key or psa_export_public_key().
*/
#define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE 0
#if (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_PK_RSA_ALT_SUPPORT)) && \
PSA_VENDOR_RSA_MAX_PUBLIC_KEY_SIZE > PSA_EXPORT_PUBLIC_KEY_MAX_SIZE
#undef PSA_EXPORT_PUBLIC_KEY_MAX_SIZE
#define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE PSA_VENDOR_RSA_MAX_PUBLIC_KEY_SIZE
#endif
#if defined(MBEDTLS_ECP_C) && \
PSA_VENDOR_ECC_MAX_PUBLIC_KEY_SIZE > PSA_EXPORT_PUBLIC_KEY_MAX_SIZE
#undef PSA_EXPORT_PUBLIC_KEY_MAX_SIZE
#define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE PSA_VENDOR_ECC_MAX_PUBLIC_KEY_SIZE
#endif

/** Sufficient output buffer size for psa_export_key() on a key pair.
*/
#define PSA_EXPORT_KEY_PAIR_MAX_SIZE 0
#if (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_PK_RSA_ALT_SUPPORT)) && \
PSA_VENDOR_RSA_MAX_KEY_PAIR_SIZE > PSA_EXPORT_KEY_PAIR_MAX_SIZE
#undef PSA_EXPORT_KEY_PAIR_MAX_SIZE
#define PSA_EXPORT_KEY_PAIR_MAX_SIZE PSA_VENDOR_RSA_MAX_KEY_PAIR_SIZE
#endif
#if defined(MBEDTLS_ECP_C) && \
PSA_VENDOR_ECC_MAX_KEY_PAIR_SIZE > PSA_EXPORT_KEY_PAIR_MAX_SIZE
#undef PSA_EXPORT_KEY_PAIR_MAX_SIZE
#define PSA_EXPORT_KEY_PAIR_MAX_SIZE PSA_VENDOR_ECC_MAX_KEY_PAIR_SIZE
#endif

#endif /* PSA_CRYPTO_SIZES_H */
109 changes: 104 additions & 5 deletions include/psa/crypto_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,117 @@ typedef uint32_t psa_algorithm_t;
* The lifetime of a key indicates where it is stored and what system actions
* may create and destroy it.
*
* Keys with the lifetime #PSA_KEY_LIFETIME_VOLATILE are automatically
* destroyed when the application terminates or on a power reset.
* Lifetime values have the following structure:
* - Bits 0-7 (#PSA_KEY_LIFETIME_GET_PERSISTENCE(\c lifetime)):
* persistence level. This value indicates what device management
* actions can cause it to be destroyed. In particular, it indicates
* whether the key is _volatile_ or _persistent_.
* See ::psa_key_persistence_t for more information.
* - Bits 8-31 (#PSA_KEY_LIFETIME_GET_LOCATION(\c lifetime)):
* location indicator. This value indicates where the key is stored
* and where operations on the key are performed.
* See ::psa_key_location_t for more information.
*
* Volatile keys are automatically destroyed when the application instance
* terminates or on a power reset of the device. Persistent keys are
* preserved until the application explicitly destroys them or until an
* implementation-specific device management event occurs (for example,
* a factory reset).
*
* Keys with a lifetime other than #PSA_KEY_LIFETIME_VOLATILE are said
* to be _persistent_.
* Persistent keys are preserved if the application or the system restarts.
* Persistent keys have a key identifier of type #psa_key_id_t.
* This identifier remains valid throughout the lifetime of the key,
* even if the application instance that created the key terminates.
* The application can call psa_open_key() to open a persistent key that
* it created previously.
*
* This specification defines two basic lifetime values:
* - Keys with the lifetime #PSA_KEY_LIFETIME_VOLATILE are volatile.
* All implementations should support this lifetime.
* - Keys with the lifetime #PSA_KEY_LIFETIME_PERSISTENT are persistent.
* All implementations that have access to persistent storage with
* appropriate security guarantees should support this lifetime.
*/
typedef uint32_t psa_key_lifetime_t;

/** Encoding of key persistence levels.
*
* What distinguishes different persistence levels is what device management
* events may cause keys to be destroyed. _Volatile_ keys are destroyed
* by a power reset. Persistent keys may be destroyed by events such as
* a transfer of ownership or a factory reset. What management events
* actually affect persistent keys at different levels is outside the
* scope of the PSA Cryptography specification.
*
* This specification defines the following values of persistence levels:
* - \c 0 = #PSA_KEY_PERSISTENCE_VOLATILE: volatile key.
* A volatile key is automatically destroyed by the implementation when
* the application instance terminates. In particular, a volatile key
* is automatically destroyed on a power reset of the device.
* - \c 1 = #PSA_KEY_PERSISTENCE_DEFAULT:
* persistent key with a default lifetime.
* Implementations should support this value if they support persistent
* keys at all.
* Applications should use this value if they have no specific needs that
* are only met by implementation-specific features.
* - \c 2-127: persistent key with a PSA-specified lifetime.
* The PSA Cryptography specification does not define the meaning of these
* values, but other PSA specifications may do so.
* - \c 128-254: persistent key with a vendor-specified lifetime.
* No PSA specification will define the meaning of these values, so
* implementations may choose the meaning freely.
* As a guideline, higher persistence levels should cause a key to survive
* more management events than lower levels.
* - \c 255 = #PSA_KEY_PERSISTENCE_READ_ONLY:
* read-only or write-once key.
* A key with this persistence level cannot be destroyed.
* Implementations that support such keys may either allow their creation
* through the PSA Cryptography API, preferably only to applications with
* the appropriate privilege, or only expose keys created through
* implementation-specific means such as a factory ROM engraving process.
* Note that keys that are read-only due to policy restrictions
* rather than due to physical limitations should not have this
* persistence levels.
*
* \note Key persistence levels are 8-bit values. Key management
* interfaces operate on lifetimes (type ::psa_key_lifetime_t) which
* encode the persistence as the lower 8 bits of a 32-bit value.
*/
typedef uint8_t psa_key_persistence_t;

/** Encoding of key location indicators.
*
* If an implementation of this API can make calls to external
* cryptoprocessors such as secure elements, the location of a key
* indicates which secure element performs the operations on the key.
* If an implementation offers multiple physical locations for persistent
* storage, the location indicator reflects at which physical location
* the key is stored.
*
* This specification defines the following values of location indicators:
* - \c 0: primary local storage.
* All implementations should support this value.
* The primary local storage is typically the same storage area that
* contains the key metadata.
* - \c 1: primary secure element.
* Implementations should support this value if there is a secure element
* attached to the operating environment.
* As a guideline, secure elements may provide higher resistance against
* side channel and physical attacks than the primary local storage, but may
* have restrictions on supported key types, sizes, policies and operations
* and may have different performance characteristics.
* - \c 2-0x7fffff: other locations defined by a PSA specification.
* The PSA Cryptography does not currently assign any meaning to these
* locations, but future versions of this specification or other PSA
* specifications may do so.
* - \c 0x800000-0xffffff: vendor-defined locations.
* No PSA specification will assign a meaning to locations in this range.
*
* \note Key location indicators are 24-bit values. Key management
* interfaces operate on lifetimes (type ::psa_key_lifetime_t) which
* encode the location as the upper 24 bits of a 32-bit value.
*/
typedef uint32_t psa_key_location_t;

/** Encoding of identifiers of persistent keys.
*
* - Applications may freely choose key identifiers in the range
Expand Down
80 changes: 78 additions & 2 deletions include/psa/crypto_values.h
Original file line number Diff line number Diff line change
Expand Up @@ -1539,12 +1539,20 @@
* @{
*/

/** A volatile key only exists as long as the handle to it is not closed.
/** The default lifetime for volatile keys.
*
* A volatile key only exists as long as the handle to it is not closed.
* The key material is guaranteed to be erased on a power reset.
*
* A key with this lifetime is typically stored in the RAM area of the
* PSA Crypto subsystem. However this is an implementation choice.
* If an implementation stores data about the key in a non-volatile memory,
* it must release all the resources associated with the key and erase the
* key material if the calling application terminates.
*/
#define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000)

/** The default storage area for persistent keys.
/** The default lifetime for persistent keys.
*
* A persistent key remains in storage until it is explicitly destroyed or
* until the corresponding storage area is wiped. This specification does
Expand All @@ -1555,9 +1563,77 @@
* This lifetime value is the default storage area for the calling
* application. Implementations may offer other storage areas designated
* by other lifetime values as implementation-specific extensions.
* See ::psa_key_lifetime_t for more information.
*/
#define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001)

/** The persistence level of volatile keys.
*
* See ::psa_key_persistence_t for more information.
*/
#define PSA_KEY_PERSISTENCE_VOLATILE ((psa_key_persistence_t)0x00)

/** The default persistence level for persistent keys.
*
* See ::psa_key_persistence_t for more information.
*/
#define PSA_KEY_PERSISTENCE_DEFAULT ((psa_key_persistence_t)0x01)

/** A persistence level indicating that a key is never destroyed.
*
* See ::psa_key_persistence_t for more information.
*/
#define PSA_KEY_PERSISTENCE_READ_ONLY ((psa_key_persistence_t)0xff)

#define PSA_KEY_LIFETIME_GET_PERSISTENCE(lifetime) \
((psa_key_persistence_t)((lifetime) & 0x000000ff))

#define PSA_KEY_LIFETIME_GET_LOCATION(lifetime) \
((psa_key_location_t)((lifetime) >> 8))

/** Whether a key lifetime indicates that the key is volatile.
*
* A volatile key is automatically destroyed by the implementation when
* the application instance terminates. In particular, a volatile key
* is automatically destroyed on a power reset of the device.
*
* A key that is not volatile is persistent. Persistent keys are
* preserved until the application explicitly destroys them or until an
* implementation-specific device management event occurs (for example,
* a factory reset).
*
* \param lifetime The lifetime value to query (value of type
* ::psa_key_lifetime_t).
*
* \return \c 1 if the key is volatile, otherwise \c 0.
*/
#define PSA_KEY_LIFETIME_IS_VOLATILE(lifetime) \
(PSA_KEY_LIFETIME_GET_PERSISTENCE(lifetime) == \
PSA_KEY_LIFETIME_PERSISTENCE_VOLATILE)

/** Construct a lifetime from a persistence level and a location.
*
* \param persistence The persistence level
* (value of type ::psa_key_persistence_t).
* \param location The location indicator
* (value of type ::psa_key_location_t).
*
* \return The constructed lifetime value.
*/
#define PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(persistence, location) \
((location) << 8 | (persistence))

/** The local storage area for persistent keys.
*
* This storage area is available on all systems that can store persistent
* keys without delegating the storage to a third-party cryptoprocessor.
*
* See ::psa_key_location_t for more information.
*/
#define PSA_KEY_LOCATION_LOCAL_STORAGE ((psa_key_location_t)0x000000)

#define PSA_KEY_LOCATION_VENDOR_FLAG ((psa_key_location_t)0x800000)

/** The minimum value for a key identifier chosen by the application.
*/
#define PSA_KEY_ID_USER_MIN ((psa_app_key_id_t)0x00000001)
Expand Down
1 change: 1 addition & 0 deletions library/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
libmbed*
*.sln
*.vcxproj
/psa_crypto_driver_wrappers.c
Loading