[dice] Move error type to upstream library libdiced_open_dice

This is part of the project of merging the two existing dice
wrapper libraries into one library. The upstream library
libdiced_open_dice will be the merged library.

Test: atest diced_utils_test diced_sample_inputs_test \
diced_test diced_vendor_test
Test: m pvmfw_img microdroid_manager && atest \
microdroid_manager_test
Bug: 267575445

Change-Id: I584dd2f91996d376dff354564b43321ef6535303
diff --git a/diced/open_dice/Android.bp b/diced/open_dice/Android.bp
index be8388f..f4c2155 100644
--- a/diced/open_dice/Android.bp
+++ b/diced/open_dice/Android.bp
@@ -24,11 +24,13 @@
     ],
 }
 
-rust_library_rlib {
+rust_library {
     name: "libdiced_open_dice",
     defaults: ["libdiced_open_dice_defaults"],
     rustlibs: [
         "libopen_dice_cbor_bindgen",
+        // For ZVec
+        "libkeystore2_crypto_rust",
     ],
     features: [
         "std",
diff --git a/diced/open_dice/src/dice.rs b/diced/open_dice/src/dice.rs
index c08cc40..d5e087f 100644
--- a/diced/open_dice/src/dice.rs
+++ b/diced/open_dice/src/dice.rs
@@ -15,11 +15,11 @@
 //! Structs and functions about the types used in DICE.
 //! This module mirrors the content in open-dice/include/dice/dice.h
 
-use core::ptr;
 use open_dice_cbor_bindgen::{
     DiceConfigType, DiceInputValues, DiceMode, DICE_HASH_SIZE, DICE_HIDDEN_SIZE,
     DICE_INLINE_CONFIG_SIZE,
 };
+use std::ptr;
 
 /// The size of a DICE hash.
 pub const HASH_SIZE: usize = DICE_HASH_SIZE as usize;
diff --git a/diced/open_dice/src/error.rs b/diced/open_dice/src/error.rs
new file mode 100644
index 0000000..9cf2ae8
--- /dev/null
+++ b/diced/open_dice/src/error.rs
@@ -0,0 +1,78 @@
+// Copyright 2023, The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+//! Errors and relating functions thrown in this library.
+
+use open_dice_cbor_bindgen::DiceResult;
+use std::{fmt, result};
+
+#[cfg(feature = "std")]
+use std::error::Error;
+
+/// Error type used by DICE.
+#[derive(Debug)]
+pub enum DiceError {
+    /// Provided input was invalid.
+    InvalidInput,
+    /// Provided buffer was too small.
+    BufferTooSmall,
+    /// Platform error.
+    PlatformError,
+    /// Input string has an interior nul byte.
+    /// TODO(b/267575445): Remove this error once we change the param of
+    /// `format_config_descriptor to take &CStr.
+    #[cfg(feature = "std")]
+    CStrNulError,
+    /// The allocation of a ZVec failed.
+    #[cfg(feature = "std")]
+    ZVecError(keystore2_crypto::zvec::Error),
+}
+
+#[cfg(feature = "std")]
+impl From<keystore2_crypto::zvec::Error> for DiceError {
+    fn from(e: keystore2_crypto::zvec::Error) -> Self {
+        Self::ZVecError(e)
+    }
+}
+
+/// This makes `DiceError` accepted by anyhow.
+#[cfg(feature = "std")]
+impl Error for DiceError {}
+
+impl fmt::Display for DiceError {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        match self {
+            Self::InvalidInput => write!(f, "invalid input"),
+            Self::BufferTooSmall => write!(f, "buffer too small"),
+            Self::PlatformError => write!(f, "platform error"),
+            #[cfg(feature = "std")]
+            Self::CStrNulError => write!(f, "input string has an interior nul byte"),
+            #[cfg(feature = "std")]
+            Self::ZVecError(e) => write!(f, "ZVec allocation failed {e}"),
+        }
+    }
+}
+
+/// DICE result type.
+pub type Result<T> = result::Result<T, DiceError>;
+
+/// Checks the given `DiceResult`. Returns an error if it's not OK.
+pub fn check_result(result: DiceResult) -> Result<()> {
+    match result {
+        DiceResult::kDiceResultOk => Ok(()),
+        DiceResult::kDiceResultInvalidInput => Err(DiceError::InvalidInput),
+        DiceResult::kDiceResultBufferTooSmall => Err(DiceError::BufferTooSmall),
+        DiceResult::kDiceResultPlatformError => Err(DiceError::PlatformError),
+    }
+}
diff --git a/diced/open_dice/src/lib.rs b/diced/open_dice/src/lib.rs
index 96e2569..f6f1781 100644
--- a/diced/open_dice/src/lib.rs
+++ b/diced/open_dice/src/lib.rs
@@ -17,6 +17,11 @@
 
 #![cfg_attr(not(feature = "std"), no_std)]
 
+#[cfg(not(feature = "std"))]
+extern crate core as std;
+
 mod dice;
+mod error;
 
 pub use dice::{Config, Hash, Hidden, InlineConfig, InputValues, HASH_SIZE, HIDDEN_SIZE};
+pub use error::{check_result, DiceError, Result};