Пример #1
0
static void tpm_clear_tests(void)
{
	/* No clear request */
	reset_common_data();
	TEST_SUCC(vb2_check_tpm_clear(&cc), "no clear request");
	TEST_EQ(mock_tpm_clear_called, 0, "tpm not cleared");

	/* Successful request */
	reset_common_data();
	vb2_nv_set(&cc, VB2_NV_CLEAR_TPM_OWNER_REQUEST, 1);
	TEST_SUCC(vb2_check_tpm_clear(&cc), "clear request");
	TEST_EQ(vb2_nv_get(&cc, VB2_NV_CLEAR_TPM_OWNER_REQUEST),
		0, "request cleared");
	TEST_EQ(vb2_nv_get(&cc, VB2_NV_CLEAR_TPM_OWNER_DONE),
		1, "done set");
	TEST_EQ(mock_tpm_clear_called, 1, "tpm cleared");

	/* Failed request */
	reset_common_data();
	mock_tpm_clear_retval = VB2_ERROR_EX_TPM_CLEAR_OWNER;
	vb2_nv_set(&cc, VB2_NV_CLEAR_TPM_OWNER_REQUEST, 1);
	TEST_EQ(vb2_check_tpm_clear(&cc),
		VB2_ERROR_EX_TPM_CLEAR_OWNER, "clear failure");
	TEST_EQ(vb2_nv_get(&cc, VB2_NV_CLEAR_TPM_OWNER_REQUEST),
		0, "request cleared");
	TEST_EQ(vb2_nv_get(&cc, VB2_NV_CLEAR_TPM_OWNER_DONE),
		0, "done not set");
}
/**
 * Test valid and invalid signatures.
 */
static void test_signatures(const struct vb2_public_key *key)
{
	uint8_t workbuf[VB2_VERIFY_DIGEST_WORKBUF_BYTES]
		 __attribute__ ((aligned (VB2_WORKBUF_ALIGN)));
	uint8_t sig[RSA1024NUMBYTES];
	struct vb2_workbuf wb;
	int unexpected_success;
	int i;

	vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));

	/* The first test signature is valid. */
	Memcpy(sig, signatures[0], sizeof(sig));
	TEST_SUCC(vb2_rsa_verify_digest(key, sig, test_message_sha1_hash, &wb),
		  "RSA Padding Test valid sig");

	/* All other signatures should fail verification. */
	unexpected_success = 0;
	for (i = 1; i < sizeof(signatures) / sizeof(signatures[0]); i++) {
		Memcpy(sig, signatures[i], sizeof(sig));
		if (!vb2_rsa_verify_digest(key, sig,
					   test_message_sha1_hash, &wb)) {
			fprintf(stderr,
				"RSA Padding Test vector %d FAILED!\n", i);
			unexpected_success++;
		}
	}
	TEST_EQ(unexpected_success, 0, "RSA Padding Test invalid sigs");
}
Пример #3
0
static void get_kernel_size_tests(void)
{
	uint32_t offs, size;

	reset_common_data(FOR_PHASE2);
	offs = size = 0;
	TEST_SUCC(vb2api_get_kernel_size(&cc, &offs, &size), "get size good");
	TEST_EQ(offs, 0x10000, "  offset");
	TEST_EQ(size, sizeof(kernel_data), "  size");

	/* Don't need to pass pointers */
	reset_common_data(FOR_PHASE2);
	TEST_SUCC(vb2api_get_kernel_size(&cc, NULL, NULL), "get size null");

	reset_common_data(FOR_PHASE2);
	sd->workbuf_preamble_size = 0;
	TEST_EQ(vb2api_get_kernel_size(&cc, &offs, &size),
		VB2_ERROR_API_GET_KERNEL_SIZE_PREAMBLE,
		"get size no preamble");
}
Пример #4
0
static void test_verify_keyblock(const struct vb2_public_key *public_key,
				const struct vb2_private_key *private_key,
				const struct vb2_packed_key *data_key)
{
	uint8_t workbuf[VB2_KEY_BLOCK_VERIFY_WORKBUF_BYTES]
		__attribute__ ((aligned (VB2_WORKBUF_ALIGN)));
	struct vb2_workbuf wb;
	struct vb2_keyblock *hdr;
	struct vb2_keyblock *h;
	uint32_t hsize;

	vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));

	hdr = vb2_create_keyblock(data_key, private_key, 0x1234);
	TEST_NEQ((size_t)hdr, 0, "vb2_verify_keyblock() prerequisites");
	if (!hdr)
		return;
	hsize = hdr->keyblock_size;
	h = (struct vb2_keyblock *)malloc(hsize + 2048);

	memcpy(h, hdr, hsize);
	TEST_SUCC(vb2_verify_keyblock(h, hsize, public_key, &wb),
		  "vb2_verify_keyblock() ok using key");

	/* Failures in keyblock check also cause verify to fail */
	memcpy(h, hdr, hsize);
	TEST_EQ(vb2_verify_keyblock(h, hsize - 1, public_key, &wb),
		VB2_ERROR_KEYBLOCK_SIZE, "vb2_verify_keyblock() check");

	/* Check signature */
	memcpy(h, hdr, hsize);
	h->keyblock_signature.sig_size--;
	resign_keyblock(h, private_key);
	TEST_EQ(vb2_verify_keyblock(h, hsize, public_key, &wb),
		VB2_ERROR_KEYBLOCK_SIG_INVALID,
		"vb2_verify_keyblock() sig too small");

	memcpy(h, hdr, hsize);
	((uint8_t *)vb2_packed_key_data(&h->data_key))[0] ^= 0x34;
	TEST_EQ(vb2_verify_keyblock(h, hsize, public_key, &wb),
		VB2_ERROR_KEYBLOCK_SIG_INVALID,
		"vb2_verify_keyblock() sig mismatch");

	/*
	 * TODO: verify parser can support a bigger header (i.e., one where
	 * data_key.key_offset is bigger than expected).
	 */

	free(h);
	free(hdr);
}
Пример #5
0
static void phase3_tests(void)
{
	uint32_t v;

	reset_common_data(FOR_PHASE3);
	TEST_SUCC(vb2api_kernel_phase3(&cc), "phase3 good");
	vb2_secdatak_get(&cc, VB2_SECDATAK_VERSIONS, &v);
	TEST_EQ(v, 0x20004, "  version");

	reset_common_data(FOR_PHASE3);
	sd->kernel_version = 0x20001;
	TEST_SUCC(vb2api_kernel_phase3(&cc), "phase3 no rollback");
	vb2_secdatak_get(&cc, VB2_SECDATAK_VERSIONS, &v);
	TEST_EQ(v, 0x20002, "  version");

	reset_common_data(FOR_PHASE3);
	sd->flags &= ~VB2_SD_FLAG_KERNEL_SIGNED;
	TEST_SUCC(vb2api_kernel_phase3(&cc), "phase3 unsigned kernel");
	vb2_secdatak_get(&cc, VB2_SECDATAK_VERSIONS, &v);
	TEST_EQ(v, 0x20002, "  version");

	reset_common_data(FOR_PHASE3);
	cc.flags |= VB2_CONTEXT_RECOVERY_MODE;
	TEST_SUCC(vb2api_kernel_phase3(&cc), "phase3 recovery");
	vb2_secdatak_get(&cc, VB2_SECDATAK_VERSIONS, &v);
	TEST_EQ(v, 0x20002, "  version");

	reset_common_data(FOR_PHASE3);
	cc.flags &= ~VB2_CONTEXT_ALLOW_KERNEL_ROLL_FORWARD;
	TEST_SUCC(vb2api_kernel_phase3(&cc), "phase3 no rollforward");
	vb2_secdatak_get(&cc, VB2_SECDATAK_VERSIONS, &v);
	TEST_EQ(v, 0x20002, "  version");

	reset_common_data(FOR_PHASE3);
	sd->status &= ~VB2_SD_STATUS_SECDATAK_INIT;
	TEST_EQ(vb2api_kernel_phase3(&cc),
		VB2_ERROR_SECDATAK_SET_UNINITIALIZED, "phase3 set fail");
}
Пример #6
0
static void load_kernel_vblock_tests(void)
{
	reset_common_data(FOR_PHASE1);
	TEST_SUCC(vb2api_load_kernel_vblock(&cc), "load vblock good");

	reset_common_data(FOR_PHASE1);
	mock_load_kernel_keyblock_retval = VB2_ERROR_MOCK;
	TEST_EQ(vb2api_load_kernel_vblock(&cc), VB2_ERROR_MOCK,
		"load vblock bad keyblock");

	reset_common_data(FOR_PHASE1);
	mock_load_kernel_preamble_retval = VB2_ERROR_MOCK;
	TEST_EQ(vb2api_load_kernel_vblock(&cc), VB2_ERROR_MOCK,
		"load vblock bad preamble");
}
Пример #7
0
static void verify_kernel_data_tests(void)
{
	reset_common_data(FOR_PHASE2);
	TEST_SUCC(vb2api_verify_kernel_data(&cc, kernel_data, sizeof(kernel_data)),
		  "verify data good");

	reset_common_data(FOR_PHASE2);
	sd->workbuf_preamble_size = 0;
	TEST_EQ(vb2api_verify_kernel_data(&cc, kernel_data, sizeof(kernel_data)),
		VB2_ERROR_API_VERIFY_KDATA_PREAMBLE, "verify no preamble");

	reset_common_data(FOR_PHASE2);
	TEST_EQ(vb2api_verify_kernel_data(&cc, kernel_data, sizeof(kernel_data) + 1),
		VB2_ERROR_API_VERIFY_KDATA_SIZE, "verify size");

	reset_common_data(FOR_PHASE2);
	cc.workbuf_used = cc.workbuf_size - sizeof(struct vb2_digest_context) + 1;
	TEST_EQ(vb2api_verify_kernel_data(&cc, kernel_data, sizeof(kernel_data)),
		VB2_ERROR_API_VERIFY_KDATA_WORKBUF, "verify workbuf");

	reset_common_data(FOR_PHASE2);
	sd->workbuf_data_key_size = 0;
	TEST_EQ(vb2api_verify_kernel_data(&cc, kernel_data, sizeof(kernel_data)),
		VB2_ERROR_API_VERIFY_KDATA_KEY, "verify no key");

	reset_common_data(FOR_PHASE2);
	mock_unpack_key_retval = VB2_ERROR_MOCK;
	TEST_EQ(vb2api_verify_kernel_data(&cc, kernel_data, sizeof(kernel_data)),
		VB2_ERROR_MOCK, "verify unpack key");

	reset_common_data(FOR_PHASE2);
	kdkey->algorithm = VB2_ALG_COUNT;
	TEST_EQ(vb2api_verify_kernel_data(&cc, kernel_data, sizeof(kernel_data)),
		VB2_ERROR_SHA_INIT_ALGORITHM, "verify hash init");

	reset_common_data(FOR_PHASE2);
	cc.workbuf_used = cc.workbuf_size - sizeof(struct vb2_digest_context) - 4;
	TEST_EQ(vb2api_verify_kernel_data(&cc, kernel_data, sizeof(kernel_data)),
		VB2_ERROR_API_CHECK_HASH_WORKBUF_DIGEST, "verify hash workbuf");

	reset_common_data(FOR_PHASE2);
	kernel_data[3] ^= 0xd0;
	TEST_EQ(vb2api_verify_kernel_data(&cc, kernel_data, sizeof(kernel_data)),
		VB2_ERROR_VDATA_VERIFY_DIGEST, "verify hash digest");
	kernel_data[3] ^= 0xd0;
}
/**
 * Test other error conditions in vb2_rsa_verify_digest().
 */
