I Have the following simple code.
Im just initializing the parameters, calling a make_key function, encrypting the data and trying to decrypted the data
#define LTM_DESC
#include <tomcrypt.h>
int main(void)
{
ecc_key mykey;
prng_state prng;
int err, prng_idx , hash_idx;
ltc_mp = ltm_desc;
/* register yarrow */
if (register_prng(&yarrow_desc) == -1) {
printf("Error registering Yarrow\n");
return -1;
}
if (register_hash(&sha1_desc) == -1) {
printf("Error registering sha1");
return EXIT_FAILURE;
}
prng_idx = find_prng("yarrow");
hash_idx = find_hash("sha1");
/* setup the PRNG */
if ((err = rng_make_prng(128, find_prng("yarrow"), &prng, NULL))
!= CRYPT_OK) {
printf("Error setting up PRNG, %s\n", error_to_string(err));
return -1;
}
/* make a 192-bit ECC key */
if ((err = ecc_make_key(&prng, find_prng("yarrow"), 55, &mykey))
!= CRYPT_OK) {
printf("Error making key: %s\n", error_to_string(err));
return -1;
}
const unsigned char *plaintext = (const unsigned char *)"Hello, this is a secret message!";
unsigned long ptlen = sizeof(plaintext);
unsigned char ciphertext[1024];
unsigned long ctlen = sizeof(ciphertext);
if ((err = ecc_encrypt_key(plaintext, // const unsinged char *in
ptlen, //unsinged long inlen
ciphertext, // unsinged *out
&ctlen, // unsinged long *outlen
&prng, // prng_state *prng
prng_idx, // int wprng
hash_idx, // int hash
&mykey)) != CRYPT_OK) { // ecc_key *ley
printf("Erro ao criptografar o texto: %s\n", error_to_string(err));
return -1;
}
printf("Text after encrypted: ");
for (unsigned long i = 0; i < ctlen; ++i) {
printf("%02x", ciphertext[i]);
}
printf("\n");
unsigned char decryptedtext[2048];
unsigned long decrypted_len = sizeof(decryptedtext);
if ((err = ecc_decrypt_key(ciphertext, ctlen, decryptedtext, &decrypted_len, &mykey)) != CRYPT_OK) {
printf("Erro ao descriptografar o texto: %s\n", error_to_string(err));
return -1;
}
decryptedtext[decrypted_len] = '\0';
printf("%d",decryptedtext == plaintext);
printf("\n");
printf("Text after decrypted: %.*s\n", (int)decrypted_len, decryptedtext);
printf("Text after decrypted: %s\n", plaintext);
printf("Text after decryptedo: ");
for (unsigned long i = 0; i < decrypted_len; ++i) {
printf("%c", decryptedtext[i]);
}
printf("\n");
// Update decrypted_len based on the actual length of the decrypted text
decrypted_len = strlen((char *)decryptedtext);
// Print the decrypted text
printf("Text after decrypted: %s\n", decryptedtext);
printf("Text after decrypted (Hex): ");
for (unsigned long i = 0; i < decrypted_len; ++i) {
printf("%02x", decryptedtext[i]);
}
printf("\n");
return 0;
}
in my terminal im compiling using
gcc -Wall -O0 -g test2.c -ltomcrypt -o test2
I'd like to see after the line
printf("Text after decrypted: %s\n", decryptedtext);
Text decrypted: Hello, this is a secret message!
but I'm seeing
Text decrypted: Hello, t
Why the print isnt complete? I dont know what to do to see the complete mensage decrypted.
** NEWS
I got news, apparently, doesnt matter the variable 'decrypted_len' after i call the function ecc_decrypted_key it will be set in 8.
What can I do?
This code
Sets
ptlento the size of the pointerplaintext.This code will be better
But that would still be subject to the length limitations of ECC encryption.