~ubuntu-branches/ubuntu/utopic/nettle/utopic

« back to all changes in this revision

Viewing changes to testsuite/aes-test.c

  • Committer: Package Import Robot
  • Author(s): Magnus Holmgren
  • Date: 2013-05-07 22:57:14 UTC
  • mfrom: (8.1.8 sid)
  • Revision ID: package-import@ubuntu.com-20130507225714-s331yr8ov53dtt17
Tags: 2.7-2
Tag some (ECC related) symbols that only exist on some architectures.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
#include "aes.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 aes_ctx encrypt;
10
10
  struct aes_ctx decrypt;
11
 
  uint8_t *data = xalloc(length);
 
11
  uint8_t *data = xalloc(cleartext->length);
 
12
  unsigned length;
 
13
  ASSERT (cleartext->length == ciphertext->length);
 
14
  length = cleartext->length;
12
15
 
13
 
  aes_set_encrypt_key (&encrypt, key_length, key);
14
 
  aes_encrypt (&encrypt, length, data, cleartext);
 
16
  aes_set_encrypt_key (&encrypt, key->length, key->data);
 
17
  aes_encrypt (&encrypt, length, data, cleartext->data);
15
18
  
16
 
  if (!MEMEQ(length, data, ciphertext))
 
19
  if (!MEMEQ(length, data, ciphertext->data))
17
20
    {
18
21
      fprintf(stderr, "test_invert: Encrypt failed:\nInput:");
19
 
      print_hex(length, cleartext);
 
22
      tstring_print_hex(cleartext);
20
23
      fprintf(stderr, "\nOutput: ");
21
24
      print_hex(length, data);
22
25
      fprintf(stderr, "\nExpected:");
23
 
      print_hex(length, ciphertext);
 
26
      tstring_print_hex(ciphertext);
24
27
      fprintf(stderr, "\n");
25
28
      FAIL();
26
29
    }
28
31
  aes_invert_key (&decrypt, &encrypt);
29
32
  aes_decrypt (&decrypt, length, data, data);
30
33
 
31
 
  if (!MEMEQ(length, data, cleartext))
 
34
  if (!MEMEQ(length, data, cleartext->data))
32
35
    {
33
36
      fprintf(stderr, "test_invert: Decrypt failed:\nInput:");
34
 
      print_hex(length, ciphertext);
 
37
      tstring_print_hex(ciphertext);
35
38
      fprintf(stderr, "\nOutput: ");
36
39
      print_hex(length, data);
37
40
      fprintf(stderr, "\nExpected:");
38
 
      print_hex(length, cleartext);
 
41
      tstring_print_hex(cleartext);
39
42
      fprintf(stderr, "\n");
40
43
      FAIL();
41
44
    }
42
45
  free (data);
43
46
}
44
47
 
45
 
int
 
48
void
46
49
test_main(void)
47
50
{
48
51
  /* 128 bit keys */
49
52
  test_cipher(&nettle_aes128, 
50
 
              HL("0001020305060708 0A0B0C0D0F101112"),
51
 
              HL("506812A45F08C889 B97F5980038B8359"),
52
 
              H("D8F532538289EF7D 06B506A4FD5BE9C9"));
 
53
              SHEX("0001020305060708 0A0B0C0D0F101112"),
 
54
              SHEX("506812A45F08C889 B97F5980038B8359"),
 
55
              SHEX("D8F532538289EF7D 06B506A4FD5BE9C9"));
53
56
  
54
57
  test_cipher(&nettle_aes128, 
55
 
              HL("14151617191A1B1C 1E1F202123242526"),
56
 
              HL("5C6D71CA30DE8B8B 00549984D2EC7D4B"),
57
 
              H("59AB30F4D4EE6E4F F9907EF65B1FB68C"));
 
58
              SHEX("14151617191A1B1C 1E1F202123242526"),
 
59
              SHEX("5C6D71CA30DE8B8B 00549984D2EC7D4B"),
 
60
              SHEX("59AB30F4D4EE6E4F F9907EF65B1FB68C"));
58
61
 
59
62
  test_cipher(&nettle_aes128, 
60
 
              HL("28292A2B2D2E2F30 323334353738393A"),
61
 
              HL("53F3F4C64F8616E4 E7C56199F48F21F6"),
62
 
              H("BF1ED2FCB2AF3FD4 1443B56D85025CB1"));
 
63
              SHEX("28292A2B2D2E2F30 323334353738393A"),
 
64
              SHEX("53F3F4C64F8616E4 E7C56199F48F21F6"),
 
65
              SHEX("BF1ED2FCB2AF3FD4 1443B56D85025CB1"));
63
66
  
64
67
  test_cipher(&nettle_aes128, 
65
 
              HL("A0A1A2A3A5A6A7A8 AAABACADAFB0B1B2"),
66
 
              HL("F5F4F7F684878689 A6A7A0A1D2CDCCCF"),
67
 
              H("CE52AF650D088CA5 59425223F4D32694"));
 
68
              SHEX("A0A1A2A3A5A6A7A8 AAABACADAFB0B1B2"),
 
