blob: f45c0bde2a4db0c43584877199029a65c0ebb814 [file] [log] [blame]
NingSun7793a612018-04-10 15:49:01 -07001/* Copyright 2018 Intel Corporation, Inc
2*
3* Licensed under the Apache License, Version 2.0 (the "License");
4* you may not use this file except in compliance with the License.
5* You may obtain a copy of the License at
6*
7* http://www.apache.org/licenses/LICENSE-2.0
8*
9* Unless required by applicable law or agreed to in writing, software
10* distributed under the License is distributed on an "AS IS" BASIS,
11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12* See the License for the specific language governing permissions and
13* limitations under the License.
14*/
NingSun535535b2018-02-28 18:24:31 -080015
16#ifndef __TPM_API_H__
17#define __TPM_API_H__
18
19#include <stdlib.h>
20#include <stdio.h>
21#include <string.h>
22#include <limits.h>
23#include <ctype.h>
24#include <getopt.h>
25
NingSun7793a612018-04-10 15:49:01 -070026#include <sapi/tpm20.h>
NingSun337e6752018-03-29 21:06:26 -070027#include "hwpluginif.h"
NingSun535535b2018-02-28 18:24:31 -080028
29#ifdef __cplusplus
30extern "C" {
31#endif
32
33#define TPM_SKM_SRK_HANDLE 0x81000011
34
35#define TPM_SKM_AC0_HANDLE 0x90000000
36#define TPM_SKM_AC1_HANDLE 0x90000001
37#define TPM_SKM_AC2_HANDLE 0x90000002
38
39#define TPM_SKM_APP_HANDLE 0x91100001
40
41#define INIT_SIMPLE_TPM2B_SIZE( type ) (type).t.size = sizeof( type ) - 2;
42
43#define APP_RC_OFFSET 0x100
44
45#define TSS2_APP_RC_PASSED (APP_RC_PASSED + APP_RC_OFFSET + TSS2_APP_ERROR_LEVEL)
46#define TSS2_APP_RC_GET_NAME_FAILED (APP_RC_GET_NAME_FAILED + APP_RC_OFFSET + TSS2_APP_ERROR_LEVEL)
47#define TSS2_APP_RC_CREATE_SESSION_KEY_FAILED (APP_RC_CREATE_SESSION_KEY_FAILED + APP_RC_OFFSET + TSS2_APP_ERROR_LEVEL)
48#define TSS2_APP_RC_SESSION_SLOT_NOT_FOUND (APP_RC_SESSION_SLOT_NOT_FOUND + APP_RC_OFFSET + TSS2_APP_ERROR_LEVEL)
49#define TSS2_APP_RC_BAD_ALGORITHM (APP_RC_BAD_ALGORITHM + APP_RC_OFFSET + TSS2_APP_ERROR_LEVEL)
50#define TSS2_APP_RC_SYS_CONTEXT_CREATE_FAILED (APP_RC_SYS_CONTEXT_CREATE_FAILED + APP_RC_OFFSET + TSS2_APP_ERROR_LEVEL)
51#define TSS2_APP_RC_GET_SESSION_STRUCT_FAILED (APP_RC_GET_SESSION_STRUCT_FAILED + APP_RC_OFFSET + TSS2_APP_ERROR_LEVEL)
52#define TSS2_APP_RC_GET_SESSION_ALG_ID_FAILED (APP_RC_GET_SESSION_ALG_ID_FAILED + APP_RC_OFFSET + TSS2_APP_ERROR_LEVEL)
53#define TSS2_APP_RC_INIT_SYS_CONTEXT_FAILED (APP_RC_INIT_SYS_CONTEXT_FAILED + APP_RC_OFFSET + TSS2_APP_ERROR_LEVEL)
54#define TSS2_APP_RC_TEARDOWN_SYS_CONTEXT_FAILED (APP_RC_TEARDOWN_SYS_CONTEXT_FAILED + APP_RC_OFFSET + TSS2_APP_ERROR_LEVEL)
55#define TSS2_APP_RC_BAD_LOCALITY (APP_RC_BAD_LOCALITY + APP_RC_OFFSET + TSS2_APP_ERROR_LEVEL)
56
NingSun7793a612018-04-10 15:49:01 -070057
58//#define HAVE_TCTI_DEV 1
59#define HAVE_TCTI_TABRMD 1
60//#define TCTI_DEFAULT HAVE_TCTI_DEV
NingSun535535b2018-02-28 18:24:31 -080061enum TSS2_APP_RC_CODE
62{
63 APP_RC_PASSED,
64 APP_RC_GET_NAME_FAILED,
65 APP_RC_CREATE_SESSION_KEY_FAILED,
66 APP_RC_SESSION_SLOT_NOT_FOUND,
67 APP_RC_BAD_ALGORITHM,
68 APP_RC_SYS_CONTEXT_CREATE_FAILED,
69 APP_RC_GET_SESSION_STRUCT_FAILED,
70 APP_RC_GET_SESSION_ALG_ID_FAILED,
71 APP_RC_INIT_SYS_CONTEXT_FAILED,
72 APP_RC_TEARDOWN_SYS_CONTEXT_FAILED,
73 APP_RC_BAD_LOCALITY
74};
75
76TSS2_SYS_CONTEXT *InitSysContext (UINT16 maxCommandSize,
77 TSS2_TCTI_CONTEXT *tctiContext,
78 TSS2_ABI_VERSION *abiVersion );
79
80void TeardownSysContext( TSS2_SYS_CONTEXT **sysContext );
81
82TSS2_RC TeardownTctiResMgrContext( TSS2_TCTI_CONTEXT *tctiContext );
83
NingSun7793a612018-04-10 15:49:01 -070084
85#ifdef HAVE_TCTI_TABRMD
86 #define TCTI_DEFAULT TABRMD_TCTI
87 #define TCTI_DEFAULT_STR "tabrmd"
88#elif HAVE_TCTI_SOCK
89 #define TCTI_DEFAULT SOCKET_TCTI
90 #define TCTI_DEFAULT_STR "socket"
91#elif HAVE_TCTI_DEV
92 #define TCTI_DEFAULT DEVICE_TCTI
93 #define TCTI_DEFAULT_STR "device"
94#endif
95
96
97/* Defaults for Device TCTI */
98#define TCTI_DEVICE_DEFAULT_PATH "/dev/tpm0"
99
100/* Deafults for Socket TCTI connections, port default is for resourcemgr */
101#define TCTI_SOCKET_DEFAULT_ADDRESS "127.0.0.1"
102#define TCTI_SOCKET_DEFAULT_PORT 2321
103
104/* Environment variables usable as alternatives to command line options */
105#define TPM2TOOLS_ENV_TCTI_NAME "TPM2TOOLS_TCTI_NAME"
106#define TPM2TOOLS_ENV_DEVICE_FILE "TPM2TOOLS_DEVICE_FILE"
107#define TPM2TOOLS_ENV_SOCKET_ADDRESS "TPM2TOOLS_SOCKET_ADDRESS"
108#define TPM2TOOLS_ENV_SOCKET_PORT "TPM2TOOLS_SOCKET_PORT"
109
110#define COMMON_OPTS_INITIALIZER { \
111 .tcti_type = TCTI_DEFAULT, \
112 .device_file = TCTI_DEVICE_DEFAULT_PATH, \
113 .socket_address = TCTI_SOCKET_DEFAULT_ADDRESS, \
114 .socket_port = TCTI_SOCKET_DEFAULT_PORT, \
115 .help = false, \
116 .verbose = false, \
117 .version = false, \
118}
119
120typedef enum {
121#ifdef HAVE_TCTI_DEV
122 DEVICE_TCTI,
123#endif
124#ifdef HAVE_TCTI_SOCK
125 SOCKET_TCTI,
126#endif
127#ifdef HAVE_TCTI_TABRMD
128 TABRMD_TCTI,
129#endif
130 UNKNOWN_TCTI,
131 N_TCTI,
132} TCTI_TYPE;
133
134typedef struct {
135 TCTI_TYPE tcti_type;
136 char *device_file;
137 char *socket_address;
138 uint16_t socket_port;
139 int help;
140 int verbose;
141 int version;
142} common_opts_t;
143
NingSun8a5b33a2018-03-27 10:42:51 -0700144int tpm2_plugin_init();
145int tpm2_plugin_uninit();
NingSun337e6752018-03-29 21:06:26 -0700146int tpm2_plugin_activate(SSHSM_HW_PLUGIN_ACTIVATE_LOAD_IN_INFO_t *activate_in_info);
NingSun8a5b33a2018-03-27 10:42:51 -0700147int tpm2_plugin_load_key(
NingSun337e6752018-03-29 21:06:26 -0700148 SSHSM_HW_PLUGIN_ACTIVATE_LOAD_IN_INFO_t *loadkey_in_info,
NingSunc1c9bfc2018-05-17 09:09:12 -0700149 void **keyHandle,
150 SSHSM_HW_PLUGIN_IMPORT_PUBLIC_KEY_INFO_t *importkey_info
NingSun8a5b33a2018-03-27 10:42:51 -0700151 );
152
NingSun535535b2018-02-28 18:24:31 -0800153int tpm2_rsa_create_object(
154 unsigned long appHandle,
155 //DhsmWPKRSAFormat* wpk,
156 void *wpk,
157 unsigned char* swk,
158 int swk_len,
159 unsigned char* iv,
160 int iv_len,
161 int tag_len,
162 void **cb_object);
163
164int tpm2_rsa_delete_object(
165 void *cb_object);
166
NingSun8a5b33a2018-03-27 10:42:51 -0700167int tpm2_plugin_rsa_sign_init(
NingSun337e6752018-03-29 21:06:26 -0700168 void *keyHandle,
NingSun7793a612018-04-10 15:49:01 -0700169 unsigned long mechanism,
NingSun535535b2018-02-28 18:24:31 -0800170 void *param,
NingSun337e6752018-03-29 21:06:26 -0700171 int len);
NingSun535535b2018-02-28 18:24:31 -0800172
NingSun8a5b33a2018-03-27 10:42:51 -0700173int tpm2_plugin_rsa_sign(
NingSun337e6752018-03-29 21:06:26 -0700174 void *keyHandle,
175 unsigned long mechanism,
NingSun535535b2018-02-28 18:24:31 -0800176 unsigned char *msg,
177 int msg_len,
178 unsigned char *sig,
179 int *sig_len);
180
181
182int tpm2_import_object(
NingSun8a5b33a2018-03-27 10:42:51 -0700183 unsigned long appHandle,
184 unsigned char* tlvbuffer,
185 int buflen,
186 unsigned char* iv,
187 int iv_len,
188 unsigned char* tpm_pwd,
NingSun535535b2018-02-28 18:24:31 -0800189 int tpm_pwd_len);
190
191
192#ifdef __cplusplus
193}
194#endif
195
196
197#endif