Skip to content

Commit

Permalink
crypto: clang-format for coherent indentation and coding style
Browse files Browse the repository at this point in the history
  • Loading branch information
linuxmaniac committed May 18, 2023
1 parent 00168ef commit 913ef5b
Show file tree
Hide file tree
Showing 9 changed files with 308 additions and 281 deletions.
2 changes: 1 addition & 1 deletion src/modules/crypto/api.c
Expand Up @@ -22,7 +22,7 @@
#include "api.h"
#include "crypto_uuid.h"

int load_crypto( struct crypto_binds *cb )
int load_crypto(struct crypto_binds *cb)
{
cb->SHA1 = crypto_generate_SHA1;

Expand Down
15 changes: 8 additions & 7 deletions src/modules/crypto/api.h
Expand Up @@ -24,38 +24,39 @@
#include "../../core/str.h"
#include "../../core/sr_module.h"

typedef int (*SHA1_hash_f)(str*, str*);
typedef int (*SHA1_hash_f)(str *, str *);

typedef struct crypto_binds {
typedef struct crypto_binds
{
SHA1_hash_f SHA1;
} crypto_api_t;


typedef int (*load_crypto_f)( struct crypto_binds* );
typedef int (*load_crypto_f)(struct crypto_binds *);

/*!
* \brief API bind function exported by the module - it will load the other functions
* \param rrb record-route API export binding
* \return 1
*/
int load_crypto( struct crypto_binds *cb );
int load_crypto(struct crypto_binds *cb);

/*!
* \brief Function to be called directly from other modules to load the CRYPTO API
* \param cb crypto API export binding
* \return 0 on success, -1 if the API loader could not imported
*/
inline static int load_crypto_api( struct crypto_binds *cb )
inline static int load_crypto_api(struct crypto_binds *cb)
{
load_crypto_f load_crypto_v;

/* import the crypto auto-loading function */
if ( !(load_crypto_v=(load_crypto_f)find_export("load_crypto", 0, 0))) {
if(!(load_crypto_v = (load_crypto_f)find_export("load_crypto", 0, 0))) {
LM_ERR("failed to import load_crypto\n");
return -1;
}
/* let the auto-loading function load all crypto stuff */
load_crypto_v( cb );
load_crypto_v(cb);

return 0;
}
Expand Down
54 changes: 28 additions & 26 deletions src/modules/crypto/crypto_aes.c
Expand Up @@ -41,16 +41,17 @@ int crypto_set_salt(char *psalt)
int i;
char k;

memset(_crypto_salt, 0, CRYPTO_SALT_BSIZE*sizeof(char));
if(psalt!=NULL) {
if(strlen(psalt)<8) {
memset(_crypto_salt, 0, CRYPTO_SALT_BSIZE * sizeof(char));
if(psalt != NULL) {
if(strlen(psalt) < 8) {
LM_ERR("salt parameter must be at least 8 characters\n");
return -1;
}
k = 97;
for(i=0; i<strlen(psalt); i++) {
if(i>=CRYPTO_SALT_BSIZE) break;
_crypto_salt[i] = (psalt[i]*7 + k + k*(i+1))%0xff;
for(i = 0; i < strlen(psalt); i++) {
if(i >= CRYPTO_SALT_BSIZE)
break;
_crypto_salt[i] = (psalt[i] * 7 + k + k * (i + 1)) % 0xff;
k = _crypto_salt[i];
}
_crypto_salt_set = 1;
Expand Down Expand Up @@ -79,7 +80,8 @@ int crypto_aes_init(unsigned char *key_data, int key_data_len,
{
int i, nrounds = 5;
int x;
unsigned char key[32], iv[32]; /* IV is only 16 bytes, but makes it easier */
unsigned char key[32],
iv[32]; /* IV is only 16 bytes, but makes it easier */
const EVP_CIPHER *cipher;

memset(key, 0, sizeof(key));
Expand All @@ -90,28 +92,28 @@ int crypto_aes_init(unsigned char *key_data, int key_data_len,
* nrounds is the number of times the we hash the material. More rounds
* are more secure but slower.
*/
if(_crypto_key_derivation){
i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha1(), salt,
key_data, key_data_len, nrounds, key, iv);
if (i != 32) {
if(_crypto_key_derivation) {
i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha1(), salt, key_data,
key_data_len, nrounds, key, iv);
if(i != 32) {
LM_ERR("key size is %d bits - should be 256 bits\n", i);
return -1;
}

for(x = 0; x<32; ++x)
for(x = 0; x < 32; ++x)
LM_DBG("key: %x iv: %x \n", key[x], iv[x]);

for(x = 0; x<8; ++x)
for(x = 0; x < 8; ++x)
LM_DBG("salt: %x\n", salt[x]);
cipher = EVP_aes_256_cbc();
} else {
cipher = (key_data_len == 16) ? EVP_aes_128_cbc() : EVP_aes_256_cbc();
LM_DBG("got %d bytes key\n", key_data_len * 8);
memcpy(key, key_data, key_data_len);
if (custom_iv)
if(custom_iv)
memcpy(iv, custom_iv, 16);

for(x = 0; x<key_data_len; ++x)
for(x = 0; x < key_data_len; ++x)
LM_DBG("key: %x, iv: %x\n", key[x], iv[x]);
}

Expand All @@ -131,8 +133,8 @@ int crypto_aes_init(unsigned char *key_data, int key_data_len,
* Encrypt *len bytes of data
* All data going in & out is considered binary (unsigned char[])
*/
unsigned char *crypto_aes_encrypt(EVP_CIPHER_CTX *e, unsigned char *plaintext,
int *len)
unsigned char *crypto_aes_encrypt(
EVP_CIPHER_CTX *e, unsigned char *plaintext, int *len)
{
/* max ciphertext len for a n bytes of plaintext is
* n + AES_BLOCK_SIZE -1 bytes */
Expand All @@ -144,22 +146,22 @@ unsigned char *crypto_aes_encrypt(EVP_CIPHER_CTX *e, unsigned char *plaintext,
return NULL;
}
/* allows reusing of 'e' for multiple encryption cycles */
if(!EVP_EncryptInit_ex(e, NULL, NULL, NULL, NULL)){
if(!EVP_EncryptInit_ex(e, NULL, NULL, NULL, NULL)) {
LM_ERR("failure in EVP_EncryptInit_ex \n");
free(ciphertext);
return NULL;
}

/* update ciphertext, c_len is filled with the length of ciphertext
* generated, *len is the size of plaintext in bytes */
if(!EVP_EncryptUpdate(e, ciphertext, &c_len, plaintext, *len)){
if(!EVP_EncryptUpdate(e, ciphertext, &c_len, plaintext, *len)) {
LM_ERR("failure in EVP_EncryptUpdate \n");
free(ciphertext);
return NULL;
}

/* update ciphertext with the final remaining bytes */
if(!EVP_EncryptFinal_ex(e, ciphertext+c_len, &f_len)){
if(!EVP_EncryptFinal_ex(e, ciphertext + c_len, &f_len)) {
LM_ERR("failure in EVP_EncryptFinal_ex \n");
free(ciphertext);
return NULL;
Expand All @@ -172,30 +174,30 @@ unsigned char *crypto_aes_encrypt(EVP_CIPHER_CTX *e, unsigned char *plaintext,
/*
* Decrypt *len bytes of ciphertext
*/
unsigned char *crypto_aes_decrypt(EVP_CIPHER_CTX *e, unsigned char *ciphertext,
int *len)
unsigned char *crypto_aes_decrypt(
EVP_CIPHER_CTX *e, unsigned char *ciphertext, int *len)
{
/* plaintext will always be equal to or lesser than length of ciphertext*/
int p_len = *len, f_len = 0;
unsigned char *plaintext = (unsigned char *)malloc(p_len);

if(plaintext==NULL) {
if(plaintext == NULL) {
SYS_MEM_ERROR;
return NULL;
}
if(!EVP_DecryptInit_ex(e, NULL, NULL, NULL, NULL)){
if(!EVP_DecryptInit_ex(e, NULL, NULL, NULL, NULL)) {
LM_ERR("failure in EVP_DecryptInit_ex \n");
free(plaintext);
return NULL;
}

if(!EVP_DecryptUpdate(e, plaintext, &p_len, ciphertext, *len)){
if(!EVP_DecryptUpdate(e, plaintext, &p_len, ciphertext, *len)) {
LM_ERR("failure in EVP_DecryptUpdate\n");
free(plaintext);
return NULL;
}

if(!EVP_DecryptFinal_ex(e, plaintext+p_len, &f_len)){
if(!EVP_DecryptFinal_ex(e, plaintext + p_len, &f_len)) {
LM_ERR("failure in EVP_DecryptFinal_ex\n");
free(plaintext);
return NULL;
Expand Down
8 changes: 4 additions & 4 deletions src/modules/crypto/crypto_aes.h
Expand Up @@ -34,9 +34,9 @@ int crypto_aes_init(unsigned char *key_data, int key_data_len,
unsigned char *salt, unsigned char *custom_iv, EVP_CIPHER_CTX *e_ctx,
EVP_CIPHER_CTX *d_ctx);

unsigned char *crypto_aes_encrypt(EVP_CIPHER_CTX *e, unsigned char *plaintext,
int *len);
unsigned char *crypto_aes_encrypt(
EVP_CIPHER_CTX *e, unsigned char *plaintext, int *len);

unsigned char *crypto_aes_decrypt(EVP_CIPHER_CTX *e, unsigned char *ciphertext,
int *len);
unsigned char *crypto_aes_decrypt(
EVP_CIPHER_CTX *e, unsigned char *ciphertext, int *len);
#endif

0 comments on commit 913ef5b

Please sign in to comment.