Whamcloud - gitweb
LU-8602 gss: Properly port gss to newer crypto api.
[fs/lustre-release.git] / libcfs / include / libcfs / libcfs_crypto.h
index 929ad6f..af67f53 100644 (file)
@@ -38,20 +38,35 @@ struct cfs_crypto_hash_type {
        unsigned int    cht_size;       /**< hash digest size */
 };
 
+struct cfs_crypto_crypt_type {
+       char           *cct_name;         /**< crypto algorithm name, equal to
+                                          * format name for crypto api */
+       unsigned int    cct_size;         /**< crypto key size */
+};
+
 enum cfs_crypto_hash_alg {
        CFS_HASH_ALG_NULL       = 0,
        CFS_HASH_ALG_ADLER32,
        CFS_HASH_ALG_CRC32,
+       CFS_HASH_ALG_CRC32C,
+       /* hashes before here will be speed-tested at module load */
        CFS_HASH_ALG_MD5,
        CFS_HASH_ALG_SHA1,
        CFS_HASH_ALG_SHA256,
        CFS_HASH_ALG_SHA384,
        CFS_HASH_ALG_SHA512,
-       CFS_HASH_ALG_CRC32C,
        CFS_HASH_ALG_MAX,
+       CFS_HASH_ALG_SPEED_MAX = CFS_HASH_ALG_MD5,
        CFS_HASH_ALG_UNKNOWN    = 0xff
 };
 
+enum cfs_crypto_crypt_alg {
+       CFS_CRYPT_ALG_NULL      = 0,
+       CFS_CRYPT_ALG_AES256_CTR,
+       CFS_CRYPT_ALG_MAX,
+       CFS_CRYPT_ALG_UNKNOWN   = 0xff
+};
+
 static struct cfs_crypto_hash_type hash_types[] = {
        [CFS_HASH_ALG_NULL] = {
                .cht_name       = "null",
@@ -105,6 +120,17 @@ static struct cfs_crypto_hash_type hash_types[] = {
        }
 };
 
+static struct cfs_crypto_crypt_type crypt_types[] = {
+       [CFS_CRYPT_ALG_NULL] = {
+               .cct_name       = "null",
+               .cct_size       = 0
+       },
+       [CFS_CRYPT_ALG_AES256_CTR] = {
+               .cct_name       = "ctr(aes)",
+               .cct_size       = 32
+       }
+};
+
 /* Maximum size of hash_types[].cht_size */
 #define CFS_CRYPTO_HASH_DIGESTSIZE_MAX 64
 
@@ -186,6 +212,86 @@ static inline unsigned char cfs_crypto_hash_alg(const char *algname)
        return CFS_HASH_ALG_UNKNOWN;
 }
 
+/**
+ * Return crypt algorithm information for the specified algorithm identifier
+ *
+ * Crypt information includes algorithm name, key size.
+ *
+ * \retval             cfs_crypto_crupt_type for valid ID (CFS_CRYPT_ALG_*)
+ * \retval             NULL for unknown algorithm identifier
+ */
+static inline const struct
+cfs_crypto_crypt_type *cfs_crypto_crypt_type(
+       enum cfs_crypto_crypt_alg crypt_alg)
+{
+       struct cfs_crypto_crypt_type *ct;
+
+       if (crypt_alg < CFS_CRYPT_ALG_MAX) {
+               ct = &crypt_types[crypt_alg];
+               if (ct->cct_name != NULL)
+                       return ct;
+       }
+       return NULL;
+}
+
+/**
+ * Return crypt name for crypt algorithm identifier
+ *
+ * \param[in] crypt_alg        crypt alrgorithm id (CFS_CRYPT_ALG_*)
+ *
+ * \retval             string name of known crypt algorithm
+ * \retval             "unknown" if hash algorithm is unknown
+ */
+static inline const
+char *cfs_crypto_crypt_name(enum cfs_crypto_crypt_alg crypt_alg)
+{
+       const struct cfs_crypto_crypt_type *ct;
+
+       ct = cfs_crypto_crypt_type(crypt_alg);
+       if (ct)
+               return ct->cct_name;
+
+       return "unknown";
+}
+
+
+/**
+ * Return key size for crypto algorithm type
+ *
+ * \param[in] crypt_alg        crypt alrgorithm id (CFS_CRYPT_ALG_*)
+ *
+ * \retval             crypt algorithm key size in bytes
+ * \retval             0 if crypt algorithm type is unknown
+ */
+static inline
+unsigned int cfs_crypto_crypt_keysize(enum cfs_crypto_crypt_alg crypt_alg)
+{
+       const struct cfs_crypto_crypt_type *ct;
+
+       ct = cfs_crypto_crypt_type(crypt_alg);
+       if (ct != NULL)
+               return ct->cct_size;
+
+       return 0;
+}
+
+/**
+ * Find crypto algorithm ID for the specified algorithm name
+ *
+ * \retval             crypto algorithm ID for valid ID (CFS_CRYPT_ALG_*)
+ * \retval             CFS_CRYPT_ALG_UNKNOWN for unknown algorithm name
+ */
+static inline unsigned char cfs_crypto_crypt_alg(const char *algname)
+{
+       enum cfs_crypto_crypt_alg crypt_alg;
+
+       for (crypt_alg = 0; crypt_alg < CFS_CRYPT_ALG_MAX; crypt_alg++)
+               if (strcmp(crypt_types[crypt_alg].cct_name, algname) == 0)
+                       return crypt_alg;
+
+       return CFS_CRYPT_ALG_UNKNOWN;
+}
+
 int cfs_crypto_hash_digest(enum cfs_crypto_hash_alg hash_alg,
                           const void *buf, unsigned int buf_len,
                           unsigned char *key, unsigned int key_len,