lijuang | c267675 | 2018-05-15 13:52:36 +0800 | [diff] [blame] | 1 | /* Copyright (c) 2015-2018, The Linux Foundation. All rights reserved. |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 2 | * |
| 3 | * Redistribution and use in source and binary forms, with or without |
| 4 | * modification, are permitted provided that the following conditions are |
| 5 | * met: |
| 6 | * * Redistributions of source code must retain the above copyright |
| 7 | * notice, this list of conditions and the following disclaimer. |
| 8 | * * Redistributions in binary form must reproduce the above |
| 9 | * copyright notice, this list of conditions and the following |
| 10 | * disclaimer in the documentation and/or other materials provided |
| 11 | * with the distribution. |
| 12 | * * Neither the name of The Linux Foundation nor the names of its |
| 13 | * contributors may be used to endorse or promote products derived |
| 14 | * from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 20 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 23 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 24 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 25 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 26 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include <debug.h> |
| 30 | #include <dev/fbcon.h> |
| 31 | #include <target.h> |
| 32 | #include <mmc.h> |
| 33 | #include <partition_parser.h> |
| 34 | #include <platform.h> |
| 35 | #include <crypto_hash.h> |
| 36 | #include <malloc.h> |
| 37 | #include <sha.h> |
| 38 | #include <string.h> |
| 39 | #include <rand.h> |
| 40 | #include <stdlib.h> |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 41 | #include <boot_verifier.h> |
| 42 | #include <image_verify.h> |
Reut Zysman | 3f3eccd | 2016-04-20 22:05:36 +0300 | [diff] [blame] | 43 | #include <qtimer.h> |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 44 | #include "scm.h" |
| 45 | #include "mdtp.h" |
Reut Zysman | ff6bab9 | 2016-02-09 14:06:31 +0200 | [diff] [blame] | 46 | #include "mdtp_fs.h" |
| 47 | |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 48 | |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 49 | #define DIP_ENCRYPT (0) |
| 50 | #define DIP_DECRYPT (1) |
| 51 | #define MAX_CIPHER_DIP_SCM_CALLS (3) |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 52 | |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 53 | #define MDTP_MAJOR_VERSION (0) |
| 54 | #define MDTP_MINOR_VERSION (2) |
| 55 | |
Reut Zysman | d713862 | 2016-01-18 14:43:59 +0200 | [diff] [blame] | 56 | #define MDTP_CORRECT_PIN_DELAY_MSEC (1000) |
| 57 | |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 58 | /** Extract major version number from complete version. */ |
| 59 | #define MDTP_GET_MAJOR_VERSION(version) ((version) >> 16) |
| 60 | |
Amir Kotzer | bb8be14 | 2016-02-23 09:38:25 +0200 | [diff] [blame] | 61 | |
| 62 | /** UT defines **/ |
| 63 | #define BAD_PARAM_SIZE 0 |
| 64 | #define BAD_PARAM_VERIF_RATIO 101 |
| 65 | #define BAD_HASH_MODE 10 |
| 66 | |
| 67 | /********************************************************************************/ |
| 68 | |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 69 | static int mdtp_tzbsp_dec_verify_DIP(DIP_t *enc_dip, DIP_t *dec_dip, uint32_t *verified); |
| 70 | static int mdtp_tzbsp_enc_hash_DIP(DIP_t *dec_dip, DIP_t *enc_dip); |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 71 | static void mdtp_tzbsp_disallow_cipher_DIP(void); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 72 | |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 73 | uint32_t g_mdtp_version = (((MDTP_MAJOR_VERSION << 16) & 0xFFFF0000) | (MDTP_MINOR_VERSION & 0x0000FFFF)); |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 74 | static int is_mdtp_activated = -1; |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 75 | |
Vijay Kumar Pendoti | 9c002ad | 2016-03-09 13:52:45 +0530 | [diff] [blame] | 76 | extern int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size); |
Rami Butstein | faecf7f | 2015-06-04 16:39:30 +0300 | [diff] [blame] | 77 | void free_mdtp_image(void); |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 78 | |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 79 | /********************************************************************************/ |
| 80 | |
| 81 | /* Read the DIP from EMMC */ |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 82 | static int read_DIP(DIP_t *dip) |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 83 | { |
| 84 | unsigned long long ptn = 0; |
| 85 | uint32_t actual_partition_size; |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 86 | uint32_t block_size = mmc_get_device_blocksize(); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 87 | |
| 88 | int index = INVALID_PTN; |
| 89 | |
| 90 | ASSERT(dip != NULL); |
| 91 | |
| 92 | index = partition_get_index("dip"); |
| 93 | ptn = partition_get_offset(index); |
| 94 | |
| 95 | if(ptn == 0) |
| 96 | { |
| 97 | return -1; |
| 98 | } |
| 99 | |
| 100 | actual_partition_size = ROUNDUP(sizeof(DIP_t), block_size); |
| 101 | |
| 102 | if(mmc_read(ptn, (void *)dip, actual_partition_size)) |
| 103 | { |
| 104 | dprintf(CRITICAL, "mdtp: read_DIP: ERROR, cannot read DIP info\n"); |
| 105 | return -1; |
| 106 | } |
| 107 | |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 108 | dprintf(SPEW, "mdtp: read_DIP: SUCCESS, read %d bytes\n", actual_partition_size); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | /* Store the DIP into the EMMC */ |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 114 | static int write_DIP(DIP_t *dip) |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 115 | { |
| 116 | unsigned long long ptn = 0; |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 117 | uint32_t block_size = mmc_get_device_blocksize(); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 118 | |
| 119 | int index = INVALID_PTN; |
| 120 | |
| 121 | ASSERT(dip != NULL); |
| 122 | |
| 123 | index = partition_get_index("dip"); |
| 124 | ptn = partition_get_offset(index); |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 125 | |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 126 | if(ptn == 0) |
| 127 | { |
| 128 | return -1; |
| 129 | } |
| 130 | |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 131 | if(mmc_write(ptn, ROUNDUP(sizeof(DIP_t), block_size), (void *)dip)) |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 132 | { |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 133 | dprintf(CRITICAL, "mdtp: write_DIP: ERROR, cannot write DIP info\n"); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 134 | return -1; |
| 135 | } |
| 136 | |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 137 | dprintf(SPEW, "mdtp: write_DIP: SUCCESS, write %d bytes\n", ROUNDUP(sizeof(DIP_t), block_size)); |
| 138 | |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 139 | return 0; |
| 140 | } |
| 141 | |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 142 | /* Deactivate MDTP by storing the default DIP into the EMMC */ |
| 143 | static void write_deactivated_DIP() |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 144 | { |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 145 | DIP_t *enc_dip; |
| 146 | DIP_t *dec_dip; |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 147 | int ret; |
| 148 | |
| 149 | enc_dip = malloc(sizeof(DIP_t)); |
| 150 | if (enc_dip == NULL) |
| 151 | { |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 152 | dprintf(CRITICAL, "mdtp: write_deactivated_DIP: ERROR, cannot allocate DIP\n"); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 153 | return; |
| 154 | } |
| 155 | |
| 156 | dec_dip = malloc(sizeof(DIP_t)); |
| 157 | if (dec_dip == NULL) |
| 158 | { |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 159 | dprintf(CRITICAL, "mdtp: write_deactivated_DIP: ERROR, cannot allocate DIP\n"); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 160 | free(enc_dip); |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | memset(dec_dip, 0, sizeof(DIP_t)); |
| 165 | |
| 166 | dec_dip->status = DIP_STATUS_DEACTIVATED; |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 167 | dec_dip->version = g_mdtp_version; |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 168 | |
| 169 | ret = mdtp_tzbsp_enc_hash_DIP(dec_dip, enc_dip); |
| 170 | if(ret < 0) |
| 171 | { |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 172 | dprintf(CRITICAL, "mdtp: write_deactivated_DIP: ERROR, cannot cipher DIP\n"); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 173 | goto out; |
| 174 | } |
| 175 | |
| 176 | ret = write_DIP(enc_dip); |
| 177 | if(ret < 0) |
| 178 | { |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 179 | dprintf(CRITICAL, "mdtp: write_deactivated_DIP: ERROR, cannot write DIP\n"); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 180 | goto out; |
| 181 | } |
| 182 | |
| 183 | out: |
| 184 | free(enc_dip); |
| 185 | free(dec_dip); |
| 186 | } |
| 187 | |
| 188 | /* Validate a hash calculated on entire given partition */ |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 189 | static int verify_partition_single_hash(char *name, uint64_t size, DIP_hash_table_entry_t *hash_table) |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 190 | { |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 191 | unsigned char digest[HASH_LEN]={0}; |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 192 | unsigned long long ptn = 0; |
| 193 | int index = INVALID_PTN; |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 194 | unsigned char *buf = (unsigned char *)target_get_scratch_address() + MDTP_SCRATCH_OFFSET; |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 195 | uint32_t block_size = mmc_get_device_blocksize(); |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 196 | uint64_t actual_partition_size = ROUNDUP(size, block_size); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 197 | |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 198 | dprintf(SPEW, "mdtp: verify_partition_single_hash: %s, %llu\n", name, size); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 199 | |
| 200 | ASSERT(name != NULL); |
| 201 | ASSERT(hash_table != NULL); |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 202 | ASSERT(size > 0); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 203 | |
| 204 | index = partition_get_index(name); |
| 205 | ptn = partition_get_offset(index); |
| 206 | |
| 207 | if(ptn == 0) { |
| 208 | dprintf(CRITICAL, "mdtp: verify_partition_single_hash: %s: partition was not found\n", name); |
| 209 | return -1; |
| 210 | } |
| 211 | |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 212 | if (mmc_read(ptn, (void *)buf, actual_partition_size)) |
| 213 | { |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 214 | dprintf(CRITICAL, "mdtp: verify_partition_single_hash: %s: mmc_read() fail.\n", name); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 215 | return -1; |
| 216 | } |
| 217 | |
Amit Blay | fe64921 | 2015-01-25 11:21:10 +0200 | [diff] [blame] | 218 | /* calculating the hash value using HW crypto */ |
| 219 | target_crypto_init_params(); |
Reut Zysman | ff6bab9 | 2016-02-09 14:06:31 +0200 | [diff] [blame] | 220 | |
| 221 | if(strcmp(name, "mdtp") == 0){ |
| 222 | buf[0] = 0; // removes first byte |
| 223 | dprintf(INFO, "mdtp: verify_partition_single_hash: removes 1st byte\n"); |
| 224 | } |
| 225 | |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 226 | hash_find(buf, size, digest, CRYPTO_AUTH_ALG_SHA256); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 227 | |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 228 | if (memcmp(digest, hash_table->hash, HASH_LEN)) |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 229 | { |
| 230 | dprintf(CRITICAL, "mdtp: verify_partition_single_hash: %s: Failed partition hash verification\n", name); |
| 231 | |
| 232 | return -1; |
| 233 | } |
| 234 | |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 235 | dprintf(SPEW, "verify_partition_single_hash: %s: VERIFIED!\n", name); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 236 | |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | /* Validate a hash table calculated per block of a given partition */ |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 241 | static int verify_partition_block_hash(char *name, |
Reut Zysman | 3f3eccd | 2016-04-20 22:05:36 +0300 | [diff] [blame] | 242 | uint64_t size, |
| 243 | uint32_t verify_num_blocks, |
| 244 | DIP_hash_table_entry_t *hash_table, |
| 245 | uint8_t *force_verify_block) |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 246 | { |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 247 | unsigned char digest[HASH_LEN]={0}; |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 248 | unsigned long long ptn = 0; |
| 249 | int index = INVALID_PTN; |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 250 | unsigned char *buf = (unsigned char *)target_get_scratch_address() + MDTP_SCRATCH_OFFSET; |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 251 | uint32_t bytes_to_read; |
| 252 | uint32_t block_num = 0; |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 253 | uint32_t total_num_blocks = ((size - 1) / MDTP_FWLOCK_BLOCK_SIZE) + 1; |
| 254 | uint32_t rand_int; |
| 255 | uint32_t block_size = mmc_get_device_blocksize(); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 256 | |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 257 | dprintf(SPEW, "mdtp: verify_partition_block_hash: %s, %llu\n", name, size); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 258 | |
| 259 | ASSERT(name != NULL); |
| 260 | ASSERT(hash_table != NULL); |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 261 | ASSERT(size > 0); |
| 262 | ASSERT(force_verify_block != NULL); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 263 | |
| 264 | index = partition_get_index(name); |
| 265 | ptn = partition_get_offset(index); |
| 266 | |
| 267 | if(ptn == 0) { |
| 268 | dprintf(CRITICAL, "mdtp: verify_partition_block_hash: %s: partition was not found\n", name); |
| 269 | return -1; |
| 270 | } |
| 271 | |
Amit Blay | fe64921 | 2015-01-25 11:21:10 +0200 | [diff] [blame] | 272 | /* initiating parameters for hash calculation using HW crypto */ |
| 273 | target_crypto_init_params(); |
Vijay Kumar Pendoti | 9c002ad | 2016-03-09 13:52:45 +0530 | [diff] [blame] | 274 | if (check_aboot_addr_range_overlap((uintptr_t)buf, ROUNDUP(MDTP_FWLOCK_BLOCK_SIZE, block_size))) |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 275 | { |
| 276 | dprintf(CRITICAL, "mdtp: verify_partition_block_hash: %s: image buffer address overlaps with aboot addresses.\n", name); |
| 277 | return -1; |
| 278 | } |
Amit Blay | fe64921 | 2015-01-25 11:21:10 +0200 | [diff] [blame] | 279 | |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 280 | while (MDTP_FWLOCK_BLOCK_SIZE * block_num < size) |
| 281 | { |
| 282 | if (*force_verify_block == 0) |
| 283 | { |
lijuang | 0889e19 | 2017-12-19 16:29:37 +0800 | [diff] [blame] | 284 | if(scm_random((uintptr_t *)&rand_int, sizeof(rand_int))) |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 285 | { |
| 286 | dprintf(CRITICAL,"mdtp: scm_call for random failed\n"); |
| 287 | return -1; |
| 288 | } |
| 289 | |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 290 | /* Skip validation of this block with probability of verify_num_blocks / total_num_blocks */ |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 291 | if ((rand_int % total_num_blocks) >= verify_num_blocks) |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 292 | { |
| 293 | block_num++; |
| 294 | hash_table += 1; |
| 295 | force_verify_block += 1; |
| 296 | dprintf(CRITICAL, "mdtp: verify_partition_block_hash: %s: skipped verification of block %d\n", name, block_num); |
| 297 | continue; |
| 298 | } |
| 299 | } |
| 300 | |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 301 | if ((size - (MDTP_FWLOCK_BLOCK_SIZE * block_num) < MDTP_FWLOCK_BLOCK_SIZE)) |
| 302 | { |
| 303 | bytes_to_read = size - (MDTP_FWLOCK_BLOCK_SIZE * block_num); |
| 304 | } else |
| 305 | { |
| 306 | bytes_to_read = MDTP_FWLOCK_BLOCK_SIZE; |
| 307 | } |
| 308 | |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 309 | if (mmc_read(ptn + (MDTP_FWLOCK_BLOCK_SIZE * block_num), (void *)buf, ROUNDUP(bytes_to_read, block_size))) |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 310 | { |
| 311 | dprintf(CRITICAL, "mdtp: verify_partition_block_hash: %s: mmc_read() fail.\n", name); |
| 312 | return -1; |
| 313 | } |
| 314 | |
Amit Blay | fe64921 | 2015-01-25 11:21:10 +0200 | [diff] [blame] | 315 | /* calculating the hash value using HW */ |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 316 | hash_find(buf, bytes_to_read, digest, CRYPTO_AUTH_ALG_SHA256); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 317 | |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 318 | if (memcmp(digest, hash_table->hash, HASH_LEN)) |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 319 | { |
| 320 | dprintf(CRITICAL, "mdtp: verify_partition_block_hash: %s: Failed partition hash[%d] verification\n", name, block_num); |
| 321 | return -1; |
| 322 | } |
| 323 | |
| 324 | block_num++; |
| 325 | hash_table += 1; |
| 326 | force_verify_block += 1; |
| 327 | } |
| 328 | |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 329 | dprintf(SPEW, "verify_partition_block_hash: %s: VERIFIED!\n", name); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 330 | |
| 331 | return 0; |
| 332 | } |
| 333 | |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 334 | /* Validate the partition parameters read from DIP */ |
| 335 | static int validate_partition_params(uint64_t size, |
Reut Zysman | 3f3eccd | 2016-04-20 22:05:36 +0300 | [diff] [blame] | 336 | mdtp_fwlock_mode_t hash_mode, |
| 337 | uint32_t verify_ratio) |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 338 | { |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 339 | if (size == 0 || size > (uint64_t)MDTP_FWLOCK_BLOCK_SIZE * (uint64_t)MAX_BLOCKS || |
Reut Zysman | 3f3eccd | 2016-04-20 22:05:36 +0300 | [diff] [blame] | 340 | hash_mode > MDTP_FWLOCK_MODE_FILES || verify_ratio > 100) |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 341 | { |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 342 | dprintf(CRITICAL, "mdtp: validate_partition_params: error, size=%llu, hash_mode=%d, verify_ratio=%d\n", |
Reut Zysman | 3f3eccd | 2016-04-20 22:05:36 +0300 | [diff] [blame] | 343 | size, hash_mode, verify_ratio); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 344 | return -1; |
| 345 | } |
| 346 | |
| 347 | return 0; |
| 348 | } |
| 349 | |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 350 | /* Verify a given partitinon */ |
| 351 | static int verify_partition(char *name, |
Reut Zysman | 3f3eccd | 2016-04-20 22:05:36 +0300 | [diff] [blame] | 352 | uint64_t size, |
| 353 | mdtp_fwlock_mode_t hash_mode, |
| 354 | uint32_t verify_num_blocks, |
| 355 | DIP_hash_table_entry_t *hash_table, |
| 356 | uint8_t *force_verify_block) |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 357 | { |
| 358 | if (hash_mode == MDTP_FWLOCK_MODE_SINGLE) |
| 359 | { |
| 360 | return verify_partition_single_hash(name, size, hash_table); |
| 361 | } else if (hash_mode == MDTP_FWLOCK_MODE_BLOCK || hash_mode == MDTP_FWLOCK_MODE_FILES) |
| 362 | { |
| 363 | return verify_partition_block_hash(name, size, verify_num_blocks, hash_table, force_verify_block); |
| 364 | } |
| 365 | |
| 366 | /* Illegal value of hash_mode */ |
| 367 | return -1; |
| 368 | } |
| 369 | |
| 370 | static int validate_dip(DIP_t *dip) |
| 371 | { |
| 372 | uint8_t *dip_p; |
| 373 | |
| 374 | ASSERT(dip != NULL); |
| 375 | |
| 376 | /* Make sure DIP version is supported by current SW */ |
| 377 | if (MDTP_GET_MAJOR_VERSION(dip->version) != MDTP_MAJOR_VERSION) |
| 378 | { |
| 379 | dprintf(CRITICAL, "mdtp: validate_dip: Wrong DIP version 0x%x\n", dip->version); |
| 380 | return -1; |
| 381 | } |
| 382 | |
| 383 | /* Make sure that deactivated DIP content is as expected */ |
| 384 | if (dip->status == DIP_STATUS_DEACTIVATED) |
| 385 | { |
| 386 | dip_p = (uint8_t*)&dip->mdtp_cfg; |
| 387 | while (dip_p < dip->hash) |
| 388 | { |
| 389 | if (*dip_p != 0) |
| 390 | { |
| 391 | dprintf(CRITICAL, "mdtp: validate_dip: error in deactivated DIP\n"); |
| 392 | return -1; |
| 393 | } |
| 394 | dip_p++; |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | return 0; |
| 399 | } |
| 400 | |
Reut Zysman | ff6bab9 | 2016-02-09 14:06:31 +0200 | [diff] [blame] | 401 | /* Display the recovery UI in case mdtp image is corrupted */ |
| 402 | static void display_mdtp_fail_recovery_ui(){ |
| 403 | display_error_msg_mdtp(); |
| 404 | } |
| 405 | |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 406 | /* Display the recovery UI to allow the user to enter the PIN and continue boot */ |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 407 | static void display_recovery_ui(mdtp_cfg_t *mdtp_cfg) |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 408 | { |
| 409 | uint32_t pin_length = 0; |
Amir Kotzer | 20716f1 | 2016-02-24 10:31:18 +0200 | [diff] [blame] | 410 | char entered_pin[MDTP_PIN_LEN+1] = {0}; |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 411 | uint32_t i; |
Amir Kotzer | 3b3217c | 2016-02-29 17:20:28 +0200 | [diff] [blame] | 412 | int pin_mismatch = -1; |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 413 | |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 414 | if (mdtp_cfg->enable_local_pin_authentication) |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 415 | { |
| 416 | dprintf(SPEW, "mdtp: display_recovery_ui: Local deactivation enabled\n"); |
| 417 | |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 418 | pin_length = strlen(mdtp_cfg->mdtp_pin.mdtp_pin); |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 419 | |
Amir Kotzer | 20716f1 | 2016-02-24 10:31:18 +0200 | [diff] [blame] | 420 | if (pin_length != MDTP_PIN_LEN) |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 421 | { |
| 422 | dprintf(CRITICAL, "mdtp: display_recovery_ui: Error, invalid PIN length\n"); |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 423 | display_error_msg(); /* This will never return */ |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | // Set entered_pin to initial '0' string + null terminator |
| 427 | for (i=0; i<pin_length; i++) |
| 428 | { |
| 429 | entered_pin[i] = '0'; |
| 430 | } |
| 431 | |
| 432 | // Allow the user to enter the PIN as many times as he wishes |
| 433 | // (with INVALID_PIN_DELAY_MSECONDS after each failed attempt) |
| 434 | while (1) |
| 435 | { |
Amir Kotzer | 3b3217c | 2016-02-29 17:20:28 +0200 | [diff] [blame] | 436 | pin_mismatch = pin_length; |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 437 | get_pin_from_user(entered_pin, pin_length); |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 438 | |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 439 | // Go over the entire PIN in any case, to prevent side-channel attacks |
| 440 | for (i=0; i<pin_length; i++) |
| 441 | { |
Amir Kotzer | 3b3217c | 2016-02-29 17:20:28 +0200 | [diff] [blame] | 442 | // If current digit match, reduce 1 from pin_mismatch |
| 443 | pin_mismatch -= (((mdtp_cfg->mdtp_pin.mdtp_pin[i] ^ entered_pin[i]) == 0) ? 1 : 0); |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 444 | } |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 445 | |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 446 | if (0 == pin_mismatch) |
| 447 | { |
| 448 | // Valid PIN - deactivate and continue boot |
| 449 | dprintf(SPEW, "mdtp: display_recovery_ui: valid PIN, continue boot\n"); |
| 450 | write_deactivated_DIP(); |
Rami Butstein | faecf7f | 2015-06-04 16:39:30 +0300 | [diff] [blame] | 451 | goto out; |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 452 | } |
| 453 | else |
| 454 | { |
| 455 | // Invalid PIN - display an appropriate message (which also includes a wait |
| 456 | // for INVALID_PIN_DELAY_MSECONDS), and allow the user to try again |
| 457 | dprintf(CRITICAL, "mdtp: display_recovery_ui: ERROR, invalid PIN\n"); |
| 458 | display_invalid_pin_msg(); |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 459 | } |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | else |
| 463 | { |
| 464 | dprintf(CRITICAL, "mdtp: display_recovery_ui: Local deactivation disabled, unable to display recovery UI\n"); |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 465 | display_error_msg(); /* This will never return */ |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 466 | } |
Rami Butstein | faecf7f | 2015-06-04 16:39:30 +0300 | [diff] [blame] | 467 | |
| 468 | out: |
Amit Blay | 5484915 | 2015-11-09 15:51:40 +0200 | [diff] [blame] | 469 | display_image_on_screen(); |
Rami Butstein | faecf7f | 2015-06-04 16:39:30 +0300 | [diff] [blame] | 470 | free_mdtp_image(); |
Reut Zysman | d713862 | 2016-01-18 14:43:59 +0200 | [diff] [blame] | 471 | mdelay(MDTP_CORRECT_PIN_DELAY_MSEC); |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 472 | } |
| 473 | |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 474 | /* Verify the boot or recovery partitions using boot_verifier. */ |
| 475 | static int verify_ext_partition(mdtp_ext_partition_verification_t *ext_partition) |
| 476 | { |
| 477 | int ret = 0; |
| 478 | bool restore_to_orange = false; |
| 479 | unsigned long long ptn = 0; |
| 480 | int index = INVALID_PTN; |
Monika Singh | 7d2fc27 | 2018-03-16 17:16:01 +0530 | [diff] [blame] | 481 | uint32_t bootstate; |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 482 | |
| 483 | /* If image was already verified in aboot, return its status */ |
| 484 | if (ext_partition->integrity_state == MDTP_PARTITION_STATE_INVALID) |
| 485 | { |
| 486 | dprintf(CRITICAL, "mdtp: verify_ext_partition: image %s verified externally and failed.\n", |
| 487 | ext_partition->partition == MDTP_PARTITION_BOOT ? "boot" : "recovery"); |
| 488 | return -1; |
| 489 | } |
| 490 | else if (ext_partition->integrity_state == MDTP_PARTITION_STATE_VALID) |
| 491 | { |
| 492 | dprintf(CRITICAL, "mdtp: verify_ext_partition: image %s verified externally succesfully.\n", |
| 493 | ext_partition->partition == MDTP_PARTITION_BOOT ? "boot" : "recovery"); |
| 494 | return 0; |
| 495 | } |
| 496 | |
| 497 | /* If image was not verified in aboot, verify it ourselves using boot_verifier. */ |
| 498 | |
| 499 | /* 1) Initialize keystore. We don't care about return value which is Verified Boot's state machine state. */ |
| 500 | boot_verify_keystore_init(); |
| 501 | |
| 502 | /* 2) If boot_verifier is ORANGE, it will prevent verifying an image. So |
| 503 | * temporarly change boot_verifier state to BOOT_INIT. |
| 504 | */ |
| 505 | if (boot_verify_get_state() == ORANGE) |
| 506 | restore_to_orange = true; |
| 507 | boot_verify_send_event(BOOT_INIT); |
| 508 | |
| 509 | switch (ext_partition->partition) |
| 510 | { |
| 511 | case MDTP_PARTITION_BOOT: |
| 512 | case MDTP_PARTITION_RECOVERY: |
| 513 | |
| 514 | /* 3) Signature may or may not be at the end of the image. Read the signature if needed. */ |
| 515 | if (!ext_partition->sig_avail) |
| 516 | { |
Vijay Kumar Pendoti | 9c002ad | 2016-03-09 13:52:45 +0530 | [diff] [blame] | 517 | if (check_aboot_addr_range_overlap((uintptr_t)(ext_partition->image_addr + ext_partition->image_size), ext_partition->page_size)) |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 518 | { |
| 519 | dprintf(CRITICAL, "ERROR: Signature read buffer address overlaps with aboot addresses.\n"); |
| 520 | return -1; |
| 521 | } |
| 522 | |
| 523 | index = partition_get_index(ext_partition->partition == MDTP_PARTITION_BOOT ? "boot" : "recovery"); |
| 524 | ptn = partition_get_offset(index); |
| 525 | if(ptn == 0) { |
| 526 | dprintf(CRITICAL, "ERROR: partition %s not found\n", |
Reut Zysman | 3f3eccd | 2016-04-20 22:05:36 +0300 | [diff] [blame] | 527 | ext_partition->partition == MDTP_PARTITION_BOOT ? "boot" : "recovery"); |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 528 | return -1; |
| 529 | } |
| 530 | |
| 531 | if(mmc_read(ptn + ext_partition->image_size, (void *)(ext_partition->image_addr + ext_partition->image_size), ext_partition->page_size)) |
| 532 | { |
| 533 | dprintf(CRITICAL, "ERROR: Cannot read %s image signature\n", |
Reut Zysman | 3f3eccd | 2016-04-20 22:05:36 +0300 | [diff] [blame] | 534 | ext_partition->partition == MDTP_PARTITION_BOOT ? "boot" : "recovery"); |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 535 | return -1; |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | /* 4) Verify the image using its signature. */ |
| 540 | ret = boot_verify_image((unsigned char *)ext_partition->image_addr, |
| 541 | ext_partition->image_size, |
Monika Singh | 7d2fc27 | 2018-03-16 17:16:01 +0530 | [diff] [blame] | 542 | ext_partition->partition == MDTP_PARTITION_BOOT ? "/boot" : "/recovery", |
| 543 | &bootstate); |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 544 | break; |
| 545 | |
| 546 | default: |
| 547 | /* Only boot and recovery are legal here */ |
| 548 | dprintf(CRITICAL, "ERROR: wrong partition %d\n", ext_partition->partition); |
| 549 | return -1; |
| 550 | } |
| 551 | |
| 552 | if (ret) |
| 553 | { |
| 554 | dprintf(INFO, "mdtp: verify_ext_partition: image %s verified succesfully in MDTP.\n", |
| 555 | ext_partition->partition == MDTP_PARTITION_BOOT ? "boot" : "recovery"); |
| 556 | } |
| 557 | else |
| 558 | { |
| 559 | dprintf(CRITICAL, "mdtp: verify_ext_partition: image %s verification failed in MDTP.\n", |
| 560 | ext_partition->partition == MDTP_PARTITION_BOOT ? "boot" : "recovery"); |
| 561 | } |
| 562 | |
| 563 | /* 5) Restore the right boot_verifier state upon exit. */ |
| 564 | if (restore_to_orange) |
| 565 | { |
| 566 | boot_verify_send_event(DEV_UNLOCK); |
| 567 | } |
| 568 | |
| 569 | return ret ? 0 : -1; |
| 570 | } |
| 571 | |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 572 | /* Verify all protected partitinons according to the DIP */ |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 573 | static void verify_all_partitions(DIP_t *dip, |
Reut Zysman | 3f3eccd | 2016-04-20 22:05:36 +0300 | [diff] [blame] | 574 | mdtp_ext_partition_verification_t *ext_partition, |
| 575 | verify_result_t *verify_result) |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 576 | { |
| 577 | int i; |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 578 | int verify_failure = 0; |
Reut Zysman | ff6bab9 | 2016-02-09 14:06:31 +0200 | [diff] [blame] | 579 | int verify_temp_result = 0; |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 580 | int ext_partition_verify_failure = 0; |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 581 | uint32_t total_num_blocks; |
| 582 | |
| 583 | ASSERT(dip != NULL); |
| 584 | ASSERT(verify_result != NULL); |
| 585 | |
| 586 | *verify_result = VERIFY_FAILED; |
| 587 | |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 588 | if (validate_dip(dip)) |
| 589 | { |
| 590 | dprintf(CRITICAL, "mdtp: verify_all_partitions: failed DIP validation\n"); |
| 591 | return; |
| 592 | } |
| 593 | |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 594 | if (dip->status == DIP_STATUS_DEACTIVATED) |
| 595 | { |
| 596 | *verify_result = VERIFY_SKIPPED; |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 597 | return; |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 598 | } |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 599 | else |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 600 | { |
Amit Blay | 8a51030 | 2015-08-17 09:20:01 +0300 | [diff] [blame] | 601 | if (ext_partition->partition != MDTP_PARTITION_NONE) |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 602 | { |
Amit Blay | 8a51030 | 2015-08-17 09:20:01 +0300 | [diff] [blame] | 603 | for(i=0; i<MAX_PARTITIONS; i++) |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 604 | { |
Reut Zysman | ff6bab9 | 2016-02-09 14:06:31 +0200 | [diff] [blame] | 605 | verify_temp_result = 0; |
Amit Blay | 8a51030 | 2015-08-17 09:20:01 +0300 | [diff] [blame] | 606 | if(dip->partition_cfg[i].lock_enabled && dip->partition_cfg[i].size) |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 607 | { |
Amit Blay | 8a51030 | 2015-08-17 09:20:01 +0300 | [diff] [blame] | 608 | total_num_blocks = ((dip->partition_cfg[i].size - 1) / MDTP_FWLOCK_BLOCK_SIZE); |
| 609 | if (validate_partition_params(dip->partition_cfg[i].size, |
| 610 | dip->partition_cfg[i].hash_mode, |
| 611 | dip->partition_cfg[i].verify_ratio)) |
| 612 | { |
| 613 | dprintf(CRITICAL, "mdtp: verify_all_partitions: Wrong partition parameters\n"); |
| 614 | verify_failure = TRUE; |
| 615 | break; |
| 616 | } |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 617 | |
Reut Zysman | ff6bab9 | 2016-02-09 14:06:31 +0200 | [diff] [blame] | 618 | verify_temp_result |= (verify_partition(dip->partition_cfg[i].name, |
Amit Blay | 8a51030 | 2015-08-17 09:20:01 +0300 | [diff] [blame] | 619 | dip->partition_cfg[i].size, |
| 620 | dip->partition_cfg[i].hash_mode, |
| 621 | (dip->partition_cfg[i].verify_ratio * total_num_blocks) / 100, |
| 622 | dip->partition_cfg[i].hash_table, |
| 623 | dip->partition_cfg[i].force_verify_block) != 0); |
Reut Zysman | ff6bab9 | 2016-02-09 14:06:31 +0200 | [diff] [blame] | 624 | |
| 625 | if((verify_temp_result) && (strcmp("mdtp",dip->partition_cfg[i].name) == 0)){ |
| 626 | *verify_result = VERIFY_MDTP_FAILED; |
| 627 | } |
| 628 | |
| 629 | verify_failure |= verify_temp_result; |
Amit Blay | 8a51030 | 2015-08-17 09:20:01 +0300 | [diff] [blame] | 630 | } |
| 631 | } |
| 632 | |
| 633 | ext_partition_verify_failure = verify_ext_partition(ext_partition); |
| 634 | |
| 635 | if (verify_failure || ext_partition_verify_failure) |
| 636 | { |
| 637 | dprintf(CRITICAL, "mdtp: verify_all_partitions: Failed partition verification\n"); |
| 638 | return; |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 639 | } |
| 640 | } |
Shay Nachmani | bc10dfe | 2015-02-10 14:45:55 +0200 | [diff] [blame] | 641 | is_mdtp_activated = 1; |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | *verify_result = VERIFY_OK; |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 645 | return; |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | /* Verify the DIP and all protected partitions */ |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 649 | static void validate_DIP_and_firmware(mdtp_ext_partition_verification_t *ext_partition) |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 650 | { |
| 651 | int ret; |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 652 | DIP_t *enc_dip; |
| 653 | DIP_t *dec_dip; |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 654 | uint32_t verified = 0; |
| 655 | verify_result_t verify_result; |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 656 | uint32_t block_size = mmc_get_device_blocksize(); |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 657 | mdtp_cfg_t mdtp_cfg; |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 658 | |
lijuang | c267675 | 2018-05-15 13:52:36 +0800 | [diff] [blame] | 659 | enc_dip = memalign(CACHE_LINE, ROUNDUP(sizeof(DIP_t), block_size)); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 660 | if (enc_dip == NULL) |
| 661 | { |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 662 | dprintf(CRITICAL, "mdtp: validate_DIP_and_firmware: ERROR, cannot allocate DIP\n"); |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 663 | display_error_msg(); /* This will never return */ |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 664 | } |
| 665 | |
lijuang | c267675 | 2018-05-15 13:52:36 +0800 | [diff] [blame] | 666 | dec_dip = memalign(CACHE_LINE, ROUNDUP(sizeof(DIP_t), block_size)); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 667 | if (dec_dip == NULL) |
| 668 | { |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 669 | dprintf(CRITICAL, "mdtp: validate_DIP_and_firmware: ERROR, cannot allocate DIP\n"); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 670 | free(enc_dip); |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 671 | display_error_msg(); /* This will never return */ |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | /* Read the DIP holding the MDTP Firmware Lock state from the DIP partition */ |
| 675 | ret = read_DIP(enc_dip); |
| 676 | if(ret < 0) |
| 677 | { |
| 678 | dprintf(CRITICAL, "mdtp: validate_DIP_and_firmware: ERROR, cannot read DIP\n"); |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 679 | display_error_msg(); /* This will never return */ |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | /* Decrypt and verify the integrity of the DIP */ |
| 683 | ret = mdtp_tzbsp_dec_verify_DIP(enc_dip, dec_dip, &verified); |
| 684 | if(ret < 0) |
| 685 | { |
| 686 | dprintf(CRITICAL, "mdtp: validate_DIP_and_firmware: ERROR, cannot verify DIP\n"); |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 687 | display_error_msg(); /* This will never return */ |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | /* In case DIP integrity verification fails, notify the user and halt */ |
| 691 | if(!verified) |
| 692 | { |
| 693 | dprintf(CRITICAL, "mdtp: validate_DIP_and_firmware: ERROR, corrupted DIP\n"); |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 694 | display_error_msg(); /* This will never return */ |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 695 | } |
| 696 | |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 697 | /* Verify the integrity of the partitions which are protected, according to the content of the DIP */ |
| 698 | verify_all_partitions(dec_dip, ext_partition, &verify_result); |
| 699 | |
| 700 | mdtp_cfg = dec_dip->mdtp_cfg; |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 701 | |
| 702 | /* Clear decrypted DIP since we don't need it anymore */ |
| 703 | memset(dec_dip, 0, sizeof(DIP_t)); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 704 | |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 705 | |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 706 | if (verify_result == VERIFY_OK) |
| 707 | { |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 708 | dprintf(SPEW, "mdtp: validate_DIP_and_firmware: Verify OK\n"); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 709 | } |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 710 | else if (verify_result == VERIFY_SKIPPED) |
| 711 | { |
| 712 | dprintf(SPEW, "mdtp: validate_DIP_and_firmware: Verify skipped\n"); |
Reut Zysman | ff6bab9 | 2016-02-09 14:06:31 +0200 | [diff] [blame] | 713 | } |
| 714 | else if(verify_result == VERIFY_MDTP_FAILED) |
| 715 | { |
| 716 | dprintf(CRITICAL, "mdtp: validate_DIP_and_firmware: ERROR, corrupted mdtp image\n"); |
| 717 | display_mdtp_fail_recovery_ui(); |
| 718 | } |
| 719 | else /* VERIFY_FAILED */ |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 720 | { |
| 721 | dprintf(CRITICAL, "mdtp: validate_DIP_and_firmware: ERROR, corrupted firmware\n"); |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 722 | display_recovery_ui(&mdtp_cfg); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 723 | } |
| 724 | |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 725 | memset(&mdtp_cfg, 0, sizeof(mdtp_cfg)); |
| 726 | |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 727 | free(enc_dip); |
| 728 | free(dec_dip); |
| 729 | |
| 730 | return; |
| 731 | } |
| 732 | |
Reut Zysman | ff6bab9 | 2016-02-09 14:06:31 +0200 | [diff] [blame] | 733 | |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 734 | /********************************************************************************/ |
| 735 | |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 736 | /** Entry point of the MDTP Firmware Lock. |
| 737 | * If needed, verify the DIP and all protected partitions. |
| 738 | * Allow passing information about partition verified using an external method |
| 739 | * (either boot or recovery). For boot and recovery, either use aboot's |
| 740 | * verification result, or use boot_verifier APIs to verify internally. |
| 741 | **/ |
| 742 | void mdtp_fwlock_verify_lock(mdtp_ext_partition_verification_t *ext_partition) |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 743 | { |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 744 | int ret; |
| 745 | bool enabled; |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 746 | |
Reut Zysman | ff6bab9 | 2016-02-09 14:06:31 +0200 | [diff] [blame] | 747 | if(mdtp_fs_init() != 0){ |
| 748 | dprintf(CRITICAL, "mdtp: mdtp_img: ERROR, image file could not be loaded\n"); |
| 749 | display_error_msg_mdtp(); /* This will never return */ |
| 750 | } |
Shay Nachmani | bc10dfe | 2015-02-10 14:45:55 +0200 | [diff] [blame] | 751 | /* sets the default value of this global to be MDTP not activated */ |
| 752 | is_mdtp_activated = 0; |
| 753 | |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 754 | do { |
| 755 | if (ext_partition == NULL) |
| 756 | { |
| 757 | dprintf(CRITICAL, "mdtp: mdtp_fwlock_verify_lock: ERROR, external partition is NULL\n"); |
| 758 | display_error_msg(); /* This will never return */ |
| 759 | break; |
| 760 | } |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 761 | |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 762 | ret = mdtp_fuse_get_enabled(&enabled); |
| 763 | if(ret) |
| 764 | { |
| 765 | dprintf(CRITICAL, "mdtp: mdtp_fwlock_verify_lock: ERROR, cannot get enabled fuse\n"); |
| 766 | display_error_msg(); /* This will never return */ |
| 767 | } |
| 768 | |
| 769 | /* Continue with Firmware Lock verification only if enabled by eFuse */ |
| 770 | if (enabled) |
| 771 | { |
| 772 | /* This function will handle firmware verification failure via UI */ |
| 773 | validate_DIP_and_firmware(ext_partition); |
| 774 | } |
| 775 | } while (0); |
| 776 | |
| 777 | /* Disallow CIPHER_DIP SCM call from this point, unless we are in recovery */ |
| 778 | /* The recovery image will disallow CIPHER_DIP SCM call by itself. */ |
Amit Blay | 8a51030 | 2015-08-17 09:20:01 +0300 | [diff] [blame] | 779 | if (ext_partition->partition == MDTP_PARTITION_BOOT) |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 780 | { |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 781 | mdtp_tzbsp_disallow_cipher_DIP(); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 782 | } |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 783 | } |
Shay Nachmani | bc10dfe | 2015-02-10 14:45:55 +0200 | [diff] [blame] | 784 | /********************************************************************************/ |
| 785 | |
| 786 | /** Indicates whether the MDTP is currently in ACTIVATED state **/ |
| 787 | int mdtp_activated(bool * activated){ |
| 788 | if(is_mdtp_activated < 0){ |
| 789 | /* mdtp_fwlock_verify_lock was not called before, the value is not valid */ |
| 790 | return is_mdtp_activated; |
| 791 | } |
| 792 | |
| 793 | *activated = is_mdtp_activated; |
| 794 | return 0; |
| 795 | } |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 796 | |
| 797 | /********************************************************************************/ |
| 798 | |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 799 | /* Decrypt a given DIP and verify its integrity */ |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 800 | static int mdtp_tzbsp_dec_verify_DIP(DIP_t *enc_dip, DIP_t *dec_dip, uint32_t *verified) |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 801 | { |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 802 | unsigned char hash[HASH_LEN]; |
| 803 | SHA256_CTX sha256_ctx; |
| 804 | int ret; |
| 805 | |
| 806 | ASSERT(enc_dip != NULL); |
| 807 | ASSERT(dec_dip != NULL); |
| 808 | ASSERT(verified != NULL); |
| 809 | |
Amit Blay | 4418fb4 | 2015-05-05 08:45:13 +0300 | [diff] [blame] | 810 | arch_clean_invalidate_cache_range((addr_t)enc_dip, sizeof(DIP_t)); |
| 811 | arch_invalidate_cache_range((addr_t)dec_dip, sizeof(DIP_t)); |
| 812 | |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 813 | ret = mdtp_cipher_dip_cmd((uint8_t*)enc_dip, sizeof(DIP_t), |
Reut Zysman | 3f3eccd | 2016-04-20 22:05:36 +0300 | [diff] [blame] | 814 | (uint8_t*)dec_dip, sizeof(DIP_t), |
| 815 | DIP_DECRYPT); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 816 | if (ret) |
| 817 | { |
| 818 | dprintf(CRITICAL, "mdtp: mdtp_tzbsp_dec_verify_DIP: ERROR, cannot cipher DIP\n"); |
| 819 | *verified = 0; |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 820 | memset(dec_dip, 0, sizeof(DIP_t)); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 821 | return -1; |
| 822 | } |
| 823 | |
Amit Blay | 4418fb4 | 2015-05-05 08:45:13 +0300 | [diff] [blame] | 824 | arch_invalidate_cache_range((addr_t)dec_dip, sizeof(DIP_t)); |
| 825 | |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 826 | SHA256_Init(&sha256_ctx); |
| 827 | SHA256_Update(&sha256_ctx, dec_dip, sizeof(DIP_t) - HASH_LEN); |
| 828 | SHA256_Final(hash, &sha256_ctx); |
| 829 | |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 830 | if (memcmp(hash, dec_dip->hash, HASH_LEN)) |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 831 | { |
| 832 | *verified = 0; |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 833 | memset(dec_dip, 0, sizeof(DIP_t)); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 834 | } |
| 835 | else |
| 836 | { |
| 837 | *verified = 1; |
| 838 | } |
| 839 | |
| 840 | return 0; |
| 841 | } |
| 842 | |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 843 | /* Encrypt a given DIP and calculate its integrity information */ |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 844 | static int mdtp_tzbsp_enc_hash_DIP(DIP_t *dec_dip, DIP_t *enc_dip) |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 845 | { |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 846 | SHA256_CTX sha256_ctx; |
| 847 | int ret; |
| 848 | |
| 849 | ASSERT(dec_dip != NULL); |
| 850 | ASSERT(enc_dip != NULL); |
| 851 | |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 852 | SHA256_Init(&sha256_ctx); |
| 853 | SHA256_Update(&sha256_ctx, dec_dip, sizeof(DIP_t) - HASH_LEN); |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 854 | SHA256_Final(dec_dip->hash, &sha256_ctx); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 855 | |
Amit Blay | 4418fb4 | 2015-05-05 08:45:13 +0300 | [diff] [blame] | 856 | arch_clean_invalidate_cache_range((addr_t)dec_dip, sizeof(DIP_t)); |
| 857 | arch_invalidate_cache_range((addr_t)enc_dip, sizeof(DIP_t)); |
| 858 | |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 859 | ret = mdtp_cipher_dip_cmd((uint8_t*)dec_dip, sizeof(DIP_t), |
Reut Zysman | 3f3eccd | 2016-04-20 22:05:36 +0300 | [diff] [blame] | 860 | (uint8_t*)enc_dip, sizeof(DIP_t), |
| 861 | DIP_ENCRYPT); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 862 | if (ret) |
| 863 | { |
| 864 | dprintf(CRITICAL, "mdtp: mdtp_tzbsp_enc_hash_DIP: ERROR, cannot cipher DIP\n"); |
| 865 | return -1; |
| 866 | } |
| 867 | |
Amit Blay | 4418fb4 | 2015-05-05 08:45:13 +0300 | [diff] [blame] | 868 | arch_invalidate_cache_range((addr_t)enc_dip, sizeof(DIP_t)); |
| 869 | |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 870 | return 0; |
| 871 | } |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 872 | |
| 873 | /* Disallow the CIPHER_DIP SCM call */ |
| 874 | static void mdtp_tzbsp_disallow_cipher_DIP(void) |
| 875 | { |
| 876 | DIP_t *dip; |
| 877 | int i; |
| 878 | |
| 879 | dip = malloc(sizeof(DIP_t)); |
| 880 | if (dip == NULL) |
| 881 | { |
| 882 | dprintf(CRITICAL, "mdtp: mdtp_tzbsp_disallow_cipher_DIP: ERROR, cannot allocate DIP\n"); |
Amir Kotzer | 7c768c0 | 2016-04-13 09:08:05 +0300 | [diff] [blame] | 883 | /* Could not allocate DIP - stop device from booting */ |
| 884 | display_error_msg(); /* This will never return */ |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | /* Disallow the CIPHER_DIP SCM by calling it MAX_CIPHER_DIP_SCM_CALLS times */ |
| 888 | for (i=0; i<MAX_CIPHER_DIP_SCM_CALLS; i++) |
| 889 | { |
Reut Zysman | 3f3eccd | 2016-04-20 22:05:36 +0300 | [diff] [blame] | 890 | mdtp_tzbsp_enc_hash_DIP(dip, dip); |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | free(dip); |
| 894 | } |
Amir Kotzer | bb8be14 | 2016-02-23 09:38:25 +0200 | [diff] [blame] | 895 | |
| 896 | /********************************************************************************/ |
| 897 | |
| 898 | /** UT functions **/ |
| 899 | |
| 900 | /** Hashing fuctions UT **/ |
| 901 | int mdtp_verify_hash_ut(){ |
| 902 | unsigned char digest[HASH_LEN]={0}; |
| 903 | unsigned int hash_expected_result = 0xD42B0A29; |
| 904 | char *buf = "MTDP LK UT hashing functions sanity check"; |
| 905 | int size = 0; |
| 906 | DIP_hash_table_entry_t partition_hash_table; |
| 907 | uint8_t partition_force_verify_block = 0; |
| 908 | |
| 909 | char ptr = buf[0]; |
| 910 | while(ptr){ |
| 911 | ptr = buf[++size]; |
| 912 | } |
| 913 | //Bad partition name - single mode |
| 914 | if(verify_partition_single_hash("BAD_PARTITION", 1, &partition_hash_table) != -1){ |
| 915 | dprintf(INFO, "verify_hash_ut: [FAIL (1)].\n"); |
| 916 | return -1; |
| 917 | } |
| 918 | |
| 919 | //Bad partition name - block mode |
| 920 | if(verify_partition_block_hash("BAD_PARTITION", 1, 1, &partition_hash_table, &partition_force_verify_block) != -1){ |
| 921 | dprintf(INFO, "verify_hash_ut: [FAIL (2)].\n"); |
| 922 | return -1; |
| 923 | } |
| 924 | |
| 925 | //Hashing sanity check |
| 926 | hash_find((unsigned char*)buf, size, digest, CRYPTO_AUTH_ALG_SHA256); |
| 927 | unsigned int *hash_res = (unsigned int *)digest; |
| 928 | if (*hash_res != hash_expected_result){ |
| 929 | dprintf(INFO, "verify_hash_ut: [FAIL (3)].\n"); |
| 930 | return -1; |
| 931 | } |
| 932 | dprintf(INFO, "verify_hash_ut: [PASS].\n"); |
| 933 | return 0; |
| 934 | } |
| 935 | |
| 936 | /** Validate partitions params UT **/ |
| 937 | int mdtp_validate_partition_params_ut(){ |
| 938 | int partition_size = 10; |
| 939 | //Bad size |
| 940 | if(validate_partition_params(BAD_PARAM_SIZE, MDTP_FWLOCK_MODE_SINGLE, 1) != -1){ |
| 941 | dprintf(INFO, "validate_partition_params_ut: [FAIL (1)].\n"); |
| 942 | return -1; |
| 943 | } |
| 944 | |
| 945 | //Bad size |
| 946 | if(validate_partition_params((uint64_t)MDTP_FWLOCK_BLOCK_SIZE * (uint64_t)MAX_BLOCKS + 1, |
Reut Zysman | 3f3eccd | 2016-04-20 22:05:36 +0300 | [diff] [blame] | 947 | MDTP_FWLOCK_MODE_SINGLE, 1) != -1){ |
Amir Kotzer | bb8be14 | 2016-02-23 09:38:25 +0200 | [diff] [blame] | 948 | dprintf(INFO, "validate_partition_params_ut: [FAIL (2)].\n"); |
| 949 | return -1; |
| 950 | } |
| 951 | |
| 952 | //Bad verification ratio |
| 953 | if(validate_partition_params(partition_size, MDTP_FWLOCK_MODE_SIZE, BAD_PARAM_VERIF_RATIO) != -1){ |
| 954 | dprintf(INFO, "validate_partition_params_ut: [FAIL (3)].\n"); |
| 955 | return -1; |
| 956 | } |
| 957 | dprintf(INFO, "MDTP LK UT: validate_partition_params_ut [ PASS ]\n"); |
| 958 | return 0; |
| 959 | } |
| 960 | |
| 961 | /** Verify partition UT **/ |
| 962 | int mdtp_verify_partition_ut(){ |
| 963 | uint8_t partition_force_verify_block = 0; |
| 964 | DIP_hash_table_entry_t partition_hash_table; |
| 965 | int verify_num_blocks = 10,partition_size = 1; |
| 966 | |
| 967 | //Unkown hashing mode |
| 968 | if(verify_partition("system", partition_size, BAD_HASH_MODE, verify_num_blocks, |
Reut Zysman | 3f3eccd | 2016-04-20 22:05:36 +0300 | [diff] [blame] | 969 | &partition_hash_table, &partition_force_verify_block) != -1){ |
Amir Kotzer | bb8be14 | 2016-02-23 09:38:25 +0200 | [diff] [blame] | 970 | dprintf(INFO, "verify_partition_ut: Failed Test 1.\n"); |
| 971 | dprintf(INFO, "MDTP LK UT: verify_partition_ut [ FAIL ]\n"); |
| 972 | return -1; |
| 973 | } |
| 974 | dprintf(INFO, "MDTP LK UT: verify_partition_ut [ PASS ]\n"); |
| 975 | return 0; |
| 976 | } |
| 977 | |