Skip to content

Commit d6292ca

Browse files
authored
Merge pull request #6 from Patater/initializers
Add initializers for crypto structs
2 parents d668bae + 9e919c6 commit d6292ca

11 files changed

+459
-192
lines changed

docs/getting_started.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,13 @@ This allows the key in the key slot to be used for RSA signing.
116116
int key_slot = 1;
117117
unsigned char key[] = "RSA_KEY";
118118
unsigned char payload[] = "ASYMMETRIC_INPUT_FOR_SIGN";
119-
psa_key_policy_t policy;
119+
psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
120120
unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
121121
size_t signature_length;
122122
123123
status = psa_crypto_init();
124124
125125
/* Import the key */
126-
psa_key_policy_init(&policy);
127126
psa_key_policy_set_usage(&policy, PSA_KEY_USAGE_SIGN,
128127
PSA_ALG_RSA_PKCS1V15_SIGN_RAW);
129128
status = psa_set_key_policy(key_slot, &policy);
@@ -343,7 +342,7 @@ At this point the derived key slot holds a new 128-bit AES-CTR encryption key de
343342
```C
344343
psa_key_slot_t base_key = 1;
345344
psa_key_slot_t derived_key = 2;
346-
psa_key_policy_t policy;
345+
psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
347346
348347
unsigned char key[] = {
349348
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
@@ -358,14 +357,14 @@ At this point the derived key slot holds a new 128-bit AES-CTR encryption key de
358357
0xf7, 0xf8, 0xf9 };
359358
360359
psa_algorithm_t alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
360+
psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
361361
psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
362362
size_t derived_bits = 128;
363363
size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
364364
365365
status = psa_crypto_init();
366366
367367
/* Import a key for use in key derivation, if such a key has already been imported you can skip this part */
368-
psa_key_policy_init(&policy);
369368
psa_key_policy_set_usage(&policy, PSA_KEY_USAGE_DERIVE, alg);
370369
status = psa_set_key_policy(base_key, &policy);
371370
@@ -416,12 +415,12 @@ To authenticate and encrypt a message:
416415
size_t output_size = 0;
417416
size_t output_length = 0;
418417
size_t tag_length = 16;
418+
psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
419419

420420
output_size = sizeof(input_data) + tag_length;
421421
output_data = malloc(output_size);
422422
status = psa_crypto_init();
423423

424-
psa_key_policy_init(&policy);
425424
psa_key_policy_set_usage(&policy, PSA_KEY_USAGE_ENCRYPT, PSA_ALG_CCM);
426425
status = psa_set_key_policy(slot, &policy);
427426

@@ -463,12 +462,12 @@ To authenticate and decrypt a message:
463462
unsigned char *output_data = NULL;
464463
size_t output_size = 0;
465464
size_t output_length = 0;
465+
psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
466466
467467
output_size = sizeof(input_data);
468468
output_data = malloc(output_size);
469469
status = psa_crypto_init();
470470
471-
psa_key_policy_init(&policy);
472471
psa_key_policy_set_usage(&policy, PSA_KEY_USAGE_DECRYPT, PSA_ALG_CCM);
473472
status = psa_set_key_policy(slot, &policy);
474473
@@ -503,10 +502,10 @@ Generate a piece of random 128-bit AES data:
503502
size_t exported_size = bits;
504503
size_t exported_length = 0;
505504
uint8_t *exported = malloc(exported_size);
505+
psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
506506

507507
psa_crypto_init();
508508

509-
psa_key_policy_init(&policy);
510509
psa_key_policy_set_usage(&policy, PSA_KEY_USAGE_EXPORT, PSA_ALG_GCM);
511510
psa_set_key_policy(slot, &policy);
512511

0 commit comments

Comments
 (0)