diff --git a/ChangeLog b/ChangeLog index 6cc87c42c99..27e9dae4d0a 100755 --- a/ChangeLog +++ b/ChangeLog @@ -11087,4 +11087,13 @@ * drivers/serial/serial.c, serialirq.c and include/nuttx/serial/serial.h: Implement high level DMA infrastructure for serial devices. From Max Neklyudov (2015-11-12). + * arch/arm/src/samv7 and arch/arm/include/samv7: Add support for the + SAME70 family of chips (2015-11-14). + * configs/stm32f429i-disco: configs/stm32f429i-disco/src/stm32_nsh.c + file calculated partition boundries based on page block sizes but + mtd_partition() is expecting calculations based on erase block size. + From Alan Carvalho de Assis (2015-11-16). + * Move rivers/wireless/cc3000/security.c to crypto/aes.c; move + include/nuttx/wireless/cc3000/security.h to include/nuttx/crypto/aes.h + (2015-11-16). diff --git a/arch b/arch index 24f87df3fa8..cce873c4606 160000 --- a/arch +++ b/arch @@ -1 +1 @@ -Subproject commit 24f87df3fa8d9fec9044e2c2bb37d12260fd02de +Subproject commit cce873c46060fe663f571cb922e642e097249c60 diff --git a/binfmt/Makefile b/binfmt/Makefile index c5cfe9736bc..9da55ea973a 100644 --- a/binfmt/Makefile +++ b/binfmt/Makefile @@ -46,7 +46,7 @@ CFLAGS += ${shell $(INCDIR) $(INCDIROPT) "$(CC)" "$(TOPDIR)$(DELIM)sched"} BINFMT_ASRCS = BINFMT_CSRCS = binfmt_globals.c binfmt_register.c binfmt_unregister.c BINFMT_CSRCS += binfmt_loadmodule.c binfmt_unloadmodule.c binfmt_execmodule.c -BINFMT_CSRCS += binfmt_exec.c binfmt_dumpmodule.c +BINFMT_CSRCS += binfmt_exec.c binfmt_copyargv.c binfmt_dumpmodule.c ifeq ($(CONFIG_BINFMT_EXEPATH),y) BINFMT_CSRCS += binfmt_exepath.c diff --git a/binfmt/binfmt_internal.h b/binfmt/binfmt.h similarity index 82% rename from binfmt/binfmt_internal.h rename to binfmt/binfmt.h index 11b036fe748..370ef57eaeb 100644 --- a/binfmt/binfmt_internal.h +++ b/binfmt/binfmt.h @@ -1,5 +1,5 @@ /**************************************************************************** - * binfmt/binfmt_internal.h + * binfmt/binfmt.h * * Copyright (C) 2009 Gregory Nutt. All rights reserved. * Author: Gregory Nutt @@ -33,8 +33,8 @@ * ****************************************************************************/ -#ifndef __BINFMT_BINFMT_INTERNAL_H -#define __BINFMT_BINFMT_INTERNAL_H +#ifndef __BINFMT_BINFMT_H +#define __BINFMT_BINFMT_H /**************************************************************************** * Included Files @@ -92,6 +92,26 @@ int dump_module(FAR const struct binary_s *bin); # define dump_module(bin) #endif +/**************************************************************************** + * Name: binfmt_copyargv + * + * Description: + * In the kernel build, the argv list will likely lie in the caller's + * address environment and, hence, by inaccessible when we swith to the + * address environment of the new process address environment. So we + * do not have any real option other than to copy the callers argv[] list. + * + * Input Parameter: + * bin - Load structure + * argv - Argument list + * + * Returned Value: + * Zero (OK) on sucess; a negater erro value on failure. + * + ****************************************************************************/ + +int binfmt_copyargv(FAR struct binary_s *bin, FAR char * const *argv); + /**************************************************************************** * Name: binfmt_freeargv * @@ -117,5 +137,5 @@ void binfmt_freeargv(FAR struct binary_s *bin); } #endif -#endif /* __BINFMT_BINFMT_INTERNAL_H */ +#endif /* __BINFMT_BINFMT_H */ diff --git a/binfmt/binfmt_copyargv.c b/binfmt/binfmt_copyargv.c new file mode 100644 index 00000000000..2fee46d047d --- /dev/null +++ b/binfmt/binfmt_copyargv.c @@ -0,0 +1,197 @@ +/**************************************************************************** + * binfmt/binfmt_copyargv.c + * + * Copyright (C) 2009, 2013-2015 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include + +#include +#include + +#include "binfmt.h" + +#ifndef CONFIG_BINFMT_DISABLE + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ +/* This is an artificial limit to detect error conditions where an argv[] + * list is not properly terminated. + */ + +#define MAX_EXEC_ARGS 256 + +/**************************************************************************** + * Public Function + ****************************************************************************/ + +/**************************************************************************** + * Name: binfmt_copyargv + * + * Description: + * In the kernel build, the argv list will likely lie in the caller's + * address environment and, hence, by inaccessible when we switch to the + * address environment of the new process address environment. So we + * do not have any real option other than to copy the callers argv[] list. + * + * Input Parameter: + * bin - Load structure + * argv - Argument list + * + * Returned Value: + * Zero (OK) on sucess; a negater erro value on failure. + * + ****************************************************************************/ + +int binfmt_copyargv(FAR struct binary_s *bin, FAR char * const *argv) +{ +#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL) + FAR char *ptr; + size_t argvsize; + size_t argsize; + int nargs; + int i; + + /* Get the number of arguments and the size of the argument list */ + + bin->argv = (FAR char **)NULL; + bin->argbuffer = (FAR char *)NULL; + + if (argv) + { + argsize = 0; + nargs = 0; + + for (i = 0; argv[i]; i++) + { + /* Increment the size of the allocation with the size of the next string */ + + argsize += (strlen(argv[i]) + 1); + nargs++; + + /* This is a sanity check to prevent running away with an unterminated + * argv[] list. MAX_EXEC_ARGS should be sufficiently large that this + * never happens in normal usage. + */ + + if (nargs > MAX_EXEC_ARGS) + { + bdbg("ERROR: Too many arguments: %lu\n", (unsigned long)argvsize); + return -E2BIG; + } + } + + bvdbg("args=%d argsize=%lu\n", nargs, (unsigned long)argsize); + + /* Allocate the argv array and an argument buffer */ + + if (argsize > 0) + { + argvsize = (nargs + 1) * sizeof(FAR char *); + bin->argbuffer = (FAR char *)kmm_malloc(argvsize + argsize); + if (!bin->argbuffer) + { + bdbg("ERROR: Failed to allocate the argument buffer\n"); + return -ENOMEM; + } + + /* Copy the argv list */ + + bin->argv = (FAR char **)bin->argbuffer; + ptr = bin->argbuffer + argvsize; + for (i = 0; argv[i]; i++) + { + bin->argv[i] = ptr; + argsize = strlen(argv[i]) + 1; + memcpy(ptr, argv[i], argsize); + ptr += argsize; + } + + /* Terminate the argv[] list */ + + bin->argv[i] = (FAR char *)NULL; + } + } + + return OK; + +#else + /* Just save the caller's argv pointer */ + + bin->argv = argv; + return OK; +#endif +} + +/**************************************************************************** + * Name: binfmt_freeargv + * + * Description: + * Release the copied argv[] list. + * + * Input Parameter: + * binp - Load structure + * + * Returned Value: + * None + * + ****************************************************************************/ + +#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL) +void binfmt_freeargv(FAR struct binary_s *binp) +{ + /* Is there an allocated argument buffer */ + + if (binp->argbuffer) + { + /* Free the argument buffer */ + + kmm_free(binp->argbuffer); + } + + /* Nullify the allocated argv[] array and the argument buffer pointers */ + + binp->argbuffer = (FAR char *)NULL; + binp->argv = (FAR char **)NULL; +} +#endif + +#endif /* !CONFIG_BINFMT_DISABLE */ diff --git a/binfmt/binfmt_dumpmodule.c b/binfmt/binfmt_dumpmodule.c index 59ed17a94e4..c69fed83c43 100644 --- a/binfmt/binfmt_dumpmodule.c +++ b/binfmt/binfmt_dumpmodule.c @@ -45,7 +45,7 @@ #include -#include "binfmt_internal.h" +#include "binfmt.h" #if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_BINFMT) && !defined(CONFIG_BINFMT_DISABLE) diff --git a/binfmt/binfmt_exec.c b/binfmt/binfmt_exec.c index 3f866fe99a0..f3f62027ebf 100644 --- a/binfmt/binfmt_exec.c +++ b/binfmt/binfmt_exec.c @@ -46,129 +46,10 @@ #include #include -#include "binfmt_internal.h" +#include "binfmt.h" #ifndef CONFIG_BINFMT_DISABLE -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ -/* This is an artificial limit to detect error conditions where an argv[] - * list is not properly terminated. - */ - -#define MAX_EXEC_ARGS 256 - -/**************************************************************************** - * Private Function Prototypes - ****************************************************************************/ - -/**************************************************************************** - * Name: binfmt_copyargv - * - * Description: - * In the kernel build, the argv list will likely lie in the caller's - * address environment and, hence, by inaccessible when we swith to the - * address environment of the new process address environment. So we - * do not have any real option other than to copy the callers argv[] list. - * - * Input Parameter: - * bin - Load structure - * argv - Argument list - * - * Returned Value: - * Zero (OK) on sucess; a negater erro value on failure. - * - ****************************************************************************/ - -static inline int binfmt_copyargv(FAR struct binary_s *bin, FAR char * const *argv) -{ -#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL) - FAR char *ptr; - size_t argvsize; - size_t argsize; - int nargs; - int i; - - /* Get the number of arguments and the size of the argument list */ - - bin->argv = (FAR char **)NULL; - bin->argbuffer = (FAR char *)NULL; - - if (argv) - { - argsize = 0; - nargs = 0; - - for (i = 0; argv[i]; i++) - { - /* Increment the size of the allocation with the size of the next string */ - - argsize += (strlen(argv[i]) + 1); - nargs++; - - /* This is a sanity check to prevent running away with an unterminated - * argv[] list. MAX_EXEC_ARGS should be sufficiently large that this - * never happens in normal usage. - */ - - if (nargs > MAX_EXEC_ARGS) - { - bdbg("ERROR: Too many arguments: %lu\n", (unsigned long)argvsize); - return -E2BIG; - } - } - - bvdbg("args=%d argsize=%lu\n", nargs, (unsigned long)argsize); - - /* Allocate the argv array and an argument buffer */ - - if (argsize > 0) - { - argvsize = (nargs + 1) * sizeof(FAR char *); - bin->argbuffer = (FAR char *)kmm_malloc(argvsize + argsize); - if (!bin->argbuffer) - { - bdbg("ERROR: Failed to allocate the argument buffer\n"); - return -ENOMEM; - } - - /* Copy the argv list */ - - bin->argv = (FAR char **)bin->argbuffer; - ptr = bin->argbuffer + argvsize; - for (i = 0; argv[i]; i++) - { - bin->argv[i] = ptr; - argsize = strlen(argv[i]) + 1; - memcpy(ptr, argv[i], argsize); - ptr += argsize; - } - - /* Terminate the argv[] list */ - - bin->argv[i] = (FAR char *)NULL; - } - } - - return OK; - -#else - /* Just save the caller's argv pointer */ - - bin->argv = argv; - return OK; -#endif -} - -/**************************************************************************** - * Private Data - ****************************************************************************/ - -/**************************************************************************** - * Private Functions - ****************************************************************************/ - /**************************************************************************** * Public Functions ****************************************************************************/ diff --git a/binfmt/binfmt_execmodule.c b/binfmt/binfmt_execmodule.c index f8701be8ce5..971c47123cc 100644 --- a/binfmt/binfmt_execmodule.c +++ b/binfmt/binfmt_execmodule.c @@ -53,7 +53,7 @@ #include #include "sched/sched.h" -#include "binfmt_internal.h" +#include "binfmt.h" #ifndef CONFIG_BINFMT_DISABLE diff --git a/binfmt/binfmt_loadmodule.c b/binfmt/binfmt_loadmodule.c index 83868460a6d..1b614838e6a 100644 --- a/binfmt/binfmt_loadmodule.c +++ b/binfmt/binfmt_loadmodule.c @@ -46,7 +46,7 @@ #include #include -#include "binfmt_internal.h" +#include "binfmt.h" #ifndef CONFIG_BINFMT_DISABLE diff --git a/binfmt/binfmt_register.c b/binfmt/binfmt_register.c index 925f29353fd..48ddfc1daab 100644 --- a/binfmt/binfmt_register.c +++ b/binfmt/binfmt_register.c @@ -46,7 +46,7 @@ #include -#include "binfmt_internal.h" +#include "binfmt.h" #ifndef CONFIG_BINFMT_DISABLE diff --git a/binfmt/binfmt_schedunload.c b/binfmt/binfmt_schedunload.c index 89d58159185..b9c6a9d6d15 100644 --- a/binfmt/binfmt_schedunload.c +++ b/binfmt/binfmt_schedunload.c @@ -46,7 +46,7 @@ #include #include -#include "binfmt_internal.h" +#include "binfmt.h" #if !defined(CONFIG_BINFMT_DISABLE) && defined(CONFIG_SCHED_HAVE_PARENT) diff --git a/binfmt/binfmt_unloadmodule.c b/binfmt/binfmt_unloadmodule.c index 535942b0f6e..887d6d66c09 100644 --- a/binfmt/binfmt_unloadmodule.c +++ b/binfmt/binfmt_unloadmodule.c @@ -48,7 +48,7 @@ #include #include -#include "binfmt_internal.h" +#include "binfmt.h" #ifndef CONFIG_BINFMT_DISABLE @@ -211,38 +211,5 @@ int unload_module(FAR struct binary_s *binp) return OK; } -/**************************************************************************** - * Name: binfmt_freeargv - * - * Description: - * Release the copied argv[] list. - * - * Input Parameter: - * binp - Load structure - * - * Returned Value: - * None - * - ****************************************************************************/ - -#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL) -void binfmt_freeargv(FAR struct binary_s *binp) -{ - /* Is there an allocated argument buffer */ - - if (binp->argbuffer) - { - /* Free the argument buffer */ - - kmm_free(binp->argbuffer); - } - - /* Nullify the allocated argv[] array and the argument buffer pointers */ - - binp->argbuffer = (FAR char *)NULL; - binp->argv = (FAR char **)NULL; -} -#endif - #endif /* CONFIG_BINFMT_DISABLE */ diff --git a/binfmt/binfmt_unregister.c b/binfmt/binfmt_unregister.c index f895e354d07..f97b06ff2da 100644 --- a/binfmt/binfmt_unregister.c +++ b/binfmt/binfmt_unregister.c @@ -46,7 +46,7 @@ #include -#include "binfmt_internal.h" +#include "binfmt.h" #ifndef CONFIG_BINFMT_DISABLE diff --git a/configs b/configs index 2a4b5e665ac..174b30da6b3 160000 --- a/configs +++ b/configs @@ -1 +1 @@ -Subproject commit 2a4b5e665ac98b72920739eb12144462ca38e675 +Subproject commit 174b30da6b334c1e040d4212e31237b5b6aabd06 diff --git a/crypto/Kconfig b/crypto/Kconfig index b23d23f56f0..022fce7a051 100644 --- a/crypto/Kconfig +++ b/crypto/Kconfig @@ -39,4 +39,15 @@ config CRYPTO_CRYPTODEV bool "cryptodev support" default n -endif +config CRYPTO_SW_AES + bool "Software AES library" + default n + ---help--- + Enable the software AES library as described in + include/nuttx/crypto/aes.h + + TODO: Adapt interfaces so that they are consistent with H/W AES + implemenations. This needs to support up_aesinitialize() and + aes_cypher() per include/nuttx/crypto/crypto.h. + +endif # CRYPTO diff --git a/crypto/Makefile b/crypto/Makefile index b3131531e83..c56640534ef 100644 --- a/crypto/Makefile +++ b/crypto/Makefile @@ -35,33 +35,41 @@ -include $(TOPDIR)/Make.defs +CRYPTO_ASRCS = +CRYPTO_CSRCS = + ifeq ($(CONFIG_CRYPTO),y) # Basic -CRYPTO_ASRCS = -CRYPTO_CSRCS = crypto.c testmngr.c +CRYPTO_CSRCS += crypto.c testmngr.c # cryptodev support ifeq ($(CONFIG_CRYPTO_CRYPTODEV),y) -CRYPTO_CSRCS += cryptodev.c + CRYPTO_CSRCS += cryptodev.c +endif + +# Sofware AES library + +ifeq ($(CONFIG_CRYPTO_SW_AES),y) + CRYPTO_CSRCS += aes.c endif endif # CONFIG_CRYPTO -ASRCS = $(CRYPTO_ASRCS) -AOBJS = $(ASRCS:.S=$(OBJEXT)) +ASRCS = $(CRYPTO_ASRCS) +AOBJS = $(ASRCS:.S=$(OBJEXT)) -CSRCS = $(CRYPTO_CSRCS) -COBJS = $(CSRCS:.c=$(OBJEXT)) +CSRCS = $(CRYPTO_CSRCS) +COBJS = $(CSRCS:.c=$(OBJEXT)) -SRCS = $(ASRCS) $(CSRCS) -OBJS = $(AOBJS) $(COBJS) +SRCS = $(ASRCS) $(CSRCS) +OBJS = $(AOBJS) $(COBJS) -BIN = libcrypto$(LIBEXT) +BIN = libcrypto$(LIBEXT) -all: $(BIN) +all: $(BIN) $(AOBJS): %$(OBJEXT): %.S $(call ASSEMBLE, $<, $@) diff --git a/drivers/wireless/cc3000/security.c b/crypto/aes.c similarity index 54% rename from drivers/wireless/cc3000/security.c rename to crypto/aes.c index bd30a781213..e7c638f7189 100644 --- a/drivers/wireless/cc3000/security.c +++ b/crypto/aes.c @@ -1,44 +1,53 @@ /**************************************************************************** - * security.c - CC3000 Host Driver Implementation. - * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ + * crypto/aes.c * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: + * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ + * Extracted from the CC3000 Host Driver Implementation. * - * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: * - * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the - * distribution. + * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. * - * Neither the name of Texas Instruments Incorporated nor the names of - * its contributors may be used to endorse or promote products derived - * from this software without specific prior written permission. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the + * distribution. * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * Neither the name of Texas Instruments Incorporated nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ****************************************************************************/ +/* TODO: Adapt interfaces so that they are consistent with H/W AES + * implemenations. This needs to support up_aesinitialize() and + * aes_cypher() per include/nuttx/crypto/crypto.h. + */ + /**************************************************************************** * Included Files ****************************************************************************/ -#include +#include -#ifndef CC3000_UNENCRYPTED_SMART_CONFIG +#include + +#include /**************************************************************************** * Private Data @@ -46,7 +55,7 @@ /* Forward sbox */ -const uint8_t sbox[256] = +static const uint8_t g_sbox[256] = { /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */ 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, /* 0 */ @@ -69,7 +78,7 @@ const uint8_t sbox[256] = /* Inverse sbox */ -const uint8_t rsbox[256] = +static const uint8_t g_rsbox[256] = { 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb, 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb, @@ -91,59 +100,61 @@ const uint8_t rsbox[256] = /* Round constant */ -const uint8_t Rcon[11] = +static const uint8_t g_rcon[11] = { 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36 }; -uint8_t aexpandedKey[176]; +static uint8_t g_expanded_key[176]; /**************************************************************************** - * Public Functions + * Private Functions ****************************************************************************/ + /**************************************************************************** - * Name: expandKey + * Name: expand_key * * Description: * Expend a 16 bytes key for AES128 implementation * * Input Parameters: * key AES128 key - 16 bytes - * expandedKey expanded AES128 key + * expanded_key expanded AES128 key * * Returned Value: * None * ****************************************************************************/ -void expandKey(uint8_t *expandedKey, uint8_t *key) +static void expand_key(FAR uint8_t *expanded_key, FAR const uint8_t *key) { - uint16_t ii, buf1; + uint16_t buf1; + uint16_t ii; for (ii = 0; ii < 16; ii++) { - expandedKey[ii] = key[ii]; + expanded_key[ii] = key[ii]; } for (ii = 1; ii < 11; ii++) { - buf1 = expandedKey[ii*16 - 4]; - expandedKey[ii * 16 + 0] = sbox[expandedKey[ii *16 - 3]] ^ expandedKey[(ii - 1) * 16 + 0] ^ Rcon[ii]; - expandedKey[ii * 16 + 1] = sbox[expandedKey[ii *16 - 2]] ^ expandedKey[(ii - 1) * 16 + 1]; - expandedKey[ii * 16 + 2] = sbox[expandedKey[ii *16 - 1]] ^ expandedKey[(ii - 1) * 16 + 2]; - expandedKey[ii * 16 + 3] = sbox[buf1] ^ expandedKey[(ii - 1) * 16 + 3]; - expandedKey[ii * 16 + 4] = expandedKey[(ii - 1) * 16 + 4] ^ expandedKey[ii * 16 + 0]; - expandedKey[ii * 16 + 5] = expandedKey[(ii - 1) * 16 + 5] ^ expandedKey[ii * 16 + 1]; - expandedKey[ii * 16 + 6] = expandedKey[(ii - 1) * 16 + 6] ^ expandedKey[ii * 16 + 2]; - expandedKey[ii * 16 + 7] = expandedKey[(ii - 1) * 16 + 7] ^ expandedKey[ii * 16 + 3]; - expandedKey[ii * 16 + 8] = expandedKey[(ii - 1) * 16 + 8] ^ expandedKey[ii * 16 + 4]; - expandedKey[ii * 16 + 9] = expandedKey[(ii - 1) * 16 + 9] ^ expandedKey[ii * 16 + 5]; - expandedKey[ii * 16 +10] = expandedKey[(ii - 1) * 16 +10] ^ expandedKey[ii * 16 + 6]; - expandedKey[ii * 16 +11] = expandedKey[(ii - 1) * 16 +11] ^ expandedKey[ii * 16 + 7]; - expandedKey[ii * 16 +12] = expandedKey[(ii - 1) * 16 +12] ^ expandedKey[ii * 16 + 8]; - expandedKey[ii * 16 +13] = expandedKey[(ii - 1) * 16 +13] ^ expandedKey[ii * 16 + 9]; - expandedKey[ii * 16 +14] = expandedKey[(ii - 1) * 16 +14] ^ expandedKey[ii * 16 +10]; - expandedKey[ii * 16 +15] = expandedKey[(ii - 1) * 16 +15] ^ expandedKey[ii * 16 +11]; + buf1 = expanded_key[ii*16 - 4]; + expanded_key[ii * 16 + 0] = g_sbox[expanded_key[ii *16 - 3]] ^ expanded_key[(ii - 1) * 16 + 0] ^ g_rcon[ii]; + expanded_key[ii * 16 + 1] = g_sbox[expanded_key[ii *16 - 2]] ^ expanded_key[(ii - 1) * 16 + 1]; + expanded_key[ii * 16 + 2] = g_sbox[expanded_key[ii *16 - 1]] ^ expanded_key[(ii - 1) * 16 + 2]; + expanded_key[ii * 16 + 3] = g_sbox[buf1] ^ expanded_key[(ii - 1) * 16 + 3]; + expanded_key[ii * 16 + 4] = expanded_key[(ii - 1) * 16 + 4] ^ expanded_key[ii * 16 + 0]; + expanded_key[ii * 16 + 5] = expanded_key[(ii - 1) * 16 + 5] ^ expanded_key[ii * 16 + 1]; + expanded_key[ii * 16 + 6] = expanded_key[(ii - 1) * 16 + 6] ^ expanded_key[ii * 16 + 2]; + expanded_key[ii * 16 + 7] = expanded_key[(ii - 1) * 16 + 7] ^ expanded_key[ii * 16 + 3]; + expanded_key[ii * 16 + 8] = expanded_key[(ii - 1) * 16 + 8] ^ expanded_key[ii * 16 + 4]; + expanded_key[ii * 16 + 9] = expanded_key[(ii - 1) * 16 + 9] ^ expanded_key[ii * 16 + 5]; + expanded_key[ii * 16 +10] = expanded_key[(ii - 1) * 16 +10] ^ expanded_key[ii * 16 + 6]; + expanded_key[ii * 16 +11] = expanded_key[(ii - 1) * 16 +11] ^ expanded_key[ii * 16 + 7]; + expanded_key[ii * 16 +12] = expanded_key[(ii - 1) * 16 +12] ^ expanded_key[ii * 16 + 8]; + expanded_key[ii * 16 +13] = expanded_key[(ii - 1) * 16 +13] ^ expanded_key[ii * 16 + 9]; + expanded_key[ii * 16 +14] = expanded_key[(ii - 1) * 16 +14] ^ expanded_key[ii * 16 +10]; + expanded_key[ii * 16 +15] = expanded_key[(ii - 1) * 16 +15] ^ expanded_key[ii * 16 +11]; } } @@ -161,7 +172,7 @@ void expandKey(uint8_t *expandedKey, uint8_t *key) * ******************************************************************************/ -uint8_t galois_mul2(uint8_t value) +static uint8_t galois_mul2(uint8_t value) { if (value >> 7) { @@ -179,62 +190,68 @@ uint8_t galois_mul2(uint8_t value) * * Description: * Internal implementation of AES128 encryption. - * Straight forward aes encryption implementation. First the group of operations + * Straight forward aes encryption implementation. First the group of + * operations: + * * - addRoundKey * - subbytes * - shiftrows * - mixcolums + * * is executed 9 times, after this addroundkey to finish the 9th round, after * that the 10th round without mixcolums no further subfunctions to save * cycles for function calls no structuring with "for (....)" to save cycles. * * Input Parameters: - * expandedKey expanded AES128 key - * state 16 bytes of plain text and cipher text + * expanded_key expanded AES128 key + * state 16 bytes of plain text and cipher text * * Returned Value: * None * ******************************************************************************/ -void aes_encr(uint8_t *state, uint8_t *expandedKey) +static void aes_encr(FAR uint8_t *state, FAR const uint8_t *expanded_key) { - uint8_t buf1, buf2, buf3, round; + uint8_t buf1; + uint8_t buf2; + uint8_t buf3; + uint8_t round; for (round = 0; round < 9; round ++) { /* addroundkey, sbox and shiftrows */ /* Row 0 */ - state[0] = sbox[(state[0] ^ expandedKey[(round * 16)])]; - state[4] = sbox[(state[4] ^ expandedKey[(round * 16) + 4])]; - state[8] = sbox[(state[8] ^ expandedKey[(round * 16) + 8])]; - state[12] = sbox[(state[12] ^ expandedKey[(round * 16) + 12])]; + state[0] = g_sbox[(state[0] ^ expanded_key[(round * 16)])]; + state[4] = g_sbox[(state[4] ^ expanded_key[(round * 16) + 4])]; + state[8] = g_sbox[(state[8] ^ expanded_key[(round * 16) + 8])]; + state[12] = g_sbox[(state[12] ^ expanded_key[(round * 16) + 12])]; /* Row 1 */ - buf1 = state[1] ^ expandedKey[(round * 16) + 1]; - state[1] = sbox[(state[5] ^ expandedKey[(round * 16) + 5])]; - state[5] = sbox[(state[9] ^ expandedKey[(round * 16) + 9])]; - state[9] = sbox[(state[13] ^ expandedKey[(round * 16) + 13])]; - state[13] = sbox[buf1]; + buf1 = state[1] ^ expanded_key[(round * 16) + 1]; + state[1] = g_sbox[(state[5] ^ expanded_key[(round * 16) + 5])]; + state[5] = g_sbox[(state[9] ^ expanded_key[(round * 16) + 9])]; + state[9] = g_sbox[(state[13] ^ expanded_key[(round * 16) + 13])]; + state[13] = g_sbox[buf1]; /* Row 2 */ - buf1 = state[2] ^ expandedKey[(round * 16) + 2]; - buf2 = state[6] ^ expandedKey[(round * 16) + 6]; - state[2] = sbox[(state[10] ^ expandedKey[(round * 16) + 10])]; - state[6] = sbox[(state[14] ^ expandedKey[(round * 16) + 14])]; - state[10] = sbox[buf1]; - state[14] = sbox[buf2]; + buf1 = state[2] ^ expanded_key[(round * 16) + 2]; + buf2 = state[6] ^ expanded_key[(round * 16) + 6]; + state[2] = g_sbox[(state[10] ^ expanded_key[(round * 16) + 10])]; + state[6] = g_sbox[(state[14] ^ expanded_key[(round * 16) + 14])]; + state[10] = g_sbox[buf1]; + state[14] = g_sbox[buf2]; /* Row 3 */ - buf1 = state[15] ^ expandedKey[(round * 16) + 15]; - state[15] = sbox[(state[11] ^ expandedKey[(round * 16) + 11])]; - state[11] = sbox[(state[7] ^ expandedKey[(round * 16) + 7])]; - state[7] = sbox[(state[3] ^ expandedKey[(round * 16) + 3])]; - state[3] = sbox[buf1]; + buf1 = state[15] ^ expanded_key[(round * 16) + 15]; + state[15] = g_sbox[(state[11] ^ expanded_key[(round * 16) + 11])]; + state[11] = g_sbox[(state[7] ^ expanded_key[(round * 16) + 7])]; + state[7] = g_sbox[(state[3] ^ expanded_key[(round * 16) + 3])]; + state[3] = g_sbox[buf1]; /* mixcolums */ /* Col1 */ @@ -276,54 +293,54 @@ void aes_encr(uint8_t *state, uint8_t *expandedKey) /* 10th round without mixcols */ - state[0] = sbox[(state[0] ^ expandedKey[(round * 16)])]; - state[4] = sbox[(state[4] ^ expandedKey[(round * 16) + 4])]; - state[8] = sbox[(state[8] ^ expandedKey[(round * 16) + 8])]; - state[12] = sbox[(state[12] ^ expandedKey[(round * 16) + 12])]; + state[0] = g_sbox[(state[0] ^ expanded_key[(round * 16)])]; + state[4] = g_sbox[(state[4] ^ expanded_key[(round * 16) + 4])]; + state[8] = g_sbox[(state[8] ^ expanded_key[(round * 16) + 8])]; + state[12] = g_sbox[(state[12] ^ expanded_key[(round * 16) + 12])]; /* Row 1 */ - buf1 = state[1] ^ expandedKey[(round * 16) + 1]; - state[1] = sbox[(state[5] ^ expandedKey[(round * 16) + 5])]; - state[5] = sbox[(state[9] ^ expandedKey[(round * 16) + 9])]; - state[9] = sbox[(state[13] ^ expandedKey[(round * 16) + 13])]; - state[13] = sbox[buf1]; + buf1 = state[1] ^ expanded_key[(round * 16) + 1]; + state[1] = g_sbox[(state[5] ^ expanded_key[(round * 16) + 5])]; + state[5] = g_sbox[(state[9] ^ expanded_key[(round * 16) + 9])]; + state[9] = g_sbox[(state[13] ^ expanded_key[(round * 16) + 13])]; + state[13] = g_sbox[buf1]; /* Row 2 */ - buf1 = state[2] ^ expandedKey[(round * 16) + 2]; - buf2 = state[6] ^ expandedKey[(round * 16) + 6]; - state[2] = sbox[(state[10] ^ expandedKey[(round * 16) + 10])]; - state[6] = sbox[(state[14] ^ expandedKey[(round * 16) + 14])]; - state[10] = sbox[buf1]; - state[14] = sbox[buf2]; + buf1 = state[2] ^ expanded_key[(round * 16) + 2]; + buf2 = state[6] ^ expanded_key[(round * 16) + 6]; + state[2] = g_sbox[(state[10] ^ expanded_key[(round * 16) + 10])]; + state[6] = g_sbox[(state[14] ^ expanded_key[(round * 16) + 14])]; + state[10] = g_sbox[buf1]; + state[14] = g_sbox[buf2]; /* Row 3 */ - buf1 = state[15] ^ expandedKey[(round * 16) + 15]; - state[15] = sbox[(state[11] ^ expandedKey[(round * 16) + 11])]; - state[11] = sbox[(state[7] ^ expandedKey[(round * 16) + 7])]; - state[7] = sbox[(state[3] ^ expandedKey[(round * 16) + 3])]; - state[3] = sbox[buf1]; + buf1 = state[15] ^ expanded_key[(round * 16) + 15]; + state[15] = g_sbox[(state[11] ^ expanded_key[(round * 16) + 11])]; + state[11] = g_sbox[(state[7] ^ expanded_key[(round * 16) + 7])]; + state[7] = g_sbox[(state[3] ^ expanded_key[(round * 16) + 3])]; + state[3] = g_sbox[buf1]; /* Last addroundkey */ - state[0] ^= expandedKey[160]; - state[1] ^= expandedKey[161]; - state[2] ^= expandedKey[162]; - state[3] ^= expandedKey[163]; - state[4] ^= expandedKey[164]; - state[5] ^= expandedKey[165]; - state[6] ^= expandedKey[166]; - state[7] ^= expandedKey[167]; - state[8] ^= expandedKey[168]; - state[9] ^= expandedKey[169]; - state[10] ^= expandedKey[170]; - state[11] ^= expandedKey[171]; - state[12] ^= expandedKey[172]; - state[13] ^= expandedKey[173]; - state[14] ^= expandedKey[174]; - state[15] ^= expandedKey[175]; + state[0] ^= expanded_key[160]; + state[1] ^= expanded_key[161]; + state[2] ^= expanded_key[162]; + state[3] ^= expanded_key[163]; + state[4] ^= expanded_key[164]; + state[5] ^= expanded_key[165]; + state[6] ^= expanded_key[166]; + state[7] ^= expanded_key[167]; + state[8] ^= expanded_key[168]; + state[9] ^= expanded_key[169]; + state[10] ^= expanded_key[170]; + state[11] ^= expanded_key[171]; + state[12] ^= expanded_key[172]; + state[13] ^= expanded_key[173]; + state[14] ^= expanded_key[174]; + state[15] ^= expanded_key[175]; } /****************************************************************************** @@ -333,77 +350,82 @@ void aes_encr(uint8_t *state, uint8_t *expandedKey) * Internal implementation of AES128 decryption. * Straight forward aes decryption implementation. The order of substeps is * the exact reverse of decryption inverse functions: + * * - addRoundKey is its own inverse * - rsbox is inverse of sbox * - rightshift instead of leftshift * - invMixColumns = barreto + mixColumns + * * No further subfunctions to save cycles for function calls no structuring * with "for (....)" to save cycles * * Input Parameters: - * expandedKey expanded AES128 key - * state 16 bytes of cipher text and plain text + * expanded_key expanded AES128 key + * state 16 bytes of cipher text and plain text * * Returned Value: * None * ******************************************************************************/ -void aes_decr(uint8_t *state, uint8_t *expandedKey) +static void aes_decr(FAR uint8_t *state, FAR const uint8_t *expanded_key) { - uint8_t buf1, buf2, buf3; + uint8_t buf1; + uint8_t buf2; + uint8_t buf3; int8_t round; + round = 9; /* Initial addroundkey */ - state[0] ^= expandedKey[160]; - state[1] ^= expandedKey[161]; - state[2] ^= expandedKey[162]; - state[3] ^= expandedKey[163]; - state[4] ^= expandedKey[164]; - state[5] ^= expandedKey[165]; - state[6] ^= expandedKey[166]; - state[7] ^= expandedKey[167]; - state[8] ^= expandedKey[168]; - state[9] ^= expandedKey[169]; - state[10] ^= expandedKey[170]; - state[11] ^= expandedKey[171]; - state[12] ^= expandedKey[172]; - state[13] ^= expandedKey[173]; - state[14] ^= expandedKey[174]; - state[15] ^= expandedKey[175]; + state[0] ^= expanded_key[160]; + state[1] ^= expanded_key[161]; + state[2] ^= expanded_key[162]; + state[3] ^= expanded_key[163]; + state[4] ^= expanded_key[164]; + state[5] ^= expanded_key[165]; + state[6] ^= expanded_key[166]; + state[7] ^= expanded_key[167]; + state[8] ^= expanded_key[168]; + state[9] ^= expanded_key[169]; + state[10] ^= expanded_key[170]; + state[11] ^= expanded_key[171]; + state[12] ^= expanded_key[172]; + state[13] ^= expanded_key[173]; + state[14] ^= expanded_key[174]; + state[15] ^= expanded_key[175]; /* 10th round without mixcols */ - state[0] = rsbox[state[0]] ^ expandedKey[(round * 16)]; - state[4] = rsbox[state[4]] ^ expandedKey[(round * 16) + 4]; - state[8] = rsbox[state[8]] ^ expandedKey[(round * 16) + 8]; - state[12] = rsbox[state[12]] ^ expandedKey[(round * 16) + 12]; + state[0] = g_rsbox[state[0]] ^ expanded_key[(round * 16)]; + state[4] = g_rsbox[state[4]] ^ expanded_key[(round * 16) + 4]; + state[8] = g_rsbox[state[8]] ^ expanded_key[(round * 16) + 8]; + state[12] = g_rsbox[state[12]] ^ expanded_key[(round * 16) + 12]; /* Row 1 */ - buf1 = rsbox[state[13]] ^ expandedKey[(round * 16) + 1]; - state[13] = rsbox[state[9]] ^ expandedKey[(round * 16) + 13]; - state[9] = rsbox[state[5]] ^ expandedKey[(round * 16) + 9]; - state[5] = rsbox[state[1]] ^ expandedKey[(round * 16) + 5]; + buf1 = g_rsbox[state[13]] ^ expanded_key[(round * 16) + 1]; + state[13] = g_rsbox[state[9]] ^ expanded_key[(round * 16) + 13]; + state[9] = g_rsbox[state[5]] ^ expanded_key[(round * 16) + 9]; + state[5] = g_rsbox[state[1]] ^ expanded_key[(round * 16) + 5]; state[1] = buf1; /* Row 2 */ - buf1 = rsbox[state[2]] ^ expandedKey[(round * 16) + 10]; - buf2 = rsbox[state[6]] ^ expandedKey[(round * 16) + 14]; - state[2] = rsbox[state[10]] ^ expandedKey[(round * 16) + 2]; - state[6] = rsbox[state[14]] ^ expandedKey[(round * 16) + 6]; + buf1 = g_rsbox[state[2]] ^ expanded_key[(round * 16) + 10]; + buf2 = g_rsbox[state[6]] ^ expanded_key[(round * 16) + 14]; + state[2] = g_rsbox[state[10]] ^ expanded_key[(round * 16) + 2]; + state[6] = g_rsbox[state[14]] ^ expanded_key[(round * 16) + 6]; state[10] = buf1; state[14] = buf2; /* Row 3 */ - buf1 = rsbox[state[3]] ^ expandedKey[(round * 16) + 15]; - state[3] = rsbox[state[7]] ^ expandedKey[(round * 16) + 3]; - state[7] = rsbox[state[11]] ^ expandedKey[(round * 16) + 7]; - state[11] = rsbox[state[15]] ^ expandedKey[(round * 16) + 11]; + buf1 = g_rsbox[state[3]] ^ expanded_key[(round * 16) + 15]; + state[3] = g_rsbox[state[7]] ^ expanded_key[(round * 16) + 3]; + state[7] = g_rsbox[state[11]] ^ expanded_key[(round * 16) + 7]; + state[11] = g_rsbox[state[15]] ^ expanded_key[(round * 16) + 11]; state[15] = buf1; for (round = 8; round >= 0; round--) @@ -485,43 +507,47 @@ void aes_decr(uint8_t *state, uint8_t *expandedKey) /* addroundkey, rsbox and shiftrows */ /* Row 0 */ - state[0] = rsbox[state[0]] ^ expandedKey[(round * 16)]; - state[4] = rsbox[state[4]] ^ expandedKey[(round * 16) + 4]; - state[8] = rsbox[state[8]] ^ expandedKey[(round * 16) + 8]; - state[12] = rsbox[state[12]] ^ expandedKey[(round * 16) + 12]; + state[0] = g_rsbox[state[0]] ^ expanded_key[(round * 16)]; + state[4] = g_rsbox[state[4]] ^ expanded_key[(round * 16) + 4]; + state[8] = g_rsbox[state[8]] ^ expanded_key[(round * 16) + 8]; + state[12] = g_rsbox[state[12]] ^ expanded_key[(round * 16) + 12]; /* Row 1 */ - buf1 = rsbox[state[13]] ^ expandedKey[(round * 16) + 1]; - state[13] = rsbox[state[9]] ^ expandedKey[(round * 16) + 13]; - state[9] = rsbox[state[5]] ^ expandedKey[(round * 16) + 9]; - state[5] = rsbox[state[1]] ^ expandedKey[(round * 16) + 5]; + buf1 = g_rsbox[state[13]] ^ expanded_key[(round * 16) + 1]; + state[13] = g_rsbox[state[9]] ^ expanded_key[(round * 16) + 13]; + state[9] = g_rsbox[state[5]] ^ expanded_key[(round * 16) + 9]; + state[5] = g_rsbox[state[1]] ^ expanded_key[(round * 16) + 5]; state[1] = buf1; /* Row 2 */ - buf1 = rsbox[state[2]] ^ expandedKey[(round * 16) + 10]; - buf2 = rsbox[state[6]] ^ expandedKey[(round * 16) + 14]; - state[2] = rsbox[state[10]] ^ expandedKey[(round * 16) + 2]; - state[6] = rsbox[state[14]] ^ expandedKey[(round * 16) + 6]; + buf1 = g_rsbox[state[2]] ^ expanded_key[(round * 16) + 10]; + buf2 = g_rsbox[state[6]] ^ expanded_key[(round * 16) + 14]; + state[2] = g_rsbox[state[10]] ^ expanded_key[(round * 16) + 2]; + state[6] = g_rsbox[state[14]] ^ expanded_key[(round * 16) + 6]; state[10] = buf1; state[14] = buf2; /* Row 3 */ - buf1 = rsbox[state[3]] ^ expandedKey[(round * 16) + 15]; - state[3] = rsbox[state[7]] ^ expandedKey[(round * 16) + 3]; - state[7] = rsbox[state[11]] ^ expandedKey[(round * 16) + 7]; - state[11] = rsbox[state[15]] ^ expandedKey[(round * 16) + 11]; + buf1 = g_rsbox[state[3]] ^ expanded_key[(round * 16) + 15]; + state[3] = g_rsbox[state[7]] ^ expanded_key[(round * 16) + 3]; + state[7] = g_rsbox[state[11]] ^ expanded_key[(round * 16) + 7]; + state[11] = g_rsbox[state[15]] ^ expanded_key[(round * 16) + 11]; state[15] = buf1; } } /**************************************************************************** + * Public Functions + ****************************************************************************/ + + /**************************************************************************** * Name: aes_encrypt * * Description: - * AES128 encryption: Given AES128 key and 16 bytes plain text, cipher + * AES128 encryption: Given AES128 key and 16 bytes plain text, cipher * text of 16 bytes is computed. The AES implementation is in mode ECB * (Electronic Code Book). * @@ -534,19 +560,19 @@ void aes_decr(uint8_t *state, uint8_t *expandedKey) * ****************************************************************************/ -void aes_encrypt(uint8_t *state, uint8_t *key) +void aes_encrypt(FAR uint8_t *state, FAR const uint8_t *key) { /* Expand the key into 176 bytes */ - expandKey(aexpandedKey, key); - aes_encr(state, aexpandedKey); + expand_key(g_expanded_key, key); + aes_encr(state, g_expanded_key); } /**************************************************************************** * Name: aes_decrypt * * Description: - * AES128 decryption: Given AES128 key and 16 bytes cipher text, plain + * AES128 decryption: Given AES128 key and 16 bytes cipher text, plain * text of 16 bytes is computed The AES implementation is in mode ECB * (Electronic Code Book). * @@ -559,58 +585,10 @@ void aes_encrypt(uint8_t *state, uint8_t *key) * ****************************************************************************/ -void aes_decrypt(uint8_t *state, uint8_t *key) +void aes_decrypt(FAR uint8_t *state, FAR const uint8_t *key) { - expandKey(aexpandedKey, key); /* Expand the key into 176 bytes */ - aes_decr(state, aexpandedKey); + /* Expand the key into 176 bytes */ + + expand_key(g_expanded_key, key); + aes_decr(state, g_expanded_key); } - -/**************************************************************************** - * Name: aes_read_key - * - * Description: - * Reads AES128 key from EEPROM. Reads the AES128 key from fileID #12 in - * EEPROM returns an error if the key does not exist. - * - * Input Parameters: - * key AES128 key of size 16 bytes - * - * Returned Value - * On success 0, error otherwise. - * - ****************************************************************************/ - -signed long aes_read_key(uint8_t *key) -{ - signed long returnValue; - - returnValue = nvmem_read(NVMEM_AES128_KEY_FILEID, AES128_KEY_SIZE, 0, key); - - return returnValue; -} - -/**************************************************************************** - * Name: aes_write_key - * - * Description: - * Writes AES128 key from EEPROM Writes the AES128 key to fileID #12 in - * EEPROM - * - * Input Parameters: - * key AES128 key of size 16 bytes - * - * Returned Value - * On success 0, error otherwise. - * - ****************************************************************************/ - -signed long aes_write_key(uint8_t *key) -{ - signed long returnValue; - - returnValue = nvmem_write(NVMEM_AES128_KEY_FILEID, AES128_KEY_SIZE, 0, key); - - return returnValue; -} - -#endif /* CC3000_UNENCRYPTED_SMART_CONFIG */ diff --git a/crypto/cryptodev.c b/crypto/cryptodev.c index c81585c3291..9710acf0281 100644 --- a/crypto/cryptodev.c +++ b/crypto/cryptodev.c @@ -50,6 +50,16 @@ #include #include +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#ifdef CONFIG_CRYPTO_AES +# define AES_CYPHER(mode) \ + aes_cypher(op->dst, op->src, op->len, op->iv, ses->key, ses->keylen, \ + mode, encrypt) +#endif + /**************************************************************************** * Private Function Prototypes ****************************************************************************/ @@ -69,13 +79,18 @@ static int cryptodev_ioctl(FAR struct file *filep, int cmd, static const struct file_operations g_cryptodevops = { - 0, /* open */ - 0, /* close */ - cryptodev_read, /* read */ - cryptodev_write, /* write */ - 0, /* seek */ - cryptodev_ioctl, /* ioctl */ - 0, /* poll */ + 0, /* open */ + 0, /* close */ + cryptodev_read, /* read */ + cryptodev_write, /* write */ + 0, /* seek */ + cryptodev_ioctl /* ioctl */ +#ifndef CONFIG_DISABLE_POLL + , 0 /* poll */ +#endif +#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS + , 0 /* unlink */ +#endif }; /**************************************************************************** @@ -110,6 +125,7 @@ static int cryptodev_ioctl(FAR struct file *filep, int cmd, unsigned long arg) return OK; } +#ifdef CONFIG_CRYPTO_AES case CIOCCRYPT: { FAR struct crypt_op *op = (FAR struct crypt_op *)arg; @@ -117,44 +133,38 @@ static int cryptodev_ioctl(FAR struct file *filep, int cmd, unsigned long arg) int encrypt; switch (op->op) - { - case COP_ENCRYPT: - encrypt = 1; - break; + { + case COP_ENCRYPT: + encrypt = 1; + break; - case COP_DECRYPT: - encrypt = 0; - break; + case COP_DECRYPT: + encrypt = 0; + break; - default: - return -EINVAL; - } + default: + return -EINVAL; + } switch (ses->cipher) - { + { + case CRYPTO_AES_ECB: + return AES_CYPHER(AES_MODE_ECB); -#if defined(CONFIG_CRYPTO_AES) -# define AES_CYPHER(mode) aes_cypher(op->dst, op->src, op->len, op->iv, ses->key, ses->keylen, mode, encrypt) + case CRYPTO_AES_CBC: + return AES_CYPHER(AES_MODE_CBC); - case CRYPTO_AES_ECB: - return AES_CYPHER(AES_MODE_ECB); + case CRYPTO_AES_CTR: + return AES_CYPHER(AES_MODE_CTR); - case CRYPTO_AES_CBC: - return AES_CYPHER(AES_MODE_CBC); - - case CRYPTO_AES_CTR: - return AES_CYPHER(AES_MODE_CTR); - -# undef AES_CYPHER + default: + return -EINVAL; + } + } #endif - default: - return -EINVAL; - } - } - default: - return -EINVAL; + return -ENOTTY; } } diff --git a/drivers/Kconfig b/drivers/Kconfig index 2ead84ff1f2..52f5b5a6f54 100644 --- a/drivers/Kconfig +++ b/drivers/Kconfig @@ -128,6 +128,7 @@ config CAN_NPENDINGRTR config CAN_TXREADY bool "can_txready interface" default n + select SCHED_WORKQUEUE ---help--- This selection enables the can_txready() interface. This interface is needed only for CAN hardware that supports queing of outgoing @@ -173,6 +174,21 @@ config CAN_TXREADY no longer full. can_txready() will then awaken the can_write() logic and the hang condition is avoided. +choice + prompt "TX Ready Work Queue" + default CAN_TXREADY_HIPRI + depends on CAN_TXREADY + +config CAN_TXREADY_LOPRI + bool "Low-priority work queue" + select SCHED_LPWORK + +config CAN_TXREADY_HIPRI + bool "High-priority work queue" + select SCHED_HPWORK + +endchoice # TX Ready Work Queue + config CAN_LOOPBACK bool "CAN loopback mode" default n diff --git a/drivers/can.c b/drivers/can.c index 2f61d6e0c73..e00d7ca337b 100644 --- a/drivers/can.c +++ b/drivers/can.c @@ -54,6 +54,10 @@ #include #include +#ifdef CONFIG_CAN_TXREADY +# include +#endif + #include #ifdef CONFIG_CAN @@ -61,6 +65,37 @@ /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ +/* Configuration ************************************************************/ + +#ifdef CONFIG_CAN_TXREADY +# if !defined(CONFIG_SCHED_WORKQUEUE) +# error Work queue support required in this configuration +# undef CONFIG_CAN_TXREADY +# undef CONFIG_CAN_TXREADY_LOPRI +# undef CONFIG_CAN_TXREADY_HIPRI +# elif defined(CONFIG_CAN_TXREADY_LOPRI) +# undef CONFIG_CAN_TXREADY_HIPRI +# ifdef CONFIG_SCHED_LPWORK +# define CANWORK LPWORK +# else +# error Low priority work queue support required in this configuration +# undef CONFIG_CAN_TXREADY +# undef CONFIG_CAN_TXREADY_LOPRI +# endif +# elif defined(CONFIG_CAN_TXREADY_HIPRI) +# ifdef CONFIG_SCHED_HPWORK +# define CANWORK HPWORK +# else +# error High priority work queue support required in this configuration +# undef CONFIG_CAN_TXREADY +# undef CONFIG_CAN_TXREADY_HIPRI +# endif +# else +# error No work queue selection +# undef CONFIG_CAN_TXREADY +# endif +#endif + /* Debug ********************************************************************/ /* Non-standard debug that may be enabled just for testing CAN */ @@ -95,6 +130,9 @@ static uint8_t can_dlc2bytes(uint8_t dlc); #if 0 /* Not used */ static uint8_t can_bytes2dlc(uint8_t nbytes); #endif +#ifdef CONFIG_CAN_TXREADY +static void can_txready_work(FAR void *arg); +#endif /* Character driver methods */ @@ -246,6 +284,59 @@ static uint8_t can_bytes2dlc(FAR struct sam_can_s *priv, uint8_t nbytes) } #endif +/**************************************************************************** + * Name: can_txready_work + * + * Description: + * This function performs deferred processing from can_txready. See the + * discription of can_txready below for additionla information. + * + ****************************************************************************/ + +#ifdef CONFIG_CAN_TXREADY +static void can_txready_work(FAR void *arg) +{ + FAR struct can_dev_s *dev = (FAR struct can_dev_s *)arg; + irqstate_t flags; + int ret; + + canllvdbg("xmit head: %d queue: %d tail: %d\n", + dev->cd_xmit.tx_head, dev->cd_xmit.tx_queue, + dev->cd_xmit.tx_tail); + + /* Verify that the xmit FIFO is not empty. The following operations must + * be performed with interrupt disabled. + */ + + flags = irqsave(); + if (dev->cd_xmit.tx_head != dev->cd_xmit.tx_tail) + { + /* Send the next message in the FIFO. */ + + ret = can_xmit(dev); + + /* If the message was successfully queued in the H/W FIFO, then + * can_txdone() should have been called. If the S/W FIFO were + * full before then there should now be free space in the S/W FIFO. + */ + + if (ret >= 0) + { + /* Are there any threads waiting for space in the TX FIFO? */ + + if (dev->cd_ntxwaiters > 0) + { + /* Yes.. Inform them that new xmit space is available */ + + (void)sem_post(&dev->cd_xmit.tx_sem); + } + } + } + + irqrestore(flags); +} +#endif + /**************************************************************************** * Name: can_open * @@ -532,12 +623,12 @@ static int can_xmit(FAR struct can_dev_s *dev) { DEBUGASSERT(dev->cd_xmit.tx_queue == dev->cd_xmit.tx_head); +#ifndef CONFIG_CAN_TXREADY /* We can disable CAN TX interrupts -- unless there is a H/W FIFO. In * that case, TX interrupts must stay enabled until the H/W FIFO is * fully emptied. */ -#ifndef CONFIG_CAN_TXREADY dev_txint(dev, false); #endif return -EIO; @@ -663,7 +754,7 @@ static ssize_t can_write(FAR struct file *filep, FAR const char *buffer, if (inactive) { - can_xmit(dev); + (void)can_xmit(dev); } /* Wait for a message to be sent */ @@ -712,7 +803,7 @@ static ssize_t can_write(FAR struct file *filep, FAR const char *buffer, if (inactive) { - can_xmit(dev); + (void)can_xmit(dev); } /* Return the number of bytes that were sent */ @@ -1036,9 +1127,11 @@ int can_receive(FAR struct can_dev_s *dev, FAR struct can_hdr_s *hdr, * 2b. H/W TX FIFO (CONFIG_CAN_TXREADY=y) and S/W TX FIFO full * * In this case, the thread calling can_write() is blocked waiting for - * space in the S/W TX FIFO. + * space in the S/W TX FIFO. can_txdone() will be called, indirectly, + * from can_txready_work() running on the thread of the work queue. * - * CAN interrupt -> can_txready() -> can_xmit() -> dev_send() -> can_txdone() + * CAN interrupt -> can_txready() -> Schedule can_txready_work() + * can_txready_work() -> can_xmit() -> dev_send() -> can_txdone() * * The call dev_send() should not fail in this case and the subsequent * call to can_txdone() will make space in the S/W TX FIFO and will @@ -1158,8 +1251,8 @@ int can_txdone(FAR struct can_dev_s *dev) * OK on success; a negated errno on failure. * * Assumptions: - * Interrupts are disabled. This is required by can_xmit() which is called - * by this function. + * Interrupts are disabled. This function may execute in the context of + * and interrupt handler. * ****************************************************************************/ @@ -1179,34 +1272,49 @@ int can_txready(FAR struct can_dev_s *dev) if (dev->cd_xmit.tx_head != dev->cd_xmit.tx_tail) { - /* Are there any threads waiting for space in the xmit FIFO? */ + /* Is work already scheduled? */ - if (dev->cd_ntxwaiters > 0) + if (work_available(&dev->cd_work)) { - /* Inform one waiter that space is now available in the S/W - * TX FIFO. + /* Yes... schedule to perform can_txready() work on the worker + * thread. Although data structures are protected by disabling + * interrupts, the can_xmit() operations may involve semaphore + * operations and, hence, should not be done at the interrupt + * level. */ - ret = sem_post(&dev->cd_xmit.tx_sem); + ret = work_queue(CANWORK, &dev->cd_work, can_txready_work, dev, 0); + } + else + { + ret = -EBUSY; } } - -#if 0 /* REVISIT */ - /* REVISIT: Does the fact that the S/W FIFO is empty also mean that the - * H/W FIFO is also empty? If we really want this to work this way, then - * we would probably need and additional parameter to tell us if the H/W - * FIFO is empty. - */ - else { + /* There should not be any threads waiting for space in the S/W TX + * FIFO is it is empty. + * + * REVISIT: Assertion can fire in certain race conditions, i.e, when + * all waiters have been awakened but have not yet had a chance to + * decrement cd_ntxwaiters. + */ + + //DEBUGASSERT(dev->cd_ntxwaiters == 0); + +#if 0 /* REVISIT */ /* When the H/W FIFO has been emptied, we can disable further TX * interrupts. + * + * REVISIT: The fact that the S/W FIFO is empty does not mean that + * the H/W FIFO is also empty. If we really want this to work this + * way, then we would probably need and additional parameter to tell + * us if the H/W FIFO is empty. */ dev_txint(dev, false); - } #endif + } return ret; } diff --git a/drivers/mtd/ftl.c b/drivers/mtd/ftl.c index a0260a1f4d3..6ed8529ac36 100644 --- a/drivers/mtd/ftl.c +++ b/drivers/mtd/ftl.c @@ -329,7 +329,7 @@ static ssize_t ftl_flush(FAR void *priv, FAR const uint8_t *buffer, buffer += dev->geo.erasesize; } - /* Finally, handler any partial blocks after the last full erase block */ + /* Finally, handle any partial blocks after the last full erase block */ if (remaining > 0) { diff --git a/drivers/mtd/mtd_progmem.c b/drivers/mtd/mtd_progmem.c index ac9be0f4977..8fcda67aae9 100644 --- a/drivers/mtd/mtd_progmem.c +++ b/drivers/mtd/mtd_progmem.c @@ -41,6 +41,7 @@ #include #include +#include #include #include @@ -199,15 +200,13 @@ static ssize_t progmem_bread(FAR struct mtd_dev_s *dev, off_t startblock, { FAR struct progmem_dev_s *priv = (FAR struct progmem_dev_s *)dev; FAR const uint8_t *src; - size_t offset; /* Read the specified blocks into the provided user buffer and return * status (The positive, number of blocks actually read or a negated * errno). */ - offset = startblock << priv->blkshift; - src = (FAR const uint8_t *)up_progmem_getaddress(offset); + src = (FAR const uint8_t *)up_progmem_getaddress(startblock); memcpy(buffer, src, nblocks << priv->blkshift); return nblocks; } diff --git a/drivers/serial/Make.defs b/drivers/serial/Make.defs index b99f4eb3621..bcbffd8368e 100644 --- a/drivers/serial/Make.defs +++ b/drivers/serial/Make.defs @@ -37,7 +37,11 @@ ifneq ($(CONFIG_NFILE_DESCRIPTORS),0) # Include serial drivers -CSRCS += serial.c serialirq.c lowconsole.c +CSRCS += serial.c serial_io.c lowconsole.c + +ifeq ($(CONFIG_SERIAL_DMA),y) + CSRCS += serial_dma.c +endif ifeq ($(CONFIG_16550_UART),y) CSRCS += uart_16550.c diff --git a/drivers/serial/serialirq.c b/drivers/serial/serial_dma.c similarity index 53% rename from drivers/serial/serialirq.c rename to drivers/serial/serial_dma.c index b58867f71de..f363f9ed0aa 100644 --- a/drivers/serial/serialirq.c +++ b/drivers/serial/serial_dma.c @@ -1,8 +1,8 @@ /************************************************************************************ - * drivers/serial/serialirq.c + * drivers/serial/serial_dma.c * - * Copyright (C) 2007-2009, 2011, 2015 Gregory Nutt. All rights reserved. - * Author: Gregory Nutt + * Copyright (C) 2015 Gregory Nutt. All rights reserved. + * Author: Max Neklyudov * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -43,96 +43,10 @@ #include #include #include + #include -/************************************************************************************ - * Pre-processor Definitions - ************************************************************************************/ - -/************************************************************************************ - * Private Types - ************************************************************************************/ - -/************************************************************************************ - * Private Function Prototypes - ************************************************************************************/ - -/************************************************************************************ - * Private Variables - ************************************************************************************/ - -/************************************************************************************ - * Private Functions - ************************************************************************************/ - -/************************************************************************************ - * Name: uart_dorxflowcontrol - * - * Description: - * Handle RX flow control using watermark levels or not - * - ************************************************************************************/ - -#ifdef CONFIG_SERIAL_IFLOWCONTROL -#ifdef CONFIG_SERIAL_IFLOWCONTROL_WATERMARKS -static inline bool uart_dorxflowcontrol(FAR uart_dev_t *dev, - FAR struct uart_buffer_s *rxbuf, - unsigned int watermark) -{ - unsigned int nbuffered; - - /* How many bytes are buffered */ - - if (rxbuf->head >= rxbuf->tail) - { - nbuffered = rxbuf->head - rxbuf->tail; - } - else - { - nbuffered = rxbuf->size - rxbuf->tail + rxbuf->head; - } - - /* Is the level now above the watermark level that we need to report? */ - - if (nbuffered >= watermark) - { - /* Let the lower level driver know that the watermark level has been - * crossed. It will probably activate RX flow control. - */ - - if (uart_rxflowcontrol(dev, nbuffered, true)) - { - /* Low-level driver activated RX flow control, exit loop now. */ - - return true; - } - } - - return false; -} -#else -static inline bool uart_dorxflowcontrol(FAR uart_dev_t *dev, - FAR struct uart_buffer_s *rxbuf, - bool is_full) -{ - /* Check if RX buffer is full and allow serial low-level driver to pause - * processing. This allows proper utilization of hardware flow control. - */ - - if (is_full) - { - if (uart_rxflowcontrol(dev, rxbuf->size, true)) - { - /* Low-level driver activated RX flow control, exit loop now. */ - - return true; - } - } - - return false; -} -#endif -#endif +#ifdef CONFIG_SERIAL_DMA /************************************************************************************ * Public Functions @@ -146,7 +60,6 @@ static inline bool uart_dorxflowcontrol(FAR uart_dev_t *dev, * ************************************************************************************/ -#ifdef CONFIG_SERIAL_DMA void uart_xmitchars_dma(FAR uart_dev_t *dev) { FAR struct uart_dmaxfer_s *xfer = &dev->dmatx; @@ -175,7 +88,6 @@ void uart_xmitchars_dma(FAR uart_dev_t *dev) uart_dmasend(dev); } -#endif /* CONFIG_SERIAL_DMA */ /************************************************************************************ * Name: uart_xmitchars_done @@ -187,7 +99,6 @@ void uart_xmitchars_dma(FAR uart_dev_t *dev) * ************************************************************************************/ -#ifdef CONFIG_SERIAL_DMA void uart_xmitchars_done(FAR uart_dev_t *dev) { FAR struct uart_dmaxfer_s *xfer = &dev->dmatx; @@ -209,7 +120,6 @@ void uart_xmitchars_done(FAR uart_dev_t *dev) uart_datasent(dev); } } -#endif /* CONFIG_SERIAL_DMA */ /************************************************************************************ * Name: uart_recvchars_dma @@ -219,12 +129,12 @@ void uart_xmitchars_done(FAR uart_dev_t *dev) * ************************************************************************************/ -#ifdef CONFIG_SERIAL_DMA void uart_recvchars_dma(FAR uart_dev_t *dev) { FAR struct uart_dmaxfer_s *xfer = &dev->dmarx; FAR struct uart_buffer_s *rxbuf = &dev->recv; #ifdef CONFIG_SERIAL_IFLOWCONTROL_WATERMARKS + unsigned int nbuffered; unsigned int watermark; #endif bool is_full; @@ -245,14 +155,46 @@ void uart_recvchars_dma(FAR uart_dev_t *dev) #ifdef CONFIG_SERIAL_IFLOWCONTROL #ifdef CONFIG_SERIAL_IFLOWCONTROL_WATERMARKS - if (uart_dorxflowcontrol(dev, rxbuf, watermark)) + /* How many bytes are buffered */ + + if (rxbuf->head >= rxbuf->tail) { - return; + nbuffered = rxbuf->head - rxbuf->tail; } -#else - if (uart_dorxflowcontrol(dev, rxbuf, is_full)) + else { - return; + nbuffered = rxbuf->size - rxbuf->tail + rxbuf->head; + } + + /* Is the level now above the watermark level that we need to report? */ + + if (nbuffered >= watermark) + { + /* Let the lower level driver know that the watermark level has been + * crossed. It will probably activate RX flow control. + */ + + if (uart_rxflowcontrol(dev, nbuffered, true)) + { + /* Low-level driver activated RX flow control, return now. */ + + return; + } + } + +#else + /* Check if RX buffer is full and allow serial low-level driver to pause + * processing. This allows proper utilization of hardware flow control. + */ + + if (is_full) + { + if (uart_rxflowcontrol(dev, rxbuf->size, true)) + { + /* Low-level driver activated RX flow control, return now. */ + + return; + } } #endif #endif @@ -292,7 +234,6 @@ void uart_recvchars_dma(FAR uart_dev_t *dev) uart_dmareceive(dev); } -#endif /* CONFIG_SERIAL_DMA */ /************************************************************************************ * Name: uart_recvchars_done @@ -304,7 +245,6 @@ void uart_recvchars_dma(FAR uart_dev_t *dev) * ************************************************************************************/ -#ifdef CONFIG_SERIAL_DMA void uart_recvchars_done(FAR uart_dev_t *dev) { FAR struct uart_dmaxfer_s *xfer = &dev->dmarx; @@ -326,153 +266,5 @@ void uart_recvchars_done(FAR uart_dev_t *dev) uart_datareceived(dev); } } + #endif /* CONFIG_SERIAL_DMA */ - -/************************************************************************************ - * Name: uart_xmitchars - * - * Description: - * This function is called from the UART interrupt handler when an interrupt - * is received indicating that there is more space in the transmit FIFO. This - * function will send characters from the tail of the xmit buffer while the driver - * write() logic adds data to the head of the xmit buffer. - * - ************************************************************************************/ - -void uart_xmitchars(FAR uart_dev_t *dev) -{ - uint16_t nbytes = 0; - - /* Send while we still have data in the TX buffer & room in the fifo */ - - while (dev->xmit.head != dev->xmit.tail && uart_txready(dev)) - { - /* Send the next byte */ - - uart_send(dev, dev->xmit.buffer[dev->xmit.tail]); - nbytes++; - - /* Increment the tail index */ - - if (++(dev->xmit.tail) >= dev->xmit.size) - { - dev->xmit.tail = 0; - } - } - - /* When all of the characters have been sent from the buffer disable the TX - * interrupt. - * - * Potential bug? If nbytes == 0 && (dev->xmit.head == dev->xmit.tail) && - * dev->xmitwaiting == true, then disabling the TX interrupt will leave - * the uart_write() logic waiting to TX to complete with no TX interrupts. - * Can that happen? - */ - - if (dev->xmit.head == dev->xmit.tail) - { - uart_disabletxint(dev); - } - - /* If any bytes were removed from the buffer, inform any waiters there there is - * space available. - */ - - if (nbytes) - { - uart_datasent(dev); - } -} - -/************************************************************************************ - * Name: uart_receivechars - * - * Description: - * This function is called from the UART interrupt handler when an interrupt - * is received indicating that are bytes available in the receive fifo. This - * function will add chars to head of receive buffer. Driver read() logic will - * take characters from the tail of the buffer. - * - ************************************************************************************/ - -void uart_recvchars(FAR uart_dev_t *dev) -{ - FAR struct uart_buffer_s *rxbuf = &dev->recv; -#ifdef CONFIG_SERIAL_IFLOWCONTROL_WATERMARKS - unsigned int watermark; -#endif - unsigned int status; - int nexthead = rxbuf->head + 1; - uint16_t nbytes = 0; - bool is_full; - - if (nexthead >= rxbuf->size) - { - nexthead = 0; - } - -#ifdef CONFIG_SERIAL_IFLOWCONTROL_WATERMARKS - /* Pre-calcuate the watermark level that we will need to test against. */ - - watermark = (CONFIG_SERIAL_IFLOWCONTROL_UPPER_WATERMARK * rxbuf->size) / 100; -#endif - - /* Loop putting characters into the receive buffer until there are no further - * characters to available. - */ - - while (uart_rxavailable(dev)) - { - is_full = (nexthead == rxbuf->tail); - char ch; - -#ifdef CONFIG_SERIAL_IFLOWCONTROL -#ifdef CONFIG_SERIAL_IFLOWCONTROL_WATERMARKS - if (uart_dorxflowcontrol(dev, rxbuf, watermark)) - { - break; - } -#else - if (uart_dorxflowcontrol(dev, rxbuf, is_full)) - { - break; - } -#endif -#endif - - ch = uart_receive(dev, &status); - - /* If the RX buffer becomes full, then the serial data is discarded. This is - * necessary because on most serial hardware, you must read the data in order - * to clear the RX interrupt. An option on some hardware might be to simply - * disable RX interrupts until the RX buffer becomes non-FULL. However, that - * would probably just cause the overrun to occur in hardware (unless it has - * some large internal buffering). - */ - - if (!is_full) - { - /* Add the character to the buffer */ - - rxbuf->buffer[rxbuf->head] = ch; - nbytes++; - - /* Increment the head index */ - - rxbuf->head = nexthead; - if (++nexthead >= rxbuf->size) - { - nexthead = 0; - } - } - } - - /* If any bytes were added to the buffer, inform any waiters there is new - * incoming data available. - */ - - if (nbytes) - { - uart_datareceived(dev); - } -} diff --git a/drivers/serial/serial_io.c b/drivers/serial/serial_io.c new file mode 100644 index 00000000000..9d28d64b74e --- /dev/null +++ b/drivers/serial/serial_io.c @@ -0,0 +1,232 @@ +/************************************************************************************ + * drivers/serial/serial_io.c + * + * Copyright (C) 2007-2009, 2011, 2015 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************************************/ + +/************************************************************************************ + * Included Files + ************************************************************************************/ + +#include + +#include +#include +#include +#include + +#include + +/************************************************************************************ + * Public Functions + ************************************************************************************/ + +/************************************************************************************ + * Name: uart_xmitchars + * + * Description: + * This function is called from the UART interrupt handler when an interrupt + * is received indicating that there is more space in the transmit FIFO. This + * function will send characters from the tail of the xmit buffer while the driver + * write() logic adds data to the head of the xmit buffer. + * + ************************************************************************************/ + +void uart_xmitchars(FAR uart_dev_t *dev) +{ + uint16_t nbytes = 0; + + /* Send while we still have data in the TX buffer & room in the fifo */ + + while (dev->xmit.head != dev->xmit.tail && uart_txready(dev)) + { + /* Send the next byte */ + + uart_send(dev, dev->xmit.buffer[dev->xmit.tail]); + nbytes++; + + /* Increment the tail index */ + + if (++(dev->xmit.tail) >= dev->xmit.size) + { + dev->xmit.tail = 0; + } + } + + /* When all of the characters have been sent from the buffer disable the TX + * interrupt. + * + * Potential bug? If nbytes == 0 && (dev->xmit.head == dev->xmit.tail) && + * dev->xmitwaiting == true, then disabling the TX interrupt will leave + * the uart_write() logic waiting to TX to complete with no TX interrupts. + * Can that happen? + */ + + if (dev->xmit.head == dev->xmit.tail) + { + uart_disabletxint(dev); + } + + /* If any bytes were removed from the buffer, inform any waiters there there is + * space available. + */ + + if (nbytes) + { + uart_datasent(dev); + } +} + +/************************************************************************************ + * Name: uart_receivechars + * + * Description: + * This function is called from the UART interrupt handler when an interrupt + * is received indicating that are bytes available in the receive fifo. This + * function will add chars to head of receive buffer. Driver read() logic will + * take characters from the tail of the buffer. + * + ************************************************************************************/ + +void uart_recvchars(FAR uart_dev_t *dev) +{ + FAR struct uart_buffer_s *rxbuf = &dev->recv; +#ifdef CONFIG_SERIAL_IFLOWCONTROL_WATERMARKS + unsigned int watermark; +#endif + unsigned int status; + int nexthead = rxbuf->head + 1; + uint16_t nbytes = 0; + + if (nexthead >= rxbuf->size) + { + nexthead = 0; + } + +#ifdef CONFIG_SERIAL_IFLOWCONTROL_WATERMARKS + /* Pre-calcuate the watermark level that we will need to test against. */ + + watermark = (CONFIG_SERIAL_IFLOWCONTROL_UPPER_WATERMARK * rxbuf->size) / 100; +#endif + + /* Loop putting characters into the receive buffer until there are no further + * characters to available. + */ + + while (uart_rxavailable(dev)) + { + bool is_full = (nexthead == rxbuf->tail); + char ch; + +#ifdef CONFIG_SERIAL_IFLOWCONTROL +#ifdef CONFIG_SERIAL_IFLOWCONTROL_WATERMARKS + unsigned int nbuffered; + + /* How many bytes are buffered */ + + if (rxbuf->head >= rxbuf->tail) + { + nbuffered = rxbuf->head - rxbuf->tail; + } + else + { + nbuffered = rxbuf->size - rxbuf->tail + rxbuf->head; + } + + /* Is the level now above the watermark level that we need to report? */ + + if (nbuffered >= watermark) + { + /* Let the lower level driver know that the watermark level has been + * crossed. It will probably activate RX flow control. + */ + + if (uart_rxflowcontrol(dev, nbuffered, true)) + { + /* Low-level driver activated RX flow control, exit loop now. */ + + break; + } + } +#else + /* Check if RX buffer is full and allow serial low-level driver to pause + * processing. This allows proper utilization of hardware flow control. + */ + + if (is_full) + { + if (uart_rxflowcontrol(dev, rxbuf->size, true)) + { + /* Low-level driver activated RX flow control, exit loop now. */ + + break; + } + } +#endif +#endif + + ch = uart_receive(dev, &status); + + /* If the RX buffer becomes full, then the serial data is discarded. This is + * necessary because on most serial hardware, you must read the data in order + * to clear the RX interrupt. An option on some hardware might be to simply + * disable RX interrupts until the RX buffer becomes non-FULL. However, that + * would probably just cause the overrun to occur in hardware (unless it has + * some large internal buffering). + */ + + if (!is_full) + { + /* Add the character to the buffer */ + + rxbuf->buffer[rxbuf->head] = ch; + nbytes++; + + /* Increment the head index */ + + rxbuf->head = nexthead; + if (++nexthead >= rxbuf->size) + { + nexthead = 0; + } + } + } + + /* If any bytes were added to the buffer, inform any waiters there is new + * incoming data available. + */ + + if (nbytes) + { + uart_datareceived(dev); + } +} diff --git a/drivers/wireless/cc3000/Kconfig b/drivers/wireless/cc3000/Kconfig index 038fd348f21..c1e65c40997 100644 --- a/drivers/wireless/cc3000/Kconfig +++ b/drivers/wireless/cc3000/Kconfig @@ -8,6 +8,8 @@ config WL_CC3000 default n select SPI select ARCH_HAVE_NET + select CRYPTO + select CRYPTO_SW_AES ---help--- Enable support for the TI CC3000 Wifi Module diff --git a/drivers/wireless/cc3000/Make.defs b/drivers/wireless/cc3000/Make.defs index 8ab9cb9211b..9571d750ab5 100644 --- a/drivers/wireless/cc3000/Make.defs +++ b/drivers/wireless/cc3000/Make.defs @@ -38,7 +38,7 @@ ifeq ($(CONFIG_WL_CC3000),y) # Include cc3000 drivers CSRCS += cc3000.c cc3000_common.c cc3000drv.c evnt_handler.c hci.c netapp.c -CSRCS += nvmem.c security.c socket.c socket_imp.c wlan.c +CSRCS += nvmem.c socket.c socket_imp.c wlan.c # Include wireless devices build support diff --git a/drivers/wireless/cc3000/wlan.c b/drivers/wireless/cc3000/wlan.c index 6d9ad129971..3b3eda23e87 100644 --- a/drivers/wireless/cc3000/wlan.c +++ b/drivers/wireless/cc3000/wlan.c @@ -43,11 +43,12 @@ #include #include +#include + #include #include #include #include -#include #include #include "cc3000.h" @@ -1204,6 +1205,58 @@ long wlan_smart_config_set_prefix(FAR char *cNewPrefix) return ret; } +/**************************************************************************** + * Name: aes_read_key + * + * Description: + * Reads AES128 key from EEPROM. Reads the AES128 key from fileID #12 in + * EEPROM returns an error if the key does not exist. + * + * Input Parameters: + * key AES128 key of size 16 bytes + * + * Returned Value + * On success 0, error otherwise. + * + ****************************************************************************/ + +#ifndef CC3000_UNENCRYPTED_SMART_CONFIG +signed long aes_read_key(uint8_t *key) +{ + signed long returnValue; + + returnValue = nvmem_read(NVMEM_AES128_KEY_FILEID, AES128_KEY_SIZE, 0, key); + + return returnValue; +} +#endif + +/**************************************************************************** + * Name: aes_write_key + * + * Description: + * Writes AES128 key from EEPROM Writes the AES128 key to fileID #12 in + * EEPROM + * + * Input Parameters: + * key AES128 key of size 16 bytes + * + * Returned Value + * On success 0, error otherwise. + * + ****************************************************************************/ + +#if 0 //#ifndef CC3000_UNENCRYPTED_SMART_CONFIG +signed long aes_write_key(uint8_t *key) +{ + signed long returnValue; + + returnValue = nvmem_write(NVMEM_AES128_KEY_FILEID, AES128_KEY_SIZE, 0, key); + + return returnValue; +} +#endif + /**************************************************************************** * Name: wlan_smart_config_process * diff --git a/include/nuttx/can.h b/include/nuttx/can.h index 904a3a16d37..38f84a42ff1 100644 --- a/include/nuttx/can.h +++ b/include/nuttx/can.h @@ -51,6 +51,10 @@ #include #include +#ifdef CONFIG_CAN_TXREADY +# include +#endif + #ifdef CONFIG_CAN /************************************************************************************ @@ -70,6 +74,12 @@ * CONFIG_CAN_LOOPBACK - A CAN driver may or may not support a loopback * mode for testing. If the driver does support loopback mode, the setting * will enable it. (If the driver does not, this setting will have no effect). + * CONFIG_CAN_TXREADY - Add support for the can_txready() callback. This is needed + * only for CAN hardware the supports an separate H/W TX message FIFO. The call + * back is needed to keep the S/W FIFO and the H/W FIFO in sync. Work queue + * support is needed for this feature. + * CONFIG_CAN_TXREADY_HIPRI or CONFIG_CAN_TXREADY_LOPRI - Selects which work queue + * will be used for the can_txready() processing. */ /* Default configuration settings that may be overridden in the NuttX configuration @@ -405,6 +415,9 @@ struct can_dev_s sem_t cd_closesem; /* Locks out new opens while close is in progress */ struct can_txfifo_s cd_xmit; /* Describes transmit FIFO */ struct can_rxfifo_s cd_recv; /* Describes receive FIFO */ +#ifdef CONFIG_CAN_TXREADY + struct work_s cd_work; /* Use to manage can_txready() work */ +#endif /* List of pending RTR requests */ struct can_rtrwait_s cd_rtr[CONFIG_CAN_NPENDINGRTR]; FAR const struct can_ops_s *cd_ops; /* Arch-specific operations */ @@ -508,7 +521,7 @@ int can_register(FAR const char *path, FAR struct can_dev_s *dev); int can_receive(FAR struct can_dev_s *dev, FAR struct can_hdr_s *hdr, FAR uint8_t *data); -/**************************************************************************** +/************************************************************************************ * Name: can_txdone * * Description: @@ -541,8 +554,8 @@ int can_receive(FAR struct can_dev_s *dev, FAR struct can_hdr_s *hdr, * * 2a. H/W TX FIFO (CONFIG_CAN_TXREADY=y) and S/W TX FIFO not full * - * This function will be back from can_xmit immediately when a new CAN - * message is added to H/W TX FIFO: + * This function will be called back from dev_send() immediately when a + * new CAN message is added to H/W TX FIFO: * * can_write() -> can_xmit() -> dev_send() -> can_txdone() * @@ -554,9 +567,11 @@ int can_receive(FAR struct can_dev_s *dev, FAR struct can_hdr_s *hdr, * 2b. H/W TX FIFO (CONFIG_CAN_TXREADY=y) and S/W TX FIFO full * * In this case, the thread calling can_write() is blocked waiting for - * space in the S/W TX FIFO. + * space in the S/W TX FIFO. can_txdone() will be called, indirectly, + * from can_txready_work() running on the thread of the work queue. * - * CAN interrupt -> can_txready() -> can_xmit() -> dev_send() -> can_txdone() + * CAN interrupt -> can_txready() -> Schedule can_txready_work() + * can_txready_work() -> can_xmit() -> dev_send() -> can_txdone() * * The call dev_send() should not fail in this case and the subsequent * call to can_txdone() will make space in the S/W TX FIFO and will @@ -570,7 +585,13 @@ int can_receive(FAR struct can_dev_s *dev, FAR struct can_hdr_s *hdr, * Returned Value: * OK on success; a negated errno on failure. * - ****************************************************************************/ + * Assumptions: + * Interrupts are disabled. This is required by can_xmit() which is called + * by this function. Interrupts are explicitly disabled when called + * through can_write(). Interrupts are expected be disabled when called + * from the CAN interrupt handler. + * + ************************************************************************************/ int can_txdone(FAR struct can_dev_s *dev); @@ -578,37 +599,39 @@ int can_txdone(FAR struct can_dev_s *dev); * Name: can_txready * * Description: - * Called from the CAN interrupt handler at the completion of a send operation. - * This interface is needed only for CAN hardware that supports queing of - * outgoing messages in a H/W FIFO. + * Called from the CAN interrupt handler at the completion of a send + * operation. This interface is needed only for CAN hardware that + * supports queing of outgoing messages in a H/W FIFO. * - * The CAN upper half driver also supports a queue of output messages in a S/W - * FIFO. Messages are added to that queue when when can_write() is called and - * removed from the queue in can_txdone() when each TX message is complete. + * The CAN upper half driver also supports a queue of output messages in a + * S/W FIFO. Messages are added to that queue when when can_write() is + * called and removed from the queue in can_txdone() when each TX message + * is complete. * - * After each message is added to the S/W FIFO, the CAN upper half driver will - * attempt to send the message by calling into the lower half driver. That send - * will not be performed if the lower half driver is busy, i.e., if dev_txready() - * returns false. In that case, the number of messages in the S/W FIFO can grow. - * If the S/W FIFO becomes full, then can_write() will wait for space in the - * S/W FIFO. + * After each message is added to the S/W FIFO, the CAN upper half driver + * will attempt to send the message by calling into the lower half driver. + * That send will not be performed if the lower half driver is busy, i.e., + * if dev_txready() returns false. In that case, the number of messages in + * the S/W FIFO can grow. If the S/W FIFO becomes full, then can_write() + * will wait for space in the S/W FIFO. * - * If the CAN hardware does not support a H/W FIFO then busy means that the - * hardware is actively sending the message and is guaranteed to become non- - * busy (i.e, dev_txready()) when the send transfer completes and can_txdone() - * is called. So the call to can_txdone() means that the transfer has - * completed and also that the hardware is ready to accept another transfer. + * If the CAN hardware does not support a H/W FIFO then busy means that + * the hardware is actively sending the message and is guaranteed to + * become non-busy (i.e, dev_txready()) when the send transfer completes + * and can_txdone() is called. So the call to can_txdone() means that the + * transfer has completed and also that the hardware is ready to accept + * another transfer. * - * If the CAN hardware supports a H/W FIFO, can_txdone() is not called when - * the tranfer is complete, but rather when the transfer is queued in the - * H/W FIFO. When the H/W FIFO becomes full, then dev_txready() will report - * false and the number of queued messages in the S/W FIFO will grow. + * If the CAN hardware supports a H/W FIFO, can_txdone() is not called + * when the tranfer is complete, but rather when the transfer is queued in + * the H/W FIFO. When the H/W FIFO becomes full, then dev_txready() will + * report false and the number of queued messages in the S/W FIFO will grow. * * There is no mechanism in this case to inform the upper half driver when * the hardware is again available, when there is again space in the H/W * FIFO. can_txdone() will not be called again. If the S/W FIFO becomes - * full, then the upper half driver will wait for space to become available, - * but there is no event to awaken it and the driver will hang. + * full, then the upper half driver will wait for space to become + * available, but there is no event to awaken it and the driver will hang. * * Enabling this feature adds support for the can_txready() interface. * This function is called from the lower half driver's CAN interrupt @@ -622,6 +645,10 @@ int can_txdone(FAR struct can_dev_s *dev); * Returned Value: * OK on success; a negated errno on failure. * + * Assumptions: + * Interrupts are disabled. This function may execute in the context of + * and interrupt handler. + * ************************************************************************************/ #ifdef CONFIG_CAN_TXREADY diff --git a/include/nuttx/crypto/aes.h b/include/nuttx/crypto/aes.h new file mode 100644 index 00000000000..5c4d3e80507 --- /dev/null +++ b/include/nuttx/crypto/aes.h @@ -0,0 +1,108 @@ +/**************************************************************************** + * include/nuttx/crypto/aes.h + * + * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ + * Extracted from the CC3000 Host Driver Implementation. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the + * distribution. + * + * Neither the name of Texas Instruments Incorporated nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +#ifndef __INCLUDE_NUTTX_CRYPTO_AES_H +#define __INCLUDE_NUTTX_CRYPTO_AES_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define AES128_KEY_SIZE 16 + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +#ifdef __cplusplus +extern "C" +{ +#endif + +/**************************************************************************** + * Public Function Prototypes +/**************************************************************************** + +/**************************************************************************** + * Name: aes_encrypt + * + * Description: + * AES128 encryption: Given AES128 key and 16 bytes plain text, cipher + * text of 16 bytes is computed. The AES implementation is in mode ECB + * (Electronic Code Book). + * + * Input Parameters: + * key AES128 key of size 16 bytes + * state 16 bytes of plain text and cipher text + * + * Returned Value + * None + * + ****************************************************************************/ + +void aes_encrypt(FAR uint8_t *state, FAR const uint8_t *key); + +/**************************************************************************** + * Name: aes_decrypt + * + * Description: + * AES128 decryption: Given AES128 key and 16 bytes cipher text, plain + * text of 16 bytes is computed The AES implementation is in mode ECB + * (Electronic Code Book). + * + * Input Parameters: + * key AES128 key of size 16 bytes + * state 16 bytes of plain text and cipher text + * + * Returned Value + * None + * + ****************************************************************************/ + +void aes_decrypt(FAR uint8_t *state, FAR const uint8_t *key); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* __INCLUDE_NUTTX_CRYPTO_AES_H */ diff --git a/include/nuttx/wireless/cc3000/security.h b/include/nuttx/wireless/cc3000/security.h deleted file mode 100644 index 4a57340bb1c..00000000000 --- a/include/nuttx/wireless/cc3000/security.h +++ /dev/null @@ -1,142 +0,0 @@ -/**************************************************************************** - * security.h - CC3000 Host Driver Implementation. - * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the - * distribution. - * - * Neither the name of Texas Instruments Incorporated nor the names of - * its contributors may be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - ****************************************************************************/ - -#ifndef _INCLUDE_NUTTX_WIRELESS_CC3000_SECURITY_H -#define _INCLUDE_NUTTX_WIRELESS_CC3000_SECURITY_H - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include "nvmem.h" - -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -#define AES128_KEY_SIZE 16 - -#ifndef CC3000_UNENCRYPTED_SMART_CONFIG - -/**************************************************************************** - * Public Data - ****************************************************************************/ - -#ifdef __cplusplus -extern "C" -{ -#endif - -/**************************************************************************** - * Public Function Prototypes -/**************************************************************************** - -/**************************************************************************** - * Name: aes_encrypt - * - * Description: - * AES128 encryption: Given AES128 key and 16 bytes plain text, cipher - * text of 16 bytes is computed. The AES implementation is in mode ECB - * (Electronic Code Book). - * - * Input Parameters: - * key AES128 key of size 16 bytes - * state 16 bytes of plain text and cipher text - * - * Returned Value - * None - * - ****************************************************************************/ - -void aes_encrypt(uint8_t *state, uint8_t *key); - -/**************************************************************************** - * Name: aes_decrypt - * - * Description: - * AES128 decryption: Given AES128 key and 16 bytes cipher text, plain - * text of 16 bytes is computed The AES implementation is in mode ECB - * (Electronic Code Book). - * - * Input Parameters: - * key AES128 key of size 16 bytes - * state 16 bytes of plain text and cipher text - * - * Returned Value - * None - * - ****************************************************************************/ - -void aes_decrypt(uint8_t *state, uint8_t *key); - -/**************************************************************************** - * Name: aes_read_key - * - * Description: - * Reads AES128 key from EEPROM. Reads the AES128 key from fileID #12 in - * EEPROM returns an error if the key does not exist. - * - * Input Parameters: - * key AES128 key of size 16 bytes - * - * Returned Value - * On success 0, error otherwise. - * - ****************************************************************************/ - -signed long aes_read_key(uint8_t *key); - -/**************************************************************************** - * Name: aes_write_key - * - * Description: - * Writes AES128 key from EEPROM Writes the AES128 key to fileID #12 in - * EEPROM - * - * Input Parameters: - * key AES128 key of size 16 bytes - * - * Returned Value - * On success 0, error otherwise. - * - ****************************************************************************/ - -signed long aes_write_key(uint8_t *key); - -#ifdef __cplusplus -} -#endif /* __cplusplus */ - -#endif /* CC3000_UNENCRYPTED_SMART_CONFIG */ -#endif /* _INCLUDE_NUTTX_WIRELESS_CC3000_SECURITY_H */ diff --git a/include/nuttx/wireless/cc3000/wlan.h b/include/nuttx/wireless/cc3000/wlan.h index 867ea2032b9..b0a07c67d83 100644 --- a/include/nuttx/wireless/cc3000/wlan.h +++ b/include/nuttx/wireless/cc3000/wlan.h @@ -489,6 +489,40 @@ long wlan_smart_config_stop(void); long wlan_smart_config_set_prefix(char* cNewPrefix); +/**************************************************************************** + * Name: aes_read_key + * + * Description: + * Reads AES128 key from EEPROM. Reads the AES128 key from fileID #12 in + * EEPROM returns an error if the key does not exist. + * + * Input Parameters: + * key AES128 key of size 16 bytes + * + * Returned Value + * On success 0, error otherwise. + * + ****************************************************************************/ + +signed long aes_read_key(FAR uint8_t *key); + +/**************************************************************************** + * Name: aes_write_key + * + * Description: + * Writes AES128 key from EEPROM Writes the AES128 key to fileID #12 in + * EEPROM + * + * Input Parameters: + * key AES128 key of size 16 bytes + * + * Returned Value + * On success 0, error otherwise. + * + ****************************************************************************/ + +signed long aes_write_key(FAR uint8_t *key); + /**************************************************************************** * Name: wlan_smart_config_process *