Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src-draft/trunk]: src/sys/arch/arm/rockchip Perform a little self-test: veri...



details:   https://anonhg.NetBSD.org/src-all/rev/b0bdd7a2908a
branches:  trunk
changeset: 932911:b0bdd7a2908a
user:      Taylor R Campbell <riastradh%NetBSD.org@localhost>
date:      Sun May 17 18:06:00 2020 +0000

description:
Perform a little self-test: verify AES-128 on all-zero key/input.

diffstat:

 sys/arch/arm/rockchip/rk_1crypto.c |  64 ++++++++++++++++++++++++++++++++++++++
 1 files changed, 64 insertions(+), 0 deletions(-)

diffs (85 lines):

diff -r a96065ae89bd -r b0bdd7a2908a sys/arch/arm/rockchip/rk_1crypto.c
--- a/sys/arch/arm/rockchip/rk_1crypto.c        Fri May 15 15:55:17 2020 +0000
+++ b/sys/arch/arm/rockchip/rk_1crypto.c        Sun May 17 18:06:00 2020 +0000
@@ -65,6 +65,7 @@
 
 static int rk_1crypto_match(device_t, cfdata_t, void *);
 static void rk_1crypto_attach(device_t, device_t, void *);
+static int rk_1crypto_selftest(struct rk_1crypto_softc *);
 static void rk_1crypto_rndsource_attach(struct rk_1crypto_softc *);
 static void rk_1crypto_rng_get(size_t, void *);
 static void rk_1crypto_sysctl_attach(struct rk_1crypto_softc *);
@@ -171,10 +172,73 @@
        ctrl |= __SHIFTIN(100, RK_1CRYPTO_TRNG_CTRL_CYCLES);
        RKC_WRITE(sc, RK_1CRYPTO_TRNG_CTRL, ctrl);
 
+       if (rk_1crypto_selftest(sc))
+               return;
        rk_1crypto_rndsource_attach(sc);
        rk_1crypto_sysctl_attach(sc);
 }
 
+static int
+rk_1crypto_selftest(struct rk_1crypto_softc *sc)
+{
+       static const uint32_t key[4] = {0};
+       static const uint32_t input[4] = {0};
+       static const uint32_t expected[4] = {
+               0x66e94bd4, 0xef8a2c3b, 0x884cfa59, 0xca342b2e,
+       };
+       uint32_t output[4];
+       uint32_t ctrl;
+       unsigned i, timo;
+
+       /* Program the key and input block.  */
+       for (i = 0; i < 4; i++)
+               RKC_WRITE(sc, RK_1CRYPTO_AES_DIN(i), key[i]);
+       for (i = 0; i < 4; i++)
+               RKC_WRITE(sc, RK_1CRYPTO_AES_DIN(i), input[i]);
+
+       /*
+        * Set up the AES unit to do AES-128 `ECB' (i.e., just the raw
+        * AES permutation) in the encryption direction.
+        */
+       ctrl = 0;
+       ctrl |= RK_1CRYPTO_AES_CTRL_KEYCHANGE;
+       ctrl |= __SHIFTIN(RK_1CRYPTO_AES_CTRL_MODE_ECB,
+           RK_1CRYPTO_AES_CTRL_MODE);
+       ctrl |= __SHIFTIN(RK_1CRYPTO_AES_CTRL_KEYSIZE_128,
+           RK_1CRYPTO_AES_CTRL_KEYSIZE);
+       ctrl |= __SHIFTIN(RK_1CRYPTO_AES_CTRL_DIR_ENC,
+           RK_1CRYPTO_AES_CTRL_DIR);
+       RKC_WRITE(sc, RK_1CRYPTO_AES_CTRL, ctrl);
+
+       /* Kick it off.  */
+       RKC_CTRL(sc, RK_1CRYPTO_CTRL_AES_START, 1);
+
+       /* Wait up to 1ms for it to complete.  */
+       timo = 1000;
+       while (RKC_READ(sc, RK_1CRYPTO_CTRL) & RK_1CRYPTO_CTRL_AES_START) {
+               if (--timo == 0) {
+                       device_printf(sc->sc_dev, "AES self-test timed out\n");
+                       return -1;
+               }
+               DELAY(1);
+       }
+
+       /* Read the output.  */
+       for (i = 0; i < 4; i++)
+               output[i] = RKC_READ(sc, RK_1CRYPTO_AES_DOUT(i));
+
+       /* Verify the output.  */
+       for (i = 0; i < 4; i++) {
+               if (output[i] != expected[i]) {
+                       device_printf(sc->sc_dev, "AES self-test failed\n");
+                       return -1;
+               }
+       }
+
+       /* Success!  */
+       return 0;
+}
+
 static void
 rk_1crypto_rndsource_attach(struct rk_1crypto_softc *sc)
 {



Home | Main Index | Thread Index | Old Index