Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Padding bug mismatch #144

Merged
merged 2 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 29 additions & 13 deletions src/ossl/aes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -749,12 +749,20 @@ impl AesOperation {
if res != 1 {
return Err(self.op_err(CKR_DEVICE_ERROR))?;
}
if self.mech == CKM_AES_CBC_PAD {
let res =
unsafe { EVP_CIPHER_CTX_set_padding(self.ctx.as_mut_ptr(), 1) };
if res != 1 {
return Err(self.op_err(CKR_DEVICE_ERROR));
}
/* OpenSSL defaults to padding on, so we need to turn it explicitly
* off for mechanisms that do not use it because PKCS#11 does not
* implicitly provide padding. */
let res = match self.mech {
CKM_AES_ECB | CKM_AES_CBC => unsafe {
EVP_CIPHER_CTX_set_padding(self.ctx.as_mut_ptr(), 0)
},
CKM_AES_CBC_PAD => unsafe {
EVP_CIPHER_CTX_set_padding(self.ctx.as_mut_ptr(), 1)
},
_ => 1,
};
if res != 1 {
return Err(self.op_err(CKR_DEVICE_ERROR));
}

if self.mech == CKM_AES_CCM {
Expand Down Expand Up @@ -850,11 +858,14 @@ impl AesOperation {
if res != 1 {
return Err(self.op_err(CKR_DEVICE_ERROR))?;
}
let res = unsafe {
EVP_CIPHER_CTX_set_padding(
self.ctx.as_mut_ptr(),
if self.mech == CKM_AES_CBC_PAD { 1 } else { 0 },
)
let res = match self.mech {
CKM_AES_ECB | CKM_AES_CBC => unsafe {
EVP_CIPHER_CTX_set_padding(self.ctx.as_mut_ptr(), 0)
},
CKM_AES_CBC_PAD => unsafe {
EVP_CIPHER_CTX_set_padding(self.ctx.as_mut_ptr(), 1)
},
_ => 1,
};
if res != 1 {
return Err(self.op_err(CKR_DEVICE_ERROR));
Expand Down Expand Up @@ -1594,10 +1605,10 @@ impl Encryption for AesOperation {
}
}
}
CKM_AES_CTS | CKM_AES_CBC | CKM_AES_ECB => (),
CKM_AES_CTS => (),
#[cfg(not(feature = "fips"))]
CKM_AES_CFB8 | CKM_AES_CFB1 | CKM_AES_CFB128 | CKM_AES_OFB => (),
CKM_AES_CBC_PAD => {
CKM_AES_ECB | CKM_AES_CBC | CKM_AES_CBC_PAD => {
/* check if this is a second call after
* we saved the final buffer */
if self.finalbuf.len() > 0 {
Expand Down Expand Up @@ -1628,6 +1639,11 @@ impl Encryption for AesOperation {
)
};
if res != 1 {
if self.mech != CKM_AES_CBC_PAD {
/* For raw ECB and CBC mode this most likely means there
* was data left in the internal OpenSSL buffer */
return Err(self.op_err(CKR_DATA_LEN_RANGE));
}
return Err(self.op_err(CKR_DEVICE_ERROR));
}
outlen = usize::try_from(outl)?;
Expand Down
17 changes: 17 additions & 0 deletions src/tests/aes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,23 @@ fn test_aes_operations() {
));
assert_eq!(dec.len(), data.len());
assert_eq!(data.as_bytes(), dec.as_slice());

/* AES CBC negative test */
let data = "short";
let iv = "FEDCBA0987654321";
err_or_panic!(
encrypt(
session,
handle,
data.as_bytes(),
&CK_MECHANISM {
mechanism: CKM_AES_CBC,
pParameter: void_ptr!(iv.as_bytes()),
ulParameterLen: iv.len() as CK_ULONG,
}
),
CKR_DATA_LEN_RANGE
);
}

{
Expand Down