<!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++: safer.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>safer.cpp</h1><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">// safer.cpp - modified by by Wei Dai from Richard De Moliner's safer.c</span> <a name="l00002"></a>00002 <a name="l00003"></a>00003 <span class="preprocessor">#include "pch.h"</span> <a name="l00004"></a>00004 <span class="preprocessor">#include "<a class="code" href="safer_8h.html">safer.h</a>"</span> <a name="l00005"></a>00005 <span class="preprocessor">#include "misc.h"</span> <a name="l00006"></a>00006 <span class="preprocessor">#include "argnames.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 const byte <a class="code" href="class_s_a_f_e_r.html" title="base class, do not use directly">SAFER</a>::Base::exp_tab[256] = <a name="l00011"></a>00011 {1, 45, 226, 147, 190, 69, 21, 174, 120, 3, 135, 164, 184, 56, 207, 63, <a name="l00012"></a>00012 8, 103, 9, 148, 235, 38, 168, 107, 189, 24, 52, 27, 187, 191, 114, 247, <a name="l00013"></a>00013 64, 53, 72, 156, 81, 47, 59, 85, 227, 192, 159, 216, 211, 243, 141, 177, <a name="l00014"></a>00014 255, 167, 62, 220, 134, 119, 215, 166, 17, 251, 244, 186, 146, 145, 100, 131, <a name="l00015"></a>00015 241, 51, 239, 218, 44, 181, 178, 43, 136, 209, 153, 203, 140, 132, 29, 20, <a name="l00016"></a>00016 129, 151, 113, 202, 95, 163, 139, 87, 60, 130, 196, 82, 92, 28, 232, 160, <a name="l00017"></a>00017 4, 180, 133, 74, 246, 19, 84, 182, 223, 12, 26, 142, 222, 224, 57, 252, <a name="l00018"></a>00018 32, 155, 36, 78, 169, 152, 158, 171, 242, 96, 208, 108, 234, 250, 199, 217, <a name="l00019"></a>00019 0, 212, 31, 110, 67, 188, 236, 83, 137, 254, 122, 93, 73, 201, 50, 194, <a name="l00020"></a>00020 249, 154, 248, 109, 22, 219, 89, 150, 68, 233, 205, 230, 70, 66, 143, 10, <a name="l00021"></a>00021 193, 204, 185, 101, 176, 210, 198, 172, 30, 65, 98, 41, 46, 14, 116, 80, <a name="l00022"></a>00022 2, 90, 195, 37, 123, 138, 42, 91, 240, 6, 13, 71, 111, 112, 157, 126, <a name="l00023"></a>00023 16, 206, 18, 39, 213, 76, 79, 214, 121, 48, 104, 54, 117, 125, 228, 237, <a name="l00024"></a>00024 128, 106, 144, 55, 162, 94, 118, 170, 197, 127, 61, 175, 165, 229, 25, 97, <a name="l00025"></a>00025 253, 77, 124, 183, 11, 238, 173, 75, 34, 245, 231, 115, 35, 33, 200, 5, <a name="l00026"></a>00026 225, 102, 221, 179, 88, 105, 99, 86, 15, 161, 49, 149, 23, 7, 58, 40}; <a name="l00027"></a>00027 <a name="l00028"></a>00028 <span class="keyword">const</span> byte SAFER::Base::log_tab[256] = <a name="l00029"></a>00029 {128, 0, 176, 9, 96, 239, 185, 253, 16, 18, 159, 228, 105, 186, 173, 248, <a name="l00030"></a>00030 192, 56, 194, 101, 79, 6, 148, 252, 25, 222, 106, 27, 93, 78, 168, 130, <a name="l00031"></a>00031 112, 237, 232, 236, 114, 179, 21, 195, 255, 171, 182, 71, 68, 1, 172, 37, <a name="l00032"></a>00032 201, 250, 142, 65, 26, 33, 203, 211, 13, 110, 254, 38, 88, 218, 50, 15, <a name="l00033"></a>00033 32, 169, 157, 132, 152, 5, 156, 187, 34, 140, 99, 231, 197, 225, 115, 198, <a name="l00034"></a>00034 175, 36, 91, 135, 102, 39, 247, 87, 244, 150, 177, 183, 92, 139, 213, 84, <a name="l00035"></a>00035 121, 223, 170, 246, 62, 163, 241, 17, 202, 245, 209, 23, 123, 147, 131, 188, <a name="l00036"></a>00036 189, 82, 30, 235, 174, 204, 214, 53, 8, 200, 138, 180, 226, 205, 191, 217, <a name="l00037"></a>00037 208, 80, 89, 63, 77, 98, 52, 10, 72, 136, 181, 86, 76, 46, 107, 158, <a name="l00038"></a>00038 210, 61, 60, 3, 19, 251, 151, 81, 117, 74, 145, 113, 35, 190, 118, 42, <a name="l00039"></a>00039 95, 249, 212, 85, 11, 220, 55, 49, 22, 116, 215, 119, 167, 230, 7, 219, <a name="l00040"></a>00040 164, 47, 70, 243, 97, 69, 103, 227, 12, 162, 59, 28, 133, 24, 4, 29, <a name="l00041"></a>00041 41, 160, 143, 178, 90, 216, 166, 126, 238, 141, 83, 75, 161, 154, 193, 14, <a name="l00042"></a>00042 122, 73, 165, 44, 129, 196, 199, 54, 43, 127, 67, 149, 51, 242, 108, 104, <a name="l00043"></a>00043 109, 240, 2, 40, 206, 221, 155, 234, 94, 153, 124, 20, 134, 207, 229, 66, <a name="l00044"></a>00044 184, 64, 120, 45, 58, 233, 100, 31, 146, 144, 125, 57, 111, 224, 137, 48}; <a name="l00045"></a>00045 <a name="l00046"></a>00046 <span class="preprocessor">#define EXP(x) exp_tab[(x)]</span> <a name="l00047"></a>00047 <span class="preprocessor"></span><span class="preprocessor">#define LOG(x) log_tab[(x)]</span> <a name="l00048"></a>00048 <span class="preprocessor"></span><span class="preprocessor">#define PHT(x, y) { y += x; x += y; }</span> <a name="l00049"></a>00049 <span class="preprocessor"></span><span class="preprocessor">#define IPHT(x, y) { x -= y; y -= x; }</span> <a name="l00050"></a>00050 <span class="preprocessor"></span> <a name="l00051"></a>00051 <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> BLOCKSIZE = 8; <a name="l00052"></a>00052 <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> MAX_ROUNDS = 13; <a name="l00053"></a>00053 <a name="l00054"></a>00054 <span class="keywordtype">void</span> SAFER::Base::UncheckedSetKey(<span class="keyword">const</span> byte *userkey_1, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> length, <span class="keyword">const</span> <a class="code" href="class_name_value_pairs.html" title="interface for retrieving values given their names">NameValuePairs</a> &params) <a name="l00055"></a>00055 { <a name="l00056"></a>00056 <span class="keywordtype">bool</span> strengthened = Strengthened(); <a name="l00057"></a>00057 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> nof_rounds = params.<a class="code" href="class_name_value_pairs.html#ac269314685b737912d3499f4a9399618" title="get a named value with type int, with default">GetIntValueWithDefault</a>(Name::Rounds(), length == 8 ? (strengthened ? 8 : 6) : 10); <a name="l00058"></a>00058 <a name="l00059"></a>00059 <span class="keyword">const</span> byte *userkey_2 = length == 8 ? userkey_1 : userkey_1 + 8; <a name="l00060"></a>00060 keySchedule.<a class="code" href="class_sec_block.html#a90d46e577c951d81a2d25a4742a3e979" title="change size, without preserving contents">New</a>(1 + BLOCKSIZE * (1 + 2 * nof_rounds)); <a name="l00061"></a>00061 <a name="l00062"></a>00062 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i, j; <a name="l00063"></a>00063 byte *key = keySchedule; <a name="l00064"></a>00064 <a class="code" href="class_sec_block.html" title="a block of memory allocated using A">SecByteBlock</a> ka(BLOCKSIZE + 1), kb(BLOCKSIZE + 1); <a name="l00065"></a>00065 <a name="l00066"></a>00066 <span class="keywordflow">if</span> (MAX_ROUNDS < nof_rounds) <a name="l00067"></a>00067 nof_rounds = MAX_ROUNDS; <a name="l00068"></a>00068 *key++ = (<span class="keywordtype">unsigned</span> char)nof_rounds; <a name="l00069"></a>00069 ka[BLOCKSIZE] = 0; <a name="l00070"></a>00070 kb[BLOCKSIZE] = 0; <a name="l00071"></a>00071 <span class="keywordflow">for</span> (j = 0; j < BLOCKSIZE; j++) <a name="l00072"></a>00072 { <a name="l00073"></a>00073 ka[BLOCKSIZE] ^= ka[j] = rotlFixed(userkey_1[j], 5U); <a name="l00074"></a>00074 kb[BLOCKSIZE] ^= kb[j] = *key++ = userkey_2[j]; <a name="l00075"></a>00075 } <a name="l00076"></a>00076 <a name="l00077"></a>00077 <span class="keywordflow">for</span> (i = 1; i <= nof_rounds; i++) <a name="l00078"></a>00078 { <a name="l00079"></a>00079 <span class="keywordflow">for</span> (j = 0; j < BLOCKSIZE + 1; j++) <a name="l00080"></a>00080 { <a name="l00081"></a>00081 ka[j] = rotlFixed(ka[j], 6U); <a name="l00082"></a>00082 kb[j] = rotlFixed(kb[j], 6U); <a name="l00083"></a>00083 } <a name="l00084"></a>00084 <span class="keywordflow">for</span> (j = 0; j < BLOCKSIZE; j++) <a name="l00085"></a>00085 <span class="keywordflow">if</span> (strengthened) <a name="l00086"></a>00086 *key++ = (ka[(j + 2 * i - 1) % (BLOCKSIZE + 1)] <a name="l00087"></a>00087 + exp_tab[exp_tab[18 * i + j + 1]]) & 0xFF; <a name="l00088"></a>00088 <span class="keywordflow">else</span> <a name="l00089"></a>00089 *key++ = (ka[j] + exp_tab[exp_tab[18 * i + j + 1]]) & 0xFF; <a name="l00090"></a>00090 <span class="keywordflow">for</span> (j = 0; j < BLOCKSIZE; j++) <a name="l00091"></a>00091 <span class="keywordflow">if</span> (strengthened) <a name="l00092"></a>00092 *key++ = (kb[(j + 2 * i) % (BLOCKSIZE + 1)] <a name="l00093"></a>00093 + exp_tab[exp_tab[18 * i + j + 10]]) & 0xFF; <a name="l00094"></a>00094 <span class="keywordflow">else</span> <a name="l00095"></a>00095 *key++ = (kb[j] + exp_tab[exp_tab[18 * i + j + 10]]) & 0xFF; <a name="l00096"></a>00096 } <a name="l00097"></a>00097 } <a name="l00098"></a>00098 <a name="l00099"></a>00099 <span class="keyword">typedef</span> <a class="code" href="struct_block_get_and_put.html">BlockGetAndPut<byte, BigEndian></a> <a class="code" href="struct_block_get_and_put.html">Block</a>; <a name="l00100"></a>00100 <a name="l00101"></a><a class="code" href="class_s_a_f_e_r_1_1_enc.html#a300fcdad081a887bcd9c37bb6e950b0d">00101</a> <span class="keywordtype">void</span> <a class="code" href="class_s_a_f_e_r_1_1_enc.html#a300fcdad081a887bcd9c37bb6e950b0d" title="encrypt or decrypt inBlock, xor with xorBlock, and write to outBlock">SAFER::Enc::ProcessAndXorBlock</a>(<span class="keyword">const</span> byte *inBlock, <span class="keyword">const</span> byte *xorBlock, byte *outBlock)<span class="keyword"> const</span> <a name="l00102"></a>00102 <span class="keyword"></span>{ <a name="l00103"></a>00103 byte a, b, c, d, e, f, g, h, t; <a name="l00104"></a>00104 <span class="keyword">const</span> byte *key = keySchedule+1; <a name="l00105"></a>00105 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> round = keySchedule[0]; <a name="l00106"></a>00106 <a name="l00107"></a>00107 Block::Get(inBlock)(a)(b)(c)(d)(e)(f)(g)(h); <a name="l00108"></a>00108 <span class="keywordflow">while</span>(round--) <a name="l00109"></a>00109 { <a name="l00110"></a>00110 a ^= key[0]; b += key[1]; c += key[2]; d ^= key[3]; <a name="l00111"></a>00111 e ^= key[4]; f += key[5]; g += key[6]; h ^= key[7]; <a name="l00112"></a>00112 a = EXP(a) + key[ 8]; b = LOG(b) ^ key[ 9]; <a name="l00113"></a>00113 c = LOG(c) ^ key[10]; d = EXP(d) + key[11]; <a name="l00114"></a>00114 e = EXP(e) + key[12]; f = LOG(f) ^ key[13]; <a name="l00115"></a>00115 g = LOG(g) ^ key[14]; h = EXP(h) + key[15]; <a name="l00116"></a>00116 key += 16; <a name="l00117"></a>00117 PHT(a, b); PHT(c, d); PHT(e, f); PHT(g, h); <a name="l00118"></a>00118 PHT(a, c); PHT(e, g); PHT(b, d); PHT(f, h); <a name="l00119"></a>00119 PHT(a, e); PHT(b, f); PHT(c, g); PHT(d, h); <a name="l00120"></a>00120 t = b; b = e; e = c; c = t; t = d; d = f; f = g; g = t; <a name="l00121"></a>00121 } <a name="l00122"></a>00122 a ^= key[0]; b += key[1]; c += key[2]; d ^= key[3]; <a name="l00123"></a>00123 e ^= key[4]; f += key[5]; g += key[6]; h ^= key[7]; <a name="l00124"></a>00124 <a class="code" href="class_put_block.html">Block::Put</a>(xorBlock, outBlock)(a)(b)(c)(d)(e)(f)(g)(h); <a name="l00125"></a>00125 } <a name="l00126"></a>00126 <a name="l00127"></a><a class="code" href="class_s_a_f_e_r_1_1_dec.html#ae6bc60a72b987972953da0e386e01dd0">00127</a> <span class="keywordtype">void</span> <a class="code" href="class_s_a_f_e_r_1_1_dec.html#ae6bc60a72b987972953da0e386e01dd0" title="encrypt or decrypt inBlock, xor with xorBlock, and write to outBlock">SAFER::Dec::ProcessAndXorBlock</a>(<span class="keyword">const</span> byte *inBlock, <span class="keyword">const</span> byte *xorBlock, byte *outBlock)<span class="keyword"> const</span> <a name="l00128"></a>00128 <span class="keyword"></span>{ <a name="l00129"></a>00129 byte a, b, c, d, e, f, g, h, t; <a name="l00130"></a>00130 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> round = keySchedule[0]; <a name="l00131"></a>00131 <span class="keyword">const</span> byte *key = keySchedule + BLOCKSIZE * (1 + 2 * round) - 7; <a name="l00132"></a>00132 <a name="l00133"></a>00133 Block::Get(inBlock)(a)(b)(c)(d)(e)(f)(g)(h); <a name="l00134"></a>00134 h ^= key[7]; g -= key[6]; f -= key[5]; e ^= key[4]; <a name="l00135"></a>00135 d ^= key[3]; c -= key[2]; b -= key[1]; a ^= key[0]; <a name="l00136"></a>00136 <span class="keywordflow">while</span> (round--) <a name="l00137"></a>00137 { <a name="l00138"></a>00138 key -= 16; <a name="l00139"></a>00139 t = e; e = b; b = c; c = t; t = f; f = d; d = g; g = t; <a name="l00140"></a>00140 IPHT(a, e); IPHT(b, f); IPHT(c, g); IPHT(d, h); <a name="l00141"></a>00141 IPHT(a, c); IPHT(e, g); IPHT(b, d); IPHT(f, h); <a name="l00142"></a>00142 IPHT(a, b); IPHT(c, d); IPHT(e, f); IPHT(g, h); <a name="l00143"></a>00143 h -= key[15]; g ^= key[14]; f ^= key[13]; e -= key[12]; <a name="l00144"></a>00144 d -= key[11]; c ^= key[10]; b ^= key[9]; a -= key[8]; <a name="l00145"></a>00145 h = LOG(h) ^ key[7]; g = EXP(g) - key[6]; <a name="l00146"></a>00146 f = EXP(f) - key[5]; e = LOG(e) ^ key[4]; <a name="l00147"></a>00147 d = LOG(d) ^ key[3]; c = EXP(c) - key[2]; <a name="l00148"></a>00148 b = EXP(b) - key[1]; a = LOG(a) ^ key[0]; <a name="l00149"></a>00149 } <a name="l00150"></a>00150 <a class="code" href="class_put_block.html">Block::Put</a>(xorBlock, outBlock)(a)(b)(c)(d)(e)(f)(g)(h); <a name="l00151"></a>00151 } <a name="l00152"></a>00152 <a name="l00153"></a>00153 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>