testutils.c 50.9 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
/* testutils.c */
Niels Möller's avatar
Niels Möller committed
2 3 4

#include "testutils.h"

5
#include "base16.h"
6
#include "cbc.h"
7
#include "cfb.h"
8
#include "ctr.h"
Niels Möller's avatar
Niels Möller committed
9
#include "knuth-lfib.h"
10
#include "macros.h"
11
#include "nettle-internal.h"
12

Niels Möller's avatar
Niels Möller committed
13
#include <assert.h>
Niels Möller's avatar
Niels Möller committed
14 15
#include <ctype.h>

16 17 18 19 20 21 22 23 24 25 26
void
die(const char *format, ...)
{
  va_list args;
  va_start(args, format);
  vfprintf(stderr, format, args);
  va_end(args);

  abort ();
}

27 28 29 30
void *
xalloc(size_t size)
{
  void *p = malloc(size);
31
  if (size && !p)
32 33 34 35 36 37 38 39
    {
      fprintf(stderr, "Virtual memory exhausted.\n");
      abort();
    }

  return p;
}

40 41 42
static struct tstring *tstring_first = NULL;

struct tstring *
Niels Möller's avatar
Niels Möller committed
43
tstring_alloc (size_t length)
44
{
Niels Möller's avatar
Niels Möller committed
45
  struct tstring *s = xalloc(sizeof(struct tstring) + length);
46 47
  s->length = length;
  s->next = tstring_first;
Niels Möller's avatar
Niels Möller committed
48 49
  /* NUL-terminate, for convenience. */
  s->data[length] = '\0';
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
  tstring_first = s;
  return s;
}

void
tstring_clear(void)
{
  while (tstring_first)
    {
      struct tstring *s = tstring_first;
      tstring_first = s->next;
      free(s);
    }
}

struct tstring *
66
tstring_data(size_t length, const uint8_t *data)
67 68 69 70 71 72 73 74
{
  struct tstring *s = tstring_alloc (length);
  memcpy (s->data, data, length);
  return s;
}

struct tstring *
tstring_hex(const char *hex)
Niels Möller's avatar
Niels Möller committed
75
{
76
  struct base16_decode_ctx ctx;
77
  struct tstring *s;
78
  size_t length = strlen(hex);
Niels Möller's avatar
Niels Möller committed
79

80 81 82 83 84
  s = tstring_alloc(BASE16_DECODE_LENGTH (length));
  base16_decode_init (&ctx);
  ASSERT (base16_decode_update (&ctx, &s->length, s->data,
				length, hex));
  ASSERT (base16_decode_final (&ctx));
Niels Möller's avatar
Niels Möller committed
85

86 87 88 89 90 91 92
  return s;
}

void
tstring_print_hex(const struct tstring *s)
{
  print_hex (s->length, s->data);
Niels Möller's avatar
Niels Möller committed
93
}
94

95
void
Niels Möller's avatar
Niels Möller committed
96
print_hex(size_t length, const uint8_t *data)
97
{
Niels Möller's avatar
Niels Möller committed
98
  size_t i;
99 100 101
  
  for (i = 0; i < length; i++)
    {
102 103 104 105 106 107 108 109 110 111 112
      switch (i % 16)
	{
	default:
	  break;
	case 0:
	  printf("\n");
	  break;
	case 8:
	  printf(" ");
	  break;
	}
113 114
      printf("%02x", data[i]);
    }
115
  printf("\n");
116 117
}

Niels Möller's avatar
Niels Möller committed
118 119 120 121 122
int verbose = 0;

int
main(int argc, char **argv)
{
123 124
  if (argc > 1)
    {
125
      if (argc == 2 && !strcmp(argv[1], "-v"))
Niels Möller's avatar
Niels Möller committed
126
	verbose = 1;
127 128 129 130 131 132 133
      else
	{
	  fprintf(stderr, "Invalid argument `%s', only accepted option is `-v'.\n",
		  argv[1]);
	  return 1;
	}
    }
Niels Möller's avatar
Niels Möller committed
134

135 136 137 138
  test_main();

  tstring_clear();
  return EXIT_SUCCESS;
Niels Möller's avatar
Niels Möller committed
139 140
}

141 142
void
test_cipher(const struct nettle_cipher *cipher,
143 144 145
	    const struct tstring *key,
	    const struct tstring *cleartext,
	    const struct tstring *ciphertext)
146
{
147
  void *ctx = xalloc(cipher->context_size);
148
  uint8_t *data = xalloc(cleartext->length);
Niels Möller's avatar
Niels Möller committed
149
  size_t length;
150 151
  ASSERT (cleartext->length == ciphertext->length);
  length = cleartext->length;
152

153 154
  ASSERT (key->length == cipher->key_size);
  cipher->set_encrypt_key(ctx, key->data);
155
  cipher->encrypt(ctx, length, data, cleartext->data);
156

157
  if (!MEMEQ(length, data, ciphertext->data))
158 159
    {
      fprintf(stderr, "Encrypt failed:\nInput:");
160
      tstring_print_hex(cleartext);
161 162 163
      fprintf(stderr, "\nOutput: ");
      print_hex(length, data);
      fprintf(stderr, "\nExpected:");
164
      tstring_print_hex(ciphertext);
165 166 167
      fprintf(stderr, "\n");
      FAIL();
    }
168
  cipher->set_decrypt_key(ctx, key->data);
169 170
  cipher->decrypt(ctx, length, data, data);

171
  if (!MEMEQ(length, data, cleartext->data))
172 173
    {
      fprintf(stderr, "Decrypt failed:\nInput:");
174
      tstring_print_hex(ciphertext);
175 176 177
      fprintf(stderr, "\nOutput: ");
      print_hex(length, data);
      fprintf(stderr, "\nExpected:");
178
      tstring_print_hex(cleartext);
179 180 181
      fprintf(stderr, "\n");
      FAIL();
    }
182 183 184

  free(ctx);
  free(data);
185 186 187 188
}

void
test_cipher_cbc(const struct nettle_cipher *cipher,
189 190 191 192
		const struct tstring *key,
		const struct tstring *cleartext,
		const struct tstring *ciphertext,
		const struct tstring *iiv)
