Coverage for tropicsquare / ecc / __init__.py: 100%

14 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-03-27 21:24 +0000

1"""ECC operations module for TROPIC01 secure element. 

2 

3This module provides structured classes for ECC key information 

4and signature objects, replacing the tuple-based API. 

5 

6Main exports: 

7 - EccKeyInfo: Information about keys in secure slots 

8 - EcdsaSignature: ECDSA signature from P256 curve 

9 - EddsaSignature: EdDSA signature from Ed25519 curve 

10 

11Example:: 

12 

13 from tropicsquare import TropicSquare 

14 from tropicsquare.ecc import EccKeyInfo, EcdsaSignature 

15 from tropicsquare.constants.ecc import ECC_CURVE_ED25519 

16 

17 ts = TropicSquare(transport) 

18 ts.start_secure_session(...) 

19 

20 # Read key information 

21 key_info = ts.ecc_key_read(0) 

22 if key_info.curve == ECC_CURVE_ED25519: 

23 print("Ed25519 key") 

24 print(key_info.public_key.hex()) 

25 

26 # Create signature 

27 signature = ts.ecdsa_sign(1, message_hash) 

28 print(signature.r.hex()) 

29""" 

30 

31"""ECC key information data structure.""" 

32 

33from tropicsquare.constants.ecc import ECC_CURVE_P256, ECC_CURVE_ED25519 

34 

35 

36class EccKeyInfo: 

37 """ECC key information from secure key slot. 

38 

39 Represents the public key and metadata stored in a TROPIC01 ECC key slot, 

40 as returned by ecc_key_read(). 

41 

42 :param curve: Curve type (ECC_CURVE_P256 or ECC_CURVE_ED25519) 

43 :param origin: Key origin (ECC_KEY_ORIGIN_GENERATED or ECC_KEY_ORIGIN_STORED) 

44 :param public_key: Public key bytes (32 or 64 bytes depending on curve) 

45 

46 Example:: 

47 

48 key_info = ts.ecc_key_read(0) 

49 if key_info.curve == ECC_CURVE_ED25519: 

50 print("Ed25519 key") 

51 print(key_info.public_key.hex()) 

52 """ 

53 

54 def __init__(self, curve: int, origin: int, public_key: bytes): 

55 """Initialize ECC key information. 

56 

57 :param curve: Curve type constant 

58 :param origin: Key origin constant 

59 :param public_key: Public key bytes 

60 """ 

61 self.curve = curve 

62 self.origin = origin 

63 self.public_key = public_key 

64 

65 def to_dict(self) -> dict: 

66 """Convert key information to dictionary. 

67 

68 :returns: Dictionary with curve, origin, and public_key fields 

69 :rtype: dict 

70 

71 Example:: 

72 

73 { 

74 'curve': 1, 

75 'origin': 0, 

76 'public_key': '04a1b2c3...' 

77 } 

78 """ 

79 return { 

80 'curve': self.curve, 

81 'origin': self.origin, 

82 'public_key': self.public_key.hex() 

83 } 

84 

85 def __str__(self) -> str: 

86 """Get human-readable string representation. 

87 

88 :returns: Formatted string with key information 

89 """ 

90 curve_name = "P256" if self.curve == ECC_CURVE_P256 else "Ed25519" if self.curve == ECC_CURVE_ED25519 else f"Unknown (0x{self.curve:02x})" 

91 return f"ECC Key: {curve_name}, Origin: {self.origin}, PubKey: {self.public_key.hex()[:32]}..." 

92 

93 def __repr__(self) -> str: 

94 """Get detailed string representation for debugging. 

95 

96 :returns: Detailed representation with class name and fields 

97 """ 

98 return f"EccKeyInfo(curve=0x{self.curve:02x}, origin=0x{self.origin:02x}, pubkey={self.public_key.hex()[:16]}...)"