69
              SHEX("F5F4F7F684878689 A6A7A0A1D2CDCCCF"),
 
70
              SHEX("CE52AF650D088CA5 59425223F4D32694"));
68
71
 
69
72
  /* 192 bit keys */
70
73
  
71
74
  test_cipher(&nettle_aes192, 
72
 
              HL("0001020305060708 0A0B0C0D0F101112"
73
 
                "14151617191A1B1C"),
74
 
              HL("2D33EEF2C0430A8A 9EBF45E809C40BB6"),
75
 
              H("DFF4945E0336DF4C 1C56BC700EFF837F"));
 
75
              SHEX("0001020305060708 0A0B0C0D0F101112"
 
76
                   "14151617191A1B1C"),
 
77
              SHEX("2D33EEF2C0430A8A 9EBF45E809C40BB6"),
 
78
              SHEX("DFF4945E0336DF4C 1C56BC700EFF837F"));
76
79
 
77
80
  /* 256 bit keys */
78
81
  
79
82
  test_cipher(&nettle_aes256,
80
 
              HL("0001020305060708 0A0B0C0D0F101112"
81
 
                "14151617191A1B1C 1E1F202123242526"),
82
 
              HL("834EADFCCAC7E1B30664B1ABA44815AB"),
83
 
              H("1946DABF6A03A2A2 C3D0B05080AED6FC"));
 
83
              SHEX("0001020305060708 0A0B0C0D0F101112"
 
84
                   "14151617191A1B1C 1E1F202123242526"),
 
85
              SHEX("834EADFCCAC7E1B30664B1ABA44815AB"),
 
86
              SHEX("1946DABF6A03A2A2 C3D0B05080AED6FC"));
84
87
 
85
88
  
86
89
  /* This test case has been problematic with the CBC test case */
87
90
  test_cipher(&nettle_aes256,
88
 
              HL("8d ae 93 ff fc 78 c9 44"
89
 
                "2a bd 0c 1e 68 bc a6 c7"
90
 
                "05 c7 84 e3 5a a9 11 8b"
91
 
                "d3 16 aa 54 9b 44 08 9e"),
92
 
              HL("a5 ce 55 d4 21 15 a1 c6 4a a4 0c b2 ca a6 d1 37"),
 
91
              SHEX("8d ae 93 ff fc 78 c9 44"
 
92
                   "2a bd 0c 1e 68 bc a6 c7"
 
93
                   "05 c7 84 e3 5a a9 11 8b"
 
94
                   "d3 16 aa 54 9b 44 08 9e"),
 
95
              SHEX("a5 ce 55 d4 21 15 a1 c6 4a a4 0c b2 ca a6 d1 37"),
93
96
              /* In the cbc test, I once got the bad value
94
97
               *   "b2 a0 6c d2 2f df 7d 2c  26 d2 42 88 8f 20 74 a2" */
95
 
              H("1f 94 fc 85 f2 36 21 06"
96
 
                "4a ea e3 c9 cc 38 01 0e"));
 
98
              SHEX("1f 94 fc 85 f2 36 21 06"
 
99
                   "4a ea e3 c9 cc 38 01 0e"));
97
100
  
98
101
  /* From draft NIST spec on AES modes.
99
102
   *
102
105
   */
103
106
 
104
107
  test_cipher(&nettle_aes128,
105
 
              HL("2b7e151628aed2a6abf7158809cf4f3c"),
106
 
              HL("6bc1bee22e409f96e93d7e117393172a"
107
 
                "ae2d8a571e03ac9c9eb76fac45af8e51"
108
 
                "30c81c46a35ce411e5fbc1191a0a52ef"
109
 
                "f69f2445df4f9b17ad2b417be66c3710"),
110
 
              H("3ad77bb40d7a3660a89ecaf32466ef97"
111
 
                "f5d3d58503b9699de785895a96fdbaaf"
112
 
                "43b1cd7f598ece23881b00e3ed030688"
113
 
                "7b0c785e27e8ad3f8223207104725dd4"));
 
108
              SHEX("2b7e151628aed2a6abf7158809cf4f3c"),
 
109
              SHEX("6bc1bee22e409f96e93d7e117393172a"
 
110
                   "ae2d8a571e03ac9c9eb76fac45af8e51"
 
111
                   "30c81c46a35ce411e5fbc1191a0a52ef"
 
112
                   "f69f2445df4f9b17ad2b417be66c3710"),
 
113
              SHEX("3ad77bb40d7a3660a89ecaf32466ef97"
 
114
                   "f5d3d58503b9699de785895a96fdbaaf"
 
115
                   "43b1cd7f598ece23881b00e3ed030688"
 
116
                   "7b0c785e27e8ad3f8223207104725dd4"));
114
117
 
115
118
  /* F.1.3 ECB-AES192-Encrypt */
116
119
 
117
120
  test_cipher(&nettle_aes192,
118
 
              HL("8e73b0f7da0e6452c810f32b809079e5 62f8ead2522c6b7b"),
119
 
              HL("6bc1bee22e409f96e93d7e117393172a"
120
 
                "ae2d8a571e03ac9c9eb76fac45af8e51"
121
 
                "30c81c46a35ce411e5fbc1191a0a52ef"
122
 
                "f69f2445df4f9b17ad2b417be66c3710"),
123
 
              H("bd334f1d6e45f25ff712a214571fa5cc"
124
 
                "974104846d0ad3ad7734ecb3ecee4eef"
125
 
                "ef7afd2270e2e60adce0ba2face6444e"
126
 
                "9a4b41ba738d6c72fb16691603c18e0e"));
 
121
              SHEX("8e73b0f7da0e6452c810f32b809079e5 62f8ead2522c6b7b"),
 
122
              SHEX("6bc1bee22e409f96e93d7e117393172a"
 
123
                   "ae2d8a571e03ac9c9eb76fac45af8e51"
 
124
                   "30c81c46a35ce411e5fbc1191a0a52ef"
 
125
                   "f69f2445df4f9b17ad2b417be66c3710"),
 
126
              SHEX("bd334f1d6e45f25ff712a214571fa5cc"
 
127
                   "974104846d0ad3ad7734ecb3ecee4eef"
 
128
                   "ef7afd2270e2e60adce0ba2face6444e"
 
129
                   "9a4b41ba738d6c72fb16691603c18e0e"));
127
130
 
128
131
  /* F.1.5 ECB-AES256-Encrypt */
129
132
  test_cipher(&nettle_aes256,
130
 
              HL("603deb1015ca71be2b73aef0857d7781"
131
 
                "1f352c073b6108d72d9810a30914dff4"),
132
 
              HL("6bc1bee22e409f96e93d7e117393172a"
133
 
                "ae2d8a571e03ac9c9eb76fac45af8e51" 
134
 
                "30c81c46a35ce411e5fbc1191a0a52ef"
135
 
                "f69f2445df4f9b17ad2b417be66c3710"),
136
 
              H("f3eed1bdb5d2a03c064b5a7e3db181f8"
137
 
                "591ccb10d410ed26dc5ba74a31362870"
138
 
                "b6ed21b99ca6f4f9f153e7b1beafed1d"
139
 
                "23304b7a39f9f3ff067d8d8f9e24ecc7"));
 
133
              SHEX("603deb1015ca71be2b73aef0857d7781"
 
134
                   "1f352c073b6108d72d9810a30914dff4"),
 
135
              SHEX("6bc1bee22e409f96e93d7e117393172a"
 
136
                   "ae2d8a571e03ac9c9eb76fac45af8e51" 
 
137
                   "30c81c46a35ce411e5fbc1191a0a52ef"
 
138
                   "f69f2445df4f9b17ad2b417be66c3710"),
 
139
              SHEX("f3eed1bdb5d2a03c064b5a7e3db181f8"
 
140
                   "591ccb10d410ed26dc5ba74a31362870"
 
141
                   "b6ed21b99ca6f4f9f153e7b1beafed1d"
 
142
                   "23304b7a39f9f3ff067d8d8f9e24ecc7"));
140
143
 
141
144
  /* Test aes_invert_key with src != dst */
142
 
  test_invert(HL("0001020305060708 0A0B0C0D0F101112"),
143
 
              HL("506812A45F08C889 B97F5980038B8359"),
144
 
              H("D8F532538289EF7D 06B506A4FD5BE9C9"));
145
 
  test_invert(HL("0001020305060708 0A0B0C0D0F101112"
146
 
                "14151617191A1B1C"),
147
 
              HL("2D33EEF2C0430A8A 9EBF45E809C40BB6"),
148
 
              H("DFF4945E0336DF4C 1C56BC700EFF837F"));
149
 
  test_invert(HL("0001020305060708 0A0B0C0D0F101112"
150
 
                "14151617191A1B1C 1E1F202123242526"),
151
 
              HL("834EADFCCAC7E1B30664B1ABA44815AB"),
152
 
              H("1946DABF6A03A2A2 C3D0B05080AED6FC"));
153
 
 
154
 
  SUCCESS();
 
145
  test_invert(SHEX("0001020305060708 0A0B0C0D0F101112"),
 
146
              SHEX("506812A45F08C889 B97F5980038B8359"),
 
147
              SHEX("D8F532538289EF7D 06B506A4FD5BE9C9"));
 
148
  test_invert(SHEX("0001020305060708 0A0B0C0D0F101112"
 
149
                   "14151617191A1B1C"),
 
150
              SHEX("2D33EEF2C0430A8A 9EBF45E809C40BB6"),
 
151
              SHEX("DFF4945E0336DF4C 1C56BC700EFF837F"));
 
152
  test_invert(SHEX("0001020305060708 0A0B0C0D0F101112"
 
153
                   "14151617191A1B1C 1E1F202123242526"),
 
154
              SHEX("834EADFCCAC7E1B30664B1ABA44815AB"),
 
155
              SHEX("1946DABF6A03A2A2 C3D0B05080AED6FC"));
155
156
}
156
157
 
157
158
/* Internal state for the first test case: