ssh/algorithm/mac/
hmac_sha2.rs1use crate::algorithm::mac::Mac;
2use ring::hmac;
3use ring::hmac::{Context, Tag};
4
5const BSIZE_256: usize = 32;
6const BSIZE_512: usize = 64;
7
8pub(super) struct HmacSha2_256;
9pub(super) struct HmacSha2_512;
10
11impl Mac for HmacSha2_256 {
12 fn sign(&self, ik: &[u8], sequence_num: u32, buf: &[u8]) -> Tag {
13 let ik = &ik[..BSIZE_256];
14 let key = hmac::Key::new(hmac::HMAC_SHA256, ik);
15 let mut c = Context::with_key(&key);
16 c.update(sequence_num.to_be_bytes().as_slice());
17 c.update(buf);
18 c.sign()
19 }
20
21 fn new() -> Self
22 where
23 Self: Sized,
24 {
25 HmacSha2_256
26 }
27
28 fn bsize(&self) -> usize {
29 BSIZE_256
30 }
31}
32
33impl Mac for HmacSha2_512 {
34 fn sign(&self, ik: &[u8], sequence_num: u32, buf: &[u8]) -> Tag {
35 let ik = &ik[..BSIZE_512];
36 let key = hmac::Key::new(hmac::HMAC_SHA512, ik);
37 let mut c = Context::with_key(&key);
38 c.update(sequence_num.to_be_bytes().as_slice());
39 c.update(buf);
40 c.sign()
41 }
42
43 fn new() -> Self
44 where
45 Self: Sized,
46 {
47 HmacSha2_512
48 }
49
50 fn bsize(&self) -> usize {
51 BSIZE_512
52 }
53}