1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert *
4*e0c4386eSCy Schubert * Licensed under the Apache License 2.0 (the "License"). You may not use
5*e0c4386eSCy Schubert * this file except in compliance with the License. You can obtain a copy
6*e0c4386eSCy Schubert * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert */
9*e0c4386eSCy Schubert
10*e0c4386eSCy Schubert /*
11*e0c4386eSCy Schubert * CMAC low level APIs are deprecated for public use, but still ok for internal
12*e0c4386eSCy Schubert * use.
13*e0c4386eSCy Schubert */
14*e0c4386eSCy Schubert #include "internal/deprecated.h"
15*e0c4386eSCy Schubert
16*e0c4386eSCy Schubert #include <stdio.h>
17*e0c4386eSCy Schubert #include <string.h>
18*e0c4386eSCy Schubert #include <stdlib.h>
19*e0c4386eSCy Schubert
20*e0c4386eSCy Schubert #include "internal/nelem.h"
21*e0c4386eSCy Schubert
22*e0c4386eSCy Schubert #include <openssl/cmac.h>
23*e0c4386eSCy Schubert #include <openssl/aes.h>
24*e0c4386eSCy Schubert #include <openssl/evp.h>
25*e0c4386eSCy Schubert
26*e0c4386eSCy Schubert #include "testutil.h"
27*e0c4386eSCy Schubert
28*e0c4386eSCy Schubert static const char xtskey[32] = {
29*e0c4386eSCy Schubert 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
30*e0c4386eSCy Schubert 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
31*e0c4386eSCy Schubert 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
32*e0c4386eSCy Schubert };
33*e0c4386eSCy Schubert
34*e0c4386eSCy Schubert static struct test_st {
35*e0c4386eSCy Schubert const char key[32];
36*e0c4386eSCy Schubert int key_len;
37*e0c4386eSCy Schubert const unsigned char data[64];
38*e0c4386eSCy Schubert int data_len;
39*e0c4386eSCy Schubert const char *mac;
40*e0c4386eSCy Schubert } test[3] = {
41*e0c4386eSCy Schubert {
42*e0c4386eSCy Schubert {
43*e0c4386eSCy Schubert 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
44*e0c4386eSCy Schubert 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
45*e0c4386eSCy Schubert },
46*e0c4386eSCy Schubert 16,
47*e0c4386eSCy Schubert "My test data",
48*e0c4386eSCy Schubert 12,
49*e0c4386eSCy Schubert "29cec977c48f63c200bd5c4a6881b224"
50*e0c4386eSCy Schubert },
51*e0c4386eSCy Schubert {
52*e0c4386eSCy Schubert {
53*e0c4386eSCy Schubert 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
54*e0c4386eSCy Schubert 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
55*e0c4386eSCy Schubert 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
56*e0c4386eSCy Schubert },
57*e0c4386eSCy Schubert 32,
58*e0c4386eSCy Schubert "My test data",
59*e0c4386eSCy Schubert 12,
60*e0c4386eSCy Schubert "db6493aa04e4761f473b2b453c031c9a"
61*e0c4386eSCy Schubert },
62*e0c4386eSCy Schubert {
63*e0c4386eSCy Schubert {
64*e0c4386eSCy Schubert 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
65*e0c4386eSCy Schubert 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
66*e0c4386eSCy Schubert 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
67*e0c4386eSCy Schubert },
68*e0c4386eSCy Schubert 32,
69*e0c4386eSCy Schubert "My test data again",
70*e0c4386eSCy Schubert 18,
71*e0c4386eSCy Schubert "65c11c75ecf590badd0a5e56cbb8af60"
72*e0c4386eSCy Schubert },
73*e0c4386eSCy Schubert };
74*e0c4386eSCy Schubert
75*e0c4386eSCy Schubert static char *pt(unsigned char *md, unsigned int len);
76*e0c4386eSCy Schubert
test_cmac_bad(void)77*e0c4386eSCy Schubert static int test_cmac_bad(void)
78*e0c4386eSCy Schubert {
79*e0c4386eSCy Schubert CMAC_CTX *ctx = NULL;
80*e0c4386eSCy Schubert int ret = 0;
81*e0c4386eSCy Schubert
82*e0c4386eSCy Schubert ctx = CMAC_CTX_new();
83*e0c4386eSCy Schubert if (!TEST_ptr(ctx)
84*e0c4386eSCy Schubert || !TEST_false(CMAC_Init(ctx, NULL, 0, NULL, NULL))
85*e0c4386eSCy Schubert || !TEST_false(CMAC_Update(ctx, test[0].data, test[0].data_len))
86*e0c4386eSCy Schubert /* Should be able to pass cipher first, and then key */
87*e0c4386eSCy Schubert || !TEST_true(CMAC_Init(ctx, NULL, 0, EVP_aes_128_cbc(), NULL))
88*e0c4386eSCy Schubert /* Must have a key */
89*e0c4386eSCy Schubert || !TEST_false(CMAC_Update(ctx, test[0].data, test[0].data_len))
90*e0c4386eSCy Schubert /* Now supply the key */
91*e0c4386eSCy Schubert || !TEST_true(CMAC_Init(ctx, test[0].key, test[0].key_len, NULL, NULL))
92*e0c4386eSCy Schubert /* Update should now work */
93*e0c4386eSCy Schubert || !TEST_true(CMAC_Update(ctx, test[0].data, test[0].data_len))
94*e0c4386eSCy Schubert /* XTS is not a suitable cipher to use */
95*e0c4386eSCy Schubert || !TEST_false(CMAC_Init(ctx, xtskey, sizeof(xtskey), EVP_aes_128_xts(),
96*e0c4386eSCy Schubert NULL))
97*e0c4386eSCy Schubert || !TEST_false(CMAC_Update(ctx, test[0].data, test[0].data_len)))
98*e0c4386eSCy Schubert goto err;
99*e0c4386eSCy Schubert
100*e0c4386eSCy Schubert ret = 1;
101*e0c4386eSCy Schubert err:
102*e0c4386eSCy Schubert CMAC_CTX_free(ctx);
103*e0c4386eSCy Schubert return ret;
104*e0c4386eSCy Schubert }
105*e0c4386eSCy Schubert
test_cmac_run(void)106*e0c4386eSCy Schubert static int test_cmac_run(void)
107*e0c4386eSCy Schubert {
108*e0c4386eSCy Schubert char *p;
109*e0c4386eSCy Schubert CMAC_CTX *ctx = NULL;
110*e0c4386eSCy Schubert unsigned char buf[AES_BLOCK_SIZE];
111*e0c4386eSCy Schubert size_t len;
112*e0c4386eSCy Schubert int ret = 0;
113*e0c4386eSCy Schubert
114*e0c4386eSCy Schubert ctx = CMAC_CTX_new();
115*e0c4386eSCy Schubert
116*e0c4386eSCy Schubert if (!TEST_true(CMAC_Init(ctx, test[0].key, test[0].key_len,
117*e0c4386eSCy Schubert EVP_aes_128_cbc(), NULL))
118*e0c4386eSCy Schubert || !TEST_true(CMAC_Update(ctx, test[0].data, test[0].data_len))
119*e0c4386eSCy Schubert || !TEST_true(CMAC_Final(ctx, buf, &len)))
120*e0c4386eSCy Schubert goto err;
121*e0c4386eSCy Schubert
122*e0c4386eSCy Schubert p = pt(buf, len);
123*e0c4386eSCy Schubert if (!TEST_str_eq(p, test[0].mac))
124*e0c4386eSCy Schubert goto err;
125*e0c4386eSCy Schubert
126*e0c4386eSCy Schubert if (!TEST_true(CMAC_Init(ctx, test[1].key, test[1].key_len,
127*e0c4386eSCy Schubert EVP_aes_256_cbc(), NULL))
128*e0c4386eSCy Schubert || !TEST_true(CMAC_Update(ctx, test[1].data, test[1].data_len))
129*e0c4386eSCy Schubert || !TEST_true(CMAC_Final(ctx, buf, &len)))
130*e0c4386eSCy Schubert goto err;
131*e0c4386eSCy Schubert
132*e0c4386eSCy Schubert p = pt(buf, len);
133*e0c4386eSCy Schubert if (!TEST_str_eq(p, test[1].mac))
134*e0c4386eSCy Schubert goto err;
135*e0c4386eSCy Schubert
136*e0c4386eSCy Schubert if (!TEST_true(CMAC_Init(ctx, test[2].key, test[2].key_len, NULL, NULL))
137*e0c4386eSCy Schubert || !TEST_true(CMAC_Update(ctx, test[2].data, test[2].data_len))
138*e0c4386eSCy Schubert || !TEST_true(CMAC_Final(ctx, buf, &len)))
139*e0c4386eSCy Schubert goto err;
140*e0c4386eSCy Schubert p = pt(buf, len);
141*e0c4386eSCy Schubert if (!TEST_str_eq(p, test[2].mac))
142*e0c4386eSCy Schubert goto err;
143*e0c4386eSCy Schubert /* Test reusing a key */
144*e0c4386eSCy Schubert if (!TEST_true(CMAC_Init(ctx, NULL, 0, NULL, NULL))
145*e0c4386eSCy Schubert || !TEST_true(CMAC_Update(ctx, test[2].data, test[2].data_len))
146*e0c4386eSCy Schubert || !TEST_true(CMAC_Final(ctx, buf, &len)))
147*e0c4386eSCy Schubert goto err;
148*e0c4386eSCy Schubert p = pt(buf, len);
149*e0c4386eSCy Schubert if (!TEST_str_eq(p, test[2].mac))
150*e0c4386eSCy Schubert goto err;
151*e0c4386eSCy Schubert
152*e0c4386eSCy Schubert /* Test setting the cipher and key separately */
153*e0c4386eSCy Schubert if (!TEST_true(CMAC_Init(ctx, NULL, 0, EVP_aes_256_cbc(), NULL))
154*e0c4386eSCy Schubert || !TEST_true(CMAC_Init(ctx, test[2].key, test[2].key_len, NULL, NULL))
155*e0c4386eSCy Schubert || !TEST_true(CMAC_Update(ctx, test[2].data, test[2].data_len))
156*e0c4386eSCy Schubert || !TEST_true(CMAC_Final(ctx, buf, &len)))
157*e0c4386eSCy Schubert goto err;
158*e0c4386eSCy Schubert p = pt(buf, len);
159*e0c4386eSCy Schubert if (!TEST_str_eq(p, test[2].mac))
160*e0c4386eSCy Schubert goto err;
161*e0c4386eSCy Schubert
162*e0c4386eSCy Schubert ret = 1;
163*e0c4386eSCy Schubert err:
164*e0c4386eSCy Schubert CMAC_CTX_free(ctx);
165*e0c4386eSCy Schubert return ret;
166*e0c4386eSCy Schubert }
167*e0c4386eSCy Schubert
test_cmac_copy(void)168*e0c4386eSCy Schubert static int test_cmac_copy(void)
169*e0c4386eSCy Schubert {
170*e0c4386eSCy Schubert char *p;
171*e0c4386eSCy Schubert CMAC_CTX *ctx = NULL, *ctx2 = NULL;
172*e0c4386eSCy Schubert unsigned char buf[AES_BLOCK_SIZE];
173*e0c4386eSCy Schubert size_t len;
174*e0c4386eSCy Schubert int ret = 0;
175*e0c4386eSCy Schubert
176*e0c4386eSCy Schubert ctx = CMAC_CTX_new();
177*e0c4386eSCy Schubert ctx2 = CMAC_CTX_new();
178*e0c4386eSCy Schubert if (!TEST_ptr(ctx) || !TEST_ptr(ctx2))
179*e0c4386eSCy Schubert goto err;
180*e0c4386eSCy Schubert
181*e0c4386eSCy Schubert if (!TEST_true(CMAC_Init(ctx, test[0].key, test[0].key_len,
182*e0c4386eSCy Schubert EVP_aes_128_cbc(), NULL))
183*e0c4386eSCy Schubert || !TEST_true(CMAC_Update(ctx, test[0].data, test[0].data_len))
184*e0c4386eSCy Schubert || !TEST_true(CMAC_CTX_copy(ctx2, ctx))
185*e0c4386eSCy Schubert || !TEST_true(CMAC_Final(ctx2, buf, &len)))
186*e0c4386eSCy Schubert goto err;
187*e0c4386eSCy Schubert
188*e0c4386eSCy Schubert p = pt(buf, len);
189*e0c4386eSCy Schubert if (!TEST_str_eq(p, test[0].mac))
190*e0c4386eSCy Schubert goto err;
191*e0c4386eSCy Schubert
192*e0c4386eSCy Schubert ret = 1;
193*e0c4386eSCy Schubert err:
194*e0c4386eSCy Schubert CMAC_CTX_free(ctx2);
195*e0c4386eSCy Schubert CMAC_CTX_free(ctx);
196*e0c4386eSCy Schubert return ret;
197*e0c4386eSCy Schubert }
198*e0c4386eSCy Schubert
pt(unsigned char * md,unsigned int len)199*e0c4386eSCy Schubert static char *pt(unsigned char *md, unsigned int len)
200*e0c4386eSCy Schubert {
201*e0c4386eSCy Schubert unsigned int i;
202*e0c4386eSCy Schubert static char buf[80];
203*e0c4386eSCy Schubert
204*e0c4386eSCy Schubert for (i = 0; i < len; i++)
205*e0c4386eSCy Schubert sprintf(&(buf[i * 2]), "%02x", md[i]);
206*e0c4386eSCy Schubert return buf;
207*e0c4386eSCy Schubert }
208*e0c4386eSCy Schubert
setup_tests(void)209*e0c4386eSCy Schubert int setup_tests(void)
210*e0c4386eSCy Schubert {
211*e0c4386eSCy Schubert ADD_TEST(test_cmac_bad);
212*e0c4386eSCy Schubert ADD_TEST(test_cmac_run);
213*e0c4386eSCy Schubert ADD_TEST(test_cmac_copy);
214*e0c4386eSCy Schubert return 1;
215*e0c4386eSCy Schubert }
216*e0c4386eSCy Schubert
217