wolfSSL 3.11.1 for TLS1.3 beta
Fork of wolfSSL by
wolfcrypt/src/misc.c@13:80fb167dafdf, 2017-05-30 (annotated)
- Committer:
- wolfSSL
- Date:
- Tue May 30 06:16:19 2017 +0000
- Revision:
- 13:80fb167dafdf
- Parent:
- 11:cee25a834751
wolfSSL 3.11.1: TLS1.3 Beta
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wolfSSL | 11:cee25a834751 | 1 | /* misc.c |
wolfSSL | 11:cee25a834751 | 2 | * |
wolfSSL | 11:cee25a834751 | 3 | * Copyright (C) 2006-2016 wolfSSL Inc. |
wolfSSL | 11:cee25a834751 | 4 | * |
wolfSSL | 11:cee25a834751 | 5 | * This file is part of wolfSSL. |
wolfSSL | 11:cee25a834751 | 6 | * |
wolfSSL | 11:cee25a834751 | 7 | * wolfSSL is free software; you can redistribute it and/or modify |
wolfSSL | 11:cee25a834751 | 8 | * it under the terms of the GNU General Public License as published by |
wolfSSL | 11:cee25a834751 | 9 | * the Free Software Foundation; either version 2 of the License, or |
wolfSSL | 11:cee25a834751 | 10 | * (at your option) any later version. |
wolfSSL | 11:cee25a834751 | 11 | * |
wolfSSL | 11:cee25a834751 | 12 | * wolfSSL is distributed in the hope that it will be useful, |
wolfSSL | 11:cee25a834751 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
wolfSSL | 11:cee25a834751 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
wolfSSL | 11:cee25a834751 | 15 | * GNU General Public License for more details. |
wolfSSL | 11:cee25a834751 | 16 | * |
wolfSSL | 11:cee25a834751 | 17 | * You should have received a copy of the GNU General Public License |
wolfSSL | 11:cee25a834751 | 18 | * along with this program; if not, write to the Free Software |
wolfSSL | 11:cee25a834751 | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
wolfSSL | 11:cee25a834751 | 20 | */ |
wolfSSL | 11:cee25a834751 | 21 | |
wolfSSL | 11:cee25a834751 | 22 | |
wolfSSL | 11:cee25a834751 | 23 | #ifdef HAVE_CONFIG_H |
wolfSSL | 11:cee25a834751 | 24 | #include <config.h> |
wolfSSL | 11:cee25a834751 | 25 | #endif |
wolfSSL | 11:cee25a834751 | 26 | |
wolfSSL | 11:cee25a834751 | 27 | #include <wolfssl/wolfcrypt/settings.h> |
wolfSSL | 11:cee25a834751 | 28 | |
wolfSSL | 11:cee25a834751 | 29 | #ifndef WOLF_CRYPT_MISC_C |
wolfSSL | 11:cee25a834751 | 30 | #define WOLF_CRYPT_MISC_C |
wolfSSL | 11:cee25a834751 | 31 | |
wolfSSL | 11:cee25a834751 | 32 | #include <wolfssl/wolfcrypt/misc.h> |
wolfSSL | 11:cee25a834751 | 33 | |
wolfSSL | 11:cee25a834751 | 34 | /* inlining these functions is a huge speed increase and a small size decrease, |
wolfSSL | 11:cee25a834751 | 35 | because the functions are smaller than function call setup/cleanup, e.g., |
wolfSSL | 11:cee25a834751 | 36 | md5 benchmark is twice as fast with inline. If you don't want it, then |
wolfSSL | 11:cee25a834751 | 37 | define NO_INLINE and compile this file into wolfssl, otherwise it's used as |
wolfSSL | 11:cee25a834751 | 38 | a source header |
wolfSSL | 11:cee25a834751 | 39 | */ |
wolfSSL | 11:cee25a834751 | 40 | |
wolfSSL | 11:cee25a834751 | 41 | #ifdef NO_INLINE |
wolfSSL | 11:cee25a834751 | 42 | #define STATIC |
wolfSSL | 11:cee25a834751 | 43 | #else |
wolfSSL | 11:cee25a834751 | 44 | #define STATIC static |
wolfSSL | 11:cee25a834751 | 45 | #endif |
wolfSSL | 11:cee25a834751 | 46 | |
wolfSSL | 11:cee25a834751 | 47 | /* Check for if compiling misc.c when not needed. */ |
wolfSSL | 11:cee25a834751 | 48 | #if !defined(WOLFSSL_MISC_INCLUDED) && !defined(NO_INLINE) |
wolfSSL | 11:cee25a834751 | 49 | #warning misc.c does not need to be compiled when using inline (NO_INLINE not defined) |
wolfSSL | 11:cee25a834751 | 50 | |
wolfSSL | 11:cee25a834751 | 51 | #else |
wolfSSL | 11:cee25a834751 | 52 | |
wolfSSL | 11:cee25a834751 | 53 | |
wolfSSL | 11:cee25a834751 | 54 | #if defined(__ICCARM__) |
wolfSSL | 11:cee25a834751 | 55 | #include <intrinsics.h> |
wolfSSL | 11:cee25a834751 | 56 | #endif |
wolfSSL | 11:cee25a834751 | 57 | |
wolfSSL | 11:cee25a834751 | 58 | |
wolfSSL | 11:cee25a834751 | 59 | #ifdef INTEL_INTRINSICS |
wolfSSL | 11:cee25a834751 | 60 | |
wolfSSL | 11:cee25a834751 | 61 | #include <stdlib.h> /* get intrinsic definitions */ |
wolfSSL | 11:cee25a834751 | 62 | |
wolfSSL | 11:cee25a834751 | 63 | /* for non visual studio probably need no long version, 32 bit only |
wolfSSL | 11:cee25a834751 | 64 | * i.e., _rotl and _rotr */ |
wolfSSL | 11:cee25a834751 | 65 | #pragma intrinsic(_lrotl, _lrotr) |
wolfSSL | 11:cee25a834751 | 66 | |
wolfSSL | 11:cee25a834751 | 67 | STATIC INLINE word32 rotlFixed(word32 x, word32 y) |
wolfSSL | 11:cee25a834751 | 68 | { |
wolfSSL | 11:cee25a834751 | 69 | return y ? _lrotl(x, y) : x; |
wolfSSL | 11:cee25a834751 | 70 | } |
wolfSSL | 11:cee25a834751 | 71 | |
wolfSSL | 11:cee25a834751 | 72 | STATIC INLINE word32 rotrFixed(word32 x, word32 y) |
wolfSSL | 11:cee25a834751 | 73 | { |
wolfSSL | 11:cee25a834751 | 74 | return y ? _lrotr(x, y) : x; |
wolfSSL | 11:cee25a834751 | 75 | } |
wolfSSL | 11:cee25a834751 | 76 | |
wolfSSL | 11:cee25a834751 | 77 | #else /* generic */ |
wolfSSL | 11:cee25a834751 | 78 | |
wolfSSL | 11:cee25a834751 | 79 | STATIC INLINE word32 rotlFixed(word32 x, word32 y) |
wolfSSL | 11:cee25a834751 | 80 | { |
wolfSSL | 11:cee25a834751 | 81 | return (x << y) | (x >> (sizeof(y) * 8 - y)); |
wolfSSL | 11:cee25a834751 | 82 | } |
wolfSSL | 11:cee25a834751 | 83 | |
wolfSSL | 11:cee25a834751 | 84 | |
wolfSSL | 11:cee25a834751 | 85 | STATIC INLINE word32 rotrFixed(word32 x, word32 y) |
wolfSSL | 11:cee25a834751 | 86 | { |
wolfSSL | 11:cee25a834751 | 87 | return (x >> y) | (x << (sizeof(y) * 8 - y)); |
wolfSSL | 11:cee25a834751 | 88 | } |
wolfSSL | 11:cee25a834751 | 89 | |
wolfSSL | 11:cee25a834751 | 90 | #endif |
wolfSSL | 11:cee25a834751 | 91 | |
wolfSSL | 11:cee25a834751 | 92 | |
wolfSSL | 11:cee25a834751 | 93 | STATIC INLINE word32 ByteReverseWord32(word32 value) |
wolfSSL | 11:cee25a834751 | 94 | { |
wolfSSL | 11:cee25a834751 | 95 | #ifdef PPC_INTRINSICS |
wolfSSL | 11:cee25a834751 | 96 | /* PPC: load reverse indexed instruction */ |
wolfSSL | 11:cee25a834751 | 97 | return (word32)__lwbrx(&value,0); |
wolfSSL | 11:cee25a834751 | 98 | #elif defined(__ICCARM__) |
wolfSSL | 11:cee25a834751 | 99 | return (word32)__REV(value); |
wolfSSL | 11:cee25a834751 | 100 | #elif defined(KEIL_INTRINSICS) |
wolfSSL | 11:cee25a834751 | 101 | return (word32)__rev(value); |
wolfSSL | 11:cee25a834751 | 102 | #elif defined(FAST_ROTATE) |
wolfSSL | 11:cee25a834751 | 103 | /* 5 instructions with rotate instruction, 9 without */ |
wolfSSL | 11:cee25a834751 | 104 | return (rotrFixed(value, 8U) & 0xff00ff00) | |
wolfSSL | 11:cee25a834751 | 105 | (rotlFixed(value, 8U) & 0x00ff00ff); |
wolfSSL | 11:cee25a834751 | 106 | #else |
wolfSSL | 11:cee25a834751 | 107 | /* 6 instructions with rotate instruction, 8 without */ |
wolfSSL | 11:cee25a834751 | 108 | value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8); |
wolfSSL | 11:cee25a834751 | 109 | return rotlFixed(value, 16U); |
wolfSSL | 11:cee25a834751 | 110 | #endif |
wolfSSL | 11:cee25a834751 | 111 | } |
wolfSSL | 11:cee25a834751 | 112 | |
wolfSSL | 11:cee25a834751 | 113 | |
wolfSSL | 11:cee25a834751 | 114 | STATIC INLINE void ByteReverseWords(word32* out, const word32* in, |
wolfSSL | 11:cee25a834751 | 115 | word32 byteCount) |
wolfSSL | 11:cee25a834751 | 116 | { |
wolfSSL | 11:cee25a834751 | 117 | word32 count = byteCount/(word32)sizeof(word32), i; |
wolfSSL | 11:cee25a834751 | 118 | |
wolfSSL | 11:cee25a834751 | 119 | for (i = 0; i < count; i++) |
wolfSSL | 11:cee25a834751 | 120 | out[i] = ByteReverseWord32(in[i]); |
wolfSSL | 11:cee25a834751 | 121 | |
wolfSSL | 11:cee25a834751 | 122 | } |
wolfSSL | 11:cee25a834751 | 123 | |
wolfSSL | 11:cee25a834751 | 124 | |
wolfSSL | 11:cee25a834751 | 125 | #ifdef WORD64_AVAILABLE |
wolfSSL | 11:cee25a834751 | 126 | |
wolfSSL | 11:cee25a834751 | 127 | |
wolfSSL | 11:cee25a834751 | 128 | STATIC INLINE word64 rotlFixed64(word64 x, word64 y) |
wolfSSL | 11:cee25a834751 | 129 | { |
wolfSSL | 11:cee25a834751 | 130 | return (x << y) | (x >> (sizeof(y) * 8 - y)); |
wolfSSL | 11:cee25a834751 | 131 | } |
wolfSSL | 11:cee25a834751 | 132 | |
wolfSSL | 11:cee25a834751 | 133 | |
wolfSSL | 11:cee25a834751 | 134 | STATIC INLINE word64 rotrFixed64(word64 x, word64 y) |
wolfSSL | 11:cee25a834751 | 135 | { |
wolfSSL | 11:cee25a834751 | 136 | return (x >> y) | (x << (sizeof(y) * 8 - y)); |
wolfSSL | 11:cee25a834751 | 137 | } |
wolfSSL | 11:cee25a834751 | 138 | |
wolfSSL | 11:cee25a834751 | 139 | |
wolfSSL | 11:cee25a834751 | 140 | STATIC INLINE word64 ByteReverseWord64(word64 value) |
wolfSSL | 11:cee25a834751 | 141 | { |
wolfSSL | 11:cee25a834751 | 142 | #if defined(WOLFCRYPT_SLOW_WORD64) |
wolfSSL | 11:cee25a834751 | 143 | return (word64)(ByteReverseWord32((word32)value)) << 32 | |
wolfSSL | 11:cee25a834751 | 144 | ByteReverseWord32((word32)(value>>32)); |
wolfSSL | 11:cee25a834751 | 145 | #else |
wolfSSL | 11:cee25a834751 | 146 | value = ((value & W64LIT(0xFF00FF00FF00FF00)) >> 8) | |
wolfSSL | 11:cee25a834751 | 147 | ((value & W64LIT(0x00FF00FF00FF00FF)) << 8); |
wolfSSL | 11:cee25a834751 | 148 | value = ((value & W64LIT(0xFFFF0000FFFF0000)) >> 16) | |
wolfSSL | 11:cee25a834751 | 149 | ((value & W64LIT(0x0000FFFF0000FFFF)) << 16); |
wolfSSL | 11:cee25a834751 | 150 | return rotlFixed64(value, 32U); |
wolfSSL | 11:cee25a834751 | 151 | #endif |
wolfSSL | 11:cee25a834751 | 152 | } |
wolfSSL | 11:cee25a834751 | 153 | |
wolfSSL | 11:cee25a834751 | 154 | |
wolfSSL | 11:cee25a834751 | 155 | STATIC INLINE void ByteReverseWords64(word64* out, const word64* in, |
wolfSSL | 11:cee25a834751 | 156 | word32 byteCount) |
wolfSSL | 11:cee25a834751 | 157 | { |
wolfSSL | 11:cee25a834751 | 158 | word32 count = byteCount/(word32)sizeof(word64), i; |
wolfSSL | 11:cee25a834751 | 159 | |
wolfSSL | 11:cee25a834751 | 160 | for (i = 0; i < count; i++) |
wolfSSL | 11:cee25a834751 | 161 | out[i] = ByteReverseWord64(in[i]); |
wolfSSL | 11:cee25a834751 | 162 | |
wolfSSL | 11:cee25a834751 | 163 | } |
wolfSSL | 11:cee25a834751 | 164 | |
wolfSSL | 11:cee25a834751 | 165 | #endif /* WORD64_AVAILABLE */ |
wolfSSL | 11:cee25a834751 | 166 | |
wolfSSL | 11:cee25a834751 | 167 | |
wolfSSL | 11:cee25a834751 | 168 | STATIC INLINE void XorWords(wolfssl_word* r, const wolfssl_word* a, word32 n) |
wolfSSL | 11:cee25a834751 | 169 | { |
wolfSSL | 11:cee25a834751 | 170 | word32 i; |
wolfSSL | 11:cee25a834751 | 171 | |
wolfSSL | 11:cee25a834751 | 172 | for (i = 0; i < n; i++) r[i] ^= a[i]; |
wolfSSL | 11:cee25a834751 | 173 | } |
wolfSSL | 11:cee25a834751 | 174 | |
wolfSSL | 11:cee25a834751 | 175 | |
wolfSSL | 11:cee25a834751 | 176 | STATIC INLINE void xorbuf(void* buf, const void* mask, word32 count) |
wolfSSL | 11:cee25a834751 | 177 | { |
wolfSSL | 11:cee25a834751 | 178 | if (((wolfssl_word)buf | (wolfssl_word)mask | count) % WOLFSSL_WORD_SIZE == 0) |
wolfSSL | 11:cee25a834751 | 179 | XorWords( (wolfssl_word*)buf, |
wolfSSL | 11:cee25a834751 | 180 | (const wolfssl_word*)mask, count / WOLFSSL_WORD_SIZE); |
wolfSSL | 11:cee25a834751 | 181 | else { |
wolfSSL | 11:cee25a834751 | 182 | word32 i; |
wolfSSL | 11:cee25a834751 | 183 | byte* b = (byte*)buf; |
wolfSSL | 11:cee25a834751 | 184 | const byte* m = (const byte*)mask; |
wolfSSL | 11:cee25a834751 | 185 | |
wolfSSL | 11:cee25a834751 | 186 | for (i = 0; i < count; i++) b[i] ^= m[i]; |
wolfSSL | 11:cee25a834751 | 187 | } |
wolfSSL | 11:cee25a834751 | 188 | } |
wolfSSL | 11:cee25a834751 | 189 | |
wolfSSL | 11:cee25a834751 | 190 | |
wolfSSL | 11:cee25a834751 | 191 | /* Make sure compiler doesn't skip */ |
wolfSSL | 11:cee25a834751 | 192 | STATIC INLINE void ForceZero(const void* mem, word32 len) |
wolfSSL | 11:cee25a834751 | 193 | { |
wolfSSL | 11:cee25a834751 | 194 | volatile byte* z = (volatile byte*)mem; |
wolfSSL | 11:cee25a834751 | 195 | #ifdef WOLFSSL_X86_64_BUILD |
wolfSSL | 11:cee25a834751 | 196 | volatile word64* w; |
wolfSSL | 11:cee25a834751 | 197 | |
wolfSSL | 11:cee25a834751 | 198 | for (w = (volatile word64*)z; len >= sizeof(*w); len -= sizeof(*w)) |
wolfSSL | 11:cee25a834751 | 199 | *w++ = 0; |
wolfSSL | 11:cee25a834751 | 200 | z = (volatile byte*)w; |
wolfSSL | 11:cee25a834751 | 201 | #endif |
wolfSSL | 11:cee25a834751 | 202 | while (len--) *z++ = 0; |
wolfSSL | 11:cee25a834751 | 203 | } |
wolfSSL | 11:cee25a834751 | 204 | |
wolfSSL | 11:cee25a834751 | 205 | |
wolfSSL | 11:cee25a834751 | 206 | /* check all length bytes for equality, return 0 on success */ |
wolfSSL | 11:cee25a834751 | 207 | STATIC INLINE int ConstantCompare(const byte* a, const byte* b, int length) |
wolfSSL | 11:cee25a834751 | 208 | { |
wolfSSL | 11:cee25a834751 | 209 | int i; |
wolfSSL | 11:cee25a834751 | 210 | int compareSum = 0; |
wolfSSL | 11:cee25a834751 | 211 | |
wolfSSL | 11:cee25a834751 | 212 | for (i = 0; i < length; i++) { |
wolfSSL | 11:cee25a834751 | 213 | compareSum |= a[i] ^ b[i]; |
wolfSSL | 11:cee25a834751 | 214 | } |
wolfSSL | 11:cee25a834751 | 215 | |
wolfSSL | 11:cee25a834751 | 216 | return compareSum; |
wolfSSL | 11:cee25a834751 | 217 | } |
wolfSSL | 11:cee25a834751 | 218 | |
wolfSSL | 11:cee25a834751 | 219 | |
wolfSSL | 11:cee25a834751 | 220 | #ifndef WOLFSSL_HAVE_MIN |
wolfSSL | 11:cee25a834751 | 221 | #define WOLFSSL_HAVE_MIN |
wolfSSL | 11:cee25a834751 | 222 | #if defined(HAVE_FIPS) && !defined(min) /* so ifdef check passes */ |
wolfSSL | 11:cee25a834751 | 223 | #define min min |
wolfSSL | 11:cee25a834751 | 224 | #endif |
wolfSSL | 11:cee25a834751 | 225 | STATIC INLINE word32 min(word32 a, word32 b) |
wolfSSL | 11:cee25a834751 | 226 | { |
wolfSSL | 11:cee25a834751 | 227 | return a > b ? b : a; |
wolfSSL | 11:cee25a834751 | 228 | } |
wolfSSL | 11:cee25a834751 | 229 | #endif /* !WOLFSSL_HAVE_MIN */ |
wolfSSL | 11:cee25a834751 | 230 | |
wolfSSL | 11:cee25a834751 | 231 | #ifndef WOLFSSL_HAVE_MAX |
wolfSSL | 11:cee25a834751 | 232 | #define WOLFSSL_HAVE_MAX |
wolfSSL | 11:cee25a834751 | 233 | #if defined(HAVE_FIPS) && !defined(max) /* so ifdef check passes */ |
wolfSSL | 11:cee25a834751 | 234 | #define max max |
wolfSSL | 11:cee25a834751 | 235 | #endif |
wolfSSL | 11:cee25a834751 | 236 | STATIC INLINE word32 max(word32 a, word32 b) |
wolfSSL | 11:cee25a834751 | 237 | { |
wolfSSL | 11:cee25a834751 | 238 | return a > b ? a : b; |
wolfSSL | 11:cee25a834751 | 239 | } |
wolfSSL | 11:cee25a834751 | 240 | #endif /* !WOLFSSL_HAVE_MAX */ |
wolfSSL | 11:cee25a834751 | 241 | |
wolfSSL | 11:cee25a834751 | 242 | |
wolfSSL | 11:cee25a834751 | 243 | #undef STATIC |
wolfSSL | 11:cee25a834751 | 244 | |
wolfSSL | 11:cee25a834751 | 245 | #endif /* !WOLFSSL_MISC_INCLUDED && !NO_INLINE */ |
wolfSSL | 11:cee25a834751 | 246 | |
wolfSSL | 11:cee25a834751 | 247 | #endif /* WOLF_CRYPT_MISC_C */ |
wolfSSL | 11:cee25a834751 | 248 |