Skip to content

feat(quote): add sig/pubkey crypto and some quote header types #75

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ rcrypto = ["rand", "rsa", "sha2", "num-integer", "num-traits"]
x86_64 = { version = "^0.14.6", default-features = false }
xsave = { version = "^2.0.0", default-features = false }
openssl = { version = "^0.10.36", optional = true }
der = { version = "0.6.0-pre.3", features = ["derive"], optional = false }
bitflags = "^1.3.2"

# Used by the rcrypto feature (see above).
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ How to use this crate partly depends on what you are trying to accomplish:
3. If you are developing an enclave loader, you probably want the
`parameters` and `page` modules. However, you may also want the
`signature` module to load a signature.
4. If you are interested in quote parsing and validation, you probably
want the `quote` module.

License: Apache-2.0
54 changes: 54 additions & 0 deletions src/crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ pub mod openssl;
#[cfg(feature = "rcrypto")]
pub mod rcrypto;

use der::{asn1::UIntBytes, Encodable, Sequence};

/// A fixed-size hash
pub trait Digest: Sized {
type Output: AsRef<[u8]>;
Expand Down Expand Up @@ -45,6 +47,58 @@ pub struct SigData {
pub q2: [u8; 384],
}

/// EC KT-I Public Key, the x-coordinate followed by
/// the y-coordinate (on the RFC 6090P-256 curve),
/// 2 x 32 bytes.
/// A.4, Table 7
#[derive(Clone, Debug)]
#[repr(C)]
pub struct EcdsaPubKey {
pub x: [u8; 32],
pub y: [u8; 32],
}

/// ECDSA signature, the r component followed by the
/// s component, 2 x 32 bytes.
/// A.4, Table 6
#[derive(Clone, Debug)]
#[repr(C)]
pub struct EcdsaP256Sig {
pub r: [u8; 32],
pub s: [u8; 32],
}

impl EcdsaP256Sig {
// The size of the buffer the DER encoded signature will occupy.
const DER_ENCODED_LEN: usize = 72;

/// Converts the concatenated signature to a DER signature.
///
/// Returns the buffer containing the DER and its length.
pub fn to_der(&self) -> Result<([u8; Self::DER_ENCODED_LEN], usize), der::Error> {
/// ECDSA-Sig-Value ::= SEQUENCE {
/// r INTEGER,
/// s INTEGER
/// }
#[derive(Clone, Debug, Sequence)]
struct EcdsaSig<'a> {
r: UIntBytes<'a>,
s: UIntBytes<'a>,
}

let es = EcdsaSig {
r: UIntBytes::new(&self.r)?,
s: UIntBytes::new(&self.s)?,
};

let mut buffer = [0; Self::DER_ENCODED_LEN];
let mut encoder = der::Encoder::new(&mut buffer);
es.encode(&mut encoder)?;
let len = encoder.finish()?.len();
Ok((buffer, len))
}
}

#[cfg(test)]
#[allow(dead_code)]
fn selftest<K: PrivateKey, D: Digest<Output = [u8; 32]>>() {
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
//! 3. If you are developing an enclave loader, you probably want the
//! `parameters` and `page` modules. However, you may also want the
//! `signature` module to load a signature.
//! 4. If you are interested in quote parsing and validation, you probably
//! want the `quote` module.

#![cfg_attr(not(test), no_std)]
#![deny(clippy::exhaustive_enums)]
Expand Down Expand Up @@ -87,6 +89,7 @@ macro_rules! testaso {
pub mod crypto;
pub mod page;
pub mod parameters;
pub mod quote;
pub mod signature;
pub mod ssa;

Expand Down
22 changes: 22 additions & 0 deletions src/quote/header.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// SPDX-License-Identifier: Apache-2.0

/// The type of attestation key used to sign the Report.
///
/// ECDSA: <https://en.wikipedia.org/wiki/Elliptic_Curve_Digital_Signature_Algorithm>
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
#[non_exhaustive]
#[repr(u16)]
pub enum KeyType {
/// ECDSA-256-with-P-256 curve
ES256 = 2,
/// ECDSA-384-with-P-384 curve
ES384 = 3,
}

/// The version of the quote.
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
#[non_exhaustive]
#[repr(u16)]
pub enum QuoteVersion {
V3 = 3,
}
3 changes: 3 additions & 0 deletions src/quote/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// SPDX-License-Identifier: Apache-2.0

pub mod header;