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()
89 {
90 TEESTATUS status = TeeInit(&_handle, &METEE_GUID_ZERO, NULL);
91 if (!TEE_IS_SUCCESS(status)) {
92 throw metee_exception("Init failed", status);
93 }
94 }
95
101 metee(const GUID &guid,
102 uint32_t log_level = TEE_LOG_LEVEL_VERBOSE, TeeLogCallback log_callback = nullptr)
103 {
104 struct tee_device_address device = { tee_device_address::TEE_DEVICE_TYPE_NONE , nullptr };
105 TEESTATUS status = TeeInitFull(&_handle, &guid, device, log_level, log_callback);
106 if (!TEE_IS_SUCCESS(status)) {
107 throw metee_exception("Init failed", status);
108 }
109 }
110
117 metee(const GUID &guid, const struct tee_device_address &device,
118 uint32_t log_level = TEE_LOG_LEVEL_VERBOSE, TeeLogCallback log_callback = nullptr)
119 {
120 TEESTATUS status = TeeInitFull(&_handle, &guid, device, log_level, log_callback);
121 if (!TEE_IS_SUCCESS(status)) {
122 throw metee_exception("Init failed", status);
123 }
124 }
125
127 metee(const metee& other) = delete;
128
132 metee(metee&& other) noexcept : _handle(other._handle)
133 {
134 other._handle.handle = nullptr;
135 }
136
138 metee& operator=(const metee& other) = delete;
139
143 metee& operator=(metee&& other) noexcept
144 {
145 TeeDisconnect(&_handle);
146 _handle = other._handle;
147 other._handle.handle = nullptr;
148 return *this;
149 }
150
152 virtual ~metee()
153 {
154 TeeDisconnect(&_handle);
155 }
156
158 void connect()
159 {
160 TEESTATUS status;
161
162 status = TeeConnect(&_handle);
163 if (!TEE_IS_SUCCESS(status)) {
164 throw metee_exception("Connect failed", status);
165 }
166 }
167
172 std::vector<uint8_t> read(uint32_t timeout)
173 {
174 TEESTATUS status;
175 size_t size = 0;
176 std::vector<uint8_t> buffer(max_msg_len());
177
178 status = TeeRead(&_handle, buffer.data(), buffer.size(), &size, timeout);
179 if (!TEE_IS_SUCCESS(status)) {
180 throw metee_exception("Read failed", status);
181 }
182
183 buffer.resize(size);
184 return std::move(buffer);
185 }
186
192 size_t write(const std::vector<uint8_t> &buffer, uint32_t timeout)
193 {
194 TEESTATUS status;
195 size_t size = 0;
196
197 status = TeeWrite(&_handle, buffer.data(), buffer.size(), &size, timeout);
198 if (!TEE_IS_SUCCESS(status)) {
199 throw metee_exception("Write failed", status);
200 }
201
202 return size;
203 }
204
209 uint32_t fw_status(uint32_t fwStatusNum)
210 {
211 TEESTATUS status;
212 uint32_t fwStatus = 0;
213
214 status = TeeFWStatus(&_handle, fwStatusNum, &fwStatus);
215 if (!TEE_IS_SUCCESS(status)) {
216 throw metee_exception("FWStatus failed", status);
217 }
218
219 return fwStatus;
220 }
221
225 uint32_t trc()
226 {
227 TEESTATUS status;
228 uint32_t trc_val = 0;
229
230 status = TeeGetTRC(&_handle, &trc_val);
231 if (!TEE_IS_SUCCESS(status)) {
232 throw metee_exception("GetTRC failed", status);
233 }
234
235 return trc_val;
236 }
237
241 std::string kind()
242 {
243 TEESTATUS status;
244 const size_t KIND_SIZE = 32;
245 char kind[KIND_SIZE];
246 size_t kind_size = KIND_SIZE;
247
248 status = TeeGetKind(&_handle, kind, &kind_size);
249 if (!TEE_IS_SUCCESS(status)) {
250 throw metee_exception("TeeGetKind failed", status);
251 }
252
253 return kind;
254 }
255
260 uint32_t log_level(uint32_t log_level)
261 {
262 return TeeSetLogLevel(&_handle, log_level);
263 }
264
269 uint32_t log_level()
270 {
271 return TeeGetLogLevel(&_handle);
272 }
273
278 void log_callback(TeeLogCallback log_callback)
279 {
280 TeeSetLogCallback(&_handle, log_callback);
281 }
282
287 uint32_t max_msg_len()
288 {
289 return TeeGetMaxMsgLen(&_handle);
290 }
291
296 uint8_t protocol_ver()
297 {
298 return TeeGetProtocolVer(&_handle);
299 }
300
305 TEE_DEVICE_HANDLE device_handle()
306 {
307 return TeeGetDeviceHandle(&_handle);
308 }
309
314 std::string driver_version()
315 {
316 TEESTATUS status;
317 teeDriverVersion_t driverVersion = { 0 };
318
319 status = ::GetDriverVersion(&_handle, &driverVersion);
320 if (!TEE_IS_SUCCESS(status)) {
321 throw metee_exception("GetDriverVersion failed", status);
322 }
323
324 std::stringstream ss;
325 ss << driverVersion.major << "."
326 << driverVersion.minor << "."
327 << driverVersion.hotfix << "."
328 << driverVersion.build;
329 return ss.str();
330 }
331
332 private:
333 _TEEHANDLE _handle;
334 };
335 } // namespace security
336} // namespace intel
337#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(const GUID &guid, const struct tee_device_address &device, uint32_t log_level=TEE_LOG_LEVEL_VERBOSE, TeeLogCallback log_callback=nullptr)
Definition meteepp.h:117
intel::security::metee::metee
metee()
Definition meteepp.h:88
intel::security::metee::metee
metee(const GUID &guid, uint32_t log_level=TEE_LOG_LEVEL_VERBOSE, TeeLogCallback log_callback=nullptr)
Definition meteepp.h:101
intel::security::metee::trc
uint32_t trc()
Definition meteepp.h:225
intel::security::metee::max_msg_len
uint32_t max_msg_len()
Definition meteepp.h:287
intel::security::metee::log_callback
void log_callback(TeeLogCallback log_callback)
Definition meteepp.h:278
intel::security::metee::metee
metee(metee &&other) noexcept
Definition meteepp.h:132
intel::security::metee::protocol_ver
uint8_t protocol_ver()
Definition meteepp.h:296
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:192
intel::security::metee::kind
std::string kind()
Definition meteepp.h:241
intel::security::metee::log_level
uint32_t log_level()
Definition meteepp.h:269
intel::security::metee::device_handle
TEE_DEVICE_HANDLE device_handle()
Definition meteepp.h:305
intel::security::metee::fw_status
uint32_t fw_status(uint32_t fwStatusNum)
Definition meteepp.h:209
intel::security::metee::connect
void connect()
Definition meteepp.h:158
intel::security::metee::operator=
metee & operator=(metee &&other) noexcept
Definition meteepp.h:143
intel::security::metee::operator=
metee & operator=(const metee &other)=delete
intel::security::metee::~metee
virtual ~metee()
Definition meteepp.h:152
intel::security::metee::driver_version
std::string driver_version()
Definition meteepp.h:314
intel::security::metee::log_level
uint32_t log_level(uint32_t log_level)
Definition meteepp.h:260
intel::security::metee::read
std::vector< uint8_t > read(uint32_t timeout)
Definition meteepp.h:172
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
TeeSetLogCallback
TEESTATUS TEEAPI TeeSetLogCallback(IN const PTEEHANDLE handle, TeeLogCallback log_callback)
TEE_LOG_LEVEL_VERBOSE
@ TEE_LOG_LEVEL_VERBOSE
Definition metee.h:89
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)
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