]> git.ipfire.org Git - thirdparty/gnutls.git/commitdiff
Added API to register MAC and digest algorithms.
authorNikos Mavrogiannopoulos <nmav@redhat.com>
Wed, 18 Mar 2015 12:04:43 +0000 (13:04 +0100)
committerNikos Mavrogiannopoulos <nmav@redhat.com>
Wed, 18 Mar 2015 12:44:21 +0000 (13:44 +0100)
lib/accelerated/cryptodev.c
lib/accelerated/x86/x86-common.c
lib/crypto-backend.c
lib/crypto-backend.h
lib/includes/gnutls/crypto.h
lib/libgnutls.map

index 9d97637458a92085cb49555d3a3936dfe821a449..c67dfd2473cb26497843b9e7bdb831a46cef2b41 100644 (file)
@@ -430,7 +430,7 @@ static int register_mac_digest(int cfd)
                ioctl(cfd, CIOCFSESSION, &sess.ses);
 
                ret =
-                   gnutls_crypto_single_mac_register(i, 90, &mac_struct);
+                   gnutls_crypto_single_mac_register(i, 90, &mac_struct, 0);
                if (ret < 0) {
                        gnutls_assert();
                        return ret;
@@ -467,7 +467,7 @@ static int register_mac_digest(int cfd)
                                  gnutls_mac_get_name(i));
                ret =
                    gnutls_crypto_single_digest_register(i, 90,
-                                                        &digest_struct);
+                                                        &digest_struct, 0);
                if (ret < 0) {
                        gnutls_assert();
                        return ret;
index 73c356594fd323024ed43615b21b68daa1a45680..6c4eb53786ee49730cc677e11d3de5cdaf068b7d 100644 (file)
@@ -242,7 +242,7 @@ void register_x86_padlock_crypto(unsigned capabilities)
                        ret =
                            gnutls_crypto_single_digest_register
                            (GNUTLS_DIG_SHA384, 80,
-                            &_gnutls_sha_padlock_nano);
+                            &_gnutls_sha_padlock_nano, 0);
                        if (ret < 0) {
                                gnutls_assert();
                        }
@@ -250,7 +250,7 @@ void register_x86_padlock_crypto(unsigned capabilities)
                        ret =
                            gnutls_crypto_single_digest_register
                            (GNUTLS_DIG_SHA512, 80,
-                            &_gnutls_sha_padlock_nano);
+                            &_gnutls_sha_padlock_nano, 0);
                        if (ret < 0) {
                                gnutls_assert();
                        }
@@ -258,7 +258,7 @@ void register_x86_padlock_crypto(unsigned capabilities)
                        ret =
                            gnutls_crypto_single_mac_register
                            (GNUTLS_MAC_SHA384, 80,
-                            &_gnutls_hmac_sha_padlock_nano);
+                            &_gnutls_hmac_sha_padlock_nano, 0);
                        if (ret < 0) {
                                gnutls_assert();
                        }
@@ -266,7 +266,7 @@ void register_x86_padlock_crypto(unsigned capabilities)
                        ret =
                            gnutls_crypto_single_mac_register
                            (GNUTLS_MAC_SHA512, 80,
-                            &_gnutls_hmac_sha_padlock_nano);
+                            &_gnutls_hmac_sha_padlock_nano, 0);
                        if (ret < 0) {
                                gnutls_assert();
                        }
@@ -275,7 +275,7 @@ void register_x86_padlock_crypto(unsigned capabilities)
                ret =
                    gnutls_crypto_single_digest_register(GNUTLS_DIG_SHA1,
                                                         80,
-                                                        &_gnutls_sha_padlock_nano);
+                                                        &_gnutls_sha_padlock_nano, 0);
                if (ret < 0) {
                        gnutls_assert();
                }
