@@ -164,6 +164,35 @@ static void ram_slots_reset( void )
164
164
ram_min_slot = 0;
165
165
}
166
166
167
+ /* Common parts of key creation.
168
+ *
169
+ * In case of error, zero out ram_slots[slot_number]. But don't
170
+ * do that if the error is PSA_ERROR_DETECTED_BY_DRIVER: in this case
171
+ * you don't need to clean up (ram_slot_reset() will take care of it
172
+ * in the test case function's cleanup code) and it might be wrong
173
+ * (if slot_number is invalid).
174
+ */
175
+ static psa_status_t ram_create_common( psa_drv_se_context_t *context,
176
+ psa_key_slot_number_t slot_number,
177
+ const psa_key_attributes_t *attributes,
178
+ size_t required_storage )
179
+ {
180
+ (void) context;
181
+ DRIVER_ASSERT_RETURN( slot_number < ARRAY_LENGTH( ram_slots ) );
182
+
183
+ ram_slots[slot_number].lifetime = psa_get_key_lifetime( attributes );
184
+ ram_slots[slot_number].type = psa_get_key_type( attributes );
185
+ ram_slots[slot_number].bits = psa_get_key_bits( attributes );
186
+
187
+ if( required_storage > sizeof( ram_slots[slot_number].content ) )
188
+ {
189
+ memset( &ram_slots[slot_number], 0, sizeof( ram_slots[slot_number] ) );
190
+ return( PSA_ERROR_INSUFFICIENT_STORAGE );
191
+ }
192
+
193
+ return( PSA_SUCCESS );
194
+ }
195
+
167
196
/* This function does everything except actually generating key material.
168
197
* After calling it, you must copy the desired key material to
169
198
* ram_slots[slot_number].content. */
@@ -174,7 +203,10 @@ static psa_status_t ram_fake_generate( psa_drv_se_context_t *context,
174
203
size_t pubkey_size,
175
204
size_t *pubkey_length )
176
205
{
177
- (void) context;
206
+ psa_status_t status;
207
+ size_t required_storage =
208
+ PSA_KEY_EXPORT_MAX_SIZE( psa_get_key_type( attributes ),
209
+ psa_get_key_bits( attributes ) );
178
210
179
211
DRIVER_ASSERT_RETURN( *pubkey_length == 0 );
180
212
if( ! PSA_KEY_TYPE_IS_KEY_PAIR( psa_get_key_type( attributes ) ) )
@@ -183,21 +215,9 @@ static psa_status_t ram_fake_generate( psa_drv_se_context_t *context,
183
215
DRIVER_ASSERT_RETURN( pubkey_size == 0 );
184
216
}
185
217
186
- {
187
- /* Check that the key can be stored in the memory slot.
188
- * This check only works for key in a "raw" representation:
189
- * symmetric keys or ECC are ok, but not RSA or FFDH. */
190
- size_t required_storage =
191
- PSA_BITS_TO_BYTES( psa_get_key_bits( attributes ) );
192
- size_t available_storage = sizeof( ram_slots[slot_number].content );
193
- if( required_storage > available_storage )
194
- return( PSA_ERROR_INSUFFICIENT_STORAGE );
195
- }
196
-
197
- ram_slots[slot_number].lifetime = psa_get_key_lifetime( attributes );
198
- ram_slots[slot_number].type = psa_get_key_type( attributes );
199
- ram_slots[slot_number].bits = psa_get_key_bits( attributes );
200
- return( PSA_SUCCESS );
218
+ status = ram_create_common( context, slot_number, attributes,
219
+ required_storage );
220
+ return( status );
201
221
}
202
222
203
223
static psa_status_t ram_import( psa_drv_se_context_t *context,
@@ -207,23 +227,36 @@ static psa_status_t ram_import( psa_drv_se_context_t *context,
207
227
size_t data_length,
208
228
size_t *bits )
209
229
{
210
- (void) context;
211
- DRIVER_ASSERT_RETURN( slot_number < ARRAY_LENGTH( ram_slots ) );
212
- if( data_length > sizeof( ram_slots[slot_number].content ) )
213
- return( PSA_ERROR_INSUFFICIENT_STORAGE );
214
- ram_slots[slot_number].lifetime = psa_get_key_lifetime( attributes );
215
- ram_slots[slot_number].type = psa_get_key_type( attributes );
216
- ram_slots[slot_number].bits = PSA_BYTES_TO_BITS( data_length );
217
- *bits = PSA_BYTES_TO_BITS( data_length );
230
+ psa_key_type_t type = psa_get_key_type( attributes );
231
+ psa_status_t status = ram_create_common( context, slot_number, attributes,
232
+ data_length );
233
+ if( status != PSA_SUCCESS )
234
+ return( status );
235
+
236
+ /* The RAM driver only works for certain key types: raw keys,
237
+ * and ECC key pairs. This is true in particular of the bit-size
238
+ * calculation here. */
239
+ if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
240
+ *bits = PSA_BYTES_TO_BITS( data_length );
241
+ else if ( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
242
+ *bits = PSA_ECC_CURVE_BITS( PSA_KEY_TYPE_GET_CURVE( type ) );
243
+ else
244
+ {
245
+ memset( &ram_slots[slot_number], 0, sizeof( ram_slots[slot_number] ) );
246
+ return( PSA_ERROR_NOT_SUPPORTED );
247
+ }
248
+
249
+ ram_slots[slot_number].bits = *bits;
218
250
memcpy( ram_slots[slot_number].content, data, data_length );
251
+
219
252
return( PSA_SUCCESS );
220
253
}
221
254
222
255
static psa_status_t ram_export( psa_drv_se_context_t *context,
223
256
psa_key_slot_number_t slot_number,
224
- uint8_t *p_data ,
257
+ uint8_t *data ,
225
258
size_t data_size,
226
- size_t *p_data_length )
259
+ size_t *data_length )
227
260
{
228
261
size_t actual_size;
229
262
(void) context;
0 commit comments