|
63 | 63 | #[cfg(feature = "std")]
|
64 | 64 | extern crate std;
|
65 | 65 |
|
66 |
| -pub use crypto_mac::{self, Mac, NewMac}; |
67 | 66 | pub use digest;
|
68 | 67 |
|
69 | 68 | use core::{cmp::min, fmt};
|
70 |
| -use crypto_mac::{ |
| 69 | +use digest::core_api::{Block, BlockSizeUser, BufferUser, FixedOutputCore, UpdateCore}; |
| 70 | +use digest::{ |
| 71 | + block_buffer::BlockBuffer, |
| 72 | + crypto_common::{Key, KeySizeUser}, |
71 | 73 | generic_array::{sequence::GenericSequence, ArrayLength, GenericArray},
|
72 |
| - InvalidKeyLength, Output, |
| 74 | + InvalidLength, KeyInit, Output, |
73 | 75 | };
|
74 |
| -use digest::{BlockInput, FixedOutput, Reset, Update}; |
75 | 76 |
|
76 | 77 | const IPAD: u8 = 0x36;
|
77 | 78 | const OPAD: u8 = 0x5C;
|
78 | 79 |
|
79 |
| -/// The `Hmac` struct represents an HMAC using a given hash function `D`. |
80 |
| -pub struct Hmac<D> |
| 80 | +/// Core HMAC operating over blocks. |
| 81 | +#[derive(Clone)] |
| 82 | +pub struct HmacCore<D> |
81 | 83 | where
|
82 |
| - D: Update + BlockInput + FixedOutput + Reset + Default + Clone, |
83 |
| - D::BlockSize: ArrayLength<u8>, |
| 84 | + D: UpdateCore + FixedOutputCore + BufferUser<Buffer = BlockBuffer<_>> + Default, |
84 | 85 | {
|
85 | 86 | digest: D,
|
86 | 87 | i_key_pad: GenericArray<u8, D::BlockSize>,
|
87 | 88 | opad_digest: D,
|
88 | 89 | }
|
| 90 | +/* |
| 91 | +impl<D> KeySizeUser for HmacCore<D> |
| 92 | +where |
| 93 | + D: UpdateCore + FixedOutputCore + BufferUser<Buffer = BlockBuffer<Self::BlockSize>> + Default, |
| 94 | +{ |
| 95 | + type KeySize = D::BlockSize; |
| 96 | +} |
| 97 | +
|
| 98 | +impl<D> BlockSizeUser for HmacCore<D> |
| 99 | +where |
| 100 | + D: UpdateCore + FixedOutputCore + Default, |
| 101 | +{ |
| 102 | + type BlockSize = D::BlockSize; |
| 103 | +} |
| 104 | +
|
| 105 | +
|
| 106 | +impl<D> KeyInit for HmacCore<D> |
| 107 | +where |
| 108 | + D: UpdateCore + FixedOutputCore + Default, |
| 109 | +{ |
| 110 | + fn new(key: &Key<Self>) -> Self { |
| 111 | + Self::new_from_slice(key.as_slice()).unwrap() |
| 112 | + } |
| 113 | +
|
| 114 | + #[inline] |
| 115 | + fn new_from_slice(key: &[u8]) -> Result<Self, InvalidLength> { |
| 116 | + let mut der_key = Block::<Self>::default(); |
| 117 | + if key.len() <= der_key.len() { |
| 118 | + der_key.copy_from_slice(key); |
| 119 | + } else { |
| 120 | + // TODO digest key |
| 121 | + } |
| 122 | +
|
| 123 | + for b in der_key.iter_mut() { |
| 124 | +
|
| 125 | + } |
| 126 | + let mut opad_digest = D::default(); |
| 127 | + opad_digest.update_blocks(slice::from_ref(&der_key)); |
| 128 | +
|
| 129 | + panic!(); |
| 130 | +/* |
| 131 | + let mut hmac = Self { |
| 132 | + digest: Default::default(), |
| 133 | + i_key_pad: GenericArray::generate(|_| IPAD), |
| 134 | + opad_digest: Default::default(), |
| 135 | + }; |
| 136 | +
|
| 137 | +
|
| 138 | + let mut opad = GenericArray::<u8, D::BlockSize>::generate(|_| OPAD); |
| 139 | + debug_assert!(hmac.i_key_pad.len() == opad.len()); |
| 140 | +
|
| 141 | + // The key that Hmac processes must be the same as the block size of the |
| 142 | + // underlying Digest. If the provided key is smaller than that, we just |
| 143 | + // pad it with zeros. If its larger, we hash it and then pad it with |
| 144 | + // zeros. |
| 145 | + if key.len() <= hmac.i_key_pad.len() { |
| 146 | + for (k_idx, k_itm) in key.iter().enumerate() { |
| 147 | + hmac.i_key_pad[k_idx] ^= *k_itm; |
| 148 | + opad[k_idx] ^= *k_itm; |
| 149 | + } |
| 150 | + } else { |
| 151 | + let mut digest = D::default(); |
| 152 | + digest.update(key); |
| 153 | + let output = digest.finalize_fixed(); |
| 154 | + // `n` is calculated at compile time and will equal |
| 155 | + // D::OutputSize. This is used to ensure panic-free code |
| 156 | + let n = min(output.len(), hmac.i_key_pad.len()); |
| 157 | + for idx in 0..n { |
| 158 | + hmac.i_key_pad[idx] ^= output[idx]; |
| 159 | + opad[idx] ^= output[idx]; |
| 160 | + } |
| 161 | + } |
| 162 | +
|
| 163 | + hmac.digest.update(&hmac.i_key_pad); |
| 164 | + hmac.opad_digest.update(&opad); |
| 165 | +
|
| 166 | + Ok(hmac) |
| 167 | + */ |
| 168 | + } |
| 169 | +} |
| 170 | +*/ |
89 | 171 |
|
| 172 | +/* |
90 | 173 | impl<D> Clone for Hmac<D>
|
91 | 174 | where
|
92 | 175 | D: Update + BlockInput + FixedOutput + Reset + Default + Clone,
|
@@ -128,7 +211,7 @@ where
|
128 | 211 | }
|
129 | 212 |
|
130 | 213 | #[inline]
|
131 |
| - fn new_from_slice(key: &[u8]) -> Result<Self, InvalidKeyLength> { |
| 214 | + fn new_from_slice(key: &[u8]) -> Result<Self, InvalidLength> { |
132 | 215 | let mut hmac = Self {
|
133 | 216 | digest: Default::default(),
|
134 | 217 | i_key_pad: GenericArray::generate(|_| IPAD),
|
@@ -211,3 +294,4 @@ where
|
211 | 294 | Ok(())
|
212 | 295 | }
|
213 | 296 | }
|
| 297 | +*/ |
0 commit comments