Skip to content
Open
38 changes: 28 additions & 10 deletions testing/drivers/crypto/3descbc.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
* Included Files
****************************************************************************/

#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <setjmp.h>
#include <cmocka.h>

#include <sys/types.h>
#include <sys/param.h>
#include <sys/ioctl.h>
Expand All @@ -41,7 +47,7 @@
#include <unistd.h>

static int syscrypt(FAR const unsigned char *key, size_t klen,
FAR const unsigned char *iv,
FAR const unsigned char *iv, size_t ivlen,
FAR const unsigned char *in, FAR unsigned char *out,
size_t len, int encrypt)
{
Expand All @@ -66,6 +72,7 @@ static int syscrypt(FAR const unsigned char *key, size_t klen,
session.cipher = CRYPTO_3DES_CBC;
session.key = (caddr_t) key;
session.keylen = klen;
session.op = encrypt ? COP_ENCRYPT : COP_DECRYPT;
if (ioctl(cryptodev_fd, CIOCGSESSION, &session) == -1)
{
warn("CIOCGSESSION");
Expand All @@ -77,6 +84,8 @@ static int syscrypt(FAR const unsigned char *key, size_t klen,
cryp.op = encrypt ? COP_ENCRYPT : COP_DECRYPT;
cryp.flags = 0;
cryp.len = len;
cryp.olen = len;
cryp.ivlen = ivlen;
cryp.src = (caddr_t) in;
cryp.dst = (caddr_t) out;
cryp.iv = (caddr_t) iv;
Expand Down Expand Up @@ -138,15 +147,11 @@ static int match(FAR unsigned char *a, FAR unsigned char *b, size_t len)
}
#define SZ 16

/****************************************************************************
* Public Functions
****************************************************************************/

int main(int argc, FAR char **argv)
static void test_3descbc(void **state)
{
const unsigned char key[24] = "012345670123456701234567";
unsigned char iv0[8];
unsigned char iv[8];
unsigned char key[24] = "012345670123456701234567";
unsigned char b1[SZ];
unsigned char b2[SZ];
int i;
Expand Down Expand Up @@ -180,14 +185,14 @@ int main(int argc, FAR char **argv)
memset(b2, 0, sizeof(b2));
memcpy(iv, iv0, sizeof(iv0));

if (syscrypt(key, sizeof(key), iv, b1, b2, sizeof(b1), 1) < 0)
if (syscrypt(key, sizeof(key), iv, sizeof(iv), b1, b2, sizeof(b1), 1) < 0)
{
warnx("encrypt with /dev/crypto failed");
fail++;
}

memcpy(iv, iv0, sizeof(iv0));
if (syscrypt(key, sizeof(key), iv, b2, b2, sizeof(b1), 0) < 0)
if (syscrypt(key, sizeof(key), iv, sizeof(iv), b2, b2, sizeof(b1), 0) < 0)
{
warnx("decrypt with /dev/crypto failed");
fail++;
Expand All @@ -202,5 +207,18 @@ int main(int argc, FAR char **argv)
printf("ok, encrypt with /dev/crypto, decrypt with /dev/crypto\n");
}

exit((fail > 0) ? 1 : 0);
assert_int_equal(fail, 0);
}

/****************************************************************************
* Public Functions
****************************************************************************/

int main(int argc, FAR char *argv[])
{
const struct CMUnitTest descbc_tests[] = {
cmocka_unit_test(test_3descbc),
};

return cmocka_run_group_tests(descbc_tests, NULL, NULL);
}
58 changes: 47 additions & 11 deletions testing/drivers/crypto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ if(CONFIG_TESTING_CRYPTO)
MODULE
${CONFIG_TESTING_CRYPTO}
SRCS
3descbc.c)
3descbc.c
DEPENDS
cmocka)
endif()

if(CONFIG_TESTING_CRYPTO_AES_XTS)
Expand All @@ -46,7 +48,9 @@ if(CONFIG_TESTING_CRYPTO)
MODULE
${CONFIG_TESTING_CRYPTO}
SRCS
aesxts.c)
aesxts.c
DEPENDS
cmocka)
endif()

if(CONFIG_TESTING_CRYPTO_AES_CTR)
Expand All @@ -60,7 +64,9 @@ if(CONFIG_TESTING_CRYPTO)
MODULE
${CONFIG_TESTING_CRYPTO}
SRCS
aesctr.c)
aesctr.c
DEPENDS
cmocka)
endif()

