blob: e92922e742c67ef77fd7a2bea58ec938ee11a4d4 [file] [log] [blame]
Sundarajan Srinivasan7dab3e62014-04-21 14:03:09 -07001/*
Channagoud Kadabif8b59802015-09-14 13:54:28 -07002 * Copyright (c) 2014-2015, The Linux Foundation. All rights reserved.
Sundarajan Srinivasan7dab3e62014-04-21 14:03:09 -07003 *
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 Kadabif8b59802015-09-14 13:54:28 -070036#include <string.h>
37#if VERIFIED_BOOT
38#include <boot_verifier.h>
39#endif
Sundarajan Srinivasan7dab3e62014-04-21 14:03:09 -070040/* 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
47void kauth_test(const char *arg, void *data, unsigned sz)
48{
Channagoud Kadabif8b59802015-09-14 13:54:28 -070049 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 Srinivasan7dab3e62014-04-21 14:03:09 -070059 unsigned long long ptn = 0;
60 int index = INVALID_PTN;
Sundarajan Srinivasan7dab3e62014-04-21 14:03:09 -070061 unsigned char *image = NULL;
Channagoud Kadabif8b59802015-09-14 13:54:28 -070062 unsigned char *signature = NULL;
63 uint32_t page_size = mmc_page_size();
64#endif
65#if !VERIFIED_BOOT
Sundarajan Srinivasan7dab3e62014-04-21 14:03:09 -070066#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 Kadabif8b59802015-09-14 13:54:28 -070071#endif
Sundarajan Srinivasan7dab3e62014-04-21 14:03:09 -070072
Channagoud Kadabif8b59802015-09-14 13:54:28 -070073#if !VERIFIED_BOOT
Sundarajan Srinivasan7dab3e62014-04-21 14:03:09 -070074 index = partition_get_index("system");
75 ptn = partition_get_offset(index);
76
77 if(!ptn)
78 {
79 dprintf(CRITICAL, "partition system not found\n");
80 return;
81 }
82
83 image = (unsigned char*) memalign(CACHE_LINE, ROUNDUP(IMAGE_SIZE, CACHE_LINE));
84
85 ASSERT(image);
86
87 if(mmc_read(ptn, (unsigned int*) image, IMAGE_SIZE))
88 {
89 dprintf(CRITICAL,"mmc_read system failed\n");
90 goto err;
91 }
Sundarajan Srinivasan7dab3e62014-04-21 14:03:09 -070092 signature = (unsigned char*) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
93
94 ASSERT(signature);
95
96 index = partition_get_index("userdata");
97 ptn = partition_get_offset(index);
98
99 if(!ptn)
100 {
101 dprintf(CRITICAL,"partition userdata not found\n");
102 goto err;
103 }
104
105 if(mmc_read(ptn, (unsigned int*) signature, page_size))
106 {
107 dprintf(CRITICAL,"mmc_read signature failed\n");
108 goto err;
109 }
Channagoud Kadabif8b59802015-09-14 13:54:28 -0700110#endif
Sundarajan Srinivasan7dab3e62014-04-21 14:03:09 -0700111
112 dprintf(INFO, "kauth_test: Authenticating boot image (%d): start\n", IMAGE_SIZE);
113
Channagoud Kadabif8b59802015-09-14 13:54:28 -0700114#if VERIFIED_BOOT
115 for (i = 0; i < 12; i++)
116 {
117 ret = boot_verify_compare_sha256((unsigned char *)image, image_len, vboot_signatures[i], ks->mykeybag->mykey->key_material);
118 vboot_ret[i] = ret;
119 }
120 for (i = 0; i < 12; i++)
121 {
122 if (vboot_ret[i] != vboot_expected[i])
123 test_pass = false;
124 ret = i;
125 }
126
127 if (test_pass)
128 dprintf(INFO, "Kauth_test: PASS for all usecases\n");
129 else
130 dprintf(INFO, "Kauth_test failed for : %s\n", vboot_signatures_str[ret]);
131#else
Sundarajan Srinivasan7dab3e62014-04-21 14:03:09 -0700132 ret = image_verify(image, signature, IMAGE_SIZE, auth_algo);
Channagoud Kadabif8b59802015-09-14 13:54:28 -0700133 dprintf(INFO, "kauth_test: Authenticating boot image: done return value = %d\n", ret);
134#endif
Sundarajan Srinivasan7dab3e62014-04-21 14:03:09 -0700135
Channagoud Kadabif8b59802015-09-14 13:54:28 -0700136#if !VERIFIED_BOOT
Sundarajan Srinivasan7dab3e62014-04-21 14:03:09 -0700137err:
138 if(image)
139 free(image);
140 if(signature)
141 free(signature);
Channagoud Kadabif8b59802015-09-14 13:54:28 -0700142#endif
Sundarajan Srinivasan7dab3e62014-04-21 14:03:09 -0700143}