Skip to contentSkip to conversation

keys: trusted and encrypted keys

PATCH In review Mimi Zohar 3 participants 9 messages Oct 11, 2010 – Nov 2, 2010

Subsystems (7)
lore
Share & tools
lore

Mimi Zohar

author
#0 lore raw

cover keys: trusted and encrypted keys

Since the original posting, the encrypted keys are started at late_initcall, in order to wait for the crypto API to be initialized, minor changes were made to Kconfig, Roberto Sassu's fixes (adding missing 'update' keyword, freeing allocated memory on failure) were merged, increased the data size limit, based on Roberto's request, and added Rajiv Andrade's ack for the TPM patch.

Trusted and Encrypted Keys are two new key types added to the existing kernel key ring service. Both of these new types are variable length symmetic keys, and in both cases all keys are created in the kernel, and user space sees, stores, and loads only encrypted blobs. Trusted Keys require the availability of a Trusted Platform Module (TPM) chip for greater security, while Encrypted Keys can be used on any system. All user level blobs, are displayed and loaded in hex ascii for convenience, and are integrity verified.

Trusted Keys use a TPM both to generate and to seal the keys. Keys are sealed under a 2048 bit RSA key in the TPM, and optionally sealed to specified PCR (integrity measurement) values, and only unsealed by the TPM, if PCRs and blob integrity verifications match. A loaded Trusted Key can be updated with new (future) PCR values, so keys are easily migrated to new pcr values, such as when the kernel and initramfs are updated. The same key can have many saved blobs under different PCR values, so multiple boots are easily supported.

Trusted Keys are sealed under the SRK, which must have the default authorization value (20 zeros). This can be set at takeownership time with the trouser's utility "tpm_takeownership -u -z".

Usage:
keyctl add trusted name "NEW keylen [hex_pcrinfo]" ring keyctl add trusted name "LOAD hex_blob" ring keyctl update key "UPDATE hex_pcrinfo"
keyctl print keyid
keyctl pipe keyid > filename

The key length for new keys are always in bytes. Trusted Keys can be 32 - 128 bytes (256 - 1024 bits), the upper limit is to fit within the 2048 bit SRK (RSA) keylength, with all necessary structure/padding. The keywords NEW, LOAD, and UPDATE can be all upper or all lower case.

Encrypted keys do not depend on a TPM, and are faster, as they use AES for encryption/decryption. New keys are created from kernel generated random numbers, and are encrypted/decrypted using a specified 'master' key. The 'master' key can either be a trusted-key or user-key type. The main disadvantage of encrypted keys is that if they are not rooted in a trusted key, they are only as secure as the user key encrypting them. The master user key should therefore be loaded in as secure a way as possible, preferably early in boot.

Usage:
keyctl add encrypted name "NEW master-key-name keylen" ring keyctl add encrypted name "LOAD master-key-name keylen hex_blob" ring keyctl update keyid "UPDATE master-key-name"

The initial consumer of trusted keys is EVM, which at boot time needs a high quality symmetric key for HMAC protection of file metadata. The use of a trusted key provides strong guarantees that the EVM key has not been compromised by a user level problem, and when sealed to specific boot PCR values, protects against boot and offline attacks. Other uses for trusted and encrypted keys, such as for disk and file encryption are anticipated.

Mimi Zohar
Dave Safford

Mimi Zohar (4):
lib: hex2bin converts ascii hexadecimal string to binary
key: add tpm_send command
keys: add new trusted key-type
keys: add new key-type encrypted

 drivers/char/tpm/tpm.c            |   17 +
 include/keys/encrypted-type.h     |   30 ++
 include/keys/trusted-type.h       |   33 ++
 include/linux/kernel.h            |    1 +
 include/linux/tpm.h               |    3 +
 lib/hexdump.c                     |   16 +
 security/Kconfig                  |   31 ++
 security/keys/Makefile            |    2 +
 security/keys/encrypted_defined.c |  782 +++++++++++++++++++++++++++++
 security/keys/encrypted_defined.h |   52 ++
 security/keys/trusted_defined.c   |  997 +++++++++++++++++++++++++++++++++++++
 security/keys/trusted_defined.h   |  125 +++++
 12 files changed, 2089 insertions(+), 0 deletions(-)
 create mode 100644 include/keys/encrypted-type.h
 create mode 100644 include/keys/trusted-type.h
 create mode 100644 security/keys/encrypted_defined.c
 create mode 100644 security/keys/encrypted_defined.h
 create mode 100644 security/keys/trusted_defined.c
 create mode 100644 security/keys/trusted_defined.h