193
{
194
  void *ctx = xalloc(cipher->context_size);
195
  uint8_t *data;
196
  uint8_t *iv = xalloc(cipher->block_size);
Niels Möller's avatar
Niels Möller committed
197
  size_t length;
198 199 200 201

  ASSERT (cleartext->length == ciphertext->length);
  length = cleartext->length;

202
  ASSERT (key->length == cipher->key_size);
203 204 205
  ASSERT (iiv->length == cipher->block_size);

  data = xalloc(length);  
206
  cipher->set_encrypt_key(ctx, key->data);
207
  memcpy(iv, iiv->data, cipher->block_size);
208 209 210

  cbc_encrypt(ctx, cipher->encrypt,
	      cipher->block_size, iv,
211
	      length, data, cleartext->data);
212

213
  if (!MEMEQ(length, data, ciphertext->data))
214 215
    {
      fprintf(stderr, "CBC encrypt failed:\nInput:");
216
      tstring_print_hex(cleartext);
217 218 219
      fprintf(stderr, "\nOutput: ");
      print_hex(length, data);
      fprintf(stderr, "\nExpected:");
220
      tstring_print_hex(ciphertext);
221 222 223
      fprintf(stderr, "\n");
      FAIL();
    }
224
  cipher->set_decrypt_key(ctx, key->data);
225
  memcpy(iv, iiv->data, cipher->block_size);
226 227 228 229 230

  cbc_decrypt(ctx, cipher->decrypt,
	      cipher->block_size, iv,
	      length, data, data);

231
  if (!MEMEQ(length, data, cleartext->data))
232 233
    {
      fprintf(stderr, "CBC decrypt failed:\nInput:");
234
      tstring_print_hex(ciphertext);
235 236 237
      fprintf(stderr, "\nOutput: ");
      print_hex(length, data);
      fprintf(stderr, "\nExpected:");
238
      tstring_print_hex(cleartext);
239 240 241
      fprintf(stderr, "\n");
      FAIL();
    }
242

243 244 245
  free(ctx);
  free(data);
  free(iv);
246 247
}

248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
void
test_cipher_cfb(const struct nettle_cipher *cipher,
		const struct tstring *key,
		const struct tstring *cleartext,
		const struct tstring *ciphertext,
		const struct tstring *iiv)
{
  void *ctx = xalloc(cipher->context_size);
  uint8_t *data, *data2;
  uint8_t *iv = xalloc(cipher->block_size);
  size_t length;

  ASSERT (cleartext->length == ciphertext->length);
  length = cleartext->length;

  ASSERT (key->length == cipher->key_size);
  ASSERT (iiv->length == cipher->block_size);

  data = xalloc(length);
  data2 = xalloc(length);

  cipher->set_encrypt_key(ctx, key->data);
  memcpy(iv, iiv->data, cipher->block_size);

  cfb_encrypt(ctx, cipher->encrypt,
	      cipher->block_size, iv,
	      length, data, cleartext->data);

  if (!MEMEQ(length, data, ciphertext->data))
    {
      fprintf(stderr, "CFB encrypt failed:\nInput:");
      tstring_print_hex(cleartext);
      fprintf(stderr, "\nOutput: ");
      print_hex(length, data);
      fprintf(stderr, "\nExpected:");
      tstring_print_hex(ciphertext);
      fprintf(stderr, "\n");
      FAIL();
    }
  cipher->set_encrypt_key(ctx, key->data);
  memcpy(iv, iiv->data, cipher->block_size);

  cfb_decrypt(ctx, cipher->encrypt,
	      cipher->block_size, iv,
	      length, data2, data);

  if (!MEMEQ(length, data2, cleartext->data))
    {
      fprintf(stderr, "CFB decrypt failed:\nInput:");
      tstring_print_hex(ciphertext);
      fprintf(stderr, "\nOutput: ");
      print_hex(length, data2);
      fprintf(stderr, "\nExpected:");
      tstring_print_hex(cleartext);
      fprintf(stderr, "\n");
      FAIL();
    }
  cipher->set_encrypt_key(ctx, key->data);
  memcpy(iv, iiv->data, cipher->block_size);
  memcpy(data, cleartext->data, length);

  cfb_encrypt(ctx, cipher->encrypt,
	      cipher->block_size, iv,
	      length, data, data);

  if (!MEMEQ(length, data, ciphertext->data))
    {
      fprintf(stderr, "CFB inplace encrypt failed:\nInput:");
      tstring_print_hex(cleartext);
      fprintf(stderr, "\nOutput: ");
      print_hex(length, data);
      fprintf(stderr, "\nExpected:");
      tstring_print_hex(ciphertext);
      fprintf(stderr, "\n");
      FAIL();
    }
  cipher->set_encrypt_key(ctx, key->data);
  memcpy(iv, iiv->data, cipher->block_size);

  cfb_decrypt(ctx, cipher->encrypt,
	      cipher->block_size, iv,
	      length, data, data);

  if (!MEMEQ(length, data, cleartext->data))
    {
      fprintf(stderr, "CFB inplace decrypt failed:\nInput:");
      tstring_print_hex(ciphertext);
      fprintf(stderr, "\nOutput: ");
      print_hex(length, data);
      fprintf(stderr, "\nExpected:");
      tstring_print_hex(cleartext);
      fprintf(stderr, "\n");
      FAIL();
    }

  /* Repeat all tests with incomplete last block */
  length -= 1;

  cipher->set_encrypt_key(ctx, key->data);
  memcpy(iv, iiv->data, cipher->block_size);

  cfb_encrypt(ctx, cipher->encrypt,
	      cipher->block_size, iv,
	      length, data, cleartext->data);

  if (!MEMEQ(length, data, ciphertext->data))
    {
      fprintf(stderr, "CFB encrypt failed:\nInput:");
      print_hex(length, cleartext->data);
      fprintf(stderr, "\nOutput: ");
      print_hex(length, data);
      fprintf(stderr, "\nExpected:");
      print_hex(length, ciphertext->data);
      fprintf(stderr, "\n");
      FAIL();
    }
  cipher->set_encrypt_key(ctx, key->data);
  memcpy(iv, iiv->data, cipher->block_size);

  cfb_decrypt(ctx, cipher->encrypt,
	      cipher->block_size, iv,
	      length, data2, data);

  if (!MEMEQ(length, data2, cleartext->data))
    {
      fprintf(stderr, "CFB decrypt failed:\nInput:");
      print_hex(length, ciphertext->data);
      fprintf(stderr, "\nOutput: ");
      print_hex(length, data2);
      fprintf(stderr, "\nExpected:");
      print_hex(length, cleartext->data);
      fprintf(stderr, "\n");
      FAIL();
    }
  cipher->set_encrypt_key(ctx, key->data);
  memcpy(iv, iiv->data, cipher->block_size);
  memcpy(data, cleartext->data, length);

  cfb_encrypt(ctx, cipher->encrypt,
	      cipher->block_size, iv,
	      length, data, data);

  if (!MEMEQ(length, data, ciphertext->data))
    {
      fprintf(stderr, "CFB inplace encrypt failed:\nInput:");
      print_hex(length, cleartext->data);
      fprintf(stderr, "\nOutput: ");
      print_hex(length, data);
      fprintf(stderr, "\nExpected:");
      print_hex(length, ciphertext->data);
      fprintf(stderr, "\n");
      FAIL();
    }
  cipher->set_encrypt_key(ctx, key->data);
  memcpy(iv, iiv->data, cipher->block_size);

  cfb_decrypt(ctx, cipher->encrypt,
	      cipher->block_size, iv,
	      length, data, data);

  if (!MEMEQ(length, data, cleartext->data))
    {
      fprintf(stderr, "CFB inplace decrypt failed:\nInput:");
      print_hex(length, ciphertext->data);
      fprintf(stderr, "\nOutput: ");
      print_hex(length, data);
      fprintf(stderr, "\nExpected:");
      print_hex(length, cleartext->data);
      fprintf(stderr, "\n");
      FAIL();
    }

  free(ctx);
  free(data);
  free(data2);
  free(iv);
}

