1 /* mpi-mul.c - MPI functions
2 * Copyright (C) 1994, 1996, 1998, 2001, 2002,
3 * 2003 Free Software Foundation, Inc.
5 * This file is part of Libgcrypt.
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.
14 #include "mpi-internal.h"
16 void mpi_mul(MPI w, MPI u, MPI v)
18 mpi_size_t usize, vsize, wsize;
21 int usign, vsign, sign_product;
23 mpi_ptr_t tmp_limb = NULL;
25 if (u->nlimbs < v->nlimbs) {
41 sign_product = usign ^ vsign;
44 /* Ensure W has space enough to store the result. */
45 wsize = usize + vsize;
46 if (w->alloced < wsize) {
47 if (wp == up || wp == vp) {
48 wp = mpi_alloc_limb_space(wsize);
54 } else { /* Make U and V not overlap with W. */
56 /* W and U are identical. Allocate temporary space for U. */
57 up = tmp_limb = mpi_alloc_limb_space(usize);
58 /* Is V identical too? Keep it identical with U. */
61 /* Copy to the temporary space. */
62 MPN_COPY(up, wp, usize);
63 } else if (wp == vp) {
64 /* W and V are identical. Allocate temporary space for V. */
65 vp = tmp_limb = mpi_alloc_limb_space(vsize);
66 /* Copy to the temporary space. */
67 MPN_COPY(vp, wp, vsize);
74 mpihelp_mul(wp, up, usize, vp, vsize, &cy);
79 mpi_assign_limb_space(w, wp, wsize);
81 w->sign = sign_product;
83 mpi_free_limb_space(tmp_limb);
86 void mpi_mulm(MPI w, MPI u, MPI v, MPI m)
91 EXPORT_SYMBOL_GPL(mpi_mulm);