nearlink_sdr.mac.crypto¶
安全子系统加密模块 -- TXS-10002-2025 标准 9.3 / 6.10.7
实现 SLE 安全子系统的密码学功能:
KDF 密钥派生函数 (AES-CMAC / HMAC-SM3)
安全随机函数 (6.10.7)
CCM Nonce 构建 (9.3.1.3)
AES-CCM 加解密 (9.3.1.3)
初始化向量计算
会话密钥派生 (9.3.1.1)
链路密钥与 DH Key 验证码密钥 (9.3.4.4)
确认码生成 (9.3.4.3)
DH Key 验证码 (9.3.4.5)
6位数字比较码 (9.3.4.6)
混淆算法 (9.3.4.7)
组播密钥管理 (9.3.2)
隐私管理 (9.4)
Module Contents¶
Classes¶
鉴权方式 (9.3.4.3) |
|
密钥派生函数类型 |
Functions¶
AES-CCM 解密, 验证失败抛出 InvalidTag。 |
|
AES-CCM 加密, 返回 (密文, MIC)。 |
|
AES-CMAC, 输入 128-bit key, 输出 128-bit MAC。 |
|
异步/同步单播组播链路的 CCM nonce (16 bytes)。 |
|
其他链路的 CCM nonce (16 bytes)。 |
|
计算最终 IV (8 bytes)。 |
|
DH Key 验证码密钥 = KDF(dh_key_low128, "dk" || ...)。 |
|
派生组播会话密钥。 |
|
Kg = KDF(link_key, rand)。 |
|
Link Key = KDF(dh_key_low128, "lk" || Ra || Rb || G_addr || T_addr)。 |
|
派生会话密钥。 |
|
生成确认码 (16 bytes)。 |
|
生成 DH Key 验证码 (16 bytes)。 |
|
GK = KDF(RAND1, RAND2)。 |
|
生成6位数字比较码 (0-999999)。 |
|
生成可解析随机标识 (48 bits)。 |
|
HMAC-SM3, 输出 256-bit 取低 128-bit。 |
|
根据 kdf_type 选择 AES-CMAC 或 HMAC-SM3。 |
|
混淆算法, 返回 16 bytes 混淆码。 |
|
验证可解析随机标识。 |
|
安全随机函数 (6.10.7), 生成256比特 (32字节) 安全序列。 |
API¶
- class nearlink_sdr.mac.crypto.AuthMethod[源代码]¶
Bases:
enum.IntEnum鉴权方式 (9.3.4.3)
Initialization
Initialize self. See help(type(self)) for accurate signature.
- NUMERIC_COMPARISON¶
0
- PASSKEY_ENTRY¶
1
- PASSWORD_VERIFY¶
2
- OOB¶
3
- PSK¶
4
- NO_INPUT¶
5
- class nearlink_sdr.mac.crypto.KdfType[源代码]¶
Bases:
enum.IntEnum密钥派生函数类型
Initialization
Initialize self. See help(type(self)) for accurate signature.
- AES_CMAC¶
0
- HMAC_SM3¶
1
- nearlink_sdr.mac.crypto.aes_ccm_decrypt(key: bytes, nonce: bytes, ciphertext: bytes, mic: bytes, associated_data: bytes = b'', mic_len: int = 4) bytes[源代码]¶
AES-CCM 解密, 验证失败抛出 InvalidTag。
- nearlink_sdr.mac.crypto.aes_ccm_encrypt(key: bytes, nonce: bytes, plaintext: bytes, associated_data: bytes = b'', mic_len: int = 4) tuple[bytes, bytes][源代码]¶
AES-CCM 加密, 返回 (密文, MIC)。
- nearlink_sdr.mac.crypto.aes_cmac(key: bytes, msg: bytes) bytes[源代码]¶
AES-CMAC, 输入 128-bit key, 输出 128-bit MAC。
- nearlink_sdr.mac.crypto.build_ccm_nonce_async(payload_count: int, direction: int, iv_base: bytes, data_length: int, flag: int = 73) bytes[源代码]¶
异步/同步单播组播链路的 CCM nonce (16 bytes)。
布局: flag(1B) | payload_count[38:0] (39b) | direction (1b) | IV[63:0] (64b) | data_len_hi(1B) | data_len_lo(1B)
- nearlink_sdr.mac.crypto.build_ccm_nonce_other(system_slot_seq: int, day_count: int, iv_base: bytes, data_length: int, flag: int = 73) bytes[源代码]¶
其他链路的 CCM nonce (16 bytes)。
布局: flag(1B) | system_slot_seq[29:0] (30b) | day_count[9:0] (10b) | IV[63:0] (64b) | data_len_hi(1B) | data_len_lo(1B)
- nearlink_sdr.mac.crypto.compute_iv(iv_base: bytes, sync_or_link_id: int, frame_type: int) bytes[源代码]¶
计算最终 IV (8 bytes)。
FT1/FT2: iv_base 低32位 XOR sync_seq 低32位 FT3/FT4: iv_base 低24位 XOR link_id 24位
- nearlink_sdr.mac.crypto.derive_dh_verify_key(kdf_type: nearlink_sdr.mac.crypto.KdfType, dh_key: bytes, ra: bytes, rb: bytes, g_addr: bytes, t_addr: bytes) bytes[源代码]¶
DH Key 验证码密钥 = KDF(dh_key_low128, "dk" || ...)。
- nearlink_sdr.mac.crypto.derive_group_session_key(kdf_type: nearlink_sdr.mac.crypto.KdfType, gk: bytes, use_authenticated: bool = True) tuple[bytes, bytes | None][源代码]¶
派生组播会话密钥。
认证加密: GSK = KDF(GK, "gsk"), 返回 (GSK, None) 分离算法: GEnK / GInK 分别派生, 返回 (GEnK, GInK)
- nearlink_sdr.mac.crypto.derive_kg(kdf_type: nearlink_sdr.mac.crypto.KdfType, link_key: bytes, rand: bytes) bytes[源代码]¶
Kg = KDF(link_key, rand)。
- nearlink_sdr.mac.crypto.derive_link_key(kdf_type: nearlink_sdr.mac.crypto.KdfType, dh_key: bytes, ra: bytes, rb: bytes, g_addr: bytes, t_addr: bytes) bytes[源代码]¶
Link Key = KDF(dh_key_low128, "lk" || Ra || Rb || G_addr || T_addr)。
- nearlink_sdr.mac.crypto.derive_session_key(kdf_type: nearlink_sdr.mac.crypto.KdfType, link_key: bytes, g_diversifier: bytes, t_diversifier: bytes, use_authenticated: bool = True) tuple[bytes, bytes | None][源代码]¶
派生会话密钥。
认证加密: SK = KDF(link_key, g_div || t_div), 返回 (SK, None) 分离算法: EnK / InK 分别派生, 返回 (EnK, InK)
- nearlink_sdr.mac.crypto.generate_confirm_code(kdf_type: nearlink_sdr.mac.crypto.KdfType, auth_method: nearlink_sdr.mac.crypto.AuthMethod, random_value: bytes, g_pubkey: bytes, t_pubkey: bytes, obfuscated: bytes = b'', is_g_node: bool = True) bytes[源代码]¶
生成确认码 (16 bytes)。
random_value: Ra (G节点) 或 Rb (T节点), PSK鉴权时为 PSK obfuscated: 通行码/口令鉴权的混淆码, PSK鉴权的对端随机数
- nearlink_sdr.mac.crypto.generate_dh_verify_code(kdf_type: nearlink_sdr.mac.crypto.KdfType, verify_key: bytes, random_value: bytes, salt: bytes, g_io_cap: int, t_io_cap: int, auth_method: int, crypto_alg: int, g_psk_ind: int, t_psk_ind: int, g_addr: bytes, t_addr: bytes) bytes[源代码]¶
生成 DH Key 验证码 (16 bytes)。
- nearlink_sdr.mac.crypto.generate_group_key(kdf_type: nearlink_sdr.mac.crypto.KdfType, rand1: bytes, rand2: bytes) bytes[源代码]¶
GK = KDF(RAND1, RAND2)。
- nearlink_sdr.mac.crypto.generate_numeric_code(kdf_type: nearlink_sdr.mac.crypto.KdfType, g_pubkey_x: bytes, t_pubkey: bytes, ra: bytes, rb: bytes) int[源代码]¶
生成6位数字比较码 (0-999999)。
K = G节点公钥X 低128比特 (g_pubkey_x 后16字节) M = G节点公钥X 高128比特 || T节点公钥 || Ra || Rb
- nearlink_sdr.mac.crypto.generate_resolvable_address(kdf_type: nearlink_sdr.mac.crypto.KdfType, irk: bytes, rand_part: int) int[源代码]¶
生成可解析随机标识 (48 bits)。
hash_part = KDF(IRK, rand_part) mod 2^16
- 返回:
高16位标记 || 中16位随机 || 低16位hash
- nearlink_sdr.mac.crypto.hmac_sm3(key: bytes, msg: bytes) bytes[源代码]¶
HMAC-SM3, 输出 256-bit 取低 128-bit。
- nearlink_sdr.mac.crypto.kdf(kdf_type: nearlink_sdr.mac.crypto.KdfType, key: bytes, msg: bytes) bytes[源代码]¶
根据 kdf_type 选择 AES-CMAC 或 HMAC-SM3。
- nearlink_sdr.mac.crypto.obfuscate(kdf_type: nearlink_sdr.mac.crypto.KdfType, g_pubkey_x: bytes, value: bytes) bytes[源代码]¶
混淆算法, 返回 16 bytes 混淆码。
K = G节点公钥X 低128比特 (g_pubkey_x 后16字节) 输出 = KDF(K, value)
- nearlink_sdr.mac.crypto.resolve_address(kdf_type: nearlink_sdr.mac.crypto.KdfType, irk: bytes, resolvable_addr: int) bool[源代码]¶
验证可解析随机标识。
- nearlink_sdr.mac.crypto.secure_random_256(seed: bytes, time_param: int, kdf_type: nearlink_sdr.mac.crypto.KdfType = KdfType.AES_CMAC) bytes[源代码]¶
安全随机函数 (6.10.7), 生成256比特 (32字节) 安全序列。
对 KDF 调用两次 (分别使用 time_param 和 time_param+1 作为消息), 拼接两个 128-bit 输出得到 256-bit 安全序列。
- 参数:
seed -- 128比特安全随机种子 (16字节)
time_param -- 32比特时间参数 (如调度时隙号*2)
kdf_type -- KDF 类型
- 返回:
32 字节安全序列