cipher->keylen = gcry_cipher_get_algo_keylen(algo);
cipher->blklen = gcry_cipher_get_algo_blklen(algo);
- cipher->key = xmalloc(cipher->keylen + cipher->blklen);
+ cipher->key = xmalloc(cipher_keylength(cipher));
cipher->padding = mode == GCRY_CIPHER_MODE_ECB || mode == GCRY_CIPHER_MODE_CBC;
return true;
}
void cipher_close(cipher_t *cipher) {
+ if(!cipher) {
+ return;
+ }
+
if(cipher->handle) {
gcry_cipher_close(cipher->handle);
- cipher->handle = NULL;
}
- free(cipher->key);
-
+ xzfree(cipher->key, cipher_keylength(cipher));
memset(cipher, 0, sizeof(*cipher));
}
bool cipher_set_key(cipher_t *cipher, void *key, bool encrypt) {
(void)encrypt;
- memcpy(cipher->key, key, cipher->keylen + cipher->blklen);
+ memcpy(cipher->key, key, cipher_keylength(cipher));
gcry_cipher_setkey(cipher->handle, cipher->key, cipher->keylen);
gcry_cipher_setiv(cipher->handle, cipher->key + cipher->keylen, cipher->blklen);