misc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  1. /* misc.c
  2. *
  3. * Copyright (C) 2006-2023 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. /* Check for if compiling misc.c when not needed. */
  40. #if !defined(WOLFSSL_MISC_INCLUDED) && !defined(NO_INLINE)
  41. #ifndef WOLFSSL_IGNORE_FILE_WARN
  42. #warning misc.c does not need to be compiled when using inline (NO_INLINE not defined)
  43. #endif
  44. #else
  45. #if defined(__ICCARM__)
  46. #include <intrinsics.h>
  47. #endif
  48. #ifdef INTEL_INTRINSICS
  49. #include <stdlib.h> /* get intrinsic definitions */
  50. /* for non visual studio probably need no long version, 32 bit only
  51. * i.e., _rotl and _rotr */
  52. #pragma intrinsic(_lrotl, _lrotr)
  53. WC_MISC_STATIC WC_INLINE word32 rotlFixed(word32 x, word32 y)
  54. {
  55. return y ? _lrotl(x, y) : x;
  56. }
  57. WC_MISC_STATIC WC_INLINE word32 rotrFixed(word32 x, word32 y)
  58. {
  59. return y ? _lrotr(x, y) : x;
  60. }
  61. #elif defined(__CCRX__)
  62. #include <builtin.h> /* get intrinsic definitions */
  63. #if !defined(NO_INLINE)
  64. #define rotlFixed(x, y) _builtin_rotl(x, y)
  65. #define rotrFixed(x, y) _builtin_rotr(x, y)
  66. #else /* create real function */
  67. WC_MISC_STATIC WC_INLINE word32 rotlFixed(word32 x, word32 y)
  68. {
  69. return _builtin_rotl(x, y);
  70. }
  71. WC_MISC_STATIC WC_INLINE word32 rotrFixed(word32 x, word32 y)
  72. {
  73. return _builtin_rotr(x, y);
  74. }
  75. #endif
  76. #else /* generic */
  77. /* This routine performs a left circular arithmetic shift of <x> by <y> value. */
  78. WC_MISC_STATIC WC_INLINE word32 rotlFixed(word32 x, word32 y)
  79. {
  80. return (x << y) | (x >> (sizeof(x) * 8 - y));
  81. }
  82. /* This routine performs a right circular arithmetic shift of <x> by <y> value. */
  83. WC_MISC_STATIC WC_INLINE word32 rotrFixed(word32 x, word32 y)
  84. {
  85. return (x >> y) | (x << (sizeof(x) * 8 - y));
  86. }
  87. #endif
  88. #ifdef WC_RC2
  89. /* This routine performs a left circular arithmetic shift of <x> by <y> value */
  90. WC_MISC_STATIC WC_INLINE word16 rotlFixed16(word16 x, word16 y)
  91. {
  92. return (x << y) | (x >> (sizeof(x) * 8 - y));
  93. }
  94. /* This routine performs a right circular arithmetic shift of <x> by <y> value */
  95. WC_MISC_STATIC WC_INLINE word16 rotrFixed16(word16 x, word16 y)
  96. {
  97. return (x >> y) | (x << (sizeof(x) * 8 - y));
  98. }
  99. #endif /* WC_RC2 */
  100. /* This routine performs a byte swap of 32-bit word value. */
  101. #if defined(__CCRX__) && !defined(NO_INLINE) /* shortest version for CC-RX */
  102. #define ByteReverseWord32(value) _builtin_revl(value)
  103. #else
  104. WC_MISC_STATIC WC_INLINE word32 ByteReverseWord32(word32 value)
  105. {
  106. #ifdef PPC_INTRINSICS
  107. /* PPC: load reverse indexed instruction */
  108. return (word32)__lwbrx(&value,0);
  109. #elif defined(__ICCARM__)
  110. return (word32)__REV(value);
  111. #elif defined(KEIL_INTRINSICS)
  112. return (word32)__rev(value);
  113. #elif defined(__CCRX__)
  114. return (word32)_builtin_revl(value);
  115. #elif defined(WOLF_ALLOW_BUILTIN) && \
  116. defined(__GNUC_PREREQ) && __GNUC_PREREQ(4, 3)
  117. return (word32)__builtin_bswap32(value);
  118. #elif defined(WOLFSSL_BYTESWAP32_ASM) && defined(__GNUC__) && \
  119. defined(__aarch64__)
  120. __asm__ volatile (
  121. "REV32 %0, %0 \n"
  122. : "+r" (value)
  123. :
  124. );
  125. return value;
  126. #elif defined(WOLFSSL_BYTESWAP32_ASM) && defined(__GNUC__) && \
  127. (defined(__thumb__) || defined(__arm__))
  128. __asm__ volatile (
  129. "REV %0, %0 \n"
  130. : "+r" (value)
  131. :
  132. );
  133. return value;
  134. #elif defined(FAST_ROTATE)
  135. /* 5 instructions with rotate instruction, 9 without */
  136. return (rotrFixed(value, 8U) & 0xff00ff00) |
  137. (rotlFixed(value, 8U) & 0x00ff00ff);
  138. #else
  139. /* 6 instructions with rotate instruction, 8 without */
  140. value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
  141. return rotlFixed(value, 16U);
  142. #endif
  143. }
  144. #endif /* __CCRX__ */
  145. /* This routine performs a byte swap of words array of a given count. */
  146. WC_MISC_STATIC WC_INLINE void ByteReverseWords(word32* out, const word32* in,
  147. word32 byteCount)
  148. {
  149. word32 i;
  150. #ifdef WOLFSSL_USE_ALIGN
  151. if ((((size_t)in & 0x3) == 0) &&
  152. (((size_t)out & 0x3) == 0))
  153. #endif
  154. {
  155. word32 count = byteCount/(word32)sizeof(word32);
  156. for (i = 0; i < count; i++)
  157. out[i] = ByteReverseWord32(in[i]);
  158. }
  159. #ifdef WOLFSSL_USE_ALIGN
  160. else {
  161. byte *in_bytes = (byte *)in;
  162. byte *out_bytes = (byte *)out;
  163. word32 scratch;
  164. byteCount &= ~0x3U;
  165. for (i = 0; i < byteCount; i += sizeof(word32)) {
  166. XMEMCPY(&scratch, in_bytes + i, sizeof(scratch));
  167. scratch = ByteReverseWord32(scratch);
  168. XMEMCPY(out_bytes + i, &scratch, sizeof(scratch));
  169. }
  170. }
  171. #endif
  172. }
  173. #if defined(WORD64_AVAILABLE) && !defined(WOLFSSL_NO_WORD64_OPS)
  174. WC_MISC_STATIC WC_INLINE word64 rotlFixed64(word64 x, word64 y)
  175. {
  176. return (x << y) | (x >> (sizeof(y) * 8 - y));
  177. }
  178. WC_MISC_STATIC WC_INLINE word64 rotrFixed64(word64 x, word64 y)
  179. {
  180. return (x >> y) | (x << (sizeof(y) * 8 - y));
  181. }
  182. WC_MISC_STATIC WC_INLINE word64 ByteReverseWord64(word64 value)
  183. {
  184. #if defined(WOLF_ALLOW_BUILTIN) && defined(__GNUC_PREREQ) && __GNUC_PREREQ(4, 3)
  185. return (word64)__builtin_bswap64(value);
  186. #elif defined(WOLFCRYPT_SLOW_WORD64)
  187. return (word64)((word64)ByteReverseWord32((word32) value)) << 32 |
  188. (word64)ByteReverseWord32((word32)(value >> 32));
  189. #else
  190. value = ((value & W64LIT(0xFF00FF00FF00FF00)) >> 8) |
  191. ((value & W64LIT(0x00FF00FF00FF00FF)) << 8);
  192. value = ((value & W64LIT(0xFFFF0000FFFF0000)) >> 16) |
  193. ((value & W64LIT(0x0000FFFF0000FFFF)) << 16);
  194. return rotlFixed64(value, 32U);
  195. #endif
  196. }
  197. WC_MISC_STATIC WC_INLINE void ByteReverseWords64(word64* out, const word64* in,
  198. word32 byteCount)
  199. {
  200. word32 count = byteCount/(word32)sizeof(word64), i;
  201. for (i = 0; i < count; i++)
  202. out[i] = ByteReverseWord64(in[i]);
  203. }
  204. #endif /* WORD64_AVAILABLE && !WOLFSSL_NO_WORD64_OPS */
  205. #ifndef WOLFSSL_NO_XOR_OPS
  206. /* This routine performs a bitwise XOR operation of <*r> and <*a> for <n> number
  207. of wolfssl_words, placing the result in <*r>. */
  208. WC_MISC_STATIC WC_INLINE void XorWordsOut(wolfssl_word** r,
  209. const wolfssl_word** a, const wolfssl_word** b, word32 n)
  210. {
  211. word32 i;
  212. for (i = 0; i < n; i++)
  213. *((*r)++) = *((*a)++) ^ *((*b)++);
  214. }
  215. /* This routine performs a bitwise XOR operation of <*buf> and <*mask> of n
  216. counts, placing the result in <*buf>. */
  217. WC_MISC_STATIC WC_INLINE void xorbufout(void* out, const void* buf,
  218. const void* mask, word32 count)
  219. {
  220. word32 i;
  221. byte* o;
  222. const byte* b;
  223. const byte* m;
  224. o = (byte*)out;
  225. b = (const byte*)buf;
  226. m = (const byte*)mask;
  227. if (((wc_ptr_t)o) % WOLFSSL_WORD_SIZE ==
  228. ((wc_ptr_t)b) % WOLFSSL_WORD_SIZE &&
  229. ((wc_ptr_t)b) % WOLFSSL_WORD_SIZE ==
  230. ((wc_ptr_t)m) % WOLFSSL_WORD_SIZE) {
  231. /* type-punning helpers */
  232. union {
  233. byte* bp;
  234. wolfssl_word* wp;
  235. } tpo;
  236. union {
  237. const byte* bp;
  238. const wolfssl_word* wp;
  239. } tpb, tpm;
  240. /* Alignment checks out. Possible to XOR words. */
  241. /* Move alignment so that it lines up with a
  242. * WOLFSSL_WORD_SIZE boundary */
  243. while (((wc_ptr_t)b) % WOLFSSL_WORD_SIZE != 0 && count > 0) {
  244. *(o++) = (byte)(*(b++) ^ *(m++));
  245. count--;
  246. }
  247. tpo.bp = o;
  248. tpb.bp = b;
  249. tpm.bp = m;
  250. XorWordsOut( &tpo.wp, &tpb.wp, &tpm.wp, count / WOLFSSL_WORD_SIZE);
  251. o = tpo.bp;
  252. b = tpb.bp;
  253. m = tpm.bp;
  254. count %= WOLFSSL_WORD_SIZE;
  255. }
  256. for (i = 0; i < count; i++)
  257. o[i] = (byte)(b[i] ^ m[i]);
  258. }
  259. /* This routine performs a bitwise XOR operation of <*r> and <*a> for <n> number
  260. of wolfssl_words, placing the result in <*r>. */
  261. WC_MISC_STATIC WC_INLINE void XorWords(wolfssl_word** r, const wolfssl_word** a,
  262. word32 n)
  263. {
  264. word32 i;
  265. for (i = 0; i < n; i++)
  266. *((*r)++) ^= *((*a)++);
  267. }
  268. /* This routine performs a bitwise XOR operation of <*buf> and <*mask> of n
  269. counts, placing the result in <*buf>. */
  270. WC_MISC_STATIC WC_INLINE void xorbuf(void* buf, const void* mask, word32 count)
  271. {
  272. word32 i;
  273. byte* b;
  274. const byte* m;
  275. b = (byte*)buf;
  276. m = (const byte*)mask;
  277. if (((wc_ptr_t)b) % WOLFSSL_WORD_SIZE ==
  278. ((wc_ptr_t)m) % WOLFSSL_WORD_SIZE) {
  279. /* type-punning helpers */
  280. union {
  281. byte* bp;
  282. wolfssl_word* wp;
  283. } tpb;
  284. union {
  285. const byte* bp;
  286. const wolfssl_word* wp;
  287. } tpm;
  288. /* Alignment checks out. Possible to XOR words. */
  289. /* Move alignment so that it lines up with a
  290. * WOLFSSL_WORD_SIZE boundary */
  291. while (((wc_ptr_t)buf) % WOLFSSL_WORD_SIZE != 0 && count > 0) {
  292. *(b++) ^= *(m++);
  293. count--;
  294. }
  295. tpb.bp = b;
  296. tpm.bp = m;
  297. XorWords( &tpb.wp, &tpm.wp, count / WOLFSSL_WORD_SIZE);
  298. b = tpb.bp;
  299. m = tpm.bp;
  300. count %= WOLFSSL_WORD_SIZE;
  301. }
  302. for (i = 0; i < count; i++)
  303. b[i] ^= m[i];
  304. }
  305. #endif
  306. #ifndef WOLFSSL_NO_FORCE_ZERO
  307. /* This routine fills the first len bytes of the memory area pointed by mem
  308. with zeros. It ensures compiler optimizations doesn't skip it */
  309. WC_MISC_STATIC WC_INLINE void ForceZero(void* mem, word32 len)
  310. {
  311. volatile byte* z = (volatile byte*)mem;
  312. #if (defined(WOLFSSL_X86_64_BUILD) || defined(WOLFSSL_AARCH64_BUILD)) \
  313. && defined(WORD64_AVAILABLE)
  314. volatile word64* w;
  315. #ifndef WOLFSSL_UNALIGNED_64BIT_ACCESS
  316. word32 l = (sizeof(word64) - ((size_t)z & (sizeof(word64)-1))) &
  317. (sizeof(word64)-1);
  318. if (len < l) l = len;
  319. len -= l;
  320. while (l--) *z++ = 0;
  321. #endif
  322. for (w = (volatile word64*)z;
  323. len >= sizeof(*w);
  324. len -= (word32)sizeof(*w))
  325. {
  326. *w++ = 0;
  327. }
  328. z = (volatile byte*)w;
  329. #endif
  330. while (len--) *z++ = 0;
  331. }
  332. #endif
  333. #ifndef WOLFSSL_NO_CONST_CMP
  334. /* check all length bytes for equality, return 0 on success */
  335. WC_MISC_STATIC WC_INLINE int ConstantCompare(const byte* a, const byte* b,
  336. int length)
  337. {
  338. int i;
  339. int compareSum = 0;
  340. for (i = 0; i < length; i++) {
  341. compareSum |= a[i] ^ b[i];
  342. }
  343. return compareSum;
  344. }
  345. #endif
  346. #ifndef WOLFSSL_HAVE_MIN
  347. #define WOLFSSL_HAVE_MIN
  348. #if defined(HAVE_FIPS) && !defined(min) /* so ifdef check passes */
  349. #define min min
  350. #endif
  351. /* returns the smaller of a and b */
  352. WC_MISC_STATIC WC_INLINE word32 min(word32 a, word32 b)
  353. {
  354. return a > b ? b : a;
  355. }
  356. #endif /* !WOLFSSL_HAVE_MIN */
  357. #ifndef WOLFSSL_HAVE_MAX
  358. #define WOLFSSL_HAVE_MAX
  359. #if defined(HAVE_FIPS) && !defined(max) /* so ifdef check passes */
  360. #define max max
  361. #endif
  362. WC_MISC_STATIC WC_INLINE word32 max(word32 a, word32 b)
  363. {
  364. return a > b ? a : b;
  365. }
  366. #endif /* !WOLFSSL_HAVE_MAX */
  367. #ifndef WOLFSSL_NO_INT_ENCODE
  368. /* converts a 32 bit integer to 24 bit */
  369. WC_MISC_STATIC WC_INLINE void c32to24(word32 in, word24 out)
  370. {
  371. out[0] = (byte)((in >> 16) & 0xff);
  372. out[1] = (byte)((in >> 8) & 0xff);
  373. out[2] = (byte)(in & 0xff);
  374. }
  375. /* convert 16 bit integer to opaque */
  376. WC_MISC_STATIC WC_INLINE void c16toa(word16 wc_u16, byte* c)
  377. {
  378. c[0] = (byte)((wc_u16 >> 8) & 0xff);
  379. c[1] = (byte)(wc_u16 & 0xff);
  380. }
  381. /* convert 32 bit integer to opaque */
  382. WC_MISC_STATIC WC_INLINE void c32toa(word32 wc_u32, byte* c)
  383. {
  384. c[0] = (byte)((wc_u32 >> 24) & 0xff);
  385. c[1] = (byte)((wc_u32 >> 16) & 0xff);
  386. c[2] = (byte)((wc_u32 >> 8) & 0xff);
  387. c[3] = (byte)(wc_u32 & 0xff);
  388. }
  389. #endif
  390. #ifndef WOLFSSL_NO_INT_DECODE
  391. /* convert a 24 bit integer into a 32 bit one */
  392. WC_MISC_STATIC WC_INLINE void c24to32(const word24 wc_u24, word32* wc_u32)
  393. {
  394. *wc_u32 = ((word32)wc_u24[0] << 16) |
  395. ((word32)wc_u24[1] << 8) |
  396. (word32)wc_u24[2];
  397. }
  398. /* convert opaque to 24 bit integer */
  399. WC_MISC_STATIC WC_INLINE void ato24(const byte* c, word32* wc_u24)
  400. {
  401. *wc_u24 = ((word32)c[0] << 16) | ((word32)c[1] << 8) | c[2];
  402. }
  403. /* convert opaque to 16 bit integer */
  404. WC_MISC_STATIC WC_INLINE void ato16(const byte* c, word16* wc_u16)
  405. {
  406. *wc_u16 = (word16) ((c[0] << 8) | (c[1]));
  407. }
  408. /* convert opaque to 32 bit integer */
  409. WC_MISC_STATIC WC_INLINE void ato32(const byte* c, word32* wc_u32)
  410. {
  411. *wc_u32 = ((word32)c[0] << 24) |
  412. ((word32)c[1] << 16) |
  413. ((word32)c[2] << 8) |
  414. (word32)c[3];
  415. }
  416. /* convert opaque to 32 bit integer. Interpret as little endian. */
  417. WC_MISC_STATIC WC_INLINE void ato32le(const byte* c, word32* wc_u32)
  418. {
  419. *wc_u32 = (word32)c[0] |
  420. ((word32)c[1] << 8) |
  421. ((word32)c[2] << 16) |
  422. ((word32)c[3] << 24);
  423. }
  424. WC_MISC_STATIC WC_INLINE word32 btoi(byte b)
  425. {
  426. return (word32)(b - 0x30);
  427. }
  428. #endif
  429. WC_MISC_STATIC WC_INLINE signed char HexCharToByte(char ch)
  430. {
  431. signed char ret = (signed char)ch;
  432. if (ret >= '0' && ret <= '9')
  433. ret -= '0';
  434. else if (ret >= 'A' && ret <= 'F')
  435. ret -= 'A' - 10;
  436. else if (ret >= 'a' && ret <= 'f')
  437. ret -= 'a' - 10;
  438. else
  439. ret = -1; /* error case - return code must be signed */
  440. return ret;
  441. }
  442. WC_MISC_STATIC WC_INLINE char ByteToHex(byte in)
  443. {
  444. static const char kHexChar[] = { '0', '1', '2', '3', '4', '5', '6', '7',
  445. '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
  446. return (char)(kHexChar[in & 0xF]);
  447. }
  448. WC_MISC_STATIC WC_INLINE int ByteToHexStr(byte in, char* out)
  449. {
  450. if (out == NULL)
  451. return -1;
  452. out[0] = ByteToHex((byte)(in >> 4));
  453. out[1] = ByteToHex((byte)(in & 0xf));
  454. return 0;
  455. }
  456. #ifndef WOLFSSL_NO_CT_OPS
  457. /* Constant time - mask set when a > b. */
  458. WC_MISC_STATIC WC_INLINE byte ctMaskGT(int a, int b)
  459. {
  460. return (byte)((((word32)a - (word32)b - 1) >> 31) - 1);
  461. }
  462. /* Constant time - mask set when a >= b. */
  463. WC_MISC_STATIC WC_INLINE byte ctMaskGTE(int a, int b)
  464. {
  465. return (byte)((((word32)a - (word32)b) >> 31) - 1);
  466. }
  467. /* Constant time - mask set when a >= b. */
  468. WC_MISC_STATIC WC_INLINE int ctMaskIntGTE(int a, int b)
  469. {
  470. return (int)((((word32)a - (word32)b) >> 31) - 1);
  471. }
  472. /* Constant time - mask set when a < b. */
  473. WC_MISC_STATIC WC_INLINE byte ctMaskLT(int a, int b)
  474. {
  475. return (byte)((((word32)b - (word32)a - 1) >> 31) - 1);
  476. }
  477. /* Constant time - mask set when a <= b. */
  478. WC_MISC_STATIC WC_INLINE byte ctMaskLTE(int a, int b)
  479. {
  480. return (byte)((((word32)b - (word32)a) >> 31) - 1);
  481. }
  482. /* Constant time - mask set when a == b. */
  483. WC_MISC_STATIC WC_INLINE byte ctMaskEq(int a, int b)
  484. {
  485. return (byte)((byte)(~ctMaskGT(a, b)) & (byte)(~ctMaskLT(a, b)));
  486. }
  487. /* Constant time - sets 16 bit integer mask when a > b */
  488. WC_MISC_STATIC WC_INLINE word16 ctMask16GT(int a, int b)
  489. {
  490. return (word16)((((word32)a - (word32)b - 1) >> 31) - 1);
  491. }
  492. /* Constant time - sets 16 bit integer mask when a >= b */
  493. WC_MISC_STATIC WC_INLINE word16 ctMask16GTE(int a, int b)
  494. {
  495. return (word16)((((word32)a - (word32)b) >> 31) - 1);
  496. }
  497. /* Constant time - sets 16 bit integer mask when a < b. */
  498. WC_MISC_STATIC WC_INLINE word16 ctMask16LT(int a, int b)
  499. {
  500. return (word16)((((word32)b - (word32)a - 1) >> 31) - 1);
  501. }
  502. /* Constant time - sets 16 bit integer mask when a <= b. */
  503. WC_MISC_STATIC WC_INLINE word16 ctMask16LTE(int a, int b)
  504. {
  505. return (word16)((((word32)b - (word32)a) >> 31) - 1);
  506. }
  507. /* Constant time - sets 16 bit integer mask when a == b. */
  508. WC_MISC_STATIC WC_INLINE word16 ctMask16Eq(int a, int b)
  509. {
  510. return (word16)((word16)(~ctMask16GT(a, b)) & (word16)(~ctMask16LT(a, b)));
  511. }
  512. /* Constant time - mask set when a != b. */
  513. WC_MISC_STATIC WC_INLINE byte ctMaskNotEq(int a, int b)
  514. {
  515. return (byte)((byte)ctMaskGT(a, b) | (byte)ctMaskLT(a, b));
  516. }
  517. /* Constant time - select a when mask is set and b otherwise. */
  518. WC_MISC_STATIC WC_INLINE byte ctMaskSel(byte m, byte a, byte b)
  519. {
  520. return (byte)((b & ((byte)~(word32)m)) | (a & m));
  521. }
  522. /* Constant time - select integer a when mask is set and integer b otherwise. */
  523. WC_MISC_STATIC WC_INLINE int ctMaskSelInt(byte m, int a, int b)
  524. {
  525. return (b & (~(signed int)(signed char)m)) |
  526. (a & ( (signed int)(signed char)m));
  527. }
  528. /* Constant time - select word32 a when mask is set and word32 b otherwise. */
  529. WC_MISC_STATIC WC_INLINE word32 ctMaskSelWord32(byte m, word32 a, word32 b)
  530. {
  531. return (((word32)b & (word32)(~(signed int)(signed char)m)) |
  532. ((word32)a & (word32)( (signed int)(signed char)m)));
  533. }
  534. /* Constant time - bit set when a <= b. */
  535. WC_MISC_STATIC WC_INLINE byte ctSetLTE(int a, int b)
  536. {
  537. return (byte)(((word32)a - (word32)b - 1) >> 31);
  538. }
  539. /* Constant time - conditionally copy size bytes from src to dst if mask is set
  540. */
  541. WC_MISC_STATIC WC_INLINE void ctMaskCopy(byte mask, byte* dst, byte* src,
  542. word16 size)
  543. {
  544. int i;
  545. for (i = 0; i < size; ++i) {
  546. dst[i] ^= (dst[i] ^ src[i]) & mask;
  547. }
  548. }
  549. #endif
  550. #if defined(WOLFSSL_W64_WRAPPER)
  551. #if defined(WORD64_AVAILABLE) && !defined(WOLFSSL_W64_WRAPPER_TEST)
  552. WC_MISC_STATIC WC_INLINE void w64Increment(w64wrapper *n) {
  553. n->n++;
  554. }
  555. WC_MISC_STATIC WC_INLINE void w64Decrement(w64wrapper *n) {
  556. n->n--;
  557. }
  558. WC_MISC_STATIC WC_INLINE byte w64Equal(w64wrapper a, w64wrapper b) {
  559. return (a.n == b.n);
  560. }
  561. WC_MISC_STATIC WC_INLINE word32 w64GetLow32(w64wrapper n) {
  562. return (word32)n.n;
  563. }
  564. WC_MISC_STATIC WC_INLINE word32 w64GetHigh32(w64wrapper n) {
  565. return (word32)(n.n >> 32);
  566. }
  567. WC_MISC_STATIC WC_INLINE void w64SetLow32(w64wrapper *n, word32 low) {
  568. n->n = (n->n & (~(word64)(0xffffffff))) | low;
  569. }
  570. WC_MISC_STATIC WC_INLINE w64wrapper w64Add32(w64wrapper a, word32 b, byte *wrap)
  571. {
  572. a.n = a.n + b;
  573. if (a.n < b && wrap != NULL)
  574. *wrap = 1;
  575. return a;
  576. }
  577. WC_MISC_STATIC WC_INLINE w64wrapper w64Sub32(w64wrapper a, word32 b, byte *wrap)
  578. {
  579. if (a.n < b && wrap != NULL)
  580. *wrap = 1;
  581. a.n = a.n - b;
  582. return a;
  583. }
  584. WC_MISC_STATIC WC_INLINE byte w64GT(w64wrapper a, w64wrapper b)
  585. {
  586. return a.n > b.n;
  587. }
  588. WC_MISC_STATIC WC_INLINE byte w64IsZero(w64wrapper a)
  589. {
  590. return a.n == 0;
  591. }
  592. WC_MISC_STATIC WC_INLINE void c64toa(const w64wrapper *a, byte *out)
  593. {
  594. #ifdef BIG_ENDIAN_ORDER
  595. XMEMCPY(out, &a->n, sizeof(a->n));
  596. #else
  597. word64 _out;
  598. _out = ByteReverseWord64(a->n);
  599. XMEMCPY(out, &_out, sizeof(_out));
  600. #endif /* BIG_ENDIAN_ORDER */
  601. }
  602. WC_MISC_STATIC WC_INLINE void ato64(const byte *in, w64wrapper *w64)
  603. {
  604. #ifdef BIG_ENDIAN_ORDER
  605. XMEMCPY(&w64->n, in, sizeof(w64->n));
  606. #else
  607. word64 _in;
  608. XMEMCPY(&_in, in, sizeof(_in));
  609. w64->n = ByteReverseWord64(_in);
  610. #endif /* BIG_ENDIAN_ORDER */
  611. }
  612. WC_MISC_STATIC WC_INLINE w64wrapper w64From32(word32 hi, word32 lo)
  613. {
  614. w64wrapper ret;
  615. ret.n = ((word64)hi << 32) | lo;
  616. return ret;
  617. }
  618. WC_MISC_STATIC WC_INLINE byte w64GTE(w64wrapper a, w64wrapper b)
  619. {
  620. return a.n >= b.n;
  621. }
  622. WC_MISC_STATIC WC_INLINE byte w64LT(w64wrapper a, w64wrapper b)
  623. {
  624. return a.n < b.n;
  625. }
  626. WC_MISC_STATIC WC_INLINE w64wrapper w64Sub(w64wrapper a, w64wrapper b)
  627. {
  628. a.n -= b.n;
  629. return a;
  630. }
  631. WC_MISC_STATIC WC_INLINE void w64Zero(w64wrapper *a)
  632. {
  633. a->n = 0;
  634. }
  635. #else
  636. WC_MISC_STATIC WC_INLINE void w64Increment(w64wrapper *n)
  637. {
  638. n->n[1]++;
  639. if (n->n[1] == 0)
  640. n->n[0]++;
  641. }
  642. WC_MISC_STATIC WC_INLINE void w64Decrement(w64wrapper *n) {
  643. if (n->n[1] == 0)
  644. n->n[0]--;
  645. n->n[1]--;
  646. }
  647. WC_MISC_STATIC WC_INLINE byte w64Equal(w64wrapper a, w64wrapper b)
  648. {
  649. return (a.n[0] == b.n[0] && a.n[1] == b.n[1]);
  650. }
  651. WC_MISC_STATIC WC_INLINE word32 w64GetLow32(w64wrapper n) {
  652. return n.n[1];
  653. }
  654. WC_MISC_STATIC WC_INLINE word32 w64GetHigh32(w64wrapper n) {
  655. return n.n[0];
  656. }
  657. WC_MISC_STATIC WC_INLINE void w64SetLow32(w64wrapper *n, word32 low)
  658. {
  659. n->n[1] = low;
  660. }
  661. WC_MISC_STATIC WC_INLINE w64wrapper w64Add32(w64wrapper a, word32 b, byte *wrap)
  662. {
  663. a.n[1] = a.n[1] + b;
  664. if (a.n[1] < b) {
  665. a.n[0]++;
  666. if (wrap != NULL && a.n[0] == 0)
  667. *wrap = 1;
  668. }
  669. return a;
  670. }
  671. WC_MISC_STATIC WC_INLINE w64wrapper w64Sub32(w64wrapper a, word32 b, byte *wrap)
  672. {
  673. byte _underflow = 0;
  674. if (a.n[1] < b)
  675. _underflow = 1;
  676. a.n[1] -= b;
  677. if (_underflow) {
  678. if (a.n[0] == 0 && wrap != NULL)
  679. *wrap = 1;
  680. a.n[0]--;
  681. }
  682. return a;
  683. }
  684. WC_MISC_STATIC WC_INLINE w64wrapper w64Sub(w64wrapper a, w64wrapper b)
  685. {
  686. if (a.n[1] < b.n[1])
  687. a.n[0]--;
  688. a.n[1] -= b.n[1];
  689. a.n[0] -= b.n[0];
  690. return a;
  691. }
  692. WC_MISC_STATIC WC_INLINE void w64Zero(w64wrapper *a)
  693. {
  694. a->n[0] = a->n[1] = 0;
  695. }
  696. WC_MISC_STATIC WC_INLINE byte w64GT(w64wrapper a, w64wrapper b)
  697. {
  698. if (a.n[0] > b.n[0])
  699. return 1;
  700. if (a.n[0] == b.n[0])
  701. return a.n[1] > b.n[1];
  702. return 0;
  703. }
  704. WC_MISC_STATIC WC_INLINE byte w64GTE(w64wrapper a, w64wrapper b)
  705. {
  706. if (a.n[0] > b.n[0])
  707. return 1;
  708. if (a.n[0] == b.n[0])
  709. return a.n[1] >= b.n[1];
  710. return 0;
  711. }
  712. WC_MISC_STATIC WC_INLINE byte w64IsZero(w64wrapper a)
  713. {
  714. return a.n[0] == 0 && a.n[1] == 0;
  715. }
  716. WC_MISC_STATIC WC_INLINE void c64toa(w64wrapper *a, byte *out)
  717. {
  718. #ifdef BIG_ENDIAN_ORDER
  719. word32 *_out = (word32*)(out);
  720. _out[0] = a->n[0];
  721. _out[1] = a->n[1];
  722. #else
  723. c32toa(a->n[0], out);
  724. c32toa(a->n[1], out + 4);
  725. #endif /* BIG_ENDIAN_ORDER */
  726. }
  727. WC_MISC_STATIC WC_INLINE void ato64(const byte *in, w64wrapper *w64)
  728. {
  729. #ifdef BIG_ENDIAN_ORDER
  730. const word32 *_in = (const word32*)(in);
  731. w64->n[0] = *_in;
  732. w64->n[1] = *(_in + 1);
  733. #else
  734. ato32(in, &w64->n[0]);
  735. ato32(in + 4, &w64->n[1]);
  736. #endif /* BIG_ENDIAN_ORDER */
  737. }
  738. WC_MISC_STATIC WC_INLINE w64wrapper w64From32(word32 hi, word32 lo)
  739. {
  740. w64wrapper w64;
  741. w64.n[0] = hi;
  742. w64.n[1] = lo;
  743. return w64;
  744. }
  745. WC_MISC_STATIC WC_INLINE byte w64LT(w64wrapper a, w64wrapper b)
  746. {
  747. if (a.n[0] < b.n[0])
  748. return 1;
  749. if (a.n[0] == b.n[0])
  750. return a.n[1] < b.n[1];
  751. return 0;
  752. }
  753. #endif /* WORD64_AVAILABLE && !WOLFSSL_W64_WRAPPER_TEST */
  754. #endif /* WOLFSSL_W64_WRAPPER */
  755. #if defined(HAVE_SESSION_TICKET) || !defined(NO_CERTS) || \
  756. !defined(NO_SESSION_CACHE)
  757. /* Make a word from the front of random hash */
  758. WC_MISC_STATIC WC_INLINE word32 MakeWordFromHash(const byte* hashID)
  759. {
  760. return ((word32)hashID[0] << 24) | ((word32)hashID[1] << 16) |
  761. ((word32)hashID[2] << 8) | (word32)hashID[3];
  762. }
  763. #endif /* HAVE_SESSION_TICKET || !NO_CERTS || !NO_SESSION_CACHE */
  764. #if !defined(WOLFCRYPT_ONLY) && !defined(NO_HASH_WRAPPER) && \
  765. (!defined(NO_SESSION_CACHE) || defined(HAVE_SESSION_TICKET))
  766. #include <wolfssl/wolfcrypt/hash.h>
  767. /* some session IDs aren't random after all, let's make them random */
  768. WC_MISC_STATIC WC_INLINE word32 HashObject(const byte* o, word32 len,
  769. int* error)
  770. {
  771. byte digest[WC_MAX_DIGEST_SIZE];
  772. #ifndef NO_MD5
  773. *error = wc_Md5Hash(o, len, digest);
  774. #elif !defined(NO_SHA)
  775. *error = wc_ShaHash(o, len, digest);
  776. #elif !defined(NO_SHA256)
  777. *error = wc_Sha256Hash(o, len, digest);
  778. #else
  779. #error "We need a digest to hash the session IDs"
  780. #endif
  781. return *error == 0 ? MakeWordFromHash(digest) : 0; /* 0 on failure */
  782. }
  783. #endif /* WOLFCRYPT_ONLY && !NO_HASH_WRAPPER &&
  784. * (!NO_SESSION_CACHE || HAVE_SESSION_TICKET) */
  785. #endif /* !WOLFSSL_MISC_INCLUDED && !NO_INLINE */
  786. #endif /* WOLF_CRYPT_MISC_C */