raw
mpi-genesis~            1 /* mpih-rshift.c  -  MPI helper functions
mpi-genesis~ 2 * Copyright (C) 1994, 1996, 1998, 1999,
mpi-genesis~ 3 * 2000, 2001 Free Software Foundation, Inc.
mpi-genesis~ 4 *
mpi-genesis~ 5 * This file is part of GNUPG
mpi-genesis~ 6 *
mpi-genesis~ 7 * GNUPG is free software; you can redistribute it and/or modify
mpi-genesis~ 8 * it under the terms of the GNU General Public License as published by
mpi-genesis~ 9 * the Free Software Foundation; either version 3 of the License, or
mpi-genesis~ 10 * (at your option) any later version.
mpi-genesis~ 11 *
mpi-genesis~ 12 * GNUPG is distributed in the hope that it will be useful,
mpi-genesis~ 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
mpi-genesis~ 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
mpi-genesis~ 15 * GNU General Public License for more details.
mpi-genesis~ 16 *
mpi-genesis~ 17 * You should have received a copy of the GNU General Public License
mpi-genesis~ 18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
mpi-genesis~ 19 *
mpi-genesis~ 20 * Note: This code is heavily based on the GNU MP Library.
mpi-genesis~ 21 * Actually it's the same code with only minor changes in the
mpi-genesis~ 22 * way the data is stored; this is to support the abstraction
mpi-genesis~ 23 * of an optional secure memory allocation which may be used
mpi-genesis~ 24 * to avoid revealing of sensitive data due to paging etc.
mpi-genesis~ 25 * The GNU MP Library itself is published under the LGPL;
mpi-genesis~ 26 * however I decided to publish this code under the plain GPL.
mpi-genesis~ 27 */
mpi-genesis~ 28
mpi-genesis~ 29 #include <config.h>
mpi-genesis~ 30 #include <stdio.h>
mpi-genesis~ 31 #include <stdlib.h>
mpi-genesis~ 32 #include "mpi-internal.h"
mpi-genesis~ 33
mpi-genesis~ 34
mpi-genesis~ 35 /* Shift U (pointed to by UP and USIZE limbs long) CNT bits to the right
mpi-genesis~ 36 * and store the USIZE least significant limbs of the result at WP.
mpi-genesis~ 37 * The bits shifted out to the right are returned.
mpi-genesis~ 38 *
mpi-genesis~ 39 * Argument constraints:
mpi-genesis~ 40 * 1. 0 < CNT < BITS_PER_MP_LIMB
mpi-genesis~ 41 * 2. If the result is to be written over the input, WP must be <= UP.
mpi-genesis~ 42 */
mpi-genesis~ 43
mpi-genesis~ 44 mpi_limb_t
mpi-genesis~ 45 mpihelp_rshift( mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize, unsigned cnt)
mpi-genesis~ 46 {
mpi-genesis~ 47 mpi_limb_t high_limb, low_limb;
mpi-genesis~ 48 unsigned sh_1, sh_2;
mpi-genesis~ 49 mpi_size_t i;
mpi-genesis~ 50 mpi_limb_t retval;
mpi-genesis~ 51
mpi-genesis~ 52 sh_1 = cnt;
mpi-genesis~ 53 wp -= 1;
mpi-genesis~ 54 sh_2 = BITS_PER_MPI_LIMB - sh_1;
mpi-genesis~ 55 high_limb = up[0];
mpi-genesis~ 56 retval = high_limb << sh_2;
mpi-genesis~ 57 low_limb = high_limb;
mpi-genesis~ 58 for( i=1; i < usize; i++) {
mpi-genesis~ 59 high_limb = up[i];
mpi-genesis~ 60 wp[i] = (low_limb >> sh_1) | (high_limb << sh_2);
mpi-genesis~ 61 low_limb = high_limb;
mpi-genesis~ 62 }
mpi-genesis~ 63 wp[i] = low_limb >> sh_1;
mpi-genesis~ 64
mpi-genesis~ 65 return retval;
mpi-genesis~ 66 }
mpi-genesis~ 67