8000 Re-apply PR #2463 (was accidentally removed) by jcape · Pull Request #2547 · mobilecoinfoundation/mobilecoin · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Re-apply PR #2463 (was accidentally removed) #2547

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 1 commit into from
Sep 21, 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
20 changes: 11 additions & 9 deletions attest/api/src/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use crate::attest::{AuthMessage, Message, NonceMessage};
use mc_attest_ake::{AuthRequestOutput, AuthResponseOutput};
use mc_attest_enclave_api::{
ClientAuthRequest, ClientAuthResponse, EnclaveMessage, EnclaveNonceMessage, PeerAuthRequest,
ClientAuthRequest, ClientAuthResponse, EnclaveMessage, NonceSession, PeerAuthRequest,
PeerAuthResponse, Session,
};
use mc_crypto_keys::Kex;
Expand Down Expand Up @@ -109,24 +109,26 @@ impl<S: Session> From<EnclaveMessage<S>> for Message {
}
}

impl<S: Session> From<NonceMessage> for EnclaveNonceMessage<S> {
fn from(src: NonceMessage) -> EnclaveNonceMessage<S> {
EnclaveNonceMessage {
impl From<NonceMessage> for EnclaveMessage<NonceSession> {
fn from(src: NonceMessage) -> Self {
let channel_id = NonceSession::new(src.channel_id, src.nonce);
Self {
aad: src.aad,
channel_id: S::from(&src.channel_id),
channel_id,
data: src.data,
nonce: src.nonce,
}
}
}

impl<S: Session> From<EnclaveNonceMessage<S>> for NonceMessage {
fn from(src: EnclaveNonceMessage<S>) -> NonceMessage {
impl From<EnclaveMessage<NonceSession>> for NonceMessage {
fn from(src: EnclaveMessage<NonceSession>) -> NonceMessage {
let mut retval = NonceMessage::default();
retval.set_aad(src.aad);
// it doesn't matter if we don't bump the nonce when retrieving it,
// src.channel_id will be discarded anyways.
retval.set_nonce(src.channel_id.peek_nonce());
retval.set_channel_id(src.channel_id.into());
retval.set_data(src.data);
retval.set_nonce(src.nonce);
retval
}
}
88 changes: 87 additions & 1 deletion attest/enclave-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ mod error;
pub use error::{Error, Result};

use alloc::vec::Vec;
use core::hash::Hash;
use core::hash::{Hash, Hasher};
use mc_attest_core::{IntelSealed, QuoteNonce, Report};
use serde::{Deserialize, Serialize};

Expand All @@ -35,6 +35,7 @@ macro_rules! impl_newtype_vec_inout {
impl_newtype_vec_inout! {
ClientAuthRequest; ClientAuthResponse; ClientSession;
PeerAuthRequest; PeerAuthResponse; PeerSession;
NonceAuthRequest; NonceAuthResponse;
}

/// The raw authentication request message, sent from an initiator to a
Expand All @@ -57,6 +58,16 @@ pub struct PeerAuthRequest(Vec<u8>);
#[derive(Clone, Debug, Default, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub struct PeerAuthResponse(Vec<u8>);

/// The raw authentication request message, sent from an initiator to a
/// responder.
#[derive(Clone, Debug, Default, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub struct NonceAuthRequest(Vec<u8>);

/// The raw authentication response message, sent from a responder to an
/// initiator.
#[derive(Clone, Debug, Default, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub struct NonceAuthResponse(Vec<u8>);

/// Inbound and outbound messages to/from an enclave.
#[derive(Clone, Debug, Default, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub struct EnclaveMessage<S: Session> {
Expand Down Expand Up @@ -143,3 +154,78 @@ impl Session for PeerSession {
type Request = PeerAuthRequest;
type Response = PeerAuthResponse;
}

/// An opaque bytestream used as a session ID for a session which uses explicit
/// nonces.
#[derive(Clone, Debug, Default, Deserialize, PartialOrd, Serialize)]
pub struct NonceSession {
channel_id: Vec<u8>,
nonce: u64,
}

impl NonceSession {
/// Create a new nonce session from a vector and nonce.
///
/// This takes a pre-created Vec in order to remove an extra allocation that
/// would be required when converting from a NonceMessage to an
/// [`EnclaveMessage`]`<`[`NonceSession`]`>`.
pub fn new(channel_id: Vec<u8>, nonce: u64) -> Self {
Self { channel_id, nonce }
}

/// Retrieves the nonce for this session
pub fn peek_nonce(&self) -> u64 {
self.nonce
}

/// Retrieves a copy of the nonce, and increments it for the next time.
pub fn get_nonce(&mut self) -> u64 {
let retval = self.nonce;
self.nonce += 1;
retval
}
}

impl AsRef<[u8]> for NonceSession {
fn as_ref(&self) -> &[u8] {
self.channel_id.as_ref()
}
}

impl<'bytes> From<&'bytes [u8]> for NonceSession {
fn from(src: &'bytes [u8]) -> Self {
Self::from(Vec::from(src))
}
}

impl From<Vec<u8>> for NonceSession {
fn from(channel_id: Vec<u8>) -> Self {
NonceSession {
channel_id,
nonce: 0,
}
}
}

impl From<NonceSession> for Vec<u8> {
fn from(src: NonceSession) -> Self {
src.channel_id
}
}

impl Hash for NonceSession {
fn hash<H: Hasher>(&self, state: &mut H) {
self.channel_id.hash(state)
}
}

impl PartialEq for NonceSession {
fn eq(&self, other: &Self) -> bool {
self.channel_id == other.channel_id
}
}

impl Session for NonceSession {
type Request = NonceAuthRequest;
type Response = NonceAuthResponse;
}
0