MultiTech / CyaSSL

Dependents:   HTTPClient-SSL HTTPClient-SSL HTTPClient-SSL HTTPClient-SSL

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-2014 wolfSSL Inc.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
00020  */
00021 
00022 #ifdef HAVE_CONFIG_H
00023     #include <config.h>
00024 #endif
00025 
00026 #include <cyassl/ctaocrypt/settings.h>
00027 
00028 #include <cyassl/ctaocrypt/misc.h>
00029 
00030 /* inlining these functions is a huge speed increase and a small size decrease, 
00031    because the functions are smaller than function call setup/cleanup, e.g.,
00032    md5 benchmark is twice as fast with inline.  If you don't want it, then
00033    define NO_INLINE and compile this file into cyassl, otherwise it's used as
00034    a source header
00035  */
00036 
00037 #ifdef NO_INLINE
00038     #define STATIC
00039 #else
00040     #define STATIC static
00041 #endif
00042 
00043 
00044 #ifdef INTEL_INTRINSICS
00045 
00046     #include <stdlib.h>      /* get intrinsic definitions */
00047 
00048     /* for non visual studio probably need no long version, 32 bit only
00049      * i.e., _rotl and _rotr */
00050     #pragma intrinsic(_lrotl, _lrotr)
00051 
00052     STATIC INLINE word32 rotlFixed(word32 x, word32 y)
00053     {
00054         return y ? _lrotl(x, y) : x;
00055     }
00056 
00057     STATIC INLINE word32 rotrFixed(word32 x, word32 y)
00058     {
00059         return y ? _lrotr(x, y) : x;
00060     }
00061 
00062 #else /* generic */
00063 
00064     STATIC INLINE word32 rotlFixed(word32 x, word32 y)
00065     {
00066         return (x << y) | (x >> (sizeof(y) * 8 - y));
00067     }   
00068 
00069 
00070     STATIC INLINE word32 rotrFixed(word32 x, word32 y)
00071     {
00072         return (x >> y) | (x << (sizeof(y) * 8 - y));
00073     }
00074 
00075 #endif
00076 
00077 
00078 STATIC INLINE word32 ByteReverseWord32(word32 value)
00079 {
00080 #ifdef PPC_INTRINSICS
00081     /* PPC: load reverse indexed instruction */
00082     return (word32)__lwbrx(&value,0);
00083 #elif defined(KEIL_INTRINSICS)
00084     return (word32)__rev(value);
00085 #elif defined(FAST_ROTATE)
00086     /* 5 instructions with rotate instruction, 9 without */
00087     return (rotrFixed(value, 8U) & 0xff00ff00) |
00088            (rotlFixed(value, 8U) & 0x00ff00ff);
00089 #else
00090     /* 6 instructions with rotate instruction, 8 without */
00091     value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
00092     return rotlFixed(value, 16U);
00093 #endif
00094 }
00095 
00096 
00097 STATIC INLINE void ByteReverseWords(word32* out, const word32* in,
00098                                     word32 byteCount)
00099 {
00100     word32 count = byteCount/(word32)sizeof(word32), i;
00101 
00102     for (i = 0; i < count; i++)
00103         out[i] = ByteReverseWord32(in[i]);
00104 
00105 }
00106 
00107 
00108 #ifdef WORD64_AVAILABLE
00109 
00110 
00111 STATIC INLINE word64 rotlFixed64(word64 x, word64 y)
00112 {
00113     return (x << y) | (x >> (sizeof(y) * 8 - y));
00114 }  
00115 
00116 
00117 STATIC INLINE word64 rotrFixed64(word64 x, word64 y)
00118 {
00119     return (x >> y) | (x << (sizeof(y) * 8 - y));
00120 }
00121 
00122 
00123 STATIC INLINE word64 ByteReverseWord64(word64 value)
00124 {
00125 #ifdef CTAOCRYPT_SLOW_WORD64
00126     return (word64)(ByteReverseWord32((word32)value)) << 32 | 
00127                     ByteReverseWord32((word32)(value>>32));
00128 #else
00129     value = ((value & W64LIT(0xFF00FF00FF00FF00)) >> 8) |
00130             ((value & W64LIT(0x00FF00FF00FF00FF)) << 8);
00131     value = ((value & W64LIT(0xFFFF0000FFFF0000)) >> 16) |
00132             ((value & W64LIT(0x0000FFFF0000FFFF)) << 16);
00133     return rotlFixed64(value, 32U);
00134 #endif
00135 }
00136 
00137 
00138 STATIC INLINE void ByteReverseWords64(word64* out, const word64* in,
00139                                       word32 byteCount)
00140 {
00141     word32 count = byteCount/(word32)sizeof(word64), i;
00142 
00143     for (i = 0; i < count; i++)
00144         out[i] = ByteReverseWord64(in[i]);
00145 
00146 }
00147 
00148 #endif /* WORD64_AVAILABLE */
00149 
00150 
00151 STATIC INLINE void XorWords(cyassl_word* r, const cyassl_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(void* buf, const void* mask, word32 count)
00160 {
00161     if (((cyassl_word)buf | (cyassl_word)mask | count) % CYASSL_WORD_SIZE == 0)
00162         XorWords( (cyassl_word*)buf,
00163                   (const cyassl_word*)mask, count / CYASSL_WORD_SIZE);
00164     else {
00165         word32 i;
00166         byte*       b = (byte*)buf;
00167         const byte* m = (const byte*)mask;
00168 
00169         for (i = 0; i < count; i++) b[i] ^= m[i];
00170     }
00171 }
00172 #undef STATIC
00173