36efe855fd669aa4f2a618efacfa62747a7b6d48
[platform/upstream/nettle.git] / testsuite / testutils.c
1 /* testutils.c */
2
3 #include "testutils.h"
4
5 #include "base16.h"
6 #include "cbc.h"
7 #include "ctr.h"
8 #include "knuth-lfib.h"
9 #include "macros.h"
10 #include "nettle-internal.h"
11
12 #include <assert.h>
13 #include <ctype.h>
14
15 void
16 die(const char *format, ...)
17 {
18   va_list args;
19   va_start(args, format);
20   vfprintf(stderr, format, args);
21   va_end(args);
22
23   abort ();
24 }
25
26 void *
27 xalloc(size_t size)
28 {
29   void *p = malloc(size);
30   if (size && !p)
31     {
32       fprintf(stderr, "Virtual memory exhausted.\n");
33       abort();
34     }
35
36   return p;
37 }
38
39 static struct tstring *tstring_first = NULL;
40
41 struct tstring *
42 tstring_alloc (size_t length)
43 {
44   struct tstring *s = xalloc(sizeof(struct tstring) + length);
45   s->length = length;
46   s->next = tstring_first;
47   /* NUL-terminate, for convenience. */
48   s->data[length] = '\0';
49   tstring_first = s;
50   return s;
51 }
52
53 void
54 tstring_clear(void)
55 {
56   while (tstring_first)
57     {
58       struct tstring *s = tstring_first;
59       tstring_first = s->next;
60       free(s);
61     }
62 }
63
64 struct tstring *
65 tstring_data(size_t length, const char *data)
66 {
67   struct tstring *s = tstring_alloc (length);
68   memcpy (s->data, data, length);
69   return s;
70 }
71
72 struct tstring *
73 tstring_hex(const char *hex)
74 {
75   struct base16_decode_ctx ctx;
76   struct tstring *s;
77   size_t length = strlen(hex);
78
79   s = tstring_alloc(BASE16_DECODE_LENGTH (length));
80   base16_decode_init (&ctx);
81   ASSERT (base16_decode_update (&ctx, &s->length, s->data,
82                                 length, hex));
83   ASSERT (base16_decode_final (&ctx));
84
85   return s;
86 }
87
88 void
89 tstring_print_hex(const struct tstring *s)
90 {
91   print_hex (s->length, s->data);
92 }
93
94 void
95 print_hex(size_t length, const uint8_t *data)
96 {
97   size_t i;
98   
99   for (i = 0; i < length; i++)
100     {
101       switch (i % 16)
102         {
103         default:
104           break;
105         case 0:
106           printf("\n");
107           break;
108         case 8:
109           printf(" ");
110           break;
111         }
112       printf("%02x", data[i]);
113     }
114   printf("\n");
115 }
116
117 int verbose = 0;
118
119 int
120 main(int argc, char **argv)
121 {
122   if (argc > 1)
123     {
124       if (argc == 2 && !strcmp(argv[1], "-v"))
125         verbose = 1;
126       else
127         {
128           fprintf(stderr, "Invalid argument `%s', only accepted option is `-v'.\n",
129                   argv[1]);
130           return 1;
131         }
132     }
133
134   test_main();
135
136   tstring_clear();
137   return EXIT_SUCCESS;
138 }
139
140 void
141 test_cipher(const struct nettle_cipher *cipher,
142             const struct tstring *key,
143             const struct tstring *cleartext,
144             const struct tstring *ciphertext)
145 {
146   void *ctx = xalloc(cipher->context_size);
147   uint8_t *data = xalloc(cleartext->length);
148   size_t length;
149   ASSERT (cleartext->length == ciphertext->length);
150   length = cleartext->length;
151
152   ASSERT (key->length == cipher->key_size);
153   cipher->set_encrypt_key(ctx, key->data);
154   cipher->encrypt(ctx, length, data, cleartext->data);
155
156   if (!MEMEQ(length, data, ciphertext->data))
157     {
158       fprintf(stderr, "Encrypt failed:\nInput:");
159       tstring_print_hex(cleartext);
160       fprintf(stderr, "\nOutput: ");
161       print_hex(length, data);
162       fprintf(stderr, "\nExpected:");
163       tstring_print_hex(ciphertext);
164       fprintf(stderr, "\n");
165       FAIL();
166     }
167   cipher->set_decrypt_key(ctx, key->data);
168   cipher->decrypt(ctx, length, data, data);
169
170   if (!MEMEQ(length, data, cleartext->data))
171     {
172       fprintf(stderr, "Decrypt failed:\nInput:");
173       tstring_print_hex(ciphertext);
174       fprintf(stderr, "\nOutput: ");
175       print_hex(length, data);
176       fprintf(stderr, "\nExpected:");
177       tstring_print_hex(cleartext);
178       fprintf(stderr, "\n");
179       FAIL();
180     }
181
182   free(ctx);
183   free(data);
184 }
185
186 void
187 test_cipher_cbc(const struct nettle_cipher *cipher,
188                 const struct tstring *key,
189                 const struct tstring *cleartext,
190                 const struct tstring *ciphertext,
191                 const struct tstring *iiv)
192 {
193   void *ctx = xalloc(cipher->context_size);
194   uint8_t *data;
195   uint8_t *iv = xalloc(cipher->block_size);
196   size_t length;
197
198   ASSERT (cleartext->length == ciphertext->length);
199   length = cleartext->length;
200
201   ASSERT (key->length == cipher->key_size);
202   ASSERT (iiv->length == cipher->block_size);
203
204   data = xalloc(length);  
205   cipher->set_encrypt_key(ctx, key->data);
206   memcpy(iv, iiv->data, cipher->block_size);
207
208   cbc_encrypt(ctx, cipher->encrypt,
209               cipher->block_size, iv,
210               length, data, cleartext->data);
211
212   if (!MEMEQ(length, data, ciphertext->data))
213     {
214       fprintf(stderr, "CBC encrypt failed:\nInput:");
215       tstring_print_hex(cleartext);
216       fprintf(stderr, "\nOutput: ");
217       print_hex(length, data);
218       fprintf(stderr, "\nExpected:");
219       tstring_print_hex(ciphertext);
220       fprintf(stderr, "\n");
221       FAIL();
222     }
223   cipher->set_decrypt_key(ctx, key->data);
224   memcpy(iv, iiv->data, cipher->block_size);
225
226   cbc_decrypt(ctx, cipher->decrypt,
227               cipher->block_size, iv,
228               length, data, data);
229
230   if (!MEMEQ(length, data, cleartext->data))
231     {
232       fprintf(stderr, "CBC decrypt failed:\nInput:");
233       tstring_print_hex(ciphertext);
234       fprintf(stderr, "\nOutput: ");
235       print_hex(length, data);
236       fprintf(stderr, "\nExpected:");
237       tstring_print_hex(cleartext);
238       fprintf(stderr, "\n");
239       FAIL();
240     }
241
242   free(ctx);
243   free(data);
244   free(iv);
245 }
246
247 void
248 test_cipher_ctr(const struct nettle_cipher *cipher,
249                 const struct tstring *key,
250                 const struct tstring *cleartext,
251                 const struct tstring *ciphertext,
252                 const struct tstring *ictr)
253 {
254   void *ctx = xalloc(cipher->context_size);
255   uint8_t *data;
256   uint8_t *ctr = xalloc(cipher->block_size);
257   uint8_t *octr = xalloc(cipher->block_size);
258   size_t length, nblocks;
259   unsigned low;
260
261   ASSERT (cleartext->length == ciphertext->length);
262   length = cleartext->length;
263
264   ASSERT (key->length == cipher->key_size);
265   ASSERT (ictr->length == cipher->block_size);
266
267   /* Compute expected counter value after the operation. */
268   nblocks = (length + cipher->block_size - 1) / cipher->block_size;
269   ASSERT (nblocks < 0x100);
270
271   memcpy (octr, ictr->data, cipher->block_size - 1);
272   low = ictr->data[cipher->block_size - 1] + nblocks;
273   octr[cipher->block_size - 1] = low;
274
275   if (low >= 0x100)
276     INCREMENT (cipher->block_size - 1, octr);
277
278   data = xalloc(length);  
279
280   cipher->set_encrypt_key(ctx, key->data);
281   memcpy(ctr, ictr->data, cipher->block_size);
282
283   ctr_crypt(ctx, cipher->encrypt,
284             cipher->block_size, ctr,
285             length, data, cleartext->data);
286
287   if (!MEMEQ(length, data, ciphertext->data))
288     {
289       fprintf(stderr, "CTR encrypt failed:\nInput:");
290       tstring_print_hex(cleartext);
291       fprintf(stderr, "\nOutput: ");
292       print_hex(length, data);
293       fprintf(stderr, "\nExpected:");
294       tstring_print_hex(ciphertext);
295       fprintf(stderr, "\n");
296       FAIL();
297     }
298
299   ASSERT (MEMEQ (cipher->block_size, ctr, octr));
300
301   memcpy(ctr, ictr->data, cipher->block_size);
302
303   ctr_crypt(ctx, cipher->encrypt,
304             cipher->block_size, ctr,
305             length, data, data);
306
307   if (!MEMEQ(length, data, cleartext->data))
308     {
309       fprintf(stderr, "CTR decrypt failed:\nInput:");
310       tstring_print_hex(ciphertext);
311       fprintf(stderr, "\nOutput: ");
312       print_hex(length, data);
313       fprintf(stderr, "\nExpected:");
314       tstring_print_hex(cleartext);
315       fprintf(stderr, "\n");
316       FAIL();
317     }
318
319   ASSERT (MEMEQ (cipher->block_size, ctr, octr));
320
321   free(ctx);
322   free(data);
323   free(octr);
324   free(ctr);
325 }
326
327 #if 0
328 void
329 test_cipher_stream(const struct nettle_cipher *cipher,
330                    const struct tstring *key,
331                    const struct tstring *cleartext,
332                    const struct tstring *ciphertext)
333 {
334   size_t block;
335   
336   void *ctx = xalloc(cipher->context_size);
337   uint8_t *data;
338   size_t length;
339
340   ASSERT (cleartext->length == ciphertext->length);
341   length = cleartext->length;
342
343   data = xalloc(length + 1);
344
345   for (block = 1; block <= length; block++)
346     {
347       size_t i;
348
349       memset(data, 0x17, length + 1);
350       cipher->set_encrypt_key(ctx, key->length, key->data);
351
352       for (i = 0; i + block < length; i += block)
353         {
354           cipher->encrypt(ctx, block, data + i, cleartext->data + i);
355           ASSERT (data[i + block] == 0x17);
356         }
357
358       cipher->encrypt(ctx, length - i, data + i, cleartext->data + i);
359       ASSERT (data[length] == 0x17);
360       
361       if (!MEMEQ(length, data, ciphertext->data))
362         {
363           fprintf(stderr, "Encrypt failed, block size %lu\nInput:",
364                   (unsigned long) block);
365           tstring_print_hex(cleartext);
366           fprintf(stderr, "\nOutput: ");
367           print_hex(length, data);
368           fprintf(stderr, "\nExpected:");
369           tstring_print_hex(ciphertext);
370           fprintf(stderr, "\n");
371           FAIL();           
372         }
373     }
374   
375   cipher->set_decrypt_key(ctx, key->length, key->data);
376   cipher->decrypt(ctx, length, data, data);
377
378   ASSERT (data[length] == 0x17);
379
380   if (!MEMEQ(length, data, cleartext->data))
381     {
382       fprintf(stderr, "Decrypt failed\nInput:");
383       tstring_print_hex(ciphertext);
384       fprintf(stderr, "\nOutput: ");
385       print_hex(length, data);
386       fprintf(stderr, "\nExpected:");
387       tstring_print_hex(cleartext);
388       fprintf(stderr, "\n");
389       FAIL();       
390     }
391
392   free(ctx);
393   free(data);
394 }
395 #endif
396
397 void
398 test_aead(const struct nettle_aead *aead,
399           nettle_hash_update_func *set_nonce,
400           const struct tstring *key,
401           const struct tstring *authtext,
402           const struct tstring *cleartext,
403           const struct tstring *ciphertext,
404           const struct tstring *nonce,
405           const struct tstring *digest)
406 {
407   void *ctx = xalloc(aead->context_size);
408   uint8_t *data;
409   uint8_t *buffer = xalloc(aead->digest_size);
410   size_t length;
411
412   ASSERT (cleartext->length == ciphertext->length);
413   length = cleartext->length;
414
415   ASSERT (key->length == aead->key_size);
416   ASSERT (digest->length <= aead->digest_size);
417
418   data = xalloc(length);
419   
420   /* encryption */
421   memset(buffer, 0, aead->digest_size);
422   aead->set_encrypt_key(ctx, key->data);
423
424   if (nonce->length != aead->nonce_size)
425     {
426       ASSERT (set_nonce);
427       set_nonce (ctx, nonce->length, nonce->data);
428     }
429   else
430     aead->set_nonce(ctx, nonce->data);
431
432   if (authtext->length)
433     aead->update(ctx, authtext->length, authtext->data);
434     
435   if (length)
436     aead->encrypt(ctx, length, data, cleartext->data);
437
438   aead->digest(ctx, digest->length, buffer);
439
440   ASSERT(MEMEQ(length, data, ciphertext->data));
441   ASSERT(MEMEQ(digest->length, buffer, digest->data));
442
443   /* decryption */
444   memset(buffer, 0, aead->digest_size);
445
446   aead->set_decrypt_key(ctx, key->data);
447
448   if (nonce->length != aead->nonce_size)
449     {
450       ASSERT (set_nonce);
451       set_nonce (ctx, nonce->length, nonce->data);
452     }
453   else
454     aead->set_nonce(ctx, nonce->data);
455
456   if (authtext->length)
457     aead->update(ctx, authtext->length, authtext->data);
458     
459   if (length)
460     aead->decrypt(ctx, length, data, data);
461
462   aead->digest(ctx, digest->length, buffer);
463
464   ASSERT(MEMEQ(length, data, cleartext->data));
465   ASSERT(MEMEQ(digest->length, buffer, digest->data));
466
467   free(ctx);
468   free(data);
469   free(buffer);
470 }
471
472 void
473 test_hash(const struct nettle_hash *hash,
474           const struct tstring *msg,
475           const struct tstring *digest)
476 {
477   void *ctx = xalloc(hash->context_size);
478   uint8_t *buffer = xalloc(hash->digest_size);
479   uint8_t *input;
480   unsigned offset;
481
482   ASSERT (digest->length == hash->digest_size);
483
484   hash->init(ctx);
485   hash->update(ctx, msg->length, msg->data);
486   hash->digest(ctx, hash->digest_size, buffer);
487
488   if (MEMEQ(hash->digest_size, digest->data, buffer) == 0)
489     {
490       fprintf(stdout, "\nGot:\n");
491       print_hex(hash->digest_size, buffer);
492       fprintf(stdout, "\nExpected:\n");
493       print_hex(hash->digest_size, digest->data);
494       abort();
495     }
496
497   memset(buffer, 0, hash->digest_size);
498
499   hash->update(ctx, msg->length, msg->data);
500   hash->digest(ctx, hash->digest_size - 1, buffer);
501
502   ASSERT(MEMEQ(hash->digest_size - 1, digest->data, buffer));
503
504   ASSERT(buffer[hash->digest_size - 1] == 0);
505
506   input = xalloc (msg->length + 16);
507   for (offset = 0; offset < 16; offset++)
508     {
509       memset (input, 0, msg->length + 16);
510       memcpy (input + offset, msg->data, msg->length);
511       hash->update (ctx, msg->length, input + offset);
512       hash->digest (ctx, hash->digest_size, buffer);
513       if (MEMEQ(hash->digest_size, digest->data, buffer) == 0)
514         {
515           fprintf(stdout, "hash input address: %p\nGot:\n", input + offset);
516           print_hex(hash->digest_size, buffer);
517           fprintf(stdout, "\nExpected:\n");
518           print_hex(hash->digest_size, digest->data);
519           abort();
520         }      
521     }
522   free(ctx);
523   free(buffer);
524   free(input);
525 }
526
527 void
528 test_hash_large(const struct nettle_hash *hash,
529                 size_t count, size_t length,
530                 uint8_t c,
531                 const struct tstring *digest)
532 {
533   void *ctx = xalloc(hash->context_size);
534   uint8_t *buffer = xalloc(hash->digest_size);
535   uint8_t *data = xalloc(length);
536   size_t i;
537
538   ASSERT (digest->length == hash->digest_size);
539
540   memset(data, c, length);
541
542   hash->init(ctx);
543   for (i = 0; i < count; i++)
544     {
545       hash->update(ctx, length, data);
546       if (i % (count / 50) == 0)
547         fprintf (stderr, ".");
548     }
549   fprintf (stderr, "\n");
550   
551   hash->digest(ctx, hash->digest_size, buffer);
552
553   print_hex(hash->digest_size, buffer);
554
555   ASSERT (MEMEQ(hash->digest_size, digest->data, buffer));
556
557   free(ctx);
558   free(buffer);
559   free(data);
560 }
561
562 void
563 test_armor(const struct nettle_armor *armor,
564            size_t data_length,
565            const uint8_t *data,
566            const uint8_t *ascii)
567 {
568   size_t ascii_length = strlen(ascii);
569   uint8_t *buffer = xalloc(1 + ascii_length);
570   uint8_t *check = xalloc(1 + armor->decode_length(ascii_length));
571   void *encode = xalloc(armor->encode_context_size);
572   void *decode = xalloc(armor->decode_context_size);
573   size_t done;
574
575   ASSERT(ascii_length
576          <= (armor->encode_length(data_length) + armor->encode_final_length));
577   ASSERT(data_length <= armor->decode_length(ascii_length));
578   
579   memset(buffer, 0x33, 1 + ascii_length);
580   memset(check, 0x55, 1 + data_length);
581
582   armor->encode_init(encode);
583   
584   done = armor->encode_update(encode, buffer, data_length, data);
585   done += armor->encode_final(encode, buffer + done);
586   ASSERT(done == ascii_length);
587
588   ASSERT (MEMEQ(ascii_length, buffer, ascii));
589   ASSERT (0x33 == buffer[strlen(ascii)]);
590
591   armor->decode_init(decode);
592   done = armor->decode_length(ascii_length);
593
594   ASSERT(armor->decode_update(decode, &done, check, ascii_length, buffer));
595   ASSERT(done == data_length);
596   ASSERT(armor->decode_final(decode));
597   
598   ASSERT (MEMEQ(data_length, check, data));
599   ASSERT (0x55 == check[data_length]);
600
601   free(buffer);
602   free(check);
603   free(encode);
604   free(decode);
605 }
606
607 #if WITH_HOGWEED
608
609 #ifndef mpz_combit
610 /* Missing in older gmp */
611 static void
612 mpz_combit (mpz_t x, unsigned long int bit)
613 {
614   if (mpz_tstbit(x, bit))
615     mpz_clrbit(x, bit);
616   else
617     mpz_setbit(x, bit);
618 }
619 #endif
620
621 #ifndef mpn_zero_p
622 int
623 mpn_zero_p (mp_srcptr ap, mp_size_t n)
624 {
625   while (--n >= 0)
626     {
627       if (ap[n] != 0)
628         return 0;
629     }
630   return 1;
631 }
632 #endif
633
634 void
635 mpn_out_str (FILE *f, int base, const mp_limb_t *xp, mp_size_t xn)
636 {
637   mpz_t x;
638   mpz_out_str (f, base, mpz_roinit_n (x, xp, xn));
639 }
640
641 #if NETTLE_USE_MINI_GMP
642 void
643 gmp_randinit_default (struct knuth_lfib_ctx *ctx)
644 {
645   knuth_lfib_init (ctx, 17);
646 }
647 void
648 mpz_urandomb (mpz_t r, struct knuth_lfib_ctx *ctx, mp_bitcnt_t bits)
649 {
650   size_t bytes = (bits+7)/8;
651   uint8_t *buf = xalloc (bytes);
652
653   knuth_lfib_random (ctx, bytes, buf);
654   buf[bytes-1] &= 0xff >> (8*bytes - bits);
655   nettle_mpz_set_str_256_u (r, bytes, buf);
656   free (buf);
657 }
658 #endif /* NETTLE_USE_MINI_GMP */
659
660 mp_limb_t *
661 xalloc_limbs (mp_size_t n)
662 {
663   return xalloc (n * sizeof (mp_limb_t));
664 }
665
666 /* Expects local variables pub, key, rstate, digest, signature */
667 #define SIGN(hash, msg, expected) do { \
668   hash##_update(&hash, LDATA(msg));                                     \
669   ASSERT(rsa_##hash##_sign(key, &hash, signature));                     \
670   if (verbose)                                                          \
671     {                                                                   \
672       fprintf(stderr, "rsa-%s signature: ", #hash);                     \
673       mpz_out_str(stderr, 16, signature);                               \
674       fprintf(stderr, "\n");                                            \
675     }                                                                   \
676   ASSERT(mpz_cmp (signature, expected) == 0);                           \
677                                                                         \
678   hash##_update(&hash, LDATA(msg));                                     \
679   ASSERT(rsa_##hash##_sign_tr(pub, key, &rstate,                        \
680                               (nettle_random_func *) knuth_lfib_random, \
681                               &hash, signature));                       \
682   ASSERT(mpz_cmp (signature, expected) == 0);                           \
683                                                                         \
684   hash##_update(&hash, LDATA(msg));                                     \
685   hash##_digest(&hash, sizeof(digest), digest);                         \
686   ASSERT(rsa_##hash##_sign_digest(key, digest, signature));             \
687   ASSERT(mpz_cmp (signature, expected) == 0);                           \
688                                                                         \
689   ASSERT(rsa_##hash##_sign_digest_tr(pub, key, &rstate,                 \
690                                      (nettle_random_func *)knuth_lfib_random, \
691                                      digest, signature));               \
692   ASSERT(mpz_cmp (signature, expected) == 0);                           \
693 } while(0)
694
695 #define VERIFY(key, hash, msg, signature) (     \
696   hash##_update(&hash, LDATA(msg)),             \
697   rsa_##hash##_verify(key, &hash, signature)    \
698 )
699
700 void
701 test_rsa_set_key_1(struct rsa_public_key *pub,
702                    struct rsa_private_key *key)
703 {
704   /* Initialize key pair for test programs */
705   /* 1000-bit key, generated by
706    *
707    *   lsh-keygen -a rsa -l 1000 -f advanced-hex
708    *
709    * (private-key (rsa-pkcs1 
710    *        (n #69abd505285af665 36ddc7c8f027e6f0 ed435d6748b16088
711    *            4fd60842b3a8d7fb bd8a3c98f0cc50ae 4f6a9f7dd73122cc
712    *            ec8afa3f77134406 f53721973115fc2d 8cfbba23b145f28d
713    *            84f81d3b6ae8ce1e 2850580c026e809b cfbb52566ea3a3b3
714    *            df7edf52971872a7 e35c1451b8636d22 279a8fb299368238
715    *            e545fbb4cf#)
716    *        (e #0db2ad57#)
717    *        (d #3240a56f4cd0dcc2 4a413eb4ea545259 5c83d771a1c2ba7b
718    *            ec47c5b43eb4b374 09bd2aa1e236dd86 481eb1768811412f
719    *            f8d91be3545912af b55c014cb55ceac6 54216af3b85d5c4f
720    *            4a32894e3b5dfcde 5b2875aa4dc8d9a8 6afd0ca92ef50d35
721    *            bd09f1c47efb4c8d c631e07698d362aa 4a83fd304e66d6c5
722    *            468863c307#)
723    *        (p #0a66399919be4b4d e5a78c5ea5c85bf9 aba8c013cb4a8732
724    *            14557a12bd67711e bb4073fd39ad9a86 f4e80253ad809e5b
725    *            f2fad3bc37f6f013 273c9552c9f489#)
726    *        (q #0a294f069f118625 f5eae2538db9338c 776a298eae953329
727    *            9fd1eed4eba04e82 b2593bc98ba8db27 de034da7daaea795
728    *            2d55b07b5f9a5875 d1ca5f6dcab897#)
729    *        (a #011b6c48eb592eee e85d1bb35cfb6e07 344ea0b5e5f03a28
730    *            5b405396cbc78c5c 868e961db160ba8d 4b984250930cf79a
731    *            1bf8a9f28963de53 128aa7d690eb87#)
732    *        (b #0409ecf3d2557c88 214f1af5e1f17853 d8b2d63782fa5628
733    *            60cf579b0833b7ff 5c0529f2a97c6452 2fa1a8878a9635ab
734    *            ce56debf431bdec2 70b308fa5bf387#)
735    *        (c #04e103ee925cb5e6 6653949fa5e1a462 c9e65e1adcd60058
736    *            e2df9607cee95fa8 daec7a389a7d9afc 8dd21fef9d83805a
737    *            40d46f49676a2f6b 2926f70c572c00#)))
738    */
739   
740   mpz_set_str(pub->n,
741               "69abd505285af665" "36ddc7c8f027e6f0" "ed435d6748b16088"
742               "4fd60842b3a8d7fb" "bd8a3c98f0cc50ae" "4f6a9f7dd73122cc"
743               "ec8afa3f77134406" "f53721973115fc2d" "8cfbba23b145f28d"
744               "84f81d3b6ae8ce1e" "2850580c026e809b" "cfbb52566ea3a3b3"
745               "df7edf52971872a7" "e35c1451b8636d22" "279a8fb299368238"
746               "e545fbb4cf", 16);
747   mpz_set_str(pub->e, "0db2ad57", 16);
748
749   ASSERT (rsa_public_key_prepare(pub));
750   
751   /* d is not used */
752 #if 0  
753   mpz_set_str(key->d,
754               "3240a56f4cd0dcc2" "4a413eb4ea545259" "5c83d771a1c2ba7b"
755               "ec47c5b43eb4b374" "09bd2aa1e236dd86" "481eb1768811412f"
756               "f8d91be3545912af" "b55c014cb55ceac6" "54216af3b85d5c4f"
757               "4a32894e3b5dfcde" "5b2875aa4dc8d9a8" "6afd0ca92ef50d35"
758               "bd09f1c47efb4c8d" "c631e07698d362aa" "4a83fd304e66d6c5"
759               "468863c307", 16);
760 #endif
761   
762   mpz_set_str(key->p,
763               "0a66399919be4b4d" "e5a78c5ea5c85bf9" "aba8c013cb4a8732"
764               "14557a12bd67711e" "bb4073fd39ad9a86" "f4e80253ad809e5b"
765               "f2fad3bc37f6f013" "273c9552c9f489", 16);
766
767   mpz_set_str(key->q,
768               "0a294f069f118625" "f5eae2538db9338c" "776a298eae953329"
769               "9fd1eed4eba04e82" "b2593bc98ba8db27" "de034da7daaea795"
770               "2d55b07b5f9a5875" "d1ca5f6dcab897", 16);
771   
772   mpz_set_str(key->a,
773               "011b6c48eb592eee" "e85d1bb35cfb6e07" "344ea0b5e5f03a28"
774               "5b405396cbc78c5c" "868e961db160ba8d" "4b984250930cf79a"
775               "1bf8a9f28963de53" "128aa7d690eb87", 16);
776   
777   mpz_set_str(key->b,
778               "0409ecf3d2557c88" "214f1af5e1f17853" "d8b2d63782fa5628"
779               "60cf579b0833b7ff" "5c0529f2a97c6452" "2fa1a8878a9635ab"
780               "ce56debf431bdec2" "70b308fa5bf387", 16);
781   
782   mpz_set_str(key->c,
783               "04e103ee925cb5e6" "6653949fa5e1a462" "c9e65e1adcd60058"
784               "e2df9607cee95fa8" "daec7a389a7d9afc" "8dd21fef9d83805a"
785               "40d46f49676a2f6b" "2926f70c572c00", 16);
786
787   ASSERT (rsa_private_key_prepare(key));
788   ASSERT (pub->size == key->size);
789 }
790
791 void
792 test_rsa_md5(struct rsa_public_key *pub,
793              struct rsa_private_key *key,
794              mpz_t expected)
795 {
796   struct md5_ctx md5;
797   struct knuth_lfib_ctx rstate;
798   uint8_t digest[MD5_DIGEST_SIZE];
799   mpz_t signature;
800
801   md5_init(&md5);
802   mpz_init(signature);
803   knuth_lfib_init (&rstate, 15);
804
805   SIGN(md5, "The magic words are squeamish ossifrage", expected);
806
807   /* Try bad data */
808   ASSERT (!VERIFY(pub, md5,
809                   "The magick words are squeamish ossifrage", signature));
810
811   /* Try correct data */
812   ASSERT (VERIFY(pub, md5,
813                  "The magic words are squeamish ossifrage", signature));
814
815   /* Try bad signature */
816   mpz_combit(signature, 17);
817   ASSERT (!VERIFY(pub, md5,
818                   "The magic words are squeamish ossifrage", signature));
819
820   mpz_clear(signature);
821 }
822
823 void
824 test_rsa_sha1(struct rsa_public_key *pub,
825               struct rsa_private_key *key,
826               mpz_t expected)
827 {
828   struct sha1_ctx sha1;
829   struct knuth_lfib_ctx rstate;
830   uint8_t digest[SHA1_DIGEST_SIZE];
831   mpz_t signature;
832
833   sha1_init(&sha1);
834   mpz_init(signature);
835   knuth_lfib_init (&rstate, 16);
836
837   SIGN(sha1, "The magic words are squeamish ossifrage", expected);
838
839   /* Try bad data */
840   ASSERT (!VERIFY(pub, sha1,
841                   "The magick words are squeamish ossifrage", signature));
842
843   /* Try correct data */
844   ASSERT (VERIFY(pub, sha1,
845                  "The magic words are squeamish ossifrage", signature));
846
847   /* Try bad signature */
848   mpz_combit(signature, 17);
849   ASSERT (!VERIFY(pub, sha1,
850                   "The magic words are squeamish ossifrage", signature));
851
852   mpz_clear(signature);
853 }
854
855 void
856 test_rsa_sha256(struct rsa_public_key *pub,
857                 struct rsa_private_key *key,
858                 mpz_t expected)
859 {
860   struct sha256_ctx sha256;
861   struct knuth_lfib_ctx rstate;
862   uint8_t digest[SHA256_DIGEST_SIZE];
863   mpz_t signature;
864
865   sha256_init(&sha256);
866   mpz_init(signature);
867   knuth_lfib_init (&rstate, 17);
868
869   SIGN(sha256, "The magic words are squeamish ossifrage", expected);
870
871   /* Try bad data */
872   ASSERT (!VERIFY(pub, sha256,
873                   "The magick words are squeamish ossifrage", signature));
874
875   /* Try correct data */
876   ASSERT (VERIFY(pub, sha256,
877                  "The magic words are squeamish ossifrage", signature));
878
879   /* Try bad signature */
880   mpz_combit(signature, 17);
881   ASSERT (!VERIFY(pub, sha256,
882                   "The magic words are squeamish ossifrage", signature));
883
884   mpz_clear(signature);
885 }
886
887 void
888 test_rsa_sha512(struct rsa_public_key *pub,
889                 struct rsa_private_key *key,
890                 mpz_t expected)
891 {
892   struct sha512_ctx sha512;
893   struct knuth_lfib_ctx rstate;
894   uint8_t digest[SHA512_DIGEST_SIZE];
895   mpz_t signature;
896
897   sha512_init(&sha512);
898   mpz_init(signature);
899   knuth_lfib_init (&rstate, 18);
900
901   SIGN(sha512, "The magic words are squeamish ossifrage", expected);
902
903   /* Try bad data */
904   ASSERT (!VERIFY(pub, sha512,
905                   "The magick words are squeamish ossifrage", signature));
906
907   /* Try correct data */
908   ASSERT (VERIFY(pub, sha512,
909                  "The magic words are squeamish ossifrage", signature));
910
911   /* Try bad signature */
912   mpz_combit(signature, 17);
913   ASSERT (!VERIFY(pub, sha512,
914                   "The magic words are squeamish ossifrage", signature));
915
916   mpz_clear(signature);
917 }
918
919 #undef SIGN
920 #undef VERIFY
921
922 void
923 test_rsa_key(struct rsa_public_key *pub,
924              struct rsa_private_key *key)
925 {
926   mpz_t tmp;
927   mpz_t phi;
928   
929   mpz_init(tmp); mpz_init(phi);
930   
931   if (verbose)
932     {
933       /* FIXME: Use gmp_printf */
934       fprintf(stderr, "Public key: n=");
935       mpz_out_str(stderr, 16, pub->n);
936       fprintf(stderr, "\n    e=");
937       mpz_out_str(stderr, 16, pub->e);
938
939       fprintf(stderr, "\n\nPrivate key: d=");
940       mpz_out_str(stderr, 16, key->d);
941       fprintf(stderr, "\n    p=");
942       mpz_out_str(stderr, 16, key->p);
943       fprintf(stderr, "\n    q=");
944       mpz_out_str(stderr, 16, key->q);
945       fprintf(stderr, "\n    a=");
946       mpz_out_str(stderr, 16, key->a);
947       fprintf(stderr, "\n    b=");
948       mpz_out_str(stderr, 16, key->b);
949       fprintf(stderr, "\n    c=");
950       mpz_out_str(stderr, 16, key->c);
951       fprintf(stderr, "\n\n");
952     }
953
954   /* Check n = p q */
955   mpz_mul(tmp, key->p, key->q);
956   ASSERT (mpz_cmp(tmp, pub->n)== 0);
957
958   /* Check c q = 1 mod p */
959   mpz_mul(tmp, key->c, key->q);
960   mpz_fdiv_r(tmp, tmp, key->p);
961   ASSERT (mpz_cmp_ui(tmp, 1) == 0);
962
963   /* Check ed = 1 (mod phi) */
964   mpz_sub_ui(phi, key->p, 1);
965   mpz_sub_ui(tmp, key->q, 1);
966
967   mpz_mul(phi, phi, tmp);
968
969   mpz_mul(tmp, pub->e, key->d);
970   mpz_fdiv_r(tmp, tmp, phi);
971   ASSERT (mpz_cmp_ui(tmp, 1) == 0);
972
973   /* Check a e = 1 (mod (p-1) ) */
974   mpz_sub_ui(phi, key->p, 1);
975   mpz_mul(tmp, pub->e, key->a);
976   mpz_fdiv_r(tmp, tmp, phi);
977   ASSERT (mpz_cmp_ui(tmp, 1) == 0);
978   
979   /* Check b e = 1 (mod (q-1) ) */
980   mpz_sub_ui(phi, key->q, 1);
981   mpz_mul(tmp, pub->e, key->b);
982   mpz_fdiv_r(tmp, tmp, phi);
983   ASSERT (mpz_cmp_ui(tmp, 1) == 0);
984   
985   mpz_clear(tmp); mpz_clear(phi);
986 }
987
988 /* Requires that the context is named like the hash algorithm. */
989 #define DSA_VERIFY(key, hash, msg, signature)   \
990   (hash##_update(&hash, LDATA(msg)),            \
991    dsa_##hash##_verify(key, &hash, signature))
992
993 void
994 test_dsa160(const struct dsa_public_key *pub,
995             const struct dsa_private_key *key,
996             const struct dsa_signature *expected)
997 {
998   struct sha1_ctx sha1;
999   struct dsa_signature signature;
1000   struct knuth_lfib_ctx lfib;
1001   
1002   sha1_init(&sha1);
1003   dsa_signature_init(&signature);
1004   knuth_lfib_init(&lfib, 1111);
1005   
1006   sha1_update(&sha1, LDATA("The magic words are squeamish ossifrage"));
1007   ASSERT (dsa_sha1_sign(pub, key,
1008                         &lfib, (nettle_random_func *) knuth_lfib_random,
1009                         &sha1, &signature));
1010
1011   if (verbose)
1012     {
1013       fprintf(stderr, "dsa160 signature: ");
1014       mpz_out_str(stderr, 16, signature.r);
1015       fprintf(stderr, ", ");
1016       mpz_out_str(stderr, 16, signature.s);
1017       fprintf(stderr, "\n");
1018     }
1019
1020   if (expected)
1021     ASSERT (mpz_cmp (signature.r, expected->r) == 0
1022             && mpz_cmp (signature.s, expected->s) == 0);
1023   
1024   /* Try bad data */
1025   ASSERT (!DSA_VERIFY(pub, sha1,
1026                       "The magick words are squeamish ossifrage",
1027                       &signature));
1028
1029   /* Try correct data */
1030   ASSERT (DSA_VERIFY(pub, sha1,
1031                      "The magic words are squeamish ossifrage",
1032                      &signature));
1033
1034   /* Try bad signature */
1035   mpz_combit(signature.r, 17);
1036   ASSERT (!DSA_VERIFY(pub, sha1,
1037                       "The magic words are squeamish ossifrage",
1038                       &signature));
1039
1040   dsa_signature_clear(&signature);
1041 }
1042
1043 void
1044 test_dsa256(const struct dsa_public_key *pub,
1045             const struct dsa_private_key *key,
1046             const struct dsa_signature *expected)
1047 {
1048   struct sha256_ctx sha256;
1049   struct dsa_signature signature;
1050   struct knuth_lfib_ctx lfib;
1051   
1052   sha256_init(&sha256);
1053   dsa_signature_init(&signature);
1054   knuth_lfib_init(&lfib, 1111);
1055   
1056   sha256_update(&sha256, LDATA("The magic words are squeamish ossifrage"));
1057   ASSERT (dsa_sha256_sign(pub, key,
1058                         &lfib, (nettle_random_func *) knuth_lfib_random,
1059                         &sha256, &signature));
1060   
1061   if (verbose)
1062     {
1063       fprintf(stderr, "dsa256 signature: ");
1064       mpz_out_str(stderr, 16, signature.r);
1065       fprintf(stderr, ", ");
1066       mpz_out_str(stderr, 16, signature.s);
1067       fprintf(stderr, "\n");
1068     }
1069
1070   if (expected)
1071     ASSERT (mpz_cmp (signature.r, expected->r) == 0
1072             && mpz_cmp (signature.s, expected->s) == 0);
1073   
1074   /* Try bad data */
1075   ASSERT (!DSA_VERIFY(pub, sha256,
1076                       "The magick words are squeamish ossifrage",
1077                       &signature));
1078
1079   /* Try correct data */
1080   ASSERT (DSA_VERIFY(pub, sha256,
1081                      "The magic words are squeamish ossifrage",
1082                      &signature));
1083
1084   /* Try bad signature */
1085   mpz_combit(signature.r, 17);
1086   ASSERT (!DSA_VERIFY(pub, sha256,
1087                       "The magic words are squeamish ossifrage",
1088                       &signature));
1089
1090   dsa_signature_clear(&signature);
1091 }
1092
1093 #if 0
1094 void
1095 test_dsa_sign(const struct dsa_public_key *pub,
1096               const struct dsa_private_key *key,
1097               const struct nettle_hash *hash,
1098               const struct dsa_signature *expected)
1099 {
1100   void *ctx = xalloc (hash->context_size);
1101   uint8_t *digest = xalloc (hash->digest_size);
1102   uint8_t *bad_digest = xalloc (hash->digest_size);
1103   struct dsa_signature signature;
1104   struct knuth_lfib_ctx lfib;
1105   
1106   dsa_signature_init(&signature);
1107   knuth_lfib_init(&lfib, 1111);
1108
1109   hash->init(ctx);
1110   
1111   hash->update(ctx, LDATA("The magic words are squeamish ossifrage"));
1112   hash->digest(ctx, hash->digest_size, digest);
1113   ASSERT (dsa_sign(pub, key,
1114                    &lfib, (nettle_random_func *) knuth_lfib_random,
1115                    hash->digest_size, digest, &signature));
1116   
1117   if (verbose)
1118     {
1119       fprintf(stderr, "dsa-%s signature: ", hash->name);
1120       mpz_out_str(stderr, 16, signature.r);
1121       fprintf(stderr, ", ");
1122       mpz_out_str(stderr, 16, signature.s);
1123       fprintf(stderr, "\n");
1124     }
1125
1126   if (expected)
1127     ASSERT (mpz_cmp (signature.r, expected->r) == 0
1128             && mpz_cmp (signature.s, expected->s) == 0);
1129   
1130   /* Try correct data */
1131   ASSERT (dsa_verify(pub, hash->digest_size, digest,
1132                      &signature));
1133   /* Try bad data */
1134   hash->update(ctx, LDATA("The magick words are squeamish ossifrage"));
1135   hash->digest(ctx, hash->digest_size, bad_digest);
1136   
1137   ASSERT (!dsa_verify(pub, hash->digest_size, bad_digest,
1138                       &signature));
1139
1140   /* Try bad signature */
1141   mpz_combit(signature.r, 17);
1142   ASSERT (!dsa_verify(pub, hash->digest_size, digest,
1143                       &signature));
1144
1145   free (ctx);
1146   free (digest);
1147   free (bad_digest);
1148   dsa_signature_clear(&signature);
1149 }
1150 #endif
1151
1152 void
1153 test_dsa_verify(const struct dsa_params *params,
1154                 const mpz_t pub,
1155                 const struct nettle_hash *hash,
1156                 struct tstring *msg,
1157                 const struct dsa_signature *ref)
1158 {
1159   void *ctx = xalloc (hash->context_size);
1160   uint8_t *digest = xalloc (hash->digest_size);
1161   struct dsa_signature signature;
1162
1163   dsa_signature_init (&signature);
1164
1165   hash->init(ctx);
1166   
1167   hash->update (ctx, msg->length, msg->data);
1168   hash->digest (ctx, hash->digest_size, digest);
1169
1170   mpz_set (signature.r, ref->r);
1171   mpz_set (signature.s, ref->s);
1172
1173   ASSERT (dsa_verify (params, pub,
1174                        hash->digest_size, digest,
1175                        &signature));
1176
1177   /* Try bad signature */
1178   mpz_combit(signature.r, 17);
1179   ASSERT (!dsa_verify (params, pub,
1180                        hash->digest_size, digest,
1181                        &signature));
1182   
1183   /* Try bad data */
1184   digest[hash->digest_size / 2-1] ^= 8;
1185   ASSERT (!dsa_verify (params, pub,
1186                        hash->digest_size, digest,
1187                        ref));
1188
1189   free (ctx);
1190   free (digest);
1191   dsa_signature_clear(&signature);  
1192 }
1193
1194 void
1195 test_dsa_key(const struct dsa_params *params,
1196              const mpz_t pub,
1197              const mpz_t key,
1198              unsigned q_size)
1199 {
1200   mpz_t t;
1201
1202   mpz_init(t);
1203
1204   ASSERT(mpz_sizeinbase(params->q, 2) == q_size);
1205   ASSERT(mpz_sizeinbase(params->p, 2) >= DSA_SHA1_MIN_P_BITS);
1206   
1207   ASSERT(mpz_probab_prime_p(params->p, 10));
1208
1209   ASSERT(mpz_probab_prime_p(params->q, 10));
1210
1211   mpz_fdiv_r(t, params->p, params->q);
1212
1213   ASSERT(0 == mpz_cmp_ui(t, 1));
1214
1215   ASSERT(mpz_cmp_ui(params->g, 1) > 0);
1216   
1217   mpz_powm(t, params->g, params->q, params->p);
1218   ASSERT(0 == mpz_cmp_ui(t, 1));
1219   
1220   mpz_powm(t, params->g, key, params->p);
1221   ASSERT(0 == mpz_cmp(t, pub));
1222
1223   mpz_clear(t);
1224 }
1225
1226 const struct ecc_curve * const ecc_curves[] = {
1227   &nettle_secp_192r1,
1228   &nettle_secp_224r1,
1229   &nettle_secp_256r1,
1230   &nettle_secp_384r1,
1231   &nettle_secp_521r1,
1232   &_nettle_curve25519,
1233   NULL
1234 };
1235
1236 static int
1237 test_mpn (const char *ref, const mp_limb_t *xp, mp_size_t n)
1238 {
1239   mpz_t r;
1240   int res;
1241
1242   mpz_init_set_str (r, ref, 16);
1243   while (n > 0 && xp[n-1] == 0)
1244     n--;
1245   
1246   res = (mpz_limbs_cmp (r, xp, n) == 0);
1247   mpz_clear (r);
1248   return res;
1249 }
1250
1251 void
1252 write_mpn (FILE *f, int base, const mp_limb_t *xp, mp_size_t n)
1253 {
1254   mpz_t t;
1255   mpz_out_str (f, base, mpz_roinit_n (t,xp, n));
1256 }
1257
1258 void
1259 test_ecc_point (const struct ecc_curve *ecc,
1260                 const struct ecc_ref_point *ref,
1261                 const mp_limb_t *p)
1262 {
1263   if (! (test_mpn (ref->x, p, ecc->p.size)
1264          && test_mpn (ref->y, p + ecc->p.size, ecc->p.size) ))
1265     {
1266       fprintf (stderr, "Incorrect point!\n"
1267                "got: x = ");
1268       write_mpn (stderr, 16, p, ecc->p.size);
1269       fprintf (stderr, "\n"
1270                "     y = ");
1271       write_mpn (stderr, 16, p + ecc->p.size, ecc->p.size);
1272       fprintf (stderr, "\n"
1273                "ref: x = %s\n"
1274                "     y = %s\n",
1275                ref->x, ref->y);
1276       abort();
1277     }
1278 }
1279
1280 void
1281 test_ecc_mul_a (unsigned curve, unsigned n, const mp_limb_t *p)
1282 {
1283   /* For each curve, the points 2 g, 3 g and 4 g */
1284   static const struct ecc_ref_point ref[6][3] = {
1285     { { "dafebf5828783f2ad35534631588a3f629a70fb16982a888",
1286         "dd6bda0d993da0fa46b27bbc141b868f59331afa5c7e93ab" },
1287       { "76e32a2557599e6edcd283201fb2b9aadfd0d359cbb263da",
1288         "782c37e372ba4520aa62e0fed121d49ef3b543660cfd05fd" },
1289       { "35433907297cc378b0015703374729d7a4fe46647084e4ba",
1290         "a2649984f2135c301ea3acb0776cd4f125389b311db3be32" }
1291     },
1292     { { "706a46dc76dcb76798e60e6d89474788d16dc18032d268fd1a704fa6",
1293         "1c2b76a7bc25e7702a704fa986892849fca629487acf3709d2e4e8bb" },
1294       { "df1b1d66a551d0d31eff822558b9d2cc75c2180279fe0d08fd896d04",
1295         "a3f7f03cadd0be444c0aa56830130ddf77d317344e1af3591981a925" },
1296       { "ae99feebb5d26945b54892092a8aee02912930fa41cd114e40447301",
1297         "482580a0ec5bc47e88bc8c378632cd196cb3fa058a7114eb03054c9" },
1298     },
1299     { { "7cf27b188d034f7e8a52380304b51ac3c08969e277f21b35a60b48fc47669978",
1300         "7775510db8ed040293d9ac69f7430dbba7dade63ce982299e04b79d227873d1" },
1301       { "5ecbe4d1a6330a44c8f7ef951d4bf165e6c6b721efada985fb41661bc6e7fd6c",
1302         "8734640c4998ff7e374b06ce1a64a2ecd82ab036384fb83d9a79b127a27d5032" },
1303       { "e2534a3532d08fbba02dde659ee62bd0031fe2db785596ef509302446b030852",
1304         "e0f1575a4c633cc719dfee5fda862d764efc96c3f30ee0055c42c23f184ed8c6" },
1305     },
1306     { { "8d999057ba3d2d969260045c55b97f089025959a6f434d651d207d19fb96e9e"
1307         "4fe0e86ebe0e64f85b96a9c75295df61",
1308         "8e80f1fa5b1b3cedb7bfe8dffd6dba74b275d875bc6cc43e904e505f256ab425"
1309         "5ffd43e94d39e22d61501e700a940e80" },
1310       { "77a41d4606ffa1464793c7e5fdc7d98cb9d3910202dcd06bea4f240d3566da6"
1311         "b408bbae5026580d02d7e5c70500c831",
1312         "c995f7ca0b0c42837d0bbe9602a9fc998520b41c85115aa5f7684c0edc111eac"
1313         "c24abd6be4b5d298b65f28600a2f1df1" },
1314       { "138251cd52ac9298c1c8aad977321deb97e709bd0b4ca0aca55dc8ad51dcfc9d"
1315         "1589a1597e3a5120e1efd631c63e1835",
1316         "cacae29869a62e1631e8a28181ab56616dc45d918abc09f3ab0e63cf792aa4dc"
1317         "ed7387be37bba569549f1c02b270ed67" },
1318     },
1319     { { "43"
1320         "3c219024277e7e682fcb288148c282747403279b1ccc06352c6e5505d769be97"
1321         "b3b204da6ef55507aa104a3a35c5af41cf2fa364d60fd967f43e3933ba6d783d",
1322         "f4"
1323         "bb8cc7f86db26700a7f3eceeeed3f0b5c6b5107c4da97740ab21a29906c42dbb"
1324         "b3e377de9f251f6b93937fa99a3248f4eafcbe95edc0f4f71be356d661f41b02"
1325       },
1326       { "1a7"
1327         "3d352443de29195dd91d6a64b5959479b52a6e5b123d9ab9e5ad7a112d7a8dd1"
1328         "ad3f164a3a4832051da6bd16b59fe21baeb490862c32ea05a5919d2ede37ad7d",
1329         "13e"
1330         "9b03b97dfa62ddd9979f86c6cab814f2f1557fa82a9d0317d2f8ab1fa355ceec"
1331         "2e2dd4cf8dc575b02d5aced1dec3c70cf105c9bc93a590425f588ca1ee86c0e5" },
1332       { "35"
1333         "b5df64ae2ac204c354b483487c9070cdc61c891c5ff39afc06c5d55541d3ceac"
1334         "8659e24afe3d0750e8b88e9f078af066a1d5025b08e5a5e2fbc87412871902f3",
1335         "82"
1336         "096f84261279d2b673e0178eb0b4abb65521aef6e6e32e1b5ae63fe2f19907f2"
1337         "79f283e54ba385405224f750a95b85eebb7faef04699d1d9e21f47fc346e4d0d" },
1338     },
1339     { { "36ab384c9f5a046c3d043b7d1833e7ac080d8e4515d7a45f83c5a14e2843ce0e",
1340         "2260cdf3092329c21da25ee8c9a21f5697390f51643851560e5f46ae6af8a3c9" },
1341       { "67ae9c4a22928f491ff4ae743edac83a6343981981624886ac62485fd3f8e25c",
1342         "1267b1d177ee69aba126a18e60269ef79f16ec176724030402c3684878f5b4d4" },
1343       { "203da8db56cff1468325d4b87a3520f91a739ec193ce1547493aa657c4c9f870",
1344         "47d0e827cb1595e1470eb88580d5716c4cf22832ea2f0ff0df38ab61ca32112f" },
1345     }
1346   };
1347   assert (curve < 6);
1348   assert (n <= 4);
1349   if (n == 0)
1350     {
1351       /* Makes sense for curve25519 only */
1352       const struct ecc_curve *ecc = ecc_curves[curve];
1353       assert (ecc->p.bit_size == 255);
1354       if (!mpn_zero_p (p, ecc->p.size)
1355           || mpn_cmp (p + ecc->p.size, ecc->unit, ecc->p.size) != 0)
1356         {
1357           fprintf (stderr, "Incorrect point (expected (0, 1))!\n"
1358                    "got: x = ");
1359           write_mpn (stderr, 16, p, ecc->p.size);
1360           fprintf (stderr, "\n"
1361                    "     y = ");
1362           write_mpn (stderr, 16, p + ecc->p.size, ecc->p.size);
1363           fprintf (stderr, "\n");
1364           abort();
1365         }
1366     }
1367   else if (n == 1)
1368     {
1369       const struct ecc_curve *ecc = ecc_curves[curve];
1370       if (mpn_cmp (p, ecc->g, 2*ecc->p.size) != 0)
1371         {
1372           fprintf (stderr, "Incorrect point (expected g)!\n"
1373                    "got: x = ");
1374           write_mpn (stderr, 16, p, ecc->p.size);
1375           fprintf (stderr, "\n"
1376                    "     y = ");
1377           write_mpn (stderr, 16, p + ecc->p.size, ecc->p.size);
1378           fprintf (stderr, "\n"
1379                    "ref: x = ");
1380           write_mpn (stderr, 16, ecc->g, ecc->p.size);
1381           fprintf (stderr, "\n"
1382                    "     y = ");
1383           write_mpn (stderr, 16, ecc->g + ecc->p.size, ecc->p.size);
1384           fprintf (stderr, "\n");
1385           abort();
1386         }
1387     }
1388   else
1389     test_ecc_point (ecc_curves[curve], &ref[curve][n-2], p);
1390 }
1391
1392 void
1393 test_ecc_mul_h (unsigned curve, unsigned n, const mp_limb_t *p)
1394 {
1395   const struct ecc_curve *ecc = ecc_curves[curve];
1396   mp_limb_t *np = xalloc_limbs (ecc_size_a (ecc));
1397   mp_limb_t *scratch = xalloc_limbs (ecc->h_to_a_itch);
1398   ecc->h_to_a (ecc, 0, np, p, scratch);
1399
1400   test_ecc_mul_a (curve, n, np);
1401
1402   free (np);
1403   free (scratch);
1404 }
1405
1406 #endif /* WITH_HOGWEED */
1407