426 427 428 429 430 431 432 433 434 435 436
void
test_cipher_cfb8(const struct nettle_cipher *cipher,
		 const struct tstring *key,
		 const struct tstring *cleartext,
		 const struct tstring *ciphertext,
		 const struct tstring *iiv)
{
  void *ctx = xalloc(cipher->context_size);
  uint8_t *data, *data2;
  uint8_t *iv = xalloc(cipher->block_size);
  size_t length;
437
  size_t block;
438 439 440 441 442 443 444

  ASSERT (cleartext->length == ciphertext->length);
  length = cleartext->length;

  ASSERT (key->length == cipher->key_size);
  ASSERT (iiv->length == cipher->block_size);

Niels Möller's avatar
Niels Möller committed
445 446
  data = xalloc(length + 1);
  data2 = xalloc(length + 1);
447

448 449 450
  for (block = 1; block <= length; block++)
    {
      size_t i;
451

452 453
      cipher->set_encrypt_key(ctx, key->data);
      memcpy(iv, iiv->data, cipher->block_size);
454

Niels Möller's avatar
Niels Möller committed
455
      memset(data, 0x17, length + 1);
456 457 458 459 460 461
      for (i = 0; i + block <= length; i += block)
	{
	  cfb8_encrypt(ctx, cipher->encrypt,
		       cipher->block_size, iv,
		       block, data + i, cleartext->data + i);
	}
Niels Möller's avatar
Niels Möller committed
462 463 464
      cfb8_encrypt(ctx, cipher->encrypt,
		   cipher->block_size, iv,
		   length - i, data + i, cleartext->data + i);
465

466 467 468
      if (!MEMEQ(length, data, ciphertext->data))
	{
	  fprintf(stderr, "CFB8 encrypt failed, block size %lu:\nInput:",
469
		  (unsigned long) block);
470 471 472 473 474 475 476 477
	  tstring_print_hex(cleartext);
	  fprintf(stderr, "\nOutput: ");
	  print_hex(length, data);
	  fprintf(stderr, "\nExpected:");
	  tstring_print_hex(ciphertext);
	  fprintf(stderr, "\n");
	  FAIL();
	}
Niels Möller's avatar
Niels Möller committed
478 479
      ASSERT (data[length] == 0x17);

480 481
      cipher->set_encrypt_key(ctx, key->data);
      memcpy(iv, iiv->data, cipher->block_size);
482

Niels Möller's avatar
Niels Möller committed
483
      memset(data2, 0x17, length + 1);
484 485 486 487 488 489
      for (i = 0; i + block <= length; i += block)
	{
	  cfb8_decrypt(ctx, cipher->encrypt,
		       cipher->block_size, iv,
		       block, data2 + i, data + i);
	}
Niels Möller's avatar
Niels Möller committed
490 491 492
      cfb8_decrypt(ctx, cipher->encrypt,
		   cipher->block_size, iv,
		   length - i, data2 + i, data + i);
493 494 495 496

      if (!MEMEQ(length, data2, cleartext->data))
	{
	  fprintf(stderr, "CFB8 decrypt failed, block size %lu:\nInput:",
497
		  (unsigned long) block);
498 499 500 501 502 503 504 505
	  tstring_print_hex(ciphertext);
	  fprintf(stderr, "\nOutput: ");
	  print_hex(length, data2);
	  fprintf(stderr, "\nExpected:");
	  tstring_print_hex(cleartext);
	  fprintf(stderr, "\n");
	  FAIL();
	}
Niels Möller's avatar
Niels Möller committed
506
      ASSERT (data[length] == 0x17);
507
    }
508

509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
  cipher->set_encrypt_key(ctx, key->data);
  memcpy(iv, iiv->data, cipher->block_size);
  memcpy(data, cleartext->data, length);

  cfb8_encrypt(ctx, cipher->encrypt,
	      cipher->block_size, iv,
	      length, data, data);

  if (!MEMEQ(length, data, ciphertext->data))
    {
      fprintf(stderr, "CFB8 inplace encrypt failed:\nInput:");
      tstring_print_hex(cleartext);
      fprintf(stderr, "\nOutput: ");
      print_hex(length, data);
      fprintf(stderr, "\nExpected:");
      tstring_print_hex(ciphertext);
      fprintf(stderr, "\n");
      FAIL();
    }
  cipher->set_encrypt_key(ctx, key->data);
  memcpy(iv, iiv->data, cipher->block_size);

  cfb8_decrypt(ctx, cipher->encrypt,
	      cipher->block_size, iv,
	      length, data, data);

  if (!MEMEQ(length, data, cleartext->data))
    {
      fprintf(stderr, "CFB8 inplace decrypt failed:\nInput:");
      tstring_print_hex(ciphertext);
      fprintf(stderr, "\nOutput: ");
      print_hex(length, data);
      fprintf(stderr, "\nExpected:");
      tstring_print_hex(cleartext);
      fprintf(stderr, "\n");
      FAIL();
    }

  /* Repeat all tests with incomplete last block */
  length -= 1;

  cipher->set_encrypt_key(ctx, key->data);
  memcpy(iv, iiv->data, cipher->block_size);

  cfb8_encrypt(ctx, cipher->encrypt,
	      cipher->block_size, iv,
	      length, data, cleartext->data);

  if (!MEMEQ(length, data, ciphertext->data))
    {
      fprintf(stderr, "CFB8 encrypt failed:\nInput:");
      print_hex(length, cleartext->data);
      fprintf(stderr, "\nOutput: ");
      print_hex(length, data);
      fprintf(stderr, "\nExpected:");
      print_hex(length, ciphertext->data);
      fprintf(stderr, "\n");
      FAIL();
    }
  cipher->set_encrypt_key(ctx, key->data);
  memcpy(iv, iiv->data, cipher->block_size);

  cfb8_decrypt(ctx, cipher->encrypt,
	      cipher->block_size, iv,
	      length, data2, data);

  if (!MEMEQ(length, data2, cleartext->data))
    {
      fprintf(stderr, "CFB8 decrypt failed:\nInput:");
      print_hex(length, ciphertext->data);
      fprintf(stderr, "\nOutput: ");
      print_hex(length, data2);
      fprintf(stderr, "\nExpected:");
      print_hex(length, cleartext->data);
      fprintf(stderr, "\n");
      FAIL();
    }
  cipher->set_encrypt_key(ctx, key->data);
  memcpy(iv, iiv->data, cipher->block_size);
  memcpy(data, cleartext->data, length);

  cfb8_encrypt(ctx, cipher->encrypt,
	      cipher->block_size, iv,
	      length, data, data);

  if (!MEMEQ(length, data, ciphertext->data))
    {
      fprintf(stderr, "CFB8 inplace encrypt failed:\nInput:");
      print_hex(length, cleartext->data);
      fprintf(stderr, "\nOutput: ");
      print_hex(length, data);
      fprintf(stderr, "\nExpected:");
      print_hex(length, ciphertext->data);
      fprintf(stderr, "\n");
      FAIL();
    }
  cipher->set_encrypt_key(ctx, key->data);
  memcpy(iv, iiv->data, cipher->block_size);

  cfb8_decrypt(ctx, cipher->encrypt,
	      cipher->block_size, iv,
	      length, data, data);

  if (!MEMEQ(length, data, cleartext->data))
    {
      fprintf(stderr, "CFB8 inplace decrypt failed:\nInput:");
      print_hex(length, ciphertext->data);
      fprintf(stderr, "\nOutput: ");
      print_hex(length, data);
      fprintf(stderr, "\nExpected:");
      print_hex(length, cleartext->data);
      fprintf(stderr, "\n");
      FAIL();
    }

  free(ctx);
  free(data);
  free(data2);
  free(iv);
}

