TropicSquare Core Class
The TropicSquare class is the main interface for communicating with the TROPIC01 secure element chip.
It implements the L2 and L3 protocol layers and provides high-level methods for all chip operations.
It serves as the common base class for the platform-specific implementations.
- class tropicsquare.TropicSquare(*args, **kwargs)[source]
Bases:
object- static __new__(cls, *args, **kwargs)[source]
Factory method that returns platform-specific implementation.
When instantiating TropicSquare directly, automatically returns either TropicSquareCPython or TropicSquareMicroPython based on the detected platform.
- This allows users to write platform-agnostic code:
from tropicsquare import TropicSquare ts = TropicSquare(transport)
- __init__(transport)[source]
Initialize TropicSquare base class.
- Parameters:
transport (L1Transport) – L1Transport instance
- Return type:
None
- property certificate: bytes
Get X509 certificate from the chip
- Returns:
X509 certificate
- Return type:
- property public_key: bytes
Get public key from the X509 certificate
In case certificate is not loaded before, it will load also certificate
- returns:
Public key
- rtype:
bytes
- property chip_id: ChipId
Get parsed chip ID structure
- Returns:
Parsed chip ID object with all fields
- Return type:
- property riscv_fw_version: tuple
Get RISCV firmware version
- Returns:
Firmware version (major, minor, patch, release)
- Return type:
- property spect_fw_version: tuple
Get SPECT firmware version
- Returns:
Firmware version (major, minor, patch, release)
- Return type:
- start_secure_session(pkey_index, shpriv, shpub)[source]
Initialize secure session for L3 commands
- Parameters:
- Returns:
True if secure session was established
- Return type:
- Raises:
TropicSquareError – If secure session handshake failed
- abort_secure_session()[source]
Abort secure session
- Returns:
True if secure session was aborted
- Return type:
- reboot(mode)[source]
Startup/reboot chip
- Parameters:
mode (int) – Startup mode (STARTUP_REBOOT or STARTUP_MAINTENANCE_REBOOT)
- Returns:
True if startup request was sent
- Return type:
- Raises:
ValueError – If invalid startup mode
TropicSquareError – If startup request failed
- sleep(mode)[source]
Put chip to sleep
- Parameters:
mode (int) – Sleep mode (SLEEP_MODE_SLEEP or SLEEP_MODE_DEEP_SLEEP)
- Returns:
True if sleep request was sent
- Return type:
- Raises:
ValueError – If invalid sleep mode
TropicSquareError – If sleep request failed
- r_config_read(address)[source]
Read and parse R-CONFIG register.
- Parameters:
address (int) – Register address (use CFG_* constants from tropicsquare.constants.config)
- Returns:
Parsed config object (StartUpConfig, SensorsConfig, etc.)
- Return type:
Example:
from tropicsquare.constants.config import CFG_START_UP config = ts.r_config_read(CFG_START_UP) print(config.mbist_dis)
- i_config_read(address)[source]
Read and parse I-CONFIG register.
- Parameters:
address (int) – Register address (use CFG_* constants from tropicsquare.constants.config)
- Returns:
Parsed config object (StartUpConfig, SensorsConfig, etc.)
- Return type:
Example:
from tropicsquare.constants.config import CFG_START_UP config = ts.i_config_read(CFG_START_UP) print(config.mbist_dis)
- r_config_erase()[source]
Erase whole R-CONFIG (sets all bits of all COs to 1).
- Returns:
True if erase succeeded
- Return type:
- mem_data_write(data, slot)[source]
Write data to memory slot
- Parameters:
- Returns:
True if data was written
- Return type:
- Raises:
ValueError – If data size is larger than 444
- ecc_key_generate(slot, curve)[source]
Generate ECC key
- Parameters:
- Returns:
True if key was generated
- Return type:
- Raises:
ValueError – If slot is larger than ECC_MAX_KEYS or curve is invalid
- ecc_key_store(slot, curve, key)[source]
Store own ECC key
- Parameters:
- Returns:
True if key was stored
- Return type:
- Raises:
ValueError – If slot is larger than ECC_MAX_KEYS or curve is invalid
- ecc_key_read(slot)[source]
Read ECC key information from slot
- Parameters:
slot (int) – Slot for key
- Returns:
Key information with curve, origin, and public_key
- Return type:
- Raises:
ValueError – If slot is larger than ECC_MAX_KEYS
Example:
key_info = ts.ecc_key_read(0) if key_info.curve == ECC_CURVE_ED25519: print("Ed25519 key") print(key_info.public_key.hex())
- ecc_key_erase(slot)[source]
Erase ECC key
- Parameters:
slot (int) – Slot for key
- Returns:
True if key was erased
- Return type:
- Raises:
ValueError – If slot is larger than ECC_MAX_KEYS
- ecdsa_sign(slot, hash)[source]
Sign hash with ECDSA using P256 key
- Parameters:
- Returns:
ECDSA signature
- Return type:
- Raises:
ValueError – If slot is larger than ECC_MAX_KEYS
Example:
import hashlib message_hash = hashlib.sha256(b"Hello").digest() signature = ts.ecdsa_sign(1, message_hash) print(signature.r.hex()) print(signature.s.hex())
- eddsa_sign(slot, message)[source]
Sign message with EdDSA using Ed25519 key
- Parameters:
- Returns:
EdDSA signature
- Return type:
Example:
signature = ts.eddsa_sign(0, message) print(signature.r.hex()) print(signature.s.hex())
- mac_and_destroy(slot, data)[source]
MAC and destroy operation for atomic PIN verification.
This command executes atomic PIN verification using Keccak-based MAC. The operation reads a slot from the MAC-and-Destroy partition (128 slots, 0-127), performs MAC calculation, and destroys/erases the slot data.
The MAC-and-Destroy partition is separate from User Data partition and uses Keccak engines with PUF-based per-chip unique keys (K_FXA, K_FXB).
- Parameters:
- Returns:
MAC result (32 bytes)
- Raises:
ValueError – If slot exceeds maximum (127) or data length is not 32 bytes
TropicSquareNoSession – If secure session is not established
- Return type:
Note
Requires active secure session via
start_secure_session().See also
TROPIC01 User API v1.1.2, Table 37: MAC_And_Destroy command specification
Example:
# Start secure session first ts.start_secure_session( FACTORY_PAIRING_KEY_INDEX, FACTORY_PAIRING_PRIVATE_KEY_PROD0, FACTORY_PAIRING_PUBLIC_KEY_PROD0 ) # Perform MAC and destroy on slot 0 pin_data = b'my_32_byte_pin_data_here_000' # Exactly 32 bytes mac_result = ts.mac_and_destroy(0, pin_data) print(f"MAC: {mac_result.hex()}") # Returns 32-byte MAC
- pairing_key_read(slot)[source]
Read pairing key information from slot.
- Parameters:
slot (int) – Pairing key slot index (0-3)
- Returns:
Pairing key information (32 bytes)
- Return type:
- Raises:
ValueError – If slot exceeds maximum (3)
- pairing_key_write(slot, key)[source]
Write pairing key information to slot.
- Parameters:
- Returns:
True if write succeeded
- Return type:
- Raises:
ValueError – If slot exceeds maximum (3) or key length is not 32 bytes
- pairing_key_invalidate(slot)[source]
Invalidate pairing key in slot.
- Parameters:
slot (int) – Pairing key slot index (0-3)
- Returns:
True if successful
- Return type:
- Raises:
ValueError – If slot exceeds maximum (3)
Platform-Specific Implementations
For actual usage, you should use one of the platform-specific implementations:
TropicSquareCPython- For CPython (development, desktop)TropicSquareMicroPython- For MicroPython (ESP32, embedded)
See Also
L2 Protocol Layer - L2 protocol implementation details
Platform Ports - Platform-specific implementations
Transport Layer - Available transport layers