misc.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* misc.h
  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. #ifndef WOLF_CRYPT_MISC_H
  27. #define WOLF_CRYPT_MISC_H
  28. #include <wolfssl/wolfcrypt/types.h>
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. #ifdef NO_INLINE
  33. #define WC_MISC_STATIC
  34. WOLFSSL_LOCAL
  35. word32 rotlFixed(word32 x, word32 y);
  36. WOLFSSL_LOCAL
  37. word32 rotrFixed(word32 x, word32 y);
  38. #ifdef WC_RC2
  39. WOLFSSL_LOCAL
  40. word16 rotlFixed16(word16 x, word16 y);
  41. WOLFSSL_LOCAL
  42. word16 rotrFixed16(word16 x, word16 y);
  43. #endif
  44. WOLFSSL_LOCAL
  45. word32 ByteReverseWord32(word32 value);
  46. WOLFSSL_LOCAL
  47. void ByteReverseWords(word32* out, const word32* in, word32 byteCount);
  48. WOLFSSL_LOCAL
  49. void XorWordsOut(wolfssl_word** r, const wolfssl_word** a,
  50. const wolfssl_word** b, word32 n);
  51. WOLFSSL_LOCAL
  52. void xorbufout(void* out, const void* buf, const void* mask, word32 count);
  53. WOLFSSL_LOCAL
  54. void XorWords(wolfssl_word** r, const wolfssl_word** a, word32 n);
  55. WOLFSSL_LOCAL
  56. void xorbuf(void* buf, const void* mask, word32 count);
  57. WOLFSSL_LOCAL
  58. void ForceZero(void* mem, word32 len);
  59. WOLFSSL_LOCAL
  60. int ConstantCompare(const byte* a, const byte* b, int length);
  61. #ifdef WORD64_AVAILABLE
  62. WOLFSSL_LOCAL
  63. word64 rotlFixed64(word64 x, word64 y);
  64. WOLFSSL_LOCAL
  65. word64 rotrFixed64(word64 x, word64 y);
  66. WOLFSSL_LOCAL
  67. word64 ByteReverseWord64(word64 value);
  68. WOLFSSL_LOCAL
  69. void ByteReverseWords64(word64* out, const word64* in, word32 byteCount);
  70. #endif /* WORD64_AVAILABLE */
  71. #ifndef WOLFSSL_HAVE_MIN
  72. #if defined(HAVE_FIPS) && !defined(min) /* so ifdef check passes */
  73. #define min min
  74. #endif
  75. WOLFSSL_LOCAL word32 min(word32 a, word32 b);
  76. #endif
  77. #ifndef WOLFSSL_HAVE_MAX
  78. #if defined(HAVE_FIPS) && !defined(max) /* so ifdef check passes */
  79. #define max max
  80. #endif
  81. WOLFSSL_LOCAL word32 max(word32 a, word32 b);
  82. #endif /* WOLFSSL_HAVE_MAX */
  83. void c32to24(word32 in, word24 out);
  84. void c16toa(word16 wc_u16, byte* c);
  85. void c32toa(word32 wc_u32, byte* c);
  86. void c24to32(const word24 wc_u24, word32* wc_u32);
  87. void ato16(const byte* c, word16* wc_u16);
  88. void ato24(const byte* c, word32* wc_u24);
  89. void ato32(const byte* c, word32* wc_u32);
  90. void ato32le(const byte* c, word32* wc_u32);
  91. word32 btoi(byte b);
  92. WOLFSSL_LOCAL signed char HexCharToByte(char ch);
  93. WOLFSSL_LOCAL char ByteToHex(byte in);
  94. WOLFSSL_LOCAL int ByteToHexStr(byte in, char* out);
  95. WOLFSSL_LOCAL byte ctMaskGT(int a, int b);
  96. WOLFSSL_LOCAL byte ctMaskGTE(int a, int b);
  97. WOLFSSL_LOCAL int ctMaskIntGTE(int a, int b);
  98. WOLFSSL_LOCAL byte ctMaskLT(int a, int b);
  99. WOLFSSL_LOCAL byte ctMaskLTE(int a, int b);
  100. WOLFSSL_LOCAL byte ctMaskEq(int a, int b);
  101. WOLFSSL_LOCAL word16 ctMask16GT(int a, int b);
  102. WOLFSSL_LOCAL word16 ctMask16GTE(int a, int b);
  103. WOLFSSL_LOCAL word16 ctMask16LT(int a, int b);
  104. WOLFSSL_LOCAL word16 ctMask16LTE(int a, int b);
  105. WOLFSSL_LOCAL word16 ctMask16Eq(int a, int b);
  106. WOLFSSL_LOCAL byte ctMaskNotEq(int a, int b);
  107. WOLFSSL_LOCAL byte ctMaskSel(byte m, byte a, byte b);
  108. WOLFSSL_LOCAL int ctMaskSelInt(byte m, int a, int b);
  109. WOLFSSL_LOCAL word32 ctMaskSelWord32(byte m, word32 a, word32 b);
  110. WOLFSSL_LOCAL byte ctSetLTE(int a, int b);
  111. WOLFSSL_LOCAL void ctMaskCopy(byte mask, byte* dst, byte* src, word16 size);
  112. WOLFSSL_LOCAL word32 MakeWordFromHash(const byte* hashID);
  113. WOLFSSL_LOCAL word32 HashObject(const byte* o, word32 len, int* error);
  114. WOLFSSL_LOCAL void w64Increment(w64wrapper *n);
  115. WOLFSSL_LOCAL void w64Decrement(w64wrapper *n);
  116. WOLFSSL_LOCAL byte w64Equal(w64wrapper a, w64wrapper b);
  117. WOLFSSL_LOCAL word32 w64GetLow32(w64wrapper n);
  118. WOLFSSL_LOCAL word32 w64GetHigh32(w64wrapper n);
  119. WOLFSSL_LOCAL void w64SetLow32(w64wrapper *n, word32 low);
  120. WOLFSSL_LOCAL w64wrapper w64Add32(w64wrapper a, word32 b, byte *wrap);
  121. WOLFSSL_LOCAL w64wrapper w64Sub32(w64wrapper a, word32 b, byte *wrap);
  122. WOLFSSL_LOCAL byte w64GT(w64wrapper a, w64wrapper b);
  123. WOLFSSL_LOCAL byte w64IsZero(w64wrapper a);
  124. WOLFSSL_LOCAL void c64toa(const w64wrapper *a, byte *out);
  125. WOLFSSL_LOCAL void ato64(const byte *in, w64wrapper *w64);
  126. WOLFSSL_LOCAL w64wrapper w64From32(word32 hi, word32 lo);
  127. WOLFSSL_LOCAL byte w64GTE(w64wrapper a, w64wrapper b);
  128. WOLFSSL_LOCAL byte w64LT(w64wrapper a, w64wrapper b);
  129. WOLFSSL_LOCAL w64wrapper w64Sub(w64wrapper a, w64wrapper b);
  130. WOLFSSL_LOCAL void w64Zero(w64wrapper *a);
  131. #else /* !NO_INLINE */
  132. #define WC_MISC_STATIC static
  133. /* Declarations for user defined functions */
  134. #ifdef WOLFSSL_NO_FORCE_ZERO
  135. void ForceZero(void* mem, word32 len);
  136. #endif
  137. #ifdef WOLFSSL_NO_CONST_CMP
  138. int ConstantCompare(const byte* a, const byte* b, int length);
  139. #endif
  140. #ifdef WOLFSSL_NO_INT_ENCODE
  141. void c32to24(word32 in, word24 out);
  142. void c16toa(word16 wc_u16, byte* c);
  143. void c32toa(word32 wc_u32, byte* c);
  144. #endif
  145. #ifdef WOLFSSL_NO_INT_DECODE
  146. void c24to32(const word24 wc_u24, word32* wc_u32);
  147. void ato24(const byte* c, word32* wc_u24);
  148. void ato16(const byte* c, word16* wc_u16);
  149. void ato32(const byte* c, word32* wc_u32);
  150. void ato32le(const byte* c, word32* wc_u32);
  151. word32 btoi(byte b);
  152. #endif
  153. #endif /* NO_INLINE */
  154. #ifdef __cplusplus
  155. } /* extern "C" */
  156. #endif
  157. #endif /* WOLF_CRYPT_MISC_H */