From 876cb1335653775b1a33ef30572a30fdd3e9890a Mon Sep 17 00:00:00 2001
From: Gregory Nutt <gnutt@nuttx.org>
Date: Mon, 16 Nov 2015 16:10:01 -0600
Subject: [PATCH] Crypto: Mostly cosmetic changes

---
 crypto/Kconfig     |  4 +++
 crypto/aes.c       | 10 ++++--
 crypto/cryptodev.c | 80 ++++++++++++++++++++++++++--------------------
 3 files changed, 57 insertions(+), 37 deletions(-)

diff --git a/crypto/Kconfig b/crypto/Kconfig
index 7da34a7a45..022fce7a05 100644
--- a/crypto/Kconfig
+++ b/crypto/Kconfig
@@ -46,4 +46,8 @@ config CRYPTO_SW_AES
 		Enable the software AES library as described in
 		include/nuttx/crypto/aes.h
 
+		TODO: Adapt interfaces so that they are consistent with H/W AES
+		implemenations.  This needs to support up_aesinitialize() and
+		aes_cypher() per include/nuttx/crypto/crypto.h.
+
 endif # CRYPTO
diff --git a/crypto/aes.c b/crypto/aes.c
index d82e16f46a..e7c638f718 100644
--- a/crypto/aes.c
+++ b/crypto/aes.c
@@ -34,6 +34,11 @@
  *
  ****************************************************************************/
 
+/* TODO: Adapt interfaces so that they are consistent with H/W AES
+ * implemenations.  This needs to support up_aesinitialize() and
+ * aes_cypher() per include/nuttx/crypto/crypto.h.
+ */
+
 /****************************************************************************
  * Included Files
  ****************************************************************************/
@@ -582,7 +587,8 @@ void aes_encrypt(FAR uint8_t *state, FAR const uint8_t *key)
 
 void aes_decrypt(FAR uint8_t *state, FAR const uint8_t *key)
 {
-  expand_key(g_expanded_key, key);       /* Expand the key into 176 bytes */
+  /* Expand the key into 176 bytes */
+
+  expand_key(g_expanded_key, key);
   aes_decr(state, g_expanded_key);
 }
-
diff --git a/crypto/cryptodev.c b/crypto/cryptodev.c
index c81585c329..9710acf028 100644
--- a/crypto/cryptodev.c
+++ b/crypto/cryptodev.c
@@ -50,6 +50,16 @@
 #include <nuttx/crypto/crypto.h>
 #include <nuttx/crypto/cryptodev.h>
 
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#ifdef CONFIG_CRYPTO_AES
+#  define AES_CYPHER(mode) \
+  aes_cypher(op->dst, op->src, op->len, op->iv, ses->key, ses->keylen, \
+             mode, encrypt)
+#endif
+
 /****************************************************************************
  * Private Function Prototypes
  ****************************************************************************/
@@ -69,13 +79,18 @@ static int cryptodev_ioctl(FAR struct file *filep, int cmd,
 
 static const struct file_operations g_cryptodevops =
 {
-  0,                  /* open */
-  0,                  /* close */
-  cryptodev_read,     /* read */
-  cryptodev_write,    /* write */
-  0,                  /* seek */
-  cryptodev_ioctl,    /* ioctl */
-  0,                  /* poll */
+  0,                  /* open   */
+  0,                  /* close  */
+  cryptodev_read,     /* read   */
+  cryptodev_write,    /* write  */
+  0,                  /* seek   */
+  cryptodev_ioctl     /* ioctl  */
+#ifndef CONFIG_DISABLE_POLL
+  , 0                 /* poll   */
+#endif
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  , 0                 /* unlink */
+#endif
 };
 
 /****************************************************************************
@@ -110,6 +125,7 @@ static int cryptodev_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
       return OK;
     }
 
+#ifdef CONFIG_CRYPTO_AES
   case CIOCCRYPT:
     {
       FAR struct crypt_op *op    = (FAR struct crypt_op *)arg;
@@ -117,44 +133,38 @@ static int cryptodev_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
       int encrypt;
 
       switch (op->op)
-      {
-      case COP_ENCRYPT:
-        encrypt = 1;
-        break;
+        {
+        case COP_ENCRYPT:
+          encrypt = 1;
+          break;
 
-      case COP_DECRYPT:
-        encrypt = 0;
-        break;
+        case COP_DECRYPT:
+          encrypt = 0;
+          break;
 
-      default:
-        return -EINVAL;
-      }
+        default:
+          return -EINVAL;
+        }
 
       switch (ses->cipher)
-      {
+        {
+        case CRYPTO_AES_ECB:
+          return AES_CYPHER(AES_MODE_ECB);
 
-#if defined(CONFIG_CRYPTO_AES)
-#  define AES_CYPHER(mode) aes_cypher(op->dst, op->src, op->len, op->iv, ses->key, ses->keylen, mode, encrypt)
+        case CRYPTO_AES_CBC:
+          return AES_CYPHER(AES_MODE_CBC);
 
-      case CRYPTO_AES_ECB:
-        return AES_CYPHER(AES_MODE_ECB);
+        case CRYPTO_AES_CTR:
+          return AES_CYPHER(AES_MODE_CTR);
 
-      case CRYPTO_AES_CBC:
-        return AES_CYPHER(AES_MODE_CBC);
-
-      case CRYPTO_AES_CTR:
-        return AES_CYPHER(AES_MODE_CTR);
-
-#  undef AES_CYPHER
+        default:
+           return -EINVAL;
+        }
+    }
 #endif
 
-      default:
-        return -EINVAL;
-      }
-    }
-
   default:
-    return -EINVAL;
+    return -ENOTTY;
   }
 }