Skip to content

Commit

Permalink
Change the static buffer size so it is easily reset on configure. Not…
Browse files Browse the repository at this point in the history
…e, this doesn't play nice at the moment.
  • Loading branch information
ejohnstown committed Jul 16, 2024
1 parent 01c1aad commit dc246f6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
15 changes: 10 additions & 5 deletions src/internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@
WOLFSSH_NO_CURVE25519_SHA256
Set when Curve25519 or SHA2-256 are disabled in wolfSSL. Set to disable use
of Curve25519 key exchange.
WOLFSSH_STATIC_BUFFER_LEN
Set to change the size of the static buffer used in the WOLFSSH_BUFFER
structure. This must be at least AES_BLOCK_SIZE. When larger, it will
reduce the chance that a dynamic receive buffer would need to be
allocated.
*/

static const char sshProtoIdStr[] = "SSH-2.0-wolfSSHv"
Expand Down Expand Up @@ -2844,13 +2849,13 @@ int BufferInit(WOLFSSH_BUFFER* buffer, word32 size, void* heap)
if (buffer == NULL)
return WS_BAD_ARGUMENT;

if (size <= STATIC_BUFFER_LEN)
size = STATIC_BUFFER_LEN;
if (size <= WOLFSSH_STATIC_BUFFER_LEN)
size = WOLFSSH_STATIC_BUFFER_LEN;

WMEMSET(buffer, 0, sizeof(WOLFSSH_BUFFER));
buffer->heap = heap;
buffer->bufferSz = size;
if (size > STATIC_BUFFER_LEN) {
if (size > WOLFSSH_STATIC_BUFFER_LEN) {
buffer->buffer = (byte*)WMALLOC(size, heap, DYNTYPE_BUFFER);
if (buffer->buffer == NULL)
return WS_MEMORY_E;
Expand Down Expand Up @@ -2924,7 +2929,7 @@ void ShrinkBuffer(WOLFSSH_BUFFER* buf, int forcedFree)
WLOG(WS_LOG_DEBUG, "SB: usedSz = %u, forcedFree = %u",
usedSz, forcedFree);

if (!forcedFree && usedSz > STATIC_BUFFER_LEN)
if (!forcedFree && usedSz > WOLFSSH_STATIC_BUFFER_LEN)
return;

if (!forcedFree && usedSz) {
Expand All @@ -2938,7 +2943,7 @@ void ShrinkBuffer(WOLFSSH_BUFFER* buf, int forcedFree)
}
buf->dynamicFlag = 0;
buf->buffer = buf->staticBuffer;
buf->bufferSz = STATIC_BUFFER_LEN;
buf->bufferSz = WOLFSSH_STATIC_BUFFER_LEN;
buf->length = forcedFree ? 0 : usedSz;
buf->idx = 0;
}
Expand Down
10 changes: 8 additions & 2 deletions wolfssh/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -449,10 +449,16 @@ WOLFSSH_LOCAL const char* IdToName(byte id);
WOLFSSH_LOCAL const char* NameByIndexType(byte type, word32* index);


#define STATIC_BUFFER_LEN AES_BLOCK_SIZE
/* This is one AES block size. We always grab one
* block size first to decrypt to find the size of
* the rest of the data. */
#ifndef WOLFSSH_STATIC_BUFFER_LEN
#define WOLFSSH_STATIC_BUFFER_LEN AES_BLOCK_SIZE
#endif

#if WOLFSSH_STATIC_BUFFER_LEN < AES_BLOCK_SIZE
#error "WOLFSSH_STATIC_BUFFER_LEN must be at least 16"
#endif


typedef struct WOLFSSH_BUFFER {
Expand All @@ -462,7 +468,7 @@ typedef struct WOLFSSH_BUFFER {
word32 idx; /* idx to part of length already consumed */
byte* buffer; /* place holder for actual buffer */
word32 bufferSz; /* current buffer size */
ALIGN16 byte staticBuffer[STATIC_BUFFER_LEN];
ALIGN16 byte staticBuffer[WOLFSSH_STATIC_BUFFER_LEN];
byte dynamicFlag; /* dynamic memory currently in use */
} WOLFSSH_BUFFER;

Expand Down

0 comments on commit dc246f6

Please sign in to comment.