• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * This file is part of the openHiTLS project.
3  *
4  * openHiTLS is licensed under the Mulan PSL v2.
5  * You can use this software according to the terms and conditions of the Mulan PSL v2.
6  * You may obtain a copy of Mulan PSL v2 at:
7  *
8  *     http://license.coscl.org.cn/MulanPSL2
9  *
10  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11  * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12  * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13  * See the Mulan PSL v2 for more details.
14  */
15 
16 #include "hitls_build.h"
17 #ifdef HITLS_CRYPTO_BN
18 
19 #include "securec.h"
20 #include "bn_bincal.h"
21 #include "crypt_errno.h"
22 #include "bsl_err_internal.h"
23 
24 /* the user should guaranteed a.size >= b.size */
USub(BN_BigNum * r,const BN_BigNum * a,const BN_BigNum * b)25 int32_t USub(BN_BigNum *r, const BN_BigNum *a, const BN_BigNum *b)
26 {
27     uint32_t maxSize = a->size;
28     uint32_t minSize = b->size;
29     // Ensure that r is sufficient.
30     int32_t ret = BnExtend(r, maxSize);
31     if (ret != CRYPT_SUCCESS) {
32         return ret;
33     }
34     BN_UINT *rr = r->data;
35     const BN_UINT *aa = a->data;
36     const BN_UINT *bb = b->data;
37 
38     BN_UINT borrow = BinSub(rr, aa, bb, minSize);
39     rr += minSize;
40     aa += minSize;
41 
42     uint32_t diff = maxSize - minSize;
43     while (diff > 0) {
44         BN_UINT t = *aa;
45         aa++;
46         *rr = t - borrow;
47         rr++;
48         borrow = t < borrow;
49         diff--;
50     }
51     while (maxSize != 0) {
52         rr--;
53         if (*rr != 0) {
54             break;
55         }
56         maxSize--;
57     }
58     r->size = maxSize;
59     return CRYPT_SUCCESS;
60 }
61 
UDec(BN_BigNum * r,const BN_BigNum * a,BN_UINT w)62 void UDec(BN_BigNum *r, const BN_BigNum *a, BN_UINT w)
63 {
64     uint32_t size = a->size;
65 
66     // the user should guaranteed size > 1, the return value must be 0 thus the return value is ignored
67     (void)BinDec(r->data, a->data, size, w);
68     r->size = BinFixSize(r->data, size);
69 }
70 
UAdd(BN_BigNum * r,const BN_BigNum * a,const BN_BigNum * b)71 int32_t UAdd(BN_BigNum *r, const BN_BigNum *a, const BN_BigNum *b)
72 {
73     const BN_BigNum *max = (a->size < b->size) ? b : a;
74     const BN_BigNum *min = (a->size < b->size) ? a : b;
75     uint32_t maxSize = max->size;
76     uint32_t minSize = min->size;
77     // Ensure that r is sufficient to carry the sum.
78     int32_t ret = BnExtend(r, maxSize + 1);
79     if (ret != CRYPT_SUCCESS) {
80         return ret;
81     }
82     r->size = maxSize;
83     BN_UINT *rr = r->data;
84     const BN_UINT *aa = max->data;
85     const BN_UINT *bb = min->data;
86 
87     BN_UINT carry = BinAdd(rr, aa, bb, minSize);
88     rr += minSize;
89     aa += minSize;
90 
91     uint32_t diff = maxSize - minSize;
92     while (diff > 0) {
93         ADD_AB(carry, *rr, *aa, carry);
94         aa++, rr++, diff--;
95     }
96     if (carry != 0) {
97         *rr = carry;
98         r->size += 1;
99     }
100     return CRYPT_SUCCESS;
101 }
102 
103 #endif /* HITLS_CRYPTO_BN */
104