src/data/format.c (fmt_affix_free): Fix memory leak
[pspp] / tests / libpspp / cmac-aes256-test.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2013 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <string.h>
20
21 #include "libpspp/cmac-aes256.h"
22
23 #undef NDEBUG
24 #include <assert.h>
25
26 static void
27 test_cmac (const uint8_t key[32], size_t key_size,
28            const uint8_t *data, size_t data_size,
29            const uint8_t *exp_cmac, size_t exp_cmac_size)
30 {
31   uint8_t cmac[16];
32
33   assert (key_size == 32);
34   assert (exp_cmac_size <= 16);
35
36   cmac_aes256 (key, data, data_size, cmac);
37   assert (!memcmp (cmac, exp_cmac, exp_cmac_size));
38 }
39
40 /* From NIST CMAC test vectors. */
41 static void
42 test_cmac1 (void)
43 {
44   static const uint8_t key[] = {
45     0x0b,0x12,0x2a,0xc8, 0xf3,0x4e,0xd1,0xfe,
46     0x08,0x2a,0x36,0x25, 0xd1,0x57,0x56,0x14,
47     0x54,0x16,0x7a,0xc1, 0x45,0xa1,0x0b,0xbf,
48     0x77,0xc6,0xa7,0x05, 0x96,0xd5,0x74,0xf1
49   };
50   static const uint8_t data[] = {
51     0x49,0x8b,0x53,0xfd, 0xec,0x87,0xed,0xcb,
52     0xf0,0x70,0x97,0xdc, 0xcd,0xe9,0x3a,0x08,
53     0x4b,0xad,0x75,0x01, 0xa2,0x24,0xe3,0x88,
54     0xdf,0x34,0x9c,0xe1, 0x89,0x59,0xfe,0x84,
55     0x85,0xf8,0xad,0x15, 0x37,0xf0,0xd8,0x96,
56     0xea,0x73,0xbe,0xdc, 0x72,0x14,0x71,0x3f,
57   };
58   static const uint8_t exp_cmac[] = { 0xf6,0x2c,0x46,0x32, 0x9b };
59
60   test_cmac (key, sizeof key,
61              data, sizeof data,
62              exp_cmac, sizeof exp_cmac);
63 }
64
65 /* CMAC-AES-256 test vectors from NIST's updated SP800-38B examples.  */
66 static void
67 test_cmac2 (void)
68 {
69   static const uint8_t key[] = {
70     0x60,0x3d,0xeb,0x10, 0x15,0xca,0x71,0xbe,
71     0x2b,0x73,0xae,0xf0, 0x85,0x7d,0x77,0x81,
72     0x1f,0x35,0x2c,0x07, 0x3b,0x61,0x08,0xd7,
73     0x2d,0x98,0x10,0xa3, 0x09,0x14,0xdf,0xf4,
74   };
75   static const uint8_t data[] = {
76     0x6b,0xc1,0xbe,0xe2, 0x2e,0x40,0x9f,0x96,
77     0xe9,0x3d,0x7e,0x11, 0x73,0x93,0x17,0x2a,
78     0xae,0x2d,0x8a,0x57, 0x1e,0x03,0xac,0x9c,
79     0x9e,0xb7,0x6f,0xac, 0x45,0xaf,0x8e,0x51,
80     0x30,0xc8,0x1c,0x46, 0xa3,0x5c,0xe4,0x11,
81     0xe5,0xfb,0xc1,0x19, 0x1a,0x0a,0x52,0xef,
82     0xf6,0x9f,0x24,0x45, 0xdf,0x4f,0x9b,0x17,
83     0xad,0x2b,0x41,0x7b, 0xe6,0x6c,0x37,0x10,
84   };
85   static const uint8_t exp_cmac0[] = {
86     0x02,0x89,0x62,0xf6, 0x1b,0x7b,0xf8,0x9e,
87     0xfc,0x6b,0x55,0x1f, 0x46,0x67,0xd9,0x83,
88   };
89   static const uint8_t exp_cmac16[] = {
90     0x28,0xa7,0x02,0x3f, 0x45,0x2e,0x8f,0x82,
91     0xbd,0x4b,0xf2,0x8d, 0x8c,0x37,0xc3,0x5c,
92   };
93   static const uint8_t exp_cmac40[] = {
94     0xaa,0xf3,0xd8,0xf1, 0xde,0x56,0x40,0xc2,
95     0x32,0xf5,0xb1,0x69, 0xb9,0xc9,0x11,0xe6,
96   };
97   static const uint8_t exp_cmac64[] = {
98     0xe1,0x99,0x21,0x90, 0x54,0x9f,0x6e,0xd5,
99     0x69,0x6a,0x2c,0x05, 0x6c,0x31,0x54,0x10,
100   };
101
102   test_cmac (key, sizeof key, data, 0, exp_cmac0, sizeof exp_cmac0);
103   test_cmac (key, sizeof key, data, 16, exp_cmac16, sizeof exp_cmac16);
104   test_cmac (key, sizeof key, data, 40, exp_cmac40, sizeof exp_cmac40);
105   test_cmac (key, sizeof key, data, 64, exp_cmac64, sizeof exp_cmac64);
106 }
107
108 int
109 main(void)
110 {
111   test_cmac1 ();
112   test_cmac2 ();
113   return 0;
114 }