rust-libteec 0.1.0

Rust bindings and wrapper library for Trusted Execution Environment Client (libteec).
Documentation
// SPDX-License-Identifier: Apache-2.0
// Copyright (C) 2025-2026 KylinSoft Co., Ltd. <https://www.kylinos.cn/>
// See LICENSES for license details.

use mbedtls::{
    Result,
    hash::{Md, Type},
};

// 用于生成 PSK 的固定字符串常量
const KYLINOS: &str = "www.kylinos.cn";
const PKG: &str = "cc-utils"; // 用作 PSK 标识
const TEEC: &str = "libcc_teec";

/// 使用SM3哈希算法生成预共享密钥 (PSK)
///
/// 将 KYLINOS、PKG 和 TEEC 三个字符串按顺序拼接后进行 SM3 哈希运算,
/// 生成 32 字节(256位)的 PSK。
///
/// # 返回
/// * `Ok([u8; 32])` - 成功生成的 32 字节 PSK
/// * `Err(_)` - 哈希运算过程中发生错误
pub fn generate_psk() -> Result<[u8; 32]> {
    let mut psk: [u8; 32] = Default::default();
    let mut ctx = Md::new(Type::SM3)?;

    ctx.update(KYLINOS.as_bytes())?;
    ctx.update(PKG.as_bytes())?;
    ctx.update(TEEC.as_bytes())?;
    let _len = ctx.finish(&mut psk);

    Ok(psk)
}

/// 获取 PSK 标识符
///
/// 返回用于标识 PSK 用途的字符串常量。
/// 在 TLS/DTLS 协议中,客户端使用此标识符告知服务器使用哪个 PSK。
pub const fn get_psk_identity() -> &'static str {
    PKG
}