Imported Upstream version 1.15.1
[platform/upstream/krb5.git] / src / lib / crypto / crypto_tests / t_cmac.c
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/crypto/crypto_tests/t_cmac.c */
3 /*
4  * Copyright 2010 by the Massachusetts Institute of Technology.
5  * All Rights Reserved.
6  *
7  * Export of this software from the United States of America may
8  *   require a specific license from the United States Government.
9  *   It is the responsibility of any person or organization contemplating
10  *   export to obtain such a license before exporting.
11  *
12  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13  * distribute this software and its documentation for any purpose and
14  * without fee is hereby granted, provided that the above copyright
15  * notice appear in all copies and that both that copyright notice and
16  * this permission notice appear in supporting documentation, and that
17  * the name of M.I.T. not be used in advertising or publicity pertaining
18  * to distribution of the software without specific, written prior
19  * permission.  Furthermore if you modify this software you must label
20  * your software as modified software and not distribute it in such a
21  * fashion that it might be confused with the original M.I.T. software.
22  * M.I.T. makes no representations about the suitability of
23  * this software for any purpose.  It is provided "as is" without express
24  * or implied warranty.
25  */
26
27 /*
28  * Test vectors for CMAC.  Inputs are taken from RFC 4493 section 4.  Outputs
29  * are changed for the use of Camellia-128 in place of AES-128.
30  *
31  * Ideally we would double-check subkey values, but we have no easy way to see
32  * them.
33  *
34  * Ideally we would test AES-CMAC against the expected results in RFC 4493,
35  * instead of Camellia-CMAC against results we generated ourselves.  This has
36  * been done manually, but is not convenient to do automatically since the
37  * AES-128 enc provider has no cbc_mac method and therefore cannot be used with
38  * krb5int_cmac_checksum.
39  */
40
41 #include "crypto_int.h"
42
43 /* All examples use the following Camellia-128 key. */
44 static unsigned char keybytes[] = {
45     0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
46     0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
47 };
48
49 /* Example inputs are this message truncated to 0, 16, 40, and 64 bytes. */
50 unsigned char input[] = {
51     0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
52     0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
53     0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
54     0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
55     0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
56     0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
57     0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
58     0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10
59 };
60
61 /* Expected result of CMAC on empty input. */
62 static unsigned char cmac1[] = {
63     0xba, 0x92, 0x57, 0x82, 0xaa, 0xa1, 0xf5, 0xd9,
64     0xa0, 0x0f, 0x89, 0x64, 0x80, 0x94, 0xfc, 0x71
65 };
66
67 /* Expected result of CMAC on first 16 bytes of input. */
68 static unsigned char cmac2[] = {
69     0x6d, 0x96, 0x28, 0x54, 0xa3, 0xb9, 0xfd, 0xa5,
70     0x6d, 0x7d, 0x45, 0xa9, 0x5e, 0xe1, 0x79, 0x93
71 };
72
73 /* Expected result of CMAC on first 40 bytes of input. */
74 static unsigned char cmac3[] = {
75     0x5c, 0x18, 0xd1, 0x19, 0xcc, 0xd6, 0x76, 0x61,
76     0x44, 0xac, 0x18, 0x66, 0x13, 0x1d, 0x9f, 0x22
77 };
78
79 /* Expected result of CMAC on all 64 bytes of input. */
80 static unsigned char cmac4[] = {
81     0xc2, 0x69, 0x9a, 0x6e, 0xba, 0x55, 0xce, 0x9d,
82     0x93, 0x9a, 0x8a, 0x4e, 0x19, 0x46, 0x6e, 0xe9
83 };
84
85 static void
86 check_result(const char *name, const unsigned char *result,
87              const unsigned char *expected)
88 {
89     int i;
90
91     for (i = 0; i < 16; i++) {
92         if (result[i] != expected[i]) {
93             fprintf(stderr, "CMAC test vector failure: %s\n", name);
94             exit(1);
95         }
96     }
97 }
98
99 int
100 main(int argc, char **argv)
101 {
102     krb5_error_code ret;
103     krb5_context context = NULL;
104     krb5_keyblock keyblock;
105     krb5_key key;
106     const struct krb5_enc_provider *enc = &krb5int_enc_camellia128;
107     krb5_crypto_iov iov;
108     unsigned char resultbuf[16];
109     krb5_data result = make_data(resultbuf, 16);
110
111     /* Create the example key. */
112     keyblock.magic = KV5M_KEYBLOCK;
113     keyblock.enctype = ENCTYPE_CAMELLIA128_CTS_CMAC;
114     keyblock.length = 16;
115     keyblock.contents = keybytes;
116     ret = krb5_k_create_key(context, &keyblock, &key);
117     assert(!ret);
118
119     /* Example 1. */
120     iov.flags = KRB5_CRYPTO_TYPE_DATA;
121     iov.data = make_data(input, 0);
122     ret = krb5int_cmac_checksum(enc, key, &iov, 1, &result);
123     assert(!ret);
124     check_result("example 1", resultbuf, cmac1);
125
126     /* Example 2. */
127     iov.data.length = 16;
128     ret = krb5int_cmac_checksum(enc, key, &iov, 1, &result);
129     assert(!ret);
130     check_result("example 2", resultbuf, cmac2);
131
132     /* Example 3. */
133     iov.data.length = 40;
134     ret = krb5int_cmac_checksum(enc, key, &iov, 1, &result);
135     assert(!ret);
136     check_result("example 3", resultbuf, cmac3);
137
138     /* Example 4. */
139     iov.data.length = 64;
140     ret = krb5int_cmac_checksum(enc, key, &iov, 1, &result);
141     assert(!ret);
142     check_result("example 4", resultbuf, cmac4);
143
144     printf("All CMAC tests passed.\n");
145     krb5_k_free_key(context, key);
146     return 0;
147 }