mirror of
https://github.com/berlin-open-wireless-lab/DAWN.git
synced 2025-03-09 15:40:12 +00:00
Change crypto to ssl
This commit is contained in:
parent
f7a85bfc7c
commit
48168cb9ac
5 changed files with 173 additions and 36 deletions
|
@ -38,7 +38,7 @@ SET(SOURCES
|
|||
utils/utils.c)
|
||||
|
||||
SET(LIBS
|
||||
ubox ubus json-c blobmsg_json config uci gcrypt openssl)
|
||||
ubox ubus json-c blobmsg_json config uci gcrypt ssl crypto)
|
||||
|
||||
ADD_EXECUTABLE(dawn ${SOURCES} utils/dawn_uci.c include/dawn_uci.h)
|
||||
|
||||
|
|
|
@ -59,7 +59,7 @@ void gcrypt_set_key_and_iv(char *key, char *iv) {
|
|||
}
|
||||
|
||||
// free out buffer after using!
|
||||
char *gcrypt_encrypt_msg(char *msg, size_t msg_length) {
|
||||
char *gcrypt_encrypt_msg(char *msg, size_t msg_length, int* out_length) {
|
||||
if (0U != (msg_length & 0xfU))
|
||||
msg_length += 0x10U - (msg_length & 0xfU);
|
||||
|
||||
|
@ -77,6 +77,7 @@ char *gcrypt_encrypt_msg(char *msg, size_t msg_length) {
|
|||
gcry_strerror(gcry_error_handle));
|
||||
return NULL;
|
||||
}
|
||||
*out_length = msg_length;
|
||||
return out;
|
||||
}
|
||||
|
||||
|
@ -202,4 +203,125 @@ void base64_cleanup() {
|
|||
free(decoding_table);
|
||||
}
|
||||
|
||||
static const unsigned char pr2six[256] =
|
||||
{
|
||||
/* ASCII table */
|
||||
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
|
||||
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
|
||||
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
|
||||
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
|
||||
64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
|
||||
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
|
||||
64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
|
||||
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
|
||||
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
|
||||
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
|
||||
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
|
||||
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
|
||||
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
|
||||
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
|
||||
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
|
||||
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
|
||||
};
|
||||
|
||||
int Base64decode_len(const char *bufcoded)
|
||||
{
|
||||
int nbytesdecoded;
|
||||
register const unsigned char *bufin;
|
||||
register int nprbytes;
|
||||
|
||||
bufin = (const unsigned char *) bufcoded;
|
||||
while (pr2six[*(bufin++)] <= 63);
|
||||
|
||||
nprbytes = (bufin - (const unsigned char *) bufcoded) - 1;
|
||||
nbytesdecoded = ((nprbytes + 3) / 4) * 3;
|
||||
|
||||
return nbytesdecoded + 1;
|
||||
}
|
||||
|
||||
int Base64decode(char *bufplain, const char *bufcoded)
|
||||
{
|
||||
int nbytesdecoded;
|
||||
register const unsigned char *bufin;
|
||||
register unsigned char *bufout;
|
||||
register int nprbytes;
|
||||
|
||||
bufin = (const unsigned char *) bufcoded;
|
||||
while (pr2six[*(bufin++)] <= 63);
|
||||
nprbytes = (bufin - (const unsigned char *) bufcoded) - 1;
|
||||
nbytesdecoded = ((nprbytes + 3) / 4) * 3;
|
||||
|
||||
bufout = (unsigned char *) bufplain;
|
||||
bufin = (const unsigned char *) bufcoded;
|
||||
|
||||
while (nprbytes > 4) {
|
||||
*(bufout++) =
|
||||
(unsigned char) (pr2six[*bufin] << 2 | pr2six[bufin[1]] >> 4);
|
||||
*(bufout++) =
|
||||
(unsigned char) (pr2six[bufin[1]] << 4 | pr2six[bufin[2]] >> 2);
|
||||
*(bufout++) =
|
||||
(unsigned char) (pr2six[bufin[2]] << 6 | pr2six[bufin[3]]);
|
||||
bufin += 4;
|
||||
nprbytes -= 4;
|
||||
}
|
||||
|
||||
/* Note: (nprbytes == 1) would be an error, so just ingore that case */
|
||||
if (nprbytes > 1) {
|
||||
*(bufout++) =
|
||||
(unsigned char) (pr2six[*bufin] << 2 | pr2six[bufin[1]] >> 4);
|
||||
}
|
||||
if (nprbytes > 2) {
|
||||
*(bufout++) =
|
||||
(unsigned char) (pr2six[bufin[1]] << 4 | pr2six[bufin[2]] >> 2);
|
||||
}
|
||||
if (nprbytes > 3) {
|
||||
*(bufout++) =
|
||||
(unsigned char) (pr2six[bufin[2]] << 6 | pr2six[bufin[3]]);
|
||||
}
|
||||
|
||||
*(bufout++) = '\0';
|
||||
nbytesdecoded -= (4 - nprbytes) & 3;
|
||||
return nbytesdecoded;
|
||||
}
|
||||
|
||||
static const char basis_64[] =
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
|
||||
int Base64encode_len(int len)
|
||||
{
|
||||
return ((len + 2) / 3 * 4) + 1;
|
||||
}
|
||||
|
||||
int Base64encode(char *encoded, const char *string, int len)
|
||||
{
|
||||
int i;
|
||||
char *p;
|
||||
|
||||
p = encoded;
|
||||
for (i = 0; i < len - 2; i += 3) {
|
||||
*p++ = basis_64[(string[i] >> 2) & 0x3F];
|
||||
*p++ = basis_64[((string[i] & 0x3) << 4) |
|
||||
((int) (string[i + 1] & 0xF0) >> 4)];
|
||||
*p++ = basis_64[((string[i + 1] & 0xF) << 2) |
|
||||
((int) (string[i + 2] & 0xC0) >> 6)];
|
||||
*p++ = basis_64[string[i + 2] & 0x3F];
|
||||
}
|
||||
if (i < len) {
|
||||
*p++ = basis_64[(string[i] >> 2) & 0x3F];
|
||||
if (i == (len - 1)) {
|
||||
*p++ = basis_64[((string[i] & 0x3) << 4)];
|
||||
*p++ = '=';
|
||||
}
|
||||
else {
|
||||
*p++ = basis_64[((string[i] & 0x3) << 4) |
|
||||
((int) (string[i + 1] & 0xF0) >> 4)];
|
||||
*p++ = basis_64[((string[i + 1] & 0xF) << 2)];
|
||||
}
|
||||
*p++ = '=';
|
||||
}
|
||||
|
||||
*p++ = '\0';
|
||||
return p - encoded;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -3,11 +3,17 @@
|
|||
|
||||
#include <stdlib.h>
|
||||
|
||||
char *base_64(const unsigned char *input, int length);
|
||||
|
||||
char *unbase_64(unsigned char *input, int length);
|
||||
|
||||
void gcrypt_init();
|
||||
|
||||
void gcrypt_set_key_and_iv(char *key, char *iv);
|
||||
|
||||
char *gcrypt_encrypt_msg(char *msg, size_t msg_length);
|
||||
//char *gcrypt_encrypt_msg(char *msg, size_t msg_length);
|
||||
char *gcrypt_encrypt_msg(char *msg, size_t msg_length, int* out_length);
|
||||
|
||||
|
||||
char *gcrypt_decrypt_msg(char *msg, size_t msg_length);
|
||||
|
||||
|
@ -24,5 +30,10 @@ void build_decoding_table();
|
|||
|
||||
void base64_cleanup();
|
||||
|
||||
int Base64decode_len(const char *bufcoded);
|
||||
int Base64encode_len(int len);
|
||||
int Base64encode(char *encoded, const char *string, int len);
|
||||
int Base64decode(char *bufplain, const char *bufcoded);
|
||||
|
||||
|
||||
#endif //DAWN_CRYPTO_H
|
||||
|
|
46
src/main.c
46
src/main.c
|
@ -6,13 +6,6 @@
|
|||
#include "ubus.h"
|
||||
#include "dawn_uci.h"
|
||||
|
||||
/* SSL TESTNG */
|
||||
|
||||
#include <openssl/bio.h>
|
||||
#include <openssl/evp.h>
|
||||
|
||||
BIO_METHOD * BIO_f_base64(void);
|
||||
|
||||
#define BUFSIZE 17
|
||||
#define BUFSIZE_DIR 256
|
||||
|
||||
|
@ -70,31 +63,40 @@ int main(int argc, char **argv) {
|
|||
* ----
|
||||
*/
|
||||
|
||||
BIO *bio, *b64;
|
||||
char message[] = "Hello World \n";
|
||||
|
||||
b64 = BIO_new(BIO_f_base64());
|
||||
bio = BIO_new_fp(stdout, BIO_NOCLOSE);
|
||||
bio = BIO_push(b64, bio);
|
||||
BIO_write(bio, message, strlen(message));
|
||||
BIO_flush(bio);
|
||||
|
||||
BIO_free_all(bio);
|
||||
|
||||
build_decoding_table();
|
||||
char msg[] = "{\"bssid\":\"a4:2b:b0:de:f1:fd\",\"freq\":5180,\"ht_supported\":true,\"vht_supported\":true,\"clients\":{\"78:02:f8:bc:ac:0b\":{\"auth\":true,\"assoc\":true,\"authorized\":true,\"preauth\":false,\"wds\":false,\"wmm\":true,\"ht\":true,\"vht\":true,\"wps\":false,\"mfp\":false,\"aid\":1}}}";
|
||||
gcrypt_init();
|
||||
gcrypt_set_key_and_iv(shared_key, iv);
|
||||
printf("Encrypting msg: %s\n", msg);
|
||||
char *enc = gcrypt_encrypt_msg(msg, strlen(msg) + 1);
|
||||
|
||||
printf("Decrypting msg: %s\n", enc);
|
||||
int length_str;
|
||||
char *enc = gcrypt_encrypt_msg(msg, strlen(msg) + 1, &length_str);
|
||||
|
||||
printf("Sizeof: %d, Strlen: %d, Acutal: %d\n", sizeof(enc) * sizeof(char), strlen(enc), strlen(msg) + 1);
|
||||
//size_t output_length;
|
||||
//char *output = base64_encode((unsigned char*)enc, strlen(msg), &output_length);
|
||||
|
||||
char *dec = gcrypt_decrypt_msg(enc, strlen(msg) + 1);//sizeof(enc));
|
||||
char* output = malloc(Base64encode_len(length_str));
|
||||
int length = Base64encode(output, enc, length_str);
|
||||
|
||||
printf("Decrypting msg: %s\n", output);
|
||||
|
||||
printf("Length: %d, Strlen: %d, Acutal: %d\n", length, strlen(output), strlen(msg) + 1);
|
||||
|
||||
//size_t decode_length;
|
||||
//unsigned char * output_dec = base64_decode(output, strlen(output), &decode_length);
|
||||
|
||||
char* output_dec = malloc(Base64decode_len(output));
|
||||
int decode_length = Base64decode(output_dec, output);
|
||||
|
||||
printf("Output dec: %s Length: %d\n", output_dec, decode_length);
|
||||
|
||||
char *dec = gcrypt_decrypt_msg((char*)output_dec, decode_length);//sizeof(enc));
|
||||
printf("Message decrypted: %s\n", dec);
|
||||
printf("Message size: %d \n", strlen(dec));
|
||||
free(enc);
|
||||
free(dec);
|
||||
free(output);
|
||||
free(output_dec);
|
||||
|
||||
/*
|
||||
* ----
|
||||
|
|
|
@ -140,11 +140,12 @@ void *receive_msg_enc(void *args) {
|
|||
}
|
||||
//recv_string[recv_string_len] = '\0';
|
||||
|
||||
// first decode base 64
|
||||
char* base64_dec_str = malloc(Base64decode_len(recv_string));
|
||||
int base64_dec_length = Base64decode(base64_dec_str, recv_string);
|
||||
|
||||
size_t base64_msg_len;
|
||||
unsigned char* base64_msg_dec = base64_decode(recv_string, recv_string_len, &base64_msg_len);
|
||||
char *dec = gcrypt_decrypt_msg((char*)base64_msg_dec, base64_msg_len);
|
||||
char *dec = gcrypt_decrypt_msg(base64_dec_str, base64_dec_length);
|
||||
|
||||
free(base64_dec_str);
|
||||
|
||||
printf("[WC] Network-Received: %s\n", dec);
|
||||
|
||||
|
@ -207,16 +208,15 @@ int send_string_enc(char *msg) {
|
|||
pthread_mutex_lock(&send_mutex);
|
||||
size_t msglen = strlen(msg);
|
||||
|
||||
char *enc = gcrypt_encrypt_msg(msg, msglen + 1);
|
||||
int length_enc;
|
||||
char *enc = gcrypt_encrypt_msg(msg, msglen + 1, &length_enc);
|
||||
|
||||
//char* base64_msg_dec = g_base64_encode(msg, msglen + 1);
|
||||
|
||||
size_t base64_msg_len;
|
||||
char* base64_msg_dec = base64_encode((unsigned char*)enc, msglen, &base64_msg_len);
|
||||
char* base64_enc_str = malloc(Base64encode_len(length_enc));
|
||||
size_t base64_enc_length = Base64encode(base64_enc_str, enc, length_enc);
|
||||
|
||||
if (sendto(sock,
|
||||
base64_msg_dec,
|
||||
msglen + 1, // very important to use actual length of string because of '\0' in encrypted msg
|
||||
base64_enc_str,
|
||||
base64_enc_length, // very important to use actual length of string because of '\0' in encrypted msg
|
||||
0,
|
||||
(struct sockaddr *) &addr,
|
||||
sizeof(addr)) < 0) {
|
||||
|
@ -224,6 +224,8 @@ int send_string_enc(char *msg) {
|
|||
pthread_mutex_unlock(&send_mutex);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
free(base64_enc_str);
|
||||
free(enc);
|
||||
pthread_mutex_unlock(&send_mutex);
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue