void check_initial_pcrs()

in nsm-test/src/main.cc [188:260]


void check_initial_pcrs(int32_t ctx, NsmDescription &description)
{
    size_t expected_pcr_len = get_pcr_len(description);
    std::vector<uint8_t> zeroed_pcr(expected_pcr_len, 0);
    std::vector<PcrData> pcr_data;

    // Get the descriptions of all PCRs.
    for (uint16_t index = 0; index < description.max_pcrs; ++index) {
        PcrData single_data;

        get_pcr_description(ctx, index, expected_pcr_len, single_data);
        pcr_data.push_back(single_data);
    }

    printf("Checked Request::DescribePCR for PCRs [0..%u).\n", description.max_pcrs);

    // PCRs [0..3) must not be empty (shund contain non-zero bytes).
    for (uint16_t index = 0; index < 3; ++index)
        if (pcr_data[index].data == zeroed_pcr) {
            fprintf(stderr, "[Error] PCR %u must not be empty.\n", index);
            exit(-1);
        }

    printf("Checked that PCRs [0..3) are not empty.\n");

    // All other PCRs should be empty.
    for (uint16_t index = 3; index < description.max_pcrs; ++index) {
        if (index == 4) {
	    // PCR4 is mapped to the parent instance-id and is not null
	    if (pcr_data[index].data == zeroed_pcr) {
                fprintf(stderr, "[Error] PCR %u must not be empty.\n", index);
                exit(-1);
            }
            continue;
        }
	if (pcr_data[index].data != zeroed_pcr) {
            fprintf(stderr, "[Error] PCR %u must be empty.\n", index);
            exit(-1);
        }
    }

    printf("Checked that PCRs [3..%u) are empty.\n", description.max_pcrs);

    // PCRs [0..16) should all be locked.
    if (description.locked_pcrs_len != 16) {
        fprintf(stderr, "[Error] Initial locked PCR list is invalid.\n");
        exit(-1);
    }

    // The list of locked PCRs from the NSM description should match [0..16).
    for (uint16_t index = 0; index < 16; ++index)
        if (description.locked_pcrs[index] != index) {
            fprintf(stderr, "[Error] Initial locked PCR list is invalid.\n");
            exit(-1);
        }

    // The PCRs [0..16) themselves should report being locked.
    for (uint16_t index = 0; index < 16; ++index)
        if (!pcr_data[index].lock) {
            fprintf(stderr, "[Error] PCR %u must be locked.\n", index);
            exit(-1);
        }

    // The rest of the PCRs should all be unlocked.
    for (uint16_t index = 16; index < description.max_pcrs; ++index)
        if (pcr_data[index].lock) {
            fprintf(stderr, "[Error] PCR %u must not be locked.\n", index);
            exit(-1);
        }

    printf("Checked that PCRs [0..16) are locked and [16..%u) are not locked.\n",
        description.max_pcrs);
}