Skip to content

Commit

Permalink
make chars legacy
Browse files Browse the repository at this point in the history
  • Loading branch information
Xavrax committed Sep 27, 2023
1 parent da535d7 commit b8339e9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
32 changes: 22 additions & 10 deletions core/pbcc_crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
//#ifdef PUBNUB_CRYPTO_API

#include "pbcc_crypto.h"
#include "pbaes256.h"
#include "pubnub_memory_block.h"
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
Expand All @@ -24,10 +26,10 @@ static int aes_decrypt(
);

struct aes_context {
const char* cipher_key;
const uint8_t* cipher_key;
};

struct pubnub_crypto_algorithm_t *pbcc_aes_cbc_init(const char* cipher_key) {
struct pubnub_crypto_algorithm_t *pbcc_aes_cbc_init(const uint8_t* cipher_key) {
struct pubnub_crypto_algorithm_t *algo =
(struct pubnub_crypto_algorithm_t *)malloc(sizeof(struct pubnub_crypto_algorithm_t));
if (algo == NULL) {
Expand Down Expand Up @@ -60,7 +62,7 @@ static size_t estimated_dec_buffer_size(size_t n) {
return n;
}

static void generate_init_vector(char *iv) {
static void generate_init_vector(uint8_t *iv) {
for (int i = 0; i < AES_IV_SIZE; i++) {
iv[i] = rand() % 256;
}
Expand All @@ -75,26 +77,29 @@ static int aes_encrypt(

size_t enc_buffer_size = estimated_enc_buffer_size(to_encrypt.size);

result->data.ptr = (char *)malloc(enc_buffer_size);
result->data.ptr = (uint8_t *)malloc(enc_buffer_size);
if (result->data.ptr == NULL) {
return -1;
}

result->data.size = enc_buffer_size;

char iv[AES_IV_SIZE];
uint8_t iv[AES_IV_SIZE];
generate_init_vector(iv);

// TODO: use proper function to fulfill the data.ptr

result->metadata.ptr = (char *)malloc(AES_IV_SIZE);
pbaes256_encrypt(
to_encrypt,
ctx->cipher_key,
iv,
&result->data
);

result->metadata.ptr = (uint8_t *)malloc(AES_IV_SIZE);
if (result->metadata.ptr == NULL) {
free(result->data.ptr);
return -1;
}

// TODO: resize the data.ptr to the actual size

memcpy(result->metadata.ptr, iv, AES_IV_SIZE);
result->metadata.size = AES_IV_SIZE;

Expand All @@ -121,6 +126,13 @@ static int aes_decrypt(

result->size = dec_buffer_size;

pbaes256_decrypt(
to_decrypt.data,
ctx->cipher_key,
to_decrypt.metadata.ptr,
result
);

// TODO: use proper function to fulfill base64_str

return 0;
Expand Down
8 changes: 4 additions & 4 deletions core/pbcc_crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
*/
struct pubnub_encrypted_data {
/** Encrypted data. */
struct pubnub_char_mem_block data;
struct pubnub_byte_mem_block data;

/** Metadata.
Expand All @@ -41,7 +41,7 @@ struct pubnub_encrypted_data {
- cipher key Identifier
- encrypted *data* length
*/
struct pubnub_char_mem_block metadata;
struct pubnub_byte_mem_block metadata;
};


Expand Down Expand Up @@ -161,7 +161,7 @@ int pubnub_cryptor_decrypt(pubnub_cryptor const *cryptor, pubnub_chamebl_t const
@return Pointer to the AES CBC algorithm structure.
*/
struct pubnub_crypto_algorithm_t *pbcc_aes_cbc_init(const char* cipher_key);
struct pubnub_crypto_algorithm_t *pbcc_aes_cbc_init(const uint8_t* cipher_key);


/**
Expand All @@ -172,7 +172,7 @@ struct pubnub_crypto_algorithm_t *pbcc_aes_cbc_init(const char* cipher_key);
@return Pointer to the legacy algorithm structure.
*/
struct pubnub_crypto_algorithm_t *pbcc_legacy_crypto_init(const char* cipher_key);
struct pubnub_crypto_algorithm_t *pbcc_legacy_crypto_init(const uint8_t* cipher_key);


#endif /* PBCC_CRYPTO_H */
Expand Down
10 changes: 5 additions & 5 deletions core/pbcc_crypto_legacy.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ static int legacy_decrypt(
);

struct legacy_context {
const char* cipher_key;
const uint8_t* cipher_key;
};

struct pubnub_crypto_algorithm_t *pbcc_legacy_crypto_init(const char* cipher_key) {
struct pubnub_crypto_algorithm_t *pbcc_legacy_crypto_init(const uint8_t* cipher_key) {
struct pubnub_crypto_algorithm_t *algo =
(struct pubnub_crypto_algorithm_t *)malloc(sizeof(struct pubnub_crypto_algorithm_t));
if (algo == NULL) {
Expand Down Expand Up @@ -68,13 +68,13 @@ static int legacy_encrypt(
struct legacy_context *ctx = (struct legacy_context *)algo->user_data;

size_t estimated_size = base64_max_size(estimated_enc_buffer_size(to_encrypt.size));
result->data.ptr = (char*)malloc(estimated_size);
result->data.ptr = (uint8_t*)malloc(estimated_size);
if (NULL == result->data.ptr) {
return -1;
}
result->data.size = estimated_size;

int res = pubnub_encrypt(ctx->cipher_key, to_encrypt, result->data.ptr, &result->data.size);
int res = pubnub_encrypt((char*)ctx->cipher_key, to_encrypt, (char*)result->data.ptr, &result->data.size);
if (0 != res) {
return res;
}
Expand All @@ -99,7 +99,7 @@ static int legacy_decrypt(
}
result->size = estimated_size;

int res = pubnub_decrypt(ctx->cipher_key, to_decrypt.data.ptr, result);
int res = pubnub_decrypt((char*)ctx->cipher_key, (char*)to_decrypt.data.ptr, result);
if (0 != res) {
return res;
}
Expand Down

0 comments on commit b8339e9

Please sign in to comment.