Sundarajan Srinivasan | 7dab3e6 | 2014-04-21 14:03:09 -0700 | [diff] [blame] | 1 | /* |
Channagoud Kadabi | f8b5980 | 2015-09-14 13:54:28 -0700 | [diff] [blame] | 2 | * Copyright (c) 2014-2015, The Linux Foundation. All rights reserved. |
Sundarajan Srinivasan | 7dab3e6 | 2014-04-21 14:03:09 -0700 | [diff] [blame] | 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * * Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * * Redistributions in binary form must reproduce the above |
| 10 | * copyright notice, this list of conditions and the following |
| 11 | * disclaimer in the documentation and/or other materials provided |
| 12 | * with the distribution. |
| 13 | * * Neither the name of The Linux Foundation, Inc. nor the names of its |
| 14 | * contributors may be used to endorse or promote products derived |
| 15 | * from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 18 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 21 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 24 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 25 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 26 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 27 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | */ |
| 29 | |
| 30 | #include <app/kauth_test.h> |
| 31 | #include <crypto_hash.h> |
| 32 | #include <partition_parser.h> |
| 33 | #include <arch/defines.h> |
| 34 | #include <debug.h> |
| 35 | #include <stdlib.h> |
Channagoud Kadabi | f8b5980 | 2015-09-14 13:54:28 -0700 | [diff] [blame] | 36 | #include <string.h> |
| 37 | #if VERIFIED_BOOT |
| 38 | #include <boot_verifier.h> |
| 39 | #endif |
Sundarajan Srinivasan | 7dab3e6 | 2014-04-21 14:03:09 -0700 | [diff] [blame] | 40 | /* The test assumes the image and its signature to be verified |
| 41 | * are flashed in the system and userdata partitions respectively. |
| 42 | * The test reads the images and validates the decrypted signature |
| 43 | * with the hash of the image. Customers can modify the partition names |
| 44 | * and image size to custom values as per their requirements. |
| 45 | */ |
| 46 | |
| 47 | void kauth_test(const char *arg, void *data, unsigned sz) |
| 48 | { |
Channagoud Kadabi | f8b5980 | 2015-09-14 13:54:28 -0700 | [diff] [blame] | 49 | int ret = 0; |
| 50 | #if VERIFIED_BOOT |
| 51 | int i; |
| 52 | int vboot_ret[12]; |
| 53 | int vboot_expected[12] = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 54 | const char *image = "Test Input File"; |
| 55 | unsigned int image_len = strlen(image); |
| 56 | KEYSTORE *ks = boot_gerity_get_oem_keystore(); |
| 57 | bool test_pass = true; |
| 58 | #else |
Sundarajan Srinivasan | 7dab3e6 | 2014-04-21 14:03:09 -0700 | [diff] [blame] | 59 | unsigned long long ptn = 0; |
| 60 | int index = INVALID_PTN; |
Sundarajan Srinivasan | 7dab3e6 | 2014-04-21 14:03:09 -0700 | [diff] [blame] | 61 | unsigned char *image = NULL; |
Channagoud Kadabi | f8b5980 | 2015-09-14 13:54:28 -0700 | [diff] [blame] | 62 | unsigned char *signature = NULL; |
| 63 | uint32_t page_size = mmc_page_size(); |
| 64 | #endif |
| 65 | #if !VERIFIED_BOOT |
Sundarajan Srinivasan | 7dab3e6 | 2014-04-21 14:03:09 -0700 | [diff] [blame] | 66 | #if IMAGE_VERIF_ALGO_SHA1 |
| 67 | uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1; |
| 68 | #else |
| 69 | uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256; |
| 70 | #endif |
Channagoud Kadabi | f8b5980 | 2015-09-14 13:54:28 -0700 | [diff] [blame] | 71 | #endif |
Sundarajan Srinivasan | 7dab3e6 | 2014-04-21 14:03:09 -0700 | [diff] [blame] | 72 | |
Channagoud Kadabi | f8b5980 | 2015-09-14 13:54:28 -0700 | [diff] [blame] | 73 | #if !VERIFIED_BOOT |
Sundarajan Srinivasan | 7dab3e6 | 2014-04-21 14:03:09 -0700 | [diff] [blame] | 74 | index = partition_get_index("system"); |
| 75 | ptn = partition_get_offset(index); |
Mayank Grover | 4886040 | 2016-11-29 12:34:53 +0530 | [diff] [blame] | 76 | mmc_set_lun(partition_get_lun(index)); |
Sundarajan Srinivasan | 7dab3e6 | 2014-04-21 14:03:09 -0700 | [diff] [blame] | 77 | |
| 78 | if(!ptn) |
| 79 | { |
| 80 | dprintf(CRITICAL, "partition system not found\n"); |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | image = (unsigned char*) memalign(CACHE_LINE, ROUNDUP(IMAGE_SIZE, CACHE_LINE)); |
| 85 | |
| 86 | ASSERT(image); |
| 87 | |
| 88 | if(mmc_read(ptn, (unsigned int*) image, IMAGE_SIZE)) |
| 89 | { |
| 90 | dprintf(CRITICAL,"mmc_read system failed\n"); |
| 91 | goto err; |
| 92 | } |
Sundarajan Srinivasan | 7dab3e6 | 2014-04-21 14:03:09 -0700 | [diff] [blame] | 93 | signature = (unsigned char*) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE)); |
| 94 | |
| 95 | ASSERT(signature); |
| 96 | |
| 97 | index = partition_get_index("userdata"); |
| 98 | ptn = partition_get_offset(index); |
| 99 | |
| 100 | if(!ptn) |
| 101 | { |
| 102 | dprintf(CRITICAL,"partition userdata not found\n"); |
| 103 | goto err; |
| 104 | } |
| 105 | |
| 106 | if(mmc_read(ptn, (unsigned int*) signature, page_size)) |
| 107 | { |
| 108 | dprintf(CRITICAL,"mmc_read signature failed\n"); |
| 109 | goto err; |
| 110 | } |
Channagoud Kadabi | f8b5980 | 2015-09-14 13:54:28 -0700 | [diff] [blame] | 111 | #endif |
Sundarajan Srinivasan | 7dab3e6 | 2014-04-21 14:03:09 -0700 | [diff] [blame] | 112 | |
| 113 | dprintf(INFO, "kauth_test: Authenticating boot image (%d): start\n", IMAGE_SIZE); |
| 114 | |
Channagoud Kadabi | f8b5980 | 2015-09-14 13:54:28 -0700 | [diff] [blame] | 115 | #if VERIFIED_BOOT |
| 116 | for (i = 0; i < 12; i++) |
| 117 | { |
| 118 | ret = boot_verify_compare_sha256((unsigned char *)image, image_len, vboot_signatures[i], ks->mykeybag->mykey->key_material); |
| 119 | vboot_ret[i] = ret; |
| 120 | } |
| 121 | for (i = 0; i < 12; i++) |
| 122 | { |
| 123 | if (vboot_ret[i] != vboot_expected[i]) |
| 124 | test_pass = false; |
| 125 | ret = i; |
| 126 | } |
| 127 | |
| 128 | if (test_pass) |
| 129 | dprintf(INFO, "Kauth_test: PASS for all usecases\n"); |
| 130 | else |
| 131 | dprintf(INFO, "Kauth_test failed for : %s\n", vboot_signatures_str[ret]); |
| 132 | #else |
Sundarajan Srinivasan | 7dab3e6 | 2014-04-21 14:03:09 -0700 | [diff] [blame] | 133 | ret = image_verify(image, signature, IMAGE_SIZE, auth_algo); |
Channagoud Kadabi | f8b5980 | 2015-09-14 13:54:28 -0700 | [diff] [blame] | 134 | dprintf(INFO, "kauth_test: Authenticating boot image: done return value = %d\n", ret); |
| 135 | #endif |
Sundarajan Srinivasan | 7dab3e6 | 2014-04-21 14:03:09 -0700 | [diff] [blame] | 136 | |
Channagoud Kadabi | f8b5980 | 2015-09-14 13:54:28 -0700 | [diff] [blame] | 137 | #if !VERIFIED_BOOT |
Sundarajan Srinivasan | 7dab3e6 | 2014-04-21 14:03:09 -0700 | [diff] [blame] | 138 | err: |
| 139 | if(image) |
| 140 | free(image); |
| 141 | if(signature) |
| 142 | free(signature); |
Channagoud Kadabi | f8b5980 | 2015-09-14 13:54:28 -0700 | [diff] [blame] | 143 | #endif |
Sundarajan Srinivasan | 7dab3e6 | 2014-04-21 14:03:09 -0700 | [diff] [blame] | 144 | } |