Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* mpih-rshift.c  -  MPI helper functions
0003  * Copyright (C) 1994, 1996, 1998, 1999,
0004  *               2000, 2001 Free Software Foundation, Inc.
0005  *
0006  * This file is part of GNUPG
0007  *
0008  * Note: This code is heavily based on the GNU MP Library.
0009  *   Actually it's the same code with only minor changes in the
0010  *   way the data is stored; this is to support the abstraction
0011  *   of an optional secure memory allocation which may be used
0012  *   to avoid revealing of sensitive data due to paging etc.
0013  *   The GNU MP Library itself is published under the LGPL;
0014  *   however I decided to publish this code under the plain GPL.
0015  */
0016 
0017 #include "mpi-internal.h"
0018 
0019 /* Shift U (pointed to by UP and USIZE limbs long) CNT bits to the right
0020  * and store the USIZE least significant limbs of the result at WP.
0021  * The bits shifted out to the right are returned.
0022  *
0023  * Argument constraints:
0024  * 1. 0 < CNT < BITS_PER_MP_LIMB
0025  * 2. If the result is to be written over the input, WP must be <= UP.
0026  */
0027 
0028 mpi_limb_t
0029 mpihelp_rshift(mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize, unsigned cnt)
0030 {
0031     mpi_limb_t high_limb, low_limb;
0032     unsigned sh_1, sh_2;
0033     mpi_size_t i;
0034     mpi_limb_t retval;
0035 
0036     sh_1 = cnt;
0037     wp -= 1;
0038     sh_2 = BITS_PER_MPI_LIMB - sh_1;
0039     high_limb = up[0];
0040     retval = high_limb << sh_2;
0041     low_limb = high_limb;
0042     for (i = 1; i < usize; i++) {
0043         high_limb = up[i];
0044         wp[i] = (low_limb >> sh_1) | (high_limb << sh_2);
0045         low_limb = high_limb;
0046     }
0047     wp[i] = low_limb >> sh_1;
0048 
0049     return retval;
0050 }