17777dab0Sopenharmony_ci/*
27777dab0Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
37777dab0Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
47777dab0Sopenharmony_ci * you may not use this file except in compliance with the License.
57777dab0Sopenharmony_ci * You may obtain a copy of the License at
67777dab0Sopenharmony_ci *
77777dab0Sopenharmony_ci *    http://www.apache.org/licenses/LICENSE-2.0
87777dab0Sopenharmony_ci *
97777dab0Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
107777dab0Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
117777dab0Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
127777dab0Sopenharmony_ci * See the License for the specific language governing permissions and
137777dab0Sopenharmony_ci * limitations under the License.
147777dab0Sopenharmony_ci */
157777dab0Sopenharmony_ci
167777dab0Sopenharmony_ci#ifndef TEE_ARITH_API_H
177777dab0Sopenharmony_ci#define TEE_ARITH_API_H
187777dab0Sopenharmony_ci
197777dab0Sopenharmony_ci/**
207777dab0Sopenharmony_ci * @addtogroup TeeTrusted
217777dab0Sopenharmony_ci * @{
227777dab0Sopenharmony_ci *
237777dab0Sopenharmony_ci * @brief TEE(Trusted Excution Environment) API.
247777dab0Sopenharmony_ci * Provides security capability APIs such as trusted storage, encryption and decryption,
257777dab0Sopenharmony_ci * and trusted time for trusted application development.
267777dab0Sopenharmony_ci *
277777dab0Sopenharmony_ci * @since 12
287777dab0Sopenharmony_ci */
297777dab0Sopenharmony_ci
307777dab0Sopenharmony_ci/**
317777dab0Sopenharmony_ci * @file tee_arith_api.h
327777dab0Sopenharmony_ci *
337777dab0Sopenharmony_ci * @brief Provides APIs for operating big integers.
347777dab0Sopenharmony_ci *
357777dab0Sopenharmony_ci * @library NA
367777dab0Sopenharmony_ci * @kit TEEKit
377777dab0Sopenharmony_ci * @syscap SystemCapability.Tee.TeeClient
387777dab0Sopenharmony_ci * @since 12
397777dab0Sopenharmony_ci * @version 1.0
407777dab0Sopenharmony_ci */
417777dab0Sopenharmony_ci
427777dab0Sopenharmony_ci#include <tee_defines.h>
437777dab0Sopenharmony_ci
447777dab0Sopenharmony_ci#ifdef __cplusplus
457777dab0Sopenharmony_ciextern "C" {
467777dab0Sopenharmony_ci#endif
477777dab0Sopenharmony_ci
487777dab0Sopenharmony_citypedef uint32_t TEE_BigInt;
497777dab0Sopenharmony_citypedef uint32_t TEE_BigIntFMM;
507777dab0Sopenharmony_citypedef uint32_t TEE_BigIntFMMContext;
517777dab0Sopenharmony_ci
527777dab0Sopenharmony_ci/**
537777dab0Sopenharmony_ci * @brief Obtains the size of the array of uint32_t values required to represent a <b>BigInt</b>.
547777dab0Sopenharmony_ci *
557777dab0Sopenharmony_ci * @param n Indicates the <b>TEE_BigInt</b> type.
567777dab0Sopenharmony_ci *
577777dab0Sopenharmony_ci * @return Returns the <b>BigInt</b> size obtained.
587777dab0Sopenharmony_ci *
597777dab0Sopenharmony_ci * @since 12
607777dab0Sopenharmony_ci * @version 1.0
617777dab0Sopenharmony_ci */
627777dab0Sopenharmony_ci#define TEE_BigIntSizeInU32(n) ((((n) + 31) / 32) + 2)
637777dab0Sopenharmony_ci
647777dab0Sopenharmony_ci/**
657777dab0Sopenharmony_ci * @brief Obtains the size of the array of uint32_t values.
667777dab0Sopenharmony_ci *
677777dab0Sopenharmony_ci * @param modulusSizeInBits Indicates the modulus size, in bits.
687777dab0Sopenharmony_ci *
697777dab0Sopenharmony_ci * @return Returns the number of bytes required to store a <b>TEE_BigIntFMM</b>,
707777dab0Sopenharmony_ci * given a modulus of length <b>modSizeInBits</b>.
717777dab0Sopenharmony_ci *
727777dab0Sopenharmony_ci * @since 12
737777dab0Sopenharmony_ci * @version 1.0
747777dab0Sopenharmony_ci */
757777dab0Sopenharmony_cisize_t TEE_BigIntFMMSizeInU32(size_t modulusSizeInBits);
767777dab0Sopenharmony_ci
777777dab0Sopenharmony_ci/**
787777dab0Sopenharmony_ci * @brief Obtains the size of an array of uint32_t values required to represent a fast modular context.
797777dab0Sopenharmony_ci *
807777dab0Sopenharmony_ci * @param modulusSizeInBits Indicates the modulus size, in bits.
817777dab0Sopenharmony_ci *
827777dab0Sopenharmony_ci * @return Returns the number of bytes required to store a <b>TEE_BigIntFMMContext</b>,
837777dab0Sopenharmony_ci * given a modulus of length <b>modSizeInBits</b>.
847777dab0Sopenharmony_ci *
857777dab0Sopenharmony_ci * @since 12
867777dab0Sopenharmony_ci * @version 1.0
877777dab0Sopenharmony_ci */
887777dab0Sopenharmony_cisize_t TEE_BigIntFMMContextSizeInU32(size_t modulusSizeInBits);
897777dab0Sopenharmony_ci
907777dab0Sopenharmony_ci/**
917777dab0Sopenharmony_ci * @brief Initializes a <b>TEE_BigInt</b>.
927777dab0Sopenharmony_ci *
937777dab0Sopenharmony_ci * @param bigInt Indicates the pointer to the <b>TEE_BigInt</b> to initialize.
947777dab0Sopenharmony_ci * @param len Indicates the size of the memory pointed to by <b>TEE_BigInt</b>, in uint32_t.
957777dab0Sopenharmony_ci *
967777dab0Sopenharmony_ci * @since 12
977777dab0Sopenharmony_ci * @version 1.0
987777dab0Sopenharmony_ci */
997777dab0Sopenharmony_civoid TEE_BigIntInit(TEE_BigInt *bigInt, size_t len);
1007777dab0Sopenharmony_ci
1017777dab0Sopenharmony_ci/**
1027777dab0Sopenharmony_ci * @brief Calculates the necessary prerequisites for fast modular multiplication and stores them in a context.
1037777dab0Sopenharmony_ci *
1047777dab0Sopenharmony_ci * @param context Indicates the pointer to the <b>TEE_BigIntFMMContext</b> to initialize.
1057777dab0Sopenharmony_ci * @param len Indicates the size of the memory pointed to by <b>context</b>, in uint32_t.
1067777dab0Sopenharmony_ci * @param modulus Indicates the pointer to the modulus.
1077777dab0Sopenharmony_ci *
1087777dab0Sopenharmony_ci * @since 12
1097777dab0Sopenharmony_ci * @version 1.0
1107777dab0Sopenharmony_ci */
1117777dab0Sopenharmony_civoid TEE_BigIntInitFMMContext(TEE_BigIntFMMContext *context, size_t len, const TEE_BigInt *modulus);
1127777dab0Sopenharmony_ci
1137777dab0Sopenharmony_ci/**
1147777dab0Sopenharmony_ci * @brief Calculates the necessary prerequisites for fast modular multiplication and stores them in a context.
1157777dab0Sopenharmony_ci *
1167777dab0Sopenharmony_ci * @param context Indicates the pointer to the <b>TEE_BigIntFMMContext</b> to initialize.
1177777dab0Sopenharmony_ci * @param len Indicates the size of the memory pointed to by <b>context</b>, in uint32_t.
1187777dab0Sopenharmony_ci * @param modulus Indicates the pointer to the modulus.
1197777dab0Sopenharmony_ci *
1207777dab0Sopenharmony_ci * @return Returns <b>TEE_SUCCESS</b> if the operation is successful.
1217777dab0Sopenharmony_ci *         Returns other values if the operation fails.
1227777dab0Sopenharmony_ci *
1237777dab0Sopenharmony_ci * @since 12
1247777dab0Sopenharmony_ci * @version 1.0
1257777dab0Sopenharmony_ci */
1267777dab0Sopenharmony_ciTEE_Result TEE_BigIntInitFMMContext1(TEE_BigIntFMMContext *context, size_t len, const TEE_BigInt *modulus);
1277777dab0Sopenharmony_ci
1287777dab0Sopenharmony_ci/**
1297777dab0Sopenharmony_ci * @brief Initializes a <b>TEE_BigIntFMM</b> and sets its represented value to zero.
1307777dab0Sopenharmony_ci *
1317777dab0Sopenharmony_ci * @param bigIntFMM Indicates the pointer to the <b>TEE_BigIntFMM</b> to initialize.
1327777dab0Sopenharmony_ci * @param len Indicates the size of the memory pointed to by <b>bigIntFMM</b>, in uint32_t.
1337777dab0Sopenharmony_ci *
1347777dab0Sopenharmony_ci * @since 12
1357777dab0Sopenharmony_ci * @version 1.0
1367777dab0Sopenharmony_ci */
1377777dab0Sopenharmony_civoid TEE_BigIntInitFMM(TEE_BigIntFMM *bigIntFMM, size_t len);
1387777dab0Sopenharmony_ci
1397777dab0Sopenharmony_ci/**
1407777dab0Sopenharmony_ci * @brief Converts an octet string buffer into the <b>TEE_BigInt</b> format.
1417777dab0Sopenharmony_ci *
1427777dab0Sopenharmony_ci * @param dest Indicates the pointer to the <b>TEE_BigInt</b> that holds the result.
1437777dab0Sopenharmony_ci * @param buffer Indicates the pointer to the buffer that holds the octet string representation of the integer.
1447777dab0Sopenharmony_ci * @param bufferLen Indicates the buffer length, in bytes.
1457777dab0Sopenharmony_ci * @param sign Indicates the sign of <b>dest</b>, which is set to the sign of <b>sign</b>.
1467777dab0Sopenharmony_ci *
1477777dab0Sopenharmony_ci * @return Returns <b>TEE_SUCCESS</b> if the operation is successful.
1487777dab0Sopenharmony_ci *         Returns <b>TEE_ERROR_OVERFLOW</b> if the memory allocated for <b>dest</b> is too small.
1497777dab0Sopenharmony_ci *
1507777dab0Sopenharmony_ci * @since 12
1517777dab0Sopenharmony_ci * @version 1.0
1527777dab0Sopenharmony_ci */
1537777dab0Sopenharmony_ciTEE_Result TEE_BigIntConvertFromOctetString(TEE_BigInt *dest, const uint8_t *buffer, size_t bufferLen, int32_t sign);
1547777dab0Sopenharmony_ci
1557777dab0Sopenharmony_ci/**
1567777dab0Sopenharmony_ci * @brief Converts the absolute value of an integer in <b>TEE_BigInt</b> format into an octet string.
1577777dab0Sopenharmony_ci *
1587777dab0Sopenharmony_ci * @param buffer Indicates the pointer to the output buffer that holds the converted octet string representation
1597777dab0Sopenharmony_ci * of the integer.
1607777dab0Sopenharmony_ci * @param bufferLen Indicates the pointer to the buffer length, in bytes.
1617777dab0Sopenharmony_ci * @param bigInt Indicates the pointer to the integer to convert.
1627777dab0Sopenharmony_ci *
1637777dab0Sopenharmony_ci * @return Returns <b>TEE_SUCCESS</b> if the operation is successful.
1647777dab0Sopenharmony_ci *         Returns <b>TEE_ERROR_SHORT_BUFFER</b> if the output buffer is too small to hold the octet string.
1657777dab0Sopenharmony_ci *
1667777dab0Sopenharmony_ci * @since 12
1677777dab0Sopenharmony_ci * @version 1.0
1687777dab0Sopenharmony_ci */
1697777dab0Sopenharmony_ciTEE_Result TEE_BigIntConvertToOctetString(void *buffer, size_t *bufferLen, const TEE_BigInt *bigInt);
1707777dab0Sopenharmony_ci
1717777dab0Sopenharmony_ci/**
1727777dab0Sopenharmony_ci * @brief Sets <b>dest</b> to the value <b>shortVal</b>.
1737777dab0Sopenharmony_ci *
1747777dab0Sopenharmony_ci * @param dest Indicates the pointer to the <b>TEE_BigInt</b> that holds the result.
1757777dab0Sopenharmony_ci * @param shortVal Indicates the value to set.
1767777dab0Sopenharmony_ci *
1777777dab0Sopenharmony_ci * @since 12
1787777dab0Sopenharmony_ci * @version 1.0
1797777dab0Sopenharmony_ci */
1807777dab0Sopenharmony_civoid TEE_BigIntConvertFromS32(TEE_BigInt *dest, int32_t shortVal);
1817777dab0Sopenharmony_ci
1827777dab0Sopenharmony_ci/**
1837777dab0Sopenharmony_ci * @brief Sets <b>dest</b> to the value of <b>src</b>, including the sign of <b>src</b>.
1847777dab0Sopenharmony_ci *
1857777dab0Sopenharmony_ci * @param dest Indicates the pointer to the <b> int32_t</b> that holds the result.
1867777dab0Sopenharmony_ci * @param src Indicates the pointer to the value to set.
1877777dab0Sopenharmony_ci *
1887777dab0Sopenharmony_ci * @return Returns <b>TEE_SUCCESS</b> if the operation is successful.
1897777dab0Sopenharmony_ci *         Returns <b>TEE_ERROR_OVERFLOW</b> if <b>src</b> does not fit within an <b> int32_t</b>.
1907777dab0Sopenharmony_ci *
1917777dab0Sopenharmony_ci * @since 12
1927777dab0Sopenharmony_ci * @version 1.0
1937777dab0Sopenharmony_ci */
1947777dab0Sopenharmony_ciTEE_Result TEE_BigIntConvertToS32(int32_t *dest, const TEE_BigInt *src);
1957777dab0Sopenharmony_ci
1967777dab0Sopenharmony_ci/**
1977777dab0Sopenharmony_ci * @brief Checks whether op1 > op2, op1 == op2, or op1 < op2.
1987777dab0Sopenharmony_ci *
1997777dab0Sopenharmony_ci * @param op1 Indicates the pointer to the first operand.
2007777dab0Sopenharmony_ci * @param op2 Indicates the pointer to the second operand.
2017777dab0Sopenharmony_ci *
2027777dab0Sopenharmony_ci * @return Returns <b>0</b> if op1 == op2.
2037777dab0Sopenharmony_ci *         Returns a positive number if op1 > op2.
2047777dab0Sopenharmony_ci *
2057777dab0Sopenharmony_ci * @since 12
2067777dab0Sopenharmony_ci * @version 1.0
2077777dab0Sopenharmony_ci */
2087777dab0Sopenharmony_ciint32_t TEE_BigIntCmp(const TEE_BigInt *op1, const TEE_BigInt *op2);
2097777dab0Sopenharmony_ci
2107777dab0Sopenharmony_ci/**
2117777dab0Sopenharmony_ci * @brief Checks whether op > shortVal, op == shortVal, or op < shortVal.
2127777dab0Sopenharmony_ci *
2137777dab0Sopenharmony_ci * @param op Indicates the pointer to the first operand.
2147777dab0Sopenharmony_ci * @param shortVal Indicates the pointer to the second operand.
2157777dab0Sopenharmony_ci *
2167777dab0Sopenharmony_ci * @return Returns <b>0</b> if op1 == shortVal.
2177777dab0Sopenharmony_ci *         Returns a positive number if op1 > shortVal.
2187777dab0Sopenharmony_ci *
2197777dab0Sopenharmony_ci * @since 12
2207777dab0Sopenharmony_ci * @version 1.0
2217777dab0Sopenharmony_ci */
2227777dab0Sopenharmony_ciint32_t TEE_BigIntCmpS32(const TEE_BigInt *op, int32_t shortVal);
2237777dab0Sopenharmony_ci
2247777dab0Sopenharmony_ci/**
2257777dab0Sopenharmony_ci * @brief Computes |dest| = |op| >> bits.
2267777dab0Sopenharmony_ci *
2277777dab0Sopenharmony_ci * @param dest Indicates the pointer to the <b>TEE_BigInt</b> that holds the shifted result.
2287777dab0Sopenharmony_ci * @param op Indicates the pointer to the operand to be shifted.
2297777dab0Sopenharmony_ci * @param bits Indicates the number of bits to shift.
2307777dab0Sopenharmony_ci *
2317777dab0Sopenharmony_ci * @since 12
2327777dab0Sopenharmony_ci * @version 1.0
2337777dab0Sopenharmony_ci */
2347777dab0Sopenharmony_civoid TEE_BigIntShiftRight(TEE_BigInt *dest, const TEE_BigInt *op, size_t bits);
2357777dab0Sopenharmony_ci
2367777dab0Sopenharmony_ci/**
2377777dab0Sopenharmony_ci * @brief Obtains the <b>bitIndex</b> bit of the natural binary representation of |src|.
2387777dab0Sopenharmony_ci *
2397777dab0Sopenharmony_ci * @param src Indicates the pointer to the integer.
2407777dab0Sopenharmony_ci * @param bitIndex Indicates the offset of the bit to read, starting from offset <b>0</b> of the least significant bit.
2417777dab0Sopenharmony_ci *
2427777dab0Sopenharmony_ci * @return Returns the Boolean value of <b>bitIndexth</b> in |src|. The value <b>true</b> represents a <b>1</b>,
2437777dab0Sopenharmony_ci * and <b>false</b> represents a <b>0</b>.
2447777dab0Sopenharmony_ci *
2457777dab0Sopenharmony_ci * @since 12
2467777dab0Sopenharmony_ci * @version 1.0
2477777dab0Sopenharmony_ci */
2487777dab0Sopenharmony_cibool TEE_BigIntGetBit(const TEE_BigInt *src, uint32_t bitIndex);
2497777dab0Sopenharmony_ci
2507777dab0Sopenharmony_ci/**
2517777dab0Sopenharmony_ci * @brief Obtains the number of bits in the natural binary representation of |src|,
2527777dab0Sopenharmony_ci * that is, the magnitude of <b>src</b>.
2537777dab0Sopenharmony_ci *
2547777dab0Sopenharmony_ci * @param src Indicates the pointer to the integer.
2557777dab0Sopenharmony_ci *
2567777dab0Sopenharmony_ci * @return Returns <b>0</b> if <b>src</b> is <b>0</b>.
2577777dab0Sopenharmony_ci *         Returns the number of bits in the natural binary representation of <b>src</b>.
2587777dab0Sopenharmony_ci *
2597777dab0Sopenharmony_ci * @since 12
2607777dab0Sopenharmony_ci * @version 1.0
2617777dab0Sopenharmony_ci */
2627777dab0Sopenharmony_ciuint32_t TEE_BigIntGetBitCount(const TEE_BigInt *src);
2637777dab0Sopenharmony_ci
2647777dab0Sopenharmony_ci#if defined(API_LEVEL) && (API_LEVEL >= API_LEVEL1_2)
2657777dab0Sopenharmony_ci/**
2667777dab0Sopenharmony_ci * @brief Sets the first bit of <b>bitIndex</b> in the natural binary representation of <b>op</b> to
2677777dab0Sopenharmony_ci * <b>1</b> or <b>0</b>.
2687777dab0Sopenharmony_ci *
2697777dab0Sopenharmony_ci * @param op Indicates the pointer to the integer.
2707777dab0Sopenharmony_ci * @param bitIndex Indicates the offset of the bit to set, starting from offset <b>0</b> of the least significant bit.
2717777dab0Sopenharmony_ci * @param value Indicates the bit value to set. The value <b>true</b> represents a <b>1</b>, and the value <b>false</b>
2727777dab0Sopenharmony_ci * represents a <b>0</b>.
2737777dab0Sopenharmony_ci *
2747777dab0Sopenharmony_ci * @return Returns <b>TEE_SUCCESS</b> if the operation is successful.
2757777dab0Sopenharmony_ci *         Returns <b>TEE_ERROR_OVERFLOW bitIndexth</b> if the <b>bitIndexth</b> bit is larger than the allocated bit
2767777dab0Sopenharmony_ci * length of <b>op</b>.
2777777dab0Sopenharmony_ci *
2787777dab0Sopenharmony_ci * @since 12
2797777dab0Sopenharmony_ci * @version 1.0
2807777dab0Sopenharmony_ci */
2817777dab0Sopenharmony_ciTEE_Result TEE_BigIntSetBit(TEE_BigInt *op, uint32_t bitIndex, bool value);
2827777dab0Sopenharmony_ci
2837777dab0Sopenharmony_ci/**
2847777dab0Sopenharmony_ci * @brief Assigns the value of <b>src</b> to <b>dest</b>.
2857777dab0Sopenharmony_ci *
2867777dab0Sopenharmony_ci * @param dest Indicates the pointer to the <b>TEE_BigInt</b> to be assigned.
2877777dab0Sopenharmony_ci * @param src Indicates the pointer to the source operand.
2887777dab0Sopenharmony_ci *
2897777dab0Sopenharmony_ci * @return Returns <b>TEE_SUCCESS</b> if the operation is successful.
2907777dab0Sopenharmony_ci *         Returns <b>TEE_ERROR_OVERFLOW</b> if the <b>dest</b> operand cannot hold the value of <b>src</b>.
2917777dab0Sopenharmony_ci *
2927777dab0Sopenharmony_ci * @since 12
2937777dab0Sopenharmony_ci * @version 1.0
2947777dab0Sopenharmony_ci */
2957777dab0Sopenharmony_ciTEE_Result TEE_BigIntAssign(TEE_BigInt *dest, const TEE_BigInt *src);
2967777dab0Sopenharmony_ci
2977777dab0Sopenharmony_ci/**
2987777dab0Sopenharmony_ci * @brief Assigns the value of <b>src</b> to <b>dest</b>.
2997777dab0Sopenharmony_ci *
3007777dab0Sopenharmony_ci * @param dest Indicates the pointer to the <b>TEE_BigInt</b> to be assigned.
3017777dab0Sopenharmony_ci * @param src Indicates the pointer to the source operand.
3027777dab0Sopenharmony_ci *
3037777dab0Sopenharmony_ci * @return Returns <b>TEE_SUCCESS</b> if the operation is successful.
3047777dab0Sopenharmony_ci *         Returns <b>TEE_ERROR_OVERFLOW</b> if the <b>dest</b> operand cannot hold the value of <b>src</b>.
3057777dab0Sopenharmony_ci *
3067777dab0Sopenharmony_ci * @since 12
3077777dab0Sopenharmony_ci * @version 1.0
3087777dab0Sopenharmony_ci */
3097777dab0Sopenharmony_ciTEE_Result TEE_BigIntAbs(TEE_BigInt *dest, const TEE_BigInt *src);
3107777dab0Sopenharmony_ci#endif /* API_LEVEL */
3117777dab0Sopenharmony_ci
3127777dab0Sopenharmony_ci/**
3137777dab0Sopenharmony_ci * @brief Computes dest = op1 + op2.
3147777dab0Sopenharmony_ci *
3157777dab0Sopenharmony_ci * @param dest Indicates the pointer to the <b>TEE_BigInt</b> that holds the sum of <b>op1</b> and <b>op2</b>.
3167777dab0Sopenharmony_ci * @param op1 Indicates the pointer to the first operand.
3177777dab0Sopenharmony_ci * @param op2 Indicates the pointer to the second operand.
3187777dab0Sopenharmony_ci *
3197777dab0Sopenharmony_ci * @since 12
3207777dab0Sopenharmony_ci * @version 1.0
3217777dab0Sopenharmony_ci */
3227777dab0Sopenharmony_civoid TEE_BigIntAdd(TEE_BigInt *dest, const TEE_BigInt *op1, const TEE_BigInt *op2);
3237777dab0Sopenharmony_ci
3247777dab0Sopenharmony_ci/**
3257777dab0Sopenharmony_ci * @brief Computes dest = op1 – op2.
3267777dab0Sopenharmony_ci *
3277777dab0Sopenharmony_ci * @param dest Indicates the pointer to the <b>TEE_BigInt</b> that holds the difference between <b>op1</b>
3287777dab0Sopenharmony_ci * and <b>op2</b>.
3297777dab0Sopenharmony_ci * @param op1 Indicates the pointer to the first operand.
3307777dab0Sopenharmony_ci * @param op2 Indicates the pointer to the second operand.
3317777dab0Sopenharmony_ci *
3327777dab0Sopenharmony_ci * @since 12
3337777dab0Sopenharmony_ci * @version 1.0
3347777dab0Sopenharmony_ci */
3357777dab0Sopenharmony_civoid TEE_BigIntSub(TEE_BigInt *dest, const TEE_BigInt *op1, const TEE_BigInt *op2);
3367777dab0Sopenharmony_ci
3377777dab0Sopenharmony_ci/**
3387777dab0Sopenharmony_ci * @brief Negates an operand: dest = –op.
3397777dab0Sopenharmony_ci *
3407777dab0Sopenharmony_ci * @param dest Indicates the pointer to the <b>TEE_BigInt</b> that holds the result –op.
3417777dab0Sopenharmony_ci * @param op Indicates the pointer to the operand to be negated.
3427777dab0Sopenharmony_ci *
3437777dab0Sopenharmony_ci * @since 12
3447777dab0Sopenharmony_ci * @version 1.0
3457777dab0Sopenharmony_ci */
3467777dab0Sopenharmony_civoid TEE_BigIntNeg(TEE_BigInt *dest, const TEE_BigInt *op);
3477777dab0Sopenharmony_ci
3487777dab0Sopenharmony_ci/**
3497777dab0Sopenharmony_ci * @brief Computes dest = op1 * op2.
3507777dab0Sopenharmony_ci *
3517777dab0Sopenharmony_ci * @param dest Indicates the pointer to the <b>TEE_BigInt</b> that holds the product of <b>op1</b> and <b>op2</b>.
3527777dab0Sopenharmony_ci * @param op1 Indicates the pointer to the first operand.
3537777dab0Sopenharmony_ci * @param op2 Indicates the pointer to the second operand.
3547777dab0Sopenharmony_ci *
3557777dab0Sopenharmony_ci * @since 12
3567777dab0Sopenharmony_ci * @version 1.0
3577777dab0Sopenharmony_ci */
3587777dab0Sopenharmony_civoid TEE_BigIntMul(TEE_BigInt *dest, const TEE_BigInt *op1, const TEE_BigInt *op2);
3597777dab0Sopenharmony_ci
3607777dab0Sopenharmony_ci/**
3617777dab0Sopenharmony_ci * @brief Computes dest = op * op.
3627777dab0Sopenharmony_ci *
3637777dab0Sopenharmony_ci * @param dest Indicates the pointer to the <b>TEE_BigInt</b> that holds the result op * op.
3647777dab0Sopenharmony_ci * @param op Indicates the pointer to the operand to be squared.
3657777dab0Sopenharmony_ci *
3667777dab0Sopenharmony_ci * @since 12
3677777dab0Sopenharmony_ci * @version 1.0
3687777dab0Sopenharmony_ci */
3697777dab0Sopenharmony_civoid TEE_BigIntSquare(TEE_BigInt *dest, const TEE_BigInt *op);
3707777dab0Sopenharmony_ci
3717777dab0Sopenharmony_ci/**
3727777dab0Sopenharmony_ci * @brief Computes <b>dest_r</b> and <b>dest_q</b> to make op1 = dest_q* op2 + dest_r.
3737777dab0Sopenharmony_ci *
3747777dab0Sopenharmony_ci * @param dest_q Indicates the pointer to the <b>TEE_BigInt</b> that holds the quotient.
3757777dab0Sopenharmony_ci * @param dest_r Indicates the pointer to the <b>TEE_BigInt</b> that holds the remainder.
3767777dab0Sopenharmony_ci * @param op1 Indicates the pointer to the first operand, which is the dividend.
3777777dab0Sopenharmony_ci * @param op2 Indicates the pointer to the second operand, which is the divisor.
3787777dab0Sopenharmony_ci *
3797777dab0Sopenharmony_ci * @return Returns <b>TEE_SUCCESS</b> if the operation is successful.
3807777dab0Sopenharmony_ci *         Returns <b>TEE_ERROR_BAD_PARAMETERS</b> if at least one parameter is null.
3817777dab0Sopenharmony_ci *
3827777dab0Sopenharmony_ci * @since 12
3837777dab0Sopenharmony_ci * @version 1.0
3847777dab0Sopenharmony_ci */
3857777dab0Sopenharmony_civoid TEE_BigIntDiv(TEE_BigInt *dest_q, TEE_BigInt *dest_r, const TEE_BigInt *op1, const TEE_BigInt *op2);
3867777dab0Sopenharmony_ci
3877777dab0Sopenharmony_ci/**
3887777dab0Sopenharmony_ci * @brief Computes dest = op (mod n) to make 0 <= dest < n.
3897777dab0Sopenharmony_ci *
3907777dab0Sopenharmony_ci * @param dest Indicates the pointer to the <b>TEE_BigInt</b> that holds the result op (mod n).
3917777dab0Sopenharmony_ci * @param op Indicates the pointer to the operand to be reduced mod n.
3927777dab0Sopenharmony_ci * @param n [IN] Indicates the pointer to the modulus, which must be greater than 1.
3937777dab0Sopenharmony_ci *
3947777dab0Sopenharmony_ci * @since 12
3957777dab0Sopenharmony_ci * @version 1.0
3967777dab0Sopenharmony_ci */
3977777dab0Sopenharmony_civoid TEE_BigIntMod(TEE_BigInt *dest, const TEE_BigInt *op, const TEE_BigInt *n);
3987777dab0Sopenharmony_ci
3997777dab0Sopenharmony_ci/**
4007777dab0Sopenharmony_ci * @brief Computes dest = (op1 + op2) (mod n).
4017777dab0Sopenharmony_ci *
4027777dab0Sopenharmony_ci * @param dest Indicates the pointer to the <b>TEE_BigInt</b> that holds the result op (op1 + op2)(mod n).
4037777dab0Sopenharmony_ci * @param op1 Indicates the pointer to the first operand.
4047777dab0Sopenharmony_ci * @param op2 Indicates the pointer to the second operand.
4057777dab0Sopenharmony_ci * @param n Indicates the pointer to the modulus, which must be greater than 1.
4067777dab0Sopenharmony_ci *
4077777dab0Sopenharmony_ci * @since 12
4087777dab0Sopenharmony_ci * @version 1.0
4097777dab0Sopenharmony_ci */
4107777dab0Sopenharmony_civoid TEE_BigIntAddMod(TEE_BigInt *dest, const TEE_BigInt *op1, const TEE_BigInt *op2, const TEE_BigInt *n);
4117777dab0Sopenharmony_ci
4127777dab0Sopenharmony_ci/**
4137777dab0Sopenharmony_ci * @brief Computes dest = (op1 – op2) (mod n).
4147777dab0Sopenharmony_ci *
4157777dab0Sopenharmony_ci * @param dest Indicates the pointer to the <b>TEE_BigInt</b> that holds the result op (op1 – op2)(mod n).
4167777dab0Sopenharmony_ci * @param op1 Indicates the pointer to the first operand.
4177777dab0Sopenharmony_ci * @param op2 Indicates the pointer to the second operand.
4187777dab0Sopenharmony_ci * @param n Indicates the pointer to the modulus, which must be greater than 1.
4197777dab0Sopenharmony_ci *
4207777dab0Sopenharmony_ci * @since 12
4217777dab0Sopenharmony_ci * @version 1.0
4227777dab0Sopenharmony_ci */
4237777dab0Sopenharmony_civoid TEE_BigIntSubMod(TEE_BigInt *dest, const TEE_BigInt *op1, const TEE_BigInt *op2, const TEE_BigInt *n);
4247777dab0Sopenharmony_ci
4257777dab0Sopenharmony_ci/**
4267777dab0Sopenharmony_ci * @brief Computes dest = (op1* op2)(mod n).
4277777dab0Sopenharmony_ci *
4287777dab0Sopenharmony_ci * @param dest Indicates the pointer to the <b>TEE_BigInt</b> that holds the result op (op1 * op2)(mod n).
4297777dab0Sopenharmony_ci * @param op1 Indicates the pointer to the first operand.
4307777dab0Sopenharmony_ci * @param op2 Indicates the pointer to the second operand.
4317777dab0Sopenharmony_ci * @param n Indicates the pointer to the modulus, which must be greater than 1.
4327777dab0Sopenharmony_ci *
4337777dab0Sopenharmony_ci * @since 12
4347777dab0Sopenharmony_ci * @version 1.0
4357777dab0Sopenharmony_ci */
4367777dab0Sopenharmony_civoid TEE_BigIntMulMod(TEE_BigInt *dest, const TEE_BigInt *op1, const TEE_BigInt *op2, const TEE_BigInt *n);
4377777dab0Sopenharmony_ci
4387777dab0Sopenharmony_ci/**
4397777dab0Sopenharmony_ci * @brief Computes dest = (op * op) (mod n).
4407777dab0Sopenharmony_ci *
4417777dab0Sopenharmony_ci * @param dest Indicates the pointer to the <b>TEE_BigInt</b> that holds the result op (op * op)(mod n).
4427777dab0Sopenharmony_ci * @param op Indicates the pointer to the operand.
4437777dab0Sopenharmony_ci * @param n [IN] Indicates the pointer to the modulus, which must be greater than 1.
4447777dab0Sopenharmony_ci *
4457777dab0Sopenharmony_ci * @since 12
4467777dab0Sopenharmony_ci * @version 1.0
4477777dab0Sopenharmony_ci */
4487777dab0Sopenharmony_civoid TEE_BigIntSquareMod(TEE_BigInt *dest, const TEE_BigInt *op, const TEE_BigInt *n);
4497777dab0Sopenharmony_ci
4507777dab0Sopenharmony_ci/**
4517777dab0Sopenharmony_ci * @brief Computes <b>dest</b> to make dest* op = 1 (mod n).
4527777dab0Sopenharmony_ci *
4537777dab0Sopenharmony_ci * @param dest Indicates the pointer to the <b>TEE_BigInt</b> that holds the result (op^–1)(mod n).
4547777dab0Sopenharmony_ci * @param op Indicates the pointer to the operand.
4557777dab0Sopenharmony_ci * @param n [IN] Indicates the pointer to the modulus, which must be greater than 1.
4567777dab0Sopenharmony_ci *
4577777dab0Sopenharmony_ci * @since 12
4587777dab0Sopenharmony_ci * @version 1.0
4597777dab0Sopenharmony_ci */
4607777dab0Sopenharmony_civoid TEE_BigIntInvMod(TEE_BigInt *dest, const TEE_BigInt *op, const TEE_BigInt *n);
4617777dab0Sopenharmony_ci
4627777dab0Sopenharmony_ci/**
4637777dab0Sopenharmony_ci * @brief Checks whether gcd(op1, op2) == 1.
4647777dab0Sopenharmony_ci *
4657777dab0Sopenharmony_ci * @param op1 Indicates the pointer to the first operand.
4667777dab0Sopenharmony_ci * @param op2 Indicates the pointer to the second operand.
4677777dab0Sopenharmony_ci *
4687777dab0Sopenharmony_ci * @return Returns <b>true</b> if gcd(op1, op2) == 1.
4697777dab0Sopenharmony_ci *         Returns <b>false</b> if gcd(op1, op2) != 1.
4707777dab0Sopenharmony_ci *
4717777dab0Sopenharmony_ci * @since 12
4727777dab0Sopenharmony_ci * @version 1.0
4737777dab0Sopenharmony_ci */
4747777dab0Sopenharmony_cibool TEE_BigIntRelativePrime(const TEE_BigInt *op1, const TEE_BigInt *op2);
4757777dab0Sopenharmony_ci
4767777dab0Sopenharmony_ci/**
4777777dab0Sopenharmony_ci * @brief Computes the greatest common divisor of <b>op1</b> and <b>op2</b>.
4787777dab0Sopenharmony_ci *
4797777dab0Sopenharmony_ci * @param gcd Indicates the pointer to the <b>TEE_BigInt</b> that holds the greatest common divisor of <b>op1</b>
4807777dab0Sopenharmony_ci * and <b>op2</b>.
4817777dab0Sopenharmony_ci * @param u Indicates the pointer to the <b>TEE_BigInt</b> that holds the first coefficient.
4827777dab0Sopenharmony_ci * @param v Indicates the pointer to the <b>TEE_BigInt</b> that holds the second coefficient.
4837777dab0Sopenharmony_ci * @param op1 Indicates the pointer to the first operand.
4847777dab0Sopenharmony_ci * @param op2 Indicates the pointer to the second operand.
4857777dab0Sopenharmony_ci *
4867777dab0Sopenharmony_ci * @since 12
4877777dab0Sopenharmony_ci * @version 1.0
4887777dab0Sopenharmony_ci */
4897777dab0Sopenharmony_civoid TEE_BigIntComputeExtendedGcd(TEE_BigInt *gcd, TEE_BigInt *u, TEE_BigInt *v, const TEE_BigInt *op1,
4907777dab0Sopenharmony_ci                                  const TEE_BigInt *op2);
4917777dab0Sopenharmony_ci/**
4927777dab0Sopenharmony_ci * @brief Performs a probabilistic primality test on <b>op</b>.
4937777dab0Sopenharmony_ci *
4947777dab0Sopenharmony_ci * @param op Indicates the pointer to the candidate number that is tested for primality.
4957777dab0Sopenharmony_ci * @param confidenceLevel Indicates the expected confidence level for a non-conclusive test.
4967777dab0Sopenharmony_ci *
4977777dab0Sopenharmony_ci * @return Returns <b>0</b> if <b>op</b> is a composite number.
4987777dab0Sopenharmony_ci *         Returns <b>1</b> if <b>op</b> is a prime number.
4997777dab0Sopenharmony_ci *         Returns <b>–1</b> if the test is non-conclusive but the probability that <b>op</b> is composite is
5007777dab0Sopenharmony_ci * less than 2^(-confidenceLevel).
5017777dab0Sopenharmony_ci *
5027777dab0Sopenharmony_ci * @since 12
5037777dab0Sopenharmony_ci * @version 1.0
5047777dab0Sopenharmony_ci */
5057777dab0Sopenharmony_ciint32_t TEE_BigIntIsProbablePrime(const TEE_BigInt *op, uint32_t confidenceLevel);
5067777dab0Sopenharmony_ci
5077777dab0Sopenharmony_ci/**
5087777dab0Sopenharmony_ci * @brief Converts <b>src</b> into a representation suitable for doing fast modular multiplication.
5097777dab0Sopenharmony_ci *
5107777dab0Sopenharmony_ci * @param dest Indicates the pointer to an initialized <b>TEE_BigIntFMM</b> memory area.
5117777dab0Sopenharmony_ci * @param src Indicates the pointer to the <b>TEE_BigInt</b> to convert.
5127777dab0Sopenharmony_ci * @param n Indicates the pointer to the modulus.
5137777dab0Sopenharmony_ci * @param context Indicates the pointer to the context that is previously initialized using
5147777dab0Sopenharmony_ci * {@link TEE_BigIntInitFMMContext1}.
5157777dab0Sopenharmony_ci *
5167777dab0Sopenharmony_ci * @since 12
5177777dab0Sopenharmony_ci * @version 1.0
5187777dab0Sopenharmony_ci */
5197777dab0Sopenharmony_civoid TEE_BigIntConvertToFMM(TEE_BigIntFMM *dest, const TEE_BigInt *src, const TEE_BigInt *n,
5207777dab0Sopenharmony_ci                            const TEE_BigIntFMMContext *context);
5217777dab0Sopenharmony_ci
5227777dab0Sopenharmony_ci/**
5237777dab0Sopenharmony_ci * @brief Converts <b>src</b> in the fast modular multiplication representation back to a
5247777dab0Sopenharmony_ci * <b>TEE_BigInt</b> representation.
5257777dab0Sopenharmony_ci *
5267777dab0Sopenharmony_ci * @param dest Indicates the pointer to an initialized <b>TEE_BigIntFMM</b> memory area to store the converted result.
5277777dab0Sopenharmony_ci * @param src Indicates the pointer to a <b>TEE_BigIntFMM</b> holding the value in the fast modular multiplication
5287777dab0Sopenharmony_ci * representation.
5297777dab0Sopenharmony_ci * @param n Indicates the pointer to the modulus.
5307777dab0Sopenharmony_ci * @param context Indicates the pointer to the context that is previously initialized using
5317777dab0Sopenharmony_ci * {@link TEE_BigIntInitFMMContext1}.
5327777dab0Sopenharmony_ci *
5337777dab0Sopenharmony_ci * @since 12
5347777dab0Sopenharmony_ci * @version 1.0
5357777dab0Sopenharmony_ci */
5367777dab0Sopenharmony_civoid TEE_BigIntConvertFromFMM(TEE_BigInt *dest, const TEE_BigIntFMM *src, const TEE_BigInt *n,
5377777dab0Sopenharmony_ci                              const TEE_BigIntFMMContext *context);
5387777dab0Sopenharmony_ci
5397777dab0Sopenharmony_ci/**
5407777dab0Sopenharmony_ci * @brief Computes dest = op1* op2 in the fast modular multiplication representation.
5417777dab0Sopenharmony_ci *
5427777dab0Sopenharmony_ci * @param dest Indicates the pointer to the <b>TEE_BigIntFMM</b> that holds the result op1* op2.
5437777dab0Sopenharmony_ci * @param op1 Indicates the pointer to the first operand.
5447777dab0Sopenharmony_ci * @param op2 Indicates the pointer to the second operand.
5457777dab0Sopenharmony_ci * @param n Indicates the pointer to the modulus.
5467777dab0Sopenharmony_ci * @param context Indicates the pointer to the context that is previously initialized using
5477777dab0Sopenharmony_ci * {@link TEE_BigIntInitFMMContext1}.
5487777dab0Sopenharmony_ci *
5497777dab0Sopenharmony_ci * @since 12
5507777dab0Sopenharmony_ci * @version 1.0
5517777dab0Sopenharmony_ci */
5527777dab0Sopenharmony_civoid TEE_BigIntComputeFMM(TEE_BigIntFMM *dest, const TEE_BigIntFMM *op1, const TEE_BigIntFMM *op2, const TEE_BigInt *n,
5537777dab0Sopenharmony_ci                          const TEE_BigIntFMMContext *context);
5547777dab0Sopenharmony_ci
5557777dab0Sopenharmony_ci/**
5567777dab0Sopenharmony_ci * @brief Computes dest = (op1 ^ op2)(mod n).
5577777dab0Sopenharmony_ci *
5587777dab0Sopenharmony_ci * @param des Indicates the pointer to the <b>TEE_BigInt</b> that holds the result (op1 ^ op2)(mod n).
5597777dab0Sopenharmony_ci * @param op1 Indicates the pointer to the first operand.
5607777dab0Sopenharmony_ci * @param op2 Indicates the pointer to the second operand.
5617777dab0Sopenharmony_ci * @param n Indicates the pointer to the modulus.
5627777dab0Sopenharmony_ci * @param context Indicates the pointer to the context that is previously initialized using
5637777dab0Sopenharmony_ci * {@link TEE_BigIntInitFMMContext1} or initialized to null.
5647777dab0Sopenharmony_ci *
5657777dab0Sopenharmony_ci * @return Returns <b>TEE_SUCCESS</b> if the operation is successful.
5667777dab0Sopenharmony_ci *         Returns <b>TEE_ERROR_NOT_SUPPORTED</b> if the value of <b>n</b> is not supported.
5677777dab0Sopenharmony_ci *
5687777dab0Sopenharmony_ci * @since 12
5697777dab0Sopenharmony_ci * @version 1.0
5707777dab0Sopenharmony_ci */
5717777dab0Sopenharmony_ciTEE_Result TEE_BigIntExpMod(TEE_BigInt *des, TEE_BigInt *op1, const TEE_BigInt *op2, const TEE_BigInt *n,
5727777dab0Sopenharmony_ci                            TEE_BigIntFMMContext *context);
5737777dab0Sopenharmony_ci
5747777dab0Sopenharmony_ci#ifdef __cplusplus
5757777dab0Sopenharmony_ci}
5767777dab0Sopenharmony_ci#endif
5777777dab0Sopenharmony_ci/** @} */
5787777dab0Sopenharmony_ci#endif
579