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