author | zautrix <zautrix> | 2004-10-19 20:16:14 (UTC) |
---|---|---|
committer | zautrix <zautrix> | 2004-10-19 20:16:14 (UTC) |
commit | eca49bb06a71980ef61d078904573f25890fc7f2 (patch) (unidiff) | |
tree | c5338e3b12430248979a9ac2c1c7e6646ea9ecdf /pwmanager/libcrypt/mpi/mpi-mul.c | |
parent | 53cc32b6e7b1f672bf91b2baf2df6c1e8baf3e0a (diff) | |
download | kdepimpi-eca49bb06a71980ef61d078904573f25890fc7f2.zip kdepimpi-eca49bb06a71980ef61d078904573f25890fc7f2.tar.gz kdepimpi-eca49bb06a71980ef61d078904573f25890fc7f2.tar.bz2 |
Initial revision
Diffstat (limited to 'pwmanager/libcrypt/mpi/mpi-mul.c') (more/less context) (ignore whitespace changes)
-rw-r--r-- | pwmanager/libcrypt/mpi/mpi-mul.c | 220 |
1 files changed, 220 insertions, 0 deletions
diff --git a/pwmanager/libcrypt/mpi/mpi-mul.c b/pwmanager/libcrypt/mpi/mpi-mul.c new file mode 100644 index 0000000..23beee0 --- a/dev/null +++ b/pwmanager/libcrypt/mpi/mpi-mul.c | |||
@@ -0,0 +1,220 @@ | |||
1 | /* mpi-mul.c - MPI functions | ||
2 | * Copyright (C) 1994, 1996, 1998, 2001, 2002, 2003 Free Software Foundation, Inc. | ||
3 | * | ||
4 | * This file is part of Libgcrypt. | ||
5 | * | ||
6 | * Libgcrypt is free software; you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU Lesser General Public License as | ||
8 | * published by the Free Software Foundation; either version 2.1 of | ||
9 | * the License, or (at your option) any later version. | ||
10 | * | ||
11 | * Libgcrypt is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | * GNU Lesser General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU Lesser General Public | ||
17 | * License along with this program; if not, write to the Free Software | ||
18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA | ||
19 | * | ||
20 | * Note: This code is heavily based on the GNU MP Library. | ||
21 | * Actually it's the same code with only minor changes in the | ||
22 | * way the data is stored; this is to support the abstraction | ||
23 | * of an optional secure memory allocation which may be used | ||
24 | * to avoid revealing of sensitive data due to paging etc. | ||
25 | */ | ||
26 | |||
27 | #include <config.h> | ||
28 | #include <stdio.h> | ||
29 | #include <stdlib.h> | ||
30 | #include "mpi-internal.h" | ||
31 | |||
32 | |||
33 | void | ||
34 | gcry_mpi_mul_ui( gcry_mpi_t prod, gcry_mpi_t mult, unsigned long small_mult ) | ||
35 | { | ||
36 | mpi_size_t size, prod_size; | ||
37 | mpi_ptr_t prod_ptr; | ||
38 | mpi_limb_t cy; | ||
39 | int sign; | ||
40 | |||
41 | size = mult->nlimbs; | ||
42 | sign = mult->sign; | ||
43 | |||
44 | if( !size || !small_mult ) { | ||
45 | prod->nlimbs = 0; | ||
46 | prod->sign = 0; | ||
47 | return; | ||
48 | } | ||
49 | |||
50 | prod_size = size + 1; | ||
51 | if( prod->alloced < prod_size ) | ||
52 | mpi_resize( prod, prod_size ); | ||
53 | prod_ptr = prod->d; | ||
54 | |||
55 | cy = _gcry_mpih_mul_1( prod_ptr, mult->d, size, (mpi_limb_t)small_mult ); | ||
56 | if( cy ) | ||
57 | prod_ptr[size++] = cy; | ||
58 | prod->nlimbs = size; | ||
59 | prod->sign = sign; | ||
60 | } | ||
61 | |||
62 | |||
63 | void | ||
64 | _gcry_mpi_mul_2exp( gcry_mpi_t w, gcry_mpi_t u, unsigned long cnt) | ||
65 | { | ||
66 | mpi_size_t usize, wsize, limb_cnt; | ||
67 | mpi_ptr_t wp; | ||
68 | mpi_limb_t wlimb; | ||
69 | int usign, wsign; | ||
70 | |||
71 | usize = u->nlimbs; | ||
72 | usign = u->sign; | ||
73 | |||
74 | if( !usize ) { | ||
75 | w->nlimbs = 0; | ||
76 | w->sign = 0; | ||
77 | return; | ||
78 | } | ||
79 | |||
80 | limb_cnt = cnt / BITS_PER_MPI_LIMB; | ||
81 | wsize = usize + limb_cnt + 1; | ||
82 | if( w->alloced < wsize ) | ||
83 | mpi_resize(w, wsize ); | ||
84 | wp = w->d; | ||
85 | wsize = usize + limb_cnt; | ||
86 | wsign = usign; | ||
87 | |||
88 | cnt %= BITS_PER_MPI_LIMB; | ||
89 | if( cnt ) { | ||
90 | wlimb = _gcry_mpih_lshift( wp + limb_cnt, u->d, usize, cnt ); | ||
91 | if( wlimb ) { | ||
92 | wp[wsize] = wlimb; | ||
93 | wsize++; | ||
94 | } | ||
95 | } | ||
96 | else { | ||
97 | MPN_COPY_DECR( wp + limb_cnt, u->d, usize ); | ||
98 | } | ||
99 | |||
100 | /* Zero all whole limbs at low end. Do it here and not before calling | ||
101 | * mpn_lshift, not to lose for U == W. */ | ||
102 | MPN_ZERO( wp, limb_cnt ); | ||
103 | |||
104 | w->nlimbs = wsize; | ||
105 | w->sign = wsign; | ||
106 | } | ||
107 | |||
108 | |||
109 | void | ||
110 | gcry_mpi_mul_2exp( gcry_mpi_t w, gcry_mpi_t u, unsigned long cnt) | ||
111 | { | ||
112 | _gcry_mpi_mul_2exp (w, u, cnt); | ||
113 | } | ||
114 | |||
115 | |||
116 | void | ||
117 | gcry_mpi_mul( gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v) | ||
118 | { | ||
119 | mpi_size_t usize, vsize, wsize; | ||
120 | mpi_ptr_t up, vp, wp; | ||
121 | mpi_limb_t cy; | ||
122 | int usign, vsign, usecure, vsecure, sign_product; | ||
123 | int assign_wp=0; | ||
124 | mpi_ptr_t tmp_limb=NULL; | ||
125 | unsigned int tmp_limb_nlimbs = 0; | ||
126 | |||
127 | if( u->nlimbs < v->nlimbs ) { /* Swap U and V. */ | ||
128 | usize = v->nlimbs; | ||
129 | usign = v->sign; | ||
130 | usecure = mpi_is_secure(v); | ||
131 | up = v->d; | ||
132 | vsize = u->nlimbs; | ||
133 | vsign = u->sign; | ||
134 | vsecure = mpi_is_secure(u); | ||
135 | vp = u->d; | ||
136 | } | ||
137 | else { | ||
138 | usize = u->nlimbs; | ||
139 | usign = u->sign; | ||
140 | usecure = mpi_is_secure(u); | ||
141 | up = u->d; | ||
142 | vsize = v->nlimbs; | ||
143 | vsign = v->sign; | ||
144 | vsecure = mpi_is_secure(v); | ||
145 | vp = v->d; | ||
146 | } | ||
147 | sign_product = usign ^ vsign; | ||
148 | wp = w->d; | ||
149 | |||
150 | /* Ensure W has space enough to store the result. */ | ||
151 | wsize = usize + vsize; | ||
152 | if ( !mpi_is_secure (w) && (mpi_is_secure (u) || mpi_is_secure (v)) ) { | ||
153 | /* w is not allocated in secure space but u or v is. To make sure | ||
154 | * that no temporray results are stored in w, we temporary use | ||
155 | * a newly allocated limb space for w */ | ||
156 | wp = mpi_alloc_limb_space( wsize, 1 ); | ||
157 | assign_wp = 2; /* mark it as 2 so that we can later copy it back to | ||
158 | * mormal memory */ | ||
159 | } | ||
160 | else if( w->alloced < wsize ) { | ||
161 | if( wp == up || wp == vp ) { | ||
162 | wp = mpi_alloc_limb_space( wsize, mpi_is_secure(w) ); | ||
163 | assign_wp = 1; | ||
164 | } | ||
165 | else { | ||
166 | mpi_resize(w, wsize ); | ||
167 | wp = w->d; | ||
168 | } | ||
169 | } | ||
170 | else { /* Make U and V not overlap with W.*/ | ||
171 | if( wp == up ) { | ||
172 | /* W and U are identical. Allocate temporary space for U.*/ | ||
173 | tmp_limb_nlimbs = usize; | ||
174 | up = tmp_limb = mpi_alloc_limb_space( usize, usecure ); | ||
175 | /* Is V identical too? Keep it identical with U. */ | ||
176 | if( wp == vp ) | ||
177 | vp = up; | ||
178 | /* Copy to the temporary space. */ | ||
179 | MPN_COPY( up, wp, usize ); | ||
180 | } | ||
181 | else if( wp == vp ) { | ||
182 | /* W and V are identical. Allocate temporary space for V.*/ | ||
183 | tmp_limb_nlimbs = vsize; | ||
184 | vp = tmp_limb = mpi_alloc_limb_space( vsize, vsecure ); | ||
185 | /* Copy to the temporary space. */ | ||
186 | MPN_COPY( vp, wp, vsize ); | ||
187 | } | ||
188 | } | ||
189 | |||
190 | if( !vsize ) | ||
191 | wsize = 0; | ||
192 | else { | ||
193 | cy = _gcry_mpih_mul( wp, up, usize, vp, vsize ); | ||
194 | wsize -= cy? 0:1; | ||
195 | } | ||
196 | |||
197 | if( assign_wp ) { | ||
198 | if (assign_wp == 2) { | ||
199 | /* copy the temp wp from secure memory back to normal memory */ | ||
200 | mpi_ptr_t tmp_wp = mpi_alloc_limb_space (wsize, 0); | ||
201 | MPN_COPY (tmp_wp, wp, wsize); | ||
202 | _gcry_mpi_free_limb_space (wp, 0); | ||
203 | wp = tmp_wp; | ||
204 | } | ||
205 | _gcry_mpi_assign_limb_space( w, wp, wsize ); | ||
206 | } | ||
207 | w->nlimbs = wsize; | ||
208 | w->sign = sign_product; | ||
209 | if( tmp_limb ) | ||
210 | _gcry_mpi_free_limb_space (tmp_limb, tmp_limb_nlimbs); | ||
211 | } | ||
212 | |||
213 | |||
214 | void | ||
215 | gcry_mpi_mulm( gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, gcry_mpi_t m) | ||
216 | { | ||
217 | gcry_mpi_mul(w, u, v); | ||
218 | _gcry_mpi_fdiv_r( w, w, m ); | ||
219 | } | ||
220 | |||