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 #include "misc.h"
00020 
00021 NAMESPACE_BEGIN(CryptoPP)
00022 
00023 ANONYMOUS_NAMESPACE_BEGIN
00024 static word32 gen_mask(word32 x)
00025 {
00026         word32  m;
00027 
00028         m = (~x ^ (x >> 1)) & 0x7fffffff;
00029         m &= (m >> 1) & (m >> 2); m &= (m >> 3) & (m >> 6); 
00030 
00031         if(!m)
00032                 return 0;
00033 
00034         m <<= 1; m |= (m << 1); m |= (m << 2); m |= (m << 4);
00035         m |= (m << 1) & ~x & 0x80000000;
00036 
00037         return m & 0xfffffffc;
00038 };
00039 NAMESPACE_END
00040 
00041 void MARS::Base::UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &)
00042 {
00043         AssertValidKeyLength(length);
00044 
00045         // Initialize T[] with the key data
00046         FixedSizeSecBlock<word32, 15> T;
00047         GetUserKey(LITTLE_ENDIAN_ORDER, T.begin(), 15, userKey, length);
00048         T[length/4] = length/4;
00049 
00050         for (unsigned int j=0; j<4; j++)        // compute 10 words of K[] in each iteration
00051         {
00052                 unsigned int i;
00053                 // Do linear transformation
00054                 for (i=0; i<15; i++)
00055                         T[i] = T[i] ^ rotlFixed(T[(i+8)%15] ^ T[(i+13)%15], 3) ^ (4*i+j);
00056 
00057                 // Do four rounds of stirring
00058                 for (unsigned int k=0; k<4; k++)
00059                         for (i=0; i<15; i++)
00060                            T[i] = rotlFixed(T[i] + Sbox[T[(i+14)%15]%512], 9);
00061 
00062                 // Store next 10 key words into K[]
00063                 for (i=0; i<10; i++)
00064                         EK[10*j+i] = T[4*i%15];
00065         }
00066 
00067         // Modify multiplication key-words
00068         for(unsigned int i = 5; i < 37; i += 2)
00069         {
00070                 word32 w = EK[i] | 3;
00071                 word32 m = gen_mask(w);
00072                 if(m)
00073                         w ^= (rotlMod(Sbox[265 + (EK[i] & 3)], EK[i-1]) & m);
00074                 EK[i] = w;
00075         }
00076 }
00077 
00078 #define f_mix(a,b,c,d)                                  \
00079                 r = rotrFixed(a, 8);                            \
00080                 b ^= Sbox[a & 255];                             \
00081                 b += Sbox[(r & 255) + 256];             \
00082                 r = rotrFixed(a, 16);                           \
00083                 a  = rotrFixed(a, 24);                          \
00084                 c += Sbox[r & 255];                             \
00085                 d ^= Sbox[(a & 255) + 256]
00086 
00087 #define b_mix(a,b,c,d)                                  \
00088                 r = rotlFixed(a, 8);                            \
00089                 b ^= Sbox[(a & 255) + 256];             \
00090                 c -= Sbox[r & 255];                             \
00091                 r = rotlFixed(a, 16);                           \
00092                 a  = rotlFixed(a, 24);                          \
00093                 d -= Sbox[(r & 255) + 256];             \
00094                 d ^= Sbox[a & 255]
00095 
00096 #define f_ktr(a,b,c,d,i)        \
00097         m = a + EK[i];                  \
00098         a = rotlFixed(a, 13);           \
00099         r = a * EK[i + 1];              \
00100         l = Sbox[m & 511];              \
00101         r = rotlFixed(r, 5);            \
00102         l ^= r;                                 \
00103         c += rotlMod(m, r);             \
00104         r = rotlFixed(r, 5);            \
00105         l ^= r;                                 \
00106         d ^= r;                                 \
00107         b += rotlMod(l, r)
00108 
00109 #define r_ktr(a,b,c,d,i)        \
00110         r = a * EK[i + 1];              \
00111         a = rotrFixed(a, 13);           \
00112         m = a + EK[i];                  \
00113         l = Sbox[m & 511];              \
00114         r = rotlFixed(r, 5);            \
00115         l ^= r;                                 \
00116         c -= rotlMod(m, r);             \
00117         r = rotlFixed(r, 5);            \
00118         l ^= r;                                 \
00119         d ^= r;                                 \
00120         b -= rotlMod(l, r)
00121 
00122 typedef BlockGetAndPut<word32, LittleEndian> Block;
00123 
00124 void MARS::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
00125 {
00126         word32 a, b, c, d, l, m, r;
00127         
00128         Block::Get(inBlock)(a)(b)(c)(d);
00129 
00130         a += EK[0];
00131         b += EK[1];
00132         c += EK[2];
00133         d += EK[3];
00134 
00135         int i;
00136         for (i = 0; i < 2; i++) {
00137                 f_mix(a,b,c,d);
00138                 a += d;
00139                 f_mix(b,c,d,a);
00140                 b += c;
00141                 f_mix(c,d,a,b);
00142                 f_mix(d,a,b,c);
00143         }
00144 
00145         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); 
00146         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); 
00147         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); 
00148         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); 
00149 
00150         for (i = 0; i < 2; i++) {
00151                 b_mix(a,b,c,d);
00152                 b_mix(b,c,d,a);
00153                 c -= b;
00154                 b_mix(c,d,a,b);
00155                 d -= a;
00156                 b_mix(d,a,b,c);
00157         }
00158 
00159         a -= EK[36];
00160         b -= EK[37];
00161         c -= EK[38];
00162         d -= EK[39];
00163 
00164         Block::Put(xorBlock, outBlock)(a)(b)(c)(d);
00165 }
00166 
00167 void MARS::Dec::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
00168 {
00169         word32 a, b, c, d, l, m, r;
00170 
00171         Block::Get(inBlock)(d)(c)(b)(a);
00172         
00173         d += EK[36];
00174         c += EK[37];
00175         b += EK[38];
00176         a += EK[39];
00177 
00178         int i;
00179         for (i = 0; i < 2; i++) {
00180                 f_mix(a,b,c,d);
00181                 a += d;
00182                 f_mix(b,c,d,a);
00183                 b += c;
00184                 f_mix(c,d,a,b);
00185                 f_mix(d,a,b,c);
00186         }
00187 
00188         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);
00189         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);
00190         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);
00191         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);
00192 
00193         for (i = 0; i < 2; i++) {
00194                 b_mix(a,b,c,d);
00195                 b_mix(b,c,d,a);
00196                 c -= b;
00197                 b_mix(c,d,a,b);
00198                 d -= a;
00199                 b_mix(d,a,b,c);
00200         }
00201 
00202         d -= EK[0];
00203         c -= EK[1];
00204         b -= EK[2];
00205         a -= EK[3];
00206 
00207         Block::Put(xorBlock, outBlock)(d)(c)(b)(a);
00208 }
00209 
00210 NAMESPACE_END

Generated on Sat Dec 23 02:07:08 2006 for Crypto++ by  doxygen 1.5.1-p1