misc.h (6966B)
1 /* misc.h 2 * 3 * Copyright (C) 2006-2025 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 3 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 23 DESCRIPTION 24 This module implements the arithmetic-shift right, left, byte swapping, XOR, 25 masking and clearing memory logic. 26 27 */ 28 #ifndef WOLF_CRYPT_MISC_H 29 #define WOLF_CRYPT_MISC_H 30 31 32 #include <wolfssl/wolfcrypt/types.h> 33 34 35 #ifdef __cplusplus 36 extern "C" { 37 #endif 38 39 40 #ifdef NO_INLINE 41 42 #define WC_MISC_STATIC 43 44 WOLFSSL_LOCAL 45 word32 rotlFixed(word32 x, word32 y); 46 WOLFSSL_LOCAL 47 word32 rotrFixed(word32 x, word32 y); 48 49 WOLFSSL_LOCAL 50 word16 rotlFixed16(word16 x, word16 y); 51 WOLFSSL_LOCAL 52 word16 rotrFixed16(word16 x, word16 y); 53 54 WOLFSSL_LOCAL 55 word32 ByteReverseWord32(word32 value); 56 WOLFSSL_LOCAL 57 void ByteReverseWords(word32* out, const word32* in, word32 byteCount); 58 59 WOLFSSL_LOCAL 60 void XorWordsOut(wolfssl_word** r, const wolfssl_word** a, 61 const wolfssl_word** b, word32 n); 62 WOLFSSL_LOCAL 63 void xorbufout(void* out, const void* buf, const void* mask, word32 count); 64 WOLFSSL_LOCAL 65 void XorWords(wolfssl_word** r, const wolfssl_word** a, word32 n); 66 WOLFSSL_LOCAL 67 void xorbuf(void* buf, const void* mask, word32 count); 68 69 WOLFSSL_LOCAL 70 void ForceZero(void* mem, word32 len); 71 72 WOLFSSL_LOCAL 73 int ConstantCompare(const byte* a, const byte* b, int length); 74 75 WOLFSSL_LOCAL 76 word32 readUnalignedWord32(const byte *in); 77 WOLFSSL_LOCAL 78 word32 writeUnalignedWord32(void *out, word32 in); 79 WOLFSSL_LOCAL 80 void readUnalignedWords32(word32 *out, const byte *in, size_t count); 81 WOLFSSL_LOCAL 82 void writeUnalignedWords32(byte *out, const word32 *in, size_t count); 83 84 #ifdef WORD64_AVAILABLE 85 WOLFSSL_LOCAL 86 word64 readUnalignedWord64(const byte *in); 87 WOLFSSL_LOCAL 88 word64 writeUnalignedWord64(void *out, word64 in); 89 WOLFSSL_LOCAL 90 void readUnalignedWords64(word64 *out, const byte *in, size_t count); 91 WOLFSSL_LOCAL 92 void writeUnalignedWords64(byte *out, const word64 *in, size_t count); 93 WOLFSSL_LOCAL 94 word64 rotlFixed64(word64 x, word64 y); 95 WOLFSSL_LOCAL 96 word64 rotrFixed64(word64 x, word64 y); 97 98 WOLFSSL_LOCAL 99 word64 ByteReverseWord64(word64 value); 100 WOLFSSL_LOCAL 101 void ByteReverseWords64(word64* out, const word64* in, word32 byteCount); 102 #endif /* WORD64_AVAILABLE */ 103 104 #ifndef WOLFSSL_HAVE_MIN 105 #if defined(HAVE_FIPS) && !defined(min) /* so ifdef check passes */ 106 #define min min 107 #endif 108 WOLFSSL_LOCAL word32 min(word32 a, word32 b); 109 #endif 110 111 #ifndef WOLFSSL_HAVE_MAX 112 #if defined(HAVE_FIPS) && !defined(max) /* so ifdef check passes */ 113 #define max max 114 #endif 115 WOLFSSL_LOCAL word32 max(word32 a, word32 b); 116 #endif /* WOLFSSL_HAVE_MAX */ 117 118 119 void c32to24(word32 in, word24 out); 120 void c16toa(word16 wc_u16, byte* c); 121 void c32toa(word32 wc_u32, byte* c); 122 void c24to32(const word24 wc_u24, word32* wc_u32); 123 void ato16(const byte* c, word16* wc_u16); 124 void ato24(const byte* c, word32* wc_u24); 125 void ato32(const byte* c, word32* wc_u32); 126 void ato32le(const byte* c, word32* wc_u32); 127 word32 btoi(byte b); 128 129 WOLFSSL_LOCAL signed char HexCharToByte(char ch); 130 WOLFSSL_LOCAL char ByteToHex(byte in); 131 WOLFSSL_LOCAL int ByteToHexStr(byte in, char* out); 132 WOLFSSL_LOCAL int CharIsWhiteSpace(char ch); 133 134 WOLFSSL_LOCAL byte ctMaskGT(int a, int b); 135 WOLFSSL_LOCAL byte ctMaskGTE(int a, int b); 136 WOLFSSL_LOCAL int ctMaskIntGTE(int a, int b); 137 #ifdef WORD64_AVAILABLE 138 WOLFSSL_LOCAL word32 ctMaskWord32GTE(word32 a, word32 b); 139 #endif 140 WOLFSSL_LOCAL byte ctMaskLT(int a, int b); 141 WOLFSSL_LOCAL byte ctMaskLTE(int a, int b); 142 WOLFSSL_LOCAL byte ctMaskEq(int a, int b); 143 WOLFSSL_LOCAL word16 ctMask16GT(int a, int b); 144 WOLFSSL_LOCAL word16 ctMask16GTE(int a, int b); 145 WOLFSSL_LOCAL word16 ctMask16LT(int a, int b); 146 WOLFSSL_LOCAL word16 ctMask16LTE(int a, int b); 147 WOLFSSL_LOCAL word16 ctMask16Eq(int a, int b); 148 WOLFSSL_LOCAL byte ctMaskNotEq(int a, int b); 149 WOLFSSL_LOCAL byte ctMaskSel(byte m, byte a, byte b); 150 WOLFSSL_LOCAL int ctMaskSelInt(byte m, int a, int b); 151 WOLFSSL_LOCAL word32 ctMaskSelWord32(byte m, word32 a, word32 b); 152 WOLFSSL_LOCAL byte ctSetLTE(int a, int b); 153 WOLFSSL_LOCAL void ctMaskCopy(byte mask, byte* dst, byte* src, word16 size); 154 WOLFSSL_LOCAL word32 MakeWordFromHash(const byte* hashID); 155 WOLFSSL_LOCAL word32 HashObject(const byte* o, word32 len, int* error); 156 WOLFSSL_LOCAL char* CopyString(const char* src, int srcLen, void* heap, 157 int type); 158 159 WOLFSSL_LOCAL void w64Increment(w64wrapper *n); 160 WOLFSSL_LOCAL void w64Decrement(w64wrapper *n); 161 WOLFSSL_LOCAL byte w64Equal(w64wrapper a, w64wrapper b); 162 WOLFSSL_LOCAL word32 w64GetLow32(w64wrapper n); 163 WOLFSSL_LOCAL word32 w64GetHigh32(w64wrapper n); 164 WOLFSSL_LOCAL void w64SetLow32(w64wrapper *n, word32 low); 165 WOLFSSL_LOCAL w64wrapper w64Add32(w64wrapper a, word32 b, byte *wrap); 166 WOLFSSL_LOCAL w64wrapper w64Add(w64wrapper a, w64wrapper b, byte *wrap); 167 WOLFSSL_LOCAL w64wrapper w64Sub32(w64wrapper a, word32 b, byte *wrap); 168 WOLFSSL_LOCAL byte w64GT(w64wrapper a, w64wrapper b); 169 WOLFSSL_LOCAL byte w64IsZero(w64wrapper a); 170 WOLFSSL_LOCAL void c64toa(const w64wrapper *a, byte *out); 171 WOLFSSL_LOCAL void ato64(const byte *in, w64wrapper *w64); 172 WOLFSSL_LOCAL w64wrapper w64From32(word32 hi, word32 lo); 173 WOLFSSL_LOCAL byte w64GTE(w64wrapper a, w64wrapper b); 174 WOLFSSL_LOCAL byte w64LT(w64wrapper a, w64wrapper b); 175 WOLFSSL_LOCAL w64wrapper w64Sub(w64wrapper a, w64wrapper b); 176 WOLFSSL_LOCAL void w64Zero(w64wrapper *a); 177 WOLFSSL_LOCAL w64wrapper w64ShiftRight(w64wrapper a, int shift); 178 WOLFSSL_LOCAL w64wrapper w64ShiftLeft(w64wrapper a, int shift); 179 WOLFSSL_LOCAL w64wrapper w64Mul(word32 a, word32 b); 180 181 #else /* !NO_INLINE */ 182 183 #define WC_MISC_STATIC static 184 185 /* Declarations for user defined functions */ 186 #ifdef WOLFSSL_NO_FORCE_ZERO 187 void ForceZero(void* mem, word32 len); 188 #endif 189 #ifdef WOLFSSL_NO_CONST_CMP 190 int ConstantCompare(const byte* a, const byte* b, int length); 191 #endif 192 #ifdef WOLFSSL_NO_INT_ENCODE 193 void c32to24(word32 in, word24 out); 194 void c16toa(word16 wc_u16, byte* c); 195 void c32toa(word32 wc_u32, byte* c); 196 #endif 197 #ifdef WOLFSSL_NO_INT_DECODE 198 void c24to32(const word24 wc_u24, word32* wc_u32); 199 void ato24(const byte* c, word32* wc_u24); 200 void ato16(const byte* c, word16* wc_u16); 201 void ato32(const byte* c, word32* wc_u32); 202 void ato32le(const byte* c, word32* wc_u32); 203 word32 btoi(byte b); 204 #endif 205 #endif /* NO_INLINE */ 206 207 208 #ifdef __cplusplus 209 } /* extern "C" */ 210 #endif 211 212 213 #endif /* WOLF_CRYPT_MISC_H */ 214