Signature · 30 lines
1.7.2.2

Mimi Zohar (4):
  lib: hex2bin converts ascii hexadecimal string to binary
  key: add tpm_send command
  keys: add new trusted key-type
  keys: add new key-type encrypted

 drivers/char/tpm/tpm.c            |   17 +
 include/keys/encrypted-type.h     |   30 ++
 include/keys/trusted-type.h       |   33 ++
 include/linux/kernel.h            |    1 +
 include/linux/tpm.h               |    3 +
 lib/hexdump.c                     |   16 +
 security/Kconfig                  |   31 ++
 security/keys/Makefile            |    2 +
 security/keys/encrypted_defined.c |  804 +++++++++++++++++++++++++++++
 security/keys/encrypted_defined.h |   52 ++
 security/keys/trusted_defined.c   |  999 +++++++++++++++++++++++++++++++++++++
 security/keys/trusted_defined.h   |  125 +++++
 12 files changed, 2113 insertions(+), 0 deletions(-)
 create mode 100644 include/keys/encrypted-type.h
 create mode 100644 include/keys/trusted-type.h
 create mode 100644 security/keys/encrypted_defined.c
 create mode 100644 security/keys/encrypted_defined.h
 create mode 100644 security/keys/trusted_defined.c
 create mode 100644 security/keys/trusted_defined.h

-- 
1.7.2.2
author
#2 lore raw

PATCH 2/4 key: add tpm_send command

Add internal kernel tpm_send() command used to seal/unseal keys.

---
 drivers/char/tpm/tpm.c |   17 +++++++++++++++++
 include/linux/tpm.h    |    3 +++
 2 files changed, 20 insertions(+), 0 deletions(-)
drivers/char/tpm/tpm.c +17 -0
index 7c41335..5987d9c 100644
@@ -780,6 +780,23 @@ int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash)
 }
 EXPORT_SYMBOL_GPL(tpm_pcr_extend);
 
+int tpm_send(u32 chip_num, char *cmd, int buflen)
+{
+	struct tpm_chip *chip;
+	int rc;
+
+	chip = tpm_chip_find_get(chip_num);
+	if (chip == NULL)
+		return -ENODEV;
+
+	rc = transmit_cmd(chip, (struct tpm_cmd_t *)cmd, buflen,
+			  "attempting tpm_cmd");
+
+	module_put(chip->dev->driver->owner);
+	return rc;
+}
+EXPORT_SYMBOL_GPL(tpm_send);
+
 ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr,
 		      char *buf)
 {
include/linux/tpm.h +3 -0
index ac5d1c1..a0ecaa9 100644
@@ -31,6 +31,7 @@
 
 extern int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf);
 extern int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash);
+extern int tpm_send(u32 chip_num, char *cmd, int buflen);
 #else
 static inline int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf) {
 	return -ENODEV;
@@ -38,5 +39,7 @@ static inline int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf) {
 static inline int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash) {
 	return -ENODEV;
 }
