|
| 1 | +/** |
| 2 | + * \file |
| 3 | + * |
| 4 | + * \brief Provides required interface between boot loader and secure boot. |
| 5 | + * |
| 6 | + * \copyright (c) 2017 Microchip Technology Inc. and its subsidiaries. |
| 7 | + * You may use this software and any derivatives exclusively with |
| 8 | + * Microchip products. |
| 9 | + * |
| 10 | + * \page License |
| 11 | + * |
| 12 | + * (c) 2017 Microchip Technology Inc. and its subsidiaries. You may use this |
| 13 | + * software and any derivatives exclusively with Microchip products. |
| 14 | + * |
| 15 | + * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER |
| 16 | + * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED |
| 17 | + * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A |
| 18 | + * PARTICULAR PURPOSE, OR ITS INTERACTION WITH MICROCHIP PRODUCTS, COMBINATION |
| 19 | + * WITH ANY OTHER PRODUCTS, OR USE IN ANY APPLICATION. |
| 20 | + * |
| 21 | + * IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, |
| 22 | + * INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND |
| 23 | + * WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS |
| 24 | + * BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE |
| 25 | + * FULLEST EXTENT ALLOWED BY LAW, MICROCHIPS TOTAL LIABILITY ON ALL CLAIMS IN |
| 26 | + * ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, |
| 27 | + * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. |
| 28 | + * |
| 29 | + * MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF THESE |
| 30 | + * TERMS. |
| 31 | + */ |
| 32 | +#include <stdlib.h> |
| 33 | +#include <stdio.h> |
| 34 | +#include "cryptoauthlib.h" |
| 35 | +#include "secure_boot.h" |
| 36 | +#include "io_protection_key.h" |
| 37 | +#include "crypto_device_app.h" |
| 38 | + |
| 39 | +/** \brief Takes care interface with secure boot and provides status about user |
| 40 | + * application. This also takes care of device configuration if enabled. |
| 41 | + * \return ATCA_SUCCESS on success, otherwise an error code. |
| 42 | + */ |
| 43 | +ATCA_STATUS crypto_device_verify_app(void) |
| 44 | +{ |
| 45 | + ATCA_STATUS status = ATCA_SUCCESS; |
| 46 | + /*Creating interface instance for 608A*/ |
| 47 | + ATCAIfaceCfg cfg_atecc608a_i2c_default = { |
| 48 | + .iface_type = ATCA_I2C_IFACE, |
| 49 | + .devtype = ATECC608A, |
| 50 | + .atcai2c.slave_address = 0xC0, |
| 51 | + .atcai2c.bus = 2, |
| 52 | + .atcai2c.baud = 400000, |
| 53 | + //.atcai2c.baud = 100000, |
| 54 | + .wake_delay = 1500, |
| 55 | + .rx_retries = 20 |
| 56 | + }; |
| 57 | + |
| 58 | + do |
| 59 | + { |
| 60 | + #if CRYPTO_DEVICE_ENABLE_SECURE_BOOT |
| 61 | + bool is_locked; |
| 62 | + |
| 63 | + /*Initialize interface on enabling any crypto operation */ |
| 64 | + if ((status = atcab_init(&cfg_atecc608a_i2c_default)) != ATCA_SUCCESS) |
| 65 | + { |
| 66 | + break; |
| 67 | + } |
| 68 | + |
| 69 | + /*Check current status of Public Key Slot lock status */ |
| 70 | + if ((status = atcab_is_slot_locked(SECURE_BOOT_PUBLIC_KEY_SLOT, &is_locked)) != ATCA_SUCCESS) |
| 71 | + { |
| 72 | + break; |
| 73 | + } |
| 74 | + |
| 75 | + /*Before doing secure boot it is expected configuration zone is locked */ |
| 76 | + if (!is_locked) |
| 77 | + { |
| 78 | + /*Trigger crypto device configuration */ |
| 79 | + #if CRYPTO_DEVICE_LOAD_CONFIG_ENABLED |
| 80 | + if ((status = crypto_device_load_configuration()) != ATCA_SUCCESS) |
| 81 | + { |
| 82 | + break; |
| 83 | + } |
| 84 | + #else |
| 85 | + status = ATCA_GEN_FAIL; |
| 86 | + break; |
| 87 | + #endif |
| 88 | + } |
| 89 | + |
| 90 | + /*Initiate secure boot operation */ |
| 91 | + if ((status = secure_boot_process()) != ATCA_SUCCESS) |
| 92 | + { |
| 93 | + break; |
| 94 | + } |
| 95 | + #endif //CRYPTO_DEVICE_ENABLE_SECURE_BOOT |
| 96 | + |
| 97 | + } |
| 98 | + while (0); |
| 99 | + |
| 100 | + |
| 101 | + return status; |
| 102 | +} |
| 103 | + |
| 104 | +#if CRYPTO_DEVICE_LOAD_CONFIG_ENABLED |
| 105 | +/** \brief Checks whether configuration is locked or not. if not, it writes |
| 106 | + * default configuration to device and locks it. |
| 107 | + * \return ATCA_SUCCESS on success, otherwise an error code. |
| 108 | + */ |
| 109 | +ATCA_STATUS crypto_device_load_configuration(void) |
| 110 | +{ |
| 111 | + ATCA_STATUS status; |
| 112 | + bool is_locked = false; |
| 113 | + |
| 114 | + uint8_t test_ecc608_configdata[ATCA_ECC_CONFIG_SIZE] = { |
| 115 | + 0x01, 0x23, 0x00, 0x00, 0x00, 0x00, 0x60, 0x00, 0x04, 0x05, 0x06, 0x07, 0xEE, 0x01, 0x01, 0x00, //15 |
| 116 | + 0xC0, 0x00, 0xA1, 0x00, 0xAF, 0x2F, 0xC4, 0x44, 0x87, 0x20, 0xC4, 0xF4, 0x8F, 0x0F, 0x0F, 0x0F, //31, 5 |
| 117 | + 0x9F, 0x8F, 0x83, 0x64, 0xC4, 0x44, 0xC4, 0x64, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, //47 |
| 118 | + 0x0F, 0x0F, 0x0F, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, //63 |
| 119 | + 0x00, 0x00, 0x00, 0x00, 0xFF, 0x84, 0x03, 0xBC, 0x09, 0x69, 0x76, 0x00, 0x00, 0x00, 0x00, 0x00, //79 |
| 120 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x06, 0x40, 0x00, 0x00, 0x00, 0x00, //95 |
| 121 | + 0x33, 0x00, 0x1C, 0x00, 0x13, 0x00, 0x1C, 0x00, 0x3C, 0x00, 0x3E, 0x00, 0x1C, 0x00, 0x33, 0x00, //111 |
| 122 | + 0x1C, 0x00, 0x1C, 0x00, 0x38, 0x10, 0x30, 0x00, 0x3C, 0x00, 0x3C, 0x00, 0x32, 0x00, 0x30, 0x00 //127 |
| 123 | + }; |
| 124 | + |
| 125 | + uint8_t public_key_slot_data[72]; |
| 126 | + uint8_t public_key_read[ATCA_PUB_KEY_SIZE]; |
| 127 | + uint8_t public_key[] = { |
| 128 | + 0x21, 0x67, 0x64, 0x1c, 0x9f, 0xc4, 0x13, 0x6c, 0xb4, 0xa9, 0x1a, 0x4f, 0x56, 0xd4, 0x8b, 0x83, |
| 129 | + 0x76, 0x9e, 0x3a, 0xd8, 0x1e, 0x0e, 0x01, 0xb7, 0x59, 0xc7, 0xc7, 0x94, 0x74, 0x3f, 0x1a, 0xa6, |
| 130 | + 0x30, 0xcc, 0xb7, 0xec, 0xfc, 0xa8, 0x2e, 0xf0, 0x5b, 0xa1, 0x3d, 0x5b, 0x34, 0x53, 0x11, 0x18, |
| 131 | + 0xa0, 0x67, 0x73, 0x7b, 0xdb, 0x1e, 0x3d, 0x1b, 0xbc, 0xdd, 0x10, 0x5a, 0x39, 0x23, 0x25, 0x3e |
| 132 | + }; |
| 133 | + |
| 134 | + do |
| 135 | + { |
| 136 | + /*Check current status of configuration lock status */ |
| 137 | + if ((status = atcab_is_locked(LOCK_ZONE_CONFIG, &is_locked)) != ATCA_SUCCESS) |
| 138 | + { |
| 139 | + break; |
| 140 | + } |
| 141 | + |
| 142 | + /*Write configuration if it is not already locked */ |
| 143 | + if (!is_locked) |
| 144 | + { |
| 145 | + /*Trigger Configuration write... ignore first 16 bytes*/ |
| 146 | + if ((status = atcab_write_bytes_zone(ATCA_ZONE_CONFIG, 0, 16, &test_ecc608_configdata[16], (sizeof(test_ecc608_configdata) - 16))) != ATCA_SUCCESS) |
| 147 | + { |
| 148 | + break; |
| 149 | + } |
| 150 | + |
| 151 | + /*Lock Configuration Zone on completing configuration*/ |
| 152 | + if ((status = atcab_lock(LOCK_ZONE_NO_CRC | LOCK_ZONE_CONFIG, 0)) != ATCA_SUCCESS) |
| 153 | + { |
| 154 | + break; |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + /*Check current status of Public Key Slot lock status */ |
| 159 | + if ((status = atcab_is_slot_locked(SECURE_BOOT_PUBLIC_KEY_SLOT, &is_locked)) != ATCA_SUCCESS) |
| 160 | + { |
| 161 | + break; |
| 162 | + } |
| 163 | + |
| 164 | + /*Write Slot Data, if it is not already locked */ |
| 165 | + if (!is_locked) |
| 166 | + { |
| 167 | + /*Check current status of Data zone lock status */ |
| 168 | + if ((status = atcab_is_locked(LOCK_ZONE_DATA, &is_locked)) != ATCA_SUCCESS) |
| 169 | + { |
| 170 | + break; |
| 171 | + } |
| 172 | + |
| 173 | + if (!is_locked) |
| 174 | + { |
| 175 | + /*Lock Data Zone if it is not */ |
| 176 | + if ((status = atcab_lock(LOCK_ZONE_NO_CRC | LOCK_ZONE_DATA, 0)) != ATCA_SUCCESS) |
| 177 | + { |
| 178 | + break; |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + /*Write Pub Key to Slot... Reformat public key into padded format */ |
| 183 | + memmove(&public_key_slot_data[40], &public_key[32], 32); // Move Y to padded position |
| 184 | + memset(&public_key_slot_data[36], 0, 4); // Add Y padding bytes |
| 185 | + memmove(&public_key_slot_data[4], &public_key[0], 32); // Move X to padded position |
| 186 | + memset(&public_key_slot_data[0], 0, 4); // Add X padding bytes |
| 187 | + |
| 188 | + /*Write Public Key to SecureBootPubKey slot*/ |
| 189 | + if ((status = atcab_write_bytes_zone(ATCA_ZONE_DATA, SECURE_BOOT_PUBLIC_KEY_SLOT, 0, public_key_slot_data, 72)) != ATCA_SUCCESS) |
| 190 | + { |
| 191 | + break; |
| 192 | + } |
| 193 | + |
| 194 | + /*Read Public Key*/ |
| 195 | + if ((status = atcab_read_pubkey(SECURE_BOOT_PUBLIC_KEY_SLOT, public_key_read)) != ATCA_SUCCESS) |
| 196 | + { |
| 197 | + break; |
| 198 | + } |
| 199 | + |
| 200 | + if ((status = memcmp(public_key, public_key_read, sizeof(public_key_read))) != ATCA_SUCCESS) |
| 201 | + { |
| 202 | + break; |
| 203 | + } |
| 204 | + |
| 205 | + /*Lock IO protection key slot */ |
| 206 | + if ((status = atcab_lock_data_slot(SECURE_BOOT_PUBLIC_KEY_SLOT)) != ATCA_SUCCESS) |
| 207 | + { |
| 208 | + break; |
| 209 | + } |
| 210 | + } |
| 211 | + } |
| 212 | + while (0); |
| 213 | + |
| 214 | + return status; |
| 215 | +} |
| 216 | +#endif //#if CRYPTO_DEVICE_CONFIG_ENABLED |
| 217 | + |
0 commit comments