Skip to content

Commit c62cfd5

Browse files
author
itayzafrir
committed
Update example to use crypto key handles APIs
1 parent c278b6e commit c62cfd5

File tree

1 file changed

+40
-26
lines changed

1 file changed

+40
-26
lines changed

main.cpp

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,15 @@ int main(void)
5858
}
5959
#else
6060

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,
6562
psa_key_usage_t key_usage,
6663
psa_algorithm_t alg)
6764
{
6865
psa_status_t status;
69-
psa_key_policy_t policy;
66+
psa_key_policy_t policy = psa_key_policy_init();
7067

71-
psa_key_policy_init(&policy);
7268
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);
7470
ASSERT_STATUS(status, PSA_SUCCESS);
7571
exit:
7672
return status;
@@ -111,7 +107,7 @@ static psa_status_t cipher_operation(psa_cipher_operation_t *operation,
111107
return status;
112108
}
113109

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,
115111
psa_algorithm_t alg,
116112
uint8_t *iv,
117113
size_t iv_size,
@@ -127,7 +123,7 @@ static psa_status_t cipher_encrypt(psa_key_slot_t key_slot,
127123
size_t iv_len = 0;
128124

129125
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);
131127
ASSERT_STATUS(status, PSA_SUCCESS);
132128

133129
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,
142138
return status;
143139
}
144140

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,
146142
psa_algorithm_t alg,
147143
const uint8_t *iv,
148144
size_t iv_size,
@@ -157,7 +153,7 @@ static psa_status_t cipher_decrypt(psa_key_slot_t key_slot,
157153
psa_cipher_operation_t operation;
158154

159155
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);
161157
ASSERT_STATUS(status, PSA_SUCCESS);
162158

163159
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)
187183
uint8_t input[block_size];
188184
uint8_t encrypt[block_size];
189185
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);
190190

191191
status = psa_generate_random(input, sizeof(input));
192192
ASSERT_STATUS(status, PSA_SUCCESS);
193193

194-
status = set_key_policy(key_slot_cipher,
194+
status = set_key_policy(key_handle,
195195
PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
196196
alg);
197197
ASSERT_STATUS(status, PSA_SUCCESS);
198198

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,
200200
NULL, 0);
201201
ASSERT_STATUS(status, PSA_SUCCESS);
202202

203-
status = cipher_encrypt(key_slot_cipher, alg, iv, sizeof(iv),
203+
status = cipher_encrypt(key_handle, alg, iv, sizeof(iv),
204204
input, sizeof(input), part_size,
205205
encrypt, sizeof(encrypt), &output_len);
206206
ASSERT_STATUS(status, PSA_SUCCESS);
207207

208-
status = cipher_decrypt(key_slot_cipher, alg, iv, sizeof(iv),
208+
status = cipher_decrypt(key_handle, alg, iv, sizeof(iv),
209209
encrypt, output_len, part_size,
210210
decrypt, sizeof(decrypt), &output_len);
211211
ASSERT_STATUS(status, PSA_SUCCESS);
@@ -214,7 +214,9 @@ static psa_status_t cipher_example_encrypt_decrypt_aes_cbc_nopad_1_block(void)
214214
ASSERT_STATUS(status, PSA_SUCCESS);
215215

216216
exit:
217-
psa_destroy_key(key_slot_cipher);
217+
if (key_handle != 0) {
218+
psa_destroy_key(key_handle);
219+
}
218220
return status;
219221
}
220222

@@ -233,25 +235,29 @@ static psa_status_t cipher_example_encrypt_decrypt_aes_cbc_pkcs7_multi(void)
233235
size_t output_len = 0;
234236
uint8_t iv[block_size], input[input_size],
235237
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);
236242

237243
status = psa_generate_random(input, sizeof(input));
238244
ASSERT_STATUS(status, PSA_SUCCESS);
239245

240-
status = set_key_policy(key_slot_cipher,
246+
status = set_key_policy(key_handle,
241247
PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
242248
alg);
243249
ASSERT_STATUS(status, PSA_SUCCESS);
244250

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,
246252
NULL, 0);
247253
ASSERT_STATUS(status, PSA_SUCCESS);
248254

249-
status = cipher_encrypt(key_slot_cipher, alg, iv, sizeof(iv),
255+
status = cipher_encrypt(key_handle, alg, iv, sizeof(iv),
250256
input, sizeof(input), part_size,
251257
encrypt, sizeof(encrypt), &output_len);
252258
ASSERT_STATUS(status, PSA_SUCCESS);
253259

254-
status = cipher_decrypt(key_slot_cipher, alg, iv, sizeof(iv),
260+
status = cipher_decrypt(key_handle, alg, iv, sizeof(iv),
255261
encrypt, output_len, part_size,
256262
decrypt, sizeof(decrypt), &output_len);
257263
ASSERT_STATUS(status, PSA_SUCCESS);
@@ -260,7 +266,9 @@ static psa_status_t cipher_example_encrypt_decrypt_aes_cbc_pkcs7_multi(void)
260266
ASSERT_STATUS(status, PSA_SUCCESS);
261267

262268
exit:
263-
psa_destroy_key(key_slot_cipher);
269+
if (key_handle != 0) {
270+
psa_destroy_key(key_handle);
271+
}
264272
return status;
265273
}
266274

@@ -278,25 +286,29 @@ static psa_status_t cipher_example_encrypt_decrypt_aes_ctr_multi(void)
278286
size_t output_len = 0;
279287
uint8_t iv[block_size], input[input_size], encrypt[input_size],
280288
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);
281293

282294
status = psa_generate_random(input, sizeof(input));
283295
ASSERT_STATUS(status, PSA_SUCCESS);
284296

285-
status = set_key_policy(key_slot_cipher,
297+
status = set_key_policy(key_handle,
286298
PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
287299
alg);
288300
ASSERT_STATUS(status, PSA_SUCCESS);
289301

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,
291303
NULL, 0);
292304
ASSERT_STATUS(status, PSA_SUCCESS);
293305

294-
status = cipher_encrypt(key_slot_cipher, alg, iv, sizeof(iv),
306+
status = cipher_encrypt(key_handle, alg, iv, sizeof(iv),
295307
input, sizeof(input), part_size,
296308
encrypt, sizeof(encrypt), &output_len);
297309
ASSERT_STATUS(status, PSA_SUCCESS);
298310

299-
status = cipher_decrypt(key_slot_cipher, alg, iv, sizeof(iv),
311+
status = cipher_decrypt(key_handle, alg, iv, sizeof(iv),
300312
encrypt, output_len, part_size,
301313
decrypt, sizeof(decrypt), &output_len);
302314
ASSERT_STATUS(status, PSA_SUCCESS);
@@ -305,7 +317,9 @@ static psa_status_t cipher_example_encrypt_decrypt_aes_ctr_multi(void)
305317
ASSERT_STATUS(status, PSA_SUCCESS);
306318

307319
exit:
308-
psa_destroy_key(key_slot_cipher);
320+
if (key_handle != 0) {
321+
psa_destroy_key(key_handle);
322+
}
309323
return status;
310324
}
311325

0 commit comments

Comments
 (0)