-
+ 8A56ADB14BCAC8D72E4454562A0455C241756EDE576D2CD4E8A42CFDFF5ABE7C803A5F1523945EE81D5A149B88D969739443D00165FDAAB7041BAC9BE10DC8CFmpi/mpi-mul.c(0 . 0)(1 . 214)
7439 /* mpi-mul.c - MPI functions
7440 * Copyright (C) 1994, 1996 Free Software Foundation, Inc.
7441 * Copyright (C) 1998, 2001 Free Software Foundation, Inc.
7442 *
7443 * This file is part of GnuPG.
7444 *
7445 * GnuPG is free software; you can redistribute it and/or modify
7446 * it under the terms of the GNU General Public License as published by
7447 * the Free Software Foundation; either version 3 of the License, or
7448 * (at your option) any later version.
7449 *
7450 * GnuPG is distributed in the hope that it will be useful,
7451 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7452 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7453 * GNU General Public License for more details.
7454 *
7455 * You should have received a copy of the GNU General Public License
7456 * along with this program; if not, see <http://www.gnu.org/licenses/>.
7457 *
7458 * Note: This code is heavily based on the GNU MP Library.
7459 * Actually it's the same code with only minor changes in the
7460 * way the data is stored; this is to support the abstraction
7461 * of an optional secure memory allocation which may be used
7462 * to avoid revealing of sensitive data due to paging etc.
7463 * The GNU MP Library itself is published under the LGPL;
7464 * however I decided to publish this code under the plain GPL.
7465 */
7466
7467 #include <config.h>
7468 #include <stdio.h>
7469 #include <stdlib.h>
7470 #include "mpi-internal.h"
7471
7472
7473 void
7474 mpi_mul_ui( MPI prod, MPI mult, unsigned long small_mult )
7475 {
7476 mpi_size_t size, prod_size;
7477 mpi_ptr_t prod_ptr;
7478 mpi_limb_t cy;
7479 int sign;
7480
7481 size = mult->nlimbs;
7482 sign = mult->sign;
7483
7484 if( !size || !small_mult ) {
7485 prod->nlimbs = 0;
7486 prod->sign = 0;
7487 return;
7488 }
7489
7490 prod_size = size + 1;
7491 if( prod->alloced < prod_size )
7492 mpi_resize( prod, prod_size );
7493 prod_ptr = prod->d;
7494
7495 cy = mpihelp_mul_1( prod_ptr, mult->d, size, (mpi_limb_t)small_mult );
7496 if( cy )
7497 prod_ptr[size++] = cy;
7498 prod->nlimbs = size;
7499 prod->sign = sign;
7500 }
7501
7502
7503 void
7504 mpi_mul_2exp( MPI w, MPI u, unsigned long cnt)
7505 {
7506 mpi_size_t usize, wsize, limb_cnt;
7507 mpi_ptr_t wp;
7508 mpi_limb_t wlimb;
7509 int usign, wsign;
7510
7511 usize = u->nlimbs;
7512 usign = u->sign;
7513
7514 if( !usize ) {
7515 w->nlimbs = 0;
7516 w->sign = 0;
7517 return;
7518 }
7519
7520 limb_cnt = cnt / BITS_PER_MPI_LIMB;
7521 wsize = usize + limb_cnt + 1;
7522 if( w->alloced < wsize )
7523 mpi_resize(w, wsize );
7524 wp = w->d;
7525 wsize = usize + limb_cnt;
7526 wsign = usign;
7527
7528 cnt %= BITS_PER_MPI_LIMB;
7529 if( cnt ) {
7530 wlimb = mpihelp_lshift( wp + limb_cnt, u->d, usize, cnt );
7531 if( wlimb ) {
7532 wp[wsize] = wlimb;
7533 wsize++;
7534 }
7535 }
7536 else {
7537 MPN_COPY_DECR( wp + limb_cnt, u->d, usize );
7538 }
7539
7540 /* Zero all whole limbs at low end. Do it here and not before calling
7541 * mpn_lshift, not to lose for U == W. */
7542 MPN_ZERO( wp, limb_cnt );
7543
7544 w->nlimbs = wsize;
7545 w->sign = wsign;
7546 }
7547
7548
7549
7550 void
7551 mpi_mul( MPI w, MPI u, MPI v)
7552 {
7553 mpi_size_t usize, vsize, wsize;
7554 mpi_ptr_t up, vp, wp;
7555 mpi_limb_t cy;
7556 int usign, vsign, usecure, vsecure, sign_product;
7557 int assign_wp=0;
7558 mpi_ptr_t tmp_limb=NULL;
7559
7560
7561 if( u->nlimbs < v->nlimbs ) { /* Swap U and V. */
7562 usize = v->nlimbs;
7563 usign = v->sign;
7564 usecure = mpi_is_secure(v);
7565 up = v->d;
7566 vsize = u->nlimbs;
7567 vsign = u->sign;
7568 vsecure = mpi_is_secure(u);
7569 vp = u->d;
7570 }
7571 else {
7572 usize = u->nlimbs;
7573 usign = u->sign;
7574 usecure = mpi_is_secure(u);
7575 up = u->d;
7576 vsize = v->nlimbs;
7577 vsign = v->sign;
7578 vsecure = mpi_is_secure(v);
7579 vp = v->d;
7580 }
7581 sign_product = usign ^ vsign;
7582 wp = w->d;
7583
7584 /* Ensure W has space enough to store the result. */
7585 wsize = usize + vsize;
7586 if ( !mpi_is_secure (w) && (mpi_is_secure (u) || mpi_is_secure (v)) ) {
7587 /* w is not allocated in secure space but u or v is. To make sure
7588 * that no temporray results are stored in w, we temporary use
7589 * a newly allocated limb space for w */
7590 wp = mpi_alloc_limb_space( wsize, 1 );
7591 assign_wp = 2; /* mark it as 2 so that we can later copy it back to
7592 * mormal memory */
7593 }
7594 else if( w->alloced < wsize ) {
7595 if( wp == up || wp == vp ) {
7596 wp = mpi_alloc_limb_space( wsize, mpi_is_secure(w) );
7597 assign_wp = 1;
7598 }
7599 else {
7600 mpi_resize(w, wsize );
7601 wp = w->d;
7602 }
7603 }
7604 else { /* Make U and V not overlap with W. */
7605 if( wp == up ) {
7606 /* W and U are identical. Allocate temporary space for U. */
7607 up = tmp_limb = mpi_alloc_limb_space( usize, usecure );
7608 /* Is V identical too? Keep it identical with U. */
7609 if( wp == vp )
7610 vp = up;
7611 /* Copy to the temporary space. */
7612 MPN_COPY( up, wp, usize );
7613 }
7614 else if( wp == vp ) {
7615 /* W and V are identical. Allocate temporary space for V. */
7616 vp = tmp_limb = mpi_alloc_limb_space( vsize, vsecure );
7617 /* Copy to the temporary space. */
7618 MPN_COPY( vp, wp, vsize );
7619 }
7620 }
7621
7622 if( !vsize )
7623 wsize = 0;
7624 else {
7625 cy = mpihelp_mul( wp, up, usize, vp, vsize );
7626 wsize -= cy? 0:1;
7627 }
7628
7629 if( assign_wp ) {
7630 if (assign_wp == 2) {
7631 /* copy the temp wp from secure memory back to normal memory */
7632 mpi_ptr_t tmp_wp = mpi_alloc_limb_space (wsize, 0);
7633 MPN_COPY (tmp_wp, wp, wsize);
7634 mpi_free_limb_space (wp);
7635 wp = tmp_wp;
7636 }
7637 mpi_assign_limb_space( w, wp, wsize );
7638 }
7639 w->nlimbs = wsize;
7640 w->sign = sign_product;
7641 if( tmp_limb )
7642 mpi_free_limb_space( tmp_limb );
7643 }
7644
7645
7646 void
7647 mpi_mulm( MPI w, MPI u, MPI v, MPI m)
7648 {
7649 mpi_mul(w, u, v);
7650 mpi_fdiv_r( w, w, m );
7651 }
7652