@@ -283,7 +283,7 @@ void register_x86_padlock_crypto(unsigned capabilities)
                ret =
                    gnutls_crypto_single_digest_register(GNUTLS_DIG_SHA224,
                                                         80,
-                                                        &_gnutls_sha_padlock_nano);
+                                                        &_gnutls_sha_padlock_nano, 0);
                if (ret < 0) {
                        gnutls_assert();
                }
@@ -291,7 +291,7 @@ void register_x86_padlock_crypto(unsigned capabilities)
                ret =
                    gnutls_crypto_single_digest_register(GNUTLS_DIG_SHA256,
                                                         80,
-                                                        &_gnutls_sha_padlock_nano);
+                                                        &_gnutls_sha_padlock_nano, 0);
                if (ret < 0) {
                        gnutls_assert();
                }
@@ -299,7 +299,7 @@ void register_x86_padlock_crypto(unsigned capabilities)
                ret =
                    gnutls_crypto_single_mac_register(GNUTLS_MAC_SHA1,
                                                      80,
-                                                     &_gnutls_hmac_sha_padlock_nano);
+                                                     &_gnutls_hmac_sha_padlock_nano, 0);
                if (ret < 0) {
                        gnutls_assert();
                }
@@ -309,7 +309,7 @@ void register_x86_padlock_crypto(unsigned capabilities)
                ret =
                    gnutls_crypto_single_mac_register(GNUTLS_MAC_SHA256,
                                                      80,
-                                                     &_gnutls_hmac_sha_padlock_nano);
+                                                     &_gnutls_hmac_sha_padlock_nano, 0);
                if (ret < 0) {
                        gnutls_assert();
                }
@@ -321,7 +321,7 @@ void register_x86_padlock_crypto(unsigned capabilities)
                ret =
                    gnutls_crypto_single_digest_register(GNUTLS_DIG_SHA1,
                                                         80,
-                                                        &_gnutls_sha_padlock);
+                                                        &_gnutls_sha_padlock, 0);
                if (ret < 0) {
                        gnutls_assert();
                }
@@ -329,7 +329,7 @@ void register_x86_padlock_crypto(unsigned capabilities)
                ret =
                    gnutls_crypto_single_digest_register(GNUTLS_DIG_SHA256,
                                                         80,
-                                                        &_gnutls_sha_padlock);
+                                                        &_gnutls_sha_padlock, 0);
                if (ret < 0) {
                        gnutls_assert();
                }
@@ -337,7 +337,7 @@ void register_x86_padlock_crypto(unsigned capabilities)
                ret =
                    gnutls_crypto_single_mac_register(GNUTLS_MAC_SHA1,
                                                      80,
-                                                     &_gnutls_hmac_sha_padlock);
+                                                     &_gnutls_hmac_sha_padlock, 0);
                if (ret < 0) {
                        gnutls_assert();
                }
@@ -345,7 +345,7 @@ void register_x86_padlock_crypto(unsigned capabilities)
                ret =
                    gnutls_crypto_single_mac_register(GNUTLS_MAC_SHA256,
                                                      80,
-                                                     &_gnutls_hmac_sha_padlock);
+                                                     &_gnutls_hmac_sha_padlock, 0);
                if (ret < 0) {
                        gnutls_assert();
                }
@@ -431,7 +431,7 @@ void register_x86_intel_crypto(unsigned capabilities)
                ret =
                    gnutls_crypto_single_digest_register(GNUTLS_DIG_SHA1,
                                                         80,
-                                                        &_gnutls_sha_x86_ssse3);
+                                                        &_gnutls_sha_x86_ssse3, 0);
                if (ret < 0) {
                        gnutls_assert();
                }
@@ -439,7 +439,7 @@ void register_x86_intel_crypto(unsigned capabilities)
                ret =
                    gnutls_crypto_single_digest_register(GNUTLS_DIG_SHA224,
                                                         80,
-                                                        &_gnutls_sha_x86_ssse3);
+                                                        &_gnutls_sha_x86_ssse3, 0);
                if (ret < 0) {
                        gnutls_assert();
                }
