LCOV - code coverage report
Current view: top level - mpi - mpi-mul.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 93 103 90.3 %
Date: 2016-11-29 14:56:30 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /* mpi-mul.c  -  MPI functions
       2             :  * Copyright (C) 1994, 1996, 1998, 2001, 2002, 2003 Free Software Foundation, Inc.
       3             :  *
       4             :  * This file is part of Libgcrypt.
       5             :  *
       6             :  * Libgcrypt is free software; you can redistribute it and/or modify
       7             :  * it under the terms of the GNU Lesser General Public License as
       8             :  * published by the Free Software Foundation; either version 2.1 of
       9             :  * the License, or (at your option) any later version.
      10             :  *
      11             :  * Libgcrypt is distributed in the hope that it will be useful,
      12             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             :  * GNU Lesser General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU Lesser General Public
      17             :  * License along with this program; if not, write to the Free Software
      18             :  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
      19             :  *
      20             :  * Note: This code is heavily based on the GNU MP Library.
      21             :  *       Actually it's the same code with only minor changes in the
      22             :  *       way the data is stored; this is to support the abstraction
      23             :  *       of an optional secure memory allocation which may be used
      24             :  *       to avoid revealing of sensitive data due to paging etc.
      25             :  */
      26             : 
      27             : #include <config.h>
      28             : #include <stdio.h>
      29             : #include <stdlib.h>
      30             : #include "mpi-internal.h"
      31             : 
      32             : 
      33             : void
      34       22032 : _gcry_mpi_mul_ui (gcry_mpi_t prod, gcry_mpi_t mult, unsigned long small_mult)
      35             : {
      36             :     mpi_size_t size, prod_size;
      37             :     mpi_ptr_t  prod_ptr;
      38             :     mpi_limb_t cy;
      39             :     int sign;
      40             : 
      41       22032 :     size = mult->nlimbs;
      42       22032 :     sign = mult->sign;
      43             : 
      44       22032 :     if( !size || !small_mult ) {
      45           0 :         prod->nlimbs = 0;
      46           0 :         prod->sign = 0;
      47       22032 :         return;
      48             :     }
      49             : 
      50       22032 :     prod_size = size + 1;
      51       22032 :     if( prod->alloced < prod_size )
      52           0 :         mpi_resize( prod, prod_size );
      53       22032 :     prod_ptr = prod->d;
      54             : 
      55       22032 :     cy = _gcry_mpih_mul_1( prod_ptr, mult->d, size, (mpi_limb_t)small_mult );
      56       22032 :     if( cy )
      57           0 :         prod_ptr[size++] = cy;
      58       22032 :     prod->nlimbs = size;
      59       22032 :     prod->sign = sign;
      60             : }
      61             : 
      62             : 
      63             : void
      64        8603 : _gcry_mpi_mul_2exp (gcry_mpi_t w, gcry_mpi_t u, unsigned long cnt)
      65             : {
      66             :     mpi_size_t usize, wsize, limb_cnt;
      67             :     mpi_ptr_t wp;
      68             :     mpi_limb_t wlimb;
      69             :     int usign, wsign;
      70             : 
      71        8603 :     usize = u->nlimbs;
      72        8603 :     usign = u->sign;
      73             : 
      74        8603 :     if( !usize ) {
      75           0 :         w->nlimbs = 0;
      76           0 :         w->sign = 0;
      77        8603 :         return;
      78             :     }
      79             : 
      80        8603 :     limb_cnt = cnt / BITS_PER_MPI_LIMB;
      81        8603 :     wsize = usize + limb_cnt + 1;
      82        8603 :     if( w->alloced < wsize )
      83           0 :         mpi_resize(w, wsize );
      84        8603 :     wp = w->d;
      85        8603 :     wsize = usize + limb_cnt;
      86        8603 :     wsign = usign;
      87             : 
      88        8603 :     cnt %= BITS_PER_MPI_LIMB;
      89        8603 :     if( cnt ) {
      90        8603 :         wlimb = _gcry_mpih_lshift( wp + limb_cnt, u->d, usize, cnt );
      91        8603 :         if( wlimb ) {
      92           0 :             wp[wsize] = wlimb;
      93           0 :             wsize++;
      94             :         }
      95             :     }
      96             :     else {
      97           0 :         MPN_COPY_DECR( wp + limb_cnt, u->d, usize );
      98             :     }
      99             : 
     100             :     /* Zero all whole limbs at low end.  Do it here and not before calling
     101             :      * mpn_lshift, not to lose for U == W.  */
     102        8603 :     MPN_ZERO( wp, limb_cnt );
     103             : 
     104        8603 :     w->nlimbs = wsize;
     105        8603 :     w->sign = wsign;
     106             : }
     107             : 
     108             : 
     109             : void
     110    27871485 : _gcry_mpi_mul (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v)
     111             : {
     112             :     mpi_size_t usize, vsize, wsize;
     113             :     mpi_ptr_t up, vp, wp;
     114             :     mpi_limb_t cy;
     115             :     int usign, vsign, usecure, vsecure, sign_product;
     116    27871485 :     int assign_wp=0;
     117    27871485 :     mpi_ptr_t tmp_limb=NULL;
     118    27871485 :     unsigned int tmp_limb_nlimbs = 0;
     119             : 
     120    27871485 :     if( u->nlimbs < v->nlimbs ) { /* Swap U and V. */
     121       99039 :         usize = v->nlimbs;
     122       99039 :         usign = v->sign;
     123       99039 :         usecure = mpi_is_secure(v);
     124       99039 :         up    = v->d;
     125       99039 :         vsize = u->nlimbs;
     126       99039 :         vsign = u->sign;
     127       99039 :         vsecure = mpi_is_secure(u);
     128       99039 :         vp    = u->d;
     129             :     }
     130             :     else {
     131    27772446 :         usize = u->nlimbs;
     132    27772446 :         usign = u->sign;
     133    27772446 :         usecure = mpi_is_secure(u);
     134    27772446 :         up    = u->d;
     135    27772446 :         vsize = v->nlimbs;
     136    27772446 :         vsign = v->sign;
     137    27772446 :         vsecure = mpi_is_secure(v);
     138    27772446 :         vp    = v->d;
     139             :     }
     140    27871485 :     sign_product = usign ^ vsign;
     141    27871485 :     wp = w->d;
     142             : 
     143             :     /* Ensure W has space enough to store the result.  */
     144    27871485 :     wsize = usize + vsize;
     145    27871485 :     if ( !mpi_is_secure (w) && (mpi_is_secure (u) || mpi_is_secure (v)) ) {
     146             :         /* w is not allocated in secure space but u or v is.  To make sure
     147             :          * that no temporray results are stored in w, we temporary use
     148             :          * a newly allocated limb space for w */
     149        1141 :         wp = mpi_alloc_limb_space( wsize, 1 );
     150        1141 :         assign_wp = 2; /* mark it as 2 so that we can later copy it back to
     151             :                         * mormal memory */
     152             :     }
     153    27870344 :     else if( w->alloced < wsize ) {
     154       48743 :         if( wp == up || wp == vp ) {
     155       16057 :             wp = mpi_alloc_limb_space( wsize, mpi_is_secure(w) );
     156       16057 :             assign_wp = 1;
     157             :         }
     158             :         else {
     159       32686 :             mpi_resize(w, wsize );
     160       32686 :             wp = w->d;
     161             :         }
     162             :     }
     163             :     else { /* Make U and V not overlap with W.  */
     164    27821601 :         if( wp == up ) {
     165             :             /* W and U are identical.  Allocate temporary space for U.  */
     166    12881145 :             tmp_limb_nlimbs = usize;
     167    12881145 :             up = tmp_limb = mpi_alloc_limb_space( usize, usecure  );
     168             :             /* Is V identical too?  Keep it identical with U.  */
     169    12881145 :             if( wp == vp )
     170     2280449 :                 vp = up;
     171             :             /* Copy to the temporary space.  */
     172    12881145 :             MPN_COPY( up, wp, usize );
     173             :         }
     174    14940456 :         else if( wp == vp ) {
     175             :             /* W and V are identical.  Allocate temporary space for V.  */
     176      512170 :             tmp_limb_nlimbs = vsize;
     177      512170 :             vp = tmp_limb = mpi_alloc_limb_space( vsize, vsecure );
     178             :             /* Copy to the temporary space.  */
     179      512170 :             MPN_COPY( vp, wp, vsize );
     180             :         }
     181             :     }
     182             : 
     183    27871485 :     if( !vsize )
     184       31274 :         wsize = 0;
     185             :     else {
     186    27840211 :         cy = _gcry_mpih_mul( wp, up, usize, vp, vsize );
     187    27840211 :         wsize -= cy? 0:1;
     188             :     }
     189             : 
     190    27871485 :     if( assign_wp ) {
     191       17198 :         if (assign_wp == 2) {
     192             :             /* copy the temp wp from secure memory back to normal memory */
     193        1141 :             mpi_ptr_t tmp_wp = mpi_alloc_limb_space (wsize, 0);
     194        1141 :             MPN_COPY (tmp_wp, wp, wsize);
     195        1141 :             _gcry_mpi_free_limb_space (wp, 0);
     196        1141 :             wp = tmp_wp;
     197             :         }
     198       17198 :         _gcry_mpi_assign_limb_space( w, wp, wsize );
     199             :     }
     200    27871485 :     w->nlimbs = wsize;
     201    27871485 :     w->sign = sign_product;
     202    27871485 :     if( tmp_limb )
     203    13393315 :         _gcry_mpi_free_limb_space (tmp_limb, tmp_limb_nlimbs);
     204    27871485 : }
     205             : 
     206             : 
     207             : void
     208       80073 : _gcry_mpi_mulm (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, gcry_mpi_t m)
     209             : {
     210       80073 :   mpi_mul (w, u, v);
     211       80073 :   _gcry_mpi_tdiv_r (w, w, m);
     212       80073 : }

Generated by: LCOV version 1.11