147f9c279SSumit Garg /* SPDX-License-Identifier: GPL-2.0 */ 247f9c279SSumit Garg #ifndef __TRUSTED_TPM_H 347f9c279SSumit Garg #define __TRUSTED_TPM_H 447f9c279SSumit Garg 547f9c279SSumit Garg #include <keys/trusted-type.h> 647f9c279SSumit Garg #include <linux/tpm_command.h> 747f9c279SSumit Garg 847f9c279SSumit Garg /* implementation specific TPM constants */ 947f9c279SSumit Garg #define MAX_BUF_SIZE 1024 1047f9c279SSumit Garg #define TPM_GETRANDOM_SIZE 14 1147f9c279SSumit Garg #define TPM_SIZE_OFFSET 2 1247f9c279SSumit Garg #define TPM_RETURN_OFFSET 6 1347f9c279SSumit Garg #define TPM_DATA_OFFSET 10 1447f9c279SSumit Garg 1547f9c279SSumit Garg #define LOAD32(buffer, offset) (ntohl(*(uint32_t *)&buffer[offset])) 1647f9c279SSumit Garg #define LOAD32N(buffer, offset) (*(uint32_t *)&buffer[offset]) 1747f9c279SSumit Garg #define LOAD16(buffer, offset) (ntohs(*(uint16_t *)&buffer[offset])) 1847f9c279SSumit Garg 1947f9c279SSumit Garg struct osapsess { 2047f9c279SSumit Garg uint32_t handle; 2147f9c279SSumit Garg unsigned char secret[SHA1_DIGEST_SIZE]; 2247f9c279SSumit Garg unsigned char enonce[TPM_NONCE_SIZE]; 2347f9c279SSumit Garg }; 2447f9c279SSumit Garg 2547f9c279SSumit Garg /* discrete values, but have to store in uint16_t for TPM use */ 2647f9c279SSumit Garg enum { 2747f9c279SSumit Garg SEAL_keytype = 1, 2847f9c279SSumit Garg SRK_keytype = 4 2947f9c279SSumit Garg }; 3047f9c279SSumit Garg 3147f9c279SSumit Garg int TSS_authhmac(unsigned char *digest, const unsigned char *key, 3247f9c279SSumit Garg unsigned int keylen, unsigned char *h1, 3347f9c279SSumit Garg unsigned char *h2, unsigned int h3, ...); 3447f9c279SSumit Garg int TSS_checkhmac1(unsigned char *buffer, 3547f9c279SSumit Garg const uint32_t command, 3647f9c279SSumit Garg const unsigned char *ononce, 3747f9c279SSumit Garg const unsigned char *key, 3847f9c279SSumit Garg unsigned int keylen, ...); 3947f9c279SSumit Garg 4047f9c279SSumit Garg int trusted_tpm_send(unsigned char *cmd, size_t buflen); 4147f9c279SSumit Garg int oiap(struct tpm_buf *tb, uint32_t *handle, unsigned char *nonce); 4247f9c279SSumit Garg 4347f9c279SSumit Garg #define TPM_DEBUG 0 4447f9c279SSumit Garg 4547f9c279SSumit Garg #if TPM_DEBUG 4647f9c279SSumit Garg static inline void dump_options(struct trusted_key_options *o) 4747f9c279SSumit Garg { 4847f9c279SSumit Garg pr_info("trusted_key: sealing key type %d\n", o->keytype); 4947f9c279SSumit Garg pr_info("trusted_key: sealing key handle %0X\n", o->keyhandle); 5047f9c279SSumit Garg pr_info("trusted_key: pcrlock %d\n", o->pcrlock); 5147f9c279SSumit Garg pr_info("trusted_key: pcrinfo %d\n", o->pcrinfo_len); 5247f9c279SSumit Garg print_hex_dump(KERN_INFO, "pcrinfo ", DUMP_PREFIX_NONE, 5347f9c279SSumit Garg 16, 1, o->pcrinfo, o->pcrinfo_len, 0); 5447f9c279SSumit Garg } 5547f9c279SSumit Garg 5647f9c279SSumit Garg static inline void dump_payload(struct trusted_key_payload *p) 5747f9c279SSumit Garg { 5847f9c279SSumit Garg pr_info("trusted_key: key_len %d\n", p->key_len); 5947f9c279SSumit Garg print_hex_dump(KERN_INFO, "key ", DUMP_PREFIX_NONE, 6047f9c279SSumit Garg 16, 1, p->key, p->key_len, 0); 6147f9c279SSumit Garg pr_info("trusted_key: bloblen %d\n", p->blob_len); 6247f9c279SSumit Garg print_hex_dump(KERN_INFO, "blob ", DUMP_PREFIX_NONE, 6347f9c279SSumit Garg 16, 1, p->blob, p->blob_len, 0); 6447f9c279SSumit Garg pr_info("trusted_key: migratable %d\n", p->migratable); 6547f9c279SSumit Garg } 6647f9c279SSumit Garg 6747f9c279SSumit Garg static inline void dump_sess(struct osapsess *s) 6847f9c279SSumit Garg { 6947f9c279SSumit Garg print_hex_dump(KERN_INFO, "trusted-key: handle ", DUMP_PREFIX_NONE, 7047f9c279SSumit Garg 16, 1, &s->handle, 4, 0); 7147f9c279SSumit Garg pr_info("trusted-key: secret:\n"); 7247f9c279SSumit Garg print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE, 7347f9c279SSumit Garg 16, 1, &s->secret, SHA1_DIGEST_SIZE, 0); 7447f9c279SSumit Garg pr_info("trusted-key: enonce:\n"); 7547f9c279SSumit Garg print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE, 7647f9c279SSumit Garg 16, 1, &s->enonce, SHA1_DIGEST_SIZE, 0); 7747f9c279SSumit Garg } 7847f9c279SSumit Garg 7947f9c279SSumit Garg static inline void dump_tpm_buf(unsigned char *buf) 8047f9c279SSumit Garg { 8147f9c279SSumit Garg int len; 8247f9c279SSumit Garg 8347f9c279SSumit Garg pr_info("\ntrusted-key: tpm buffer\n"); 8447f9c279SSumit Garg len = LOAD32(buf, TPM_SIZE_OFFSET); 8547f9c279SSumit Garg print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE, 16, 1, buf, len, 0); 8647f9c279SSumit Garg } 8747f9c279SSumit Garg #else 8847f9c279SSumit Garg static inline void dump_options(struct trusted_key_options *o) 8947f9c279SSumit Garg { 9047f9c279SSumit Garg } 9147f9c279SSumit Garg 9247f9c279SSumit Garg static inline void dump_payload(struct trusted_key_payload *p) 9347f9c279SSumit Garg { 9447f9c279SSumit Garg } 9547f9c279SSumit Garg 9647f9c279SSumit Garg static inline void dump_sess(struct osapsess *s) 9747f9c279SSumit Garg { 9847f9c279SSumit Garg } 9947f9c279SSumit Garg 10047f9c279SSumit Garg static inline void dump_tpm_buf(unsigned char *buf) 10147f9c279SSumit Garg { 10247f9c279SSumit Garg } 10347f9c279SSumit Garg #endif 10447f9c279SSumit Garg #endif 105