Skip to content

Commit 2467d6f

Browse files
tss2_sign: Add new parameter from which the digest is computed.
* The parameter --data (-m) can be used to compute the digest from the this data if the new function Fapi_DigestAndSign is available. * Add conditional integration test depending on the FAPI version. Signed-off-by: Juergen Repp <juergen_repp@web.de>
1 parent c9a5dff commit 2467d6f

File tree

4 files changed

+80
-13
lines changed

4 files changed

+80
-13
lines changed

configure.ac

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ AC_CHECK_LIB(crypto, [EVP_sm3], [
8282
AC_CHECK_LIB(crypto, [EVP_sm4_cfb128], [
8383
AC_DEFINE([HAVE_EVP_SM4_CFB], [1], [Support EVP_sm4_cfb in openssl])],
8484
[])
85+
AC_CHECK_LIB(tss2-fapi, [Fapi_DigestAndSign], [
86+
AC_DEFINE([HAVE_FAPI_DIGEST_AND_SIGN], [1], [Support signing with restricted keys])],
87+
[])
8588
LIBS="${LIBS_save}"
8689
PKG_CHECK_MODULES([CURL], [libcurl])
8790

man/tss2_sign.1.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717
**tss2_sign**(1) - This command uses a key inside the TPM to sign a digest value
1818
using the TPM signing schemes as specified in the cryptographic profile
19-
(cf., **fapi-profile(5)**).
19+
(cf., **fapi-profile(5)**). The digest can be provided as an argument
20+
or computed from data passed as an argument.
2021

2122
# OPTIONS
2223

@@ -40,6 +41,10 @@ These are the available options:
4041

4142
The data to be signed, already hashed.
4243

44+
* **-m**, **\--data**=_FILENAME_ or _-_ (for stdin):
45+
46+
The data from which the hash is computed, which is then signed.
47+
4348
* **-f**, **\--force**:
4449

4550
Force overwriting the output file.

test/integration/fapi/fapi-sign-verify.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,4 +329,27 @@ if {[lindex \$ret 2] || [lindex \$ret 3] != 1} {
329329
}
330330
EOF
331331

332+
# Check tss2_sign which computes the digest from data if the FAPI function
333+
# Fapi_DigestAndSign is available
334+
335+
DATA_FILE=$TEMP_DIR/data.file
336+
dd if=/dev/zero of=$DATA_FILE bs=1 count=1024
337+
if [ "$CRYPTO_PROFILE" = "RSA" ]; then
338+
PADDING="--padding=RSA_PSS"
339+
fi
340+
if nm -D $(ldconfig -p | grep "libtss2-fapi" | head -n1 | awk '{print $NF}') | \
341+
grep "Fapi_DigestAndSign";
342+
then
343+
tss2 sign --data=$DATA_FILE --keyPath=$KEY_PATH $PADDING \
344+
--signature=$SIGNATURE_FILE --publicKey=$PUBLIC_KEY_FILE -f
345+
346+
shasum -a 256 $DATA_FILE | awk '{ $1 }' | xxd -r -p > $DIGEST_FILE
347+
tss2 verifysignature --keyPath=$PUB_KEY_DIR/$IMPORTED_KEY_NAME \
348+
--digest=$DIGEST_FILE --signature=$SIGNATURE_FILE
349+
else
350+
# The sign should fail because Fapi_DigestAndSign is not available
351+
! tss2 sign --data=$DATA_FILE --keyPath=$KEY_PATH $PADDING \
352+
--signature=$SIGNATURE_FILE --publicKey=$PUBLIC_KEY_FILE
353+
fi
354+
332355
exit 0

tools/fapi/tss2_sign.c

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
static struct cxt {
1212
char const *keyPath;
1313
char const *digest;
14+
char const *data;
1415
char const *signature;
1516
char const *publicKey;
1617
char const *certificate;
@@ -27,6 +28,9 @@ static bool on_option(char key, char *value) {
2728
case 'd':
2829
ctx.digest = value;
2930
break;
31+
case 'm':
32+
ctx.data = value;
33+
break;
3034
case 'f':
3135
ctx.overwrite = true;
3236
break;
@@ -52,22 +56,34 @@ static bool tss2_tool_onstart(tpm2_options **opts) {
5256
{"keyPath", required_argument, NULL, 'p'},
5357
{"padding", required_argument, NULL, 's'},
5458
{"digest", required_argument, NULL, 'd'},
59+
{"data", required_argument, NULL, 'm'},
5560
{"signature", required_argument, NULL, 'o'},
5661
{"publicKey", required_argument, NULL, 'k'},
5762
{"force", no_argument , NULL, 'f'},
5863
{"certificate", required_argument, NULL, 'c'},
5964

6065
};
61-
return (*opts = tpm2_options_new ("c:d:fp:k:o:s:", ARRAY_LEN(topts), topts,
66+
return (*opts = tpm2_options_new ("c:d:m:fp:k:o:s:", ARRAY_LEN(topts), topts,
6267
on_option, NULL, 0)) != NULL;
6368
}
6469

6570
/* Execute specific tool */
6671
static int tss2_tool_onrun (FAPI_CONTEXT *fctx) {
6772

6873
/* Check availability of required parameters */
69-
if (!ctx.digest) {
70-
fprintf (stderr, "digest missing, use --digest\n");
74+
75+
#ifndef HAVE_FAPI_DIGEST_AND_SIGN
76+
if (ctx.data) {
77+
fprintf (stderr, "Fapi_DigestAndSign not available in the current FAPI version.\n");
78+
return -1;
79+
}
80+
#endif
81+
if (!ctx.digest && !ctx.data) {
82+
fprintf (stderr, "digest or dataa missing, use --digest or --data\n");
83+
return -1;
84+
}
85+
if (ctx.digest && ctx.data) {
86+
fprintf (stderr, "use --digest or --data\n");
7187
return -1;
7288
}
7389
if (!ctx.keyPath) {
@@ -91,23 +107,43 @@ static int tss2_tool_onrun (FAPI_CONTEXT *fctx) {
91107
}
92108

93109
/* Read data needed to create signature */
94-
uint8_t *digest, *signature;
95-
size_t digestSize, signatureSize;
96-
char *publicKey, *certificate = NULL;
97-
TSS2_RC r = open_read_and_close (ctx.digest, (void**)&digest, &digestSize);
110+
uint8_t *data = NULL, *digest = NULL, *signature = NULL;
111+
size_t dataSize = 0, digestSize = 0, signatureSize;
112+
char *publicKey = NULL, *certificate = NULL;
113+
TSS2_RC r;
114+
if (ctx.digest) {
115+
r = open_read_and_close (ctx.digest, (void**)&digest, &digestSize);
116+
} else {
117+
r = open_read_and_close (ctx.data, (void**)&data, &dataSize);
118+
}
119+
98120
if (r){
99121
return 1;
100122
}
101123

102124
/* Execute FAPI command with passed arguments */
103-
r = Fapi_Sign (fctx, ctx.keyPath, ctx.padding, digest,
125+
if (ctx.digest) {
126+
r = Fapi_Sign (fctx, ctx.keyPath, ctx.padding, digest,
104127
digestSize, &signature, &signatureSize, &publicKey, &certificate);
105-
if (r != TSS2_RC_SUCCESS) {
106-
LOG_PERR ("Fapi_Sign", r);
128+
if (r != TSS2_RC_SUCCESS) {
129+
LOG_PERR ("Fapi_Sign", r);
130+
free (digest);
131+
return 1;
132+
}
107133
free (digest);
108-
return 1;
109134
}
110-
free (digest);
135+
#ifdef HAVE_FAPI_DIGEST_AND_SIGN
136+
else if (ctx.data) {
137+
r = Fapi_DigestAndSign (fctx, ctx.keyPath, ctx.padding, data,
138+
dataSize, &signature, &signatureSize, &publicKey, &certificate);
139+
if (r != TSS2_RC_SUCCESS) {
140+
LOG_PERR ("Fapi_Sign", r);
141+
free (data);
142+
return 1;
143+
}
144+
free (data);
145+
}
146+
#endif
111147

112148
/* Write returned data to file(s) */
113149
if (ctx.certificate && certificate && strlen(certificate)) {

0 commit comments

Comments
 (0)