wolfssh/src/misc.c

134 lines
2.9 KiB
C

/* 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;
}
#ifdef WOLFSSH_NO_FORCEZERO
/* Make sure compiler doesn't skip */
STATIC INLINE void wolfSSH_ForceZero(void* mem, size_t len)
{
volatile byte* z = (volatile byte*)mem;
while (len--) *z++ = 0;
}
#endif
/* 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 */