-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmultisig.c
More file actions
206 lines (195 loc) · 5.65 KB
/
multisig.c
File metadata and controls
206 lines (195 loc) · 5.65 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "multisig.h"
#include <stdio.h>
#include <string.h>
static EC_POINT **
sortkeys(EC_GROUP *group, EC_POINT **keys, unsigned int len)
{
unsigned long i, j;
EC_POINT *t;
for(i=0; i<len; i++)
{
EC_POINT *a = keys[i];
for(j=i+1; j<len; j++)
{
EC_POINT *b = keys[j];
unsigned char *abuf;
unsigned char *bbuf;
int alen = EC_POINT_point2buf(group, a,
POINT_CONVERSION_UNCOMPRESSED, &abuf, 0);
int blen = EC_POINT_point2buf(group, b,
POINT_CONVERSION_UNCOMPRESSED, &bbuf, 0);
if(alen != blen)
{
fprintf(stderr, "ERROR: %s:%d key sizes are not equal\n", __FILE__, __LINE__);
return 0;
}
int diff = memcmp(abuf, bbuf, alen);
if(diff > 0)
{
t = EC_POINT_dup(keys[i], group);
keys[i] = keys[j];
keys[j] = EC_POINT_dup(t, group);
EC_POINT_free(t);
}
}
}
return keys;
}
int
RTRS_MS_keygen(EC_GROUP *group, EC_POINT **pubkey, BIGNUM **privkey)
{
if(*pubkey == 0 || *privkey == 0)
{
fprintf(stderr, "ERROR: %s:%d pubkey or privkey is not allocated!\n", __FILE__, __LINE__);
return 0;
}
if(!BN_rand(*privkey, 32, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY))
{
fprintf(stderr, "ERROR: %s:%d Could not set up secure random privkey!\n", __FILE__, __LINE__);
return 0;
}
EC_POINT_mul(group ,*pubkey, 0, EC_GROUP_get0_generator(group), *privkey, 0);
if(!*pubkey)
{
fprintf(stderr, "ERROR: %s:%d Could not set up secure pubkey!\n", __FILE__, __LINE__);
return 0;
}
return 1;
}
size_t
RTRS_MS_sign(unsigned char **ret, EC_GROUP *group, unsigned char *msg, unsigned long msg_len,
EC_POINT **pubkeys, BIGNUM **privkeys, unsigned int klen)
{
unsigned long i;
unsigned char *hash;
unsigned char *t;
unsigned long hlen = 0;
EC_POINT **sorted_pubkeys = sortkeys(group, pubkeys, klen);
for(i=0; i<klen; i++)
{
int tlen = EC_POINT_point2buf(group, sorted_pubkeys[i],
POINT_CONVERSION_UNCOMPRESSED, &t, 0);
if(tlen == 0)
{
fprintf(stderr, "ERROR: point serialization error\n");
return 0;
}
hlen += tlen;
unsigned char *rhash = realloc(hash, hlen);
if(!rhash)
{
perror("memory allocation error ");
OPENSSL_free(hash);
return 0;
}
hash = rhash;
memcpy(hash+hlen, t, tlen);
}
BIGNUM *xasterisk = BN_hash(hash, hlen);
BIGNUM **r_array = OPENSSL_malloc(sizeof(BIGNUM*)*klen);
BIGNUM *r_sum = BN_new();
BN_zero(r_sum);
for(i=0; i<klen; i++)
{
r_array[i] = BN_new();
BN_rand(r_array[i], 32, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY);
BN_add(r_sum, r_sum, r_array[i]);
}
const EC_POINT *G = EC_GROUP_get0_generator(group);
EC_POINT *R = EC_POINT_new(group);
EC_POINT_mul(group, R, 0, G, r_sum, 0);
BIGNUM **c = OPENSSL_malloc(sizeof(BIGNUM*)*klen);
BIGNUM **s_array = OPENSSL_malloc(sizeof(BIGNUM*)*klen);
BIGNUM *s_sum = BN_new();
BN_zero(s_sum);
OPENSSL_free(hash); hash = 0;
OPENSSL_free(t); t = 0;
hlen = 0;
unsigned char *XIbuf;
unsigned char *Rbuf;
int Rbuf_len = EC_POINT_point2buf(group, R,
POINT_CONVERSION_UNCOMPRESSED, &Rbuf, 0);
unsigned char *Xastbuf = OPENSSL_malloc(BN_num_bytes(xasterisk));
int Xastbuf_len = BN_bn2bin(xasterisk, Xastbuf);
for(i=0; i<klen; i++)
{
int tlen = EC_POINT_point2buf(group, sorted_pubkeys[i],
POINT_CONVERSION_UNCOMPRESSED, &XIbuf, 0);
hlen = Rbuf_len + Xastbuf_len + tlen + msg_len;
hash = OPENSSL_malloc(hlen);
memcpy(hash, XIbuf, tlen);
memcpy(hash+tlen, Rbuf, Rbuf_len);
memcpy(hash+tlen+Rbuf_len, Xastbuf, Xastbuf_len);
memcpy(hash+tlen+Rbuf_len+Xastbuf_len, msg, msg_len);
c[i] = BN_hash(hash, hlen);
s_array[i] = BN_new();
BN_mul(s_array[i], privkeys[i], c[i], 0);
BN_add(s_array[i], r_array[i], s_array[i]);
BN_add(s_sum, s_sum, s_array[i]);
}
size_t retlen = Rbuf_len + BN_num_bytes(s_sum) + sizeof(int);
*ret = OPENSSL_malloc(retlen);
memcpy(*ret, &Rbuf_len, sizeof(int));
memcpy(*ret, Rbuf, Rbuf_len);
memcpy(*ret, s_sum, BN_num_bytes(s_sum));
return retlen;
}
int
RTRS_MS_verify(EC_GROUP *group, unsigned char *msg, unsigned long len, EC_POINT **pubkeys, unsigned int pklen, unsigned char *signature, unsigned long slen)
{
EC_POINT **sorted = sortkeys(group, pubkeys, pklen);
unsigned char *xastbuf = 0;
int xastlen = 0;
unsigned char *t;
unsigned char *r;
size_t tlen;
unsigned int i;
for(i=0; i<pklen; i++)
{
tlen = EC_POINT_point2buf(group, sorted[i],
POINT_CONVERSION_UNCOMPRESSED, &t, 0);
r = realloc(xastbuf, xastlen + tlen);
if (!r)
{
perror("memory allocation error ");
OPENSSL_free(xastbuf);
return(0);
}
memcpy(xastbuf+xastlen, t, tlen);
xastlen += tlen;
}
BIGNUM **c = OPENSSL_malloc(sizeof(BIGNUM*)*pklen);
EC_POINT *R = EC_POINT_new(group);
BIGNUM *s = BN_new();
int *rlen = OPENSSL_malloc(sizeof(int));
memcpy(rlen, signature, sizeof(int));
signature+=sizeof(int);
EC_POINT_oct2point(group, R, signature, *rlen, 0);
BN_bin2bn(signature+*rlen, slen-*rlen, s);
EC_POINT *Xi;
unsigned char *Xibuf;
for(i=0; i<pklen; i++)
{
Xi = EC_POINT_dup(pubkeys[i], group);
size_t Xibuflen = EC_POINT_point2buf(group, Xi,
POINT_CONVERSION_UNCOMPRESSED, &Xibuf, 0);
unsigned char *hashed = OPENSSL_malloc(Xibuflen + *rlen + xastlen + len);
memcpy(hashed, Xibuf, Xibuflen);
memcpy(hashed+Xibuflen, signature, *rlen);
memcpy(hashed+Xibuflen+*rlen, xastbuf, xastlen);
memcpy(hashed+Xibuflen+*rlen+xastlen, msg, len);
c[i] = BN_hash(hashed, Xibuflen+*rlen+xastlen+len);
OPENSSL_free(hashed);
}
const EC_POINT *g = EC_GROUP_get0_generator(group);
EC_POINT *sg = EC_POINT_new(group);
EC_POINT *sg1 = EC_POINT_dup(R, group);
EC_POINT_mul(group, sg, 0, g, s, 0);
EC_POINT *Xc = EC_POINT_new(group);
for(i=0; i<pklen; i++)
{
EC_POINT_mul(group, Xc, 0, pubkeys[i], c[i], 0);
EC_POINT_add(group, sg1, sg1, Xc, 0);
}
return EC_POINT_cmp(group, sg, sg1, 0);
}