This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / crypto / mpi / mpi-mul.c
1 /* mpi-mul.c  -  MPI functions
2  *      Copyright (C) 1994, 1996 Free Software Foundation, Inc.
3  *      Copyright (C) 1998, 2001 Free Software Foundation, Inc.
4  *
5  * This file is part of GnuPG.
6  *
7  * GnuPG is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * GnuPG is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20  *
21  * Note: This code is heavily based on the GNU MP Library.
22  *       Actually it's the same code with only minor changes in the
23  *       way the data is stored; this is to support the abstraction
24  *       of an optional secure memory allocation which may be used
25  *       to avoid revealing of sensitive data due to paging etc.
26  *       The GNU MP Library itself is published under the LGPL;
27  *       however I decided to publish this code under the plain GPL.
28  */
29
30 #include "mpi-internal.h"
31
32
33 int
34 mpi_mul_ui( MPI prod, MPI 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 0;
48     }
49
50     prod_size = size + 1;
51     if( prod->alloced < prod_size )
52             if (mpi_resize( prod, prod_size ) < 0)
53                     return -ENOMEM;
54     prod_ptr = prod->d;
55
56     cy = mpihelp_mul_1( prod_ptr, mult->d, size, (mpi_limb_t)small_mult );
57     if( cy )
58         prod_ptr[size++] = cy;
59     prod->nlimbs = size;
60     prod->sign = sign;
61     return 0;
62 }
63
64
65 int
66 mpi_mul_2exp( MPI w, MPI u, unsigned long cnt)
67 {
68     mpi_size_t usize, wsize, limb_cnt;
69     mpi_ptr_t wp;
70     mpi_limb_t wlimb;
71     int usign, wsign;
72
73     usize = u->nlimbs;
74     usign = u->sign;
75
76     if( !usize ) {
77         w->nlimbs = 0;
78         w->sign = 0;
79         return 0;
80     }
81
82     limb_cnt = cnt / BITS_PER_MPI_LIMB;
83     wsize = usize + limb_cnt + 1;
84     if( w->alloced < wsize )
85             if (mpi_resize(w, wsize ) < 0)
86                     return -ENOMEM;
87     wp = w->d;
88     wsize = usize + limb_cnt;
89     wsign = usign;
90
91     cnt %= BITS_PER_MPI_LIMB;
92     if( cnt ) {
93         wlimb = mpihelp_lshift( wp + limb_cnt, u->d, usize, cnt );
94         if( wlimb ) {
95             wp[wsize] = wlimb;
96             wsize++;
97         }
98     }
99     else {
100         MPN_COPY_DECR( wp + limb_cnt, u->d, usize );
101     }
102
103     /* Zero all whole limbs at low end.  Do it here and not before calling
104      * mpn_lshift, not to lose for U == W.  */
105     MPN_ZERO( wp, limb_cnt );
106
107     w->nlimbs = wsize;
108     w->sign = wsign;
109     return 0;
110 }
111
112 int
113 mpi_mul( MPI w, MPI u, MPI v)
114 {
115         int rc = -ENOMEM;
116         mpi_size_t usize, vsize, wsize;
117         mpi_ptr_t up, vp, wp;
118         mpi_limb_t cy;
119         int usign, vsign, sign_product;
120         int assign_wp=0;
121         mpi_ptr_t tmp_limb=NULL;
122
123
124         if( u->nlimbs < v->nlimbs ) { /* Swap U and V. */
125                 usize = v->nlimbs;
126                 usign = v->sign;
127                 up    = v->d;
128                 vsize = u->nlimbs;
129                 vsign = u->sign;
130                 vp    = u->d;
131         }
132         else {
133                 usize = u->nlimbs;
134                 usign = u->sign;
135                 up    = u->d;
136                 vsize = v->nlimbs;
137                 vsign = v->sign;
138                 vp    = v->d;
139         }
140         sign_product = usign ^ vsign;
141         wp = w->d;
142
143         /* Ensure W has space enough to store the result.  */
144         wsize = usize + vsize;
145         if( w->alloced < (size_t)wsize ) {
146                 if( wp == up || wp == vp ) {
147                         wp = mpi_alloc_limb_space(wsize);
148                         if (!wp) goto nomem;
149                         assign_wp = 1;
150                 }
151                 else {
152                         if (mpi_resize(w, wsize ) < 0) goto nomem;
153                         wp = w->d;
154                 }
155         }
156         else { /* Make U and V not overlap with W.      */
157                 if( wp == up ) {
158                         /* W and U are identical.  Allocate temporary space for U.      */
159                         up = tmp_limb = mpi_alloc_limb_space( usize);
160                         if (!up) goto nomem;
161                         /* Is V identical too?  Keep it identical with U.  */
162                         if( wp == vp )
163                                 vp = up;
164                         /* Copy to the temporary space.  */
165                         MPN_COPY( up, wp, usize );
166                 }
167                 else if( wp == vp ) {
168                         /* W and V are identical.  Allocate temporary space for V.      */
169                         vp = tmp_limb = mpi_alloc_limb_space( vsize);
170                         if (!vp) goto nomem;
171                         /* Copy to the temporary space.  */
172                         MPN_COPY( vp, wp, vsize );
173                 }
174         }
175
176         if( !vsize )
177                 wsize = 0;
178         else {
179                 if (mpihelp_mul( wp, up, usize, vp, vsize, &cy) < 0)
180                         goto nomem;
181                 wsize -= cy? 0:1;
182         }
183
184         if( assign_wp )
185                 mpi_assign_limb_space( w, wp, wsize );
186
187         w->nlimbs = wsize;
188         w->sign = sign_product;
189         rc = 0;
190  nomem:
191         if( tmp_limb )
192                 mpi_free_limb_space( tmp_limb );
193         return rc;
194 }
195
196 int
197 mpi_mulm( MPI w, MPI u, MPI v, MPI m)
198 {
199         if (mpi_mul(w, u, v) < 0)
200                 return -ENOMEM;
201         return mpi_fdiv_r( w, w, m );
202 }