477f83131f
Signed-off-by: Jouni Malinen <j@w1.fi>
419 lines
12 KiB
C
419 lines
12 KiB
C
/*
|
|
* crypto module tests
|
|
* Copyright (c) 2014-2015, Jouni Malinen <j@w1.fi>
|
|
*
|
|
* This software may be distributed under the terms of the BSD license.
|
|
* See README for more details.
|
|
*/
|
|
|
|
#include "utils/includes.h"
|
|
|
|
#include "utils/common.h"
|
|
#include "crypto/aes_siv.h"
|
|
#include "crypto/aes_wrap.h"
|
|
#include "crypto/aes.h"
|
|
|
|
|
|
static int test_siv(void)
|
|
{
|
|
#ifdef CONFIG_MESH
|
|
/* RFC 5297, A.1. Deterministic Authenticated Encryption Example */
|
|
u8 key[] = {
|
|
0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8,
|
|
0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
|
|
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
|
|
0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
|
|
};
|
|
u8 ad[] = {
|
|
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
|
|
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
|
|
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27
|
|
};
|
|
u8 plaintext[] = {
|
|
0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
|
|
0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee
|
|
};
|
|
u8 iv_c[] = {
|
|
0x85, 0x63, 0x2d, 0x07, 0xc6, 0xe8, 0xf3, 0x7f,
|
|
0x95, 0x0a, 0xcd, 0x32, 0x0a, 0x2e, 0xcc, 0x93,
|
|
0x40, 0xc0, 0x2b, 0x96, 0x90, 0xc4, 0xdc, 0x04,
|
|
0xda, 0xef, 0x7f, 0x6a, 0xfe, 0x5c
|
|
};
|
|
/* RFC 5297, A.2. Nonce-Based Authenticated Encryption Example */
|
|
u8 key_2[] = {
|
|
0x7f, 0x7e, 0x7d, 0x7c, 0x7b, 0x7a, 0x79, 0x78,
|
|
0x77, 0x76, 0x75, 0x74, 0x73, 0x72, 0x71, 0x70,
|
|
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
|
|
0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
|
|
};
|
|
u8 ad1_2[] = {
|
|
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
|
|
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
|
|
0xde, 0xad, 0xda, 0xda, 0xde, 0xad, 0xda, 0xda,
|
|
0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88,
|
|
0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x00
|
|
};
|
|
u8 ad2_2[] = {
|
|
0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80,
|
|
0x90, 0xa0
|
|
};
|
|
u8 nonce_2[] = {
|
|
0x09, 0xf9, 0x11, 0x02, 0x9d, 0x74, 0xe3, 0x5b,
|
|
0xd8, 0x41, 0x56, 0xc5, 0x63, 0x56, 0x88, 0xc0
|
|
};
|
|
u8 plaintext_2[] = {
|
|
0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
|
|
0x73, 0x6f, 0x6d, 0x65, 0x20, 0x70, 0x6c, 0x61,
|
|
0x69, 0x6e, 0x74, 0x65, 0x78, 0x74, 0x20, 0x74,
|
|
0x6f, 0x20, 0x65, 0x6e, 0x63, 0x72, 0x79, 0x70,
|
|
0x74, 0x20, 0x75, 0x73, 0x69, 0x6e, 0x67, 0x20,
|
|
0x53, 0x49, 0x56, 0x2d, 0x41, 0x45, 0x53
|
|
};
|
|
u8 iv_c_2[] = {
|
|
0x7b, 0xdb, 0x6e, 0x3b, 0x43, 0x26, 0x67, 0xeb,
|
|
0x06, 0xf4, 0xd1, 0x4b, 0xff, 0x2f, 0xbd, 0x0f,
|
|
0xcb, 0x90, 0x0f, 0x2f, 0xdd, 0xbe, 0x40, 0x43,
|
|
0x26, 0x60, 0x19, 0x65, 0xc8, 0x89, 0xbf, 0x17,
|
|
0xdb, 0xa7, 0x7c, 0xeb, 0x09, 0x4f, 0xa6, 0x63,
|
|
0xb7, 0xa3, 0xf7, 0x48, 0xba, 0x8a, 0xf8, 0x29,
|
|
0xea, 0x64, 0xad, 0x54, 0x4a, 0x27, 0x2e, 0x9c,
|
|
0x48, 0x5b, 0x62, 0xa3, 0xfd, 0x5c, 0x0d
|
|
};
|
|
u8 out[2 * AES_BLOCK_SIZE + sizeof(plaintext_2)];
|
|
const u8 *addr[3];
|
|
size_t len[3];
|
|
|
|
/* RFC 5297, A.1. Deterministic Authenticated Encryption Example */
|
|
addr[0] = ad;
|
|
len[0] = sizeof(ad);
|
|
|
|
if (aes_siv_encrypt(key, plaintext, sizeof(plaintext),
|
|
1, addr, len, out)) {
|
|
wpa_printf(MSG_ERROR, "AES-SIV mode encryption failed");
|
|
return 1;
|
|
}
|
|
if (os_memcmp(out, iv_c, sizeof(iv_c)) != 0) {
|
|
wpa_printf(MSG_ERROR,
|
|
"AES-SIV mode encryption returned invalid cipher text");
|
|
return 1;
|
|
}
|
|
|
|
if (aes_siv_decrypt(key, iv_c, sizeof(iv_c), 1, addr, len, out)) {
|
|
wpa_printf(MSG_ERROR, "AES-SIV mode decryption failed");
|
|
return 1;
|
|
}
|
|
if (os_memcmp(out, plaintext, sizeof(plaintext)) != 0) {
|
|
wpa_printf(MSG_ERROR,
|
|
"AES-SIV mode decryption returned invalid plain text");
|
|
return 1;
|
|
}
|
|
|
|
/* RFC 5297, A.2. Nonce-Based Authenticated Encryption Example */
|
|
addr[0] = ad1_2;
|
|
len[0] = sizeof(ad1_2);
|
|
addr[1] = ad2_2;
|
|
len[1] = sizeof(ad2_2);
|
|
addr[2] = nonce_2;
|
|
len[2] = sizeof(nonce_2);
|
|
|
|
if (aes_siv_encrypt(key_2, plaintext_2, sizeof(plaintext_2),
|
|
3, addr, len, out)) {
|
|
wpa_printf(MSG_ERROR, "AES-SIV mode encryption failed");
|
|
return 1;
|
|
}
|
|
if (os_memcmp(out, iv_c_2, sizeof(iv_c_2)) != 0) {
|
|
wpa_printf(MSG_ERROR,
|
|
"AES-SIV mode encryption returned invalid cipher text");
|
|
return 1;
|
|
}
|
|
|
|
if (aes_siv_decrypt(key_2, iv_c_2, sizeof(iv_c_2), 3, addr, len, out)) {
|
|
wpa_printf(MSG_ERROR, "AES-SIV mode decryption failed");
|
|
return 1;
|
|
}
|
|
if (os_memcmp(out, plaintext_2, sizeof(plaintext_2)) != 0) {
|
|
wpa_printf(MSG_ERROR,
|
|
"AES-SIV mode decryption returned invalid plain text");
|
|
return 1;
|
|
}
|
|
|
|
wpa_printf(MSG_INFO, "AES-SIV test cases passed");
|
|
#endif /* CONFIG_MESH */
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
/* OMAC1 AES-128 test vectors from
|
|
* http://csrc.nist.gov/CryptoToolkit/modes/proposedmodes/omac/omac-ad.pdf
|
|
* which are same as the examples from NIST SP800-38B
|
|
* http://csrc.nist.gov/CryptoToolkit/modes/800-38_Series_Publications/SP800-38B.pdf
|
|
*/
|
|
|
|
struct omac1_test_vector {
|
|
u8 k[16];
|
|
u8 msg[64];
|
|
int msg_len;
|
|
u8 tag[16];
|
|
};
|
|
|
|
static struct omac1_test_vector omac1_test_vectors[] =
|
|
{
|
|
{
|
|
{ 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
|
|
0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c },
|
|
{ },
|
|
0,
|
|
{ 0xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28,
|
|
0x7f, 0xa3, 0x7d, 0x12, 0x9b,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2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
|
|
0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
|
|
0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
|
|
0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
|
|
0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
|
|
0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 },
|
|
64,
|
|
{ 0x51, 0xf0, 0xbe, 0xbf, 0x7e, 0x3b, 0x9d, 0x92,
|
|
0xfc, 0x49, 0x74, 0x17, 0x79, 0x36, 0x3c, 0xfe }
|
|
},
|
|
};
|
|
|
|
|
|
static int test_omac1_vector(struct omac1_test_vector *tv, unsigned int i)
|
|
{
|
|
u8 key[] = {
|
|
0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
|
|
0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
|
|
};
|
|
u8 msg[] = { 0x12, 0x34, 0x56 };
|
|
u8 result[24], result2[24];
|
|
const u8 *addr[3];
|
|
size_t len[3];
|
|
|
|
if (omac1_aes_128(tv->k, tv->msg, tv->msg_len, result) ||
|
|
os_memcmp(result, tv->tag, 16) != 0) {
|
|
wpa_printf(MSG_ERROR, "OMAC1-AES-128 test vector %u failed", i);
|
|
return 1;
|
|
}
|
|
|
|
if (tv->msg_len > 1) {
|
|
|
|
addr[0] = tv->msg;
|
|
len[0] = 1;
|
|
addr[1] = tv->msg + 1;
|
|
len[1] = tv->msg_len - 1;
|
|
|
|
if (omac1_aes_128_vector(tv->k, 2, addr, len, result) ||
|
|
os_memcmp(result, tv->tag, 16) != 0) {
|
|
wpa_printf(MSG_ERROR,
|
|
"OMAC1-AES-128(vector) test vector %u failed",
|
|
i);
|
|
return 1;
|
|
}
|
|
|
|
addr[0] = tv->msg;
|
|
len[0] = tv->msg_len - 2;
|
|
addr[1] = tv->msg + tv->msg_len - 2;
|
|
len[1] = 1;
|
|
addr[2] = tv->msg + tv->msg_len - 1;
|
|
len[2] = 1;
|
|
|
|
if (omac1_aes_128_vector(tv->k, 3, addr, len, result) ||
|
|
os_memcmp(result, tv->tag, 16) != 0) {
|
|
wpa_printf(MSG_ERROR,
|
|
"OMAC1-AES-128(vector2) test vector %u failed",
|
|
i);
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
addr[0] = &msg[0];
|
|
len[0] = 1;
|
|
addr[1] = &msg[1];
|
|
len[1] = 1;
|
|
addr[2] = &msg[2];
|
|
len[2] = 1;
|
|
if (omac1_aes_128(key, msg, sizeof(msg), result) ||
|
|
omac1_aes_128_vector(key, 3, addr, len, result2) ||
|
|
os_memcmp(result, result2, 16) != 0) {
|
|
wpa_printf(MSG_ERROR, "OMAC1-AES-128 short test mismatch");
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
static int test_omac1(void)
|
|
{
|
|
unsigned int i;
|
|
|
|
for (i = 0; i < ARRAY_SIZE(omac1_test_vectors); i++) {
|
|
if (test_omac1_vector(&omac1_test_vectors[i], i))
|
|
return 1;
|
|
}
|
|
|
|
wpa_printf(MSG_INFO, "OMAC1-AES-128 test cases passed");
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
static int test_eax(void)
|
|
{
|
|
#ifdef EAP_PSK
|
|
u8 msg[] = { 0xF7, 0xFB };
|
|
u8 key[] = { 0x91, 0x94, 0x5D, 0x3F, 0x4D, 0xCB, 0xEE, 0x0B,
|
|
0xF4, 0x5E, 0xF5, 0x22, 0x55, 0xF0, 0x95, 0xA4 };
|
|
u8 nonce[] = { 0xBE, 0xCA, 0xF0, 0x43, 0xB0, 0xA2, 0x3D, 0x84,
|
|
0x31, 0x94, 0xBA, 0x97, 0x2C, 0x66, 0xDE, 0xBD };
|
|
u8 hdr[] = { 0xFA, 0x3B, 0xFD, 0x48, 0x06, 0xEB, 0x53, 0xFA };
|
|
u8 cipher[] = { 0x19, 0xDD, 0x5C, 0x4C, 0x93, 0x31, 0x04, 0x9D,
|
|
0x0B, 0xDA, 0xB0, 0x27, 0x74, 0x08, 0xF6, 0x79,
|
|
0x67, 0xE5 };
|
|
u8 data[sizeof(msg)], tag[AES_BLOCK_SIZE];
|
|
|
|
os_memcpy(data, msg, sizeof(msg));
|
|
if (aes_128_eax_encrypt(key, nonce, sizeof(nonce), hdr, sizeof(hdr),
|
|
data, sizeof(data), tag)) {
|
|
wpa_printf(MSG_ERROR, "AES-128 EAX mode encryption failed");
|
|
return 1;
|
|
}
|
|
if (os_memcmp(data, cipher, sizeof(data)) != 0) {
|
|
wpa_printf(MSG_ERROR,
|
|
"AES-128 EAX mode encryption returned invalid cipher text");
|
|
return 1;
|
|
}
|
|
if (os_memcmp(tag, cipher + sizeof(data), AES_BLOCK_SIZE) != 0) {
|
|
wpa_printf(MSG_ERROR,
|
|
"AES-128 EAX mode encryption returned invalid tag");
|
|
return 1;
|
|
}
|
|
|
|
if (aes_128_eax_decrypt(key, nonce, sizeof(nonce), hdr, sizeof(hdr),
|
|
data, sizeof(data), tag)) {
|
|
wpa_printf(MSG_ERROR, "AES-128 EAX mode decryption failed");
|
|
return 1;
|
|
}
|
|
if (os_memcmp(data, msg, sizeof(data)) != 0) {
|
|
wpa_printf(MSG_ERROR,
|
|
"AES-128 EAX mode decryption returned invalid plain text");
|
|
return 1;
|
|
}
|
|
|
|
wpa_printf(MSG_INFO, "AES-128 EAX mode test cases passed");
|
|
#endif /* EAP_PSK */
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
static int test_cbc(void)
|
|
{
|
|
struct cbc_test_vector {
|
|
u8 key[16];
|
|
u8 iv[16];
|
|
u8 plain[32];
|
|
u8 cipher[32];
|
|
size_t len;
|
|
} vectors[] = {
|
|
{
|
|
{ 0x06, 0xa9, 0x21, 0x40, 0x36, 0xb8, 0xa1, 0x5b,
|
|
0x51, 0x2e, 0x03, 0xd5, 0x34, 0x12, 0x00, 0x06 },
|
|
{ 0x3d, 0xaf, 0xba, 0x42, 0x9d, 0x9e, 0xb4, 0x30,
|
|
0xb4, 0x22, 0xda, 0x80, 0x2c, 0x9f, 0xac, 0x41 },
|
|
"Single block msg",
|
|
{ 0xe3, 0x53, 0x77, 0x9c, 0x10, 0x79, 0xae, 0xb8,
|
|
0x27, 0x08, 0x94, 0x2d, 0xbe, 0x77, 0x18, 0x1a },
|
|
16
|
|
},
|
|
{
|
|
{ 0xc2, 0x86, 0x69, 0x6d, 0x88, 0x7c, 0x9a, 0xa0,
|
|
0x61, 0x1b, 0xbb, 0x3e, 0x20, 0x25, 0xa4, 0x5a },
|
|
{ 0x56, 0x2e, 0x17, 0x99, 0x6d, 0x09, 0x3d, 0x28,
|
|
0xdd, 0xb3, 0xba, 0x69, 0x5a, 0x2e, 0x6f, 0x58 },
|
|
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
|
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
|
|
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
|
|
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f },
|
|
{ 0xd2, 0x96, 0xcd, 0x94, 0xc2, 0xcc, 0xcf, 0x8a,
|
|
0x3a, 0x86, 0x30, 0x28, 0xb5, 0xe1, 0xdc, 0x0a,
|
|
0x75, 0x86, 0x60, 0x2d, 0x25, 0x3c, 0xff, 0xf9,
|
|
0x1b, 0x82, 0x66, 0xbe, 0xa6, 0xd6, 0x1a, 0xb1 },
|
|
32
|
|
}
|
|
};
|
|
int ret = 0;
|
|
u8 *buf;
|
|
unsigned int i;
|
|
|
|
for (i = 0; i < ARRAY_SIZE(vectors); i++) {
|
|
struct cbc_test_vector *tv = &vectors[i];
|
|
|
|
buf = os_malloc(tv->len);
|
|
if (buf == NULL) {
|
|
ret++;
|
|
break;
|
|
}
|
|
|
|
os_memcpy(buf, tv->plain, tv->len);
|
|
if (aes_128_cbc_encrypt(tv->key, tv->iv, buf, tv->len) ||
|
|
os_memcmp(buf, tv->cipher, tv->len) != 0) {
|
|
wpa_printf(MSG_ERROR, "AES-CBC encrypt %d failed", i);
|
|
ret++;
|
|
}
|
|
|
|
os_memcpy(buf, tv->cipher, tv->len);
|
|
if (aes_128_cbc_decrypt(tv->key, tv->iv, buf, tv->len) ||
|
|
os_memcmp(buf, tv->plain, tv->len) != 0) {
|
|
wpa_printf(MSG_ERROR, "AES-CBC decrypt %d failed", i);
|
|
ret++;
|
|
}
|
|
|
|
os_free(buf);
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
|
|
int crypto_module_tests(void)
|
|
{
|
|
int ret = 0;
|
|
|
|
wpa_printf(MSG_INFO, "crypto module tests");
|
|
if (test_siv() ||
|
|
test_omac1() ||
|
|
test_eax() ||
|
|
test_cbc())
|
|
ret = -1;
|
|
|
|
return ret;
|
|
}
|