^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #ifndef __TRUSTED_TPM_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #define __TRUSTED_TPM_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <keys/trusted-type.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/tpm_command.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) /* implementation specific TPM constants */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #define MAX_BUF_SIZE 1024
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #define TPM_GETRANDOM_SIZE 14
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #define TPM_SIZE_OFFSET 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #define TPM_RETURN_OFFSET 6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #define TPM_DATA_OFFSET 10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #define LOAD32(buffer, offset) (ntohl(*(uint32_t *)&buffer[offset]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define LOAD32N(buffer, offset) (*(uint32_t *)&buffer[offset])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define LOAD16(buffer, offset) (ntohs(*(uint16_t *)&buffer[offset]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct osapsess {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) uint32_t handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) unsigned char secret[SHA1_DIGEST_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) unsigned char enonce[TPM_NONCE_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) /* discrete values, but have to store in uint16_t for TPM use */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) SEAL_keytype = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) SRK_keytype = 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) int TSS_authhmac(unsigned char *digest, const unsigned char *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) unsigned int keylen, unsigned char *h1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) unsigned char *h2, unsigned int h3, ...);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) int TSS_checkhmac1(unsigned char *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) const uint32_t command,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) const unsigned char *ononce,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) const unsigned char *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) unsigned int keylen, ...);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) int trusted_tpm_send(unsigned char *cmd, size_t buflen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) int oiap(struct tpm_buf *tb, uint32_t *handle, unsigned char *nonce);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) int tpm2_seal_trusted(struct tpm_chip *chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) struct trusted_key_payload *payload,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct trusted_key_options *options);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) int tpm2_unseal_trusted(struct tpm_chip *chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct trusted_key_payload *payload,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct trusted_key_options *options);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #define TPM_DEBUG 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #if TPM_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) static inline void dump_options(struct trusted_key_options *o)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) pr_info("trusted_key: sealing key type %d\n", o->keytype);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) pr_info("trusted_key: sealing key handle %0X\n", o->keyhandle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) pr_info("trusted_key: pcrlock %d\n", o->pcrlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) pr_info("trusted_key: pcrinfo %d\n", o->pcrinfo_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) print_hex_dump(KERN_INFO, "pcrinfo ", DUMP_PREFIX_NONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) 16, 1, o->pcrinfo, o->pcrinfo_len, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) static inline void dump_payload(struct trusted_key_payload *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) pr_info("trusted_key: key_len %d\n", p->key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) print_hex_dump(KERN_INFO, "key ", DUMP_PREFIX_NONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) 16, 1, p->key, p->key_len, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) pr_info("trusted_key: bloblen %d\n", p->blob_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) print_hex_dump(KERN_INFO, "blob ", DUMP_PREFIX_NONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) 16, 1, p->blob, p->blob_len, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) pr_info("trusted_key: migratable %d\n", p->migratable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) static inline void dump_sess(struct osapsess *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) print_hex_dump(KERN_INFO, "trusted-key: handle ", DUMP_PREFIX_NONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) 16, 1, &s->handle, 4, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) pr_info("trusted-key: secret:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) 16, 1, &s->secret, SHA1_DIGEST_SIZE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) pr_info("trusted-key: enonce:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) 16, 1, &s->enonce, SHA1_DIGEST_SIZE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static inline void dump_tpm_buf(unsigned char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) pr_info("\ntrusted-key: tpm buffer\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) len = LOAD32(buf, TPM_SIZE_OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE, 16, 1, buf, len, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) static inline void dump_options(struct trusted_key_options *o)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) static inline void dump_payload(struct trusted_key_payload *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static inline void dump_sess(struct osapsess *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static inline void dump_tpm_buf(unsigned char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) #endif