Mercurial > dropbear
comparison libtommath/bn_mp_read_radix.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 | 1051e4eea25a |
comparison
equal
deleted
inserted
replaced
1654:cc0fc5131c5c | 1655:f52919ffd3b1 |
---|---|
1 #include <tommath_private.h> | 1 #include "tommath_private.h" |
2 #ifdef BN_MP_READ_RADIX_C | 2 #ifdef BN_MP_READ_RADIX_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 /* read a string [ASCII] in a given radix */ | 15 /* read a string [ASCII] in a given radix */ |
19 int mp_read_radix (mp_int * a, const char *str, int radix) | 16 int mp_read_radix(mp_int *a, const char *str, int radix) |
20 { | 17 { |
21 int y, res, neg; | 18 int y, res, neg; |
22 char ch; | 19 unsigned pos; |
20 char ch; | |
23 | 21 |
24 /* zero the digit bignum */ | 22 /* zero the digit bignum */ |
25 mp_zero(a); | 23 mp_zero(a); |
26 | 24 |
27 /* make sure the radix is ok */ | 25 /* make sure the radix is ok */ |
28 if ((radix < 2) || (radix > 64)) { | 26 if ((radix < 2) || (radix > 64)) { |
29 return MP_VAL; | 27 return MP_VAL; |
30 } | 28 } |
31 | 29 |
32 /* if the leading digit is a | 30 /* if the leading digit is a |
33 * minus set the sign to negative. | 31 * minus set the sign to negative. |
34 */ | 32 */ |
35 if (*str == '-') { | 33 if (*str == '-') { |
36 ++str; | 34 ++str; |
37 neg = MP_NEG; | 35 neg = MP_NEG; |
38 } else { | 36 } else { |
39 neg = MP_ZPOS; | 37 neg = MP_ZPOS; |
40 } | 38 } |
41 | 39 |
42 /* set the integer to the default of zero */ | 40 /* set the integer to the default of zero */ |
43 mp_zero (a); | 41 mp_zero(a); |
44 | 42 |
45 /* process each digit of the string */ | 43 /* process each digit of the string */ |
46 while (*str != '\0') { | 44 while (*str != '\0') { |
47 /* if the radix <= 36 the conversion is case insensitive | 45 /* if the radix <= 36 the conversion is case insensitive |
48 * this allows numbers like 1AB and 1ab to represent the same value | 46 * this allows numbers like 1AB and 1ab to represent the same value |
49 * [e.g. in hex] | 47 * [e.g. in hex] |
50 */ | 48 */ |
51 ch = (radix <= 36) ? (char)toupper((int)*str) : *str; | 49 ch = (radix <= 36) ? (char)toupper((int)*str) : *str; |
52 for (y = 0; y < 64; y++) { | 50 pos = (unsigned)(ch - '('); |
53 if (ch == mp_s_rmap[y]) { | 51 if (mp_s_rmap_reverse_sz < pos) { |
54 break; | 52 break; |
55 } | 53 } |
56 } | 54 y = (int)mp_s_rmap_reverse[pos]; |
57 | 55 |
58 /* if the char was found in the map | 56 /* if the char was found in the map |
59 * and is less than the given radix add it | 57 * and is less than the given radix add it |
60 * to the number, otherwise exit the loop. | 58 * to the number, otherwise exit the loop. |
61 */ | 59 */ |
62 if (y < radix) { | 60 if ((y == 0xff) || (y >= radix)) { |
63 if ((res = mp_mul_d (a, (mp_digit) radix, a)) != MP_OKAY) { | 61 break; |
62 } | |
63 if ((res = mp_mul_d(a, (mp_digit)radix, a)) != MP_OKAY) { | |
64 return res; | 64 return res; |
65 } | 65 } |
66 if ((res = mp_add_d (a, (mp_digit) y, a)) != MP_OKAY) { | 66 if ((res = mp_add_d(a, (mp_digit)y, a)) != MP_OKAY) { |
67 return res; | 67 return res; |
68 } | 68 } |
69 } else { | 69 ++str; |
70 break; | 70 } |
71 } | 71 |
72 ++str; | 72 /* if an illegal character was found, fail. */ |
73 } | 73 if (!((*str == '\0') || (*str == '\r') || (*str == '\n'))) { |
74 | 74 mp_zero(a); |
75 /* set the sign only if a != 0 */ | 75 return MP_VAL; |
76 if (mp_iszero(a) != MP_YES) { | 76 } |
77 a->sign = neg; | 77 |
78 } | 78 /* set the sign only if a != 0 */ |
79 return MP_OKAY; | 79 if (mp_iszero(a) != MP_YES) { |
80 a->sign = neg; | |
81 } | |
82 return MP_OKAY; | |
80 } | 83 } |
81 #endif | 84 #endif |
82 | 85 |
83 /* ref: $Format:%D$ */ | 86 /* ref: HEAD -> master, tag: v1.1.0 */ |
84 /* git commit: $Format:%H$ */ | 87 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */ |
85 /* commit time: $Format:%ai$ */ | 88 /* commit time: 2019-01-28 20:32:32 +0100 */ |