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

Add support for raw RSA signatures. #127

Merged
merged 2 commits into from
Dec 11, 2024
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
227 changes: 99 additions & 128 deletions src/ossl/rsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,24 @@ impl RsaPKCSOperation {
}
}

fn get_key_size(key: &Object, info: &CK_MECHANISM_INFO) -> Result<usize> {
let modulus = key.get_attr_as_bytes(CKA_MODULUS)?;
let modulus_bits: CK_ULONG = modulus.len() as CK_ULONG * 8;
if modulus_bits < info.ulMinKeySize
|| (info.ulMaxKeySize != 0 && modulus_bits > info.ulMaxKeySize)
{
return Err(CKR_KEY_SIZE_RANGE)?;
}
Ok(modulus.len())
}

fn max_message_len(
modulus: usize,
mech: CK_MECHANISM_TYPE,
hash: CK_MECHANISM_TYPE,
) -> Result<usize> {
match mech {
CKM_RSA_X_509 => Ok(modulus),
CKM_RSA_PKCS => Ok(modulus - 11),
CKM_RSA_PKCS_OAEP => {
let hs = Self::hash_len(hash)?;
Expand All @@ -243,30 +255,23 @@ impl RsaPKCSOperation {
}
}

pub fn encrypt_new(
fn encdec_new(
mech: &CK_MECHANISM,
key: &Object,
info: &CK_MECHANISM_INFO,
pubkey: Option<EvpPkey>,
privkey: Option<EvpPkey>,
keysize: usize,
) -> Result<RsaPKCSOperation> {
let modulus = key.get_attr_as_bytes(CKA_MODULUS)?;
let modulus_bits: CK_ULONG = modulus.len() as CK_ULONG * 8;
if modulus_bits < info.ulMinKeySize
|| (info.ulMaxKeySize != 0 && modulus_bits > info.ulMaxKeySize)
{
return Err(CKR_KEY_SIZE_RANGE)?;
}
let oaep_params = parse_oaep_params(mech)?;
let pubkey = EvpPkey::pubkey_from_object(key)?;
Ok(RsaPKCSOperation {
mech: mech.mechanism,
max_input: Self::max_message_len(
modulus.len(),
keysize,
mech.mechanism,
oaep_params.hash,
)?,
output_len: modulus.len(),
public_key: Some(pubkey),
private_key: None,
output_len: keysize,
public_key: pubkey,
private_key: privkey,
finalized: false,
in_use: false,
sigctx: None,
Expand All @@ -277,71 +282,49 @@ impl RsaPKCSOperation {
})
}

pub fn decrypt_new(
pub fn encrypt_new(
mech: &CK_MECHANISM,
key: &Object,
info: &CK_MECHANISM_INFO,
) -> Result<RsaPKCSOperation> {
let modulus = key.get_attr_as_bytes(CKA_MODULUS)?;
let modulus_bits: CK_ULONG = modulus.len() as CK_ULONG * 8;
if modulus_bits < info.ulMinKeySize
|| (info.ulMaxKeySize != 0 && modulus_bits > info.ulMaxKeySize)
{
return Err(CKR_KEY_SIZE_RANGE)?;
}
let oaep_params = parse_oaep_params(mech)?;
let keysize = Self::get_key_size(key, info)?;
let pubkey = EvpPkey::pubkey_from_object(key)?;
let privkey = EvpPkey::privkey_from_object(key)?;
Ok(RsaPKCSOperation {
mech: mech.mechanism,
max_input: modulus.len(),
output_len: Self::max_message_len(
modulus.len(),
mech.mechanism,
oaep_params.hash,
)?,
public_key: Some(pubkey),
private_key: Some(privkey),
finalized: false,
in_use: false,
sigctx: None,
pss: no_pss_params(),
oaep: oaep_params,
#[cfg(feature = "fips")]
fips_approved: None,
})
Self::encdec_new(mech, Some(pubkey), None, keysize)
}

pub fn sign_new(
pub fn decrypt_new(
mech: &CK_MECHANISM,
key: &Object,
info: &CK_MECHANISM_INFO,
) -> Result<RsaPKCSOperation> {
let modulus = key.get_attr_as_bytes(CKA_MODULUS)?;
let modulus_bits: CK_ULONG = modulus.len() as CK_ULONG * 8;
if modulus_bits < info.ulMinKeySize
|| (info.ulMaxKeySize != 0 && modulus_bits > info.ulMaxKeySize)
{
return Err(CKR_KEY_SIZE_RANGE)?;
}

let pss_params = parse_pss_params(mech)?;
let keysize = Self::get_key_size(key, info)?;
let pubkey = EvpPkey::pubkey_from_object(key)?;
let privkey = EvpPkey::privkey_from_object(key)?;
Self::encdec_new(mech, Some(pubkey), Some(privkey), keysize)
}

fn sigver_new(
mech: &CK_MECHANISM,
pubkey: Option<EvpPkey>,
privkey: Option<EvpPkey>,
keysize: usize,
) -> Result<RsaPKCSOperation> {
let pss_params = parse_pss_params(mech)?;
Ok(RsaPKCSOperation {
mech: mech.mechanism,
max_input: match mech.mechanism {
CKM_RSA_PKCS => modulus.len() - 11,
CKM_RSA_X_509 => keysize,
CKM_RSA_PKCS => keysize - 11,
CKM_RSA_PKCS_PSS => Self::hash_len(pss_params.hash)?,
_ => 0,
},
output_len: modulus.len(),
public_key: Some(pubkey),
private_key: Some(privkey),
output_len: keysize,
public_key: pubkey,
private_key: privkey,
finalized: false,
in_use: false,
sigctx: match mech.mechanism {
CKM_RSA_PKCS => None,
CKM_RSA_X_509 | CKM_RSA_PKCS => None,
#[cfg(feature = "fips")]
_ => Some(ProviderSignatureCtx::new(name_as_char(RSA_NAME))?),
#[cfg(not(feature = "fips"))]
Expand All @@ -354,44 +337,25 @@ impl RsaPKCSOperation {
})
}

pub fn verify_new(
pub fn sign_new(
mech: &CK_MECHANISM,
key: &Object,
info: &CK_MECHANISM_INFO,
) -> Result<RsaPKCSOperation> {
let modulus = key.get_attr_as_bytes(CKA_MODULUS)?;
let modulus_bits: CK_ULONG = modulus.len() as CK_ULONG * 8;
if modulus_bits < info.ulMinKeySize
|| (info.ulMaxKeySize != 0 && modulus_bits > info.ulMaxKeySize)
{
return Err(CKR_KEY_SIZE_RANGE)?;
}
let keysize = Self::get_key_size(key, info)?;
let pubkey = EvpPkey::pubkey_from_object(key)?;
let privkey = EvpPkey::privkey_from_object(key)?;
Self::sigver_new(mech, Some(pubkey), Some(privkey), keysize)
}

let pss_params = parse_pss_params(mech)?;
pub fn verify_new(
mech: &CK_MECHANISM,
key: &Object,
info: &CK_MECHANISM_INFO,
) -> Result<RsaPKCSOperation> {
let keysize = Self::get_key_size(key, info)?;
let pubkey = EvpPkey::pubkey_from_object(key)?;
Ok(RsaPKCSOperation {
mech: mech.mechanism,
max_input: match mech.mechanism {
CKM_RSA_PKCS => modulus.len() - 11,
_ => 0,
},
output_len: modulus.len(),
public_key: Some(pubkey),
private_key: None,
finalized: false,
in_use: false,
sigctx: match mech.mechanism {
CKM_RSA_PKCS => None,
simo5 marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(feature = "fips")]
_ => Some(ProviderSignatureCtx::new(name_as_char(RSA_NAME))?),
#[cfg(not(feature = "fips"))]
_ => Some(EvpMdCtx::new()?),
},
pss: pss_params,
oaep: no_oaep_params(),
#[cfg(feature = "fips")]
fips_approved: None,
})
Self::sigver_new(mech, Some(pubkey), None, keysize)
}

pub fn generate_keypair(
Expand Down Expand Up @@ -849,10 +813,10 @@ impl Sign for RsaPKCSOperation {
return Err(CKR_OPERATION_NOT_INITIALIZED)?;
}
match self.mech {
CKM_RSA_PKCS | CKM_RSA_PKCS_PSS => {
CKM_RSA_X_509 | CKM_RSA_PKCS | CKM_RSA_PKCS_PSS => {
self.finalized = true;
if match self.mech {
CKM_RSA_PKCS => data.len() > self.max_input,
CKM_RSA_X_509 | CKM_RSA_PKCS => data.len() > self.max_input,
CKM_RSA_PKCS_PSS => data.len() != self.max_input,
_ => return Err(CKR_GENERAL_ERROR)?,
} {
Expand Down Expand Up @@ -921,8 +885,11 @@ impl Sign for RsaPKCSOperation {
return Err(CKR_OPERATION_NOT_INITIALIZED)?;
}
if !self.in_use {
if self.mech == CKM_RSA_PKCS || self.mech == CKM_RSA_PKCS_PSS {
return Err(CKR_OPERATION_NOT_INITIALIZED)?;
match self.mech {
CKM_RSA_X_509 | CKM_RSA_PKCS | CKM_RSA_PKCS_PSS => {
return Err(CKR_OPERATION_NOT_INITIALIZED)?;
}
_ => (),
}
self.in_use = true;

Expand Down Expand Up @@ -1023,43 +990,47 @@ impl Verify for RsaPKCSOperation {
if self.finalized {
return Err(CKR_OPERATION_NOT_INITIALIZED)?;
}
if self.mech == CKM_RSA_PKCS {
self.finalized = true;
if data.len() > self.max_input {
return Err(CKR_DATA_LEN_RANGE)?;
}
if signature.len() != self.output_len {
return Err(CKR_GENERAL_ERROR)?;
}
let mut ctx = some_or_err!(mut self.public_key).new_ctx()?;
let res = unsafe { EVP_PKEY_verify_init(ctx.as_mut_ptr()) };
if res != 1 {
return Err(CKR_DEVICE_ERROR)?;
}
let params = self.rsa_sig_params();
let res = unsafe {
EVP_PKEY_CTX_set_params(ctx.as_mut_ptr(), params.as_ptr())
};
if res != 1 {
return Err(CKR_DEVICE_ERROR)?;
}
match self.mech {
CKM_RSA_X_509 | CKM_RSA_PKCS | CKM_RSA_PKCS_PSS => {
self.finalized = true;
if data.len() > self.max_input {
return Err(CKR_DATA_LEN_RANGE)?;
}
if signature.len() != self.output_len {
return Err(CKR_GENERAL_ERROR)?;
}
let mut ctx = some_or_err!(mut self.public_key).new_ctx()?;
let res = unsafe { EVP_PKEY_verify_init(ctx.as_mut_ptr()) };
if res != 1 {
return Err(CKR_DEVICE_ERROR)?;
}
let params = self.rsa_sig_params();
let res = unsafe {
EVP_PKEY_CTX_set_params(ctx.as_mut_ptr(), params.as_ptr())
};
if res != 1 {
return Err(CKR_DEVICE_ERROR)?;
}

let res = unsafe {
EVP_PKEY_verify(
ctx.as_mut_ptr(),
signature.as_ptr(),
signature.len(),
data.as_ptr(),
data.len(),
)
};
if res != 1 {
return Err(CKR_SIGNATURE_INVALID)?;
let res = unsafe {
EVP_PKEY_verify(
ctx.as_mut_ptr(),
signature.as_ptr(),
signature.len(),
data.as_ptr(),
data.len(),
)
};
if res != 1 {
return Err(CKR_SIGNATURE_INVALID)?;
}
Ok(())
}
_ => {
self.verify_update(data)?;
self.verify_final(signature)
}
return Ok(());
}
self.verify_update(data)?;
self.verify_final(signature)
}

fn verify_update(&mut self, data: &[u8]) -> Result<()> {
Expand Down
25 changes: 13 additions & 12 deletions src/rsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,18 +374,19 @@ impl RsaPKCSMechanism {
}

pub fn register_mechanisms(mechs: &mut Mechanisms) {
mechs.add_mechanism(
CKM_RSA_PKCS,
Self::new_mechanism(
CKF_ENCRYPT
| CKF_DECRYPT
| CKF_SIGN
| CKF_VERIFY
| CKF_WRAP
| CKF_UNWRAP,
),
);

for ckm in &[CKM_RSA_X_509, CKM_RSA_PKCS] {
mechs.add_mechanism(
*ckm,
Self::new_mechanism(
CKF_ENCRYPT
| CKF_DECRYPT
| CKF_SIGN
| CKF_VERIFY
| CKF_WRAP
| CKF_UNWRAP,
),
);
}
for ckm in &[
CKM_SHA1_RSA_PKCS,
CKM_SHA224_RSA_PKCS,
Expand Down
Loading