misc.c 14 KB

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