You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by GitBox <gi...@apache.org> on 2020/03/13 01:57:09 UTC

[GitHub] [mynewt-core] nkaje commented on a change in pull request #2175: da1469x: add initial crypto driver

nkaje commented on a change in pull request #2175: da1469x: add initial crypto driver
URL: https://github.com/apache/mynewt-core/pull/2175#discussion_r391991491
 
 

 ##########
 File path: hw/drivers/crypto/crypto_da1469x/src/crypto_da1469x.c
 ##########
 @@ -0,0 +1,246 @@
+/*
+ * 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.
+ */
+
+#include <string.h>
+#include <mcu/cmsis_nvic.h>
+#include <os/mynewt.h>
+#include <mcu/da1469x_clock.h>
+#include <crypto/crypto.h>
+#include <crypto_da1469x/crypto_da1469x.h>
+
+static struct os_mutex gmtx;
+
+#define VALID_AES_KEYLEN(x) (((x) == 128) || ((x) == 192) || ((x) == 256))
+
+static bool
+da1469x_has_support(struct crypto_dev *crypto, uint8_t op, uint16_t algo,
+                    uint16_t mode, uint16_t keylen)
+{
+    (void)op;
+
+    if (algo != CRYPTO_ALGO_AES || !VALID_AES_KEYLEN(keylen)) {
+        return false;
+    }
+
+    switch (mode) {
+    case CRYPTO_MODE_ECB:
+    case CRYPTO_MODE_CBC:
+    case CRYPTO_MODE_CTR:
+        return true;
+    }
+
+    return false;
+}
+
+static uint32_t
+da1469x_crypto_op(struct crypto_dev *crypto, uint8_t op, uint16_t algo,
+                  uint16_t mode, const uint8_t *key, uint16_t keylen,
+                  uint8_t *iv, const uint8_t *inbuf, uint8_t *outbuf,
+                  uint32_t len)
+{
+    uint8_t i;
+    uint8_t iv_save[AES_BLOCK_LEN];
+    unsigned int inc;
+    unsigned int carry;
+    unsigned int v;
+    uint32_t *keyreg;
+    uint32_t *keyp32;
+    uint32_t ctrl_reg;
+
+    if (!da1469x_has_support(crypto, op, algo, mode, keylen)) {
+        return 0;
+    }
+
+    if (mode == CRYPTO_MODE_CBC && op == CRYPTO_OP_DECRYPT) {
+        memcpy(iv_save, &inbuf[len-AES_BLOCK_LEN], AES_BLOCK_LEN);
+    }
+
+    os_mutex_pend(&gmtx, OS_TIMEOUT_NEVER);
+    da1469x_clock_amba_enable(CRG_TOP_CLK_AMBA_REG_AES_CLK_ENABLE_Msk);
+
+    /* enable AES / disable HASH */
+    ctrl_reg = ~AES_HASH_CRYPTO_CTRL_REG_CRYPTO_HASH_SEL_Msk;
+
+    /* Select mode
+     *
+     * Note: DS also mentions value 1 for ECB.
+     */
+    ctrl_reg &= ~AES_HASH_CRYPTO_CTRL_REG_CRYPTO_ALG_MD_Msk;
+    switch (mode) {
+    case CRYPTO_MODE_ECB:
+        ctrl_reg |= 0 << AES_HASH_CRYPTO_CTRL_REG_CRYPTO_ALG_MD_Pos;
+        break;
+    case CRYPTO_MODE_CBC:
+        ctrl_reg |= 3 << AES_HASH_CRYPTO_CTRL_REG_CRYPTO_ALG_MD_Pos;
+        break;
+    case CRYPTO_MODE_CTR:
+        ctrl_reg |= 2 << AES_HASH_CRYPTO_CTRL_REG_CRYPTO_ALG_MD_Pos;
+        break;
+    }
+
+    /* AES algorithm, there's no need to check algo here! */
+    ctrl_reg &= ~AES_HASH_CRYPTO_CTRL_REG_CRYPTO_ALG_MD_Msk;
 
 Review comment:
   This should be `ctrl_reg &= ~AES_HASH_CRYPTO_CTRL_REG_CRYPTO_ALG_Msk`

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services