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

feat(crypto): Make box_size parameter on c2pa_crypto::cose::sign an Option #879

Merged
merged 1 commit into from
Jan 23, 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
24 changes: 14 additions & 10 deletions internal/crypto/src/cose/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@
#[async_generic(async_signature(
signer: &dyn AsyncRawSigner,
data: &[u8],
box_size: usize,
box_size: Option<usize>,
tss: TimeStampStorage
))]
pub fn sign(
signer: &dyn RawSigner,
data: &[u8],
box_size: usize,
box_size: Option<usize>,
tss: TimeStampStorage,
) -> Result<Vec<u8>, CoseError> {
if _sync {
Expand All @@ -101,13 +101,13 @@
#[async_generic(async_signature(
signer: &dyn AsyncRawSigner,
data: &[u8],
box_size: usize,
box_size: Option<usize>,
tss: TimeStampStorage
))]
pub fn sign_v1(
signer: &dyn RawSigner,
data: &[u8],
box_size: usize,
box_size: Option<usize>,
tss: TimeStampStorage,
) -> Result<Vec<u8>, CoseError> {
let alg = signer.alg();
Expand Down Expand Up @@ -171,13 +171,13 @@
#[async_generic(async_signature(
signer: &dyn AsyncRawSigner,
data: &[u8],
box_size: usize,
box_size: Option<usize>,
tss: TimeStampStorage
))]
pub fn sign_v2(
signer: &dyn RawSigner,
data: &[u8],
box_size: usize,
box_size: Option<usize>,

Check warning on line 180 in internal/crypto/src/cose/sign.rs

View check run for this annotation

Codecov / codecov/patch

internal/crypto/src/cose/sign.rs#L180

Added line #L180 was not covered by tests
tss: TimeStampStorage,
) -> Result<Vec<u8>, CoseError> {
let alg = signer.alg();
Expand Down Expand Up @@ -283,7 +283,7 @@
Ok(ph2)
}

#[async_generic(async_signature(signer: &dyn AsyncRawSigner, data: &[u8], p_header: &ProtectedHeader, tss: TimeStampStorage,))]
#[async_generic(async_signature(signer: &dyn AsyncRawSigner, data: &[u8], p_header: &ProtectedHeader, tss: TimeStampStorage,))]
fn build_unprotected_header(
signer: &dyn RawSigner,
data: &[u8],
Expand Down Expand Up @@ -332,13 +332,17 @@
// when that happens a second padding is added to change the remaining needed
// padding. The default initial guess works for almost all sizes, without the
// need for additional loops.
fn pad_cose_sig(sign1: &mut CoseSign1, end_size: usize) -> Result<Vec<u8>, CoseError> {
fn pad_cose_sig(sign1: &mut CoseSign1, end_size: Option<usize>) -> Result<Vec<u8>, CoseError> {
let mut sign1_clone = sign1.clone();

let cur_vec = sign1_clone
.to_tagged_vec()
.map_err(|e| CoseError::CborGenerationError(e.to_string()))?;

let Some(end_size) = end_size else {
return Ok(cur_vec);

Check warning on line 343 in internal/crypto/src/cose/sign.rs

View check run for this annotation

Codecov / codecov/patch

internal/crypto/src/cose/sign.rs#L343

Added line #L343 was not covered by tests
};

let cur_size = cur_vec.len();
if cur_size == end_size {
return Ok(cur_vec);
Expand Down Expand Up @@ -375,7 +379,7 @@
Label::Text(PAD.to_string()),
Value::Bytes(vec![0u8; target_guess]),
));
return pad_cose_sig(&mut sign1_clone, end_size);
return pad_cose_sig(&mut sign1_clone, Some(end_size));
}

// Get current CBOR vec to see if we reached target size.
Expand All @@ -397,5 +401,5 @@
Value::Bytes(vec![0u8; last_pad - 10]),
));

pad_cose_sig(sign1, end_size)
pad_cose_sig(sign1, Some(end_size))

Check warning on line 404 in internal/crypto/src/cose/sign.rs

View check run for this annotation

Codecov / codecov/patch

internal/crypto/src/cose/sign.rs#L404

Added line #L404 was not covered by tests
}
8 changes: 4 additions & 4 deletions sdk/src/cose_sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,20 +115,20 @@ pub(crate) fn cose_sign(

if _sync {
match signer.raw_signer() {
Some(raw_signer) => Ok(sign(*raw_signer, data, box_size, time_stamp_storage)?),
Some(raw_signer) => Ok(sign(*raw_signer, data, Some(box_size), time_stamp_storage)?),
None => {
let wrapper = SignerWrapper(signer);
Ok(sign(&wrapper, data, box_size, time_stamp_storage)?)
Ok(sign(&wrapper, data, Some(box_size), time_stamp_storage)?)
}
}
} else {
match signer.async_raw_signer() {
Some(raw_signer) => {
Ok(sign_async(*raw_signer, data, box_size, time_stamp_storage).await?)
Ok(sign_async(*raw_signer, data, Some(box_size), time_stamp_storage).await?)
}
None => {
let wrapper = AsyncSignerWrapper(signer);
Ok(sign_async(&wrapper, data, box_size, time_stamp_storage).await?)
Ok(sign_async(&wrapper, data, Some(box_size), time_stamp_storage).await?)
}
}
}
Expand Down
Loading