This is a port of cyaSSL 2.7.0.

Dependents:   CyaSSL_DTLS_Cellular CyaSSL_DTLS_Ethernet

Committer:
ashleymills
Date:
Thu Sep 05 15:55:50 2013 +0000
Revision:
1:c0ce1562443a
Parent:
0:714293de3836
Nothing;

Who changed what in which revision?

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