Format code

This commit is contained in:
PolynomialDivision 2017-08-16 22:24:25 +02:00
parent 88ca87c654
commit 7ba1f28f6b
9 changed files with 77 additions and 73 deletions

View file

@ -6,28 +6,25 @@
#define GCRY_CIPHER GCRY_CIPHER_AES128 // Pick the cipher here
#define GCRY_C_MODE GCRY_CIPHER_MODE_ECB // Pick the cipher mode here
gcry_error_t gcry_error_handle;
gcry_error_t gcry_error_handle;
gcry_cipher_hd_t gcry_cipher_hd;
void gcrypt_init()
{
if (!gcry_check_version (GCRYPT_VERSION))
{
fprintf(stderr,"gcrypt: library version mismatch");
void gcrypt_init() {
if (!gcry_check_version(GCRYPT_VERSION)) {
fprintf(stderr, "gcrypt: library version mismatch");
}
gcry_error_t err = 0;
err = gcry_control (GCRYCTL_SUSPEND_SECMEM_WARN);
err |= gcry_control (GCRYCTL_INIT_SECMEM, 16384, 0);
err |= gcry_control (GCRYCTL_RESUME_SECMEM_WARN);
err |= gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
err = gcry_control(GCRYCTL_SUSPEND_SECMEM_WARN);
err |= gcry_control(GCRYCTL_INIT_SECMEM, 16384, 0);
err |= gcry_control(GCRYCTL_RESUME_SECMEM_WARN);
err |= gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
if (err) {
fprintf(stderr,"gcrypt: failed initialization");
fprintf(stderr, "gcrypt: failed initialization");
}
}
void gcrypt_set_key_and_iv(char *key, char *iv)
{
void gcrypt_set_key_and_iv(char *key, char *iv) {
size_t keylen = gcry_cipher_get_algo_keylen(GCRY_CIPHER);
size_t blklen = gcry_cipher_get_algo_blklen(GCRY_CIPHER);
@ -36,37 +33,33 @@ void gcrypt_set_key_and_iv(char *key, char *iv)
GCRY_CIPHER, // int
GCRY_C_MODE, // int
0);
if (gcry_error_handle)
{
if (gcry_error_handle) {
fprintf(stderr, "gcry_cipher_open failed: %s/%s\n",
gcry_strsource(gcry_error_handle),
gcry_strerror(gcry_error_handle));
gcry_strsource(gcry_error_handle),
gcry_strerror(gcry_error_handle));
return;
}
gcry_error_handle = gcry_cipher_setkey(gcry_cipher_hd, key, keylen);
if (gcry_error_handle)
{
if (gcry_error_handle) {
fprintf(stderr, "gcry_cipher_setkey failed: %s/%s\n",
gcry_strsource(gcry_error_handle),
gcry_strerror(gcry_error_handle));
gcry_strsource(gcry_error_handle),
gcry_strerror(gcry_error_handle));
return;
}
gcry_error_handle = gcry_cipher_setiv(gcry_cipher_hd, iv, blklen);
if (gcry_error_handle)
{
if (gcry_error_handle) {
fprintf(stderr, "gcry_cipher_setiv failed: %s/%s\n",
gcry_strsource(gcry_error_handle),
gcry_strerror(gcry_error_handle));
gcry_strsource(gcry_error_handle),
gcry_strerror(gcry_error_handle));
return;
}
}
// free out buffer after using!
char* gcrypt_encrypt_msg(char* msg, size_t msg_length)
{
if ( 0U != (msg_length & 0xfU) )
char *gcrypt_encrypt_msg(char *msg, size_t msg_length) {
if (0U != (msg_length & 0xfU))
msg_length += 0x10U - (msg_length & 0xfU);
//msg_length++; // increase because of \0
@ -77,8 +70,7 @@ char* gcrypt_encrypt_msg(char* msg, size_t msg_length)
msg_length, // size_t
msg, // const void *
msg_length); // size_t
if (gcry_error_handle)
{
if (gcry_error_handle) {
printf("gcry_cipher_encrypt failed: %s/%s\n",
gcry_strsource(gcry_error_handle),
gcry_strerror(gcry_error_handle));
@ -88,27 +80,25 @@ char* gcrypt_encrypt_msg(char* msg, size_t msg_length)
}
// free out buffer after using!
char* gcrypt_decrypt_msg(char* msg, size_t msg_length)
{
if ( 0U != (msg_length & 0xfU) )
char *gcrypt_decrypt_msg(char *msg, size_t msg_length) {
if (0U != (msg_length & 0xfU))
msg_length += 0x10U - (msg_length & 0xfU);
char* out_buffer = malloc(msg_length);
char *out_buffer = malloc(msg_length);
gcry_error_handle = gcry_cipher_decrypt(
gcry_cipher_hd, // gcry_cipher_hd_t
out_buffer, // void *
msg_length, // size_t
msg, // const void *
msg_length); // size_t
if (gcry_error_handle)
{
if (gcry_error_handle) {
printf("gcry_cipher_encrypt failed: %s/%s\n",
gcry_strsource(gcry_error_handle),
gcry_strerror(gcry_error_handle));
free(out_buffer);
return NULL;
}
char* out = malloc(strlen(out_buffer) + 1);
char *out = malloc(strlen(out_buffer) + 1);
strcpy(out, out_buffer);
free(out_buffer);
return out;