static void test_verify_digest(struct vb2_public_key *key) {
	uint8_t workbuf[VB2_VERIFY_DIGEST_WORKBUF_BYTES]
		 __attribute__ ((aligned (VB2_WORKBUF_ALIGN)));
	uint8_t sig[RSA1024NUMBYTES];
	struct vb2_workbuf wb;
	enum vb2_signature_algorithm orig_key_alg = key->sig_alg;

	vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));

	Memcpy(sig, signatures[0], sizeof(sig));
	TEST_SUCC(vb2_rsa_verify_digest(key, sig, test_message_sha1_hash, &wb),
		  "vb2_rsa_verify_digest() good");

	Memcpy(sig, signatures[0], sizeof(sig));
	vb2_workbuf_init(&wb, workbuf, sizeof(sig) * 3 - 1);
	TEST_EQ(vb2_rsa_verify_digest(key, sig, test_message_sha1_hash, &wb),
		VB2_ERROR_RSA_VERIFY_WORKBUF,
		"vb2_rsa_verify_digest() small workbuf");
	vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));

	key->sig_alg = VB2_SIG_INVALID;
	Memcpy(sig, signatures[0], sizeof(sig));
	TEST_EQ(vb2_rsa_verify_digest(key, sig, test_message_sha1_hash, &wb),
		VB2_ERROR_RSA_VERIFY_ALGORITHM,
		"vb2_rsa_verify_digest() bad key alg");
	key->sig_alg = orig_key_alg;

	key->arrsize *= 2;
	Memcpy(sig, signatures[0], sizeof(sig));
	TEST_EQ(vb2_rsa_verify_digest(key, sig, test_message_sha1_hash, &wb),
		VB2_ERROR_RSA_VERIFY_SIG_LEN,
		"vb2_rsa_verify_digest() bad sig len");
	key->arrsize /= 2;

	/* Corrupt the signature near start and end */
	Memcpy(sig, signatures[0], sizeof(sig));
	sig[3] ^= 0x42;
	TEST_EQ(vb2_rsa_verify_digest(key, sig, test_message_sha1_hash, &wb),
		VB2_ERROR_RSA_PADDING, "vb2_rsa_verify_digest() bad sig");

	Memcpy(sig, signatures[0], sizeof(sig));
	sig[RSA1024NUMBYTES - 3] ^= 0x56;
	TEST_EQ(vb2_rsa_verify_digest(key, sig, test_message_sha1_hash, &wb),
		VB2_ERROR_RSA_PADDING, "vb2_rsa_verify_digest() bad sig end");
}
Пример #9
0
void sha256_tests(void)
{
	uint8_t digest[VB2_SHA256_DIGEST_SIZE];
	uint8_t *test_inputs[3];
	int i;

	test_inputs[0] = (uint8_t *) oneblock_msg;
	test_inputs[1] = (uint8_t *) multiblock_msg1;
	test_inputs[2] = (uint8_t *) long_msg;

	for (i = 0; i < 3; i++) {
		TEST_SUCC(vb2_digest(test_inputs[i],
				     strlen((char *)test_inputs[i]),
				     VB2_HASH_SHA256, digest, sizeof(digest)),
			  "vb2_digest() SHA256");
		TEST_EQ(memcmp(digest, sha256_results[i], sizeof(digest)),
			0, "SHA-256 digest");
	}

	TEST_EQ(vb2_digest(test_inputs[0], strlen((char *)test_inputs[0]),
			   VB2_HASH_SHA256, digest, sizeof(digest) - 1),
		VB2_ERROR_SHA_FINALIZE_DIGEST_SIZE, "vb2_digest() too small");
}
Пример #10
0
static void verify_preamble_tests(void)
{
	struct vb2_fw_preamble *pre = &mock_vblock.p.pre;
	int wb_used_before;
	uint32_t v;

	/* Test successful call */
	reset_common_data(FOR_PREAMBLE);
	wb_used_before = cc.workbuf_used;
	TEST_SUCC(vb2_load_fw_preamble(&cc), "preamble good");
	TEST_EQ(sd->fw_version, 0x20002, "combined version");
	TEST_EQ(sd->workbuf_preamble_offset,
		(wb_used_before + (VB2_WORKBUF_ALIGN - 1)) &
		~(VB2_WORKBUF_ALIGN - 1),
		"preamble offset");
	TEST_EQ(sd->workbuf_preamble_size, pre->preamble_size, "preamble size");
	TEST_EQ(cc.workbuf_used,
		sd->workbuf_preamble_offset + sd->workbuf_preamble_size,
		"workbuf used");

	/* Expected failures */
	reset_common_data(FOR_PREAMBLE);
	sd->workbuf_data_key_size = 0;
	TEST_EQ(vb2_load_fw_preamble(&cc),
		VB2_ERROR_FW_PREAMBLE2_DATA_KEY,
		"preamble no data key");

	reset_common_data(FOR_PREAMBLE);
	mock_unpack_key_retval = VB2_ERROR_UNPACK_KEY_HASH_ALGORITHM;
	TEST_EQ(vb2_load_fw_preamble(&cc),
		VB2_ERROR_UNPACK_KEY_HASH_ALGORITHM,
		"preamble unpack data key");

	reset_common_data(FOR_PREAMBLE);
	cc.workbuf_used = cc.workbuf_size - sizeof(struct vb2_fw_preamble) + 8;
	TEST_EQ(vb2_load_fw_preamble(&cc),
		VB2_ERROR_FW_PREAMBLE2_WORKBUF_HEADER,
		"preamble not enough workbuf for header");

	reset_common_data(FOR_PREAMBLE);
	sd->vblock_preamble_offset = sizeof(mock_vblock);
	TEST_EQ(vb2_load_fw_preamble(&cc),
		VB2_ERROR_EX_READ_RESOURCE_SIZE,
		"preamble read header");

	reset_common_data(FOR_PREAMBLE);
	cc.workbuf_used = cc.workbuf_size - sizeof(mock_vblock.p) + 8;
	TEST_EQ(vb2_load_fw_preamble(&cc),
		VB2_ERROR_FW_PREAMBLE2_WORKBUF,
		"preamble not enough workbuf");

	reset_common_data(FOR_PREAMBLE);
	pre->preamble_size = sizeof(mock_vblock);
	TEST_EQ(vb2_load_fw_preamble(&cc),
		VB2_ERROR_EX_READ_RESOURCE_SIZE,
		"preamble read full");

	reset_common_data(FOR_PREAMBLE);
	mock_verify_preamble_retval = VB2_ERROR_PREAMBLE_SIG_INVALID;
	TEST_EQ(vb2_load_fw_preamble(&cc),
		VB2_ERROR_PREAMBLE_SIG_INVALID,
		"preamble verify");

	reset_common_data(FOR_PREAMBLE);
	pre->firmware_version = 0x10000;
	TEST_EQ(vb2_load_fw_preamble(&cc),
		VB2_ERROR_FW_PREAMBLE_VERSION_RANGE,
		"preamble version range");

	reset_common_data(FOR_PREAMBLE);
	pre->firmware_version = 1;
	TEST_EQ(vb2_load_fw_preamble(&cc),
		VB2_ERROR_FW_PREAMBLE_VERSION_ROLLBACK,
		"preamble version rollback");

	reset_common_data(FOR_PREAMBLE);
	pre->firmware_version = 3;
	TEST_SUCC(vb2_load_fw_preamble(&cc),
		  "preamble version roll forward");
	vb2_secdata_get(&cc, VB2_SECDATA_VERSIONS, &v);
	TEST_EQ(v, 0x20003, "roll forward");

	/* Newer version without result success doesn't roll forward */
	reset_common_data(FOR_PREAMBLE);
	pre->firmware_version = 3;
	sd->last_fw_result = VB2_FW_RESULT_UNKNOWN;
	TEST_SUCC(vb2_load_fw_preamble(&cc),
		  "preamble version no roll forward 1");
	vb2_secdata_get(&cc, VB2_SECDATA_VERSIONS, &v);
	TEST_EQ(v, 0x20002, "no roll forward");

	/* Newer version with success but for other slot doesn't roll forward */
	reset_common_data(FOR_PREAMBLE);
	pre->firmware_version = 3;
	sd->last_fw_slot = 1;
	TEST_SUCC(vb2_load_fw_preamble(&cc),
		  "preamble version no roll forward 2");
	vb2_secdata_get(&cc, VB2_SECDATA_VERSIONS, &v);
	TEST_EQ(v, 0x20002, "no roll forward");
}
static void keyblock_tests(const char *keys_dir)
{
	struct vb2_public_key *pubk2048, *pubk4096, *pubk8192, pubkhash;
	struct vb2_private_key *prik4096, *prik8192;
	struct vb2_packed_key *pak, *pakgood;
	struct vb2_keyblock *kb;
	const struct vb2_private_key *prikhash;
	const struct vb2_private_key *prik[2];
	char fname[1024];
	const char test_desc[] = "Test keyblock";

	uint8_t workbuf[VB2_KEY_BLOCK_VERIFY_WORKBUF_BYTES]
		 __attribute__ ((aligned (VB2_WORKBUF_ALIGN)));
	struct vb2_workbuf wb;

	vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));

	/* Read keys */
	sprintf(fname, "%s/key_rsa2048.keyb", keys_dir);
	TEST_SUCC(vb2_public_key_read_keyb(&pubk2048, fname),
					   "Read public key 2");
	vb2_public_key_set_desc(pubk2048, "Test RSA2048 public key");
	pubk2048->hash_alg = VB2_HASH_SHA256;

	sprintf(fname, "%s/key_rsa4096.keyb", keys_dir);
	TEST_SUCC(vb2_public_key_read_keyb(&pubk4096, fname),
					   "Read public key 1");
	vb2_public_key_set_desc(pubk4096, "Test RSA4096 public key");
	pubk4096->hash_alg = VB2_HASH_SHA256;

	sprintf(fname, "%s/key_rsa8192.keyb", keys_dir);
	TEST_SUCC(vb2_public_key_read_keyb(&pubk8192, fname),
					   "Read public key 2");
	vb2_public_key_set_desc(pubk8192, "Test RSA8192 public key");
	pubk8192->hash_alg = VB2_HASH_SHA512;

	sprintf(fname, "%s/key_rsa4096.pem", keys_dir);
	TEST_SUCC(vb2_private_key_read_pem(&prik4096, fname),
		  "Read private key 2");
	vb2_private_key_set_desc(prik4096, "Test RSA4096 private key");
	prik4096->sig_alg = VB2_SIG_RSA4096;
	prik4096->hash_alg = VB2_HASH_SHA256;

	sprintf(fname, "%s/key_rsa8192.pem", keys_dir);
	TEST_SUCC(vb2_private_key_read_pem(&prik8192, fname),
		  "Read private key 1");
	vb2_private_key_set_desc(prik8192, "Test RSA8192 private key");
	prik8192->sig_alg = VB2_SIG_RSA8192;
	prik8192->hash_alg = VB2_HASH_SHA512;

	TEST_SUCC(vb2_private_key_hash(&prikhash, VB2_HASH_SHA512),
		  "Create private hash key");

	TEST_SUCC(vb2_public_key_hash(&pubkhash, VB2_HASH_SHA512),
		  "Create public hash key");

	TEST_SUCC(vb2_public_key_pack(&pakgood, pubk2048), "Test packed key");

	/* Sign a keyblock with one key */
	prik[0] = prik4096;
	TEST_SUCC(vb2_keyblock_create(&kb, pubk2048, prik, 1, 0x1234, NULL),
		  "Keyblock single");
	TEST_PTR_NEQ(kb, NULL, "  kb_ptr");
	TEST_SUCC(vb2_verify_keyblock(kb, kb->c.total_size, pubk4096, &wb),
		  "  verify");
	TEST_EQ(strcmp(vb2_common_desc(kb), pubk2048->desc), 0,	"  desc");
	TEST_EQ(kb->flags, 0x1234, "  flags");

	pak = (struct vb2_packed_key *)((uint8_t *)kb + kb->key_offset);
	TEST_EQ(0, memcmp(pak, pakgood, pakgood->c.total_size), "  data key");
	free(kb);

	/* Sign a keyblock with two keys */
	prik[0] = prik8192;
	prik[1] = prikhash;
	TEST_SUCC(vb2_keyblock_create(&kb, pubk4096, prik, 2, 0, test_desc),
		  "Keyblock multiple");
	TEST_SUCC(vb2_verify_keyblock(kb, kb->c.total_size, pubk8192, &wb),
		  "  verify 1");
	TEST_SUCC(vb2_verify_keyblock(kb, kb->c.total_size, &pubkhash, &wb),
		  "  verify 2");
	TEST_EQ(strcmp(vb2_common_desc(kb), test_desc), 0,	"  desc");
	TEST_EQ(kb->flags, 0, "  flags");
	free(kb);

	/* Test errors */
	prik[0] = prik8192;
	prik8192->hash_alg = VB2_HASH_INVALID;
	TEST_EQ(vb2_keyblock_create(&kb, pubk4096, prik, 1, 0, NULL),
		VB2_KEYBLOCK_CREATE_SIG_SIZE, "Keyblock bad sig size");
	TEST_PTR_EQ(kb, NULL, "  kb_ptr");

	prik[0] = prik4096;
	pubk4096->sig_alg = VB2_SIG_INVALID;
	TEST_EQ(vb2_keyblock_create(&kb, pubk4096, prik, 1, 0, NULL),
		VB2_KEYBLOCK_CREATE_DATA_KEY, "Keyblock bad data key");

	/* Free keys */
	free(pakgood);
	vb2_public_key_free(pubk2048);
	vb2_public_key_free(pubk4096);
	vb2_public_key_free(pubk8192);
	vb2_private_key_free(prik4096);
	vb2_private_key_free(prik8192);
}
Пример #12
0
static void verify_keyblock_tests(void)
{
	struct vb2_keyblock *kb = &mock_vblock.k.kb;
	struct vb2_packed_key *k;
	int wb_used_before;

	/* Test successful call */
	reset_common_data(FOR_KEYBLOCK);
	wb_used_before = cc.workbuf_used;
	TEST_SUCC(vb2_load_fw_keyblock(&cc), "keyblock verify");
	TEST_EQ(sd->fw_version, 0x20000, "keyblock version");
	TEST_EQ(sd->vblock_preamble_offset, sizeof(mock_vblock.k),
		"preamble offset");
	TEST_EQ(sd->workbuf_data_key_offset,
		(wb_used_before + (VB2_WORKBUF_ALIGN - 1)) &
		~(VB2_WORKBUF_ALIGN - 1),
		"keyblock data key offset");
	TEST_EQ(cc.workbuf_used,
		sd->workbuf_data_key_offset + sd->workbuf_data_key_size,
		"workbuf used");

	/* Make sure data key was properly saved */
	k = (struct vb2_packed_key *)(cc.workbuf + sd->workbuf_data_key_offset);
	TEST_EQ(k->algorithm, 7, "data key algorithm");
	TEST_EQ(k->key_version, 2, "data key version");
	TEST_EQ(k->key_size, sizeof(mock_vblock.k.data_key_data),
		"data key size");
	TEST_EQ(memcmp(cc.workbuf + sd->workbuf_data_key_offset +
		       k->key_offset, mock_vblock.k.data_key_data,
		       sizeof(mock_vblock.k.data_key_data)),
		0, "data key data");
	TEST_EQ(cc.workbuf_used,
		sd->workbuf_data_key_offset + sd->workbuf_data_key_size,
		"workbuf used after");

	/* Test failures */
	reset_common_data(FOR_KEYBLOCK);
	cc.workbuf_used = cc.workbuf_size - sd->gbb_rootkey_size + 8;
	TEST_EQ(vb2_load_fw_keyblock(&cc),
		VB2_ERROR_FW_KEYBLOCK_WORKBUF_ROOT_KEY,
		"keyblock not enough workbuf for root key");

	reset_common_data(FOR_KEYBLOCK);
	sd->gbb_rootkey_size = sizeof(mock_gbb);
	TEST_EQ(vb2_load_fw_keyblock(&cc),
		VB2_ERROR_EX_READ_RESOURCE_SIZE,
		"keyblock read root key");

	reset_common_data(FOR_KEYBLOCK);
	mock_unpack_key_retval = VB2_ERROR_UNPACK_KEY_SIG_ALGORITHM;
	TEST_EQ(vb2_load_fw_keyblock(&cc),
		VB2_ERROR_UNPACK_KEY_SIG_ALGORITHM,
		"keyblock unpack root key");

	reset_common_data(FOR_KEYBLOCK);
	cc.workbuf_used = cc.workbuf_size - sd->gbb_rootkey_size - 8;
	TEST_EQ(vb2_load_fw_keyblock(&cc),
		VB2_ERROR_FW_KEYBLOCK_WORKBUF_HEADER,
		"keyblock not enough workbuf for header");

	reset_common_data(FOR_KEYBLOCK);
	mock_read_res_fail_on_call = 2;
	TEST_EQ(vb2_load_fw_keyblock(&cc),
		VB2_ERROR_EX_READ_RESOURCE_INDEX,
		"keyblock read keyblock header");

	reset_common_data(FOR_KEYBLOCK);
	cc.workbuf_used = cc.workbuf_size - sd->gbb_rootkey_size
		- sizeof(struct vb2_keyblock);
	TEST_EQ(vb2_load_fw_keyblock(&cc),
		VB2_ERROR_FW_KEYBLOCK_WORKBUF,
		"keyblock not enough workbuf for entire keyblock");

	reset_common_data(FOR_KEYBLOCK);
	kb->keyblock_size = sizeof(mock_vblock) + 1;
	TEST_EQ(vb2_load_fw_keyblock(&cc),
		VB2_ERROR_EX_READ_RESOURCE_SIZE,
		"keyblock read keyblock");

	reset_common_data(FOR_KEYBLOCK);
	mock_verify_keyblock_retval = VB2_ERROR_KEYBLOCK_MAGIC;
	TEST_EQ(vb2_load_fw_keyblock(&cc),
		VB2_ERROR_KEYBLOCK_MAGIC,
		"keyblock verify keyblock");

	reset_common_data(FOR_KEYBLOCK);
	kb->data_key.key_version = 0x10000;
	TEST_EQ(vb2_load_fw_keyblock(&cc),
		VB2_ERROR_FW_KEYBLOCK_VERSION_RANGE,
		"keyblock version range");

	reset_common_data(FOR_KEYBLOCK);
	kb->data_key.key_version = 1;
	TEST_EQ(vb2_load_fw_keyblock(&cc),
		VB2_ERROR_FW_KEYBLOCK_VERSION_ROLLBACK,
		"keyblock rollback");
}
Пример #13
0
static void test_verify_fw_preamble(const VbPublicKey *public_key,
				    const VbPrivateKey *private_key,
				    const VbPublicKey *kernel_subkey)
{
	struct vb2_fw_preamble *hdr;
	struct vb2_fw_preamble *h;
	struct vb2_public_key rsa;
	uint8_t workbuf[VB2_VERIFY_FIRMWARE_PREAMBLE_WORKBUF_BYTES]
		 __attribute__ ((aligned (VB2_WORKBUF_ALIGN)));
	struct vb2_workbuf wb;
	uint32_t hsize;

	vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));

	/* Create a dummy signature */
	VbSignature *body_sig = SignatureAlloc(56, 78);

	TEST_SUCC(vb2_unpack_key(&rsa, (uint8_t *)public_key,
				 public_key->key_offset + public_key->key_size),
		  "vb2_verify_fw_preamble() prereq key");

	hdr = (struct vb2_fw_preamble *)
		CreateFirmwarePreamble(0x1234, kernel_subkey, body_sig,
				       private_key, 0x5678);
	TEST_PTR_NEQ(hdr, NULL,
		     "VerifyFirmwarePreamble() prereq test preamble");
	if (!hdr)
		return;
	hsize = (uint32_t) hdr->preamble_size;
	h = (struct vb2_fw_preamble *)malloc(hsize + 16384);

	Memcpy(h, hdr, hsize);
	TEST_SUCC(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
		  "vb2_verify_fw_preamble() ok using key");

	Memcpy(h, hdr, hsize);
	TEST_EQ(vb2_verify_fw_preamble(h, 4, &rsa, &wb),
		VB2_ERROR_PREAMBLE_TOO_SMALL_FOR_HEADER,
		"vb2_verify_fw_preamble() size tiny");

	Memcpy(h, hdr, hsize);
	TEST_EQ(vb2_verify_fw_preamble(h, hsize - 1, &rsa, &wb),
		VB2_ERROR_PREAMBLE_SIZE,
		"vb2_verify_fw_preamble() size--");

	/* Buffer is allowed to be bigger than preamble */
	Memcpy(h, hdr, hsize);
	TEST_SUCC(vb2_verify_fw_preamble(h, hsize + 1, &rsa, &wb),
		  "vb2_verify_fw_preamble() size++");

	/* Care about major version but not minor */
	Memcpy(h, hdr, hsize);
	h->header_version_major++;
	resign_fw_preamble(h, private_key);
	TEST_EQ(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
		VB2_ERROR_PREAMBLE_HEADER_VERSION
		, "vb2_verify_fw_preamble() major++");

	Memcpy(h, hdr, hsize);
	h->header_version_major--;
	resign_fw_preamble(h, private_key);
	TEST_EQ(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
		VB2_ERROR_PREAMBLE_HEADER_VERSION,
		"vb2_verify_fw_preamble() major--");

	Memcpy(h, hdr, hsize);
	h->header_version_minor++;
	resign_fw_preamble(h, private_key);
	TEST_SUCC(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
		  "vb2_verify_fw_preamble() minor++");

	Memcpy(h, hdr, hsize);
	h->header_version_minor--;
	resign_fw_preamble(h, private_key);
	TEST_EQ(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
		VB2_ERROR_PREAMBLE_HEADER_OLD,
		"vb2_verify_fw_preamble() 2.0 not supported");

	/* Check signature */
	Memcpy(h, hdr, hsize);
	h->preamble_signature.sig_offset = hsize;
	resign_fw_preamble(h, private_key);
	TEST_EQ(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
		VB2_ERROR_PREAMBLE_SIG_OUTSIDE,
		"vb2_verify_fw_preamble() sig off end");

	Memcpy(h, hdr, hsize);
	h->preamble_signature.sig_size--;
	resign_fw_preamble(h, private_key);
	TEST_EQ(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
		VB2_ERROR_PREAMBLE_SIG_INVALID,
		"vb2_verify_fw_preamble() sig too small");

	Memcpy(h, hdr, hsize);
	((uint8_t *)vb2_packed_key_data(&h->kernel_subkey))[0] ^= 0x34;
	TEST_EQ(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
		VB2_ERROR_PREAMBLE_SIG_INVALID,
		"vb2_verify_fw_preamble() sig mismatch");

	/* Check that we signed header, kernel subkey, and body sig */
	Memcpy(h, hdr, hsize);
	h->preamble_signature.data_size = 4;
	h->kernel_subkey.key_offset = 0;
	h->kernel_subkey.key_size = 0;
	h->body_signature.sig_offset = 0;
	h->body_signature.sig_size = 0;
	resign_fw_preamble(h, private_key);
	TEST_EQ(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
		VB2_ERROR_PREAMBLE_SIGNED_TOO_LITTLE,
		"vb2_verify_fw_preamble() didn't sign header");

	Memcpy(h, hdr, hsize);
	h->kernel_subkey.key_offset = hsize;
	resign_fw_preamble(h, private_key);
	TEST_EQ(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
		VB2_ERROR_PREAMBLE_KERNEL_SUBKEY_OUTSIDE,
		"vb2_verify_fw_preamble() kernel subkey off end");

	Memcpy(h, hdr, hsize);
	h->body_signature.sig_offset = hsize;
	resign_fw_preamble(h, private_key);
	TEST_EQ(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
		VB2_ERROR_PREAMBLE_BODY_SIG_OUTSIDE,
		"vb2_verify_fw_preamble() body sig off end");

	/* TODO: verify with extra padding at end of header. */

	free(h);
	free(hdr);
}
Пример #14
0
static void test_verify_keyblock(const VbPublicKey *public_key,
				 const VbPrivateKey *private_key,
				 const VbPublicKey *data_key)
{
	uint8_t workbuf[VB2_KEY_BLOCK_VERIFY_WORKBUF_BYTES]
		__attribute__ ((aligned (VB2_WORKBUF_ALIGN)));
	struct vb2_workbuf wb;
	struct vb2_public_key key;
	struct vb2_keyblock *hdr;
	struct vb2_keyblock *h;
	uint32_t hsize;

	vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));

	/* Unpack public key */
	TEST_SUCC(vb2_unpack_key(&key, (uint8_t *)public_key,
				 public_key->key_offset + public_key->key_size),
		  "vb2_verify_keyblock public key");

	hdr = (struct vb2_keyblock *)
		KeyBlockCreate(data_key, private_key, 0x1234);
	TEST_NEQ((size_t)hdr, 0, "vb2_verify_keyblock() prerequisites");
	if (!hdr)
		return;
	hsize = hdr->keyblock_size;
	h = (struct vb2_keyblock *)malloc(hsize + 2048);

	Memcpy(h, hdr, hsize);
	TEST_SUCC(vb2_verify_keyblock(h, hsize, &key, &wb),
		  "vb2_verify_keyblock() ok using key");

	Memcpy(h, hdr, hsize);
	TEST_EQ(vb2_verify_keyblock(h, hsize - 1, &key, &wb),
		VB2_ERROR_KEYBLOCK_SIZE, "vb2_verify_keyblock() size--");

	/* Buffer is allowed to be bigger than keyblock */
	Memcpy(h, hdr, hsize);
	TEST_SUCC(vb2_verify_keyblock(h, hsize + 1, &key, &wb),
		  "vb2_verify_keyblock() size++");

	Memcpy(h, hdr, hsize);
	h->magic[0] &= 0x12;
	TEST_EQ(vb2_verify_keyblock(h, hsize, &key, &wb),
		VB2_ERROR_KEYBLOCK_MAGIC, "vb2_verify_keyblock() magic");

	/* Care about major version but not minor */
	Memcpy(h, hdr, hsize);
	h->header_version_major++;
	resign_keyblock(h, private_key);
	TEST_EQ(vb2_verify_keyblock(h, hsize, &key, &wb),
		VB2_ERROR_KEYBLOCK_HEADER_VERSION,
		"vb2_verify_keyblock() major++");

	Memcpy(h, hdr, hsize);
	h->header_version_major--;
	resign_keyblock(h, private_key);
	TEST_EQ(vb2_verify_keyblock(h, hsize, &key, &wb),
		VB2_ERROR_KEYBLOCK_HEADER_VERSION,
		"vb2_verify_keyblock() major--");

	Memcpy(h, hdr, hsize);
	h->header_version_minor++;
	resign_keyblock(h, private_key);
	TEST_SUCC(vb2_verify_keyblock(h, hsize, &key, &wb),
		  "vb2_verify_keyblock() minor++");

	Memcpy(h, hdr, hsize);
	h->header_version_minor--;
	resign_keyblock(h, private_key);
	TEST_SUCC(vb2_verify_keyblock(h, hsize, &key, &wb),
		  "vb2_verify_keyblock() minor--");

	/* Check signature */
	Memcpy(h, hdr, hsize);
	h->keyblock_signature.sig_offset = hsize;
	resign_keyblock(h, private_key);
	TEST_EQ(vb2_verify_keyblock(h, hsize, &key, &wb),
		VB2_ERROR_KEYBLOCK_SIG_OUTSIDE,
		"vb2_verify_keyblock() sig off end");

	Memcpy(h, hdr, hsize);
	h->keyblock_signature.sig_size--;
	resign_keyblock(h, private_key);
	TEST_EQ(vb2_verify_keyblock(h, hsize, &key, &wb),
		VB2_ERROR_KEYBLOCK_SIG_INVALID,
		"vb2_verify_keyblock() sig too small");

	Memcpy(h, hdr, hsize);
	((uint8_t *)vb2_packed_key_data(&h->data_key))[0] ^= 0x34;
	TEST_EQ(vb2_verify_keyblock(h, hsize, &key, &wb),
		VB2_ERROR_KEYBLOCK_SIG_INVALID,
		"vb2_verify_keyblock() sig mismatch");

	Memcpy(h, hdr, hsize);
	h->keyblock_signature.data_size = h->keyblock_size + 1;
	TEST_EQ(vb2_verify_keyblock(h, hsize, &key, &wb),
		VB2_ERROR_KEYBLOCK_SIGNED_TOO_MUCH,
		"vb2_verify_keyblock() sig data past end of block");

	/* Check that we signed header and data key */
	Memcpy(h, hdr, hsize);
	h->keyblock_signature.data_size = 4;
	h->data_key.key_offset = 0;
	h->data_key.key_size = 0;
	resign_keyblock(h, private_key);
	TEST_EQ(vb2_verify_keyblock(h, hsize, &key, &wb),
		VB2_ERROR_KEYBLOCK_SIGNED_TOO_LITTLE,
		"vb2_verify_keyblock() didn't sign header");

	Memcpy(h, hdr, hsize);
	h->data_key.key_offset = hsize;
	resign_keyblock(h, private_key);
	TEST_EQ(vb2_verify_keyblock(h, hsize, &key, &wb),
		VB2_ERROR_KEYBLOCK_DATA_KEY_OUTSIDE,
		"vb2_verify_keyblock() data key off end");

	/* Corner cases for error checking */
	TEST_EQ(vb2_verify_keyblock(NULL, 4, &key, &wb),
		VB2_ERROR_KEYBLOCK_TOO_SMALL_FOR_HEADER,
		"vb2_verify_keyblock size too small");

	/*
	 * TODO: verify parser can support a bigger header (i.e., one where
	 * data_key.key_offset is bigger than expected).
	 */

	free(h);
	free(hdr);
}
Пример #15
0
static void init_context_tests(void)
{
	/* Use our own context struct so we can re-init it */
	struct vb2_context c = {
		.workbuf = workbuf,
		.workbuf_size = sizeof(workbuf),
	};

	reset_common_data();

	TEST_SUCC(vb2_init_context(&c), "Init context good");
	TEST_EQ(c.workbuf_used, sizeof(struct vb2_shared_data),
		"Init vbsd");

	/* Don't re-init if used is non-zero */
	c.workbuf_used = 200;
	TEST_SUCC(vb2_init_context(&c), "Re-init context good");
	TEST_EQ(c.workbuf_used, 200, "Didn't re-init");

	/* Handle workbuf errors */
	c.workbuf_used = 0;
	c.workbuf_size = sizeof(struct vb2_shared_data) - 1;
	TEST_EQ(vb2_init_context(&c),
		VB2_ERROR_INITCTX_WORKBUF_SMALL, "Init too small");
	c.workbuf_size = sizeof(workbuf);

	/* Handle workbuf unaligned */
	c.workbuf++;
	TEST_EQ(vb2_init_context(&c),
		VB2_ERROR_INITCTX_WORKBUF_ALIGN, "Init unaligned");
}

