-
Notifications
You must be signed in to change notification settings - Fork 8
/
CDS.cs
188 lines (170 loc) · 6.11 KB
/
CDS.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* Описание класса цифровой подписи.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DS_GOST_34._10_2012
{
class CDS
{
private BigInteger a = new BigInteger();
private BigInteger b = new BigInteger();
private BigInteger n = new BigInteger();
private BigInteger p = new BigInteger();
private byte[] xG;
private CECPoint G = new CECPoint();
public CDS(BigInteger p, BigInteger a, BigInteger b, BigInteger n, byte[] xG)
{
this.a = a;
this.b = b;
this.n = n;
this.p = p;
this.xG = xG;
}
//генерация секретного ключа заданной длины.
public BigInteger genPrivateKey(int BitSize)
{
BigInteger d = new BigInteger();
do
{
d.genRandomBits(BitSize, new Random());
} while ((d < 0) || (d > n));
return d;
}
//генерация публичного ключа (с помощью секретного).
public CECPoint genPublicKey(BigInteger d)
{
CECPoint G=gDecompression();
CECPoint Q = CECPoint.multiply(G, d);
return Q;
}
//восстановление координат Y из координаты X и бита четности Y.
private CECPoint gDecompression()
{
byte y = xG[0];
byte[] x=new byte[xG.Length-1];
Array.Copy(xG, 1, x, 0, xG.Length - 1);
BigInteger Xcord = new BigInteger(x);
BigInteger temp = (Xcord * Xcord * Xcord + a * Xcord + b) % p;
BigInteger beta = modSqrt(temp, p);
BigInteger Ycord = new BigInteger();
if ((beta % 2) == (y % 2))
Ycord = beta;
else
Ycord = p - beta;
CECPoint G = new CECPoint();
G.a = a;
G.b = b;
G.fieldChar = p;
G.x = Xcord;
G.y = Ycord;
this.G = G;
return G;
}
//вычисление квадратоного корня по модулю простого числа q.
public BigInteger modSqrt(BigInteger a, BigInteger q)
{
BigInteger b = new BigInteger();
do
{
b.genRandomBits(255, new Random());
}
while (legendre(b, q) == 1);
BigInteger s = 0;
BigInteger t = q - 1;
while ((t & 1) != 1)
{
s++;
t = t >> 1;
}
BigInteger InvA = a.modInverse(q);
BigInteger c = b.modPow(t, q);
BigInteger r = a.modPow(((t + 1) / 2), q);
BigInteger d = new BigInteger();
for (int i = 1; i < s; i++)
{
BigInteger temp = 2;
temp = temp.modPow((s - i - 1), q);
d = (r.modPow(2, q) * InvA).modPow(temp, q);
if (d == (q - 1))
r = (r * c) % q;
c = c.modPow(2, q);
}
return r;
}
//вычисление символа Лежандра.
public BigInteger legendre(BigInteger a, BigInteger q)
{
return a.modPow((q - 1) / 2, q);
}
//формирование цифровой подписи.
public string genDS(byte[] h, BigInteger d)
{
BigInteger a = new BigInteger(h);
BigInteger e = a % n;
if (e == 0)
e = 1;
BigInteger k = new BigInteger();
CECPoint C=new CECPoint();
BigInteger r=new BigInteger();
BigInteger s = new BigInteger();
do
{
do
{
k.genRandomBits(n.bitCount(), new Random());
}
while ((k < 0) || (k > n));
C = CECPoint.multiply(G, k);
r = C.x % n;
s = ((r * d) + (k * e)) % n;
}
while ((r == 0)||(s==0));
string Rvector = padding(r.ToHexString(), n.bitCount() / 4);
string Svector = padding(s.ToHexString(), n.bitCount() / 4);
return Rvector + Svector;
}
//проверка цифровой подписи.
public bool verifDS(byte[] H, string sign, CECPoint Q)
{
string Rvector = sign.Substring(0, n.bitCount() / 4);
string Svector = sign.Substring(n.bitCount() / 4, n.bitCount() / 4);
BigInteger r = new BigInteger(Rvector, 16);
BigInteger s = new BigInteger(Svector, 16);
if ((r < 1) || (r > (n - 1)) || (s < 1) || (s > (n - 1)))
return (false);
BigInteger a = new BigInteger(H);
BigInteger e = a % n;
if (e == 0)
e = 1;
BigInteger v = e.modInverse(n);
BigInteger z1 = (s * v) % n;
BigInteger z2 = n + ((-(r * v)) % n);
this.G = gDecompression();
CECPoint A = CECPoint.multiply(G, z1);
CECPoint B = CECPoint.multiply(Q, z2);
CECPoint C = A + B;
BigInteger R = C.x % n;
if (R == r)
return (true);
else
return (false);
}
//дополнить подпись нулями слева до длины n,
// где n - длина модуля в битах.
private string padding(string input, int size)
{
if (input.Length < size)
{
do
{
input = "0" + input;
}
while (input.Length < size);
}
return (input);
}
}
}