Skip to content

Commit 9f1a7c8

Browse files
authored
Refactors verify_hashes_with_sei slightly (#469)
Moves out some of the logic that should be handled generically.
1 parent d3de8a0 commit 9f1a7c8

File tree

1 file changed

+30
-53
lines changed

1 file changed

+30
-53
lines changed

lib/src/sv_auth.c

Lines changed: 30 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -576,66 +576,41 @@ verify_hashes_with_hash_list(signed_video_t *self,
576576
* to reflect the total number of expected and received BUs.
577577
*/
578578
static bool
579-
verify_hashes_with_sei(signed_video_t *self,
580-
bu_list_item_t *sei,
581-
int *num_expected,
582-
int *num_received)
579+
verify_hashes_with_sei(signed_video_t *self, bu_list_item_t *sei)
583580
{
584-
assert(self);
585-
586-
int num_expected_hashes = -1;
587-
int num_received_hashes = -1;
588-
char validation_status = 'P';
581+
assert(self && sei);
589582

583+
bu_list_t *bu_list = self->bu_list;
590584
bool sei_is_maybe_ok =
591585
(!sei->bu->is_signed || (sei->bu->is_signed && sei->verified_signature == 1));
592-
bool gop_is_ok = verify_gop_hash(self);
593-
bool order_ok = verify_linked_hash(self);
594-
// If the order is correct, the SEI is for sure in sync.
595-
self->validation_flags.sei_in_sync |= order_ok;
596-
597-
// The content of the SEI can only be trusted and used if the signature was verified
598-
// successfully. If not, mark GOP as not OK.
599-
if (sei_is_maybe_ok) {
600-
validation_status = (gop_is_ok && order_ok) ? '.' : 'N';
601-
num_expected_hashes = (int)self->gop_info->num_sent;
602-
// If the signature is verified but GOP hash or the linked hash is not, continue validation with
603-
// the hash list if it is present.
604-
if (validation_status != '.' && self->gop_info->list_idx > 0) {
605-
// Extend partial GOP with more items, since the failure can be due to added BUs.
606-
extend_partial_gop(self, sei);
607-
return verify_hashes_with_hash_list(self, sei, num_expected, num_received, order_ok);
586+
bool gop_hash_ok = verify_gop_hash(self);
587+
bool linked_hash_ok = verify_linked_hash(self);
588+
self->validation_flags.sei_in_sync |= linked_hash_ok;
589+
// For complete and successful validation both the GOP hash and the linked hash have
590+
// to be correct (given that the signature could be verified successfully of course).
591+
// If the gop hash could not be verified correct, there is a second chance by
592+
// verifying individual hashes, if a hash list was sent in the SEI.
593+
bool verify_success = gop_hash_ok && sei_is_maybe_ok;
594+
if (linked_hash_ok && !gop_hash_ok && self->gop_info->list_idx > 0) {
595+
// If the GOP hash could not successfully be verified and a hash list was
596+
// transmitted in the SEI, verify individual hashes.
597+
DEBUG_LOG("GOP hash could not be verified. Verifying individual hashes.");
598+
// Associate more items, since the failure can be due to added Bitstream Units.
599+
extend_partial_gop(self, sei);
600+
// verify_indiviual_hashes(self, sei);
601+
verify_hashes_with_hash_list(self, sei, NULL, NULL, true);
602+
if (sei->bu->is_signed) {
603+
// If the SEI is signed mark previous GOPs if there are any.
604+
mark_associated_items(bu_list, true, linked_hash_ok, sei);
608605
}
609606
} else {
610-
validation_status = sei->tmp_validation_status;
611-
// An error occurred when verifying the GOP hash. Verify without a SEI.
612-
if (validation_status == 'E') {
613-
remove_sei_association(self->bu_list, sei);
614-
return verify_hashes_without_sei(self, 0);
615-
}
607+
int num_expected = self->gop_info->num_sent;
608+
int num_received = self->tmp_num_in_partial_gop;
609+
bu_list_add_missing_items_at_end_of_partial_gop(bu_list, num_expected - num_received, sei);
610+
mark_associated_items(bu_list, verify_success, linked_hash_ok, sei);
616611
}
617612

618-
// Identify the first BU used in the GOP hash. This will be used to add missing BUs.
619-
bu_list_item_t *first_gop_hash_item = self->bu_list->first_item;
620-
while (first_gop_hash_item && (first_gop_hash_item->associated_sei != sei)) {
621-
first_gop_hash_item = first_gop_hash_item->next;
622-
}
623-
// Number of received hashes equals the number used when computing the GOP hash.
624-
num_received_hashes = self->tmp_num_in_partial_gop;
625-
mark_associated_items(self->bu_list, validation_status == '.', order_ok, sei);
626-
627-
if (!self->validation_flags.is_first_validation && first_gop_hash_item) {
628-
int num_missing = num_expected_hashes - num_received_hashes;
629-
const bool append = first_gop_hash_item->bu->is_first_bu_in_gop;
630-
// No need to check the return value. A failure only affects the statistics. In the worst case
631-
// we may signal SV_AUTH_RESULT_OK instead of SV_AUTH_RESULT_OK_WITH_MISSING_INFO.
632-
bu_list_add_missing(self->bu_list, num_missing, append, first_gop_hash_item, sei);
633-
}
634-
635-
if (num_expected) *num_expected = num_expected_hashes;
636-
if (num_received) *num_received = num_received_hashes;
637-
638-
return true;
613+
return verify_success;
639614
}
640615

641616
/* Verifying hashes without the SEI means that we have nothing to verify against. Therefore, we mark
@@ -792,14 +767,16 @@ validate_authenticity(signed_video_t *self, bu_list_item_t *sei)
792767
verify_success = verify_hashes_without_sei(self, 0);
793768
num_expected = -1;
794769
} else {
795-
verify_success = verify_hashes_with_sei(self, sei, &num_expected, &num_received);
770+
verify_success = verify_hashes_with_sei(self, sei);
796771
}
797772

798773
// Collect statistics from the bu_list. This is used to validate the GOP and provide additional
799774
// information to the user.
800775
bool has_valid_bu = bu_list_get_stats(self->bu_list, sei, &num_invalid, &num_missed);
801776
DEBUG_LOG("Number of invalid Bitstream Units = %d.", num_invalid);
802777
DEBUG_LOG("Number of missed Bitstream Units = %d.", num_missed);
778+
// Update the counted Bitstream Units part of this validation, since it may have changed.
779+
num_received = self->tmp_num_in_partial_gop;
803780

804781
valid = (num_invalid > 0) ? SV_AUTH_RESULT_NOT_OK : SV_AUTH_RESULT_OK;
805782

0 commit comments

Comments
 (0)