MCCI Trusted Bootloader
Simple trusted bootloader and tools for small embedded systems
mccibootloader_checkcodevalid.c
Go to the documentation of this file.
1/* mccibootloader_checkcodevalid.c Tue Jul 14 2020 11:31:05 tmm */
2
3/*
4
5Module: mccibootloader_checkcodevalid.c
6
7Function:
8 McciBootloader_checkCodeValid().
9
10Version:
11 V0.1.0 Tue Jul 14 2020 11:31:05 tmm Edit level 1
12
13Copyright notice:
14 This file copyright (C) 2020 by
15
16 MCCI Corporation
17 3520 Krums Corners Road
18 Ithaca, NY 14850
19
20 An unpublished work. All rights reserved.
21
22 This file is proprietary information, and may not be disclosed or
23 copied without the prior permission of MCCI Corporation.
24
25Author:
26 Terry Moore, MCCI Corporation July 2020
27
28Revision history:
29 0.1.0 Tue Jul 14 2020 11:31:05 tmm
30 Module created.
31
32*/
33
34#include "mcci_bootloader.h"
35
38#include "mcci_tweetnacl_hash.h"
39#include "mcci_tweetnacl_sign.h"
40
41/****************************************************************************\
42|
43| Manifest constants & typedefs.
44|
45\****************************************************************************/
46
47
48/****************************************************************************\
49|
50| Read-only data.
51|
52\****************************************************************************/
53
54/****************************************************************************\
55|
56| Variables.
57|
58\****************************************************************************/
59
60
61/*
62
63Name: McciBootloader_checkCodeValid()
64
65Function:
66 Check whether a region of code is valid.
67
68Definition:
69 bool McciBootloader_checkCodeValid(
70 const void *pBase,
71 size_t nBytes
72 );
73
74Description:
75 This function validates a NOR-flash app image to
76 confirm that it looks like a valid image for this
77 device.
78
79 We check the following conditions.
80
81 * The pointer must be properly aligned so that we
82 can set the vector table to the address (multiple
83 of 256).
84 * The first dword in the image (the stack pointer)
85 must be DWORD aligned and must point into system RAM.
86 * There must be a valid header at the beginning of the
87 region -- the first 64+128 bytes are the vector image
88 image; the next 4 bytes specify the size of the image,
89 in bytes. This size must be less then or equal to
90 `nBytes`.
91 * The signature of the region must be valid. (This
92 is initially a CRC32, but may change to ED25519,
93 if that doesn't bloat the bootloader.
94 * The reset vector must be odd (Thumb mode) and
95 must point into the image.
96
97 We assume the image is completely visible, but
98 in order to share code with the SPI flash validation
99 we locate regions and then call subroutines.
100
101Returns:
102 This function returns true if the image is valid,
103 false otherwise.
104
105*/
106
107bool
109 const void *pBase,
110 size_t nBytes
111 )
112 {
113 // compute the hash over pBase
114 mcci_tweetnacl_sha512_t hash;
115 mcci_tweetnacl_result_t invalid;
116 const McciBootloader_AppInfo_t *pAppInfo;
117
118 // check whether the image is valid.
119 pAppInfo = McciBootloaderPlatform_checkImageValid(pBase, nBytes, (uintptr_t)pBase, nBytes);
120 if (pAppInfo == NULL)
121 return false;
122
123 // compute the hash
124 mcci_tweetnacl_hash_sha512(
125 &hash,
126 pBase,
127 pAppInfo->imagesize + sizeof(mcci_tweetnacl_sign_publickey_t)
128 );
129
130 // find the signature block
131 const McciBootloader_SignatureBlock_t * const pSigBlock =
132 (const void *)((const uint8_t *)pBase + pAppInfo->imagesize);
133
134 invalid = mcci_tweetnacl_verify_64(
135 hash.bytes,
136 pSigBlock->hash.bytes
137 );
138
139 return mcci_tweetnacl_result_is_success(invalid);
140 }
bool McciBootloader_checkCodeValid(const void *pBase, size_t nBytes)
const McciBootloader_AppInfo_t * McciBootloaderPlatform_checkImageValid(const void *pHeader, size_t nHeader, uintptr_t targetAddress, size_t targetSize)