630 631
void
test_cipher_ctr(const struct nettle_cipher *cipher,
632 633 634 635
		const struct tstring *key,
		const struct tstring *cleartext,
		const struct tstring *ciphertext,
		const struct tstring *ictr)
636 637
{
  void *ctx = xalloc(cipher->context_size);
638
  uint8_t *data;
639
  uint8_t *ctr = xalloc(cipher->block_size);
640
  uint8_t *octr = xalloc(cipher->block_size);
Niels Möller's avatar
Niels Möller committed
641 642
  size_t length, nblocks;
  unsigned low;
Niels Möller's avatar
Niels Möller committed
643
  size_t i;
644 645 646 647

  ASSERT (cleartext->length == ciphertext->length);
  length = cleartext->length;

648
  ASSERT (key->length == cipher->key_size);
649
  ASSERT (ictr->length == cipher->block_size);
650 651 652 653 654 655 656 657 658 659 660 661

  /* Compute expected counter value after the operation. */
  nblocks = (length + cipher->block_size - 1) / cipher->block_size;
  ASSERT (nblocks < 0x100);

  memcpy (octr, ictr->data, cipher->block_size - 1);
  low = ictr->data[cipher->block_size - 1] + nblocks;
  octr[cipher->block_size - 1] = low;

  if (low >= 0x100)
    INCREMENT (cipher->block_size - 1, octr);

662 663
  data = xalloc(length);  

664
  cipher->set_encrypt_key(ctx, key->data);
665

Niels Möller's avatar
Niels Möller committed
666
  for (i = 0; i <= length; i++)
667
    {
Niels Möller's avatar
Niels Möller committed
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
      memcpy(ctr, ictr->data, cipher->block_size);
      memset(data, 17, length);

      ctr_crypt(ctx, cipher->encrypt,
		cipher->block_size, ctr,
		i, data, cleartext->data);

      if (!MEMEQ(i, data, ciphertext->data)
	  || (i < length && data[i] != 17))
	{
	  fprintf(stderr, "CTR encrypt failed (length %d of %d):\nInput:",
		  (int) i, (int) length);
	  tstring_print_hex(cleartext);
	  fprintf(stderr, "\nOutput: ");
	  print_hex(length, data);
	  fprintf(stderr, "\nExpected:");
	  tstring_print_hex(ciphertext);
	  fprintf(stderr, "\n");
	  FAIL();
	}
688
    }
689

690 691
  ASSERT (MEMEQ (cipher->block_size, ctr, octr));

692
  memcpy(ctr, ictr->data, cipher->block_size);
693 694 695 696 697

  ctr_crypt(ctx, cipher->encrypt,
	    cipher->block_size, ctr,
	    length, data, data);

698
  if (!MEMEQ(length, data, cleartext->data))
699 700
    {
      fprintf(stderr, "CTR decrypt failed:\nInput:");
701
      tstring_print_hex(ciphertext);
702 703 704
      fprintf(stderr, "\nOutput: ");
      print_hex(length, data);
      fprintf(stderr, "\nExpected:");
705
      tstring_print_hex(cleartext);
706 707 708
      fprintf(stderr, "\n");
      FAIL();
    }
709

710 711
  ASSERT (MEMEQ (cipher->block_size, ctr, octr));

712 713
  free(ctx);
  free(data);
714
  free(octr);
715 716 717
  free(ctr);
}

718
#if 0
719 720
void
test_cipher_stream(const struct nettle_cipher *cipher,
721 722 723
		   const struct tstring *key,
		   const struct tstring *cleartext,
		   const struct tstring *ciphertext)
