-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdsa_verify.c
More file actions
158 lines (132 loc) · 5.86 KB
/
Copy pathdsa_verify.c
File metadata and controls
158 lines (132 loc) · 5.86 KB
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
/*
* dsa_verify - http://opensource.implicit-link.com/
* Copyright (c) 2010 Implicit Link
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include <stdio.h>
#include "mp_math.h"
#include "sha1.h"
#include "dsa_verify.h"
#define MP_OP(op) if ((ret = (op)) != MP_OKAY) goto error;
/*char *debugMp (mp_int *d)
{
char *ret = malloc(1024); // leak leak
mp_tohex(d, ret);
return ret;
}*/
int _dsa_verify_hash (mp_int *r, mp_int *s, mp_int *hash,
mp_int *keyG, mp_int *keyP, mp_int *keyQ, mp_int *keyY)
{
mp_int w, v, u1, u2;
int ret;
MP_OP(mp_init_multi(&w, &v, &u1, &u2, NULL));
// neither r or s can be 0 or >q
if (mp_iszero(r) == MP_YES || mp_iszero(s) == MP_YES || mp_cmp(r, keyQ) != MP_LT || mp_cmp(s, keyQ) != MP_LT) {
ret = -1;
goto error;
}
// w = 1/s mod q
MP_OP(mp_invmod(s, keyQ, &w));
// u1 = m * w mod q
MP_OP(mp_mulmod(hash, &w, keyQ, &u1));
// u2 = r*w mod q
MP_OP(mp_mulmod(r, &w, keyQ, &u2));
// v = g^u1 * y^u2 mod p mod q
MP_OP(mp_exptmod(keyG, &u1, keyP, &u1));
MP_OP(mp_exptmod(keyY, &u2, keyP, &u2));
MP_OP(mp_mulmod(&u1, &u2, keyP, &v));
MP_OP(mp_mod(&v, keyQ, &v));
// if r = v then we're set
ret = 0;
if (mp_cmp(r, &v) == MP_EQ) ret = 1;
error:
mp_clear_multi(&w, &v, &u1, &u2, NULL);
return ret;
}
const unsigned char *read_key(mp_int* keyPart, const unsigned char* keyData)
{
int len = keyData[0]*256 + keyData[1];
mp_read_unsigned_bin(keyPart, keyData+2, len);
return keyData+len+2;
}
int dsa_verify_blob(const char *data, int dataLen, const unsigned char* keyData, const char* sigR, const char* sigS)
{
// dss1 hashing algorithm is actually sha1
SHA1Context sha1;
uint8_t sha1sum[SHA1HashSize];
SHA1Reset(&sha1);
SHA1Input(&sha1, (const unsigned char *) data, dataLen);
SHA1Result(&sha1, sha1sum);
mp_int hash, keyG, keyP, keyQ, keyY, r, s;
mp_init_multi(&hash, &keyG, &keyP, &keyQ, &keyY, &r, &s, NULL);
mp_read_unsigned_bin(&hash, sha1sum, sizeof(sha1sum));
keyData = read_key(&keyY, keyData);
keyData = read_key(&keyP, keyData);
keyData = read_key(&keyQ, keyData);
keyData = read_key(&keyG, keyData);
mp_read_radix(&r, sigR, 16);
mp_read_radix(&s, sigS, 16);
return _dsa_verify_hash(&r, &s, &hash, &keyG, &keyP, &keyQ, &keyY);
}
#ifdef TEST
int main()
{
char *message = \
"I think computer viruses should count as life. I think it\n" \
" says something about human nature that the only form of\n" \
" life we have created so far is purely destructive. We've\n" \
" created life in our own image.\n";
const char publicKey[] = {
0x00,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0x13, 0x9a, 0x55,
0x91, 0x8a, 0x44, 0x45, 0x9d, 0x5e, 0x5e, 0xa1, 0x42, 0x94, 0x45, 0x1e, 0x58, 0x27, 0xe9, 0x3e,
0x45, 0x8a, 0x0b, 0xf6, 0x05, 0x01, 0x0a, 0xa2, 0x0d, 0xbd, 0x1a, 0xd3, 0x61, 0x8e, 0xa7, 0x38,
0x69, 0xf8, 0x0c, 0x90, 0xf8, 0x75, 0xb3, 0xfe, 0x6b, 0x18, 0xce, 0x5a, 0x69, 0x8b, 0x84, 0xd2,
0x5a
};
return dsa_verify_blob(
message, strlen(message),
publicKey,
"D795D68F0CFB19F8A5C042B6427DB8132D1403D1",
"5D1E9010B9B0605BA6F0983CF49A14FD6F18892D");
}
#endif