X-Git-Url: https://tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Fopenssl%2Fcipher.c;h=6b2affc0cfa2553f77e0e3058720d36f0e6b1499;hb=a4b5665643d48596b2659dbd69fe8e9b2a944e35;hp=45d101df7daa742948e4e538981ffd2131e2ca87;hpb=2c6b2d70e6640f39563ad7bb0aa0ba87f883848c;p=tinc diff --git a/src/openssl/cipher.c b/src/openssl/cipher.c index 45d101df..6b2affc0 100644 --- a/src/openssl/cipher.c +++ b/src/openssl/cipher.c @@ -1,6 +1,6 @@ /* cipher.c -- Symmetric block cipher handling - Copyright (C) 2007-2017 Guus Sliepen + Copyright (C) 2007-2022 Guus Sliepen This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -23,47 +23,41 @@ #include #include +#include "cipher.h" #include "../cipher.h" #include "../logger.h" -#include "../xalloc.h" -struct cipher { - EVP_CIPHER_CTX *ctx; - const EVP_CIPHER *cipher; -}; - -static cipher_t *cipher_open(const EVP_CIPHER *evp_cipher) { - cipher_t *cipher = xzalloc(sizeof(*cipher)); +static void cipher_open(cipher_t *cipher, const EVP_CIPHER *evp_cipher) { cipher->cipher = evp_cipher; cipher->ctx = EVP_CIPHER_CTX_new(); if(!cipher->ctx) { abort(); } - - return cipher; } -cipher_t *cipher_open_by_name(const char *name) { +bool cipher_open_by_name(cipher_t *cipher, const char *name) { const EVP_CIPHER *evp_cipher = EVP_get_cipherbyname(name); if(!evp_cipher) { logger(DEBUG_ALWAYS, LOG_ERR, "Unknown cipher name '%s'!", name); - return NULL; + return false; } - return cipher_open(evp_cipher); + cipher_open(cipher, evp_cipher); + return true; } -cipher_t *cipher_open_by_nid(int nid) { +bool cipher_open_by_nid(cipher_t *cipher, int nid) { const EVP_CIPHER *evp_cipher = EVP_get_cipherbynid(nid); if(!evp_cipher) { logger(DEBUG_ALWAYS, LOG_ERR, "Unknown cipher nid %d!", nid); - return NULL; + return false; } - return cipher_open(evp_cipher); + cipher_open(cipher, evp_cipher); + return true; } void cipher_close(cipher_t *cipher) { @@ -71,8 +65,11 @@ void cipher_close(cipher_t *cipher) { return; } - EVP_CIPHER_CTX_free(cipher->ctx); - free(cipher); + if(cipher->ctx) { + EVP_CIPHER_CTX_free(cipher->ctx); + } + + memset(cipher, 0, sizeof(*cipher)); } size_t cipher_keylength(const cipher_t *cipher) {