724
{
Niels Möller's avatar
Niels Möller committed
725
  size_t block;
726
  
727
  void *ctx = xalloc(cipher->context_size);
728
  uint8_t *data;
Niels Möller's avatar
Niels Möller committed
729
  size_t length;
730 731 732 733 734 735

  ASSERT (cleartext->length == ciphertext->length);
  length = cleartext->length;

  data = xalloc(length + 1);

736 737
  for (block = 1; block <= length; block++)
    {
Niels Möller's avatar
Niels Möller committed
738
      size_t i;
739 740

      memset(data, 0x17, length + 1);
741
      cipher->set_encrypt_key(ctx, key->length, key->data);
742 743 744

      for (i = 0; i + block < length; i += block)
	{
745 746
	  cipher->encrypt(ctx, block, data + i, cleartext->data + i);
	  ASSERT (data[i + block] == 0x17);
747
	}
748

749 750
      cipher->encrypt(ctx, length - i, data + i, cleartext->data + i);
      ASSERT (data[length] == 0x17);
751
      
752
      if (!MEMEQ(length, data, ciphertext->data))
753
	{
Niels Möller's avatar
Niels Möller committed
754 755
	  fprintf(stderr, "Encrypt failed, block size %lu\nInput:",
		  (unsigned long) block);
756
	  tstring_print_hex(cleartext);
757 758 759
	  fprintf(stderr, "\nOutput: ");
	  print_hex(length, data);
	  fprintf(stderr, "\nExpected:");
760
	  tstring_print_hex(ciphertext);
761 762 763
	  fprintf(stderr, "\n");
	  FAIL();	    
	}
764 765
    }
  
766
  cipher->set_decrypt_key(ctx, key->length, key->data);
767 768
  cipher->decrypt(ctx, length, data, data);

769
  ASSERT (data[length] == 0x17);
770

771
  if (!MEMEQ(length, data, cleartext->data))
772 773
    {
      fprintf(stderr, "Decrypt failed\nInput:");
774
      tstring_print_hex(ciphertext);
775 776 777
      fprintf(stderr, "\nOutput: ");
      print_hex(length, data);
      fprintf(stderr, "\nExpected:");
778
      tstring_print_hex(cleartext);
779 780 781
      fprintf(stderr, "\n");
      FAIL();	    
    }
782 783 784

  free(ctx);
  free(data);
785
}
786
#endif
787

788 789
void
test_aead(const struct nettle_aead *aead,
790
	  nettle_hash_update_func *set_nonce,
791 792 793 794
	  const struct tstring *key,
	  const struct tstring *authtext,
	  const struct tstring *cleartext,
	  const struct tstring *ciphertext,
795
	  const struct tstring *nonce,
796
	  const struct tstring *digest)
797 798
{
  void *ctx = xalloc(aead->context_size);
799
  uint8_t *data;
800
  uint8_t *buffer = xalloc(aead->digest_size);
Niels Möller's avatar
Niels Möller committed
801
  size_t length;
802 803 804

  ASSERT (cleartext->length == ciphertext->length);
  length = cleartext->length;
805

806
  ASSERT (key->length == aead->key_size);
807
  ASSERT (digest->length <= aead->digest_size);
808 809 810

  data = xalloc(length);
  
811
  /* encryption */
812
  memset(buffer, 0, aead->digest_size);
813
  aead->set_encrypt_key(ctx, key->data);
814

815 816 817 818 819 820 821
  if (nonce->length != aead->nonce_size)
    {
      ASSERT (set_nonce);
      set_nonce (ctx, nonce->length, nonce->data);
    }
  else
    aead->set_nonce(ctx, nonce->data);
822

823 824
  if (authtext->length)
    aead->update(ctx, authtext->length, authtext->data);
825 826
    
  if (length)
827
    aead->encrypt(ctx, length, data, cleartext->data);
828

829
  aead->digest(ctx, digest->length, buffer);
830

831
  ASSERT(MEMEQ(length, data, ciphertext->data));
832
  ASSERT(MEMEQ(digest->length, buffer, digest->data));
833 834

  /* decryption */
835
  memset(buffer, 0, aead->digest_size);
836 837 838 839 840 841 842 843 844 845

  aead->set_decrypt_key(ctx, key->data);

  if (nonce->length != aead->nonce_size)
    {
      ASSERT (set_nonce);
      set_nonce (ctx, nonce->length, nonce->data);
    }
  else
    aead->set_nonce(ctx, nonce->data);
846

847 848
  if (authtext->length)
    aead->update(ctx, authtext->length, authtext->data);
849 850 851 852
    
  if (length)
    aead->decrypt(ctx, length, data, data);

853
  aead->digest(ctx, digest->length, buffer);
854

855
  ASSERT(MEMEQ(length, data, cleartext->data));
856
  ASSERT(MEMEQ(digest->length, buffer, digest->data));
857 858 859 860 861 862

  free(ctx);
  free(data);
  free(buffer);
}

863 864
void
test_hash(const struct nettle_hash *hash,
865 866
	  const struct tstring *msg,
	  const struct tstring *digest)
867
{
868
  void *ctx = xalloc(hash->context_size);
869
  uint8_t *buffer = xalloc(digest->length);
870 871
  uint8_t *input;
  unsigned offset;
872

873 874 875
  /* Here, hash->digest_size zero means arbitrary size. */
  if (hash->digest_size)
    ASSERT (digest->length == hash->digest_size);
876

877
  hash->init(ctx);
878
  hash->update(ctx, msg->length, msg->data);
879
  hash->digest(ctx, digest->length, buffer);
880

881
  if (MEMEQ(digest->length, digest->data, buffer) == 0)
882 883
    {
      fprintf(stdout, "\nGot:\n");
884
      print_hex(digest->length, buffer);
885
      fprintf(stdout, "\nExpected:\n");
886
      print_hex(digest->length, digest->data);
887 888
      abort();
    }
889

890
  memset(buffer, 0, digest->length);
891

892
  hash->update(ctx, msg->length, msg->data);
893 894
  ASSERT(digest->length > 0);
  hash->digest(ctx, digest->length - 1, buffer);
895

896
  ASSERT(MEMEQ(digest->length - 1, digest->data, buffer));
897

898
  ASSERT(buffer[digest->length - 1] == 0);
899

900 901 902 903 904 905
  input = xalloc (msg->length + 16);
  for (offset = 0; offset < 16; offset++)
    {
      memset (input, 0, msg->length + 16);
      memcpy (input + offset, msg->data, msg->length);
      hash->update (ctx, msg->length, input + offset);
906 907
      hash->digest (ctx, digest->length, buffer);
      if (MEMEQ(digest->length, digest->data, buffer) == 0)
908 909
	{
	  fprintf(stdout, "hash input address: %p\nGot:\n", input + offset);
910
	  print_hex(digest->length, buffer);
911
	  fprintf(stdout, "\nExpected:\n");
912
	  print_hex(digest->length, digest->data);
913 914 915
	  abort();
	}      
    }
916 917
  free(ctx);
  free(buffer);
918
  free(input);
919
}
920

