Xuyi Wang / wolfSSL

Dependents:   OS

Committer:
wolfSSL
Date:
Sat Aug 18 22:20:43 2018 +0000
Revision:
15:117db924cf7c
wolfSSL 3.15.3

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 15:117db924cf7c 1 /* misc.c
wolfSSL 15:117db924cf7c 2 *
wolfSSL 15:117db924cf7c 3 * Copyright (C) 2006-2017 wolfSSL Inc.
wolfSSL 15:117db924cf7c 4 *
wolfSSL 15:117db924cf7c 5 * This file is part of wolfSSL.
wolfSSL 15:117db924cf7c 6 *
wolfSSL 15:117db924cf7c 7 * wolfSSL is free software; you can redistribute it and/or modify
wolfSSL 15:117db924cf7c 8 * it under the terms of the GNU General Public License as published by
wolfSSL 15:117db924cf7c 9 * the Free Software Foundation; either version 2 of the License, or
wolfSSL 15:117db924cf7c 10 * (at your option) any later version.
wolfSSL 15:117db924cf7c 11 *
wolfSSL 15:117db924cf7c 12 * wolfSSL is distributed in the hope that it will be useful,
wolfSSL 15:117db924cf7c 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wolfSSL 15:117db924cf7c 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wolfSSL 15:117db924cf7c 15 * GNU General Public License for more details.
wolfSSL 15:117db924cf7c 16 *
wolfSSL 15:117db924cf7c 17 * You should have received a copy of the GNU General Public License
wolfSSL 15:117db924cf7c 18 * along with this program; if not, write to the Free Software
wolfSSL 15:117db924cf7c 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
wolfSSL 15:117db924cf7c 20 */
wolfSSL 15:117db924cf7c 21
wolfSSL 15:117db924cf7c 22
wolfSSL 15:117db924cf7c 23 #ifdef HAVE_CONFIG_H
wolfSSL 15:117db924cf7c 24 #include <config.h>
wolfSSL 15:117db924cf7c 25 #endif
wolfSSL 15:117db924cf7c 26
wolfSSL 15:117db924cf7c 27 #include <wolfssl/wolfcrypt/settings.h>
wolfSSL 15:117db924cf7c 28
wolfSSL 15:117db924cf7c 29 #ifndef WOLF_CRYPT_MISC_C
wolfSSL 15:117db924cf7c 30 #define WOLF_CRYPT_MISC_C
wolfSSL 15:117db924cf7c 31
wolfSSL 15:117db924cf7c 32 #include <wolfssl/wolfcrypt/misc.h>
wolfSSL 15:117db924cf7c 33
wolfSSL 15:117db924cf7c 34 /* inlining these functions is a huge speed increase and a small size decrease,
wolfSSL 15:117db924cf7c 35 because the functions are smaller than function call setup/cleanup, e.g.,
wolfSSL 15:117db924cf7c 36 md5 benchmark is twice as fast with inline. If you don't want it, then
wolfSSL 15:117db924cf7c 37 define NO_INLINE and compile this file into wolfssl, otherwise it's used as
wolfSSL 15:117db924cf7c 38 a source header
wolfSSL 15:117db924cf7c 39 */
wolfSSL 15:117db924cf7c 40
wolfSSL 15:117db924cf7c 41 #ifdef NO_INLINE
wolfSSL 15:117db924cf7c 42 #define STATIC
wolfSSL 15:117db924cf7c 43 #else
wolfSSL 15:117db924cf7c 44 #define STATIC static
wolfSSL 15:117db924cf7c 45 #endif
wolfSSL 15:117db924cf7c 46
wolfSSL 15:117db924cf7c 47 /* Check for if compiling misc.c when not needed. */
wolfSSL 15:117db924cf7c 48 #if !defined(WOLFSSL_MISC_INCLUDED) && !defined(NO_INLINE)
wolfSSL 15:117db924cf7c 49 #ifndef WOLFSSL_IGNORE_FILE_WARN
wolfSSL 15:117db924cf7c 50 #warning misc.c does not need to be compiled when using inline (NO_INLINE not defined)
wolfSSL 15:117db924cf7c 51 #endif
wolfSSL 15:117db924cf7c 52
wolfSSL 15:117db924cf7c 53 #else
wolfSSL 15:117db924cf7c 54
wolfSSL 15:117db924cf7c 55
wolfSSL 15:117db924cf7c 56 #if defined(__ICCARM__)
wolfSSL 15:117db924cf7c 57 #include <intrinsics.h>
wolfSSL 15:117db924cf7c 58 #endif
wolfSSL 15:117db924cf7c 59
wolfSSL 15:117db924cf7c 60
wolfSSL 15:117db924cf7c 61 #ifdef INTEL_INTRINSICS
wolfSSL 15:117db924cf7c 62
wolfSSL 15:117db924cf7c 63 #include <stdlib.h> /* get intrinsic definitions */
wolfSSL 15:117db924cf7c 64
wolfSSL 15:117db924cf7c 65 /* for non visual studio probably need no long version, 32 bit only
wolfSSL 15:117db924cf7c 66 * i.e., _rotl and _rotr */
wolfSSL 15:117db924cf7c 67 #pragma intrinsic(_lrotl, _lrotr)
wolfSSL 15:117db924cf7c 68
wolfSSL 15:117db924cf7c 69 STATIC WC_INLINE word32 rotlFixed(word32 x, word32 y)
wolfSSL 15:117db924cf7c 70 {
wolfSSL 15:117db924cf7c 71 return y ? _lrotl(x, y) : x;
wolfSSL 15:117db924cf7c 72 }
wolfSSL 15:117db924cf7c 73
wolfSSL 15:117db924cf7c 74 STATIC WC_INLINE word32 rotrFixed(word32 x, word32 y)
wolfSSL 15:117db924cf7c 75 {
wolfSSL 15:117db924cf7c 76 return y ? _lrotr(x, y) : x;
wolfSSL 15:117db924cf7c 77 }
wolfSSL 15:117db924cf7c 78
wolfSSL 15:117db924cf7c 79 #else /* generic */
wolfSSL 15:117db924cf7c 80
wolfSSL 15:117db924cf7c 81 STATIC WC_INLINE word32 rotlFixed(word32 x, word32 y)
wolfSSL 15:117db924cf7c 82 {
wolfSSL 15:117db924cf7c 83 return (x << y) | (x >> (sizeof(y) * 8 - y));
wolfSSL 15:117db924cf7c 84 }
wolfSSL 15:117db924cf7c 85
wolfSSL 15:117db924cf7c 86
wolfSSL 15:117db924cf7c 87 STATIC WC_INLINE word32 rotrFixed(word32 x, word32 y)
wolfSSL 15:117db924cf7c 88 {
wolfSSL 15:117db924cf7c 89 return (x >> y) | (x << (sizeof(y) * 8 - y));
wolfSSL 15:117db924cf7c 90 }
wolfSSL 15:117db924cf7c 91
wolfSSL 15:117db924cf7c 92 #endif
wolfSSL 15:117db924cf7c 93
wolfSSL 15:117db924cf7c 94
wolfSSL 15:117db924cf7c 95 STATIC WC_INLINE word32 ByteReverseWord32(word32 value)
wolfSSL 15:117db924cf7c 96 {
wolfSSL 15:117db924cf7c 97 #ifdef PPC_INTRINSICS
wolfSSL 15:117db924cf7c 98 /* PPC: load reverse indexed instruction */
wolfSSL 15:117db924cf7c 99 return (word32)__lwbrx(&value,0);
wolfSSL 15:117db924cf7c 100 #elif defined(__ICCARM__)
wolfSSL 15:117db924cf7c 101 return (word32)__REV(value);
wolfSSL 15:117db924cf7c 102 #elif defined(KEIL_INTRINSICS)
wolfSSL 15:117db924cf7c 103 return (word32)__rev(value);
wolfSSL 15:117db924cf7c 104 #elif defined(WOLF_ALLOW_BUILTIN) && \
wolfSSL 15:117db924cf7c 105 defined(__GNUC_PREREQ) && __GNUC_PREREQ(4, 3)
wolfSSL 15:117db924cf7c 106 return (word32)__builtin_bswap32(value);
wolfSSL 15:117db924cf7c 107 #elif defined(FAST_ROTATE)
wolfSSL 15:117db924cf7c 108 /* 5 instructions with rotate instruction, 9 without */
wolfSSL 15:117db924cf7c 109 return (rotrFixed(value, 8U) & 0xff00ff00) |
wolfSSL 15:117db924cf7c 110 (rotlFixed(value, 8U) & 0x00ff00ff);
wolfSSL 15:117db924cf7c 111 #else
wolfSSL 15:117db924cf7c 112 /* 6 instructions with rotate instruction, 8 without */
wolfSSL 15:117db924cf7c 113 value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
wolfSSL 15:117db924cf7c 114 return rotlFixed(value, 16U);
wolfSSL 15:117db924cf7c 115 #endif
wolfSSL 15:117db924cf7c 116 }
wolfSSL 15:117db924cf7c 117
wolfSSL 15:117db924cf7c 118
wolfSSL 15:117db924cf7c 119 STATIC WC_INLINE void ByteReverseWords(word32* out, const word32* in,
wolfSSL 15:117db924cf7c 120 word32 byteCount)
wolfSSL 15:117db924cf7c 121 {
wolfSSL 15:117db924cf7c 122 word32 count = byteCount/(word32)sizeof(word32), i;
wolfSSL 15:117db924cf7c 123
wolfSSL 15:117db924cf7c 124 for (i = 0; i < count; i++)
wolfSSL 15:117db924cf7c 125 out[i] = ByteReverseWord32(in[i]);
wolfSSL 15:117db924cf7c 126
wolfSSL 15:117db924cf7c 127 }
wolfSSL 15:117db924cf7c 128
wolfSSL 15:117db924cf7c 129
wolfSSL 15:117db924cf7c 130 #ifdef WORD64_AVAILABLE
wolfSSL 15:117db924cf7c 131
wolfSSL 15:117db924cf7c 132
wolfSSL 15:117db924cf7c 133 STATIC WC_INLINE word64 rotlFixed64(word64 x, word64 y)
wolfSSL 15:117db924cf7c 134 {
wolfSSL 15:117db924cf7c 135 return (x << y) | (x >> (sizeof(y) * 8 - y));
wolfSSL 15:117db924cf7c 136 }
wolfSSL 15:117db924cf7c 137
wolfSSL 15:117db924cf7c 138
wolfSSL 15:117db924cf7c 139 STATIC WC_INLINE word64 rotrFixed64(word64 x, word64 y)
wolfSSL 15:117db924cf7c 140 {
wolfSSL 15:117db924cf7c 141 return (x >> y) | (x << (sizeof(y) * 8 - y));
wolfSSL 15:117db924cf7c 142 }
wolfSSL 15:117db924cf7c 143
wolfSSL 15:117db924cf7c 144
wolfSSL 15:117db924cf7c 145 STATIC WC_INLINE word64 ByteReverseWord64(word64 value)
wolfSSL 15:117db924cf7c 146 {
wolfSSL 15:117db924cf7c 147 #if defined(WOLF_ALLOW_BUILTIN) && defined(__GNUC_PREREQ) && __GNUC_PREREQ(4, 3)
wolfSSL 15:117db924cf7c 148 return (word64)__builtin_bswap64(value);
wolfSSL 15:117db924cf7c 149 #elif defined(WOLFCRYPT_SLOW_WORD64)
wolfSSL 15:117db924cf7c 150 return (word64)((word64)ByteReverseWord32((word32) value)) << 32 |
wolfSSL 15:117db924cf7c 151 (word64)ByteReverseWord32((word32)(value >> 32));
wolfSSL 15:117db924cf7c 152 #else
wolfSSL 15:117db924cf7c 153 value = ((value & W64LIT(0xFF00FF00FF00FF00)) >> 8) |
wolfSSL 15:117db924cf7c 154 ((value & W64LIT(0x00FF00FF00FF00FF)) << 8);
wolfSSL 15:117db924cf7c 155 value = ((value & W64LIT(0xFFFF0000FFFF0000)) >> 16) |
wolfSSL 15:117db924cf7c 156 ((value & W64LIT(0x0000FFFF0000FFFF)) << 16);
wolfSSL 15:117db924cf7c 157 return rotlFixed64(value, 32U);
wolfSSL 15:117db924cf7c 158 #endif
wolfSSL 15:117db924cf7c 159 }
wolfSSL 15:117db924cf7c 160
wolfSSL 15:117db924cf7c 161
wolfSSL 15:117db924cf7c 162 STATIC WC_INLINE void ByteReverseWords64(word64* out, const word64* in,
wolfSSL 15:117db924cf7c 163 word32 byteCount)
wolfSSL 15:117db924cf7c 164 {
wolfSSL 15:117db924cf7c 165 word32 count = byteCount/(word32)sizeof(word64), i;
wolfSSL 15:117db924cf7c 166
wolfSSL 15:117db924cf7c 167 for (i = 0; i < count; i++)
wolfSSL 15:117db924cf7c 168 out[i] = ByteReverseWord64(in[i]);
wolfSSL 15:117db924cf7c 169
wolfSSL 15:117db924cf7c 170 }
wolfSSL 15:117db924cf7c 171
wolfSSL 15:117db924cf7c 172 #endif /* WORD64_AVAILABLE */
wolfSSL 15:117db924cf7c 173
wolfSSL 15:117db924cf7c 174
wolfSSL 15:117db924cf7c 175 STATIC WC_INLINE void XorWords(wolfssl_word* r, const wolfssl_word* a, word32 n)
wolfSSL 15:117db924cf7c 176 {
wolfSSL 15:117db924cf7c 177 word32 i;
wolfSSL 15:117db924cf7c 178
wolfSSL 15:117db924cf7c 179 for (i = 0; i < n; i++) r[i] ^= a[i];
wolfSSL 15:117db924cf7c 180 }
wolfSSL 15:117db924cf7c 181
wolfSSL 15:117db924cf7c 182
wolfSSL 15:117db924cf7c 183 STATIC WC_INLINE void xorbuf(void* buf, const void* mask, word32 count)
wolfSSL 15:117db924cf7c 184 {
wolfSSL 15:117db924cf7c 185 if (((wolfssl_word)buf | (wolfssl_word)mask | count) % WOLFSSL_WORD_SIZE == 0)
wolfSSL 15:117db924cf7c 186 XorWords( (wolfssl_word*)buf,
wolfSSL 15:117db924cf7c 187 (const wolfssl_word*)mask, count / WOLFSSL_WORD_SIZE);
wolfSSL 15:117db924cf7c 188 else {
wolfSSL 15:117db924cf7c 189 word32 i;
wolfSSL 15:117db924cf7c 190 byte* b = (byte*)buf;
wolfSSL 15:117db924cf7c 191 const byte* m = (const byte*)mask;
wolfSSL 15:117db924cf7c 192
wolfSSL 15:117db924cf7c 193 for (i = 0; i < count; i++) b[i] ^= m[i];
wolfSSL 15:117db924cf7c 194 }
wolfSSL 15:117db924cf7c 195 }
wolfSSL 15:117db924cf7c 196
wolfSSL 15:117db924cf7c 197
wolfSSL 15:117db924cf7c 198 /* Make sure compiler doesn't skip */
wolfSSL 15:117db924cf7c 199 STATIC WC_INLINE void ForceZero(const void* mem, word32 len)
wolfSSL 15:117db924cf7c 200 {
wolfSSL 15:117db924cf7c 201 volatile byte* z = (volatile byte*)mem;
wolfSSL 15:117db924cf7c 202
wolfSSL 15:117db924cf7c 203 #if defined(WOLFSSL_X86_64_BUILD) && defined(WORD64_AVAILABLE)
wolfSSL 15:117db924cf7c 204 volatile word64* w;
wolfSSL 15:117db924cf7c 205 #ifndef WOLFSSL_UNALIGNED_64BIT_ACCESS
wolfSSL 15:117db924cf7c 206 word32 l = (sizeof(word64) - ((size_t)z & (sizeof(word64)-1))) &
wolfSSL 15:117db924cf7c 207 (sizeof(word64)-1);
wolfSSL 15:117db924cf7c 208
wolfSSL 15:117db924cf7c 209 if (len < l) l = len;
wolfSSL 15:117db924cf7c 210 len -= l;
wolfSSL 15:117db924cf7c 211 while (l--) *z++ = 0;
wolfSSL 15:117db924cf7c 212 #endif
wolfSSL 15:117db924cf7c 213 for (w = (volatile word64*)z; len >= sizeof(*w); len -= sizeof(*w))
wolfSSL 15:117db924cf7c 214 *w++ = 0;
wolfSSL 15:117db924cf7c 215 z = (volatile byte*)w;
wolfSSL 15:117db924cf7c 216 #endif
wolfSSL 15:117db924cf7c 217
wolfSSL 15:117db924cf7c 218 while (len--) *z++ = 0;
wolfSSL 15:117db924cf7c 219 }
wolfSSL 15:117db924cf7c 220
wolfSSL 15:117db924cf7c 221
wolfSSL 15:117db924cf7c 222 /* check all length bytes for equality, return 0 on success */
wolfSSL 15:117db924cf7c 223 STATIC WC_INLINE int ConstantCompare(const byte* a, const byte* b, int length)
wolfSSL 15:117db924cf7c 224 {
wolfSSL 15:117db924cf7c 225 int i;
wolfSSL 15:117db924cf7c 226 int compareSum = 0;
wolfSSL 15:117db924cf7c 227
wolfSSL 15:117db924cf7c 228 for (i = 0; i < length; i++) {
wolfSSL 15:117db924cf7c 229 compareSum |= a[i] ^ b[i];
wolfSSL 15:117db924cf7c 230 }
wolfSSL 15:117db924cf7c 231
wolfSSL 15:117db924cf7c 232 return compareSum;
wolfSSL 15:117db924cf7c 233 }
wolfSSL 15:117db924cf7c 234
wolfSSL 15:117db924cf7c 235
wolfSSL 15:117db924cf7c 236 #ifndef WOLFSSL_HAVE_MIN
wolfSSL 15:117db924cf7c 237 #define WOLFSSL_HAVE_MIN
wolfSSL 15:117db924cf7c 238 #if defined(HAVE_FIPS) && !defined(min) /* so ifdef check passes */
wolfSSL 15:117db924cf7c 239 #define min min
wolfSSL 15:117db924cf7c 240 #endif
wolfSSL 15:117db924cf7c 241 STATIC WC_INLINE word32 min(word32 a, word32 b)
wolfSSL 15:117db924cf7c 242 {
wolfSSL 15:117db924cf7c 243 return a > b ? b : a;
wolfSSL 15:117db924cf7c 244 }
wolfSSL 15:117db924cf7c 245 #endif /* !WOLFSSL_HAVE_MIN */
wolfSSL 15:117db924cf7c 246
wolfSSL 15:117db924cf7c 247 #ifndef WOLFSSL_HAVE_MAX
wolfSSL 15:117db924cf7c 248 #define WOLFSSL_HAVE_MAX
wolfSSL 15:117db924cf7c 249 #if defined(HAVE_FIPS) && !defined(max) /* so ifdef check passes */
wolfSSL 15:117db924cf7c 250 #define max max
wolfSSL 15:117db924cf7c 251 #endif
wolfSSL 15:117db924cf7c 252 STATIC WC_INLINE word32 max(word32 a, word32 b)
wolfSSL 15:117db924cf7c 253 {
wolfSSL 15:117db924cf7c 254 return a > b ? a : b;
wolfSSL 15:117db924cf7c 255 }
wolfSSL 15:117db924cf7c 256 #endif /* !WOLFSSL_HAVE_MAX */
wolfSSL 15:117db924cf7c 257
wolfSSL 15:117db924cf7c 258 /* converts a 32 bit integer to 24 bit */
wolfSSL 15:117db924cf7c 259 STATIC WC_INLINE void c32to24(word32 in, word24 out)
wolfSSL 15:117db924cf7c 260 {
wolfSSL 15:117db924cf7c 261 out[0] = (in >> 16) & 0xff;
wolfSSL 15:117db924cf7c 262 out[1] = (in >> 8) & 0xff;
wolfSSL 15:117db924cf7c 263 out[2] = in & 0xff;
wolfSSL 15:117db924cf7c 264 }
wolfSSL 15:117db924cf7c 265
wolfSSL 15:117db924cf7c 266 /* convert 16 bit integer to opaque */
wolfSSL 15:117db924cf7c 267 STATIC WC_INLINE void c16toa(word16 wc_u16, byte* c)
wolfSSL 15:117db924cf7c 268 {
wolfSSL 15:117db924cf7c 269 c[0] = (wc_u16 >> 8) & 0xff;
wolfSSL 15:117db924cf7c 270 c[1] = wc_u16 & 0xff;
wolfSSL 15:117db924cf7c 271 }
wolfSSL 15:117db924cf7c 272
wolfSSL 15:117db924cf7c 273 /* convert 32 bit integer to opaque */
wolfSSL 15:117db924cf7c 274 STATIC WC_INLINE void c32toa(word32 wc_u32, byte* c)
wolfSSL 15:117db924cf7c 275 {
wolfSSL 15:117db924cf7c 276 c[0] = (wc_u32 >> 24) & 0xff;
wolfSSL 15:117db924cf7c 277 c[1] = (wc_u32 >> 16) & 0xff;
wolfSSL 15:117db924cf7c 278 c[2] = (wc_u32 >> 8) & 0xff;
wolfSSL 15:117db924cf7c 279 c[3] = wc_u32 & 0xff;
wolfSSL 15:117db924cf7c 280 }
wolfSSL 15:117db924cf7c 281
wolfSSL 15:117db924cf7c 282 /* convert a 24 bit integer into a 32 bit one */
wolfSSL 15:117db924cf7c 283 STATIC WC_INLINE void c24to32(const word24 wc_u24, word32* wc_u32)
wolfSSL 15:117db924cf7c 284 {
wolfSSL 15:117db924cf7c 285 *wc_u32 = (wc_u24[0] << 16) | (wc_u24[1] << 8) | wc_u24[2];
wolfSSL 15:117db924cf7c 286 }
wolfSSL 15:117db924cf7c 287
wolfSSL 15:117db924cf7c 288
wolfSSL 15:117db924cf7c 289 /* convert opaque to 24 bit integer */
wolfSSL 15:117db924cf7c 290 STATIC WC_INLINE void ato24(const byte* c, word32* wc_u24)
wolfSSL 15:117db924cf7c 291 {
wolfSSL 15:117db924cf7c 292 *wc_u24 = (c[0] << 16) | (c[1] << 8) | c[2];
wolfSSL 15:117db924cf7c 293 }
wolfSSL 15:117db924cf7c 294
wolfSSL 15:117db924cf7c 295 /* convert opaque to 16 bit integer */
wolfSSL 15:117db924cf7c 296 STATIC WC_INLINE void ato16(const byte* c, word16* wc_u16)
wolfSSL 15:117db924cf7c 297 {
wolfSSL 15:117db924cf7c 298 *wc_u16 = (word16) ((c[0] << 8) | (c[1]));
wolfSSL 15:117db924cf7c 299 }
wolfSSL 15:117db924cf7c 300
wolfSSL 15:117db924cf7c 301 /* convert opaque to 32 bit integer */
wolfSSL 15:117db924cf7c 302 STATIC WC_INLINE void ato32(const byte* c, word32* wc_u32)
wolfSSL 15:117db924cf7c 303 {
wolfSSL 15:117db924cf7c 304 *wc_u32 = ((word32)c[0] << 24) | (c[1] << 16) | (c[2] << 8) | c[3];
wolfSSL 15:117db924cf7c 305 }
wolfSSL 15:117db924cf7c 306
wolfSSL 15:117db924cf7c 307
wolfSSL 15:117db924cf7c 308 STATIC WC_INLINE word32 btoi(byte b)
wolfSSL 15:117db924cf7c 309 {
wolfSSL 15:117db924cf7c 310 return (word32)(b - 0x30);
wolfSSL 15:117db924cf7c 311 }
wolfSSL 15:117db924cf7c 312
wolfSSL 15:117db924cf7c 313
wolfSSL 15:117db924cf7c 314 /* Constant time - mask set when a > b. */
wolfSSL 15:117db924cf7c 315 STATIC WC_INLINE byte ctMaskGT(int a, int b)
wolfSSL 15:117db924cf7c 316 {
wolfSSL 15:117db924cf7c 317 return (((word32)a - b - 1) >> 31) - 1;
wolfSSL 15:117db924cf7c 318 }
wolfSSL 15:117db924cf7c 319
wolfSSL 15:117db924cf7c 320 /* Constant time - mask set when a >= b. */
wolfSSL 15:117db924cf7c 321 STATIC WC_INLINE byte ctMaskGTE(int a, int b)
wolfSSL 15:117db924cf7c 322 {
wolfSSL 15:117db924cf7c 323 return (((word32)a - b ) >> 31) - 1;
wolfSSL 15:117db924cf7c 324 }
wolfSSL 15:117db924cf7c 325
wolfSSL 15:117db924cf7c 326 /* Constant time - mask set when a < b. */
wolfSSL 15:117db924cf7c 327 STATIC WC_INLINE byte ctMaskLT(int a, int b)
wolfSSL 15:117db924cf7c 328 {
wolfSSL 15:117db924cf7c 329 return (((word32)b - a - 1) >> 31) - 1;
wolfSSL 15:117db924cf7c 330 }
wolfSSL 15:117db924cf7c 331
wolfSSL 15:117db924cf7c 332 /* Constant time - mask set when a <= b. */
wolfSSL 15:117db924cf7c 333 STATIC WC_INLINE byte ctMaskLTE(int a, int b)
wolfSSL 15:117db924cf7c 334 {
wolfSSL 15:117db924cf7c 335 return (((word32)b - a ) >> 31) - 1;
wolfSSL 15:117db924cf7c 336 }
wolfSSL 15:117db924cf7c 337
wolfSSL 15:117db924cf7c 338 /* Constant time - mask set when a == b. */
wolfSSL 15:117db924cf7c 339 STATIC WC_INLINE byte ctMaskEq(int a, int b)
wolfSSL 15:117db924cf7c 340 {
wolfSSL 15:117db924cf7c 341 return 0 - (a == b);
wolfSSL 15:117db924cf7c 342 }
wolfSSL 15:117db924cf7c 343
wolfSSL 15:117db924cf7c 344 /* Constant time - select b when mask is set and a otherwise. */
wolfSSL 15:117db924cf7c 345 STATIC WC_INLINE byte ctMaskSel(byte m, byte a, byte b)
wolfSSL 15:117db924cf7c 346 {
wolfSSL 15:117db924cf7c 347 return (a & ((byte)~(word32)m)) | (b & m);
wolfSSL 15:117db924cf7c 348 }
wolfSSL 15:117db924cf7c 349
wolfSSL 15:117db924cf7c 350 /* Constant time - bit set when a <= b. */
wolfSSL 15:117db924cf7c 351 STATIC WC_INLINE byte ctSetLTE(int a, int b)
wolfSSL 15:117db924cf7c 352 {
wolfSSL 15:117db924cf7c 353 return ((word32)a - b - 1) >> 31;
wolfSSL 15:117db924cf7c 354 }
wolfSSL 15:117db924cf7c 355
wolfSSL 15:117db924cf7c 356
wolfSSL 15:117db924cf7c 357 #undef STATIC
wolfSSL 15:117db924cf7c 358
wolfSSL 15:117db924cf7c 359 #endif /* !WOLFSSL_MISC_INCLUDED && !NO_INLINE */
wolfSSL 15:117db924cf7c 360
wolfSSL 15:117db924cf7c 361 #endif /* WOLF_CRYPT_MISC_C */
wolfSSL 15:117db924cf7c 362