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;
24 unsigned int tmp_limb_nlimbs = 0;
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);
55 } else { /* Make U and V not overlap with W. */
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. */
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);
77 mpihelp_mul(wp, up, usize, vp, vsize, &cy);
82 mpi_assign_limb_space(w, wp, wsize);
84 w->sign = sign_product;
86 mpi_free_limb_space(tmp_limb);
89 void mpi_mulm(MPI w, MPI u, MPI v, MPI m)
94 EXPORT_SYMBOL_GPL(mpi_mulm);