Coverage Report

Created: 2023-03-20 06:28

/src/dropbear/libtommath/bn_mp_to_ubin.c
Line
Count
Source (jump to first uncovered line)
1
#include "tommath_private.h"
2
#ifdef BN_MP_TO_UBIN_C
3
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
4
/* SPDX-License-Identifier: Unlicense */
5
6
/* store in unsigned [big endian] format */
7
mp_err mp_to_ubin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *written)
8
2.70k
{
9
2.70k
   size_t  x, count;
10
2.70k
   mp_err  err;
11
2.70k
   mp_int  t;
12
13
2.70k
   count = mp_ubin_size(a);
14
2.70k
   if (count > maxlen) {
15
0
      return MP_BUF;
16
0
   }
17
18
2.70k
   if ((err = mp_init_copy(&t, a)) != MP_OKAY) {
19
0
      return err;
20
0
   }
21
22
341k
   for (x = count; x --> 0u;) {
23
338k
#ifndef MP_8BIT
24
338k
      buf[x] = (unsigned char)(t.dp[0] & 255u);
25
#else
26
      buf[x] = (unsigned char)(t.dp[0] | ((t.dp[1] & 1u) << 7));
27
#endif
28
338k
      if ((err = mp_div_2d(&t, 8, &t, NULL)) != MP_OKAY) {
29
0
         goto LBL_ERR;
30
0
      }
31
338k
   }
32
33
2.70k
   if (written != NULL) {
34
1.62k
      *written = count;
35
1.62k
   }
36
37
2.70k
LBL_ERR:
38
2.70k
   mp_clear(&t);
39
2.70k
   return err;
40
2.70k
}
41
#endif