Mercurial > dropbear
comparison libtommath/bn_mp_exteuclid.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_EXTEUCLID_C | 2 #ifdef BN_MP_EXTEUCLID_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 /* Extended euclidean algorithm of (a, b) produces | 15 /* Extended euclidean algorithm of (a, b) produces |
19 a*u1 + b*u2 = u3 | 16 a*u1 + b*u2 = u3 |
20 */ | 17 */ |
21 int mp_exteuclid(mp_int *a, mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3) | 18 int mp_exteuclid(const mp_int *a, const mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3) |
22 { | 19 { |
23 mp_int u1,u2,u3,v1,v2,v3,t1,t2,t3,q,tmp; | 20 mp_int u1, u2, u3, v1, v2, v3, t1, t2, t3, q, tmp; |
24 int err; | 21 int err; |
25 | 22 |
26 if ((err = mp_init_multi(&u1, &u2, &u3, &v1, &v2, &v3, &t1, &t2, &t3, &q, &tmp, NULL)) != MP_OKAY) { | 23 if ((err = mp_init_multi(&u1, &u2, &u3, &v1, &v2, &v3, &t1, &t2, &t3, &q, &tmp, NULL)) != MP_OKAY) { |
27 return err; | 24 return err; |
28 } | 25 } |
29 | 26 |
30 /* initialize, (u1,u2,u3) = (1,0,a) */ | 27 /* initialize, (u1,u2,u3) = (1,0,a) */ |
31 mp_set(&u1, 1); | 28 mp_set(&u1, 1uL); |
32 if ((err = mp_copy(a, &u3)) != MP_OKAY) { goto LBL_ERR; } | 29 if ((err = mp_copy(a, &u3)) != MP_OKAY) { |
30 goto LBL_ERR; | |
31 } | |
33 | 32 |
34 /* initialize, (v1,v2,v3) = (0,1,b) */ | 33 /* initialize, (v1,v2,v3) = (0,1,b) */ |
35 mp_set(&v2, 1); | 34 mp_set(&v2, 1uL); |
36 if ((err = mp_copy(b, &v3)) != MP_OKAY) { goto LBL_ERR; } | 35 if ((err = mp_copy(b, &v3)) != MP_OKAY) { |
36 goto LBL_ERR; | |
37 } | |
37 | 38 |
38 /* loop while v3 != 0 */ | 39 /* loop while v3 != 0 */ |
39 while (mp_iszero(&v3) == MP_NO) { | 40 while (mp_iszero(&v3) == MP_NO) { |
40 /* q = u3/v3 */ | 41 /* q = u3/v3 */ |
41 if ((err = mp_div(&u3, &v3, &q, NULL)) != MP_OKAY) { goto LBL_ERR; } | 42 if ((err = mp_div(&u3, &v3, &q, NULL)) != MP_OKAY) { |
43 goto LBL_ERR; | |
44 } | |
42 | 45 |
43 /* (t1,t2,t3) = (u1,u2,u3) - (v1,v2,v3)q */ | 46 /* (t1,t2,t3) = (u1,u2,u3) - (v1,v2,v3)q */ |
44 if ((err = mp_mul(&v1, &q, &tmp)) != MP_OKAY) { goto LBL_ERR; } | 47 if ((err = mp_mul(&v1, &q, &tmp)) != MP_OKAY) { |
45 if ((err = mp_sub(&u1, &tmp, &t1)) != MP_OKAY) { goto LBL_ERR; } | 48 goto LBL_ERR; |
46 if ((err = mp_mul(&v2, &q, &tmp)) != MP_OKAY) { goto LBL_ERR; } | 49 } |
47 if ((err = mp_sub(&u2, &tmp, &t2)) != MP_OKAY) { goto LBL_ERR; } | 50 if ((err = mp_sub(&u1, &tmp, &t1)) != MP_OKAY) { |
48 if ((err = mp_mul(&v3, &q, &tmp)) != MP_OKAY) { goto LBL_ERR; } | 51 goto LBL_ERR; |
49 if ((err = mp_sub(&u3, &tmp, &t3)) != MP_OKAY) { goto LBL_ERR; } | 52 } |
53 if ((err = mp_mul(&v2, &q, &tmp)) != MP_OKAY) { | |
54 goto LBL_ERR; | |
55 } | |
56 if ((err = mp_sub(&u2, &tmp, &t2)) != MP_OKAY) { | |
57 goto LBL_ERR; | |
58 } | |
59 if ((err = mp_mul(&v3, &q, &tmp)) != MP_OKAY) { | |
60 goto LBL_ERR; | |
61 } | |
62 if ((err = mp_sub(&u3, &tmp, &t3)) != MP_OKAY) { | |
63 goto LBL_ERR; | |
64 } | |
50 | 65 |
51 /* (u1,u2,u3) = (v1,v2,v3) */ | 66 /* (u1,u2,u3) = (v1,v2,v3) */ |
52 if ((err = mp_copy(&v1, &u1)) != MP_OKAY) { goto LBL_ERR; } | 67 if ((err = mp_copy(&v1, &u1)) != MP_OKAY) { |
53 if ((err = mp_copy(&v2, &u2)) != MP_OKAY) { goto LBL_ERR; } | 68 goto LBL_ERR; |
54 if ((err = mp_copy(&v3, &u3)) != MP_OKAY) { goto LBL_ERR; } | 69 } |
70 if ((err = mp_copy(&v2, &u2)) != MP_OKAY) { | |
71 goto LBL_ERR; | |
72 } | |
73 if ((err = mp_copy(&v3, &u3)) != MP_OKAY) { | |
74 goto LBL_ERR; | |
75 } | |
55 | 76 |
56 /* (v1,v2,v3) = (t1,t2,t3) */ | 77 /* (v1,v2,v3) = (t1,t2,t3) */ |
57 if ((err = mp_copy(&t1, &v1)) != MP_OKAY) { goto LBL_ERR; } | 78 if ((err = mp_copy(&t1, &v1)) != MP_OKAY) { |
58 if ((err = mp_copy(&t2, &v2)) != MP_OKAY) { goto LBL_ERR; } | 79 goto LBL_ERR; |
59 if ((err = mp_copy(&t3, &v3)) != MP_OKAY) { goto LBL_ERR; } | 80 } |
81 if ((err = mp_copy(&t2, &v2)) != MP_OKAY) { | |
82 goto LBL_ERR; | |
83 } | |
84 if ((err = mp_copy(&t3, &v3)) != MP_OKAY) { | |
85 goto LBL_ERR; | |
86 } | |
60 } | 87 } |
61 | 88 |
62 /* make sure U3 >= 0 */ | 89 /* make sure U3 >= 0 */ |
63 if (u3.sign == MP_NEG) { | 90 if (u3.sign == MP_NEG) { |
64 if ((err = mp_neg(&u1, &u1)) != MP_OKAY) { goto LBL_ERR; } | 91 if ((err = mp_neg(&u1, &u1)) != MP_OKAY) { |
65 if ((err = mp_neg(&u2, &u2)) != MP_OKAY) { goto LBL_ERR; } | 92 goto LBL_ERR; |
66 if ((err = mp_neg(&u3, &u3)) != MP_OKAY) { goto LBL_ERR; } | 93 } |
94 if ((err = mp_neg(&u2, &u2)) != MP_OKAY) { | |
95 goto LBL_ERR; | |
96 } | |
97 if ((err = mp_neg(&u3, &u3)) != MP_OKAY) { | |
98 goto LBL_ERR; | |
99 } | |
67 } | 100 } |
68 | 101 |
69 /* copy result out */ | 102 /* copy result out */ |
70 if (U1 != NULL) { mp_exch(U1, &u1); } | 103 if (U1 != NULL) { |
71 if (U2 != NULL) { mp_exch(U2, &u2); } | 104 mp_exch(U1, &u1); |
72 if (U3 != NULL) { mp_exch(U3, &u3); } | 105 } |
106 if (U2 != NULL) { | |
107 mp_exch(U2, &u2); | |
108 } | |
109 if (U3 != NULL) { | |
110 mp_exch(U3, &u3); | |
111 } | |
73 | 112 |
74 err = MP_OKAY; | 113 err = MP_OKAY; |
75 LBL_ERR: | 114 LBL_ERR: |
76 mp_clear_multi(&u1, &u2, &u3, &v1, &v2, &v3, &t1, &t2, &t3, &q, &tmp, NULL); | 115 mp_clear_multi(&u1, &u2, &u3, &v1, &v2, &v3, &t1, &t2, &t3, &q, &tmp, NULL); |
77 return err; | 116 return err; |
78 } | 117 } |
79 #endif | 118 #endif |
80 | 119 |
81 /* ref: $Format:%D$ */ | 120 /* ref: HEAD -> master, tag: v1.1.0 */ |
82 /* git commit: $Format:%H$ */ | 121 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */ |
83 /* commit time: $Format:%ai$ */ | 122 /* commit time: 2019-01-28 20:32:32 +0100 */ |