|
| 1 | +/* |
| 2 | + * Copyright (C) 2006-2026 wolfSSL Inc. |
| 3 | + * |
| 4 | + * This file is part of wolfSSL. |
| 5 | + * |
| 6 | + * wolfSSL is free software; you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation; either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * wolfSSL is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with this program; if not, write to the Free Software |
| 18 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
| 19 | + */ |
| 20 | + |
| 21 | +/*! |
| 22 | +RustCrypto `digest::Mac` trait implementations for the wolfCrypt CMAC types. |
| 23 | +
|
| 24 | +This module provides typed AES-CMAC wrappers with implementations of the |
| 25 | +traits from the `digest` crate (`MacMarker`, `KeyInit`, `Update`, |
| 26 | +`FixedOutput`) for each AES key size (128, 192, 256). With these |
| 27 | +implementations the `digest::Mac` trait becomes available via its blanket |
| 28 | +implementation, allowing these CMAC types to be used anywhere a RustCrypto |
| 29 | +`Mac` is accepted. |
| 30 | +
|
| 31 | +Any failure returned by the underlying wolfCrypt call in a trait method will |
| 32 | +result in a panic, matching the infallible signatures required by the |
| 33 | +RustCrypto traits. |
| 34 | +*/ |
| 35 | + |
| 36 | +use digest::consts::{U16, U24, U32}; |
| 37 | + |
| 38 | +macro_rules! impl_cmac_mac { |
| 39 | + ( |
| 40 | + $(#[$attr:meta])* |
| 41 | + $name:ident, key = $key_size:ty |
| 42 | + ) => { |
| 43 | + $(#[$attr])* |
| 44 | + pub struct $name { |
| 45 | + cmac: crate::cmac::CMAC, |
| 46 | + } |
| 47 | + |
| 48 | + $(#[$attr])* |
| 49 | + impl digest::MacMarker for $name {} |
| 50 | + |
| 51 | + $(#[$attr])* |
| 52 | + impl digest::OutputSizeUser for $name { |
| 53 | + type OutputSize = U16; |
| 54 | + } |
| 55 | + |
| 56 | + $(#[$attr])* |
| 57 | + impl digest::common::KeySizeUser for $name { |
| 58 | + type KeySize = $key_size; |
| 59 | + } |
| 60 | + |
| 61 | + $(#[$attr])* |
| 62 | + impl digest::KeyInit for $name { |
| 63 | + fn new(key: &digest::Key<Self>) -> Self { |
| 64 | + Self { |
| 65 | + cmac: crate::cmac::CMAC::new(key.as_slice()) |
| 66 | + .expect("wolfCrypt CMAC init failed"), |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + $(#[$attr])* |
| 72 | + impl digest::Update for $name { |
| 73 | + fn update(&mut self, data: &[u8]) { |
| 74 | + crate::cmac::CMAC::update(&mut self.cmac, data) |
| 75 | + .expect("wolfCrypt CMAC update failed"); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + $(#[$attr])* |
| 80 | + impl digest::FixedOutput for $name { |
| 81 | + fn finalize_into(self, out: &mut digest::Output<Self>) { |
| 82 | + self.cmac.finalize(out.as_mut_slice()) |
| 83 | + .expect("wolfCrypt CMAC finalize failed"); |
| 84 | + } |
| 85 | + } |
| 86 | + }; |
| 87 | +} |
| 88 | + |
| 89 | +impl_cmac_mac! { |
| 90 | + CmacAes128, key = U16 |
| 91 | +} |
| 92 | + |
| 93 | +impl_cmac_mac! { |
| 94 | + CmacAes192, key = U24 |
| 95 | +} |
| 96 | + |
| 97 | +impl_cmac_mac! { |
| 98 | + CmacAes256, key = U32 |
| 99 | +} |
0 commit comments