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