📄 gfpcrypt_8cpp-source.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"><title>Crypto++: gfpcrypt.cpp Source File</title><link href="doxygen.css" rel="stylesheet" type="text/css"><link href="tabs.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.5.2 --><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="classes.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>gfpcrypt.cpp</h1><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">// dsa.cpp - written and placed in the public domain by Wei Dai</span><a name="l00002"></a>00002 <a name="l00003"></a>00003 <span class="preprocessor">#include "pch.h"</span><a name="l00004"></a>00004 <a name="l00005"></a>00005 <span class="preprocessor">#ifndef CRYPTOPP_IMPORTS</span><a name="l00006"></a>00006 <span class="preprocessor"></span><a name="l00007"></a>00007 <span class="preprocessor">#include "<a class="code" href="gfpcrypt_8h.html" title="Implementation of schemes based on DL over GF(p).">gfpcrypt.h</a>"</span><a name="l00008"></a>00008 <span class="preprocessor">#include "asn.h"</span><a name="l00009"></a>00009 <span class="preprocessor">#include "oids.h"</span><a name="l00010"></a>00010 <span class="preprocessor">#include "nbtheory.h"</span><a name="l00011"></a>00011 <a name="l00012"></a>00012 NAMESPACE_BEGIN(CryptoPP)<a name="l00013"></a>00013 <a name="l00014"></a>00014 void TestInstantiations_gfpcrypt()<a name="l00015"></a>00015 {<a name="l00016"></a>00016 <a class="code" href="struct_g_d_s_a.html" title="DSA-1363">GDSA<SHA>::Signer</a> test;<a name="l00017"></a>00017 <a class="code" href="struct_g_d_s_a.html" title="DSA-1363">GDSA<SHA>::Verifier</a> test1;<a name="l00018"></a>00018 <a class="code" href="class_p_k___final_template.html" title="A template implementing constructors for public key algorithm classes.">DSA::Signer</a> test5(NullRNG(), 100);<a name="l00019"></a>00019 <a class="code" href="class_p_k___final_template.html" title="A template implementing constructors for public key algorithm classes.">DSA::Signer</a> test2(test5);<a name="l00020"></a>00020 <a class="code" href="struct_n_r.html" title="NR">NR<SHA>::Signer</a> test3;<a name="l00021"></a>00021 <a class="code" href="struct_n_r.html" title="NR">NR<SHA>::Verifier</a> test4;<a name="l00022"></a>00022 <a class="code" href="struct_d_l_i_e_s.html" title="Discrete Log Integrated Encryption Scheme, AKA DLIES.">DLIES<>::Encryptor</a> test6;<a name="l00023"></a>00023 <a class="code" href="struct_d_l_i_e_s.html" title="Discrete Log Integrated Encryption Scheme, AKA DLIES.">DLIES<>::Decryptor</a> test7;<a name="l00024"></a>00024 }<a name="l00025"></a>00025 <a name="l00026"></a><a class="code" href="class_d_l___group_parameters___d_s_a.html#c83d3668c342b3a1fb367ed7785e27a3">00026</a> <span class="keywordtype">void</span> <a class="code" href="class_d_l___group_parameters___d_s_a.html#c83d3668c342b3a1fb367ed7785e27a3">DL_GroupParameters_DSA::GenerateRandom</a>(<a class="code" href="class_random_number_generator.html" title="interface for random number generators">RandomNumberGenerator</a> &rng, <span class="keyword">const</span> <a class="code" href="class_name_value_pairs.html" title="interface for retrieving values given their names">NameValuePairs</a> &alg)<a name="l00027"></a>00027 {<a name="l00028"></a>00028 <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> p, q, g;<a name="l00029"></a>00029 <a name="l00030"></a>00030 <span class="keywordflow">if</span> (alg.<a class="code" href="class_name_value_pairs.html#96686e9f8d6ce3ab870e516fb72b608e" title="get a named value, returns true if the name exists">GetValue</a>(<span class="stringliteral">"Modulus"</span>, p) && alg.<a class="code" href="class_name_value_pairs.html#96686e9f8d6ce3ab870e516fb72b608e" title="get a named value, returns true if the name exists">GetValue</a>(<span class="stringliteral">"SubgroupGenerator"</span>, g))<a name="l00031"></a>00031 {<a name="l00032"></a>00032 q = alg.<a class="code" href="class_name_value_pairs.html#943b2009297783f1c35bae46efc3b5f7" title="get a named value, returns the default if the name doesn't exist">GetValueWithDefault</a>(<span class="stringliteral">"SubgroupOrder"</span>, <a class="code" href="class_d_l___group_parameters___integer_based.html#05c2b65749d0357845599a7d434e3903">ComputeGroupOrder</a>(p)/2);<a name="l00033"></a>00033 }<a name="l00034"></a>00034 <span class="keywordflow">else</span><a name="l00035"></a>00035 {<a name="l00036"></a>00036 <span class="keywordtype">int</span> modulusSize = 1024;<a name="l00037"></a>00037 alg.<a class="code" href="class_name_value_pairs.html#39b6daefcabcdd07f5ae482a075e1728" title="get a named value with type int">GetIntValue</a>(<span class="stringliteral">"ModulusSize"</span>, modulusSize) || alg.<a class="code" href="class_name_value_pairs.html#39b6daefcabcdd07f5ae482a075e1728" title="get a named value with type int">GetIntValue</a>(<span class="stringliteral">"KeySize"</span>, modulusSize);<a name="l00038"></a>00038 <a name="l00039"></a>00039 <span class="keywordflow">if</span> (!<a class="code" href="struct_d_s_a.html#d810c437ef4b830229aeed410621381f">DSA::IsValidPrimeLength</a>(modulusSize))<a name="l00040"></a>00040 <span class="keywordflow">throw</span> <a class="code" href="class_invalid_argument.html" title="exception thrown when an invalid argument is detected">InvalidArgument</a>(<span class="stringliteral">"DSA: not a valid prime length"</span>);<a name="l00041"></a>00041 <a name="l00042"></a>00042 <a class="code" href="class_sec_block.html" title="a block of memory allocated using A">SecByteBlock</a> seed(<a class="code" href="class_iterated_hash_with_static_transform.html#86286d03ba76ccdd7bf3007088250859">SHA::DIGESTSIZE</a>);<a name="l00043"></a>00043 <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> h;<a name="l00044"></a>00044 <span class="keywordtype">int</span> c;<a name="l00045"></a>00045 <a name="l00046"></a>00046 <span class="keywordflow">do</span><a name="l00047"></a>00047 {<a name="l00048"></a>00048 rng.<a class="code" href="class_random_number_generator.html#497145546d24e6d4abaf10b7e0f1ba17" title="generate random array of bytes">GenerateBlock</a>(seed, <a class="code" href="class_iterated_hash_with_static_transform.html#86286d03ba76ccdd7bf3007088250859">SHA::DIGESTSIZE</a>);<a name="l00049"></a>00049 } <span class="keywordflow">while</span> (!<a class="code" href="struct_d_s_a.html#48eab0da4234a2d20f3ef7250fbad5f5" title="Generate DSA primes according to NIST standard.">DSA::GeneratePrimes</a>(seed, <a class="code" href="class_iterated_hash_with_static_transform.html#86286d03ba76ccdd7bf3007088250859">SHA::DIGESTSIZE</a>*8, c, p, modulusSize, q));<a name="l00050"></a>00050 <a name="l00051"></a>00051 <span class="keywordflow">do</span><a name="l00052"></a>00052 {<a name="l00053"></a>00053 h.<a class="code" href="class_integer.html#0f0574b9cae3cddf62c155da93085f0d">Randomize</a>(rng, 2, p-2);<a name="l00054"></a>00054 g = a_exp_b_mod_c(h, (p-1)/q, p);<a name="l00055"></a>00055 } <span class="keywordflow">while</span> (g <= 1);<a name="l00056"></a>00056 }<a name="l00057"></a>00057 <a name="l00058"></a>00058 <a class="code" href="class_d_l___group_parameters___integer_based.html#c0a0277c8d9d397cc30722aac33ad1b6">Initialize</a>(p, q, g);<a name="l00059"></a>00059 }<a name="l00060"></a>00060 <a name="l00061"></a><a class="code" href="class_d_l___group_parameters___d_s_a.html#a6928614d0438d0ec6bbac7d5e4289a1">00061</a> <span class="keywordtype">bool</span> <a class="code" href="class_d_l___group_parameters___d_s_a.html#a6928614d0438d0ec6bbac7d5e4289a1">DL_GroupParameters_DSA::ValidateGroup</a>(<a class="code" href="class_random_number_generator.html" title="interface for random number generators">RandomNumberGenerator</a> &rng, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> level)<span class="keyword"> const</span><a name="l00062"></a>00062 <span class="keyword"></span>{<a name="l00063"></a>00063 <span class="keywordtype">bool</span> pass = <a class="code" href="class_d_l___group_parameters___integer_based.html#be42dee24455ea7aac8edee78012cbad">DL_GroupParameters_GFP::ValidateGroup</a>(rng, level);<a name="l00064"></a>00064 pass = pass && <a class="code" href="struct_d_s_a.html#d810c437ef4b830229aeed410621381f">DSA::IsValidPrimeLength</a>(<a class="code" href="class_d_l___group_parameters___integer_based_impl.html#2c3b85d7565e222bb5492ae7d141ec85">GetModulus</a>().BitCount());<a name="l00065"></a>00065 pass = pass && <a class="code" href="class_d_l___group_parameters___integer_based.html#e6eb7fd2937f5eee9740d0db173f0475">GetSubgroupOrder</a>().<a class="code" href="class_integer.html#867356d88074424328d0ebb9bea63254" title="number of significant bits = floor(log2(abs(*this))) + 1">BitCount</a>() == 160;<a name="l00066"></a>00066 <span class="keywordflow">return</span> pass;<a name="l00067"></a>00067 }<a name="l00068"></a>00068 <a name="l00069"></a><a class="code" href="class_d_l___signature_message_encoding_method___d_s_a.html#59b2500db20a98787fecb272bc0754fe">00069</a> <span class="keywordtype">void</span> <a class="code" href="class_d_l___signature_message_encoding_method___d_s_a.html#59b2500db20a98787fecb272bc0754fe">DL_SignatureMessageEncodingMethod_DSA::ComputeMessageRepresentative</a>(<a class="code" href="class_random_number_generator.html" title="interface for random number generators">RandomNumberGenerator</a> &rng, <a name="l00070"></a>00070 <span class="keyword">const</span> byte *recoverableMessage, <span class="keywordtype">size_t</span> recoverableMessageLength,<a name="l00071"></a>00071 <a class="code" href="class_hash_transformation.html" title="interface for hash functions and data processing part of MACs">HashTransformation</a> &hash, <a class="code" href="pubkey_8h.html#ef874ed3f2d212ac4629255c8ef86dd0">HashIdentifier</a> hashIdentifier, <span class="keywordtype">bool</span> messageEmpty,<a name="l00072"></a>00072 byte *representative, <span class="keywordtype">size_t</span> representativeBitLength)<span class="keyword"> const</span><a name="l00073"></a>00073 <span class="keyword"></span>{<a name="l00074"></a>00074 assert(recoverableMessageLength == 0);<a name="l00075"></a>00075 assert(hashIdentifier.second == 0);<a name="l00076"></a>00076 <span class="keyword">const</span> <span class="keywordtype">size_t</span> representativeByteLength = BitsToBytes(representativeBitLength);<a name="l00077"></a>00077 <span class="keyword">const</span> <span class="keywordtype">size_t</span> digestSize = hash.<a class="code" href="class_hash_transformation.html#41faf8b1293a6c72390cb0c8bd668097" title="size of the hash returned by Final()">DigestSize</a>();<a name="l00078"></a>00078 <span class="keyword">const</span> <span class="keywordtype">size_t</span> paddingLength = SaturatingSubtract(representativeByteLength, digestSize);<a name="l00079"></a>00079 <a name="l00080"></a>00080 memset(representative, 0, paddingLength);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -