|
| 1 | +/* |
| 2 | + * Copyright (c) 2019, Arm Limited and affiliates |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#include <stdio.h> |
| 19 | +#include <stdlib.h> |
| 20 | + |
| 21 | +#if defined(ATCA_HAL_I2C) |
| 22 | +#include "mbedtls/pk.h" |
| 23 | +#include "psa/crypto.h" |
| 24 | +#include "psa/lifecycle.h" |
| 25 | +#include "atecc608a_se.h" |
| 26 | + |
| 27 | +/* The slot number for the device private key stored in the secure element by |
| 28 | + * the secure element factory. |
| 29 | + * |
| 30 | + * ATECC608A secure elements with pre-provisioned keys often have that |
| 31 | + * key present in slot 0, so we use 0 here for our example. |
| 32 | + * |
| 33 | + * Note that unlike most numerical values in the PSA Crypto API, 0 is a valid |
| 34 | + * value for a slot number. Like many other secure elements, the ATECC608A |
| 35 | + * numbers its slots from 0. |
| 36 | + */ |
| 37 | +#define EXAMPLE_FACTORY_KEY_SE_SLOT 0 |
| 38 | + |
| 39 | +/* The slot number for the device private key which is generated within the |
| 40 | + * secure element (never leaving the secure element) by this example |
| 41 | + * provisioning application. */ |
| 42 | +#define EXAMPLE_GENERATED_KEY_SE_SLOT 2 |
| 43 | + |
| 44 | +/* The application-specific key ID for the secure element factory-provided |
| 45 | + * device private key. This provisioning example application will associate the |
| 46 | + * factory-provided key with this key ID for use by other applications. Any |
| 47 | + * valid ID can be chosen here; the chosen ID does not need to correlate in any |
| 48 | + * way with the physical location of the key (within the secure element). */ |
| 49 | +#define EXAMPLE_FACTORY_KEY_ID 16 |
| 50 | + |
| 51 | +/* The application-specific key ID for the device private key imported into the |
| 52 | + * secure element by this example provisioning application. */ |
| 53 | +#define EXAMPLE_GENERATED_KEY_ID 18 |
| 54 | + |
| 55 | +static psa_status_t generate_key_on_device(void) |
| 56 | +{ |
| 57 | + psa_status_t status; |
| 58 | + psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 59 | + psa_key_handle_t handle; |
| 60 | + |
| 61 | + /* Set device private key attributes. */ |
| 62 | + /* Note that it is not necessary to specify the physical slot number within |
| 63 | + * the secure element. The secure element driver can automatically allocate |
| 64 | + * a slot that fits the use case the key attributes request. */ |
| 65 | + psa_set_key_id(&attributes, EXAMPLE_GENERATED_KEY_ID); |
| 66 | + psa_set_key_lifetime(&attributes, PSA_ATECC608A_LIFETIME); |
| 67 | + psa_set_key_slot_number(&attributes, EXAMPLE_GENERATED_KEY_SE_SLOT); |
| 68 | + psa_set_key_type(&attributes, |
| 69 | + PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_CURVE_SECP256R1)); |
| 70 | + psa_set_key_bits(&attributes, 256); |
| 71 | + psa_set_key_algorithm(&attributes, PSA_ALG_ECDSA(PSA_ALG_SHA_256)); |
| 72 | + psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN); |
| 73 | + |
| 74 | + /* Generate the key inside the secure element. */ |
| 75 | + status = psa_generate_key(&attributes, &handle); |
| 76 | + if (status != PSA_SUCCESS) { |
| 77 | + return status; |
| 78 | + } |
| 79 | + |
| 80 | + return psa_close_key(handle); |
| 81 | +} |
| 82 | + |
| 83 | +static psa_status_t register_preprovisioned_keys(void) |
| 84 | +{ |
| 85 | + psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 86 | + |
| 87 | + /* Set device private key attributes. */ |
| 88 | + psa_set_key_id(&attributes, EXAMPLE_FACTORY_KEY_ID); |
| 89 | + psa_set_key_lifetime(&attributes, PSA_ATECC608A_LIFETIME); |
| 90 | + psa_set_key_slot_number(&attributes, EXAMPLE_FACTORY_KEY_SE_SLOT); |
| 91 | + psa_set_key_type(&attributes, |
| 92 | + PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_CURVE_SECP256R1)); |
| 93 | + psa_set_key_bits(&attributes, 256); |
| 94 | + psa_set_key_algorithm(&attributes, PSA_ALG_ECDSA(PSA_ALG_SHA_256)); |
| 95 | + psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN); |
| 96 | + |
| 97 | + /* Register the factory-created key with Mbed Crypto, so that Mbed Crypto |
| 98 | + * knows that the key exists and how to find and access the key. This |
| 99 | + * registration only needs doing once, as Mbed Crypto will remember the |
| 100 | + * registration even across reboots. */ |
| 101 | + return mbedtls_psa_register_se_key(&attributes); |
| 102 | +} |
| 103 | + |
| 104 | +static void print_public_key(psa_key_id_t key_id) |
| 105 | +{ |
| 106 | + enum { PUBKEY_PEM_LEN = 256 }; |
| 107 | + int ret; |
| 108 | + psa_status_t status; |
| 109 | + unsigned char *output; |
| 110 | + psa_key_handle_t handle; |
| 111 | + mbedtls_pk_context pk; |
| 112 | + |
| 113 | + printf("\tKey ID %lu:\n", key_id); |
| 114 | + |
| 115 | + /* Open the specified key. */ |
| 116 | + status = psa_open_key(key_id, &handle); |
| 117 | + if (status != PSA_SUCCESS) |
| 118 | + { |
| 119 | + printf("Failed to open key %lu with status=%ld\n", key_id, status); |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + output = calloc(1, PUBKEY_PEM_LEN); |
| 124 | + if (!output) { |
| 125 | + puts("Out of memory"); |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + /* mbedtls_* functions, rather than PSA functions, are used here because |
| 130 | + * PSA currently lacks a way to format the key in the desired format. Mbed |
| 131 | + * Crypto's PK module can transform PSA keys to our desired key format, so |
| 132 | + * we employ Mbed Crypto's PK module here. */ |
| 133 | + |
| 134 | + mbedtls_pk_init(&pk); |
| 135 | + ret = mbedtls_pk_setup_opaque(&pk, handle); |
| 136 | + if (ret != 0) |
| 137 | + { |
| 138 | + printf("Failed to setup PK with ret=%d\n", ret); |
| 139 | + goto done; |
| 140 | + } |
| 141 | + |
| 142 | + ret = mbedtls_pk_write_pubkey_pem(&pk, output, PUBKEY_PEM_LEN); |
| 143 | + if (ret != 0) { |
| 144 | + printf("Failed to print pubkey with ret=%d\n", ret); |
| 145 | + goto done; |
| 146 | + } |
| 147 | + |
| 148 | + printf("%s\n", output); |
| 149 | + |
| 150 | +done: |
| 151 | + mbedtls_pk_free(&pk); |
| 152 | + free(output); |
| 153 | +} |
| 154 | + |
| 155 | +int main(void) |
| 156 | +{ |
| 157 | + psa_status_t status; |
| 158 | + printf("Provisioning device...\n"); |
| 159 | + |
| 160 | + /* Erase secure storage (PSA ITS and PSA PS) to give Mbed Crypto a blank |
| 161 | + * slate to provision the device from. Any keys it used to know about will |
| 162 | + * be forgotten. Any keys stored in PSA ITS will be erased. All metadata |
| 163 | + * stored in PSA ITS about keys stored in secure elements will be erased. |
| 164 | + * |
| 165 | + * Note that the device may reboot as a part of this erase step, depending |
| 166 | + * on how mbed_psa_reboot_and_request_new_security_state() is implemented. |
| 167 | + * Currently, the function does not reboot in a manner such that this |
| 168 | + * function would be executed again and again in a loop. |
| 169 | + */ |
| 170 | + printf("\tErasing secure storage... "); |
| 171 | + fflush(stdout); |
| 172 | + status = mbed_psa_reboot_and_request_new_security_state(PSA_LIFECYCLE_ASSEMBLY_AND_TEST); |
| 173 | + if (status != PSA_SUCCESS) |
| 174 | + { |
| 175 | + printf("failed with status=%ld\n", status); |
| 176 | + return status; |
| 177 | + } |
| 178 | + printf("done.\n"); |
| 179 | + |
| 180 | + printf("\tRegistering drivers... "); |
| 181 | + fflush(stdout); |
| 182 | + status = psa_register_se_driver(PSA_ATECC608A_LIFETIME, &atecc608a_drv_info); |
| 183 | + if (status != PSA_SUCCESS) |
| 184 | + { |
| 185 | + printf("failed with status=%ld\n", status); |
| 186 | + return status; |
| 187 | + } |
| 188 | + printf("done.\n"); |
| 189 | + |
| 190 | + printf("\tInitializing PSA Crypto... "); |
| 191 | + fflush(stdout); |
| 192 | + status = psa_crypto_init(); |
| 193 | + if (status != PSA_SUCCESS) |
| 194 | + { |
| 195 | + printf("failed with status=%ld\n", status); |
| 196 | + return status; |
| 197 | + } |
| 198 | + printf("done.\n"); |
| 199 | + |
| 200 | + /* The secure element factory put a device private key pair into a slot in |
| 201 | + * the secure element. We need to tell Mbed Crypto that this key pair |
| 202 | + * exists so that it can be used. */ |
| 203 | + printf("\tRegistering factory-created keys... "); |
| 204 | + fflush(stdout); |
| 205 | + status = register_preprovisioned_keys(); |
| 206 | + if (status != PSA_SUCCESS) |
| 207 | + { |
| 208 | + printf("failed with status=%ld\n", status); |
| 209 | + return status; |
| 210 | + } |
| 211 | + printf("done.\n"); |
| 212 | + |
| 213 | + /* We also want to generate our own key entirely within the secure element |
| 214 | + * during the factory device provisioning stage in device assembly. Ask |
| 215 | + * Mbed Crypto to generate this key for us. */ |
| 216 | + printf("\tGenerating keys within the secure element... "); |
| 217 | + fflush(stdout); |
| 218 | + status = generate_key_on_device(); |
| 219 | + if (status != PSA_SUCCESS) |
| 220 | + { |
| 221 | + printf("\n\t\tfailed with status=%ld\n", status); |
| 222 | + return status; |
| 223 | + } |
| 224 | + printf("done.\n"); |
| 225 | + |
| 226 | + printf("Device provisioned\n"); |
| 227 | + |
| 228 | + printf("\n---------------------------------------------------------------------\n\n"); |
| 229 | + printf("Device public keys:\n"); |
| 230 | + print_public_key(EXAMPLE_FACTORY_KEY_ID); |
| 231 | + print_public_key(EXAMPLE_GENERATED_KEY_ID); |
| 232 | + |
| 233 | + return PSA_SUCCESS; |
| 234 | +} |
| 235 | +#else |
| 236 | +int main(void) |
| 237 | +{ |
| 238 | + printf("Not all of the required options are defined:\n" |
| 239 | + " - ATCA_HAL_I2C\n"); |
| 240 | + return 0; |
| 241 | +} |
| 242 | +#endif |
0 commit comments