<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> <title>Crypto++: cast.cpp Source File</title> <link href="tabs.css" rel="stylesheet" type="text/css"/> <link href="doxygen.css" rel="stylesheet" type="text/css"/> </head> <body> <!-- Generated by Doxygen 1.6.1 --> <div class="navigation" id="top"> <div class="tabs"> <ul> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="namespaces.html"><span>Namespaces</span></a></li> <li><a href="annotated.html"><span>Classes</span></a></li> <li class="current"><a href="files.html"><span>Files</span></a></li> </ul> </div> <div class="tabs"> <ul> <li><a href="files.html"><span>File List</span></a></li> <li><a href="globals.html"><span>File Members</span></a></li> </ul> </div> <h1>cast.cpp</h1><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">// cast.cpp - written and placed in the public domain by Wei Dai and Leonard Janke</span> <a name="l00002"></a>00002 <span class="comment">// based on Steve Reid's public domain cast.c</span> <a name="l00003"></a>00003 <a name="l00004"></a>00004 <span class="preprocessor">#include "pch.h"</span> <a name="l00005"></a>00005 <span class="preprocessor">#include "<a class="code" href="cast_8h.html">cast.h</a>"</span> <a name="l00006"></a>00006 <span class="preprocessor">#include "misc.h"</span> <a name="l00007"></a>00007 <a name="l00008"></a>00008 NAMESPACE_BEGIN(CryptoPP) <a name="l00009"></a>00009 <a name="l00010"></a>00010 <span class="comment">/* Macros to access 8-bit bytes out of a 32-bit word */</span> <a name="l00011"></a>00011 <span class="preprocessor">#define U8a(x) GETBYTE(x,3)</span> <a name="l00012"></a>00012 <span class="preprocessor"></span><span class="preprocessor">#define U8b(x) GETBYTE(x,2)</span> <a name="l00013"></a>00013 <span class="preprocessor"></span><span class="preprocessor">#define U8c(x) GETBYTE(x,1)</span> <a name="l00014"></a>00014 <span class="preprocessor"></span><span class="preprocessor">#define U8d(x) GETBYTE(x,0)</span> <a name="l00015"></a>00015 <span class="preprocessor"></span> <a name="l00016"></a>00016 <span class="comment">/* CAST uses three different round functions */</span> <a name="l00017"></a>00017 <span class="preprocessor">#define f1(l, r, km, kr) \</span> <a name="l00018"></a>00018 <span class="preprocessor"> t = rotlVariable(km + r, kr); \</span> <a name="l00019"></a>00019 <span class="preprocessor"> l ^= ((S[0][U8a(t)] ^ S[1][U8b(t)]) - \</span> <a name="l00020"></a>00020 <span class="preprocessor"> S[2][U8c(t)]) + S[3][U8d(t)];</span> <a name="l00021"></a>00021 <span class="preprocessor"></span><span class="preprocessor">#define f2(l, r, km, kr) \</span> <a name="l00022"></a>00022 <span class="preprocessor"> t = rotlVariable(km ^ r, kr); \</span> <a name="l00023"></a>00023 <span class="preprocessor"> l ^= ((S[0][U8a(t)] - S[1][U8b(t)]) + \</span> <a name="l00024"></a>00024 <span class="preprocessor"> S[2][U8c(t)]) ^ S[3][U8d(t)];</span> <a name="l00025"></a>00025 <span class="preprocessor"></span><span class="preprocessor">#define f3(l, r, km, kr) \</span> <a name="l00026"></a>00026 <span class="preprocessor"> t = rotlVariable(km - r, kr); \</span> <a name="l00027"></a>00027 <span class="preprocessor"> l ^= ((S[0][U8a(t)] + S[1][U8b(t)]) ^ \</span> <a name="l00028"></a>00028 <span class="preprocessor"> S[2][U8c(t)]) - S[3][U8d(t)];</span> <a name="l00029"></a>00029 <span class="preprocessor"></span> <a name="l00030"></a>00030 <span class="preprocessor">#define F1(l, r, i, j) f1(l, r, K[i], K[i+j])</span> <a name="l00031"></a>00031 <span class="preprocessor"></span><span class="preprocessor">#define F2(l, r, i, j) f2(l, r, K[i], K[i+j])</span> <a name="l00032"></a>00032 <span class="preprocessor"></span><span class="preprocessor">#define F3(l, r, i, j) f3(l, r, K[i], K[i+j])</span> <a name="l00033"></a>00033 <span class="preprocessor"></span> <a name="l00034"></a>00034 <span class="keyword">typedef</span> <a class="code" href="struct_block_get_and_put.html">BlockGetAndPut<word32, BigEndian></a> <a class="code" href="struct_block_get_and_put.html">Block</a>; <a name="l00035"></a>00035 <a name="l00036"></a>00036 <span class="keywordtype">void</span> CAST128::Enc::ProcessAndXorBlock(<span class="keyword">const</span> byte *inBlock, <span class="keyword">const</span> byte *xorBlock, byte *outBlock)<span class="keyword"> const</span> <a name="l00037"></a>00037 <span class="keyword"></span>{ <a name="l00038"></a>00038 word32 t, l, r; <a name="l00039"></a>00039 <a name="l00040"></a>00040 <span class="comment">/* Get inblock into l,r */</span> <a name="l00041"></a>00041 Block::Get(inBlock)(l)(r); <a name="l00042"></a>00042 <span class="comment">/* Do the work */</span> <a name="l00043"></a>00043 F1(l, r, 0, 16); <a name="l00044"></a>00044 F2(r, l, 1, 16); <a name="l00045"></a>00045 F3(l, r, 2, 16); <a name="l00046"></a>00046 F1(r, l, 3, 16); <a name="l00047"></a>00047 F2(l, r, 4, 16); <a name="l00048"></a>00048 F3(r, l, 5, 16); <a name="l00049"></a>00049 F1(l, r, 6, 16); <a name="l00050"></a>00050 F2(r, l, 7, 16); <a name="l00051"></a>00051 F3(l, r, 8, 16); <a name="l00052"></a>00052 F1(r, l, 9, 16); <a name="l00053"></a>00053 F2(l, r, 10, 16); <a name="l00054"></a>00054 F3(r, l, 11, 16); <a name="l00055"></a>00055 <span class="comment">/* Only do full 16 rounds if key length > 80 bits */</span> <a name="l00056"></a>00056 <span class="keywordflow">if</span> (!reduced) { <a name="l00057"></a>00057 F1(l, r, 12, 16); <a name="l00058"></a>00058 F2(r, l, 13, 16); <a name="l00059"></a>00059 F3(l, r, 14, 16); <a name="l00060"></a>00060 F1(r, l, 15, 16); <a name="l00061"></a>00061 } <a name="l00062"></a>00062 <span class="comment">/* Put l,r into outblock */</span> <a name="l00063"></a>00063 <a class="code" href="class_put_block.html">Block::Put</a>(xorBlock, outBlock)(r)(l); <a name="l00064"></a>00064 } <a name="l00065"></a>00065 <a name="l00066"></a>00066 <span class="keywordtype">void</span> CAST128::Dec::ProcessAndXorBlock(<span class="keyword">const</span> byte *inBlock, <span class="keyword">const</span> byte *xorBlock, byte *outBlock)<span class="keyword"> const</span> <a name="l00067"></a>00067 <span class="keyword"></span>{ <a name="l00068"></a>00068 word32 t, l, r; <a name="l00069"></a>00069 <a name="l00070"></a>00070 <span class="comment">/* Get inblock into l,r */</span> <a name="l00071"></a>00071 Block::Get(inBlock)(r)(l); <a name="l00072"></a>00072 <span class="comment">/* Only do full 16 rounds if key length > 80 bits */</span> <a name="l00073"></a>00073 <span class="keywordflow">if</span> (!reduced) { <a name="l00074"></a>00074 F1(r, l, 15, 16); <a name="l00075"></a>00075 F3(l, r, 14, 16); <a name="l00076"></a>00076 F2(r, l, 13, 16); <a name="l00077"></a>00077 F1(l, r, 12, 16); <a name="l00078"></a>00078 } <a name="l00079"></a>00079 F3(r, l, 11, 16); <a name="l00080"></a>00080 F2(l, r, 10, 16); <a name="l00081"></a>00081 F1(r, l, 9, 16); <a name="l00082"></a>00082 F3(l, r, 8, 16); <a name="l00083"></a>00083 F2(r, l, 7, 16); <a name="l00084"></a>00084 F1(l, r, 6, 16); <a name="l00085"></a>00085 F3(r, l, 5, 16); <a name="l00086"></a>00086 F2(l, r, 4, 16); <a name="l00087"></a>00087 F1(r, l, 3, 16); <a name="l00088"></a>00088 F3(l, r, 2, 16); <a name="l00089"></a>00089 F2(r, l, 1, 16); <a name="l00090"></a>00090 F1(l, r, 0, 16); <a name="l00091"></a>00091 <span class="comment">/* Put l,r into outblock */</span> <a name="l00092"></a>00092 <a class="code" href="class_put_block.html">Block::Put</a>(xorBlock, outBlock)(l)(r); <a name="l00093"></a>00093 <span class="comment">/* Wipe clean */</span> <a name="l00094"></a>00094 t = l = r = 0; <a name="l00095"></a>00095 } <a name="l00096"></a>00096 <a name="l00097"></a>00097 <span class="keywordtype">void</span> CAST128::Base::UncheckedSetKey(<span class="keyword">const</span> byte *userKey, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> keylength, <span class="keyword">const</span> <a class="code" href="class_name_value_pairs.html" title="interface for retrieving values given their names">NameValuePairs</a> &) <a name="l00098"></a>00098 { <a name="l00099"></a>00099 AssertValidKeyLength(keylength); <a name="l00100"></a>00100 <a name="l00101"></a>00101 reduced = (keylength <= 10); <a name="l00102"></a>00102 <a name="l00103"></a>00103 word32 X[4], Z[4]; <a name="l00104"></a>00104 GetUserKey(BIG_ENDIAN_ORDER, X, 4, userKey, keylength); <a name="l00105"></a>00105 <a name="l00106"></a>00106 <span class="preprocessor">#define x(i) GETBYTE(X[i/4], 3-i%4)</span> <a name="l00107"></a>00107 <span class="preprocessor"></span><span class="preprocessor">#define z(i) GETBYTE(Z[i/4], 3-i%4)</span> <a name="l00108"></a>00108 <span class="preprocessor"></span> <a name="l00109"></a>00109 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i; <a name="l00110"></a>00110 <span class="keywordflow">for</span> (i=0; i<=16; i+=16) <a name="l00111"></a>00111 { <a name="l00112"></a>00112 <span class="comment">// this part is copied directly from RFC 2144 (with some search and replace) by Wei Dai</span> <a name="l00113"></a>00113 Z[0] = X[0] ^ S[4][x(0xD)] ^ S[5][x(0xF)] ^ S[6][x(0xC)] ^ S[7][x(0xE)] ^ S[6][x(0x8)]; <a name="l00114"></a>00114 Z[1] = X[2] ^ S[4][z(0x0)] ^ S[5][z(0x2)] ^ S[6][z(0x1)] ^ S[7][z(0x3)] ^ S[7][x(0xA)]; <a name="l00115"></a>00115 Z[2] = X[3] ^ S[4][z(0x7)] ^ S[5][z(0x6)] ^ S[6][z(0x5)] ^ S[7][z(0x4)] ^ S[4][x(0x9)]; <a name="l00116"></a>00116 Z[3] = X[1] ^ S[4][z(0xA)] ^ S[5][z(0x9)] ^ S[6][z(0xB)] ^ S[7][z(0x8)] ^ S[5][x(0xB)]; <a name="l00117"></a>00117 K[i+0] = S[4][z(0x8)] ^ S[5][z(0x9)] ^ S[6][z(0x7)] ^ S[7][z(0x6)] ^ S[4][z(0x2)]; <a name="l00118"></a>00118 K[i+1] = S[4][z(0xA)] ^ S[5][z(0xB)] ^ S[6][z(0x5)] ^ S[7][z(0x4)] ^ S[5][z(0x6)]; <a name="l00119"></a>00119 K[i+2] = S[4][z(0xC)] ^ S[5][z(0xD)] ^ S[6][z(0x3)] ^ S[7][z(0x2)] ^ S[6][z(0x9)]; <a name="l00120"></a>00120 K[i+3] = S[4][z(0xE)] ^ S[5][z(0xF)] ^ S[6][z(0x1)] ^ S[7][z(0x0)] ^ S[7][z(0xC)]; <a name="l00121"></a>00121 X[0] = Z[2] ^ S[4][z(0x5)] ^ S[5][z(0x7)] ^ S[6][z(0x4)] ^ S[7][z(0x6)] ^ S[6][z(0x0)]; <a name="l00122"></a>00122 X[1] = Z[0] ^ S[4][x(0x0)] ^ S[5][x(0x2)] ^ S[6][x(0x1)] ^ S[7][x(0x3)] ^ S[7][z(0x2)]; <a name="l00123"></a>00123 X[2] = Z[1] ^ S[4][x(0x7)] ^ S[5][x(0x6)] ^ S[6][x(0x5)] ^ S[7][x(0x4)] ^ S[4][z(0x1)]; <a name="l00124"></a>00124 X[3] = Z[3] ^ S[4][x(0xA)] ^ S[5][x(0x9)] ^ S[6][x(0xB)] ^ S[7][x(0x8)] ^ S[5][z(0x3)]; <a name="l00125"></a>00125 K[i+4] = S[4][x(0x3)] ^ S[5][x(0x2)] ^ S[6][x(0xC)] ^ S[7][x(0xD)] ^ S[4][x(0x8)]; <a name="l00126"></a>00126 K[i+5] = S[4][x(0x1)] ^ S[5][x(0x0)] ^ S[6][x(0xE)] ^ S[7][x(0xF)] ^ S[5][x(0xD)]; <a name="l00127"></a>00127 K[i+6] = S[4][x(0x7)] ^ S[5][x(0x6)] ^ S[6][x(0x8)] ^ S[7][x(0x9)] ^ S[6][x(0x3)]; <a name="l00128"></a>00128 K[i+7] = S[4][x(0x5)] ^ S[5][x(0x4)] ^ S[6][x(0xA)] ^ S[7][x(0xB)] ^ S[7][x(0x7)]; <a name="l00129"></a>00129 Z[0] = X[0] ^ S[4][x(0xD)] ^ S[5][x(0xF)] ^ S[6][x(0xC)] ^ S[7][x(0xE)] ^ S[6][x(0x8)]; <a name="l00130"></a>00130 Z[1] = X[2] ^ S[4][z(0x0)] ^ S[5][z(0x2)] ^ S[6][z(0x1)] ^ S[7][z(0x3)] ^ S[7][x(0xA)]; <a name="l00131"></a>00131 Z[2] = X[3] ^ S[4][z(0x7)] ^ S[5][z(0x6)] ^ S[6][z(0x5)] ^ S[7][z(0x4)] ^ S[4][x(0x9)]; <a name="l00132"></a>00132 Z[3] = X[1] ^ S[4][z(0xA)] ^ S[5][z(0x9)] ^ S[6][z(0xB)] ^ S[7][z(0x8)] ^ S[5][x(0xB)]; <a name="l00133"></a>00133 K[i+8] = S[4][z(0x3)] ^ S[5][z(0x2)] ^ S[6][z(0xC)] ^ S[7][z(0xD)] ^ S[4][z(0x9)]; <a name="l00134"></a>00134 K[i+9] = S[4][z(0x1)] ^ S[5][z(0x0)] ^ S[6][z(0xE)] ^ S[7][z(0xF)] ^ S[5][z(0xC)]; <a name="l00135"></a>00135 K[i+10] = S[4][z(0x7)] ^ S[5][z(0x6)] ^ S[6][z(0x8)] ^ S[7][z(0x9)] ^ S[6][z(0x2)]; <a name="l00136"></a>00136 K[i+11] = S[4][z(0x5)] ^ S[5][z(0x4)] ^ S[6][z(0xA)] ^ S[7][z(0xB)] ^ S[7][z(0x6)]; <a name="l00137"></a>00137 X[0] = Z[2] ^ S[4][z(0x5)] ^ S[5][z(0x7)] ^ S[6][z(0x4)] ^ S[7][z(0x6)] ^ S[6][z(0x0)]; <a name="l00138"></a>00138 X[1] = Z[0] ^ S[4][x(0x0)] ^ S[5][x(0x2)] ^ S[6][x(0x1)] ^ S[7][x(0x3)] ^ S[7][z(0x2)]; <a name="l00139"></a>00139 X[2] = Z[1] ^ S[4][x(0x7)] ^ S[5][x(0x6)] ^ S[6][x(0x5)] ^ S[7][x(0x4)] ^ S[4][z(0x1)]; <a name="l00140"></a>00140 X[3] = Z[3] ^ S[4][x(0xA)] ^ S[5][x(0x9)] ^ S[6][x(0xB)] ^ S[7][x(0x8)] ^ S[5][z(0x3)]; <a name="l00141"></a>00141 K[i+12] = S[4][x(0x8)] ^ S[5][x(0x9)] ^ S[6][x(0x7)] ^ S[7][x(0x6)] ^ S[4][x(0x3)]; <a name="l00142"></a>00142 K[i+13] = S[4][x(0xA)] ^ S[5][x(0xB)] ^ S[6][x(0x5)] ^ S[7][x(0x4)] ^ S[5][x(0x7)]; <a name="l00143"></a>00143 K[i+14] = S[4][x(0xC)] ^ S[5][x(0xD)] ^ S[6][x(0x3)] ^ S[7][x(0x2)] ^ S[6][x(0x8)]; <a name="l00144"></a>00144 K[i+15] = S[4][x(0xE)] ^ S[5][x(0xF)] ^ S[6][x(0x1)] ^ S[7][x(0x0)] ^ S[7][x(0xD)]; <a name="l00145"></a>00145 } <a name="l00146"></a>00146 <a name="l00147"></a>00147 <span class="keywordflow">for</span> (i=16; i<32; i++) <a name="l00148"></a>00148 K[i] &= 0x1f; <a name="l00149"></a>00149 } <a name="l00150"></a>00150 <a name="l00151"></a>00151 <span class="comment">// The following CAST-256 implementation was contributed by Leonard Janke</span> <a name="l00152"></a>00152 <a name="l00153"></a>00153 <span class="keyword">const</span> word32 CAST256::Base::t_m[8][24]={ <a name="l00154"></a>00154 { 0x5a827999, 0xd151d6a1, 0x482133a9, 0xbef090b1, 0x35bfedb9, 0xac8f4ac1, <a name="l00155"></a>00155 0x235ea7c9, 0x9a2e04d1, 0x10fd61d9, 0x87ccbee1, 0xfe9c1be9, 0x756b78f1, <a name="l00156"></a>00156 0xec3ad5f9, 0x630a3301, 0xd9d99009, 0x50a8ed11, 0xc7784a19, 0x3e47a721, <a name="l00157"></a>00157 0xb5170429, 0x2be66131, 0xa2b5be39, 0x19851b41, 0x90547849, 0x0723d551}, <a name="l00158"></a>00158 { 0xc95c653a, 0x402bc242, 0xb6fb1f4a, 0x2dca7c52, 0xa499d95a, 0x1b693662, <a name="l00159"></a>00159 0x9238936a, 0x0907f072, 0x7fd74d7a, 0xf6a6aa82, 0x6d76078a, 0xe4456492, <a name="l00160"></a>00160 0x5b14c19a, 0xd1e41ea2, 0x48b37baa, 0xbf82d8b2, 0x365235ba, 0xad2192c2, <a name="l00161"></a>00161 0x23f0efca, 0x9ac04cd2, 0x118fa9da, 0x885f06e2, 0xff2e63ea, 0x75fdc0f2}, <a name="l00162"></a>00162 { 0x383650db, 0xaf05ade3, 0x25d50aeb, 0x9ca467f3, 0x1373c4fb, 0x8a432203, <a name="l00163"></a>00163 0x01127f0b, 0x77e1dc13, 0xeeb1391b, 0x65809623, 0xdc4ff32b, 0x531f5033, <a name="l00164"></a>00164 0xc9eead3b, 0x40be0a43, 0xb78d674b, 0x2e5cc453, 0xa52c215b, 0x1bfb7e63, <a name="l00165"></a>00165 0x92cadb6b, 0x099a3873, 0x8069957b, 0xf738f283, 0x6e084f8b, 0xe4d7ac93}, <a name="l00166"></a>00166 { 0xa7103c7c, 0x1ddf9984, 0x94aef68c, 0x0b7e5394, 0x824db09c, 0xf91d0da4, <a name="l00167"></a>00167 0x6fec6aac, 0xe6bbc7b4, 0x5d8b24bc, 0xd45a81c4, 0x4b29decc, 0xc1f93bd4, <a name="l00168"></a>00168 0x38c898dc, 0xaf97f5e4, 0x266752ec, 0x9d36aff4, 0x14060cfc, 0x8ad56a04, <a name="l00169"></a>00169 0x01a4c70c, 0x78742414, 0xef43811c, 0x6612de24, 0xdce23b2c, 0x53b19834}, <a name="l00170"></a>00170 { 0x15ea281d, 0x8cb98525, 0x0388e22d, 0x7a583f35, 0xf1279c3d, 0x67f6f945, <a name="l00171"></a>00171 0xdec6564d, 0x5595b355, 0xcc65105d, 0x43346d65, 0xba03ca6d, 0x30d32775, <a name="l00172"></a>00172 0xa7a2847d, 0x1e71e185, 0x95413e8d, 0x0c109b95, 0x82dff89d, 0xf9af55a5, <a name="l00173"></a>00173 0x707eb2ad, 0xe74e0fb5, 0x5e1d6cbd, 0xd4ecc9c5, 0x4bbc26cd, 0xc28b83d5}, <a name="l00174"></a>00174 { 0x84c413be, 0xfb9370c6, 0x7262cdce, 0xe9322ad6, 0x600187de, 0xd6d0e4e6, <a name="l00175"></a>00175 0x4da041ee, 0xc46f9ef6, 0x3b3efbfe, 0xb20e5906, 0x28ddb60e, 0x9fad1316, <a name="l00176"></a>00176 0x167c701e, 0x8d4bcd26, 0x041b2a2e, 0x7aea8736, 0xf1b9e43e, 0x68894146, <a name="l00177"></a>00177 0xdf589e4e, 0x5627fb56, 0xccf7585e, 0x43c6b566, 0xba96126e, 0x31656f76}, <a name="l00178"></a>00178 { 0xf39dff5f, 0x6a6d5c67, 0xe13cb96f, 0x580c1677, 0xcedb737f, 0x45aad087, <a name="l00179"></a>00179 0xbc7a2d8f, 0x33498a97, 0xaa18e79f, 0x20e844a7, 0x97b7a1af, 0x0e86feb7, <a name="l00180"></a>00180 0x85565bbf, 0xfc25b8c7, 0x72f515cf, 0xe9c472d7, 0x6093cfdf, 0xd7632ce7, <a name="l00181"></a>00181 0x4e3289ef, 0xc501e6f7, 0x3bd143ff, 0xb2a0a107, 0x296ffe0f, 0xa03f5b17}, <a name="l00182"></a>00182 { 0x6277eb00, 0xd9474808, 0x5016a510, 0xc6e60218, 0x3db55f20, 0xb484bc28, <a name="l00183"></a>00183 0x2b541930, 0xa2237638, 0x18f2d340, 0x8fc23048, 0x06918d50, 0x7d60ea58, <a name="l00184"></a>00184 0xf4304760, 0x6affa468, 0xe1cf0170, 0x589e5e78, 0xcf6dbb80, 0x463d1888, <a name="l00185"></a>00185 0xbd0c7590, 0x33dbd298, 0xaaab2fa0, 0x217a8ca8, 0x9849e9b0, 0x0f1946b8} <a name="l00186"></a>00186 }; <a name="l00187"></a>00187 <a name="l00188"></a>00188 <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> CAST256::Base::t_r[8][24]={ <a name="l00189"></a>00189 {19, 27, 3, 11, 19, 27, 3, 11, 19, 27, 3, 11, 19, 27, 3, 11, 19, 27, 3, 11, 19, 27, 3, 11}, <a name="l00190"></a>00190 {4, 12, 20, 28, 4, 12, 20, 28, 4, 12, 20, 28, 4, 12, 20, 28, 4, 12, 20, 28, 4, 12, 20, 28}, <a name="l00191"></a>00191 {21, 29, 5, 13, 21, 29, 5, 13, 21, 29, 5, 13, 21, 29, 5, 13, 21, 29, 5, 13, 21, 29, 5, 13}, <a name="l00192"></a>00192 {6, 14, 22, 30, 6, 14, 22, 30, 6, 14, 22, 30, 6, 14, 22, 30, 6, 14, 22, 30, 6, 14, 22, 30}, <a name="l00193"></a>00193 {23, 31, 7, 15, 23, 31, 7, 15, 23, 31, 7, 15, 23, 31, 7, 15, 23, 31, 7, 15, 23, 31, 7, 15}, <a name="l00194"></a>00194 {8, 16, 24, 0, 8, 16, 24, 0, 8, 16, 24, 0, 8, 16, 24, 0, 8, 16, 24, 0, 8, 16, 24, 0}, <a name="l00195"></a>00195 {25, 1, 9, 17, 25, 1, 9, 17, 25, 1, 9, 17, 25, 1, 9, 17, 25, 1, 9, 17, 25, 1, 9, 17}, <a name="l00196"></a>00196 {10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2} <a name="l00197"></a>00197 }; <a name="l00198"></a>00198 <a name="l00199"></a>00199 <span class="preprocessor">#define Q(i) \</span> <a name="l00200"></a>00200 <span class="preprocessor"> F1(block[2],block[3],8*i+4,-4); \</span> <a name="l00201"></a>00201 <span class="preprocessor"> F2(block[1],block[2],8*i+5,-4); \</span> <a name="l00202"></a>00202 <span class="preprocessor"> F3(block[0],block[1],8*i+6,-4); \</span> <a name="l00203"></a>00203 <span class="preprocessor"> F1(block[3],block[0],8*i+7,-4);</span> <a name="l00204"></a>00204 <span class="preprocessor"></span> <a name="l00205"></a>00205 <span class="preprocessor">#define QBar(i) \</span> <a name="l00206"></a>00206 <span class="preprocessor"> F1(block[3],block[0],8*i+7,-4); \</span> <a name="l00207"></a>00207 <span class="preprocessor"> F3(block[0],block[1],8*i+6,-4); \</span> <a name="l00208"></a>00208 <span class="preprocessor"> F2(block[1],block[2],8*i+5,-4); \</span> <a name="l00209"></a>00209 <span class="preprocessor"> F1(block[2],block[3],8*i+4,-4);</span> <a name="l00210"></a>00210 <span class="preprocessor"></span> <a name="l00211"></a>00211 <span class="comment">/* CAST256's encrypt/decrypt functions are identical except for the order that</span> <a name="l00212"></a>00212 <span class="comment">the keys are used */</span> <a name="l00213"></a>00213 <a name="l00214"></a>00214 <span class="keywordtype">void</span> CAST256::Base::ProcessAndXorBlock(<span class="keyword">const</span> byte *inBlock, <span class="keyword">const</span> byte *xorBlock, byte *outBlock)<span class="keyword"> const</span> <a name="l00215"></a>00215 <span class="keyword"></span>{ <a name="l00216"></a>00216 word32 t, block[4]; <a name="l00217"></a>00217 Block::Get(inBlock)(block[0])(block[1])(block[2])(block[3]); <a name="l00218"></a>00218 <a name="l00219"></a>00219 <span class="comment">// Perform 6 forward quad rounds</span> <a name="l00220"></a>00220 Q(0); <a name="l00221"></a>00221 Q(1); <a name="l00222"></a>00222 Q(2); <a name="l00223"></a>00223 Q(3); <a name="l00224"></a>00224 Q(4); <a name="l00225"></a>00225 Q(5); <a name="l00226"></a>00226 <a name="l00227"></a>00227 <span class="comment">// Perform 6 reverse quad rounds</span> <a name="l00228"></a>00228 QBar(6); <a name="l00229"></a>00229 QBar(7); <a name="l00230"></a>00230 QBar(8); <a name="l00231"></a>00231 QBar(9); <a name="l00232"></a>00232 QBar(10); <a name="l00233"></a>00233 QBar(11); <a name="l00234"></a>00234 <a name="l00235"></a>00235 <a class="code" href="class_put_block.html">Block::Put</a>(xorBlock, outBlock)(block[0])(block[1])(block[2])(block[3]); <a name="l00236"></a>00236 } <a name="l00237"></a>00237 <a name="l00238"></a>00238 <span class="comment">/* Set up a CAST-256 key */</span> <a name="l00239"></a>00239 <a name="l00240"></a>00240 <span class="keywordtype">void</span> CAST256::Base::Omega(<span class="keywordtype">int</span> i, word32 kappa[8]) <a name="l00241"></a>00241 { <a name="l00242"></a>00242 word32 t; <a name="l00243"></a>00243 <a name="l00244"></a>00244 f1(kappa[6],kappa[7],t_m[0][i],t_r[0][i]); <a name="l00245"></a>00245 f2(kappa[5],kappa[6],t_m[1][i],t_r[1][i]); <a name="l00246"></a>00246 f3(kappa[4],kappa[5],t_m[2][i],t_r[2][i]); <a name="l00247"></a>00247 f1(kappa[3],kappa[4],t_m[3][i],t_r[3][i]); <a name="l00248"></a>00248 f2(kappa[2],kappa[3],t_m[4][i],t_r[4][i]); <a name="l00249"></a>00249 f3(kappa[1],kappa[2],t_m[5][i],t_r[5][i]); <a name="l00250"></a>00250 f1(kappa[0],kappa[1],t_m[6][i],t_r[6][i]); <a name="l00251"></a>00251 f2(kappa[7],kappa[0],t_m[7][i],t_r[7][i]); <a name="l00252"></a>00252 } <a name="l00253"></a>00253 <a name="l00254"></a>00254 <span class="keywordtype">void</span> CAST256::Base::UncheckedSetKey(<span class="keyword">const</span> byte *userKey, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> keylength, <span class="keyword">const</span> <a class="code" href="class_name_value_pairs.html" title="interface for retrieving values given their names">NameValuePairs</a> &) <a name="l00255"></a>00255 { <a name="l00256"></a>00256 AssertValidKeyLength(keylength); <a name="l00257"></a>00257 <a name="l00258"></a>00258 word32 kappa[8]; <a name="l00259"></a>00259 GetUserKey(BIG_ENDIAN_ORDER, kappa, 8, userKey, keylength); <a name="l00260"></a>00260 <a name="l00261"></a>00261 <span class="keywordflow">for</span>(<span class="keywordtype">int</span> i=0; i<12; ++i) <a name="l00262"></a>00262 { <a name="l00263"></a>00263 Omega(2*i,kappa); <a name="l00264"></a>00264 Omega(2*i+1,kappa); <a name="l00265"></a>00265 <a name="l00266"></a>00266 K[8*i]=kappa[0] & 31; <a name="l00267"></a>00267 K[8*i+1]=kappa[2] & 31; <a name="l00268"></a>00268 K[8*i+2]=kappa[4] & 31; <a name="l00269"></a>00269 K[8*i+3]=kappa[6] & 31; <a name="l00270"></a>00270 K[8*i+4]=kappa[7]; <a name="l00271"></a>00271 K[8*i+5]=kappa[5]; <a name="l00272"></a>00272 K[8*i+6]=kappa[3]; <a name="l00273"></a>00273 K[8*i+7]=kappa[1]; <a name="l00274"></a>00274 } <a name="l00275"></a>00275 <a name="l00276"></a>00276 <span class="keywordflow">if</span> (!IsForwardTransformation()) <a name="l00277"></a>00277 { <a name="l00278"></a>00278 <span class="keywordflow">for</span>(<span class="keywordtype">int</span> j=0; j<6; ++j) <a name="l00279"></a>00279 { <a name="l00280"></a>00280 <span class="keywordflow">for</span>(<span class="keywordtype">int</span> i=0; i<4; ++i) <a name="l00281"></a>00281 { <a name="l00282"></a>00282 <span class="keywordtype">int</span> i1=8*j+i; <a name="l00283"></a>00283 <span class="keywordtype">int</span> i2=8*(11-j)+i; <a name="l00284"></a>00284 <a name="l00285"></a>00285 assert(i1<i2); <a name="l00286"></a>00286 <a name="l00287"></a>00287 std::swap(K[i1],K[i2]); <a name="l00288"></a>00288 std::swap(K[i1+4],K[i2+4]); <a name="l00289"></a>00289 } <a name="l00290"></a>00290 } <a name="l00291"></a>00291 } <a name="l00292"></a>00292 <a name="l00293"></a>00293 memset(kappa, 0, <span class="keyword">sizeof</span>(kappa)); <a name="l00294"></a>00294 } <a name="l00295"></a>00295 <a name="l00296"></a>00296 NAMESPACE_END </pre></div></div> <hr size="1"/><address style="text-align: right;"><small>Generated on 9 Dec 2009 for Crypto++ by <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.1 </small></address> </body> </html>