Skip to content

Commit 8510a42

Browse files
committed
add len-prefix to msg hash
1 parent e4f6a7a commit 8510a42

File tree

3 files changed

+32
-18
lines changed

3 files changed

+32
-18
lines changed

contracts/programs/keystone-forwarder/src/lib.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,13 +196,24 @@ pub mod keystone_forwarder {
196196
let signatures: &[u8] = &data[..total_signature_len];
197197
// raw_report | report context
198198
let data = &data[total_signature_len..];
199-
let hashed_report = hash::hash(data).to_bytes();
199+
200+
// Build the preimage the same way the OCR keyring does:
201+
// SHA256( [u8(len(raw_report))] || raw_report || ctx)
202+
let mut preimage = vec![0u8; 1 + data.len()];
203+
204+
let raw_report_len = data.len() - REPORT_CONTEXT_LEN;
205+
// OCR keyring also does not error on overflow
206+
let raw_report_len_u8: u8 = raw_report_len as u8;
207+
208+
preimage[0] = raw_report_len_u8;
209+
preimage[1..].copy_from_slice(data);
210+
211+
let hashed_report = hash::hash(&preimage).to_bytes();
200212

201213
verify_signatures(&hashed_report, signatures, &oracles_config, num_signatures)?;
202214

203215
// slice raw_report from the report context
204-
let raw_report_end = data.len() - REPORT_CONTEXT_LEN;
205-
let raw_report = &data[..raw_report_end];
216+
let raw_report = &data[..raw_report_len];
206217

207218
let transmission_id =
208219
extract_transmission_id(raw_report, ctx.accounts.receiver_program.key);

contracts/tests/keystone_forwarder.spec.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -527,16 +527,6 @@ describe("keystone_storage", function () {
527527
program.programId
528528
);
529529

530-
// const actualState = await program.account.forwarderState.fetch(
531-
// forwarderState.publicKey
532-
// );
533-
534-
// DELETE ME assert.equal(
535-
// actualState.authorityNonce,
536-
// forwarderAuthorityBump,
537-
// "forwarder authority PDA bumps should be equal"
538-
// );
539-
540530
// begin initializing the receiver program
541531

542532
await receiverProgram.methods
@@ -582,7 +572,7 @@ describe("keystone_storage", function () {
582572
Buffer.from([255])
583573
);
584574

585-
// metadata length + actual report payload length (todo change)
575+
// metadata length + actual report payload length
586576
const rawReportBytes = Buffer.alloc(109 + forwarderReportBuffer.length);
587577

588578
// version offset 0, size 1
@@ -613,15 +603,22 @@ describe("keystone_storage", function () {
613603
rawReportBytes.writeUint8(workflowOwner, 106);
614604
rawReportBytes.writeUint8(reportId, 108);
615605

616-
// payload todo (change to the wrapped forwarder report)
617-
// rawReportBytes.writeUint8(255, 109);
606+
// copies forwarderReportBytes into rawReportBytes
618607
forwarderReportBuffer.copy(rawReportBytes, 109);
619608

620609
// just keep this zero-ed since we don't use it outside of the hash
621610
const reportContextBytes = Buffer.alloc(96);
622611

612+
// the msg to sign includes the prefix of u8(len(rawReportBytes))
613+
const rawReportLenU8 = Buffer.alloc(1);
614+
rawReportLenU8.writeUint8(rawReportBytes.length & 0xff);
615+
616+
console.log("raw report length", rawReportBytes.length);
617+
623618
const msgHashToSign = createHash("sha256")
624-
.update(Buffer.concat([rawReportBytes, reportContextBytes]))
619+
.update(
620+
Buffer.concat([rawReportLenU8, rawReportBytes, reportContextBytes])
621+
)
625622
.digest();
626623

627624
const signaturesInfo = signers.map((s) =>

contracts/tests/utils.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,14 @@ export class Forwarder {
424424
// just keep this zero-ed since we don't use it outside of the hash
425425
const reportContextBytes = Buffer.alloc(96);
426426

427+
// the msg to sign includes the prefix of u8(len(rawReportBytes))
428+
const rawReportLenU8 = Buffer.alloc(1);
429+
rawReportLenU8.writeUint8(rawReportBytes.length & 0xff);
430+
427431
const msgHashToSign = createHash("sha256")
428-
.update(Buffer.concat([rawReportBytes, reportContextBytes]))
432+
.update(
433+
Buffer.concat([rawReportLenU8, rawReportBytes, reportContextBytes])
434+
)
429435
.digest();
430436

431437
const signaturesInfo = reportSigners.map((s) =>

0 commit comments

Comments
 (0)