@@ -447,7 +447,7 @@ void register_x86_intel_crypto(unsigned capabilities)
                ret =
                    gnutls_crypto_single_digest_register(GNUTLS_DIG_SHA256,
                                                         80,
-                                                        &_gnutls_sha_x86_ssse3);
+                                                        &_gnutls_sha_x86_ssse3, 0);
                if (ret < 0) {
                        gnutls_assert();
                }
@@ -456,21 +456,21 @@ void register_x86_intel_crypto(unsigned capabilities)
                ret =
                    gnutls_crypto_single_mac_register(GNUTLS_MAC_SHA1,
                                                         80,
-                                                        &_gnutls_hmac_sha_x86_ssse3);
+                                                        &_gnutls_hmac_sha_x86_ssse3, 0);
                if (ret < 0)
                        gnutls_assert();
 
                ret =
                    gnutls_crypto_single_mac_register(GNUTLS_MAC_SHA224,
                                                         80,
-                                                        &_gnutls_hmac_sha_x86_ssse3);
+                                                        &_gnutls_hmac_sha_x86_ssse3, 0);
                if (ret < 0)
                        gnutls_assert();
 
                ret =
                    gnutls_crypto_single_mac_register(GNUTLS_MAC_SHA256,
                                                         80,
-                                                        &_gnutls_hmac_sha_x86_ssse3);
+                                                        &_gnutls_hmac_sha_x86_ssse3, 0);
                if (ret < 0)
                        gnutls_assert();
 
@@ -478,27 +478,27 @@ void register_x86_intel_crypto(unsigned capabilities)
                ret =
                    gnutls_crypto_single_digest_register(GNUTLS_DIG_SHA384,
                                                         80,
-                                                        &_gnutls_sha_x86_ssse3);
+                                                        &_gnutls_sha_x86_ssse3, 0);
                if (ret < 0)
                        gnutls_assert();
 
                ret =
                    gnutls_crypto_single_digest_register(GNUTLS_DIG_SHA512,
                                                         80,
-                                                        &_gnutls_sha_x86_ssse3);
+                                                        &_gnutls_sha_x86_ssse3, 0);
                if (ret < 0)
                        gnutls_assert();
                ret =
                    gnutls_crypto_single_mac_register(GNUTLS_MAC_SHA384,
                                                         80,
-                                                        &_gnutls_hmac_sha_x86_ssse3);
+                                                        &_gnutls_hmac_sha_x86_ssse3, 0);
                if (ret < 0)
                        gnutls_assert();
 
                ret =
                    gnutls_crypto_single_mac_register(GNUTLS_MAC_SHA512,
                                                         80,
-                                                        &_gnutls_hmac_sha_x86_ssse3);
+                                                        &_gnutls_hmac_sha_x86_ssse3, 0);
                if (ret < 0)
                        gnutls_assert();
 #endif
index 4e5e7515b19f3b90875eabab62727050dc12c12d..a4415169ca8ed2f14ec49c2ca223d9b559af9c62 100644 (file)
@@ -111,9 +111,9 @@ static const void *_get_algo(algo_list * al, int algo)
        return NULL;
 }
 
