Commit 8732b070 authored by Che-liang Chiou's avatar Che-liang Chiou Committed by Simon Glass
Browse files

tpm: Add TPM command library



TPM command library implements a subset of TPM commands defined in TCG
Main Specification 1.2 that are useful for implementing secure boot.
More TPM commands could be added out of necessity.

You may exercise these commands through the 'tpm' command.  However, the
raw TPM commands are too primitive for writing secure boot in command
interpreter scripts; so the 'tpm' command also provides helper functions
to make scripting easier.

For example, to define a counter in TPM non-volatile storage and
initialize it to zero:

$ tpm init
$ tpm startup TPM_ST_CLEAR
$ tpm nv_define d 0x1001 0x1
$ tpm nv_write d 0x1001 0

And then increment the counter by one:

$ tpm nv_read d 0x1001 i
$ setexpr.l i $i + 1
$ tpm nv_write d 0x1001 $i
Signed-off-by: default avatarChe-Liang Chiou <clchiou@chromium.org>
parent c1af608f
This diff is collapsed.
/*
* Copyright (c) 2011 The Chromium OS Authors.
*
* See file CREDITS for list of people who contributed to this
* project.
*
* 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; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*/
#ifndef __TIS_H
#define __TIS_H
#include <common.h>
/* Low-level interface to access TPM */
/*
* tis_init()
*
* Initialize the TPM device. Returns 0 on success or -1 on
* failure (in case device probing did not succeed).
*/
int tis_init(void);
/*
* tis_open()
*
* Requests access to locality 0 for the caller. After all commands have been
* completed the caller is supposed to call tis_close().
*
* Returns 0 on success, -1 on failure.
*/
int tis_open(void);
/*
* tis_close()
*
* terminate the currect session with the TPM by releasing the locked
* locality. Returns 0 on success of -1 on failure (in case lock
* removal did not succeed).
*/
int tis_close(void);
/*
* tis_sendrecv()
*
* Send the requested data to the TPM and then try to get its response
*
* @sendbuf - buffer of the data to send
* @send_size size of the data to send
* @recvbuf - memory to save the response to
* @recv_len - pointer to the size of the response buffer
*
* Returns 0 on success (and places the number of response bytes at recv_len)
* or -1 on failure.
*/
int tis_sendrecv(const uint8_t *sendbuf, size_t send_size, uint8_t *recvbuf,
size_t *recv_len);
#endif /* __TIS_H */
/*
* Copyright (c) 2011 The Chromium OS Authors.
* Copyright (c) 2013 The Chromium OS Authors.
*
* See file CREDITS for list of people who contributed to this
* project.
......@@ -20,52 +20,185 @@
* MA 02111-1307 USA
*/
#ifndef _INCLUDE_TPM_H_
#define _INCLUDE_TPM_H_
#ifndef __TPM_H
#define __TPM_H
#include <common.h>
#include <tis.h>
/*
* tis_init()
* Here is a partial implementation of TPM commands. Please consult TCG Main
* Specification for definitions of TPM commands.
*/
enum tpm_startup_type {
TPM_ST_CLEAR = 0x0001,
TPM_ST_STATE = 0x0002,
TPM_ST_DEACTIVATED = 0x0003,
};
enum tpm_physical_presence {
TPM_PHYSICAL_PRESENCE_HW_DISABLE = 0x0200,
TPM_PHYSICAL_PRESENCE_CMD_DISABLE = 0x0100,
TPM_PHYSICAL_PRESENCE_LIFETIME_LOCK = 0x0080,
TPM_PHYSICAL_PRESENCE_HW_ENABLE = 0x0040,
TPM_PHYSICAL_PRESENCE_CMD_ENABLE = 0x0020,
TPM_PHYSICAL_PRESENCE_NOTPRESENT = 0x0010,
TPM_PHYSICAL_PRESENCE_PRESENT = 0x0008,
TPM_PHYSICAL_PRESENCE_LOCK = 0x0004,
};
enum tpm_nv_index {
TPM_NV_INDEX_LOCK = 0xffffffff,
TPM_NV_INDEX_0 = 0x00000000,
TPM_NV_INDEX_DIR = 0x10000001,
};
/**
* Initialize TPM device. It must be called before any TPM commands.
*
* Initialize the TPM device. Returns 0 on success or -1 on
* failure (in case device probing did not succeed).
* @return 0 on success, non-0 on error.
*/
int tis_init(void);
uint32_t tpm_init(void);
/*
* tis_open()
/**
* Issue a TPM_Startup command.
*
* Requests access to locality 0 for the caller. After all commands have been
* completed the caller is supposed to call tis_close().
* @param mode TPM startup mode
* @return return code of the operation
*/
uint32_t tpm_startup(enum tpm_startup_type mode);
/**
* Issue a TPM_SelfTestFull command.
*
* Returns 0 on success, -1 on failure.
* @return return code of the operation
*/
int tis_open(void);
uint32_t tpm_self_test_full(void);
/*
* tis_close()
/**
* Issue a TPM_ContinueSelfTest command.
*
* terminate the currect session with the TPM by releasing the locked
* locality. Returns 0 on success of -1 on failure (in case lock
* removal did not succeed).
* @return return code of the operation
*/
int tis_close(void);
uint32_t tpm_continue_self_test(void);
/*
* tis_sendrecv()
/**
* Issue a TPM_NV_DefineSpace command. The implementation is limited
* to specify TPM_NV_ATTRIBUTES and size of the area. The area index
* could be one of the special value listed in enum tpm_nv_index.
*
* Send the requested data to the TPM and then try to get its response
* @param index index of the area
* @param perm TPM_NV_ATTRIBUTES of the area
* @param size size of the area
* @return return code of the operation
*/
uint32_t tpm_nv_define_space(uint32_t index, uint32_t perm, uint32_t size);
/**
* Issue a TPM_NV_ReadValue command. This implementation is limited
* to read the area from offset 0. The area index could be one of
* the special value listed in enum tpm_nv_index.
*
* @param index index of the area
* @param data output buffer of the area contents
* @param count size of output buffer
* @return return code of the operation
*/
uint32_t tpm_nv_read_value(uint32_t index, void *data, uint32_t count);
/**
* Issue a TPM_NV_WriteValue command. This implementation is limited
* to write the area from offset 0. The area index could be one of
* the special value listed in enum tpm_nv_index.
*
* @param index index of the area
* @param data input buffer to be wrote to the area
* @param length length of data bytes of input buffer
* @return return code of the operation
*/
uint32_t tpm_nv_write_value(uint32_t index, const void *data, uint32_t length);
/**
* Issue a TPM_Extend command.
*
* @param index index of the PCR
* @param in_digest 160-bit value representing the event to be
* recorded
* @param out_digest 160-bit PCR value after execution of the
* command
* @return return code of the operation
*/
uint32_t tpm_extend(uint32_t index, const void *in_digest, void *out_digest);
/**
* Issue a TPM_PCRRead command.
*
* @sendbuf - buffer of the data to send
* @send_size size of the data to send
* @recvbuf - memory to save the response to
* @recv_len - pointer to the size of the response buffer
* @param index index of the PCR
* @param data output buffer for contents of the named PCR
* @param count size of output buffer
* @return return code of the operation
*/
uint32_t tpm_pcr_read(uint32_t index, void *data, size_t count);
/**
* Issue a TSC_PhysicalPresence command. TPM physical presence flag
* is bit-wise OR'ed of flags listed in enum tpm_physical_presence.
*
* @param presence TPM physical presence flag
* @return return code of the operation
*/
uint32_t tpm_tsc_physical_presence(uint16_t presence);
/**
* Issue a TPM_ReadPubek command.
*
* @param data output buffer for the public endorsement key
* @param count size of ouput buffer
* @return return code of the operation
*/
uint32_t tpm_read_pubek(void *data, size_t count);
/**
* Issue a TPM_ForceClear command.
*
* @return return code of the operation
*/
uint32_t tpm_force_clear(void);
/**
* Issue a TPM_PhysicalEnable command.
*
* @return return code of the operation
*/
uint32_t tpm_physical_enable(void);
/**
* Issue a TPM_PhysicalDisable command.
*
* @return return code of the operation
*/
uint32_t tpm_physical_disable(void);
/**
* Issue a TPM_PhysicalSetDeactivated command.
*
* @param state boolean state of the deactivated flag
* @return return code of the operation
*/
uint32_t tpm_physical_set_deactivated(uint8_t state);
/**
* Issue a TPM_GetCapability command. This implementation is limited
* to query sub_cap index that is 4-byte wide.
*
* Returns 0 on success (and places the number of response bytes at recv_len)
* or -1 on failure.
* @param cap_area partition of capabilities
* @param sub_cap further definition of capability, which is
* limited to be 4-byte wide
* @param cap output buffer for capability information
* @param count size of ouput buffer
* @return return code of the operation
*/
int tis_sendrecv(const uint8_t *sendbuf, size_t send_size, uint8_t *recvbuf,
size_t *recv_len);
uint32_t tpm_get_capability(uint32_t cap_area, uint32_t sub_cap,
void *cap, size_t count);
#endif /* _INCLUDE_TPM_H_ */
#endif /* __TPM_H */
......@@ -54,6 +54,7 @@ COBJS-y += qsort.o
COBJS-$(CONFIG_SHA1) += sha1.o
COBJS-$(CONFIG_SHA256) += sha256.o
COBJS-y += strmhz.o
COBJS-$(CONFIG_TPM) += tpm.o
COBJS-$(CONFIG_RBTREE) += rbtree.o
endif
......
/*
* Copyright (c) 2013 The Chromium OS Authors.
*
* See file CREDITS for list of people who contributed to this
* project.
*
* 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; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*/
#include <common.h>
#include <stdarg.h>
#include <tpm.h>
#include <asm/unaligned.h>
/* Internal error of TPM command library */
#define TPM_LIB_ERROR ((uint32_t)~0u)
/* Useful constants */
enum {
COMMAND_BUFFER_SIZE = 256,
TPM_PUBEK_SIZE = 256,
TPM_REQUEST_HEADER_LENGTH = 10,
TPM_RESPONSE_HEADER_LENGTH = 10,
PCR_DIGEST_LENGTH = 20,
};
/**
* Pack data into a byte string. The data types are specified in
* the format string: 'b' means unsigned byte, 'w' unsigned word,
* 'd' unsigned double word, and 's' byte string. The data are a
* series of offsets and values (for type byte string there are also
* lengths). The data values are packed into the byte string
* sequentially, and so a latter value could over-write a former
* value.
*
* @param str output string
* @param size size of output string
* @param format format string
* @param ... data points
* @return 0 on success, non-0 on error
*/
int pack_byte_string(uint8_t *str, size_t size, const char *format, ...)
{
va_list args;
size_t offset = 0, length = 0;
uint8_t *data = NULL;
uint32_t value = 0;
va_start(args, format);
for (; *format; format++) {
switch (*format) {
case 'b':
offset = va_arg(args, size_t);
value = va_arg(args, int);
length = 1;
break;
case 'w':
offset = va_arg(args, size_t);
value = va_arg(args, int);
length = 2;
break;
case 'd':
offset = va_arg(args, size_t);
value = va_arg(args, uint32_t);
length = 4;
break;
case 's':
offset = va_arg(args, size_t);
data = va_arg(args, uint8_t *);
length = va_arg(args, uint32_t);
break;
default:
debug("Couldn't recognize format string\n");
return -1;
}
if (offset + length > size)
return -1;
switch (*format) {
case 'b':
str[offset] = value;
break;
case 'w':
put_unaligned_be16(value, str + offset);
break;
case 'd':
put_unaligned_be32(value, str + offset);
break;
case 's':
memcpy(str + offset, data, length);
break;
}
}
va_end(args);
return 0;
}
/**
* Unpack data from a byte string. The data types are specified in
* the format string: 'b' means unsigned byte, 'w' unsigned word,
* 'd' unsigned double word, and 's' byte string. The data are a
* series of offsets and pointers (for type byte string there are also
* lengths).
*
* @param str output string
* @param size size of output string
* @param format format string
* @param ... data points
* @return 0 on success, non-0 on error
*/
int unpack_byte_string(const uint8_t *str, size_t size, const char *format, ...)
{
va_list args;
size_t offset = 0, length = 0;
uint8_t *ptr8 = NULL;
uint16_t *ptr16 = NULL;
uint32_t *ptr32 = NULL;
va_start(args, format);
for (; *format; format++) {
switch (*format) {
case 'b':
offset = va_arg(args, size_t);
ptr8 = va_arg(args, uint8_t *);
length = 1;
break;
case 'w':
offset = va_arg(args, size_t);
ptr16 = va_arg(args, uint16_t *);
length = 2;
break;
case 'd':
offset = va_arg(args, size_t);
ptr32 = va_arg(args, uint32_t *);
length = 4;
break;
case 's':
offset = va_arg(args, size_t);
ptr8 = va_arg(args, uint8_t *);
length = va_arg(args, uint32_t);
break;
default:
debug("Couldn't recognize format string\n");
return -1;
}
if (offset + length > size)
return -1;
switch (*format) {
case 'b':
*ptr8 = str[offset];
break;
case 'w':
*ptr16 = get_unaligned_be16(str + offset);
break;
case 'd':
*ptr32 = get_unaligned_be32(str + offset);
break;
case 's':
memcpy(ptr8, str + offset, length);
break;
}
}
va_end(args);
return 0;
}
/**
* Get TPM command size.
*
* @param command byte string of TPM command
* @return command size of the TPM command
*/
static uint32_t tpm_command_size(const void *command)
{
const size_t command_size_offset = 2;
return get_unaligned_be32(command + command_size_offset);
}
/**
* Get TPM response return code, which is one of TPM_RESULT values.
*
* @param response byte string of TPM response
* @return return code of the TPM response
*/
static uint32_t tpm_return_code(const void *response)
{
const size_t return_code_offset = 6;
return get_unaligned_be32(response + return_code_offset);
}
/**
* Send a TPM command and return response's return code, and optionally
* return response to caller.
*
* @param command byte string of TPM command
* @param response output buffer for TPM response, or NULL if the
* caller does not care about it
* @param size_ptr output buffer size (input parameter) and TPM
* response length (output parameter); this parameter
* is a bidirectional
* @return return code of the TPM response
*/
static uint32_t tpm_sendrecv_command(const void *command,
void *response, size_t *size_ptr)
{
uint8_t response_buffer[COMMAND_BUFFER_SIZE];
size_t response_length;
uint32_t err;
if (response) {
response_length = *size_ptr;
} else {
response = response_buffer;
response_length = sizeof(response_buffer);
}
err = tis_sendrecv(command, tpm_command_size(command),
response, &response_length);
if (err)
return TPM_LIB_ERROR;
if (response)
*size_ptr = response_length;
return tpm_return_code(response);
}
uint32_t tpm_init(void)
{
uint32_t err;
err = tis_init();
if (err)
return err;
return tis_open();
}
uint32_t tpm_startup(enum tpm_startup_type mode)
{
const uint8_t command[12] = {
0x0, 0xc1, 0x0, 0x0, 0x0, 0xc, 0x0, 0x0, 0x0, 0x99, 0x0, 0x0,
};
const size_t mode_offset = 10;
uint8_t buf[COMMAND_BUFFER_SIZE];
if (pack_byte_string(buf, sizeof(buf), "sw",
0, command, sizeof(command),
mode_offset, mode))
return TPM_LIB_ERROR;
return tpm_sendrecv_command(buf, NULL, NULL);
}
uint32_t tpm_self_test_full(void)
{
const uint8_t command[10] = {
0x0, 0xc1, 0x0, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0x50,
};
return tpm_sendrecv_command(command, NULL, NULL);
}
uint32_t tpm_continue_self_test(void)
{
const uint8_t command[10] = {
0x0, 0xc1, 0x0, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0x53,
};
return tpm_sendrecv_command(command, NULL, NULL);
}
uint32_t tpm_nv_define_space(uint32_t index, uint32_t perm, uint32_t size)
{
const uint8_t command[101] = {
0x0, 0xc1, /* TPM_TAG */
0x0, 0x0, 0x0, 0x65, /* parameter size */
0x0, 0x0, 0x0, 0xcc, /* TPM_COMMAND_CODE */
/* TPM_NV_DATA_PUBLIC->... */
0x0, 0x18, /* ...->TPM_STRUCTURE_TAG */
0, 0, 0, 0, /* ...->TPM_NV_INDEX */
/* TPM_NV_DATA_PUBLIC->TPM_PCR_INFO_SHORT */
0x0, 0x3,
0, 0, 0,
0x1f,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* TPM_NV_DATA_PUBLIC->TPM_PCR_INFO_SHORT */
0x0, 0x3,
0, 0, 0,
0x1f,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* TPM_NV_ATTRIBUTES->... */
0x0, 0x17, /* ...->TPM_STRUCTURE_TAG */
0, 0, 0, 0, /* ...->attributes */
/* End of TPM_NV_ATTRIBUTES */
0, /* bReadSTClear */
0, /* bWriteSTClear */
0, /* bWriteDefine */
0, 0, 0, 0, /* size */
};
const size_t index_offset = 12;
const size_t perm_offset = 70;
const size_t size_offset = 77;
uint8_t buf[COMMAND_BUFFER_SIZE];
if (pack_byte_string(buf, sizeof(buf), "sddd",
0, command, sizeof(command),
index_offset, index,
perm_offset, perm,
size_offset, size))
return TPM_LIB_ERROR;
return tpm_sendrecv_command(buf, NULL, NULL);
}
uint32_t tpm_nv_read_value(uint32_t index, void *data, uint32_t count)
{
const uint8_t command[22] = {
0x0, 0xc1, 0x0, 0x0, 0x0, 0x16, 0x0, 0x0, 0x0, 0xcf,
};