gfpcrypt_8cpp-source.html
来自「Crypto++是一个非常强大的密码学库,主要是功能全」· HTML 代码 · 共 268 行 · 第 1/2 页
HTML
268 行
<!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++: gfpcrypt.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>gfpcrypt.cpp</h1><div class="fragment"><pre>00001 <span class="comment">// dsa.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="gfpcrypt_8h.html">gfpcrypt.h</a>"</span>00008 <span class="preprocessor">#include "asn.h"</span>00009 <span class="preprocessor">#include "oids.h"</span>00010 <span class="preprocessor">#include "nbtheory.h"</span>00011 00012 NAMESPACE_BEGIN(CryptoPP)00013 00014 <span class="keywordtype">void</span> TestInstantiations_gfpcrypt()00015 {00016 <a class="code" href="struct_g_d_s_a.html">GDSA<SHA></a>::Signer test;00017 <a class="code" href="struct_g_d_s_a.html">GDSA<SHA></a>::Verifier test1;00018 <a class="code" href="class_d_l___s_s_a.html#_d_l___s_s_aw0">DSA::Signer</a> test5(NullRNG(), 100);00019 <a class="code" href="class_d_l___s_s_a.html#_d_l___s_s_aw0">DSA::Signer</a> test2(test5);00020 <a class="code" href="struct_n_r.html">NR<SHA></a>::Signer test3;00021 <a class="code" href="struct_n_r.html">NR<SHA></a>::Verifier test4;00022 <a class="code" href="struct_d_l_i_e_s.html">DLIES<></a>::Encryptor test6;00023 <a class="code" href="struct_d_l_i_e_s.html">DLIES<></a>::Decryptor test7;00024 }00025 <a name="l00026"></a><a class="code" href="class_d_l___group_parameters___d_s_a.html#_d_l___group_parameters___d_s_aa1">00026</a> <span class="keywordtype">void</span> <a class="code" href="class_d_l___group_parameters___d_s_a.html#_d_l___group_parameters___d_s_aa1">DL_GroupParameters_DSA::GenerateRandom</a>(<a class="code" href="class_random_number_generator.html">RandomNumberGenerator</a> &rng, <span class="keyword">const</span> <a class="code" href="class_name_value_pairs.html">NameValuePairs</a> &alg)00027 {00028 <a class="code" href="class_integer.html">Integer</a> p, q, g;00029 00030 <span class="keywordflow">if</span> (alg.<a class="code" href="class_name_value_pairs.html#_x_t_r___d_ha39">GetValue</a>(<span class="stringliteral">"Modulus"</span>, p) && alg.<a class="code" href="class_name_value_pairs.html#_x_t_r___d_ha39">GetValue</a>(<span class="stringliteral">"SubgroupGenerator"</span>, g))00031 {00032 q = alg.<a class="code" href="class_name_value_pairs.html#_x_t_r___d_ha40">GetValueWithDefault</a>(<span class="stringliteral">"SubgroupOrder"</span>, ComputeGroupOrder(p)/2);00033 }00034 <span class="keywordflow">else</span>00035 {00036 <span class="keywordtype">int</span> modulusSize = 1024;00037 alg.<a class="code" href="class_name_value_pairs.html#_x_t_r___d_ha42">GetIntValue</a>(<span class="stringliteral">"ModulusSize"</span>, modulusSize) || alg.<a class="code" href="class_name_value_pairs.html#_x_t_r___d_ha42">GetIntValue</a>(<span class="stringliteral">"KeySize"</span>, modulusSize);00038 00039 <span class="keywordflow">if</span> (!DSA::IsValidPrimeLength(modulusSize))00040 <span class="keywordflow">throw</span> <a class="code" href="class_invalid_argument.html">InvalidArgument</a>(<span class="stringliteral">"DSA: not a valid prime length"</span>);00041 00042 <a class="code" href="class_sec_block.html">SecByteBlock</a> seed(SHA::DIGESTSIZE);00043 <a class="code" href="class_integer.html">Integer</a> h;00044 <span class="keywordtype">int</span> c;00045 00046 <span class="keywordflow">do</span>00047 {00048 rng.<a class="code" href="class_random_number_generator.html#_x917_r_n_ga4">GenerateBlock</a>(seed, SHA::DIGESTSIZE);00049 } <span class="keywordflow">while</span> (!<a class="code" href="struct_d_s_a.html#_d_s_ae1">DSA::GeneratePrimes</a>(seed, SHA::DIGESTSIZE*8, c, p, modulusSize, q));00050 00051 <span class="keywordflow">do</span>00052 {00053 h.Randomize(rng, 2, p-2);00054 g = a_exp_b_mod_c(h, (p-1)/q, p);00055 } <span class="keywordflow">while</span> (g <= 1);00056 }00057 00058 Initialize(p, q, g);00059 }00060 <a name="l00061"></a><a class="code" href="class_d_l___group_parameters___d_s_a.html#_d_l___group_parameters___d_s_aa0">00061</a> <span class="keywordtype">bool</span> <a class="code" href="class_d_l___group_parameters___d_s_a.html#_d_l___group_parameters___d_s_aa0">DL_GroupParameters_DSA::ValidateGroup</a>(<a class="code" href="class_random_number_generator.html">RandomNumberGenerator</a> &rng, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> level)<span class="keyword"> const</span>00062 <span class="keyword"></span>{00063 <span class="keywordtype">bool</span> pass = DL_GroupParameters_GFP::ValidateGroup(rng, level);00064 pass = pass && DSA::IsValidPrimeLength(GetModulus().BitCount());00065 pass = pass && GetSubgroupOrder().<a class="code" href="class_integer.html#_integerz41_2">BitCount</a>() == 160;00066 <span class="keywordflow">return</span> pass;00067 }00068 00069 <a class="code" href="class_integer.html">Integer</a> NR_EncodeDigest(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> modulusBits, <span class="keyword">const</span> byte *digest, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> digestLen)00070 {00071 <a class="code" href="class_integer.html">Integer</a> h;00072 <span class="keywordflow">if</span> (digestLen*8 < modulusBits)00073 h.Decode(digest, digestLen);00074 <span class="keywordflow">else</span>00075 {00076 h.Decode(digest, BitsToBytes(modulusBits));00077 h >>= BitsToBytes(modulusBits)*8 - modulusBits + 1;00078 }00079 <span class="keywordflow">return</span> h;00080 }00081 00082 <a class="code" href="class_integer.html">Integer</a> DSA_EncodeDigest(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> modulusBits, <span class="keyword">const</span> byte *digest, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> digestLen)00083 {00084 <a class="code" href="class_integer.html">Integer</a> h;00085 <span class="keywordflow">if</span> (digestLen*8 <= modulusBits)00086 h.Decode(digest, digestLen);00087 <span class="keywordflow">else</span>00088 {00089 h.Decode(digest, BitsToBytes(modulusBits));00090 h >>= BitsToBytes(modulusBits)*8 - modulusBits;00091 }00092 <span class="keywordflow">return</span> h;00093 }00094 00095 <span class="keywordtype">bool</span> DL_GroupParameters_IntegerBased::ValidateGroup(<a class="code" href="class_random_number_generator.html">RandomNumberGenerator</a> &rng, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> level)<span class="keyword"> const</span>00096 <span class="keyword"></span>{00097 <span class="keyword">const</span> <a class="code" href="class_integer.html">Integer</a> &p = GetModulus(), &q = GetSubgroupOrder();00098 00099 <span class="keywordtype">bool</span> pass = <span class="keyword">true</span>;00100 pass = pass && p > <a class="code" href="class_integer.html#_integerz37_11">Integer::One</a>() && p.<a class="code" href="class_integer.html#_integerz41_15">IsOdd</a>();00101 pass = pass && q > <a class="code" href="class_integer.html#_integerz37_11">Integer::One</a>() && q.<a class="code" href="class_integer.html#_integerz41_15">IsOdd</a>();00102 00103 <span class="keywordflow">if</span> (level >= 1)00104 pass = pass && GetCofactor() > <a class="code" href="class_integer.html#_integerz37_11">Integer::One</a>() && GetGroupOrder() % q == <a class="code" href="class_integer.html#_integerz37_10">Integer::Zero</a>();00105 <span class="keywordflow">if</span> (level >= 2)00106 pass = pass && VerifyPrime(rng, q, level-2) && VerifyPrime(rng, p, level-2);00107 00108 <span class="keywordflow">return</span> pass;00109 }00110 00111 <span class="keywordtype">bool</span> DL_GroupParameters_IntegerBased::ValidateElement(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> level, <span class="keyword">const</span> <a class="code" href="class_integer.html">Integer</a> &g, <span class="keyword">const</span> DL_FixedBasePrecomputation<Integer> *gpc)<span class="keyword"> const</span>00112 <span class="keyword"></span>{00113 <span class="keyword">const</span> <a class="code" href="class_integer.html">Integer</a> &p = GetModulus(), &q = GetSubgroupOrder();00114 00115 <span class="keywordtype">bool</span> pass = <span class="keyword">true</span>;00116 pass = pass && GetFieldType() == 1 ? g.IsPositive() : g.NotNegative();00117 pass = pass && g < p && !IsIdentity(g);00118 00119 <span class="keywordflow">if</span> (level >= 1)00120 {00121 <span class="keywordflow">if</span> (gpc)00122 pass = pass && gpc->Exponentiate(GetGroupPrecomputation(), Integer::One()) == g;00123 }00124 <span class="keywordflow">if</span> (level >= 2)00125 {00126 <span class="keywordflow">if</span> (GetFieldType() == 2)00127 pass = pass && Jacobi(g*g-4, p)==-1;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?