cya_u

Fork of CyaSSL-forEncrypt by Mobius IoT

Committer:
toddouska
Date:
Sat Feb 05 01:09:17 2011 +0000
Revision:
0:5045d2638c29
Beta Version

Who changed what in which revision?

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