Merge "Fix the missing std"
diff --git a/diced/Android.bp b/diced/Android.bp
index 6bd7c06..3e15ed7 100644
--- a/diced/Android.bp
+++ b/diced/Android.bp
@@ -25,8 +25,6 @@
     name: "libdiced_utils",
     crate_name: "diced_utils",
     srcs: ["src/utils.rs"],
-    vendor_available: true,
-
     rustlibs: [
         "libanyhow",
     ],
@@ -47,12 +45,11 @@
     name: "libdiced_sample_inputs",
     crate_name: "diced_sample_inputs",
     srcs: ["src/sample_inputs.rs"],
-    vendor_available: true,
-
     rustlibs: [
         "libanyhow",
+        "libciborium",
+        "libcoset",
         "libdiced_open_dice",
-        "libdiced_utils",
     ],
 }
 
@@ -64,7 +61,8 @@
     auto_gen_config: true,
     rustlibs: [
         "libanyhow",
+        "libciborium",
+        "libcoset",
         "libdiced_open_dice",
-        "libdiced_utils",
     ],
 }
diff --git a/diced/src/sample_inputs.rs b/diced/src/sample_inputs.rs
index 19200a5..8b914f3 100644
--- a/diced/src/sample_inputs.rs
+++ b/diced/src/sample_inputs.rs
@@ -16,14 +16,14 @@
 //! as well as tuple of CDIs and BCC derived thereof.
 
 use anyhow::{anyhow, Context, Result};
+use ciborium::{de, ser, value::Value};
+use coset::{iana, Algorithm, AsCborValue, CoseKey, KeyOperation, KeyType, Label};
 use diced_open_dice::{
     derive_cdi_private_key_seed, keypair_from_seed, retry_bcc_format_config_descriptor,
     retry_bcc_main_flow, retry_dice_main_flow, Config, DiceArtifacts, DiceMode, InputValues,
     OwnedDiceArtifacts, CDI_SIZE, HASH_SIZE, HIDDEN_SIZE,
 };
-use diced_utils::cbor;
 use std::ffi::CStr;
-use std::io::Write;
 
 /// Sample UDS used to perform the root dice flow by `make_sample_bcc_and_cdis`.
 pub const UDS: &[u8; CDI_SIZE] = &[
@@ -74,35 +74,22 @@
     0x0a, 0xde, 0x29, 0x24, 0xff, 0x2e, 0xfa, 0xc7, 0x10, 0xd5, 0x73, 0xd4, 0xc6, 0xdf, 0x62, 0x9f,
 ];
 
-fn encode_pub_key_ed25519(pub_key: &[u8], stream: &mut dyn Write) -> Result<()> {
-    cbor::encode_header(5 /* CBOR MAP */, 5, stream)
-        .context("In encode_pub_key_ed25519: Trying to encode map header.")?;
-    cbor::encode_number(1, stream)
-        .context("In encode_pub_key_ed25519: Trying to encode Key type tag.")?;
-    cbor::encode_number(1, stream)
-        .context("In encode_pub_key_ed25519: Trying to encode Key type.")?;
-    cbor::encode_number(3, stream)
-        .context("In encode_pub_key_ed25519: Trying to encode algorithm tag.")?;
-    // Encoding a -8 for AlgorithmEdDSA. The encoded number is -1 - <header argument>,
-    // the an argument of 7 below.
-    cbor::encode_header(1 /* CBOR NEGATIVE INT */, 7 /* -1 -7 = -8*/, stream)
-        .context("In encode_pub_key_ed25519: Trying to encode algorithm.")?;
-    cbor::encode_number(4, stream)
-        .context("In encode_pub_key_ed25519: Trying to encode ops tag.")?;
-    // Encoding a single-element array for key ops
-    cbor::encode_header(4 /* CBOR ARRAY */, 1, stream)
-        .context("In encode_pub_key_ed25519: Trying to encode ops array header.")?;
-    // Ops 2 for verify.
-    cbor::encode_number(2, stream).context("In encode_pub_key_ed25519: Trying to encode ops.")?;
-    cbor::encode_header(1 /* CBOR NEGATIVE INT */, 0 /* -1 -0 = -1*/, stream)
-        .context("In encode_pub_key_ed25519: Trying to encode curve tag.")?;
-    // Curve 6 for Ed25519
-    cbor::encode_number(6, stream).context("In encode_pub_key_ed25519: Trying to encode curve.")?;
-    cbor::encode_header(1 /* CBOR NEGATIVE INT */, 1 /* -1 -1 = -2*/, stream)
-        .context("In encode_pub_key_ed25519: Trying to encode X coordinate tag.")?;
-    cbor::encode_bstr(pub_key, stream)
-        .context("In encode_pub_key_ed25519: Trying to encode X coordinate.")?;
-    Ok(())
+fn ed25519_public_key_to_cbor_value(public_key: &[u8]) -> Result<Value> {
+    let key = CoseKey {
+        kty: KeyType::Assigned(iana::KeyType::OKP),
+        alg: Some(Algorithm::Assigned(iana::Algorithm::EdDSA)),
+        key_ops: vec![KeyOperation::Assigned(iana::KeyOperation::Verify)].into_iter().collect(),
+        params: vec![
+            (
+                Label::Int(iana::Ec2KeyParameter::Crv as i64),
+                Value::from(iana::EllipticCurve::Ed25519 as u64),
+            ),
+            (Label::Int(iana::Ec2KeyParameter::X as i64), Value::Bytes(public_key.to_vec())),
+        ],
+        ..Default::default()
+    };
+    key.to_cbor_value()
+        .map_err(|e| anyhow!(format!("Failed to serialize the key to CBOR data. Error: {e}")))
 }
 
 /// Makes a DICE chain (BCC) from the sample input.
@@ -113,16 +100,12 @@
     let private_key_seed = derive_cdi_private_key_seed(UDS)
         .context("In make_sample_bcc_and_cdis: Trying to derive private key seed.")?;
 
-    // Sets the root public key in DICE chain (BCC).
+    // Gets the root public key in DICE chain (BCC).
     let (public_key, _) = keypair_from_seed(private_key_seed.as_array())
         .context("In make_sample_bcc_and_cids: Failed to generate key pair.")?;
-    let mut bcc: Vec<u8> = vec![];
-    cbor::encode_header(4 /* CBOR ARRAY */, 2, &mut bcc)
-        .context("In make_sample_bcc_and_cdis: Trying to encode array header.")?;
-    encode_pub_key_ed25519(&public_key, &mut bcc)
-        .context("In make_sample_bcc_and_cdis: Trying encode pub_key.")?;
+    let ed25519_public_key_value = ed25519_public_key_to_cbor_value(&public_key)?;
 
-    // Appends ABL certificate to DICE chain.
+    // Gets the ABL certificate to as the root certificate of DICE chain.
     let config_descriptor = retry_bcc_format_config_descriptor(
         Some(CStr::from_bytes_with_nul(b"ABL\0").unwrap()),
         Some(1), // version
@@ -135,9 +118,14 @@
         DiceMode::kDiceModeNormal,
         HIDDEN_ABL,
     );
-    let (cdi_values, mut cert) = retry_dice_main_flow(UDS, UDS, &input_values)
+    let (cdi_values, cert) = retry_dice_main_flow(UDS, UDS, &input_values)
         .context("In make_sample_bcc_and_cdis: Trying to run first main flow.")?;
-    bcc.append(&mut cert);
+    let bcc_value = Value::Array(vec![
+        ed25519_public_key_value,
+        de::from_reader(&cert[..]).context("Deserialize root DICE certificate failed")?,
+    ]);
+    let mut bcc: Vec<u8> = vec![];
+    ser::into_writer(&bcc_value, &mut bcc)?;
 
     // Appends AVB certificate to DICE chain.
     let config_descriptor = retry_bcc_format_config_descriptor(