Low-Level TKey Protocol¶
This page documents the lower-level protocol, framing classes, and exception hierarchy of the keylet library. These are useful if you want to implement custom host applications or interact with the TKey firmware directly.
Base TKey Client¶
keylet.tkey.TKey
¶
Base TKey Client
TKey is used to build host (client) applications for a Tillitis TKey. It implements the Firmware protocol and provides serial IO as well as some helpers for the actual application implementation.
Links:
Source code in src/keylet/tkey.py
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 | |
Methods:¶
__init__(device)
¶
Initialize serial connection to TKey device.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
str | None
|
Optional serial port path (e.g., |
required |
Raises:
| Type | Description |
|---|---|
TKeyNotFoundError
|
If no TKey device is found. |
TKeyError
|
If the serial connection fails to open. |
Source code in src/keylet/tkey.py
load_app(app_binary, secret=None)
¶
Load an application binary into the TKey device.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app_binary
|
bytes
|
The raw bytes of the application binary. |
required |
secret
|
str | None
|
Optional User Supplied Secret (passphrase) to configure the app with. |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True if the application was successfully loaded, False if the device was not in firmware mode (i.e. already running an application). |
Raises:
| Type | Description |
|---|---|
TKeyAppError
|
If the binary is too large, the device is not ready, or the loaded app's digest does not match the local digest. |
TKeyError
|
If the device is running an unknown firmware or if loading data fails. |
Source code in src/keylet/tkey.py
recv_response(cmd)
¶
Receive and return a response frame.
recv_response() can only be called if there are unread frames from a previous
send() call for the given command.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cmd
|
Cmd
|
The command that this response is for. |
required |
Returns:
| Type | Description |
|---|---|
bytes
|
The response frame as bytes. |
Raises:
| Type | Description |
|---|---|
TKeyError
|
If the TKey is not connected. |
TKeyIOError
|
If reading the response from the serial port fails or returns no data. |
TKeyProtocolError
|
If the response status is not OK, the response length is invalid, the frame ID/endpoint mismatch, or the response ID is unexpected. |
Source code in src/keylet/tkey.py
send(cmd, data=b'', timeout=-1)
¶
Frame and send a command, then read the first response frame.
Caller is expected to call recv_response() if the response is longer than one frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cmd
|
Cmd
|
The command to send. |
required |
data
|
bytes
|
Optional payload data for the command. |
b''
|
timeout
|
int
|
Optional serial timeout in seconds. If -1, the default connection timeout is used. |
-1
|
Returns:
| Type | Description |
|---|---|
bytes
|
The first response frame as bytes. |
Raises:
| Type | Description |
|---|---|
TKeyError
|
If the TKey is not connected. |
TKeyProtocolError
|
If the data exceeds the command's maximum length. |
TKeyIOError
|
If writing the frame fails. |
Source code in src/keylet/tkey.py
Protocol Structures¶
keylet.tkey.Cmd
dataclass
¶
Protocol command definition.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
int
|
The command identifier byte. |
endpoint |
int
|
The target endpoint on the device. |
len_idx |
int
|
The length index indicating the payload data size. |
valid_responses |
tuple[Rsp, ...]
|
A tuple of acceptable responses for this command. |
Source code in src/keylet/tkey.py
keylet.tkey.Rsp
dataclass
¶
Protocol response definition.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
int
|
The response identifier byte. |
len_idx |
int
|
The length index indicating the expected data size. |
Source code in src/keylet/tkey.py
keylet.tkey.LenIdx
¶
Source code in src/keylet/tkey.py
Attributes¶
I1 = 0
class-attribute
instance-attribute
¶
1-byte payload length index.
I128 = 3
class-attribute
instance-attribute
¶
128-byte payload length index.
I32 = 2
class-attribute
instance-attribute
¶
32-byte payload length index.
I4 = 1
class-attribute
instance-attribute
¶
4-byte payload length index.
Firmware Commands and Responses¶
keylet.tkey.FwCmd
¶
Firmware commands
Source code in src/keylet/tkey.py
Attributes¶
LOAD_APP = Cmd(3, 2, LenIdx.I128, (FwRsp.LOAD_APP,))
class-attribute
instance-attribute
¶
Command to initiate application loading with size and optional secret.
LOAD_APP_DATA = Cmd(5, 2, LenIdx.I128, (FwRsp.LOAD_APP_DATA, FwRsp.LOAD_APP_DATA_READY))
class-attribute
instance-attribute
¶
Command to send a chunk of application binary data.
NAME_VERSION = Cmd(1, 2, LenIdx.I1, (FwRsp.NAME_VERSION,))
class-attribute
instance-attribute
¶
Command to query the firmware name and version.
keylet.tkey.FwRsp
¶
Firmware responses
Source code in src/keylet/tkey.py
Attributes¶
LOAD_APP = Rsp(4, LenIdx.I4)
class-attribute
instance-attribute
¶
Response indicating the application loading status.
LOAD_APP_DATA = Rsp(6, LenIdx.I4)
class-attribute
instance-attribute
¶
Response indicating the application data chunk status.
LOAD_APP_DATA_READY = Rsp(7, LenIdx.I128)
class-attribute
instance-attribute
¶
Response indicating all application data has been received and verified.
NAME_VERSION = Rsp(2, LenIdx.I32)
class-attribute
instance-attribute
¶
Response containing firmware name and version.