]>
git.ipfire.org Git - thirdparty/openssl.git/blob - test/cmactest.c
2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 * CMAC low level APIs are deprecated for public use, but still ok for internal
14 #include "internal/deprecated.h"
20 #include "internal/nelem.h"
22 #include <openssl/cmac.h>
23 #include <openssl/aes.h>
24 #include <openssl/evp.h>
28 static const char xtskey
[32] = {
29 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
30 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
31 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
34 static struct test_st
{
37 const unsigned char data
[64];
43 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
44 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
49 "29cec977c48f63c200bd5c4a6881b224"
53 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
54 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
55 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
60 "db6493aa04e4761f473b2b453c031c9a"
64 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
65 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
66 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
71 "65c11c75ecf590badd0a5e56cbb8af60"
75 static char *pt(unsigned char *md
, unsigned int len
);
77 static int test_cmac_bad(void)
84 || !TEST_false(CMAC_Init(ctx
, NULL
, 0, NULL
, NULL
))
85 || !TEST_false(CMAC_Update(ctx
, test
[0].data
, test
[0].data_len
))
86 /* Should be able to pass cipher first, and then key */
87 || !TEST_true(CMAC_Init(ctx
, NULL
, 0, EVP_aes_128_cbc(), NULL
))
89 || !TEST_false(CMAC_Update(ctx
, test
[0].data
, test
[0].data_len
))
90 /* Now supply the key */
91 || !TEST_true(CMAC_Init(ctx
, test
[0].key
, test
[0].key_len
, NULL
, NULL
))
92 /* Update should now work */
93 || !TEST_true(CMAC_Update(ctx
, test
[0].data
, test
[0].data_len
))
94 /* XTS is not a suitable cipher to use */
95 || !TEST_false(CMAC_Init(ctx
, xtskey
, sizeof(xtskey
), EVP_aes_128_xts(),
97 || !TEST_false(CMAC_Update(ctx
, test
[0].data
, test
[0].data_len
)))
106 static int test_cmac_run(void)
109 CMAC_CTX
*ctx
= NULL
;
110 unsigned char buf
[AES_BLOCK_SIZE
];
114 ctx
= CMAC_CTX_new();
116 if (!TEST_true(CMAC_Init(ctx
, test
[0].key
, test
[0].key_len
,
117 EVP_aes_128_cbc(), NULL
))
118 || !TEST_true(CMAC_Update(ctx
, test
[0].data
, test
[0].data_len
))
119 || !TEST_true(CMAC_Final(ctx
, buf
, &len
)))
123 if (!TEST_str_eq(p
, test
[0].mac
))
126 if (!TEST_true(CMAC_Init(ctx
, test
[1].key
, test
[1].key_len
,
127 EVP_aes_256_cbc(), NULL
))
128 || !TEST_true(CMAC_Update(ctx
, test
[1].data
, test
[1].data_len
))
129 || !TEST_true(CMAC_Final(ctx
, buf
, &len
)))
133 if (!TEST_str_eq(p
, test
[1].mac
))
136 if (!TEST_true(CMAC_Init(ctx
, test
[2].key
, test
[2].key_len
, NULL
, NULL
))
137 || !TEST_true(CMAC_Update(ctx
, test
[2].data
, test
[2].data_len
))
138 || !TEST_true(CMAC_Final(ctx
, buf
, &len
)))
141 if (!TEST_str_eq(p
, test
[2].mac
))
143 /* Test reusing a key */
144 if (!TEST_true(CMAC_Init(ctx
, NULL
, 0, NULL
, NULL
))
145 || !TEST_true(CMAC_Update(ctx
, test
[2].data
, test
[2].data_len
))
146 || !TEST_true(CMAC_Final(ctx
, buf
, &len
)))
149 if (!TEST_str_eq(p
, test
[2].mac
))
152 /* Test setting the cipher and key separately */
153 if (!TEST_true(CMAC_Init(ctx
, NULL
, 0, EVP_aes_256_cbc(), NULL
))
154 || !TEST_true(CMAC_Init(ctx
, test
[2].key
, test
[2].key_len
, NULL
, NULL
))
155 || !TEST_true(CMAC_Update(ctx
, test
[2].data
, test
[2].data_len
))
156 || !TEST_true(CMAC_Final(ctx
, buf
, &len
)))
159 if (!TEST_str_eq(p
, test
[2].mac
))
168 static int test_cmac_copy(void)
171 CMAC_CTX
*ctx
= NULL
, *ctx2
= NULL
;
172 unsigned char buf
[AES_BLOCK_SIZE
];
176 ctx
= CMAC_CTX_new();
177 ctx2
= CMAC_CTX_new();
178 if (!TEST_ptr(ctx
) || !TEST_ptr(ctx2
))
181 if (!TEST_true(CMAC_Init(ctx
, test
[0].key
, test
[0].key_len
,
182 EVP_aes_128_cbc(), NULL
))
183 || !TEST_true(CMAC_Update(ctx
, test
[0].data
, test
[0].data_len
))
184 || !TEST_true(CMAC_CTX_copy(ctx2
, ctx
))
185 || !TEST_true(CMAC_Final(ctx2
, buf
, &len
)))
189 if (!TEST_str_eq(p
, test
[0].mac
))
199 static char *pt(unsigned char *md
, unsigned int len
)
204 for (i
= 0; i
< len
; i
++)
205 sprintf(&(buf
[i
* 2]), "%02x", md
[i
]);
209 int setup_tests(void)
211 ADD_TEST(test_cmac_bad
);
212 ADD_TEST(test_cmac_run
);
213 ADD_TEST(test_cmac_copy
);