blob: ca0e3419c224a2073f44f90100fb1491758e4b54 [file] [log] [blame]
Alice Wang24954b42023-02-06 10:03:45 +00001// Copyright 2023, The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! This module mirrors the content in open-dice/include/dice/ops.h
16//! It contains the set of functions that implement various operations that the
17//! main DICE functions depend on.
18
19use crate::dice::{Hash, HASH_SIZE};
20use crate::error::{check_result, Result};
21use open_dice_cbor_bindgen::DiceHash;
22use std::ptr;
23
24/// Hashes the provided input using DICE's hash function `DiceHash`.
25pub fn hash(input: &[u8]) -> Result<Hash> {
26 let mut output: Hash = [0; HASH_SIZE];
27 // SAFETY: DiceHash takes a sized input buffer and writes to a constant-sized output buffer.
28 // The first argument context is not used in this function.
29 check_result(unsafe {
30 DiceHash(
31 ptr::null_mut(), // context
32 input.as_ptr(),
33 input.len(),
34 output.as_mut_ptr(),
35 )
36 })?;
37 Ok(output)
38}