arctwo-test.c 3.32 KB
Newer Older
Simon Josefsson's avatar
Simon Josefsson committed
1 2 3
/* nettle, low-level cryptographics library
 *
 * Copyright (C) 2004 Simon Josefsson
Niels Möller's avatar
Niels Möller committed
4
 * Copyright (C) 2004 Niels Möller
Simon Josefsson's avatar
Simon Josefsson committed
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * The nettle library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version.
 *
 * The nettle library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with the nettle library; see the file COPYING.LIB.  If not, write to
18 19
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02111-1301, USA.
Simon Josefsson's avatar
Simon Josefsson committed
20 21 22 23 24 25 26
 */

#include "testutils.h"
#include "arctwo.h"

/* For tests with obscure values of ebk. */
static void
27
test_arctwo(unsigned ekb,
28 29 30
	    const struct tstring *key,
	    const struct tstring *cleartext,
	    const struct tstring *ciphertext)
Simon Josefsson's avatar
Simon Josefsson committed
31 32
{
  struct arctwo_ctx ctx;
33 34
  uint8_t *data;
  unsigned length;
Simon Josefsson's avatar
Simon Josefsson committed
35

36 37 38 39
  ASSERT (cleartext->length == ciphertext->length);
  length = cleartext->length;
  
  data = xalloc(length);
Simon Josefsson's avatar
Simon Josefsson committed
40

41 42 43 44
  arctwo_set_key_ekb(&ctx, key->length, key->data, ekb);
  arctwo_encrypt(&ctx, length, data, cleartext->data);

  ASSERT(MEMEQ(length, data, ciphertext->data));
Simon Josefsson's avatar
Simon Josefsson committed
45 46 47

  arctwo_decrypt(&ctx, length, data, data);

48
  ASSERT(MEMEQ(length, data, cleartext->data));
Simon Josefsson's avatar
Simon Josefsson committed
49 50 51 52

  free(data);
}

53
void
Simon Josefsson's avatar
Simon Josefsson committed
54 55 56 57
test_main(void)
{
  /* Test vectors from Peter Gutmann's paper. */
  test_cipher(&nettle_arctwo_gutmann128,
58 59 60
	      SHEX("00000000 00000000 00000000 00000000"),
	      SHEX("00000000 00000000"),
	      SHEX("1c198a83 8df028b7"));
Simon Josefsson's avatar
Simon Josefsson committed
61 62

  test_cipher(&nettle_arctwo_gutmann128,
63 64 65
	      SHEX("00010203 04050607 08090a0b 0c0d0e0f"),
	      SHEX("00000000 00000000"),
	      SHEX("50dc0162 bd757f31"));
Simon Josefsson's avatar
Simon Josefsson committed
66 67 68

  /* This one was checked against libmcrypt's RFC2268. */
  test_cipher(&nettle_arctwo_gutmann128,
69 70 71
	      SHEX("30000000 00000000 00000000 00000000"),
	      SHEX("10000000 00000000"),
	      SHEX("8fd10389 336bf95e"));
Simon Josefsson's avatar
Simon Josefsson committed
72 73 74

  /* Test vectors from RFC 2268. */
  test_cipher(&nettle_arctwo64,
75 76 77
	      SHEX("ffffffff ffffffff"),
	      SHEX("ffffffff ffffffff"),
	      SHEX("278b27e4 2e2f0d49"));
Simon Josefsson's avatar
Simon Josefsson committed
78 79

  test_cipher(&nettle_arctwo64,
80 81 82
	      SHEX("30000000 00000000"),
	      SHEX("10000000 00000001"),
	      SHEX("30649edf 9be7d2c2"));
Simon Josefsson's avatar
Simon Josefsson committed
83 84

  test_cipher(&nettle_arctwo128,
85 86 87
	      SHEX("88bca90e 90875a7f 0f79c384 627bafb2"),
	      SHEX("00000000 00000000"),
	      SHEX("2269552a b0f85ca6"));
Simon Josefsson's avatar
Simon Josefsson committed
88 89 90

  /* More obscure tests from RFC 2286 */
  test_arctwo(63,
91 92 93
	      SHEX("00000000 00000000"),
	      SHEX("00000000 00000000"),
	      SHEX("ebb773f9 93278eff"));
Simon Josefsson's avatar
Simon Josefsson committed
94 95

  test_arctwo(64,
96 97 98
	      SHEX("88"),
	      SHEX("00000000 00000000"),
	      SHEX("61a8a244 adacccf0"));
Simon Josefsson's avatar
Simon Josefsson committed
99 100

  test_arctwo(64,
101 102 103
	      SHEX("88bca90e 90875a"),
	      SHEX("00000000 00000000"),
	      SHEX("6ccf4308 974c267f"));
Simon Josefsson's avatar
Simon Josefsson committed
104 105

  test_arctwo(64,
106 107 108
	      SHEX("88bca90e 90875a7f 0f79c384 627bafb2"),
	      SHEX("00000000 00000000"),
	      SHEX("1a807d27 2bbe5db1"));
Simon Josefsson's avatar
Simon Josefsson committed
109 110

  test_arctwo(129,
111 112 113 114
	      SHEX("88bca90e 90875a7f 0f79c384 627bafb2"
		   "16f80a6f 85920584 c42fceb0 be255daf 1e"),
	      SHEX("00000000 00000000"),
	      SHEX("5b78d3a4 3dfff1f1"));
Simon Josefsson's avatar
Simon Josefsson committed
115
}