mirror of
https://github.com/berlin-open-wireless-lab/DAWN.git
synced 2025-03-09 15:40:12 +00:00
Add crypto
This commit is contained in:
parent
1271a50eaf
commit
57209931db
9 changed files with 251 additions and 8 deletions
117
src/crypto/crypto.c
Normal file
117
src/crypto/crypto.c
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
#include "crypto.h"
|
||||
|
||||
#include <gcrypt.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#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_cipher_hd_t gcry_cipher_hd;
|
||||
|
||||
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);
|
||||
|
||||
if (err) {
|
||||
fprintf(stderr,"gcrypt: failed initialization");
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
gcry_error_handle = gcry_cipher_open(
|
||||
&gcry_cipher_hd, // gcry_cipher_hd_t *
|
||||
GCRY_CIPHER, // int
|
||||
GCRY_C_MODE, // int
|
||||
0);
|
||||
if (gcry_error_handle)
|
||||
{
|
||||
printf("gcry_cipher_open failed: %s/%s\n",
|
||||
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)
|
||||
{
|
||||
printf("gcry_cipher_setkey failed: %s/%s\n",
|
||||
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)
|
||||
{
|
||||
printf("gcry_cipher_setiv failed: %s/%s\n",
|
||||
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) )
|
||||
msg_length += 0x10U - (msg_length & 0xfU);
|
||||
|
||||
//msg_length++; // increase because of \0
|
||||
char *out = malloc(msg_length);
|
||||
gcry_error_handle = gcry_cipher_encrypt(
|
||||
gcry_cipher_hd, // gcry_cipher_hd_t
|
||||
out, // void *
|
||||
msg_length, // size_t
|
||||
msg, // const void *
|
||||
msg_length); // size_t
|
||||
printf("Message encrypted: %s : %s size: %d\n", msg, out, msg_length);
|
||||
if (gcry_error_handle)
|
||||
{
|
||||
printf("gcry_cipher_encrypt failed: %s/%s\n",
|
||||
gcry_strsource(gcry_error_handle),
|
||||
gcry_strerror(gcry_error_handle));
|
||||
return NULL;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// free out buffer after using!
|
||||
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);
|
||||
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)
|
||||
{
|
||||
printf("gcry_cipher_encrypt failed: %s/%s\n",
|
||||
gcry_strsource(gcry_error_handle),
|
||||
gcry_strerror(gcry_error_handle));
|
||||
return NULL;
|
||||
}
|
||||
char* out = malloc(strlen(out_buffer) + 1);
|
||||
strcpy(out, out_buffer);
|
||||
free(out_buffer);
|
||||
return out;
|
||||
}
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue