Coverage Report

Created: 2025-08-29 06:35

/src/dropbear/libtomcrypt/src/math/ltm_desc.c
Line
Count
Source (jump to first uncovered line)
1
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
2
 *
3
 * LibTomCrypt is a library that provides various cryptographic
4
 * algorithms in a highly modular and flexible manner.
5
 *
6
 * The library is free for all purposes without any express
7
 * guarantee it works.
8
 */
9
10
#define DESC_DEF_ONLY
11
#include "tomcrypt.h"
12
13
#ifdef LTM_DESC
14
15
#include <tommath.h>
16
17
static const struct {
18
    mp_err mpi_code;
19
    int ltc_code;
20
} mpi_to_ltc_codes[] = {
21
   { MP_OKAY ,  CRYPT_OK},
22
   { MP_MEM  ,  CRYPT_MEM},
23
   { MP_VAL  ,  CRYPT_INVALID_ARG},
24
   { MP_ITER ,  CRYPT_INVALID_PACKET},
25
   { MP_BUF  ,  CRYPT_BUFFER_OVERFLOW},
26
};
27
28
/**
29
   Convert a MPI error to a LTC error (Possibly the most powerful function ever!  Oh wait... no)
30
   @param err    The error to convert
31
   @return The equivalent LTC error code or CRYPT_ERROR if none found
32
*/
33
static int mpi_to_ltc_error(mp_err err)
34
0
{
35
0
   size_t x;
36
37
0
   for (x = 0; x < sizeof(mpi_to_ltc_codes)/sizeof(mpi_to_ltc_codes[0]); x++) {
38
0
       if (err == mpi_to_ltc_codes[x].mpi_code) {
39
0
          return mpi_to_ltc_codes[x].ltc_code;
40
0
       }
41
0
   }
42
0
   return CRYPT_ERROR;
43
0
}
44
45
static int init_mpi(void **a)
46
0
{
47
0
   LTC_ARGCHK(a != NULL);
48
49
0
   *a = XCALLOC(1, sizeof(mp_int));
50
0
   if (*a == NULL) {
51
0
      return CRYPT_MEM;
52
0
   } else {
53
0
      return CRYPT_OK;
54
0
   }
55
0
}
56
57
static int init(void **a)
58
0
{
59
0
   int err;
60
61
0
   LTC_ARGCHK(a != NULL);
62
63
0
   if ((err = init_mpi(a)) != CRYPT_OK) {
64
0
      return err;
65
0
   }
66
0
   if ((err = mpi_to_ltc_error(mp_init(*a))) != CRYPT_OK) {
67
0
      XFREE(*a);
68
0
   }
69
0
   return err;
70
0
}
71
72
static void deinit(void *a)
73
0
{
74
0
   LTC_ARGCHKVD(a != NULL);
75
0
   mp_clear(a);
76
0
   XFREE(a);
77
0
}
78
79
static int neg(void *a, void *b)
80
0
{
81
0
   LTC_ARGCHK(a != NULL);
82
0
   LTC_ARGCHK(b != NULL);
83
0
   return mpi_to_ltc_error(mp_neg(a, b));
84
0
}
85
86
static int copy(void *a, void *b)
87
0
{
88
0
   LTC_ARGCHK(a != NULL);
89
0
   LTC_ARGCHK(b != NULL);
90
0
   return mpi_to_ltc_error(mp_copy(a, b));
91
0
}
92
93
static int init_copy(void **a, void *b)
94
0
{
95
0
   int err;
96
0
   LTC_ARGCHK(a  != NULL);
97
0
   LTC_ARGCHK(b  != NULL);
98
0
   if ((err = init_mpi(a)) != CRYPT_OK) return err;
99
0
   return mpi_to_ltc_error(mp_init_copy(*a, b));
100
0
}
101
102
/* ---- trivial ---- */
103
static int set_int(void *a, ltc_mp_digit b)
104
0
{
105
0
   LTC_ARGCHK(a != NULL);
106
0
   mp_set_u32(a, b);
107
0
   return CRYPT_OK;
108
0
}
109
110
static unsigned long get_int(void *a)
111
0
{
112
0
   LTC_ARGCHK(a != NULL);
113
0
   return mp_get_ul(a);
114
0
}
115
116
static ltc_mp_digit get_digit(void *a, int n)
117
0
{
118
0
   mp_int *A;
119
0
   LTC_ARGCHK(a != NULL);
120
0
   A = a;
121
0
   return (n >= A->used || n < 0) ? 0 : A->dp[n];
122
0
}
123
124
static int get_digit_count(void *a)
125
0
{
126
0
   mp_int *A;
127
0
   LTC_ARGCHK(a != NULL);
128
0
   A = a;
129
0
   return A->used;
130
0
}
131
132
static int compare(void *a, void *b)
133
0
{
134
0
   LTC_ARGCHK(a != NULL);
135
0
   LTC_ARGCHK(b != NULL);
136
0
   switch (mp_cmp(a, b)) {
137
0
      case MP_LT: return LTC_MP_LT;
138
0
      case MP_EQ: return LTC_MP_EQ;
139
0
      case MP_GT: return LTC_MP_GT;
140
0
      default:    return 0;
141
0
   }
142
0
}
143
144
static int compare_d(void *a, ltc_mp_digit b)
145
0
{
146
0
   LTC_ARGCHK(a != NULL);
147
0
   switch (mp_cmp_d(a, b)) {
148
0
      case MP_LT: return LTC_MP_LT;
149
0
      case MP_EQ: return LTC_MP_EQ;
150
0
      case MP_GT: return LTC_MP_GT;
151
0
      default:    return 0;
152
0
   }
153
0
}
154
155
static int count_bits(void *a)
156
0
{
157
0
   LTC_ARGCHK(a != NULL);
158
0
   return mp_count_bits(a);
159
0
}
160
161
static int count_lsb_bits(void *a)
162
0
{
163
0
   LTC_ARGCHK(a != NULL);
164
0
   return mp_cnt_lsb(a);
165
0
}
166
167
168
static int twoexpt(void *a, int n)
169
0
{
170
0
   LTC_ARGCHK(a != NULL);
171
0
   return mpi_to_ltc_error(mp_2expt(a, n));
172
0
}
173
174
/* ---- conversions ---- */
175
176
/* read ascii string */
177
static int read_radix(void *a, const char *b, int radix)
178
0
{
179
0
   LTC_ARGCHK(a != NULL);
180
0
   LTC_ARGCHK(b != NULL);
181
0
   return mpi_to_ltc_error(mp_read_radix(a, b, radix));
182
0
}
183
184
/* write one */
185
static int write_radix(void *a, char *b, int radix)
186
0
{
187
0
   LTC_ARGCHK(a != NULL);
188
0
   LTC_ARGCHK(b != NULL);
189
0
   return mpi_to_ltc_error(mp_to_radix(a, b, SIZE_MAX, NULL, radix));
190
0
}
191
192
/* get size as unsigned char string */
193
static unsigned long unsigned_size(void *a)
194
0
{
195
0
   LTC_ARGCHK(a != NULL);
196
0
   return (unsigned long)mp_ubin_size(a);
197
0
}
198
199
/* store */
200
static int unsigned_write(void *a, unsigned char *b)
201
0
{
202
0
   LTC_ARGCHK(a != NULL);
203
0
   LTC_ARGCHK(b != NULL);
204
0
   return mpi_to_ltc_error(mp_to_ubin(a, b, SIZE_MAX, NULL));
205
0
}
206
207
/* read */
208
static int unsigned_read(void *a, unsigned char *b, unsigned long len)
209
0
{
210
0
   LTC_ARGCHK(a != NULL);
211
0
   LTC_ARGCHK(b != NULL);
212
0
   return mpi_to_ltc_error(mp_from_ubin(a, b, (size_t)len));
213
0
}
214
215
/* add */
216
static int add(void *a, void *b, void *c)
217
0
{
218
0
   LTC_ARGCHK(a != NULL);
219
0
   LTC_ARGCHK(b != NULL);
220
0
   LTC_ARGCHK(c != NULL);
221
0
   return mpi_to_ltc_error(mp_add(a, b, c));
222
0
}
223
224
static int addi(void *a, ltc_mp_digit b, void *c)
225
0
{
226
0
   LTC_ARGCHK(a != NULL);
227
0
   LTC_ARGCHK(c != NULL);
228
0
   return mpi_to_ltc_error(mp_add_d(a, b, c));
229
0
}
230
231
/* sub */
232
static int sub(void *a, void *b, void *c)
233
0
{
234
0
   LTC_ARGCHK(a != NULL);
235
0
   LTC_ARGCHK(b != NULL);
236
0
   LTC_ARGCHK(c != NULL);
237
0
   return mpi_to_ltc_error(mp_sub(a, b, c));
238
0
}
239
240
static int subi(void *a, ltc_mp_digit b, void *c)
241
0
{
242
0
   LTC_ARGCHK(a != NULL);
243
0
   LTC_ARGCHK(c != NULL);
244
0
   return mpi_to_ltc_error(mp_sub_d(a, b, c));
245
0
}
246
247
/* mul */
248
static int mul(void *a, void *b, void *c)
249
0
{
250
0
   LTC_ARGCHK(a != NULL);
251
0
   LTC_ARGCHK(b != NULL);
252
0
   LTC_ARGCHK(c != NULL);
253
0
   return mpi_to_ltc_error(mp_mul(a, b, c));
254
0
}
255
256
static int muli(void *a, ltc_mp_digit b, void *c)
257
0
{
258
0
   LTC_ARGCHK(a != NULL);
259
0
   LTC_ARGCHK(c != NULL);
260
0
   return mpi_to_ltc_error(mp_mul_d(a, b, c));
261
0
}
262
263
/* sqr */
264
static int sqr(void *a, void *b)
265
0
{
266
0
   LTC_ARGCHK(a != NULL);
267
0
   LTC_ARGCHK(b != NULL);
268
0
   return mpi_to_ltc_error(mp_sqr(a, b));
269
0
}
270
271
/* div */
272
static int divide(void *a, void *b, void *c, void *d)
273
0
{
274
0
   LTC_ARGCHK(a != NULL);
275
0
   LTC_ARGCHK(b != NULL);
276
0
   return mpi_to_ltc_error(mp_div(a, b, c, d));
277
0
}
278
279
static int div_2(void *a, void *b)
280
0
{
281
0
   LTC_ARGCHK(a != NULL);
282
0
   LTC_ARGCHK(b != NULL);
283
0
   return mpi_to_ltc_error(mp_div_2(a, b));
284
0
}
285
286
/* modi */
287
static int modi(void *a, ltc_mp_digit b, ltc_mp_digit *c)
288
0
{
289
0
   mp_digit tmp;
290
0
   int      err;
291
292
0
   LTC_ARGCHK(a != NULL);
293
0
   LTC_ARGCHK(c != NULL);
294
295
0
   if ((err = mpi_to_ltc_error(mp_mod_d(a, b, &tmp))) != CRYPT_OK) {
296
0
      return err;
297
0
   }
298
0
   *c = tmp;
299
0
   return CRYPT_OK;
300
0
}
301
302
/* gcd */
303
static int gcd(void *a, void *b, void *c)
304
0
{
305
0
   LTC_ARGCHK(a != NULL);
306
0
   LTC_ARGCHK(b != NULL);
307
0
   LTC_ARGCHK(c != NULL);
308
0
   return mpi_to_ltc_error(mp_gcd(a, b, c));
309
0
}
310
311
/* lcm */
312
static int lcm(void *a, void *b, void *c)
313
0
{
314
0
   LTC_ARGCHK(a != NULL);
315
0
   LTC_ARGCHK(b != NULL);
316
0
   LTC_ARGCHK(c != NULL);
317
0
   return mpi_to_ltc_error(mp_lcm(a, b, c));
318
0
}
319
320
static int addmod(void *a, void *b, void *c, void *d)
321
0
{
322
0
   LTC_ARGCHK(a != NULL);
323
0
   LTC_ARGCHK(b != NULL);
324
0
   LTC_ARGCHK(c != NULL);
325
0
   LTC_ARGCHK(d != NULL);
326
0
   return mpi_to_ltc_error(mp_addmod(a,b,c,d));
327
0
}
328
329
static int submod(void *a, void *b, void *c, void *d)
330
0
{
331
0
   LTC_ARGCHK(a != NULL);
332
0
   LTC_ARGCHK(b != NULL);
333
0
   LTC_ARGCHK(c != NULL);
334
0
   LTC_ARGCHK(d != NULL);
335
0
   return mpi_to_ltc_error(mp_submod(a,b,c,d));
336
0
}
337
338
static int mulmod(void *a, void *b, void *c, void *d)
339
0
{
340
0
   LTC_ARGCHK(a != NULL);
341
0
   LTC_ARGCHK(b != NULL);
342
0
   LTC_ARGCHK(c != NULL);
343
0
   LTC_ARGCHK(d != NULL);
344
0
   return mpi_to_ltc_error(mp_mulmod(a,b,c,d));
345
0
}
346
347
static int sqrmod(void *a, void *b, void *c)
348
0
{
349
0
   LTC_ARGCHK(a != NULL);
350
0
   LTC_ARGCHK(b != NULL);
351
0
   LTC_ARGCHK(c != NULL);
352
0
   return mpi_to_ltc_error(mp_sqrmod(a,b,c));
353
0
}
354
355
/* invmod */
356
static int invmod(void *a, void *b, void *c)
357
0
{
358
0
   LTC_ARGCHK(a != NULL);
359
0
   LTC_ARGCHK(b != NULL);
360
0
   LTC_ARGCHK(c != NULL);
361
0
   return mpi_to_ltc_error(mp_invmod(a, b, c));
362
0
}
363
364
/* setup */
365
static int montgomery_setup(void *a, void **b)
366
0
{
367
0
   int err;
368
0
   LTC_ARGCHK(a != NULL);
369
0
   LTC_ARGCHK(b != NULL);
370
0
   *b = XCALLOC(1, sizeof(mp_digit));
371
0
   if (*b == NULL) {
372
0
      return CRYPT_MEM;
373
0
   }
374
0
   if ((err = mpi_to_ltc_error(mp_montgomery_setup(a, (mp_digit *)*b))) != CRYPT_OK) {
375
0
      XFREE(*b);
376
0
   }
377
0
   return err;
378
0
}
379
380
/* get normalization value */
381
static int montgomery_normalization(void *a, void *b)
382
0
{
383
0
   LTC_ARGCHK(a != NULL);
384
0
   LTC_ARGCHK(b != NULL);
385
0
   return mpi_to_ltc_error(mp_montgomery_calc_normalization(a, b));
386
0
}
387
388
/* reduce */
389
static int montgomery_reduce(void *a, void *b, void *c)
390
0
{
391
0
   LTC_ARGCHK(a != NULL);
392
0
   LTC_ARGCHK(b != NULL);
393
0
   LTC_ARGCHK(c != NULL);
394
0
   return mpi_to_ltc_error(mp_montgomery_reduce(a, b, *((mp_digit *)c)));
395
0
}
396
397
/* clean up */
398
static void montgomery_deinit(void *a)
399
0
{
400
0
   XFREE(a);
401
0
}
402
403
static int exptmod(void *a, void *b, void *c, void *d)
404
0
{
405
0
   LTC_ARGCHK(a != NULL);
406
0
   LTC_ARGCHK(b != NULL);
407
0
   LTC_ARGCHK(c != NULL);
408
0
   LTC_ARGCHK(d != NULL);
409
0
   return mpi_to_ltc_error(mp_exptmod(a,b,c,d));
410
0
}
411
412
static int isprime(void *a, int b, int *c)
413
0
{
414
0
   int err;
415
0
   LTC_ARGCHK(a != NULL);
416
0
   LTC_ARGCHK(c != NULL);
417
0
   b = mp_prime_rabin_miller_trials(mp_count_bits(a));
418
0
   err = mpi_to_ltc_error(mp_prime_is_prime(a, b, c));
419
0
   *c = (*c == MP_YES) ? LTC_MP_YES : LTC_MP_NO;
420
0
   return err;
421
0
}
422
423
static int set_rand(void *a, int size)
424
0
{
425
0
   LTC_ARGCHK(a != NULL);
426
0
   return mpi_to_ltc_error(mp_rand(a, size));
427
0
}
428
429
const ltc_math_descriptor ltm_desc = {
430
431
   "LibTomMath",
432
   (int)MP_DIGIT_BIT,
433
434
   &init,
435
   &init_copy,
436
   &deinit,
437
438
   &neg,
439
   &copy,
440
441
   &set_int,
442
   &get_int,
443
   &get_digit,
444
   &get_digit_count,
445
   &compare,
446
   &compare_d,
447
   &count_bits,
448
   &count_lsb_bits,
449
   &twoexpt,
450
451
   &read_radix,
452
   &write_radix,
453
   &unsigned_size,
454
   &unsigned_write,
455
   &unsigned_read,
456
457
   &add,
458
   &addi,
459
   &sub,
460
   &subi,
461
   &mul,
462
   &muli,
463
   &sqr,
464
   &divide,
465
   &div_2,
466
   &modi,
467
   &gcd,
468
   &lcm,
469
470
   &mulmod,
471
   &sqrmod,
472
   &invmod,
473
474
   &montgomery_setup,
475
   &montgomery_normalization,
476
   &montgomery_reduce,
477
   &montgomery_deinit,
478
479
   &exptmod,
480
   &isprime,
481
482
#ifdef LTC_MECC
483
#ifdef LTC_MECC_FP
484
   &ltc_ecc_fp_mulmod,
485
#else
486
   &ltc_ecc_mulmod,
487
#endif
488
   &ltc_ecc_projective_add_point,
489
   &ltc_ecc_projective_dbl_point,
490
   &ltc_ecc_map,
491
#ifdef LTC_ECC_SHAMIR
492
#ifdef LTC_MECC_FP
493
   &ltc_ecc_fp_mul2add,
494
#else
495
   &ltc_ecc_mul2add,
496
#endif /* LTC_MECC_FP */
497
#else
498
   NULL,
499
#endif /* LTC_ECC_SHAMIR */
500
#else
501
   NULL, NULL, NULL, NULL, NULL,
502
#endif /* LTC_MECC */
503
504
#ifdef LTC_MRSA
505
   &rsa_make_key,
506
   &rsa_exptmod,
507
#else
508
   NULL, NULL,
509
#endif
510
   &addmod,
511
   &submod,
512
513
   &set_rand,
514
515
};
516
517
518
#endif
519
520
/* ref:         $Format:%D$ */
521
/* git commit:  $Format:%H$ */
522
/* commit time: $Format:%ai$ */