~ubuntu-branches/ubuntu/vivid/nettle/vivid-proposed

« back to all changes in this revision

Viewing changes to testsuite/camellia-test.c

  • Committer: Package Import Robot
  • Author(s): Magnus Holmgren
  • Date: 2013-03-24 11:38:21 UTC
  • mfrom: (1.5.2)
  • mto: (8.1.7 sid)
  • mto: This revision was merged to the branch mainline in revision 14.
  • Revision ID: package-import@ubuntu.com-20130324113821-47kc1q7ojsxmuevv
Tags: 2.6-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
#include "camellia.h"
3
3
 
4
4
static void
5
 
test_invert(unsigned key_length, const uint8_t *key,
6
 
            unsigned length, const uint8_t *cleartext,
7
 
            const uint8_t *ciphertext)
 
5
test_invert(const struct tstring *key,
 
6
            const struct tstring *cleartext,
 
7
            const struct tstring *ciphertext)
8
8
{
9
9
  struct camellia_ctx encrypt;
10
10
  struct camellia_ctx decrypt;
11
 
  uint8_t *data = xalloc(length);
12
 
 
13
 
  camellia_set_encrypt_key (&encrypt, key_length, key);
14
 
  camellia_crypt (&encrypt, length, data, cleartext);
 
11
  uint8_t *data;
 
12
  unsigned length;
 
13
 
 
14
  ASSERT (cleartext->length == ciphertext->length);
 
15
  length = cleartext->length;
 
16
 
 
17
  data = xalloc(length);
 
18
 
 
19
  camellia_set_encrypt_key (&encrypt, key->length, key->data);
 
20
  camellia_crypt (&encrypt, length, data, cleartext->data);
15
21
  
16
 
  if (!MEMEQ(length, data, ciphertext))
 
22
  if (!MEMEQ(length, data, ciphertext->data))
17
23
    {
18
 
      fprintf(stderr, "test_invert: Encrypt failed:\nInput:");
19
 
      print_hex(length, cleartext);
 
24
      tstring_print_hex(cleartext);
20
25
      fprintf(stderr, "\nOutput: ");
21
26
      print_hex(length, data);
22
27
      fprintf(stderr, "\nExpected:");
23
 
      print_hex(length, ciphertext);
 
28
      tstring_print_hex(ciphertext);
24
29
      fprintf(stderr, "\n");
25
30
      FAIL();
26
31
    }
28
33
  camellia_invert_key (&decrypt, &encrypt);
29
34
  camellia_crypt (&decrypt, length, data, data);
30
35
 
31
 
  if (!MEMEQ(length, data, cleartext))
 
36
  if (!MEMEQ(length, data, cleartext->data))
32
37
    {
33
38
      fprintf(stderr, "test_invert: Decrypt failed:\nInput:");
34
 
      print_hex(length, ciphertext);
 
39
      tstring_print_hex(ciphertext);
35
40
      fprintf(stderr, "\nOutput: ");
36
41
      print_hex(length, data);
37
42
      fprintf(stderr, "\nExpected:");
38
 
      print_hex(length, cleartext);
 
43
      tstring_print_hex(cleartext);
39
44
      fprintf(stderr, "\n");
40
45
      FAIL();
41
46
    }
42
47
  free (data);
43
48
}
44
49
 
45
 
int
 
50
void
46
51
test_main(void)
47
52
{
48
53
  /* Test vectors from RFC 3713 */
49
54
  /* 128 bit keys */
50
55
  test_cipher(&nettle_camellia128,
51
 
              HL("01 23 45 67 89 ab cd ef fe dc ba 98 76 54 32 10"),
52
 
              HL("01 23 45 67 89 ab cd ef fe dc ba 98 76 54 32 10"),
53
 
              H("67 67 31 38 54 96 69 73 08 57 06 56 48 ea be 43"));
 
56
              SHEX("01 23 45 67 89 ab cd ef fe dc ba 98 76 54 32 10"),
 
57
              SHEX("01 23 45 67 89 ab cd ef fe dc ba 98 76 54 32 10"),
 
58
              SHEX("67 67 31 38 54 96 69 73 08 57 06 56 48 ea be 43"));
54
59
 
55
60
  /* 192 bit keys */
