Skip to content

Commit d75542b

Browse files
authored
dtls: refactor cipher suite error handling with ok_or() (#694)
Replace verbose if-let-else patterns with concise ok_or() calls for better code readability and consistency across all cipher suite implementations. Changes: - CipherSuiteAes128Ccm: simplify encrypt/decrypt methods - CipherSuiteAes128GcmSha256: simplify encrypt/decrypt methods - CipherSuiteAes256CbcSha: simplify encrypt/decrypt methods - CipherSuiteChaCha20Poly1305Sha256: simplify encrypt/decrypt methods - CipherSuiteTlsPskWithAes128GcmSha256: simplify encrypt/decrypt methods Signed-off-by: Xiaobo Liu <cppcoffee@gmail.com>
1 parent f6cf722 commit d75542b

5 files changed

+40
-70
lines changed

dtls/src/cipher_suite/cipher_suite_aes_128_ccm.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,22 +97,16 @@ impl CipherSuite for CipherSuiteAes128Ccm {
9797
}
9898

9999
fn encrypt(&self, pkt_rlh: &RecordLayerHeader, raw: &[u8]) -> Result<Vec<u8>> {
100-
if let Some(ccm) = &self.ccm {
101-
ccm.encrypt(pkt_rlh, raw)
102-
} else {
103-
Err(Error::Other(
104-
"CipherSuite has not been initialized, unable to encrypt".to_owned(),
105-
))
106-
}
100+
let ccm = self.ccm.as_ref().ok_or(Error::Other(
101+
"CipherSuite has not been initialized, unable to encrypt".to_owned(),
102+
))?;
103+
ccm.encrypt(pkt_rlh, raw)
107104
}
108105

109106
fn decrypt(&self, input: &[u8]) -> Result<Vec<u8>> {
110-
if let Some(ccm) = &self.ccm {
111-
ccm.decrypt(input)
112-
} else {
113-
Err(Error::Other(
114-
"CipherSuite has not been initialized, unable to decrypt".to_owned(),
115-
))
116-
}
107+
let ccm = self.ccm.as_ref().ok_or(Error::Other(
108+
"CipherSuite has not been initialized, unable to decrypt".to_owned(),
109+
))?;
110+
ccm.decrypt(input)
117111
}
118112
}

dtls/src/cipher_suite/cipher_suite_aes_128_gcm_sha256.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -92,22 +92,16 @@ impl CipherSuite for CipherSuiteAes128GcmSha256 {
9292
}
9393

9494
fn encrypt(&self, pkt_rlh: &RecordLayerHeader, raw: &[u8]) -> Result<Vec<u8>> {
95-
if let Some(cg) = &self.gcm {
96-
cg.encrypt(pkt_rlh, raw)
97-
} else {
98-
Err(Error::Other(
99-
"CipherSuite has not been initialized, unable to encrypt".to_owned(),
100-
))
101-
}
95+
let cg = self.gcm.as_ref().ok_or(Error::Other(
96+
"CipherSuite has not been initialized, unable to encrypt".to_owned(),
97+
))?;
98+
cg.encrypt(pkt_rlh, raw)
10299
}
103100

104101
fn decrypt(&self, input: &[u8]) -> Result<Vec<u8>> {
105-
if let Some(cg) = &self.gcm {
106-
cg.decrypt(input)
107-
} else {
108-
Err(Error::Other(
109-
"CipherSuite has not been initialized, unable to decrypt".to_owned(),
110-
))
111-
}
102+
let cg = self.gcm.as_ref().ok_or(Error::Other(
103+
"CipherSuite has not been initialized, unable to decrypt".to_owned(),
104+
))?;
105+
cg.decrypt(input)
112106
}
113107
}

dtls/src/cipher_suite/cipher_suite_aes_256_cbc_sha.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -92,22 +92,16 @@ impl CipherSuite for CipherSuiteAes256CbcSha {
9292
}
9393

9494
fn encrypt(&self, pkt_rlh: &RecordLayerHeader, raw: &[u8]) -> Result<Vec<u8>> {
95-
if let Some(cg) = &self.cbc {
96-
cg.encrypt(pkt_rlh, raw)
97-
} else {
98-
Err(Error::Other(
99-
"CipherSuite has not been initialized, unable to encrypt".to_owned(),
100-
))
101-
}
95+
let cg = self.cbc.as_ref().ok_or(Error::Other(
96+
"CipherSuite has not been initialized, unable to encrypt".to_owned(),
97+
))?;
98+
cg.encrypt(pkt_rlh, raw)
10299
}
103100

104101
fn decrypt(&self, input: &[u8]) -> Result<Vec<u8>> {
105-
if let Some(cg) = &self.cbc {
106-
cg.decrypt(input)
107-
} else {
108-
Err(Error::Other(
109-
"CipherSuite has not been initialized, unable to decrypt".to_owned(),
110-
))
111-
}
102+
let cg = self.cbc.as_ref().ok_or(Error::Other(
103+
"CipherSuite has not been initialized, unable to decrypt".to_owned(),
104+
))?;
105+
cg.decrypt(input)
112106
}
113107
}

dtls/src/cipher_suite/cipher_suite_chacha20_poly1305_sha256.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -92,22 +92,16 @@ impl CipherSuite for CipherSuiteChaCha20Poly1305Sha256 {
9292
}
9393

9494
fn encrypt(&self, pkt_rlh: &RecordLayerHeader, raw: &[u8]) -> Result<Vec<u8>> {
95-
if let Some(cg) = &self.cipher {
96-
cg.encrypt(pkt_rlh, raw)
97-
} else {
98-
Err(Error::Other(
99-
"CipherSuite has not been initialized, unable to encrypt".to_owned(),
100-
))
101-
}
95+
let cg = self.cipher.as_ref().ok_or(Error::Other(
96+
"CipherSuite has not been initialized, unable to encrypt".to_owned(),
97+
))?;
98+
cg.encrypt(pkt_rlh, raw)
10299
}
103100

104101
fn decrypt(&self, input: &[u8]) -> Result<Vec<u8>> {
105-
if let Some(cg) = &self.cipher {
106-
cg.decrypt(input)
107-
} else {
108-
Err(Error::Other(
109-
"CipherSuite has not been initialized, unable to decrypt".to_owned(),
110-
))
111-
}
102+
let cg = self.cipher.as_ref().ok_or(Error::Other(
103+
"CipherSuite has not been initialized, unable to decrypt".to_owned(),
104+
))?;
105+
cg.decrypt(input)
112106
}
113107
}

dtls/src/cipher_suite/cipher_suite_tls_psk_with_aes_128_gcm_sha256.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,16 @@ impl CipherSuite for CipherSuiteTlsPskWithAes128GcmSha256 {
7575
}
7676

7777
fn encrypt(&self, pkt_rlh: &RecordLayerHeader, raw: &[u8]) -> Result<Vec<u8>> {
78-
if let Some(cg) = &self.gcm {
79-
cg.encrypt(pkt_rlh, raw)
80-
} else {
81-
Err(Error::Other(
82-
"CipherSuite has not been initialized, unable to encrypt".to_owned(),
83-
))
84-
}
78+
let cg = self.gcm.as_ref().ok_or(Error::Other(
79+
"CipherSuite has not been initialized, unable to encrypt".to_owned(),
80+
))?;
81+
cg.encrypt(pkt_rlh, raw)
8582
}
8683

8784
fn decrypt(&self, input: &[u8]) -> Result<Vec<u8>> {
88-
if let Some(cg) = &self.gcm {
89-
cg.decrypt(input)
90-
} else {
91-
Err(Error::Other(
92-
"CipherSuite has not been initialized, unable to decrypt".to_owned(),
93-
))
94-
}
85+
let cg = self.gcm.as_ref().ok_or(Error::Other(
86+
"CipherSuite has not been initialized, unable to decrypt".to_owned(),
87+
))?;
88+
cg.decrypt(input)
9589
}
9690
}

0 commit comments

Comments
 (0)