1
/* mpi-mul.c - MPI functions
2
* Copyright (C) 1994, 1996 Free Software Foundation, Inc.
3
* Copyright (C) 1998, 2001 Free Software Foundation, Inc.
5
* This file is part of GnuPG.
7
* GnuPG is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2 of the License, or
10
* (at your option) any later version.
12
* GnuPG is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
21
* Note: This code is heavily based on the GNU MP Library.
22
* Actually it's the same code with only minor changes in the
23
* way the data is stored; this is to support the abstraction
24
* of an optional secure memory allocation which may be used
25
* to avoid revealing of sensitive data due to paging etc.
26
* The GNU MP Library itself is published under the LGPL;
27
* however I decided to publish this code under the plain GPL.
33
#include "mpi-internal.h"
37
mpi_mul_ui( MPI prod, MPI mult, unsigned long small_mult )
39
mpi_size_t size, prod_size;
47
if( !size || !small_mult ) {
54
if( prod->alloced < prod_size )
55
mpi_resize( prod, prod_size );
58
cy = mpihelp_mul_1( prod_ptr, mult->d, size, (mpi_limb_t)small_mult );
60
prod_ptr[size++] = cy;
67
mpi_mul_2exp( MPI w, MPI u, unsigned long cnt)
69
mpi_size_t usize, wsize, limb_cnt;
83
limb_cnt = cnt / BITS_PER_MPI_LIMB;
84
wsize = usize + limb_cnt + 1;
85
if( w->alloced < wsize )
86
mpi_resize(w, wsize );
88
wsize = usize + limb_cnt;
91
cnt %= BITS_PER_MPI_LIMB;
93
wlimb = mpihelp_lshift( wp + limb_cnt, u->d, usize, cnt );
100
MPN_COPY_DECR( wp + limb_cnt, u->d, usize );
103
/* Zero all whole limbs at low end. Do it here and not before calling
104
* mpn_lshift, not to lose for U == W. */
105
MPN_ZERO( wp, limb_cnt );
114
mpi_mul( MPI w, MPI u, MPI v)
116
mpi_size_t usize, vsize, wsize;
117
mpi_ptr_t up, vp, wp;
119
int usign, vsign, usecure, vsecure, sign_product;
121
mpi_ptr_t tmp_limb=NULL;
124
if( u->nlimbs < v->nlimbs ) { /* Swap U and V. */
127
usecure = mpi_is_secure(v);
131
vsecure = mpi_is_secure(u);
137
usecure = mpi_is_secure(u);
141
vsecure = mpi_is_secure(v);
144
sign_product = usign ^ vsign;
147
/* Ensure W has space enough to store the result. */
148
wsize = usize + vsize;
149
if ( !mpi_is_secure (w) && (mpi_is_secure (u) || mpi_is_secure (v)) ) {
150
/* w is not allocated in secure space but u or v is. To make sure
151
* that no temporray results are stored in w, we temporary use
152
* a newly allocated limb space for w */
153
wp = mpi_alloc_limb_space( wsize, 1 );
154
assign_wp = 2; /* mark it as 2 so that we can later copy it back to
157
else if( w->alloced < wsize ) {
158
if( wp == up || wp == vp ) {
159
wp = mpi_alloc_limb_space( wsize, mpi_is_secure(w) );
163
mpi_resize(w, wsize );
167
else { /* Make U and V not overlap with W. */
169
/* W and U are identical. Allocate temporary space for U. */
170
up = tmp_limb = mpi_alloc_limb_space( usize, usecure );
171
/* Is V identical too? Keep it identical with U. */
174
/* Copy to the temporary space. */
175
MPN_COPY( up, wp, usize );
177
else if( wp == vp ) {
178
/* W and V are identical. Allocate temporary space for V. */
179
vp = tmp_limb = mpi_alloc_limb_space( vsize, vsecure );
180
/* Copy to the temporary space. */
181
MPN_COPY( vp, wp, vsize );
188
cy = mpihelp_mul( wp, up, usize, vp, vsize );
193
if (assign_wp == 2) {
194
/* copy the temp wp from secure memory back to normal memory */
195
mpi_ptr_t tmp_wp = mpi_alloc_limb_space (wsize, 0);
196
MPN_COPY (tmp_wp, wp, wsize);
197
mpi_free_limb_space (wp);
200
mpi_assign_limb_space( w, wp, wsize );
203
w->sign = sign_product;
205
mpi_free_limb_space( tmp_limb );
210
mpi_mulm( MPI w, MPI u, MPI v, MPI m)
213
mpi_fdiv_r( w, w, m );