raw
mpi-genesis             1 /* mpi-mul.c  -  MPI functions
mpi_second_cut 2 * Modified by No Such Labs. (C) 2015. See README.
mpi-genesis 3 *
mpi_second_cut 4 * This file was originally part of Gnu Privacy Guard (GPG), ver. 1.4.10,
mpi_second_cut 5 * SHA256(gnupg-1.4.10.tar.gz):
mpi_second_cut 6 * 0bfd74660a2f6cedcf7d8256db4a63c996ffebbcdc2cf54397bfb72878c5a85a
mpi_second_cut 7 * (C) 1994-2005 Free Software Foundation, Inc.
mpi-genesis 8 *
mpi_second_cut 9 * This program is free software: you can redistribute it and/or modify
mpi-genesis 10 * it under the terms of the GNU General Public License as published by
mpi_second_cut 11 * the Free Software Foundation, either version 3 of the License, or
mpi-genesis 12 * (at your option) any later version.
mpi-genesis 13 *
mpi_second_cut 14 * This program is distributed in the hope that it will be useful,
mpi-genesis 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
mpi-genesis 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
mpi-genesis 17 * GNU General Public License for more details.
mpi-genesis 18 *
mpi-genesis 19 * You should have received a copy of the GNU General Public License
mpi_second_cut 20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
mpi-genesis 21 */
mpi-genesis 22
mpi-genesis 23 #include <stdio.h>
mpi-genesis 24 #include <stdlib.h>
mpi_second_cut 25
mpi_second_cut 26 #include "knobs.h"
mpi-genesis 27 #include "mpi-internal.h"
mpi-genesis 28
mpi-genesis 29
mpi-genesis 30 void
mpi-genesis 31 mpi_mul_ui( MPI prod, MPI mult, unsigned long small_mult )
mpi-genesis 32 {
mpi-genesis 33 mpi_size_t size, prod_size;
mpi-genesis 34 mpi_ptr_t prod_ptr;
mpi-genesis 35 mpi_limb_t cy;
mpi-genesis 36 int sign;
mpi-genesis 37
mpi-genesis 38 size = mult->nlimbs;
mpi-genesis 39 sign = mult->sign;
mpi-genesis 40
mpi-genesis 41 if( !size || !small_mult ) {
mpi-genesis 42 prod->nlimbs = 0;
mpi-genesis 43 prod->sign = 0;
mpi-genesis 44 return;
mpi-genesis 45 }
mpi-genesis 46
mpi-genesis 47 prod_size = size + 1;
mpi-genesis 48 if( prod->alloced < prod_size )
mpi-genesis 49 mpi_resize( prod, prod_size );
mpi-genesis 50 prod_ptr = prod->d;
mpi-genesis 51
mpi-genesis 52 cy = mpihelp_mul_1( prod_ptr, mult->d, size, (mpi_limb_t)small_mult );
mpi-genesis 53 if( cy )
mpi-genesis 54 prod_ptr[size++] = cy;
mpi-genesis 55 prod->nlimbs = size;
mpi-genesis 56 prod->sign = sign;
mpi-genesis 57 }
mpi-genesis 58
mpi-genesis 59
mpi-genesis 60 void
mpi-genesis 61 mpi_mul_2exp( MPI w, MPI u, unsigned long cnt)
mpi-genesis 62 {
mpi-genesis 63 mpi_size_t usize, wsize, limb_cnt;
mpi-genesis 64 mpi_ptr_t wp;
mpi-genesis 65 mpi_limb_t wlimb;
mpi-genesis 66 int usign, wsign;
mpi-genesis 67
mpi-genesis 68 usize = u->nlimbs;
mpi-genesis 69 usign = u->sign;
mpi-genesis 70
mpi-genesis 71 if( !usize ) {
mpi-genesis 72 w->nlimbs = 0;
mpi-genesis 73 w->sign = 0;
mpi-genesis 74 return;
mpi-genesis 75 }
mpi-genesis 76
mpi-genesis 77 limb_cnt = cnt / BITS_PER_MPI_LIMB;
mpi-genesis 78 wsize = usize + limb_cnt + 1;
mpi-genesis 79 if( w->alloced < wsize )
mpi-genesis 80 mpi_resize(w, wsize );
mpi-genesis 81 wp = w->d;
mpi-genesis 82 wsize = usize + limb_cnt;
mpi-genesis 83 wsign = usign;
mpi-genesis 84
mpi-genesis 85 cnt %= BITS_PER_MPI_LIMB;
mpi-genesis 86 if( cnt ) {
mpi-genesis 87 wlimb = mpihelp_lshift( wp + limb_cnt, u->d, usize, cnt );
mpi-genesis 88 if( wlimb ) {
mpi-genesis 89 wp[wsize] = wlimb;
mpi-genesis 90 wsize++;
mpi-genesis 91 }
mpi-genesis 92 }
mpi-genesis 93 else {
mpi-genesis 94 MPN_COPY_DECR( wp + limb_cnt, u->d, usize );
mpi-genesis 95 }
mpi-genesis 96
mpi-genesis 97 /* Zero all whole limbs at low end. Do it here and not before calling
mpi-genesis 98 * mpn_lshift, not to lose for U == W. */
mpi-genesis 99 MPN_ZERO( wp, limb_cnt );
mpi-genesis 100
mpi-genesis 101 w->nlimbs = wsize;
mpi-genesis 102 w->sign = wsign;
mpi-genesis 103 }
mpi-genesis 104
mpi-genesis 105
mpi-genesis 106
mpi-genesis 107 void
mpi-genesis 108 mpi_mul( MPI w, MPI u, MPI v)
mpi-genesis 109 {
mpi-genesis 110 mpi_size_t usize, vsize, wsize;
mpi-genesis 111 mpi_ptr_t up, vp, wp;
mpi-genesis 112 mpi_limb_t cy;
mpi-genesis 113 int usign, vsign, usecure, vsecure, sign_product;
mpi-genesis 114 int assign_wp=0;
mpi-genesis 115 mpi_ptr_t tmp_limb=NULL;
mpi-genesis 116
mpi-genesis 117
mpi-genesis 118 if( u->nlimbs < v->nlimbs ) { /* Swap U and V. */
mpi-genesis 119 usize = v->nlimbs;
mpi-genesis 120 usign = v->sign;
mpi-genesis 121 usecure = mpi_is_secure(v);
mpi-genesis 122 up = v->d;
mpi-genesis 123 vsize = u->nlimbs;
mpi-genesis 124 vsign = u->sign;
mpi-genesis 125 vsecure = mpi_is_secure(u);
mpi-genesis 126 vp = u->d;
mpi-genesis 127 }
mpi-genesis 128 else {
mpi-genesis 129 usize = u->nlimbs;
mpi-genesis 130 usign = u->sign;
mpi-genesis 131 usecure = mpi_is_secure(u);
mpi-genesis 132 up = u->d;
mpi-genesis 133 vsize = v->nlimbs;
mpi-genesis 134 vsign = v->sign;
mpi-genesis 135 vsecure = mpi_is_secure(v);
mpi-genesis 136 vp = v->d;
mpi-genesis 137 }
mpi-genesis 138 sign_product = usign ^ vsign;
mpi-genesis 139 wp = w->d;
mpi-genesis 140
mpi-genesis 141 /* Ensure W has space enough to store the result. */
mpi-genesis 142 wsize = usize + vsize;
mpi-genesis 143 if ( !mpi_is_secure (w) && (mpi_is_secure (u) || mpi_is_secure (v)) ) {
mpi-genesis 144 /* w is not allocated in secure space but u or v is. To make sure
mpi-genesis 145 * that no temporray results are stored in w, we temporary use
mpi-genesis 146 * a newly allocated limb space for w */
mpi-genesis 147 wp = mpi_alloc_limb_space( wsize, 1 );
mpi-genesis 148 assign_wp = 2; /* mark it as 2 so that we can later copy it back to
mpi-genesis 149 * mormal memory */
mpi-genesis 150 }
mpi-genesis 151 else if( w->alloced < wsize ) {
mpi-genesis 152 if( wp == up || wp == vp ) {
mpi-genesis 153 wp = mpi_alloc_limb_space( wsize, mpi_is_secure(w) );
mpi-genesis 154 assign_wp = 1;
mpi-genesis 155 }
mpi-genesis 156 else {
mpi-genesis 157 mpi_resize(w, wsize );
mpi-genesis 158 wp = w->d;
mpi-genesis 159 }
mpi-genesis 160 }
mpi-genesis 161 else { /* Make U and V not overlap with W. */
mpi-genesis 162 if( wp == up ) {
mpi-genesis 163 /* W and U are identical. Allocate temporary space for U. */
mpi-genesis 164 up = tmp_limb = mpi_alloc_limb_space( usize, usecure );
mpi-genesis 165 /* Is V identical too? Keep it identical with U. */
mpi-genesis 166 if( wp == vp )
mpi-genesis 167 vp = up;
mpi-genesis 168 /* Copy to the temporary space. */
mpi-genesis 169 MPN_COPY( up, wp, usize );
mpi-genesis 170 }
mpi-genesis 171 else if( wp == vp ) {
mpi-genesis 172 /* W and V are identical. Allocate temporary space for V. */
mpi-genesis 173 vp = tmp_limb = mpi_alloc_limb_space( vsize, vsecure );
mpi-genesis 174 /* Copy to the temporary space. */
mpi-genesis 175 MPN_COPY( vp, wp, vsize );
mpi-genesis 176 }
mpi-genesis 177 }
mpi-genesis 178
mpi-genesis 179 if( !vsize )
mpi-genesis 180 wsize = 0;
mpi-genesis 181 else {
mpi-genesis 182 cy = mpihelp_mul( wp, up, usize, vp, vsize );
mpi-genesis 183 wsize -= cy? 0:1;
mpi-genesis 184 }
mpi-genesis 185
mpi-genesis 186 if( assign_wp ) {
mpi-genesis 187 if (assign_wp == 2) {
mpi-genesis 188 /* copy the temp wp from secure memory back to normal memory */
mpi-genesis 189 mpi_ptr_t tmp_wp = mpi_alloc_limb_space (wsize, 0);
mpi-genesis 190 MPN_COPY (tmp_wp, wp, wsize);
mpi-genesis 191 mpi_free_limb_space (wp);
mpi-genesis 192 wp = tmp_wp;
mpi-genesis 193 }
mpi-genesis 194 mpi_assign_limb_space( w, wp, wsize );
mpi-genesis 195 }
mpi-genesis 196 w->nlimbs = wsize;
mpi-genesis 197 w->sign = sign_product;
mpi-genesis 198 if( tmp_limb )
mpi-genesis 199 mpi_free_limb_space( tmp_limb );
mpi-genesis 200 }
mpi-genesis 201
mpi-genesis 202
mpi-genesis 203 void
mpi-genesis 204 mpi_mulm( MPI w, MPI u, MPI v, MPI m)
mpi-genesis 205 {
mpi-genesis 206 mpi_mul(w, u, v);
mpi-genesis 207 mpi_fdiv_r( w, w, m );
mpi-genesis 208 }
mpi-genesis 209