static void misc_tests(void)
{
	struct vb2_workbuf wb;

	reset_common_data();
	cc.workbuf_used = 16;

	vb2_workbuf_from_ctx(&cc, &wb);

	TEST_PTR_EQ(wb.buf, workbuf + 16, "vb_workbuf_from_ctx() buf");
	TEST_EQ(wb.size, cc.workbuf_size - 16, "vb_workbuf_from_ctx() size");
}

static void gbb_tests(void)
{
	struct vb2_gbb_header gbb = {
		.signature = {'$', 'G', 'B', 'B'},
		.major_version = VB2_GBB_MAJOR_VER,
		.minor_version = VB2_GBB_MINOR_VER,
		.header_size = sizeof(struct vb2_gbb_header),
		.flags = 0x1234,
		.rootkey_offset = 240,
		.rootkey_size = 1040,
	};

	struct vb2_gbb_header gbbdest;

	TEST_EQ(sizeof(struct vb2_gbb_header),
		EXPECTED_VB2_GBB_HEADER_SIZE,
		"sizeof(struct vb2_gbb_header)");

	reset_common_data();

	/* Good contents */
	mock_resource_index = VB2_RES_GBB;
	mock_resource_ptr = &gbb;
	mock_resource_size = sizeof(gbb);
	TEST_SUCC(vb2_read_gbb_header(&cc, &gbbdest), "read gbb header good");
	TEST_SUCC(memcmp(&gbb, &gbbdest, sizeof(gbb)), "read gbb contents");

	mock_resource_index = VB2_RES_GBB + 1;
	TEST_EQ(vb2_read_gbb_header(&cc, &gbbdest),
		VB2_ERROR_EX_READ_RESOURCE_INDEX, "read gbb header missing");
	mock_resource_index = VB2_RES_GBB;

	gbb.signature[0]++;
	TEST_EQ(vb2_read_gbb_header(&cc, &gbbdest),
		VB2_ERROR_GBB_MAGIC, "read gbb header bad magic");
	gbb.signature[0]--;

	gbb.major_version = VB2_GBB_MAJOR_VER + 1;
	TEST_EQ(vb2_read_gbb_header(&cc, &gbbdest),
		VB2_ERROR_GBB_VERSION, "read gbb header major version");
	gbb.major_version = VB2_GBB_MAJOR_VER;

	gbb.minor_version = VB2_GBB_MINOR_VER + 1;
	TEST_SUCC(vb2_read_gbb_header(&cc, &gbbdest),
		  "read gbb header minor++");
	gbb.minor_version = 1;
	TEST_EQ(vb2_read_gbb_header(&cc, &gbbdest),
		VB2_ERROR_GBB_TOO_OLD, "read gbb header 1.1 fails");
	gbb.minor_version = 0;
	TEST_EQ(vb2_read_gbb_header(&cc, &gbbdest),
		VB2_ERROR_GBB_TOO_OLD, "read gbb header 1.0 fails");
	gbb.minor_version = VB2_GBB_MINOR_VER;

	gbb.header_size--;
	TEST_EQ(vb2_read_gbb_header(&cc, &gbbdest),
		VB2_ERROR_GBB_HEADER_SIZE, "read gbb header size");
	TEST_EQ(vb2_fw_parse_gbb(&cc),
		VB2_ERROR_GBB_HEADER_SIZE, "parse gbb failure");
	gbb.header_size++;

	/* Parse GBB */
	TEST_SUCC(vb2_fw_parse_gbb(&cc), "parse gbb");
	TEST_EQ(sd->gbb_flags, gbb.flags, "gbb flags");
	TEST_EQ(sd->gbb_rootkey_offset, gbb.rootkey_offset, "rootkey offset");
	TEST_EQ(sd->gbb_rootkey_size, gbb.rootkey_size, "rootkey size");

	/* Workbuf failure */
	reset_common_data();
	cc.workbuf_used = cc.workbuf_size - 4;
	TEST_EQ(vb2_fw_parse_gbb(&cc),
		VB2_ERROR_GBB_WORKBUF, "parse gbb no workbuf");
}

