lib/mpi: Extend the MPI library
[linux-2.6-microblaze.git] / lib / mpi / mpi-mul.c
1 /* mpi-mul.c  -  MPI functions
2  * Copyright (C) 1994, 1996, 1998, 2001, 2002,
3  *               2003 Free Software Foundation, Inc.
4  *
5  * This file is part of Libgcrypt.
6  *
7  * Note: This code is heavily based on the GNU MP Library.
8  *       Actually it's the same code with only minor changes in the
9  *       way the data is stored; this is to support the abstraction
10  *       of an optional secure memory allocation which may be used
11  *       to avoid revealing of sensitive data due to paging etc.
12  */
13
14 #include "mpi-internal.h"
15
16 void mpi_mul(MPI w, MPI u, MPI v)
17 {
18         mpi_size_t usize, vsize, wsize;
19         mpi_ptr_t up, vp, wp;
20         mpi_limb_t cy;
21         int usign, vsign, sign_product;
22         int assign_wp = 0;
23         mpi_ptr_t tmp_limb = NULL;
24         unsigned int tmp_limb_nlimbs = 0;
25
26         if (u->nlimbs < v->nlimbs) {
27                 /* Swap U and V. */
28                 usize = v->nlimbs;
29                 usign = v->sign;
30                 up    = v->d;
31                 vsize = u->nlimbs;
32                 vsign = u->sign;
33                 vp    = u->d;
34         } else {
35                 usize = u->nlimbs;
36                 usign = u->sign;
37                 up    = u->d;
38                 vsize = v->nlimbs;
39                 vsign = v->sign;
40                 vp    = v->d;
41         }
42         sign_product = usign ^ vsign;
43         wp = w->d;
44
45         /* Ensure W has space enough to store the result.  */
46         wsize = usize + vsize;
47         if (w->alloced < wsize) {
48                 if (wp == up || wp == vp) {
49                         wp = mpi_alloc_limb_space(wsize);
50                         assign_wp = 1;
51                 } else {
52                         mpi_resize(w, wsize);
53                         wp = w->d;
54                 }
55         } else { /* Make U and V not overlap with W.    */
56                 if (wp == up) {
57                         /* W and U are identical.  Allocate temporary space for U. */
58                         tmp_limb_nlimbs = usize;
59                         up = tmp_limb = mpi_alloc_limb_space(usize);
60                         /* Is V identical too?  Keep it identical with U.  */
61                         if (wp == vp)
62                                 vp = up;
63                         /* Copy to the temporary space.  */
64                         MPN_COPY(up, wp, usize);
65                 } else if (wp == vp) {
66                         /* W and V are identical.  Allocate temporary space for V. */
67                         tmp_limb_nlimbs = vsize;
68                         vp = tmp_limb = mpi_alloc_limb_space(vsize);
69                         /* Copy to the temporary space.  */
70                         MPN_COPY(vp, wp, vsize);
71                 }
72         }
73
74         if (!vsize)
75                 wsize = 0;
76         else {
77                 mpihelp_mul(wp, up, usize, vp, vsize, &cy);
78                 wsize -= cy ? 0:1;
79         }
80
81         if (assign_wp)
82                 mpi_assign_limb_space(w, wp, wsize);
83         w->nlimbs = wsize;
84         w->sign = sign_product;
85         if (tmp_limb)
86                 mpi_free_limb_space(tmp_limb);
87 }
88
89 void mpi_mulm(MPI w, MPI u, MPI v, MPI m)
90 {
91         mpi_mul(w, u, v);
92         mpi_tdiv_r(w, w, m);
93 }
94 EXPORT_SYMBOL_GPL(mpi_mulm);