Wiselib
wiselib.testing/algorithms/crypto/diffie_hellman_lite/sha256.h
Go to the documentation of this file.
00001 /***************************************************************************
00002  ** This file is part of the generic algorithm library Wiselib.           **
00003  ** Copyright (C) 2008,2009 by the Wisebed (www.wisebed.eu) project.      **
00004  **                                                                       **
00005  ** The Wiselib is free software: you can redistribute it and/or modify   **
00006  ** it under the terms of the GNU Lesser General Public License as        **
00007  ** published by the Free Software Foundation, either version 3 of the    **
00008  ** License, or (at your option) any later version.                       **
00009  **                                                                       **
00010  ** The Wiselib is distributed in the hope that it will be useful,        **
00011  ** but WITHOUT ANY WARRANTY; without even the implied warranty of        **
00012  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         **
00013  ** GNU Lesser General Public License for more details.                   **
00014  **                                                                       **
00015  ** You should have received a copy of the GNU Lesser General Public      **
00016  ** License along with the Wiselib.                                       **
00017  ** If not, see <http://www.gnu.org/licenses/>.                           **
00018  **                                                                       **
00019  ** Original Author: Christophe Devine                                    **
00020  ** Source: http://www.spale.com/download/scrypt/scrypt1.0/               **
00021  **                                                                       **
00022  ** Ported by: Christoph Knecht, University of Bern 2010                  **
00023  ***************************************************************************/
00024 #ifndef SHA256_H
00025 #define SHA256_H
00026 
00027 #define GET_UINT32(n,b,i)                       \
00028 {                                               \
00029     (n) = ( (uint32_t) (b)[(i)    ] << 24 )       \
00030         | ( (uint32_t) (b)[(i) + 1] << 16 )       \
00031         | ( (uint32_t) (b)[(i) + 2] <<  8 )       \
00032         | ( (uint32_t) (b)[(i) + 3]       );      \
00033 }
00034 
00035 #define PUT_UINT32(n,b,i)                       \
00036 {                                               \
00037     (b)[(i)    ] = (uint8_t) ( (n) >> 24 );       \
00038     (b)[(i) + 1] = (uint8_t) ( (n) >> 16 );       \
00039     (b)[(i) + 2] = (uint8_t) ( (n) >>  8 );       \
00040     (b)[(i) + 3] = (uint8_t) ( (n)       );       \
00041 }
00042 
00043 namespace wiselib
00044 {
00045    typedef struct
00046    {
00047       uint32_t total[2];
00048       uint32_t state[8];
00049       uint8_t buffer[64];
00050    }
00051    sha256_context;
00052 
00053    class Sha256
00054    {
00055    public:
00056       inline void reset();
00057       inline void update( uint8_t *input, uint32_t );
00058       inline void finish( uint8_t[32] );
00059 
00060    private:
00061       inline void process( uint8_t[64] );
00062       uint8_t padding[64];
00063       sha256_context ctx_;
00064    };
00065    // --------------------------------------------------------------------
00066    // --------------------------------------------------------------------
00067    // --------------------------------------------------------------------
00068    void
00069    Sha256::
00070    reset()
00071    {
00072       memset(padding, 0x0, 64);
00073       padding[0] = 0x80;
00074       ctx_.total[0] = 0;
00075       ctx_.total[1] = 0;
00076 
00077       ctx_.state[0] = 0x6A09E667;
00078       ctx_.state[1] = 0xBB67AE85;
00079       ctx_.state[2] = 0x3C6EF372;
00080       ctx_.state[3] = 0xA54FF53A;
00081       ctx_.state[4] = 0x510E527F;
00082       ctx_.state[5] = 0x9B05688C;
00083       ctx_.state[6] = 0x1F83D9AB;
00084       ctx_.state[7] = 0x5BE0CD19;
00085    }
00086    // --------------------------------------------------------------------
00087    void
00088    Sha256::
00089    process( uint8_t data[64] )
00090    {
00091       uint32_t temp1, temp2, W[64];
00092       uint32_t A, B, C, D, E, F, G, H;
00093 
00094       GET_UINT32( W[0],  data,  0 );
00095       GET_UINT32( W[1],  data,  4 );
00096       GET_UINT32( W[2],  data,  8 );
00097       GET_UINT32( W[3],  data, 12 );
00098       GET_UINT32( W[4],  data, 16 );
00099       GET_UINT32( W[5],  data, 20 );
00100       GET_UINT32( W[6],  data, 24 );
00101       GET_UINT32( W[7],  data, 28 );
00102       GET_UINT32( W[8],  data, 32 );
00103       GET_UINT32( W[9],  data, 36 );
00104       GET_UINT32( W[10], data, 40 );
00105       GET_UINT32( W[11], data, 44 );
00106       GET_UINT32( W[12], data, 48 );
00107       GET_UINT32( W[13], data, 52 );
00108       GET_UINT32( W[14], data, 56 );
00109       GET_UINT32( W[15], data, 60 );
00110 
00111       #define  SHR(x,n) ((x & 0xFFFFFFFF) >> n)
00112       #define ROTR(x,n) (SHR(x,n) | (x << (32 - n)))
00113 
00114       #define S0(x) (ROTR(x, 7) ^ ROTR(x,18) ^  SHR(x, 3))
00115       #define S1(x) (ROTR(x,17) ^ ROTR(x,19) ^  SHR(x,10))
00116 
00117       #define S2(x) (ROTR(x, 2) ^ ROTR(x,13) ^ ROTR(x,22))
00118       #define S3(x) (ROTR(x, 6) ^ ROTR(x,11) ^ ROTR(x,25))
00119 
00120       #define F0(x,y,z) ((x & y) | (z & (x | y)))
00121       #define F1(x,y,z) (z ^ (x & (y ^ z)))
00122 
00123       #define R(t)                                    \
00124       (                                               \
00125          W[t] = S1(W[t -  2]) + W[t -  7] +          \
00126                 S0(W[t - 15]) + W[t - 16]            \
00127       )
00128 
00129       #define P(a,b,c,d,e,f,g,h,x,K)                  \
00130       {                                               \
00131          temp1 = h + S3(e) + F1(e,f,g) + K + x;      \
00132          temp2 = S2(a) + F0(a,b,c);                  \
00133          d += temp1; h = temp1 + temp2;              \
00134       }
00135 
00136       A = ctx_.state[0];
00137       B = ctx_.state[1];
00138       C = ctx_.state[2];
00139       D = ctx_.state[3];
00140       E = ctx_.state[4];
00141       F = ctx_.state[5];
00142       G = ctx_.state[6];
00143       H = ctx_.state[7];
00144 
00145       P( A, B, C, D, E, F, G, H, W[ 0], 0x428A2F98 );
00146       P( H, A, B, C, D, E, F, G, W[ 1], 0x71374491 );
00147       P( G, H, A, B, C, D, E, F, W[ 2], 0xB5C0FBCF );
00148       P( F, G, H, A, B, C, D, E, W[ 3], 0xE9B5DBA5 );
00149       P( E, F, G, H, A, B, C, D, W[ 4], 0x3956C25B );
00150       P( D, E, F, G, H, A, B, C, W[ 5], 0x59F111F1 );
00151       P( C, D, E, F, G, H, A, B, W[ 6], 0x923F82A4 );
00152       P( B, C, D, E, F, G, H, A, W[ 7], 0xAB1C5ED5 );
00153       P( A, B, C, D, E, F, G, H, W[ 8], 0xD807AA98 );
00154       P( H, A, B, C, D, E, F, G, W[ 9], 0x12835B01 );
00155       P( G, H, A, B, C, D, E, F, W[10], 0x243185BE );
00156       P( F, G, H, A, B, C, D, E, W[11], 0x550C7DC3 );
00157       P( E, F, G, H, A, B, C, D, W[12], 0x72BE5D74 );
00158       P( D, E, F, G, H, A, B, C, W[13], 0x80DEB1FE );
00159       P( C, D, E, F, G, H, A, B, W[14], 0x9BDC06A7 );
00160       P( B, C, D, E, F, G, H, A, W[15], 0xC19BF174 );
00161       P( A, B, C, D, E, F, G, H, R(16), 0xE49B69C1 );
00162       P( H, A, B, C, D, E, F, G, R(17), 0xEFBE4786 );
00163       P( G, H, A, B, C, D, E, F, R(18), 0x0FC19DC6 );
00164       P( F, G, H, A, B, C, D, E, R(19), 0x240CA1CC );
00165       P( E, F, G, H, A, B, C, D, R(20), 0x2DE92C6F );
00166       P( D, E, F, G, H, A, B, C, R(21), 0x4A7484AA );
00167       P( C, D, E, F, G, H, A, B, R(22), 0x5CB0A9DC );
00168       P( B, C, D, E, F, G, H, A, R(23), 0x76F988DA );
00169       P( A, B, C, D, E, F, G, H, R(24), 0x983E5152 );
00170       P( H, A, B, C, D, E, F, G, R(25), 0xA831C66D );
00171       P( G, H, A, B, C, D, E, F, R(26), 0xB00327C8 );
00172       P( F, G, H, A, B, C, D, E, R(27), 0xBF597FC7 );
00173       P( E, F, G, H, A, B, C, D, R(28), 0xC6E00BF3 );
00174       P( D, E, F, G, H, A, B, C, R(29), 0xD5A79147 );
00175       P( C, D, E, F, G, H, A, B, R(30), 0x06CA6351 );
00176       P( B, C, D, E, F, G, H, A, R(31), 0x14292967 );
00177       P( A, B, C, D, E, F, G, H, R(32), 0x27B70A85 );
00178       P( H, A, B, C, D, E, F, G, R(33), 0x2E1B2138 );
00179       P( G, H, A, B, C, D, E, F, R(34), 0x4D2C6DFC );
00180       P( F, G, H, A, B, C, D, E, R(35), 0x53380D13 );
00181       P( E, F, G, H, A, B, C, D, R(36), 0x650A7354 );
00182       P( D, E, F, G, H, A, B, C, R(37), 0x766A0ABB );
00183       P( C, D, E, F, G, H, A, B, R(38), 0x81C2C92E );
00184       P( B, C, D, E, F, G, H, A, R(39), 0x92722C85 );
00185       P( A, B, C, D, E, F, G, H, R(40), 0xA2BFE8A1 );
00186       P( H, A, B, C, D, E, F, G, R(41), 0xA81A664B );
00187       P( G, H, A, B, C, D, E, F, R(42), 0xC24B8B70 );
00188       P( F, G, H, A, B, C, D, E, R(43), 0xC76C51A3 );
00189       P( E, F, G, H, A, B, C, D, R(44), 0xD192E819 );
00190       P( D, E, F, G, H, A, B, C, R(45), 0xD6990624 );
00191       P( C, D, E, F, G, H, A, B, R(46), 0xF40E3585 );
00192       P( B, C, D, E, F, G, H, A, R(47), 0x106AA070 );
00193       P( A, B, C, D, E, F, G, H, R(48), 0x19A4C116 );
00194       P( H, A, B, C, D, E, F, G, R(49), 0x1E376C08 );
00195       P( G, H, A, B, C, D, E, F, R(50), 0x2748774C );
00196       P( F, G, H, A, B, C, D, E, R(51), 0x34B0BCB5 );
00197       P( E, F, G, H, A, B, C, D, R(52), 0x391C0CB3 );
00198       P( D, E, F, G, H, A, B, C, R(53), 0x4ED8AA4A );
00199       P( C, D, E, F, G, H, A, B, R(54), 0x5B9CCA4F );
00200       P( B, C, D, E, F, G, H, A, R(55), 0x682E6FF3 );
00201       P( A, B, C, D, E, F, G, H, R(56), 0x748F82EE );
00202       P( H, A, B, C, D, E, F, G, R(57), 0x78A5636F );
00203       P( G, H, A, B, C, D, E, F, R(58), 0x84C87814 );
00204       P( F, G, H, A, B, C, D, E, R(59), 0x8CC70208 );
00205       P( E, F, G, H, A, B, C, D, R(60), 0x90BEFFFA );
00206       P( D, E, F, G, H, A, B, C, R(61), 0xA4506CEB );
00207       P( C, D, E, F, G, H, A, B, R(62), 0xBEF9A3F7 );
00208       P( B, C, D, E, F, G, H, A, R(63), 0xC67178F2 );
00209 
00210       ctx_.state[0] += A;
00211       ctx_.state[1] += B;
00212       ctx_.state[2] += C;
00213       ctx_.state[3] += D;
00214       ctx_.state[4] += E;
00215       ctx_.state[5] += F;
00216       ctx_.state[6] += G;
00217       ctx_.state[7] += H;
00218    }
00219    // --------------------------------------------------------------------
00220    void
00221    Sha256::
00222    update( uint8_t *input, uint32_t length )
00223    {
00224       uint32_t left, fill;
00225 
00226       if( ! length )
00227          return;
00228 
00229       left = ctx_.total[0] & 0x3F;
00230       fill = 64 - left;
00231 
00232       ctx_.total[0] += length;
00233       ctx_.total[0] &= 0xFFFFFFFF;
00234 
00235       if( ctx_.total[0] < length )
00236          ctx_.total[1]++;
00237 
00238       if( left && length >= fill )
00239       {
00240          memcpy( (void *) (ctx_.buffer + left), (void *) input, fill );
00241          process( ctx_.buffer );
00242          length -= fill;
00243          input  += fill;
00244          left = 0;
00245       }
00246 
00247       while( length >= 64 )
00248       {
00249          process( input );
00250          length -= 64;
00251          input  += 64;
00252       }
00253 
00254       if( length )
00255       {
00256          memcpy( (void *) (ctx_.buffer + left), (void *) input, length );
00257       }
00258    }
00259    // --------------------------------------------------------------------
00260    void
00261    Sha256::
00262    finish( uint8_t digest[32] )
00263    {
00264       uint32_t last, padn;
00265       uint32_t high, low;
00266       uint8_t msglen[8];
00267 
00268       high = ( ctx_.total[0] >> 29 )
00269          | ( ctx_.total[1] <<  3 );
00270       low  = ( ctx_.total[0] <<  3 );
00271 
00272       PUT_UINT32( high, msglen, 0 );
00273       PUT_UINT32( low,  msglen, 4 );
00274 
00275       last = ctx_.total[0] & 0x3F;
00276       padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
00277 
00278       update( padding, padn );
00279       update( msglen, 8 );
00280 
00281       PUT_UINT32( ctx_.state[0], digest,  0 );
00282       PUT_UINT32( ctx_.state[1], digest,  4 );
00283       PUT_UINT32( ctx_.state[2], digest,  8 );
00284       PUT_UINT32( ctx_.state[3], digest, 12 );
00285       PUT_UINT32( ctx_.state[4], digest, 16 );
00286       PUT_UINT32( ctx_.state[5], digest, 20 );
00287       PUT_UINT32( ctx_.state[6], digest, 24 );
00288       PUT_UINT32( ctx_.state[7], digest, 28 );
00289    }
00290 }
00291 
00292 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines