On Mon, Nov 07, 2022 at 06:51:26PM +0200, Ilari Liusvaara wrote:
> 
>     - Making sender structure to be dictionary makes it more complex to
>       decode versus array structure, since implementation has to look up
>       the required keys (and handle those being missing!) instead of
>       just doing array indexing (after having ensured that array is big
>       enough).
> 

This might be artifact of CBOR library used, but here is some untested
code I wrote to pull apart PR9 and PR10 COSE_HPKE_Sender formats:

------------------------------------------------------------------------
use btls_aux_cbor::CBOR;
use core::convert::TryInto;

macro_rules! throw { ($x:expr) => { $x.ok_or(())? } }

fn __get_u16(val: &CBOR) -> Result<u16, ()>
{
        throw!(val.as_integer()).try_into().map_err(|_|())
}

pub struct HpkeSender<'a>
{
        pub kem: Option<u16>,
        pub kdf: u16,
        pub aead: u16,
        pub enc: &'a [u8],
}

impl<'a> HpkeSender<'a>
{
        pub fn decode_pr9(cbor: &'a CBOR) -> Result<HpkeSender<'a>, ()>
        {
                let cbor = throw!(cbor.as_dictionary());
                let kem = cbor.get(&CBOR::Integer(1));
                let kdf = throw!(cbor.get(&CBOR::Integer(2)));
                let aead = throw!(cbor.get(&CBOR::Integer(3)));
                let enc = throw!(cbor.get(&CBOR::Integer(4)));
                for key in cbor.keys() {
                        let key = throw!(key.as_integer());
                        if key < 1 || key > 4 { return Err(()); }
                }
                Ok(HpkeSender{
                        kem: kem.map(__get_u16).transpose()?,
                        kdf: __get_u16(kdf)?,
                        aead: __get_u16(aead)?,
                        enc: throw!(enc.as_octets()),
                })
        }
        pub fn decode_pr10(cbor: &'a CBOR) -> Result<HpkeSender<'a>, ()>
        {
                let cbor = throw!(cbor.as_array());
                if cbor.len() < 3 || cbor.len() > 4 { return Err(()); }
                let kdf = &cbor[0];
                let aead = &cbor[1];
                let enc = &cbor[2];
                let kem = cbor.get(3);
                Ok(HpkeSender{
                        kem: kem.map(__get_u16).transpose()?,
                        kdf: __get_u16(kdf)?,
                        aead: __get_u16(aead)?,
                        enc: throw!(enc.as_octets()),
                })
        }
}
------------------------------------------------------------------------

The difference is certainly much smaller than I expected...



-Ilari

_______________________________________________
COSE mailing list
[email protected]
https://www.ietf.org/mailman/listinfo/cose

Reply via email to