| 1 |
/* mpi-inline.h - Internal to the Multi Precision Integers |
| 2 |
* Copyright (C) 1994, 1996, 1998, 1999, |
| 3 |
* 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 |
#ifndef G10_MPI_INLINE_H |
| 29 |
#define G10_MPI_INLINE_H |
| 30 |
|
| 31 |
#include "longlong.h" |
| 32 |
|
| 33 |
/* Starting with gcc 4.3 "extern inline" conforms in c99 mode to the |
| 34 |
c99 semantics. To keep the useful old semantics we use an |
| 35 |
attribute. */ |
| 36 |
#ifndef G10_MPI_INLINE_DECL |
| 37 |
# ifdef __GNUC_STDC_INLINE__ |
| 38 |
# define G10_MPI_INLINE_DECL extern inline __attribute__ ((__gnu_inline__)) |
| 39 |
# else |
| 40 |
# define G10_MPI_INLINE_DECL extern __inline__ |
| 41 |
# endif |
| 42 |
#endif |
| 43 |
|
| 44 |
G10_MPI_INLINE_DECL mpi_limb_t |
| 45 |
_gcry_mpih_add_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, |
| 46 |
mpi_size_t s1_size, mpi_limb_t s2_limb) |
| 47 |
{ |
| 48 |
mpi_limb_t x; |
| 49 |
|
| 50 |
x = *s1_ptr++; |
| 51 |
s2_limb += x; |
| 52 |
*res_ptr++ = s2_limb; |
| 53 |
if( s2_limb < x ) { /* sum is less than the left operand: handle carry */ |
| 54 |
while( --s1_size ) { |
| 55 |
x = *s1_ptr++ + 1; /* add carry */ |
| 56 |
*res_ptr++ = x; /* and store */ |
| 57 |
if( x ) /* not 0 (no overflow): we can stop */ |
| 58 |
goto leave; |
| 59 |
} |
| 60 |
return 1; /* return carry (size of s1 to small) */ |
| 61 |
} |
| 62 |
|
| 63 |
leave: |
| 64 |
if( res_ptr != s1_ptr ) { /* not the same variable */ |
| 65 |
mpi_size_t i; /* copy the rest */ |
| 66 |
for( i=0; i < s1_size-1; i++ ) |
| 67 |
res_ptr[i] = s1_ptr[i]; |
| 68 |
} |
| 69 |
return 0; /* no carry */ |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
/* Do same calculation as _gcry_mpih_add_1 does (under the condition |
| 74 |
of RES_PTR == S1_PTR), Least Leak Intended. */ |
| 75 |
G10_MPI_INLINE_DECL mpi_limb_t |
| 76 |
_gcry_mpih_add_1_lli (mpi_ptr_t s1_ptr, mpi_size_t s1_size, mpi_limb_t s2_limb) |
| 77 |
{ |
| 78 |
mpi_limb_t x; |
| 79 |
mpi_limb_t cy; |
| 80 |
|
| 81 |
cy = s2_limb; |
| 82 |
while ( s1_size ) |
| 83 |
{ |
| 84 |
x = *s1_ptr; |
| 85 |
add_ssaaaa (cy, x, 0, cy, 0, x); |
| 86 |
*s1_ptr++ = x; |
| 87 |
s1_size--; |
| 88 |
} |
| 89 |
return cy; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
G10_MPI_INLINE_DECL mpi_limb_t |
| 94 |
_gcry_mpih_add(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size, |
| 95 |
mpi_ptr_t s2_ptr, mpi_size_t s2_size) |
| 96 |
{ |
| 97 |
mpi_limb_t cy = 0; |
| 98 |
|
| 99 |
if( s2_size ) |
| 100 |
cy = _gcry_mpih_add_n( res_ptr, s1_ptr, s2_ptr, s2_size ); |
| 101 |
|
| 102 |
if( s1_size - s2_size ) |
| 103 |
cy = _gcry_mpih_add_1( res_ptr + s2_size, s1_ptr + s2_size, |
| 104 |
s1_size - s2_size, cy); |
| 105 |
return cy; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
G10_MPI_INLINE_DECL mpi_limb_t |
| 111 |
_gcry_mpih_sub_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, |
| 112 |
mpi_size_t s1_size, mpi_limb_t s2_limb ) |
| 113 |
{ |
| 114 |
mpi_limb_t x; |
| 115 |
|
| 116 |
x = *s1_ptr++; |
| 117 |
s2_limb = x - s2_limb; |
| 118 |
*res_ptr++ = s2_limb; |
| 119 |
if( s2_limb > x ) { |
| 120 |
while( --s1_size ) { |
| 121 |
x = *s1_ptr++; |
| 122 |
*res_ptr++ = x - 1; |
| 123 |
if( x ) |
| 124 |
goto leave; |
| 125 |
} |
| 126 |
return 1; |
| 127 |
} |
| 128 |
|
| 129 |
leave: |
| 130 |
if( res_ptr != s1_ptr ) { |
| 131 |
mpi_size_t i; |
| 132 |
for( i=0; i < s1_size-1; i++ ) |
| 133 |
res_ptr[i] = s1_ptr[i]; |
| 134 |
} |
| 135 |
return 0; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
G10_MPI_INLINE_DECL mpi_limb_t |
| 141 |
_gcry_mpih_sub( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size, |
| 142 |
mpi_ptr_t s2_ptr, mpi_size_t s2_size) |
| 143 |
{ |
| 144 |
mpi_limb_t cy = 0; |
| 145 |
|
| 146 |
if( s2_size ) |
| 147 |
cy = _gcry_mpih_sub_n(res_ptr, s1_ptr, s2_ptr, s2_size); |
| 148 |
|
| 149 |
if( s1_size - s2_size ) |
| 150 |
cy = _gcry_mpih_sub_1(res_ptr + s2_size, s1_ptr + s2_size, |
| 151 |
s1_size - s2_size, cy); |
| 152 |
return cy; |
| 153 |
} |
| 154 |
|
| 155 |
/**************** |
| 156 |
* Compare OP1_PTR/OP1_SIZE with OP2_PTR/OP2_SIZE. |
| 157 |
* There are no restrictions on the relative sizes of |
| 158 |
* the two arguments. |
| 159 |
* Return 1 if OP1 > OP2, 0 if they are equal, and -1 if OP1 < OP2. |
| 160 |
*/ |
| 161 |
G10_MPI_INLINE_DECL int |
| 162 |
_gcry_mpih_cmp( mpi_ptr_t op1_ptr, mpi_ptr_t op2_ptr, mpi_size_t size ) |
| 163 |
{ |
| 164 |
mpi_size_t i; |
| 165 |
mpi_limb_t op1_word, op2_word; |
| 166 |
|
| 167 |
for( i = size - 1; i >= 0 ; i--) { |
| 168 |
op1_word = op1_ptr[i]; |
| 169 |
op2_word = op2_ptr[i]; |
| 170 |
if( op1_word != op2_word ) |
| 171 |
goto diff; |
| 172 |
} |
| 173 |
return 0; |
| 174 |
|
| 175 |
diff: |
| 176 |
/* This can *not* be simplified to |
| 177 |
* op2_word - op2_word |
| 178 |
* since that expression might give signed overflow. */ |
| 179 |
return (op1_word > op2_word) ? 1 : -1; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
#endif /*G10_MPI_INLINE_H*/ |