1 /*
2 * Copyright (C) 2022 Huawei Technologies Co., Ltd.
3 * Licensed under the Mulan PSL v2.
4 * You can use this software according to the terms and conditions of the Mulan PSL v2.
5 * You may obtain a copy of Mulan PSL v2 at:
6 * http://license.coscl.org.cn/MulanPSL2
7 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
8 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
9 * PURPOSE.
10 * See the Mulan PSL v2 for more details.
11 */
12 #include "tee_arith_api.h"
13 #include <pthread.h>
14 #include <openssl/bn.h>
15 #include <securec.h>
16 #include <tee_log.h>
17 #include <tee_mem_mgmt_api.h>
18 #include <tee_log.h>
19 #include <ta_framework.h>
20
21 #define METADATA_SIZE_IN_U32 2
22 #define BN_DIGIT_OFFSET METADATA_SIZE_IN_U32
23 #define BYTE_LEN_PADD 3
24 #define BYTE_LEN_U32S(a) (((a) + BYTE_LEN_PADD) / sizeof(uint32_t))
25 #define BN_LEN(a) ((a)[BIG_INT_BYTES_OFFSET] & 0xffff)
26 #define BN_SIGN(a) ((a)[BIG_INT_BYTES_OFFSET] >> 16)
27 #define BIGINT_MEM_POOL_SIZE 12
28 #define SYMBOL_BIT 16
29 #define BYTE_SIZE 8
30 #define BYTE_SHIFT 7
31 #define TRIAL_DIVISION 1
32
33 #define BIG_INT_ERROR (-1)
34 #define BIG_INT_SUCCESS 0
35 #define BIG_INT_ALLOC_OFFSET 0
36 #define BIG_INT_BYTES_OFFSET 1
37 #define BIG_INT_POSITIVE_FLAG 0
38 #define BIG_INT_NEGITIVE_FLAG 1
39 #define BIG_INT_INIT_SIZE 0
40 #define BIG_INT_INIT_VALUE 1
41 #define BN_INVALID_LEN (-1)
42 #define BN_SET_WORD_SUCC 1
43 #define BN_OP_SUCC 1
44
45 struct bn_mem_pool_t {
46 BIGNUM *a;
47 BIGNUM *b;
48 BIGNUM *c;
49 BIGNUM *d;
50 BIGNUM *e;
51 BN_CTX *ctx;
52 BN_MONT_CTX *mont_ctx;
53 };
54
55 /* overwrite GP standard interface, enable it only in GP certificate */
56 #ifndef SUPPORT_GP_PANIC
57 #define TEE_Panic(x) \
58 do { \
59 } while (0)
60 #endif
61
62 static void release_mem_pool(struct bn_mem_pool_t *pool);
reserve_mem_pool(void)63 static struct bn_mem_pool_t *reserve_mem_pool(void)
64 {
65 struct bn_mem_pool_t *pool = TEE_Malloc(sizeof(*pool), 0);
66 if (pool == NULL) {
67 tloge("malloc mem pool failed!");
68 return NULL;
69 }
70 pool->a = BN_new();
71 pool->b = BN_new();
72 pool->c = BN_new();
73 pool->d = BN_new();
74 pool->e = BN_new();
75 pool->ctx = BN_CTX_new();
76 pool->mont_ctx = BN_MONT_CTX_new();
77 bool check = ((pool->a == NULL) || (pool->b == NULL) || (pool->c == NULL) || (pool->d == NULL) ||
78 (pool->e == NULL) || (pool->ctx == NULL) || (pool->mont_ctx == NULL));
79 if (check) {
80 release_mem_pool(pool);
81 return NULL;
82 }
83 return pool;
84 }
85
release_mem_pool(struct bn_mem_pool_t * pool)86 static void release_mem_pool(struct bn_mem_pool_t *pool)
87 {
88 if (pool != NULL) {
89 BN_free(pool->a);
90 pool->a = NULL;
91 BN_free(pool->b);
92 pool->b = NULL;
93 BN_free(pool->c);
94 pool->c = NULL;
95 BN_free(pool->d);
96 pool->d = NULL;
97 BN_free(pool->e);
98 pool->e = NULL;
99 BN_CTX_free(pool->ctx);
100 pool->ctx = NULL;
101 BN_MONT_CTX_free(pool->mont_ctx);
102 pool->mont_ctx = NULL;
103 TEE_Free(pool);
104 pool = NULL;
105 }
106 }
107
invalid_big_int_len(size_t len)108 static bool invalid_big_int_len(size_t len)
109 {
110 bool check = ((len < METADATA_SIZE_IN_U32) || ((len - METADATA_SIZE_IN_U32) > (UINT32_MAX / sizeof(uint32_t))));
111 if (check)
112 return true;
113
114 return false;
115 }
116
big_int_to_bn(BIGNUM * bn,const TEE_BigInt * big_int,uint32_t len)117 static int32_t big_int_to_bn(BIGNUM *bn, const TEE_BigInt *big_int, uint32_t len)
118 {
119 bool check = ((bn == NULL) || (big_int == NULL) || (len <= METADATA_SIZE_IN_U32));
120 if (check)
121 return BIG_INT_ERROR;
122
123 if (BN_bin2bn((uint8_t *)&big_int[BN_DIGIT_OFFSET], BN_LEN(big_int), bn) == NULL)
124 return BIG_INT_ERROR;
125
126 if (BN_SIGN(big_int) != BIG_INT_POSITIVE_FLAG)
127 BN_set_negative(bn, BIG_INT_NEGITIVE_FLAG);
128
129 return BIG_INT_SUCCESS;
130 }
131
bn_to_big_int(TEE_BigInt * big_int,uint32_t len,const BIGNUM * bn)132 static int32_t bn_to_big_int(TEE_BigInt *big_int, uint32_t len, const BIGNUM *bn)
133 {
134 uint32_t blen;
135
136 bool check = ((bn == NULL) || (big_int == NULL) || (len <= METADATA_SIZE_IN_U32) ||
137 invalid_big_int_len((size_t)big_int[BIG_INT_ALLOC_OFFSET]));
138 if (check)
139 return BIG_INT_ERROR;
140
141 /* check that bn fits into bigInt */
142 blen = BN_num_bytes(bn);
143 if (blen > (sizeof(uint32_t) * (big_int[BIG_INT_ALLOC_OFFSET] - METADATA_SIZE_IN_U32)))
144 return BIG_INT_ERROR;
145
146 if (BN_bn2bin(bn, (uint8_t *)&big_int[BN_DIGIT_OFFSET]) == BN_INVALID_LEN)
147 return BIG_INT_ERROR;
148
149 big_int[BIG_INT_BYTES_OFFSET] = blen;
150 if (BN_is_negative(bn))
151 big_int[BIG_INT_BYTES_OFFSET] |= ((uint32_t)BIG_INT_NEGITIVE_FLAG << SYMBOL_BIT);
152
153 return BIG_INT_SUCCESS;
154 }
155
156 /*
157 * below APIs are defined by Global Platform, need to follow Global Platform code style
158 * don't change function name / return value type / parameters types / parameters names
159 */
160 /*
161 * TEE_BigInt representation is following: a0, a1, a2, ..., an, where
162 * a0 is number of allocated uint32s
163 * a1 the a1&0xffff is number of bytes for bigint-> a1>>16 is sign of bigint if a1>>16 = 0 then number is positive
164 * othervice negative
165 * a2,...,an contains digits in bytes (Note: this a1 has number in uint32s)
166 */
TEE_BigIntInit(TEE_BigInt * bigInt,size_t len)167 void TEE_BigIntInit(TEE_BigInt *bigInt, size_t len)
168 {
169 bool check = ((bigInt == NULL) || invalid_big_int_len(len));
170 if (check) {
171 TEE_Panic(TEE_ERROR_BAD_PARAMETERS);
172 return;
173 }
174 bigInt[BIG_INT_ALLOC_OFFSET] = len;
175 bigInt[BIG_INT_BYTES_OFFSET] = BIG_INT_INIT_SIZE;
176 return;
177 }
178
TEE_BigIntInitFMM(TEE_BigIntFMM * bigIntFMM,size_t len)179 void TEE_BigIntInitFMM(TEE_BigIntFMM *bigIntFMM, size_t len)
180 {
181 TEE_BigIntInit(bigIntFMM, len);
182 }
183
TEE_BigIntInitFMMContext1(TEE_BigIntFMMContext * context,size_t len,const TEE_BigInt * modulus)184 TEE_Result TEE_BigIntInitFMMContext1(TEE_BigIntFMMContext *context, size_t len, const TEE_BigInt *modulus)
185 {
186 errno_t ret;
187 uint32_t modlen;
188
189 bool check = ((context == NULL) || (modulus == NULL) || invalid_big_int_len(len));
190 if (check) {
191 tloge("parameters is invalid!");
192 return TEE_ERROR_BAD_PARAMETERS;
193 }
194
195 modlen = BN_LEN(modulus);
196 if (((len - METADATA_SIZE_IN_U32) * sizeof(uint32_t)) < modlen) {
197 TEE_Panic(TEE_ERROR_NOT_SUPPORTED);
198 return TEE_ERROR_NOT_SUPPORTED;
199 }
200 context[BIG_INT_ALLOC_OFFSET] = len - METADATA_SIZE_IN_U32;
201 context[BIG_INT_BYTES_OFFSET] = modulus[BIG_INT_BYTES_OFFSET];
202 ret = memcpy_s(&context[BN_DIGIT_OFFSET], (len - METADATA_SIZE_IN_U32) * sizeof(uint32_t),
203 &modulus[BN_DIGIT_OFFSET], modlen);
204 if (ret != EOK) {
205 tloge("copy data failed\n");
206 return TEE_ERROR_SECURITY;
207 }
208
209 return TEE_SUCCESS;
210 }
211
TEE_BigIntInitFMMContext(TEE_BigIntFMMContext * context,size_t len,const TEE_BigInt * modulus)212 void TEE_BigIntInitFMMContext(TEE_BigIntFMMContext *context, size_t len, const TEE_BigInt *modulus)
213 {
214 if (TEE_BigIntInitFMMContext1(context, len, modulus) != TEE_SUCCESS)
215 tloge("init big int context failed\n");
216 }
217
TEE_BigIntFMMSizeInU32(size_t modulusSizeInBits)218 size_t TEE_BigIntFMMSizeInU32(size_t modulusSizeInBits)
219 {
220 return TEE_BigIntSizeInU32(modulusSizeInBits);
221 }
222
TEE_BigIntFMMContextSizeInU32(size_t modulusSizeInBits)223 size_t TEE_BigIntFMMContextSizeInU32(size_t modulusSizeInBits)
224 {
225 return TEE_BigIntSizeInU32(modulusSizeInBits);
226 }
227
TEE_BigIntConvertFromOctetString(TEE_BigInt * dest,const uint8_t * buffer,size_t bufferLen,int32_t sign)228 TEE_Result TEE_BigIntConvertFromOctetString(TEE_BigInt *dest, const uint8_t *buffer, size_t bufferLen, int32_t sign)
229 {
230 size_t len;
231 errno_t rc;
232
233 bool check = ((dest == NULL) || (buffer == NULL) || invalid_big_int_len((size_t)dest[BIG_INT_ALLOC_OFFSET]));
234 if (check) {
235 tloge("parameters is invalid!\n");
236 return TEE_ERROR_BAD_PARAMETERS;
237 }
238
239 while ((bufferLen > 0) && (buffer[BIG_INT_ALLOC_OFFSET] == 0)) {
240 buffer++;
241 bufferLen--;
242 }
243
244 len = (dest[BIG_INT_ALLOC_OFFSET] - METADATA_SIZE_IN_U32) * sizeof(uint32_t);
245 if (len < bufferLen) {
246 tloge("buffer over flow, bigInt len %zu, bufferLen %zu\n", len, bufferLen);
247 return TEE_ERROR_OVERFLOW;
248 }
249
250 /*
251 * dest[BIG_INT_ALLOC_OFFSET] tells length of 1) number and 2) METADATA in UINT32s. The from &dest[2] there is
252 * available space 4 * (dest[BIG_INT_ALLOC_OFFSET] - METADATA_SIZE_IN_U32) bytes
253 */
254 rc = memcpy_s(&dest[BN_DIGIT_OFFSET], len, buffer, bufferLen);
255 if (rc != EOK) {
256 tloge("copy data failed\n");
257 return TEE_ERROR_SECURITY;
258 }
259 dest[BIG_INT_BYTES_OFFSET] = bufferLen;
260 if (sign < BIG_INT_POSITIVE_FLAG)
261 dest[BIG_INT_BYTES_OFFSET] |= ((uint32_t)BIG_INT_NEGITIVE_FLAG << SYMBOL_BIT);
262 return TEE_SUCCESS;
263 }
264
TEE_BigIntConvertToOctetString(void * buffer,size_t * bufferLen,const TEE_BigInt * bigInt)265 TEE_Result TEE_BigIntConvertToOctetString(void *buffer, size_t *bufferLen, const TEE_BigInt *bigInt)
266 {
267 errno_t ret;
268 bool check = ((buffer == NULL) || (bufferLen == NULL) || (bigInt == NULL));
269 if (check) {
270 tloge("parameters is invalid\n");
271 return TEE_ERROR_BAD_PARAMETERS;
272 }
273
274 if (*bufferLen < BN_LEN(bigInt)) {
275 tloge("bufferLen too short, dest len %zu, bigInt len %u\n", *bufferLen, BN_LEN(bigInt));
276 return TEE_ERROR_SHORT_BUFFER;
277 }
278
279 /* *bufferLen is INPUT and OUTPUT, it tells how much buffer has memory and BN_LEN(bigInt) will be written */
280 ret = memcpy_s(buffer, *bufferLen, &bigInt[BN_DIGIT_OFFSET], BN_LEN(bigInt));
281 if (ret != EOK) {
282 tloge("copy data failed\n");
283 return TEE_ERROR_SECURITY;
284 }
285 *bufferLen = BN_LEN(bigInt);
286
287 return TEE_SUCCESS;
288 }
289
TEE_BigIntConvertFromS32(TEE_BigInt * dest,int32_t shortVal)290 void TEE_BigIntConvertFromS32(TEE_BigInt *dest, int32_t shortVal)
291 {
292 struct bn_mem_pool_t *pool = NULL;
293 if (dest == NULL) {
294 tloge("parameters is invalid\n");
295 TEE_Panic(TEE_ERROR_BAD_PARAMETERS);
296 return;
297 }
298
299 pool = reserve_mem_pool();
300 if (pool == NULL) {
301 tloge("reserve memory pool for convert s32 is failed\n");
302 TEE_Panic(TEE_ERROR_OUT_OF_MEMORY);
303 return;
304 }
305
306 if (shortVal < BIG_INT_POSITIVE_FLAG) {
307 if ((BN_set_word(pool->a, -shortVal) != BN_SET_WORD_SUCC) ||
308 (bn_to_big_int(dest, *dest, pool->a) != BIG_INT_SUCCESS))
309 goto end;
310 TEE_BigIntNeg(dest, dest);
311 } else {
312 if ((BN_set_word(pool->a, shortVal) != BN_SET_WORD_SUCC) ||
313 (bn_to_big_int(dest, *dest, pool->a) != BIG_INT_SUCCESS))
314 goto end;
315 }
316 end:
317 release_mem_pool(pool);
318 }
319
TEE_BigIntConvertToS32(int32_t * dest,const TEE_BigInt * src)320 TEE_Result TEE_BigIntConvertToS32(int32_t *dest, const TEE_BigInt *src)
321 {
322 uint8_t *tmp = NULL;
323 uint32_t i;
324 int32_t tmp2 = 0;
325 uint32_t blen;
326 bool check = ((dest == NULL) || (src == NULL));
327 if (check) {
328 tloge("parameters is invalid\n");
329 return TEE_ERROR_BAD_PARAMETERS;
330 }
331
332 blen = BN_LEN(src);
333 if (blen > sizeof(uint32_t))
334 return TEE_ERROR_OVERFLOW;
335
336 if (blen == 0)
337 *dest = 0;
338
339 tmp = (uint8_t *)&src[BN_DIGIT_OFFSET];
340
341 if ((blen == sizeof(uint32_t)) && (tmp[BIG_INT_ALLOC_OFFSET] >> BYTE_SHIFT))
342 return TEE_ERROR_OVERFLOW;
343
344 for (i = 0; i < blen; i++)
345 tmp2 += (tmp[blen - 1 - i] << (BYTE_SIZE * i));
346
347 if ((src[BIG_INT_BYTES_OFFSET] >> SYMBOL_BIT) != BIG_INT_POSITIVE_FLAG)
348 *dest = -tmp2;
349 else
350 *dest = tmp2;
351
352 return TEE_SUCCESS;
353 }
354
TEE_BigIntCmp(const TEE_BigInt * op1,const TEE_BigInt * op2)355 int32_t TEE_BigIntCmp(const TEE_BigInt *op1, const TEE_BigInt *op2)
356 {
357 int32_t ret;
358 struct bn_mem_pool_t *pool = NULL;
359 if ((op1 == NULL) || (op2 == NULL)) {
360 tloge("parameters is invalid!\n");
361 TEE_Panic(TEE_ERROR_BAD_PARAMETERS);
362 return (int32_t)TEE_ERROR_BAD_PARAMETERS;
363 }
364
365 pool = reserve_mem_pool();
366 if (pool == NULL) {
367 tloge("reserve memory pool for cmp is failed\n");
368 TEE_Panic(TEE_ERROR_OUT_OF_MEMORY);
369 return (int32_t)TEE_ERROR_OUT_OF_MEMORY;
370 }
371 if ((big_int_to_bn(pool->a, op1, *op1) != BIG_INT_SUCCESS) ||
372 (big_int_to_bn(pool->b, op2, *op2) != BIG_INT_SUCCESS)) {
373 release_mem_pool(pool);
374 return BIG_INT_ERROR;
375 }
376
377 ret = BN_cmp(pool->a, pool->b);
378 release_mem_pool(pool);
379
380 return ret;
381 }
382
TEE_BigIntCmpS32(const TEE_BigInt * op,int32_t shortVal)383 int32_t TEE_BigIntCmpS32(const TEE_BigInt *op, int32_t shortVal)
384 {
385 TEE_BigInt *op2 = NULL;
386 int32_t ret;
387
388 if (op == NULL) {
389 TEE_Panic(TEE_ERROR_BAD_PARAMETERS);
390 return (int32_t)TEE_ERROR_BAD_PARAMETERS;
391 }
392 op2 = TEE_Malloc(TEE_BigIntSizeInU32(BIG_INT_INIT_VALUE) * sizeof(*op2), 0);
393 if (op2 == NULL) {
394 tloge("apply op2 buffer is failed!\n");
395 TEE_Panic(TEE_ERROR_OUT_OF_MEMORY);
396 return (int32_t)TEE_ERROR_OUT_OF_MEMORY;
397 }
398 TEE_BigIntInit(op2, TEE_BigIntSizeInU32(BIG_INT_INIT_VALUE));
399 TEE_BigIntConvertFromS32(op2, shortVal);
400
401 ret = TEE_BigIntCmp(op, op2);
402 TEE_Free(op2);
403 return ret;
404 }
405
TEE_BigIntShiftRight(TEE_BigInt * dest,const TEE_BigInt * op,size_t bits)406 void TEE_BigIntShiftRight(TEE_BigInt *dest, const TEE_BigInt *op, size_t bits)
407 {
408 struct bn_mem_pool_t *pool = NULL;
409 if ((dest == NULL) || (op == NULL)) {
410 tloge("parameters is invalid\n");
411 TEE_Panic(TEE_ERROR_BAD_PARAMETERS);
412 return;
413 }
414
415 pool = reserve_mem_pool();
416 if (pool == NULL) {
417 tloge("reserve memory pool for shift right is failed\n");
418 TEE_Panic(TEE_ERROR_OUT_OF_MEMORY);
419 return;
420 }
421
422 if ((big_int_to_bn(pool->a, op, *op) != BIG_INT_SUCCESS) || (BN_rshift(pool->a, pool->a, bits) != BN_OP_SUCC))
423 goto end;
424
425 if (bn_to_big_int(dest, *dest, pool->a) != BIG_INT_SUCCESS)
426 tloge("bn convert failed\n");
427 end:
428 release_mem_pool(pool);
429 }
430
TEE_BigIntGetBit(const TEE_BigInt * src,uint32_t bitIndex)431 bool TEE_BigIntGetBit(const TEE_BigInt *src, uint32_t bitIndex)
432 {
433 struct bn_mem_pool_t *pool = NULL;
434 if (src == NULL) {
435 tloge("parameters is invalid\n");
436 TEE_Panic(TEE_ERROR_BAD_PARAMETERS);
437 return false;
438 }
439
440 pool = reserve_mem_pool();
441 if (pool == NULL) {
442 tloge("reserve memory pool for get bit is failed\n");
443 TEE_Panic(TEE_ERROR_OUT_OF_MEMORY);
444 return false;
445 }
446
447 if (big_int_to_bn(pool->a, src, *src) != BIG_INT_SUCCESS) {
448 release_mem_pool(pool);
449 return false;
450 }
451
452 if ((uint32_t)BN_num_bits(pool->a) < bitIndex) {
453 release_mem_pool(pool);
454 return false;
455 }
456
457 if (big_int_to_bn(pool->a, src, *src) != BIG_INT_SUCCESS) {
458 release_mem_pool(pool);
459 return false;
460 }
461
462 bool ret = (bool)BN_is_bit_set(pool->a, bitIndex);
463 release_mem_pool(pool);
464
465 return ret;
466 }
467
TEE_BigIntGetBitCount(const TEE_BigInt * src)468 uint32_t TEE_BigIntGetBitCount(const TEE_BigInt *src)
469 {
470 uint32_t ret;
471 struct bn_mem_pool_t *pool = NULL;
472 if (src == NULL) {
473 tloge("parameters is invalid\n");
474 TEE_Panic(TEE_ERROR_BAD_PARAMETERS);
475 return (uint32_t)TEE_ERROR_BAD_PARAMETERS;
476 }
477
478 pool = reserve_mem_pool();
479 if (pool == NULL) {
480 tloge("reserve memory pool for bit count is failed\n");
481 TEE_Panic(TEE_ERROR_OUT_OF_MEMORY);
482 return (uint32_t)TEE_ERROR_OUT_OF_MEMORY;
483 }
484 if (big_int_to_bn(pool->a, src, *src) != BIG_INT_SUCCESS) {
485 release_mem_pool(pool);
486 return (uint32_t)TEE_ERROR_GENERIC;
487 }
488 ret = (uint32_t)BN_num_bits(pool->a);
489 release_mem_pool(pool);
490
491 return ret;
492 }
493
TEE_BigIntSetBit(TEE_BigInt * op,uint32_t bitIndex,bool value)494 TEE_Result TEE_BigIntSetBit(TEE_BigInt *op, uint32_t bitIndex, bool value)
495 {
496 struct bn_mem_pool_t *pool = NULL;
497 int32_t ret;
498
499 if (op == NULL) {
500 tloge("parameters is invalid");
501 return TEE_ERROR_BAD_PARAMETERS;
502 }
503
504 pool = reserve_mem_pool();
505 if (pool == NULL) {
506 tloge("reserve memory pool for set bit is failed\n");
507 TEE_Panic(TEE_ERROR_OUT_OF_MEMORY);
508 return TEE_ERROR_OUT_OF_MEMORY;
509 }
510
511 if (big_int_to_bn(pool->a, op, *op) != BIG_INT_SUCCESS) {
512 release_mem_pool(pool);
513 return (uint32_t)TEE_ERROR_GENERIC;
514 }
515
516 if ((uint32_t)BN_num_bits(pool->a) < bitIndex) {
517 release_mem_pool(pool);
518 return TEE_ERROR_OVERFLOW;
519 }
520
521 if (value)
522 ret = BN_set_bit(pool->a, bitIndex);
523 else
524 ret = BN_clear_bit(pool->a, bitIndex);
525
526 if ((ret != BN_OP_SUCC) || (bn_to_big_int(op, *op, pool->a) != BIG_INT_SUCCESS)) {
527 release_mem_pool(pool);
528 return TEE_ERROR_GENERIC;
529 }
530 release_mem_pool(pool);
531 return TEE_SUCCESS;
532 }
533
TEE_BigIntAssign(TEE_BigInt * dest,const TEE_BigInt * src)534 TEE_Result TEE_BigIntAssign(TEE_BigInt *dest, const TEE_BigInt *src)
535 {
536 struct bn_mem_pool_t *pool = NULL;
537 bool check = ((dest == NULL) || (src == NULL));
538 if (check) {
539 tloge("parameters is invalid\n");
540 return TEE_ERROR_BAD_PARAMETERS;
541 }
542
543 pool = reserve_mem_pool();
544 if (pool == NULL) {
545 tloge("reserve memory pool for assign is failed\n");
546 TEE_Panic(TEE_ERROR_OUT_OF_MEMORY);
547 return TEE_ERROR_OUT_OF_MEMORY;
548 }
549
550 if (big_int_to_bn(pool->a, src, *src) != BIG_INT_SUCCESS) {
551 release_mem_pool(pool);
552 return TEE_ERROR_BAD_PARAMETERS;
553 }
554
555 if (bn_to_big_int(dest, *dest, pool->a) != BIG_INT_SUCCESS) {
556 release_mem_pool(pool);
557 return TEE_ERROR_OVERFLOW;
558 }
559
560 release_mem_pool(pool);
561 return TEE_SUCCESS;
562 }
563
TEE_BigIntAbs(TEE_BigInt * dest,const TEE_BigInt * src)564 TEE_Result TEE_BigIntAbs(TEE_BigInt *dest, const TEE_BigInt *src)
565 {
566 bool check = ((dest == NULL) || (src == NULL));
567 if (check) {
568 tloge("parameters is invalid\n");
569 return TEE_ERROR_BAD_PARAMETERS;
570 }
571
572 if (TEE_BigIntCmpS32(src, 0) < 0) {
573 TEE_BigIntNeg(dest, src);
574 return TEE_SUCCESS;
575 }
576
577 return TEE_BigIntAssign(dest, src);
578 }
579
TEE_BigIntAdd(TEE_BigInt * dest,const TEE_BigInt * op1,const TEE_BigInt * op2)580 void TEE_BigIntAdd(TEE_BigInt *dest, const TEE_BigInt *op1, const TEE_BigInt *op2)
581 {
582 struct bn_mem_pool_t *pool = NULL;
583
584 bool check = ((dest == NULL) || (op1 == NULL) || (op2 == NULL));
585 if (check) {
586 tloge("parameters is invalid for add\n");
587 TEE_Panic(TEE_ERROR_BAD_PARAMETERS);
588 return;
589 }
590
591 pool = reserve_mem_pool();
592 if (pool == NULL) {
593 tloge("reserve memory pool for add is failed\n");
594 TEE_Panic(TEE_ERROR_OUT_OF_MEMORY);
595 return;
596 }
597
598 check = ((big_int_to_bn(pool->a, op1, *op1) != BIG_INT_SUCCESS) ||
599 (big_int_to_bn(pool->b, op2, *op2) != BIG_INT_SUCCESS) || (BN_add(pool->c, pool->a, pool->b) != BN_OP_SUCC) ||
600 (bn_to_big_int(dest, *dest, pool->c) != BIG_INT_SUCCESS));
601 if (check)
602 tloge("big int operation error");
603
604 release_mem_pool(pool);
605 }
606
TEE_BigIntSub(TEE_BigInt * dest,const TEE_BigInt * op1,const TEE_BigInt * op2)607 void TEE_BigIntSub(TEE_BigInt *dest, const TEE_BigInt *op1, const TEE_BigInt *op2)
608 {
609 struct bn_mem_pool_t *pool = NULL;
610
611 bool check = ((dest == NULL) || (op1 == NULL) || (op2 == NULL));
612 if (check) {
613 tloge("parameters is invalid for sub\n");
614 TEE_Panic(TEE_ERROR_BAD_PARAMETERS);
615 return;
616 }
617
618 pool = reserve_mem_pool();
619 if (pool == NULL) {
620 tloge("reserve memory pool for sub is failed\n");
621 TEE_Panic(TEE_ERROR_OUT_OF_MEMORY);
622 return;
623 }
624
625 check = ((big_int_to_bn(pool->a, op1, *op1) != BIG_INT_SUCCESS) ||
626 (big_int_to_bn(pool->b, op2, *op2) != BIG_INT_SUCCESS) || (BN_sub(pool->c, pool->a, pool->b) != BN_OP_SUCC) ||
627 (bn_to_big_int(dest, *dest, pool->c) != BIG_INT_SUCCESS));
628 if (check)
629 tloge("big int operation error");
630
631 release_mem_pool(pool);
632 }
633
TEE_BigIntNeg(TEE_BigInt * dest,const TEE_BigInt * src)634 void TEE_BigIntNeg(TEE_BigInt *dest, const TEE_BigInt *src)
635 {
636 struct bn_mem_pool_t *pool = NULL;
637
638 if ((dest == NULL) || (src == NULL)) {
639 tloge("parameters is invalid\n");
640 TEE_Panic(TEE_ERROR_BAD_PARAMETERS);
641 return;
642 }
643
644 pool = reserve_mem_pool();
645 if (pool == NULL) {
646 tloge("reserve memory pool for neg failed\n");
647 TEE_Panic(TEE_ERROR_OUT_OF_MEMORY);
648 return;
649 }
650
651 big_int_to_bn(pool->a, src, *src);
652
653 if (BN_is_negative(pool->a))
654 BN_set_negative(pool->a, BIG_INT_POSITIVE_FLAG);
655 else
656 BN_set_negative(pool->a, BIG_INT_NEGITIVE_FLAG);
657
658 if (bn_to_big_int(dest, *dest, pool->a) != BIG_INT_SUCCESS)
659 tloge("operation failed\n");
660
661 release_mem_pool(pool);
662 }
663
TEE_BigIntMul(TEE_BigInt * dest,const TEE_BigInt * op1,const TEE_BigInt * op2)664 void TEE_BigIntMul(TEE_BigInt *dest, const TEE_BigInt *op1, const TEE_BigInt *op2)
665 {
666 struct bn_mem_pool_t *pool = NULL;
667
668 bool check = ((dest == NULL) || (op1 == NULL) || (op2 == NULL));
669 if (check) {
670 tloge("parameters is invalid for mul\n");
671 TEE_Panic(TEE_ERROR_BAD_PARAMETERS);
672 return;
673 }
674
675 pool = reserve_mem_pool();
676 if (pool == NULL) {
677 tloge("reserve memory pool for mul is failed\n");
678 TEE_Panic(TEE_ERROR_OUT_OF_MEMORY);
679 return;
680 }
681
682 check = ((big_int_to_bn(pool->a, op1, *op1) != BIG_INT_SUCCESS) ||
683 (big_int_to_bn(pool->b, op2, *op2) != BIG_INT_SUCCESS) ||
684 (BN_mul(pool->c, pool->a, pool->b, pool->ctx) != BN_OP_SUCC) ||
685 (bn_to_big_int(dest, *dest, pool->c) != BIG_INT_SUCCESS));
686 if (check)
687 tloge("big int operation error");
688
689 release_mem_pool(pool);
690 }
691
TEE_BigIntSquare(TEE_BigInt * dest,const TEE_BigInt * op)692 void TEE_BigIntSquare(TEE_BigInt *dest, const TEE_BigInt *op)
693 {
694 struct bn_mem_pool_t *pool = NULL;
695
696 if ((dest == NULL) || (op == NULL)) {
697 tloge("parameters is invalid\n");
698 TEE_Panic(TEE_ERROR_BAD_PARAMETERS);
699 return;
700 }
701
702 pool = reserve_mem_pool();
703 if (pool == NULL) {
704 tloge("reserve memory pool for square is failed\n");
705 TEE_Panic(TEE_ERROR_OUT_OF_MEMORY);
706 return;
707 }
708
709 bool check =
710 ((big_int_to_bn(pool->a, op, *op) != BIG_INT_SUCCESS) || (BN_sqr(pool->b, pool->a, pool->ctx) != BN_OP_SUCC) ||
711 (bn_to_big_int(dest, *dest, pool->b) != BIG_INT_SUCCESS));
712 if (check)
713 tloge("big int operation error");
714
715 release_mem_pool(pool);
716 }
717
TEE_BigIntDiv(TEE_BigInt * dest_q,TEE_BigInt * dest_r,const TEE_BigInt * op1,const TEE_BigInt * op2)718 void TEE_BigIntDiv(TEE_BigInt *dest_q, TEE_BigInt *dest_r, const TEE_BigInt *op1, const TEE_BigInt *op2)
719 {
720 struct bn_mem_pool_t *pool = NULL;
721
722 bool check = ((dest_q == NULL) || (dest_r == NULL) || (op1 == NULL) || (op2 == NULL));
723 if (check) {
724 tloge("parameters is invalid\n");
725 TEE_Panic(TEE_ERROR_BAD_PARAMETERS);
726 return;
727 }
728
729 pool = reserve_mem_pool();
730 if (pool == NULL) {
731 tloge("reserve memory pool for div is failed\n");
732 TEE_Panic(TEE_ERROR_OUT_OF_MEMORY);
733 return;
734 }
735
736 check = ((big_int_to_bn(pool->a, op1, *op1) != BIG_INT_SUCCESS) ||
737 (big_int_to_bn(pool->b, op2, *op2) != BIG_INT_SUCCESS) ||
738 (BN_div(pool->d, pool->c, pool->a, pool->b, pool->ctx) != BN_OP_SUCC) ||
739 (bn_to_big_int(dest_q, *dest_q, pool->d) != BIG_INT_SUCCESS) ||
740 (bn_to_big_int(dest_r, *dest_r, pool->c) != BIG_INT_SUCCESS));
741 if (check)
742 tloge("big int operation error");
743
744 release_mem_pool(pool);
745 }
746
TEE_BigIntMod(TEE_BigInt * dest,const TEE_BigInt * op,const TEE_BigInt * n)747 void TEE_BigIntMod(TEE_BigInt *dest, const TEE_BigInt *op, const TEE_BigInt *n)
748 {
749 struct bn_mem_pool_t *pool = NULL;
750
751 bool check = ((dest == NULL) || (op == NULL) || (n == NULL));
752 if (check) {
753 tloge("parameters is invalid for int mod\n");
754 TEE_Panic(TEE_ERROR_BAD_PARAMETERS);
755 return;
756 }
757
758 pool = reserve_mem_pool();
759 if (pool == NULL) {
760 tloge("reserve memory pool for int mod is failed\n");
761 TEE_Panic(TEE_ERROR_OUT_OF_MEMORY);
762 return;
763 }
764
765 check =
766 ((big_int_to_bn(pool->a, op, *op) != BIG_INT_SUCCESS) || (big_int_to_bn(pool->b, n, *n) != BIG_INT_SUCCESS) ||
767 (BN_mod(pool->c, pool->a, pool->b, pool->ctx) != BN_OP_SUCC) ||
768 (bn_to_big_int(dest, *dest, pool->c) != BIG_INT_SUCCESS));
769 if (check)
770 tloge("big int operation error");
771
772 release_mem_pool(pool);
773 }
774
TEE_BigIntAddMod(TEE_BigInt * dest,const TEE_BigInt * op1,const TEE_BigInt * op2,const TEE_BigInt * n)775 void TEE_BigIntAddMod(TEE_BigInt *dest, const TEE_BigInt *op1, const TEE_BigInt *op2, const TEE_BigInt *n)
776 {
777 struct bn_mem_pool_t *pool = NULL;
778
779 bool check = ((dest == NULL) || (op1 == NULL) || (op2 == NULL) || (n == NULL));
780 if (check) {
781 tloge("parameters is invalid for add mod\n");
782 TEE_Panic(TEE_ERROR_BAD_PARAMETERS);
783 return;
784 }
785
786 pool = reserve_mem_pool();
787 if (pool == NULL) {
788 tloge("reserve memory pool for add mod is failed\n");
789 TEE_Panic(TEE_ERROR_OUT_OF_MEMORY);
790 return;
791 }
792 check = ((big_int_to_bn(pool->a, op1, *op1) != BIG_INT_SUCCESS) ||
793 (big_int_to_bn(pool->b, op2, *op2) != BIG_INT_SUCCESS) || (big_int_to_bn(pool->c, n, *n) != BIG_INT_SUCCESS) ||
794 (BN_mod_add(pool->d, pool->a, pool->b, pool->c, pool->ctx) != BN_OP_SUCC) ||
795 (bn_to_big_int(dest, *dest, pool->d) != BIG_INT_SUCCESS));
796 if (check)
797 tloge("big int operation error");
798
799 release_mem_pool(pool);
800 }
801
TEE_BigIntSubMod(TEE_BigInt * dest,const TEE_BigInt * op1,const TEE_BigInt * op2,const TEE_BigInt * n)802 void TEE_BigIntSubMod(TEE_BigInt *dest, const TEE_BigInt *op1, const TEE_BigInt *op2, const TEE_BigInt *n)
803 {
804 struct bn_mem_pool_t *pool = NULL;
805
806 bool check = ((dest == NULL) || (op1 == NULL) || (op2 == NULL) || (n == NULL));
807 if (check) {
808 tloge("parameters is invalid for sub mod\n");
809 TEE_Panic(TEE_ERROR_BAD_PARAMETERS);
810 return;
811 }
812
813 pool = reserve_mem_pool();
814 if (pool == NULL) {
815 tloge("reserve memory pool for sub mod is failed\n");
816 TEE_Panic(TEE_ERROR_OUT_OF_MEMORY);
817 return;
818 }
819
820 check = ((big_int_to_bn(pool->a, op1, *op1) != BIG_INT_SUCCESS) ||
821 (big_int_to_bn(pool->b, op2, *op2) != BIG_INT_SUCCESS) || (big_int_to_bn(pool->c, n, *n) != BIG_INT_SUCCESS) ||
822 (BN_mod_sub(pool->d, pool->a, pool->b, pool->c, pool->ctx) != BN_OP_SUCC) ||
823 (bn_to_big_int(dest, *dest, pool->d) != BIG_INT_SUCCESS));
824 if (check)
825 tloge("big int operation error");
826
827 release_mem_pool(pool);
828 }
829
TEE_BigIntMulMod(TEE_BigInt * dest,const TEE_BigInt * op1,const TEE_BigInt * op2,const TEE_BigInt * n)830 void TEE_BigIntMulMod(TEE_BigInt *dest, const TEE_BigInt *op1, const TEE_BigInt *op2, const TEE_BigInt *n)
831 {
832 struct bn_mem_pool_t *pool = NULL;
833 bool check = ((dest == NULL) || (op1 == NULL) || (op2 == NULL) || (n == NULL));
834 if (check) {
835 tloge("parameters is invalid for mul mod\n");
836 TEE_Panic(TEE_ERROR_BAD_PARAMETERS);
837 return;
838 }
839
840 pool = reserve_mem_pool();
841 if (pool == NULL) {
842 tloge("reserve memory pool for mul mod is failed\n");
843 TEE_Panic(TEE_ERROR_OUT_OF_MEMORY);
844 return;
845 }
846
847 check = ((big_int_to_bn(pool->a, op1, *op1) != BIG_INT_SUCCESS) ||
848 (big_int_to_bn(pool->b, op2, *op2) != BIG_INT_SUCCESS) || (big_int_to_bn(pool->c, n, *n) != BIG_INT_SUCCESS) ||
849 (BN_mod_mul(pool->d, pool->a, pool->b, pool->c, pool->ctx) != BN_OP_SUCC) ||
850 (bn_to_big_int(dest, *dest, pool->d) != BIG_INT_SUCCESS));
851 if (check)
852 tloge("big int operation error");
853
854 release_mem_pool(pool);
855 }
856
TEE_BigIntSquareMod(TEE_BigInt * dest,const TEE_BigInt * op,const TEE_BigInt * n)857 void TEE_BigIntSquareMod(TEE_BigInt *dest, const TEE_BigInt *op, const TEE_BigInt *n)
858 {
859 struct bn_mem_pool_t *pool = NULL;
860
861 bool check = ((dest == NULL) || (op == NULL) || (n == NULL));
862 if (check) {
863 tloge("parameters is invalid for square mod\n");
864 TEE_Panic(TEE_ERROR_BAD_PARAMETERS);
865 return;
866 }
867
868 pool = reserve_mem_pool();
869 if (pool == NULL) {
870 tloge("reserve memory pool for square mod is failed\n");
871 TEE_Panic(TEE_ERROR_OUT_OF_MEMORY);
872 return;
873 }
874
875 check =
876 ((big_int_to_bn(pool->a, op, *op) != BIG_INT_SUCCESS) || (big_int_to_bn(pool->b, n, *n) != BIG_INT_SUCCESS) ||
877 (BN_mod_sqr(pool->c, pool->a, pool->b, pool->ctx) != BN_OP_SUCC) ||
878 (bn_to_big_int(dest, *dest, pool->c) != BIG_INT_SUCCESS));
879 if (check)
880 tloge("big int operation error");
881
882 release_mem_pool(pool);
883 }
884
TEE_BigIntInvMod(TEE_BigInt * dest,const TEE_BigInt * op,const TEE_BigInt * n)885 void TEE_BigIntInvMod(TEE_BigInt *dest, const TEE_BigInt *op, const TEE_BigInt *n)
886 {
887 struct bn_mem_pool_t *pool = NULL;
888
889 if ((dest == NULL) || (op == NULL) || (n == NULL)) {
890 tloge("parameters is invalid\n");
891 TEE_Panic(TEE_ERROR_BAD_PARAMETERS);
892 return;
893 }
894
895 pool = reserve_mem_pool();
896 if (pool == NULL) {
897 tloge("reserve memory pool for inv mod is failed\n");
898 TEE_Panic(TEE_ERROR_OUT_OF_MEMORY);
899 return;
900 }
901
902 bool check =
903 ((big_int_to_bn(pool->a, op, *op) != BIG_INT_SUCCESS) || (big_int_to_bn(pool->b, n, *n) != BIG_INT_SUCCESS) ||
904 (bn_to_big_int(dest, *dest, BN_mod_inverse(pool->c, pool->a, pool->b, pool->ctx)) != BIG_INT_SUCCESS));
905 if (check)
906 tloge("big int operation error");
907
908 release_mem_pool(pool);
909 }
910
EXT_TEE_BigIntExpMod(TEE_BigInt * out,TEE_BigInt * in,const TEE_BigInt * exp,const TEE_BigInt * n)911 bool EXT_TEE_BigIntExpMod(TEE_BigInt *out, TEE_BigInt *in, const TEE_BigInt *exp, const TEE_BigInt *n)
912 {
913 struct bn_mem_pool_t *pool = NULL;
914
915 bool check = ((out == NULL) || (in == NULL) || (exp == NULL) || (n == NULL));
916 if (check) {
917 tloge("parameters is invalid\n");
918 TEE_Panic(TEE_ERROR_BAD_PARAMETERS);
919 return false;
920 }
921
922 pool = reserve_mem_pool();
923 if (pool == NULL) {
924 tloge("reserve memory pool for exp mod is failed\n");
925 TEE_Panic(TEE_ERROR_OUT_OF_MEMORY);
926 return false;
927 }
928
929 check = ((big_int_to_bn(pool->a, in, *in) != BIG_INT_SUCCESS) ||
930 (big_int_to_bn(pool->b, exp, *exp) != BIG_INT_SUCCESS) || (big_int_to_bn(pool->c, n, *n) != BIG_INT_SUCCESS) ||
931 (BN_mod_exp(pool->d, pool->a, pool->b, pool->c, pool->ctx) != BN_OP_SUCC) ||
932 (bn_to_big_int(out, *out, pool->d) != BIG_INT_SUCCESS));
933 if (check) {
934 release_mem_pool(pool);
935 return false;
936 }
937
938 release_mem_pool(pool);
939 return true;
940 }
TEE_BigIntExpMod(TEE_BigInt * des,TEE_BigInt * op1,const TEE_BigInt * op2,const TEE_BigInt * n,TEE_BigIntFMMContext * context)941 TEE_Result TEE_BigIntExpMod(TEE_BigInt *des, TEE_BigInt *op1, const TEE_BigInt *op2, const TEE_BigInt *n,
942 TEE_BigIntFMMContext *context)
943 {
944 (void)context;
945
946 if (!EXT_TEE_BigIntExpMod(des, op1, op2, n))
947 return TEE_ERROR_BAD_PARAMETERS;
948
949 return TEE_SUCCESS;
950 }
951
TEE_BigIntRelativePrime(const TEE_BigInt * op1,const TEE_BigInt * op2)952 bool TEE_BigIntRelativePrime(const TEE_BigInt *op1, const TEE_BigInt *op2)
953 {
954 struct bn_mem_pool_t *pool = NULL;
955 if ((op1 == NULL) || (op2 == NULL)) {
956 tloge("parameters is invalid\n");
957 TEE_Panic(TEE_ERROR_BAD_PARAMETERS);
958 return false;
959 }
960
961 pool = reserve_mem_pool();
962 if (pool == NULL) {
963 tloge("reserve memory pool for relative prime is failed\n");
964 TEE_Panic(TEE_ERROR_OUT_OF_MEMORY);
965 return false;
966 }
967
968 if ((big_int_to_bn(pool->a, op1, *op1) != BIG_INT_SUCCESS) ||
969 (big_int_to_bn(pool->b, op2, *op2) != BIG_INT_SUCCESS) ||
970 (BN_gcd(pool->c, pool->a, pool->b, pool->ctx) != BN_OP_SUCC)) {
971 release_mem_pool(pool);
972 return false;
973 }
974
975 bool ret = (bool)BN_is_one(pool->c);
976 release_mem_pool(pool);
977 return ret;
978 }
979
TEE_BigIntComputeExtendedGcd(TEE_BigInt * gcd,TEE_BigInt * u,TEE_BigInt * v,const TEE_BigInt * op1,const TEE_BigInt * op2)980 void TEE_BigIntComputeExtendedGcd(TEE_BigInt *gcd, TEE_BigInt *u, TEE_BigInt *v, const TEE_BigInt *op1,
981 const TEE_BigInt *op2)
982 {
983 struct bn_mem_pool_t *pool = NULL;
984 if ((gcd == NULL) || (u == NULL) || (v == NULL) || (op1 == NULL) || (op2 == NULL)) {
985 tloge("parameters is invalid\n");
986 TEE_Panic(TEE_ERROR_BAD_PARAMETERS);
987 return;
988 }
989
990 pool = reserve_mem_pool();
991 if (pool == NULL) {
992 tloge("reserve memory pool for extended gcd is failed\n");
993 TEE_Panic(TEE_ERROR_OUT_OF_MEMORY);
994 return;
995 }
996
997 bool check = ((big_int_to_bn(pool->a, op1, *op1) != BIG_INT_SUCCESS) ||
998 (big_int_to_bn(pool->b, op2, *op2) != BIG_INT_SUCCESS) ||
999 (BN_gcd(pool->d, pool->a, pool->b, pool->ctx) != BN_OP_SUCC) ||
1000 (bn_to_big_int(gcd, *gcd, pool->d) != BIG_INT_SUCCESS) ||
1001 (BN_div(pool->a, pool->e, pool->a, pool->d, pool->ctx) != BN_OP_SUCC) ||
1002 (BN_div(pool->b, pool->e, pool->b, pool->d, pool->ctx) != BN_OP_SUCC) ||
1003 (bn_to_big_int(u, *u, BN_mod_inverse(pool->c, pool->a, pool->b, pool->ctx)) != BIG_INT_SUCCESS) ||
1004 (BN_mul(pool->e, pool->a, pool->c, pool->ctx) != BN_OP_SUCC) ||
1005 (BN_set_word(pool->c, BIG_INT_INIT_VALUE) != BN_OP_SUCC) ||
1006 (BN_sub(pool->e, pool->c, pool->e) != BN_OP_SUCC) ||
1007 (BN_div(pool->e, pool->c, pool->e, pool->b, pool->ctx) != BN_OP_SUCC) ||
1008 (bn_to_big_int(v, *v, pool->e) != BIG_INT_SUCCESS));
1009 if (check)
1010 tloge("big int operation error");
1011
1012 release_mem_pool(pool);
1013 }
1014
TEE_BigIntIsProbablePrime(const TEE_BigInt * op,uint32_t confidenceLevel)1015 int32_t TEE_BigIntIsProbablePrime(const TEE_BigInt *op, uint32_t confidenceLevel)
1016 {
1017 (void)confidenceLevel;
1018
1019 int32_t ret;
1020 struct bn_mem_pool_t *pool = NULL;
1021 if (op == NULL) {
1022 tloge("parameters is invalid\n");
1023 TEE_Panic(TEE_ERROR_BAD_PARAMETERS);
1024 return (int32_t)TEE_ERROR_BAD_PARAMETERS;
1025 }
1026
1027 pool = reserve_mem_pool();
1028 if (pool == NULL) {
1029 tloge("reserve memory pool for probable prime is failed\n");
1030 TEE_Panic(TEE_ERROR_OUT_OF_MEMORY);
1031 return (int32_t)TEE_ERROR_OUT_OF_MEMORY;
1032 }
1033
1034 if (big_int_to_bn(pool->a, op, *op) != BIG_INT_SUCCESS) {
1035 release_mem_pool(pool);
1036 return BIG_INT_ERROR;
1037 }
1038 ret = BN_is_prime_fasttest_ex(pool->a, BN_prime_checks, pool->ctx, TRIAL_DIVISION, NULL);
1039 release_mem_pool(pool);
1040 return ret;
1041 }
1042
TEE_BigIntConvertToFMM(TEE_BigIntFMM * dest,const TEE_BigInt * src,const TEE_BigInt * n,const TEE_BigIntFMMContext * context)1043 void TEE_BigIntConvertToFMM(TEE_BigIntFMM *dest, const TEE_BigInt *src, const TEE_BigInt *n,
1044 const TEE_BigIntFMMContext *context)
1045 {
1046 struct bn_mem_pool_t *pool = NULL;
1047
1048 if ((dest == NULL) || (src == NULL) || (context == NULL) || (n == NULL)) {
1049 tloge("parameters is invalid for convert to fmm\n");
1050 TEE_Panic(TEE_ERROR_BAD_PARAMETERS);
1051 return;
1052 }
1053
1054 pool = reserve_mem_pool();
1055 if (pool == NULL) {
1056 tloge("reserve memory pool for convert to fmm is failed\n");
1057 TEE_Panic(TEE_ERROR_OUT_OF_MEMORY);
1058 return;
1059 }
1060
1061 bool check =
1062 ((big_int_to_bn(pool->a, src, *src) != BIG_INT_SUCCESS) || (big_int_to_bn(pool->c, n, *n) != BIG_INT_SUCCESS) ||
1063 (BN_MONT_CTX_set(pool->mont_ctx, pool->c, pool->ctx) != BN_OP_SUCC) ||
1064 (BN_to_montgomery(pool->b, pool->a, pool->mont_ctx, pool->ctx) != BN_OP_SUCC) ||
1065 (bn_to_big_int(dest, *dest, pool->b) != BIG_INT_SUCCESS));
1066 if (check)
1067 tloge("big int operation error");
1068
1069 release_mem_pool(pool);
1070 }
1071
TEE_BigIntConvertFromFMM(TEE_BigInt * dest,const TEE_BigIntFMM * src,const TEE_BigInt * n,const TEE_BigIntFMMContext * context)1072 void TEE_BigIntConvertFromFMM(TEE_BigInt *dest, const TEE_BigIntFMM *src, const TEE_BigInt *n,
1073 const TEE_BigIntFMMContext *context)
1074 {
1075 struct bn_mem_pool_t *pool = NULL;
1076
1077 if ((dest == NULL) || (src == NULL) || (context == NULL) || (n == NULL)) {
1078 tloge("parameters is invalid for convert from fmm\n");
1079 TEE_Panic(TEE_ERROR_BAD_PARAMETERS);
1080 return;
1081 }
1082
1083 pool = reserve_mem_pool();
1084 if (pool == NULL) {
1085 tloge("reserve memory pool for convert from fmm is failed\n");
1086 TEE_Panic(TEE_ERROR_OUT_OF_MEMORY);
1087 return;
1088 }
1089
1090 bool check =
1091 ((big_int_to_bn(pool->a, src, *src) != BIG_INT_SUCCESS) || (big_int_to_bn(pool->c, n, *n) != BIG_INT_SUCCESS) ||
1092 (BN_MONT_CTX_set(pool->mont_ctx, pool->c, pool->ctx) != BN_OP_SUCC) ||
1093 (BN_from_montgomery(pool->b, pool->a, pool->mont_ctx, pool->ctx) != BN_OP_SUCC) ||
1094 (bn_to_big_int(dest, *dest, pool->b) != BIG_INT_SUCCESS));
1095 if (check)
1096 tloge("big int operation error");
1097
1098 release_mem_pool(pool);
1099 }
1100
TEE_BigIntComputeFMM(TEE_BigIntFMM * dest,const TEE_BigIntFMM * op1,const TEE_BigIntFMM * op2,const TEE_BigInt * n,const TEE_BigIntFMMContext * context)1101 void TEE_BigIntComputeFMM(TEE_BigIntFMM *dest, const TEE_BigIntFMM *op1, const TEE_BigIntFMM *op2, const TEE_BigInt *n,
1102 const TEE_BigIntFMMContext *context)
1103 {
1104 struct bn_mem_pool_t *pool = NULL;
1105
1106 if ((dest == NULL) || (op1 == NULL) || (op2 == NULL) || (context == NULL) || (n == NULL)) {
1107 tloge("parameters is invalid\n");
1108 TEE_Panic(TEE_ERROR_BAD_PARAMETERS);
1109 return;
1110 }
1111
1112 pool = reserve_mem_pool();
1113 if (pool == NULL) {
1114 tloge("reserve memory pool for compute fmm is failed\n");
1115 TEE_Panic(TEE_ERROR_OUT_OF_MEMORY);
1116 return;
1117 }
1118
1119 bool check = ((big_int_to_bn(pool->a, op1, *op1) != BIG_INT_SUCCESS) ||
1120 (big_int_to_bn(pool->b, op2, *op2) != BIG_INT_SUCCESS) ||
1121 (big_int_to_bn(pool->d, context, *context) != BIG_INT_SUCCESS) ||
1122 (BN_MONT_CTX_set(pool->mont_ctx, pool->d, pool->ctx) != BN_OP_SUCC) ||
1123 (BN_mod_mul_montgomery(pool->c, pool->a, pool->b, pool->mont_ctx, pool->ctx) != BN_OP_SUCC) ||
1124 (bn_to_big_int(dest, *dest, pool->c) != BIG_INT_SUCCESS));
1125 if (check)
1126 tloge("big int operation error");
1127
1128 release_mem_pool(pool);
1129 }
1130