56
61
  test_cipher(&nettle_camellia192, 
57
 
              HL("01 23 45 67 89 ab cd ef fe dc ba 98 76 54 32 10"
58
 
                 "00 11 22 33 44 55 66 77"),
59
 
              HL("01 23 45 67 89 ab cd ef fe dc ba 98 76 54 32 10"),
60
 
              H("b4 99 34 01 b3 e9 96 f8 4e e5 ce e7 d7 9b 09 b9"));
 
62
              SHEX("01 23 45 67 89 ab cd ef fe dc ba 98 76 54 32 10"
 
63
                   "00 11 22 33 44 55 66 77"),
 
64
              SHEX("01 23 45 67 89 ab cd ef fe dc ba 98 76 54 32 10"),
 
65
              SHEX("b4 99 34 01 b3 e9 96 f8 4e e5 ce e7 d7 9b 09 b9"));
61
66
 
62
67
  /* 256 bit keys */
63
68
  test_cipher(&nettle_camellia256, 
64
 
              HL("01 23 45 67 89 ab cd ef fe dc ba 98 76 54 32 10"
65
 
                 "00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff"),
66
 
              HL("01 23 45 67 89 ab cd ef fe dc ba 98 76 54 32 10"),
67
 
              H("9a cc 23 7d ff 16 d7 6c 20 ef 7c 91 9e 3a 75 09"));
 
69
              SHEX("01 23 45 67 89 ab cd ef fe dc ba 98 76 54 32 10"
 
70
                   "00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff"),
 
71
              SHEX("01 23 45 67 89 ab cd ef fe dc ba 98 76 54 32 10"),
 
72
              SHEX("9a cc 23 7d ff 16 d7 6c 20 ef 7c 91 9e 3a 75 09"));
68
73
 
69
74
  /* Test camellia_invert_key with src != dst */
70
 
  test_invert(HL("01 23 45 67 89 ab cd ef fe dc ba 98 76 54 32 10"),
71
 
              HL("01 23 45 67 89 ab cd ef fe dc ba 98 76 54 32 10"),
72
 
              H("67 67 31 38 54 96 69 73 08 57 06 56 48 ea be 43"));
 
75
  test_invert(SHEX("01 23 45 67 89 ab cd ef fe dc ba 98 76 54 32 10"),
 
76
              SHEX("01 23 45 67 89 ab cd ef fe dc ba 98 76 54 32 10"),
 
77
              SHEX("67 67 31 38 54 96 69 73 08 57 06 56 48 ea be 43"));
73
78
  
74
 
  test_invert(HL("01 23 45 67 89 ab cd ef fe dc ba 98 76 54 32 10"
75
 
                 "00 11 22 33 44 55 66 77"),
76
 
              HL("01 23 45 67 89 ab cd ef fe dc ba 98 76 54 32 10"),
77
 
              H("b4 99 34 01 b3 e9 96 f8 4e e5 ce e7 d7 9b 09 b9"));
 
79
  test_invert(SHEX("01 23 45 67 89 ab cd ef fe dc ba 98 76 54 32 10"
 
80
                   "00 11 22 33 44 55 66 77"),
 
81
              SHEX("01 23 45 67 89 ab cd ef fe dc ba 98 76 54 32 10"),
 
82
              SHEX("b4 99 34 01 b3 e9 96 f8 4e e5 ce e7 d7 9b 09 b9"));
78
83
 
79
 
  test_invert(HL("01 23 45 67 89 ab cd ef fe dc ba 98 76 54 32 10"
80
 
                 "00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff"),
81
 
              HL("01 23 45 67 89 ab cd ef fe dc ba 98 76 54 32 10"),
82
 
              H("9a cc 23 7d ff 16 d7 6c 20 ef 7c 91 9e 3a 75 09"));
83
 
  SUCCESS();
 
84
  test_invert(SHEX("01 23 45 67 89 ab cd ef fe dc ba 98 76 54 32 10"
 
85
                   "00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff"),
 
86
              SHEX("01 23 45 67 89 ab cd ef fe dc ba 98 76 54 32 10"),
 
87
              SHEX("9a cc 23 7d ff 16 d7 6c 20 ef 7c 91 9e 3a 75 09"));
84
88
}