Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

mars.cpp

00001 // mars.cpp - modified by Sean Woods from Brian Gladman's mars6.c for Crypto++
00002 // key setup updated by Wei Dai to reflect IBM's "tweak" proposed in August 1999
00003 
00004 /* This is an independent implementation of the MARS encryption         */
00005 /* algorithm designed by a team at IBM as a candidate for the US        */
00006 /* NIST Advanced Encryption Standard (AES) effort. The algorithm        */
00007 /* is subject to Patent action by IBM, who intend to offer royalty      */
00008 /* free use if a Patent is granted.                                     */
00009 /*                                                                      */
00010 /* Copyright in this implementation is held by Dr B R Gladman but       */
00011 /* I hereby give permission for its free direct or derivative use       */
00012 /* subject to acknowledgment of its origin and compliance with any      */
00013 /* constraints that IBM place on the use of the MARS algorithm.         */
00014 /*                                                                      */
00015 /* Dr Brian Gladman (gladman@seven77.demon.co.uk) 4th October 1998      */
00016 
00017 #include "pch.h"
00018 #include "mars.h"
00019 
00020 NAMESPACE_BEGIN(CryptoPP)
00021 
00022 ANONYMOUS_NAMESPACE_BEGIN
00023 static word32 gen_mask(word32 x)
00024 {
00025         word32  m;
00026 
00027         m = (~x ^ (x >> 1)) & 0x7fffffff;
00028         m &= (m >> 1) & (m >> 2); m &= (m >> 3) & (m >> 6); 
00029         
00030         if(!m)
00031                 return 0;
00032         
00033         m <<= 1; m |= (m << 1); m |= (m << 2); m |= (m << 4);
00034         m |= (m << 1) & ~x & 0x80000000;
00035 
00036         return m & 0xfffffffc;
00037 };
00038 NAMESPACE_END
00039 
00040 MARS::MARS(const byte *userKey, unsigned int keylen)
00041         : EK(40)
00042 {
00043         assert(keylen == KeyLength(keylen));
00044 
00045         // Initialize T[] with the key data
00046         SecBlock<word32> T(15);
00047         GetUserKeyLittleEndian(T.ptr, 15, userKey, keylen);
00048         assert(keylen%4==0 && keylen/4 < 15);
00049         T[keylen/4] = keylen/4;
00050 
00051         for (unsigned int j=0; j<4; j++)        // compute 10 words of K[] in each iteration
00052         {
00053                 unsigned int i;
00054                 // Do linear transformation
00055                 for (i=0; i<15; i++)
00056                         T[i] = T[i] ^ rotlFixed(T[(i+8)%15] ^ T[(i+13)%15], 3) ^ (4*i+j);
00057 
00058                 // Do four rounds of stirring
00059                 for (unsigned int k=0; k<4; k++)
00060                         for (i=0; i<15; i++)
00061                            T[i] = rotlFixed(T[i] + Sbox[T[(i+14)%15]%512], 9);
00062 
00063                 // Store next 10 key words into K[]
00064                 for (i=0; i<10; i++)
00065                         EK[10*j+i] = T[4*i%15];
00066         }
00067 
00068         // Modify multiplication key-words
00069         for(unsigned int i = 5; i < 37; i += 2)
00070         {
00071                 word32 w = EK[i] | 3;
00072                 word32 m = gen_mask(w);
00073                 if(m)
00074                         w ^= (rotlMod(Sbox[265 + (EK[i] & 3)], EK[i-1]) & m);
00075                 EK[i] = w;
00076         }
00077 }
00078 
00079 #define f_mix(a,b,c,d)                                  \
00080                 r = rotrFixed(a, 8);                            \
00081                 b ^= Sbox[a & 255];                             \
00082                 b += Sbox[(r & 255) + 256];             \
00083                 r = rotrFixed(a, 16);                           \
00084                 a  = rotrFixed(a, 24);                          \
00085                 c += Sbox[r & 255];                             \
00086                 d ^= Sbox[(a & 255) + 256]
00087 
00088 #define b_mix(a,b,c,d)                                  \
00089                 r = rotlFixed(a, 8);                            \
00090                 b ^= Sbox[(a & 255) + 256];             \
00091                 c -= Sbox[r & 255];                             \
00092                 r = rotlFixed(a, 16);                           \
00093                 a  = rotlFixed(a, 24);                          \
00094                 d -= Sbox[(r & 255) + 256];             \
00095                 d ^= Sbox[a & 255]
00096 
00097 #define f_ktr(a,b,c,d,i)        \
00098         m = a + EK[i];                  \
00099         a = rotlFixed(a, 13);           \
00100         r = a * EK[i + 1];              \
00101         l = Sbox[m & 511];              \
00102         r = rotlFixed(r, 5);            \
00103         l ^= r;                                 \
00104         c += rotlMod(m, r);             \
00105         r = rotlFixed(r, 5);            \
00106         l ^= r;                                 \
00107         d ^= r;                                 \
00108         b += rotlMod(l, r)
00109 
00110 #define r_ktr(a,b,c,d,i)        \
00111         r = a * EK[i + 1];              \
00112         a = rotrFixed(a, 13);           \
00113         m = a + EK[i];                  \
00114         l = Sbox[m & 511];              \
00115         r = rotlFixed(r, 5);            \
00116         l ^= r;                                 \
00117         c -= rotlMod(m, r);             \
00118         r = rotlFixed(r, 5);            \
00119         l ^= r;                                 \
00120         d ^= r;                                 \
00121         b -= rotlMod(l, r)
00122 
00123 void MARSEncryption::ProcessBlock(const byte *inBlock, byte *outBlock) const
00124 {
00125         word32 a, b, c, d, l, m, r;
00126         
00127         GetBlockLittleEndian(inBlock,a,b,c,d);
00128 
00129         a += EK[0];
00130         b += EK[1];
00131         c += EK[2];
00132         d += EK[3];
00133 
00134         int i;
00135         for (i = 0; i < 2; i++) {
00136                 f_mix(a,b,c,d);
00137                 a += d;
00138                 f_mix(b,c,d,a);
00139                 b += c;
00140                 f_mix(c,d,a,b);
00141                 f_mix(d,a,b,c);
00142         }
00143 
00144         f_ktr(a,b,c,d, 4); f_ktr(b,c,d,a, 6); f_ktr(c,d,a,b, 8); f_ktr(d,a,b,c,10); 
00145         f_ktr(a,b,c,d,12); f_ktr(b,c,d,a,14); f_ktr(c,d,a,b,16); f_ktr(d,a,b,c,18); 
00146         f_ktr(a,d,c,b,20); f_ktr(b,a,d,c,22); f_ktr(c,b,a,d,24); f_ktr(d,c,b,a,26); 
00147         f_ktr(a,d,c,b,28); f_ktr(b,a,d,c,30); f_ktr(c,b,a,d,32); f_ktr(d,c,b,a,34); 
00148 
00149         for (i = 0; i < 2; i++) {
00150                 b_mix(a,b,c,d);
00151                 b_mix(b,c,d,a);
00152                 c -= b;
00153                 b_mix(c,d,a,b);
00154                 d -= a;
00155                 b_mix(d,a,b,c);
00156         }
00157 
00158         a -= EK[36];
00159         b -= EK[37];
00160         c -= EK[38];
00161         d -= EK[39];
00162 
00163         PutBlockLittleEndian(outBlock,a,b,c,d);
00164 }
00165 
00166 void MARSDecryption::ProcessBlock(const byte *inBlock, byte *outBlock) const
00167 {
00168         word32 a, b, c, d, l, m, r;
00169 
00170         GetBlockLittleEndian(inBlock,d,c,b,a);
00171         
00172         d += EK[36];
00173         c += EK[37];
00174         b += EK[38];
00175         a += EK[39];
00176 
00177         int i;
00178         for (i = 0; i < 2; i++) {
00179                 f_mix(a,b,c,d);
00180                 a += d;
00181                 f_mix(b,c,d,a);
00182                 b += c;
00183                 f_mix(c,d,a,b);
00184                 f_mix(d,a,b,c);
00185         }
00186 
00187         r_ktr(a,b,c,d,34); r_ktr(b,c,d,a,32); r_ktr(c,d,a,b,30); r_ktr(d,a,b,c,28);
00188         r_ktr(a,b,c,d,26); r_ktr(b,c,d,a,24); r_ktr(c,d,a,b,22); r_ktr(d,a,b,c,20);
00189         r_ktr(a,d,c,b,18); r_ktr(b,a,d,c,16); r_ktr(c,b,a,d,14); r_ktr(d,c,b,a,12);
00190         r_ktr(a,d,c,b,10); r_ktr(b,a,d,c, 8); r_ktr(c,b,a,d, 6); r_ktr(d,c,b,a, 4);
00191 
00192         for (i = 0; i < 2; i++) {
00193                 b_mix(a,b,c,d);
00194                 b_mix(b,c,d,a);
00195                 c -= b;
00196                 b_mix(c,d,a,b);
00197                 d -= a;
00198                 b_mix(d,a,b,c);
00199         }
00200 
00201         d -= EK[0];
00202         c -= EK[1];
00203         b -= EK[2];
00204         a -= EK[3];
00205 
00206         PutBlockLittleEndian(outBlock,d,c,b,a);
00207 }
00208 
00209 NAMESPACE_END

Generated at Mon Jan 15 01:16:33 2001 for Crypto++ by doxygen1.2.4 written by Dimitri van Heesch, © 1997-2000