+static inline int tpm_send(u32 chip_num, char *cmd, int buflen) {
+	return -ENODEV;
 #endif
 #endif
Signature · 1 line
1.7.2.2
author
#3 lore raw

PATCH 3/4 keys: add new trusted key-type

In reply to Mimi Zohar

Defines a new kernel key-type called 'trusted'. Trusted keys are TPM generated random numbers, RSA sealed by the TPM, and only unsealed by the TPM, if boot PCRs and other criteria match. Trusted keys are created/encrypted/decrypted in the kernel. Userspace ever only sees/stores encrypted blobs.

Changelog:
- Free payload on failure to create key(reported/fixed by Roberto Sassu)

---
 include/keys/trusted-type.h     |   33 ++
 security/Kconfig                |   15 +
 security/keys/Makefile          |    1 +
 security/keys/trusted_defined.c |  999 +++++++++++++++++++++++++++++++++++++++
 security/keys/trusted_defined.h |  125 +++++
 5 files changed, 1173 insertions(+), 0 deletions(-)
 create mode 100644 include/keys/trusted-type.h
 create mode 100644 security/keys/trusted_defined.c
 create mode 100644 security/keys/trusted_defined.h
include/keys/trusted-type.h new file +33 -0
new file mode 100644
index 0000000..be7d44e
@@ -0,0 +1,33 @@
+/* trusted-type.h: trusted-defined key type
+ *
+ * Copyright (C) 2010 IBM Corporation
+ * Author: David Safford <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 2 of the License.
+ */
+
+#ifndef _KEYS_TRUSTED_TYPE_H
+#define _KEYS_TRUSTED_TYPE_H
+
+#include <linux/key.h>
+#include <linux/rcupdate.h>
+
+#define MAX_KEY_SIZE			128
+#define MAX_BLOB_SIZE			320
+#define MAX_PCRINFO_SIZE		64
+
+struct trusted_key_payload {
+	struct rcu_head rcu;	/* RCU destructor */
+	unsigned int key_len;
+	unsigned int blob_len;
+	unsigned int pcrinfo_len;
+	unsigned char key[MAX_KEY_SIZE];
+	unsigned char blob[MAX_BLOB_SIZE];
+	unsigned char pcrinfo[MAX_PCRINFO_SIZE];
+};
+
+extern struct key_type key_type_trusted;
+
+#endif /* _KEYS_TRUSTED_TYPE_H */
security/Kconfig +15 -0
index bd72ae6..38fdaf6 100644
@@ -21,6 +21,21 @@ config KEYS
 
 	  If you are unsure as to whether this is required, answer N.
 
+config TRUSTED_KEYS
+	tristate "TRUSTED KEYS"
+	depends on KEYS && TCG_TPM
+	select CRYPTO
+	select CRYPTO_HMAC
+	select CRYPTO_SHA1
+	help
+	  This option provides support for creating/sealing/unsealing keys
+	  in the kernel. Trusted keys are TPM generated random numbers
+	  symmetric keys, RSA sealed by the TPM, and only unsealed by the
+	  TPM, if boot PCRs and other criteria match.  Userspace ever only
+	  sees/stores encrypted blobs.
+
+	  If you are unsure as to whether this is required, answer N.
+
 config KEYS_DEBUG_PROC_KEYS
 	bool "Enable the /proc/keys file by which keys may be viewed"
 	depends on KEYS
security/keys/Makefile +1 -0
index 74d5447..fcb1070 100644
@@ -13,6 +13,7 @@ obj-y := \
 	request_key_auth.o \
 	user_defined.o
 
+obj-$(CONFIG_TRUSTED_KEYS) += trusted_defined.o
 obj-$(CONFIG_KEYS_COMPAT) += compat.o
 obj-$(CONFIG_PROC_FS) += proc.o
 obj-$(CONFIG_SYSCTL) += sysctl.o
security/keys/trusted_defined.c new file +999 -0

999 lines not on this page · raw message

security/keys/trusted_defined.h new file +125 -0

125 lines not on this page · raw message

Signature · 1 line
1.7.2.2
author
#6 lore raw

PATCH 4/4 keys: add new key-type encrypted

In reply to Mimi Zohar

Defines a new kernel key-type called 'encrypted'. Encrypted keys are kernel generated random numbers, which are encrypted/decrypted with a 'trusted' symmetric key. Encrypted keys are created/encrypted/decrypted in the kernel. Userspace only ever sees/stores encrypted blobs.

Changelog:
- wait until late_initcall for crypto libraries to be registered
- cleanup security/Kconfig
- Add missing 'update' keyword (reported/fixed by Roberto Sassu)
- Free epayload on failure to create key (reported/fixed by Roberto Sassu)
- Increase the data size limit (requested by Roberto Sassu)
- Crypto return codes are always 0 on success and negative on failure, remove unnecessary tests.

---
 include/keys/encrypted-type.h     |   30 ++
 security/Kconfig                  |   16 +
 security/keys/Makefile            |    1 +
 security/keys/encrypted_defined.c |  804 +++++++++++++++++++++++++++++++++++++
 security/keys/encrypted_defined.h |   52 +++
 5 files changed, 903 insertions(+), 0 deletions(-)
 create mode 100644 include/keys/encrypted-type.h
 create mode 100644 security/keys/encrypted_defined.c
 create mode 100644 security/keys/encrypted_defined.h
include/keys/encrypted-type.h new file +30 -0
new file mode 100644
index 0000000..e2312e0
@@ -0,0 +1,30 @@
+/* encrypted-type.h: encrypted-defined key type
+ *
+ * Copyright (C) 2010 IBM Corporation
+ * Author: Mimi Zohar <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 2 of the License.
+ */
+
+#ifndef _KEYS_ENCRYPTED_TYPE_H
+#define _KEYS_ENCRYPTED_TYPE_H
+
+#include <linux/key.h>
+#include <linux/rcupdate.h>
+
+struct encrypted_key_payload {
+	struct rcu_head rcu;	/* RCU destructor */
+	char *master_desc;	/* datablob: master key name */
+	char *datalen;		/* datablob: decrypted key length */
+	void *iv;		/* datablob: iv */
+	void *encrypted_data;	/* datablob: encrypted key */
+	unsigned short datablob_len;	/* length of datablob */
+	unsigned short decrypted_datalen;	/* decrypted data length */
+	char decrypted_data[0];	/* decrypted data +  datablob + hmac */
+};
+
+extern struct key_type key_type_encrypted;
+
+#endif /* _KEYS_ENCRYPTED_TYPE_H */
security/Kconfig +16 -0
index 38fdaf6..d591398 100644
@@ -36,6 +36,22 @@ config TRUSTED_KEYS
 
 	  If you are unsure as to whether this is required, answer N.
 
+config ENCRYPTED_KEYS
+	tristate "ENCRYPTED KEYS"
+	depends on KEYS && TRUSTED_KEYS
+	select CRYPTO_AES
+	select CRYPTO_CBC
+	select CRYPTO_SHA256
+	select CRYPTO_RNG
+	help
+	  This option provides support for create/encrypting/decrypting keys
+	  in the kernel.  Encrypted keys are kernel generated random numbers,
+	  which are encrypted/decrypted with a 'master' symmetric key. The
+	  'master' key can be either a trusted-key or user-key type.
+	  Userspace only ever sees/stores encrypted blobs.
+
+	  If you are unsure as to whether this is required, answer N.
+
 config KEYS_DEBUG_PROC_KEYS
 	bool "Enable the /proc/keys file by which keys may be viewed"
 	depends on KEYS
security/keys/Makefile +1 -0
index fcb1070..6c94105 100644
@@ -14,6 +14,7 @@ obj-y := \
 	user_defined.o
 
 obj-$(CONFIG_TRUSTED_KEYS) += trusted_defined.o
+obj-$(CONFIG_ENCRYPTED_KEYS) += encrypted_defined.o
 obj-$(CONFIG_KEYS_COMPAT) += compat.o
 obj-$(CONFIG_PROC_FS) += proc.o
 obj-$(CONFIG_SYSCTL) += sysctl.o
security/keys/encrypted_defined.c new file +804 -0

804 lines not on this page · raw message

security/keys/encrypted_defined.h new file +52 -0

52 lines not on this page · raw message

Signature · 1 line
1.7.2.2
author
#1 lore raw

PATCH 1/4 lib: hex2bin converts ascii hexadecimal string to binary

In reply to Mimi Zohar

Similar to the kgdb_hex2mem() code, hex2bin converts a string to binary using the hex_to_bin() library call.

---
 include/linux/kernel.h |    1 +
 lib/hexdump.c          |   16 ++++++++++++++++
 2 files changed, 17 insertions(+), 0 deletions(-)
include/linux/kernel.h +1 -0
index 2b0a35e..69ddea8 100644
@@ -396,6 +396,7 @@ static inline char *pack_hex_byte(char *buf, u8 byte)
 }
 
 extern int hex_to_bin(char ch);
