cryptlib_8cpp-source.html
来自「Crypto++是一个非常强大的密码学库,主要是功能全」· HTML 代码 · 共 649 行 · 第 1/5 页
HTML
649 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"><title>Crypto++: cryptlib.cpp Source File</title><link href="doxygen.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.3.2 --><div class="qindex"><a class="qindex" href="index.html">Main Page</a> | <a class="qindex" href="namespaces.html">Namespace List</a> | <a class="qindex" href="hierarchy.html">Class Hierarchy</a> | <a class="qindex" href="classes.html">Alphabetical List</a> | <a class="qindex" href="annotated.html">Compound List</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="namespacemembers.html">Namespace Members</a> | <a class="qindex" href="functions.html">Compound Members</a> | <a class="qindex" href="globals.html">File Members</a></div><h1>cryptlib.cpp</h1><div class="fragment"><pre>00001 <span class="comment">// cryptlib.cpp - written and placed in the public domain by Wei Dai</span>00002 00003 <span class="preprocessor">#include "pch.h"</span>00004 00005 <span class="preprocessor">#ifndef CRYPTOPP_IMPORTS</span>00006 <span class="preprocessor"></span>00007 <span class="preprocessor">#include "<a class="code" href="cryptlib_8h.html">cryptlib.h</a>"</span>00008 <span class="preprocessor">#include "misc.h"</span>00009 <span class="preprocessor">#include "filters.h"</span>00010 <span class="preprocessor">#include "algparam.h"</span>00011 <span class="preprocessor">#include "<a class="code" href="fips140_8h.html">fips140.h</a>"</span>00012 <span class="preprocessor">#include "argnames.h"</span>00013 00014 <span class="preprocessor">#include <memory></span>00015 00016 NAMESPACE_BEGIN(CryptoPP)00017 00018 CRYPTOPP_COMPILE_ASSERT(<span class="keyword">sizeof</span>(byte) == 1);00019 CRYPTOPP_COMPILE_ASSERT(<span class="keyword">sizeof</span>(word16) == 2);00020 CRYPTOPP_COMPILE_ASSERT(<span class="keyword">sizeof</span>(word32) == 4);00021 <span class="preprocessor">#ifdef WORD64_AVAILABLE</span>00022 <span class="preprocessor"></span>CRYPTOPP_COMPILE_ASSERT(<span class="keyword">sizeof</span>(word64) == 8);00023 <span class="preprocessor">#endif</span>00024 <span class="preprocessor"></span>CRYPTOPP_COMPILE_ASSERT(<span class="keyword">sizeof</span>(dword) == 2*<span class="keyword">sizeof</span>(word));00025 00026 <span class="keyword">const</span> std::string BufferedTransformation::NULL_CHANNEL;00027 <span class="keyword">const</span> <a class="code" href="class_null_name_value_pairs.html">NullNameValuePairs</a> g_nullNameValuePairs;00028 00029 <a class="code" href="class_buffered_transformation.html">BufferedTransformation</a> & TheBitBucket()00030 {00031 <span class="keyword">static</span> BitBucket bitBucket;00032 <span class="keywordflow">return</span> bitBucket;00033 }00034 <a name="l00035"></a><a class="code" href="class_algorithm.html#_algorithma0">00035</a> <a class="code" href="class_algorithm.html#_algorithma0">Algorithm::Algorithm</a>(<span class="keywordtype">bool</span> checkSelfTestStatus)00036 {00037 <span class="keywordflow">if</span> (checkSelfTestStatus && FIPS_140_2_ComplianceEnabled())00038 {00039 <span class="keywordflow">if</span> (GetPowerUpSelfTestStatus() == POWER_UP_SELF_TEST_NOT_DONE && !PowerUpSelfTestInProgressOnThisThread())00040 <span class="keywordflow">throw</span> <a class="code" href="class_self_test_failure.html">SelfTestFailure</a>(<span class="stringliteral">"Cryptographic algorithms are disabled before the power-up self tests are performed."</span>);00041 00042 <span class="keywordflow">if</span> (GetPowerUpSelfTestStatus() == POWER_UP_SELF_TEST_FAILED)00043 <span class="keywordflow">throw</span> <a class="code" href="class_self_test_failure.html">SelfTestFailure</a>(<span class="stringliteral">"Cryptographic algorithms are disabled after a power-up self test failed."</span>);00044 }00045 }00046 <a name="l00047"></a><a class="code" href="class_simple_keying_interface.html#_x_m_a_c_c___basea27">00047</a> <span class="keywordtype">void</span> <a class="code" href="class_simple_keying_interface.html#_x_m_a_c_c___basea27">SimpleKeyingInterface::SetKeyWithRounds</a>(<span class="keyword">const</span> byte *key, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> length, <span class="keywordtype">int</span> rounds)00048 {00049 <a class="code" href="class_simple_keying_interface.html#_x_m_a_c_c___basea26">SetKey</a>(key, length, MakeParameters(Name::Rounds(), rounds));00050 }00051 <a name="l00052"></a><a class="code" href="class_simple_keying_interface.html#_x_m_a_c_c___basea28">00052</a> <span class="keywordtype">void</span> <a class="code" href="class_simple_keying_interface.html#_x_m_a_c_c___basea28">SimpleKeyingInterface::SetKeyWithIV</a>(<span class="keyword">const</span> byte *key, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> length, <span class="keyword">const</span> byte *iv)00053 {00054 <a class="code" href="class_simple_keying_interface.html#_x_m_a_c_c___basea26">SetKey</a>(key, length, MakeParameters(Name::IV(), iv));00055 }00056 00057 <span class="keywordtype">void</span> SimpleKeyingInterface::ThrowIfInvalidKeyLength(<span class="keyword">const</span> <a class="code" href="class_algorithm.html">Algorithm</a> &algorithm, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> length)00058 {00059 <span class="keywordflow">if</span> (!<a class="code" href="class_simple_keying_interface.html#_x_m_a_c_c___basea25">IsValidKeyLength</a>(length))00060 <span class="keywordflow">throw</span> <a class="code" href="class_invalid_key_length.html">InvalidKeyLength</a>(algorithm.<a class="code" href="class_algorithm.html#_zlib_decompressora17">AlgorithmName</a>(), length);00061 }00062 <a name="l00063"></a><a class="code" href="class_block_transformation.html#_twofish_1_1_enca15">00063</a> <span class="keywordtype">void</span> <a class="code" href="class_block_transformation.html#_twofish_1_1_enca15">BlockTransformation::ProcessAndXorMultipleBlocks</a>(<span class="keyword">const</span> byte *inBlocks, <span class="keyword">const</span> byte *xorBlocks, byte *outBlocks, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> numberOfBlocks)<span class="keyword"> const</span>00064 <span class="keyword"></span>{00065 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> blockSize = <a class="code" href="class_block_transformation.html#_simple_keying_interface_impl_3_01_s_a_f_e_r_1_1_enc_00_01_s_a_f_e_r___s_k___info_01_4a10">BlockSize</a>();00066 <span class="keywordflow">while</span> (numberOfBlocks--)00067 {00068 <a class="code" href="class_block_transformation.html#_twofish_1_1_basea8">ProcessAndXorBlock</a>(inBlocks, xorBlocks, outBlocks);00069 inBlocks += blockSize;00070 outBlocks += blockSize;00071 <span class="keywordflow">if</span> (xorBlocks)00072 xorBlocks += blockSize;00073 }00074 }00075 <a name="l00076"></a><a class="code" href="class_stream_transformation.html#_symmetric_ciphera6">00076</a> <span class="keywordtype">void</span> <a class="code" href="class_stream_transformation.html#_symmetric_ciphera6">StreamTransformation::ProcessLastBlock</a>(byte *outString, <span class="keyword">const</span> byte *inString, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> length)00077 {00078 assert(<a class="code" href="class_stream_transformation.html#_symmetric_ciphera7">MinLastBlockSize</a>() == 0); <span class="comment">// this function should be overriden otherwise</span>00079 00080 <span class="keywordflow">if</span> (length == <a class="code" href="class_stream_transformation.html#_symmetric_ciphera1">MandatoryBlockSize</a>())00081 <a class="code" href="class_stream_transformation.html#_symmetric_ciphera5">ProcessData</a>(outString, inString, length);00082 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (length != 0)00083 <span class="keywordflow">throw</span> <a class="code" href="class_not_implemented.html">NotImplemented</a>(<span class="stringliteral">"StreamTransformation: this object does't support a special last block"</span>);00084 }00085 <a name="l00086"></a><a class="code" href="class_random_number_generator.html#_x917_r_n_ga2">00086</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="class_random_number_generator.html#_x917_r_n_ga2">RandomNumberGenerator::GenerateBit</a>()00087 {00088 <span class="keywordflow">return</span> Parity(<a class="code" href="class_random_number_generator.html#_sapphire_r_n_ga2">GenerateByte</a>());00089 }00090 <a name="l00091"></a><a class="code" href="class_random_number_generator.html#_x917_r_n_ga4">00091</a> <span class="keywordtype">void</span> <a class="code" href="class_random_number_generator.html#_x917_r_n_ga4">RandomNumberGenerator::GenerateBlock</a>(byte *output, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> size)00092 {00093 <span class="keywordflow">while</span> (size--)00094 *output++ = <a class="code" href="class_random_number_generator.html#_sapphire_r_n_ga2">GenerateByte</a>();00095 }00096 <a name="l00097"></a><a class="code" href="class_random_number_generator.html#_x917_r_n_ga3">00097</a> word32 <a class="code" href="class_random_number_generator.html#_x917_r_n_ga3">RandomNumberGenerator::GenerateWord32</a>(word32 min, word32 max)00098 {00099 word32 range = max-min;00100 <span class="keyword">const</span> <span class="keywordtype">int</span> maxBytes = BytePrecision(range);00101 <span class="keyword">const</span> <span class="keywordtype">int</span> maxBits = BitPrecision(range);00102 00103 word32 value;00104 00105 <span class="keywordflow">do</span>00106 {00107 value = 0;00108 <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i=0; i<maxBytes; i++)00109 value = (value << 8) | <a class="code" href="class_random_number_generator.html#_sapphire_r_n_ga2">GenerateByte</a>();00110 00111 value = Crop(value, maxBits);00112 } <span class="keywordflow">while</span> (value > range);00113 00114 <span class="keywordflow">return</span> value+min;00115 }00116 <a name="l00117"></a><a class="code" href="class_random_number_generator.html#_x917_r_n_ga5">00117</a> <span class="keywordtype">void</span> <a class="code" href="class_random_number_generator.html#_x917_r_n_ga5">RandomNumberGenerator::DiscardBytes</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> n)00118 {00119 <span class="keywordflow">while</span> (n--)00120 <a class="code" href="class_random_number_generator.html#_sapphire_r_n_ga2">GenerateByte</a>();00121 }00122 00123 <a class="code" href="class_random_number_generator.html">RandomNumberGenerator</a> & NullRNG()
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?