Coverage Report

Created: 2025-08-29 06:35

/src/dropbear/libtommath/bn_s_mp_exptmod.c
Line
Count
Source (jump to first uncovered line)
1
#include "tommath_private.h"
2
#ifdef BN_S_MP_EXPTMOD_C
3
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
4
/* SPDX-License-Identifier: Unlicense */
5
6
#ifdef MP_LOW_MEM
7
#   define TAB_SIZE 32
8
#   define MAX_WINSIZE 5
9
#else
10
#   define TAB_SIZE 256
11
857
#   define MAX_WINSIZE 0
12
#endif
13
14
mp_err s_mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode)
15
857
{
16
857
   mp_int  M[TAB_SIZE], res, mu;
17
857
   mp_digit buf;
18
857
   mp_err   err;
19
857
   int      bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize;
20
857
   mp_err(*redux)(mp_int *x, const mp_int *m, const mp_int *mu);
21
22
   /* find window size */
23
857
   x = mp_count_bits(X);
24
857
   if (x <= 7) {
25
213
      winsize = 2;
26
644
   } else if (x <= 36) {
27
247
      winsize = 3;
28
397
   } else if (x <= 140) {
29
132
      winsize = 4;
30
265
   } else if (x <= 450) {
31
265
      winsize = 5;
32
265
   } else if (x <= 1303) {
33
0
      winsize = 6;
34
0
   } else if (x <= 3529) {
35
0
      winsize = 7;
36
0
   } else {
37
0
      winsize = 8;
38
0
   }
39
40
857
   winsize = MAX_WINSIZE ? MP_MIN(MAX_WINSIZE, winsize) : winsize;
41
42
   /* init M array */
43
   /* init first cell */
44
857
   if ((err = mp_init(&M[1])) != MP_OKAY) {
45
0
      return err;
46
0
   }
47
48
   /* now init the second half of the array */
49
7.56k
   for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
50
6.71k
      if ((err = mp_init(&M[x])) != MP_OKAY) {
51
0
         for (y = 1<<(winsize-1); y < x; y++) {
52
0
            mp_clear(&M[y]);
53
0
         }
54
0
         mp_clear(&M[1]);
55
0
         return err;
56
0
      }
57
6.71k
   }
58
59
   /* create mu, used for Barrett reduction */
60
857
   if ((err = mp_init(&mu)) != MP_OKAY)                           goto LBL_M;
61
62
857
   if (redmode == 0) {
63
544
      if ((err = mp_reduce_setup(&mu, P)) != MP_OKAY)             goto LBL_MU;
64
544
      redux = mp_reduce;
65
544
   } else {
66
313
      if ((err = mp_reduce_2k_setup_l(P, &mu)) != MP_OKAY)        goto LBL_MU;
67
313
      redux = mp_reduce_2k_l;
68
313
   }
69
70
   /* create M table
71
    *
72
    * The M table contains powers of the base,
73
    * e.g. M[x] = G**x mod P
74
    *
75
    * The first half of the table is not
76
    * computed though accept for M[0] and M[1]
77
    */
78
857
   if ((err = mp_mod(G, P, &M[1])) != MP_OKAY)                    goto LBL_MU;
79
80
   /* compute the value at M[1<<(winsize-1)] by squaring
81
    * M[1] (winsize-1) times
82
    */
83
857
   if ((err = mp_copy(&M[1], &M[(size_t)1 << (winsize - 1)])) != MP_OKAY) goto LBL_MU;
84
85
3.02k
   for (x = 0; x < (winsize - 1); x++) {
86
      /* square it */
87
2.16k
      if ((err = mp_sqr(&M[(size_t)1 << (winsize - 1)],
88
2.16k
                        &M[(size_t)1 << (winsize - 1)])) != MP_OKAY) goto LBL_MU;
89
90
      /* reduce modulo P */
91
2.16k
      if ((err = redux(&M[(size_t)1 << (winsize - 1)], P, &mu)) != MP_OKAY) goto LBL_MU;
92
2.16k
   }
93
94
   /* create upper table, that is M[x] = M[x-1] * M[1] (mod P)
95
    * for x = (2**(winsize - 1) + 1) to (2**winsize - 1)
96
    */
97
6.71k
   for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) {
98
5.85k
      if ((err = mp_mul(&M[x - 1], &M[1], &M[x])) != MP_OKAY)     goto LBL_MU;
99
5.85k
      if ((err = redux(&M[x], P, &mu)) != MP_OKAY)                goto LBL_MU;
100
5.85k
   }
101
102
   /* setup result */
103
857
   if ((err = mp_init(&res)) != MP_OKAY)                          goto LBL_MU;
104
857
   mp_set(&res, 1uL);
105
106
   /* set initial mode and bit cnt */
107
857
   mode   = 0;
108
857
   bitcnt = 1;
109
857
   buf    = 0;
110
857
   digidx = X->used - 1;
111
857
   bitcpy = 0;
112
857
   bitbuf = 0;
113
114
83.6k
   for (;;) {
115
      /* grab next digit as required */
116
83.6k
      if (--bitcnt == 0) {
117
         /* if digidx == -1 we are out of digits */
118
2.23k
         if (digidx == -1) {
119
857
            break;
120
857
         }
121
         /* read next digit and reset the bitcnt */
122
1.38k
         buf    = X->dp[digidx--];
123
1.38k
         bitcnt = (int)MP_DIGIT_BIT;
124
1.38k
      }
125
126
      /* grab the next msb from the exponent */
127
82.8k
      y     = (buf >> (mp_digit)(MP_DIGIT_BIT - 1)) & 1uL;
128
82.8k
      buf <<= (mp_digit)1;
129
130
      /* if the bit is zero and mode == 0 then we ignore it
131
       * These represent the leading zero bits before the first 1 bit
132
       * in the exponent.  Technically this opt is not required but it
133
       * does lower the # of trivial squaring/reductions used
134
       */
135
82.8k
      if ((mode == 0) && (y == 0)) {
136
23.8k
         continue;
137
23.8k
      }
138
139
      /* if the bit is zero and mode == 1 then we square */
140
58.9k
      if ((mode == 1) && (y == 0)) {
141
11.9k
         if ((err = mp_sqr(&res, &res)) != MP_OKAY)               goto LBL_RES;
142
11.9k
         if ((err = redux(&res, P, &mu)) != MP_OKAY)              goto LBL_RES;
143
11.9k
         continue;
144
11.9k
      }
145
146
      /* else we add it to the window */
147
47.0k
      bitbuf |= (y << (winsize - ++bitcpy));
148
47.0k
      mode    = 2;
149
150
47.0k
      if (bitcpy == winsize) {
151
         /* ok window is filled so square as required and multiply  */
152
         /* square first */
153
56.3k
         for (x = 0; x < winsize; x++) {
154
46.2k
            if ((err = mp_sqr(&res, &res)) != MP_OKAY)            goto LBL_RES;
155
46.2k
            if ((err = redux(&res, P, &mu)) != MP_OKAY)           goto LBL_RES;
156
46.2k
         }
157
158
         /* then multiply */
159
10.1k
         if ((err = mp_mul(&res, &M[bitbuf], &res)) != MP_OKAY)  goto LBL_RES;
160
10.1k
         if ((err = redux(&res, P, &mu)) != MP_OKAY)             goto LBL_RES;
161
162
         /* empty window and reset */
163
10.1k
         bitcpy = 0;
164
10.1k
         bitbuf = 0;
165
10.1k
         mode   = 1;
166
10.1k
      }
167
47.0k
   }
168
169
   /* if bits remain then square/multiply */
170
857
   if ((mode == 2) && (bitcpy > 0)) {
171
      /* square then multiply if the bit is set */
172
1.12k
      for (x = 0; x < bitcpy; x++) {
173
796
         if ((err = mp_sqr(&res, &res)) != MP_OKAY)               goto LBL_RES;
174
796
         if ((err = redux(&res, P, &mu)) != MP_OKAY)              goto LBL_RES;
175
176
796
         bitbuf <<= 1;
177
796
         if ((bitbuf & (1 << winsize)) != 0) {
178
            /* then multiply */
179
619
            if ((err = mp_mul(&res, &M[1], &res)) != MP_OKAY)     goto LBL_RES;
180
619
            if ((err = redux(&res, P, &mu)) != MP_OKAY)           goto LBL_RES;
181
619
         }
182
796
      }
183
328
   }
184
185
857
   mp_exch(&res, Y);
186
857
   err = MP_OKAY;
187
857
LBL_RES:
188
857
   mp_clear(&res);
189
857
LBL_MU:
190
857
   mp_clear(&mu);
191
857
LBL_M:
192
857
   mp_clear(&M[1]);
193
7.56k
   for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
194
6.71k
      mp_clear(&M[x]);
195
6.71k
   }
196
857
   return err;
197
857
}
198
#endif