metee

Loading...
Searching...
No Matches
meteepp.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: Apache-2.0 */
2/*
3 * Copyright (C) 2021-2025 Intel Corporation
4 */
8#ifndef _METEEPP_H_
9#define _METEEPP_H_
10
11#include <sstream>
12#include <stdexcept>
13#include <string>
14#include <system_error>
15#include <vector>
16#include "metee.h"
17
18 namespace intel {
19 namespace security {
20
24
26 static class metee_category_t : public std::error_category {
27 public:
28 virtual const char* name() const noexcept { return "MeTee"; }
29 virtual std::string message(int ev) const {
30#define TEE_ERR_STATE(state) case TEE_##state: return #state
31 switch (ev) {
32 TEE_ERR_STATE(SUCCESS);
33 TEE_ERR_STATE(INTERNAL_ERROR);
34 TEE_ERR_STATE(DEVICE_NOT_FOUND);
35 TEE_ERR_STATE(DEVICE_NOT_READY);
36 TEE_ERR_STATE(INVALID_PARAMETER);
37 TEE_ERR_STATE(UNABLE_TO_COMPLETE_OPERATION);
38 TEE_ERR_STATE(TIMEOUT);
39 TEE_ERR_STATE(NOTSUPPORTED);
40 TEE_ERR_STATE(CLIENT_NOT_FOUND);
41 TEE_ERR_STATE(BUSY);
42 TEE_ERR_STATE(DISCONNECTED);
43 TEE_ERR_STATE(INSUFFICIENT_BUFFER);
44 TEE_ERR_STATE(PERMISSION_DENIED);
45 default:
46 return std::to_string(ev);
47 }
48#undef TEE_ERR_STATE
49 }
50 } metee_category;
51
53 class metee_exception : public std::system_error
54 {
55 public:
60 metee_exception(const std::string& what, int err = TEE_INTERNAL_ERROR) : std::system_error(err, metee_category, what) {}
65 metee_exception(int err, const std::error_category& cat) : std::system_error(err, cat) {}
71 metee_exception(int err, const std::error_category& cat, const std::string& what)
72 : std::system_error(err, cat, what) {}
73
74 virtual ~metee_exception() noexcept {}
75 };
76
78 DEFINE_GUID(METEE_GUID_ZERO,
79 0x00000000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
80
84 class metee
85 {
86 public:
88 metee() : metee(METEE_GUID_ZERO) {}
89
94 metee(uint32_t log_level, TeeLogCallback2 log_callback) : metee(METEE_GUID_ZERO, log_level, log_callback) {}
95
99 metee(const GUID& guid)
100 {
101 TEESTATUS status = TeeInit(&_handle, &guid, nullptr);
102 if (!TEE_IS_SUCCESS(status)) {
103 throw metee_exception("Init failed", status);
104 }
105 }
106
110 metee(const GUID& guid, uint32_t log_level) : metee(guid, log_level, static_cast<TeeLogCallback2>(nullptr)) {}
111
117 metee(const GUID& guid, uint32_t log_level, TeeLogCallback log_callback) :
118 metee(guid, { tee_device_address::TEE_DEVICE_TYPE_NONE , nullptr }, log_level, log_callback) {}
119
125 metee(const GUID& guid, uint32_t log_level, TeeLogCallback2 log_callback) :
126 metee(guid, { tee_device_address::TEE_DEVICE_TYPE_NONE , nullptr }, log_level, log_callback) {}
127
133 metee(const GUID& guid, const struct tee_device_address& device, uint32_t log_level) :
134 metee(guid, device, log_level, static_cast<TeeLogCallback2>(nullptr)) {}
135
142 metee(const GUID &guid, const struct tee_device_address &device, uint32_t log_level, TeeLogCallback log_callback)
143 {
144 TEESTATUS status = TeeInitFull(&_handle, &guid, device, log_level, log_callback);
145 if (!TEE_IS_SUCCESS(status)) {
146 throw metee_exception("Init failed", status);
147 }
148 }
149
156 metee(const GUID& guid, const struct tee_device_address& device, uint32_t log_level, TeeLogCallback2 log_callback)
157 {
158 TEESTATUS status = TeeInitFull2(&_handle, &guid, device, log_level, log_callback);
159 if (!TEE_IS_SUCCESS(status)) {
160 throw metee_exception("Init failed", status);
161 }
162 }
163
165 metee(const metee& other) = delete;
166
170 metee(metee&& other) noexcept : _handle(other._handle)
171 {
172 other._handle.handle = nullptr;
173 }
174
176 metee& operator=(const metee& other) = delete;
177
181 metee& operator=(metee&& other) noexcept
182 {
183 TeeDisconnect(&_handle);
184 _handle = other._handle;
185 other._handle.handle = nullptr;
186 return *this;
187 }
188
190 virtual ~metee()
191 {
192 TeeDisconnect(&_handle);
193 }
194
196 void connect()
197 {
198 TEESTATUS status;
199
200 status = TeeConnect(&_handle);
201 if (!TEE_IS_SUCCESS(status)) {
202 throw metee_exception("Connect failed", status);
203 }
204 }
205
210 std::vector<uint8_t> read(uint32_t timeout)
211 {
212 TEESTATUS status;
213 size_t size = 0;
214 std::vector<uint8_t> buffer(max_msg_len());
215
216 status = TeeRead(&_handle, buffer.data(), buffer.size(), &size, timeout);
217 if (!TEE_IS_SUCCESS(status)) {
218 throw metee_exception("Read failed", status);
219 }
220
221 buffer.resize(size);
222 return std::move(buffer);
223 }
224
230 size_t write(const std::vector<uint8_t> &buffer, uint32_t timeout)
231 {
232 TEESTATUS status;
233 size_t size = 0;
234
235 status = TeeWrite(&_handle, buffer.data(), buffer.size(), &size, timeout);
236 if (!TEE_IS_SUCCESS(status)) {
237 throw metee_exception("Write failed", status);
238 }
239
240 return size;
241 }
242
247 uint32_t fw_status(uint32_t fwStatusNum)
248 {
249 TEESTATUS status;
250 uint32_t fwStatus = 0;
251
252 status = TeeFWStatus(&_handle, fwStatusNum, &fwStatus);
253 if (!TEE_IS_SUCCESS(status)) {
254 throw metee_exception("FWStatus failed", status);
255 }
256
257 return fwStatus;
258 }
259
263 uint32_t trc()
264 {
265 TEESTATUS status;
266 uint32_t trc_val = 0;
267
268 status = TeeGetTRC(&_handle, &trc_val);
269 if (!TEE_IS_SUCCESS(status)) {
270 throw metee_exception("GetTRC failed", status);
271 }
272
273 return trc_val;
274 }
275
279 std::string kind()
280 {
281 TEESTATUS status;
282 const size_t KIND_SIZE = 32;
283 char kind[KIND_SIZE];
284 size_t kind_size = KIND_SIZE;
285
286 status = TeeGetKind(&_handle, kind, &kind_size);
287 if (!TEE_IS_SUCCESS(status)) {
288 throw metee_exception("TeeGetKind failed", status);
289 }
290
291 return kind;
292 }
293
298 uint32_t log_level(uint32_t log_level)
299 {
300 return TeeSetLogLevel(&_handle, log_level);
301 }
302
307 uint32_t log_level()
308 {
309 return TeeGetLogLevel(&_handle);
310 }
311
316 void log_callback(TeeLogCallback log_callback)
317 {
318 TeeSetLogCallback(&_handle, log_callback);
319 }
320
325 uint32_t max_msg_len()
326 {
327 return TeeGetMaxMsgLen(&_handle);
328 }
329
334 uint8_t protocol_ver()
335 {
336 return TeeGetProtocolVer(&_handle);
337 }
338
343 TEE_DEVICE_HANDLE device_handle()
344 {
345 return TeeGetDeviceHandle(&_handle);
346 }
347
352 std::string driver_version()
353 {
354 TEESTATUS status;
355 teeDriverVersion_t driverVersion = { 0 };
356
357 status = ::GetDriverVersion(&_handle, &driverVersion);
358 if (!TEE_IS_SUCCESS(status)) {
359 throw metee_exception("GetDriverVersion failed", status);
360 }
361
362 std::stringstream ss;
363 ss << driverVersion.major << "."
364 << driverVersion.minor << "."
365 << driverVersion.hotfix << "."
366 << driverVersion.build;
367 return ss.str();
368 }
369
370 private:
371 _TEEHANDLE _handle;
372 };
373 } // namespace security
374} // namespace intel
375#endif // _METEEPP_H_
intel::security::metee_exception
Definition meteepp.h:54
intel::security::metee_exception::metee_exception
metee_exception(const std::string &what, int err=TEE_INTERNAL_ERROR)
Definition meteepp.h:60
intel::security::metee_exception::~metee_exception
virtual ~metee_exception() noexcept
Definition meteepp.h:74
intel::security::metee_exception::metee_exception
metee_exception(int err, const std::error_category &cat, const std::string &what)
Definition meteepp.h:71
intel::security::metee_exception::metee_exception
metee_exception(int err, const std::error_category &cat)
Definition meteepp.h:65
intel::security::metee::metee
metee()
Definition meteepp.h:88
intel::security::metee::trc
uint32_t trc()
Definition meteepp.h:263
intel::security::metee::max_msg_len
uint32_t max_msg_len()
Definition meteepp.h:325
intel::security::metee::log_callback
void log_callback(TeeLogCallback log_callback)
Definition meteepp.h:316
intel::security::metee::metee
metee(const GUID &guid, const struct tee_device_address &device, uint32_t log_level)
Definition meteepp.h:133
intel::security::metee::metee
metee(const GUID &guid, uint32_t log_level)
Definition meteepp.h:110
intel::security::metee::metee
metee(const GUID &guid, uint32_t log_level, TeeLogCallback2 log_callback)
Definition meteepp.h:125
intel::security::metee::metee
metee(uint32_t log_level, TeeLogCallback2 log_callback)
Definition meteepp.h:94
intel::security::metee::metee
metee(metee &&other) noexcept
Definition meteepp.h:170
intel::security::metee::protocol_ver
uint8_t protocol_ver()
Definition meteepp.h:334
intel::security::metee::metee
metee(const GUID &guid, uint32_t log_level, TeeLogCallback log_callback)
Definition meteepp.h:117
intel::security::metee::metee
metee(const GUID &guid)
Definition meteepp.h:99
intel::security::metee::metee
metee(const metee &other)=delete
intel::security::metee::write
size_t write(const std::vector< uint8_t > &buffer, uint32_t timeout)
Definition meteepp.h:230
intel::security::metee::kind
std::string kind()
Definition meteepp.h:279
intel::security::metee::log_level
uint32_t log_level()
Definition meteepp.h:307
intel::security::metee::device_handle
TEE_DEVICE_HANDLE device_handle()
Definition meteepp.h:343
intel::security::metee::fw_status
uint32_t fw_status(uint32_t fwStatusNum)
Definition meteepp.h:247
intel::security::metee::connect
void connect()
Definition meteepp.h:196
intel::security::metee::operator=
metee & operator=(metee &&other) noexcept
Definition meteepp.h:181
intel::security::metee::metee
metee(const GUID &guid, const struct tee_device_address &device, uint32_t log_level, TeeLogCallback log_callback)
Definition meteepp.h:142
intel::security::metee::operator=
metee & operator=(const metee &other)=delete
intel::security::metee::~metee
virtual ~metee()
Definition meteepp.h:190
intel::security::metee::metee
metee(const GUID &guid, const struct tee_device_address &device, uint32_t log_level, TeeLogCallback2 log_callback)
Definition meteepp.h:156
intel::security::metee::driver_version
std::string driver_version()
Definition meteepp.h:352
intel::security::metee::log_level
uint32_t log_level(uint32_t log_level)
Definition meteepp.h:298
intel::security::metee::read
std::vector< uint8_t > read(uint32_t timeout)
Definition meteepp.h:210
metee.h
metee library API
TEESTATUS
uint16_t TEESTATUS
Definition metee.h:164
TeeLogCallback
void(* TeeLogCallback)(bool is_error, const char *fmt,...)
Definition metee.h:96
TeeLogCallback2
void(* TeeLogCallback2)(bool is_error, const char *msg)
Definition metee.h:100
TeeSetLogCallback
TEESTATUS TEEAPI TeeSetLogCallback(IN const PTEEHANDLE handle, TeeLogCallback log_callback)
TEE_IS_SUCCESS
#define TEE_IS_SUCCESS(Status)
Definition metee.h:196
GetDriverVersion
TEESTATUS TEEAPI GetDriverVersion(IN PTEEHANDLE handle, IN OUT teeDriverVersion_t *driverVersion)
TeeRead
TEESTATUS TEEAPI TeeRead(IN PTEEHANDLE handle, IN OUT void *buffer, IN size_t bufferSize, OUT OPTIONAL size_t *pNumOfBytesRead, IN OPTIONAL uint32_t timeout)
TeeGetDeviceHandle
TEE_DEVICE_HANDLE TEEAPI TeeGetDeviceHandle(IN PTEEHANDLE handle)
TeeFWStatus
TEESTATUS TEEAPI TeeFWStatus(IN PTEEHANDLE handle, IN uint32_t fwStatusNum, OUT uint32_t *fwStatus)
TeeGetLogLevel
uint32_t TEEAPI TeeGetLogLevel(IN const PTEEHANDLE handle)
TeeInitFull2
TEESTATUS TEEAPI TeeInitFull2(IN OUT PTEEHANDLE handle, IN const GUID *guid, IN const struct tee_device_address device, IN uint32_t log_level, IN OPTIONAL TeeLogCallback2 log_callback)
TeeGetTRC
TEESTATUS TEEAPI TeeGetTRC(IN PTEEHANDLE handle, OUT uint32_t *trc_val)
TeeGetKind
TEESTATUS TEEAPI TeeGetKind(IN PTEEHANDLE handle, IN OUT char *kind, IN OUT size_t *kindSize)
TeeInit
TEESTATUS TEEAPI TeeInit(IN OUT PTEEHANDLE handle, IN const GUID *guid, IN OPTIONAL const char *device)
TeeGetMaxMsgLen
uint32_t TEEAPI TeeGetMaxMsgLen(IN const PTEEHANDLE handle)
TeeDisconnect
void TEEAPI TeeDisconnect(IN PTEEHANDLE handle)
TeeConnect
TEESTATUS TEEAPI TeeConnect(OUT PTEEHANDLE handle)
TeeInitFull
TEESTATUS TEEAPI TeeInitFull(IN OUT PTEEHANDLE handle, IN const GUID *guid, IN const struct tee_device_address device, IN uint32_t log_level, IN OPTIONAL TeeLogCallback log_callback)
TeeWrite
TEESTATUS TEEAPI TeeWrite(IN PTEEHANDLE handle, IN const void *buffer, IN size_t bufferSize, OUT OPTIONAL size_t *numberOfBytesWritten, IN OPTIONAL uint32_t timeout)
TEE_INTERNAL_ERROR
#define TEE_INTERNAL_ERROR
Definition metee.h:170
TeeSetLogLevel
uint32_t TEEAPI TeeSetLogLevel(IN PTEEHANDLE handle, IN uint32_t log_level)
TeeGetProtocolVer
uint8_t TEEAPI TeeGetProtocolVer(IN const PTEEHANDLE handle)
TEE_ERR_STATE
#define TEE_ERR_STATE(state)
intel::security::DEFINE_GUID
DEFINE_GUID(METEE_GUID_ZERO, 0x00000000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
_TEEHANDLE
Definition metee.h:107
teeDriverVersion_t
Definition metee.h:320
teeDriverVersion_t::major
uint16_t major
Definition metee.h:321
teeDriverVersion_t::build
uint16_t build
Definition metee.h:324
teeDriverVersion_t::minor
uint16_t minor
Definition metee.h:322
teeDriverVersion_t::hotfix
uint16_t hotfix
Definition metee.h:323
tee_device_address
Definition metee.h:125
tee_device_address::TEE_DEVICE_TYPE_NONE
@ TEE_DEVICE_TYPE_NONE
Definition metee.h:129