Skip to content

Commit 00263a1

Browse files
feat(crypto): Make box_size parameter on c2pa_crypto::cose::sign an Option (#879)
Signing via CAWG identity SDK for X.509 credentials does the padding at a different stage so I need to disable it here.
1 parent 06c57a5 commit 00263a1

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

internal/crypto/src/cose/sign.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@ use crate::{
7676
#[async_generic(async_signature(
7777
signer: &dyn AsyncRawSigner,
7878
data: &[u8],
79-
box_size: usize,
79+
box_size: Option<usize>,
8080
tss: TimeStampStorage
8181
))]
8282
pub fn sign(
8383
signer: &dyn RawSigner,
8484
data: &[u8],
85-
box_size: usize,
85+
box_size: Option<usize>,
8686
tss: TimeStampStorage,
8787
) -> Result<Vec<u8>, CoseError> {
8888
if _sync {
@@ -101,13 +101,13 @@ pub fn sign(
101101
#[async_generic(async_signature(
102102
signer: &dyn AsyncRawSigner,
103103
data: &[u8],
104-
box_size: usize,
104+
box_size: Option<usize>,
105105
tss: TimeStampStorage
106106
))]
107107
pub fn sign_v1(
108108
signer: &dyn RawSigner,
109109
data: &[u8],
110-
box_size: usize,
110+
box_size: Option<usize>,
111111
tss: TimeStampStorage,
112112
) -> Result<Vec<u8>, CoseError> {
113113
let alg = signer.alg();
@@ -171,13 +171,13 @@ pub fn sign_v1(
171171
#[async_generic(async_signature(
172172
signer: &dyn AsyncRawSigner,
173173
data: &[u8],
174-
box_size: usize,
174+
box_size: Option<usize>,
175175
tss: TimeStampStorage
176176
))]
177177
pub fn sign_v2(
178178
signer: &dyn RawSigner,
179179
data: &[u8],
180-
box_size: usize,
180+
box_size: Option<usize>,
181181
tss: TimeStampStorage,
182182
) -> Result<Vec<u8>, CoseError> {
183183
let alg = signer.alg();
@@ -283,7 +283,7 @@ fn build_protected_header(
283283
Ok(ph2)
284284
}
285285

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

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

342+
let Some(end_size) = end_size else {
343+
return Ok(cur_vec);
344+
};
345+
342346
let cur_size = cur_vec.len();
343347
if cur_size == end_size {
344348
return Ok(cur_vec);
@@ -375,7 +379,7 @@ fn pad_cose_sig(sign1: &mut CoseSign1, end_size: usize) -> Result<Vec<u8>, CoseE
375379
Label::Text(PAD.to_string()),
376380
Value::Bytes(vec![0u8; target_guess]),
377381
));
378-
return pad_cose_sig(&mut sign1_clone, end_size);
382+
return pad_cose_sig(&mut sign1_clone, Some(end_size));
379383
}
380384

381385
// Get current CBOR vec to see if we reached target size.
@@ -397,5 +401,5 @@ fn pad_cose_sig(sign1: &mut CoseSign1, end_size: usize) -> Result<Vec<u8>, CoseE
397401
Value::Bytes(vec![0u8; last_pad - 10]),
398402
));
399403

400-
pad_cose_sig(sign1, end_size)
404+
pad_cose_sig(sign1, Some(end_size))
401405
}

sdk/src/cose_sign.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,20 +115,20 @@ pub(crate) fn cose_sign(
115115

116116
if _sync {
117117
match signer.raw_signer() {
118-
Some(raw_signer) => Ok(sign(*raw_signer, data, box_size, time_stamp_storage)?),
118+
Some(raw_signer) => Ok(sign(*raw_signer, data, Some(box_size), time_stamp_storage)?),
119119
None => {
120120
let wrapper = SignerWrapper(signer);
121-
Ok(sign(&wrapper, data, box_size, time_stamp_storage)?)
121+
Ok(sign(&wrapper, data, Some(box_size), time_stamp_storage)?)
122122
}
123123
}
124124
} else {
125125
match signer.async_raw_signer() {
126126
Some(raw_signer) => {
127-
Ok(sign_async(*raw_signer, data, box_size, time_stamp_storage).await?)
127+
Ok(sign_async(*raw_signer, data, Some(box_size), time_stamp_storage).await?)
128128
}
129129
None => {
130130
let wrapper = AsyncSignerWrapper(signer);
131-
Ok(sign_async(&wrapper, data, box_size, time_stamp_storage).await?)
131+
Ok(sign_async(&wrapper, data, Some(box_size), time_stamp_storage).await?)
132132
}
133133
}
134134
}

0 commit comments

Comments
 (0)