NingSun | 7793a61 | 2018-04-10 15:49:01 -0700 | [diff] [blame] | 1 | /* 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 | */ |
NingSun | 535535b | 2018-02-28 18:24:31 -0800 | [diff] [blame] | 15 | |
| 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 | |
NingSun | 7793a61 | 2018-04-10 15:49:01 -0700 | [diff] [blame] | 26 | #include <sapi/tpm20.h> |
NingSun | 337e675 | 2018-03-29 21:06:26 -0700 | [diff] [blame] | 27 | #include "hwpluginif.h" |
NingSun | 535535b | 2018-02-28 18:24:31 -0800 | [diff] [blame] | 28 | |
| 29 | #ifdef __cplusplus |
| 30 | extern "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 | |
NingSun | 7793a61 | 2018-04-10 15:49:01 -0700 | [diff] [blame] | 57 | |
| 58 | //#define HAVE_TCTI_DEV 1 |
| 59 | #define HAVE_TCTI_TABRMD 1 |
| 60 | //#define TCTI_DEFAULT HAVE_TCTI_DEV |
NingSun | 535535b | 2018-02-28 18:24:31 -0800 | [diff] [blame] | 61 | enum 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 | |
| 76 | TSS2_SYS_CONTEXT *InitSysContext (UINT16 maxCommandSize, |
| 77 | TSS2_TCTI_CONTEXT *tctiContext, |
| 78 | TSS2_ABI_VERSION *abiVersion ); |
| 79 | |
| 80 | void TeardownSysContext( TSS2_SYS_CONTEXT **sysContext ); |
| 81 | |
| 82 | TSS2_RC TeardownTctiResMgrContext( TSS2_TCTI_CONTEXT *tctiContext ); |
| 83 | |
NingSun | 7793a61 | 2018-04-10 15:49:01 -0700 | [diff] [blame] | 84 | |
| 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 | |
| 120 | typedef 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 | |
| 134 | typedef 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 | |
NingSun | 8a5b33a | 2018-03-27 10:42:51 -0700 | [diff] [blame] | 144 | int tpm2_plugin_init(); |
| 145 | int tpm2_plugin_uninit(); |
NingSun | 337e675 | 2018-03-29 21:06:26 -0700 | [diff] [blame] | 146 | int tpm2_plugin_activate(SSHSM_HW_PLUGIN_ACTIVATE_LOAD_IN_INFO_t *activate_in_info); |
NingSun | 8a5b33a | 2018-03-27 10:42:51 -0700 | [diff] [blame] | 147 | int tpm2_plugin_load_key( |
NingSun | 337e675 | 2018-03-29 21:06:26 -0700 | [diff] [blame] | 148 | SSHSM_HW_PLUGIN_ACTIVATE_LOAD_IN_INFO_t *loadkey_in_info, |
NingSun | c1c9bfc | 2018-05-17 09:09:12 -0700 | [diff] [blame^] | 149 | void **keyHandle, |
| 150 | SSHSM_HW_PLUGIN_IMPORT_PUBLIC_KEY_INFO_t *importkey_info |
NingSun | 8a5b33a | 2018-03-27 10:42:51 -0700 | [diff] [blame] | 151 | ); |
| 152 | |
NingSun | 535535b | 2018-02-28 18:24:31 -0800 | [diff] [blame] | 153 | int 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 | |
| 164 | int tpm2_rsa_delete_object( |
| 165 | void *cb_object); |
| 166 | |
NingSun | 8a5b33a | 2018-03-27 10:42:51 -0700 | [diff] [blame] | 167 | int tpm2_plugin_rsa_sign_init( |
NingSun | 337e675 | 2018-03-29 21:06:26 -0700 | [diff] [blame] | 168 | void *keyHandle, |
NingSun | 7793a61 | 2018-04-10 15:49:01 -0700 | [diff] [blame] | 169 | unsigned long mechanism, |
NingSun | 535535b | 2018-02-28 18:24:31 -0800 | [diff] [blame] | 170 | void *param, |
NingSun | 337e675 | 2018-03-29 21:06:26 -0700 | [diff] [blame] | 171 | int len); |
NingSun | 535535b | 2018-02-28 18:24:31 -0800 | [diff] [blame] | 172 | |
NingSun | 8a5b33a | 2018-03-27 10:42:51 -0700 | [diff] [blame] | 173 | int tpm2_plugin_rsa_sign( |
NingSun | 337e675 | 2018-03-29 21:06:26 -0700 | [diff] [blame] | 174 | void *keyHandle, |
| 175 | unsigned long mechanism, |
NingSun | 535535b | 2018-02-28 18:24:31 -0800 | [diff] [blame] | 176 | unsigned char *msg, |
| 177 | int msg_len, |
| 178 | unsigned char *sig, |
| 179 | int *sig_len); |
| 180 | |
| 181 | |
| 182 | int tpm2_import_object( |
NingSun | 8a5b33a | 2018-03-27 10:42:51 -0700 | [diff] [blame] | 183 | unsigned long appHandle, |
| 184 | unsigned char* tlvbuffer, |
| 185 | int buflen, |
| 186 | unsigned char* iv, |
| 187 | int iv_len, |
| 188 | unsigned char* tpm_pwd, |
NingSun | 535535b | 2018-02-28 18:24:31 -0800 | [diff] [blame] | 189 | int tpm_pwd_len); |
| 190 | |
| 191 | |
| 192 | #ifdef __cplusplus |
| 193 | } |
| 194 | #endif |
| 195 | |
| 196 | |
| 197 | #endif |