Skip to content

Commit

Permalink
crypto:add some hardware support
Browse files Browse the repository at this point in the history
esp32c3: aes hmac-sha1 hmac-sha256
stm32f0l0g0 stm32l1 : aes
sam34: aes
lpc43: aes
Signed-off-by: anjiahao <anjiahao@xiaomi.com>
  • Loading branch information
anjiahao1 committed Dec 13, 2022
1 parent 54e7ea6 commit 8292c81
Show file tree
Hide file tree
Showing 20 changed files with 1,440 additions and 53 deletions.
4 changes: 4 additions & 0 deletions arch/arm/src/lpc43xx/Make.defs
Expand Up @@ -126,6 +126,10 @@ ifeq ($(CONFIG_CRYPTO_AES),y)
CHIP_CSRCS += lpc43_aes.c
endif

ifeq ($(CONFIG_CRYPTO_CRYPTODEV_HARDWARE),y)
CHIP_CSRCS += lpc43_crypto.c
endif

ifeq ($(CONFIG_LPC43_USB0),y)
ifeq ($(CONFIG_USBDEV),y)
CHIP_CSRCS += lpc43_usb0dev.c
Expand Down
2 changes: 1 addition & 1 deletion arch/arm/src/lpc43xx/lpc43_aes.c
Expand Up @@ -37,7 +37,7 @@

#include "arm_internal.h"
#include "chip.h"
#include <chip/lpc43_aes.h>
#include <hardware/lpc43_aes.h>

#define AES_BLOCK_SIZE 16

Expand Down
130 changes: 130 additions & 0 deletions arch/arm/src/lpc43xx/lpc43_crypto.c
@@ -0,0 +1,130 @@
/****************************************************************************
* arch/arm/src/lpc43xx/lpc43_crypto.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/

/****************************************************************************
* Included Files
****************************************************************************/

#include <errno.h>
#include <stdint.h>

#include <crypto/cryptodev.h>
#include <crypto/xform.h>
#include <nuttx/crypto/crypto.h>

/****************************************************************************
* Private Data
****************************************************************************/

static uint32_t g_lpc43_sesnum;

/****************************************************************************
* Private Functions
****************************************************************************/

/****************************************************************************
* Name: authcompute
*
* Description:
* Calculate the hash.
*
****************************************************************************/

/****************************************************************************
* Name: lpc43_newsession
*
* Description:
* create new session for crypto.
*
****************************************************************************/

static int lpc43_newsession(uint32_t *sid, struct cryptoini *cri)
{
if (sid == NULL || cri == NULL || cri->cri_alg != CRYPTO_AES_CBC)
{
return -EINVAL;
}

sid = g_lpc43_sesnum++;
return OK;
}

/****************************************************************************
* Name: lpc43_freesession
*
* Description:
* free session.
*
****************************************************************************/

static int lpc43_freesession(uint64_t tid)
{
return 0;
}

/****************************************************************************
* Name: lpc43_process
*
* Description:
* process session to use hardware algorithm.
*
****************************************************************************/

static int lpc43_process(struct cryptop *crp)
{
struct cryptodesc *crd;

for (crd = crp->crp_desc; crd; crd = crd->crd_next)
{
switch (crd->crd_alg)
{
case CRYPTO_AES_CBC:
return aes_cypher(crp->crp_dst, crp->crp_buf, crd->crd_len,
crd->crd_iv, crd->crd_key, 16,
AES_MODE_CBC, crd->crd_flags & CRD_F_ENCRYPT);
default:
return -EINVAL;
}
}
}

/****************************************************************************
* Name: hwcr_init
*
* Description:
* register the hardware crypto driver.
*
****************************************************************************/

void hwcr_init(void)
{
int hwcr_id;
int algs[CRYPTO_ALGORITHM_MAX + 1];

hwcr_id = crypto_get_driverid(0);
DEBUGASSERT(hwcr_id >= 0);

memset(algs, 0, sizeof(algs));

algs[CRYPTO_AES_CBC] = CRYPTO_ALG_FLAG_SUPPORTED;

crypto_register(hwcr_id, algs, lpc43_newsession,
lpc43_freesession, lpc43_process);
}
4 changes: 4 additions & 0 deletions arch/arm/src/sam34/Make.defs
Expand Up @@ -99,6 +99,10 @@ ifeq ($(CONFIG_SAM34_AES),y)
CHIP_CSRCS += sam_aes.c
endif

ifeq ($(CONFIG_CRYPTO_CRYPTODEV_HARDWARE),y)
CHIP_CSRCS += sam_crypto.c
endif

