Debian Codegraph @

mpi/generic/mpih-rshift.c

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