Todd Ouska / CyaSSL

Dependents:   cyassl-client Sync

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers misc.c Source File

misc.c

00001 /* misc.c
00002  *
00003  * Copyright (C) 2006-2009 Sawtooth Consulting Ltd.
00004  *
00005  * This file is part of CyaSSL.
00006  *
00007  * CyaSSL is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * CyaSSL is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
00020  */
00021 
00022 
00023 #include "misc.h"
00024 
00025 /* inlining these functions is a huge speed increase and a small size decrease, 
00026    because the functions are smaller than function call setup/cleanup, e.g.,
00027    md5 benchmark is twice as fast with inline.  If you don't want it, then
00028    define NO_INLINE and compile this file into cyassl, otherwise it's used as
00029    a source header
00030  */
00031 
00032 #ifdef NO_INLINE
00033     #define STATIC
00034 #else
00035     #define STATIC static
00036 #endif
00037 
00038 
00039 #ifdef INTEL_INTRINSICS
00040 
00041     #include <stdlib.h>      /* get intrinsic definitions */
00042 
00043     #pragma intrinsic(_lrotl, _lrotr)
00044 
00045     STATIC INLINE word32 rotlFixed(word32 x, word32 y)
00046     {
00047         return y ? _lrotl(x, y) : x;
00048     }
00049 
00050     STATIC INLINE word32 rotrFixed(word32 x, word32 y)
00051     {
00052         return y ? _lrotr(x, y) : x;
00053     }
00054 
00055 #else /* generic */
00056 
00057     STATIC INLINE word32 rotlFixed(word32 x, word32 y)
00058     {
00059         return (x << y) | (x >> (sizeof(y) * 8 - y));
00060     }   
00061 
00062 
00063     STATIC INLINE word32 rotrFixed(word32 x, word32 y)
00064     {
00065         return (x >> y) | (x << (sizeof(y) * 8 - y));
00066     }
00067 
00068 #endif
00069 
00070 
00071 STATIC INLINE word32 ByteReverseWord32(word32 value)
00072 {
00073 #ifdef PPC_INTRINSICS
00074     /* PPC: load reverse indexed instruction */
00075     return (word32)__lwbrx(&value,0);
00076 #elif defined(FAST_ROTATE)
00077     /* 5 instructions with rotate instruction, 9 without */
00078     return (rotrFixed(value, 8U) & 0xff00ff00) |
00079            (rotlFixed(value, 8U) & 0x00ff00ff);
00080 #else
00081     /* 6 instructions with rotate instruction, 8 without */
00082     value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
00083     return rotlFixed(value, 16U);
00084 #endif
00085 }
00086 
00087 
00088 STATIC INLINE void ByteReverseWords(word32* out, const word32* in,
00089                                     word32 byteCount)
00090 {
00091     word32 count = byteCount/sizeof(word32), i;
00092 
00093     for (i = 0; i < count; i++)
00094         out[i] = ByteReverseWord32(in[i]);
00095 
00096 }
00097 
00098 
00099 #ifdef WORD64_AVAILABLE
00100 
00101 
00102 STATIC INLINE word64 rotlFixed64(word64 x, word64 y)
00103 {
00104     return (x << y) | (x >> (sizeof(y) * 8 - y));
00105 }  
00106 
00107 
00108 STATIC INLINE word64 rotrFixed64(word64 x, word64 y)
00109 {
00110     return (x >> y) | (x << (sizeof(y) * 8 - y));
00111 }
00112 
00113 
00114 STATIC INLINE word64 ByteReverseWord64(word64 value)
00115 {
00116 #ifdef CTAOCRYPT_SLOW_WORD64
00117     return (word64)(ByteReverseWord32((word32)value)) << 32 | 
00118                     ByteReverseWord32((word32)(value>>32));
00119 #else
00120     value = ((value & W64LIT(0xFF00FF00FF00FF00)) >> 8) |
00121             ((value & W64LIT(0x00FF00FF00FF00FF)) << 8);
00122     value = ((value & W64LIT(0xFFFF0000FFFF0000)) >> 16) |
00123             ((value & W64LIT(0x0000FFFF0000FFFF)) << 16);
00124     return rotlFixed64(value, 32U);
00125 #endif
00126 }
00127 
00128 
00129 STATIC INLINE void ByteReverseWords64(word64* out, const word64* in,
00130                                       word32 byteCount)
00131 {
00132     word32 count = byteCount/sizeof(word64), i;
00133 
00134     for (i = 0; i < count; i++)
00135         out[i] = ByteReverseWord64(in[i]);
00136 
00137 }
00138 
00139 #endif /* WORD64_AVAILABLE */
00140 
00141 
00142 STATIC INLINE void ByteReverseBytes(byte* out, const byte* in, word32 byteCount)
00143 {
00144     word32* op       = (word32*)out;
00145     const word32* ip = (const word32*)in;
00146 
00147     ByteReverseWords(op, ip, byteCount);
00148 }
00149 
00150 
00151 STATIC INLINE void XorWords(word* r, const word* a, word32 n)
00152 {
00153     word32 i;
00154 
00155     for (i = 0; i < n; i++) r[i] ^= a[i];
00156 }
00157 
00158 
00159 STATIC INLINE void xorbuf(byte* buf, const byte* mask, word32 count)
00160 {
00161     if (((size_t)buf | (size_t)mask | count) % WORD_SIZE == 0)
00162         XorWords( (word*)buf, (const word*)mask, count / WORD_SIZE);
00163     else {
00164         word32 i;
00165         for (i = 0; i < count; i++) buf[i] ^= mask[i];
00166     }
00167 }
00168 
00169 
00170 #undef STATIC
00171