@@ -72,9 +72,10 @@ with other function calls.
72
72
73
73
This example shows how to import a key:
74
74
``` C
75
+ void import_a_key (const uint8_t * key, size_t key_len)
76
+ {
75
77
psa_status_t status;
76
78
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
77
- uint8_t data[] = AES_KEY;
78
79
psa_key_handle_t handle;
79
80
80
81
printf("Import an AES key...\t");
@@ -94,7 +95,7 @@ This example shows how to import a key:
94
95
psa_set_key_bits(&attributes, 128);
95
96
96
97
/* Import the key */
97
- status = psa_import_key(&attributes, data, sizeof(data) , &handle);
98
+ status = psa_import_key(&attributes, key, key_len , &handle);
98
99
if (status != PSA_SUCCESS) {
99
100
printf("Failed to import key\n");
100
101
return;
@@ -108,6 +109,7 @@ This example shows how to import a key:
108
109
psa_destroy_key(handle);
109
110
110
111
mbedtls_psa_crypto_free();
112
+ }
111
113
```
112
114
113
115
### Signing a message using RSA
@@ -123,9 +125,10 @@ Mbed Crypto supports encrypting, decrypting, signing and verifying messages usin
123
125
124
126
This example shows how to sign a hash that has already been calculated:
125
127
```C
128
+ void sign_a_message_using_rsa(const uint8_t *key, size_t key_len)
129
+ {
126
130
psa_status_t status;
127
131
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
128
- uint8_t key[] = RSA_KEY;
129
132
uint8_t hash[32] = {0x50, 0xd8, 0x58, 0xe0, 0x98, 0x5e, 0xcc, 0x7f,
130
133
0x60, 0x41, 0x8a, 0xaf, 0x0c, 0xc5, 0xab, 0x58,
131
134
0x7f, 0x42, 0xc2, 0x57, 0x0a, 0x88, 0x40, 0x95,
@@ -151,7 +154,7 @@ This example shows how to sign a hash that has already been calculated:
151
154
psa_set_key_bits(&attributes, 1024);
152
155
153
156
/* Import the key */
154
- status = psa_import_key(&attributes, key, sizeof(key) , &handle);
157
+ status = psa_import_key(&attributes, key, key_len , &handle);
155
158
if (status != PSA_SUCCESS) {
156
159
printf("Failed to import key\n");
157
160
return;
@@ -176,6 +179,7 @@ This example shows how to sign a hash that has already been calculated:
176
179
psa_destroy_key(handle);
177
180
178
181
mbedtls_psa_crypto_free();
182
+ }
179
183
```
180
184
181
185
### Using symmetric ciphers
@@ -196,6 +200,8 @@ Mbed Crypto supports encrypting and decrypting messages using various symmetric
196
200
197
201
This example shows how to encrypt data using an AES (Advanced Encryption Standard) key in CBC (Cipher Block Chaining) mode with no padding (assuming all prerequisites have been fulfilled):
198
202
``` c
203
+ void encrypt_with_symmetric_ciphers (const uint8_t * key, size_t key_len)
204
+ {
199
205
enum {
200
206
block_size = PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES),
201
207
};
@@ -205,7 +211,6 @@ This example shows how to encrypt data using an AES (Advanced Encryption Standar
205
211
uint8_t plaintext[ block_size] = SOME_PLAINTEXT;
206
212
uint8_t iv[ block_size] ;
207
213
size_t iv_len;
208
- uint8_t key[] = AES_KEY;
209
214
uint8_t output[ block_size] ;
210
215
size_t output_len;
211
216
psa_key_handle_t handle;
@@ -227,7 +232,7 @@ This example shows how to encrypt data using an AES (Advanced Encryption Standar
227
232
psa_set_key_algorithm(&attributes, alg);
228
233
psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
229
234
psa_set_key_bits(&attributes, 128);
230
- status = psa_import_key(&attributes, key, sizeof(key) , &handle);
235
+ status = psa_import_key(&attributes, key, key_len , &handle);
231
236
if (status != PSA_SUCCESS) {
232
237
printf("Failed to import a key\n");
233
238
return;
@@ -266,6 +271,7 @@ This example shows how to encrypt data using an AES (Advanced Encryption Standar
266
271
psa_destroy_key(handle);
267
272
268
273
mbedtls_psa_crypto_free();
274
+ }
269
275
```
270
276
271
277
**To decrypt a message with a symmetric cipher:**
@@ -279,6 +285,8 @@ This example shows how to encrypt data using an AES (Advanced Encryption Standar
279
285
This example shows how to decrypt encrypted data using an AES key in CBC mode with no padding
280
286
(assuming all prerequisites have been fulfilled):
281
287
```c
288
+ void decrypt_with_symmetric_ciphers(const uint8_t *key, size_t key_len)
289
+ {
282
290
enum {
283
291
block_size = PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES),
284
292
};
@@ -288,7 +296,6 @@ This example shows how to decrypt encrypted data using an AES key in CBC mode wi
288
296
psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
289
297
uint8_t ciphertext[block_size] = SOME_CIPHERTEXT;
290
298
uint8_t iv[block_size] = ENCRYPTED_WITH_IV;
291
- uint8_t key[] = AES_KEY;
292
299
uint8_t output[block_size];
293
300
size_t output_len;
294
301
psa_key_handle_t handle;
@@ -309,7 +316,7 @@ This example shows how to decrypt encrypted data using an AES key in CBC mode wi
309
316
psa_set_key_algorithm(&attributes, alg);
310
317
psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
311
318
psa_set_key_bits(&attributes, 128);
312
- status = psa_import_key(&attributes, key, sizeof(key) , &handle);
319
+ status = psa_import_key(&attributes, key, key_len , &handle);
313
320
if (status != PSA_SUCCESS) {
314
321
printf("Failed to import a key\n");
315
322
return;
@@ -348,6 +355,7 @@ This example shows how to decrypt encrypted data using an AES key in CBC mode wi
348
355
psa_destroy_key(handle);
349
356
350
357
mbedtls_psa_crypto_free();
358
+ }
351
359
```
352
360
353
361
#### Handling cipher operation contexts
0 commit comments