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