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 int 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;
26 if (u->nlimbs < v->nlimbs) {
42 sign_product = usign ^ vsign;
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);
54 err = mpi_resize(w, wsize);
59 } else { /* Make U and V not overlap with W. */
61 /* W and U are identical. Allocate temporary space for U. */
62 up = tmp_limb = mpi_alloc_limb_space(usize);
65 /* Is V identical too? Keep it identical with U. */
68 /* Copy to the temporary space. */
69 MPN_COPY(up, wp, usize);
70 } else if (wp == vp) {
71 /* W and V are identical. Allocate temporary space for V. */
72 vp = tmp_limb = mpi_alloc_limb_space(vsize);
75 /* Copy to the temporary space. */
76 MPN_COPY(vp, wp, vsize);
83 err = mpihelp_mul(wp, up, usize, vp, vsize, &cy);
86 mpi_free_limb_space(wp);
93 mpi_assign_limb_space(w, wp, wsize);
95 w->sign = sign_product;
99 mpi_free_limb_space(tmp_limb);
102 EXPORT_SYMBOL_GPL(mpi_mul);
104 int mpi_mulm(MPI w, MPI u, MPI v, MPI m)
106 return mpi_mul(w, u, v) ?:
109 EXPORT_SYMBOL_GPL(mpi_mulm);