921 922
void
test_hash_large(const struct nettle_hash *hash,
Niels Möller's avatar
Niels Möller committed
923
		size_t count, size_t length,
924
		uint8_t c,
925
		const struct tstring *digest)
926 927 928 929
{
  void *ctx = xalloc(hash->context_size);
  uint8_t *buffer = xalloc(hash->digest_size);
  uint8_t *data = xalloc(length);
Niels Möller's avatar
Niels Möller committed
930
  size_t i;
931

932 933
  ASSERT (digest->length == hash->digest_size);

934 935 936 937
  memset(data, c, length);

  hash->init(ctx);
  for (i = 0; i < count; i++)
938 939 940 941 942 943 944
    {
      hash->update(ctx, length, data);
      if (i % (count / 50) == 0)
	fprintf (stderr, ".");
    }
  fprintf (stderr, "\n");
  
945 946 947 948
  hash->digest(ctx, hash->digest_size, buffer);

  print_hex(hash->digest_size, buffer);

949
  ASSERT (MEMEQ(hash->digest_size, digest->data, buffer));
950 951 952 953 954 955

  free(ctx);
  free(buffer);
  free(data);
}

956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
void
test_mac(const struct nettle_mac *mac,
	 const struct tstring *key,
	 const struct tstring *msg,
	 const struct tstring *digest)
{
  void *ctx = xalloc(mac->context_size);
  uint8_t *hash = xalloc(mac->digest_size);
  unsigned i;


  ASSERT (digest->length == mac->digest_size);
  ASSERT (key->length == mac->key_size);
  mac->set_key (ctx, key->data);
  mac->update (ctx, msg->length, msg->data);
  mac->digest (ctx, digest->length, hash);

  if (!MEMEQ (digest->length, digest->data, hash))
    {
      fprintf (stderr, "test_mac failed, msg: ");
      print_hex (msg->length, msg->data);
      fprintf(stderr, "Output:");
      print_hex (mac->digest_size, hash);
      fprintf(stderr, "Expected:");
      tstring_print_hex(digest);
      fprintf(stderr, "\n");
      FAIL();
    }

  /* attempt to re-use the structure */
  mac->update (ctx, msg->length, msg->data);
  mac->digest (ctx, digest->length, hash);
  if (!MEMEQ (digest->length, digest->data, hash))
    {
      fprintf (stderr, "test_mac: failed on re-use, msg: ");
      print_hex (msg->length, msg->data);
      fprintf(stderr, "Output:");
      print_hex (mac->digest_size, hash);
      fprintf(stderr, "Expected:");
      tstring_print_hex(digest);
      fprintf(stderr, "\n");
      FAIL();
    }

  /* attempt byte-by-byte hashing */
  mac->set_key (ctx, key->data);
  for (i=0;i<msg->length;i++)
    mac->update (ctx, 1, msg->data+i);
  mac->digest (ctx, digest->length, hash);
  if (!MEMEQ (digest->length, digest->data, hash))
    {
      fprintf (stderr, "cmac_hash failed on byte-by-byte, msg: ");
      print_hex (msg->length, msg->data);
      fprintf(stderr, "Output:");
      print_hex (16, hash);
      fprintf(stderr, "Expected:");
      tstring_print_hex(digest);
      fprintf(stderr, "\n");
      FAIL();
    }
  free (ctx);
  free (hash);
}

Niels Möller's avatar
Niels Möller committed
1020 1021
void
test_armor(const struct nettle_armor *armor,
Niels Möller's avatar
Niels Möller committed
1022
           size_t data_length,
Niels Möller's avatar
Niels Möller committed
1023
           const uint8_t *data,
1024
           const char *ascii)
Niels Möller's avatar
Niels Möller committed
1025
{
1026
  size_t ascii_length = strlen(ascii);
1027
  char *buffer = xalloc(1 + ascii_length);
1028 1029 1030
  uint8_t *check = xalloc(1 + armor->decode_length(ascii_length));
  void *encode = xalloc(armor->encode_context_size);
  void *decode = xalloc(armor->decode_context_size);
1031
  size_t done;
1032 1033 1034 1035

  ASSERT(ascii_length
	 <= (armor->encode_length(data_length) + armor->encode_final_length));
  ASSERT(data_length <= armor->decode_length(ascii_length));
1036 1037
  
  memset(buffer, 0x33, 1 + ascii_length);
1038
  memset(check, 0x55, 1 + data_length);
Niels Möller's avatar
Niels Möller committed
1039

1040 1041 1042 1043 1044 1045
  armor->encode_init(encode);
  
  done = armor->encode_update(encode, buffer, data_length, data);
  done += armor->encode_final(encode, buffer + done);
  ASSERT(done == ascii_length);

1046 1047
  ASSERT (MEMEQ(ascii_length, buffer, ascii));
  ASSERT (0x33 == buffer[strlen(ascii)]);
Niels Möller's avatar
Niels Möller committed
1048

1049
  armor->decode_init(decode);
1050
  done = armor->decode_length(ascii_length);
Niels Möller's avatar
Niels Möller committed
1051

1052 1053 1054
  ASSERT(armor->decode_update(decode, &done, check, ascii_length, buffer));
  ASSERT(done == data_length);
  ASSERT(armor->decode_final(decode));
1055
  
1056 1057
  ASSERT (MEMEQ(data_length, check, data));
  ASSERT (0x55 == check[data_length]);
1058 1059 1060 1061 1062

  free(buffer);
  free(check);
  free(encode);
  free(decode);
Niels Möller's avatar
Niels Möller committed
1063 1064
}

1065 1066
#if WITH_HOGWEED

