metee: meteepp.h Source File

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
18namespace 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 cancel_io()
197 {
198 TeeCancelIO(&_handle);
199 }
200
202 void connect()
203 {
204 TEESTATUS status;
205
206 status = TeeConnect(&_handle);
207 if (!TEE_IS_SUCCESS(status)) {
208 throw metee_exception("Connect failed", status);
209 }
210 }
211
216 std::vector<uint8_t> read(uint32_t timeout)
217 {
218 TEESTATUS status;
219 size_t size = 0;
220 std::vector<uint8_t> buffer(max_msg_len());
221
222 status = TeeRead(&_handle, buffer.data(), buffer.size(), &size, timeout);
223 if (!TEE_IS_SUCCESS(status)) {
224 throw metee_exception("Read failed", status);
225 }
226
227 buffer.resize(size);
228 return std::move(buffer);
229 }
230
236 size_t write(const std::vector<uint8_t> &buffer, uint32_t timeout)
237 {
238 TEESTATUS status;
239 size_t size = 0;
240
241 status = TeeWrite(&_handle, buffer.data(), buffer.size(), &size, timeout);
242 if (!TEE_IS_SUCCESS(status)) {
243 throw metee_exception("Write failed", status);
244 }
245
246 return size;
247 }
248
253 uint32_t fw_status(uint32_t fwStatusNum)
254 {
255 TEESTATUS status;
256 uint32_t fwStatus = 0;
257
258 status = TeeFWStatus(&_handle, fwStatusNum, &fwStatus);
259 if (!TEE_IS_SUCCESS(status)) {
260 throw metee_exception("FWStatus failed", status);
261 }
262
263 return fwStatus;
264 }
265
269 uint32_t trc()
270 {
271 TEESTATUS status;
272 uint32_t trc_val = 0;
273
274 status = TeeGetTRC(&_handle, &trc_val);
275 if (!TEE_IS_SUCCESS(status)) {
276 throw metee_exception("GetTRC failed", status);
277 }
278
279 return trc_val;
280 }
281
285 std::string kind()
286 {
287 TEESTATUS status;
288 const size_t KIND_SIZE = 32;
289 char kind[KIND_SIZE];
290 size_t kind_size = KIND_SIZE;
291
292 status = TeeGetKind(&_handle, kind, &kind_size);
293 if (!TEE_IS_SUCCESS(status)) {
294 throw metee_exception("TeeGetKind failed", status);
295 }
296
297 return kind;
298 }
299
304 uint32_t log_level(uint32_t log_level)
305 {
306 return TeeSetLogLevel(&_handle, log_level);
307 }
308
313 uint32_t log_level()
314 {
315 return TeeGetLogLevel(&_handle);
316 }
317
322 void log_callback(TeeLogCallback log_callback)
323 {
324 TeeSetLogCallback(&_handle, log_callback);
325 }
326
331 uint32_t max_msg_len()
332 {
333 return TeeGetMaxMsgLen(&_handle);
334 }
335
340 uint8_t protocol_ver()
341 {
342 return TeeGetProtocolVer(&_handle);
343 }
344
349 TEE_DEVICE_HANDLE device_handle()
350 {
351 return TeeGetDeviceHandle(&_handle);
352 }
353
358 std::string driver_version()
359 {
360 TEESTATUS status;
361 teeDriverVersion_t driverVersion = { 0 };
362
363 status = ::GetDriverVersion(&_handle, &driverVersion);
364 if (!TEE_IS_SUCCESS(status)) {
365 throw metee_exception("GetDriverVersion failed", status);
366 }
367
368 std::stringstream ss;
369 ss << driverVersion.major << "."
370 << driverVersion.minor << "."
371 << driverVersion.hotfix << "."
372 << driverVersion.build;
373 return ss.str();
374 }
375
376 private:
377 _TEEHANDLE _handle;
378 };
379 } // namespace security
380} // namespace intel
381#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:269
intel::security::metee::max_msg_len
uint32_t max_msg_len()
Definition meteepp.h:331
intel::security::metee::log_callback
void log_callback(TeeLogCallback log_callback)
Definition meteepp.h:322
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:340
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:236
intel::security::metee::kind
std::string kind()
Definition meteepp.h:285
intel::security::metee::log_level
uint32_t log_level()
Definition meteepp.h:313
intel::security::metee::device_handle
TEE_DEVICE_HANDLE device_handle()
Definition meteepp.h:349
intel::security::metee::cancel_io
void cancel_io()
Definition meteepp.h:196
intel::security::metee::fw_status
uint32_t fw_status(uint32_t fwStatusNum)
Definition meteepp.h:253
intel::security::metee::connect
void connect()
Definition meteepp.h:202
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:358
intel::security::metee::log_level
uint32_t log_level(uint32_t log_level)
Definition meteepp.h:304
intel::security::metee::read
std::vector< uint8_t > read(uint32_t timeout)
Definition meteepp.h:216
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)
TeeCancelIO
void TEEAPI TeeCancelIO(IN PTEEHANDLE handle)
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:325
teeDriverVersion_t::major
uint16_t major
Definition metee.h:326
teeDriverVersion_t::build
uint16_t build
Definition metee.h:329
teeDriverVersion_t::minor
uint16_t minor
Definition metee.h:327
teeDriverVersion_t::hotfix
uint16_t hotfix
Definition metee.h:328
tee_device_address
Definition metee.h:125
tee_device_address::TEE_DEVICE_TYPE_NONE
@ TEE_DEVICE_TYPE_NONE
Definition metee.h:129