vboot: Add vb2_unpack_key_buffer

Previously, vb2_unpack_key() actually unpacked a key buffer.  Callers
that had a vb2_packed_key had to typecast it back to a uint8_t buffer to
unpack it.  Rename vb2_unpack_key() to vb2_unpack_key_buffer(), and make
vb2_unpack_key() unpack a vb2_packed_key.

BUG=chromium:611535
BRANCH=none
TEST=make runtests; emerge-kevin coreboot depthcharge;
     emerge-samus and boot it

Change-Id: I9ee38a819c59cc58a72ead78cf5ddf3d0f301ae7
Signed-off-by: Randall Spangler <rspangler@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/400906
Reviewed-by: Daisuke Nojiri <dnojiri@chromium.org>
diff --git a/firmware/2lib/include/2return_codes.h b/firmware/2lib/include/2return_codes.h
index 27b80d1..d03b073 100644
--- a/firmware/2lib/include/2return_codes.h
+++ b/firmware/2lib/include/2return_codes.h
@@ -161,16 +161,16 @@
 	/* Member data outside parent in vb2_verify_member_inside() */
 	VB2_ERROR_INSIDE_DATA_OUTSIDE,
 
-	/* Unsupported signature algorithm in vb2_unpack_key() */
+	/* Unsupported signature algorithm in vb2_unpack_key_buffer() */
 	VB2_ERROR_UNPACK_KEY_SIG_ALGORITHM,                      /* 0x150008 */
 
-	/* Bad key size in vb2_unpack_key() */
+	/* Bad key size in vb2_unpack_key_buffer() */
 	VB2_ERROR_UNPACK_KEY_SIZE,
 
-	/* Bad key alignment in vb2_unpack_key() */
+	/* Bad key alignment in vb2_unpack_key_buffer() */
 	VB2_ERROR_UNPACK_KEY_ALIGN,
 
-	/* Bad key array size in vb2_unpack_key() */
+	/* Bad key array size in vb2_unpack_key_buffer() */
 	VB2_ERROR_UNPACK_KEY_ARRAY_SIZE,
 
 	/* Bad algorithm in vb2_verify_data() */
@@ -194,7 +194,7 @@
 	 */
 	VB2_ERROR_VDATA_DIGEST_SIZE,
 
-	/* Unsupported hash algorithm in vb2_unpack_key() */
+	/* Unsupported hash algorithm in vb2_unpack_key_buffer() */
 	VB2_ERROR_UNPACK_KEY_HASH_ALGORITHM,
 
 	/* Member data overlaps member header */
@@ -256,9 +256,12 @@
 	/* Key algorithm doesn't match signature algorithm */
 	VB2_ERROR_VDATA_ALGORITHM_MISMATCH,
 
-	/* Bad magic number in vb2_unpack_key() */
+	/* Bad magic number in vb2_unpack_key_buffer() */
 	VB2_ERROR_UNPACK_KEY_MAGIC,
 
+	/* Null public key buffer passed to vb2_unpack_key_buffer() */
+	VB2_ERROR_UNPACK_KEY_BUFFER,
+
         /**********************************************************************
 	 * Keyblock verification errors (all in vb2_verify_keyblock())
 	 */
diff --git a/firmware/lib/vboot_api_kernel.c b/firmware/lib/vboot_api_kernel.c
index f264fb7..549df24 100644
--- a/firmware/lib/vboot_api_kernel.c
+++ b/firmware/lib/vboot_api_kernel.c
@@ -1417,10 +1417,9 @@
 	} else {
 		/* Unpack kernel subkey */
 		struct vb2_public_key kernel_subkey2;
-		if (VB2_SUCCESS != vb2_unpack_key(&kernel_subkey2,
-					  (const uint8_t *)kernel_subkey,
-					  kernel_subkey->key_offset +
-					  kernel_subkey->key_size)) {
+		if (VB2_SUCCESS !=
+		    vb2_unpack_key(&kernel_subkey2,
+				   (struct vb2_packed_key *)kernel_subkey)) {
 			VBDEBUG(("Unable to unpack kernel subkey\n"));
 			goto fail;
 		}
@@ -1450,11 +1449,7 @@
 
 	/* Get key for preamble/data verification from the key block. */
 	struct vb2_public_key data_key2;
-	if (VB2_SUCCESS !=
-	    vb2_unpack_key(&data_key2,
-			   (const uint8_t *)&keyblock2->data_key,
-			   keyblock2->data_key.key_offset +
-			   keyblock2->data_key.key_size)) {
+	if (VB2_SUCCESS != vb2_unpack_key(&data_key2, &keyblock2->data_key)) {
 		VBDEBUG(("Unable to unpack kernel data key\n"));
 		goto fail;
 	}
diff --git a/firmware/lib/vboot_kernel.c b/firmware/lib/vboot_kernel.c
index 65792e9..e599dd2 100644
--- a/firmware/lib/vboot_kernel.c
+++ b/firmware/lib/vboot_kernel.c
@@ -159,10 +159,9 @@
 
 	/* Unpack kernel subkey */
 	struct vb2_public_key kernel_subkey2;
-	if (VB2_SUCCESS != vb2_unpack_key(&kernel_subkey2,
-					  (const uint8_t *)kernel_subkey,
-					  kernel_subkey->key_offset +
-					  kernel_subkey->key_size)) {
+	if (VB2_SUCCESS !=
+	    vb2_unpack_key(&kernel_subkey2,
+			   (struct vb2_packed_key *)kernel_subkey)) {
 		VBDEBUG(("Unable to unpack kernel subkey\n"));
 		goto bad_gpt;
 	}
@@ -334,10 +333,7 @@
 		/* Get key for preamble/data verification from the key block. */
 		struct vb2_public_key data_key2;
 		if (VB2_SUCCESS !=
-		    vb2_unpack_key(&data_key2,
-				   (const uint8_t *)&keyblock2->data_key,
-				   keyblock2->data_key.key_offset +
-				   keyblock2->data_key.key_size)) {
+		    vb2_unpack_key(&data_key2, &keyblock2->data_key)) {
 			VBDEBUG(("Unable to unpack kernel data key\n"));
 			shpart->check_result = VBSD_LKP_CHECK_DATA_KEY_PARSE;
 			goto bad_kernel;
diff --git a/firmware/lib20/api.c b/firmware/lib20/api.c
index 7c25345..327a25c 100644
--- a/firmware/lib20/api.c
+++ b/firmware/lib20/api.c
@@ -96,7 +96,7 @@
 	if (!sd->workbuf_data_key_size)
 		return VB2_ERROR_API_INIT_HASH_DATA_KEY;
 
-	rv = vb2_unpack_key(&key,
+	rv = vb2_unpack_key_buffer(&key,
 			    ctx->workbuf + sd->workbuf_data_key_offset,
 			    sd->workbuf_data_key_size);
 	if (rv)
@@ -186,7 +186,7 @@
 	if (!sd->workbuf_data_key_size)
 		return VB2_ERROR_API_CHECK_HASH_DATA_KEY;
 
-	rv = vb2_unpack_key(&key,
+	rv = vb2_unpack_key_buffer(&key,
 			    ctx->workbuf + sd->workbuf_data_key_offset,
 			    sd->workbuf_data_key_size);
 	if (rv)
diff --git a/firmware/lib20/api_kernel.c b/firmware/lib20/api_kernel.c
index 0c8b598..ee5c41a 100644
--- a/firmware/lib20/api_kernel.c
+++ b/firmware/lib20/api_kernel.c
@@ -213,7 +213,7 @@
 	if (!sd->workbuf_data_key_size)
 		return VB2_ERROR_API_VERIFY_KDATA_KEY;
 
-	rv = vb2_unpack_key(&key,
+	rv = vb2_unpack_key_buffer(&key,
 			    ctx->workbuf + sd->workbuf_data_key_offset,
 			    sd->workbuf_data_key_size);
 	if (rv)
diff --git a/firmware/lib20/include/vb2_common.h b/firmware/lib20/include/vb2_common.h
index 536fbbb..96deb51 100644
--- a/firmware/lib20/include/vb2_common.h
+++ b/firmware/lib20/include/vb2_common.h
@@ -78,7 +78,7 @@
 				 const struct vb2_packed_key *key);
 
 /**
- * Unpack a vboot1-format key for use in verification
+ * Unpack a vboot1-format key buffer for use in verification
  *
  * The elements of the unpacked key will point into the source buffer, so don't
  * free the source buffer until you're done with the key.
@@ -88,9 +88,23 @@
  * @param size		Size of buffer in bytes
  * @return VB2_SUCCESS, or non-zero error code if error.
  */
+int vb2_unpack_key_buffer(struct vb2_public_key *key,
+			  const uint8_t *buf,
+			  uint32_t size);
+
+/**
+ * Unpack a vboot1-format key for use in verification
+ *
+ * The elements of the unpacked key will point into the source packed key, so
+ * don't free the source until you're done with the public key.
+ *
+ * @param key		Destintion for unpacked key
+ * @param packed_key	Source packed key
+ * @param size		Size of buffer in bytes
+ * @return VB2_SUCCESS, or non-zero error code if error.
+ */
 int vb2_unpack_key(struct vb2_public_key *key,
-		   const uint8_t *buf,
-		   uint32_t size);
+		   const struct vb2_packed_key *packed_key);
 
 /**
  * Verify a signature against an expected hash digest.
diff --git a/firmware/lib20/kernel.c b/firmware/lib20/kernel.c
index 809e1b6..f5632e5 100644
--- a/firmware/lib20/kernel.c
+++ b/firmware/lib20/kernel.c
@@ -122,7 +122,7 @@
 	/* Unpack the kernel key */
 	key_data = ctx->workbuf + sd->workbuf_kernel_key_offset;
 	key_size = sd->workbuf_kernel_key_size;
-	rv = vb2_unpack_key(&kernel_key, key_data, key_size);
+	rv = vb2_unpack_key_buffer(&kernel_key, key_data, key_size);
 	if (rv)
 		return rv;
 
@@ -374,7 +374,7 @@
 	if (!sd->workbuf_data_key_size)
 		return VB2_ERROR_KERNEL_PREAMBLE2_DATA_KEY;
 
-	rv = vb2_unpack_key(&data_key, key_data, key_size);
+	rv = vb2_unpack_key_buffer(&data_key, key_data, key_size);
 	if (rv)
 		return rv;
 
diff --git a/firmware/lib20/misc.c b/firmware/lib20/misc.c
index dee6b13..cbe894c 100644
--- a/firmware/lib20/misc.c
+++ b/firmware/lib20/misc.c
@@ -94,7 +94,7 @@
 		return rv;
 
 	/* Unpack the root key */
-	rv = vb2_unpack_key(&root_key, key_data, key_size);
+	rv = vb2_unpack_key_buffer(&root_key, key_data, key_size);
 	if (rv)
 		return rv;
 
@@ -212,7 +212,7 @@
 	if (!sd->workbuf_data_key_size)
 		return VB2_ERROR_FW_PREAMBLE2_DATA_KEY;
 
-	rv = vb2_unpack_key(&data_key, key_data, key_size);
+	rv = vb2_unpack_key_buffer(&data_key, key_data, key_size);
 	if (rv)
 		return rv;
 
diff --git a/firmware/lib20/packed_key.c b/firmware/lib20/packed_key.c
index 4baf97c..48f1111 100644
--- a/firmware/lib20/packed_key.c
+++ b/firmware/lib20/packed_key.c
@@ -23,9 +23,9 @@
 					key->key_offset, key->key_size);
 }
 
-int vb2_unpack_key(struct vb2_public_key *key,
-		   const uint8_t *buf,
-		   uint32_t size)
+int vb2_unpack_key_buffer(struct vb2_public_key *key,
+			  const uint8_t *buf,
+			  uint32_t size)
 {
 	const struct vb2_packed_key *packed_key =
 		(const struct vb2_packed_key *)buf;
@@ -75,3 +75,15 @@
 
 	return VB2_SUCCESS;
 }
+
+int vb2_unpack_key(struct vb2_public_key *key,
+		   const struct vb2_packed_key *packed_key)
+{
+	if (!packed_key)
+		return VB2_ERROR_UNPACK_KEY_BUFFER;
+
+	return vb2_unpack_key_buffer(key,
+				     (const uint8_t *)packed_key,
+				     packed_key->key_offset +
+				     packed_key->key_size);
+}
diff --git a/futility/cmd_show.c b/futility/cmd_show.c
index 91c24d0..bfe668c 100644
--- a/futility/cmd_show.c
+++ b/futility/cmd_show.c
@@ -177,8 +177,9 @@
 	if (state) {
 		if (!sign_key &&
 		    state->rootkey.is_valid &&
-		    VB2_SUCCESS == vb2_unpack_key(&root_key, state->rootkey.buf,
-						  state->rootkey.len)) {
+		    VB2_SUCCESS == vb2_unpack_key_buffer(&root_key,
+							 state->rootkey.buf,
+							 state->rootkey.len)) {
 			/* BIOS should have a rootkey in the GBB */
 			sign_key = &root_key;
 		}
@@ -201,10 +202,7 @@
 		retval = 1;
 
 	struct vb2_public_key data_key;
-	if (VB2_SUCCESS !=
-	    vb2_unpack_key(&data_key, (const uint8_t *)&keyblock->data_key,
-			   keyblock->data_key.key_offset +
-			   keyblock->data_key.key_size)) {
+	if (VB2_SUCCESS != vb2_unpack_key(&data_key, &keyblock->data_key)) {
 		fprintf(stderr, "Error parsing data key in %s\n", name);
 		return 1;
 	}
@@ -308,10 +306,7 @@
 		retval = 1;
 
 	struct vb2_public_key data_key;
-	if (VB2_SUCCESS !=
-	    vb2_unpack_key(&data_key, (const uint8_t *)&keyblock->data_key,
-			   keyblock->data_key.key_offset +
-			   keyblock->data_key.key_size)) {
+	if (VB2_SUCCESS != vb2_unpack_key(&data_key, &keyblock->data_key)) {
 		fprintf(stderr, "Error parsing data key in %s\n", name);
 		return 1;
 	}
@@ -510,7 +505,7 @@
 			}
 
 			if (VB2_SUCCESS !=
-			    vb2_unpack_key(&pubk2, pubkbuf, len)) {
+			    vb2_unpack_key_buffer(&pubk2, pubkbuf, len)) {
 				fprintf(stderr, "Error unpacking %s\n", optarg);
 				errorcnt++;
 				break;
diff --git a/futility/cmd_vbutil_firmware.c b/futility/cmd_vbutil_firmware.c
index 3bf3492..ef1148e 100644
--- a/futility/cmd_vbutil_firmware.c
+++ b/futility/cmd_vbutil_firmware.c
@@ -211,7 +211,7 @@
 		fprintf(stderr, "Error reading signpubkey.\n");
 		goto verify_cleanup;
 	}
-	if (VB2_SUCCESS != vb2_unpack_key(&sign_key, pubkbuf, pubklen)) {
+	if (VB2_SUCCESS != vb2_unpack_key_buffer(&sign_key, pubkbuf, pubklen)) {
 		fprintf(stderr, "Error unpacking signpubkey.\n");
 		goto verify_cleanup;
 	}
@@ -254,9 +254,7 @@
 
 	struct vb2_public_key data_key;
 	if (VB2_SUCCESS !=
-	    vb2_unpack_key(&data_key, (const uint8_t *)&keyblock->data_key,
-			   keyblock->data_key.key_offset +
-			   keyblock->data_key.key_size)) {
+	    vb2_unpack_key(&data_key, &keyblock->data_key)) {
 		fprintf(stderr, "Error parsing data key.\n");
 		goto verify_cleanup;
 	}
diff --git a/futility/cmd_vbutil_keyblock.c b/futility/cmd_vbutil_keyblock.c
index bb8d62a..f79b8ea 100644
--- a/futility/cmd_vbutil_keyblock.c
+++ b/futility/cmd_vbutil_keyblock.c
@@ -189,9 +189,7 @@
 			return 1;
 		}
 		struct vb2_public_key key;
-		if (VB2_SUCCESS !=
-		    vb2_unpack_key(&key, (uint8_t *)sign_key,
-				   sign_key->key_offset + sign_key->key_size)) {
+		if (VB2_SUCCESS != vb2_unpack_key(&key, sign_key)) {
 			fprintf(stderr,
 				"vbutil_keyblock: Error reading signpubkey.\n");
 			return 1;
diff --git a/futility/vb1_helper.c b/futility/vb1_helper.c
index 83acba5..fb9022f 100644
--- a/futility/vb1_helper.c
+++ b/futility/vb1_helper.c
@@ -501,11 +501,7 @@
 
 	if (signpub_key) {
 		struct vb2_public_key pubkey;
-		if (VB2_SUCCESS !=
-		    vb2_unpack_key(&pubkey,
-				   (uint8_t *)signpub_key,
-				   signpub_key->key_offset +
-				   signpub_key->key_size)) {
+		if (VB2_SUCCESS != vb2_unpack_key(&pubkey, signpub_key)) {
 			fprintf(stderr, "Error unpacking signing key.\n");
 			goto done;
 		}
@@ -568,9 +564,7 @@
 	}
 
 	struct vb2_public_key pubkey;
-	if (VB2_SUCCESS !=
-	    vb2_unpack_key(&pubkey, (uint8_t *)data_key,
-			   data_key->key_offset + data_key->key_size)) {
+	if (VB2_SUCCESS != vb2_unpack_key(&pubkey, data_key)) {
 		fprintf(stderr, "Error parsing data key.\n");
 		goto done;
 	}
@@ -743,9 +737,7 @@
 	/* Try unpacking the data key from the keyblock */
 	struct vb2_public_key data_key;
 	if (VB2_SUCCESS !=
-	    vb2_unpack_key(&data_key, (const uint8_t *)&keyblock->data_key,
-			   keyblock->data_key.key_offset +
-			   keyblock->data_key.key_size)) {
+	    vb2_unpack_key(&data_key, &keyblock->data_key)) {
 		/* It looks like a bad keyblock, but still a keyblock */
 		free(buf2);
 		return FILE_TYPE_KEYBLOCK;
diff --git a/host/lib/host_key.c b/host/lib/host_key.c
index 10efc76..499fe2d 100644
--- a/host/lib/host_key.c
+++ b/host/lib/host_key.c
@@ -25,7 +25,9 @@
 int packed_key_looks_ok(const struct vb2_packed_key *key, uint32_t size)
 {
 	struct vb2_public_key pubkey;
-	if (VB2_SUCCESS != vb2_unpack_key(&pubkey, (const uint8_t *)key, size))
+	if (VB2_SUCCESS != vb2_unpack_key_buffer(&pubkey,
+						 (const uint8_t *)key,
+						 size))
 		return 0;
 
 	if (key->key_version > VB2_MAX_KEY_VERSION) {
diff --git a/tests/vb20_api_kernel_tests.c b/tests/vb20_api_kernel_tests.c
index 0c8cb59..212ad93 100644
--- a/tests/vb20_api_kernel_tests.c
+++ b/tests/vb20_api_kernel_tests.c
@@ -194,7 +194,7 @@
 	return mock_load_kernel_preamble_retval;
 }
 
-int vb2_unpack_key(struct vb2_public_key *key,
+int vb2_unpack_key_buffer(struct vb2_public_key *key,
 		   const uint8_t *buf,
 		   uint32_t size)
 {
diff --git a/tests/vb20_api_tests.c b/tests/vb20_api_tests.c
index f0bc98b..a7efca9 100644
--- a/tests/vb20_api_tests.c
+++ b/tests/vb20_api_tests.c
@@ -117,7 +117,7 @@
 	return retval_vb2_load_fw_preamble;
 }
 
-int vb2_unpack_key(struct vb2_public_key *key,
+int vb2_unpack_key_buffer(struct vb2_public_key *key,
 		   const uint8_t *buf,
 		   uint32_t size)
 {
diff --git a/tests/vb20_common2_tests.c b/tests/vb20_common2_tests.c
index 00861e5..61e28d1 100644
--- a/tests/vb20_common2_tests.c
+++ b/tests/vb20_common2_tests.c
@@ -35,44 +35,49 @@
 	struct vb2_packed_key *key = (struct vb2_packed_key *)buf;
 
 	memcpy(key, key1, size);
-	TEST_SUCC(vb2_unpack_key(&pubk, buf, size), "vb2_unpack_key() ok");
+	TEST_SUCC(vb2_unpack_key_buffer(&pubk, buf, size),
+		  "vb2_unpack_key_buffer() ok");
 
 	TEST_EQ(pubk.sig_alg, vb2_crypto_to_signature(key->algorithm),
-		"vb2_unpack_key() sig_alg");
+		"vb2_unpack_key_buffer() sig_alg");
 	TEST_EQ(pubk.hash_alg, vb2_crypto_to_hash(key->algorithm),
-		"vb2_unpack_key() hash_alg");
+		"vb2_unpack_key_buffer() hash_alg");
 
 
 	memcpy(key, key1, size);
 	key->algorithm = VB2_ALG_COUNT;
-	TEST_EQ(vb2_unpack_key(&pubk, buf, size),
+	TEST_EQ(vb2_unpack_key_buffer(&pubk, buf, size),
 		VB2_ERROR_UNPACK_KEY_SIG_ALGORITHM,
-		"vb2_unpack_key() invalid algorithm");
+		"vb2_unpack_key_buffer() invalid algorithm");
 
 	memcpy(key, key1, size);
 	key->key_size--;
-	TEST_EQ(vb2_unpack_key(&pubk, buf, size),
+	TEST_EQ(vb2_unpack_key_buffer(&pubk, buf, size),
 		VB2_ERROR_UNPACK_KEY_SIZE,
-		"vb2_unpack_key() invalid size");
+		"vb2_unpack_key_buffer() invalid size");
 
 	memcpy(key, key1, size);
 	key->key_offset++;
-	TEST_EQ(vb2_unpack_key(&pubk, buf, size + 1),
+	TEST_EQ(vb2_unpack_key_buffer(&pubk, buf, size + 1),
 		VB2_ERROR_UNPACK_KEY_ALIGN,
-		"vb2_unpack_key() unaligned data");
+		"vb2_unpack_key_buffer() unaligned data");
 
 	memcpy(key, key1, size);
 	*(uint32_t *)(buf + key->key_offset) /= 2;
-	TEST_EQ(vb2_unpack_key(&pubk, buf, size),
+	TEST_EQ(vb2_unpack_key_buffer(&pubk, buf, size),
 		VB2_ERROR_UNPACK_KEY_ARRAY_SIZE,
-		"vb2_unpack_key() invalid key array size");
+		"vb2_unpack_key_buffer() invalid key array size");
 
 	memcpy(key, key1, size);
-	TEST_EQ(vb2_unpack_key(&pubk, buf, size - 1),
+	TEST_EQ(vb2_unpack_key_buffer(&pubk, buf, size - 1),
 		VB2_ERROR_INSIDE_DATA_OUTSIDE,
-		"vb2_unpack_key() buffer too small");
+		"vb2_unpack_key_buffer() buffer too small");
 
 	free(key);
+
+	TEST_EQ(vb2_unpack_key(&pubk, NULL),
+		VB2_ERROR_UNPACK_KEY_BUFFER,
+		"vb2_unpack_key_() buffer NULL");
 }
 
 static void test_verify_data(const struct vb2_packed_key *key1,
@@ -82,7 +87,6 @@
 		 __attribute__ ((aligned (VB2_WORKBUF_ALIGN)));
 	struct vb2_workbuf wb;
 
-	uint32_t pubkey_size = key1->key_offset + key1->key_size;
 	struct vb2_public_key pubk, pubk_orig;
 	uint32_t sig_total_size = sig->sig_offset + sig->sig_size;
 	struct vb2_signature *sig2;
@@ -92,8 +96,7 @@
 	/* Allocate signature copy for tests */
 	sig2 = (struct vb2_signature *)malloc(sig_total_size);
 
-	TEST_EQ(vb2_unpack_key(&pubk, (uint8_t *)key1, pubkey_size),
-		0, "vb2_verify_data() unpack key");
+	TEST_SUCC(vb2_unpack_key(&pubk, key1), "vb2_verify_data() unpack key");
 	pubk_orig = pubk;
 
 	memcpy(sig2, sig, sig_total_size);
diff --git a/tests/vb20_common3_tests.c b/tests/vb20_common3_tests.c
index e553867..b5ad1eb 100644
--- a/tests/vb20_common3_tests.c
+++ b/tests/vb20_common3_tests.c
@@ -216,8 +216,7 @@
 	/* Create a dummy signature */
 	struct vb2_signature *body_sig = vb2_alloc_signature(56, 78);
 
-	TEST_SUCC(vb2_unpack_key(&rsa, (uint8_t *)public_key,
-				 public_key->key_offset + public_key->key_size),
+	TEST_SUCC(vb2_unpack_key(&rsa, public_key),
 		  "vb2_verify_fw_preamble() prereq key");
 
 	hdr = vb2_create_fw_preamble(0x1234, kernel_subkey, body_sig,
@@ -359,8 +358,7 @@
 	/* Create a dummy signature */
 	struct vb2_signature *body_sig = vb2_alloc_signature(56, 0x214000);
 
-	TEST_SUCC(vb2_unpack_key(&rsa, (uint8_t *)public_key,
-				 public_key->key_offset + public_key->key_size),
+	TEST_SUCC(vb2_unpack_key(&rsa, public_key),
 		  "vb2_verify_kernel_preamble() prereq key");
 
 	struct vb2_kernel_preamble *hdr =
@@ -563,7 +561,7 @@
 	/* Unpack public key */
 	struct vb2_public_key signing_public_key2;
 	if (VB2_SUCCESS !=
-	    vb2_unpack_key(&signing_public_key2,
+	    vb2_unpack_key_buffer(&signing_public_key2,
 			   (uint8_t *)signing_public_key,
 			   signing_public_key->key_offset +
 			   signing_public_key->key_size)) {
diff --git a/tests/vb20_kernel_tests.c b/tests/vb20_kernel_tests.c
index 2304b32..2823050 100644
--- a/tests/vb20_kernel_tests.c
+++ b/tests/vb20_kernel_tests.c
@@ -167,7 +167,7 @@
 	return VB2_SUCCESS;
 }
 
-int vb2_unpack_key(struct vb2_public_key *key,
+int vb2_unpack_key_buffer(struct vb2_public_key *key,
 		   const uint8_t *buf,
 		   uint32_t size)
 {
diff --git a/tests/vb20_misc_tests.c b/tests/vb20_misc_tests.c
index e3a5123..2156e91 100644
--- a/tests/vb20_misc_tests.c
+++ b/tests/vb20_misc_tests.c
@@ -146,7 +146,7 @@
 	return VB2_SUCCESS;
 }
 
-int vb2_unpack_key(struct vb2_public_key *key,
+int vb2_unpack_key_buffer(struct vb2_public_key *key,
 		   const uint8_t *buf,
 		   uint32_t size)
 {
diff --git a/tests/vb20_rsa_padding_tests.c b/tests/vb20_rsa_padding_tests.c
index dbfdc38..7c1df6a 100644
--- a/tests/vb20_rsa_padding_tests.c
+++ b/tests/vb20_rsa_padding_tests.c
@@ -114,9 +114,7 @@
 		fprintf(stderr, "Couldn't read RSA public key for the test.\n");
 		return 1;
 	}
-
-	if (VB2_SUCCESS != vb2_unpack_key(&k2, (const uint8_t *)pk,
-					  pk->key_offset + pk->key_size)) {
+	if (VB2_SUCCESS != vb2_unpack_key(&k2, pk)) {
 		fprintf(stderr, "Couldn't unpack RSA public key.\n");
 		free(pk);
 		return 1;
diff --git a/tests/vboot_api_kernel5_tests.c b/tests/vboot_api_kernel5_tests.c
index 8633f7e..077c07a 100644
--- a/tests/vboot_api_kernel5_tests.c
+++ b/tests/vboot_api_kernel5_tests.c
@@ -96,7 +96,7 @@
 }
 
 /* Mocks */
-int vb2_unpack_key(struct vb2_public_key *key,
+int vb2_unpack_key_buffer(struct vb2_public_key *key,
 		   const uint8_t *buf,
 		   uint32_t size)
 {
diff --git a/tests/vboot_kernel_tests.c b/tests/vboot_kernel_tests.c
index 057e2dd..21ee895 100644
--- a/tests/vboot_kernel_tests.c
+++ b/tests/vboot_kernel_tests.c
@@ -243,7 +243,7 @@
 	memcpy(dest, fake_guid, sizeof(fake_guid));
 }
 
-int vb2_unpack_key(struct vb2_public_key *key,
+int vb2_unpack_key_buffer(struct vb2_public_key *key,
 		   const uint8_t *buf,
 		   uint32_t size)
 {
diff --git a/utility/verify_data.c b/utility/verify_data.c
index a3ab277..50eeb9d 100644
--- a/utility/verify_data.c
+++ b/utility/verify_data.c
@@ -97,8 +97,7 @@
 	}
 
 	struct vb2_public_key k2;
-	if (VB2_SUCCESS != vb2_unpack_key(&k2, (const uint8_t *)pk,
-					  pk->key_offset + pk->key_size)) {
+	if (VB2_SUCCESS != vb2_unpack_key(&k2, pk)) {
 		fprintf(stderr, "Can't unpack RSA public key.\n");
 		goto error;
 	}