Skip to content

Commit 452c708

Browse files
mayuanchenmaWilliam Roberts
authored andcommitted
tools/tpm2_createek: add sm2 ek template support.
Signed-off-by: mayuanchen <94815698+mayuanchenma@users.noreply.github.com>
1 parent 05a5cbf commit 452c708

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

tools/tpm2_createek.c

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define RSA_EK_TEMPLATE_NV_INDEX 0x01c00004
1919
#define ECC_EK_NONCE_NV_INDEX 0x01c0000b
2020
#define ECC_EK_TEMPLATE_NV_INDEX 0x01c0000c
21+
#define ECC_SM2_EK_TEMPLATE_NV_INDEX 0x01c0001b
2122

2223
#define DEFAULT_KEY_ALG "rsa2048"
2324

@@ -175,7 +176,8 @@ static tool_rc init_ek_public(const char *key_alg, TPM2B_PUBLIC *public) {
175176
public->publicArea.authPolicy = *m->policy;
176177

177178
if (public->publicArea.type == TPM2_ALG_ECC &&
178-
public->publicArea.parameters.eccDetail.curveID == TPM2_ECC_NIST_P256) {
179+
(public->publicArea.parameters.eccDetail.curveID == TPM2_ECC_NIST_P256 ||
180+
public->publicArea.parameters.eccDetail.curveID == TPM2_ECC_SM2_P256)) {
179181
public->publicArea.unique.ecc.x.size = 32;
180182
public->publicArea.unique.ecc.y.size = 32;
181183
} else if (public->publicArea.type == TPM2_ALG_RSA &&
@@ -195,8 +197,17 @@ static tool_rc set_ek_template(ESYS_CONTEXT *ectx, TPM2B_PUBLIC *input_public) {
195197
nonce_nv_index = RSA_EK_NONCE_NV_INDEX;
196198
break;
197199
case TPM2_ALG_ECC:
198-
template_nv_index = ECC_EK_TEMPLATE_NV_INDEX;
199-
nonce_nv_index = ECC_EK_NONCE_NV_INDEX;
200+
if (input_public->publicArea.parameters.eccDetail.curveID == TPM2_ECC_NIST_P256) {
201+
template_nv_index = ECC_EK_TEMPLATE_NV_INDEX;
202+
nonce_nv_index = ECC_EK_NONCE_NV_INDEX;
203+
} else if (input_public->publicArea.parameters.eccDetail.curveID == TPM2_ECC_SM2_P256) {
204+
template_nv_index = ECC_SM2_EK_TEMPLATE_NV_INDEX;
205+
// EK Nonces SHALL NOT be Populated in any NV Index in the High Range.
206+
nonce_nv_index = 0;
207+
} else {
208+
template_nv_index = ECC_EK_TEMPLATE_NV_INDEX;
209+
nonce_nv_index = ECC_EK_NONCE_NV_INDEX;
210+
}
200211
break;
201212
default:
202213
LOG_ERR("EK template and EK nonce for algorithm type input(%4.4x)"
@@ -228,21 +239,25 @@ static tool_rc set_ek_template(ESYS_CONTEXT *ectx, TPM2B_PUBLIC *input_public) {
228239

229240
// Read EK nonce
230241
UINT16 nonce_size = 0;
231-
rc = tpm2_util_nv_read(ectx, nonce_nv_index, 0, 0,
232-
&ctx.auth_owner_hierarchy.object, &nonce, &nonce_size, &cp_hash,
233-
&rp_hash, TPM2_ALG_SHA256, 0, ESYS_TR_NONE, ESYS_TR_NONE, NULL);
234-
if (rc != tool_rc_success) {
235-
goto out;
242+
if (nonce_nv_index) {
243+
rc = tpm2_util_nv_read(ectx, nonce_nv_index, 0, 0,
244+
&ctx.auth_owner_hierarchy.object, &nonce, &nonce_size, &cp_hash,
245+
&rp_hash, TPM2_ALG_SHA256, 0, ESYS_TR_NONE, ESYS_TR_NONE, NULL);
246+
if (rc != tool_rc_success) {
247+
goto out;
248+
}
236249
}
237250

238251
if (input_public->publicArea.type == TPM2_ALG_RSA) {
239252
memcpy(&input_public->publicArea.unique.rsa.buffer, &nonce, nonce_size);
240253
input_public->publicArea.unique.rsa.size = 256;
241254
} else {
242255
// ECC is only other supported algorithm
243-
memcpy(&input_public->publicArea.unique.ecc.x.buffer, &nonce, nonce_size);
244-
input_public->publicArea.unique.ecc.x.size = 32;
245-
input_public->publicArea.unique.ecc.y.size = 32;
256+
if (nonce_size) {
257+
memcpy(&input_public->publicArea.unique.ecc.x.buffer, &nonce, nonce_size);
258+
input_public->publicArea.unique.ecc.x.size = 32;
259+
input_public->publicArea.unique.ecc.y.size = 32;
260+
}
246261
}
247262

248263
out: if (template) {

0 commit comments

Comments
 (0)