A library for setting up Secure Socket Layer (SSL) connections and verifying remote hosts using certificates. Contains only the source files for mbed platform implementation of the library.
Dependents: HTTPClient-SSL HTTPClient-SSL HTTPClient-SSL HTTPClient-SSL
ctaocrypt/src/misc.c@6:cf58d49e1a86, 2015-03-23 (annotated)
- Committer:
- Mike Fiore
- Date:
- Mon Mar 23 16:51:07 2015 -0500
- Revision:
- 6:cf58d49e1a86
- Parent:
- 0:b86d15c6ba29
fix whitespace in sha512.c
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Vanger | 0:b86d15c6ba29 | 1 | /* misc.c |
Vanger | 0:b86d15c6ba29 | 2 | * |
Vanger | 0:b86d15c6ba29 | 3 | * Copyright (C) 2006-2014 wolfSSL Inc. |
Vanger | 0:b86d15c6ba29 | 4 | * |
Vanger | 0:b86d15c6ba29 | 5 | * This file is part of CyaSSL. |
Vanger | 0:b86d15c6ba29 | 6 | * |
Vanger | 0:b86d15c6ba29 | 7 | * CyaSSL is free software; you can redistribute it and/or modify |
Vanger | 0:b86d15c6ba29 | 8 | * it under the terms of the GNU General Public License as published by |
Vanger | 0:b86d15c6ba29 | 9 | * the Free Software Foundation; either version 2 of the License, or |
Vanger | 0:b86d15c6ba29 | 10 | * (at your option) any later version. |
Vanger | 0:b86d15c6ba29 | 11 | * |
Vanger | 0:b86d15c6ba29 | 12 | * CyaSSL is distributed in the hope that it will be useful, |
Vanger | 0:b86d15c6ba29 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
Vanger | 0:b86d15c6ba29 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Vanger | 0:b86d15c6ba29 | 15 | * GNU General Public License for more details. |
Vanger | 0:b86d15c6ba29 | 16 | * |
Vanger | 0:b86d15c6ba29 | 17 | * You should have received a copy of the GNU General Public License |
Vanger | 0:b86d15c6ba29 | 18 | * along with this program; if not, write to the Free Software |
Vanger | 0:b86d15c6ba29 | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
Vanger | 0:b86d15c6ba29 | 20 | */ |
Vanger | 0:b86d15c6ba29 | 21 | |
Vanger | 0:b86d15c6ba29 | 22 | #ifdef HAVE_CONFIG_H |
Vanger | 0:b86d15c6ba29 | 23 | #include <config.h> |
Vanger | 0:b86d15c6ba29 | 24 | #endif |
Vanger | 0:b86d15c6ba29 | 25 | |
Vanger | 0:b86d15c6ba29 | 26 | #include <cyassl/ctaocrypt/settings.h> |
Vanger | 0:b86d15c6ba29 | 27 | |
Vanger | 0:b86d15c6ba29 | 28 | #include <cyassl/ctaocrypt/misc.h> |
Vanger | 0:b86d15c6ba29 | 29 | |
Vanger | 0:b86d15c6ba29 | 30 | /* inlining these functions is a huge speed increase and a small size decrease, |
Vanger | 0:b86d15c6ba29 | 31 | because the functions are smaller than function call setup/cleanup, e.g., |
Vanger | 0:b86d15c6ba29 | 32 | md5 benchmark is twice as fast with inline. If you don't want it, then |
Vanger | 0:b86d15c6ba29 | 33 | define NO_INLINE and compile this file into cyassl, otherwise it's used as |
Vanger | 0:b86d15c6ba29 | 34 | a source header |
Vanger | 0:b86d15c6ba29 | 35 | */ |
Vanger | 0:b86d15c6ba29 | 36 | |
Vanger | 0:b86d15c6ba29 | 37 | #ifdef NO_INLINE |
Vanger | 0:b86d15c6ba29 | 38 | #define STATIC |
Vanger | 0:b86d15c6ba29 | 39 | #else |
Vanger | 0:b86d15c6ba29 | 40 | #define STATIC static |
Vanger | 0:b86d15c6ba29 | 41 | #endif |
Vanger | 0:b86d15c6ba29 | 42 | |
Vanger | 0:b86d15c6ba29 | 43 | |
Vanger | 0:b86d15c6ba29 | 44 | #ifdef INTEL_INTRINSICS |
Vanger | 0:b86d15c6ba29 | 45 | |
Vanger | 0:b86d15c6ba29 | 46 | #include <stdlib.h> /* get intrinsic definitions */ |
Vanger | 0:b86d15c6ba29 | 47 | |
Vanger | 0:b86d15c6ba29 | 48 | /* for non visual studio probably need no long version, 32 bit only |
Vanger | 0:b86d15c6ba29 | 49 | * i.e., _rotl and _rotr */ |
Vanger | 0:b86d15c6ba29 | 50 | #pragma intrinsic(_lrotl, _lrotr) |
Vanger | 0:b86d15c6ba29 | 51 | |
Vanger | 0:b86d15c6ba29 | 52 | STATIC INLINE word32 rotlFixed(word32 x, word32 y) |
Vanger | 0:b86d15c6ba29 | 53 | { |
Vanger | 0:b86d15c6ba29 | 54 | return y ? _lrotl(x, y) : x; |
Vanger | 0:b86d15c6ba29 | 55 | } |
Vanger | 0:b86d15c6ba29 | 56 | |
Vanger | 0:b86d15c6ba29 | 57 | STATIC INLINE word32 rotrFixed(word32 x, word32 y) |
Vanger | 0:b86d15c6ba29 | 58 | { |
Vanger | 0:b86d15c6ba29 | 59 | return y ? _lrotr(x, y) : x; |
Vanger | 0:b86d15c6ba29 | 60 | } |
Vanger | 0:b86d15c6ba29 | 61 | |
Vanger | 0:b86d15c6ba29 | 62 | #else /* generic */ |
Vanger | 0:b86d15c6ba29 | 63 | |
Vanger | 0:b86d15c6ba29 | 64 | STATIC INLINE word32 rotlFixed(word32 x, word32 y) |
Vanger | 0:b86d15c6ba29 | 65 | { |
Vanger | 0:b86d15c6ba29 | 66 | return (x << y) | (x >> (sizeof(y) * 8 - y)); |
Vanger | 0:b86d15c6ba29 | 67 | } |
Vanger | 0:b86d15c6ba29 | 68 | |
Vanger | 0:b86d15c6ba29 | 69 | |
Vanger | 0:b86d15c6ba29 | 70 | STATIC INLINE word32 rotrFixed(word32 x, word32 y) |
Vanger | 0:b86d15c6ba29 | 71 | { |
Vanger | 0:b86d15c6ba29 | 72 | return (x >> y) | (x << (sizeof(y) * 8 - y)); |
Vanger | 0:b86d15c6ba29 | 73 | } |
Vanger | 0:b86d15c6ba29 | 74 | |
Vanger | 0:b86d15c6ba29 | 75 | #endif |
Vanger | 0:b86d15c6ba29 | 76 | |
Vanger | 0:b86d15c6ba29 | 77 | |
Vanger | 0:b86d15c6ba29 | 78 | STATIC INLINE word32 ByteReverseWord32(word32 value) |
Vanger | 0:b86d15c6ba29 | 79 | { |
Vanger | 0:b86d15c6ba29 | 80 | #ifdef PPC_INTRINSICS |
Vanger | 0:b86d15c6ba29 | 81 | /* PPC: load reverse indexed instruction */ |
Vanger | 0:b86d15c6ba29 | 82 | return (word32)__lwbrx(&value,0); |
Vanger | 0:b86d15c6ba29 | 83 | #elif defined(KEIL_INTRINSICS) |
Vanger | 0:b86d15c6ba29 | 84 | return (word32)__rev(value); |
Vanger | 0:b86d15c6ba29 | 85 | #elif defined(FAST_ROTATE) |
Vanger | 0:b86d15c6ba29 | 86 | /* 5 instructions with rotate instruction, 9 without */ |
Vanger | 0:b86d15c6ba29 | 87 | return (rotrFixed(value, 8U) & 0xff00ff00) | |
Vanger | 0:b86d15c6ba29 | 88 | (rotlFixed(value, 8U) & 0x00ff00ff); |
Vanger | 0:b86d15c6ba29 | 89 | #else |
Vanger | 0:b86d15c6ba29 | 90 | /* 6 instructions with rotate instruction, 8 without */ |
Vanger | 0:b86d15c6ba29 | 91 | value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8); |
Vanger | 0:b86d15c6ba29 | 92 | return rotlFixed(value, 16U); |
Vanger | 0:b86d15c6ba29 | 93 | #endif |
Vanger | 0:b86d15c6ba29 | 94 | } |
Vanger | 0:b86d15c6ba29 | 95 | |
Vanger | 0:b86d15c6ba29 | 96 | |
Vanger | 0:b86d15c6ba29 | 97 | STATIC INLINE void ByteReverseWords(word32* out, const word32* in, |
Vanger | 0:b86d15c6ba29 | 98 | word32 byteCount) |
Vanger | 0:b86d15c6ba29 | 99 | { |
Vanger | 0:b86d15c6ba29 | 100 | word32 count = byteCount/(word32)sizeof(word32), i; |
Vanger | 0:b86d15c6ba29 | 101 | |
Vanger | 0:b86d15c6ba29 | 102 | for (i = 0; i < count; i++) |
Vanger | 0:b86d15c6ba29 | 103 | out[i] = ByteReverseWord32(in[i]); |
Vanger | 0:b86d15c6ba29 | 104 | |
Vanger | 0:b86d15c6ba29 | 105 | } |
Vanger | 0:b86d15c6ba29 | 106 | |
Vanger | 0:b86d15c6ba29 | 107 | |
Vanger | 0:b86d15c6ba29 | 108 | #ifdef WORD64_AVAILABLE |
Vanger | 0:b86d15c6ba29 | 109 | |
Vanger | 0:b86d15c6ba29 | 110 | |
Vanger | 0:b86d15c6ba29 | 111 | STATIC INLINE word64 rotlFixed64(word64 x, word64 y) |
Vanger | 0:b86d15c6ba29 | 112 | { |
Vanger | 0:b86d15c6ba29 | 113 | return (x << y) | (x >> (sizeof(y) * 8 - y)); |
Vanger | 0:b86d15c6ba29 | 114 | } |
Vanger | 0:b86d15c6ba29 | 115 | |
Vanger | 0:b86d15c6ba29 | 116 | |
Vanger | 0:b86d15c6ba29 | 117 | STATIC INLINE word64 rotrFixed64(word64 x, word64 y) |
Vanger | 0:b86d15c6ba29 | 118 | { |
Vanger | 0:b86d15c6ba29 | 119 | return (x >> y) | (x << (sizeof(y) * 8 - y)); |
Vanger | 0:b86d15c6ba29 | 120 | } |
Vanger | 0:b86d15c6ba29 | 121 | |
Vanger | 0:b86d15c6ba29 | 122 | |
Vanger | 0:b86d15c6ba29 | 123 | STATIC INLINE word64 ByteReverseWord64(word64 value) |
Vanger | 0:b86d15c6ba29 | 124 | { |
Vanger | 0:b86d15c6ba29 | 125 | #ifdef CTAOCRYPT_SLOW_WORD64 |
Vanger | 0:b86d15c6ba29 | 126 | return (word64)(ByteReverseWord32((word32)value)) << 32 | |
Vanger | 0:b86d15c6ba29 | 127 | ByteReverseWord32((word32)(value>>32)); |
Vanger | 0:b86d15c6ba29 | 128 | #else |
Vanger | 0:b86d15c6ba29 | 129 | value = ((value & W64LIT(0xFF00FF00FF00FF00)) >> 8) | |
Vanger | 0:b86d15c6ba29 | 130 | ((value & W64LIT(0x00FF00FF00FF00FF)) << 8); |
Vanger | 0:b86d15c6ba29 | 131 | value = ((value & W64LIT(0xFFFF0000FFFF0000)) >> 16) | |
Vanger | 0:b86d15c6ba29 | 132 | ((value & W64LIT(0x0000FFFF0000FFFF)) << 16); |
Vanger | 0:b86d15c6ba29 | 133 | return rotlFixed64(value, 32U); |
Vanger | 0:b86d15c6ba29 | 134 | #endif |
Vanger | 0:b86d15c6ba29 | 135 | } |
Vanger | 0:b86d15c6ba29 | 136 | |
Vanger | 0:b86d15c6ba29 | 137 | |
Vanger | 0:b86d15c6ba29 | 138 | STATIC INLINE void ByteReverseWords64(word64* out, const word64* in, |
Vanger | 0:b86d15c6ba29 | 139 | word32 byteCount) |
Vanger | 0:b86d15c6ba29 | 140 | { |
Vanger | 0:b86d15c6ba29 | 141 | word32 count = byteCount/(word32)sizeof(word64), i; |
Vanger | 0:b86d15c6ba29 | 142 | |
Vanger | 0:b86d15c6ba29 | 143 | for (i = 0; i < count; i++) |
Vanger | 0:b86d15c6ba29 | 144 | out[i] = ByteReverseWord64(in[i]); |
Vanger | 0:b86d15c6ba29 | 145 | |
Vanger | 0:b86d15c6ba29 | 146 | } |
Vanger | 0:b86d15c6ba29 | 147 | |
Vanger | 0:b86d15c6ba29 | 148 | #endif /* WORD64_AVAILABLE */ |
Vanger | 0:b86d15c6ba29 | 149 | |
Vanger | 0:b86d15c6ba29 | 150 | |
Vanger | 0:b86d15c6ba29 | 151 | STATIC INLINE void XorWords(cyassl_word* r, const cyassl_word* a, word32 n) |
Vanger | 0:b86d15c6ba29 | 152 | { |
Vanger | 0:b86d15c6ba29 | 153 | word32 i; |
Vanger | 0:b86d15c6ba29 | 154 | |
Vanger | 0:b86d15c6ba29 | 155 | for (i = 0; i < n; i++) r[i] ^= a[i]; |
Vanger | 0:b86d15c6ba29 | 156 | } |
Vanger | 0:b86d15c6ba29 | 157 | |
Vanger | 0:b86d15c6ba29 | 158 | |
Vanger | 0:b86d15c6ba29 | 159 | STATIC INLINE void xorbuf(void* buf, const void* mask, word32 count) |
Vanger | 0:b86d15c6ba29 | 160 | { |
Vanger | 0:b86d15c6ba29 | 161 | if (((cyassl_word)buf | (cyassl_word)mask | count) % CYASSL_WORD_SIZE == 0) |
Vanger | 0:b86d15c6ba29 | 162 | XorWords( (cyassl_word*)buf, |
Vanger | 0:b86d15c6ba29 | 163 | (const cyassl_word*)mask, count / CYASSL_WORD_SIZE); |
Vanger | 0:b86d15c6ba29 | 164 | else { |
Vanger | 0:b86d15c6ba29 | 165 | word32 i; |
Vanger | 0:b86d15c6ba29 | 166 | byte* b = (byte*)buf; |
Vanger | 0:b86d15c6ba29 | 167 | const byte* m = (const byte*)mask; |
Vanger | 0:b86d15c6ba29 | 168 | |
Vanger | 0:b86d15c6ba29 | 169 | for (i = 0; i < count; i++) b[i] ^= m[i]; |
Vanger | 0:b86d15c6ba29 | 170 | } |
Vanger | 0:b86d15c6ba29 | 171 | } |
Vanger | 0:b86d15c6ba29 | 172 | #undef STATIC |
Vanger | 0:b86d15c6ba29 | 173 |