+extern void hex2bin(unsigned char *mem, char *buf, int count);
 
 #ifndef pr_fmt
 #define pr_fmt(fmt) fmt
lib/hexdump.c +16 -0
index 5d7a480..66f96bb 100644
@@ -34,6 +34,22 @@ int hex_to_bin(char ch)
 EXPORT_SYMBOL(hex_to_bin);
 
 /**
+ * hex2bin - convert an ascii hexadecimal string to its binary representation
+ * @mem: result
+ * @buf: ascii hexadecimal string
+ * @count: result length
+ */
+void hex2bin(unsigned char *mem, char *buf, int count)
+{
+	while (count--) {
+		*mem = hex_to_bin(*buf++) << 4;
+		*mem += hex_to_bin(*buf++);
+		mem++;
+	}
+}
+EXPORT_SYMBOL(hex2bin);
+
+/**
  * hex_dump_to_buffer - convert a blob of data to "hex ASCII" in memory
  * @buf: data blob to dump
  * @len: number of bytes in the @buf
Signature · 1 line
1.7.2.2
maintainer Acked-by on v1 3/4
#4 lore raw

Re: [PATCH v1.1 3/4] keys: add new trusted key-type

In reply to Mimi Zohar

Quoting Mimi Zohar ([email protected]):

Looks fine to me, and very useful.

(for 1-3, haven't looked at 4 yet and won't tonight)

security/Kconfig config KEYS · line 35

Quoted 12 lines
security/Kconfig +12 -0
+config TRUSTED_KEYS
+	tristate "TRUSTED KEYS"
+	depends on KEYS && TCG_TPM
+	select CRYPTO
+	select CRYPTO_HMAC
+	select CRYPTO_SHA1
+	help
+	  This option provides support for creating/sealing/unsealing keys
+	  in the kernel. Trusted keys are TPM generated random numbers
+	  symmetric keys, RSA sealed by the TPM, and only unsealed by the
+	  TPM, if boot PCRs and other criteria match.  Userspace ever only
+	  sees/stores encrypted blobs.

"
This option provides support for creating, sealing, and unsealing keys in the kernel. Trusted keys are ramdon symmetric keys created RSA-sealed, and stored in the TPM. The TPM only unseals the keys if the boot PCRs and other criteria match. Userspace can only ever see encrypted blobs.
"

Quoted 7 lines
security/keys/trusted_defined.c new file +1 -0
new file mode 100644
index 0000000..0bd935f
@@ -0,0 +1,999 @@
+/*

security/keys/trusted_defined.c line 11

Quoted 10 lines
security/keys/trusted_defined.c new file +10 -0
+ * Copyright (C) 2010 IBM Corporation
+ *
+ * Author:
+ * David Safford <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 2 of the License.
+ *
+ * File: trusted_defined.c

(don't put filenames in comments :)

security/keys/trusted_defined.c

Quoted 8 lines
security/keys/trusted_defined.c new file +8 -0
+ * Defines a new kernel key-type called 'trusted'. Trusted keys are
+ * TPM generated random numbers, RSA sealed by the TPM, and only unsealed
+ * by the TPM, if boot PCRs and other criteria match. Trusted keys are
+ * created/sealed/unsealed in the kernel. Userspace ever only sees/stores
+ * encrypted blobs.
+ *
+ * Keys are sealed under the SRK, which must have the default
+ * authorization value (20 zeros).

What does this mean? They have to be sealed by a key that starts with 20 zeros? (of course it doesn't mean that, but i don't understand what it does mean :)

Quoted 72 lines

This can be set at takeownership
+ * time with the trouser's utility "tpm_takeownership -u -z".
+ *
+ * Usage:

+ *   keyctl add trusted name "NEW keylen [hex_pcrinfo]" ring
+ *   keyctl add trusted name "LOAD hex_blob" ring
+ *   keyctl update key "UPDATE hex_pcrinfo"
+ *   keyctl print keyid

+ * keys can be 32 - 128 bytes, blob max is 1024 hex ascii characters
+ * binary pcrinfo max is 512 hex ascii characters
+ */