static void fail_tests(void)
{
	/* Early fail (before even NV init) */
	reset_common_data();
	sd->status &= ~VB2_SD_STATUS_NV_INIT;
	vb2_fail(&cc, 1, 2);
	TEST_NEQ(sd->status & VB2_SD_STATUS_NV_INIT, 0, "vb2_fail inits NV");
	TEST_EQ(vb2_nv_get(&cc, VB2_NV_RECOVERY_REQUEST),
		1, "vb2_fail request");
	TEST_EQ(vb2_nv_get(&cc, VB2_NV_RECOVERY_SUBCODE),
		2, "vb2_fail subcode");

	/* Repeated fail doesn't overwrite the error code */
	vb2_fail(&cc, 3, 4);
	TEST_EQ(vb2_nv_get(&cc, VB2_NV_RECOVERY_REQUEST),
		1, "vb2_fail repeat");
	TEST_EQ(vb2_nv_get(&cc, VB2_NV_RECOVERY_SUBCODE),
		2, "vb2_fail repeat2");

	/* Fail with other slot good doesn't trigger recovery */
	reset_common_data();
	vb2_nv_set(&cc, VB2_NV_TRY_COUNT, 3);
	vb2_nv_set(&cc, VB2_NV_FW_RESULT, VB2_FW_RESULT_UNKNOWN);
	sd->status |= VB2_SD_STATUS_CHOSE_SLOT;
	sd->fw_slot = 0;
	sd->last_fw_slot = 1;
	sd->last_fw_result = VB2_FW_RESULT_UNKNOWN;
	vb2_fail(&cc, 5, 6);
	TEST_EQ(vb2_nv_get(&cc, VB2_NV_RECOVERY_REQUEST), 0, "vb2_failover");
	TEST_EQ(vb2_nv_get(&cc, VB2_NV_FW_RESULT),
		VB2_FW_RESULT_FAILURE, "vb2_fail this fw");
	TEST_EQ(vb2_nv_get(&cc, VB2_NV_TRY_COUNT), 0, "vb2_fail use up tries");
	TEST_EQ(vb2_nv_get(&cc, VB2_NV_TRY_NEXT), 1, "vb2_fail try other slot");

	/* Fail with other slot already failing triggers recovery */
	reset_common_data();
	sd->status |= VB2_SD_STATUS_CHOSE_SLOT;
	sd->fw_slot = 1;
	sd->last_fw_slot = 0;
	sd->last_fw_result = VB2_FW_RESULT_FAILURE;
	vb2_fail(&cc, 7, 8);
	TEST_EQ(vb2_nv_get(&cc, VB2_NV_RECOVERY_REQUEST), 7,
		"vb2_fail both slots bad");
	TEST_EQ(vb2_nv_get(&cc, VB2_NV_FW_RESULT),
		VB2_FW_RESULT_FAILURE, "vb2_fail this fw");
	TEST_EQ(vb2_nv_get(&cc, VB2_NV_TRY_NEXT), 0, "vb2_fail try other slot");
}
Пример #16
0
static void select_slot_tests(void)
{
	/* Slot A */
	reset_common_data();
	TEST_SUCC(vb2_select_fw_slot(&cc), "select slot A");
	TEST_EQ(vb2_nv_get(&cc, VB2_NV_FW_RESULT),
		VB2_FW_RESULT_UNKNOWN, "result unknown");
	TEST_NEQ(sd->status & VB2_SD_STATUS_CHOSE_SLOT, 0, "chose slot");
	TEST_EQ(vb2_nv_get(&cc, VB2_NV_FW_TRIED), 0, "tried A");
	TEST_EQ(sd->fw_slot, 0, "selected A");
	TEST_EQ(cc.flags & VB2_CONTEXT_FW_SLOT_B, 0, "didn't choose B");

	/* Slot B */
	reset_common_data();
	vb2_nv_set(&cc, VB2_NV_TRY_NEXT, 1);
	TEST_SUCC(vb2_select_fw_slot(&cc), "select slot B");
	TEST_EQ(vb2_nv_get(&cc, VB2_NV_FW_RESULT),
		VB2_FW_RESULT_UNKNOWN, "result unknown");
	TEST_NEQ(sd->status & VB2_SD_STATUS_CHOSE_SLOT, 0, "chose slot");
	TEST_EQ(vb2_nv_get(&cc, VB2_NV_FW_TRIED), 1, "tried B");
	TEST_EQ(sd->fw_slot, 1, "selected B");
	TEST_NEQ(cc.flags & VB2_CONTEXT_FW_SLOT_B, 0, "ctx says choose B");

	/* Slot A ran out of tries */
	reset_common_data();
	vb2_nv_set(&cc, VB2_NV_FW_RESULT, VB2_FW_RESULT_TRYING);
	TEST_SUCC(vb2_select_fw_slot(&cc), "select slot A out of tries");
	TEST_EQ(vb2_nv_get(&cc, VB2_NV_TRY_NEXT), 1, "try B next");
	TEST_NEQ(sd->status & VB2_SD_STATUS_CHOSE_SLOT, 0, "chose slot");
	TEST_EQ(vb2_nv_get(&cc, VB2_NV_FW_TRIED), 1, "tried B");
	TEST_EQ(sd->fw_slot, 1, "selected B");
	TEST_NEQ(cc.flags & VB2_CONTEXT_FW_SLOT_B, 0, "ctx says choose B");

	/* Slot A ran out of tries, even with nofail active */
	reset_common_data();
	cc.flags |= VB2_CONTEXT_NOFAIL_BOOT;
	vb2_nv_set(&cc, VB2_NV_FW_RESULT, VB2_FW_RESULT_TRYING);
	TEST_SUCC(vb2_select_fw_slot(&cc), "select slot A out of tries");
	TEST_EQ(vb2_nv_get(&cc, VB2_NV_TRY_NEXT), 1, "try B next");
	TEST_NEQ(sd->status & VB2_SD_STATUS_CHOSE_SLOT, 0, "chose slot");
	TEST_EQ(vb2_nv_get(&cc, VB2_NV_FW_TRIED), 1, "tried B");
	TEST_EQ(sd->fw_slot, 1, "selected B");
	TEST_NEQ(cc.flags & VB2_CONTEXT_FW_SLOT_B, 0, "ctx says choose B");

	/* Slot A used up a try */
	reset_common_data();
	vb2_nv_set(&cc, VB2_NV_TRY_COUNT, 3);
	TEST_SUCC(vb2_select_fw_slot(&cc), "try slot A");
	TEST_EQ(vb2_nv_get(&cc, VB2_NV_FW_RESULT),
		VB2_FW_RESULT_TRYING, "result trying");
	TEST_NEQ(sd->status & VB2_SD_STATUS_CHOSE_SLOT, 0, "chose slot");
	TEST_EQ(vb2_nv_get(&cc, VB2_NV_FW_TRIED), 0, "tried A");
	TEST_EQ(sd->fw_slot, 0, "selected A");
	TEST_EQ(cc.flags & VB2_CONTEXT_FW_SLOT_B, 0, "didn't choose B");
	TEST_EQ(vb2_nv_get(&cc, VB2_NV_TRY_COUNT), 2, "tries decremented");

	/* Slot A failed, but nofail active */
	reset_common_data();
	cc.flags |= VB2_CONTEXT_NOFAIL_BOOT;
	vb2_nv_set(&cc, VB2_NV_TRY_COUNT, 3);
	TEST_SUCC(vb2_select_fw_slot(&cc), "try slot A");
	TEST_EQ(vb2_nv_get(&cc, VB2_NV_FW_RESULT),
		VB2_FW_RESULT_TRYING, "result trying");
	TEST_NEQ(sd->status & VB2_SD_STATUS_CHOSE_SLOT, 0, "chose slot");
	TEST_EQ(vb2_nv_get(&cc, VB2_NV_FW_TRIED), 0, "tried A");
	TEST_EQ(sd->fw_slot, 0, "selected A");
	TEST_EQ(cc.flags & VB2_CONTEXT_FW_SLOT_B, 0, "didn't choose B");
	TEST_EQ(vb2_nv_get(&cc, VB2_NV_TRY_COUNT), 3, "tries not decremented");

	/* Tried/result get copied to the previous fields */
	reset_common_data();
	vb2_nv_set(&cc, VB2_NV_FW_TRIED, 0);
	vb2_nv_set(&cc, VB2_NV_FW_RESULT, VB2_FW_RESULT_SUCCESS);
	vb2_select_fw_slot(&cc);
	TEST_EQ(vb2_nv_get(&cc, VB2_NV_FW_PREV_TRIED), 0, "prev A");
	TEST_EQ(vb2_nv_get(&cc, VB2_NV_FW_PREV_RESULT),	VB2_FW_RESULT_SUCCESS,
		"prev success");

	reset_common_data();
	vb2_nv_set(&cc, VB2_NV_FW_TRIED, 1);
	vb2_nv_set(&cc, VB2_NV_FW_RESULT, VB2_FW_RESULT_FAILURE);
	vb2_select_fw_slot(&cc);
	TEST_EQ(vb2_nv_get(&cc, VB2_NV_FW_PREV_TRIED), 1, "prev B");
	TEST_EQ(vb2_nv_get(&cc, VB2_NV_FW_PREV_RESULT),	VB2_FW_RESULT_FAILURE,
		"prev failure");
}
Пример #17
0
static void test_check_keyblock(const struct vb2_public_key *public_key,
				const struct vb2_private_key *private_key,
				const struct vb2_packed_key *data_key)
{
	struct vb2_keyblock *hdr;
	struct vb2_keyblock *h;
	struct vb2_signature *sig;
	uint32_t hsize;

	hdr = vb2_create_keyblock(data_key, private_key, 0x1234);
	TEST_NEQ((size_t)hdr, 0, "vb2_verify_keyblock() prerequisites");
	if (!hdr)
		return;
	hsize = hdr->keyblock_size;
	h = (struct vb2_keyblock *)malloc(hsize + 2048);
	sig = &h->keyblock_signature;

	memcpy(h, hdr, hsize);
	TEST_SUCC(vb2_check_keyblock(h, hsize, sig),
		  "vb2_check_keyblock() ok");

	memcpy(h, hdr, hsize);
	TEST_EQ(vb2_check_keyblock(h, hsize - 1, sig),
		VB2_ERROR_KEYBLOCK_SIZE, "vb2_check_keyblock() size--");

	/* Buffer is allowed to be bigger than keyblock */
	memcpy(h, hdr, hsize);
	TEST_SUCC(vb2_check_keyblock(h, hsize + 1, sig),
		  "vb2_check_keyblock() size++");

	memcpy(h, hdr, hsize);
	h->magic[0] &= 0x12;
	TEST_EQ(vb2_check_keyblock(h, hsize, sig),
		VB2_ERROR_KEYBLOCK_MAGIC, "vb2_check_keyblock() magic");

	/* Care about major version but not minor */
	memcpy(h, hdr, hsize);
	h->header_version_major++;
	resign_keyblock(h, private_key);
	TEST_EQ(vb2_check_keyblock(h, hsize, sig),
		VB2_ERROR_KEYBLOCK_HEADER_VERSION,
		"vb2_check_keyblock() major++");

	memcpy(h, hdr, hsize);
	h->header_version_major--;
	resign_keyblock(h, private_key);
	TEST_EQ(vb2_check_keyblock(h, hsize, sig),
		VB2_ERROR_KEYBLOCK_HEADER_VERSION,
		"vb2_check_keyblock() major--");

	memcpy(h, hdr, hsize);
	h->header_version_minor++;
	resign_keyblock(h, private_key);
	TEST_SUCC(vb2_check_keyblock(h, hsize, sig),
		  "vb2_check_keyblock() minor++");

	memcpy(h, hdr, hsize);
	h->header_version_minor--;
	resign_keyblock(h, private_key);
	TEST_SUCC(vb2_check_keyblock(h, hsize, sig),
		  "vb2_check_keyblock() minor--");

	/* Check signature */
	memcpy(h, hdr, hsize);
	h->keyblock_signature.sig_offset = hsize;
	resign_keyblock(h, private_key);
	TEST_EQ(vb2_check_keyblock(h, hsize, sig),
		VB2_ERROR_KEYBLOCK_SIG_OUTSIDE,
		"vb2_check_keyblock() sig off end");

	memcpy(h, hdr, hsize);
	h->keyblock_signature.data_size = h->keyblock_size + 1;
	TEST_EQ(vb2_check_keyblock(h, hsize, sig),
		VB2_ERROR_KEYBLOCK_SIGNED_TOO_MUCH,
		"vb2_check_keyblock() sig data past end of block");

	/* Check that we signed header and data key */
	memcpy(h, hdr, hsize);
	h->keyblock_signature.data_size = 4;
	h->data_key.key_offset = 0;
	h->data_key.key_size = 0;
	resign_keyblock(h, private_key);
	TEST_EQ(vb2_check_keyblock(h, hsize, sig),
		VB2_ERROR_KEYBLOCK_SIGNED_TOO_LITTLE,
		"vb2_check_keyblock() didn't sign header");

	memcpy(h, hdr, hsize);
	h->data_key.key_offset = hsize;
	resign_keyblock(h, private_key);
	TEST_EQ(vb2_check_keyblock(h, hsize, sig),
		VB2_ERROR_KEYBLOCK_DATA_KEY_OUTSIDE,
		"vb2_check_keyblock() data key off end");

	/* Corner cases for error checking */
	TEST_EQ(vb2_check_keyblock(NULL, 4, sig),
		VB2_ERROR_KEYBLOCK_TOO_SMALL_FOR_HEADER,
		"vb2_check_keyblock size too small");

	/*
	 * TODO: verify parser can support a bigger header (i.e., one where
	 * data_key.key_offset is bigger than expected).
	 */

	free(h);
	free(hdr);
}
Пример #18
0
static void test_verify_kernel_preamble(
		const struct vb2_packed_key *public_key,
		const struct vb2_private_key *private_key)
{
	struct vb2_public_key rsa;
	// TODO: how many workbuf bytes?
	uint8_t workbuf[VB2_VERIFY_FIRMWARE_PREAMBLE_WORKBUF_BYTES]
		 __attribute__ ((aligned (VB2_WORKBUF_ALIGN)));
	struct vb2_workbuf wb;
	uint32_t hsize;

	vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));

	/* Create a dummy signature */
	struct vb2_signature *body_sig = vb2_alloc_signature(56, 0x214000);

	TEST_SUCC(vb2_unpack_key(&rsa, public_key),
		  "vb2_verify_kernel_preamble() prereq key");

	struct vb2_kernel_preamble *hdr =
		vb2_create_kernel_preamble(0x1234, 0x100000, 0x300000, 0x4000,
					   body_sig, 0x304000, 0x10000, 0, 0,
					   private_key);
	TEST_PTR_NEQ(hdr, NULL,
		     "vb2_verify_kernel_preamble() prereq test preamble");
	if (!hdr) {
		free(body_sig);
		return;
	}

	hsize = (uint32_t) hdr->preamble_size;
	struct vb2_kernel_preamble *h =
		(struct vb2_kernel_preamble *)malloc(hsize + 16384);

	memcpy(h, hdr, hsize);
	TEST_SUCC(vb2_verify_kernel_preamble(h, hsize, &rsa, &wb),
		  "vb2_verify_kernel_preamble() ok using key");

	memcpy(h, hdr, hsize);
	TEST_EQ(vb2_verify_kernel_preamble(h, 4, &rsa, &wb),
		VB2_ERROR_PREAMBLE_TOO_SMALL_FOR_HEADER,
		"vb2_verify_kernel_preamble() size tiny");

	memcpy(h, hdr, hsize);
	TEST_EQ(vb2_verify_kernel_preamble(h, hsize - 1, &rsa, &wb),
		VB2_ERROR_PREAMBLE_SIZE,
		"vb2_verify_kernel_preamble() size--");

	/* Buffer is allowed to be bigger than preamble */
	memcpy(h, hdr, hsize);
	TEST_SUCC(vb2_verify_kernel_preamble(h, hsize + 1, &rsa, &wb),
		  "vb2_verify_kernel_preamble() size++");

	/* Care about major version but not minor */
	memcpy(h, hdr, hsize);
	h->header_version_major++;
	resign_kernel_preamble(h, private_key);
	TEST_EQ(vb2_verify_kernel_preamble(h, hsize, &rsa, &wb),
		VB2_ERROR_PREAMBLE_HEADER_VERSION
		, "vb2_verify_kernel_preamble() major++");

	memcpy(h, hdr, hsize);
	h->header_version_major--;
	resign_kernel_preamble(h, private_key);
	TEST_EQ(vb2_verify_kernel_preamble(h, hsize, &rsa, &wb),
		VB2_ERROR_PREAMBLE_HEADER_VERSION,
		"vb2_verify_kernel_preamble() major--");

	memcpy(h, hdr, hsize);
	h->header_version_minor++;
	resign_kernel_preamble(h, private_key);
	TEST_SUCC(vb2_verify_kernel_preamble(h, hsize, &rsa, &wb),
		  "vb2_verify_kernel_preamble() minor++");

	/* Check signature */
	memcpy(h, hdr, hsize);
	h->preamble_signature.sig_offset = hsize;
	resign_kernel_preamble(h, private_key);
	TEST_EQ(vb2_verify_kernel_preamble(h, hsize, &rsa, &wb),
		VB2_ERROR_PREAMBLE_SIG_OUTSIDE,
		"vb2_verify_kernel_preamble() sig off end");

	memcpy(h, hdr, hsize);
	h->preamble_signature.sig_size--;
	resign_kernel_preamble(h, private_key);
	TEST_EQ(vb2_verify_kernel_preamble(h, hsize, &rsa, &wb),
		VB2_ERROR_PREAMBLE_SIG_INVALID,
		"vb2_verify_kernel_preamble() sig too small");

	memcpy(h, hdr, hsize);
	h->flags++;
	TEST_EQ(vb2_verify_kernel_preamble(h, hsize, &rsa, &wb),
		VB2_ERROR_PREAMBLE_SIG_INVALID,
		"vb2_verify_kernel_preamble() sig mismatch");

	/* Check that we signed header and body sig */
	memcpy(h, hdr, hsize);
	h->preamble_signature.data_size = 4;
	h->body_signature.sig_offset = 0;
	h->body_signature.sig_size = 0;
	resign_kernel_preamble(h, private_key);
	TEST_EQ(vb2_verify_kernel_preamble(h, hsize, &rsa, &wb),
		VB2_ERROR_PREAMBLE_SIGNED_TOO_LITTLE,
		"vb2_verify_kernel_preamble() didn't sign header");

	memcpy(h, hdr, hsize);
	h->body_signature.sig_offset = hsize;
	resign_kernel_preamble(h, private_key);
	TEST_EQ(vb2_verify_kernel_preamble(h, hsize, &rsa, &wb),
		VB2_ERROR_PREAMBLE_BODY_SIG_OUTSIDE,
		"vb2_verify_kernel_preamble() body sig off end");

	/* Check bootloader inside signed body */
	memcpy(h, hdr, hsize);
	h->bootloader_address = h->body_load_address - 1;
	resign_kernel_preamble(h, private_key);
	TEST_EQ(vb2_verify_kernel_preamble(h, hsize, &rsa, &wb),
		VB2_ERROR_PREAMBLE_BOOTLOADER_OUTSIDE,
		"vb2_verify_kernel_preamble() bootloader before body");

	memcpy(h, hdr, hsize);
	h->bootloader_address = h->body_load_address +
		h->body_signature.data_size + 1;
	resign_kernel_preamble(h, private_key);
	TEST_EQ(vb2_verify_kernel_preamble(h, hsize, &rsa, &wb),
		VB2_ERROR_PREAMBLE_BOOTLOADER_OUTSIDE,
		"vb2_verify_kernel_preamble() bootloader off end of body");

	memcpy(h, hdr, hsize);
	h->bootloader_address = h->body_load_address +
		h->body_signature.data_size + 1;
	h->bootloader_size = 0;
	resign_kernel_preamble(h, private_key);
	TEST_SUCC(vb2_verify_kernel_preamble(h, hsize, &rsa, &wb),
		  "vb2_verify_kernel_preamble() no bootloader");

	/* Check vmlinuz inside signed body */
	memcpy(h, hdr, hsize);
	h->vmlinuz_header_address = h->body_load_address - 1;
	resign_kernel_preamble(h, private_key);
	TEST_EQ(vb2_verify_kernel_preamble(h, hsize, &rsa, &wb),
		VB2_ERROR_PREAMBLE_VMLINUZ_HEADER_OUTSIDE,
		"vb2_verify_kernel_preamble() vmlinuz_header before body");

	memcpy(h, hdr, hsize);
	h->vmlinuz_header_address = h->body_load_address +
		h->body_signature.data_size + 1;
	resign_kernel_preamble(h, private_key);
	TEST_EQ(vb2_verify_kernel_preamble(h, hsize, &rsa, &wb),
		VB2_ERROR_PREAMBLE_VMLINUZ_HEADER_OUTSIDE,
		"vb2_verify_kernel_preamble() vmlinuz_header off end of body");

	memcpy(h, hdr, hsize);
	h->vmlinuz_header_address = h->body_load_address +
		h->body_signature.data_size + 1;
	h->vmlinuz_header_size = 0;
	resign_kernel_preamble(h, private_key);
	TEST_SUCC(vb2_verify_kernel_preamble(h, hsize, &rsa, &wb),
		  "vb2_verify_kernel_preamble() no vmlinuz_header");

	/* TODO: verify with extra padding at end of header. */

	free(h);
	free(hdr);
	free(body_sig);
}
Пример #19
0
static void nv_storage_test(void)
{
    struct nv_field *vnf;
    uint8_t goodcrc;
    uint8_t workbuf[VB2_WORKBUF_RECOMMENDED_SIZE]
    __attribute__ ((aligned (VB2_WORKBUF_ALIGN)));
    struct vb2_context c = {
        .flags = 0,
        .workbuf = workbuf,
        .workbuf_size = sizeof(workbuf),
    };
    struct vb2_shared_data *sd = vb2_get_sd(&c);

    memset(c.nvdata, 0xA6, sizeof(c.nvdata));
    vb2_init_context(&c);

    /* Init with invalid data should set defaults and regenerate CRC */
    vb2_nv_init(&c);
    TEST_EQ(c.nvdata[0], 0x70, "vb2_nv_init() reset header byte");
    TEST_NEQ(c.nvdata[15], 0, "vb2_nv_init() CRC");
    TEST_EQ(sd->status, VB2_SD_STATUS_NV_INIT | VB2_SD_STATUS_NV_REINIT,
            "vb2_nv_init() status changed");
    test_changed(&c, 1, "vb2_nv_init() reset changed");
    goodcrc = c.nvdata[15];
    TEST_SUCC(vb2_nv_check_crc(&c), "vb2_nv_check_crc() good");

    /* Another init should not cause further changes */
    c.flags = 0;
    sd->status = 0;
    vb2_nv_init(&c);
    test_changed(&c, 0, "vb2_nv_init() didn't re-reset");
    TEST_EQ(c.nvdata[15], goodcrc, "vb2_nv_init() CRC same");
    TEST_EQ(sd->status, VB2_SD_STATUS_NV_INIT, "vb2_nv_init() status same");

    /* Perturbing the header should force defaults */
    c.nvdata[0] ^= 0x40;
    TEST_EQ(vb2_nv_check_crc(&c),
            VB2_ERROR_NV_HEADER, "vb2_nv_check_crc() bad header");
    vb2_nv_init(&c);
    TEST_EQ(c.nvdata[0], 0x70, "vb2_nv_init() reset header byte again");
    test_changed(&c, 1, "vb2_nv_init() corrupt changed");
    TEST_EQ(c.nvdata[15], goodcrc, "vb2_nv_init() CRC same again");

    /* So should perturbing some other byte */
    TEST_EQ(c.nvdata[11], 0, "Kernel byte starts at 0");
    c.nvdata[11] = 12;
    TEST_EQ(vb2_nv_check_crc(&c),
            VB2_ERROR_NV_CRC, "vb2_nv_check_crc() bad CRC");
    vb2_nv_init(&c);
    TEST_EQ(c.nvdata[11], 0, "vb2_nv_init() reset kernel byte");
    test_changed(&c, 1, "vb2_nv_init() corrupt elsewhere changed");
    TEST_EQ(c.nvdata[15], goodcrc, "vb2_nv_init() CRC same again");

    /* Clear the kernel and firmware flags */
    vb2_nv_init(&c);
    TEST_EQ(vb2_nv_get(&c, VB2_NV_FIRMWARE_SETTINGS_RESET),
            1, "Firmware settings are reset");
    vb2_nv_set(&c, VB2_NV_FIRMWARE_SETTINGS_RESET, 0);
    TEST_EQ(vb2_nv_get(&c, VB2_NV_FIRMWARE_SETTINGS_RESET),
            0, "Firmware settings are clear");

    TEST_EQ(vb2_nv_get(&c, VB2_NV_KERNEL_SETTINGS_RESET),
            1, "Kernel settings are reset");
    vb2_nv_set(&c, VB2_NV_KERNEL_SETTINGS_RESET, 0);
    TEST_EQ(vb2_nv_get(&c, VB2_NV_KERNEL_SETTINGS_RESET),
            0, "Kernel settings are clear");

    TEST_EQ(c.nvdata[0], 0x40, "Header byte now just has the header bit");
    /* That should have changed the CRC */
    TEST_NEQ(c.nvdata[15], goodcrc,
             "vb2_nv_init() CRC changed due to flags clear");

    /* Test explicitly setting the reset flags again */
    vb2_nv_init(&c);
    vb2_nv_set(&c, VB2_NV_FIRMWARE_SETTINGS_RESET, 1);
    TEST_EQ(vb2_nv_get(&c, VB2_NV_FIRMWARE_SETTINGS_RESET),
            1, "Firmware settings forced reset");
    vb2_nv_set(&c, VB2_NV_FIRMWARE_SETTINGS_RESET, 0);

    vb2_nv_set(&c, VB2_NV_KERNEL_SETTINGS_RESET, 1);
    TEST_EQ(vb2_nv_get(&c, VB2_NV_KERNEL_SETTINGS_RESET),
            1, "Kernel settings forced reset");
    vb2_nv_set(&c, VB2_NV_KERNEL_SETTINGS_RESET, 0);

    /* Get/set an invalid field */
    vb2_nv_init(&c);
    vb2_nv_set(&c, -1, 1);
    TEST_EQ(vb2_nv_get(&c, -1), 0, "Get invalid setting");

    /* Test other fields */
    vb2_nv_init(&c);
    for (vnf = nvfields; vnf->desc; vnf++) {
        TEST_EQ(vb2_nv_get(&c, vnf->param), vnf->default_value,
                vnf->desc);
        vb2_nv_set(&c, vnf->param, vnf->test_value);
        TEST_EQ(vb2_nv_get(&c, vnf->param), vnf->test_value, vnf->desc);
        vb2_nv_set(&c, vnf->param, vnf->test_value2);
        TEST_EQ(vb2_nv_get(&c, vnf->param), vnf->test_value2,
                vnf->desc);
    }

    /* None of those changes should have caused a reset to defaults */
    vb2_nv_init(&c);
    TEST_EQ(vb2_nv_get(&c, VB2_NV_FIRMWARE_SETTINGS_RESET),
            0, "Firmware settings are still clear");
    TEST_EQ(vb2_nv_get(&c, VB2_NV_KERNEL_SETTINGS_RESET),
            0, "Kernel settings are still clear");

    /* Writing identical settings doesn't cause the CRC to regenerate */
    c.flags = 0;
    vb2_nv_init(&c);
    test_changed(&c, 0, "No regen CRC on open");
    for (vnf = nvfields; vnf->desc; vnf++)
        vb2_nv_set(&c, vnf->param, vnf->test_value2);
    test_changed(&c, 0, "No regen CRC if data not changed");

    /* Test out-of-range fields mapping to defaults or failing */
    vb2_nv_init(&c);
    vb2_nv_set(&c, VB2_NV_TRY_COUNT, 16);
    TEST_EQ(vb2_nv_get(&c, VB2_NV_TRY_COUNT),
            15, "Try b count out of range");
    vb2_nv_set(&c, VB2_NV_RECOVERY_REQUEST, 0x101);
    TEST_EQ(vb2_nv_get(&c, VB2_NV_RECOVERY_REQUEST),
            VB2_RECOVERY_LEGACY, "Recovery request out of range");
    vb2_nv_set(&c, VB2_NV_LOCALIZATION_INDEX, 0x102);
    TEST_EQ(vb2_nv_get(&c, VB2_NV_LOCALIZATION_INDEX),
            0, "Localization index out of range");

    vb2_nv_set(&c, VB2_NV_FW_RESULT, VB2_FW_RESULT_UNKNOWN + 1);
    vb2_nv_set(&c, VB2_NV_FW_RESULT, VB2_FW_RESULT_UNKNOWN + 100);
    TEST_EQ(vb2_nv_get(&c, VB2_NV_FW_RESULT),
            VB2_FW_RESULT_UNKNOWN, "Firmware result out of range");
}

