whirlpool/whirlpool_transform.c

Go to the documentation of this file.
00001 // Copyright (C) 2006-2007 Benedikt Böhm <hollow@gentoo.org>
00002 //
00003 // The Whirlpool algorithm was developed by
00004 //                Paulo S. L. M. Barreto <pbarreto@scopus.com.br> and
00005 //                Vincent Rijmen <vincent.rijmen@cryptomathic.com>
00006 //
00007 // This program is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2
00010 // of the License, or (at your option) any later version.
00011 //
00012 // This program 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 #include <stdint.h>
00022 
00023 #include "whirlpool.h"
00024 #include "whirlpool_tables.h"
00025 
00026 void whirlpool_transform(whirlpool_t * const context)
00027 {
00028         int i, r;
00029         uint64_t K[8];
00030         uint64_t block[8];
00031         uint64_t state[8];
00032         uint64_t L[8];
00033         uint8_t *buf = context->buf;
00034 
00035         /* map the buffer to a block */
00036         for (i = 0; i < 8; i++, buf += 8) {
00037                 block[i] = (((uint64_t)buf[0]        ) << 56) ^
00038                            (((uint64_t)buf[1] & 0xffL) << 48) ^
00039                            (((uint64_t)buf[2] & 0xffL) << 40) ^
00040                            (((uint64_t)buf[3] & 0xffL) << 32) ^
00041                            (((uint64_t)buf[4] & 0xffL) << 24) ^
00042                            (((uint64_t)buf[5] & 0xffL) << 16) ^
00043                            (((uint64_t)buf[6] & 0xffL) <<  8) ^
00044                            (((uint64_t)buf[7] & 0xffL)      );
00045         }
00046 
00047         /* compute and apply K^0 to the cipher state */
00048         state[0] = block[0] ^ (K[0] = context->hash[0]);
00049         state[1] = block[1] ^ (K[1] = context->hash[1]);
00050         state[2] = block[2] ^ (K[2] = context->hash[2]);
00051         state[3] = block[3] ^ (K[3] = context->hash[3]);
00052         state[4] = block[4] ^ (K[4] = context->hash[4]);
00053         state[5] = block[5] ^ (K[5] = context->hash[5]);
00054         state[6] = block[6] ^ (K[6] = context->hash[6]);
00055         state[7] = block[7] ^ (K[7] = context->hash[7]);
00056 
00057         /* iterate over all rounds */
00058         for (r = 1; r <= R; r++) {
00059                 /* compute K^r from K^{r-1} */
00060                 L[0] = C0[(int)(K[0] >> 56)       ] ^
00061                        C1[(int)(K[7] >> 48) & 0xff] ^
00062                        C2[(int)(K[6] >> 40) & 0xff] ^
00063                        C3[(int)(K[5] >> 32) & 0xff] ^
00064                        C4[(int)(K[4] >> 24) & 0xff] ^
00065                        C5[(int)(K[3] >> 16) & 0xff] ^
00066                        C6[(int)(K[2] >>  8) & 0xff] ^
00067                        C7[(int)(K[1]      ) & 0xff] ^
00068                        rc[r];
00069 
00070                 L[1] = C0[(int)(K[1] >> 56)       ] ^
00071                        C1[(int)(K[0] >> 48) & 0xff] ^
00072                        C2[(int)(K[7] >> 40) & 0xff] ^
00073                        C3[(int)(K[6] >> 32) & 0xff] ^
00074                        C4[(int)(K[5] >> 24) & 0xff] ^
00075                        C5[(int)(K[4] >> 16) & 0xff] ^
00076                        C6[(int)(K[3] >>  8) & 0xff] ^
00077                        C7[(int)(K[2]      ) & 0xff];
00078 
00079                 L[2] = C0[(int)(K[2] >> 56)       ] ^
00080                        C1[(int)(K[1] >> 48) & 0xff] ^
00081                        C2[(int)(K[0] >> 40) & 0xff] ^
00082                        C3[(int)(K[7] >> 32) & 0xff] ^
00083                        C4[(int)(K[6] >> 24) & 0xff] ^
00084                        C5[(int)(K[5] >> 16) & 0xff] ^
00085                        C6[(int)(K[4] >>  8) & 0xff] ^
00086                        C7[(int)(K[3]      ) & 0xff];
00087 
00088                 L[3] = C0[(int)(K[3] >> 56)       ] ^
00089                        C1[(int)(K[2] >> 48) & 0xff] ^
00090                        C2[(int)(K[1] >> 40) & 0xff] ^
00091                        C3[(int)(K[0] >> 32) & 0xff] ^
00092                        C4[(int)(K[7] >> 24) & 0xff] ^
00093                        C5[(int)(K[6] >> 16) & 0xff] ^
00094                        C6[(int)(K[5] >>  8) & 0xff] ^
00095                        C7[(int)(K[4]      ) & 0xff];
00096 
00097                 L[4] = C0[(int)(K[4] >> 56)       ] ^
00098                        C1[(int)(K[3] >> 48) & 0xff] ^
00099                        C2[(int)(K[2] >> 40) & 0xff] ^
00100                        C3[(int)(K[1] >> 32) & 0xff] ^
00101                        C4[(int)(K[0] >> 24) & 0xff] ^
00102                        C5[(int)(K[7] >> 16) & 0xff] ^
00103                        C6[(int)(K[6] >>  8) & 0xff] ^
00104                        C7[(int)(K[5]      ) & 0xff];
00105 
00106                 L[5] = C0[(int)(K[5] >> 56)       ] ^
00107                        C1[(int)(K[4] >> 48) & 0xff] ^
00108                        C2[(int)(K[3] >> 40) & 0xff] ^
00109                        C3[(int)(K[2] >> 32) & 0xff] ^
00110                        C4[(int)(K[1] >> 24) & 0xff] ^
00111                        C5[(int)(K[0] >> 16) & 0xff] ^
00112                        C6[(int)(K[7] >>  8) & 0xff] ^
00113                        C7[(int)(K[6]      ) & 0xff];
00114 
00115                 L[6] = C0[(int)(K[6] >> 56)       ] ^
00116                        C1[(int)(K[5] >> 48) & 0xff] ^
00117                        C2[(int)(K[4] >> 40) & 0xff] ^
00118                        C3[(int)(K[3] >> 32) & 0xff] ^
00119                        C4[(int)(K[2] >> 24) & 0xff] ^
00120                        C5[(int)(K[1] >> 16) & 0xff] ^
00121                        C6[(int)(K[0] >>  8) & 0xff] ^
00122                        C7[(int)(K[7]      ) & 0xff];
00123 
00124                 L[7] = C0[(int)(K[7] >> 56)       ] ^
00125                        C1[(int)(K[6] >> 48) & 0xff] ^
00126                        C2[(int)(K[5] >> 40) & 0xff] ^
00127                        C3[(int)(K[4] >> 32) & 0xff] ^
00128                        C4[(int)(K[3] >> 24) & 0xff] ^
00129                        C5[(int)(K[2] >> 16) & 0xff] ^
00130                        C6[(int)(K[1] >>  8) & 0xff] ^
00131                        C7[(int)(K[0]      ) & 0xff];
00132 
00133                 K[0] = L[0];
00134                 K[1] = L[1];
00135                 K[2] = L[2];
00136                 K[3] = L[3];
00137                 K[4] = L[4];
00138                 K[5] = L[5];
00139                 K[6] = L[6];
00140                 K[7] = L[7];
00141 
00142                 /* apply the r-th round transformation */
00143                 L[0] = C0[(int)(state[0] >> 56)       ] ^
00144                        C1[(int)(state[7] >> 48) & 0xff] ^
00145                        C2[(int)(state[6] >> 40) & 0xff] ^
00146                        C3[(int)(state[5] >> 32) & 0xff] ^
00147                        C4[(int)(state[4] >> 24) & 0xff] ^
00148                        C5[(int)(state[3] >> 16) & 0xff] ^
00149                        C6[(int)(state[2] >>  8) & 0xff] ^
00150                        C7[(int)(state[1]      ) & 0xff] ^
00151                        K[0];
00152 
00153                 L[1] = C0[(int)(state[1] >> 56)       ] ^
00154                        C1[(int)(state[0] >> 48) & 0xff] ^
00155                        C2[(int)(state[7] >> 40) & 0xff] ^
00156                        C3[(int)(state[6] >> 32) & 0xff] ^
00157                        C4[(int)(state[5] >> 24) & 0xff] ^
00158                        C5[(int)(state[4] >> 16) & 0xff] ^
00159                        C6[(int)(state[3] >>  8) & 0xff] ^
00160                        C7[(int)(state[2]      ) & 0xff] ^
00161                        K[1];
00162 
00163                 L[2] = C0[(int)(state[2] >> 56)       ] ^
00164                        C1[(int)(state[1] >> 48) & 0xff] ^
00165                        C2[(int)(state[0] >> 40) & 0xff] ^
00166                        C3[(int)(state[7] >> 32) & 0xff] ^
00167                        C4[(int)(state[6] >> 24) & 0xff] ^
00168                        C5[(int)(state[5] >> 16) & 0xff] ^
00169                        C6[(int)(state[4] >>  8) & 0xff] ^
00170                        C7[(int)(state[3]      ) & 0xff] ^
00171                        K[2];
00172 
00173                 L[3] = C0[(int)(state[3] >> 56)       ] ^
00174                        C1[(int)(state[2] >> 48) & 0xff] ^
00175                        C2[(int)(state[1] >> 40) & 0xff] ^
00176                        C3[(int)(state[0] >> 32) & 0xff] ^
00177                        C4[(int)(state[7] >> 24) & 0xff] ^
00178                        C5[(int)(state[6] >> 16) & 0xff] ^
00179                        C6[(int)(state[5] >>  8) & 0xff] ^
00180                        C7[(int)(state[4]      ) & 0xff] ^
00181                        K[3];
00182 
00183                 L[4] = C0[(int)(state[4] >> 56)       ] ^
00184                        C1[(int)(state[3] >> 48) & 0xff] ^
00185                        C2[(int)(state[2] >> 40) & 0xff] ^
00186                        C3[(int)(state[1] >> 32) & 0xff] ^
00187                        C4[(int)(state[0] >> 24) & 0xff] ^
00188                        C5[(int)(state[7] >> 16) & 0xff] ^
00189                        C6[(int)(state[6] >>  8) & 0xff] ^
00190                        C7[(int)(state[5]      ) & 0xff] ^
00191                        K[4];
00192 
00193                 L[5] = C0[(int)(state[5] >> 56)       ] ^
00194                        C1[(int)(state[4] >> 48) & 0xff] ^
00195                        C2[(int)(state[3] >> 40) & 0xff] ^
00196                        C3[(int)(state[2] >> 32) & 0xff] ^
00197                        C4[(int)(state[1] >> 24) & 0xff] ^
00198                        C5[(int)(state[0] >> 16) & 0xff] ^
00199                        C6[(int)(state[7] >>  8) & 0xff] ^
00200                        C7[(int)(state[6]      ) & 0xff] ^
00201                        K[5];
00202 
00203                 L[6] = C0[(int)(state[6] >> 56)       ] ^
00204                        C1[(int)(state[5] >> 48) & 0xff] ^
00205                        C2[(int)(state[4] >> 40) & 0xff] ^
00206                        C3[(int)(state[3] >> 32) & 0xff] ^
00207                        C4[(int)(state[2] >> 24) & 0xff] ^
00208                        C5[(int)(state[1] >> 16) & 0xff] ^
00209                        C6[(int)(state[0] >>  8) & 0xff] ^
00210                        C7[(int)(state[7]      ) & 0xff] ^
00211                        K[6];
00212 
00213                 L[7] = C0[(int)(state[7] >> 56)       ] ^
00214                        C1[(int)(state[6] >> 48) & 0xff] ^
00215                        C2[(int)(state[5] >> 40) & 0xff] ^
00216                        C3[(int)(state[4] >> 32) & 0xff] ^
00217                        C4[(int)(state[3] >> 24) & 0xff] ^
00218                        C5[(int)(state[2] >> 16) & 0xff] ^
00219                        C6[(int)(state[1] >>  8) & 0xff] ^
00220                        C7[(int)(state[0]      ) & 0xff] ^
00221                        K[7];
00222 
00223                 state[0] = L[0];
00224                 state[1] = L[1];
00225                 state[2] = L[2];
00226                 state[3] = L[3];
00227                 state[4] = L[4];
00228                 state[5] = L[5];
00229                 state[6] = L[6];
00230                 state[7] = L[7];
00231         }
00232 
00233         /* apply the Miyaguchi-Preneel compression function */
00234         context->hash[0] ^= state[0] ^ block[0];
00235         context->hash[1] ^= state[1] ^ block[1];
00236         context->hash[2] ^= state[2] ^ block[2];
00237         context->hash[3] ^= state[3] ^ block[3];
00238         context->hash[4] ^= state[4] ^ block[4];
00239         context->hash[5] ^= state[5] ^ block[5];
00240         context->hash[6] ^= state[6] ^ block[6];
00241         context->hash[7] ^= state[7] ^ block[7];
00242 }

Generated on Tue Jun 19 20:38:27 2007 for lucid by  doxygen 1.5.2