From: Luiz Augusto von Dentz Date: Fri, 15 Jan 2021 18:52:42 +0000 (-0800) Subject: shared/crypto: Transform bt_cryto in a singleton X-Git-Tag: submit/tizen/20220313.220938~495 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=da82e582f3215b57ec74cd06c77b0b0e240e17bb;p=platform%2Fupstream%2Fbluez.git shared/crypto: Transform bt_cryto in a singleton This makes bt_crypto always return the same instance thus avoiding the problem of having multiple PF sockets opened which may hit the file descriptor limit. Signed-off-by: Anuj Jain Signed-off-by: Ayush Garg --- diff --git a/src/shared/crypto.c b/src/shared/crypto.c index 378bb61..8afdbf4 100755 --- a/src/shared/crypto.c +++ b/src/shared/crypto.c @@ -126,34 +126,40 @@ static int cmac_aes_setup(void) return fd; } +static struct bt_crypto *singleton; + struct bt_crypto *bt_crypto_new(void) { - struct bt_crypto *crypto; + if (singleton) + return bt_crypto_ref(singleton); - crypto = new0(struct bt_crypto, 1); + singleton = new0(struct bt_crypto, 1); - crypto->ecb_aes = ecb_aes_setup(); - if (crypto->ecb_aes < 0) { - free(crypto); + singleton->ecb_aes = ecb_aes_setup(); + if (singleton->ecb_aes < 0) { + free(singleton); + singleton = NULL; return NULL; } - crypto->urandom = urandom_setup(); - if (crypto->urandom < 0) { - close(crypto->ecb_aes); - free(crypto); + singleton->urandom = urandom_setup(); + if (singleton->urandom < 0) { + close(singleton->ecb_aes); + free(singleton); + singleton = NULL; return NULL; } - crypto->cmac_aes = cmac_aes_setup(); - if (crypto->cmac_aes < 0) { - close(crypto->urandom); - close(crypto->ecb_aes); - free(crypto); + singleton->cmac_aes = cmac_aes_setup(); + if (singleton->cmac_aes < 0) { + close(singleton->urandom); + close(singleton->ecb_aes); + free(singleton); + singleton = NULL; return NULL; } - return bt_crypto_ref(crypto); + return bt_crypto_ref(singleton); } struct bt_crypto *bt_crypto_ref(struct bt_crypto *crypto) @@ -179,6 +185,7 @@ void bt_crypto_unref(struct bt_crypto *crypto) close(crypto->cmac_aes); free(crypto); + singleton = NULL; } bool bt_crypto_random_bytes(struct bt_crypto *crypto,