Skip to content

Commit

Permalink
Added Linux SPI stdio support for running on Rasberry Pi. Cleanups fo…
Browse files Browse the repository at this point in the history
…r wolfTPM.
  • Loading branch information
dgarske committed Feb 8, 2018
1 parent 9036e7b commit 156fc9b
Show file tree
Hide file tree
Showing 11 changed files with 242 additions and 165 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ test-suite.log
src/.deps
src/.libs
RemoteSystemsTempFiles
examples/tpm/.deps
*.o
examples/tpm/tpm2_demo
examples/tpm/.libs
2 changes: 1 addition & 1 deletion IDE/OPENSTM32/Src/wolftpm_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ extern SPI_HandleTypeDef hspi1;


#ifdef WOLF_TPM2
extern int TPM2_Demo(void);
#include <examples/tpm/tpm2_demo.h>
#endif


Expand Down
2 changes: 1 addition & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ maintainer-clean-local:

# !!!! first line of rule has to start with a hard (real) tab, not spaces
egs:
$(MAKE) examples/mqttclient/mqttclient;
$(MAKE) examples/tpm/tpm2_demo;

install-exec-local: install-generic-config

Expand Down
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,23 @@ This example demonstrates calling the various TPM 2.0 API's.

## Building

`./configure && make`
1. Build wolfSSL:

```
./autogen.sh
./configure --enable-ecc --enable-sha512 && make && sudo make install
```

2. Build wolfTPM:

```
./configure && make
```


## Platform

This example was written for the STM32 with the CubeMX HAL. To add additional SPI hardware support insert your own interface call in `tpm2_demo.c` for the `TPM2_IoCb` function.
This example was written for use on Raspberry Pi or the STM32 with the CubeMX HAL. To add additional SPI hardware support insert your own interface call in `tpm2_demo.c` for the `TPM2_IoCb` function.


## Sample Output
Expand Down
8 changes: 4 additions & 4 deletions configure.ac
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# wolftpm
# Copyright (C) 2016 wolfSSL Inc.
# Copyright (C) 2018 wolfSSL Inc.
# All right reserved.

