* [RFCv3 08/13] KEYS: asym_tpm: Add loadkey2 and flushspecific
@ 2018-08-02 18:31 Denis Kenzior
2018-08-07 20:07 ` James Morris
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: Denis Kenzior @ 2018-08-02 18:31 UTC (permalink / raw)
To: keyrings
This commit adds TPM_LoadKey2 and TPM_FlushSpecific operations.
Signed-off-by: Denis Kenzior <denkenz@gmail.com>
---
crypto/asymmetric_keys/asym_tpm.c | 94 +++++++++++++++++++++++++++++++++++++++
1 file changed, 94 insertions(+)
diff --git a/crypto/asymmetric_keys/asym_tpm.c b/crypto/asymmetric_keys/asym_tpm.c
index b41ff0a07bab..9aed598dd9b0 100644
--- a/crypto/asymmetric_keys/asym_tpm.c
+++ b/crypto/asymmetric_keys/asym_tpm.c
@@ -7,11 +7,105 @@
#include <linux/seq_file.h>
#include <linux/scatterlist.h>
#include <linux/tpm.h>
+#include <linux/tpm_command.h>
#include <crypto/akcipher.h>
+#include <crypto/hash.h>
+#include <crypto/sha.h>
#include <asm/unaligned.h>
#include <keys/asymmetric-subtype.h>
+#include "../../security/keys/trusted.h"
#include <crypto/asym_tpm_subtype.h>
+#define TPM_ORD_FLUSHSPECIFIC 186
+#define TPM_ORD_LOADKEY2 65
+#define TPM_LOADKEY2_SIZE 59
+#define TPM_FLUSHSPECIFIC_SIZE 18
+
+#define TPM_RT_KEY 0x00000001
+
+/*
+ * Load a TPM key from the blob provided by userspace
+ */
+static int tpm_loadkey2(struct tpm_buf *tb,
+ uint32_t keyhandle, unsigned char *keyauth,
+ const unsigned char *keyblob, int keybloblen,
+ uint32_t *newhandle)
+{
+ unsigned char nonceodd[TPM_NONCE_SIZE];
+ unsigned char enonce[TPM_NONCE_SIZE];
+ unsigned char authdata[SHA1_DIGEST_SIZE];
+ uint32_t authhandle = 0;
+ unsigned char cont = 0;
+ uint32_t ordinal;
+ int ret;
+
+ ordinal = htonl(TPM_ORD_LOADKEY2);
+
+ /* session for loading the key */
+ ret = oiap(tb, &authhandle, enonce);
+ if (ret < 0) {
+ pr_info("oiap failed (%d)\n", ret);
+ return ret;
+ }
+
+ /* generate odd nonce */
+ ret = tpm_get_random(NULL, nonceodd, TPM_NONCE_SIZE);
+ if (ret < 0) {
+ pr_info("tpm_get_random failed (%d)\n", ret);
+ return ret;
+ }
+
+ /* calculate authorization HMAC value */
+ ret = TSS_authhmac(authdata, keyauth, SHA1_DIGEST_SIZE, enonce,
+ nonceodd, cont, sizeof(uint32_t), &ordinal,
+ keybloblen, keyblob, 0, 0);
+ if (ret < 0)
+ return ret;
+
+ /* build the request buffer */
+ INIT_BUF(tb);
+ store16(tb, TPM_TAG_RQU_AUTH1_COMMAND);
+ store32(tb, TPM_LOADKEY2_SIZE + keybloblen);
+ store32(tb, TPM_ORD_LOADKEY2);
+ store32(tb, keyhandle);
+ storebytes(tb, keyblob, keybloblen);
+ store32(tb, authhandle);
+ storebytes(tb, nonceodd, TPM_NONCE_SIZE);
+ store8(tb, cont);
+ storebytes(tb, authdata, SHA1_DIGEST_SIZE);
+
+ ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE);
+ if (ret < 0) {
+ pr_info("authhmac failed (%d)\n", ret);
+ return ret;
+ }
+
+ ret = TSS_checkhmac1(tb->data, ordinal, nonceodd, keyauth,
+ SHA1_DIGEST_SIZE, 0, 0);
+ if (ret < 0) {
+ pr_info("TSS_checkhmac1 failed (%d)\n", ret);
+ return ret;
+ }
+
+ *newhandle = LOAD32(tb->data, TPM_DATA_OFFSET);
+ return 0;
+}
+
+/*
+ * Execute the FlushSpecific TPM command
+ */
+static uint32_t tpm_flushspecific(struct tpm_buf *tb, uint32_t handle)
+{
+ INIT_BUF(tb);
+ store16(tb, TPM_TAG_RQU_COMMAND);
+ store32(tb, TPM_FLUSHSPECIFIC_SIZE);
+ store32(tb, TPM_ORD_FLUSHSPECIFIC);
+ store32(tb, handle);
+ store32(tb, TPM_RT_KEY);
+
+ return trusted_tpm_send(tb->data, MAX_BUF_SIZE);
+}
+
/*
* Maximum buffer size for the BER/DER encoded public key. The public key
* is of the form SEQUENCE { INTEGER n, INTEGER e } where n is a maximum 2048
--
2.16.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [RFCv3 08/13] KEYS: asym_tpm: Add loadkey2 and flushspecific
2018-08-02 18:31 [RFCv3 08/13] KEYS: asym_tpm: Add loadkey2 and flushspecific Denis Kenzior
@ 2018-08-07 20:07 ` James Morris
2018-08-08 15:46 ` David Howells
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: James Morris @ 2018-08-07 20:07 UTC (permalink / raw)
To: keyrings
On Thu, 2 Aug 2018, Denis Kenzior wrote:
> This commit adds TPM_LoadKey2 and TPM_FlushSpecific operations.
>
> Signed-off-by: Denis Kenzior <denkenz@gmail.com>
Reviewed-by: James Morris <james.morris@microsoft.com>
--
James Morris
<jmorris@namei.org>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [RFCv3 08/13] KEYS: asym_tpm: Add loadkey2 and flushspecific
2018-08-02 18:31 [RFCv3 08/13] KEYS: asym_tpm: Add loadkey2 and flushspecific Denis Kenzior
2018-08-07 20:07 ` James Morris
@ 2018-08-08 15:46 ` David Howells
2018-08-08 15:49 ` Denis Kenzior
2018-08-08 15:53 ` James Bottomley
3 siblings, 0 replies; 5+ messages in thread
From: David Howells @ 2018-08-08 15:46 UTC (permalink / raw)
To: keyrings
Denis Kenzior <denkenz@gmail.com> wrote:
> +#include "../../security/keys/trusted.h"
Should this be moved to include/keys/ somewhere, at least in part?
David
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [RFCv3 08/13] KEYS: asym_tpm: Add loadkey2 and flushspecific
2018-08-02 18:31 [RFCv3 08/13] KEYS: asym_tpm: Add loadkey2 and flushspecific Denis Kenzior
2018-08-07 20:07 ` James Morris
2018-08-08 15:46 ` David Howells
@ 2018-08-08 15:49 ` Denis Kenzior
2018-08-08 15:53 ` James Bottomley
3 siblings, 0 replies; 5+ messages in thread
From: Denis Kenzior @ 2018-08-08 15:49 UTC (permalink / raw)
To: keyrings
Hi David,
On 08/08/2018 10:46 AM, David Howells wrote:
> Denis Kenzior <denkenz@gmail.com> wrote:
>
>> +#include "../../security/keys/trusted.h"
>
> Should this be moved to include/keys/ somewhere, at least in part?
>
That is one of the questions in my mind as well. Should we factor out
the core TPM primitives out of trusted.[ch] into a separate library /
module or just have asym_tpm depend on the trusted subtype. I have no
real preference here as long as the stuff in trusted.[ch] can be shared
somehow.
If you want to simply move trusted.h to include/keys that would be good
with me as well.
Regards,
-Denis
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [RFCv3 08/13] KEYS: asym_tpm: Add loadkey2 and flushspecific
2018-08-02 18:31 [RFCv3 08/13] KEYS: asym_tpm: Add loadkey2 and flushspecific Denis Kenzior
` (2 preceding siblings ...)
2018-08-08 15:49 ` Denis Kenzior
@ 2018-08-08 15:53 ` James Bottomley
3 siblings, 0 replies; 5+ messages in thread
From: James Bottomley @ 2018-08-08 15:53 UTC (permalink / raw)
To: keyrings
On Wed, 2018-08-08 at 10:49 -0500, Denis Kenzior wrote:
> Hi David,
>
> On 08/08/2018 10:46 AM, David Howells wrote:
> > Denis Kenzior <denkenz@gmail.com> wrote:
> >
> > > +#include "../../security/keys/trusted.h"
> >
> > Should this be moved to include/keys/ somewhere, at least in part?
> >
>
> That is one of the questions in my mind as well. Should we factor
> out the core TPM primitives out of trusted.[ch] into a separate
> library / module or just have asym_tpm depend on the trusted
> subtype. I have no real preference here as long as the stuff in
> trusted.[ch] can be shared somehow.
>
> If you want to simply move trusted.h to include/keys that would be
> good with me as well.
I'm happy with that. The trusted code is a bit of a mess at the
moment: all the tpm1.2 handling is inside trusted.c but the tpm2
handling is done by callouts to drivers/char/tpm/
I think, ideally, the tpm1.2 pieces should similarly move into
drivers/char/tpm/
James
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2018-08-08 15:53 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-08-02 18:31 [RFCv3 08/13] KEYS: asym_tpm: Add loadkey2 and flushspecific Denis Kenzior
2018-08-07 20:07 ` James Morris
2018-08-08 15:46 ` David Howells
2018-08-08 15:49 ` Denis Kenzior
2018-08-08 15:53 ` James Bottomley
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.