cyassl re-port with cellular comms, PSK test

Dependencies:   VodafoneUSBModem_bleedingedge2 mbed-rtos mbed-src

Committer:
ashleymills
Date:
Fri Apr 26 16:59:36 2013 +0000
Revision:
1:b211d97b0068
Parent:
0:e979170e02e7
nothing

Who changed what in which revision?

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