summaryrefslogtreecommitdiff
path: root/testsuite/blowfish-test.c
blob: cadeda5f2ba809001eb69065ee1538c2b8877b02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "testutils.h"
#include "nettle-internal.h"
#include "blowfish.h"

static void
test_blowfish(const struct tstring *key,
	      const struct tstring *cleartext,
	      const struct tstring *ciphertext)
{
  struct blowfish_ctx ctx;
  uint8_t *data = xalloc(cleartext->length);
  size_t length;
  ASSERT (cleartext->length == ciphertext->length);
  length = cleartext->length;

  blowfish_set_key(&ctx, key->length, key->data);
  blowfish_encrypt(&ctx, length, data, cleartext->data);

  if (!MEMEQ(length, data, ciphertext->data))
    {
      fprintf(stderr, "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();
    }
  blowfish_set_key(&ctx, key->length, key->data);
  blowfish_decrypt(&ctx, length, data, data);

  if (!MEMEQ(length, data, cleartext->data))
    {
      fprintf(stderr, "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();
    }

  free(data);
}

void
test_main(void)
{
  /* 208 bit key. Test from GNUPG. */
  test_blowfish(SDATA("abcdefghijklmnopqrstuvwxyz"),
		SDATA("BLOWFISH"),
		SHEX("32 4E D0 FE F4 13 A2 03"));
}