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