build: Build with system md5.h on OpenIndiana
[samba.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/crypto.h"
21
22 struct torture_context;
23 bool torture_local_crypto_aes_cmac_128(struct torture_context *torture);
24
25 /*
26  This uses the test values from rfc 4493
27 */
28 bool torture_local_crypto_aes_cmac_128(struct torture_context *torture)
29 {
30         bool ret = true;
31         uint32_t i;
32         DATA_BLOB key;
33         struct {
34                 DATA_BLOB data;
35                 DATA_BLOB cmac;
36         } testarray[5];
37
38         TALLOC_CTX *tctx = talloc_new(torture);
39         if (!tctx) { return false; };
40
41         key = strhex_to_data_blob(tctx, "2b7e151628aed2a6abf7158809cf4f3c");
42
43         testarray[0].data = data_blob_null;
44         testarray[0].cmac = strhex_to_data_blob(tctx,
45                                 "bb1d6929e95937287fa37d129b756746");
46
47         testarray[1].data = strhex_to_data_blob(tctx,
48                                 "6bc1bee22e409f96e93d7e117393172a");
49         testarray[1].cmac = strhex_to_data_blob(tctx,
50                                 "070a16b46b4d4144f79bdd9dd04a287c");
51
52         testarray[2].data = strhex_to_data_blob(tctx,
53                                 "6bc1bee22e409f96e93d7e117393172a"
54                                 "ae2d8a571e03ac9c9eb76fac45af8e51"
55                                 "30c81c46a35ce411");
56         testarray[2].cmac = strhex_to_data_blob(tctx,
57                                 "dfa66747de9ae63030ca32611497c827");
58
59         testarray[3].data = strhex_to_data_blob(tctx,
60                                 "6bc1bee22e409f96e93d7e117393172a"
61                                 "ae2d8a571e03ac9c9eb76fac45af8e51"
62                                 "30c81c46a35ce411e5fbc1191a0a52ef"
63                                 "f69f2445df4f9b17ad2b417be66c3710");
64         testarray[3].cmac = strhex_to_data_blob(tctx,
65                                 "51f0bebf7e3b9d92fc49741779363cfe");
66
67         ZERO_STRUCT(testarray[4]);
68
69         for (i=0; testarray[i].cmac.length != 0; i++) {
70                 struct aes_cmac_128_context ctx;
71                 uint8_t cmac[AES_BLOCK_SIZE];
72                 int e;
73
74                 aes_cmac_128_init(&ctx, key.data);
75                 aes_cmac_128_update(&ctx,
76                                     testarray[i].data.data,
77                                     testarray[i].data.length);
78                 aes_cmac_128_final(&ctx, cmac);
79
80                 e = memcmp(testarray[i].cmac.data, cmac, sizeof(cmac));
81                 if (e != 0) {
82                         printf("aes_cmac_128 test[%u]: failed\n", i);
83                         dump_data(0, key.data, key.length);
84                         dump_data(0, testarray[i].data.data, testarray[i].data.length);
85                         dump_data(0, testarray[i].cmac.data, testarray[i].cmac.length);
86                         dump_data(0, cmac, sizeof(cmac));
87                         ret = false;
88                 }
89         }
90         talloc_free(tctx);
91         return ret;
92 }