Topics

[PATCH 1/4] tpm2-common: add point conversion routines

James Bottomley
 

Point conversion from openssl to TPM format is now used in a couple of
places in the current code, so move it into the common routines.

Signed-off-by: James Bottomley <James.Bottomley@...>
---
e_tpm2-ecc.c | 22 +++-------------------
tpm2-common.c | 26 ++++++++++++++++++++++++++
tpm2-common.h | 2 ++
3 files changed, 31 insertions(+), 19 deletions(-)

diff --git a/e_tpm2-ecc.c b/e_tpm2-ecc.c
index e0b0c47..21a636c 100644
--- a/e_tpm2-ecc.c
+++ b/e_tpm2-ecc.c
@@ -236,26 +236,11 @@ static int tpm2_ecc_compute_key(unsigned char **psec, size_t *pseclen,
TPM_SE sessionType;
char *auth;
size_t len;
- const EC_GROUP *group;
- BN_CTX *ctx;
- unsigned char point[MAX_ECC_KEY_BYTES*2 + 1];
int num_commands;
struct policy_command *commands;
TPM_ALG_ID nameAlg;
int ret;

- group = EC_KEY_get0_group(eck);
- ctx = BN_CTX_new();
- if (!ctx)
- return 0;
- BN_CTX_start(ctx);
- len = EC_POINT_point2oct(group, pt, POINT_CONVERSION_UNCOMPRESSED,
- point, sizeof(point), ctx);
- BN_CTX_free(ctx);
-
- len--;
- len >>= 1;
-
in.keyHandle = tpm2_load_key_from_ecc(eck, &tssContext, &auth,
&sessionType, &num_commands,
&commands, &nameAlg);
@@ -263,10 +248,9 @@ static int tpm2_ecc_compute_key(unsigned char **psec, size_t *pseclen,
fprintf(stderr, "Failed to get Key Handle in TPM EC key routines\n");
return 0;
}
- memcpy(in.inPoint.point.x.t.buffer, point + 1, len);
- in.inPoint.point.x.t.size = len;
- memcpy(in.inPoint.point.y.t.buffer, point + 1 + len, len);
- in.inPoint.point.y.t.size = len;
+ len = tpm2_get_public_point(&in.inPoint, EC_KEY_get0_group(eck), pt);
+ if (!len)
+ return 0;

ret = 0;
rc = tpm2_get_session_handle(tssContext, &authHandle, 0, sessionType,
diff --git a/tpm2-common.c b/tpm2-common.c
index 1b1eead..9d1737b 100644
--- a/tpm2-common.c
+++ b/tpm2-common.c
@@ -1022,3 +1022,29 @@ TPM_RC tpm2_create(TSS_CONTEXT **tsscp, const char *dir)

return TPM_RC_SUCCESS;
}
+
+int tpm2_get_public_point(TPM2B_ECC_POINT *tpmpt, const EC_GROUP *group,
+ const EC_POINT *pt)
+{
+ BN_CTX *ctx;
+ size_t len;
+ unsigned char point[MAX_ECC_KEY_BYTES*2 + 1];
+
+ ctx = BN_CTX_new();
+ if (!ctx)
+ return 0;
+ BN_CTX_start(ctx);
+ len = EC_POINT_point2oct(group, pt, POINT_CONVERSION_UNCOMPRESSED,
+ point, sizeof(point), ctx);
+ BN_CTX_free(ctx);
+
+ len--;
+ len >>= 1;
+
+ memcpy(tpmpt->point.x.t.buffer, point + 1, len);
+ tpmpt->point.x.t.size = len;
+ memcpy(tpmpt->point.y.t.buffer, point + 1 + len, len);
+ tpmpt->point.y.t.size = len;
+
+ return len;
+}
diff --git a/tpm2-common.h b/tpm2-common.h
index 14aae40..1cf3b23 100644
--- a/tpm2-common.h
+++ b/tpm2-common.h
@@ -42,4 +42,6 @@ TPM_RC tpm2_create(TSS_CONTEXT **tsscp, const char *dir);
TPM_RC tpm2_readpublic(TSS_CONTEXT *tssContext, TPM_HANDLE handle,
TPMT_PUBLIC *pub);
void tpm2_rm_tssdir(const char *dir, TPM_HANDLE extrakey);
+int tpm2_get_public_point(TPM2B_ECC_POINT *tpmpt, const EC_GROUP *group,
+ const EC_POINT *pt);
#endif
--
2.16.4