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 2019/07/11 07:36:23 UTC

[GitHub] [mynewt-core] mkiiskila commented on a change in pull request #1876: [RFC] Add cryptographic HASH driver

mkiiskila commented on a change in pull request #1876: [RFC] Add cryptographic HASH driver
URL: https://github.com/apache/mynewt-core/pull/1876#discussion_r302401196
 
 

 ##########
 File path: hw/drivers/hash/hash_stm32/src/hash_stm32.c
 ##########
 @@ -0,0 +1,190 @@
+/*
+ * 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/stm32_hal.h"
+#include "hash/hash.h"
+#include "hash_stm32/hash_stm32.h"
+
+static struct os_mutex gmtx;
+
+/* FIXME: update this for non-F4 models */
+#if defined(STM32F437xx) || defined(STM32F439xx) || defined(STM32F479xx)
+#define HAS_SHA2 1
+#endif
+
+static bool
+stm32_hash_has_support(struct hash_dev *hash, uint16_t algo)
+{
+    switch (algo) {
+#ifdef HAS_SHA2
+    case HASH_ALGO_SHA224:  /* fallthrough */
+    case HASH_ALGO_SHA256:  /* fallthrough */
+#endif
+    case HASH_ALGO_MD5:     /* fallthrough */
+    case HASH_ALGO_SHA1:
+        return true;
+    }
+
+    return false;
+}
+
+static int
+stm32_hash_start(struct hash_dev *hash, void *ctx, uint16_t algo)
+{
+    uint32_t algomask;
+
+    (void)ctx;
+
+    if (!stm32_hash_has_support(hash, algo)) {
+        return -1;
+    }
+
+    os_mutex_pend(&gmtx, OS_TIMEOUT_NEVER);
+
+    switch (algo) {
+    case HASH_ALGO_MD5:
+        algomask = HASH_ALGOSELECTION_MD5;
+        break;
+    case HASH_ALGO_SHA1:
+        algomask = HASH_ALGOSELECTION_SHA1;
+        break;
+    case HASH_ALGO_SHA224:
+        algomask = HASH_ALGOSELECTION_SHA224;
+        break;
+    case HASH_ALGO_SHA256:
+        algomask = HASH_ALGOSELECTION_SHA256;
+        break;
+    default:
+        assert(0);
+        return -1;
+    }
+
+    HASH->CR = algomask | HASH_CR_INIT | HASH_DATATYPE_8B;
+
+    return 0;
+}
+
+static int
+stm32_hash_update(struct hash_dev *hash, void *ctx, uint16_t algo,
+        const void *inbuf, uint32_t inlen)
+{
+    uint32_t *u32p;
+    int i;
+
+    (void)ctx;
+
+    u32p = (uint32_t *)inbuf;
+    for (i = 0; i < (inlen + 3) / 4; i++) {
+        HASH->DIN = u32p[i];
+    }
+    __HAL_HASH_SET_NBVALIDBITS(inlen);
+
+    return 0;
+}
+
+static int
+stm32_hash_finish(struct hash_dev *hash, void *ctx, uint16_t algo,
+        void *outbuf)
+{
+    uint8_t digestsz;
+    uint32_t *u32p;
+    int i;
+
+    (void)ctx;
+
+    switch (algo) {
+    case HASH_ALGO_MD5:
+        digestsz = MD5_DIGEST_LEN / 4;
+        break;
+    case HASH_ALGO_SHA1:
+        digestsz = SHA1_DIGEST_LEN / 4;
+        break;
+    case HASH_ALGO_SHA224:
+        digestsz = SHA224_DIGEST_LEN / 4;
+        break;
+    case HASH_ALGO_SHA256:
+        digestsz = SHA256_DIGEST_LEN / 4;
+        break;
+    default:
+        assert(0);
+        return -1;
+    }
+
+    __HAL_HASH_START_DIGEST();
+
+    while (HASH->SR & HASH_FLAG_BUSY) ;
+
+    u32p = (uint32_t *)outbuf;
+    for (i = 0; i < digestsz; i++) {
+#ifdef HAS_SHA2
+        /*
+         * XXX HASH_DIGEST is only available on devices that support
+         * SHA-2, and its first 5 words are mapped at the same address
+         * as HASH below.
+         */
+        u32p[i] = os_bswap_32(HASH_DIGEST->HR[i]);
+#else
+        u32p[i] = os_bswap_32(HASH->HR[i]);
+#endif
+    }
+
+    os_mutex_release(&gmtx);
+    return 0;
+}
+
+static int
+stm32_hash_dev_open(struct os_dev *dev, uint32_t wait, void *arg)
+{
+    struct hash_dev *hash;
+
+    hash = (struct hash_dev *)dev;
+    assert(hash);
+
+    /* XXX Not reentrant? */
 
 Review comment:
   I think 'start->update->finish' requirement is ok.  Use of mutex is good here. I don't think any of the SHA users currently expect the calls to return with an error, making them wait instead is ok. Also having that assert() for the failure paths is good.

----------------------------------------------------------------
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