Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

diamond.cpp

00001 // diamond.cpp - modified by Wei Dai from:
00002 
00003 /* diamond2.c - Encryption designed to exceed DES in security.
00004    This file and the Diamond2 and Diamond2 Lite Block Ciphers
00005    described herein are hereby dedicated to the Public Domain by the
00006    author and inventor, Michael Paul Johnson.  Feel free to use these
00007    for any purpose that is legally and morally right.  The names
00008    "Diamond2 Block Cipher" and "Diamond2 Lite Block Cipher" should only
00009    be used to describe the algorithms described in this file, to avoid
00010    confusion.
00011 
00012    Disclaimers:  the following comes with no warranty, expressed or
00013    implied.  You, the user, must determine the suitability of this
00014    information to your own uses.  You must also find out what legal
00015    requirements exist with respect to this data and programs using
00016    it, and comply with whatever valid requirements exist.
00017 */
00018 
00019 #include "pch.h"
00020 #include "diamond.h"
00021 #include "crc.h"
00022 
00023 NAMESPACE_BEGIN(CryptoPP)
00024 
00025 class Diamond2SboxMaker
00026 {
00027 public:
00028         Diamond2SboxMaker(const byte *external_key, unsigned int key_size,
00029                                          unsigned int rounds, bool lite);
00030 
00031         void MakeSbox(byte *sbox, CipherDir direction);
00032 
00033 private:
00034         unsigned int keyrand(unsigned int max_value, const byte *prevSbox);
00035         void makeonebox(byte *s, unsigned int i, unsigned int j);
00036 
00037         CRC32 crc;
00038         const byte *const key;
00039         const unsigned keysize;
00040         unsigned keyindex;
00041         const unsigned numrounds;
00042         const unsigned roundsize; // Number of bytes in one round of substitution boxes
00043         const unsigned blocksize;
00044 };
00045 
00046 Diamond2SboxMaker::Diamond2SboxMaker(const byte *external_key, unsigned int key_size, unsigned int rounds,
00047                                                                    bool lite)
00048         : key(external_key),
00049           keysize(key_size),
00050           keyindex(0),
00051           numrounds(rounds),
00052           roundsize(lite ? 2048 : 4096),
00053           blocksize(lite ? 8 : 16)
00054 {
00055         assert((rounds * blocksize) <= 255);
00056 }
00057 
00058 // Returns uniformly distributed pseudorandom value based on key[], sized keysize
00059 inline unsigned int Diamond2SboxMaker::keyrand(unsigned int max_value, const byte *prevSbox)
00060 {
00061         assert(max_value <= 255);
00062 
00063         if (!max_value) return 0;
00064 
00065         unsigned int mask, prandvalue, i;
00066 
00067         // Create a mask to get the minimum number of 
00068         // bits to cover the range 0 to max_value.
00069         for (i=max_value, mask=0; i > 0; i = i >> 1)
00070                 mask = (mask << 1) | 1;
00071 
00072         assert(i==0);
00073         do
00074         {
00075                 if (prevSbox)
00076                         crc.UpdateByte(prevSbox[key[keyindex++]]);
00077                 else
00078                         crc.UpdateByte(key[keyindex++]);
00079 
00080                 if (keyindex >= keysize)
00081                 {
00082                         keyindex = 0;   /* Recycle thru the key */
00083                         crc.UpdateByte(byte(keysize));
00084                         crc.UpdateByte(byte(keysize >> 8));
00085                 }
00086                 prandvalue = crc.GetCrcByte(0) & mask;
00087                 if ((++i>97) && (prandvalue > max_value))   /* Don't loop forever. */
00088                         prandvalue -= max_value;                /* Introduce negligible bias. */
00089         }
00090         while (prandvalue > max_value); /* Discard out of range values. */
00091         return prandvalue;
00092 }
00093 
00094 void Diamond2SboxMaker::makeonebox(byte *s, unsigned int i, unsigned int j)
00095 {
00096         bool filled[256];
00097         byte *sbox = s + (roundsize*i) + (256*j);
00098         byte *prevSbox = (i||j) ? sbox-256 : 0;
00099 
00100         unsigned m;
00101         for (m = 0; m < 256; m++)   /* The filled array is used to make sure that */
00102                 filled[m] = false;      /* each byte of the array is filled only once. */
00103         for (int n = 255; n >= 0 ; n--) /* n counts the number of bytes left to fill */
00104         {
00105                 // pos is the position among the UNFILLED
00106                 // components of the s array that the number n should be placed.
00107                 unsigned pos = keyrand(n, prevSbox);   
00108                 unsigned p=0;
00109                 while (filled[p]) p++;
00110                 for (m=0; m<pos; m++)
00111                 {
00112                         p++;
00113                         while (filled[p]) p++;
00114                 }
00115                 assert(p<256);
00116                 sbox[p] = n;
00117                 filled[p] = true;
00118         }
00119 }
00120 
00121 void Diamond2SboxMaker::MakeSbox(byte *s, CipherDir direction)
00122 {
00123         unsigned int i, j, k;
00124 
00125         for (i = 0; i < numrounds; i++)
00126                 for (j = 0; j < blocksize; j++)
00127                         makeonebox(s, i, j);
00128 
00129         if (direction==DECRYPTION)
00130         {
00131                 SecByteBlock si(numrounds * roundsize);
00132                 for (i = 0; i < numrounds; i++)
00133                         for (j = 0; j < blocksize; j++)
00134                                 for (k = 0; k < 256; k++)
00135                                         *(si + (roundsize * i) + (256 * j) + *(s + (roundsize * i) + (256 * j) + k)) = k;
00136                 memcpy(s, si, numrounds * roundsize);
00137         }
00138 }
00139 
00140 void Diamond2::Base::UncheckedSetKey(CipherDir direction, const byte *userKey, unsigned int length, unsigned int rounds)
00141 {
00142         AssertValidKeyLength(length);
00143 
00144         numrounds = rounds;
00145         s.New(numrounds * ROUNDSIZE);
00146 
00147         Diamond2SboxMaker m(userKey, length, rounds, false);
00148         m.MakeSbox(s, direction);
00149 }
00150 
00151 inline void Diamond2::Base::substitute(int round, byte *x, const byte *y) const
00152 {
00153         const byte *sbox = s + (ROUNDSIZE*round);
00154         x[0] = sbox[0*256+y[0]];
00155         x[1] = sbox[1*256+y[1]];
00156         x[2] = sbox[2*256+y[2]];
00157         x[3] = sbox[3*256+y[3]];
00158         x[4] = sbox[4*256+y[4]];
00159         x[5] = sbox[5*256+y[5]];
00160         x[6] = sbox[6*256+y[6]];
00161         x[7] = sbox[7*256+y[7]];
00162         x[8] = sbox[8*256+y[8]];
00163         x[9] = sbox[9*256+y[9]];
00164         x[10] = sbox[10*256+y[10]];
00165         x[11] = sbox[11*256+y[11]];
00166         x[12] = sbox[12*256+y[12]];
00167         x[13] = sbox[13*256+y[13]];
00168         x[14] = sbox[14*256+y[14]];
00169         x[15] = sbox[15*256+y[15]];
00170 }
00171 
00172 #ifdef DIAMOND_USE_PERMTABLE
00173 
00174 inline void Diamond2::Base::permute(byte *a)
00175 {
00176 #ifdef IS_LITTLE_ENDIAN
00177         word32 temp0     = (a[0] | (word32(a[10])<<24)) & 0x80000001;
00178 #else
00179         word32 temp0     = ((word32(a[0])<<24) | a[10]) & 0x01000080;
00180 #endif
00181                    temp0    |=                      permtable[0][a[1]] |
00182                                            permtable[1][a[2]] | permtable[2][a[3]] |
00183                                            permtable[3][a[4]] | permtable[4][a[5]] |
00184                                            permtable[5][a[6]] | permtable[6][a[7]] |
00185                                            permtable[7][a[8]] | permtable[8][a[9]];
00186 
00187 #ifdef IS_LITTLE_ENDIAN
00188         word32 temp1     = (a[4] | (word32(a[14])<<24)) & 0x80000001;
00189 #else
00190         word32 temp1     = ((word32(a[4])<<24) | a[14]) & 0x01000080;
00191 #endif
00192                    temp1    |=                      permtable[0][a[5]] |
00193                                            permtable[1][a[6]] | permtable[2][a[7]] |
00194                                            permtable[3][a[8]] | permtable[4][a[9]] |
00195                                            permtable[5][a[10]] | permtable[6][a[11]] |
00196                                            permtable[7][a[12]] | permtable[8][a[13]];
00197 
00198 #ifdef IS_LITTLE_ENDIAN
00199         word32 temp2     = (a[8] | (word32(a[2])<<24)) & 0x80000001;
00200 #else
00201         word32 temp2     = ((word32(a[8])<<24) | a[2]) & 0x01000080;
00202 #endif
00203                    temp2    |=                       permtable[0][a[9]] |
00204                                            permtable[1][a[10]] | permtable[2][a[11]] |
00205                                            permtable[3][a[12]] | permtable[4][a[13]] |
00206                                            permtable[5][a[14]] | permtable[6][a[15]] |
00207                                            permtable[7][a[0]] | permtable[8][a[1]];
00208 
00209 #ifdef IS_LITTLE_ENDIAN
00210         word32 temp3     = (a[12] | (word32(a[6])<<24)) & 0x80000001;
00211 #else
00212         word32 temp3     = ((word32(a[12])<<24) | a[6]) & 0x01000080;
00213 #endif
00214         ((word32 *)a)[3] = temp3 |               permtable[0][a[13]] |
00215                                            permtable[1][a[14]] | permtable[2][a[15]] |
00216                                            permtable[3][a[0]] | permtable[4][a[1]] |
00217                                            permtable[5][a[2]] | permtable[6][a[3]] |
00218                                            permtable[7][a[4]] | permtable[8][a[5]];
00219 
00220         ((word32 *)a)[0] = temp0;
00221         ((word32 *)a)[1] = temp1;
00222         ((word32 *)a)[2] = temp2;
00223 }
00224 
00225 inline void Diamond2::Base::ipermute(byte *a)
00226 {
00227 #ifdef IS_LITTLE_ENDIAN
00228         word32 temp0     = (a[9] | (word32(a[3])<<24)) & 0x01000080;
00229 #else
00230         word32 temp0     = ((word32(a[9])<<24) | a[3]) & 0x80000001;
00231 #endif
00232                    temp0    |=                      ipermtable[0][a[2]] |
00233                                            ipermtable[1][a[1]] | ipermtable[2][a[0]] |
00234                                            ipermtable[3][a[15]] | ipermtable[4][a[14]] |
00235                                            ipermtable[5][a[13]] | ipermtable[6][a[12]] |
00236                                            ipermtable[7][a[11]] | ipermtable[8][a[10]];
00237 
00238 #ifdef IS_LITTLE_ENDIAN
00239         word32 temp1     = (a[13] | (word32(a[7])<<24)) & 0x01000080;
00240 #else
00241         word32 temp1     = ((word32(a[13])<<24) | a[7]) & 0x80000001;
00242 #endif
00243                    temp1    |=                      ipermtable[0][a[6]] |
00244                                            ipermtable[1][a[5]] | ipermtable[2][a[4]] |
00245                                            ipermtable[3][a[3]] | ipermtable[4][a[2]] |
00246                                            ipermtable[5][a[1]] | ipermtable[6][a[0]] |
00247                                            ipermtable[7][a[15]] | ipermtable[8][a[14]];
00248 
00249 #ifdef IS_LITTLE_ENDIAN
00250         word32 temp2     = (a[1] | (word32(a[11])<<24)) & 0x01000080;
00251 #else
00252         word32 temp2     = ((word32(a[1])<<24) | a[11]) & 0x80000001;
00253 #endif
00254                    temp2    |=                      ipermtable[0][a[10]] |
00255                                            ipermtable[1][a[9]] | ipermtable[2][a[8]] |
00256                                            ipermtable[3][a[7]] | ipermtable[4][a[6]] |
00257                                            ipermtable[5][a[5]] | ipermtable[6][a[4]] |
00258                                            ipermtable[7][a[3]] | ipermtable[8][a[2]];
00259 
00260 #ifdef IS_LITTLE_ENDIAN
00261         word32 temp3     = (a[5] | (word32(a[15])<<24)) & 0x01000080;
00262 #else
00263         word32 temp3     = ((word32(a[5])<<24) | a[15]) & 0x80000001;
00264 #endif
00265         ((word32 *)a)[3] = temp3 |               ipermtable[0][a[14]] |
00266                                            ipermtable[1][a[13]] | ipermtable[2][a[12]] |
00267                                            ipermtable[3][a[11]] | ipermtable[4][a[10]] |
00268                                            ipermtable[5][a[9]] | ipermtable[6][a[8]] |
00269                                            ipermtable[7][a[7]] | ipermtable[8][a[6]];
00270 
00271         ((word32 *)a)[0] = temp0;
00272         ((word32 *)a)[1] = temp1;
00273         ((word32 *)a)[2] = temp2;
00274 }
00275 
00276 #else // DIAMOND_USE_PERMTABLE
00277 
00278 inline void Diamond2::Base::permute(byte *x)
00279 {
00280         byte y[16];
00281 
00282         y[0] = (x[0] & 1) | (x[1] & 2) | (x[2] & 4) |
00283                         (x[3] & 8) | (x[4] & 16) | (x[5] & 32) |
00284                         (x[6] & 64) | (x[7] & 128);
00285         y[1] = (x[1] & 1) | (x[2] & 2) | (x[3] & 4) |
00286                         (x[4] & 8) | (x[5] & 16) | (x[6] & 32) |
00287                         (x[7] & 64) | (x[8] & 128);
00288         y[2] = (x[2] & 1) | (x[3] & 2) | (x[4] & 4) |
00289                         (x[5] & 8) | (x[6] & 16) | (x[7] & 32) |
00290                         (x[8] & 64) | (x[9] & 128);
00291         y[3] = (x[3] & 1) | (x[4] & 2) | (x[5] & 4) |
00292                         (x[6] & 8) | (x[7] & 16) | (x[8] & 32) |
00293                         (x[9] & 64) | (x[10] & 128);
00294         y[4] = (x[4] & 1) | (x[5] & 2) | (x[6] & 4) |
00295                         (x[7] & 8) | (x[8] & 16) | (x[9] & 32) |
00296                         (x[10] & 64) | (x[11] & 128);
00297         y[5] = (x[5] & 1) | (x[6] & 2) | (x[7] & 4) |
00298                         (x[8] & 8) | (x[9] & 16) | (x[10] & 32) |
00299                         (x[11] & 64) | (x[12] & 128);
00300         y[6] = (x[6] & 1) | (x[7] & 2) | (x[8] & 4) |
00301                         (x[9] & 8) | (x[10] & 16) | (x[11] & 32) |
00302                         (x[12] & 64) | (x[13] & 128);
00303         y[7] = (x[7] & 1) | (x[8] & 2) | (x[9] & 4) |
00304                         (x[10] & 8) | (x[11] & 16) | (x[12] & 32) |
00305                         (x[13] & 64) | (x[14] & 128);
00306         y[8] = (x[8] & 1) | (x[9] & 2) | (x[10] & 4) |
00307                         (x[11] & 8) | (x[12] & 16) | (x[13] & 32) |
00308                         (x[14] & 64) | (x[15] & 128);
00309         y[9] = (x[9] & 1) | (x[10] & 2) | (x[11] & 4) |
00310                         (x[12] & 8) | (x[13] & 16) | (x[14] & 32) |
00311                         (x[15] & 64) | (x[0] & 128);
00312         y[10] = (x[10] & 1) | (x[11] & 2) | (x[12] & 4) |
00313                         (x[13] & 8) | (x[14] & 16) | (x[15] & 32) |
00314                         (x[0] & 64) | (x[1] & 128);
00315         y[11] = (x[11] & 1) | (x[12] & 2) | (x[13] & 4) |
00316                         (x[14] & 8) | (x[15] & 16) | (x[0] & 32) |
00317                         (x[1] & 64) | (x[2] & 128);
00318         y[12] = (x[12] & 1) | (x[13] & 2) | (x[14] & 4) |
00319                         (x[15] & 8) | (x[0] & 16) | (x[1] & 32) |
00320                         (x[2] & 64) | (x[3] & 128);
00321         y[13] = (x[13] & 1) | (x[14] & 2) | (x[15] & 4) |
00322                         (x[0] & 8) | (x[1] & 16) | (x[2] & 32) |
00323                         (x[3] & 64) | (x[4] & 128);
00324         y[14] = (x[14] & 1) | (x[15] & 2) | (x[0] & 4) |
00325                         (x[1] & 8) | (x[2] & 16) | (x[3] & 32) |
00326                         (x[4] & 64) | (x[5] & 128);
00327         y[15] = (x[15] & 1) | (x[0] & 2) | (x[1] & 4) |
00328                         (x[2] & 8) | (x[3] & 16) | (x[4] & 32) |
00329                         (x[5] & 64) | (x[6] & 128);
00330 
00331         memcpy(x, y, 16);
00332 }
00333 
00334 inline void Diamond2::Base::ipermute(byte *x)
00335 {
00336         byte y[16];
00337 
00338         y[0] = (x[0] & 1) | (x[15] & 2) | (x[14] & 4) |
00339                         (x[13] & 8) | (x[12] & 16) | (x[11] & 32) |
00340                         (x[10] & 64) | (x[9] & 128);
00341         y[1] = (x[1] & 1) | (x[0] & 2) | (x[15] & 4) |
00342                         (x[14] & 8) | (x[13] & 16) | (x[12] & 32) |
00343                         (x[11] & 64) | (x[10] & 128);
00344         y[2] = (x[2] & 1) | (x[1] & 2) | (x[0] & 4) |
00345                         (x[15] & 8) | (x[14] & 16) | (x[13] & 32) |
00346                         (x[12] & 64) | (x[11] & 128);
00347         y[3] = (x[3] & 1) | (x[2] & 2) | (x[1] & 4) |
00348                         (x[0] & 8) | (x[15] & 16) | (x[14] & 32) |
00349                         (x[13] & 64) | (x[12] & 128);
00350         y[4] = (x[4] & 1) | (x[3] & 2) | (x[2] & 4) |
00351                         (x[1] & 8) | (x[0] & 16) | (x[15] & 32) |
00352                         (x[14] & 64) | (x[13] & 128);
00353         y[5] = (x[5] & 1) | (x[4] & 2) | (x[3] & 4) |
00354                         (x[2] & 8) | (x[1] & 16) | (x[0] & 32) |
00355                         (x[15] & 64) | (x[14] & 128);
00356         y[6] = (x[6] & 1) | (x[5] & 2) | (x[4] & 4) |
00357                         (x[3] & 8) | (x[2] & 16) | (x[1] & 32) |
00358                         (x[0] & 64) | (x[15] & 128);
00359         y[7] = (x[7] & 1) | (x[6] & 2) | (x[5] & 4) |
00360                         (x[4] & 8) | (x[3] & 16) | (x[2] & 32) |
00361                         (x[1] & 64) | (x[0] & 128);
00362         y[8] = (x[8] & 1) | (x[7] & 2) | (x[6] & 4) |
00363                         (x[5] & 8) | (x[4] & 16) | (x[3] & 32) |
00364                         (x[2] & 64) | (x[1] & 128);
00365         y[9] = (x[9] & 1) | (x[8] & 2) | (x[7] & 4) |
00366                         (x[6] & 8) | (x[5] & 16) | (x[4] & 32) |
00367                         (x[3] & 64) | (x[2] & 128);
00368         y[10] = (x[10] & 1) | (x[9] & 2) | (x[8] & 4) |
00369                         (x[7] & 8) | (x[6] & 16) | (x[5] & 32) |
00370                         (x[4] & 64) | (x[3] & 128);
00371         y[11] = (x[11] & 1) | (x[10] & 2) | (x[9] & 4) |
00372                         (x[8] & 8) | (x[7] & 16) | (x[6] & 32) |
00373                         (x[5] & 64) | (x[4] & 128);
00374         y[12] = (x[12] & 1) | (x[11] & 2) | (x[10] & 4) |
00375                         (x[9] & 8) | (x[8] & 16) | (x[7] & 32) |
00376                         (x[6] & 64) | (x[5] & 128);
00377         y[13] = (x[13] & 1) | (x[12] & 2) | (x[11] & 4) |
00378                         (x[10] & 8) | (x[9] & 16) | (x[8] & 32) |
00379                         (x[7] & 64) | (x[6] & 128);
00380         y[14] = (x[14] & 1) | (x[13] & 2) | (x[12] & 4) |
00381                         (x[11] & 8) | (x[10] & 16) | (x[9] & 32) |
00382                         (x[8] & 64) | (x[7] & 128);
00383         y[15] = (x[15] & 1) | (x[14] & 2) | (x[13] & 4) |
00384                         (x[12] & 8) | (x[11] & 16) | (x[10] & 32) |
00385                         (x[9] & 64) | (x[8] & 128);
00386 
00387         memcpy(x, y, 16);
00388 }
00389 
00390 #endif // DIAMOND_USE_PERMTABLE
00391 
00392 void Diamond2::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
00393 {
00394         const byte *x = inBlock;
00395         byte y[16];
00396 
00397         substitute(0, y, x);
00398         for (int round=1; round < numrounds; round++)
00399         {
00400                 permute(y);
00401                 substitute(round, y, y);
00402         }
00403 
00404         if (xorBlock)
00405                 xorbuf(outBlock, xorBlock, y, BLOCKSIZE);
00406         else
00407                 memcpy(outBlock, y, BLOCKSIZE);
00408 }
00409 
00410 void Diamond2::Dec::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
00411 {
00412         const byte *x = inBlock;
00413         byte y[16];
00414 
00415         substitute(numrounds-1, y, x);
00416         for (int round=numrounds-2; round >= 0; round--)
00417         {
00418                 ipermute(y);
00419                 substitute(round, y, y);
00420         }
00421 
00422         if (xorBlock)
00423                 xorbuf(outBlock, xorBlock, y, BLOCKSIZE);
00424         else
00425                 memcpy(outBlock, y, BLOCKSIZE);
00426 }
00427 
00428 void Diamond2Lite::Base::UncheckedSetKey(CipherDir direction, const byte *userKey, unsigned int length, unsigned int rounds)
00429 {
00430         AssertValidKeyLength(length);
00431 
00432         numrounds = rounds;
00433         s.New(numrounds * ROUNDSIZE);
00434 
00435         Diamond2SboxMaker m(userKey, length, rounds, true);
00436         m.MakeSbox(s, direction);
00437 }
00438 
00439 inline void Diamond2Lite::Base::substitute(int round, byte *x, const byte *y) const
00440 {
00441         const byte *sbox = s + (ROUNDSIZE*round);
00442         x[0] = sbox[0*256+y[0]];
00443         x[1] = sbox[1*256+y[1]];
00444         x[2] = sbox[2*256+y[2]];
00445         x[3] = sbox[3*256+y[3]];
00446         x[4] = sbox[4*256+y[4]];
00447         x[5] = sbox[5*256+y[5]];
00448         x[6] = sbox[6*256+y[6]];
00449         x[7] = sbox[7*256+y[7]];
00450 }
00451 
00452 #ifdef DIAMOND_USE_PERMTABLE
00453 
00454 inline void Diamond2Lite::Base::permute(byte *a)
00455 {
00456         word32 temp      = permtable[0][a[0]] | permtable[1][a[1]] |
00457                                            permtable[2][a[2]] | permtable[3][a[3]] |
00458                                            permtable[4][a[4]] | permtable[5][a[5]] |
00459                                            permtable[6][a[6]] | permtable[7][a[7]];
00460 
00461         ((word32 *)a)[1] = permtable[0][a[4]] | permtable[1][a[5]] |
00462                                            permtable[2][a[6]] | permtable[3][a[7]] |
00463                                            permtable[4][a[0]] | permtable[5][a[1]] |
00464                                            permtable[6][a[2]] | permtable[7][a[3]];
00465 
00466         ((word32 *)a)[0] = temp;
00467 }
00468 
00469 inline void Diamond2Lite::Base::ipermute(byte *a)
00470 {
00471         word32 temp      = ipermtable[0][a[0]] | ipermtable[1][a[1]] |
00472                                            ipermtable[2][a[2]] | ipermtable[3][a[3]] |
00473                                            ipermtable[4][a[4]] | ipermtable[5][a[5]] |
00474                                            ipermtable[6][a[6]] | ipermtable[7][a[7]];
00475 
00476         ((word32 *)a)[1] = ipermtable[0][a[4]] | ipermtable[1][a[5]] |
00477                                            ipermtable[2][a[6]] | ipermtable[3][a[7]] |
00478                                            ipermtable[4][a[0]] | ipermtable[5][a[1]] |
00479                                            ipermtable[6][a[2]] | ipermtable[7][a[3]];
00480 
00481         ((word32 *)a)[0] = temp;
00482 }
00483 
00484 #else
00485 
00486 inline void Diamond2Lite::Base::permute(byte *a)
00487 {
00488         byte b[8];
00489 
00490         b[0] = (a[0] & 1) + (a[1] & 2) + (a[2] & 4) + (a[3] & 8) + (a[4] & 0x10) +
00491                 (a[5] & 0x20) + (a[6] & 0x40) + (a[7] & 0x80);
00492         b[1] = (a[1] & 1) + (a[2] & 2) + (a[3] & 4) + (a[4] & 8) + (a[5] & 0x10) +
00493                 (a[6] & 0x20) + (a[7] & 0x40) + (a[0] & 0x80);
00494         b[2] = (a[2] & 1) + (a[3] & 2) + (a[4] & 4) + (a[5] & 8) + (a[6] & 0x10) +
00495                 (a[7] & 0x20) + (a[0] & 0x40) + (a[1] & 0x80);
00496         b[3] = (a[3] & 1) + (a[4] & 2) + (a[5] & 4) + (a[6] & 8) + (a[7] & 0x10) +
00497                 (a[0] & 0x20) + (a[1] & 0x40) + (a[2] & 0x80);
00498         b[4] = (a[4] & 1) + (a[5] & 2) + (a[6] & 4) + (a[7] & 8) + (a[0] & 0x10) +
00499                 (a[1] & 0x20) + (a[2] & 0x40) + (a[3] & 0x80);
00500         b[5] = (a[5] & 1) + (a[6] & 2) + (a[7] & 4) + (a[0] & 8) + (a[1] & 0x10) +
00501                 (a[2] & 0x20) + (a[3] & 0x40) + (a[4] & 0x80);
00502         b[6] = (a[6] & 1) + (a[7] & 2) + (a[0] & 4) + (a[1] & 8) + (a[2] & 0x10) +
00503                 (a[3] & 0x20) + (a[4] & 0x40) + (a[5] & 0x80);
00504         b[7] = (a[7] & 1) + (a[0] & 2) + (a[1] & 4) + (a[2] & 8) + (a[3] & 0x10) +
00505                 (a[4] & 0x20) + (a[5] & 0x40) + (a[6] & 0x80);
00506 
00507         memcpy(a, b, 8);
00508 }
00509 
00510 inline void Diamond2Lite::Base::ipermute(byte *b)
00511 {
00512         byte a[8];
00513 
00514         a[0] = (b[0] & 1) + (b[7] & 2) + (b[6] & 4) + (b[5] & 8) + (b[4] & 0x10) +
00515                 (b[3] & 0x20) + (b[2] & 0x40) + (b[1] & 0x80);
00516         a[1] = (b[1] & 1) + (b[0] & 2) + (b[7] & 4) + (b[6] & 8) + (b[5] & 0x10) +
00517                 (b[4] & 0x20) + (b[3] & 0x40) + (b[2] & 0x80);
00518         a[2] = (b[2] & 1) + (b[1] & 2) + (b[0] & 4) + (b[7] & 8) + (b[6] & 0x10) +
00519                 (b[5] & 0x20) + (b[4] & 0x40) + (b[3] & 0x80);
00520         a[3] = (b[3] & 1) + (b[2] & 2) + (b[1] & 4) + (b[0] & 8) + (b[7] & 0x10) +
00521                 (b[6] & 0x20) + (b[5] & 0x40) + (b[4] & 0x80);
00522         a[4] = (b[4] & 1) + (b[3] & 2) + (b[2] & 4) + (b[1] & 8) + (b[0] & 0x10) +
00523                 (b[7] & 0x20) + (b[6] & 0x40) + (b[5] & 0x80);
00524         a[5] = (b[5] & 1) + (b[4] & 2) + (b[3] & 4) + (b[2] & 8) + (b[1] & 0x10) +
00525                 (b[0] & 0x20) + (b[7] & 0x40) + (b[6] & 0x80);
00526         a[6] = (b[6] & 1) + (b[5] & 2) + (b[4] & 4) + (b[3] & 8) + (b[2] & 0x10) +
00527                 (b[1] & 0x20) + (b[0] & 0x40) + (b[7] & 0x80);
00528         a[7] = (b[7] & 1) + (b[6] & 2) + (b[5] & 4) + (b[4] & 8) + (b[3] & 0x10) +
00529                 (b[2] & 0x20) + (b[1] & 0x40) + (b[0] & 0x80);
00530 
00531         memcpy(b, a, 8);
00532 }
00533 
00534 #endif // DIAMOND_USE_PERMTABLE
00535 
00536 void Diamond2Lite::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
00537 {
00538         const byte *x = inBlock;
00539         byte y[8];
00540 
00541         substitute(0, y, x);
00542         for (int round=1; round < numrounds; round++)
00543         {
00544                 permute(y);
00545                 substitute(round, y, y);
00546         }
00547 
00548         if (xorBlock)
00549                 xorbuf(outBlock, xorBlock, y, BLOCKSIZE);
00550         else
00551                 memcpy(outBlock, y, BLOCKSIZE);
00552 }
00553 
00554 void Diamond2Lite::Dec::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
00555 {
00556         const byte *x = inBlock;
00557         byte y[8];
00558 
00559         substitute(numrounds-1, y, x);
00560         for (int round=numrounds-2; round >= 0; round--)
00561         {
00562                 ipermute(y);
00563                 substitute(round, y, y);
00564         }
00565 
00566         if (xorBlock)
00567                 xorbuf(outBlock, xorBlock, y, BLOCKSIZE);
00568         else
00569                 memcpy(outBlock, y, BLOCKSIZE);
00570 }
00571 
00572 NAMESPACE_END

Generated on Mon Apr 19 18:12:29 2004 for Crypto++ by doxygen 1.3.6-20040222