Skip to content

Commit 2f3cf22

Browse files
author
Yiming Lin
committed
Use LcPtr
1 parent f3094b5 commit 2f3cf22

File tree

2 files changed

+22
-48
lines changed

2 files changed

+22
-48
lines changed

aws-lc-rs/src/cmac.rs

Lines changed: 19 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,12 @@
9292
//! [NIST SP 800-38B]: https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-38b.pdf
9393
9494
use crate::aws_lc::{
95-
CMAC_CTX_copy, CMAC_CTX_free, CMAC_CTX_new, CMAC_Final, CMAC_Init,
95+
CMAC_CTX_copy, CMAC_CTX_new, CMAC_Final, CMAC_Init,
9696
CMAC_Update, EVP_aes_128_cbc, EVP_aes_192_cbc, EVP_aes_256_cbc, EVP_des_ede3_cbc, CMAC_CTX,
9797
};
9898
use crate::error::Unspecified;
9999
use crate::fips::indicator_check;
100+
use crate::ptr::LcPtr;
100101
use crate::{constant_time, rand};
101102
use core::mem::MaybeUninit;
102103
use core::ptr::null_mut;
@@ -187,46 +188,6 @@ impl AsRef<[u8]> for Tag {
187188
}
188189
}
189190

190-
struct LcCmacCtx(*mut CMAC_CTX);
191-
192-
impl LcCmacCtx {
193-
fn new() -> Result<Self, Unspecified> {
194-
let ptr = unsafe { CMAC_CTX_new() };
195-
if ptr.is_null() {
196-
return Err(Unspecified);
197-
}
198-
Ok(LcCmacCtx(ptr))
199-
}
200-
201-
fn as_mut_ptr(&mut self) -> *mut CMAC_CTX {
202-
self.0
203-
}
204-
205-
fn try_clone(&self) -> Result<Self, Unspecified> {
206-
let new_ctx = Self::new()?;
207-
unsafe {
208-
if 1 != CMAC_CTX_copy(new_ctx.0, self.0) {
209-
return Err(Unspecified);
210-
}
211-
}
212-
Ok(new_ctx)
213-
}
214-
}
215-
216-
unsafe impl Send for LcCmacCtx {}
217-
218-
impl Drop for LcCmacCtx {
219-
fn drop(&mut self) {
220-
unsafe { CMAC_CTX_free(self.0) }
221-
}
222-
}
223-
224-
impl Clone for LcCmacCtx {
225-
fn clone(&self) -> Self {
226-
self.try_clone().expect("Unable to clone LcCmacCtx")
227-
}
228-
}
229-
230191
/// A key to use for CMAC signing.
231192
//
232193
// # FIPS
@@ -236,7 +197,19 @@ impl Clone for LcCmacCtx {
236197
#[derive(Clone)]
237198
pub struct Key {
238199
algorithm: Algorithm,
239-
ctx: LcCmacCtx,
200+
ctx: LcPtr<CMAC_CTX>,
201+
}
202+
203+
impl Clone for LcPtr<CMAC_CTX> {
204+
fn clone(&self) -> Self {
205+
let mut new_ctx = LcPtr::new(unsafe { CMAC_CTX_new() }).expect("CMAC_CTX_new failed");
206+
unsafe {
207+
if 1 != CMAC_CTX_copy(*new_ctx.as_mut(), *self.as_const()) {
208+
panic!("CMAC_CTX_copy failed");
209+
}
210+
}
211+
new_ctx
212+
}
240213
}
241214

242215
unsafe impl Send for Key {}
@@ -292,12 +265,12 @@ impl Key {
292265
return Err(Unspecified);
293266
}
294267

295-
let mut ctx = LcCmacCtx::new()?;
268+
let mut ctx = LcPtr::new(unsafe { CMAC_CTX_new() })?;
296269

297270
unsafe {
298271
let cipher = (algorithm.cipher_fn)();
299272
if 1 != CMAC_Init(
300-
ctx.as_mut_ptr(),
273+
*ctx.as_mut(),
301274
key_value.as_ptr().cast(),
302275
key_value.len(),
303276
cipher,
@@ -366,7 +339,7 @@ impl Context {
366339
#[inline]
367340
fn try_update(&mut self, data: &[u8]) -> Result<(), Unspecified> {
368341
unsafe {
369-
if 1 != CMAC_Update(self.key.ctx.as_mut_ptr(), data.as_ptr(), data.len()) {
342+
if 1 != CMAC_Update(*self.key.ctx.as_mut(), data.as_ptr(), data.len()) {
370343
return Err(Unspecified);
371344
}
372345
}
@@ -402,7 +375,7 @@ impl Context {
402375

403376
unsafe {
404377
if 1 != indicator_check!(CMAC_Final(
405-
self.key.ctx.as_mut_ptr(),
378+
*self.key.ctx.as_mut(),
406379
output.as_mut_ptr(),
407380
out_len.as_mut_ptr(),
408381
)) {

aws-lc-rs/src/ptr.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
// SPDX-License-Identifier: Apache-2.0 OR ISC
33

44
use crate::aws_lc::{
5-
BN_free, ECDSA_SIG_free, EC_GROUP_free, EC_KEY_free, EC_POINT_free, EVP_AEAD_CTX_free,
5+
BN_free, CMAC_CTX_free, ECDSA_SIG_free, EC_GROUP_free, EC_KEY_free, EC_POINT_free, EVP_AEAD_CTX_free,
66
EVP_CIPHER_CTX_free, EVP_PKEY_CTX_free, EVP_PKEY_free, OPENSSL_free, RSA_free, BIGNUM,
7-
ECDSA_SIG, EC_GROUP, EC_KEY, EC_POINT, EVP_AEAD_CTX, EVP_CIPHER_CTX, EVP_PKEY, EVP_PKEY_CTX,
7+
CMAC_CTX, ECDSA_SIG, EC_GROUP, EC_KEY, EC_POINT, EVP_AEAD_CTX, EVP_CIPHER_CTX, EVP_PKEY, EVP_PKEY_CTX,
88
RSA,
99
};
1010
use core::ops::Deref;
@@ -271,6 +271,7 @@ create_pointer!(EVP_PKEY_CTX, EVP_PKEY_CTX_free);
271271
create_pointer!(RSA, RSA_free);
272272
create_pointer!(EVP_AEAD_CTX, EVP_AEAD_CTX_free);
273273
create_pointer!(EVP_CIPHER_CTX, EVP_CIPHER_CTX_free);
274+
create_pointer!(CMAC_CTX, CMAC_CTX_free);
274275

275276
#[cfg(test)]
276277
mod tests {

0 commit comments

Comments
 (0)