@@ -58,19 +58,15 @@ int main(void)
58
58
}
59
59
#else
60
60
61
- /* Use key slot 1 for our cipher key. Key slot 0 is reserved as unused. */
62
- static const psa_key_slot_t key_slot_cipher = 1 ;
63
-
64
- static psa_status_t set_key_policy (psa_key_slot_t key_slot,
61
+ static psa_status_t set_key_policy (psa_key_handle_t key_handle,
65
62
psa_key_usage_t key_usage,
66
63
psa_algorithm_t alg)
67
64
{
68
65
psa_status_t status;
69
- psa_key_policy_t policy;
66
+ psa_key_policy_t policy = psa_key_policy_init () ;
70
67
71
- psa_key_policy_init (&policy);
72
68
psa_key_policy_set_usage (&policy, key_usage, alg);
73
- status = psa_set_key_policy (key_slot , &policy);
69
+ status = psa_set_key_policy (key_handle , &policy);
74
70
ASSERT_STATUS (status, PSA_SUCCESS);
75
71
exit:
76
72
return status;
@@ -111,7 +107,7 @@ static psa_status_t cipher_operation(psa_cipher_operation_t *operation,
111
107
return status;
112
108
}
113
109
114
- static psa_status_t cipher_encrypt (psa_key_slot_t key_slot ,
110
+ static psa_status_t cipher_encrypt (psa_key_handle_t key_handle ,
115
111
psa_algorithm_t alg,
116
112
uint8_t *iv,
117
113
size_t iv_size,
@@ -127,7 +123,7 @@ static psa_status_t cipher_encrypt(psa_key_slot_t key_slot,
127
123
size_t iv_len = 0 ;
128
124
129
125
memset (&operation, 0 , sizeof (operation));
130
- status = psa_cipher_encrypt_setup (&operation, key_slot , alg);
126
+ status = psa_cipher_encrypt_setup (&operation, key_handle , alg);
131
127
ASSERT_STATUS (status, PSA_SUCCESS);
132
128
133
129
status = psa_cipher_generate_iv (&operation, iv, iv_size, &iv_len);
@@ -142,7 +138,7 @@ static psa_status_t cipher_encrypt(psa_key_slot_t key_slot,
142
138
return status;
143
139
}
144
140
145
- static psa_status_t cipher_decrypt (psa_key_slot_t key_slot ,
141
+ static psa_status_t cipher_decrypt (psa_key_handle_t key_handle ,
146
142
psa_algorithm_t alg,
147
143
const uint8_t *iv,
148
144
size_t iv_size,
@@ -157,7 +153,7 @@ static psa_status_t cipher_decrypt(psa_key_slot_t key_slot,
157
153
psa_cipher_operation_t operation;
158
154
159
155
memset (&operation, 0 , sizeof (operation));
160
- status = psa_cipher_decrypt_setup (&operation, key_slot , alg);
156
+ status = psa_cipher_decrypt_setup (&operation, key_handle , alg);
161
157
ASSERT_STATUS (status, PSA_SUCCESS);
162
158
163
159
status = psa_cipher_set_iv (&operation, iv, iv_size);
@@ -187,25 +183,29 @@ static psa_status_t cipher_example_encrypt_decrypt_aes_cbc_nopad_1_block(void)
187
183
uint8_t input[block_size];
188
184
uint8_t encrypt[block_size];
189
185
uint8_t decrypt[block_size];
186
+ psa_key_handle_t key_handle = 0 ;
187
+
188
+ status = psa_allocate_key (&key_handle);
189
+ ASSERT_STATUS (status, PSA_SUCCESS);
190
190
191
191
status = psa_generate_random (input, sizeof (input));
192
192
ASSERT_STATUS (status, PSA_SUCCESS);
193
193
194
- status = set_key_policy (key_slot_cipher ,
194
+ status = set_key_policy (key_handle ,
195
195
PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
196
196
alg);
197
197
ASSERT_STATUS (status, PSA_SUCCESS);
198
198
199
- status = psa_generate_key (key_slot_cipher , PSA_KEY_TYPE_AES, key_bits,
199
+ status = psa_generate_key (key_handle , PSA_KEY_TYPE_AES, key_bits,
200
200
NULL , 0 );
201
201
ASSERT_STATUS (status, PSA_SUCCESS);
202
202
203
- status = cipher_encrypt (key_slot_cipher , alg, iv, sizeof (iv),
203
+ status = cipher_encrypt (key_handle , alg, iv, sizeof (iv),
204
204
input, sizeof (input), part_size,
205
205
encrypt, sizeof (encrypt), &output_len);
206
206
ASSERT_STATUS (status, PSA_SUCCESS);
207
207
208
- status = cipher_decrypt (key_slot_cipher , alg, iv, sizeof (iv),
208
+ status = cipher_decrypt (key_handle , alg, iv, sizeof (iv),
209
209
encrypt, output_len, part_size,
210
210
decrypt, sizeof (decrypt), &output_len);
211
211
ASSERT_STATUS (status, PSA_SUCCESS);
@@ -214,7 +214,9 @@ static psa_status_t cipher_example_encrypt_decrypt_aes_cbc_nopad_1_block(void)
214
214
ASSERT_STATUS (status, PSA_SUCCESS);
215
215
216
216
exit:
217
- psa_destroy_key (key_slot_cipher);
217
+ if (key_handle != 0 ) {
218
+ psa_destroy_key (key_handle);
219
+ }
218
220
return status;
219
221
}
220
222
@@ -233,25 +235,29 @@ static psa_status_t cipher_example_encrypt_decrypt_aes_cbc_pkcs7_multi(void)
233
235
size_t output_len = 0 ;
234
236
uint8_t iv[block_size], input[input_size],
235
237
encrypt[input_size + block_size], decrypt[input_size + block_size];
238
+ psa_key_handle_t key_handle = 0 ;
239
+
240
+ status = psa_allocate_key (&key_handle);
241
+ ASSERT_STATUS (status, PSA_SUCCESS);
236
242
237
243
status = psa_generate_random (input, sizeof (input));
238
244
ASSERT_STATUS (status, PSA_SUCCESS);
239
245
240
- status = set_key_policy (key_slot_cipher ,
246
+ status = set_key_policy (key_handle ,
241
247
PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
242
248
alg);
243
249
ASSERT_STATUS (status, PSA_SUCCESS);
244
250
245
- status = psa_generate_key (key_slot_cipher , PSA_KEY_TYPE_AES, key_bits,
251
+ status = psa_generate_key (key_handle , PSA_KEY_TYPE_AES, key_bits,
246
252
NULL , 0 );
247
253
ASSERT_STATUS (status, PSA_SUCCESS);
248
254
249
- status = cipher_encrypt (key_slot_cipher , alg, iv, sizeof (iv),
255
+ status = cipher_encrypt (key_handle , alg, iv, sizeof (iv),
250
256
input, sizeof (input), part_size,
251
257
encrypt, sizeof (encrypt), &output_len);
252
258
ASSERT_STATUS (status, PSA_SUCCESS);
253
259
254
- status = cipher_decrypt (key_slot_cipher , alg, iv, sizeof (iv),
260
+ status = cipher_decrypt (key_handle , alg, iv, sizeof (iv),
255
261
encrypt, output_len, part_size,
256
262
decrypt, sizeof (decrypt), &output_len);
257
263
ASSERT_STATUS (status, PSA_SUCCESS);
@@ -260,7 +266,9 @@ static psa_status_t cipher_example_encrypt_decrypt_aes_cbc_pkcs7_multi(void)
260
266
ASSERT_STATUS (status, PSA_SUCCESS);
261
267
262
268
exit:
263
- psa_destroy_key (key_slot_cipher);
269
+ if (key_handle != 0 ) {
270
+ psa_destroy_key (key_handle);
271
+ }
264
272
return status;
265
273
}
266
274
@@ -278,25 +286,29 @@ static psa_status_t cipher_example_encrypt_decrypt_aes_ctr_multi(void)
278
286
size_t output_len = 0 ;
279
287
uint8_t iv[block_size], input[input_size], encrypt[input_size],
280
288
decrypt[input_size];
289
+ psa_key_handle_t key_handle = 0 ;
290
+
291
+ status = psa_allocate_key (&key_handle);
292
+ ASSERT_STATUS (status, PSA_SUCCESS);
281
293
282
294
status = psa_generate_random (input, sizeof (input));
283
295
ASSERT_STATUS (status, PSA_SUCCESS);
284
296
285
- status = set_key_policy (key_slot_cipher ,
297
+ status = set_key_policy (key_handle ,
286
298
PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
287
299
alg);
288
300
ASSERT_STATUS (status, PSA_SUCCESS);
289
301
290
- status = psa_generate_key (key_slot_cipher , PSA_KEY_TYPE_AES, key_bits,
302
+ status = psa_generate_key (key_handle , PSA_KEY_TYPE_AES, key_bits,
291
303
NULL , 0 );
292
304
ASSERT_STATUS (status, PSA_SUCCESS);
293
305
294
- status = cipher_encrypt (key_slot_cipher , alg, iv, sizeof (iv),
306
+ status = cipher_encrypt (key_handle , alg, iv, sizeof (iv),
295
307
input, sizeof (input), part_size,
296
308
encrypt, sizeof (encrypt), &output_len);
297
309
ASSERT_STATUS (status, PSA_SUCCESS);
298
310
299
- status = cipher_decrypt (key_slot_cipher , alg, iv, sizeof (iv),
311
+ status = cipher_decrypt (key_handle , alg, iv, sizeof (iv),
300
312
encrypt, output_len, part_size,
301
313
decrypt, sizeof (decrypt), &output_len);
302
314
ASSERT_STATUS (status, PSA_SUCCESS);
@@ -305,7 +317,9 @@ static psa_status_t cipher_example_encrypt_decrypt_aes_ctr_multi(void)
305
317
ASSERT_STATUS (status, PSA_SUCCESS);
306
318
307
319
exit:
308
- psa_destroy_key (key_slot_cipher);
320
+ if (key_handle != 0 ) {
321
+ psa_destroy_key (key_handle);
322
+ }
309
323
return status;
310
324
}
311
325
0 commit comments