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

rc5.cpp

00001 // rc5.cpp - written and placed in the public domain by Wei Dai 00002 00003 #include "pch.h" 00004 #include "rc5.h" 00005 #include "misc.h" 00006 00007 NAMESPACE_BEGIN(CryptoPP) 00008 00009 void RC5::Base::UncheckedSetKey(CipherDir direction, const byte *k, unsigned int keylen, unsigned int rounds) 00010 { 00011 AssertValidKeyLength(keylen); 00012 AssertValidRounds(rounds); 00013 00014 r = rounds; 00015 sTable.New(2*(r+1)); 00016 00017 static const RC5_WORD MAGIC_P = 0xb7e15163L; // magic constant P for wordsize 00018 static const RC5_WORD MAGIC_Q = 0x9e3779b9L; // magic constant Q for wordsize 00019 static const int U=sizeof(RC5_WORD); 00020 00021 const unsigned int c = STDMAX((keylen+U-1)/U, 1U); // RC6 paper says c=1 if keylen==0 00022 SecBlock<RC5_WORD> l(c); 00023 00024 GetUserKey(LITTLE_ENDIAN_ORDER, l.begin(), c, k, keylen); 00025 00026 sTable[0] = MAGIC_P; 00027 for (unsigned j=1; j<sTable.size();j++) 00028 sTable[j] = sTable[j-1] + MAGIC_Q; 00029 00030 RC5_WORD a=0, b=0; 00031 const unsigned n = 3*STDMAX((unsigned int)sTable.size(), c); 00032 00033 for (unsigned h=0; h < n; h++) 00034 { 00035 a = sTable[h % sTable.size()] = rotlFixed((sTable[h % sTable.size()] + a + b), 3); 00036 b = l[h % c] = rotlMod((l[h % c] + a + b), (a+b)); 00037 } 00038 } 00039 00040 typedef BlockGetAndPut<RC5::RC5_WORD, LittleEndian> Block; 00041 00042 void RC5::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const 00043 { 00044 const RC5_WORD *sptr = sTable; 00045 RC5_WORD a, b; 00046 00047 Block::Get(inBlock)(a)(b); 00048 a += sptr[0]; 00049 b += sptr[1]; 00050 sptr += 2; 00051 00052 for(unsigned i=0; i<r; i++) 00053 { 00054 a = rotlMod(a^b,b) + sptr[2*i+0]; 00055 b = rotlMod(a^b,a) + sptr[2*i+1]; 00056 } 00057 00058 Block::Put(xorBlock, outBlock)(a)(b); 00059 } 00060 00061 void RC5::Dec::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const 00062 { 00063 const RC5_WORD *sptr = sTable.end(); 00064 RC5_WORD a, b; 00065 00066 Block::Get(inBlock)(a)(b); 00067 00068 for (unsigned i=0; i<r; i++) 00069 { 00070 sptr-=2; 00071 b = rotrMod(b-sptr[1], a) ^ a; 00072 a = rotrMod(a-sptr[0], b) ^ b; 00073 } 00074 b -= sTable[1]; 00075 a -= sTable[0]; 00076 00077 Block::Put(xorBlock, outBlock)(a)(b); 00078 } 00079 00080 NAMESPACE_END

Generated on Fri Aug 27 15:51:10 2004 for Crypto++ by doxygen 1.3.8