forked from wolfSSL/wolfssh
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmisc.c
More file actions
131 lines (97 loc) · 2.88 KB
/
misc.c
File metadata and controls
131 lines (97 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* misc.c
*
* Copyright (C) 2014-2026 wolfSSL Inc.
*
* This file is part of wolfSSH.
*
* wolfSSH is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfSSH is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with wolfSSH. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* The misc module contains inline functions. This file is either included
* into source files or built separately depending on the inline configure
* option.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifdef WOLFSSL_USER_SETTINGS
#include <wolfssl/wolfcrypt/settings.h>
#else
#include <wolfssl/options.h>
#endif
#ifndef WOLFSSH_MISC_C
#define WOLFSSH_MISC_C
#include <wolfssh/misc.h>
#include <wolfssh/log.h>
#ifdef NO_INLINE
#define STATIC
#else
#define STATIC static
#endif
/* Check for if compiling misc.c when not needed. */
#if !defined(WOLFSSH_MISC_INCLUDED) && !defined(NO_INLINE) && \
!defined(WOLFSSH_IGNORE_FILE_WARN)
#ifndef _MSC_VER
#warning "misc.c does not need to be compiled when using inline (NO_INLINE not defined))"
#else
#pragma message("warning: misc.c does not need to be compiled when using inline (NO_INLINE not defined))")
#endif
#else /* !WOLFSSL_MISC_INCLUDED && !NO_INLINE && !WOLFSSH_IGNORE_FILE_WARN */
#ifndef min
STATIC INLINE word32 min(word32 a, word32 b)
{
return a > b ? b : a;
}
#endif /* min */
/* convert opaque to 32 bit integer */
STATIC INLINE void ato32(const byte* c, word32* u32)
{
word32 v = 0;
v |= (word32)(c[0] & 0xFF);
v <<= 8;
v |= (word32)(c[1] & 0xFF);
v <<= 8;
v |= (word32)(c[2] & 0xFF);
v <<= 8;
v |= (word32)(c[3] & 0xFF);
*u32 = v;
}
/* convert 32 bit integer to opaque */
STATIC INLINE void c32toa(word32 u32, byte* c)
{
c[0] = (u32 >> 24) & 0xff;
c[1] = (u32 >> 16) & 0xff;
c[2] = (u32 >> 8) & 0xff;
c[3] = u32 & 0xff;
}
/* Make sure compiler doesn't skip */
STATIC INLINE void ForceZero(const void* mem, word32 length)
{
volatile byte* z = (volatile byte*)mem;
while (length--) *z++ = 0;
}
/* check all length bytes for equality, return 0 on success */
STATIC INLINE int ConstantCompare(const byte* a, const byte* b,
word32 length)
{
word32 i;
word32 compareSum = 0;
for (i = 0; i < length; i++) {
compareSum |= a[i] ^ b[i];
}
return compareSum;
}
#undef STATIC
#endif /* !WOLFSSL_MISC_INCLUDED && !NO_INLINE */
#endif /* WOLFSSH_MISC_C */