You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by GitBox <gi...@apache.org> on 2022/05/13 09:09:17 UTC

[GitHub] [incubator-nuttx] CV-Bowen commented on a diff in pull request #6259: arm/tlsr82: add hardware aes encrypt and decrypt support.

CV-Bowen commented on code in PR #6259:
URL: https://github.com/apache/incubator-nuttx/pull/6259#discussion_r872167800


##########
arch/arm/src/tlsr82/tlsr82_aes.c:
##########
@@ -0,0 +1,255 @@
+/****************************************************************************
+ * arch/arm/src/tlsr82/tlsr82_aes.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 <nuttx/config.h>
+
+#include <stdint.h>
+
+#include <nuttx/irq.h>
+#include <nuttx/crypto/crypto.h>
+#include <nuttx/semaphore.h>
+#include <assert.h>
+#include <debug.h>
+#include <semaphore.h>
+
+#include "hardware/tlsr82_aes.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define AES_BLK_SHIFT                   (4)
+#define AES_BLK_SIZE                    (1 << AES_BLK_SHIFT)
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private data
+ ****************************************************************************/
+
+static sem_t g_aes_sem = SEM_INITIALIZER(1);
+
+/****************************************************************************
+ * Public data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: tlsr82_aes_encrypt
+ *
+ * Description:
+ *   This function servers to perform aes_128 encryption for 16-Byte input
+ *   data with specific 16-Byte key.
+ *
+ * Input Parameters:
+ *   key    - the pointer to the 16-Byte key
+ *   data   - the pointer to the 16-Byte plain text
+ *   result - the pointer to the encryption result cipher text
+ *
+ * Returned Value:
+ *   0: Success, not 0: Failure
+ *
+ ****************************************************************************/
+
+int tlsr82_aes_encrypt(const uint8_t *key, const uint8_t *data,
+                       uint8_t *result)
+{
+  uint32_t tmp;
+  int i;
+
+  /* Trigger encrypt operation */
+
+  AES_CTRL_REG &= (~AES_CTRL_CODEC_TRIG);
+
+  /* Set the aes key */
+
+  for (i = 0; i < 16; i++)
+    {
+      AES_KEY_REG(i) = key[i];
+    }
+
+  /* Feed data to hardware */
+
+  while (AES_CTRL_REG & AES_CTRL_DATA_FEED)
+    {
+      tmp = data[0] + (data[1] << 8) + (data[2] << 16) + (data[3] << 24);
+      AES_DATA_REG = tmp;
+      data += 4;
+    }
+
+  /* Wait for the encrypt finished */
+
+  while ((AES_CTRL_REG & AES_CTRL_CODEC_FINISHED) == 0);
+
+  /* Asign the result */
+
+  for (i = 0; i < 4; i++)
+    {
+      tmp  = AES_DATA_REG;
+      *result++ = tmp & 0xff;
+      *result++ = (tmp >> 8) & 0xff;
+      *result++ = (tmp >> 16) & 0xff;
+      *result++ = (tmp >> 24) & 0xff;
+    }
+
+  return 0;
+}
+
+/****************************************************************************
+ * Name: tlsr82_aes_decrypt
+ *
+ * Description:
+ *   This function servers to perform aes_128 decryption for 16-Byte input
+ *   data with specific 16-Byte key.
+ *
+ * Input Parameters:
+ *   key    - the pointer to the 16-Byte key
+ *   data   - the pointer to the 16-Byte cipher text
+ *   result - the pointer to the decryption result plain text
+ *
+ * Returned Value:
+ *   0: Success, not 0: Failure
+ *
+ ****************************************************************************/
+
+int tlsr82_aes_decrypt(const uint8_t *key, const uint8_t *data,
+                       uint8_t *result)
+{
+  uint32_t tmp = 0;
+  int i = 0;
+
+  /* Trigger decrypt operation */
+
+  AES_CTRL_REG |= AES_CTRL_CODEC_TRIG;
+
+  /* Set the key */
+
+  for (i = 0; i < 16; i++)
+    {
+      AES_KEY_REG(i) = key[i];
+    }
+
+  /* Feed data to hardware */
+
+  while (AES_CTRL_REG & AES_CTRL_DATA_FEED)
+    {
+      tmp = data[0] + (data[1] << 8) + (data[2] << 16) + (data[3] << 24);
+      AES_DATA_REG = tmp;
+      data += 4;
+    }
+
+  /* Wait for decrypt finished */
+
+  while ((AES_CTRL_REG & AES_CTRL_CODEC_FINISHED) == 0);
+
+  /* Assign the result */
+
+  for (i = 0; i < 4; i++)
+    {
+      tmp       = AES_DATA_REG;
+      *result++ = tmp & 0xff;
+      *result++ = (tmp >> 8) & 0xff;
+      *result++ = (tmp >> 16) & 0xff;
+      *result++ = (tmp >> 24) & 0xff;
+    }
+
+  return 0;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+int aes_cypher(void *out, const void *in, size_t size, const void *iv,
+               const void *key, size_t keysize, int mode, int encrypt)
+{
+  int ret = OK;
+  int i;
+  int nblk;
+
+  const uint8_t *inbuf = (const uint8_t *)in;
+  uint8_t *outbuf = (uint8_t *)out;
+
+  /* Input check, error condition:
+   * 1. size == 0 or size is not aligned with AES_BLK_SIZE
+   * 2. not ECB mode, tlsr82 only support ECB mode
+   * 3. not aes-128, tlsr82 only support aes-128
+   */
+
+  if (!size || (size & (AES_BLK_SIZE - 1)))

Review Comment:
   Done



##########
arch/arm/src/tlsr82/tlsr82_aes.c:
##########
@@ -0,0 +1,255 @@
+/****************************************************************************
+ * arch/arm/src/tlsr82/tlsr82_aes.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 <nuttx/config.h>
+
+#include <stdint.h>
+
+#include <nuttx/irq.h>
+#include <nuttx/crypto/crypto.h>
+#include <nuttx/semaphore.h>
+#include <assert.h>
+#include <debug.h>
+#include <semaphore.h>
+
+#include "hardware/tlsr82_aes.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define AES_BLK_SHIFT                   (4)
+#define AES_BLK_SIZE                    (1 << AES_BLK_SHIFT)
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private data
+ ****************************************************************************/
+
+static sem_t g_aes_sem = SEM_INITIALIZER(1);
+
+/****************************************************************************
+ * Public data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: tlsr82_aes_encrypt
+ *
+ * Description:
+ *   This function servers to perform aes_128 encryption for 16-Byte input
+ *   data with specific 16-Byte key.
+ *
+ * Input Parameters:
+ *   key    - the pointer to the 16-Byte key
+ *   data   - the pointer to the 16-Byte plain text
+ *   result - the pointer to the encryption result cipher text
+ *
+ * Returned Value:
+ *   0: Success, not 0: Failure
+ *
+ ****************************************************************************/
+
+int tlsr82_aes_encrypt(const uint8_t *key, const uint8_t *data,
+                       uint8_t *result)
+{
+  uint32_t tmp;
+  int i;
+
+  /* Trigger encrypt operation */
+
+  AES_CTRL_REG &= (~AES_CTRL_CODEC_TRIG);

Review Comment:
   Done



##########
arch/arm/src/tlsr82/tlsr82_aes.c:
##########
@@ -0,0 +1,255 @@
+/****************************************************************************
+ * arch/arm/src/tlsr82/tlsr82_aes.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 <nuttx/config.h>
+
+#include <stdint.h>
+
+#include <nuttx/irq.h>
+#include <nuttx/crypto/crypto.h>
+#include <nuttx/semaphore.h>
+#include <assert.h>
+#include <debug.h>
+#include <semaphore.h>
+
+#include "hardware/tlsr82_aes.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define AES_BLK_SHIFT                   (4)
+#define AES_BLK_SIZE                    (1 << AES_BLK_SHIFT)
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private data
+ ****************************************************************************/
+
+static sem_t g_aes_sem = SEM_INITIALIZER(1);

Review Comment:
   Done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org