]> git.xonotic.org Git - xonotic/d0_blind_id.git/blob - d0_blind_id.c
Merge branch 'little_update' into 'master'
[xonotic/d0_blind_id.git] / d0_blind_id.c
1 /*
2  * FILE:        d0_blind_id.c
3  * AUTHOR:      Rudolf Polzer - divVerent@xonotic.org
4  * 
5  * Copyright (c) 2010, Rudolf Polzer
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the copyright holder nor the names of contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  * 
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $Format:commit %H$
33  * $Id$
34  */
35
36 #include "d0_blind_id.h"
37
38 #include <stdio.h>
39 #include <string.h>
40 #include "d0_bignum.h"
41 #include "sha2.h"
42
43 // old "positive" protocol, uses one extra mod_inv in verify stages
44 // #define D0_BLIND_ID_POSITIVE_PROTOCOL
45
46 // our SHA is SHA-256
47 #define SHA_DIGESTSIZE 32
48 const unsigned char *sha(unsigned char *h, const unsigned char *in, size_t len)
49 {
50         d0_blind_id_util_sha256((char *) h, (const char *) in, len);
51         return h;
52 }
53
54 // for zero knowledge, we need multiple instances of schnorr ID scheme... should normally be sequential
55 // parallel schnorr ID is not provably zero knowledge :(
56 //   (evil verifier can know all questions in advance, so sequential is disadvantage for him)
57 // we'll just live with a 1:1048576 chance of cheating, and support reauthenticating
58
59 #define SCHNORR_BITS 20
60 // probability of cheat: 2^(-bits+1)
61
62 #define SCHNORR_HASHSIZE SHA_DIGESTSIZE
63 // cannot be >= SHA_DIGESTSIZE
64 // *8 must be >= SCHNORR_BITS
65 // no need to save bits here
66
67 #define MSGSIZE 640 // ought to be enough for anyone
68
69 struct d0_blind_id_s
70 {
71         // signing (Xonotic pub and priv key)
72         d0_bignum_t *rsa_n, *rsa_e, *rsa_d;
73
74         // public data (Schnorr ID)
75         d0_bignum_t *schnorr_G;
76
77         // private data (player ID private key)
78         d0_bignum_t *schnorr_s;
79
80         // public data (player ID public key, this is what the server gets to know)
81         d0_bignum_t *schnorr_g_to_s;
82         d0_bignum_t *schnorr_H_g_to_s_signature; // 0 when signature is invalid
83         // as hash function H, we get the SHA1 and reinterpret as bignum - yes, it always is < 160 bits
84
85         // temp data
86         d0_bignum_t *rsa_blind_signature_camouflage; // random number blind signature
87
88         d0_bignum_t *r; // random number for schnorr ID
89         d0_bignum_t *t; // for DH key exchange
90         d0_bignum_t *g_to_t; // for DH key exchange
91         d0_bignum_t *other_g_to_t; // for DH key exchange
92         d0_bignum_t *challenge; // challenge
93
94         char msghash[SCHNORR_HASHSIZE]; // init hash
95         char msg[MSGSIZE]; // message
96         size_t msglen; // message length
97 };
98
99 //#define CHECKDEBUG
100 #ifdef CHECKDEBUG
101 #define CHECK(x) do { if(!(x)) { fprintf(stderr, "CHECK FAILED (%s:%d): %s\n", __FILE__, __LINE__, #x); goto fail; } } while(0)
102 #define CHECK_ASSIGN(var, value) do { d0_bignum_t *val; val = value; if(!val) { fprintf(stderr, "CHECK FAILED (%s:%d): %s\n", __FILE__, __LINE__, #value); goto fail; } var = val; } while(0)
103 #else
104 #define CHECK(x) do { if(!(x)) goto fail; } while(0)
105 #define CHECK_ASSIGN(var, value) do { d0_bignum_t *val; val = value; if(!val) goto fail; var = val; } while(0)
106 #endif
107
108 #define USING(x) if(!(ctx->x)) return 0
109 #define REPLACING(x)
110
111 // safe to use
112 static d0_bignum_t *zero, *one, *four;
113
114 static d0_bignum_t *temp0, *temp1, *temp2, *temp3, *temp4;
115 static void *tempmutex = NULL; // hold this mutex when using temp0 to temp4
116 #define USINGTEMPS() int locked = 0
117 #define LOCKTEMPS() do { if(!locked) d0_lockmutex(tempmutex); locked = 1; } while(0)
118 #define UNLOCKTEMPS() do { if(locked) d0_unlockmutex(tempmutex); locked = 0; } while(0);
119
120 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_INITIALIZE(void)
121 {
122         USINGTEMPS();
123         d0_initfuncs();
124         tempmutex = d0_createmutex();
125         LOCKTEMPS();
126         CHECK(d0_bignum_INITIALIZE());
127         CHECK_ASSIGN(zero, d0_bignum_int(zero, 0));
128         CHECK_ASSIGN(one, d0_bignum_int(one, 1));
129         CHECK_ASSIGN(four, d0_bignum_int(four, 4));
130         CHECK_ASSIGN(temp0, d0_bignum_int(temp0, 0));
131         CHECK_ASSIGN(temp1, d0_bignum_int(temp1, 0));
132         CHECK_ASSIGN(temp2, d0_bignum_int(temp2, 0));
133         CHECK_ASSIGN(temp3, d0_bignum_int(temp3, 0));
134         CHECK_ASSIGN(temp4, d0_bignum_int(temp4, 0));
135         UNLOCKTEMPS();
136         return 1;
137 fail:
138         UNLOCKTEMPS();
139         return 0;
140 }
141
142 void d0_blind_id_SHUTDOWN(void)
143 {
144         USINGTEMPS();
145         LOCKTEMPS();
146         d0_bignum_free(zero);
147         d0_bignum_free(one);
148         d0_bignum_free(four);
149         d0_bignum_free(temp0);
150         d0_bignum_free(temp1);
151         d0_bignum_free(temp2);
152         d0_bignum_free(temp3);
153         d0_bignum_free(temp4);
154         d0_bignum_SHUTDOWN();
155         UNLOCKTEMPS();
156         d0_destroymutex(tempmutex);
157         tempmutex = NULL;
158 }
159
160 // (G-1)/2
161 static d0_bignum_t *d0_dl_get_order(d0_bignum_t *o, const d0_bignum_t *G)
162 {
163         CHECK_ASSIGN(o, d0_bignum_sub(o, G, one));
164         CHECK(d0_bignum_shl(o, o, -1)); // order o = (G-1)/2
165         return o;
166 fail:
167         return NULL;
168 }
169 // 2o+1
170 d0_bignum_t *d0_dl_get_from_order(d0_bignum_t *G, const d0_bignum_t *o)
171 {
172         CHECK_ASSIGN(G, d0_bignum_shl(G, o, 1));
173         CHECK(d0_bignum_add(G, G, one));
174         return G;
175 fail:
176         return NULL;
177 }
178
179 // temps must NOT be locked when calling this
180 static D0_BOOL d0_dl_generate_key(size_t size, d0_bignum_t *G)
181 {
182         USINGTEMPS(); // using: temp0
183         if(size < 16)
184                 size = 16;
185         for(;;)
186         {
187                 LOCKTEMPS();
188                 CHECK(d0_bignum_rand_bit_exact(temp0, size-1));
189                 if(d0_bignum_isprime(temp0, 0) == 0)
190                         continue;
191                 CHECK(d0_dl_get_from_order(G, temp0));
192                 if(d0_bignum_isprime(G, 10) == 0)
193                         continue;
194                 if(d0_bignum_isprime(temp0, 10) == 0) // finish the previous test
195                         continue;
196                 UNLOCKTEMPS();
197                 break;
198         }
199         return 1;
200 fail:
201         UNLOCKTEMPS();
202         return 0;
203 }
204
205 // temps must NOT be locked when calling this
206 static D0_BOOL d0_rsa_generate_key(size_t size, d0_blind_id_t *ctx)
207 {
208         USINGTEMPS(); // uses temp1 to temp4
209         int fail = 0;
210         int gcdfail = 0;
211         int pb = (size + 1)/2;
212         int qb = size - pb;
213         if(pb < 8)
214                 pb = 8;
215         if(qb < 8)
216                 qb = 8;
217
218         // we use ctx->rsa_d for the first result so that we can unlock temps later
219         for (;;)
220         {
221                 LOCKTEMPS();
222                 CHECK(d0_bignum_rand_bit_exact(ctx->rsa_d, pb));
223                 if(d0_bignum_isprime(ctx->rsa_d, 10) == 0)
224                 {
225                         UNLOCKTEMPS();
226                         continue;
227                 }
228                 CHECK(d0_bignum_sub(temp2, ctx->rsa_d, one));
229                 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp2, ctx->rsa_e));
230                 if(!d0_bignum_cmp(temp4, one))
231                         break;
232                 if(++gcdfail == 16)
233                         goto fail;
234         }
235         UNLOCKTEMPS();
236
237         gcdfail = 0;
238         for (;;)
239         {
240                 LOCKTEMPS();
241                 CHECK(d0_bignum_rand_bit_exact(temp1, qb));
242                 if(!d0_bignum_cmp(temp1, ctx->rsa_d))
243                 {
244                         UNLOCKTEMPS();
245                         if(++fail == 16)
246                                 goto fail;
247                         continue;
248                 }
249                 fail = 0;
250                 if(d0_bignum_isprime(temp1, 10) == 0)
251                 {
252                         UNLOCKTEMPS();
253                         continue;
254                 }
255                 CHECK(d0_bignum_sub(temp3, temp1, one));
256                 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp3, ctx->rsa_e));
257                 if(!d0_bignum_cmp(temp4, one))
258                 {
259                         // we do NOT unlock, as we still need temp1 and temp3
260                         break;
261                 }
262                 UNLOCKTEMPS();
263                 if(++gcdfail == 16)
264                         goto fail;
265         }
266
267         // ctx->rsa_n = ctx->rsa_d*temp1
268         CHECK(d0_bignum_mul(ctx->rsa_n, ctx->rsa_d, temp1));
269
270         // ctx->rsa_d = ctx->rsa_e^-1 mod (ctx->rsa_d-1)(temp1-1)
271         CHECK(d0_bignum_sub(temp2, ctx->rsa_d, one)); // we can't reuse the value from above because temps were unlocked
272         CHECK(d0_bignum_mul(temp1, temp2, temp3));
273         CHECK(d0_bignum_mod_inv(ctx->rsa_d, ctx->rsa_e, temp1));
274         UNLOCKTEMPS();
275         return 1;
276 fail:
277         UNLOCKTEMPS();
278         return 0;
279 }
280
281 // temps must NOT be locked when calling this
282 static D0_BOOL d0_rsa_generate_key_fastreject(size_t size, d0_fastreject_function reject, d0_blind_id_t *ctx, void *pass)
283 {
284         USINGTEMPS(); // uses temp1 to temp4
285         int fail = 0;
286         int gcdfail = 0;
287         int pb = (size + 1)/2;
288         int qb = size - pb;
289         if(pb < 8)
290                 pb = 8;
291         if(qb < 8)
292                 qb = 8;
293
294         // we use ctx->rsa_d for the first result so that we can unlock temps later
295         for (;;)
296         {
297                 LOCKTEMPS();
298                 CHECK(d0_bignum_rand_bit_exact(ctx->rsa_d, pb));
299                 if(d0_bignum_isprime(ctx->rsa_d, 10) == 0)
300                 {
301                         UNLOCKTEMPS();
302                         continue;
303                 }
304                 CHECK(d0_bignum_sub(temp2, ctx->rsa_d, one));
305                 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp2, ctx->rsa_e));
306                 if(!d0_bignum_cmp(temp4, one))
307                         break;
308                 if(++gcdfail == 16)
309                         return 0;
310         }
311         UNLOCKTEMPS();
312
313         gcdfail = 0;
314         for (;;)
315         {
316                 LOCKTEMPS();
317                 CHECK(d0_bignum_rand_bit_exact(temp1, qb));
318                 if(!d0_bignum_cmp(temp1, ctx->rsa_d))
319                 {
320                         UNLOCKTEMPS();
321                         if(++fail == 16)
322                                 return 0;
323                         continue;
324                 }
325                 fail = 0;
326
327                 // n = ctx->rsa_d*temp1
328                 CHECK(d0_bignum_mul(ctx->rsa_n, ctx->rsa_d, temp1));
329                 if(reject(ctx, pass))
330                 {
331                         UNLOCKTEMPS();
332                         continue;
333                 }
334
335                 if(d0_bignum_isprime(temp1, 10) == 0)
336                 {
337                         UNLOCKTEMPS();
338                         continue;
339                 }
340                 CHECK(d0_bignum_sub(temp3, temp1, one));
341                 CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp3, ctx->rsa_e));
342                 if(!d0_bignum_cmp(temp4, one))
343                 {
344                         // we do NOT unlock, as we still need temp3
345                         break;
346                 }
347                 UNLOCKTEMPS();
348                 if(++gcdfail == 16)
349                         return 0;
350         }
351
352         // ctx->rsa_d = ctx->rsa_e^-1 mod (ctx->rsa_d-1)(temp1-1)
353         CHECK(d0_bignum_sub(temp2, ctx->rsa_d, one)); // we can't reuse the value from above because temps were unlocked
354         CHECK(d0_bignum_mul(temp1, temp2, temp3));
355         CHECK(d0_bignum_mod_inv(ctx->rsa_d, ctx->rsa_e, temp1));
356         UNLOCKTEMPS();
357         return 1;
358 fail:
359         UNLOCKTEMPS();
360         return 0;
361 }
362
363 D0_WARN_UNUSED_RESULT D0_BOOL d0_longhash_destructive(unsigned char *convbuf, size_t sz, unsigned char *outbuf, size_t outbuflen)
364 {
365         size_t n, i;
366         char shabuf[32];
367
368         n = outbuflen;
369         while(n > SHA_DIGESTSIZE)
370         {
371                 memcpy(outbuf, sha(shabuf, convbuf, sz), SHA_DIGESTSIZE);
372                 outbuf += SHA_DIGESTSIZE;
373                 n -= SHA_DIGESTSIZE;
374                 for(i = 0; i < sz; ++i)
375                         if(++convbuf[i])
376                                 break; // stop until no carry
377         }
378         memcpy(outbuf, sha(shabuf, convbuf, sz), n);
379         return 1;
380 }
381
382 D0_WARN_UNUSED_RESULT D0_BOOL d0_longhash_bignum(const d0_bignum_t *in, unsigned char *outbuf, size_t outbuflen)
383 {
384         unsigned char convbuf[1024];
385         size_t sz;
386
387         CHECK(d0_bignum_export_unsigned(in, convbuf, sizeof(convbuf)) >= 0);
388         sz = (d0_bignum_size(in) + 7) / 8;
389         CHECK(d0_longhash_destructive(convbuf, sz, outbuf, outbuflen));
390         return 1;
391
392 fail:
393         return 0;
394 }
395
396 void d0_blind_id_clear(d0_blind_id_t *ctx)
397 {
398         if(ctx->rsa_n) d0_bignum_free(ctx->rsa_n);
399         if(ctx->rsa_e) d0_bignum_free(ctx->rsa_e);
400         if(ctx->rsa_d) d0_bignum_free(ctx->rsa_d);
401         if(ctx->schnorr_G) d0_bignum_free(ctx->schnorr_G);
402         if(ctx->schnorr_s) d0_bignum_free(ctx->schnorr_s);
403         if(ctx->schnorr_g_to_s) d0_bignum_free(ctx->schnorr_g_to_s);
404         if(ctx->schnorr_H_g_to_s_signature) d0_bignum_free(ctx->schnorr_H_g_to_s_signature);
405         if(ctx->rsa_blind_signature_camouflage) d0_bignum_free(ctx->rsa_blind_signature_camouflage);
406         if(ctx->r) d0_bignum_free(ctx->r);
407         if(ctx->challenge) d0_bignum_free(ctx->challenge);
408         if(ctx->t) d0_bignum_free(ctx->t);
409         if(ctx->g_to_t) d0_bignum_free(ctx->g_to_t);
410         if(ctx->other_g_to_t) d0_bignum_free(ctx->other_g_to_t);
411         memset(ctx, 0, sizeof(*ctx));
412 }
413
414 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_copy(d0_blind_id_t *ctx, const d0_blind_id_t *src)
415 {
416         d0_blind_id_clear(ctx);
417         if(src->rsa_n) CHECK_ASSIGN(ctx->rsa_n, d0_bignum_mov(NULL, src->rsa_n));
418         if(src->rsa_e) CHECK_ASSIGN(ctx->rsa_e, d0_bignum_mov(NULL, src->rsa_e));
419         if(src->rsa_d) CHECK_ASSIGN(ctx->rsa_d, d0_bignum_mov(NULL, src->rsa_d));
420         if(src->schnorr_G) CHECK_ASSIGN(ctx->schnorr_G, d0_bignum_mov(NULL, src->schnorr_G));
421         if(src->schnorr_s) CHECK_ASSIGN(ctx->schnorr_s, d0_bignum_mov(NULL, src->schnorr_s));
422         if(src->schnorr_g_to_s) CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_bignum_mov(NULL, src->schnorr_g_to_s));
423         if(src->schnorr_H_g_to_s_signature) CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_bignum_mov(NULL, src->schnorr_H_g_to_s_signature));
424         if(src->rsa_blind_signature_camouflage) CHECK_ASSIGN(ctx->rsa_blind_signature_camouflage, d0_bignum_mov(NULL, src->rsa_blind_signature_camouflage));
425         if(src->r) CHECK_ASSIGN(ctx->r, d0_bignum_mov(NULL, src->r));
426         if(src->challenge) CHECK_ASSIGN(ctx->challenge, d0_bignum_mov(NULL, src->challenge));
427         if(src->t) CHECK_ASSIGN(ctx->t, d0_bignum_mov(NULL, src->t));
428         if(src->g_to_t) CHECK_ASSIGN(ctx->g_to_t, d0_bignum_mov(NULL, src->g_to_t));
429         if(src->other_g_to_t) CHECK_ASSIGN(ctx->other_g_to_t, d0_bignum_mov(NULL, src->other_g_to_t));
430         memcpy(ctx->msg, src->msg, sizeof(ctx->msg));
431         ctx->msglen = src->msglen;
432         memcpy(ctx->msghash, src->msghash, sizeof(ctx->msghash));
433         return 1;
434 fail:
435         d0_blind_id_clear(ctx);
436         return 0;
437 }
438
439 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_generate_private_key_fastreject(d0_blind_id_t *ctx, int k, d0_fastreject_function reject, void *pass)
440 {
441         REPLACING(rsa_e); REPLACING(rsa_d); REPLACING(rsa_n);
442
443         CHECK_ASSIGN(ctx->rsa_e, d0_bignum_int(ctx->rsa_e, 65537));
444         CHECK_ASSIGN(ctx->rsa_d, d0_bignum_zero(ctx->rsa_d));
445         CHECK_ASSIGN(ctx->rsa_n, d0_bignum_zero(ctx->rsa_n));
446         if(reject)
447                 CHECK(d0_rsa_generate_key_fastreject(k+1, reject, ctx, pass)); // must fit G for sure
448         else
449                 CHECK(d0_rsa_generate_key(k+1, ctx)); // must fit G for sure
450         return 1;
451 fail:
452         return 0;
453 }
454
455 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_generate_private_key(d0_blind_id_t *ctx, int k)
456 {
457         return d0_blind_id_generate_private_key_fastreject(ctx, k, NULL, NULL);
458 }
459
460 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_read_private_key(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
461 {
462         d0_iobuf_t *in = NULL;
463
464         REPLACING(rsa_n); REPLACING(rsa_e); REPLACING(rsa_d);
465
466         in = d0_iobuf_open_read(inbuf, inbuflen);
467
468         CHECK_ASSIGN(ctx->rsa_n, d0_iobuf_read_bignum(in, ctx->rsa_n));
469         CHECK_ASSIGN(ctx->rsa_e, d0_iobuf_read_bignum(in, ctx->rsa_e));
470         CHECK_ASSIGN(ctx->rsa_d, d0_iobuf_read_bignum(in, ctx->rsa_d));
471         return d0_iobuf_close(in, NULL);
472
473 fail:
474         d0_iobuf_close(in, NULL);
475         return 0;
476 }
477
478 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_read_public_key(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
479 {
480         d0_iobuf_t *in = NULL;
481
482         REPLACING(rsa_n); REPLACING(rsa_e);
483
484         in = d0_iobuf_open_read(inbuf, inbuflen);
485         CHECK_ASSIGN(ctx->rsa_n, d0_iobuf_read_bignum(in, ctx->rsa_n));
486         CHECK_ASSIGN(ctx->rsa_e, d0_iobuf_read_bignum(in, ctx->rsa_e));
487         return d0_iobuf_close(in, NULL);
488
489 fail:
490         d0_iobuf_close(in, NULL);
491         return 0;
492 }
493
494 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_write_private_key(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
495 {
496         d0_iobuf_t *out = NULL;
497
498         USING(rsa_n); USING(rsa_e); USING(rsa_d);
499
500         out = d0_iobuf_open_write(outbuf, *outbuflen);
501         CHECK(d0_iobuf_write_bignum(out, ctx->rsa_n));
502         CHECK(d0_iobuf_write_bignum(out, ctx->rsa_e));
503         CHECK(d0_iobuf_write_bignum(out, ctx->rsa_d));
504         return d0_iobuf_close(out, outbuflen);
505
506 fail:
507         d0_iobuf_close(out, outbuflen);
508         return 0;
509 }
510
511 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_write_public_key(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
512 {
513         d0_iobuf_t *out = NULL;
514
515         USING(rsa_n); USING(rsa_e);
516
517         out = d0_iobuf_open_write(outbuf, *outbuflen);
518         CHECK(d0_iobuf_write_bignum(out, ctx->rsa_n));
519         CHECK(d0_iobuf_write_bignum(out, ctx->rsa_e));
520         return d0_iobuf_close(out, outbuflen);
521
522 fail:
523         if(!d0_iobuf_close(out, outbuflen))
524                 return 0;
525         return 0;
526 }
527
528 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_fingerprint64_public_key(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
529 {
530         d0_iobuf_t *out = NULL;
531         unsigned char convbuf[2048];
532         d0_iobuf_t *conv = NULL;
533         size_t sz, n;
534         char shabuf[32];
535
536         USING(rsa_n); USING(rsa_e);
537
538         out = d0_iobuf_open_write(outbuf, *outbuflen);
539         conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
540
541         CHECK(d0_iobuf_write_bignum(conv, ctx->rsa_n));
542         CHECK(d0_iobuf_write_bignum(conv, ctx->rsa_e));
543         CHECK(d0_iobuf_close(conv, &sz));
544         conv = NULL;
545
546         n = (*outbuflen / 4) * 3;
547         if(n > SHA_DIGESTSIZE)
548                 n = SHA_DIGESTSIZE;
549         CHECK(d0_iobuf_write_raw(out, sha(shabuf, convbuf, sz), n) == n);
550         CHECK(d0_iobuf_conv_base64_out(out));
551
552         return d0_iobuf_close(out, outbuflen);
553
554 fail:
555         if(conv)
556                 d0_iobuf_close(conv, &sz);
557         d0_iobuf_close(out, outbuflen);
558         return 0;
559 }
560
561 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_generate_private_id_modulus(d0_blind_id_t *ctx)
562 {
563         USING(rsa_n);
564         REPLACING(schnorr_G);
565
566         CHECK_ASSIGN(ctx->schnorr_G, d0_bignum_zero(ctx->schnorr_G));
567         CHECK(d0_dl_generate_key(d0_bignum_size(ctx->rsa_n)-1, ctx->schnorr_G));
568         return 1;
569 fail:
570         return 0;
571 }
572
573 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_read_private_id_modulus(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
574 {
575         d0_iobuf_t *in = NULL;
576
577         REPLACING(schnorr_G);
578
579         in = d0_iobuf_open_read(inbuf, inbuflen);
580         CHECK_ASSIGN(ctx->schnorr_G, d0_iobuf_read_bignum(in, ctx->schnorr_G));
581         return d0_iobuf_close(in, NULL);
582
583 fail:
584         d0_iobuf_close(in, NULL);
585         return 0;
586 }
587
588 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_write_private_id_modulus(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
589 {
590         d0_iobuf_t *out = NULL;
591
592         USING(schnorr_G);
593
594         out = d0_iobuf_open_write(outbuf, *outbuflen);
595         CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_G));
596         return d0_iobuf_close(out, outbuflen);
597
598 fail:
599         d0_iobuf_close(out, outbuflen);
600         return 0;
601 }
602
603 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_generate_private_id_start(d0_blind_id_t *ctx)
604 {
605         USINGTEMPS(); // temps: temp0 = order
606         USING(schnorr_G);
607         REPLACING(schnorr_s); REPLACING(schnorr_g_to_s);
608
609         LOCKTEMPS();
610         CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
611         CHECK_ASSIGN(ctx->schnorr_s, d0_bignum_rand_range(ctx->schnorr_s, zero, temp0));
612         CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_bignum_mod_pow(ctx->schnorr_g_to_s, four, ctx->schnorr_s, ctx->schnorr_G));
613         CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_bignum_zero(ctx->schnorr_H_g_to_s_signature));
614         UNLOCKTEMPS();
615         return 1;
616
617 fail:
618         UNLOCKTEMPS();
619         return 0;
620 }
621
622 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_generate_private_id_request(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
623 {
624         d0_iobuf_t *out = NULL;
625         unsigned char hashbuf[2048];
626         size_t sz;
627
628         USINGTEMPS(); // temps: temp0 rsa_blind_signature_camouflage^challenge, temp1 (4^s)*rsa_blind_signature_camouflage^challenge
629         USING(rsa_n); USING(rsa_e); USING(schnorr_g_to_s);
630         REPLACING(rsa_blind_signature_camouflage);
631
632         out = d0_iobuf_open_write(outbuf, *outbuflen);
633
634         CHECK_ASSIGN(ctx->rsa_blind_signature_camouflage, d0_bignum_rand_bit_atmost(ctx->rsa_blind_signature_camouflage, d0_bignum_size(ctx->rsa_n)));
635         CHECK(d0_bignum_mod_pow(temp0, ctx->rsa_blind_signature_camouflage, ctx->rsa_e, ctx->rsa_n));
636
637         // we will actually sign HA(4^s) to prevent a malleability attack!
638         LOCKTEMPS();
639         sz = (d0_bignum_size(ctx->rsa_n) + 7) / 8; // this is too long, so we have to take the value % rsa_n when "decrypting"
640         if(sz > sizeof(hashbuf))
641                 sz = sizeof(hashbuf);
642         CHECK(d0_longhash_bignum(ctx->schnorr_g_to_s, hashbuf, sz));
643         CHECK(d0_bignum_import_unsigned(temp2, hashbuf, sz));
644
645         // hash complete
646         CHECK(d0_bignum_mod_mul(temp1, temp2, temp0, ctx->rsa_n));
647         CHECK(d0_iobuf_write_bignum(out, temp1));
648         UNLOCKTEMPS();
649         return d0_iobuf_close(out, outbuflen);
650
651 fail:
652         UNLOCKTEMPS();
653         d0_iobuf_close(out, outbuflen);
654         return 0;
655 }
656
657 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_answer_private_id_request(const d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen, char *outbuf, size_t *outbuflen)
658 {
659         d0_iobuf_t *in = NULL;
660         d0_iobuf_t *out = NULL;
661
662         USINGTEMPS(); // temps: temp0 input, temp1 temp0^d
663         USING(rsa_d); USING(rsa_n);
664
665         in = d0_iobuf_open_read(inbuf, inbuflen);
666         out = d0_iobuf_open_write(outbuf, *outbuflen);
667
668         LOCKTEMPS();
669         CHECK(d0_iobuf_read_bignum(in, temp0));
670         CHECK(d0_bignum_mod_pow(temp1, temp0, ctx->rsa_d, ctx->rsa_n));
671         CHECK(d0_iobuf_write_bignum(out, temp1));
672
673         UNLOCKTEMPS();
674         d0_iobuf_close(in, NULL);
675         return d0_iobuf_close(out, outbuflen);
676
677 fail:
678         UNLOCKTEMPS();
679         d0_iobuf_close(in, NULL);
680         d0_iobuf_close(out, outbuflen);
681         return 0;
682 }
683
684 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_finish_private_id_request(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
685 {
686         d0_iobuf_t *in = NULL;
687
688         USINGTEMPS(); // temps: temp0 input, temp1 rsa_blind_signature_camouflage^-1
689         USING(rsa_blind_signature_camouflage); USING(rsa_n);
690         REPLACING(schnorr_H_g_to_s_signature);
691
692         in = d0_iobuf_open_read(inbuf, inbuflen);
693
694         LOCKTEMPS();
695
696         CHECK(d0_iobuf_read_bignum(in, temp0));
697         CHECK(d0_bignum_mod_inv(temp1, ctx->rsa_blind_signature_camouflage, ctx->rsa_n));
698         CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_bignum_mod_mul(ctx->schnorr_H_g_to_s_signature, temp0, temp1, ctx->rsa_n));
699
700         UNLOCKTEMPS();
701         return d0_iobuf_close(in, NULL);
702
703 fail:
704         UNLOCKTEMPS();
705         d0_iobuf_close(in, NULL);
706         return 0;
707 }
708
709 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_read_private_id_request_camouflage(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
710 {
711         d0_iobuf_t *in = NULL;
712
713         REPLACING(rsa_blind_signature_camouflage);
714
715         in = d0_iobuf_open_read(inbuf, inbuflen);
716
717         CHECK_ASSIGN(ctx->rsa_blind_signature_camouflage, d0_iobuf_read_bignum(in, ctx->rsa_blind_signature_camouflage));
718
719         return d0_iobuf_close(in, NULL);
720
721 fail:
722         d0_iobuf_close(in, NULL);
723         return 0;
724 }
725
726 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_write_private_id_request_camouflage(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
727 {
728         d0_iobuf_t *out = NULL;
729
730         USING(rsa_blind_signature_camouflage);
731
732         out = d0_iobuf_open_write(outbuf, *outbuflen);
733
734         CHECK(d0_iobuf_write_bignum(out, ctx->rsa_blind_signature_camouflage));
735
736         return d0_iobuf_close(out, outbuflen);
737
738 fail:
739         d0_iobuf_close(out, outbuflen);
740         return 0;
741 }
742
743 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_read_private_id(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
744 {
745         d0_iobuf_t *in = NULL;
746
747         REPLACING(schnorr_s); REPLACING(schnorr_g_to_s); REPLACING(schnorr_H_g_to_s_signature);
748
749         in = d0_iobuf_open_read(inbuf, inbuflen);
750
751         CHECK_ASSIGN(ctx->schnorr_s, d0_iobuf_read_bignum(in, ctx->schnorr_s));
752         CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_g_to_s));
753         CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_H_g_to_s_signature));
754
755         return d0_iobuf_close(in, NULL);
756
757 fail:
758         d0_iobuf_close(in, NULL);
759         return 0;
760 }
761
762 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_read_public_id(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen)
763 {
764         d0_iobuf_t *in = NULL;
765
766         REPLACING(schnorr_g_to_s); REPLACING(schnorr_H_g_to_s_signature);
767
768         in = d0_iobuf_open_read(inbuf, inbuflen);
769
770         CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_g_to_s));
771         CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_H_g_to_s_signature));
772
773         return d0_iobuf_close(in, NULL);
774
775 fail:
776         d0_iobuf_close(in, NULL);
777         return 0;
778 }
779
780 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_write_private_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
781 {
782         d0_iobuf_t *out = NULL;
783
784         USING(schnorr_s); USING(schnorr_g_to_s); USING(schnorr_H_g_to_s_signature);
785
786         out = d0_iobuf_open_write(outbuf, *outbuflen);
787
788         CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_s));
789         CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_g_to_s));
790         CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_H_g_to_s_signature));
791
792         return d0_iobuf_close(out, outbuflen);
793
794 fail:
795         d0_iobuf_close(out, outbuflen);
796         return 0;
797 }
798
799 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_write_public_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
800 {
801         d0_iobuf_t *out = NULL;
802
803         USING(schnorr_g_to_s); USING(schnorr_H_g_to_s_signature);
804
805         out = d0_iobuf_open_write(outbuf, *outbuflen);
806
807         CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_g_to_s));
808         CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_H_g_to_s_signature));
809
810         return d0_iobuf_close(out, outbuflen);
811
812 fail:
813         d0_iobuf_close(out, outbuflen);
814         return 0;
815 }
816
817 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_authenticate_with_private_id_start(d0_blind_id_t *ctx, D0_BOOL is_first, D0_BOOL send_modulus, const char *msg, size_t msglen, char *outbuf, size_t *outbuflen)
818 // start =
819 //   first run: send 4^s, 4^s signature
820 //   1. get random r, send HASH(4^r)
821 {
822         d0_iobuf_t *out = NULL;
823         unsigned char convbuf[1024];
824         d0_iobuf_t *conv = NULL;
825         size_t sz = 0;
826         D0_BOOL failed = 0;
827         char shabuf[32];
828
829         USINGTEMPS(); // temps: temp0 order, temp0 4^r
830         if(is_first)
831         {
832                 USING(schnorr_g_to_s); USING(schnorr_H_g_to_s_signature);
833         }
834         USING(schnorr_G);
835         REPLACING(r); REPLACING(t); REPLACING(g_to_t);
836
837         out = d0_iobuf_open_write(outbuf, *outbuflen);
838
839         if(is_first)
840         {
841                 // send ID
842                 if(send_modulus)
843                         CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_G));
844                 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_g_to_s));
845                 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_H_g_to_s_signature));
846         }
847
848         // start schnorr ID scheme
849         // generate random number r; x = g^r; send hash of x, remember r, forget x
850         LOCKTEMPS();
851         CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
852 #ifdef RNG_XKCD
853         CHECK_ASSIGN(ctx->r, d0_bignum_int(ctx->r, 4)); // decided by fair dice roll
854 #else
855         CHECK_ASSIGN(ctx->r, d0_bignum_rand_range(ctx->r, zero, temp0));
856 #endif
857
858         // initialize Signed Diffie Hellmann
859         // we already have the group order in temp1
860 #ifdef RNG_XKCD
861         CHECK_ASSIGN(ctx->t, d0_bignum_int(ctx->t, 4)); // decided by fair dice roll
862 #else
863         CHECK_ASSIGN(ctx->t, d0_bignum_rand_range(ctx->t, zero, temp0));
864 #endif
865         // can we SOMEHOW do this with just one mod_pow?
866
867         CHECK(d0_bignum_mod_pow(temp0, four, ctx->r, ctx->schnorr_G));
868         CHECK_ASSIGN(ctx->g_to_t, d0_bignum_mod_pow(ctx->g_to_t, four, ctx->t, ctx->schnorr_G));
869         CHECK(!failed);
870
871         // hash it, hash it, everybody hash it
872         conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
873         CHECK(d0_iobuf_write_bignum(conv, temp0));
874         CHECK(d0_iobuf_write_bignum(conv, ctx->g_to_t));
875         CHECK(d0_iobuf_write_packet(conv, msg, msglen));
876         CHECK(d0_iobuf_write_bignum(conv, temp0));
877         UNLOCKTEMPS();
878         CHECK(d0_iobuf_write_bignum(conv, ctx->g_to_t));
879         d0_iobuf_close(conv, &sz);
880         conv = NULL;
881         CHECK(d0_iobuf_write_raw(out, sha(shabuf, convbuf, sz), SCHNORR_HASHSIZE) == SCHNORR_HASHSIZE);
882         CHECK(d0_iobuf_write_packet(out, msg, msglen));
883
884         return d0_iobuf_close(out, outbuflen);
885
886 fail:
887         UNLOCKTEMPS();
888         d0_iobuf_close(out, outbuflen);
889         return 0;
890 }
891
892 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_authenticate_with_private_id_challenge(d0_blind_id_t *ctx, D0_BOOL is_first, D0_BOOL recv_modulus, const char *inbuf, size_t inbuflen, char *outbuf, size_t *outbuflen, D0_BOOL *status)
893 //   first run: get 4^s, 4^s signature
894 //   1. check sig
895 //   2. save HASH(4^r)
896 //   3. send challenge challenge of SCHNORR_BITS
897 {
898         d0_iobuf_t *in = NULL;
899         d0_iobuf_t *out = NULL;
900         unsigned char hashbuf[2048];
901         size_t sz;
902
903         USINGTEMPS(); // temps: temp0 order, temp0 signature check
904         if(is_first)
905         {
906                 REPLACING(schnorr_g_to_s); REPLACING(schnorr_H_g_to_s_signature);
907                 if(recv_modulus)
908                         REPLACING(schnorr_G);
909                 else
910                         USING(schnorr_G);
911         }
912         else
913         {
914                 USING(schnorr_g_to_s); USING(schnorr_H_g_to_s_signature);
915                 USING(schnorr_G);
916         }
917         USING(rsa_e); USING(rsa_n);
918         REPLACING(challenge); REPLACING(msg); REPLACING(msglen); REPLACING(msghash); REPLACING(r); REPLACING(t);
919
920         in = d0_iobuf_open_read(inbuf, inbuflen);
921         out = d0_iobuf_open_write(outbuf, *outbuflen);
922
923         if(is_first)
924         {
925                 if(recv_modulus)
926                 {
927                         CHECK_ASSIGN(ctx->schnorr_G, d0_iobuf_read_bignum(in, ctx->schnorr_G));
928                         CHECK(d0_bignum_cmp(ctx->schnorr_G, zero) > 0);
929                         CHECK(d0_bignum_cmp(ctx->schnorr_G, ctx->rsa_n) < 0);
930                 }
931                 CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_g_to_s));
932                 CHECK(d0_bignum_cmp(ctx->schnorr_g_to_s, zero) >= 0);
933                 CHECK(d0_bignum_cmp(ctx->schnorr_g_to_s, ctx->schnorr_G) < 0);
934                 CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_H_g_to_s_signature));
935                 CHECK(d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, zero) >= 0);
936                 CHECK(d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, ctx->rsa_n) < 0);
937
938                 if(d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, zero))
939                 {
940                         // check signature of key (t = k^d, so, t^challenge = k)
941                         LOCKTEMPS();
942                         CHECK(d0_bignum_mod_pow(temp0, ctx->schnorr_H_g_to_s_signature, ctx->rsa_e, ctx->rsa_n));
943
944                         // we will actually sign SHA(4^s) to prevent a malleability attack!
945                         sz = (d0_bignum_size(ctx->rsa_n) + 7) / 8; // this is too long, so we have to take the value % rsa_n when "decrypting"
946                         if(sz > sizeof(hashbuf))
947                                 sz = sizeof(hashbuf);
948                         CHECK(d0_longhash_bignum(ctx->schnorr_g_to_s, hashbuf, sz));
949                         CHECK(d0_bignum_import_unsigned(temp2, hashbuf, sz));
950
951                         // + 7 / 8 is too large, so let's mod it
952                         CHECK(d0_bignum_divmod(NULL, temp1, temp2, ctx->rsa_n));
953
954                         // hash complete
955                         CHECK(d0_bignum_cmp(temp0, temp1) == 0);
956                 }
957         }
958
959         CHECK(d0_iobuf_read_raw(in, ctx->msghash, SCHNORR_HASHSIZE));
960         ctx->msglen = MSGSIZE;
961         CHECK(d0_iobuf_read_packet(in, ctx->msg, &ctx->msglen));
962
963         // send challenge
964 #ifdef RNG_XKCD
965         CHECK_ASSIGN(ctx->challenge, d0_bignum_int(ctx->challenge, 4)); // decided by fair dice roll
966 #else
967         CHECK_ASSIGN(ctx->challenge, d0_bignum_rand_bit_atmost(ctx->challenge, SCHNORR_BITS));
968 #endif
969         CHECK(d0_iobuf_write_bignum(out, ctx->challenge));
970
971         // Diffie Hellmann send
972         LOCKTEMPS();
973         CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
974 #ifdef RNG_XKCD
975         CHECK_ASSIGN(ctx->t, d0_bignum_int(ctx->t, 4)); // decided by fair dice roll
976 #else
977         CHECK_ASSIGN(ctx->t, d0_bignum_rand_range(ctx->t, zero, temp0));
978 #endif
979         CHECK(d0_bignum_mod_pow(temp0, four, ctx->t, ctx->schnorr_G));
980         CHECK(d0_iobuf_write_bignum(out, temp0));
981         UNLOCKTEMPS();
982
983         if(status)
984                 *status = !!d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, zero);
985
986         d0_iobuf_close(in, NULL);
987         return d0_iobuf_close(out, outbuflen);
988
989 fail:
990         UNLOCKTEMPS();
991         d0_iobuf_close(in, NULL);
992         d0_iobuf_close(out, outbuflen);
993         return 0;
994 }
995
996 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_authenticate_with_private_id_response(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen, char *outbuf, size_t *outbuflen)
997 //   1. read challenge challenge of SCHNORR_BITS
998 //   2. reply with r + s * challenge mod order
999 {
1000         d0_iobuf_t *in = NULL;
1001         d0_iobuf_t *out = NULL;
1002
1003         USINGTEMPS(); // temps: 0 order, 1 prod, 2 y, 3 challenge
1004         REPLACING(other_g_to_t); REPLACING(t);
1005         USING(schnorr_G); USING(schnorr_s); USING(r); USING(g_to_t);
1006
1007         in = d0_iobuf_open_read(inbuf, inbuflen);
1008         out = d0_iobuf_open_write(outbuf, *outbuflen);
1009
1010         LOCKTEMPS();
1011         CHECK(d0_iobuf_read_bignum(in, temp3));
1012         CHECK(d0_bignum_cmp(temp3, zero) >= 0);
1013         CHECK(d0_bignum_size(temp3) <= SCHNORR_BITS);
1014
1015         // send response for schnorr ID scheme
1016         // i.challenge. r + ctx->schnorr_s * temp3
1017         CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
1018         CHECK(d0_bignum_mod_mul(temp1, ctx->schnorr_s, temp3, temp0));
1019 #ifdef D0_BLIND_ID_POSITIVE_PROTOCOL
1020         CHECK(d0_bignum_mod_add(temp2, ctx->r, temp1, temp0));
1021 #else
1022         CHECK(d0_bignum_mod_sub(temp2, ctx->r, temp1, temp0));
1023 #endif
1024         CHECK(d0_iobuf_write_bignum(out, temp2));
1025         UNLOCKTEMPS();
1026
1027         // Diffie Hellmann recv
1028         CHECK_ASSIGN(ctx->other_g_to_t, d0_iobuf_read_bignum(in, ctx->other_g_to_t));
1029         CHECK(d0_bignum_cmp(ctx->other_g_to_t, zero) > 0);
1030         CHECK(d0_bignum_cmp(ctx->other_g_to_t, ctx->schnorr_G) < 0);
1031         // Diffie Hellmann send
1032         CHECK(d0_iobuf_write_bignum(out, ctx->g_to_t));
1033
1034         d0_iobuf_close(in, NULL);
1035         return d0_iobuf_close(out, outbuflen);
1036
1037 fail:
1038         UNLOCKTEMPS();
1039         d0_iobuf_close(in, NULL);
1040         d0_iobuf_close(out, outbuflen);
1041         return 0;
1042 }
1043
1044 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_authenticate_with_private_id_verify(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen, char *msg, size_t *msglen, D0_BOOL *status)
1045 //   1. read y = r + s * challenge mod order
1046 //   2. verify: g^y (g^s)^-challenge = g^(r+s*challenge-s*challenge) = g^r
1047 //      (check using H(g^r) which we know)
1048 {
1049         d0_iobuf_t *in = NULL;
1050         unsigned char convbuf[1024];
1051         d0_iobuf_t *conv = NULL;
1052         size_t sz;
1053         char shabuf[32];
1054
1055         USINGTEMPS(); // temps: 0 y 1 order
1056         USING(challenge); USING(schnorr_G);
1057         REPLACING(other_g_to_t);
1058
1059         in = d0_iobuf_open_read(inbuf, inbuflen);
1060
1061         LOCKTEMPS();
1062
1063         CHECK(d0_dl_get_order(temp1, ctx->schnorr_G));
1064         CHECK(d0_iobuf_read_bignum(in, temp0));
1065         CHECK(d0_bignum_cmp(temp0, zero) >= 0);
1066         CHECK(d0_bignum_cmp(temp0, temp1) < 0);
1067
1068         // verify schnorr ID scheme
1069 #ifdef D0_BLIND_ID_POSITIVE_PROTOCOL
1070         // we need 4^r = 4^temp0 (g^s)^-challenge
1071         CHECK(d0_bignum_mod_inv(temp1, ctx->schnorr_g_to_s, ctx->schnorr_G));
1072         CHECK(d0_bignum_mod_pow(temp2, temp1, ctx->challenge, ctx->schnorr_G));
1073 #else
1074         // we need 4^r = 4^temp0 (g^s)^challenge
1075         CHECK(d0_bignum_mod_pow(temp2, ctx->schnorr_g_to_s, ctx->challenge, ctx->schnorr_G));
1076 #endif
1077         CHECK(d0_bignum_mod_pow(temp1, four, temp0, ctx->schnorr_G));
1078         CHECK_ASSIGN(temp3, d0_bignum_mod_mul(temp3, temp1, temp2, ctx->schnorr_G));
1079
1080         // Diffie Hellmann recv
1081         CHECK_ASSIGN(ctx->other_g_to_t, d0_iobuf_read_bignum(in, ctx->other_g_to_t));
1082         CHECK(d0_bignum_cmp(ctx->other_g_to_t, zero) > 0);
1083         CHECK(d0_bignum_cmp(ctx->other_g_to_t, ctx->schnorr_G) < 0);
1084
1085         // hash it, hash it, everybody hash it
1086         conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
1087         CHECK(d0_iobuf_write_bignum(conv, temp3));
1088         CHECK(d0_iobuf_write_bignum(conv, ctx->other_g_to_t));
1089         CHECK(d0_iobuf_write_packet(conv, ctx->msg, ctx->msglen));
1090         CHECK(d0_iobuf_write_bignum(conv, temp3));
1091         UNLOCKTEMPS();
1092         CHECK(d0_iobuf_write_bignum(conv, ctx->other_g_to_t));
1093         d0_iobuf_close(conv, &sz);
1094         conv = NULL;
1095         if(memcmp(sha(shabuf, convbuf, sz), ctx->msghash, SCHNORR_HASHSIZE))
1096         {
1097                 // FAIL (not owned by player)
1098                 goto fail;
1099         }
1100
1101         if(status)
1102                 *status = !!d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, zero);
1103
1104         if(ctx->msglen <= *msglen)
1105                 memcpy(msg, ctx->msg, ctx->msglen);
1106         else
1107                 memcpy(msg, ctx->msg, *msglen);
1108         *msglen = ctx->msglen;
1109
1110         d0_iobuf_close(in, NULL);
1111         return 1;
1112
1113 fail:
1114         UNLOCKTEMPS();
1115         d0_iobuf_close(in, NULL);
1116         return 0;
1117 }
1118
1119 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_authenticate_with_private_id_generate_missing_signature(d0_blind_id_t *ctx)
1120 {
1121         size_t sz;
1122         unsigned char hashbuf[2048];
1123
1124         USINGTEMPS(); // temps: 2 hash
1125         REPLACING(schnorr_H_g_to_s_signature);
1126         USING(schnorr_g_to_s); USING(rsa_d); USING(rsa_n);
1127
1128         LOCKTEMPS();
1129
1130         // we will actually sign SHA(4^s) to prevent a malleability attack!
1131         sz = (d0_bignum_size(ctx->rsa_n) + 7) / 8; // this is too long, so we have to take the value % rsa_n when "decrypting"
1132         if(sz > sizeof(hashbuf))
1133                 sz = sizeof(hashbuf);
1134         CHECK(d0_longhash_bignum(ctx->schnorr_g_to_s, hashbuf, sz));
1135         LOCKTEMPS();
1136         CHECK(d0_bignum_import_unsigned(temp2, hashbuf, sz));
1137
1138         // + 7 / 8 is too large, so let's mod it
1139         CHECK(d0_bignum_divmod(NULL, temp1, temp2, ctx->rsa_n));
1140         CHECK(d0_bignum_mod_pow(ctx->schnorr_H_g_to_s_signature, temp1, ctx->rsa_d, ctx->rsa_n));
1141
1142         UNLOCKTEMPS();
1143         return 1;
1144
1145 fail:
1146         UNLOCKTEMPS();
1147         return 0;
1148 }
1149
1150 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_sign_with_private_id_sign_internal(d0_blind_id_t *ctx, D0_BOOL is_first, D0_BOOL send_modulus, D0_BOOL with_msg, const char *message, size_t msglen, char *outbuf, size_t *outbuflen)
1151 {
1152         d0_iobuf_t *out = NULL;
1153         unsigned char *convbuf = NULL;
1154         unsigned char hashbuf[2048];
1155         d0_iobuf_t *conv = NULL;
1156         size_t sz = 0;
1157
1158         USINGTEMPS(); // temps: 0 order 1 4^r 2 hash
1159         if(is_first)
1160         {
1161                 USING(schnorr_g_to_s); USING(schnorr_H_g_to_s_signature);
1162         }
1163         USING(schnorr_G);
1164         USING(schnorr_s);
1165         REPLACING(r);
1166
1167         out = d0_iobuf_open_write(outbuf, *outbuflen);
1168
1169         if(is_first)
1170         {
1171                 // send ID
1172                 if(send_modulus)
1173                         CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_G));
1174                 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_g_to_s));
1175                 CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_H_g_to_s_signature));
1176         }
1177
1178         // start schnorr SIGNATURE scheme
1179         // generate random number r; x = g^r; send hash of H(m||r), remember r, forget x
1180         LOCKTEMPS();
1181         CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
1182         CHECK_ASSIGN(ctx->r, d0_bignum_rand_range(ctx->r, zero, temp0));
1183         CHECK(d0_bignum_mod_pow(temp1, four, ctx->r, ctx->schnorr_G));
1184
1185         // hash it, hash it, everybody hash it
1186         conv = d0_iobuf_open_write_p((void **) &convbuf, 0);
1187         CHECK(d0_iobuf_write_packet(conv, message, msglen));
1188         CHECK(d0_iobuf_write_bignum(conv, temp1));
1189         d0_iobuf_close(conv, &sz);
1190         conv = NULL;
1191         CHECK(d0_longhash_destructive(convbuf, sz, hashbuf, (d0_bignum_size(temp0) + 7) / 8));
1192         d0_free(convbuf);
1193         convbuf = NULL;
1194         CHECK(d0_bignum_import_unsigned(temp2, hashbuf, (d0_bignum_size(temp0) + 7) / 8));
1195         CHECK(d0_iobuf_write_bignum(out, temp2));
1196
1197         // multiply with secret, sub k, modulo order
1198         CHECK(d0_bignum_mod_mul(temp1, temp2, ctx->schnorr_s, temp0));
1199 #ifdef D0_BLIND_ID_POSITIVE_PROTOCOL
1200         CHECK(d0_bignum_mod_add(temp2, ctx->r, temp1, temp0));
1201 #else
1202         CHECK(d0_bignum_mod_sub(temp2, ctx->r, temp1, temp0));
1203 #endif
1204         CHECK(d0_iobuf_write_bignum(out, temp2));
1205         UNLOCKTEMPS();
1206
1207         // write the message itself
1208         if(with_msg)
1209                 CHECK(d0_iobuf_write_packet(out, message, msglen));
1210
1211         return d0_iobuf_close(out, outbuflen);
1212
1213 fail:
1214         UNLOCKTEMPS();
1215         d0_iobuf_close(out, outbuflen);
1216         return 0;
1217 }
1218 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_sign_with_private_id_sign(d0_blind_id_t *ctx, D0_BOOL is_first, D0_BOOL send_modulus, const char *message, size_t msglen, char *outbuf, size_t *outbuflen)
1219 {
1220         return d0_blind_id_sign_with_private_id_sign_internal(ctx, is_first, send_modulus, 1, message, msglen, outbuf, outbuflen);
1221 }
1222 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_sign_with_private_id_sign_detached(d0_blind_id_t *ctx, D0_BOOL is_first, D0_BOOL send_modulus, const char *message, size_t msglen, char *outbuf, size_t *outbuflen)
1223 {
1224         return d0_blind_id_sign_with_private_id_sign_internal(ctx, is_first, send_modulus, 0, message, msglen, outbuf, outbuflen);
1225 }
1226
1227 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_sign_with_private_id_verify_internal(d0_blind_id_t *ctx, D0_BOOL is_first, D0_BOOL recv_modulus, D0_BOOL with_msg, const char *inbuf, size_t inbuflen, char *msg, size_t *msglen, D0_BOOL *status)
1228 {
1229         d0_iobuf_t *in = NULL;
1230         d0_iobuf_t *conv = NULL;
1231         unsigned char *convbuf = NULL;
1232         unsigned char hashbuf[2048];
1233         size_t sz;
1234
1235         USINGTEMPS(); // temps: 0 sig^e 2 g^s 3 g^-s 4 order
1236         if(is_first)
1237         {
1238                 REPLACING(schnorr_g_to_s); REPLACING(schnorr_H_g_to_s_signature);
1239                 if(recv_modulus)
1240                         REPLACING(schnorr_G);
1241                 else
1242                         USING(schnorr_G);
1243         }
1244         else
1245         {
1246                 USING(schnorr_g_to_s); USING(schnorr_H_g_to_s_signature);
1247                 USING(schnorr_G);
1248         }
1249         USING(rsa_e); USING(rsa_n);
1250
1251         in = d0_iobuf_open_read(inbuf, inbuflen);
1252
1253         if(is_first)
1254         {
1255                 if(recv_modulus)
1256                 {
1257                         CHECK_ASSIGN(ctx->schnorr_G, d0_iobuf_read_bignum(in, ctx->schnorr_G));
1258                         CHECK(d0_bignum_cmp(ctx->schnorr_G, zero) > 0);
1259                         CHECK(d0_bignum_cmp(ctx->schnorr_G, ctx->rsa_n) < 0);
1260                 }
1261                 CHECK_ASSIGN(ctx->schnorr_g_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_g_to_s));
1262                 CHECK(d0_bignum_cmp(ctx->schnorr_g_to_s, zero) >= 0);
1263                 CHECK(d0_bignum_cmp(ctx->schnorr_g_to_s, ctx->schnorr_G) < 0);
1264                 CHECK_ASSIGN(ctx->schnorr_H_g_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_H_g_to_s_signature));
1265                 CHECK(d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, zero) >= 0);
1266                 CHECK(d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, ctx->rsa_n) < 0);
1267
1268                 // check signature of key (t = k^d, so, t^challenge = k)
1269                 LOCKTEMPS();
1270                 CHECK(d0_bignum_mod_pow(temp0, ctx->schnorr_H_g_to_s_signature, ctx->rsa_e, ctx->rsa_n));
1271
1272                 // we will actually sign SHA(4^s) to prevent a malleability attack!
1273                 sz = (d0_bignum_size(ctx->rsa_n) + 7) / 8; // this is too long, so we have to take the value % rsa_n when "decrypting"
1274                 if(sz > sizeof(hashbuf))
1275                         sz = sizeof(hashbuf);
1276                 CHECK(d0_longhash_bignum(ctx->schnorr_g_to_s, hashbuf, sz));
1277                 CHECK(d0_bignum_import_unsigned(temp2, hashbuf, sz));
1278
1279                 // + 7 / 8 is too large, so let's mod it
1280                 CHECK(d0_bignum_divmod(NULL, temp1, temp2, ctx->rsa_n));
1281
1282                 // hash complete
1283                 if(d0_bignum_cmp(temp0, temp1))
1284                 {
1285                         // accept the key anyway, but mark as failed signature! will later return 0 in status
1286                         CHECK(d0_bignum_zero(ctx->schnorr_H_g_to_s_signature));
1287                 }
1288         }
1289
1290         CHECK(d0_dl_get_order(temp4, ctx->schnorr_G));
1291         CHECK(d0_iobuf_read_bignum(in, temp0)); // e == H(m || g^r)
1292         CHECK(d0_iobuf_read_bignum(in, temp1)); // x == (r - s*e) mod |G|
1293         if(with_msg)
1294                 CHECK(d0_iobuf_read_packet(in, msg, msglen));
1295
1296         // VERIFY: g^x * (g^s)^-e = g^(x - s*e) = g^r
1297
1298         // verify schnorr ID scheme
1299         // we need g^r = g^x (g^s)^e
1300         CHECK(d0_bignum_mod_pow(temp2, four, temp1, ctx->schnorr_G));
1301 #ifdef D0_BLIND_ID_POSITIVE_PROTOCOL
1302         CHECK(d0_bignum_mod_inv(temp3, ctx->schnorr_g_to_s, ctx->schnorr_G));
1303         CHECK(d0_bignum_mod_pow(temp1, temp3, temp0, ctx->schnorr_G));
1304 #else
1305         CHECK(d0_bignum_mod_pow(temp1, ctx->schnorr_g_to_s, temp0, ctx->schnorr_G));
1306 #endif
1307         CHECK_ASSIGN(temp3, d0_bignum_mod_mul(temp3, temp1, temp2, ctx->schnorr_G)); // temp3 now is g^r
1308
1309         // hash it, hash it, everybody hash it
1310         conv = d0_iobuf_open_write_p((void **) &convbuf, 0);
1311         CHECK(d0_iobuf_write_packet(conv, msg, *msglen));
1312         CHECK(d0_iobuf_write_bignum(conv, temp3));
1313         d0_iobuf_close(conv, &sz);
1314         conv = NULL;
1315         CHECK(d0_longhash_destructive(convbuf, sz, hashbuf, (d0_bignum_size(temp4) + 7) / 8));
1316         d0_free(convbuf);
1317         convbuf = NULL;
1318         CHECK(d0_bignum_import_unsigned(temp1, hashbuf, (d0_bignum_size(temp4) + 7) / 8));
1319
1320         // verify signature
1321         CHECK(!d0_bignum_cmp(temp0, temp1));
1322         UNLOCKTEMPS();
1323
1324         if(status)
1325                 *status = !!d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, zero);
1326
1327         d0_iobuf_close(in, NULL);
1328         return 1;
1329
1330 fail:
1331         UNLOCKTEMPS();
1332         d0_iobuf_close(in, NULL);
1333         return 0;
1334 }
1335 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_sign_with_private_id_verify(d0_blind_id_t *ctx, D0_BOOL is_first, D0_BOOL recv_modulus, const char *inbuf, size_t inbuflen, char *msg, size_t *msglen, D0_BOOL *status)
1336 {
1337         return d0_blind_id_sign_with_private_id_verify_internal(ctx, is_first, recv_modulus, 1, inbuf, inbuflen, msg, msglen, status);
1338 }
1339 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_sign_with_private_id_verify_detached(d0_blind_id_t *ctx, D0_BOOL is_first, D0_BOOL recv_modulus, const char *inbuf, size_t inbuflen, const char *msg, size_t msglen, D0_BOOL *status)
1340 {
1341         return d0_blind_id_sign_with_private_id_verify_internal(ctx, is_first, recv_modulus, 0, inbuf, inbuflen, (char *) msg, &msglen, status);
1342 }
1343
1344 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_fingerprint64_public_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
1345 {
1346         d0_iobuf_t *out = NULL;
1347         unsigned char convbuf[1024];
1348         d0_iobuf_t *conv = NULL;
1349         size_t sz, n;
1350         char shabuf[32];
1351
1352         USING(rsa_n);
1353         USING(rsa_e);
1354         USING(schnorr_g_to_s);
1355
1356         out = d0_iobuf_open_write(outbuf, *outbuflen);
1357         conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
1358
1359         CHECK(d0_iobuf_write_bignum(conv, ctx->rsa_n));
1360         CHECK(d0_iobuf_write_bignum(conv, ctx->rsa_e));
1361         CHECK(d0_iobuf_write_bignum(conv, ctx->schnorr_g_to_s));
1362         CHECK(d0_iobuf_close(conv, &sz));
1363         conv = NULL;
1364
1365         n = (*outbuflen / 4) * 3;
1366         if(n > SHA_DIGESTSIZE)
1367                 n = SHA_DIGESTSIZE;
1368         CHECK(d0_iobuf_write_raw(out, sha(shabuf, convbuf, sz), n) == n);
1369         CHECK(d0_iobuf_conv_base64_out(out));
1370
1371         return d0_iobuf_close(out, outbuflen);
1372
1373 fail:
1374         if(conv)
1375                 d0_iobuf_close(conv, &sz);
1376         d0_iobuf_close(out, outbuflen);
1377         return 0;
1378 }
1379
1380 D0_BOOL d0_blind_id_sessionkey_public_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
1381 {
1382         D0_BOOL ret;
1383
1384         USINGTEMPS(); // temps: temp0 result
1385         USING(t); USING(other_g_to_t); USING(schnorr_G);
1386
1387         LOCKTEMPS();
1388         CHECK(d0_bignum_mod_pow(temp0, ctx->other_g_to_t, ctx->t, ctx->schnorr_G));
1389         ret = d0_longhash_bignum(temp0, (unsigned char *) outbuf, *outbuflen);
1390         UNLOCKTEMPS();
1391         return ret;
1392
1393 fail:
1394         UNLOCKTEMPS();
1395         return 0;
1396 }
1397
1398 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_verify_public_id(const d0_blind_id_t *ctx, D0_BOOL *status)
1399 {
1400         unsigned char hashbuf[2048];
1401         size_t sz;
1402
1403         USINGTEMPS(); // temps: temp0 temp1 temp2
1404         USING(schnorr_H_g_to_s_signature); USING(rsa_e); USING(rsa_n); USING(schnorr_g_to_s);
1405
1406         if(d0_bignum_cmp(ctx->schnorr_H_g_to_s_signature, zero))
1407         {
1408                 // check signature of key (t = k^d, so, t^challenge = k)
1409                 LOCKTEMPS();
1410
1411                 CHECK(d0_bignum_mod_pow(temp0, ctx->schnorr_H_g_to_s_signature, ctx->rsa_e, ctx->rsa_n));
1412
1413                 // we will actually sign SHA(4^s) to prevent a malleability attack!
1414                 sz = (d0_bignum_size(ctx->rsa_n) + 7) / 8; // this is too long, so we have to take the value % rsa_n when "decrypting"
1415                 if(sz > sizeof(hashbuf))
1416                         sz = sizeof(hashbuf);
1417                 CHECK(d0_longhash_bignum(ctx->schnorr_g_to_s, hashbuf, sz));
1418                 CHECK(d0_bignum_import_unsigned(temp2, hashbuf, sz));
1419
1420                 // + 7 / 8 is too large, so let's mod it
1421                 CHECK(d0_bignum_divmod(NULL, temp1, temp2, ctx->rsa_n));
1422
1423                 // hash complete
1424                 CHECK(d0_bignum_cmp(temp0, temp1) == 0);
1425
1426                 *status = 1;
1427         }
1428         else
1429                 *status = 0;
1430
1431         UNLOCKTEMPS();
1432         return 1;
1433
1434 fail:
1435         UNLOCKTEMPS();
1436         return 0;
1437 }
1438
1439 D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_verify_private_id(const d0_blind_id_t *ctx)
1440 {
1441         USINGTEMPS(); // temps: temp0 = g^s
1442         USING(schnorr_G); USING(schnorr_s); USING(schnorr_g_to_s);
1443
1444         LOCKTEMPS();
1445         CHECK(d0_bignum_mod_pow(temp0, four, ctx->schnorr_s, ctx->schnorr_G));
1446         CHECK(!d0_bignum_cmp(temp0, ctx->schnorr_g_to_s));
1447         UNLOCKTEMPS();
1448         return 1;
1449
1450 fail:
1451         UNLOCKTEMPS();
1452         return 0;
1453 }
1454
1455 d0_blind_id_t *d0_blind_id_new(void)
1456 {
1457         d0_blind_id_t *b = d0_malloc(sizeof(d0_blind_id_t));
1458         memset(b, 0, sizeof(*b));
1459         return b;
1460 }
1461
1462 void d0_blind_id_free(d0_blind_id_t *a)
1463 {
1464         d0_blind_id_clear(a);
1465         d0_free(a);
1466 }
1467
1468 void d0_blind_id_util_sha256(char *out, const char *in, size_t n)
1469 {
1470         SHA256_CTX context;
1471         SHA256_Init(&context);
1472         SHA256_Update(&context, (const unsigned char *) in, n);
1473         return SHA256_Final((unsigned char *) out, &context);
1474 }
1475
1476 void d0_blind_id_setmallocfuncs(d0_malloc_t *m, d0_free_t *f)
1477 {
1478         d0_setmallocfuncs(m, f);
1479 }
1480 void d0_blind_id_setmutexfuncs(d0_createmutex_t *c, d0_destroymutex_t *d, d0_lockmutex_t *l, d0_unlockmutex_t *u)
1481 {
1482         d0_setmutexfuncs(c, d, l, u);
1483 }