Mercurial > dropbear
comparison libtommath/bn_mp_fread.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_FREAD_C | 2 #ifdef BN_MP_FREAD_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 #ifndef LTM_NO_FILE | 15 #ifndef LTM_NO_FILE |
19 /* read a bigint from a file stream in ASCII */ | 16 /* read a bigint from a file stream in ASCII */ |
20 int mp_fread(mp_int *a, int radix, FILE *stream) | 17 int mp_fread(mp_int *a, int radix, FILE *stream) |
21 { | 18 { |
22 int err, ch, neg, y; | 19 int err, ch, neg, y; |
23 | 20 unsigned pos; |
21 | |
24 /* clear a */ | 22 /* clear a */ |
25 mp_zero(a); | 23 mp_zero(a); |
26 | 24 |
27 /* if first digit is - then set negative */ | 25 /* if first digit is - then set negative */ |
28 ch = fgetc(stream); | 26 ch = fgetc(stream); |
29 if (ch == '-') { | 27 if (ch == (int)'-') { |
30 neg = MP_NEG; | 28 neg = MP_NEG; |
31 ch = fgetc(stream); | 29 ch = fgetc(stream); |
32 } else { | 30 } else { |
33 neg = MP_ZPOS; | 31 neg = MP_ZPOS; |
34 } | 32 } |
35 | 33 |
36 for (;;) { | 34 for (;;) { |
37 /* find y in the radix map */ | 35 pos = (unsigned)(ch - (int)'('); |
38 for (y = 0; y < radix; y++) { | 36 if (mp_s_rmap_reverse_sz < pos) { |
39 if (mp_s_rmap[y] == ch) { | |
40 break; | |
41 } | |
42 } | |
43 if (y == radix) { | |
44 break; | 37 break; |
45 } | 38 } |
46 | 39 |
40 y = (int)mp_s_rmap_reverse[pos]; | |
41 | |
42 if ((y == 0xff) || (y >= radix)) { | |
43 break; | |
44 } | |
45 | |
47 /* shift up and add */ | 46 /* shift up and add */ |
48 if ((err = mp_mul_d(a, radix, a)) != MP_OKAY) { | 47 if ((err = mp_mul_d(a, (mp_digit)radix, a)) != MP_OKAY) { |
49 return err; | 48 return err; |
50 } | 49 } |
51 if ((err = mp_add_d(a, y, a)) != MP_OKAY) { | 50 if ((err = mp_add_d(a, (mp_digit)y, a)) != MP_OKAY) { |
52 return err; | 51 return err; |
53 } | 52 } |
54 | 53 |
55 ch = fgetc(stream); | 54 ch = fgetc(stream); |
56 } | 55 } |
57 if (mp_cmp_d(a, 0) != MP_EQ) { | 56 if (mp_cmp_d(a, 0uL) != MP_EQ) { |
58 a->sign = neg; | 57 a->sign = neg; |
59 } | 58 } |
60 | 59 |
61 return MP_OKAY; | 60 return MP_OKAY; |
62 } | 61 } |
63 #endif | 62 #endif |
64 | 63 |
65 #endif | 64 #endif |
66 | 65 |
67 /* ref: $Format:%D$ */ | 66 /* ref: HEAD -> master, tag: v1.1.0 */ |
68 /* git commit: $Format:%H$ */ | 67 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */ |
69 /* commit time: $Format:%ai$ */ | 68 /* commit time: 2019-01-28 20:32:32 +0100 */ |