-static cipher_list glob_cl = { GNUTLS_CIPHER_NULL, 0, NULL, NULL };
-static mac_list glob_ml = { GNUTLS_MAC_NULL, 0, NULL, NULL };
-static digest_list glob_dl = { GNUTLS_MAC_NULL, 0, NULL, NULL };
+static cipher_list glob_cl = { GNUTLS_CIPHER_NULL, 0, NULL, 0, NULL };
+static mac_list glob_ml = { GNUTLS_MAC_NULL, 0, NULL, 0, NULL };
+static digest_list glob_dl = { GNUTLS_MAC_NULL, 0, NULL, 0, NULL };
 
 static void _deregister(algo_list * cl)
 {
@@ -326,9 +326,10 @@ gnutls_crypto_rnd_register(int priority, const gnutls_crypto_rnd_st * s)
 int
 gnutls_crypto_single_mac_register(gnutls_mac_algorithm_t algorithm,
                                  int priority,
-                                 const gnutls_crypto_mac_st * s)
+                                 const gnutls_crypto_mac_st * s,
+                                 int free_s)
 {
-       return _algo_register(&glob_ml, algorithm, priority, s, 0);
+       return _algo_register(&glob_ml, algorithm, priority, (void*)s, free_s);
 }
 
 const gnutls_crypto_mac_st *_gnutls_get_crypto_mac(gnutls_mac_algorithm_t
@@ -361,9 +362,10 @@ const gnutls_crypto_mac_st *_gnutls_get_crypto_mac(gnutls_mac_algorithm_t
 int
 gnutls_crypto_single_digest_register(gnutls_digest_algorithm_t algorithm,
                                     int priority,
-                                    const gnutls_crypto_digest_st * s)
+                                    const gnutls_crypto_digest_st * s,
+                                    int free_s)
 {
-       return _algo_register(&glob_dl, algorithm, priority, s, 0);
+       return _algo_register(&glob_dl, algorithm, priority, (void*)s, free_s);
 }
 
 const gnutls_crypto_digest_st
@@ -442,3 +444,92 @@ int gnutls_crypto_pk_register(int priority, const gnutls_crypto_pk_st * s)
        return GNUTLS_E_CRYPTO_ALREADY_REGISTERED;
 }
 
