Skip to content

Commit 00d44c7

Browse files
author
David Saada
committed
Modify PSA related error codes and types:
- Change values of PSA error codes according to PSA spec. - Change ITS related types to the updated ones in the PSA spec.
1 parent 2d7e5fe commit 00d44c7

File tree

4 files changed

+53
-133
lines changed

4 files changed

+53
-133
lines changed

include/psa/crypto_values.h

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,18 @@
4040
* @{
4141
*/
4242

43-
#if !defined(PSA_SUCCESS)
44-
/* If PSA_SUCCESS is defined, assume that PSA crypto is being used
45-
* together with PSA IPC, which also defines the identifier
46-
* PSA_SUCCESS. We must not define PSA_SUCCESS ourselves in that case;
47-
* the other error code names don't clash. This is a temporary hack
48-
* until we unify error reporting in PSA IPC and PSA crypto.
49-
*
50-
* Note that psa_defs.h must be included before this header!
43+
/* PSA error codes */
44+
/* List should comply with the ones defined in https://github.com/ARMmbed/PSA-IPC-doc/blob/master/allocated_errors.h
5145
*/
5246
/** The action was completed successfully. */
5347
#define PSA_SUCCESS ((psa_status_t)0)
54-
#endif /* !defined(PSA_SUCCESS) */
5548

5649
/** An error occurred that does not correspond to any defined
5750
* failure cause.
5851
*
5952
* Implementations may use this error code if none of the other standard
6053
* error codes are applicable. */
61-
#define PSA_ERROR_UNKNOWN_ERROR ((psa_status_t)1)
54+
#define PSA_ERROR_GENERIC_ERROR ((psa_status_t)-132)
6255

6356
/** The requested operation or a parameter is not supported
6457
* by this implementation.
@@ -67,7 +60,7 @@
6760
* parameter such as a key type, algorithm, etc. is not recognized.
6861
* If a combination of parameters is recognized and identified as
6962
* not valid, return #PSA_ERROR_INVALID_ARGUMENT instead. */
70-
#define PSA_ERROR_NOT_SUPPORTED ((psa_status_t)2)
63+
#define PSA_ERROR_NOT_SUPPORTED ((psa_status_t)-134)
7164

7265
/** The requested action is denied by a policy.
7366
*
@@ -80,7 +73,7 @@
8073
* not valid or not supported, it is unspecified whether the function
8174
* returns #PSA_ERROR_NOT_PERMITTED, #PSA_ERROR_NOT_SUPPORTED or
8275
* #PSA_ERROR_INVALID_ARGUMENT. */
83-
#define PSA_ERROR_NOT_PERMITTED ((psa_status_t)3)
76+
#define PSA_ERROR_NOT_PERMITTED ((psa_status_t)-133)
8477

8578
/** An output buffer is too small.
8679
*
@@ -92,23 +85,23 @@
9285
* buffer would succeed. However implementations may return this
9386
* error if a function has invalid or unsupported parameters in addition
9487
* to the parameters that determine the necessary output buffer size. */
95-
#define PSA_ERROR_BUFFER_TOO_SMALL ((psa_status_t)4)
88+
#define PSA_ERROR_BUFFER_TOO_SMALL ((psa_status_t)-138)
9689

9790
/** A slot is occupied, but must be empty to carry out the
9891
* requested action.
9992
*
10093
* If a handle is invalid, it does not designate an occupied slot.
10194
* The error for an invalid handle is #PSA_ERROR_INVALID_HANDLE.
10295
*/
103-
#define PSA_ERROR_OCCUPIED_SLOT ((psa_status_t)5)
96+
#define PSA_ERROR_OCCUPIED_SLOT ((psa_status_t)-153)
10497

10598
/** A slot is empty, but must be occupied to carry out the
10699
* requested action.
107100
*
108101
* If a handle is invalid, it does not designate an empty slot.
109102
* The error for an invalid handle is #PSA_ERROR_INVALID_HANDLE.
110103
*/
111-
#define PSA_ERROR_EMPTY_SLOT ((psa_status_t)6)
104+
#define PSA_ERROR_EMPTY_SLOT ((psa_status_t)-154)
112105

113106
/** The requested action cannot be performed in the current state.
114107
*
@@ -120,7 +113,7 @@
120113
* that a key slot is occupied when it needs to be free or vice versa,
121114
* but shall return #PSA_ERROR_OCCUPIED_SLOT or #PSA_ERROR_EMPTY_SLOT
122115
* as applicable. */
123-
#define PSA_ERROR_BAD_STATE ((psa_status_t)7)
116+
#define PSA_ERROR_BAD_STATE ((psa_status_t)-137)
124117

125118
/** The parameters passed to the function are invalid.
126119
*
@@ -136,13 +129,13 @@
136129
* key handle is invalid, but shall return #PSA_ERROR_INVALID_HANDLE
137130
* instead.
138131
*/
139-
#define PSA_ERROR_INVALID_ARGUMENT ((psa_status_t)8)
132+
#define PSA_ERROR_INVALID_ARGUMENT ((psa_status_t)-135)
140133

141134
/** There is not enough runtime memory.
142135
*
143136
* If the action is carried out across multiple security realms, this
144137
* error can refer to available memory in any of the security realms. */
145-
#define PSA_ERROR_INSUFFICIENT_MEMORY ((psa_status_t)9)
138+
#define PSA_ERROR_INSUFFICIENT_MEMORY ((psa_status_t)-141)
146139

147140
/** There is not enough persistent storage.
148141
*
@@ -151,7 +144,7 @@
151144
* many functions that do not otherwise access storage may return this
152145
* error code if the implementation requires a mandatory log entry for
153146
* the requested action and the log storage space is full. */
154-
#define PSA_ERROR_INSUFFICIENT_STORAGE ((psa_status_t)10)
147+
#define PSA_ERROR_INSUFFICIENT_STORAGE ((psa_status_t)-142)
155148

156149
/** There was a communication failure inside the implementation.
157150
*
@@ -168,7 +161,7 @@
168161
* cryptoprocessor but there was a breakdown of communication before
169162
* the cryptoprocessor could report the status to the application.
170163
*/
171-
#define PSA_ERROR_COMMUNICATION_FAILURE ((psa_status_t)11)
164+
#define PSA_ERROR_COMMUNICATION_FAILURE ((psa_status_t)-145)
172165

173166
/** There was a storage failure that may have led to data loss.
174167
*
@@ -193,13 +186,13 @@
193186
* permanent storage corruption. However application writers should
194187
* keep in mind that transient errors while reading the storage may be
195188
* reported using this error code. */
196-
#define PSA_ERROR_STORAGE_FAILURE ((psa_status_t)12)
189+
#define PSA_ERROR_STORAGE_FAILURE ((psa_status_t)-146)
197190

198191
/** A hardware failure was detected.
199192
*
200193
* A hardware failure may be transient or permanent depending on the
201194
* cause. */
202-
#define PSA_ERROR_HARDWARE_FAILURE ((psa_status_t)13)
195+
#define PSA_ERROR_HARDWARE_FAILURE ((psa_status_t)-147)
203196

204197
/** A tampering attempt was detected.
205198
*
@@ -230,7 +223,7 @@
230223
* This error indicates an attack against the application. Implementations
231224
* shall not return this error code as a consequence of the behavior of
232225
* the application itself. */
233-
#define PSA_ERROR_TAMPERING_DETECTED ((psa_status_t)14)
226+
#define PSA_ERROR_TAMPERING_DETECTED ((psa_status_t)-151)
234227

235228
/** There is not enough entropy to generate random data needed
236229
* for the requested action.
@@ -249,7 +242,7 @@
249242
* secure pseudorandom generator (PRNG). However implementations may return
250243
* this error at any time if a policy requires the PRNG to be reseeded
251244
* during normal operation. */
252-
#define PSA_ERROR_INSUFFICIENT_ENTROPY ((psa_status_t)15)
245+
#define PSA_ERROR_INSUFFICIENT_ENTROPY ((psa_status_t)-148)
253246

254247
/** The signature, MAC or hash is incorrect.
255248
*
@@ -259,7 +252,7 @@
259252
*
260253
* If the value to verify has an invalid size, implementations may return
261254
* either #PSA_ERROR_INVALID_ARGUMENT or #PSA_ERROR_INVALID_SIGNATURE. */
262-
#define PSA_ERROR_INVALID_SIGNATURE ((psa_status_t)16)
255+
#define PSA_ERROR_INVALID_SIGNATURE ((psa_status_t)-149)
263256

264257
/** The decrypted padding is incorrect.
265258
*
@@ -275,17 +268,17 @@
275268
* as close as possible to indistinguishable to an external observer.
276269
* In particular, the timing of a decryption operation should not
277270
* depend on the validity of the padding. */
278-
#define PSA_ERROR_INVALID_PADDING ((psa_status_t)17)
271+
#define PSA_ERROR_INVALID_PADDING ((psa_status_t)-150)
279272

280273
/** The generator has insufficient capacity left.
281274
*
282275
* Once a function returns this error, attempts to read from the
283276
* generator will always return this error. */
284-
#define PSA_ERROR_INSUFFICIENT_CAPACITY ((psa_status_t)18)
277+
#define PSA_ERROR_INSUFFICIENT_CAPACITY ((psa_status_t)-155)
285278

286279
/** The key handle is not valid.
287280
*/
288-
#define PSA_ERROR_INVALID_HANDLE ((psa_status_t)19)
281+
#define PSA_ERROR_INVALID_HANDLE ((psa_status_t)-136)
289282

290283
/**@}*/
291284

library/psa_crypto.c

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ static psa_status_t mbedtls_to_psa_error( int ret )
346346
return( PSA_ERROR_HARDWARE_FAILURE );
347347

348348
default:
349-
return( PSA_ERROR_UNKNOWN_ERROR );
349+
return( PSA_ERROR_GENERIC_ERROR );
350350
}
351351
}
352352

