A simple CyaSSL-based HMAC-MD5 implementation. Licensed under GPL v2.

Dependents:   RFrec_full RFtrans_full

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