1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
#ifndef mpn_zero_p
int
mpn_zero_p (mp_srcptr ap, mp_size_t n)
{
  while (--n >= 0)
    {
      if (ap[n] != 0)
	return 0;
    }
  return 1;
}
#endif

1080 1081 1082 1083 1084 1085 1086
void
mpn_out_str (FILE *f, int base, const mp_limb_t *xp, mp_size_t xn)
{
  mpz_t x;
  mpz_out_str (f, base, mpz_roinit_n (x, xp, xn));
}

1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
#if NETTLE_USE_MINI_GMP
void
gmp_randinit_default (struct knuth_lfib_ctx *ctx)
{
  knuth_lfib_init (ctx, 17);
}
void
mpz_urandomb (mpz_t r, struct knuth_lfib_ctx *ctx, mp_bitcnt_t bits)
{
  size_t bytes = (bits+7)/8;
  uint8_t *buf = xalloc (bytes);

  knuth_lfib_random (ctx, bytes, buf);
1100
  buf[0] &= 0xff >> (8*bytes - bits);
1101 1102 1103 1104 1105
  nettle_mpz_set_str_256_u (r, bytes, buf);
  free (buf);
}
#endif /* NETTLE_USE_MINI_GMP */

Niels Möller's avatar
Niels Möller committed
1106 1107 1108 1109 1110 1111
mp_limb_t *
xalloc_limbs (mp_size_t n)
{
  return xalloc (n * sizeof (mp_limb_t));
}

1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
/* Expects local variables pub, key, rstate, digest, signature */
#define SIGN(hash, msg, expected) do { \
  hash##_update(&hash, LDATA(msg));					\
  ASSERT(rsa_##hash##_sign(key, &hash, signature));			\
  if (verbose)								\
    {									\
      fprintf(stderr, "rsa-%s signature: ", #hash);			\
      mpz_out_str(stderr, 16, signature);				\
      fprintf(stderr, "\n");						\
    }									\
  ASSERT(mpz_cmp (signature, expected) == 0);				\
									\
  hash##_update(&hash, LDATA(msg));					\
  ASSERT(rsa_##hash##_sign_tr(pub, key, &rstate,			\
			      (nettle_random_func *) knuth_lfib_random,	\
			      &hash, signature));			\
  ASSERT(mpz_cmp (signature, expected) == 0);				\
									\
  hash##_update(&hash, LDATA(msg));					\
  hash##_digest(&hash, sizeof(digest), digest);				\
  ASSERT(rsa_##hash##_sign_digest(key, digest, signature));		\
  ASSERT(mpz_cmp (signature, expected) == 0);				\
									\
  ASSERT(rsa_##hash##_sign_digest_tr(pub, key, &rstate,			\
				     (nettle_random_func *)knuth_lfib_random, \
				     digest, signature));		\
  ASSERT(mpz_cmp (signature, expected) == 0);				\
1139 1140 1141 1142 1143 1144
} while(0)

#define VERIFY(key, hash, msg, signature) (	\
  hash##_update(&hash, LDATA(msg)),		\
  rsa_##hash##_verify(key, &hash, signature)	\
)
1145

1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
void
test_rsa_set_key_1(struct rsa_public_key *pub,
		   struct rsa_private_key *key)
{
  /* Initialize key pair for test programs */
  /* 1000-bit key, generated by
   *
   *   lsh-keygen -a rsa -l 1000 -f advanced-hex
   *
   * (private-key (rsa-pkcs1 
   *        (n #69abd505285af665 36ddc7c8f027e6f0 ed435d6748b16088
   *            4fd60842b3a8d7fb bd8a3c98f0cc50ae 4f6a9f7dd73122cc
   *            ec8afa3f77134406 f53721973115fc2d 8cfbba23b145f28d
   *            84f81d3b6ae8ce1e 2850580c026e809b cfbb52566ea3a3b3
   *            df7edf52971872a7 e35c1451b8636d22 279a8fb299368238
   *            e545fbb4cf#)
   *        (e #0db2ad57#)
   *        (d #3240a56f4cd0dcc2 4a413eb4ea545259 5c83d771a1c2ba7b
   *            ec47c5b43eb4b374 09bd2aa1e236dd86 481eb1768811412f
   *            f8d91be3545912af b55c014cb55ceac6 54216af3b85d5c4f
   *            4a32894e3b5dfcde 5b2875aa4dc8d9a8 6afd0ca92ef50d35
   *            bd09f1c47efb4c8d c631e07698d362aa 4a83fd304e66d6c5
   *            468863c307#)
   *        (p #0a66399919be4b4d e5a78c5ea5c85bf9 aba8c013cb4a8732
   *            14557a12bd67711e bb4073fd39ad9a86 f4e80253ad809e5b
   *            f2fad3bc37f6f013 273c9552c9f489#)
   *        (q #0a294f069f118625 f5eae2538db9338c 776a298eae953329
   *            9fd1eed4eba04e82 b2593bc98ba8db27 de034da7daaea795
   *            2d55b07b5f9a5875 d1ca5f6dcab897#)
   *        (a #011b6c48eb592eee e85d1bb35cfb6e07 344ea0b5e5f03a28
   *            5b405396cbc78c5c 868e961db160ba8d 4b984250930cf79a
   *            1bf8a9f28963de53 128aa7d690eb87#)
   *        (b #0409ecf3d2557c88 214f1af5e1f17853 d8b2d63782fa5628
   *            60cf579b0833b7ff 5c0529f2a97c6452 2fa1a8878a9635ab
   *            ce56debf431bdec2 70b308fa5bf387#)
   *        (c #04e103ee925cb5e6 6653949fa5e1a462 c9e65e1adcd60058
   *            e2df9607cee95fa8 daec7a389a7d9afc 8dd21fef9d83805a
   *            40d46f49676a2f6b 2926f70c572c00#)))
   */
  
  mpz_set_str(pub->n,
	      "69abd505285af665" "36ddc7c8f027e6f0" "ed435d6748b16088"
	      "4fd60842b3a8d7fb" "bd8a3c98f0cc50ae" "4f6a9f7dd73122cc"
	      "ec8afa3f77134406" "f53721973115fc2d" "8cfbba23b145f28d"
	      "84f81d3b6ae8ce1e" "2850580c026e809b" "cfbb52566ea3a3b3"
	      "df7edf52971872a7" "e35c1451b8636d22" "279a8fb299368238"
	      "e545fbb4cf", 16);
  mpz_set_str(pub->e, "0db2ad57", 16);

1195
  ASSERT (rsa_public_key_prepare(pub));
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
  
