@@ -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,7 @@ 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
+ psa_destroy_key (key_handle );
218
218
return status;
219
219
}
220
220
@@ -233,25 +233,29 @@ static psa_status_t cipher_example_encrypt_decrypt_aes_cbc_pkcs7_multi(void)
233
233
size_t output_len = 0 ;
234
234
uint8_t iv[block_size], input[input_size],
235
235
encrypt[input_size + block_size], decrypt[input_size + block_size];
236
+ psa_key_handle_t key_handle = 0 ;
237
+
238
+ status = psa_allocate_key (&key_handle);
239
+ ASSERT_STATUS (status, PSA_SUCCESS);
236
240
237
241
status = psa_generate_random (input, sizeof (input));
238
242
ASSERT_STATUS (status, PSA_SUCCESS);
239
243
240
- status = set_key_policy (key_slot_cipher ,
244
+ status = set_key_policy (key_handle ,
241
245
PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
242
246
alg);
243
247
ASSERT_STATUS (status, PSA_SUCCESS);
244
248
245
- status = psa_generate_key (key_slot_cipher , PSA_KEY_TYPE_AES, key_bits,
249
+ status = psa_generate_key (key_handle , PSA_KEY_TYPE_AES, key_bits,
246
250
NULL , 0 );
247
251
ASSERT_STATUS (status, PSA_SUCCESS);
248
252
249
- status = cipher_encrypt (key_slot_cipher , alg, iv, sizeof (iv),
253
+ status = cipher_encrypt (key_handle , alg, iv, sizeof (iv),
250
254
input, sizeof (input), part_size,
251
255
encrypt, sizeof (encrypt), &output_len);
252
256
ASSERT_STATUS (status, PSA_SUCCESS);
253
257
254
- status = cipher_decrypt (key_slot_cipher , alg, iv, sizeof (iv),
258
+ status = cipher_decrypt (key_handle , alg, iv, sizeof (iv),
255
259
encrypt, output_len, part_size,
256
260
decrypt, sizeof (decrypt), &output_len);
257
261
ASSERT_STATUS (status, PSA_SUCCESS);
@@ -260,7 +264,7 @@ static psa_status_t cipher_example_encrypt_decrypt_aes_cbc_pkcs7_multi(void)
260
264
ASSERT_STATUS (status, PSA_SUCCESS);
261
265
262
266
exit:
263
- psa_destroy_key (key_slot_cipher );
267
+ psa_destroy_key (key_handle );
264
268
return status;
265
269
}
266
270
@@ -278,25 +282,29 @@ static psa_status_t cipher_example_encrypt_decrypt_aes_ctr_multi(void)
278
282
size_t output_len = 0 ;
279
283
uint8_t iv[block_size], input[input_size], encrypt[input_size],
280
284
decrypt[input_size];
285
+ psa_key_handle_t key_handle = 0 ;
286
+
287
+ status = psa_allocate_key (&key_handle);
288
+ ASSERT_STATUS (status, PSA_SUCCESS);
281
289
282
290
status = psa_generate_random (input, sizeof (input));
283
291
ASSERT_STATUS (status, PSA_SUCCESS);
284
292
285
- status = set_key_policy (key_slot_cipher ,
293
+ status = set_key_policy (key_handle ,
286
294
PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
287
295
alg);
288
296
ASSERT_STATUS (status, PSA_SUCCESS);
289
297
290
- status = psa_generate_key (key_slot_cipher , PSA_KEY_TYPE_AES, key_bits,
298
+ status = psa_generate_key (key_handle , PSA_KEY_TYPE_AES, key_bits,
291
299
NULL , 0 );
292
300
ASSERT_STATUS (status, PSA_SUCCESS);
293
301
294
- status = cipher_encrypt (key_slot_cipher , alg, iv, sizeof (iv),
302
+ status = cipher_encrypt (key_handle , alg, iv, sizeof (iv),
295
303
input, sizeof (input), part_size,
296
304
encrypt, sizeof (encrypt), &output_len);
297
305
ASSERT_STATUS (status, PSA_SUCCESS);
298
306
299
- status = cipher_decrypt (key_slot_cipher , alg, iv, sizeof (iv),
307
+ status = cipher_decrypt (key_handle , alg, iv, sizeof (iv),
300
308
encrypt, output_len, part_size,
301
309
decrypt, sizeof (decrypt), &output_len);
302
310
ASSERT_STATUS (status, PSA_SUCCESS);
@@ -305,7 +313,7 @@ static psa_status_t cipher_example_encrypt_decrypt_aes_ctr_multi(void)
305
313
ASSERT_STATUS (status, PSA_SUCCESS);
306
314
307
315
exit:
308
- psa_destroy_key (key_slot_cipher );
316
+ psa_destroy_key (key_handle );
309
317
return status;
310
318
}
311
319
0 commit comments