1*60b67249SAndroid Build Coastguard Worker# Open Profile for DICE 2*60b67249SAndroid Build Coastguard Worker 3*60b67249SAndroid Build Coastguard WorkerThis repository contains the specification for the 4*60b67249SAndroid Build Coastguard Worker[Open Profile for DICE](docs/specification.md) along with production-quality 5*60b67249SAndroid Build Coastguard Workercode. This profile is a specialization of the 6*60b67249SAndroid Build Coastguard Worker[Hardware Requirements for a Device Identifier Composition Engine](https://trustedcomputinggroup.org/resource/hardware-requirements-for-a-device-identifier-composition-engine/) 7*60b67249SAndroid Build Coastguard Workerand 8*60b67249SAndroid Build Coastguard Worker[DICE Layering Architecture](https://trustedcomputinggroup.org/resource/dice-layering-architecture/) 9*60b67249SAndroid Build Coastguard Workerspecifications published by the Trusted Computing Group (TCG). For readers 10*60b67249SAndroid Build Coastguard Workeralready familiar with those specs, notable distinctives of this profile include: 11*60b67249SAndroid Build Coastguard Worker 12*60b67249SAndroid Build Coastguard Worker* Separate CDIs for attestation and sealing use cases 13*60b67249SAndroid Build Coastguard Worker* Categorized inputs, including values related to verified boot 14*60b67249SAndroid Build Coastguard Worker* Certified UDS values 15*60b67249SAndroid Build Coastguard Worker* X.509 or CBOR certificates 16*60b67249SAndroid Build Coastguard Worker 17*60b67249SAndroid Build Coastguard Worker## Mailing List 18*60b67249SAndroid Build Coastguard Worker 19*60b67249SAndroid Build Coastguard WorkerYou can find us (and join us!) at 20*60b67249SAndroid Build Coastguard Workerhttps://groups.google.com/g/open-profile-for-dice. We're happy to answer 21*60b67249SAndroid Build Coastguard Workerquestions and discuss proposed changes or features. 22*60b67249SAndroid Build Coastguard Worker 23*60b67249SAndroid Build Coastguard Worker## Specification 24*60b67249SAndroid Build Coastguard Worker 25*60b67249SAndroid Build Coastguard WorkerThe specification can be found [here](docs/specification.md). It is versioned 26*60b67249SAndroid Build Coastguard Workerusing a major.minor scheme. Compatibility is maintained across minor versions 27*60b67249SAndroid Build Coastguard Workerbut not necessarily across major versions. 28*60b67249SAndroid Build Coastguard Worker 29*60b67249SAndroid Build Coastguard Worker## Code 30*60b67249SAndroid Build Coastguard Worker 31*60b67249SAndroid Build Coastguard WorkerProduction quality, portable C code is included. The main code is in 32*60b67249SAndroid Build Coastguard Worker[dice.h](include/dice/dice.h) and [dice.c](src/dice.c). Cryptographic and 33*60b67249SAndroid Build Coastguard Workercertificate generation operations are injected via a set of callbacks. Multiple 34*60b67249SAndroid Build Coastguard Workerimplementations of these operations are provided, all equally acceptable. 35*60b67249SAndroid Build Coastguard WorkerIntegrators should choose just one of these, or write their own. 36*60b67249SAndroid Build Coastguard Worker 37*60b67249SAndroid Build Coastguard WorkerTests are included for all code and the build files in this repository can be 38*60b67249SAndroid Build Coastguard Workerused to build and run these tests. 39*60b67249SAndroid Build Coastguard Worker 40*60b67249SAndroid Build Coastguard WorkerDisclaimer: This is not an officially supported Google product. 41*60b67249SAndroid Build Coastguard Worker 42*60b67249SAndroid Build Coastguard Worker### Thirdparty Dependencies 43*60b67249SAndroid Build Coastguard Worker 44*60b67249SAndroid Build Coastguard WorkerDifferent implementations use different third party libraries. The third\_party 45*60b67249SAndroid Build Coastguard Workerdirectory contains build files and git submodules for each of these. The 46*60b67249SAndroid Build Coastguard Workersubmodules must be initialized once after cloning the repo, using `git submodule 47*60b67249SAndroid Build Coastguard Workerupdate --init`, and updated after pulling commits that roll the submodules using 48*60b67249SAndroid Build Coastguard Worker`git submodule update`. 49*60b67249SAndroid Build Coastguard Worker 50*60b67249SAndroid Build Coastguard Worker### Building and Running Tests 51*60b67249SAndroid Build Coastguard Worker 52*60b67249SAndroid Build Coastguard Worker#### Quick setup 53*60b67249SAndroid Build Coastguard Worker 54*60b67249SAndroid Build Coastguard WorkerTo setup the build environment the first time: 55*60b67249SAndroid Build Coastguard Worker 56*60b67249SAndroid Build Coastguard Worker```bash 57*60b67249SAndroid Build Coastguard Worker$ git submodule update --init 58*60b67249SAndroid Build Coastguard Worker$ source bootstrap.sh 59*60b67249SAndroid Build Coastguard Worker$ gn gen out 60*60b67249SAndroid Build Coastguard Worker``` 61*60b67249SAndroid Build Coastguard Worker 62*60b67249SAndroid Build Coastguard WorkerTo build and run tests: 63*60b67249SAndroid Build Coastguard Worker 64*60b67249SAndroid Build Coastguard Worker```bash 65*60b67249SAndroid Build Coastguard Worker$ ninja -C out 66*60b67249SAndroid Build Coastguard Worker``` 67*60b67249SAndroid Build Coastguard Worker 68*60b67249SAndroid Build Coastguard Worker#### More details 69*60b67249SAndroid Build Coastguard Worker 70*60b67249SAndroid Build Coastguard WorkerThe easiest way, and currently the only supported way, to build and run tests is 71*60b67249SAndroid Build Coastguard Workerfrom a [Pigweed](https://pigweed.googlesource.com/pigweed/pigweed/) environment 72*60b67249SAndroid Build Coastguard Workeron Linux. Pigweed does support other host platforms so it shouldn't be too hard 73*60b67249SAndroid Build Coastguard Workerto get this running on Windows for example, but we use Linux. 74*60b67249SAndroid Build Coastguard Worker 75*60b67249SAndroid Build Coastguard WorkerThere are two scripts to help set this up: 76*60b67249SAndroid Build Coastguard Worker 77*60b67249SAndroid Build Coastguard Worker* [bootstrap.sh](bootstrap.sh) will initialize submodules, bootstrap a Pigweed 78*60b67249SAndroid Build Coastguard Worker environment, and generate build files. This can take some time and may 79*60b67249SAndroid Build Coastguard Worker download on the order of 1GB of dependencies so the normal workflow is to 80*60b67249SAndroid Build Coastguard Worker just do this once. 81*60b67249SAndroid Build Coastguard Worker 82*60b67249SAndroid Build Coastguard Worker* [activate.sh](activate.sh) quickly reactivates an environment that has been 83*60b67249SAndroid Build Coastguard Worker previously bootstrapped. 84*60b67249SAndroid Build Coastguard Worker 85*60b67249SAndroid Build Coastguard WorkerThese scripts must be sourced into the current session: `source activate.sh`. 86*60b67249SAndroid Build Coastguard Worker 87*60b67249SAndroid Build Coastguard WorkerIn the environment, from the base directory of the dice-profile checkout, run 88*60b67249SAndroid Build Coastguard Worker`ninja -C out` to build everything and run all tests. You can also run `pw 89*60b67249SAndroid Build Coastguard Workerwatch` which will build, run tests, and continue to watch for changes. 90*60b67249SAndroid Build Coastguard Worker 91*60b67249SAndroid Build Coastguard WorkerThis will build and run tests on the host using the clang toolchain. Pigweed 92*60b67249SAndroid Build Coastguard Workermakes it easy to configure other targets and toolchains. See 93*60b67249SAndroid Build Coastguard Worker[toolchains/BUILD.gn](toolchains/BUILD.gn) and the Pigweed documentation. 94*60b67249SAndroid Build Coastguard Worker 95*60b67249SAndroid Build Coastguard Worker### Porting 96*60b67249SAndroid Build Coastguard Worker 97*60b67249SAndroid Build Coastguard WorkerThe code is designed to be portable and should work with a variety of modern 98*60b67249SAndroid Build Coastguard Workertoolchains and in a variety of environments. The main code in dice.h and dice.c 99*60b67249SAndroid Build Coastguard Workeris C99; it uses uint8\_t, size\_t, and memcpy from the C standard library. The 100*60b67249SAndroid Build Coastguard Workervarious ops implementations are as portable as their dependencies (often not C99 101*60b67249SAndroid Build Coastguard Workerbut still very portable). Notably, this code uses designated initializers for 102*60b67249SAndroid Build Coastguard Workerreadability. This is a feature available in C since C99 but missing from C++ 103*60b67249SAndroid Build Coastguard Workeruntil C++20 where it appears in a stricter form. 104*60b67249SAndroid Build Coastguard Worker 105*60b67249SAndroid Build Coastguard Worker### Style 106*60b67249SAndroid Build Coastguard Worker 107*60b67249SAndroid Build Coastguard WorkerThe [Google C++ Style Guide](https://google.github.io/styleguide/cppguide.html) 108*60b67249SAndroid Build Coastguard Workeris used. A `.clang-format` file is provided for convenience. 109*60b67249SAndroid Build Coastguard Worker 110*60b67249SAndroid Build Coastguard Worker### Incorporating 111*60b67249SAndroid Build Coastguard Worker 112*60b67249SAndroid Build Coastguard WorkerTo incorporate the code into another project, there are a few options: 113*60b67249SAndroid Build Coastguard Worker 114*60b67249SAndroid Build Coastguard Worker* Copy only the necessary code. For example: 115*60b67249SAndroid Build Coastguard Worker 116*60b67249SAndroid Build Coastguard Worker 1. Take the main code as is: [include/dice/dice.h](include/dice/dice.h), 117*60b67249SAndroid Build Coastguard Worker [src/dice.c](src/dice.c) 118*60b67249SAndroid Build Coastguard Worker 119*60b67249SAndroid Build Coastguard Worker 1. Choose an implementation for crypto and certificate generation or choose 120*60b67249SAndroid Build Coastguard Worker to write your own. If you choose the boringssl implementation, for 121*60b67249SAndroid Build Coastguard Worker example, take [include/dice/utils.h](include/dice/utils.h), 122*60b67249SAndroid Build Coastguard Worker [include/dice/boringssl_ops.h](include/dice/boringssl_ops.h), 123*60b67249SAndroid Build Coastguard Worker [src/utils.c](src/utils.c), and 124*60b67249SAndroid Build Coastguard Worker [src/boringssl_ops.c](src/boringssl_ops.c). Taking a look at the library 125*60b67249SAndroid Build Coastguard Worker targets in BUILD.gn may be helpful. 126*60b67249SAndroid Build Coastguard Worker 127*60b67249SAndroid Build Coastguard Worker* Add this repository as a git submodule and integrate into the project build, 128*60b67249SAndroid Build Coastguard Worker optionally using the gn library targets provided. 129*60b67249SAndroid Build Coastguard Worker 130*60b67249SAndroid Build Coastguard Worker* Integrate into a project already using Pigweed using the gn build files 131*60b67249SAndroid Build Coastguard Worker provided. 132*60b67249SAndroid Build Coastguard Worker 133*60b67249SAndroid Build Coastguard Worker### Size Reports 134*60b67249SAndroid Build Coastguard Worker 135*60b67249SAndroid Build Coastguard WorkerThe build reports code size using 136*60b67249SAndroid Build Coastguard Worker[Bloaty McBloatface](https://github.com/google/bloaty) via the pw\_bloat Pigweed 137*60b67249SAndroid Build Coastguard Workermodule. There are two reports generated: 138*60b67249SAndroid Build Coastguard Worker 139*60b67249SAndroid Build Coastguard Worker* Library sizes - This report includes just the library code in this 140*60b67249SAndroid Build Coastguard Worker repository. It shows the baseline DICE code with no ops selected, and it 141*60b67249SAndroid Build Coastguard Worker shows the delta introduced by choosing various ops implementations. This 142*60b67249SAndroid Build Coastguard Worker report **does not** include the size of the third party dependencies. 143*60b67249SAndroid Build Coastguard Worker 144*60b67249SAndroid Build Coastguard Worker* Executable sizes - This report includes sizes for the library code in this 145*60b67249SAndroid Build Coastguard Worker repository plus all dependencies linked into a simple main function which 146*60b67249SAndroid Build Coastguard Worker makes a single DICE call with all-zero input. It shows the baseline DICE 147*60b67249SAndroid Build Coastguard Worker code with no ops (and therefore no dependencies other than libc), and it 148*60b67249SAndroid Build Coastguard Worker shows the delta introduced by choosing various ops implementations. This 149*60b67249SAndroid Build Coastguard Worker report **does** include the size of the third party dependencies. Note that 150*60b67249SAndroid Build Coastguard Worker rows specialized from 'Boringssl Ops' use that as a baseline for sizing. 151*60b67249SAndroid Build Coastguard Worker 152*60b67249SAndroid Build Coastguard WorkerThe reports will be in the build output, but you can also find the reports in 153*60b67249SAndroid Build Coastguard Worker`.txt` files in the build output. For example, `cat out/host_optimized/gen/*.txt 154*60b67249SAndroid Build Coastguard Worker| less` will display all reports. 155*60b67249SAndroid Build Coastguard Worker 156*60b67249SAndroid Build Coastguard Worker### Thread Safety 157*60b67249SAndroid Build Coastguard Worker 158*60b67249SAndroid Build Coastguard WorkerThis code does not itself use mutable global variables, or any other type of 159*60b67249SAndroid Build Coastguard Workershared data structure so there is no thread-safety concerns. However, additional 160*60b67249SAndroid Build Coastguard Workercare is needed to ensure dependencies are configured to be thread-safe. For 161*60b67249SAndroid Build Coastguard Workerexample, the current boringssl configuration defines 162*60b67249SAndroid Build Coastguard WorkerOPENSSL\_NO\_THREADS\_CORRUPT\_MEMORY\_AND\_LEAK\_SECRETS\_IF\_THREADED, and 163*60b67249SAndroid Build Coastguard Workerthat would need to be changed before running in a threaded environment. 164*60b67249SAndroid Build Coastguard Worker 165*60b67249SAndroid Build Coastguard Worker### Clearing Sensitive Data 166*60b67249SAndroid Build Coastguard Worker 167*60b67249SAndroid Build Coastguard WorkerThis code makes a reasonable effort to clear memory holding sensitive data. This 168*60b67249SAndroid Build Coastguard Workermay help with a broader strategy to clear sensitive data but it is not 169*60b67249SAndroid Build Coastguard Workersufficient on its own. Here are a few things to consider. 170*60b67249SAndroid Build Coastguard Worker 171*60b67249SAndroid Build Coastguard Worker* The caller of this code is responsible for buffers they own (of course). 172*60b67249SAndroid Build Coastguard Worker* The ops implementations need to clear any copies they make of sensitive 173*60b67249SAndroid Build Coastguard Worker data. Both boringssl and mbedtls attempt to zeroize but this may need 174*60b67249SAndroid Build Coastguard Worker additional care to integrate correctly. For example, boringssl skips 175*60b67249SAndroid Build Coastguard Worker optimization prevention when OPENSSL\_NO\_ASM is defined (and it is 176*60b67249SAndroid Build Coastguard Worker currently defined). 177*60b67249SAndroid Build Coastguard Worker* Sensitive data may remain in cache. 178*60b67249SAndroid Build Coastguard Worker* Sensitive data may have been swapped out. 179*60b67249SAndroid Build Coastguard Worker* Sensitive data may be included in a crash dump. 180