+/**
+ * gnutls_crypto_register_mac:
+ * @algorithm: is the gnutls MAC identifier
+ * @priority: is the priority of the algorithm
+ * @init: A function which initializes the MAC
+ * @setkey: A function which sets the key of the MAC
+ * @setnonce: A function which sets the nonce for the mac (may be %NULL for common MAC algorithms)
+ * @hash: Perform the hash operation
+ * @output: Provide the output of the MAC
+ * @deinit: A function which deinitializes the MAC
+ * @hash_fast: Perform the MAC operation in one go
+ *
+ * This function will register a MAC algorithm to be used by gnutls.
+ * Any algorithm registered will override the included algorithms and
+ * by convention kernel implemented algorithms have priority of 90
+ *  and CPU-assisted of 80.
+ * The algorithm with the lowest priority will be used by gnutls.
+ *
+ * Returns: %GNUTLS_E_SUCCESS on success, otherwise a negative error code.
+ *
+ * Since: 3.4.0
+ **/
+int
+gnutls_crypto_register_mac(gnutls_mac_algorithm_t algorithm,
+                          int priority,
+                          gnutls_mac_init_func init,
+                          gnutls_mac_setkey_func setkey,
+                          gnutls_mac_setnonce_func setnonce,
+                          gnutls_mac_hash_func hash,
+                          gnutls_mac_output_func output,
+                          gnutls_mac_deinit_func deinit,
+                          gnutls_mac_fast_func hash_fast)
+{
+       gnutls_crypto_mac_st *s = gnutls_calloc(1, sizeof(gnutls_crypto_mac_st));
+       if (s == NULL)
+               return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
+
+       s->init = init;
+       s->setkey = setkey;
+       s->setnonce = setnonce;
+       s->hash = hash;
+       s->output = output;
+       s->fast = hash_fast;
+       s->deinit = deinit;
+
+       return gnutls_crypto_single_mac_register(algorithm, priority, s, 1);
+}
+
+/**
+ * gnutls_crypto_register_digest:
+ * @algorithm: is the gnutls digest identifier
+ * @priority: is the priority of the algorithm
+ * @init: A function which initializes the digest
+ * @hash: Perform the hash operation
+ * @output: Provide the output of the digest
+ * @deinit: A function which deinitializes the digest
+ * @hash_fast: Perform the digest operation in one go
+ *
+ * This function will register a digest algorithm to be used by gnutls.
+ * Any algorithm registered will override the included algorithms and
+ * by convention kernel implemented algorithms have priority of 90
+ *  and CPU-assisted of 80.
+ * The algorithm with the lowest priority will be used by gnutls.
+ *
+ * Returns: %GNUTLS_E_SUCCESS on success, otherwise a negative error code.
+ *
+ * Since: 3.4.0
+ **/
+int
+gnutls_crypto_register_digest(gnutls_digest_algorithm_t algorithm,
+                          int priority,
+                          gnutls_digest_init_func init,
+                          gnutls_digest_hash_func hash,
+                          gnutls_digest_output_func output,
+                          gnutls_digest_deinit_func deinit,
+                          gnutls_digest_fast_func hash_fast)
+{
+       gnutls_crypto_digest_st *s = gnutls_calloc(1, sizeof(gnutls_crypto_digest_st));
+       if (s == NULL)
+               return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
+
+       s->init = init;
+       s->hash = hash;
+       s->output = output;
+       s->fast = hash_fast;
+       s->deinit = deinit;
+
+       return gnutls_crypto_single_digest_register(algorithm, priority, s, 1);
+}
index 1984d90562e9aa974522178376ace8964a41c87c..5f6c228fb4ce08bc335ddb9ccb5970df89ba14e3 100644 (file)
@@ -47,15 +47,13 @@ typedef struct {
 } gnutls_crypto_cipher_st;
 
 typedef struct {
-       int (*init) (gnutls_mac_algorithm_t, void **ctx);
-       int (*setkey) (void *ctx, const void *key, size_t keysize);
-       int (*setnonce) (void *ctx, const void *nonce, size_t noncesize);
-       int (*hash) (void *ctx, const void *text, size_t textsize);
-       int (*output) (void *src_ctx, void *digest, size_t digestsize);
-       void (*deinit) (void *ctx);
-       int (*fast) (gnutls_mac_algorithm_t, const void *nonce,
-                    size_t nonce_size, const void *key, size_t keysize,
-                    const void *text, size_t textsize, void *digest);
+       gnutls_mac_init_func init;
+       gnutls_mac_setkey_func setkey;
+       gnutls_mac_setnonce_func setnonce;
+       gnutls_mac_hash_func hash;
+       gnutls_mac_output_func output;
+       gnutls_mac_deinit_func deinit;
+       gnutls_mac_fast_func fast;
 
        /* Not needed for registered on run-time. Only included
         * should define it. */
@@ -63,12 +61,11 @@ typedef struct {
 } gnutls_crypto_mac_st;
 
 typedef struct {
-       int (*init) (gnutls_digest_algorithm_t, void **ctx);
-       int (*hash) (void *ctx, const void *src, size_t srcsize);
-       int (*output) (void *src_ctx, void *digest, size_t digestsize);
-       void (*deinit) (void *ctx);
-       int (*fast) (gnutls_digest_algorithm_t, const void *src,
-                    size_t srcsize, void *digest);
+       gnutls_digest_init_func init;
+       gnutls_digest_hash_func hash;
+       gnutls_digest_output_func output;
+       gnutls_digest_deinit_func deinit;
+       gnutls_digest_fast_func fast;
 
        /* Not needed for registered on run-time. Only included
         * should define it. */
@@ -346,12 +343,12 @@ int gnutls_crypto_single_cipher_register(gnutls_cipher_algorithm_t
 int gnutls_crypto_single_mac_register(gnutls_mac_algorithm_t algorithm,
                                      int priority,
                                      const gnutls_crypto_single_mac_st *
-                                     s);
+                                     s, int free_s);
 int gnutls_crypto_single_digest_register(gnutls_digest_algorithm_t
                                         algorithm, int priority,
                                         const
                                         gnutls_crypto_single_digest_st *
-                                        s);
+                                        s, int free_s);
 
 int gnutls_crypto_rnd_register(int priority,
                               const gnutls_crypto_rnd_st * s);
index 76bcc0652d43f29e956d4f578bd1fcbcc86bb000..157a5b7acbce720ef297157f3510c5e85ae3f8e6 100644 (file)
@@ -135,6 +135,10 @@ int gnutls_rnd(gnutls_rnd_level_t level, void *data, size_t len);
 
 void gnutls_rnd_refresh(void);
 
+
+/* API to override ciphers and MAC algorithms 
+ */
+
 typedef int (*gnutls_cipher_init_func) (gnutls_cipher_algorithm_t, void **ctx, int enc);
 typedef int (*gnutls_cipher_setkey_func) (void *ctx, const void *key, size_t keysize);
 /* old style ciphers */
@@ -181,6 +185,43 @@ gnutls_crypto_register_aead_cipher(gnutls_cipher_algorithm_t algorithm,
                              gnutls_cipher_aead_decrypt_func aead_decrypt,
                              gnutls_cipher_deinit_func deinit);
 
+typedef int (*gnutls_mac_init_func) (gnutls_mac_algorithm_t, void **ctx);
+typedef int (*gnutls_mac_setkey_func) (void *ctx, const void *key, size_t keysize);
+typedef int (*gnutls_mac_setnonce_func) (void *ctx, const void *nonce, size_t noncesize);
+typedef int (*gnutls_mac_hash_func) (void *ctx, const void *text, size_t textsize);
+typedef int (*gnutls_mac_output_func) (void *src_ctx, void *digest, size_t digestsize);
+typedef void (*gnutls_mac_deinit_func) (void *ctx);
+typedef int (*gnutls_mac_fast_func) (gnutls_mac_algorithm_t, const void *nonce,
+                    size_t nonce_size, const void *key, size_t keysize,
+                    const void *text, size_t textsize, void *digest);
+
+int
+gnutls_crypto_register_mac(gnutls_mac_algorithm_t mac,
+                          int priority,
+                          gnutls_mac_init_func init,
+                          gnutls_mac_setkey_func setkey,
+                          gnutls_mac_setnonce_func setnonce,
+                          gnutls_mac_hash_func hash,
+                          gnutls_mac_output_func output,
+                          gnutls_mac_deinit_func deinit,
+                          gnutls_mac_fast_func hash_fast);
+
+typedef int (*gnutls_digest_init_func) (gnutls_digest_algorithm_t, void **ctx);
+typedef int (*gnutls_digest_hash_func) (void *ctx, const void *text, size_t textsize);
+typedef int (*gnutls_digest_output_func) (void *src_ctx, void *digest, size_t digestsize);
+typedef void (*gnutls_digest_deinit_func) (void *ctx);
+typedef int (*gnutls_digest_fast_func) (gnutls_digest_algorithm_t,
+                    const void *text, size_t textsize, void *digest);
+
+int
+gnutls_crypto_register_digest(gnutls_digest_algorithm_t digest,
+                          int priority,
+                          gnutls_digest_init_func init,
+                          gnutls_digest_hash_func hash,
+                          gnutls_digest_output_func output,
+                          gnutls_digest_deinit_func deinit,
+                          gnutls_digest_fast_func hash_fast);
+
 /* *INDENT-OFF* */
 #ifdef __cplusplus
 }
index 3ea41a057e1a96d91cd73b103b52c77729c2de58..f72c4bbf5b892e596a4983b61947cd780bbab30e 100644 (file)
@@ -1017,6 +1017,8 @@ GNUTLS_3_4
        gnutls_pkcs11_obj_set_info;
        gnutls_crypto_register_cipher;
        gnutls_crypto_register_aead_cipher;
+       gnutls_crypto_register_mac;
+       gnutls_crypto_register_digest;
  local:
        *;
 };