AC_INIT([wolftpm],[0.1.0],[https://github.com/wolfssl/wolfTPM/issues],[wolftpm],[http://www.wolfssl.com])
Expand All @@ -18,7 +18,7 @@ AC_ARG_PROGRAM
AC_CONFIG_MACRO_DIR([m4])
AC_CONFIG_HEADERS([src/config.h])

WOLFMQTT_LIBRARY_VERSION=1:0:0
WOLFTPM_LIBRARY_VERSION=1:0:0
# | | |
# +------+ | +---+
# | | |
Expand All @@ -30,7 +30,7 @@ WOLFMQTT_LIBRARY_VERSION=1:0:0
# | +- increment if source code has changed
# | set to zero if current is incremented
# +- increment if interfaces have been added, removed or changed
AC_SUBST([WOLFMQTT_LIBRARY_VERSION])
AC_SUBST([WOLFTPM_LIBRARY_VERSION])

LT_PREREQ([2.2])
LT_INIT([disable-static], [win32-dll])
Expand Down Expand Up @@ -67,7 +67,7 @@ AC_CHECK_LIB(network,socket)

# DEBUG
DEBUG_CFLAGS="-g -O0"
DEBUG_CPPFLAGS="-DDEBUG -DDEBUG_WOLFMQTT"
DEBUG_CPPFLAGS="-DDEBUG -DDEBUG_WOLFTPM"

AX_DEBUG
AS_IF([test "x$ax_enable_debug" = "xyes"],
Expand Down
11 changes: 10 additions & 1 deletion examples/tpm/include.am
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
# vim:ft=automake
# All paths should be given relative to the root

EXTRA_DIST+= examples/tpm/tpm2_demo.c
if BUILD_EXAMPLES
noinst_PROGRAMS += examples/tpm/tpm2_demo
noinst_HEADERS += examples/tpm/tpm2_demo.h
examples_tpm_tpm2_demo_SOURCES = examples/tpm/tpm2_demo.c
examples_tpm_tpm2_demo_LDADD = src/libwolftpm.la $(LIB_STATIC_ADD)
examples_tpm_tpm2_demo_DEPENDENCIES = src/libwolftpm.la
endif

dist_example_DATA+= examples/tpm/tpm2_demo.c
DISTCLEANFILES+= examples/tpm/.libs/tpm_demo
45 changes: 34 additions & 11 deletions examples/tpm/tpm2_demo.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* tpm2_demo.c
*
* Copyright (C) 2006-2017 wolfSSL Inc.
* Copyright (C) 2006-2018 wolfSSL Inc.
*
* This file is part of wolfSSL. (formerly known as CyaSSL)
*
Expand Down Expand Up @@ -30,40 +30,57 @@
#include <wolfssl/wolfcrypt/logging.h>

#include <wolftpm/tpm2.h>
#include <examples/tpm/tpm2_demo.h>

/* Local variables */
static TPM2_CTX gTpm2Ctx;

/* Configuration for the SPI interface */
#ifdef WOLFSSL_STM32_CUBEMX
extern SPI_HandleTypeDef hspi1;
#define TPM2_USER_CTX &hspi1
#else
#define TPM2_USER_CTX NULL
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define TPM2_USER_CTX (void*)"/dev/spidev0.0"
#endif

/* IO Callback */
static TPM_RC TPM2_IoCb(TPM2_CTX* ctx, const byte* txBuf, byte* rxBuf,
word16 xferSz, void* userCtx)
{
int ret = TPM_RC_FAILURE;
#ifdef WOLFSSL_STM32_CUBEMX
/* STM32 CubeMX Hal */
SPI_HandleTypeDef* hspi = (SPI_HandleTypeDef*)userCtx;
HAL_StatusTypeDef status;

__HAL_SPI_ENABLE(hspi);
status = HAL_SPI_TransmitReceive(hspi, (byte*)txBuf, rxBuf, xferSz, 5000);
__HAL_SPI_DISABLE(hspi);
if (status == HAL_OK)
return TPM_RC_SUCCESS;
ret = TPM_RC_SUCCESS;

#else
/* TODO: Add your platform here for HW interface */
/* Use Linux Style SPI access */
const char* devPath = (const char*)userCtx;
size_t size;
int devFile = open(devPath, O_RDWR);
if (devFile >= 0) {
size = write(devFile, txBuf, xferSz);
if (size == xferSz) {
size = read(devFile, rxBuf, xferSz);
ret = TPM_RC_SUCCESS;
}
close(devFile);
}
#endif

(void)ctx;
(void)txBuf;
(void)rxBuf;
(void)xferSz;
(void)userCtx;

#endif
return TPM_RC_FAILURE;
return ret;
}

#define RAND_GET_SZ 32
Expand Down Expand Up @@ -104,7 +121,6 @@ int TPM2_Demo(void)
} cmdOut;
int pcrCount, pcrIndex, i;
TPML_TAGGED_TPM_PROPERTY* tpmProp;
TPM_HANDLE ek;

#ifdef DEBUG_WOLFSSL
wolfSSL_Debugging_ON();
Expand Down Expand Up @@ -263,3 +279,10 @@ int TPM2_Demo(void)

return rc;
}

#ifndef NO_MAIN_DRIVER
int main(void)
{
return TPM2_Demo();
}
#endif
29 changes: 29 additions & 0 deletions examples/tpm/tpm2_demo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* tpm2_demo.h
*
* Copyright (C) 2006-2018 wolfSSL Inc.
*
* This file is part of wolfSSL. (formerly known as CyaSSL)
*
* wolfTPM 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.
*
* wolfTPM 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/

#ifndef _TPM2_DEMO_H_
#define _TPM2_DEMO_H_


int TPM2_Demo(void);


#endif /* _TPM2_DEMO_H_ */
2 changes: 1 addition & 1 deletion src/include.am
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ lib_LTLIBRARIES+= src/libwolftpm.la
src_libwolftpm_la_SOURCES = src/tpm2.c
src_libwolftpm_la_CFLAGS = -DBUILDING_WOLFTPM $(AM_CFLAGS)
src_libwolftpm_la_CPPFLAGS = -DBUILDING_WOLFTPM $(AM_CPPFLAGS)
src_libwolftpm_la_LDFLAGS = ${AM_LDFLAGS} -no-undefined -version-info ${WOLFMQTT_LIBRARY_VERSION}
src_libwolftpm_la_LDFLAGS = ${AM_LDFLAGS} -no-undefined -version-info ${WOLFTPM_LIBRARY_VERSION}
src_libwolftpm_la_DEPENDENCIES =
EXTRA_DIST +=
Loading

0 comments on commit 156fc9b

Please sign in to comment.