int main(int argc, char* argv[])
{
    nv_storage_test();

    return gTestSuccess ? 0 : 255;
}
Пример #20
0
static void dev_switch_tests(void)
{
	uint32_t v;

	/* Normal mode */
	reset_common_data();
	TEST_SUCC(vb2_check_dev_switch(&cc), "dev mode off");
	TEST_EQ(sd->flags & VB2_SD_DEV_MODE_ENABLED, 0, "sd not in dev");
	TEST_EQ(cc.flags & VB2_CONTEXT_DEVELOPER_MODE, 0, "ctx not in dev");
	TEST_EQ(mock_tpm_clear_called, 0, "no tpm clear");

	/* Dev mode */
	reset_common_data();
	vb2_secdata_set(&cc, VB2_SECDATA_FLAGS,
			(VB2_SECDATA_FLAG_DEV_MODE |
			 VB2_SECDATA_FLAG_LAST_BOOT_DEVELOPER));
	TEST_SUCC(vb2_check_dev_switch(&cc), "dev mode on");
	TEST_NEQ(sd->flags & VB2_SD_DEV_MODE_ENABLED, 0, "sd in dev");
	TEST_NEQ(cc.flags & VB2_CONTEXT_DEVELOPER_MODE, 0, "ctx in dev");
	TEST_EQ(mock_tpm_clear_called, 0, "no tpm clear");

	/* Any normal mode boot clears dev boot flags */
	reset_common_data();
	vb2_nv_set(&cc, VB2_NV_DEV_BOOT_USB, 1);
	vb2_nv_set(&cc, VB2_NV_DEV_BOOT_LEGACY, 1);
	vb2_nv_set(&cc, VB2_NV_DEV_BOOT_SIGNED_ONLY, 1);
	vb2_nv_set(&cc, VB2_NV_DEV_BOOT_FASTBOOT_FULL_CAP, 1);
	vb2_nv_set(&cc, VB2_NV_FASTBOOT_UNLOCK_IN_FW, 1);
	TEST_SUCC(vb2_check_dev_switch(&cc), "dev mode off");
	TEST_EQ(vb2_nv_get(&cc, VB2_NV_DEV_BOOT_USB),
		0, "cleared dev boot usb");
	TEST_EQ(vb2_nv_get(&cc, VB2_NV_DEV_BOOT_LEGACY),
		0, "cleared dev boot legacy");
	TEST_EQ(vb2_nv_get(&cc, VB2_NV_DEV_BOOT_SIGNED_ONLY),
		0, "cleared dev boot signed only");
	TEST_EQ(vb2_nv_get(&cc, VB2_NV_DEV_BOOT_FASTBOOT_FULL_CAP),
		0, "cleared dev boot fastboot full cap");
	TEST_EQ(vb2_nv_get(&cc, VB2_NV_FASTBOOT_UNLOCK_IN_FW),
		0, "cleared dev boot fastboot unlock in fw");

	/* Normal-dev transition clears TPM */
	reset_common_data();
	vb2_secdata_set(&cc, VB2_SECDATA_FLAGS, VB2_SECDATA_FLAG_DEV_MODE);
	TEST_SUCC(vb2_check_dev_switch(&cc), "to dev mode");
	TEST_EQ(mock_tpm_clear_called, 1, "tpm clear");
	vb2_secdata_get(&cc, VB2_SECDATA_FLAGS, &v);
	TEST_EQ(v, (VB2_SECDATA_FLAG_DEV_MODE |
		    VB2_SECDATA_FLAG_LAST_BOOT_DEVELOPER),
		"last boot developer now");

	/* Dev-normal transition clears TPM too */
	reset_common_data();
	vb2_secdata_set(&cc, VB2_SECDATA_FLAGS,
			VB2_SECDATA_FLAG_LAST_BOOT_DEVELOPER);
	TEST_SUCC(vb2_check_dev_switch(&cc), "from dev mode");
	TEST_EQ(mock_tpm_clear_called, 1, "tpm clear");
	vb2_secdata_get(&cc, VB2_SECDATA_FLAGS, &v);
	TEST_EQ(v, 0, "last boot not developer now");

	/* Disable dev mode */
	reset_common_data();
	vb2_secdata_set(&cc, VB2_SECDATA_FLAGS,
			(VB2_SECDATA_FLAG_DEV_MODE |
			 VB2_SECDATA_FLAG_LAST_BOOT_DEVELOPER));
	vb2_nv_set(&cc, VB2_NV_DISABLE_DEV_REQUEST, 1);
	TEST_SUCC(vb2_check_dev_switch(&cc), "disable dev request");
	TEST_EQ(sd->flags & VB2_SD_DEV_MODE_ENABLED, 0, "sd not in dev");
	TEST_EQ(vb2_nv_get(&cc, VB2_NV_DISABLE_DEV_REQUEST),
		0, "request cleared");

	/* Force enabled by gbb */
	reset_common_data();
	sd->gbb_flags |= VB2_GBB_FLAG_FORCE_DEV_SWITCH_ON;
	TEST_SUCC(vb2_check_dev_switch(&cc), "dev on via gbb");
	TEST_NEQ(sd->flags & VB2_SD_DEV_MODE_ENABLED, 0, "sd in dev");
	vb2_secdata_get(&cc, VB2_SECDATA_FLAGS, &v);
	TEST_EQ(v, VB2_SECDATA_FLAG_LAST_BOOT_DEVELOPER,
		"doesn't set dev on in secdata but does set last boot dev");
	TEST_EQ(mock_tpm_clear_called, 1, "tpm clear");

	/* Force enabled by ctx flag */
	reset_common_data();
	cc.flags |= VB2_CONTEXT_FORCE_DEVELOPER_MODE;
	TEST_SUCC(vb2_check_dev_switch(&cc), "dev on via ctx flag");
	TEST_NEQ(sd->flags & VB2_SD_DEV_MODE_ENABLED, 0, "sd in dev");
	vb2_secdata_get(&cc, VB2_SECDATA_FLAGS, &v);
	TEST_EQ(v, VB2_SECDATA_FLAG_LAST_BOOT_DEVELOPER,
		"doesn't set dev on in secdata but does set last boot dev");
	TEST_EQ(mock_tpm_clear_called, 1, "tpm clear");

	/* Simulate clear owner failure */
	reset_common_data();
	vb2_secdata_set(&cc, VB2_SECDATA_FLAGS,
			VB2_SECDATA_FLAG_LAST_BOOT_DEVELOPER);
	mock_tpm_clear_retval = VB2_ERROR_EX_TPM_CLEAR_OWNER;
	TEST_EQ(vb2_check_dev_switch(&cc),
		VB2_ERROR_EX_TPM_CLEAR_OWNER, "tpm clear fail");
	TEST_EQ(mock_tpm_clear_called, 1, "tpm clear");
	vb2_secdata_get(&cc, VB2_SECDATA_FLAGS, &v);
	TEST_EQ(v, VB2_SECDATA_FLAG_LAST_BOOT_DEVELOPER,
		"last boot still developer");
	TEST_EQ(vb2_nv_get(&cc, VB2_NV_RECOVERY_REQUEST),
		VB2_RECOVERY_TPM_CLEAR_OWNER, "requests recovery");
	TEST_EQ(vb2_nv_get(&cc, VB2_NV_RECOVERY_SUBCODE),
		(uint8_t)VB2_ERROR_EX_TPM_CLEAR_OWNER, "recovery subcode");
}
Пример #21
0
static void phase1_tests(void)
{
	struct vb2_packed_key *k;
	uint32_t old_preamble_offset;

	/* Test successful call */
	reset_common_data(FOR_PHASE1);
	old_preamble_offset = sd->workbuf_preamble_offset;
	TEST_SUCC(vb2api_kernel_phase1(&cc), "phase1 good");
	TEST_EQ(sd->workbuf_preamble_size, 0, "  no more fw preamble");
	/* Make sure normal key was loaded */
	TEST_EQ(sd->workbuf_kernel_key_offset, old_preamble_offset,
		"  workbuf key offset");
	k = (struct vb2_packed_key *)
		(cc.workbuf + sd->workbuf_kernel_key_offset);
	TEST_EQ(sd->workbuf_kernel_key_size, k->key_offset + k->key_size,
		"  workbuf key size");
	TEST_EQ(cc.workbuf_used, sd->workbuf_kernel_key_offset +
		sd->workbuf_kernel_key_size, "  workbuf used");
	TEST_EQ(k->algorithm, 7, "  key algorithm");
	TEST_EQ(k->key_size, sizeof(fw_kernel_key_data), "  key_size");
	TEST_EQ(memcmp((uint8_t *)k + k->key_offset, fw_kernel_key_data,
		       k->key_size), 0, "  key data");
	TEST_EQ(sd->kernel_version_secdatak, 0x20002, "  secdatak version");

	/* Test successful call in recovery mode */
	reset_common_data(FOR_PHASE1);
	cc.flags |= VB2_CONTEXT_RECOVERY_MODE;
	/* No preamble loaded in recovery mode */
	cc.workbuf_used = old_preamble_offset = sd->workbuf_preamble_offset;
	sd->workbuf_preamble_offset = sd->workbuf_preamble_size = 0;
	TEST_SUCC(vb2api_kernel_phase1(&cc), "phase1 rec good");
	TEST_EQ(sd->workbuf_preamble_size, 0, "no more fw preamble");
	/* Make sure recovery key was loaded */
	TEST_EQ(sd->workbuf_kernel_key_offset, old_preamble_offset,
		"  workbuf key offset");
	k = (struct vb2_packed_key *)
		(cc.workbuf + sd->workbuf_kernel_key_offset);
	TEST_EQ(sd->workbuf_kernel_key_size, k->key_offset + k->key_size,
		"  workbuf key size");
	TEST_EQ(cc.workbuf_used, sd->workbuf_kernel_key_offset +
		sd->workbuf_kernel_key_size, "  workbuf used");
	TEST_EQ(k->algorithm, 11, "  key algorithm");
	TEST_EQ(k->key_size, sizeof(mock_gbb.recovery_key_data), "  key_size");
	TEST_EQ(memcmp((uint8_t *)k + k->key_offset,
		       mock_gbb.recovery_key_data, k->key_size), 0,
		"  key data");
	TEST_EQ(sd->kernel_version_secdatak, 0x20002, "  secdatak version");

	/* Bad secdatak causes failure in normal mode only */
	reset_common_data(FOR_PHASE1);
	cc.secdatak[0] ^= 0x33;
	TEST_EQ(vb2api_kernel_phase1(&cc), VB2_ERROR_SECDATAK_CRC,
		"phase1 bad secdata");
	reset_common_data(FOR_PHASE1);

	cc.secdatak[0] ^= 0x33;
	cc.flags |= VB2_CONTEXT_RECOVERY_MODE;
	TEST_SUCC(vb2api_kernel_phase1(&cc), "phase1 bad secdata rec");
	TEST_EQ(sd->kernel_version_secdatak, 0, "  secdatak version");

	/* Failures while reading recovery key */
	reset_common_data(FOR_PHASE1);
	cc.flags |= VB2_CONTEXT_RECOVERY_MODE;
	cc.workbuf_used = cc.workbuf_size - sizeof(struct vb2_gbb_header) + 1;
	TEST_EQ(vb2api_kernel_phase1(&cc), VB2_ERROR_GBB_WORKBUF,
		"phase1 rec workbuf gbb header");

	reset_common_data(FOR_PHASE1);
	cc.flags |= VB2_CONTEXT_RECOVERY_MODE;
	mock_read_gbb_header_retval = VB2_ERROR_MOCK;
	TEST_EQ(vb2api_kernel_phase1(&cc), VB2_ERROR_MOCK,
		"phase1 rec gbb read header");

	reset_common_data(FOR_PHASE1);
	cc.flags |= VB2_CONTEXT_RECOVERY_MODE;
	mock_gbb.h.recovery_key_size = cc.workbuf_size - 1;
	TEST_EQ(vb2api_kernel_phase1(&cc), VB2_ERROR_API_KPHASE1_WORKBUF_REC_KEY,
		"phase1 rec workbuf key");

	reset_common_data(FOR_PHASE1);
	cc.flags |= VB2_CONTEXT_RECOVERY_MODE;
	mock_read_res_fail_on_call = 1;
	TEST_EQ(vb2api_kernel_phase1(&cc), VB2_ERROR_MOCK,
		"phase1 rec gbb read key");

	/* Failures while parsing subkey from firmware preamble */
	reset_common_data(FOR_PHASE1);
	sd->workbuf_preamble_size = 0;
	TEST_EQ(vb2api_kernel_phase1(&cc), VB2_ERROR_API_KPHASE1_PREAMBLE,
		"phase1 fw preamble");
}