Mercurial > dropbear
view dbmalloc.c @ 1672:3a97f14c0235
Add Chacha20-Poly1305, AES128-GCM and AES256-GCM support (#93)
* Add Chacha20-Poly1305 authenticated encryption
* Add general AEAD approach.
* Add [email protected] algo using LibTomCrypt chacha and
poly1305 routines.
Chacha20-Poly1305 is generally faster than AES256 on CPU w/o dedicated
AES instructions, having the same key size.
Compiling in will add ~5,5kB to binary size on x86-64.
function old new delta
chacha_crypt - 1397 +1397
_poly1305_block - 608 +608
poly1305_done - 595 +595
dropbear_chachapoly_crypt - 457 +457
.rodata 26976 27392 +416
poly1305_process - 290 +290
poly1305_init - 221 +221
chacha_setup - 218 +218
encrypt_packet 1068 1270 +202
dropbear_chachapoly_getlength - 147 +147
decrypt_packet 756 897 +141
chacha_ivctr64 - 137 +137
read_packet 543 637 +94
dropbear_chachapoly_start - 94 +94
read_kex_algos 792 880 +88
chacha_keystream - 69 +69
dropbear_mode_chachapoly - 48 +48
sshciphers 280 320 +40
dropbear_mode_none 24 48 +24
dropbear_mode_ctr 24 48 +24
dropbear_mode_cbc 24 48 +24
dropbear_chachapoly_mac - 24 +24
dropbear_chachapoly - 24 +24
gen_new_keys 848 854 +6
------------------------------------------------------------------------------
(add/remove: 14/0 grow/shrink: 10/0 up/down: 5388/0) Total: 5388 bytes
* Add AES128-GCM and AES256-GCM authenticated encryption
* Add general AES-GCM mode.
* Add [email protected] and [email protected] algo using
LibTomCrypt gcm routines.
AES-GCM is combination of AES CTR mode and GHASH, slower than AES-CTR on
CPU w/o dedicated AES/GHASH instructions therefore disabled by default.
Compiling in will add ~6kB to binary size on x86-64.
function old new delta
gcm_process - 1060 +1060
.rodata 26976 27808 +832
gcm_gf_mult - 820 +820
gcm_add_aad - 660 +660
gcm_shift_table - 512 +512
gcm_done - 471 +471
gcm_add_iv - 384 +384
gcm_init - 347 +347
dropbear_gcm_crypt - 309 +309
encrypt_packet 1068 1270 +202
decrypt_packet 756 897 +141
gcm_reset - 118 +118
read_packet 543 637 +94
read_kex_algos 792 880 +88
sshciphers 280 360 +80
gcm_mult_h - 80 +80
dropbear_gcm_start - 62 +62
dropbear_mode_gcm - 48 +48
dropbear_mode_none 24 48 +24
dropbear_mode_ctr 24 48 +24
dropbear_mode_cbc 24 48 +24
dropbear_ghash - 24 +24
dropbear_gcm_getlength - 24 +24
gen_new_keys 848 854 +6
------------------------------------------------------------------------------
(add/remove: 14/0 grow/shrink: 10/0 up/down: 6434/0) Total: 6434 bytes
author | Vladislav Grishenko <themiron@users.noreply.github.com> |
---|---|
date | Mon, 25 May 2020 20:50:25 +0500 |
parents | c42e8ff42bd1 |
children | 1051e4eea25a |
line wrap: on
line source
#include "dbmalloc.h" #include "dbutil.h" void * m_calloc(size_t nmemb, size_t size) { if (SIZE_T_MAX / nmemb < size) { dropbear_exit("m_calloc failed"); } return m_malloc(nmemb*size); } void * m_strdup(const char * str) { char* ret; unsigned int len; len = strlen(str); ret = m_malloc(len+1); if (ret == NULL) { dropbear_exit("m_strdup failed"); } memcpy(ret, str, len+1); return ret; } #if !DROPBEAR_TRACKING_MALLOC /* Simple wrappers around malloc etc */ void * m_malloc(size_t size) { void* ret; if (size == 0) { dropbear_exit("m_malloc failed"); } ret = calloc(1, size); if (ret == NULL) { dropbear_exit("m_malloc failed"); } return ret; } void * m_realloc(void* ptr, size_t size) { void *ret; if (size == 0) { dropbear_exit("m_realloc failed"); } ret = realloc(ptr, size); if (ret == NULL) { dropbear_exit("m_realloc failed"); } return ret; } #else /* For fuzzing */ struct dbmalloc_header { unsigned int epoch; struct dbmalloc_header *prev; struct dbmalloc_header *next; }; static void put_alloc(struct dbmalloc_header *header); static void remove_alloc(struct dbmalloc_header *header); /* end of the linked list */ static struct dbmalloc_header* staple; unsigned int current_epoch = 0; void m_malloc_set_epoch(unsigned int epoch) { current_epoch = epoch; } void m_malloc_free_epoch(unsigned int epoch, int dofree) { struct dbmalloc_header* header; struct dbmalloc_header* nextheader = NULL; struct dbmalloc_header* oldstaple = staple; staple = NULL; /* free allocations from this epoch, create a new staple-anchored list from the remainder */ for (header = oldstaple; header; header = nextheader) { nextheader = header->next; if (header->epoch == epoch) { if (dofree) { free(header); } } else { header->prev = NULL; header->next = NULL; put_alloc(header); } } } static void put_alloc(struct dbmalloc_header *header) { assert(header->next == NULL); assert(header->prev == NULL); if (staple) { staple->prev = header; } header->next = staple; staple = header; } static void remove_alloc(struct dbmalloc_header *header) { if (header->prev) { header->prev->next = header->next; } if (header->next) { header->next->prev = header->prev; } if (staple == header) { staple = header->next; } header->prev = NULL; header->next = NULL; } static struct dbmalloc_header* get_header(void* ptr) { char* bptr = ptr; return (struct dbmalloc_header*)&bptr[-sizeof(struct dbmalloc_header)]; } void * m_malloc(size_t size) { char* mem = NULL; struct dbmalloc_header* header = NULL; if (size == 0 || size > 1e9) { dropbear_exit("m_malloc failed"); } size = size + sizeof(struct dbmalloc_header); mem = calloc(1, size); if (mem == NULL) { dropbear_exit("m_malloc failed"); } header = (struct dbmalloc_header*)mem; put_alloc(header); header->epoch = current_epoch; return &mem[sizeof(struct dbmalloc_header)]; } void * m_realloc(void* ptr, size_t size) { char* mem = NULL; struct dbmalloc_header* header = NULL; if (size == 0 || size > 1e9) { dropbear_exit("m_realloc failed"); } header = get_header(ptr); remove_alloc(header); size = size + sizeof(struct dbmalloc_header); mem = realloc(header, size); if (mem == NULL) { dropbear_exit("m_realloc failed"); } header = (struct dbmalloc_header*)mem; put_alloc(header); return &mem[sizeof(struct dbmalloc_header)]; } void m_free_direct(void* ptr) { struct dbmalloc_header* header = NULL; if (!ptr) { return; } header = get_header(ptr); remove_alloc(header); free(header); } #endif /* DROPBEAR_TRACKING_MALLOC */