|
| 1 | +use super::crypto_ccm::*; |
1 | 2 | use super::*;
|
2 | 3 |
|
| 4 | +use crate::content::ContentType; |
| 5 | +use crate::record_layer::record_layer_header::{ProtocolVersion, RECORD_LAYER_HEADER_SIZE}; |
| 6 | + |
3 | 7 | use std::io::Cursor;
|
4 | 8 |
|
5 | 9 | use util::Error;
|
@@ -98,3 +102,53 @@ fn test_generate_key_signature() -> Result<(), Error> {
|
98 | 102 |
|
99 | 103 | Ok(())
|
100 | 104 | }
|
| 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