Assertify the crypto api

This commit is contained in:
pancake 2022-09-07 01:41:58 +02:00 committed by pancake
parent 943c01c16d
commit ee762bf15d

View File

@ -2,6 +2,7 @@
#include "r_crypto.h"
#include "config.h"
#include "r_util/r_assert.h"
R_LIB_VERSION (r_crypto);
@ -88,23 +89,26 @@ R_API RCrypto *r_crypto_init(RCrypto *cry, int hard) {
return cry;
}
// R2_580 - return bool
R_API int r_crypto_add(RCrypto *cry, RCryptoPlugin *h) {
// add a check ?
r_return_val_if_fail (cry && cry->plugins && h, false);
r_list_append (cry->plugins, h);
return true;
}
// R2_580 bool
R_API int r_crypto_del(RCrypto *cry, RCryptoPlugin *h) {
r_return_val_if_fail (cry && h, false);
r_list_delete_data (cry->plugins, h);
return true;
}
R_API struct r_crypto_t *r_crypto_new(void) {
R_API RCrypto *r_crypto_new(void) {
RCrypto *cry = R_NEW0 (RCrypto);
return r_crypto_init (cry, true);
}
R_API struct r_crypto_t *r_crypto_as_new(struct r_crypto_t *cry) {
R_API RCrypto *r_crypto_as_new(struct r_crypto_t *cry) {
RCrypto *c = R_NEW0 (RCrypto);
if (c) {
r_crypto_init (c, false); // soft init
@ -113,17 +117,21 @@ R_API struct r_crypto_t *r_crypto_as_new(struct r_crypto_t *cry) {
return c;
}
R_API struct r_crypto_t *r_crypto_free(RCrypto *cry) {
// TODO: call the destructor function of the plugin to destroy the *user pointer if needed
r_list_free (cry->plugins);
free (cry->output);
free (cry->key);
free (cry->iv);
free (cry);
// R2_580 R_API void r_crypto_free(RCrypto *cry) {
R_API RCrypto *r_crypto_free(RCrypto *cry) {
if (cry) {
// TODO: call the destructor function of the plugin to destroy the *user pointer if needed
r_list_free (cry->plugins);
free (cry->output);
free (cry->key);
free (cry->iv);
free (cry);
}
return NULL;
}
R_API bool r_crypto_use(RCrypto *cry, const char *algo) {
r_return_val_if_fail (cry && algo, false);
RListIter *iter;
RCryptoPlugin *h;
r_list_foreach (cry->plugins, iter, h) {
@ -136,10 +144,11 @@ R_API bool r_crypto_use(RCrypto *cry, const char *algo) {
}
R_API bool r_crypto_set_key(RCrypto *cry, const ut8* key, int keylen, int mode, int direction) {
r_return_val_if_fail (cry, false);
if (keylen < 0) {
keylen = strlen ((const char *)key);
}
if (!cry || !cry->h || !cry->h->set_key) {
if (!cry->h || !cry->h->set_key) {
return false;
}
cry->key_len = keylen;
@ -148,31 +157,32 @@ R_API bool r_crypto_set_key(RCrypto *cry, const ut8* key, int keylen, int mode,
}
R_API int r_crypto_get_key_size(RCrypto *cry) {
return (cry && cry->h && cry->h->get_key_size)?
r_return_val_if_fail (cry, false);
return (cry->h && cry->h->get_key_size)?
cry->h->get_key_size (cry): 0;
}
R_API bool r_crypto_set_iv(RCrypto *cry, const ut8 *iv, int ivlen) {
return (cry && cry->h && cry->h->set_iv)?
r_return_val_if_fail (cry, false);
return (cry->h && cry->h->set_iv)?
cry->h->set_iv(cry, iv, ivlen): 0;
}
// return the number of bytes written in the output buffer
R_API int r_crypto_update(RCrypto *cry, const ut8 *buf, int len) {
return (cry && cry->h && cry->h->update)?
cry->h->update (cry, buf, len): 0;
r_return_val_if_fail (cry, 0);
return (cry->h && cry->h->update)? cry->h->update (cry, buf, len): 0;
}
R_API int r_crypto_final(RCrypto *cry, const ut8 *buf, int len) {
return (cry && cry->h && cry->h->final)?
r_return_val_if_fail (cry, 0);
return (cry->h && cry->h->final)?
cry->h->final (cry, buf, len): 0;
}
// TODO: internal api?? used from plugins? TODO: use r_buf here
R_API int r_crypto_append(RCrypto *cry, const ut8 *buf, int len) {
if (!cry || !buf) {
return -1;
}
r_return_val_if_fail (cry && buf, -1);
if (cry->output_len+len > cry->output_size) {
cry->output_size += 4096 + len;
cry->output = realloc (cry->output, cry->output_size);
@ -183,6 +193,7 @@ R_API int r_crypto_append(RCrypto *cry, const ut8 *buf, int len) {
}
R_API ut8 *r_crypto_get_output(RCrypto *cry, int *size) {
r_return_val_if_fail (cry, NULL);
if (cry->output_size < 1) {
return NULL;
}