Skip to content

Commit ee61b66

Browse files
Merge pull request #3302 from gilles-peskine-arm/psa-lifetime-persistence-indicator
Define some structure for lifetime values
2 parents e24fc7b + fb79dfe commit ee61b66

10 files changed

+360
-149
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Default behavior changes
2+
* In the experimental PSA secure element interface, change the encoding of
3+
key lifetimes to encode a persistence level and the location. Although C
4+
prototypes do not effectively change, code calling
5+
psa_register_se_driver() must be modified to pass the driver's location
6+
instead of the keys' lifetime. If the library is upgraded on an existing
7+
device, keys created with the old lifetime value will not be readable or
8+
removable through Mbed TLS after the upgrade.

include/psa/crypto_se_driver.h

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,10 @@ typedef struct {
119119
* \param[in,out] drv_context The driver context structure.
120120
* \param[in,out] persistent_data A pointer to the persistent data
121121
* that allows writing.
122-
* \param lifetime The lifetime value for which this driver
123-
* is registered.
122+
* \param location The location value for which this driver
123+
* is registered. The driver will be invoked
124+
* for all keys whose lifetime is in this
125+
* location.
124126
*
125127
* \retval #PSA_SUCCESS
126128
* The driver is operational.
@@ -132,7 +134,7 @@ typedef struct {
132134
*/
133135
typedef psa_status_t (*psa_drv_se_init_t)(psa_drv_se_context_t *drv_context,
134136
void *persistent_data,
135-
psa_key_lifetime_t lifetime);
137+
psa_key_location_t location);
136138

137139
#if defined(__DOXYGEN_ONLY__) || !defined(MBEDTLS_PSA_CRYPTO_SE_C)
138140
/* Mbed Crypto with secure element support enabled defines this type in
@@ -1341,17 +1343,19 @@ typedef struct {
13411343
* after psa_crypto_init().
13421344
*
13431345
* \note Implementations store metadata about keys including the lifetime
1344-
* value. Therefore, from one instantiation of the PSA Cryptography
1346+
* value, which contains the driver's location indicator. Therefore,
1347+
* from one instantiation of the PSA Cryptography
13451348
* library to the next one, if there is a key in storage with a certain
13461349
* lifetime value, you must always register the same driver (or an
13471350
* updated version that communicates with the same secure element)
1348-
* with the same lifetime value.
1351+
* with the same location value.
13491352
*
1350-
* \param lifetime The lifetime value through which this driver will
1353+
* \param location The location value through which this driver will
13511354
* be exposed to applications.
1352-
* The values #PSA_KEY_LIFETIME_VOLATILE and
1353-
* #PSA_KEY_LIFETIME_PERSISTENT are reserved and
1354-
* may not be used for drivers. Implementations
1355+
* This driver will be used for all keys such that
1356+
* `location == PSA_KEY_LIFETIME_LOCATION( lifetime )`.
1357+
* The value #PSA_KEY_LOCATION_LOCAL_STORAGE is reserved
1358+
* and may not be used for drivers. Implementations
13551359
* may reserve other values.
13561360
* \param[in] methods The method table of the driver. This structure must
13571361
* remain valid for as long as the cryptography
@@ -1376,7 +1380,7 @@ typedef struct {
13761380
* \return PSA_ERROR_NOT_PERMITTED
13771381
*/
13781382
psa_status_t psa_register_se_driver(
1379-
psa_key_lifetime_t lifetime,
1383+
psa_key_location_t location,
13801384
const psa_drv_se_t *methods);
13811385

13821386
/**@}*/

include/psa/crypto_types.h

Lines changed: 104 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,117 @@ typedef uint32_t psa_algorithm_t;
108108
* The lifetime of a key indicates where it is stored and what system actions
109109
* may create and destroy it.
110110
*
111-
* Keys with the lifetime #PSA_KEY_LIFETIME_VOLATILE are automatically
112-
* destroyed when the application terminates or on a power reset.
111+
* Lifetime values have the following structure:
112+
* - Bits 0-7 (#PSA_KEY_LIFETIME_GET_PERSISTENCE(\c lifetime)):
113+
* persistence level. This value indicates what device management
114+
* actions can cause it to be destroyed. In particular, it indicates
115+
* whether the key is _volatile_ or _persistent_.
116+
* See ::psa_key_persistence_t for more information.
117+
* - Bits 8-31 (#PSA_KEY_LIFETIME_GET_LOCATION(\c lifetime)):
118+
* location indicator. This value indicates where the key is stored
119+
* and where operations on the key are performed.
120+
* See ::psa_key_location_t for more information.
121+
*
122+
* Volatile keys are automatically destroyed when the application instance
123+
* terminates or on a power reset of the device. Persistent keys are
124+
* preserved until the application explicitly destroys them or until an
125+
* implementation-specific device management event occurs (for example,
126+
* a factory reset).
113127
*
114-
* Keys with a lifetime other than #PSA_KEY_LIFETIME_VOLATILE are said
115-
* to be _persistent_.
116-
* Persistent keys are preserved if the application or the system restarts.
117128
* Persistent keys have a key identifier of type #psa_key_id_t.
129+
* This identifier remains valid throughout the lifetime of the key,
130+
* even if the application instance that created the key terminates.
118131
* The application can call psa_open_key() to open a persistent key that
119132
* it created previously.
133+
*
134+
* This specification defines two basic lifetime values:
135+
* - Keys with the lifetime #PSA_KEY_LIFETIME_VOLATILE are volatile.
136+
* All implementations should support this lifetime.
137+
* - Keys with the lifetime #PSA_KEY_LIFETIME_PERSISTENT are persistent.
138+
* All implementations that have access to persistent storage with
139+
* appropriate security guarantees should support this lifetime.
120140
*/
121141
typedef uint32_t psa_key_lifetime_t;
122142

143+
/** Encoding of key persistence levels.
144+
*
145+
* What distinguishes different persistence levels is what device management
146+
* events may cause keys to be destroyed. _Volatile_ keys are destroyed
147+
* by a power reset. Persistent keys may be destroyed by events such as
148+
* a transfer of ownership or a factory reset. What management events
149+
* actually affect persistent keys at different levels is outside the
150+
* scope of the PSA Cryptography specification.
151+
*
152+
* This specification defines the following values of persistence levels:
153+
* - \c 0 = #PSA_KEY_PERSISTENCE_VOLATILE: volatile key.
154+
* A volatile key is automatically destroyed by the implementation when
155+
* the application instance terminates. In particular, a volatile key
156+
* is automatically destroyed on a power reset of the device.
157+
* - \c 1 = #PSA_KEY_PERSISTENCE_DEFAULT:
158+
* persistent key with a default lifetime.
159+
* Implementations should support this value if they support persistent
160+
* keys at all.
161+
* Applications should use this value if they have no specific needs that
162+
* are only met by implementation-specific features.
163+
* - \c 2-127: persistent key with a PSA-specified lifetime.
164+
* The PSA Cryptography specification does not define the meaning of these
165+
* values, but other PSA specifications may do so.
166+
* - \c 128-254: persistent key with a vendor-specified lifetime.
167+
* No PSA specification will define the meaning of these values, so
168+
* implementations may choose the meaning freely.
169+
* As a guideline, higher persistence levels should cause a key to survive
170+
* more management events than lower levels.
171+
* - \c 255 = #PSA_KEY_PERSISTENCE_READ_ONLY:
172+
* read-only or write-once key.
173+
* A key with this persistence level cannot be destroyed.
174+
* Implementations that support such keys may either allow their creation
175+
* through the PSA Cryptography API, preferably only to applications with
176+
* the appropriate privilege, or only expose keys created through
177+
* implementation-specific means such as a factory ROM engraving process.
178+
* Note that keys that are read-only due to policy restrictions
179+
* rather than due to physical limitations should not have this
180+
* persistence levels.
181+
*
182+
* \note Key persistence levels are 8-bit values. Key management
183+
* interfaces operate on lifetimes (type ::psa_key_lifetime_t) which
184+
* encode the persistence as the lower 8 bits of a 32-bit value.
185+
*/
186+
typedef uint8_t psa_key_persistence_t;
187+
188+
/** Encoding of key location indicators.
189+
*
190+
* If an implementation of this API can make calls to external
191+
* cryptoprocessors such as secure elements, the location of a key
192+
* indicates which secure element performs the operations on the key.
193+
* If an implementation offers multiple physical locations for persistent
194+
* storage, the location indicator reflects at which physical location
195+
* the key is stored.
196+
*
197+
* This specification defines the following values of location indicators:
198+
* - \c 0: primary local storage.
199+
* All implementations should support this value.
200+
* The primary local storage is typically the same storage area that
201+
* contains the key metadata.
202+
* - \c 1: primary secure element.
203+
* Implementations should support this value if there is a secure element
204+
* attached to the operating environment.
205+
* As a guideline, secure elements may provide higher resistance against
206+
* side channel and physical attacks than the primary local storage, but may
207+
* have restrictions on supported key types, sizes, policies and operations
208+
* and may have different performance characteristics.
209+
* - \c 2-0x7fffff: other locations defined by a PSA specification.
210+
* The PSA Cryptography API does not currently assign any meaning to these
211+
* locations, but future versions of this specification or other PSA
212+
* specifications may do so.
213+
* - \c 0x800000-0xffffff: vendor-defined locations.
214+
* No PSA specification will assign a meaning to locations in this range.
215+
*
216+
* \note Key location indicators are 24-bit values. Key management
217+
* interfaces operate on lifetimes (type ::psa_key_lifetime_t) which
218+
* encode the location as the upper 24 bits of a 32-bit value.
219+
*/
220+
typedef uint32_t psa_key_location_t;
221+
123222
/** Encoding of identifiers of persistent keys.
124223
*
125224
* - Applications may freely choose key identifiers in the range

include/psa/crypto_values.h

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1539,12 +1539,20 @@
15391539
* @{
15401540
*/
15411541

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

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

1570+
/** The persistence level of volatile keys.
1571+
*
1572+
* See ::psa_key_persistence_t for more information.
1573+
*/
1574+
#define PSA_KEY_PERSISTENCE_VOLATILE ((psa_key_persistence_t)0x00)
1575+
1576+
/** The default persistence level for persistent keys.
1577+
*
1578+
* See ::psa_key_persistence_t for more information.
1579+
*/
1580+
#define PSA_KEY_PERSISTENCE_DEFAULT ((psa_key_persistence_t)0x01)
1581+
1582+
/** A persistence level indicating that a key is never destroyed.
1583+
*
1584+
* See ::psa_key_persistence_t for more information.
1585+
*/
1586+
#define PSA_KEY_PERSISTENCE_READ_ONLY ((psa_key_persistence_t)0xff)
1587+
1588+
#define PSA_KEY_LIFETIME_GET_PERSISTENCE(lifetime) \
1589+
((psa_key_persistence_t)((lifetime) & 0x000000ff))
1590+
1591+
#define PSA_KEY_LIFETIME_GET_LOCATION(lifetime) \
1592+
((psa_key_location_t)((lifetime) >> 8))
1593+
1594+
/** Whether a key lifetime indicates that the key is volatile.
1595+
*
1596+
* A volatile key is automatically destroyed by the implementation when
1597+
* the application instance terminates. In particular, a volatile key
1598+
* is automatically destroyed on a power reset of the device.
1599+
*
1600+
* A key that is not volatile is persistent. Persistent keys are
1601+
* preserved until the application explicitly destroys them or until an
1602+
* implementation-specific device management event occurs (for example,
1603+
* a factory reset).
1604+
*
1605+
* \param lifetime The lifetime value to query (value of type
1606+
* ::psa_key_lifetime_t).
1607+
*
1608+
* \return \c 1 if the key is volatile, otherwise \c 0.
1609+
*/
1610+
#define PSA_KEY_LIFETIME_IS_VOLATILE(lifetime) \
1611+
(PSA_KEY_LIFETIME_GET_PERSISTENCE(lifetime) == \
1612+
PSA_KEY_LIFETIME_PERSISTENCE_VOLATILE)
1613+
1614+
/** Construct a lifetime from a persistence level and a location.
1615+
*
1616+
* \param persistence The persistence level
1617+
* (value of type ::psa_key_persistence_t).
1618+
* \param location The location indicator
1619+
* (value of type ::psa_key_location_t).
1620+
*
1621+
* \return The constructed lifetime value.
1622+
*/
1623+
#define PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(persistence, location) \
1624+
((location) << 8 | (persistence))
1625+
1626+
/** The local storage area for persistent keys.
1627+
*
1628+
* This storage area is available on all systems that can store persistent
1629+
* keys without delegating the storage to a third-party cryptoprocessor.
1630+
*
1631+
* See ::psa_key_location_t for more information.
1632+
*/
1633+
#define PSA_KEY_LOCATION_LOCAL_STORAGE ((psa_key_location_t)0x000000)
1634+
1635+
#define PSA_KEY_LOCATION_VENDOR_FLAG ((psa_key_location_t)0x800000)
1636+
15611637
/** The minimum value for a key identifier chosen by the application.
15621638
*/
15631639
#define PSA_KEY_ID_USER_MIN ((psa_app_key_id_t)0x00000001)

0 commit comments

Comments
 (0)