Skip to main content

rust_utee/api/
tee_api_arith_mpi.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (C) 2025 KylinSoft Co., Ltd. <https://www.kylinos.cn/>
3// See LICENSES for license details.
4//
5// This file has been modified by KylinSoft on 2025.
6
7// TEE Internal Core API Specification – Public Release v1.3.1
8// 8 TEE Arithmetical API
9
10use crate::tee_api_defines::{TEE_ERROR_OVERFLOW, TEE_SUCCESS};
11use crate::tee_api_types::{TEE_BigInt, TEE_BigIntFMM, TEE_BigIntFMMContext, TEE_Result};
12use mbedtls::bignum::Mpi;
13use mbedtls::error::Error;
14use mbedtls::rng::RngCallback;
15pub use mbedtls_sys_auto::mpi_sint;
16use std::ops::ShrAssign;
17
18// 为了访问底层函数,我们需要导入正确的模块
19use mbedtls_sys_auto as mbedtls_sys;
20
21#[repr(C)]
22struct BigintHdr {
23    pub sign: i32,       // 对应 int32_t
24    pub alloc_size: u16, // 对应 uint16_t
25    pub nblimbs: u16,    // 对应 uint16_t
26}
27
28pub const BIGINT_HDR_SIZE_IN_U32: usize = 2;
29
30// 示例定义(您需要根据实际情况调整这些值)
31const CFG_TA_BIGNUM_MAX_BITS: usize = 4096;
32//const MBEDTLS_MPI_MAX_LIMBS: usize = 128;
33
34/// TEE BigInt 扩展 trait
35pub trait TeeBigIntExt {
36    /// 将 MPI 转换为 TEE_BigInt
37    unsafe fn to_teebigint(&self, bigint: *mut TEE_BigInt, alloc_size: usize) -> Result<(), Error>;
38
39    /// 从 TEE_BigInt 创建 MPI
40    unsafe fn from_teebigint(bigint: *const TEE_BigInt) -> Result<Self, Error>
41    where
42        Self: Sized;
43}
44
45/// 为 Mpi 实现 TEE BigInt 扩展 trait
46impl TeeBigIntExt for Mpi {
47    /// 将 Mpi 对象复制到 TEE_BigInt 结构中
48    ///
49    /// 注意:mbedtls 使用 64-bit limbs,而 TEE_BigInt 使用 32-bit limbs
50    unsafe fn to_teebigint(&self, bigint: *mut TEE_BigInt, alloc_size: usize) -> Result<(), Error> {
51        // 检查指针有效性
52        if bigint.is_null() {
53            return Err(mbedtls::error::codes::MpiBadInputData.into());
54        }
55
56        // 计算 MPI 中有效 limbs 的数量(去除尾部的零值)
57        let mpi_limbs_count = {
58            let handle: *const mbedtls_sys::mpi = self.into();
59            unsafe {
60                let mut limbs_count = (*handle).n;
61                // 去除尾部的零值 limbs
62                while limbs_count > 0 && self.get_limb(limbs_count - 1) == 0 {
63                    limbs_count -= 1;
64                }
65                limbs_count
66            }
67        };
68
69        // 计算 TEE_BigInt 需要的 32-bit limbs 数量
70        // 每个 64-bit limb 都需要 2 个 32-bit limbs
71        let mut tee_limbs_count = 0;
72        for i in 0..mpi_limbs_count {
73            let limb = self.get_limb(i);
74            if i == mpi_limbs_count - 1 {
75                // 最高位的 limb
76                if limb == 0 {
77                    continue; // 跳过零 limb
78                } else if limb <= 0xFFFFFFFF {
79                    // 如果高32位是0,只需要1个32-bit limb
80                    tee_limbs_count += 1;
81                } else {
82                    // 需要2个32-bit limbs
83                    tee_limbs_count += 2;
84                }
85            } else {
86                // 非最高位的 limb,总是需要 2 个 32-bit limbs
87                tee_limbs_count += 2;
88            }
89        }
90
91        // 检查目标缓冲区是否足够大
92        if alloc_size < tee_limbs_count {
93            return Err(mbedtls::error::codes::MpiBufferTooSmall.into());
94        }
95
96        // 设置头部信息和复制数据
97        unsafe {
98            // 将指针转换为头部结构
99            let header = bigint as *mut BigintHdr;
100
101            // 设置头部信息
102            let handle: *const mbedtls_sys::mpi = self.into();
103            (*header).sign = (*handle).s; // 符号位
104            (*header).alloc_size = alloc_size as u16; // 分配大小
105            (*header).nblimbs = tee_limbs_count as u16; // limbs 数量
106
107            // 复制数据,将 64-bit limbs 转换为 32-bit limbs
108            let mut tee_index = 0;
109            for i in 0..mpi_limbs_count {
110                let limb = self.get_limb(i);
111
112                // 存储低 32 位
113                let low_ptr = bigint.add(2 + tee_index) as *mut u32;
114                *low_ptr = (limb & 0xFFFFFFFF) as u32;
115                tee_index += 1;
116
117                // 如果需要,存储高 32 位
118                if i < mpi_limbs_count - 1 || limb > 0xFFFFFFFF {
119                    let high_ptr = bigint.add(2 + tee_index) as *mut u32;
120                    *high_ptr = ((limb >> 32) & 0xFFFFFFFF) as u32;
121                    tee_index += 1;
122                }
123            }
124        }
125
126        Ok(())
127    }
128
129    /// 从 TEE_BigInt 结构体创建 Mpi 对象
130    ///
131    /// 从 TEE_BigInt 提取数据并初始化 Mpi
132    unsafe fn from_teebigint(bigint: *const TEE_BigInt) -> Result<Self, Error> {
133        // 检查指针有效性
134        if bigint.is_null() {
135            return Mpi::new(0);
136        }
137
138        // 读取头部信息
139        let (sign, nblimbs) = unsafe {
140            let header = bigint as *const BigintHdr;
141            ((*header).sign, (*header).nblimbs as usize)
142        };
143
144        // 如果没有 limbs,返回0
145        if nblimbs == 0 {
146            return Mpi::new(0);
147        }
148
149        // 手动读取数据而不是使用 slice,避免对齐问题
150        // 将 32-bit limbs 转换为 64-bit limbs
151        // 注意:存储顺序是 limb[0]=低32位, limb[1]=高32位, limb[2]=低32位, limb[3]=高32位...
152        let mut data_vec = Vec::with_capacity((nblimbs + 1) / 2);
153        let mut i = 0;
154        while i < nblimbs {
155            let low = unsafe {
156                let low_ptr = bigint.add(2 + i) as *const u32;
157                if low_ptr.is_null() { 0 } else { *low_ptr }
158            };
159
160            if i + 1 < nblimbs {
161                // 有下一个 limb,合并两个 32-bit limbs 成一个 64-bit limb
162                let high = unsafe {
163                    let high_ptr = bigint.add(2 + i + 1) as *const u32;
164                    if high_ptr.is_null() { 0 } else { *high_ptr }
165                };
166
167                // 合并:[high32][low32] -> 64-bit
168                // 根据存储格式:limb[0]=低32位, limb[1]=高32位
169                // 所以组合为:high<<32 | low
170                let combined = ((high as u64) << 32) | (low as u64);
171                data_vec.push(combined as mbedtls_sys::mpi_uint);
172                i += 2;
173            } else {
174                // 最后一个 limb,直接存储
175                data_vec.push(low as mbedtls_sys::mpi_uint);
176                i += 1;
177            }
178        }
179
180        // 去除尾部的零值 limbs
181        let trimmed_data = {
182            let mut len = data_vec.len();
183            while len > 0 && data_vec[len - 1] == 0 {
184                len -= 1;
185            }
186            &data_vec[..len]
187        };
188
189        // 如果没有有效数据,返回0
190        if trimmed_data.is_empty() {
191            return Mpi::new(0);
192        }
193
194        // 创建一个新的 Mpi 实例
195        let mut mpi = Mpi::new(0)?;
196
197        // 设置 MPI 值
198        unsafe {
199            // 增长 MPI 以容纳所需数量的 limbs
200            let handle: *mut mbedtls_sys::mpi = (&mut mpi).into();
201            let result = mbedtls_sys::mpi_grow(handle, trimmed_data.len());
202            if result != 0 {
203                return Err(mbedtls::error::codes::MpiBadInputData.into());
204            }
205
206            // 设置符号
207            (*handle).s = sign;
208
209            // 复制数据,根据目标指针类型进行转换
210            let dst_ptr = (*handle).p;
211            std::ptr::copy_nonoverlapping(trimmed_data.as_ptr(), dst_ptr, trimmed_data.len());
212        }
213
214        Ok(mpi)
215    }
216}
217
218// 为 Mpi 添加 get_limb 方法
219trait MpiExt {
220    fn get_limb(&self, n: usize) -> mbedtls_sys::mpi_uint;
221}
222
223impl MpiExt for Mpi {
224    fn get_limb(&self, n: usize) -> mbedtls_sys::mpi_uint {
225        let handle: *const mbedtls_sys::mpi = self.into();
226        let n_limbs = unsafe { (*handle).n };
227        if n < n_limbs {
228            unsafe { *(*handle).p.offset(n as isize) }
229        } else {
230            // zero pad
231            0
232        }
233    }
234}
235
236/// 初始化一个 TEE_BigInt 对象
237///
238/// 参数:
239/// - big_int: 指向 TEE_BigInt 的指针
240/// - len: 以 u32 为单位的长度
241#[unsafe(no_mangle)]
242pub extern "C" fn TEE_BigIntInit(big_int: *mut TEE_BigInt, len: usize) {
243    // 安全代码:参数检查和计算
244    if len > CFG_TA_BIGNUM_MAX_BITS / 4 {
245        panic!("Too large bigint");
246    }
247    let alloc_size = (len - BIGINT_HDR_SIZE_IN_U32) as u16;
248
249    // 必须的 unsafe:只有真正需要 unsafe 的操作放在这里
250    unsafe {
251        core::ptr::write_bytes(big_int as *mut u8, 0, len * 4);
252        let hdr = big_int as *mut BigintHdr;
253        (*hdr).sign = 1;
254        (*hdr).alloc_size = alloc_size;
255        (*hdr).nblimbs = 0;
256    }
257}
258
259/// 将八进制字符串转换为 TEE_BigInt
260///
261/// 参数:
262/// - dest: 目标 TEE_BigInt 指针
263/// - buffer: 源数据缓冲区指针
264/// - buffer_len: 缓冲区长度
265/// - sign: 符号值
266///
267/// 返回值:
268/// - TEE_Result: 转换结果
269#[unsafe(no_mangle)]
270pub extern "C" fn TEE_BigIntConvertFromOctetString(
271    dest: *mut TEE_BigInt,
272    buffer: *const u8,
273    buffer_len: usize,
274    sign: i32,
275) -> TEE_Result {
276    // 从二进制数据创建 MPI 对象
277    let buffer_slice = unsafe { core::slice::from_raw_parts(buffer, buffer_len) };
278
279    match Mpi::from_binary(buffer_slice) {
280        Ok(mut mpi) => {
281            // 如果符号为负,将 MPI 设置为负数
282            if sign < 0 {
283                match Mpi::new(-1) {
284                    Ok(neg_one) => {
285                        unsafe {
286                            // 使用 mpi_mul_mpi 实现取负操作,通过 Into trait 获取内部引用
287                            let result = mbedtls_sys::mpi_mul_mpi(
288                                (&mut mpi).into(),
289                                (&mpi).into(),
290                                (&neg_one).into(),
291                            );
292                            if result != 0 {
293                                return TEE_ERROR_OVERFLOW;
294                            }
295                        }
296                    }
297                    Err(_) => return TEE_ERROR_OVERFLOW,
298                }
299            }
300
301            // 获取目标缓冲区的分配大小
302            unsafe {
303                let hdr = dest as *mut BigintHdr;
304                let alloc_size = (*hdr).alloc_size as usize;
305
306                // 使用正确的 to_teebigint 方法进行转换
307                match mpi.to_teebigint(dest, alloc_size) {
308                    Ok(()) => TEE_SUCCESS,
309                    Err(_) => TEE_ERROR_OVERFLOW,
310                }
311            }
312        }
313        Err(_) => TEE_ERROR_OVERFLOW,
314    }
315}
316
317/// 将 TEE_BigInt 转换为八进制字符串(字节数组)
318///
319/// 参数:
320/// - buffer: 目标缓冲区指针
321/// - buffer_len: 缓冲区长度的指针(输入时为缓冲区大小,输出时为实际数据大小)
322/// - big_int: 源 TEE_BigInt 指针
323///
324/// 返回值:
325/// - TEE_Result: 转换结果
326#[unsafe(no_mangle)]
327pub extern "C" fn TEE_BigIntConvertToOctetString(
328    buffer: *mut u8,
329    buffer_len: *mut usize,
330    big_int: *const TEE_BigInt,
331) -> TEE_Result {
332    // 检查输入参数
333    if buffer_len.is_null() || big_int.is_null() {
334        return TEE_ERROR_OVERFLOW; // 使用合适的错误码
335    }
336
337    // 从 TEE_BigInt 创建 MPI 对象,使用正确的方法
338    let mpi = match unsafe { Mpi::from_teebigint(big_int) } {
339        Ok(mpi) => mpi,
340        Err(_) => return TEE_ERROR_OVERFLOW,
341    };
342
343    // 获取 MPI 的字节长度
344    let sz = match mpi.byte_length() {
345        Ok(len) => len,
346        Err(_) => return TEE_ERROR_OVERFLOW,
347    };
348
349    // 检查缓冲区大小
350    let provided_buffer_len = unsafe { *buffer_len };
351
352    if sz <= provided_buffer_len {
353        if !buffer.is_null() {
354            // 写入二进制数据
355            match mpi.to_binary() {
356                Ok(binary_data) => {
357                    // 复制数据到目标缓冲区
358                    unsafe {
359                        core::ptr::copy_nonoverlapping(
360                            binary_data.as_ptr(),
361                            buffer,
362                            binary_data.len(),
363                        );
364                    }
365                }
366                Err(_) => return TEE_ERROR_OVERFLOW,
367            }
368        }
369    } else {
370        // 缓冲区太小
371        unsafe { *buffer_len = sz };
372        return TEE_ERROR_OVERFLOW; // 应该使用 TEE_ERROR_SHORT_BUFFER
373    }
374
375    // 更新缓冲区长度
376    unsafe { *buffer_len = sz };
377
378    TEE_SUCCESS
379}
380
381/// 将 32 位有符号整数转换为 TEE_BigInt
382///
383/// 参数:
384/// - dest: 目标 TEE_BigInt 指针
385/// - short_val: 源 32 位有符号整数
386#[unsafe(no_mangle)]
387pub extern "C" fn TEE_BigIntConvertFromS32(dest: *mut TEE_BigInt, short_val: i32) {
388    unsafe {
389        let hdr = dest as *mut BigintHdr;
390
391        // 设置符号
392        if short_val < 0 {
393            (*hdr).sign = -1;
394        } else {
395            (*hdr).sign = 1;
396        }
397
398        // 获取绝对值
399        let abs_val = if short_val < 0 {
400            -(short_val as i64) as u32
401        } else {
402            short_val as u32
403        };
404
405        // 清零数据区域
406        let data_ptr = dest.add(2) as *mut u32;
407        let alloc_size = (*hdr).alloc_size as usize;
408        for i in 0..alloc_size {
409            *data_ptr.add(i) = 0;
410        }
411
412        // 设置值(如果没有空间,则至少设置第一个 limb)
413        if alloc_size > 0 {
414            *data_ptr = abs_val;
415            (*hdr).nblimbs = if abs_val == 0 { 0 } else { 1 };
416        } else {
417            (*hdr).nblimbs = 0;
418        }
419    }
420}
421
422/// 将 TEE_BigInt 转换为 32 位有符号整数
423///
424/// 参数:
425/// - dest: 目标 32 位有符号整数指针
426/// - src: 源 TEE_BigInt 指针
427///
428/// 返回值:
429/// - TEE_Result: 转换结果
430#[unsafe(no_mangle)]
431pub extern "C" fn TEE_BigIntConvertToS32(dest: *mut i32, src: *const TEE_BigInt) -> TEE_Result {
432    // 从 TEE_BigInt 创建 MPI 对象,使用正确的方法
433    let mpi = match unsafe { Mpi::from_teebigint(src) } {
434        Ok(mpi) => mpi,
435        Err(_) => return TEE_ERROR_OVERFLOW,
436    };
437
438    // 使用 Mpi 的 to_binary 方法获取二进制数据
439    match mpi.to_binary() {
440        Ok(binary_data) => {
441            // 检查数据长度是否适合 32 位整数
442            if binary_data.len() > 4 {
443                return TEE_ERROR_OVERFLOW;
444            }
445
446            // 将二进制数据转换为 u32(大端序)
447            let mut v: u32 = 0;
448            for &byte in &binary_data {
449                v = (v << 8) | byte as u32;
450            }
451
452            // 根据符号处理数值
453            if mpi.sign() == mbedtls::bignum::Sign::Positive {
454                // 正数情况
455                if v > i32::MAX as u32 {
456                    return TEE_ERROR_OVERFLOW;
457                }
458                unsafe {
459                    *dest = v as i32;
460                }
461            } else {
462                // 负数情况
463                // 对于负数,直接取二进制数据并转换为 i32
464                // 由于 TEE_BigInt 使用符号-幅值表示法,负数的幅值需要转换为补码
465                if v > (i32::MAX as u32 + 1) {
466                    return TEE_ERROR_OVERFLOW;
467                }
468                unsafe {
469                    *dest = if v as i32 == i32::MIN {
470                        i32::MIN
471                    } else {
472                        -(v as i32)
473                    };
474                }
475            }
476
477            TEE_SUCCESS
478        }
479        Err(_) => TEE_ERROR_OVERFLOW,
480    }
481}
482
483/// 比较两个 TEE_BigInt 值
484///
485/// 参数:
486/// - op1: 第一个 TEE_BigInt 指针
487/// - op2: 第二个 TEE_BigInt 指针
488///
489/// 返回值:
490/// - i32: 比较结果 (-1, 0, 或 1)
491#[unsafe(no_mangle)]
492pub extern "C" fn TEE_BigIntCmp(op1: *const TEE_BigInt, op2: *const TEE_BigInt) -> i32 {
493    // 从 TEE_BigInt 创建 MPI 对象,使用正确的方法
494    let mpi1 = match unsafe { Mpi::from_teebigint(op1) } {
495        Ok(mpi) => mpi,
496        Err(_) => return 0, // 出错时返回相等
497    };
498
499    let mpi2 = match unsafe { Mpi::from_teebigint(op2) } {
500        Ok(mpi) => mpi,
501        Err(_) => return 0, // 出错时返回相等
502    };
503
504    // 比较两个 Mpi 值
505    match mpi1.cmp(&mpi2) {
506        std::cmp::Ordering::Less => -1,
507        std::cmp::Ordering::Equal => 0,
508        std::cmp::Ordering::Greater => 1,
509    }
510}
511
512/// 比较 TEE_BigInt 与 32 位有符号整数
513///
514/// 参数:
515/// - src: TEE_BigInt 指针
516/// - short_val: 32 位有符号整数
517///
518/// 返回值:
519/// - i32: 比较结果 (-1, 0, 或 1)
520#[unsafe(no_mangle)]
521pub extern "C" fn TEE_BigIntCmpS32(src: *const TEE_BigInt, short_val: i32) -> i32 {
522    // 从 TEE_BigInt 创建 MPI 对象
523    let mpi = match unsafe { Mpi::from_teebigint(src) } {
524        Ok(mpi) => mpi,
525        Err(_) => return 0, // 出错时返回相等
526    };
527
528    // 创建用于比较的 MPI 对象
529    let cmp_mpi = match Mpi::new(short_val as mpi_sint) {
530        Ok(mpi) => mpi,
531        Err(_) => return 0, // 出错时返回相等
532    };
533
534    // 比较两个 Mpi 值
535    match mpi.cmp(&cmp_mpi) {
536        std::cmp::Ordering::Less => -1,
537        std::cmp::Ordering::Equal => 0,
538        std::cmp::Ordering::Greater => 1,
539    }
540}
541
542/// 将 TEE_BigInt 右移指定位数
543///
544/// 参数:
545/// - dest: 目标 TEE_BigInt 指针
546/// - op: 源 TEE_BigInt 指针
547/// - bits: 要右移的位数
548#[unsafe(no_mangle)]
549pub extern "C" fn TEE_BigIntShiftRight(dest: *mut TEE_BigInt, op: *const TEE_BigInt, bits: usize) {
550    // 创建临时 MPI 对象来处理移位操作
551    let mut temp_mpi = match unsafe { Mpi::from_teebigint(op) } {
552        Ok(mpi) => mpi,
553        Err(_) => return, // 如果转换失败则直接返回
554    };
555
556    // 执行移位操作
557    temp_mpi.shr_assign(bits);
558
559    // 将结果复制到目标
560    let dest_info = unsafe {
561        let hdr = dest as *mut BigintHdr;
562        (*hdr).alloc_size as usize
563    };
564
565    // 将临时 MPI 转换为目标 TEE_BigInt
566    unsafe {
567        match temp_mpi.to_teebigint(dest, dest_info) {
568            Ok(_) => {
569                // 转换成功
570            }
571            Err(_) => {
572                // 如果转换失败,至少确保目标被正确初始化为0
573                let hdr = dest as *mut BigintHdr;
574                (*hdr).sign = 0;
575                (*hdr).nblimbs = 0;
576
577                // 清零数据区域
578                let data_ptr = dest.add(2) as *mut u32;
579                for i in 0..dest_info {
580                    *data_ptr.add(i) = 0;
581                }
582            }
583        }
584    }
585}
586
587/// 获取 TEE_BigInt 中指定位置的位值
588///
589/// 参数:
590/// - src: 源 TEE_BigInt 指针
591/// - bit_index: 位索引
592///
593/// 返回值:
594/// - bool: 指定位的值
595#[unsafe(no_mangle)]
596pub extern "C" fn TEE_BigIntGetBit(src: *const TEE_BigInt, bit_index: u32) -> bool {
597    // 从 TEE_BigInt 创建 MPI 对象
598    let mpi = match unsafe { Mpi::from_teebigint(src) } {
599        Ok(mpi) => mpi,
600        Err(_) => return false, // 出错时返回 false
601    };
602
603    // 获取指定位的值
604    mpi.get_bit(bit_index as usize)
605}
606
607/// 获取 TEE_BigInt 的位长度
608///
609/// 参数:
610/// - src: 源 TEE_BigInt 指针
611///
612/// 返回值:
613/// - u32: 位长度
614#[unsafe(no_mangle)]
615pub extern "C" fn TEE_BigIntGetBitCount(src: *const TEE_BigInt) -> u32 {
616    // 从 TEE_BigInt 创建 MPI 对象
617    let mpi = match unsafe { Mpi::from_teebigint(src) } {
618        Ok(mpi) => mpi,
619        Err(_) => return 0, // 出错时返回 0
620    };
621
622    // 获取位长度
623    match mpi.bit_length() {
624        Ok(len) => len as u32,
625        Err(_) => 0, // 出错时返回 0
626    }
627}
628
629/// 设置 TEE_BigInt 中指定位置的位值
630///
631/// 参数:
632/// - op: 目标 TEE_BigInt 指针
633/// - bit_index: 位索引
634/// - value: 要设置的位值
635///
636/// 返回值:
637/// - TEE_Result: 操作结果
638#[unsafe(no_mangle)]
639pub extern "C" fn TEE_BigIntSetBit(op: *mut TEE_BigInt, bit_index: u32, value: bool) -> TEE_Result {
640    // 从 TEE_BigInt 创建 MPI 对象
641    let mut mpi = match unsafe { Mpi::from_teebigint(op as *const TEE_BigInt) } {
642        Ok(mpi) => mpi,
643        Err(_) => return TEE_ERROR_OVERFLOW,
644    };
645
646    // 设置指定位的值
647    match mpi.set_bit(bit_index as usize, value) {
648        Ok(()) => {
649            // 将结果复制回 TEE_BigInt
650            unsafe {
651                let hdr = op as *mut BigintHdr;
652                let alloc_size = (*hdr).alloc_size as usize;
653
654                match mpi.to_teebigint(op, alloc_size) {
655                    Ok(()) => TEE_SUCCESS,
656                    Err(_) => TEE_ERROR_OVERFLOW,
657                }
658            }
659        }
660        Err(_) => TEE_ERROR_OVERFLOW,
661    }
662}
663
664/// 将一个 TEE_BigInt 的值赋给另一个 TEE_BigInt
665///
666/// 参数:
667/// - dest: 目标 TEE_BigInt 指针
668/// - src: 源 TEE_BigInt 指针
669///
670/// 返回值:
671/// - TEE_Result: 操作结果
672#[unsafe(no_mangle)]
673pub extern "C" fn TEE_BigIntAssign(dest: *mut TEE_BigInt, src: *const TEE_BigInt) -> TEE_Result {
674    // 检查是否为同一对象
675    if dest == src as *mut TEE_BigInt {
676        return TEE_SUCCESS;
677    }
678
679    // 检查空指针
680    if dest.is_null() || src.is_null() {
681        return TEE_ERROR_OVERFLOW;
682    }
683
684    unsafe {
685        let src_hdr = src as *const BigintHdr;
686        let dst_hdr = dest as *mut BigintHdr;
687
688        // 检查目标分配大小是否足够
689        if (*dst_hdr).alloc_size < (*src_hdr).nblimbs {
690            return TEE_ERROR_OVERFLOW;
691        }
692
693        // 使用 slice 方式进行复制,避免直接指针操作
694        let src_slice = core::slice::from_raw_parts(
695            (src as *const u32).add(BIGINT_HDR_SIZE_IN_U32),
696            (*src_hdr).nblimbs as usize,
697        );
698
699        let dst_slice = core::slice::from_raw_parts_mut(
700            dest.add(BIGINT_HDR_SIZE_IN_U32),
701            (*src_hdr).nblimbs as usize,
702        );
703
704        // 复制头部信息
705        (*dst_hdr).nblimbs = (*src_hdr).nblimbs;
706        (*dst_hdr).sign = (*src_hdr).sign;
707
708        // 复制数据部分
709        dst_slice.copy_from_slice(src_slice);
710    }
711
712    TEE_SUCCESS
713}
714
715/// 计算 TEE_BigInt 的绝对值
716///
717/// 参数:
718/// - dest: 目标 TEE_BigInt 指针
719/// - src: 源 TEE_BigInt 指针
720///
721/// 返回值:
722/// - TEE_Result: 操作结果
723#[unsafe(no_mangle)]
724pub extern "C" fn TEE_BigIntAbs(dest: *mut TEE_BigInt, src: *const TEE_BigInt) -> TEE_Result {
725    let res = TEE_BigIntAssign(dest, src);
726
727    if res == TEE_SUCCESS {
728        unsafe {
729            let dst_hdr = dest as *mut BigintHdr;
730            (*dst_hdr).sign = 1; // 设置为正数
731        }
732    }
733
734    res
735}
736
737/// 执行两个 TEE_BigInt 的二元运算
738///
739/// 参数:
740/// - dest: 目标 TEE_BigInt 指针
741/// - op1: 第一个操作数 TEE_BigInt 指针
742/// - op2: 第二个操作数 TEE_BigInt 指针
743/// - func: 执行实际运算的函数
744fn bigint_binary(
745    dest: *mut TEE_BigInt,
746    op1: *const TEE_BigInt,
747    op2: *const TEE_BigInt,
748    func: unsafe extern "C" fn(
749        *mut mbedtls_sys_auto::mpi,
750        *const mbedtls_sys_auto::mpi,
751        *const mbedtls_sys_auto::mpi,
752    ) -> i32,
753) -> TEE_Result {
754    unsafe {
755        // 获取目标缓冲区信息
756        let dst_hdr = dest as *mut BigintHdr;
757        let alloc_size = (*dst_hdr).alloc_size as usize;
758
759        // 从操作数创建 MPI 对象
760        let mpi_op1 = if op1 == dest as *const TEE_BigInt {
761            None // 稍后使用目标 MPI
762        } else {
763            match Mpi::from_teebigint(op1) {
764                Ok(mpi) => Some(mpi),
765                Err(_) => return TEE_ERROR_OVERFLOW,
766            }
767        };
768
769        let mpi_op2 = if op2 == dest as *const TEE_BigInt {
770            None // 稍后使用目标 MPI
771        } else if op2 == op1 {
772            mpi_op1.clone() // 复用第一个操作数
773        } else {
774            match Mpi::from_teebigint(op2) {
775                Ok(mpi) => Some(mpi),
776                Err(_) => return TEE_ERROR_OVERFLOW,
777            }
778        };
779
780        // 从目标创建 MPI 对象或使用现有对象
781        let mut mpi_dest = match Mpi::from_teebigint(dest as *const TEE_BigInt) {
782            Ok(mpi) => mpi,
783            Err(_) => return TEE_ERROR_OVERFLOW,
784        };
785
786        // 根据不同情况执行运算
787        let result = if op1 == dest as *const TEE_BigInt && op2 == dest as *const TEE_BigInt {
788            // op1 和 op2 都等于 dest,都使用目标 MPI
789            func(
790                (&mut mpi_dest).into(),
791                (&mpi_dest).into(),
792                (&mpi_dest).into(),
793            )
794        } else if op1 == dest as *const TEE_BigInt {
795            // 只有 op1 等于 dest
796            if op2 == op1 {
797                // op2 也等于 op1 (即 dest)
798                func(
799                    (&mut mpi_dest).into(),
800                    (&mpi_dest).into(),
801                    (&mpi_dest).into(),
802                )
803            } else {
804                // op2 不等于 op1
805                func(
806                    (&mut mpi_dest).into(),
807                    (&mpi_dest).into(),
808                    mpi_op2.as_ref().unwrap().into(),
809                )
810            }
811        } else if op2 == dest as *const TEE_BigInt {
812            // 只有 op2 等于 dest
813            func(
814                (&mut mpi_dest).into(),
815                mpi_op1.as_ref().unwrap().into(),
816                (&mpi_dest).into(),
817            )
818        } else {
819            // op1 和 op2 都不等于 dest
820            if op2 == op1 {
821                // op2 复用 op1
822                let op1_handle = mpi_op1.as_ref().unwrap().into();
823                func((&mut mpi_dest).into(), op1_handle, op1_handle)
824            } else {
825                // op1 和 op2 都是独立的操作数
826                func(
827                    (&mut mpi_dest).into(),
828                    mpi_op1.as_ref().unwrap().into(),
829                    mpi_op2.as_ref().unwrap().into(),
830                )
831            }
832        };
833
834        if result != 0 {
835            return TEE_ERROR_OVERFLOW;
836        }
837
838        // 将结果复制回目标 TEE_BigInt
839        match mpi_dest.to_teebigint(dest, alloc_size) {
840            Ok(()) => TEE_SUCCESS,
841            Err(_) => TEE_ERROR_OVERFLOW,
842        }
843    }
844}
845
846/// 执行两个 TEE_BigInt 的模运算二元运算
847///
848/// 参数:
849/// - dest: 目标 TEE_BigInt 指针
850/// - op1: 第一个操作数 TEE_BigInt 指针
851/// - op2: 第二个操作数 TEE_BigInt 指针
852/// - n: 模数 TEE_BigInt 指针
853/// - func: 执行实际运算的函数
854fn bigint_binary_mod(
855    dest: *mut TEE_BigInt,
856    op1: *const TEE_BigInt,
857    op2: *const TEE_BigInt,
858    n: *const TEE_BigInt,
859    func: unsafe extern "C" fn(
860        *mut mbedtls_sys_auto::mpi,
861        *const mbedtls_sys_auto::mpi,
862        *const mbedtls_sys_auto::mpi,
863    ) -> i32,
864) -> TEE_Result {
865    unsafe {
866        // 检查模数是否有效(大于等于2)
867        if TEE_BigIntCmpS32(n, 2) < 0 {
868            panic!("Modulus is too short");
869        }
870
871        // 获取目标缓冲区信息
872        let dst_hdr = dest as *mut BigintHdr;
873        let alloc_size = (*dst_hdr).alloc_size as usize;
874
875        // 从模数创建 MPI 对象
876        let mpi_n = match Mpi::from_teebigint(n) {
877            Ok(mpi) => mpi,
878            Err(_) => return TEE_ERROR_OVERFLOW,
879        };
880
881        // 从操作数创建 MPI 对象
882        let mpi_op1 = if op1 == dest as *const TEE_BigInt {
883            None // 稍后使用目标 MPI
884        } else {
885            match Mpi::from_teebigint(op1) {
886                Ok(mpi) => Some(mpi),
887                Err(_) => return TEE_ERROR_OVERFLOW,
888            }
889        };
890
891        let mpi_op2 = if op2 == dest as *const TEE_BigInt {
892            None // 稍后使用目标 MPI
893        } else if op2 == op1 {
894            mpi_op1.clone() // 复用第一个操作数
895        } else {
896            match Mpi::from_teebigint(op2) {
897                Ok(mpi) => Some(mpi),
898                Err(_) => return TEE_ERROR_OVERFLOW,
899            }
900        };
901
902        // 从目标创建 MPI 对象或使用现有对象
903        let mut mpi_dest = match Mpi::from_teebigint(dest as *const TEE_BigInt) {
904            Ok(mpi) => mpi,
905            Err(_) => return TEE_ERROR_OVERFLOW,
906        };
907
908        // 创建临时 MPI 对象用于中间计算
909        let mut mpi_t = match Mpi::new(0) {
910            Ok(mpi) => mpi,
911            Err(_) => return TEE_ERROR_OVERFLOW,
912        };
913
914        // 根据不同情况执行运算
915        let result = if op1 == dest as *const TEE_BigInt && op2 == dest as *const TEE_BigInt {
916            // op1 和 op2 都等于 dest,都使用目标 MPI
917            func((&mut mpi_t).into(), (&mpi_dest).into(), (&mpi_dest).into())
918        } else if op1 == dest as *const TEE_BigInt {
919            // 只有 op1 等于 dest
920            if op2 == op1 {
921                // op2 也等于 op1 (即 dest)
922                func((&mut mpi_t).into(), (&mpi_dest).into(), (&mpi_dest).into())
923            } else {
924                // op2 不等于 op1
925                func(
926                    (&mut mpi_t).into(),
927                    (&mpi_dest).into(),
928                    mpi_op2.as_ref().unwrap().into(),
929                )
930            }
931        } else if op2 == dest as *const TEE_BigInt {
932            // 只有 op2 等于 dest
933            func(
934                (&mut mpi_t).into(),
935                mpi_op1.as_ref().unwrap().into(),
936                (&mpi_dest).into(),
937            )
938        } else {
939            // op1 和 op2 都不等于 dest
940            if op2 == op1 {
941                // op2 复用 op1
942                let op1_handle = mpi_op1.as_ref().unwrap().into();
943                func((&mut mpi_t).into(), op1_handle, op1_handle)
944            } else {
945                // op1 和 op2 都是独立的操作数
946                func(
947                    (&mut mpi_t).into(),
948                    mpi_op1.as_ref().unwrap().into(),
949                    mpi_op2.as_ref().unwrap().into(),
950                )
951            }
952        };
953
954        if result != 0 {
955            return TEE_ERROR_OVERFLOW;
956        }
957
958        // 执行模运算: mpi_dest = mpi_t % mpi_n
959        let mod_result =
960            mbedtls_sys::mpi_mod_mpi((&mut mpi_dest).into(), (&mpi_t).into(), (&mpi_n).into());
961
962        if mod_result != 0 {
963            return TEE_ERROR_OVERFLOW;
964        }
965
966        // 将结果复制回目标 TEE_BigInt
967        match mpi_dest.to_teebigint(dest, alloc_size) {
968            Ok(()) => TEE_SUCCESS,
969            Err(_) => TEE_ERROR_OVERFLOW,
970        }
971    }
972}
973
974/// 对两个 TEE_BigInt 执行加法运算
975///
976/// 参数:
977/// - dest: 目标 TEE_BigInt 指针
978/// - op1: 第一个操作数 TEE_BigInt 指针
979/// - op2: 第二个操作数 TEE_BigInt 指针
980#[unsafe(no_mangle)]
981pub extern "C" fn TEE_BigIntAdd(
982    dest: *mut TEE_BigInt,
983    op1: *const TEE_BigInt,
984    op2: *const TEE_BigInt,
985) {
986    let _ = bigint_binary(dest, op1, op2, mbedtls_sys_auto::mpi_add_mpi);
987}
988
989/// 对两个 TEE_BigInt 执行减法运算
990///
991/// 参数:
992/// - dest: 目标 TEE_BigInt 指针
993/// - op1: 第一个操作数 TEE_BigInt 指针
994/// - op2: 第二个操作数 TEE_BigInt 指针
995#[unsafe(no_mangle)]
996pub extern "C" fn TEE_BigIntSub(
997    dest: *mut TEE_BigInt,
998    op1: *const TEE_BigInt,
999    op2: *const TEE_BigInt,
1000) {
1001    let _ = bigint_binary(dest, op1, op2, mbedtls_sys_auto::mpi_sub_mpi);
1002}
1003
1004/// 对 TEE_BigInt 执行取负运算
1005///
1006/// 参数:
1007/// - dest: 目标 TEE_BigInt 指针
1008/// - src: 源 TEE_BigInt 指针
1009#[unsafe(no_mangle)]
1010pub extern "C" fn TEE_BigIntNeg(dest: *mut TEE_BigInt, src: *const TEE_BigInt) {
1011    unsafe {
1012        // 获取目标缓冲区信息
1013        let dst_hdr = dest as *mut BigintHdr;
1014        let alloc_size = (*dst_hdr).alloc_size as usize;
1015
1016        // 从源创建 MPI 对象
1017        let mut mpi_src = if dest == src as *mut TEE_BigInt {
1018            // 如果源和目标相同,直接从目标创建 MPI 对象
1019            match Mpi::from_teebigint(src) {
1020                Ok(mpi) => mpi,
1021                Err(_) => return, // 出错时直接返回
1022            }
1023        } else {
1024            // 如果源和目标不同,从源创建 MPI 对象并复制到目标
1025            match Mpi::from_teebigint(src) {
1026                Ok(mpi) => mpi,
1027                Err(_) => return, // 出错时直接返回
1028            }
1029        };
1030
1031        // 执行取负操作(改变符号)
1032        // 通过修改符号字段实现取负
1033        let handle: *mut mbedtls_sys::mpi = (&mut mpi_src).into();
1034        (*handle).s *= -1;
1035
1036        // 将结果复制回目标 TEE_BigInt
1037        let _ = mpi_src.to_teebigint(dest, alloc_size);
1038    }
1039}
1040
1041/// 计算所需的 TEE_BigInt 大小(以 u32 为单位)
1042///
1043/// 参数:
1044/// - n: 位数
1045///
1046/// 返回值:
1047/// - usize: 所需的 u32 数量
1048fn tee_big_int_size_in_u32(n: usize) -> usize {
1049    ((n + 31) / 32) + BIGINT_HDR_SIZE_IN_U32
1050}
1051
1052/// 计算两个 TEE_BigInt 的乘积
1053///
1054/// 参数:
1055/// - dest: 目标 TE_BigInt 指针
1056/// - op1: 第一个操作数 TEE_BigInt 指针
1057/// - op2: 第二个操作数 TEE_BigInt 指针
1058#[unsafe(no_mangle)]
1059pub extern "C" fn TEE_BigIntMul(
1060    dest: *mut TEE_BigInt,
1061    op1: *const TEE_BigInt,
1062    op2: *const TEE_BigInt,
1063) {
1064    // 获取操作数的位数
1065    let bs1 = TEE_BigIntGetBitCount(op1);
1066    let bs2 = TEE_BigIntGetBitCount(op2);
1067
1068    // 计算所需的空间大小
1069    let s = tee_big_int_size_in_u32(bs1 as usize) + tee_big_int_size_in_u32(bs2 as usize);
1070
1071    // 分配临时缓冲区
1072    let mut tmp_storage = vec![0u32; s];
1073    let tmp = tmp_storage.as_mut_ptr();
1074
1075    // 初始化临时缓冲区
1076    TEE_BigIntInit(tmp, s);
1077
1078    // 执行乘法运算
1079    let _ = bigint_binary(tmp, op1, op2, mbedtls_sys_auto::mpi_mul_mpi);
1080
1081    // 将结果复制到目标
1082    let zero_storage = [0u32; BIGINT_HDR_SIZE_IN_U32 + 1];
1083    let zero = zero_storage.as_ptr();
1084    TEE_BigIntInit(zero as *mut TEE_BigInt, BIGINT_HDR_SIZE_IN_U32 + 1);
1085
1086    TEE_BigIntAdd(dest, tmp, zero);
1087
1088    // tmp_storage 会自动释放
1089}
1090
1091/// 计算 TEE_BigInt 的平方
1092///
1093/// 参数:
1094/// - dest: 目标 TEE_BigInt 指针
1095/// - op: 操作数 TEE_BigInt 指针
1096#[unsafe(no_mangle)]
1097pub extern "C" fn TEE_BigIntSquare(dest: *mut TEE_BigInt, op: *const TEE_BigInt) {
1098    // 平方就是自己乘以自己
1099    TEE_BigIntMul(dest, op, op);
1100}
1101
1102/// 计算两个 TEE_BigInt 的除法运算
1103///
1104/// 参数:
1105/// - dest_q: 商的目标 TEE_BigInt 指针(可为空)
1106/// - dest_r: 余数的目标 TEE_BigInt 指针(可为空)
1107/// - op1: 被除数 TEE_BigInt 指针
1108/// - op2: 除数 TEE_BigInt 指针
1109#[unsafe(no_mangle)]
1110pub extern "C" fn TEE_BigIntDiv(
1111    dest_q: *mut TEE_BigInt,
1112    dest_r: *mut TEE_BigInt,
1113    op1: *const TEE_BigInt,
1114    op2: *const TEE_BigInt,
1115) {
1116    unsafe {
1117        // 检查除数是否为零
1118        let zero_check = Mpi::from_teebigint(op2);
1119        if let Ok(ref mpi_op2) = zero_check {
1120            // 检查是否为零值
1121            let is_zero = match mpi_op2.to_binary() {
1122                Ok(binary_data) => binary_data.iter().all(|&x| x == 0),
1123                Err(_) => true, // 出错时当作零处理以保证安全
1124            };
1125
1126            if is_zero {
1127                panic!("Division by zero");
1128            }
1129        }
1130
1131        // 获取目标缓冲区信息
1132        let q_alloc_size = if !dest_q.is_null() {
1133            let q_hdr = dest_q as *mut BigintHdr;
1134            Some((*q_hdr).alloc_size as usize)
1135        } else {
1136            None
1137        };
1138
1139        let r_alloc_size = if !dest_r.is_null() {
1140            let r_hdr = dest_r as *mut BigintHdr;
1141            Some((*r_hdr).alloc_size as usize)
1142        } else {
1143            None
1144        };
1145
1146        // 从操作数创建 MPI 对象
1147        let mpi_op1 = if op1 == dest_q || op1 == dest_r {
1148            // 如果操作数与目标相同,需要特殊处理
1149            match Mpi::from_teebigint(op1) {
1150                Ok(mpi) => mpi,
1151                Err(_) => return, // 出错时直接返回
1152            }
1153        } else {
1154            match Mpi::from_teebigint(op1) {
1155                Ok(mpi) => mpi,
1156                Err(_) => return, // 出错时直接返回
1157            }
1158        };
1159
1160        let mpi_op2 = if op2 == op1 {
1161            // 复用第一个操作数
1162            mpi_op1.clone()
1163        } else if op2 == dest_q || op2 == dest_r {
1164            // 如果操作数与目标相同,需要特殊处理
1165            match Mpi::from_teebigint(op2) {
1166                Ok(mpi) => mpi,
1167                Err(_) => return, // 出错时直接返回
1168            }
1169        } else {
1170            match Mpi::from_teebigint(op2) {
1171                Ok(mpi) => mpi,
1172                Err(_) => return, // 出错时直接返回
1173            }
1174        };
1175
1176        // 创建目标 MPI 对象
1177        let mut mpi_dest_q = match Mpi::new(0) {
1178            Ok(mpi) => mpi,
1179            Err(_) => return, // 出错时直接返回
1180        };
1181
1182        let mut mpi_dest_r = match Mpi::new(0) {
1183            Ok(mpi) => mpi,
1184            Err(_) => return, // 出错时直接返回
1185        };
1186
1187        // 执行除法运算
1188        let result = mbedtls_sys::mpi_div_mpi(
1189            (&mut mpi_dest_q).into(),
1190            (&mut mpi_dest_r).into(),
1191            (&mpi_op1).into(),
1192            (&mpi_op2).into(),
1193        );
1194
1195        if result != 0 {
1196            return; // 出错时直接返回
1197        }
1198
1199        // 将结果复制回目标 TEE_BigInt
1200        if !dest_q.is_null() {
1201            let _ = mpi_dest_q.to_teebigint(dest_q, q_alloc_size.unwrap());
1202        }
1203
1204        if !dest_r.is_null() {
1205            let _ = mpi_dest_r.to_teebigint(dest_r, r_alloc_size.unwrap());
1206        }
1207    }
1208}
1209
1210/// 计算 TEE_BigInt 的模运算
1211///
1212/// 参数:
1213/// - dest: 目标 TEE_BigInt 指针
1214/// - op: 操作数 TEE_BigInt 指针
1215/// - n: 模数 TEE_BigInt 指针
1216#[unsafe(no_mangle)]
1217pub extern "C" fn TEE_BigIntMod(
1218    dest: *mut TEE_BigInt,
1219    op: *const TEE_BigInt,
1220    n: *const TEE_BigInt,
1221) {
1222    // 检查模数是否有效(大于等于2)
1223    if TEE_BigIntCmpS32(n, 2) < 0 {
1224        panic!("Modulus is too short");
1225    }
1226
1227    let _ = bigint_binary(dest, op, n, mbedtls_sys_auto::mpi_mod_mpi);
1228}
1229
1230/// 计算两个 TEE_BigInt 的模加法运算
1231///
1232/// 参数:
1233/// - dest: 目标 TEE_BigInt 指针
1234/// - op1: 第一个操作数 TEE_BigInt 指针
1235/// - op2: 第二个操作数 TEE_BigInt 指针
1236/// - n: 模数 TEE_BigInt 指针
1237#[unsafe(no_mangle)]
1238pub extern "C" fn TEE_BigIntAddMod(
1239    dest: *mut TEE_BigInt,
1240    op1: *const TEE_BigInt,
1241    op2: *const TEE_BigInt,
1242    n: *const TEE_BigInt,
1243) {
1244    let _ = bigint_binary_mod(dest, op1, op2, n, mbedtls_sys_auto::mpi_add_mpi);
1245}
1246
1247/// 计算两个 TEE_BigInt 的模减法运算
1248///
1249/// 参数:
1250/// - dest: 目标 TEE_BigInt 指针
1251/// - op1: 第一个操作数 TEE_BigInt 指针
1252/// - op2: 第二个操作数 TEE_BigInt 指针
1253/// - n: 模数 TEE_BigInt 指针
1254#[unsafe(no_mangle)]
1255pub extern "C" fn TEE_BigIntSubMod(
1256    dest: *mut TEE_BigInt,
1257    op1: *const TEE_BigInt,
1258    op2: *const TEE_BigInt,
1259    n: *const TEE_BigInt,
1260) {
1261    let _ = bigint_binary_mod(dest, op1, op2, n, mbedtls_sys_auto::mpi_sub_mpi);
1262}
1263
1264/// 计算两个 TEE_BigInt 的模乘法运算
1265///
1266/// 参数:
1267/// - dest: 目标 TEE_BigInt 指针
1268/// - op1: 第一个操作数 TEE_BigInt 指针
1269/// - op2: 第二个操作数 TEE_BigInt 指针
1270/// - n: 模数 TEE_BigInt 指针
1271#[unsafe(no_mangle)]
1272pub extern "C" fn TEE_BigIntMulMod(
1273    dest: *mut TEE_BigInt,
1274    op1: *const TEE_BigInt,
1275    op2: *const TEE_BigInt,
1276    n: *const TEE_BigInt,
1277) {
1278    let _ = bigint_binary_mod(dest, op1, op2, n, mbedtls_sys_auto::mpi_mul_mpi);
1279}
1280
1281/// 计算 TEE_BigInt 的模平方运算
1282///
1283/// 参数:
1284/// - dest: 目标 TEE_BigInt 指针
1285/// - op: 操作数 TEE_BigInt 指针
1286/// - n: 模数 TEE_BigInt 指针
1287#[unsafe(no_mangle)]
1288pub extern "C" fn TEE_BigIntSquareMod(
1289    dest: *mut TEE_BigInt,
1290    op: *const TEE_BigInt,
1291    n: *const TEE_BigInt,
1292) {
1293    // 平方模运算就是自己与自己做模乘法
1294    TEE_BigIntMulMod(dest, op, op, n);
1295}
1296
1297/// 计算 TEE_BigInt 的模逆运算
1298///
1299/// 参数:
1300/// - dest: 目标 TEE_BigInt 指针
1301/// - op: 操作数 TEE_BigInt 指针
1302/// - n: 模数 TEE_BigInt 指针
1303#[unsafe(no_mangle)]
1304pub extern "C" fn TEE_BigIntInvMod(
1305    dest: *mut TEE_BigInt,
1306    op: *const TEE_BigInt,
1307    n: *const TEE_BigInt,
1308) {
1309    // 检查模数是否有效(大于等于2)以及操作数是否为零
1310    if TEE_BigIntCmpS32(n, 2) < 0 || TEE_BigIntCmpS32(op, 0) == 0 {
1311        panic!("too small modulus or trying to invert zero");
1312    }
1313
1314    unsafe {
1315        // 获取目标缓冲区信息
1316        let dst_hdr = dest as *mut BigintHdr;
1317        let alloc_size = (*dst_hdr).alloc_size as usize;
1318
1319        // 从模数创建 MPI 对象
1320        let mpi_n = match Mpi::from_teebigint(n) {
1321            Ok(mpi) => mpi,
1322            Err(_) => return, // 出错时直接返回
1323        };
1324
1325        // 从操作数创建 MPI 对象
1326        let mpi_op = if op == dest as *const TEE_BigInt {
1327            // 如果操作数与目标相同,直接从目标创建 MPI 对象
1328            match Mpi::from_teebigint(op) {
1329                Ok(mpi) => mpi,
1330                Err(_) => return, // 出错时直接返回
1331            }
1332        } else {
1333            // 如果操作数与目标不同
1334            match Mpi::from_teebigint(op) {
1335                Ok(mpi) => mpi,
1336                Err(_) => return, // 出错时直接返回
1337            }
1338        };
1339
1340        // 创建目标 MPI 对象
1341        let mut mpi_dest = match Mpi::new(0) {
1342            Ok(mpi) => mpi,
1343            Err(_) => return, // 出错时直接返回
1344        };
1345
1346        // 执行模逆运算
1347        let result =
1348            mbedtls_sys::mpi_inv_mod((&mut mpi_dest).into(), (&mpi_op).into(), (&mpi_n).into());
1349
1350        if result != 0 {
1351            return; // 出错时直接返回
1352        }
1353
1354        // 将结果复制回目标 TEE_BigInt
1355        let _ = mpi_dest.to_teebigint(dest, alloc_size);
1356    }
1357}
1358
1359/// 判断 TEE_BigInt 是否为奇数
1360///
1361/// 参数:
1362/// - src: TEE_BigInt 指针
1363///
1364/// 返回值:
1365/// - bool: 如果是奇数返回true,否则返回false
1366fn tee_bigint_is_odd(src: *const TEE_BigInt) -> bool {
1367    // 获取最低位的值来判断奇偶性
1368    TEE_BigIntGetBit(src, 0)
1369}
1370
1371/// 判断 TEE_BigInt 是否为偶数
1372///
1373/// 参数:
1374/// - src: TEE_BigInt 指针
1375///
1376/// 返回值:
1377/// - bool: 如果是偶数返回true,否则返回false
1378fn tee_bigint_is_even(src: *const TEE_BigInt) -> bool {
1379    !tee_bigint_is_odd(src)
1380}
1381
1382/// 计算 TEE_BigInt 的模幂运算
1383///
1384/// 参数:
1385/// - dest: 目标 TEE_BigInt 指针
1386/// - op1: 底数 TEE_BigInt 指针
1387/// - op2: 指数 TEE_BigInt 指针
1388/// - n: 模数 TEE_BigInt 指针
1389/// - context: FMM 上下文指针(未使用)
1390///
1391/// 返回值:
1392/// - TEE_Result: 操作结果
1393#[unsafe(no_mangle)]
1394pub extern "C" fn TEE_BigIntExpMod(
1395    dest: *mut TEE_BigInt,
1396    op1: *const TEE_BigInt,
1397    op2: *const TEE_BigInt,
1398    n: *const TEE_BigInt,
1399    _context: *const TEE_BigIntFMMContext,
1400) -> TEE_Result {
1401    // 检查模数是否有效(大于等于2)
1402    if TEE_BigIntCmpS32(n, 2) <= 0 {
1403        panic!("too small modulus");
1404    }
1405
1406    // 检查模数是否为奇数
1407    if tee_bigint_is_even(n) {
1408        return TEE_ERROR_OVERFLOW; // 使用合适的错误码替代 TEE_ERROR_NOT_SUPPORTED
1409    }
1410
1411    unsafe {
1412        // 获取目标缓冲区信息
1413        let dst_hdr = dest as *mut BigintHdr;
1414        let alloc_size = (*dst_hdr).alloc_size as usize;
1415
1416        // 从模数创建 MPI 对象
1417        let mpi_n = match Mpi::from_teebigint(n) {
1418            Ok(mpi) => mpi,
1419            Err(_) => return TEE_ERROR_OVERFLOW,
1420        };
1421
1422        // 从底数创建 MPI 对象
1423        let mpi_op1 = if op1 == dest as *const TEE_BigInt {
1424            // 如果底数与目标相同,直接从目标创建 MPI 对象
1425            match Mpi::from_teebigint(op1) {
1426                Ok(mpi) => mpi,
1427                Err(_) => return TEE_ERROR_OVERFLOW,
1428            }
1429        } else {
1430            // 如果底数与目标不同
1431            match Mpi::from_teebigint(op1) {
1432                Ok(mpi) => mpi,
1433                Err(_) => return TEE_ERROR_OVERFLOW,
1434            }
1435        };
1436
1437        // 从指数创建 MPI 对象
1438        let mpi_op2 = if op2 == dest as *const TEE_BigInt {
1439            // 如果指数与目标相同,直接从目标创建 MPI 对象
1440            match Mpi::from_teebigint(op2) {
1441                Ok(mpi) => mpi,
1442                Err(_) => return TEE_ERROR_OVERFLOW,
1443            }
1444        } else if op2 == op1 {
1445            // 如果指数与底数相同,复用底数的MPI对象
1446            mpi_op1.clone()
1447        } else {
1448            // 如果指数与底数、目标都不同
1449            match Mpi::from_teebigint(op2) {
1450                Ok(mpi) => mpi,
1451                Err(_) => return TEE_ERROR_OVERFLOW,
1452            }
1453        };
1454
1455        // 创建目标 MPI 对象
1456        let mut mpi_dest = match Mpi::new(0) {
1457            Ok(mpi) => mpi,
1458            Err(_) => return TEE_ERROR_OVERFLOW,
1459        };
1460
1461        // 执行模幂运算
1462        let result = mbedtls_sys::mpi_exp_mod(
1463            (&mut mpi_dest).into(),
1464            (&mpi_op1).into(),
1465            (&mpi_op2).into(),
1466            (&mpi_n).into(),
1467            core::ptr::null_mut(), // context参数为NULL
1468        );
1469
1470        if result != 0 {
1471            return TEE_ERROR_OVERFLOW;
1472        }
1473
1474        // 将结果复制回目标 TEE_BigInt
1475        match mpi_dest.to_teebigint(dest, alloc_size) {
1476            Ok(()) => TEE_SUCCESS,
1477            Err(_) => TEE_ERROR_OVERFLOW,
1478        }
1479    }
1480}
1481
1482/// 判断两个 TEE_BigInt 是否互质
1483///
1484/// 参数:
1485/// - op1: 第一个 TEE_BigInt 指针
1486/// - op2: 第二个 TEE_BigInt 指针
1487///
1488/// 返回值:
1489/// - bool: 如果互质返回true,否则返回false
1490#[unsafe(no_mangle)]
1491pub extern "C" fn TEE_BigIntRelativePrime(op1: *const TEE_BigInt, op2: *const TEE_BigInt) -> bool {
1492    unsafe {
1493        // 从第一个操作数创建 MPI 对象
1494        let mpi_op1 = match Mpi::from_teebigint(op1) {
1495            Ok(mpi) => mpi,
1496            Err(_) => return false, // 出错时返回false
1497        };
1498
1499        // 从第二个操作数创建 MPI 对象
1500        let mpi_op2 = if op2 == op1 {
1501            // 如果两个操作数相同,复用第一个MPI对象
1502            mpi_op1.clone()
1503        } else {
1504            match Mpi::from_teebigint(op2) {
1505                Ok(mpi) => mpi,
1506                Err(_) => return false, // 出错时返回false
1507            }
1508        };
1509
1510        // 创建用于计算GCD的MPI对象
1511        let mut gcd = match Mpi::new(0) {
1512            Ok(mpi) => mpi,
1513            Err(_) => return false, // 出错时返回false
1514        };
1515
1516        // 计算最大公约数
1517        let result = mbedtls_sys::mpi_gcd((&mut gcd).into(), (&mpi_op1).into(), (&mpi_op2).into());
1518
1519        if result != 0 {
1520            return false; // 出错时返回false
1521        }
1522
1523        // 检查GCD是否为1(互质的定义)
1524        // 使用现有的比较函数来比较GCD与1
1525        match Mpi::new(1) {
1526            Ok(one) => {
1527                match gcd.cmp(&one) {
1528                    std::cmp::Ordering::Equal => true, // GCD为1,互质
1529                    _ => false,                        // GCD不为1,不互质
1530                }
1531            }
1532            Err(_) => false, // 出错时返回false
1533        }
1534    }
1535}
1536
1537/// 计算两个 TEE_BigInt 的扩展最大公约数
1538///
1539/// 参数:
1540/// - gcd: 最大公约数的目标 TEE_BigInt 指针
1541/// - u: 系数u的目标 TEE_BigInt 指针(可为空)
1542/// - v: 系数v的目标 TEE_BigInt 指针(可为空)
1543/// - op1: 第一个操作数 TEE_BigInt 指针
1544/// - op2: 第二个操作数 TEE_BigInt 指针
1545#[unsafe(no_mangle)]
1546pub extern "C" fn TEE_BigIntComputeExtendedGcd(
1547    gcd: *mut TEE_BigInt,
1548    u: *mut TEE_BigInt,
1549    v: *mut TEE_BigInt,
1550    op1: *const TEE_BigInt,
1551    op2: *const TEE_BigInt,
1552) {
1553    // 检查必要参数
1554    if gcd.is_null() || op1.is_null() || op2.is_null() {
1555        return;
1556    }
1557
1558    unsafe {
1559        // 从操作数创建 MPI 对象
1560        let mpi_op1 = match Mpi::from_teebigint(op1) {
1561            Ok(mpi) => mpi,
1562            Err(_) => return,
1563        };
1564
1565        let mpi_op2 = if op2 == op1 {
1566            mpi_op1.clone()
1567        } else {
1568            match Mpi::from_teebigint(op2) {
1569                Ok(mpi) => mpi,
1570                Err(_) => return,
1571            }
1572        };
1573
1574        // 如果不需要计算系数u和v,直接使用内置GCD函数
1575        if u.is_null() && v.is_null() {
1576            let mut mpi_gcd = match Mpi::new(0) {
1577                Ok(mpi) => mpi,
1578                Err(_) => return,
1579            };
1580
1581            let result =
1582                mbedtls_sys::mpi_gcd((&mut mpi_gcd).into(), (&mpi_op1).into(), (&mpi_op2).into());
1583
1584            if result != 0 {
1585                return;
1586            }
1587
1588            // 将结果复制回目标 TEE_BigInt
1589            let hdr = gcd as *mut BigintHdr;
1590            let alloc_size = (*hdr).alloc_size as usize;
1591            let _ = mpi_gcd.to_teebigint(gcd, alloc_size);
1592            return;
1593        }
1594
1595        // 需要计算系数,执行扩展欧几里得算法
1596        let s1 = mpi_op1.sign();
1597        let s2 = mpi_op2.sign();
1598
1599        // 使用绝对值进行计算(通过创建新正值MPI代替set_sign)
1600        let abs_op1 = mpi_abs_value(&mpi_op1);
1601        let abs_op2 = mpi_abs_value(&mpi_op2);
1602
1603        let cmp = abs_op1.cmp(&abs_op2);
1604
1605        let (mpi_gcd, mpi_u, mpi_v) = match cmp {
1606            // 移除了mut
1607            std::cmp::Ordering::Equal => {
1608                // 两数相等的情况
1609                let gcd_result = abs_op1;
1610                let u_result = match Mpi::new(1) {
1611                    Ok(mpi) => mpi,
1612                    Err(_) => return,
1613                };
1614                let v_result = match Mpi::new(0) {
1615                    Ok(mpi) => mpi,
1616                    Err(_) => return,
1617                };
1618                (gcd_result, u_result, v_result)
1619            }
1620            std::cmp::Ordering::Greater => extended_gcd_algorithm(&abs_op1, &abs_op2),
1621            std::cmp::Ordering::Less => {
1622                // op1 < op2,交换参数
1623                let (gcd_result, v_result, u_result) = extended_gcd_algorithm(&abs_op2, &abs_op1);
1624                (gcd_result, u_result, v_result)
1625            }
1626        };
1627
1628        // 根据原始符号调整系数(使用negate_mpi_safe代替neg方法)
1629        let final_mpi_u = if s1 == mbedtls::bignum::Sign::Negative {
1630            negate_mpi_safe(&mpi_u)
1631        } else {
1632            mpi_u
1633        };
1634
1635        let final_mpi_v = if s2 == mbedtls::bignum::Sign::Negative {
1636            negate_mpi_safe(&mpi_v)
1637        } else {
1638            mpi_v
1639        };
1640
1641        // 将结果复制回目标 TEE_BigInt
1642        if !u.is_null() {
1643            let hdr = u as *mut BigintHdr;
1644            let alloc_size = (*hdr).alloc_size as usize;
1645            let _ = final_mpi_u.to_teebigint(u, alloc_size);
1646        }
1647
1648        if !v.is_null() {
1649            let hdr = v as *mut BigintHdr;
1650            let alloc_size = (*hdr).alloc_size as usize;
1651            let _ = final_mpi_v.to_teebigint(v, alloc_size);
1652        }
1653
1654        let hdr = gcd as *mut BigintHdr;
1655        let alloc_size = (*hdr).alloc_size as usize;
1656        let _ = mpi_gcd.to_teebigint(gcd, alloc_size);
1657    }
1658}
1659
1660/// 获取MPI的绝对值(创建一个新的正值MPI)
1661fn mpi_abs_value(mpi: &Mpi) -> Mpi {
1662    let mut result = mpi.clone();
1663    // 强制设置为正数
1664    unsafe {
1665        let handle: *mut mbedtls_sys::mpi = (&mut result).into();
1666        (*handle).s = 1;
1667    }
1668    result
1669}
1670
1671/// 对MPI取负值
1672fn negate_mpi_safe(mpi: &Mpi) -> Mpi {
1673    let mut result = mpi.clone();
1674    // 改变符号
1675    unsafe {
1676        let handle: *mut mbedtls_sys::mpi = (&mut result).into();
1677        (*handle).s *= -1;
1678    }
1679    result
1680}
1681
1682/// 扩展欧几里得算法实现
1683///
1684/// 参数:
1685/// - x: 较大的数
1686/// - y: 较小的数
1687///
1688/// 返回值:
1689/// - (gcd, a, b) 满足 ax + by = gcd(x,y)
1690fn extended_gcd_algorithm(x: &Mpi, y: &Mpi) -> (Mpi, Mpi, Mpi) {
1691    // 安全检查
1692    if let (Ok(x_binary), Ok(y_binary)) = (x.to_binary(), y.to_binary()) {
1693        if x_binary.iter().all(|&b| b == 0) || y_binary.iter().all(|&b| b == 0) {
1694            // 处理零值情况
1695            return (
1696                Mpi::new(0).unwrap_or_else(|_| Mpi::new(0).expect("Failed to create Mpi")),
1697                Mpi::new(0).unwrap_or_else(|_| Mpi::new(0).expect("Failed to create Mpi")),
1698                Mpi::new(0).unwrap_or_else(|_| Mpi::new(0).expect("Failed to create Mpi")),
1699            );
1700        }
1701    }
1702
1703    let mut u = x.clone();
1704    let mut v = y.clone();
1705
1706    // 初始化系数矩阵
1707    let mut a = Mpi::new(1).expect("Failed to create Mpi");
1708    let mut b = Mpi::new(0).expect("Failed to create Mpi");
1709    let mut c = Mpi::new(0).expect("Failed to create Mpi");
1710    let mut d = Mpi::new(1).expect("Failed to create Mpi");
1711
1712    // 计算公共因子2^k
1713    let mut k = 0;
1714    while mpi_is_even(&u) && mpi_is_even(&v) {
1715        k += 1;
1716        u = (&u >> 1).expect("Shift operation failed");
1717        v = (&v >> 1).expect("Shift operation failed");
1718    }
1719
1720    let mut x_copy = u.clone();
1721    let mut y_copy = v.clone();
1722
1723    // 主循环
1724    while !is_mpi_zero(&x_copy) {
1725        while mpi_is_even(&x_copy) {
1726            x_copy = (&x_copy >> 1).expect("Shift operation failed");
1727            // ...
1728
1729            if mpi_is_odd(&a) || mpi_is_odd(&b) {
1730                a = add_mpi_safe(&a, &y_copy);
1731                b = sub_mpi_safe(&b, &u);
1732            }
1733
1734            a = (&a >> 1).expect("Shift operation failed");
1735            b = (&b >> 1).expect("Shift operation failed");
1736        }
1737
1738        while mpi_is_even(&y_copy) {
1739            y_copy = (&y_copy >> 1).expect("Shift operation failed");
1740
1741            if mpi_is_odd(&c) || mpi_is_odd(&d) {
1742                c = add_mpi_safe(&c, &y_copy);
1743                d = sub_mpi_safe(&d, &u);
1744            }
1745
1746            c = (&c >> 1).expect("Shift operation failed");
1747            d = (&d >> 1).expect("Shift operation failed");
1748        }
1749
1750        match x_copy.cmp(&y_copy) {
1751            std::cmp::Ordering::Greater | std::cmp::Ordering::Equal => {
1752                x_copy = sub_mpi_safe(&x_copy, &y_copy);
1753                a = sub_mpi_safe(&a, &c);
1754                b = sub_mpi_safe(&b, &d);
1755            }
1756            std::cmp::Ordering::Less => {
1757                y_copy = sub_mpi_safe(&y_copy, &x_copy);
1758                c = sub_mpi_safe(&c, &a);
1759                d = sub_mpi_safe(&d, &b);
1760            }
1761        }
1762    }
1763
1764    // 左移k位恢复公共因子
1765    let gcd = (&y_copy << k).expect("Shift operation failed");
1766
1767    (gcd, c, d)
1768}
1769
1770/// 安全的MPI加法
1771fn add_mpi_safe(op1: &Mpi, op2: &Mpi) -> Mpi {
1772    let mut result = Mpi::new(0).expect("Failed to create Mpi");
1773    let ret = unsafe { mbedtls_sys::mpi_add_mpi((&mut result).into(), op1.into(), op2.into()) };
1774
1775    if ret == 0 {
1776        result
1777    } else {
1778        Mpi::new(0).expect("Failed to create Mpi")
1779    }
1780}
1781
1782/// 安全的MPI减法
1783fn sub_mpi_safe(op1: &Mpi, op2: &Mpi) -> Mpi {
1784    let mut result = Mpi::new(0).expect("Failed to create Mpi");
1785    let ret = unsafe { mbedtls_sys::mpi_sub_mpi((&mut result).into(), op1.into(), op2.into()) };
1786
1787    if ret == 0 {
1788        result
1789    } else {
1790        Mpi::new(0).expect("Failed to create Mpi")
1791    }
1792}
1793
1794/// 检查MPI是否为零
1795fn is_mpi_zero(mpi: &Mpi) -> bool {
1796    match mpi.to_binary() {
1797        Ok(data) => data.iter().all(|&b| b == 0),
1798        Err(_) => true,
1799    }
1800}
1801
1802/// 检查MPI是否为偶数
1803fn mpi_is_even(mpi: &Mpi) -> bool {
1804    match mpi.to_binary() {
1805        Ok(data) => {
1806            if data.is_empty() {
1807                true
1808            } else {
1809                (data[data.len() - 1] & 1) == 0
1810            }
1811        }
1812        Err(_) => true,
1813    }
1814}
1815
1816/// 检查MPI是否为奇数
1817fn mpi_is_odd(mpi: &Mpi) -> bool {
1818    !mpi_is_even(mpi)
1819}
1820
1821/// 检查 TEE_BigInt 是否可能是素数
1822///
1823/// 参数:
1824/// - op: 要检查的 TEE_BigInt 指针
1825/// - confidenceLevel: 置信水平(最小为80)
1826///
1827/// 返回值:
1828/// - i32: 1表示可能是素数,0表示不是素数
1829#[unsafe(no_mangle)]
1830pub extern "C" fn TEE_BigIntIsProbablePrime(op: *const TEE_BigInt, confidenceLevel: u32) -> i32 {
1831    // 检查输入参数
1832    if op.is_null() {
1833        return 0;
1834    }
1835
1836    // 从 TEE_BigInt 创建 MPI 对象
1837    let mpi = match unsafe { Mpi::from_teebigint(op) } {
1838        Ok(mpi) => mpi,
1839        Err(_) => return 0,
1840    };
1841
1842    // 使用至少80轮测试确保准确性
1843    let rounds = confidenceLevel.max(80);
1844
1845    // 创建一个符合 RngCallback 要求的结构体
1846    struct TeeRng;
1847
1848    impl RngCallback for TeeRng {
1849        unsafe extern "C" fn call(
1850            _user_data: *mut mbedtls_sys_auto::types::raw_types::c_void,
1851            data: *mut u8,
1852            len: usize,
1853        ) -> i32 {
1854            // 在实际实现中,这里应该调用真正的随机数生成器
1855            // 作为示例,我们使用简单的方式填充数据
1856            unsafe {
1857                for i in 0..len {
1858                    *data.add(i) = (i % 256) as u8;
1859                }
1860            }
1861            0 // 成功
1862        }
1863
1864        fn data_ptr(&self) -> *mut mbedtls_sys_auto::types::raw_types::c_void {
1865            core::ptr::null_mut()
1866        }
1867    }
1868
1869    // 创建 RNG 实例并执行素性测试
1870    let mut rng = TeeRng;
1871    match mpi.is_probably_prime(rounds, &mut rng) {
1872        Ok(()) => 1, // 通过素性测试,可能是素数
1873        Err(_) => 0, // 未通过素性测试,不是素数
1874    }
1875}
1876
1877/// 初始化一个 TEE_BigIntFMM 对象
1878///
1879/// 参数:
1880/// - big_int_fmm: 指向 TEE_BigIntFMM 的指针
1881/// - len: 以 u32 为单位的长度
1882#[unsafe(no_mangle)]
1883pub extern "C" fn TEE_BigIntInitFMM(big_int_fmm: *mut TEE_BigIntFMM, len: usize) {
1884    TEE_BigIntInit(big_int_fmm, len);
1885}
1886
1887/// 初始化一个 TEE_BigIntFMMContext 对象 (带返回值版本)
1888///
1889/// 参数:
1890/// - context: 指向 TEE_BigIntFMMContext 的指针
1891/// - len: 以 u32 为单位的长度
1892/// - modulus: 模数 TEE_BigInt 指针
1893///
1894/// 返回值:
1895/// - TEE_Result: 操作结果
1896#[unsafe(no_mangle)]
1897pub extern "C" fn TEE_BigIntInitFMMContext1(
1898    context: *mut TEE_BigIntFMMContext,
1899    len: usize,
1900    modulus: *const TEE_BigInt,
1901) -> TEE_Result {
1902    // 仅保留参数签名并返回成功
1903    let _ = context;
1904    let _ = len;
1905    let _ = modulus;
1906    TEE_SUCCESS
1907}
1908
1909/// 计算所需的 TEE_BigIntFMM 大小(以 u32 为单位)
1910///
1911/// 参数:
1912/// - modulus_size_in_bits: 模数的位数
1913///
1914/// 返回值:
1915/// - usize: 所需的 u32 数量
1916#[unsafe(no_mangle)]
1917pub extern "C" fn TEE_BigIntFMMSizeInU32(modulus_size_in_bits: usize) -> usize {
1918    // 复用已有的 TEE_BigIntSizeInU32 函数逻辑
1919    tee_big_int_size_in_u32(modulus_size_in_bits)
1920}
1921
1922/// 计算所需的 TEE_BigIntFMMContext 大小(以 u32 为单位)
1923///
1924/// 参数:
1925/// - modulus_size_in_bits: 模数的位数
1926///
1927/// 返回值:
1928/// - usize: 所需的 u32 数量
1929#[unsafe(no_mangle)]
1930pub extern "C" fn TEE_BigIntFMMContextSizeInU32(modulus_size_in_bits: usize) -> usize {
1931    // 返回大于0的值以使 malloc 等函数正常工作
1932    let _ = modulus_size_in_bits; // 未使用参数
1933    1
1934}
1935
1936/// 将 TEE_BigInt 转换为 TEE_BigIntFMM
1937///
1938/// 参数:
1939/// - dest: 目标 TEE_BigIntFMM 指针
1940/// - src: 源 TEE_BigInt 指针
1941/// - n: 模数 TEE_BigInt 指针
1942/// - context: FMM 上下文指针(未使用)
1943#[unsafe(no_mangle)]
1944pub extern "C" fn TEE_BigIntConvertToFMM(
1945    dest: *mut TEE_BigIntFMM,
1946    src: *const TEE_BigInt,
1947    n: *const TEE_BigInt,
1948    context: *const TEE_BigIntFMMContext,
1949) {
1950    // 调用 TEE_BigIntMod 函数
1951    let _ = context; // 未使用的参数
1952    TEE_BigIntMod(dest as *mut TEE_BigInt, src, n);
1953}
1954
1955/// 将 TEE_BigIntFMM 转换为 TEE_BigInt
1956///
1957/// 参数:
1958/// - dest: 目标 TEE_BigInt 指针
1959/// - src: 源 TEE_BigIntFMM 指针
1960/// - n: 模数 TEE_BigInt 指针(未使用)
1961/// - context: FMM 上下文指针(未使用)
1962#[unsafe(no_mangle)]
1963pub extern "C" fn TEE_BigIntConvertFromFMM(
1964    dest: *mut TEE_BigInt,
1965    src: *const TEE_BigIntFMM,
1966    n: *const TEE_BigInt,
1967    context: *const TEE_BigIntFMMContext,
1968) {
1969    // 因为 TEE_BigIntFMM 和 TEE_BigInt 都是 u32 类型别名,所以可以直接复制
1970
1971    // 检查空指针
1972    if dest.is_null() || src.is_null() {
1973        return;
1974    }
1975
1976    // 从源创建 MPI 对象
1977    let mpi_src = match unsafe { Mpi::from_teebigint(src as *const TEE_BigInt) } {
1978        Ok(mpi) => mpi,
1979        Err(_) => return,
1980    };
1981
1982    // 获取目标缓冲区信息
1983    let hdr = dest as *mut BigintHdr;
1984    let alloc_size = unsafe { (*hdr).alloc_size as usize };
1985
1986    // 将源 MPI 复制到目标 TEE_BigInt
1987    let _ = unsafe { mpi_src.to_teebigint(dest, alloc_size) };
1988
1989    // 未使用的参数
1990    let _ = n;
1991    let _ = context;
1992}
1993
1994/// 计算 TEE_BigIntFMM 的快速模乘运算
1995///
1996/// 参数:
1997/// - dest: 目标 TEE_BigIntFMM 指针
1998/// - op1: 第一个操作数 TEE_BigIntFMM 指针
1999/// - op2: 第二个操作数 TEE_BigIntFMM 指针
2000/// - n: 模数 TEE_BigInt 指针
2001/// - context: FMM 上下文指针(未使用)
2002#[unsafe(no_mangle)]
2003pub extern "C" fn TEE_BigIntComputeFMM(
2004    dest: *mut TEE_BigIntFMM,
2005    op1: *const TEE_BigIntFMM,
2006    op2: *const TEE_BigIntFMM,
2007    n: *const TEE_BigInt,
2008    context: *const TEE_BigIntFMMContext,
2009) {
2010    // 检查必要的参数
2011    if dest.is_null() || op1.is_null() || op2.is_null() || n.is_null() {
2012        return;
2013    }
2014
2015    // 未使用的参数
2016    let _ = context;
2017
2018    // 从操作数创建 MPI 对象
2019    let mpi_op1 = match unsafe { Mpi::from_teebigint(op1 as *const TEE_BigInt) } {
2020        Ok(mpi) => mpi,
2021        Err(_) => return,
2022    };
2023
2024    let mpi_op2 = if op2 as *const TEE_BigInt == op1 as *const TEE_BigInt {
2025        // 复用第一个操作数
2026        mpi_op1.clone()
2027    } else {
2028        match unsafe { Mpi::from_teebigint(op2 as *const TEE_BigInt) } {
2029            Ok(mpi) => mpi,
2030            Err(_) => return,
2031        }
2032    };
2033
2034    let mpi_n = match unsafe { Mpi::from_teebigint(n) } {
2035        Ok(mpi) => mpi,
2036        Err(_) => return,
2037    };
2038
2039    // 创建临时 MPI 对象用于中间计算
2040    let mut mpi_t = match Mpi::new(0) {
2041        Ok(mpi) => mpi,
2042        Err(_) => return,
2043    };
2044
2045    // 执行乘法运算: mpi_t = mpi_op1 * mpi_op2
2046    let mul_result = unsafe {
2047        mbedtls_sys::mpi_mul_mpi((&mut mpi_t).into(), (&mpi_op1).into(), (&mpi_op2).into())
2048    };
2049
2050    if mul_result != 0 {
2051        return;
2052    }
2053
2054    // 获取目标缓冲区信息
2055    let dst_hdr = dest as *mut BigintHdr;
2056    let alloc_size = unsafe { (*dst_hdr).alloc_size as usize };
2057
2058    // 执行模运算: dest = mpi_t % mpi_n
2059    let mod_result = unsafe {
2060        mbedtls_sys::mpi_mod_mpi(
2061            (&mut mpi_t).into(), // 我们可以重用 mpi_t 作为目标
2062            (&mpi_t).into(),     // 被模数
2063            (&mpi_n).into(),     // 模数
2064        )
2065    };
2066
2067    if mod_result != 0 {
2068        return;
2069    }
2070
2071    // 将结果复制回目标 TEE_BigIntFMM
2072    let _ = unsafe { mpi_t.to_teebigint(dest as *mut TEE_BigInt, alloc_size) };
2073}