security/keys/trusted_defined.c new file +61 -0
+
+#include <linux/uaccess.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/slab.h>
+#include <linux/parser.h>
+#include <linux/string.h>
+#include <keys/user-type.h>
+#include <keys/trusted-type.h>
+#include <linux/key-type.h>
+#include <linux/random.h>
+#include <linux/rcupdate.h>
+#include <linux/scatterlist.h>
+#include <linux/crypto.h>
+#include <linux/tpm.h>
+
+#include "trusted_defined.h"
+
+static char hmac_alg[] = "hmac(sha1)";
+static char hash_alg[] = "sha1";
+
+static int init_sha1_desc(struct hash_desc *desc)
+{
+	int rc;
+
+	desc->tfm = crypto_alloc_hash(hash_alg, 0, CRYPTO_ALG_ASYNC);
+	if (IS_ERR(desc->tfm)) {
+		rc = PTR_ERR(desc->tfm);
+		return rc;
+	}
+	desc->flags = 0;
+	rc = crypto_hash_init(desc);
+	if (rc)
+		crypto_free_hash(desc->tfm);
+	return rc;
+}
+
+static int init_hmac_desc(struct hash_desc *desc, unsigned char *key,
+			  int keylen)
+{
+	int rc;
+
+	desc->tfm = crypto_alloc_hash(hmac_alg, 0, CRYPTO_ALG_ASYNC);
+	if (IS_ERR(desc->tfm)) {
+		rc = PTR_ERR(desc->tfm);
+		return rc;
+	}
+	desc->flags = 0;
+	crypto_hash_setkey(desc->tfm, key, keylen);
+	rc = crypto_hash_init(desc);
+	if (rc)
+		crypto_free_hash(desc->tfm);
+	return rc;
+}
+
+static int TSS_sha1(unsigned char *data, int datalen, unsigned char *digest)
+{
+	struct hash_desc desc;
+	struct scatterlist sg[1];
+	int rc;

security/keys/trusted_defined.c line 101

Quoted 10 lines
security/keys/trusted_defined.c new file +10 -0
+
+	rc = init_sha1_desc(&desc);
+	if (rc != 0)
+		return rc;
+
+	sg_init_one(sg, data, datalen);
+	crypto_hash_update(&desc, sg, datalen);
+	crypto_hash_final(&desc, digest);
+	crypto_free_hash(desc.tfm);
+	return rc;

In later functions where rc can only be 0, you 'return 0;', but here you return rc. Is that an oversight, or is there something actually wrong here (like a missing hunk)?

There are also several places where you:

security/keys/trusted_defined.c line 814

Quoted 4 lines
security/keys/trusted_defined.c new file +4 -0
+	datablob = kzalloc(datalen + 1, GFP_KERNEL);
+	if (!datablob)
+		return -ENOMEM;
+	memcpy(datablob, data, datalen);

don't need to kzalloc.

-serge

author
#5 lore raw

Re: [PATCH v1.1 3/4] keys: add new trusted key-type

Quoted 7 lines · Serge E. Hallyn

Quoting Mimi Zohar ([email protected]):

Looks fine to me, and very useful.

(for 1-3, haven't looked at 4 yet and won't tonight)

Thanks Serge!

Quoted 13 lines

security/Kconfig config KEYS · line 35

Quoted 12 lines
security/Kconfig +12 -0
+config TRUSTED_KEYS
+	tristate "TRUSTED KEYS"
+	depends on KEYS && TCG_TPM
+	select CRYPTO
+	select CRYPTO_HMAC
+	select CRYPTO_SHA1
+	help
+	  This option provides support for creating/sealing/unsealing keys
+	  in the kernel. Trusted keys are TPM generated random numbers
+	  symmetric keys, RSA sealed by the TPM, and only unsealed by the
+	  TPM, if boot PCRs and other criteria match.  Userspace ever only
+	  sees/stores encrypted blobs.
Quoted 7 lines

"
This option provides support for creating, sealing, and unsealing keys in the kernel. Trusted keys are ramdon symmetric keys created RSA-sealed, and stored in the TPM. The TPM only unseals the keys if the boot PCRs and other criteria match. Userspace can only ever see encrypted blobs.
"

Better, but keys are not stored in the TPM. How about:

This option provides support for creating, sealing, and unsealing keys in the kernel. Trusted keys are random number symmetric keys, generated and RSA-sealed by the TPM. The TPM only unseals the keys, if the boot PCRs and other criteria match. Userspace can only ever see encrypted blobs.

Quoted 11 lines
Quoted 11 lines
security/keys/trusted_defined.c new file +5 -0
new file mode 100644
index 0000000..0bd935f
@@ -0,0 +1,999 @@
+/*
+ * Copyright (C) 2010 IBM Corporation
+ *
+ * Author:
+ * David Safford <[email protected]>
Quoted 8 lines

security/keys/trusted_defined.c line 11

Quoted 6 lines
security/keys/trusted_defined.c new file +6 -0
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 2 of the License.
+ *
+ * File: trusted_defined.c

(don't put filenames in comments :)

There are plenty of examples containing the filename. I guess this changed at some point. Thanks for bringing it to my attention.

Quoted 9 lines

security/keys/trusted_defined.c

Quoted 8 lines
security/keys/trusted_defined.c new file +8 -0
+ * Defines a new kernel key-type called 'trusted'. Trusted keys are
+ * TPM generated random numbers, RSA sealed by the TPM, and only unsealed
+ * by the TPM, if boot PCRs and other criteria match. Trusted keys are
+ * created/sealed/unsealed in the kernel. Userspace ever only sees/stores
+ * encrypted blobs.
+ *
+ * Keys are sealed under the SRK, which must have the default
+ * authorization value (20 zeros).
Quoted 3 lines

What does this mean? They have to be sealed by a key that starts with 20 zeros? (of course it doesn't mean that, but i don't understand what it does mean :)

Defines a new kernel key-type called 'trusted'. Trusted keys are random number symmetric keys, generated and RSA-sealed by the TPM. The TPM only unseals the keys, if the boot PCRs and other criteria match. Userspace can only ever see encrypted blobs.

Trusted keys are sealed with the Storage Root Key(SRK). The SRK usage authorization value is the well-known authorization value of 20 zeroes.

(In english this means we follow accepted convention and do not bother with a password on the SRK.)

Quoted 78 lines
Quoted 78 lines

This can be set at takeownership
+ * time with the trouser's utility "tpm_takeownership -u -z".
+ *
+ * Usage:

+ *   keyctl add trusted name "NEW keylen [hex_pcrinfo]" ring
+ *   keyctl add trusted name "LOAD hex_blob" ring
+ *   keyctl update key "UPDATE hex_pcrinfo"
+ *   keyctl print keyid

+ * keys can be 32 - 128 bytes, blob max is 1024 hex ascii characters
+ * binary pcrinfo max is 512 hex ascii characters
+ */

security/keys/trusted_defined.c new file +67 -0
+
+#include <linux/uaccess.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/slab.h>
+#include <linux/parser.h>
+#include <linux/string.h>
+#include <keys/user-type.h>
+#include <keys/trusted-type.h>
+#include <linux/key-type.h>
+#include <linux/random.h>
+#include <linux/rcupdate.h>
+#include <linux/scatterlist.h>
+#include <linux/crypto.h>
+#include <linux/tpm.h>
+
+#include "trusted_defined.h"
+
+static char hmac_alg[] = "hmac(sha1)";
+static char hash_alg[] = "sha1";
+
+static int init_sha1_desc(struct hash_desc *desc)
+{
+	int rc;
+
+	desc->tfm = crypto_alloc_hash(hash_alg, 0, CRYPTO_ALG_ASYNC);
+	if (IS_ERR(desc->tfm)) {
+		rc = PTR_ERR(desc->tfm);
+		return rc;
+	}
+	desc->flags = 0;
+	rc = crypto_hash_init(desc);
+	if (rc)
+		crypto_free_hash(desc->tfm);
+	return rc;
+}
+
+static int init_hmac_desc(struct hash_desc *desc, unsigned char *key,
+			  int keylen)
+{
+	int rc;
+
+	desc->tfm = crypto_alloc_hash(hmac_alg, 0, CRYPTO_ALG_ASYNC);
+	if (IS_ERR(desc->tfm)) {
+		rc = PTR_ERR(desc->tfm);
+		return rc;
+	}
+	desc->flags = 0;
+	crypto_hash_setkey(desc->tfm, key, keylen);
+	rc = crypto_hash_init(desc);
+	if (rc)
+		crypto_free_hash(desc->tfm);
+	return rc;
+}
+
+static int TSS_sha1(unsigned char *data, int datalen, unsigned char *digest)
+{
+	struct hash_desc desc;
+	struct scatterlist sg[1];
+	int rc;
+
+	rc = init_sha1_desc(&desc);
+	if (rc != 0)
+		return rc;
+
+	sg_init_one(sg, data, datalen);
Quoted 8 lines

security/keys/trusted_defined.c line 101

Quoted 4 lines
security/keys/trusted_defined.c new file +4 -0
+	crypto_hash_update(&desc, sg, datalen);
+	crypto_hash_final(&desc, digest);
+	crypto_free_hash(desc.tfm);
+	return rc;

In later functions where rc can only be 0, you 'return 0;', but here you return rc. Is that an oversight, or is there something actually wrong here (like a missing hunk)?

just inconsistent

Quoted 10 lines

There are also several places where you:

security/keys/trusted_defined.c line 814

Quoted 4 lines
security/keys/trusted_defined.c new file +4 -0
+	datablob = kzalloc(datalen + 1, GFP_KERNEL);
+	if (!datablob)
+		return -ENOMEM;
+	memcpy(datablob, data, datalen);

don't need to kzalloc.

-serge

true, will correct.

thanks!

Mimi

#7 lore raw

Re: [PATCH v1.1 4/4] keys: add new key-type encrypted

In reply to Mimi Zohar

The buffer 'derived_buf' in the function get_derived_key() must be allocated dynamically in order to make room for an arbitrary length master key.

---
 security/keys/encrypted_defined.c |   15 +++++++++++++--
 1 files changed, 13 insertions(+), 2 deletions(-)
security/keys/encrypted_defined.c +13 -2
index 5331925..65d9b13 100644
@@ -289,10 +289,20 @@ enum derived_key_type { ENC_KEY, AUTH_KEY };
 static int get_derived_key(char *derived_key, enum derived_key_type key_type,
 			   void *master_key, unsigned int master_keylen)
 {
-	char derived_buf[hash_size + 10];
+	char *derived_buf;
 	int ret;
+	unsigned int derived_buf_len;
+
+	derived_buf_len = strlen("AUTH_KEY") + 1 + master_keylen;
+	if (derived_buf_len < hash_size)
+		derived_buf_len = hash_size;
+
+	derived_buf = kzalloc(derived_buf_len, GFP_KERNEL);
+	if (!derived_buf) {
+		pr_err("encrypted_key: out of memory\n");
+		return -ENOMEM;
+	}
 
-	memset(derived_buf, 0, sizeof derived_buf);
 	if (key_type)
 		strcpy(derived_buf, "AUTH_KEY");
 	else
@@ -301,6 +311,7 @@ static int get_derived_key(char *derived_key, enum derived_key_type key_type,
 	memcpy(derived_buf + strlen(derived_buf) + 1, master_key,
 	       master_keylen);
 	ret = calc_hash(derived_key, derived_buf, sizeof derived_buf);
+	kfree(derived_buf);
 	return ret;
 }
 
Signature · 1 line
1.7.2.3
author
#8 lore raw

Re: [PATCH v1.1 4/4] keys: add new key-type encrypted

Quoted 23 lines · Roberto Sassu

The buffer 'derived_buf' in the function get_derived_key() must be allocated dynamically in order to make room for an arbitrary length master key.

---
 security/keys/encrypted_defined.c |   15 +++++++++++++--
 1 files changed, 13 insertions(+), 2 deletions(-)
security/keys/encrypted_defined.c +4 -1
index 5331925..65d9b13 100644
@@ -289,10 +289,20 @@ enum derived_key_type { ENC_KEY, AUTH_KEY };
 static int get_derived_key(char *derived_key, enum derived_key_type key_type,
 			   void *master_key, unsigned int master_keylen)
 {
-	char derived_buf[hash_size + 10];
+	char *derived_buf;
 	int ret;
+	unsigned int derived_buf_len;
+
+	derived_buf_len = strlen("AUTH_KEY") + 1 + master_keylen;

security/keys/encrypted_defined.c enum derived_key_type { ENC_KEY, AUTH_KEY }; · line 295

Quoted 10 lines
security/keys/encrypted_defined.c +8 -1
+	if (derived_buf_len < hash_size)
+		derived_buf_len = hash_size;
+
+	derived_buf = kzalloc(derived_buf_len, GFP_KERNEL);
+	if (!derived_buf) {
+		pr_err("encrypted_key: out of memory\n");
+		return -ENOMEM;
+	}
 
-	memset(derived_buf, 0, sizeof derived_buf);

Thanks Roberto!

security/keys/encrypted_defined.c static int get_derived_key(char *derived_key, enum derived_key_type key_type, · line 317

Quoted 11 lines
security/keys/encrypted_defined.c +1 -0
 	if (key_type)
 		strcpy(derived_buf, "AUTH_KEY");
 	else
@@ -301,6 +311,7 @@ static int get_derived_key(char *derived_key, enum derived_key_type key_type,
 	memcpy(derived_buf + strlen(derived_buf) + 1, master_key,
 	       master_keylen);
 	ret = calc_hash(derived_key, derived_buf, sizeof derived_buf);
+	kfree(derived_buf);
 	return ret;
 }
 

Yes, and the length in calc_hash() would then need to be derived_buf_len.

thanks,

Mimi