Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 1 | ## Getting started with Mbed TLS |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 2 | |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 3 | ### What is Mbed TLS? |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 4 | |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 5 | Mbed TLS is an open source cryptographic library that supports a wide range of |
| 6 | cryptographic operations, including: |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 7 | * Key management |
| 8 | * Hashing |
| 9 | * Symmetric cryptography |
| 10 | * Asymmetric cryptography |
| 11 | * Message authentication (MAC) |
| 12 | * Key generation and derivation |
| 13 | * Authenticated encryption with associated data (AEAD) |
| 14 | |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 15 | Mbed TLS provides a reference implementation of the cryptography interface of |
| 16 | the Arm Platform Security Architecture (PSA). It is written in portable C. |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 17 | |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 18 | Mbed TLS is distributed under the Apache License, version 2.0. |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 19 | |
| 20 | #### Platform Security Architecture (PSA) |
| 21 | |
| 22 | Arm's Platform Security Architecture (PSA) is a holistic set of threat models, |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 23 | security analyses, hardware and firmware architecture specifications, and an open |
| 24 | source firmware reference implementation. PSA provides a recipe, based on |
| 25 | industry best practice, that enables you to design security into both hardware |
| 26 | and firmware consistently. Part of the API provided by PSA is the cryptography |
| 27 | interface, which provides access to a set of primitives. |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 28 | |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 29 | ### Using Mbed TLS |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 30 | |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 31 | * [Getting the Mbed TLS library](#getting-the-mbed-tls-library) |
| 32 | * [Building the Mbed TLS library](#building-the-mbed-tls-library) |
| 33 | * [Using the PSA Crypto API](#using-the-psa-crypto-api) |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 34 | * [Importing a key](#importing-a-key) |
| 35 | * [Signing a message using RSA](#signing-a-message-using-RSA) |
| 36 | * [Encrypting or decrypting using symmetric ciphers](#encrypting-or-decrypting-using-symmetric-ciphers) |
| 37 | * [Hashing a message](#hashing-a-message) |
| 38 | * [Deriving a new key from an existing key](#deriving-a-new-key-from-an-existing-key) |
| 39 | * [Generating a random value](#generating-a-random-value) |
| 40 | * [Authenticating and encrypting or decrypting a message](#authenticating-and-encrypting-or-decrypting-a-message) |
| 41 | * [Generating and exporting keys](#generating-and-exporting-keys) |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 42 | * [More about the Mbed TLS library](#more-about-the-psa-crypto-api) |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 43 | |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 44 | ### Getting the Mbed TLS library |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 45 | |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 46 | Mbed TLS releases are available in the [public GitHub repository](https://github.com/Mbed-TLS/mbedtls). |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 47 | |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 48 | ### Building the Mbed TLS library |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 49 | |
Guy Wild | c03c0fc | 2019-09-03 13:18:04 +0300 | [diff] [blame] | 50 | **Prerequisites to building the library with the provided makefiles:** |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 51 | * GNU Make. |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 52 | * A C toolchain (compiler, linker, archiver) that supports C99. |
| 53 | * Python 3.6 to generate the test code. |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 54 | * Perl to run the tests. |
| 55 | |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 56 | If you have a C compiler such as GCC or Clang, just run `make` in the top-level |
| 57 | directory to build the library, a set of unit tests and some sample programs. |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 58 | |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 59 | To select a different compiler, set the `CC` variable to the name or path of the |
| 60 | compiler and linker (default: `cc`) and set `AR` to a compatible archiver |
| 61 | (default: `ar`); for example: |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 62 | ``` |
| 63 | make CC=arm-linux-gnueabi-gcc AR=arm-linux-gnueabi-ar |
| 64 | ``` |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 65 | The provided makefiles pass options to the compiler that assume a GCC-like |
| 66 | command line syntax. To use a different compiler, you may need to pass different |
| 67 | values for `CFLAGS`, `WARNINGS_CFLAGS` and `LDFLAGS`. |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 68 | |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 69 | To run the unit tests on the host machine, run `make test` from the top-level |
| 70 | directory. If you are cross-compiling, copy the test executable from the `tests` |
| 71 | directory to the target machine. |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 72 | |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 73 | ### Using the PSA Crypto API |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 74 | |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 75 | To use the PSA APIs, call `psa_crypto_init()` before calling any other PSA API. |
| 76 | This initializes the library. |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 77 | |
| 78 | ### Importing a key |
| 79 | |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 80 | To use a key for cryptography operations in PSA, you need to first |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 81 | import it. The import operation returns the identifier of the key for use |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 82 | with other function calls. |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 83 | |
Guy Wild | 802b19f | 2019-09-03 16:40:44 +0300 | [diff] [blame] | 84 | **Prerequisites to importing keys:** |
Guy Wild | c03c0fc | 2019-09-03 13:18:04 +0300 | [diff] [blame] | 85 | * Initialize the library with a successful call to `psa_crypto_init()`. |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 86 | |
Guy Wild | c03c0fc | 2019-09-03 13:18:04 +0300 | [diff] [blame] | 87 | This example shows how to import a key: |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 88 | ```C |
Jaeden Amero | fbdf150 | 2019-11-08 09:59:16 +0000 | [diff] [blame] | 89 | void import_a_key(const uint8_t *key, size_t key_len) |
| 90 | { |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 91 | psa_status_t status; |
| 92 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Andrzej Kurek | e3ed824 | 2021-11-19 13:40:20 +0100 | [diff] [blame] | 93 | psa_key_id_t key_id; |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 94 | |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 95 | printf("Import an AES key...\t"); |
| 96 | fflush(stdout); |
| 97 | |
| 98 | /* Initialize PSA Crypto */ |
| 99 | status = psa_crypto_init(); |
| 100 | if (status != PSA_SUCCESS) { |
| 101 | printf("Failed to initialize PSA Crypto\n"); |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | /* Set key attributes */ |
| 106 | psa_set_key_usage_flags(&attributes, 0); |
| 107 | psa_set_key_algorithm(&attributes, 0); |
| 108 | psa_set_key_type(&attributes, PSA_KEY_TYPE_AES); |
| 109 | psa_set_key_bits(&attributes, 128); |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 110 | |
| 111 | /* Import the key */ |
Andrzej Kurek | e3ed824 | 2021-11-19 13:40:20 +0100 | [diff] [blame] | 112 | status = psa_import_key(&attributes, key, key_len, &key_id); |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 113 | if (status != PSA_SUCCESS) { |
| 114 | printf("Failed to import key\n"); |
| 115 | return; |
| 116 | } |
| 117 | printf("Imported a key\n"); |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 118 | |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 119 | /* Free the attributes */ |
| 120 | psa_reset_key_attributes(&attributes); |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 121 | |
| 122 | /* Destroy the key */ |
Andrzej Kurek | e3ed824 | 2021-11-19 13:40:20 +0100 | [diff] [blame] | 123 | psa_destroy_key(key_id); |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 124 | |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 125 | mbedtls_psa_crypto_free(); |
Jaeden Amero | fbdf150 | 2019-11-08 09:59:16 +0000 | [diff] [blame] | 126 | } |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 127 | ``` |
| 128 | |
| 129 | ### Signing a message using RSA |
| 130 | |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 131 | The PSA Crypto API supports encrypting, decrypting, signing and verifying |
| 132 | messages using public key signature algorithms, such as RSA or ECDSA. |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 133 | |
Guy Wild | c03c0fc | 2019-09-03 13:18:04 +0300 | [diff] [blame] | 134 | **Prerequisites to performing asymmetric signature operations:** |
| 135 | * Initialize the library with a successful call to `psa_crypto_init()`. |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 136 | * Have a valid key with appropriate attributes set: |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 137 | * Usage flag `PSA_KEY_USAGE_SIGN_HASH` to allow signing. |
| 138 | * Usage flag `PSA_KEY_USAGE_VERIFY_HASH` to allow signature verification. |
Guy Wild | c03c0fc | 2019-09-03 13:18:04 +0300 | [diff] [blame] | 139 | * Algorithm set to the desired signature algorithm. |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 140 | |
Guy Wild | 5033fdd | 2019-09-04 09:14:55 +0300 | [diff] [blame] | 141 | This example shows how to sign a hash that has already been calculated: |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 142 | ```C |
Jaeden Amero | fbdf150 | 2019-11-08 09:59:16 +0000 | [diff] [blame] | 143 | void sign_a_message_using_rsa(const uint8_t *key, size_t key_len) |
| 144 | { |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 145 | psa_status_t status; |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 146 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Guy Wild | 5033fdd | 2019-09-04 09:14:55 +0300 | [diff] [blame] | 147 | uint8_t hash[32] = {0x50, 0xd8, 0x58, 0xe0, 0x98, 0x5e, 0xcc, 0x7f, |
Guy Wild | 5b1347a | 2019-09-05 09:46:31 +0300 | [diff] [blame] | 148 | 0x60, 0x41, 0x8a, 0xaf, 0x0c, 0xc5, 0xab, 0x58, |
| 149 | 0x7f, 0x42, 0xc2, 0x57, 0x0a, 0x88, 0x40, 0x95, |
| 150 | 0xa9, 0xe8, 0xcc, 0xac, 0xd0, 0xf6, 0x54, 0x5c}; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 151 | uint8_t signature[PSA_SIGNATURE_MAX_SIZE] = {0}; |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 152 | size_t signature_length; |
Andrzej Kurek | e3ed824 | 2021-11-19 13:40:20 +0100 | [diff] [blame] | 153 | psa_key_id_t key_id; |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 154 | |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 155 | printf("Sign a message...\t"); |
| 156 | fflush(stdout); |
| 157 | |
| 158 | /* Initialize PSA Crypto */ |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 159 | status = psa_crypto_init(); |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 160 | if (status != PSA_SUCCESS) { |
| 161 | printf("Failed to initialize PSA Crypto\n"); |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | /* Set key attributes */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 166 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 167 | psa_set_key_algorithm(&attributes, PSA_ALG_RSA_PKCS1V15_SIGN_RAW); |
| 168 | psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR); |
| 169 | psa_set_key_bits(&attributes, 1024); |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 170 | |
| 171 | /* Import the key */ |
Andrzej Kurek | e3ed824 | 2021-11-19 13:40:20 +0100 | [diff] [blame] | 172 | status = psa_import_key(&attributes, key, key_len, &key_id); |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 173 | if (status != PSA_SUCCESS) { |
| 174 | printf("Failed to import key\n"); |
| 175 | return; |
| 176 | } |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 177 | |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 178 | /* Sign message using the key */ |
Andrzej Kurek | e3ed824 | 2021-11-19 13:40:20 +0100 | [diff] [blame] | 179 | status = psa_sign_hash(key_id, PSA_ALG_RSA_PKCS1V15_SIGN_RAW, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 180 | hash, sizeof(hash), |
| 181 | signature, sizeof(signature), |
| 182 | &signature_length); |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 183 | if (status != PSA_SUCCESS) { |
| 184 | printf("Failed to sign\n"); |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | printf("Signed a message\n"); |
| 189 | |
| 190 | /* Free the attributes */ |
| 191 | psa_reset_key_attributes(&attributes); |
| 192 | |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 193 | /* Destroy the key */ |
Andrzej Kurek | e3ed824 | 2021-11-19 13:40:20 +0100 | [diff] [blame] | 194 | psa_destroy_key(key_id); |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 195 | |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 196 | mbedtls_psa_crypto_free(); |
Jaeden Amero | fbdf150 | 2019-11-08 09:59:16 +0000 | [diff] [blame] | 197 | } |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 198 | ``` |
| 199 | |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 200 | ### Using symmetric ciphers |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 201 | |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 202 | The PSA Crypto API supports encrypting and decrypting messages using various |
| 203 | symmetric cipher algorithms (both block and stream ciphers). |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 204 | |
Guy Wild | c03c0fc | 2019-09-03 13:18:04 +0300 | [diff] [blame] | 205 | **Prerequisites to working with the symmetric cipher API:** |
| 206 | * Initialize the library with a successful call to `psa_crypto_init()`. |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 207 | * Have a symmetric key. This key's usage flags must include `PSA_KEY_USAGE_ENCRYPT` |
| 208 | to allow encryption or `PSA_KEY_USAGE_DECRYPT` to allow decryption. |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 209 | |
Guy Wild | c03c0fc | 2019-09-03 13:18:04 +0300 | [diff] [blame] | 210 | **To encrypt a message with a symmetric cipher:** |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 211 | 1. Allocate an operation (`psa_cipher_operation_t`) structure to pass to the |
| 212 | cipher functions. |
Guy Wild | 33d421d | 2019-09-04 09:16:14 +0300 | [diff] [blame] | 213 | 1. Initialize the operation structure to zero or to `PSA_CIPHER_OPERATION_INIT`. |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 214 | 1. Call `psa_cipher_encrypt_setup()` to specify the algorithm and the key to be |
| 215 | used. |
| 216 | 1. Call either `psa_cipher_generate_iv()` or `psa_cipher_set_iv()` to generate |
| 217 | or set the initialization vector (IV). We recommend calling |
| 218 | `psa_cipher_generate_iv()`, unless you require a specific IV value. |
| 219 | 1. Call `psa_cipher_update()` with the message to encrypt. You may call this |
| 220 | function multiple times, passing successive fragments of the message on |
| 221 | successive calls. |
| 222 | 1. Call `psa_cipher_finish()` to end the operation and output the encrypted |
| 223 | message. |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 224 | |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 225 | This example shows how to encrypt data using an AES (Advanced Encryption |
| 226 | Standard) key in CBC (Cipher Block Chaining) mode with no padding (assuming all |
| 227 | prerequisites have been fulfilled): |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 228 | ```c |
Jaeden Amero | fbdf150 | 2019-11-08 09:59:16 +0000 | [diff] [blame] | 229 | void encrypt_with_symmetric_ciphers(const uint8_t *key, size_t key_len) |
| 230 | { |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 231 | enum { |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 232 | block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES), |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 233 | }; |
| 234 | psa_status_t status; |
| 235 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 236 | psa_algorithm_t alg = PSA_ALG_CBC_NO_PADDING; |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 237 | uint8_t plaintext[block_size] = SOME_PLAINTEXT; |
| 238 | uint8_t iv[block_size]; |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 239 | size_t iv_len; |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 240 | uint8_t output[block_size]; |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 241 | size_t output_len; |
Andrzej Kurek | e3ed824 | 2021-11-19 13:40:20 +0100 | [diff] [blame] | 242 | psa_key_id_t key_id; |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 243 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 244 | |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 245 | printf("Encrypt with cipher...\t"); |
| 246 | fflush(stdout); |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 247 | |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 248 | /* Initialize PSA Crypto */ |
| 249 | status = psa_crypto_init(); |
| 250 | if (status != PSA_SUCCESS) |
| 251 | { |
| 252 | printf("Failed to initialize PSA Crypto\n"); |
| 253 | return; |
| 254 | } |
| 255 | |
| 256 | /* Import a key */ |
| 257 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 258 | psa_set_key_algorithm(&attributes, alg); |
| 259 | psa_set_key_type(&attributes, PSA_KEY_TYPE_AES); |
| 260 | psa_set_key_bits(&attributes, 128); |
Andrzej Kurek | e3ed824 | 2021-11-19 13:40:20 +0100 | [diff] [blame] | 261 | status = psa_import_key(&attributes, key, key_len, &key_id); |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 262 | if (status != PSA_SUCCESS) { |
| 263 | printf("Failed to import a key\n"); |
| 264 | return; |
| 265 | } |
| 266 | psa_reset_key_attributes(&attributes); |
| 267 | |
| 268 | /* Encrypt the plaintext */ |
Andrzej Kurek | e3ed824 | 2021-11-19 13:40:20 +0100 | [diff] [blame] | 269 | status = psa_cipher_encrypt_setup(&operation, key_id, alg); |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 270 | if (status != PSA_SUCCESS) { |
| 271 | printf("Failed to begin cipher operation\n"); |
| 272 | return; |
| 273 | } |
| 274 | status = psa_cipher_generate_iv(&operation, iv, sizeof(iv), &iv_len); |
| 275 | if (status != PSA_SUCCESS) { |
| 276 | printf("Failed to generate IV\n"); |
| 277 | return; |
| 278 | } |
| 279 | status = psa_cipher_update(&operation, plaintext, sizeof(plaintext), |
| 280 | output, sizeof(output), &output_len); |
| 281 | if (status != PSA_SUCCESS) { |
| 282 | printf("Failed to update cipher operation\n"); |
| 283 | return; |
| 284 | } |
| 285 | status = psa_cipher_finish(&operation, output + output_len, |
| 286 | sizeof(output) - output_len, &output_len); |
| 287 | if (status != PSA_SUCCESS) { |
| 288 | printf("Failed to finish cipher operation\n"); |
| 289 | return; |
| 290 | } |
| 291 | printf("Encrypted plaintext\n"); |
| 292 | |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 293 | /* Clean up cipher operation context */ |
| 294 | psa_cipher_abort(&operation); |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 295 | |
| 296 | /* Destroy the key */ |
Andrzej Kurek | e3ed824 | 2021-11-19 13:40:20 +0100 | [diff] [blame] | 297 | psa_destroy_key(key_id); |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 298 | |
| 299 | mbedtls_psa_crypto_free(); |
Jaeden Amero | fbdf150 | 2019-11-08 09:59:16 +0000 | [diff] [blame] | 300 | } |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 301 | ``` |
| 302 | |
Guy Wild | c03c0fc | 2019-09-03 13:18:04 +0300 | [diff] [blame] | 303 | **To decrypt a message with a symmetric cipher:** |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 304 | 1. Allocate an operation (`psa_cipher_operation_t`) structure to pass to the |
| 305 | cipher functions. |
Guy Wild | 2a9e9f7 | 2019-09-04 13:45:54 +0300 | [diff] [blame] | 306 | 1. Initialize the operation structure to zero or to `PSA_CIPHER_OPERATION_INIT`. |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 307 | 1. Call `psa_cipher_decrypt_setup()` to specify the algorithm and the key to be |
| 308 | used. |
Guy Wild | c03c0fc | 2019-09-03 13:18:04 +0300 | [diff] [blame] | 309 | 1. Call `psa_cipher_set_iv()` with the IV for the decryption. |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 310 | 1. Call `psa_cipher_update()` with the message to encrypt. You may call this |
| 311 | function multiple times, passing successive fragments of the message on |
| 312 | successive calls. |
| 313 | 1. Call `psa_cipher_finish()` to end the operation and output the decrypted |
| 314 | message. |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 315 | |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 316 | This example shows how to decrypt encrypted data using an AES key in CBC mode |
| 317 | with no padding (assuming all prerequisites have been fulfilled): |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 318 | ```c |
Jaeden Amero | fbdf150 | 2019-11-08 09:59:16 +0000 | [diff] [blame] | 319 | void decrypt_with_symmetric_ciphers(const uint8_t *key, size_t key_len) |
| 320 | { |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 321 | enum { |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 322 | block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES), |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 323 | }; |
| 324 | psa_status_t status; |
| 325 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 326 | psa_algorithm_t alg = PSA_ALG_CBC_NO_PADDING; |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 327 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 328 | uint8_t ciphertext[block_size] = SOME_CIPHERTEXT; |
| 329 | uint8_t iv[block_size] = ENCRYPTED_WITH_IV; |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 330 | uint8_t output[block_size]; |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 331 | size_t output_len; |
Andrzej Kurek | e3ed824 | 2021-11-19 13:40:20 +0100 | [diff] [blame] | 332 | psa_key_id_t key_id; |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 333 | |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 334 | printf("Decrypt with cipher...\t"); |
| 335 | fflush(stdout); |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 336 | |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 337 | /* Initialize PSA Crypto */ |
| 338 | status = psa_crypto_init(); |
| 339 | if (status != PSA_SUCCESS) |
| 340 | { |
| 341 | printf("Failed to initialize PSA Crypto\n"); |
| 342 | return; |
| 343 | } |
| 344 | |
| 345 | /* Import a key */ |
| 346 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 347 | psa_set_key_algorithm(&attributes, alg); |
| 348 | psa_set_key_type(&attributes, PSA_KEY_TYPE_AES); |
| 349 | psa_set_key_bits(&attributes, 128); |
Andrzej Kurek | e3ed824 | 2021-11-19 13:40:20 +0100 | [diff] [blame] | 350 | status = psa_import_key(&attributes, key, key_len, &key_id); |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 351 | if (status != PSA_SUCCESS) { |
| 352 | printf("Failed to import a key\n"); |
| 353 | return; |
| 354 | } |
| 355 | psa_reset_key_attributes(&attributes); |
| 356 | |
| 357 | /* Decrypt the ciphertext */ |
Andrzej Kurek | e3ed824 | 2021-11-19 13:40:20 +0100 | [diff] [blame] | 358 | status = psa_cipher_decrypt_setup(&operation, key_id, alg); |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 359 | if (status != PSA_SUCCESS) { |
| 360 | printf("Failed to begin cipher operation\n"); |
| 361 | return; |
| 362 | } |
| 363 | status = psa_cipher_set_iv(&operation, iv, sizeof(iv)); |
| 364 | if (status != PSA_SUCCESS) { |
| 365 | printf("Failed to set IV\n"); |
| 366 | return; |
| 367 | } |
| 368 | status = psa_cipher_update(&operation, ciphertext, sizeof(ciphertext), |
| 369 | output, sizeof(output), &output_len); |
| 370 | if (status != PSA_SUCCESS) { |
| 371 | printf("Failed to update cipher operation\n"); |
| 372 | return; |
| 373 | } |
| 374 | status = psa_cipher_finish(&operation, output + output_len, |
| 375 | sizeof(output) - output_len, &output_len); |
| 376 | if (status != PSA_SUCCESS) { |
| 377 | printf("Failed to finish cipher operation\n"); |
| 378 | return; |
| 379 | } |
| 380 | printf("Decrypted ciphertext\n"); |
| 381 | |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 382 | /* Clean up cipher operation context */ |
| 383 | psa_cipher_abort(&operation); |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 384 | |
| 385 | /* Destroy the key */ |
Andrzej Kurek | e3ed824 | 2021-11-19 13:40:20 +0100 | [diff] [blame] | 386 | psa_destroy_key(key_id); |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 387 | |
| 388 | mbedtls_psa_crypto_free(); |
Jaeden Amero | fbdf150 | 2019-11-08 09:59:16 +0000 | [diff] [blame] | 389 | } |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 390 | ``` |
| 391 | |
| 392 | #### Handling cipher operation contexts |
| 393 | |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 394 | After you've initialized the operation structure with a successful call to |
| 395 | `psa_cipher_encrypt_setup()` or `psa_cipher_decrypt_setup()`, you can terminate |
| 396 | the operation at any time by calling `psa_cipher_abort()`. |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 397 | |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 398 | The call to `psa_cipher_abort()` frees any resources associated with the |
| 399 | operation, except for the operation structure itself. |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 400 | |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 401 | The PSA Crypto API implicitly calls `psa_cipher_abort()` when: |
| 402 | * A call to `psa_cipher_generate_iv()`, `psa_cipher_set_iv()` or |
| 403 | `psa_cipher_update()` fails (returning any status other than `PSA_SUCCESS`). |
Guy Wild | c03c0fc | 2019-09-03 13:18:04 +0300 | [diff] [blame] | 404 | * A call to `psa_cipher_finish()` succeeds or fails. |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 405 | |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 406 | After an implicit or explicit call to `psa_cipher_abort()`, the operation |
| 407 | structure is invalidated; in other words, you cannot reuse the operation |
| 408 | structure for the same operation. You can, however, reuse the operation |
| 409 | structure for a different operation by calling either |
| 410 | `psa_cipher_encrypt_setup()` or `psa_cipher_decrypt_setup()` again. |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 411 | |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 412 | You must call `psa_cipher_abort()` at some point for any operation that is |
| 413 | initialized successfully (by a successful call to `psa_cipher_encrypt_setup()` |
| 414 | or `psa_cipher_decrypt_setup()`). |
Guy Wild | c03c0fc | 2019-09-03 13:18:04 +0300 | [diff] [blame] | 415 | |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 416 | Making multiple sequential calls to `psa_cipher_abort()` on an operation that |
| 417 | is terminated (either implicitly or explicitly) is safe and has no effect. |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 418 | |
| 419 | ### Hashing a message |
| 420 | |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 421 | The PSA Crypto API lets you compute and verify hashes using various hashing |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 422 | algorithms. |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 423 | |
Guy Wild | c03c0fc | 2019-09-03 13:18:04 +0300 | [diff] [blame] | 424 | **Prerequisites to working with the hash APIs:** |
| 425 | * Initialize the library with a successful call to `psa_crypto_init()`. |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 426 | |
Guy Wild | c03c0fc | 2019-09-03 13:18:04 +0300 | [diff] [blame] | 427 | **To calculate a hash:** |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 428 | 1. Allocate an operation structure (`psa_hash_operation_t`) to pass to the hash |
| 429 | functions. |
Guy Wild | eefc517 | 2019-09-04 09:16:53 +0300 | [diff] [blame] | 430 | 1. Initialize the operation structure to zero or to `PSA_HASH_OPERATION_INIT`. |
| 431 | 1. Call `psa_hash_setup()` to specify the hash algorithm. |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 432 | 1. Call `psa_hash_update()` with the message to encrypt. You may call this |
| 433 | function multiple times, passing successive fragments of the message on |
| 434 | successive calls. |
| 435 | 1. Call `psa_hash_finish()` to calculate the hash, or `psa_hash_verify()` to |
| 436 | compare the computed hash with an expected hash value. |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 437 | |
Guy Wild | 2a9e9f7 | 2019-09-04 13:45:54 +0300 | [diff] [blame] | 438 | This example shows how to calculate the SHA-256 hash of a message: |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 439 | ```c |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 440 | psa_status_t status; |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 441 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 442 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 443 | unsigned char input[] = { 'a', 'b', 'c' }; |
| 444 | unsigned char actual_hash[PSA_HASH_MAX_SIZE]; |
| 445 | size_t actual_hash_len; |
| 446 | |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 447 | printf("Hash a message...\t"); |
| 448 | fflush(stdout); |
| 449 | |
| 450 | /* Initialize PSA Crypto */ |
| 451 | status = psa_crypto_init(); |
| 452 | if (status != PSA_SUCCESS) { |
| 453 | printf("Failed to initialize PSA Crypto\n"); |
| 454 | return; |
| 455 | } |
| 456 | |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 457 | /* Compute hash of message */ |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 458 | status = psa_hash_setup(&operation, alg); |
| 459 | if (status != PSA_SUCCESS) { |
| 460 | printf("Failed to begin hash operation\n"); |
| 461 | return; |
| 462 | } |
| 463 | status = psa_hash_update(&operation, input, sizeof(input)); |
| 464 | if (status != PSA_SUCCESS) { |
| 465 | printf("Failed to update hash operation\n"); |
| 466 | return; |
| 467 | } |
| 468 | status = psa_hash_finish(&operation, actual_hash, sizeof(actual_hash), |
| 469 | &actual_hash_len); |
| 470 | if (status != PSA_SUCCESS) { |
| 471 | printf("Failed to finish hash operation\n"); |
| 472 | return; |
| 473 | } |
| 474 | |
| 475 | printf("Hashed a message\n"); |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 476 | |
| 477 | /* Clean up hash operation context */ |
| 478 | psa_hash_abort(&operation); |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 479 | |
| 480 | mbedtls_psa_crypto_free(); |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 481 | ``` |
| 482 | |
Guy Wild | 2a9e9f7 | 2019-09-04 13:45:54 +0300 | [diff] [blame] | 483 | This example shows how to verify the SHA-256 hash of a message: |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 484 | ```c |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 485 | psa_status_t status; |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 486 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 487 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 488 | unsigned char input[] = { 'a', 'b', 'c' }; |
| 489 | unsigned char expected_hash[] = { |
| 490 | 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40, 0xde, |
| 491 | 0x5d, 0xae, 0x22, 0x23, 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, |
| 492 | 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad |
| 493 | }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 494 | size_t expected_hash_len = PSA_HASH_LENGTH(alg); |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 495 | |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 496 | printf("Verify a hash...\t"); |
| 497 | fflush(stdout); |
| 498 | |
| 499 | /* Initialize PSA Crypto */ |
| 500 | status = psa_crypto_init(); |
| 501 | if (status != PSA_SUCCESS) { |
| 502 | printf("Failed to initialize PSA Crypto\n"); |
| 503 | return; |
| 504 | } |
| 505 | |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 506 | /* Verify message hash */ |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 507 | status = psa_hash_setup(&operation, alg); |
| 508 | if (status != PSA_SUCCESS) { |
| 509 | printf("Failed to begin hash operation\n"); |
| 510 | return; |
| 511 | } |
| 512 | status = psa_hash_update(&operation, input, sizeof(input)); |
| 513 | if (status != PSA_SUCCESS) { |
| 514 | printf("Failed to update hash operation\n"); |
| 515 | return; |
| 516 | } |
| 517 | status = psa_hash_verify(&operation, expected_hash, expected_hash_len); |
| 518 | if (status != PSA_SUCCESS) { |
| 519 | printf("Failed to verify hash\n"); |
| 520 | return; |
| 521 | } |
| 522 | |
| 523 | printf("Verified a hash\n"); |
| 524 | |
| 525 | /* Clean up hash operation context */ |
| 526 | psa_hash_abort(&operation); |
| 527 | |
| 528 | mbedtls_psa_crypto_free(); |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 529 | ``` |
| 530 | |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 531 | The API provides the macro `PSA_HASH_LENGTH`, which returns the expected hash |
| 532 | length (in bytes) for the specified algorithm. |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 533 | |
| 534 | #### Handling hash operation contexts |
| 535 | |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 536 | After a successful call to `psa_hash_setup()`, you can terminate the operation |
| 537 | at any time by calling `psa_hash_abort()`. The call to `psa_hash_abort()` frees |
| 538 | any resources associated with the operation, except for the operation structure |
| 539 | itself. |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 540 | |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 541 | The PSA Crypto API implicitly calls `psa_hash_abort()` when: |
| 542 | 1. A call to `psa_hash_update()` fails (returning any status other than |
| 543 | `PSA_SUCCESS`). |
Guy Wild | c03c0fc | 2019-09-03 13:18:04 +0300 | [diff] [blame] | 544 | 1. A call to `psa_hash_finish()` succeeds or fails. |
| 545 | 1. A call to `psa_hash_verify()` succeeds or fails. |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 546 | |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 547 | After an implicit or explicit call to `psa_hash_abort()`, the operation |
| 548 | structure is invalidated; in other words, you cannot reuse the operation |
| 549 | structure for the same operation. You can, however, reuse the operation |
| 550 | structure for a different operation by calling `psa_hash_setup()` again. |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 551 | |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 552 | You must call `psa_hash_abort()` at some point for any operation that is |
| 553 | initialized successfully (by a successful call to `psa_hash_setup()`) . |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 554 | |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 555 | Making multiple sequential calls to `psa_hash_abort()` on an operation that has |
| 556 | already been terminated (either implicitly or explicitly) is safe and has no |
| 557 | effect. |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 558 | |
| 559 | ### Generating a random value |
| 560 | |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 561 | The PSA Crypto API can generate random data. |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 562 | |
Guy Wild | 802b19f | 2019-09-03 16:40:44 +0300 | [diff] [blame] | 563 | **Prerequisites to generating random data:** |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 564 | * Initialize the library with a successful call to `psa_crypto_init()`. |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 565 | |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 566 | <span class="notes">**Note:** To generate a random key, use `psa_generate_key()` |
| 567 | instead of `psa_generate_random()`.</span> |
Guy Wild | 802b19f | 2019-09-03 16:40:44 +0300 | [diff] [blame] | 568 | |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 569 | This example shows how to generate ten bytes of random data by calling |
| 570 | `psa_generate_random()`: |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 571 | ```C |
| 572 | psa_status_t status; |
| 573 | uint8_t random[10] = { 0 }; |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 574 | |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 575 | printf("Generate random...\t"); |
| 576 | fflush(stdout); |
| 577 | |
| 578 | /* Initialize PSA Crypto */ |
| 579 | status = psa_crypto_init(); |
| 580 | if (status != PSA_SUCCESS) { |
| 581 | printf("Failed to initialize PSA Crypto\n"); |
| 582 | return; |
| 583 | } |
| 584 | |
| 585 | status = psa_generate_random(random, sizeof(random)); |
| 586 | if (status != PSA_SUCCESS) { |
| 587 | printf("Failed to generate a random value\n"); |
| 588 | return; |
| 589 | } |
| 590 | |
| 591 | printf("Generated random data\n"); |
| 592 | |
| 593 | /* Clean up */ |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 594 | mbedtls_psa_crypto_free(); |
| 595 | ``` |
| 596 | |
| 597 | ### Deriving a new key from an existing key |
| 598 | |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 599 | The PSA Crypto API provides a key derivation API that lets you derive new keys |
| 600 | from existing ones. The key derivation API has functions to take inputs, |
| 601 | including other keys and data, and functions to generate outputs, such as |
| 602 | new keys or other data. |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 603 | |
Guy Wild | c03c0fc | 2019-09-03 13:18:04 +0300 | [diff] [blame] | 604 | You must first initialize and set up a key derivation context, |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 605 | provided with a key and, optionally, other data. Then, use the key derivation context |
| 606 | to either read derived data to a buffer or send derived data directly to a key slot. |
Guy Wild | c03c0fc | 2019-09-03 13:18:04 +0300 | [diff] [blame] | 607 | |
| 608 | See the documentation for the particular algorithm (such as HKDF or the TLS1.2 PRF) for |
| 609 | information about which inputs to pass when, and when you can obtain which outputs. |
| 610 | |
| 611 | **Prerequisites to working with the key derivation APIs:** |
| 612 | * Initialize the library with a successful call to `psa_crypto_init()`. |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 613 | * Use a key with the appropriate attributes set: |
| 614 | * Usage flags set for key derivation (`PSA_KEY_USAGE_DERIVE`) |
| 615 | * Key type set to `PSA_KEY_TYPE_DERIVE`. |
| 616 | * Algorithm set to a key derivation algorithm |
Guy Wild | 2a9e9f7 | 2019-09-04 13:45:54 +0300 | [diff] [blame] | 617 | (for example, `PSA_ALG_HKDF(PSA_ALG_SHA_256)`). |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 618 | |
Guy Wild | c03c0fc | 2019-09-03 13:18:04 +0300 | [diff] [blame] | 619 | **To derive a new AES-CTR 128-bit encryption key into a given key slot using HKDF |
Guy Wild | 2a9e9f7 | 2019-09-04 13:45:54 +0300 | [diff] [blame] | 620 | with a given key, salt and info:** |
Guy Wild | c03c0fc | 2019-09-03 13:18:04 +0300 | [diff] [blame] | 621 | |
| 622 | 1. Set up the key derivation context using the `psa_key_derivation_setup()` |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 623 | function, specifying the derivation algorithm `PSA_ALG_HKDF(PSA_ALG_SHA_256)`. |
Guy Wild | 2900811 | 2019-09-05 11:38:14 +0300 | [diff] [blame] | 624 | 1. Provide an optional salt with `psa_key_derivation_input_bytes()`. |
| 625 | 1. Provide info with `psa_key_derivation_input_bytes()`. |
| 626 | 1. Provide a secret with `psa_key_derivation_input_key()`, referencing a key that |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 627 | can be used for key derivation. |
| 628 | 1. Set the key attributes desired for the new derived key. We'll set |
Guy Wild | 0058ab6 | 2019-09-04 09:17:54 +0300 | [diff] [blame] | 629 | the `PSA_KEY_USAGE_ENCRYPT` usage flag and the `PSA_ALG_CTR` algorithm for this |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 630 | example. |
| 631 | 1. Derive the key by calling `psa_key_derivation_output_key()`. |
| 632 | 1. Clean up the key derivation context. |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 633 | |
Guy Wild | c03c0fc | 2019-09-03 13:18:04 +0300 | [diff] [blame] | 634 | At this point, the derived key slot holds a new 128-bit AES-CTR encryption key |
Guy Wild | ce56077 | 2019-09-05 11:35:16 +0300 | [diff] [blame] | 635 | derived from the key, salt and info provided: |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 636 | ```C |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 637 | psa_status_t status; |
| 638 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 639 | static const unsigned char key[] = { |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 640 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 641 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 642 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 643 | 0x0b }; |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 644 | static const unsigned char salt[] = { |
| 645 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, |
| 646 | 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c }; |
| 647 | static const unsigned char info[] = { |
| 648 | 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, |
| 649 | 0xf7, 0xf8, 0xf9 }; |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 650 | psa_algorithm_t alg = PSA_ALG_HKDF(PSA_ALG_SHA_256); |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 651 | psa_key_derivation_operation_t operation = |
| 652 | PSA_KEY_DERIVATION_OPERATION_INIT; |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 653 | size_t derived_bits = 128; |
| 654 | size_t capacity = PSA_BITS_TO_BYTES(derived_bits); |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 655 | psa_key_id_t base_key; |
| 656 | psa_key_id_t derived_key; |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 657 | |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 658 | printf("Derive a key (HKDF)...\t"); |
| 659 | fflush(stdout); |
| 660 | |
| 661 | /* Initialize PSA Crypto */ |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 662 | status = psa_crypto_init(); |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 663 | if (status != PSA_SUCCESS) { |
| 664 | printf("Failed to initialize PSA Crypto\n"); |
| 665 | return; |
| 666 | } |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 667 | |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 668 | /* Import a key for use in key derivation. If such a key has already been |
| 669 | * generated or imported, you can skip this part. */ |
| 670 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 671 | psa_set_key_algorithm(&attributes, alg); |
| 672 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
| 673 | status = psa_import_key(&attributes, key, sizeof(key), &base_key); |
| 674 | if (status != PSA_SUCCESS) { |
| 675 | printf("Failed to import a key\n"); |
| 676 | return; |
| 677 | } |
| 678 | psa_reset_key_attributes(&attributes); |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 679 | |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 680 | /* Derive a key */ |
| 681 | status = psa_key_derivation_setup(&operation, alg); |
| 682 | if (status != PSA_SUCCESS) { |
| 683 | printf("Failed to begin key derivation\n"); |
| 684 | return; |
| 685 | } |
| 686 | status = psa_key_derivation_set_capacity(&operation, capacity); |
| 687 | if (status != PSA_SUCCESS) { |
| 688 | printf("Failed to set capacity\n"); |
| 689 | return; |
| 690 | } |
| 691 | status = psa_key_derivation_input_bytes(&operation, |
| 692 | PSA_KEY_DERIVATION_INPUT_SALT, |
| 693 | salt, sizeof(salt)); |
| 694 | if (status != PSA_SUCCESS) { |
| 695 | printf("Failed to input salt (extract)\n"); |
| 696 | return; |
| 697 | } |
| 698 | status = psa_key_derivation_input_key(&operation, |
| 699 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 700 | base_key); |
| 701 | if (status != PSA_SUCCESS) { |
| 702 | printf("Failed to input key (extract)\n"); |
| 703 | return; |
| 704 | } |
| 705 | status = psa_key_derivation_input_bytes(&operation, |
| 706 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 707 | info, sizeof(info)); |
| 708 | if (status != PSA_SUCCESS) { |
| 709 | printf("Failed to input info (expand)\n"); |
| 710 | return; |
| 711 | } |
| 712 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 713 | psa_set_key_algorithm(&attributes, PSA_ALG_CTR); |
| 714 | psa_set_key_type(&attributes, PSA_KEY_TYPE_AES); |
| 715 | psa_set_key_bits(&attributes, 128); |
| 716 | status = psa_key_derivation_output_key(&attributes, &operation, |
| 717 | &derived_key); |
| 718 | if (status != PSA_SUCCESS) { |
| 719 | printf("Failed to derive key\n"); |
| 720 | return; |
| 721 | } |
| 722 | psa_reset_key_attributes(&attributes); |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 723 | |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 724 | printf("Derived key\n"); |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 725 | |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 726 | /* Clean up key derivation operation */ |
| 727 | psa_key_derivation_abort(&operation); |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 728 | |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 729 | /* Destroy the keys */ |
| 730 | psa_destroy_key(derived_key); |
| 731 | psa_destroy_key(base_key); |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 732 | |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 733 | mbedtls_psa_crypto_free(); |
| 734 | ``` |
| 735 | |
| 736 | ### Authenticating and encrypting or decrypting a message |
| 737 | |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 738 | The PSA Crypto API provides a simple way to authenticate and encrypt with |
| 739 | associated data (AEAD), supporting the `PSA_ALG_CCM` algorithm. |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 740 | |
Guy Wild | c03c0fc | 2019-09-03 13:18:04 +0300 | [diff] [blame] | 741 | **Prerequisites to working with the AEAD cipher APIs:** |
| 742 | * Initialize the library with a successful call to `psa_crypto_init()`. |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 743 | * The key attributes for the key used for derivation must have the |
| 744 | `PSA_KEY_USAGE_ENCRYPT` or `PSA_KEY_USAGE_DECRYPT` usage flags. |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 745 | |
Guy Wild | c03c0fc | 2019-09-03 13:18:04 +0300 | [diff] [blame] | 746 | This example shows how to authenticate and encrypt a message: |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 747 | ```C |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 748 | psa_status_t status; |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 749 | static const uint8_t key[] = { |
| 750 | 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, |
| 751 | 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF }; |
| 752 | static const uint8_t nonce[] = { |
| 753 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 754 | 0x08, 0x09, 0x0A, 0x0B }; |
| 755 | static const uint8_t additional_data[] = { |
| 756 | 0xEC, 0x46, 0xBB, 0x63, 0xB0, 0x25, |
| 757 | 0x20, 0xC3, 0x3C, 0x49, 0xFD, 0x70 }; |
| 758 | static const uint8_t input_data[] = { |
| 759 | 0xB9, 0x6B, 0x49, 0xE2, 0x1D, 0x62, 0x17, 0x41, |
| 760 | 0x63, 0x28, 0x75, 0xDB, 0x7F, 0x6C, 0x92, 0x43, |
| 761 | 0xD2, 0xD7, 0xC2 }; |
| 762 | uint8_t *output_data = NULL; |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 763 | size_t output_size = 0; |
| 764 | size_t output_length = 0; |
| 765 | size_t tag_length = 16; |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 766 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Andrzej Kurek | e3ed824 | 2021-11-19 13:40:20 +0100 | [diff] [blame] | 767 | psa_key_id_t key_id; |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 768 | |
| 769 | printf("Authenticate encrypt...\t"); |
| 770 | fflush(stdout); |
| 771 | |
| 772 | /* Initialize PSA Crypto */ |
| 773 | status = psa_crypto_init(); |
| 774 | if (status != PSA_SUCCESS) { |
| 775 | printf("Failed to initialize PSA Crypto\n"); |
| 776 | return; |
| 777 | } |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 778 | |
| 779 | output_size = sizeof(input_data) + tag_length; |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 780 | output_data = (uint8_t *)malloc(output_size); |
| 781 | if (!output_data) { |
| 782 | printf("Out of memory\n"); |
| 783 | return; |
| 784 | } |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 785 | |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 786 | /* Import a key */ |
| 787 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 788 | psa_set_key_algorithm(&attributes, PSA_ALG_CCM); |
| 789 | psa_set_key_type(&attributes, PSA_KEY_TYPE_AES); |
| 790 | psa_set_key_bits(&attributes, 128); |
Andrzej Kurek | e3ed824 | 2021-11-19 13:40:20 +0100 | [diff] [blame] | 791 | status = psa_import_key(&attributes, key, sizeof(key), &key_id); |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 792 | psa_reset_key_attributes(&attributes); |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 793 | |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 794 | /* Authenticate and encrypt */ |
Andrzej Kurek | e3ed824 | 2021-11-19 13:40:20 +0100 | [diff] [blame] | 795 | status = psa_aead_encrypt(key_id, PSA_ALG_CCM, |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 796 | nonce, sizeof(nonce), |
| 797 | additional_data, sizeof(additional_data), |
| 798 | input_data, sizeof(input_data), |
| 799 | output_data, output_size, |
| 800 | &output_length); |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 801 | if (status != PSA_SUCCESS) { |
| 802 | printf("Failed to authenticate and encrypt\n"); |
| 803 | return; |
| 804 | } |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 805 | |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 806 | printf("Authenticated and encrypted\n"); |
| 807 | |
| 808 | /* Clean up */ |
| 809 | free(output_data); |
| 810 | |
| 811 | /* Destroy the key */ |
Andrzej Kurek | e3ed824 | 2021-11-19 13:40:20 +0100 | [diff] [blame] | 812 | psa_destroy_key(key_id); |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 813 | |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 814 | mbedtls_psa_crypto_free(); |
| 815 | ``` |
| 816 | |
Guy Wild | c03c0fc | 2019-09-03 13:18:04 +0300 | [diff] [blame] | 817 | This example shows how to authenticate and decrypt a message: |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 818 | |
| 819 | ```C |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 820 | psa_status_t status; |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 821 | static const uint8_t key_data[] = { |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 822 | 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 823 | 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF }; |
| 824 | static const uint8_t nonce[] = { |
| 825 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 826 | 0x08, 0x09, 0x0A, 0x0B }; |
| 827 | static const uint8_t additional_data[] = { |
| 828 | 0xEC, 0x46, 0xBB, 0x63, 0xB0, 0x25, |
| 829 | 0x20, 0xC3, 0x3C, 0x49, 0xFD, 0x70 }; |
| 830 | static const uint8_t input_data[] = { |
| 831 | 0x20, 0x30, 0xE0, 0x36, 0xED, 0x09, 0xA0, 0x45, 0xAF, 0x3C, 0xBA, 0xEE, |
| 832 | 0x0F, 0xC8, 0x48, 0xAF, 0xCD, 0x89, 0x54, 0xF4, 0xF6, 0x3F, 0x28, 0x9A, |
| 833 | 0xA1, 0xDD, 0xB2, 0xB8, 0x09, 0xCD, 0x7C, 0xE1, 0x46, 0xE9, 0x98 }; |
| 834 | uint8_t *output_data = NULL; |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 835 | size_t output_size = 0; |
| 836 | size_t output_length = 0; |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 837 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Andrzej Kurek | e3ed824 | 2021-11-19 13:40:20 +0100 | [diff] [blame] | 838 | psa_key_id_t key_id; |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 839 | |
| 840 | printf("Authenticate decrypt...\t"); |
| 841 | fflush(stdout); |
| 842 | |
| 843 | /* Initialize PSA Crypto */ |
| 844 | status = psa_crypto_init(); |
| 845 | if (status != PSA_SUCCESS) { |
| 846 | printf("Failed to initialize PSA Crypto\n"); |
| 847 | return; |
| 848 | } |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 849 | |
| 850 | output_size = sizeof(input_data); |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 851 | output_data = (uint8_t *)malloc(output_size); |
| 852 | if (!output_data) { |
| 853 | printf("Out of memory\n"); |
| 854 | return; |
| 855 | } |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 856 | |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 857 | /* Import a key */ |
| 858 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 859 | psa_set_key_algorithm(&attributes, PSA_ALG_CCM); |
| 860 | psa_set_key_type(&attributes, PSA_KEY_TYPE_AES); |
| 861 | psa_set_key_bits(&attributes, 128); |
Andrzej Kurek | e3ed824 | 2021-11-19 13:40:20 +0100 | [diff] [blame] | 862 | status = psa_import_key(&attributes, key_data, sizeof(key_data), &key_id); |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 863 | if (status != PSA_SUCCESS) { |
| 864 | printf("Failed to import a key\n"); |
| 865 | return; |
| 866 | } |
| 867 | psa_reset_key_attributes(&attributes); |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 868 | |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 869 | /* Authenticate and decrypt */ |
Andrzej Kurek | e3ed824 | 2021-11-19 13:40:20 +0100 | [diff] [blame] | 870 | status = psa_aead_decrypt(key_id, PSA_ALG_CCM, |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 871 | nonce, sizeof(nonce), |
| 872 | additional_data, sizeof(additional_data), |
| 873 | input_data, sizeof(input_data), |
| 874 | output_data, output_size, |
| 875 | &output_length); |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 876 | if (status != PSA_SUCCESS) { |
| 877 | printf("Failed to authenticate and decrypt %ld\n", status); |
| 878 | return; |
| 879 | } |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 880 | |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 881 | printf("Authenticated and decrypted\n"); |
| 882 | |
| 883 | /* Clean up */ |
| 884 | free(output_data); |
| 885 | |
| 886 | /* Destroy the key */ |
Andrzej Kurek | e3ed824 | 2021-11-19 13:40:20 +0100 | [diff] [blame] | 887 | psa_destroy_key(key_id); |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 888 | |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 889 | mbedtls_psa_crypto_free(); |
| 890 | ``` |
| 891 | |
| 892 | ### Generating and exporting keys |
| 893 | |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 894 | The PSA Crypto API provides a simple way to generate a key or key pair. |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 895 | |
Guy Wild | c03c0fc | 2019-09-03 13:18:04 +0300 | [diff] [blame] | 896 | **Prerequisites to using key generation and export APIs:** |
| 897 | * Initialize the library with a successful call to `psa_crypto_init()`. |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 898 | |
Guy Wild | c03c0fc | 2019-09-03 13:18:04 +0300 | [diff] [blame] | 899 | **To generate an ECDSA key:** |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 900 | 1. Set the desired key attributes for key generation by calling |
| 901 | `psa_set_key_algorithm()` with the chosen ECDSA algorithm (such as |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 902 | `PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_SHA_256)`). You only want to export the |
| 903 | public key, not the key pair (or private key); therefore, do not |
| 904 | set `PSA_KEY_USAGE_EXPORT`. |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 905 | 1. Generate a key by calling `psa_generate_key()`. |
Guy Wild | c03c0fc | 2019-09-03 13:18:04 +0300 | [diff] [blame] | 906 | 1. Export the generated public key by calling `psa_export_public_key()`: |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 907 | ```C |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 908 | enum { |
| 909 | key_bits = 256, |
| 910 | }; |
| 911 | psa_status_t status; |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 912 | size_t exported_length = 0; |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 913 | static uint8_t exported[PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits)]; |
| 914 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Andrzej Kurek | e3ed824 | 2021-11-19 13:40:20 +0100 | [diff] [blame] | 915 | psa_key_id_t key_id; |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 916 | |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 917 | printf("Generate a key pair...\t"); |
| 918 | fflush(stdout); |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 919 | |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 920 | /* Initialize PSA Crypto */ |
| 921 | status = psa_crypto_init(); |
| 922 | if (status != PSA_SUCCESS) { |
| 923 | printf("Failed to initialize PSA Crypto\n"); |
| 924 | return; |
| 925 | } |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 926 | |
| 927 | /* Generate a key */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 928 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 929 | psa_set_key_algorithm(&attributes, |
| 930 | PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_SHA_256)); |
| 931 | psa_set_key_type(&attributes, |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 932 | PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1)); |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 933 | psa_set_key_bits(&attributes, key_bits); |
Andrzej Kurek | e3ed824 | 2021-11-19 13:40:20 +0100 | [diff] [blame] | 934 | status = psa_generate_key(&attributes, &key_id); |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 935 | if (status != PSA_SUCCESS) { |
| 936 | printf("Failed to generate key\n"); |
| 937 | return; |
| 938 | } |
| 939 | psa_reset_key_attributes(&attributes); |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 940 | |
Andrzej Kurek | e3ed824 | 2021-11-19 13:40:20 +0100 | [diff] [blame] | 941 | status = psa_export_public_key(key_id, exported, sizeof(exported), |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 942 | &exported_length); |
| 943 | if (status != PSA_SUCCESS) { |
| 944 | printf("Failed to export public key %ld\n", status); |
| 945 | return; |
| 946 | } |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 947 | |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 948 | printf("Exported a public key\n"); |
| 949 | |
| 950 | /* Destroy the key */ |
Andrzej Kurek | e3ed824 | 2021-11-19 13:40:20 +0100 | [diff] [blame] | 951 | psa_destroy_key(key_id); |
Jaeden Amero | 884738a | 2019-08-16 17:58:31 +0100 | [diff] [blame] | 952 | |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 953 | mbedtls_psa_crypto_free(); |
| 954 | ``` |
| 955 | |
Guy Wild | 2a9e9f7 | 2019-09-04 13:45:54 +0300 | [diff] [blame] | 956 | ### More about the PSA Crypto API |
mohammad1603 | 87a7eeb | 2018-11-01 11:25:49 +0200 | [diff] [blame] | 957 | |
Dave Rodgman | 38699e5 | 2023-01-20 12:43:53 +0000 | [diff] [blame^] | 958 | For more information about the PSA Crypto API, please see the |
| 959 | [PSA Cryptography API Specification](https://arm-software.github.io/psa-api/crypto/). |