Skip to content
This repository has been archived by the owner on Jul 2, 2023. It is now read-only.

KMS: add API definitions and crate for KMS integration #189

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Xynnn007
Copy link
Member

This PR include the following:

  • Added a crate deps/kms
  • Added Datakey API abstraction inside deps/kms
  • Added KMS module API abstraction inside deps/kms
  • Implemented an example for KMS

related to confidential-containers/guest-components#188

This PR will help to give a basic lib for image en/decryption using KMS.

cc @fitzthum @jialez0

@Xynnn007 Xynnn007 force-pushed the feat-kms branch 3 times, most recently from f5cff4b to 96f486a Compare April 23, 2023 09:44
Copy link
Member

@jepio jepio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the "datakey" abstraction in the context of AA/KBS. Can you explain how it is going to be used?

const IV: &[u8] = b"unique nonce";

/// Only for sample
pub const EXAMPLE_CMK: &[u8] = &[
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is CMK

Copy link
Member Author

@Xynnn007 Xynnn007 Apr 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is Customer Master Key abstraction. Different KMS will call it a different name. The core idea of this key is that it never leave KMS in plaintext. Sometimes it is also named "key encryption key".

async fn decrypt(&mut self, ciphertext: &[u8]) -> Result<Vec<u8>>;

/// Export the key blob that can be used to reconstruct the Datakey
/// except the plaintext of the Datakey.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does this part mean "except the plaintext of the datakey"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my mind the key blob will be stored in KBS. When the datakey is to be used, KBS can use this blob to reconstruct the plaintext of this datakey. But the plaintext cannot appear when the key blob is being stored. Usually the ciphertext of the datakey, the CMK (a.k.a key encryption key) id to encrypt this datakey, etc are in the key blob.

Comment on lines 19 to 20
//! - KMS can help to decrypt the ciphertext of the key to get the plaintext
//! of the key.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need the KMS to "decrypt the ciphertext of the key", if "the plaintext of the key can be exported outside the KMS".

i thought keys were going to be wrapped using a TEE pubkey.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This design philosophy comes from the API design from aliyun KMS, aws KMS etc. It is similiar to "Envelope encryption", s.t. wrap the datakey with CMK.

async fn encrypt(&mut self, plaintext: &[u8]) -> Result<Vec<u8>> {
let encryption_key = Key::<Aes256Gcm>::from_slice(&self.key);
let cipher = Aes256Gcm::new(encryption_key);
let nonce = Nonce::from_slice(IV);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is kind of a bad example, because the IV needs to be returned together with the ciphertext.
And decrypt needs to receive the IV.

@Xynnn007 Xynnn007 marked this pull request as draft May 10, 2023 03:42
@Xynnn007 Xynnn007 mentioned this pull request May 10, 2023
This commit include the following:
- Added a crate `deps/kms`
- Added Datakey API abstraction inside `deps/kms`
- Added KMS module API abstraction inside `deps/kms`
- Implemented an example for KMS

Signed-off-by: Xynnn007 <xynnn@linux.alibaba.com>
Also, use rstest dep v0.17.0 for all subcrates under this repo

Signed-off-by: Xynnn007 <xynnn@linux.alibaba.com>
Signed-off-by: Xynnn007 <xynnn@linux.alibaba.com>
@Xynnn007
Copy link
Member Author

Hi @jepio , I've updated the PR to try to simplify the API. Also, iv is attached in Sample KMS. Thanks for your suggestions, please take another look if it looks good to you.

@Xynnn007 Xynnn007 marked this pull request as ready for review May 11, 2023 03:56
@jepio
Copy link
Member

jepio commented May 12, 2023

So the interface now is:

  • generate_key generates a symmetric KEK
  • encrypt is used for LEK encryption (wrapping)
  • decrypt is used for LEK decryption (unwrapping)
    ?

If this is only for image decryption/encryption then that's probably good enough, but we'll need to see how the API fits existing implementations.

@jepio
Copy link
Member

jepio commented May 12, 2023

I'm gone for 1 week so don't wait for me.

@Xynnn007
Copy link
Member Author

@jepio This is a basic abstraction for KMSes. We can use this as a primitive to construct the image en/decryption. After this PR I will open another to impl this.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants