lib:crypto: Include only the required header files
[gd/samba-autobuild/.git] / lib / crypto / aes_cmac_128_test.c
1 /*
2    AES-CMAC-128 tests
3    Copyright (C) Stefan Metzmacher 2012
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "replace.h"
19 #include "../lib/util/samba_util.h"
20 #include "lib/crypto/aes.h"
21 #include "lib/crypto/aes_cmac_128.h"
22
23 struct torture_context;
24 bool torture_local_crypto_aes_cmac_128(struct torture_context *torture);
25
26 /*
27  This uses the test values from rfc 4493
28 */
29 bool torture_local_crypto_aes_cmac_128(struct torture_context *torture)
30 {
31         bool ret = true;
32         uint32_t i;
33         DATA_BLOB key;
34         struct {
35                 DATA_BLOB data;
36                 DATA_BLOB cmac;
37         } testarray[5];
38
39         TALLOC_CTX *tctx = talloc_new(torture);
40         if (!tctx) { return false; };
41
42         key = strhex_to_data_blob(tctx, "2b7e151628aed2a6abf7158809cf4f3c");
43
44         testarray[0].data = data_blob_null;
45         testarray[0].cmac = strhex_to_data_blob(tctx,
46                                 "bb1d6929e95937287fa37d129b756746");
47
48         testarray[1].data = strhex_to_data_blob(tctx,
49                                 "6bc1bee22e409f96e93d7e117393172a");
50         testarray[1].cmac = strhex_to_data_blob(tctx,
51                                 "070a16b46b4d4144f79bdd9dd04a287c");
52
53         testarray[2].data = strhex_to_data_blob(tctx,
54                                 "6bc1bee22e409f96e93d7e117393172a"
55                                 "ae2d8a571e03ac9c9eb76fac45af8e51"
56                                 "30c81c46a35ce411");
57         testarray[2].cmac = strhex_to_data_blob(tctx,
58                                 "dfa66747de9ae63030ca32611497c827");
59
60         testarray[3].data = strhex_to_data_blob(tctx,
61                                 "6bc1bee22e409f96e93d7e117393172a"
62                                 "ae2d8a571e03ac9c9eb76fac45af8e51"
63                                 "30c81c46a35ce411e5fbc1191a0a52ef"
64                                 "f69f2445df4f9b17ad2b417be66c3710");
65         testarray[3].cmac = strhex_to_data_blob(tctx,
66                                 "51f0bebf7e3b9d92fc49741779363cfe");
67
68         ZERO_STRUCT(testarray[4]);
69
70         for (i=0; testarray[i].cmac.length != 0; i++) {
71                 struct aes_cmac_128_context ctx;
72                 uint8_t cmac[AES_BLOCK_SIZE];
73                 int e;
74
75                 aes_cmac_128_init(&ctx, key.data);
76                 aes_cmac_128_update(&ctx,
77                                     testarray[i].data.data,
78                                     testarray[i].data.length);
79                 aes_cmac_128_final(&ctx, cmac);
80
81                 e = memcmp(testarray[i].cmac.data, cmac, sizeof(cmac));
82                 if (e != 0) {
83                         printf("aes_cmac_128 test[%u]: failed\n", i);
84                         dump_data(0, key.data, key.length);
85                         dump_data(0, testarray[i].data.data, testarray[i].data.length);
86                         dump_data(0, testarray[i].cmac.data, testarray[i].cmac.length);
87                         dump_data(0, cmac, sizeof(cmac));
88                         ret = false;
89                 }
90         }
91         for (i=0; testarray[i].cmac.length != 0; i++) {
92                 struct aes_cmac_128_context ctx;
93                 uint8_t cmac[AES_BLOCK_SIZE];
94                 int e;
95                 size_t j;
96
97                 aes_cmac_128_init(&ctx, key.data);
98                 for (j=0; j < testarray[i].data.length; j++) {
99                         aes_cmac_128_update(&ctx, NULL, 0);
100                         aes_cmac_128_update(&ctx,
101                                             &testarray[i].data.data[j],
102                                             1);
103                         aes_cmac_128_update(&ctx, NULL, 0);
104                 }
105                 aes_cmac_128_final(&ctx, cmac);
106
107                 e = memcmp(testarray[i].cmac.data, cmac, sizeof(cmac));
108                 if (e != 0) {
109                         printf("aes_cmac_128 chunked test[%u]: failed\n", i);
110                         dump_data(0, key.data, key.length);
111                         dump_data(0, testarray[i].data.data, testarray[i].data.length);
112                         dump_data(0, testarray[i].cmac.data, testarray[i].cmac.length);
113                         dump_data(0, cmac, sizeof(cmac));
114                         ret = false;
115                 }
116         }
117         talloc_free(tctx);
118         return ret;
119 }