if(CONFIG_TESTING_CRYPTO_AES_CBC)
Expand All @@ -74,7 +80,9 @@ if(CONFIG_TESTING_CRYPTO)
MODULE
${CONFIG_TESTING_CRYPTO}
SRCS
aescbc.c)
aescbc.c
DEPENDS
cmocka)
endif()

if(CONFIG_TESTING_CRYPTO_HMAC)
Expand All @@ -88,7 +96,9 @@ if(CONFIG_TESTING_CRYPTO)
MODULE
${CONFIG_TESTING_CRYPTO}
SRCS
hmac.c)
hmac.c
DEPENDS
cmocka)
endif()

if(CONFIG_TESTING_CRYPTO_HASH)
Expand All @@ -102,7 +112,9 @@ if(CONFIG_TESTING_CRYPTO)
MODULE
${CONFIG_TESTING_CRYPTO}
SRCS
hash.c)
hash.c
DEPENDS
cmocka)
endif()

if(CONFIG_TESTING_CRYPTO_CRC32)
Expand All @@ -116,7 +128,9 @@ if(CONFIG_TESTING_CRYPTO)
MODULE
${CONFIG_TESTING_CRYPTO}
SRCS
crc32.c)
crc32.c
DEPENDS
cmocka)
endif()

if(CONFIG_TESTING_CRYPTO_AES_CMAC)
Expand All @@ -130,7 +144,9 @@ if(CONFIG_TESTING_CRYPTO)
MODULE
${CONFIG_TESTING_CRYPTO}
SRCS
aescmac.c)
aescmac.c
DEPENDS
cmocka)
endif()

if(CONFIG_TESTING_CRYPTO_ECDSA)
Expand All @@ -144,7 +160,9 @@ if(CONFIG_TESTING_CRYPTO)
MODULE
${CONFIG_TESTING_CRYPTO}
SRCS
ecdsa.c)
ecdsa.c
DEPENDS
cmocka)
endif()

if(CONFIG_TESTING_CRYPTO_ECDH)
Expand All @@ -158,7 +176,9 @@ if(CONFIG_TESTING_CRYPTO)
MODULE
${CONFIG_TESTING_CRYPTO}
SRCS
dhm.c)
dhm.c
DEPENDS
cmocka)
endif()

if(CONFIG_TESTING_CRYPTO_RSA)
Expand All @@ -172,7 +192,23 @@ if(CONFIG_TESTING_CRYPTO)
MODULE
${CONFIG_TESTING_CRYPTO}
SRCS
rsa.c)
rsa.c
DEPENDS
cmocka)
endif()

if(CONFIG_TESTING_CRYPTO_KEY_MANAGEMENT)
nuttx_add_application(
NAME
keymanagement
PRIORITY
${CONFIG_TESTING_CRYPTO_PRIORITY}
STACKSIZE
${CONFIG_TESTING_CRYPTO_STACKSIZE}
MODULE
${CONFIG_TESTING_CRYPTO}
SRCS
keymanagement.c)
endif()

endif()
6 changes: 6 additions & 0 deletions testing/drivers/crypto/Kconfig
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
config TESTING_CRYPTO
tristate "crypto test"
default n
depends on TESTING_CMOCKA
---help---
Enable crypto test

Expand Down Expand Up @@ -52,12 +53,17 @@ config TESTING_CRYPTO_ECDSA

config TESTING_CRYPTO_ECDH
bool "ecdh crypto test"
depends on CRYPTO_RANDOM_POOL
default n

config TESTING_CRYPTO_RSA
bool "rsa crypto test"
default n

config TESTING_CRYPTO_KEY_MANAGEMENT
bool "key management test"
default n

config TESTING_CRYPTO_PRIORITY
int "crypto test task priority"
default 100
Expand Down
5 changes: 5 additions & 0 deletions testing/drivers/crypto/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ PROGNAME += rsa
MAINSRC += rsa.c
endif

ifeq ($(CONFIG_TESTING_CRYPTO_KEY_MANAGEMENT),y)
PROGNAME += keymanagement
MAINSRC += keymanagement.c
endif

PRIORITY = $(CONFIG_TESTING_CRYPTO_PRIORITY)
STACKSIZE = $(CONFIG_TESTING_CRYPTO_STACKSIZE)
MODULE = $(CONFIG_TESTING_CRYPTO)
Expand Down
Loading
Loading