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