blob: 862f3e9b66efdbad28717a8e065b633c66095057 [file] [log] [blame]
David Brazdil9a83e612022-09-27 17:38:10 +00001/*
2 * Copyright 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//! Bare metal wrapper around libopen_dice.
18
19#![no_std]
20
Pierre-Clément Tosic3b61732022-12-06 16:30:51 +000021use core::fmt;
Pierre-Clément Tosidb14ada2022-12-06 15:27:16 +000022use core::result;
Pierre-Clément Tosic3b61732022-12-06 16:30:51 +000023
24use open_dice_cbor_bindgen::DiceHash;
25use open_dice_cbor_bindgen::DiceResult;
26use open_dice_cbor_bindgen::DiceResult_kDiceResultBufferTooSmall as DICE_RESULT_BUFFER_TOO_SMALL;
27use open_dice_cbor_bindgen::DiceResult_kDiceResultInvalidInput as DICE_RESULT_INVALID_INPUT;
28use open_dice_cbor_bindgen::DiceResult_kDiceResultOk as DICE_RESULT_OK;
29use open_dice_cbor_bindgen::DiceResult_kDiceResultPlatformError as DICE_RESULT_PLATFORM_ERROR;
David Brazdil9a83e612022-09-27 17:38:10 +000030
31const HASH_SIZE: usize = open_dice_cbor_bindgen::DICE_HASH_SIZE as usize;
32
33/// Array type of hashes used by DICE.
34pub type Hash = [u8; HASH_SIZE];
35
36/// Error type used by DICE.
37pub enum Error {
38 /// Provided input was invalid.
39 InvalidInput,
40 /// Provided buffer was too small.
41 BufferTooSmall,
42 /// Unexpected platform error.
43 PlatformError,
44 /// Unexpected return value.
45 Unknown(DiceResult),
46}
47
Pierre-Clément Tosic3b61732022-12-06 16:30:51 +000048impl fmt::Debug for Error {
David Brazdil9a83e612022-09-27 17:38:10 +000049 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
50 match self {
51 Error::InvalidInput => write!(f, "invalid input"),
52 Error::BufferTooSmall => write!(f, "buffer too small"),
53 Error::PlatformError => write!(f, "platform error"),
54 Error::Unknown(n) => write!(f, "unknown error: {}", n),
55 }
56 }
57}
58
Pierre-Clément Tosidb14ada2022-12-06 15:27:16 +000059/// Result of DICE functions.
60pub type Result<T> = result::Result<T, Error>;
61
62fn check_call(ret: DiceResult) -> Result<()> {
David Brazdil9a83e612022-09-27 17:38:10 +000063 match ret {
64 DICE_RESULT_OK => Ok(()),
65 DICE_RESULT_INVALID_INPUT => Err(Error::InvalidInput),
66 DICE_RESULT_BUFFER_TOO_SMALL => Err(Error::BufferTooSmall),
67 DICE_RESULT_PLATFORM_ERROR => Err(Error::PlatformError),
68 n => Err(Error::Unknown(n)),
69 }
70}
71
72fn ctx() -> *mut core::ffi::c_void {
73 core::ptr::null_mut()
74}
75
76/// Hash the provided input using DICE's default hash function.
Pierre-Clément Tosidb14ada2022-12-06 15:27:16 +000077pub fn hash(bytes: &[u8]) -> Result<Hash> {
David Brazdil9a83e612022-09-27 17:38:10 +000078 let mut output: Hash = [0; HASH_SIZE];
79 // SAFETY - DiceHash takes a sized input buffer and writes to a constant-sized output buffer.
80 check_call(unsafe { DiceHash(ctx(), bytes.as_ptr(), bytes.len(), output.as_mut_ptr()) })?;
81 Ok(output)
82}