| 1 | //! This crate provides trait for Message Authentication Code (MAC) algorithms. |
| 2 | |
| 3 | #![no_std] |
| 4 | #![cfg_attr(docsrs, feature(doc_cfg))] |
| 5 | #![doc( |
| 6 | html_logo_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg", |
| 7 | html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg" |
| 8 | )] |
| 9 | #![forbid(unsafe_code)] |
| 10 | #![warn(missing_docs, rust_2018_idioms)] |
| 11 | |
| 12 | #[cfg(feature = "std")] |
| 13 | extern crate stdrust-analyzer cargo crypto-mac 0.10.0 std/; |
| 14 | |
| 15 | #[cfg(feature = "cipher")] |
| 16 | pub use cipher; |
| 17 | #[cfg(feature = "cipher")] |
| 18 | use cipher::{BlockCipher, NewBlockCipher}; |
| 19 | |
| 20 | #[cfg(feature = "dev")] |
| 21 | #[cfg_attr(docsrs, doc(cfg(feature = "dev")))] |
| 22 | pub mod dev; |
| 23 | |
| 24 | mod errorsrust-analyzer cargo crypto-mac 0.10.0 errors/; |
| 25 | |
| 26 | pub use craterust-analyzer cargo crypto-mac 0.10.0 crate/::errorsrust-analyzer cargo crypto-mac 0.10.0 errors/::{InvalidKeyLengthrust-analyzer cargo crypto-mac 0.10.0 errors/InvalidKeyLength#, MacErrorrust-analyzer cargo crypto-mac 0.10.0 errors/MacError#}; |
| 27 | pub use generic_arrayrust-analyzer cargo generic-array 0.14.9 crate/::{selfrust-analyzer cargo generic-array 0.14.9 crate/, typenumrust-analyzer cargo typenum 1.20.1 crate/::constsrust-analyzer cargo typenum 1.20.1 gen/consts/}; |
| 28 | |
| 29 | use generic_arrayrust-analyzer cargo generic-array 0.14.9 crate/::typenumrust-analyzer cargo typenum 1.20.1 crate/::Unsignedrust-analyzer cargo typenum 1.20.1 marker_traits/Unsigned#; |
| 30 | use generic_arrayrust-analyzer cargo generic-array 0.14.9 crate/::{ArrayLengthrust-analyzer cargo generic-array 0.14.9 ArrayLength#, GenericArrayrust-analyzer cargo generic-array 0.14.9 GenericArray#}; |
| 31 | use subtlerust-analyzer cargo subtle 2.6.1 crate/::{Choicerust-analyzer cargo subtle 2.6.1 Choice#, ConstantTimeEqrust-analyzer cargo subtle 2.6.1 ConstantTimeEq#}; |
| 32 | |
| 33 | /// Key for an algorithm that implements [`NewMac`]. |
| 34 | pub type Keyrust-analyzer cargo crypto-mac 0.10.0 Key#<M> = GenericArrayrust-analyzer cargo generic-array 0.14.9 GenericArray#<u8, <M as NewMacrust-analyzer cargo crypto-mac 0.10.0 NewMac#>::KeySizerust-analyzer cargo crypto-mac 0.10.0 NewMac#KeySize#>; |
| 35 | |
| 36 | /// Instantiate a [`Mac`] algorithm. |
| 37 | pub trait NewMacrust-analyzer cargo crypto-mac 0.10.0 NewMac#: Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized# { |
| 38 | /// Key size in bytes with which cipher guaranteed to be initialized. |
| 39 | type KeySizerust-analyzer cargo crypto-mac 0.10.0 NewMac#KeySize#: ArrayLengthrust-analyzer cargo generic-array 0.14.9 ArrayLength#<u8>; |
| 40 | |
| 41 | /// Initialize new MAC instance from key with fixed size. |
| 42 | fn newrust-analyzer cargo crypto-mac 0.10.0 NewMac#new().(key: &Keyrust-analyzer cargo crypto-mac 0.10.0 Key#<Self>) -> Self; |
| 43 | |
| 44 | /// Initialize new MAC instance from key with variable size. |
| 45 | /// |
| 46 | /// Default implementation will accept only keys with length equal to |
| 47 | /// `KeySize`, but some MACs can accept range of key lengths. |
| 48 | fn new_varkeyrust-analyzer cargo crypto-mac 0.10.0 NewMac#new_varkey().(key: &[u8]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Self, InvalidKeyLengthrust-analyzer cargo crypto-mac 0.10.0 errors/InvalidKeyLength#> { |
| 49 | if key.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().Self::KeySizerust-analyzer cargo crypto-mac 0.10.0 NewMac#KeySize#::to_usizerust-analyzer cargo typenum 1.20.1 marker_traits/Unsigned#to_usize().() { |
| 50 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(InvalidKeyLengthrust-analyzer cargo crypto-mac 0.10.0 errors/InvalidKeyLength#) |
| 51 | } else { |
| 52 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Self::newrust-analyzer cargo crypto-mac 0.10.0 NewMac#new().(GenericArrayrust-analyzer cargo generic-array 0.14.9 GenericArray#::from_slicerust-analyzer cargo generic-array 0.14.9 impl#[`GenericArray<T, N>`]from_slice().(key))) |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /// The [`Mac`] trait defines methods for a Message Authentication algorithm. |
| 58 | pub trait Macrust-analyzer cargo crypto-mac 0.10.0 Mac#: Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone# { |
| 59 | /// Output size of the [[`Mac`]] |
| 60 | type OutputSizerust-analyzer cargo crypto-mac 0.10.0 Mac#OutputSize#: ArrayLengthrust-analyzer cargo generic-array 0.14.9 ArrayLength#<u8>; |
| 61 | |
| 62 | /// Update MAC state with the given data. |
| 63 | fn updaterust-analyzer cargo crypto-mac 0.10.0 Mac#update().(&mut self, data: &[u8]); |
| 64 | |
| 65 | /// Reset [`Mac`] instance. |
| 66 | fn resetrust-analyzer cargo crypto-mac 0.10.0 Mac#reset().(&mut self); |
| 67 | |
| 68 | /// Obtain the result of a [`Mac`] computation as a [`Output`] and consume |
| 69 | /// [`Mac`] instance. |
| 70 | fn finalizerust-analyzer cargo crypto-mac 0.10.0 Mac#finalize().(self) -> Outputrust-analyzer cargo crypto-mac 0.10.0 Output#<Self>; |
| 71 | |
| 72 | /// Obtain the result of a [`Mac`] computation as a [`Output`] and reset |
| 73 | /// [`Mac`] instance. |
| 74 | fn finalize_resetrust-analyzer cargo crypto-mac 0.10.0 Mac#finalize_reset().(&mut self) -> Outputrust-analyzer cargo crypto-mac 0.10.0 Output#<Self> { |
| 75 | let res = self.clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#clone().().finalizerust-analyzer cargo crypto-mac 0.10.0 Mac#finalize().(); |
| 76 | self.resetrust-analyzer cargo crypto-mac 0.10.0 Mac#reset().(); |
| 77 | res |
| 78 | } |
| 79 | |
| 80 | /// Check if tag/code value is correct for the processed input. |
| 81 | fn verifyrust-analyzer cargo crypto-mac 0.10.0 Mac#verify().(self, tag: &[u8]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), MacErrorrust-analyzer cargo crypto-mac 0.10.0 errors/MacError#> { |
| 82 | let choice = self.finalizerust-analyzer cargo crypto-mac 0.10.0 Mac#finalize().().bytesrust-analyzer cargo crypto-mac 0.10.0 Output#bytes..ct_eqrust-analyzer cargo subtle 2.6.1 impl#[`[T]`][ConstantTimeEq]ct_eq().(tag); |
| 83 | |
| 84 | if choice.unwrap_u8rust-analyzer cargo subtle 2.6.1 impl#[Choice]unwrap_u8().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().1 { |
| 85 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 86 | } else { |
| 87 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(MacErrorrust-analyzer cargo crypto-mac 0.10.0 errors/MacError#) |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /// [`Output`] is a thin wrapper around bytes array which provides a safe `Eq` |
| 93 | /// implementation that runs in a fixed time. |
| 94 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)] |
| 95 | pub struct Outputrust-analyzer cargo crypto-mac 0.10.0 Output#<M: Macrust-analyzer cargo crypto-mac 0.10.0 Mac#> { |
| 96 | bytesrust-analyzer cargo crypto-mac 0.10.0 Output#bytes.: GenericArrayrust-analyzer cargo generic-array 0.14.9 GenericArray#<u8, M::OutputSizerust-analyzer cargo crypto-mac 0.10.0 Mac#OutputSize#>, |
| 97 | } |
| 98 | |
| 99 | impl<M: Macrust-analyzer cargo crypto-mac 0.10.0 Mac#> Outputrust-analyzer cargo crypto-mac 0.10.0 Output#<M> { |
| 100 | /// Create a new MAC [`Output`]. |
| 101 | pub fn newrust-analyzer cargo crypto-mac 0.10.0 impl#[`Output<M>`]new().(bytes: GenericArrayrust-analyzer cargo generic-array 0.14.9 GenericArray#<u8, M::OutputSizerust-analyzer cargo crypto-mac 0.10.0 Mac#OutputSize#>) -> Outputrust-analyzer cargo crypto-mac 0.10.0 Output#<M> { |
| 102 | Outputrust-analyzer cargo crypto-mac 0.10.0 Output# { bytesrust-analyzer cargo crypto-mac 0.10.0 Output#bytes. } |
| 103 | } |
| 104 | |
| 105 | /// Get the MAC tag/code value as a byte array. |
| 106 | /// |
| 107 | /// Be very careful using this method, since incorrect use of the tag value |
| 108 | /// may permit timing attacks which defeat the security provided by the |
| 109 | /// [`Mac`] trait. |
| 110 | pub fn into_bytesrust-analyzer cargo crypto-mac 0.10.0 impl#[`Output<M>`]into_bytes().(self) -> GenericArrayrust-analyzer cargo generic-array 0.14.9 GenericArray#<u8, M::OutputSizerust-analyzer cargo crypto-mac 0.10.0 Mac#OutputSize#> { |
| 111 | self.bytesrust-analyzer cargo crypto-mac 0.10.0 Output#bytes. |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | impl<M: Macrust-analyzer cargo crypto-mac 0.10.0 Mac#> ConstantTimeEqrust-analyzer cargo subtle 2.6.1 ConstantTimeEq# for Outputrust-analyzer cargo crypto-mac 0.10.0 Output#<M> { |
| 116 | fn ct_eqrust-analyzer cargo crypto-mac 0.10.0 impl#[`Output<M>`][ConstantTimeEq]ct_eq().(&self, other: &Selfrust-analyzer cargo crypto-mac 0.10.0 impl#[`Output<M>`][ConstantTimeEq]) -> Choicerust-analyzer cargo subtle 2.6.1 Choice# { |
| 117 | self.bytesrust-analyzer cargo crypto-mac 0.10.0 Output#bytes..ct_eqrust-analyzer cargo subtle 2.6.1 impl#[`[T]`][ConstantTimeEq]ct_eq().(&other.bytesrust-analyzer cargo crypto-mac 0.10.0 Output#bytes.) |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | impl<M: Macrust-analyzer cargo crypto-mac 0.10.0 Mac#> PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq# for Outputrust-analyzer cargo crypto-mac 0.10.0 Output#<M> { |
| 122 | fn eqrust-analyzer cargo crypto-mac 0.10.0 impl#[`Output<M>`][`PartialEq<Self>`]eq().(&self, x: &Outputrust-analyzer cargo crypto-mac 0.10.0 Output#<M>) -> bool { |
| 123 | self.ct_eqrust-analyzer cargo crypto-mac 0.10.0 impl#[`Output<M>`][ConstantTimeEq]ct_eq().(x).unwrap_u8rust-analyzer cargo subtle 2.6.1 impl#[Choice]unwrap_u8().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().1 |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | impl<M: Macrust-analyzer cargo crypto-mac 0.10.0 Mac#> Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq# for Outputrust-analyzer cargo crypto-mac 0.10.0 Output#<M> {} |
| 128 | |
| 129 | #[cfg(feature = "cipher")] |
| 130 | #[cfg_attr(docsrs, doc(cfg(feature = "cipher")))] |
| 131 | /// Trait for MAC functions which can be created from block cipher. |
| 132 | pub trait FromBlockCipher { |
| 133 | /// Block cipher type |
| 134 | type Cipher: BlockCipher; |
| 135 | |
| 136 | /// Create new MAC isntance from provided block cipher. |
| 137 | fn from_cipher(cipher: Self::Cipher) -> Self; |
| 138 | } |
| 139 | |
| 140 | #[cfg(feature = "cipher")] |
| 141 | #[cfg_attr(docsrs, doc(cfg(feature = "cipher")))] |
| 142 | impl<T> NewMacrust-analyzer cargo crypto-mac 0.10.0 NewMac# for T |
| 143 | where |
| 144 | T: FromBlockCipher, |
| 145 | T::Cipher: NewBlockCipher, |
| 146 | { |
| 147 | type KeySize = <<Self as FromBlockCipher>::Cipher as NewBlockCipher>::KeySize; |
| 148 | |
| 149 | fn new(key: &Keyrust-analyzer cargo crypto-mac 0.10.0 Key#<Self>) -> Self { |
| 150 | let cipher = <Self as FromBlockCipher>::Cipher::new(key); |
| 151 | Self::from_cipher(cipher) |
| 152 | } |
| 153 | |
| 154 | fn new_varkey(key: &[u8]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Self, InvalidKeyLengthrust-analyzer cargo crypto-mac 0.10.0 errors/InvalidKeyLength#> { |
| 155 | <Self as FromBlockCipher>::Cipher::new_varkey(key) |
| 156 | .map_err(|_| InvalidKeyLengthrust-analyzer cargo crypto-mac 0.10.0 errors/InvalidKeyLength#) |
| 157 | .map(Self::from_cipher) |
| 158 | } |
| 159 | } |