blob: 3308e0d1a5536d614f7bca8a81fe274841fd96d9 [file] [log] [blame]
Shikha Panwar95084df2023-07-22 11:47: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//! Class for encapsulating & managing represent VM secrets.
16
17use anyhow::Result;
18use diced_open_dice::{DiceArtifacts, OwnedDiceArtifacts};
19use keystore2_crypto::ZVec;
20use openssl::hkdf::hkdf;
21use openssl::md::Md;
22use openssl::sha;
23
24// Size of the secret stored in Secretkeeper.
25const SK_SECRET_SIZE: usize = 64;
26
27pub enum VmSecret {
28 // V2 secrets are derived from 2 independently secured secrets:
29 // 1. Secretkeeper protected secrets (skp secret).
30 // 2. Dice Sealing CDIs (Similar to V1).
31 //
32 // These are protected against rollback of boot images i.e. VM instance rebooted
33 // with downgraded images will not have access to VM's secret.
34 // V2 secrets require hardware support - Secretkeeper HAL, which (among other things)
35 // is backed by tamper-evident storage, providing rollback protection to these secrets.
36 V2 { dice: OwnedDiceArtifacts, skp_secret: ZVec },
37 // V1 secrets are not protected against rollback of boot images.
38 // They are reliable only if rollback of images was prevented by verified boot ie,
39 // each stage (including pvmfw/Microdroid/Microdroid Manager) prevents downgrade of next
40 // stage. These are now legacy secrets & used only when Secretkeeper HAL is not supported
41 // by device.
42 V1 { dice: OwnedDiceArtifacts },
43}
44
45impl VmSecret {
46 pub fn new(dice_artifacts: OwnedDiceArtifacts) -> Result<VmSecret> {
47 if is_sk_supported() {
48 // TODO(b/291213394): Change this to real Sk protected secret.
49 let fake_skp_secret = ZVec::new(SK_SECRET_SIZE)?;
50 return Ok(Self::V2 { dice: dice_artifacts, skp_secret: fake_skp_secret });
51 }
52 Ok(Self::V1 { dice: dice_artifacts })
53 }
54 pub fn dice(&self) -> &OwnedDiceArtifacts {
55 match self {
56 Self::V2 { dice, .. } => dice,
57 Self::V1 { dice } => dice,
58 }
59 }
60
61 fn get_vm_secret(&self, salt: &[u8], identifier: &[u8], key: &mut [u8]) -> Result<()> {
62 match self {
63 Self::V2 { dice, skp_secret } => {
64 let mut hasher = sha::Sha256::new();
65 hasher.update(dice.cdi_seal());
66 hasher.update(skp_secret);
67 hkdf(key, Md::sha256(), &hasher.finish(), salt, identifier)?
68 }
69 Self::V1 { dice } => hkdf(key, Md::sha256(), dice.cdi_seal(), salt, identifier)?,
70 }
71 Ok(())
72 }
73
74 /// Derives a sealing key of `key_length` bytes from the VmSecret.
75 /// Essentially key expansion.
76 pub fn derive_sealing_key(&self, salt: &[u8], identifier: &[u8], key: &mut [u8]) -> Result<()> {
77 self.get_vm_secret(salt, identifier, key)
78 }
79}
80
81// Does the hardware support Secretkeeper.
82fn is_sk_supported() -> bool {
83 if cfg!(llpvm_changes) {
84 return false;
85 };
86 // TODO(b/292209416): This value should be extracted from device tree.
87 // Note: this does not affect the security of pVM. pvmfw & microdroid_manager continue to block
88 // upgraded images. Setting this true is equivalent to including constant salt in vm secrets.
89 true
90}