Skip to content

API Gap: KeyWrapper, KeyUwrapper #150

Description

@dghgit

These are also on the OpenSSL list with the driving specifications being SP 800-38F "Recommendation for Block Cipher Modes of Operation: Methods for Key Wrapping" and RFC 5649.

Primarily the algorithms described are for AES and TDES, however Aria and Camellia (and I think SM4, not 100% sure....) support the same algorithms.

The algorithms require the full material being wrapped to be passed in.

Typing all this into Opus resulted in a suggestion looking like this:

pub trait KeyWrapper<const KEK_LEN: usize>: Algorithm {
    /// Half the underlying block cipher's block size, in bytes -- "semiblock", Sec. 4.1.
    /// 8 for KW and KWP (128-bit block); 4 for TKW (TDEA, 64-bit block).
    const SEMIBLOCK_LEN: usize;

    /// Wraps `key` under `kek`. `CT_LEN` is determined by `KEY_LEN` and checked at compile time.
    ///
    /// # Errors
    /// Rejects a KEK whose [`KeyType`] is not [`KeyType::SymmetricCipherKey`], and one whose
    /// security strength is below that of the key being wrapped (Appendix A.2), both as
    /// [`KeyWrapError::KeyMaterialError`].
    fn wrap_key<const KEY_LEN: usize, const CT_LEN: usize>(
        kek: &KeyMaterial<KEK_LEN>,
        key: &KeyMaterial<KEY_LEN>,
    ) -> Result<[u8; CT_LEN], KeyWrapError>;

    /// As [`wrap_key`](Self::wrap_key), writing into a caller-provided buffer. Returns the number
    /// of bytes written, which is always `CT_LEN`.
    fn wrap_key_out<const KEY_LEN: usize, const CT_LEN: usize>(
        kek: &KeyMaterial<KEK_LEN>,
        key: &KeyMaterial<KEY_LEN>,
        ciphertext: &mut [u8; CT_LEN],
    ) -> Result<usize, KeyWrapError> {
        *ciphertext = Self::wrap_key(kek, key)?;
        Ok(CT_LEN)
    }
}

With an equivalent trait for KeyUnwrapper. I note KeyWrapErrror is also new.

Also of note is that these algorithms (especially the KWP (padded) format), get used for things other than symmetric and private keys. So while the suggestion above shows KeyMaterial going in (mostly correct) input as [u8] might be better.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions