comparison libtommath/bn_mp_toradix_n.c @ 1655:f52919ffd3b1

update ltm to 1.1.0 and enable FIPS 186.4 compliant key-generation (#79) * make key-generation compliant to FIPS 186.4 * fix includes in tommath_class.h * update fuzzcorpus instead of error-out * fixup fuzzing make-targets * update Makefile.in * apply necessary patches to ltm sources * clean-up not required ltm files * update to vanilla ltm 1.1.0 this already only contains the required files * remove set/get double
author Steffen Jaeckel <s_jaeckel@gmx.de>
date Mon, 16 Sep 2019 15:50:38 +0200
parents 8bba51a55704
children
comparison
equal deleted inserted replaced
1654:cc0fc5131c5c 1655:f52919ffd3b1
1 #include <tommath_private.h> 1 #include "tommath_private.h"
2 #ifdef BN_MP_TORADIX_N_C 2 #ifdef BN_MP_TORADIX_N_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis 3 /* LibTomMath, multiple-precision integer library -- Tom St Denis
4 * 4 *
5 * LibTomMath is a library that provides multiple-precision 5 * LibTomMath is a library that provides multiple-precision
6 * integer arithmetic as well as number theoretic functionality. 6 * integer arithmetic as well as number theoretic functionality.
7 * 7 *
8 * The library was designed directly after the MPI library by 8 * The library was designed directly after the MPI library by
9 * Michael Fromberger but has been written from scratch with 9 * Michael Fromberger but has been written from scratch with
10 * additional optimizations in place. 10 * additional optimizations in place.
11 * 11 *
12 * The library is free for all purposes without any express 12 * SPDX-License-Identifier: Unlicense
13 * guarantee it works.
14 *
15 * Tom St Denis, [email protected], http://libtom.org
16 */ 13 */
17 14
18 /* stores a bignum as a ASCII string in a given radix (2..64) 15 /* stores a bignum as a ASCII string in a given radix (2..64)
19 * 16 *
20 * Stores upto maxlen-1 chars and always a NULL byte 17 * Stores upto maxlen-1 chars and always a NULL byte
21 */ 18 */
22 int mp_toradix_n(mp_int * a, char *str, int radix, int maxlen) 19 int mp_toradix_n(const mp_int *a, char *str, int radix, int maxlen)
23 { 20 {
24 int res, digs; 21 int res, digs;
25 mp_int t; 22 mp_int t;
26 mp_digit d; 23 mp_digit d;
27 char *_s = str; 24 char *_s = str;
28 25
29 /* check range of the maxlen, radix */ 26 /* check range of the maxlen, radix */
30 if ((maxlen < 2) || (radix < 2) || (radix > 64)) { 27 if ((maxlen < 2) || (radix < 2) || (radix > 64)) {
31 return MP_VAL; 28 return MP_VAL;
32 } 29 }
33 30
34 /* quick out if its zero */ 31 /* quick out if its zero */
35 if (mp_iszero(a) == MP_YES) { 32 if (mp_iszero(a) == MP_YES) {
36 *str++ = '0'; 33 *str++ = '0';
37 *str = '\0'; 34 *str = '\0';
38 return MP_OKAY; 35 return MP_OKAY;
39 } 36 }
40 37
41 if ((res = mp_init_copy (&t, a)) != MP_OKAY) { 38 if ((res = mp_init_copy(&t, a)) != MP_OKAY) {
42 return res; 39 return res;
43 } 40 }
44 41
45 /* if it is negative output a - */ 42 /* if it is negative output a - */
46 if (t.sign == MP_NEG) { 43 if (t.sign == MP_NEG) {
47 /* we have to reverse our digits later... but not the - sign!! */ 44 /* we have to reverse our digits later... but not the - sign!! */
48 ++_s; 45 ++_s;
49 46
50 /* store the flag and mark the number as positive */ 47 /* store the flag and mark the number as positive */
51 *str++ = '-'; 48 *str++ = '-';
52 t.sign = MP_ZPOS; 49 t.sign = MP_ZPOS;
53
54 /* subtract a char */
55 --maxlen;
56 }
57 50
58 digs = 0; 51 /* subtract a char */
59 while (mp_iszero (&t) == MP_NO) { 52 --maxlen;
60 if (--maxlen < 1) { 53 }
61 /* no more room */
62 break;
63 }
64 if ((res = mp_div_d (&t, (mp_digit) radix, &t, &d)) != MP_OKAY) {
65 mp_clear (&t);
66 return res;
67 }
68 *str++ = mp_s_rmap[d];
69 ++digs;
70 }
71 54
72 /* reverse the digits of the string. In this case _s points 55 digs = 0;
73 * to the first digit [exluding the sign] of the number 56 while (mp_iszero(&t) == MP_NO) {
74 */ 57 if (--maxlen < 1) {
75 bn_reverse ((unsigned char *)_s, digs); 58 /* no more room */
59 break;
60 }
61 if ((res = mp_div_d(&t, (mp_digit)radix, &t, &d)) != MP_OKAY) {
62 mp_clear(&t);
63 return res;
64 }
65 *str++ = mp_s_rmap[d];
66 ++digs;
67 }
76 68
77 /* append a NULL so the string is properly terminated */ 69 /* reverse the digits of the string. In this case _s points
78 *str = '\0'; 70 * to the first digit [exluding the sign] of the number
71 */
72 bn_reverse((unsigned char *)_s, digs);
79 73
80 mp_clear (&t); 74 /* append a NULL so the string is properly terminated */
81 return MP_OKAY; 75 *str = '\0';
76
77 mp_clear(&t);
78 return MP_OKAY;
82 } 79 }
83 80
84 #endif 81 #endif
85 82
86 /* ref: $Format:%D$ */ 83 /* ref: HEAD -> master, tag: v1.1.0 */
87 /* git commit: $Format:%H$ */ 84 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */
88 /* commit time: $Format:%ai$ */ 85 /* commit time: 2019-01-28 20:32:32 +0100 */