Skip to content

Commit 493c104

Browse files
committed
Add test case
1 parent 7247f64 commit 493c104

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

dtls/src/crypto/crypto_test.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
use super::crypto_ccm::*;
12
use super::*;
23

4+
use crate::content::ContentType;
5+
use crate::record_layer::record_layer_header::{ProtocolVersion, RECORD_LAYER_HEADER_SIZE};
6+
37
use std::io::Cursor;
48

59
use util::Error;
@@ -98,3 +102,53 @@ fn test_generate_key_signature() -> Result<(), Error> {
98102

99103
Ok(())
100104
}
105+
106+
#[test]
107+
fn test_ccm_encryption_and_decryption() -> Result<(), Error> {
108+
let key = vec![
109+
0x18, 0x78, 0xac, 0xc2, 0x2a, 0xd8, 0xbd, 0xd8, 0xc6, 0x01, 0xa6, 0x17, 0x12, 0x6f, 0x63,
110+
0x54, 0x18, 0x78, 0xac, 0xc2, 0x2a, 0xd8, 0xbd, 0xd8, 0xc6, 0x01, 0xa6, 0x17, 0x12, 0x6f,
111+
0x63, 0x54,
112+
];
113+
let iv = vec![0x0e, 0xb2, 0x09, 0x06];
114+
115+
let ccm = CryptCcm::new(CryptoCcmTagLen::CryptoCcmTagLength, &key, &iv, &key, &iv);
116+
117+
let rlh = RecordLayerHeader {
118+
content_type: ContentType::ApplicationData,
119+
protocol_version: ProtocolVersion {
120+
major: 0xfe,
121+
minor: 0xff,
122+
},
123+
epoch: 0,
124+
sequence_number: 18,
125+
content_len: 3,
126+
};
127+
128+
let raw = vec![
129+
0x17, 0xfe, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x00, 0x03, 0xff, 0xaa,
130+
0xbb,
131+
];
132+
133+
let cipher_text = ccm.encrypt(&rlh, &raw)?;
134+
135+
assert_eq!(
136+
[0, 27],
137+
&cipher_text[RECORD_LAYER_HEADER_SIZE - 2..RECORD_LAYER_HEADER_SIZE],
138+
"RecordLayer size updating failed \nexp: {:?} \nactual {:?} ",
139+
[0, 27],
140+
&cipher_text[RECORD_LAYER_HEADER_SIZE - 2..RECORD_LAYER_HEADER_SIZE]
141+
);
142+
143+
let plain_text = ccm.decrypt(&cipher_text)?;
144+
145+
assert_eq!(
146+
raw[RECORD_LAYER_HEADER_SIZE..],
147+
plain_text[RECORD_LAYER_HEADER_SIZE..],
148+
"Decryption failed \nexp: {:?} \nactual {:?} ",
149+
&raw[RECORD_LAYER_HEADER_SIZE..],
150+
&plain_text[RECORD_LAYER_HEADER_SIZE..]
151+
);
152+
153+
Ok(())
154+
}

0 commit comments

Comments
 (0)