  /* d is not used */
#if 0  
  mpz_set_str(key->d,
	      "3240a56f4cd0dcc2" "4a413eb4ea545259" "5c83d771a1c2ba7b"
	      "ec47c5b43eb4b374" "09bd2aa1e236dd86" "481eb1768811412f"
	      "f8d91be3545912af" "b55c014cb55ceac6" "54216af3b85d5c4f"
	      "4a32894e3b5dfcde" "5b2875aa4dc8d9a8" "6afd0ca92ef50d35"
	      "bd09f1c47efb4c8d" "c631e07698d362aa" "4a83fd304e66d6c5"
	      "468863c307", 16);
#endif
  
  mpz_set_str(key->p,
	      "0a66399919be4b4d" "e5a78c5ea5c85bf9" "aba8c013cb4a8732"
	      "14557a12bd67711e" "bb4073fd39ad9a86" "f4e80253ad809e5b"
	      "f2fad3bc37f6f013" "273c9552c9f489", 16);

  mpz_set_str(key->q,
	      "0a294f069f118625" "f5eae2538db9338c" "776a298eae953329"
	      "9fd1eed4eba04e82" "b2593bc98ba8db27" "de034da7daaea795"
	      "2d55b07b5f9a5875" "d1ca5f6dcab897", 16);
  
  mpz_set_str(key->a,
	      "011b6c48eb592eee" "e85d1bb35cfb6e07" "344ea0b5e5f03a28"
	      "5b405396cbc78c5c" "868e961db160ba8d" "4b984250930cf79a"
	      "1bf8a9f28963de53" "128aa7d690eb87", 16);
  
  mpz_set_str(key->b,
	      "0409ecf3d2557c88" "214f1af5e1f17853" "d8b2d63782fa5628"
	      "60cf579b0833b7ff" "5c0529f2a97c6452" "2fa1a8878a9635ab"
	      "ce56debf431bdec2" "70b308fa5bf387", 16);
  
  mpz_set_str(key->c,
	      "04e103ee925cb5e6" "6653949fa5e1a462" "c9e65e1adcd60058"
	      "e2df9607cee95fa8" "daec7a389a7d9afc" "8dd21fef9d83805a"
	      "40d46f49676a2f6b" "2926f70c572c00", 16);

1233 1234
  ASSERT (rsa_private_key_prepare(key));
  ASSERT (pub->size == key->size);
1235 1236
}

1237 1238 1239 1240 1241 1242
void
test_rsa_md5(struct rsa_public_key *pub,
	     struct rsa_private_key *key,
	     mpz_t expected)
{
  struct md5_ctx md5;
1243 1244
  struct knuth_lfib_ctx rstate;
  uint8_t digest[MD5_DIGEST_SIZE];
1245 1246 1247 1248
  mpz_t signature;

  md5_init(&md5);
  mpz_init(signature);
1249
  knuth_lfib_init (&rstate, 15);
1250

1251
  SIGN(md5, "The magic words are squeamish ossifrage", expected);
1252 1253

  /* Try bad data */
1254 1255
  ASSERT (!VERIFY(pub, md5,
		  "The magick words are squeamish ossifrage", signature));
1256 1257

  /* Try correct data */
1258 1259
  ASSERT (VERIFY(pub, md5,
		 "The magic words are squeamish ossifrage", signature));
1260 1261

  /* Try bad signature */
1262
  mpz_combit(signature, 17);
1263 1264
  ASSERT (!VERIFY(pub, md5,
		  "The magic words are squeamish ossifrage", signature));
1265 1266 1267 1268 1269 1270

  mpz_clear(signature);
}

void
test_rsa_sha1(struct rsa_public_key *pub,
1271 1272
	      struct rsa_private_key *key,
	      mpz_t expected)
1273 1274
{
  struct sha1_ctx sha1;
1275 1276
  struct knuth_lfib_ctx rstate;
  uint8_t digest[SHA1_DIGEST_SIZE];
1277 1278 1279 1280
  mpz_t signature;

  sha1_init(&sha1);
  mpz_init(signature);
1281
  knuth_lfib_init (&rstate, 16);
1282

1283
  SIGN(sha1, "The magic words are squeamish ossifrage", expected);
1284 1285

  /* Try bad data */
1286 1287
  ASSERT (!VERIFY(pub, sha1,
		  "The magick words are squeamish ossifrage", signature));
1288 1289

  /* Try correct data */
1290 1291
  ASSERT (VERIFY(pub, sha1,
		 "The magic words are squeamish ossifrage", signature));
1292 1293

  /* Try bad signature */
1294
  mpz_combit(signature, 17);
1295 1296
  ASSERT (!VERIFY(pub, sha1,
		  "The magic words are squeamish ossifrage", signature));
1297 1298 1299

  mpz_clear(signature);
}
1300

1301 1302
void
test_rsa_sha256(struct rsa_public_key *pub,
1303 1304
		struct rsa_private_key *key,
		mpz_t expected)
1305 1306
{
  struct sha256_ctx sha256;
1307 1308
  struct knuth_lfib_ctx rstate;
  uint8_t digest[SHA256_DIGEST_SIZE];
1309 1310 1311 1312
  mpz_t signature;

  sha256_init(&sha256);
  mpz_init(signature);
1313
  knuth_lfib_init (&rstate, 17);
1314

1315
  SIGN(sha256, "The magic words are squeamish ossifrage", expected);
1316 1317

  /* Try bad data */
1318 1319
  ASSERT (!VERIFY(pub, sha256,
		  "The magick words are squeamish ossifrage", signature));
1320 1321

  /* Try correct data */
1322 1323
  ASSERT (VERIFY(pub, sha256,
		 "The magic words are squeamish ossifrage", signature));
1324 1325

  /* Try bad signature */
1326
  mpz_combit(signature, 17);
1327 1328
  ASSERT (!VERIFY(pub, sha256,
		  "The magic words are squeamish ossifrage", signature));
1329 1330 1331 1332

  mpz_clear(signature);
}

1333 1334 1335 1336 1337 1338
void
test_rsa_sha512(struct rsa_public_key *pub,
		struct rsa_private_key *key,
		mpz_t expected)
{
  struct sha512_ctx sha512;
1339 1340
  struct knuth_lfib_ctx rstate;
  uint8_t digest[SHA512_DIGEST_SIZE];