1 /* misc.h
2  *
3  * Copyright (C) 2006-2021 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 
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 WOLFSSL_LOCAL
42 word32 rotlFixed(word32, word32);
43 WOLFSSL_LOCAL
44 word32 rotrFixed(word32, word32);
45 
46 #ifdef WC_RC2
47 WOLFSSL_LOCAL
48 word16 rotlFixed16(word16, word16);
49 WOLFSSL_LOCAL
50 word16 rotrFixed16(word16, word16);
51 #endif
52 
53 WOLFSSL_LOCAL
54 word32 ByteReverseWord32(word32);
55 WOLFSSL_LOCAL
56 void   ByteReverseWords(word32*, const word32*, word32);
57 
58 WOLFSSL_LOCAL
59 void XorWordsOut(wolfssl_word* r, const wolfssl_word* a, const wolfssl_word* b,
60                  word32 n);
61 WOLFSSL_LOCAL
62 void xorbufout(void*, const void*, const void*, word32);
63 WOLFSSL_LOCAL
64 void XorWords(wolfssl_word*, const wolfssl_word*, word32);
65 WOLFSSL_LOCAL
66 void xorbuf(void*, const void*, word32);
67 
68 WOLFSSL_LOCAL
69 void ForceZero(void*, word32);
70 
71 WOLFSSL_LOCAL
72 int ConstantCompare(const byte*, const byte*, int);
73 
74 #ifdef WORD64_AVAILABLE
75 WOLFSSL_LOCAL
76 word64 rotlFixed64(word64, word64);
77 WOLFSSL_LOCAL
78 word64 rotrFixed64(word64, word64);
79 
80 WOLFSSL_LOCAL
81 word64 ByteReverseWord64(word64);
82 WOLFSSL_LOCAL
83 void   ByteReverseWords64(word64*, const word64*, word32);
84 #endif /* WORD64_AVAILABLE */
85 
86 #ifndef WOLFSSL_HAVE_MIN
87     #if defined(HAVE_FIPS) && !defined(min) /* so ifdef check passes */
88         #define min min
89     #endif
90     WOLFSSL_LOCAL word32 min(word32 a, word32 b);
91 #endif
92 
93 #ifndef WOLFSSL_HAVE_MAX
94     #if defined(HAVE_FIPS) && !defined(max) /* so ifdef check passes */
95         #define max max
96     #endif
97     WOLFSSL_LOCAL word32 max(word32 a, word32 b);
98 #endif /* WOLFSSL_HAVE_MAX */
99 
100 
101 void c32to24(word32 in, word24 out);
102 void c16toa(word16 u16, byte* c);
103 void c32toa(word32 u32, byte* c);
104 void c24to32(const word24 u24, word32* u32);
105 void ato16(const byte* c, word16* u16);
106 void ato24(const byte* c, word32* u24);
107 void ato32(const byte* c, word32* u32);
108 word32 btoi(byte b);
109 
110 WOLFSSL_LOCAL signed char HexCharToByte(char ch);
111 WOLFSSL_LOCAL char ByteToHex(byte in);
112 WOLFSSL_LOCAL int  ByteToHexStr(byte in, char* out);
113 
114 WOLFSSL_LOCAL byte ctMaskGT(int a, int b);
115 WOLFSSL_LOCAL byte ctMaskGTE(int a, int b);
116 WOLFSSL_LOCAL int  ctMaskIntGTE(int a, int b);
117 WOLFSSL_LOCAL byte ctMaskLT(int a, int b);
118 WOLFSSL_LOCAL byte ctMaskLTE(int a, int b);
119 WOLFSSL_LOCAL byte ctMaskEq(int a, int b);
120 WOLFSSL_LOCAL word16 ctMask16GT(int a, int b);
121 WOLFSSL_LOCAL word16 ctMask16GTE(int a, int b);
122 WOLFSSL_LOCAL word16 ctMask16LT(int a, int b);
123 WOLFSSL_LOCAL word16 ctMask16LTE(int a, int b);
124 WOLFSSL_LOCAL word16 ctMask16Eq(int a, int b);
125 WOLFSSL_LOCAL byte ctMaskNotEq(int a, int b);
126 WOLFSSL_LOCAL byte ctMaskSel(byte m, byte a, byte b);
127 WOLFSSL_LOCAL int  ctMaskSelInt(byte m, int a, int b);
128 WOLFSSL_LOCAL byte ctSetLTE(int a, int b);
129 
130 #endif /* NO_INLINE */
131 
132 
133 #ifdef __cplusplus
134     }   /* extern "C" */
135 #endif
136 
137 
138 #endif /* WOLF_CRYPT_MISC_H */
139 
140