From 787539e7720c99785f6c664a7484842bba08f6ed Mon Sep 17 00:00:00 2001 From: Cristian Stoica Date: Thu, 19 Feb 2015 13:39:52 +0200 Subject: [PATCH 26/26] cryptodev: simplify cryptodev pkc support code - Engine init returns directly a file descriptor instead of a pointer to one - Similarly, the Engine close will now just close the file Change-Id: Ief736d0776c7009dee002204fb1d4ce9d31c8787 Signed-off-by: Cristian Stoica Reviewed-on: http://git.am.freescale.net:8181/34221 --- crypto/crypto.h | 2 +- crypto/engine/eng_cryptodev.c | 35 +++----------------------- crypto/engine/eng_int.h | 14 +++-------- crypto/engine/eng_lib.c | 57 +++++++++++++++++++++---------------------- crypto/engine/engine.h | 13 +++++----- 5 files changed, 42 insertions(+), 79 deletions(-) diff --git a/crypto/crypto.h b/crypto/crypto.h index ce12731..292427e 100644 --- a/crypto/crypto.h +++ b/crypto/crypto.h @@ -618,7 +618,7 @@ struct pkc_cookie_s { * -EINVAL: Parameters Invalid */ void (*pkc_callback)(struct pkc_cookie_s *cookie, int status); - void *eng_handle; + int eng_handle; }; #ifdef __cplusplus diff --git a/crypto/engine/eng_cryptodev.c b/crypto/engine/eng_cryptodev.c index c9db27d..f173bde 100644 --- a/crypto/engine/eng_cryptodev.c +++ b/crypto/engine/eng_cryptodev.c @@ -1742,7 +1742,7 @@ cryptodev_asym_async(struct crypt_kop *kop, int rlen, BIGNUM *r, int slen, struct pkc_cookie_s *cookie = kop->cookie; struct cryptodev_cookie_s *eng_cookie; - fd = *(int *)cookie->eng_handle; + fd = cookie->eng_handle; eng_cookie = malloc(sizeof(struct cryptodev_cookie_s)); if (!eng_cookie) @@ -1802,38 +1802,11 @@ cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r, int slen, BIGNUM *s) return (ret); } -/* Close an opened instance of cryptodev engine */ -void cryptodev_close_instance(void *handle) -{ - int fd; - - if (handle) { - fd = *(int *)handle; - close(fd); - free(handle); - } -} - -/* Create an instance of cryptodev for asynchronous interface */ -void *cryptodev_init_instance(void) -{ - int *fd = malloc(sizeof(int)); - - if (fd) { - if ((*fd = open("/dev/crypto", O_RDWR, 0)) == -1) { - free(fd); - return NULL; - } - } - return fd; -} - #include /* Return 0 on success and 1 on failure */ -int cryptodev_check_availability(void *eng_handle) +int cryptodev_check_availability(int fd) { - int fd = *(int *)eng_handle; struct pkc_cookie_list_s cookie_list; struct pkc_cookie_s *cookie; int i; @@ -4540,8 +4513,8 @@ ENGINE_load_cryptodev(void) } ENGINE_set_check_pkc_availability(engine, cryptodev_check_availability); - ENGINE_set_close_instance(engine, cryptodev_close_instance); - ENGINE_set_init_instance(engine, cryptodev_init_instance); + ENGINE_set_close_instance(engine, put_dev_crypto); + ENGINE_set_open_instance(engine, open_dev_crypto); ENGINE_set_async_map(engine, ENGINE_ALLPKC_ASYNC); ENGINE_add(engine); diff --git a/crypto/engine/eng_int.h b/crypto/engine/eng_int.h index 8fc3077..8fb79c0 100644 --- a/crypto/engine/eng_int.h +++ b/crypto/engine/eng_int.h @@ -181,23 +181,15 @@ struct engine_st ENGINE_LOAD_KEY_PTR load_pubkey; ENGINE_SSL_CLIENT_CERT_PTR load_ssl_client_cert; - /* - * Instantiate Engine handle to be passed in check_pkc_availability - * Ensure that Engine is instantiated before any pkc asynchronous call. - */ - void *(*engine_init_instance)(void); - /* - * Instantiated Engine handle will be closed with this call. - * Ensure that no pkc asynchronous call is made after this call - */ - void (*engine_close_instance)(void *handle); + int (*engine_open_instance)(void); + int (*engine_close_instance)(int fd); /* * Check availability will extract the data from kernel. * eng_handle: This is the Engine handle corresponds to which * the cookies needs to be polled. * return 0 if cookie available else 1 */ - int (*check_pkc_availability)(void *eng_handle); + int (*check_pkc_availability)(int fd); /* * The following map is used to check if the engine supports asynchronous implementation * ENGINE_ASYNC_FLAG* for available bitmap. Any application checking for asynchronous diff --git a/crypto/engine/eng_lib.c b/crypto/engine/eng_lib.c index 6fa621c..6c9471b 100644 --- a/crypto/engine/eng_lib.c +++ b/crypto/engine/eng_lib.c @@ -99,7 +99,7 @@ void engine_set_all_null(ENGINE *e) e->load_privkey = NULL; e->load_pubkey = NULL; e->check_pkc_availability = NULL; - e->engine_init_instance = NULL; + e->engine_open_instance = NULL; e->engine_close_instance = NULL; e->cmd_defns = NULL; e->async_map = 0; @@ -237,47 +237,46 @@ int ENGINE_set_id(ENGINE *e, const char *id) return 1; } -void ENGINE_set_init_instance(ENGINE *e, void *(*engine_init_instance)(void)) - { - e->engine_init_instance = engine_init_instance; - } +void ENGINE_set_open_instance(ENGINE *e, int (*engine_open_instance)(void)) +{ + e->engine_open_instance = engine_open_instance; +} -void ENGINE_set_close_instance(ENGINE *e, - void (*engine_close_instance)(void *)) - { - e->engine_close_instance = engine_close_instance; - } +void ENGINE_set_close_instance(ENGINE *e, int (*engine_close_instance)(int)) +{ + e->engine_close_instance = engine_close_instance; +} void ENGINE_set_async_map(ENGINE *e, int async_map) { e->async_map = async_map; } -void *ENGINE_init_instance(ENGINE *e) - { - return e->engine_init_instance(); - } - -void ENGINE_close_instance(ENGINE *e, void *eng_handle) - { - e->engine_close_instance(eng_handle); - } - int ENGINE_get_async_map(ENGINE *e) { return e->async_map; } -void ENGINE_set_check_pkc_availability(ENGINE *e, - int (*check_pkc_availability)(void *eng_handle)) - { - e->check_pkc_availability = check_pkc_availability; - } +int ENGINE_open_instance(ENGINE *e) +{ + return e->engine_open_instance(); +} -int ENGINE_check_pkc_availability(ENGINE *e, void *eng_handle) - { - return e->check_pkc_availability(eng_handle); - } +int ENGINE_close_instance(ENGINE *e, int fd) +{ + return e->engine_close_instance(fd); +} + +void ENGINE_set_check_pkc_availability(ENGINE *e, + int (*check_pkc_availability)(int fd)) +{ + e->check_pkc_availability = check_pkc_availability; +} + +int ENGINE_check_pkc_availability(ENGINE *e, int fd) +{ + return e->check_pkc_availability(fd); +} int ENGINE_set_name(ENGINE *e, const char *name) { diff --git a/crypto/engine/engine.h b/crypto/engine/engine.h index ccff86a..3ba3e97 100644 --- a/crypto/engine/engine.h +++ b/crypto/engine/engine.h @@ -473,9 +473,6 @@ ENGINE *ENGINE_new(void); int ENGINE_free(ENGINE *e); int ENGINE_up_ref(ENGINE *e); int ENGINE_set_id(ENGINE *e, const char *id); -void ENGINE_set_init_instance(ENGINE *e, void *(*engine_init_instance)(void)); -void ENGINE_set_close_instance(ENGINE *e, - void (*engine_free_instance)(void *)); /* * Following FLAGS are bitmap store in async_map to set asynchronous interface capability *of the engine @@ -492,11 +489,13 @@ void ENGINE_set_async_map(ENGINE *e, int async_map); * to confirm asynchronous methods supported */ int ENGINE_get_async_map(ENGINE *e); -void *ENGINE_init_instance(ENGINE *e); -void ENGINE_close_instance(ENGINE *e, void *eng_handle); +int ENGINE_open_instance(ENGINE *e); +int ENGINE_close_instance(ENGINE *e, int fd); +void ENGINE_set_init_instance(ENGINE *e, int(*engine_init_instance)(void)); +void ENGINE_set_close_instance(ENGINE *e, int(*engine_close_instance)(int)); void ENGINE_set_check_pkc_availability(ENGINE *e, - int (*check_pkc_availability)(void *eng_handle)); -int ENGINE_check_pkc_availability(ENGINE *e, void *eng_handle); + int (*check_pkc_availability)(int fd)); +int ENGINE_check_pkc_availability(ENGINE *e, int fd); int ENGINE_set_name(ENGINE *e, const char *name); int ENGINE_set_RSA(ENGINE *e, const RSA_METHOD *rsa_meth); int ENGINE_set_DSA(ENGINE *e, const DSA_METHOD *dsa_meth); -- 2.3.5