-
+ 8A56ADB14BCAC8D72E4454562A0455C241756EDE576D2CD4E8A42CFDFF5ABE7C803A5F1523945EE81D5A149B88D969739443D00165FDAAB7041BAC9BE10DC8CFmpi/mpi-mul.c(0 . 0)(1 . 214)
9456 /* mpi-mul.c - MPI functions
9457 * Copyright (C) 1994, 1996 Free Software Foundation, Inc.
9458 * Copyright (C) 1998, 2001 Free Software Foundation, Inc.
9459 *
9460 * This file is part of GnuPG.
9461 *
9462 * GnuPG is free software; you can redistribute it and/or modify
9463 * it under the terms of the GNU General Public License as published by
9464 * the Free Software Foundation; either version 3 of the License, or
9465 * (at your option) any later version.
9466 *
9467 * GnuPG is distributed in the hope that it will be useful,
9468 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9469 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9470 * GNU General Public License for more details.
9471 *
9472 * You should have received a copy of the GNU General Public License
9473 * along with this program; if not, see <http://www.gnu.org/licenses/>.
9474 *
9475 * Note: This code is heavily based on the GNU MP Library.
9476 * Actually it's the same code with only minor changes in the
9477 * way the data is stored; this is to support the abstraction
9478 * of an optional secure memory allocation which may be used
9479 * to avoid revealing of sensitive data due to paging etc.
9480 * The GNU MP Library itself is published under the LGPL;
9481 * however I decided to publish this code under the plain GPL.
9482 */
9483
9484 #include <config.h>
9485 #include <stdio.h>
9486 #include <stdlib.h>
9487 #include "mpi-internal.h"
9488
9489
9490 void
9491 mpi_mul_ui( MPI prod, MPI mult, unsigned long small_mult )
9492 {
9493 mpi_size_t size, prod_size;
9494 mpi_ptr_t prod_ptr;
9495 mpi_limb_t cy;
9496 int sign;
9497
9498 size = mult->nlimbs;
9499 sign = mult->sign;
9500
9501 if( !size || !small_mult ) {
9502 prod->nlimbs = 0;
9503 prod->sign = 0;
9504 return;
9505 }
9506
9507 prod_size = size + 1;
9508 if( prod->alloced < prod_size )
9509 mpi_resize( prod, prod_size );
9510 prod_ptr = prod->d;
9511
9512 cy = mpihelp_mul_1( prod_ptr, mult->d, size, (mpi_limb_t)small_mult );
9513 if( cy )
9514 prod_ptr[size++] = cy;
9515 prod->nlimbs = size;
9516 prod->sign = sign;
9517 }
9518
9519
9520 void
9521 mpi_mul_2exp( MPI w, MPI u, unsigned long cnt)
9522 {
9523 mpi_size_t usize, wsize, limb_cnt;
9524 mpi_ptr_t wp;
9525 mpi_limb_t wlimb;
9526 int usign, wsign;
9527
9528 usize = u->nlimbs;
9529 usign = u->sign;
9530
9531 if( !usize ) {
9532 w->nlimbs = 0;
9533 w->sign = 0;
9534 return;
9535 }
9536
9537 limb_cnt = cnt / BITS_PER_MPI_LIMB;
9538 wsize = usize + limb_cnt + 1;
9539 if( w->alloced < wsize )
9540 mpi_resize(w, wsize );
9541 wp = w->d;
9542 wsize = usize + limb_cnt;
9543 wsign = usign;
9544
9545 cnt %= BITS_PER_MPI_LIMB;
9546 if( cnt ) {
9547 wlimb = mpihelp_lshift( wp + limb_cnt, u->d, usize, cnt );
9548 if( wlimb ) {
9549 wp[wsize] = wlimb;
9550 wsize++;
9551 }
9552 }
9553 else {
9554 MPN_COPY_DECR( wp + limb_cnt, u->d, usize );
9555 }
9556
9557 /* Zero all whole limbs at low end. Do it here and not before calling
9558 * mpn_lshift, not to lose for U == W. */
9559 MPN_ZERO( wp, limb_cnt );
9560
9561 w->nlimbs = wsize;
9562 w->sign = wsign;
9563 }
9564
9565
9566
9567 void
9568 mpi_mul( MPI w, MPI u, MPI v)
9569 {
9570 mpi_size_t usize, vsize, wsize;
9571 mpi_ptr_t up, vp, wp;
9572 mpi_limb_t cy;
9573 int usign, vsign, usecure, vsecure, sign_product;
9574 int assign_wp=0;
9575 mpi_ptr_t tmp_limb=NULL;
9576
9577
9578 if( u->nlimbs < v->nlimbs ) { /* Swap U and V. */
9579 usize = v->nlimbs;
9580 usign = v->sign;
9581 usecure = mpi_is_secure(v);
9582 up = v->d;
9583 vsize = u->nlimbs;
9584 vsign = u->sign;
9585 vsecure = mpi_is_secure(u);
9586 vp = u->d;
9587 }
9588 else {
9589 usize = u->nlimbs;
9590 usign = u->sign;
9591 usecure = mpi_is_secure(u);
9592 up = u->d;
9593 vsize = v->nlimbs;
9594 vsign = v->sign;
9595 vsecure = mpi_is_secure(v);
9596 vp = v->d;
9597 }
9598 sign_product = usign ^ vsign;
9599 wp = w->d;
9600
9601 /* Ensure W has space enough to store the result. */
9602 wsize = usize + vsize;
9603 if ( !mpi_is_secure (w) && (mpi_is_secure (u) || mpi_is_secure (v)) ) {
9604 /* w is not allocated in secure space but u or v is. To make sure
9605 * that no temporray results are stored in w, we temporary use
9606 * a newly allocated limb space for w */
9607 wp = mpi_alloc_limb_space( wsize, 1 );
9608 assign_wp = 2; /* mark it as 2 so that we can later copy it back to
9609 * mormal memory */
9610 }
9611 else if( w->alloced < wsize ) {
9612 if( wp == up || wp == vp ) {
9613 wp = mpi_alloc_limb_space( wsize, mpi_is_secure(w) );
9614 assign_wp = 1;
9615 }
9616 else {
9617 mpi_resize(w, wsize );
9618 wp = w->d;
9619 }
9620 }
9621 else { /* Make U and V not overlap with W. */
9622 if( wp == up ) {
9623 /* W and U are identical. Allocate temporary space for U. */
9624 up = tmp_limb = mpi_alloc_limb_space( usize, usecure );
9625 /* Is V identical too? Keep it identical with U. */
9626 if( wp == vp )
9627 vp = up;
9628 /* Copy to the temporary space. */
9629 MPN_COPY( up, wp, usize );
9630 }
9631 else if( wp == vp ) {
9632 /* W and V are identical. Allocate temporary space for V. */
9633 vp = tmp_limb = mpi_alloc_limb_space( vsize, vsecure );
9634 /* Copy to the temporary space. */
9635 MPN_COPY( vp, wp, vsize );
9636 }
9637 }
9638
9639 if( !vsize )
9640 wsize = 0;
9641 else {
9642 cy = mpihelp_mul( wp, up, usize, vp, vsize );
9643 wsize -= cy? 0:1;
9644 }
9645
9646 if( assign_wp ) {
9647 if (assign_wp == 2) {
9648 /* copy the temp wp from secure memory back to normal memory */
9649 mpi_ptr_t tmp_wp = mpi_alloc_limb_space (wsize, 0);
9650 MPN_COPY (tmp_wp, wp, wsize);
9651 mpi_free_limb_space (wp);
9652 wp = tmp_wp;
9653 }
9654 mpi_assign_limb_space( w, wp, wsize );
9655 }
9656 w->nlimbs = wsize;
9657 w->sign = sign_product;
9658 if( tmp_limb )
9659 mpi_free_limb_space( tmp_limb );
9660 }
9661
9662
9663 void
9664 mpi_mulm( MPI w, MPI u, MPI v, MPI m)
9665 {
9666 mpi_mul(w, u, v);
9667 mpi_fdiv_r( w, w, m );
9668 }
9669