comparison libtommath/bn_mp_to_ubin.c @ 1692:1051e4eea25a

Update LibTomMath to 1.2.0 (#84) * update C files * update other files * update headers * update makefiles * remove mp_set/get_double() * use ltm 1.2.0 API * update ltm_desc * use bundled tommath if system-tommath is too old * XMALLOC etc. were changed to MP_MALLOC etc.
author Steffen Jaeckel <s@jaeckel.eu>
date Tue, 26 May 2020 17:36:47 +0200
parents
children
comparison
equal deleted inserted replaced
1691:2d3745d58843 1692:1051e4eea25a
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 {
9 size_t x, count;
10 mp_err err;
11 mp_int t;
12
13 count = mp_ubin_size(a);
14 if (count > maxlen) {
15 return MP_BUF;
16 }
17
18 if ((err = mp_init_copy(&t, a)) != MP_OKAY) {
19 return err;
20 }
21
22 for (x = count; x --> 0u;) {
23 #ifndef MP_8BIT
24 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 if ((err = mp_div_2d(&t, 8, &t, NULL)) != MP_OKAY) {
29 goto LBL_ERR;
30 }
31 }
32
33 if (written != NULL) {
34 *written = count;
35 }
36
37 LBL_ERR:
38 mp_clear(&t);
39 return err;
40 }
41 #endif