@@ -3098,7 +3098,7 @@ psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
30983098
size_t output_size,
30993099
size_t *output_length )
31003100
{
3101-
psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
3101+
psa_status_t status = PSA_ERROR_GENERIC_ERROR;
31023102
int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
31033103
uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
31043104

@@ -4391,45 +4391,12 @@ psa_status_t psa_generate_random( uint8_t *output,
43914391

43924392
#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
43934393

4394-
/* Support function for error conversion between psa_its error codes to psa crypto */
4395-
static psa_status_t its_to_psa_error( psa_its_status_t ret )
4396-
{
4397-
switch( ret )
4398-
{
4399-
case PSA_ITS_SUCCESS:
4400-
return( PSA_SUCCESS );
4401-
4402-
case PSA_ITS_ERROR_UID_NOT_FOUND:
4403-
return( PSA_ERROR_EMPTY_SLOT );
4404-
4405-
case PSA_ITS_ERROR_STORAGE_FAILURE:
4406-
return( PSA_ERROR_STORAGE_FAILURE );
4407-
4408-
case PSA_ITS_ERROR_INSUFFICIENT_SPACE:
4409-
return( PSA_ERROR_INSUFFICIENT_STORAGE );
4410-
4411-
case PSA_ITS_ERROR_OFFSET_INVALID:
4412-
case PSA_ITS_ERROR_INCORRECT_SIZE:
4413-
case PSA_ITS_ERROR_INVALID_ARGUMENTS:
4414-
return( PSA_ERROR_INVALID_ARGUMENT );
4415-
4416-
case PSA_ITS_ERROR_FLAGS_NOT_SUPPORTED:
4417-
return( PSA_ERROR_NOT_SUPPORTED );
4418-
4419-
case PSA_ITS_ERROR_WRITE_ONCE:
4420-
return( PSA_ERROR_OCCUPIED_SLOT );
4421-
4422-
default:
4423-
return( PSA_ERROR_UNKNOWN_ERROR );
4424-
}
4425-
}
44264394

44274395
psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
44284396
size_t seed_size )
44294397
{
44304398
psa_status_t status;
4431-
psa_its_status_t its_status;
4432-
struct psa_its_info_t p_info;
4399+
struct psa_storage_info_t p_info;
44334400
if( global_data.initialized )
44344401
return( PSA_ERROR_NOT_PERMITTED );
44354402

@@ -4438,15 +4405,13 @@ psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
44384405
( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
44394406
return( PSA_ERROR_INVALID_ARGUMENT );
44404407

4441-
its_status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
4442-
status = its_to_psa_error( its_status );
4408+
status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
44434409

4444-
if( PSA_ITS_ERROR_UID_NOT_FOUND == its_status ) /* No seed exists */
4410+
if( PSA_ERROR_DOES_NOT_EXIST == status ) /* No seed exists */
44454411
{
4446-
its_status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
4447-
status = its_to_psa_error( its_status );
4412+
status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
44484413
}
4449-
else if( PSA_ITS_SUCCESS == its_status )
4414+
else if( PSA_SUCCESS == status )
44504415
{
44514416
/* You should not be here. Seed needs to be injected only once */
44524417
status = PSA_ERROR_NOT_PERMITTED;

0 commit comments

Comments
 (0)