ifeq ($(CONFIG_SAM34_RTC),y)
CHIP_CSRCS += sam_rtc.c
endif
Expand Down
160 changes: 160 additions & 0 deletions arch/arm/src/sam34/sam_crypto.c
@@ -0,0 +1,160 @@
/****************************************************************************
* arch/arm/src/sam34/sam_crypto.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/

/****************************************************************************
* Included Files
****************************************************************************/

#include <errno.h>
#include <stdint.h>

#include <crypto/cryptodev.h>
#include <crypto/xform.h>
#include <nuttx/crypto/crypto.h>

/****************************************************************************
* Private Data
****************************************************************************/

FAR static uint32_t g_sam_sesnum = 0;

/****************************************************************************
* Private Functions
****************************************************************************/

/****************************************************************************
* Name: authcompute
*
* Description:
* Calculate the hash.
*
****************************************************************************/

/****************************************************************************
* Name: sam_newsession
*
* Description:
* create new session for crypto.
*
****************************************************************************/

static int sam_newsession(FAR uint32_t *sid, FAR struct cryptoini *cri)
{
if (sid == NULL || cri == NULL)
{
return -EINVAL;
}

switch (cri->cri_alg)
{
case CRYPTO_AES_CBC:
*sid = g_sam_sesnum++;
break;
case CRYPTO_AES_CTR:
if ((cri->cri_klen / 8 - 4) != 16)
{
/* sam aes-ctr key bits just support 128 */

return -EINVAL;
}

*sid = g_sam_sesnum++;
break;
default :
return -EINVAL;
}

return OK;
}

/****************************************************************************
* Name: sam_freesession
*
* Description:
* free session.
*
****************************************************************************/

static int sam_freesession(uint64_t tid)
{
return 0;
}

/****************************************************************************
* Name: sam_process
*
* Description:
* process session to use hardware algorithm.
*
****************************************************************************/

static int sam_process(struct cryptop *crp)
{
struct cryptodesc *crd;
uint8_t iv[AESCTR_BLOCKSIZE];

for (crd = crp->crp_desc; crd; crd = crd->crd_next)
{
switch (crd->crd_alg)
{
case CRYPTO_AES_CBC:
return aes_cypher(crp->crp_dst, crp->crp_buf, crd->crd_len,
crd->crd_iv, crd->crd_key, 16,
AES_MODE_CBC, crd->crd_flags & CRD_F_ENCRYPT);
case CRYPTO_AES_CTR:

memcpy(iv, crd->crd_key + crd->crd_klen / 8 - AESCTR_NONCESIZE,
AESCTR_NONCESIZE);
memcpy(iv + AESCTR_NONCESIZE, crd->crd_iv, AESCTR_IVSIZE);
memset(iv + AESCTR_NONCESIZE + AESCTR_IVSIZE , 0, 4);

return aes_cypher(crp->crp_dst, crp->crp_buf, crd->crd_len,
iv, crd->crd_key, crd->crd_klen / 8 - 4,
AES_MODE_CTR, crd->crd_flags & CRD_F_ENCRYPT);
default:
return -EINVAL;
}
}
}

/****************************************************************************
* Name: hwcr_init
*
* Description:
* register the hardware crypto driver.
*
****************************************************************************/

void hwcr_init(void)
{
int hwcr_id;
int algs[CRYPTO_ALGORITHM_MAX + 1];

hwcr_id = crypto_get_driverid(0);
DEBUGASSERT(hwcr_id >= 0);

memset(algs, 0, sizeof(algs));

algs[CRYPTO_AES_CBC] = CRYPTO_ALG_FLAG_SUPPORTED;
algs[CRYPTO_AES_CTR] = CRYPTO_ALG_FLAG_SUPPORTED;

crypto_register(hwcr_id, algs, sam_newsession,
sam_freesession, sam_process);
}
4 changes: 4 additions & 0 deletions arch/arm/src/stm32/Make.defs
Expand Up @@ -221,6 +221,10 @@ ifeq ($(CONFIG_STM32_AES),y)
CHIP_CSRCS += stm32_aes.c
endif

ifeq ($(CONFIG_CRYPTO_CRYPTODEV_HARDWARE),y)
CHIP_CSRCS += stm32_crypto.c
endif

ifeq ($(CONFIG_STM32_BBSRAM),y)
CHIP_CSRCS += stm32_bbsram.c
endif
Expand Down

0 comments on commit 8292c81

Please sign in to comment.