Topic: How to reduce the executable binary size?

Hello,

I recently used wolfssl(version 4.0.0) to write a simple ssl client, and my build size is ~348KB.  My object is to reduce the size to less than 100KB.

I am using  win7 32bit, VS2008 and my code is :

// testwolf.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdio.h"
#include "wolfssl/ssl.h"
#include "winsock2.h"
#pragma comment(lib, "wolfssl.lib")
#pragma comment(lib, "ws2_32.lib")
#define MAXLINE 1024
SOCKET MySock;

 
bool tcp_connect_test()
{
    WORD wVersionRequested;
    WSADATA wsaData;
    int err;
    
    wVersionRequested = MAKEWORD( 1, 1 );
    
    err = WSAStartup( wVersionRequested, &wsaData );
    if ( err != 0 ) 
    {
        WSACleanup();
        return false ;
    }
    
    if ( LOBYTE( wsaData.wVersion ) != 1 ||
        HIBYTE( wsaData.wVersion ) != 1 ) 
    {
        WSACleanup();
        return false ; 
    }
    MySock=socket(AF_INET,SOCK_STREAM,0);
 
    SOCKADDR_IN addrSrv;
    addrSrv.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
    addrSrv.sin_family=AF_INET;
    addrSrv.sin_port=htons(11111);

    if(!connect(MySock,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR)))
        return true;
    return false;//failed

}
 

int str_cli(FILE *fp, WOLFSSL* ssl)
 {
 char sendline[MAXLINE], recvline[MAXLINE];
 int n = 0;
 while (fgets(sendline, MAXLINE, fp) != NULL) {
 if(wolfSSL_write(ssl, sendline, strlen(sendline)) !=
 strlen(sendline)){
 
 }
 if ((n = wolfSSL_read(ssl, recvline, MAXLINE)) <= 0)
 
 recvline[n] = '\0';
 fputs(recvline, stdout);
 }
 return 0;
}

int main()
{
    WOLFSSL_CTX* ctx; 
    WOLFSSL* ssl;
    wolfSSL_Init();
    /* Initialize wolfSSL *//* Create the WOLFSSL_CTX */
    if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method())) == NULL)
    { 
        fprintf(stderr, "wolfSSL_CTX_new error.\n"); 
        exit(EXIT_FAILURE); 
    }

    

    /* Load CA certificates into WOLFSSL_CTX */
    if (wolfSSL_CTX_load_verify_locations(ctx, "../certs/ca-cert.pem", 0) != SSL_SUCCESS) 
    {
        fprintf(stderr, "Error loading ../certs/ca-cert.pem, please check              the file.\n"); 
        exit(EXIT_FAILURE);
    }

    tcp_connect_test();
    
    

    if( (ssl = wolfSSL_new(ctx)) == NULL) 
    {
     fprintf(stderr, "wolfSSL_new error.\n");
     exit(EXIT_FAILURE);
     }

    wolfSSL_set_fd(ssl, MySock);


    str_cli(stdin, ssl);


    wolfSSL_free(ssl); 
    wolfSSL_CTX_free(ctx);
    wolfSSL_Cleanup();
    return 0;
}

Share

Re: How to reduce the executable binary size?

Hi @jackforbackup,

Thank you for reaching out to wolfSSL about optimizing size of the executable. Chapter 2 section 2.4 of the wolfSSL Manual (https://www.wolfssl.com/docs/wolfssl-manual/ch2/) discusses options for removing features to reduce overall build size. When building in windows we typically have the project define the pre-processor macro WOLFSSL_USER_SETTINGS globally at the project level. This will cause the header wolfssl-4.0.0/wolfssl/wolfcrypt/settings.h to search for a header named "user_settings.h" which you can use to customize your build. In user_settings.h is where you will want to set the defines from Chapter 2 of the manual.

Furthermore in the wolfssl-4.0.0/IDE/ROWLEY-CROSSWORKS-ARM/ directory we have a user_settings.h example that is heavily commented to note which defines in there can be used to further reduce either footprint or run-time stack and heap use. Please let us know if you have any other questions and if you ever need quicker response times you can reach our engineers directly through https://wolfssl.zendesk.com or by sending an email to support@wolfssl.com anytime.

Warm Regards,

K