3030#include <stdarg.h>
3131#include <stdlib.h>
3232#include <string.h>
33+ #include <stdint.h>
3334#include <limits.h>
35+ #include <errno.h>
3436#include <sys/stat.h>
3537#include <sys/types.h>
3638
3739#include <wolfssl/wolfcrypt/settings.h>
40+
41+ #ifdef HAVE_CHACHA
42+ #include <wolfssl/wolfcrypt/chacha.h>
43+ #endif
44+
3845#ifndef NO_RSA
3946 #include <wolfssl/wolfcrypt/rsa.h>
4047#endif
93100#define SIGN_RSA2048 HDR_IMG_TYPE_AUTH_RSA2048
94101#define SIGN_RSA4096 HDR_IMG_TYPE_AUTH_RSA4096
95102
103+ #define ENC_BLOCK_SIZE 16
104+
96105static void header_append_u32 (uint8_t * header , uint32_t * idx , uint32_t tmp32 )
97106{
98107 memcpy (& header [* idx ], & tmp32 , sizeof (tmp32 ));
@@ -119,17 +128,20 @@ int main(int argc, char** argv)
119128 int self_update = 0 ;
120129 int sha_only = 0 ;
121130 int manual_sign = 0 ;
131+ int encrypt = 0 ;
122132 int hash_algo = HASH_SHA256 ;
123133 int sign = SIGN_AUTO ;
124134 const char * image_file = NULL ;
125135 const char * key_file = NULL ;
126136 const char * fw_version = NULL ;
127137 const char * signature_file = NULL ;
128138 char output_image_file [PATH_MAX ];
139+ char output_encrypted_image_file [PATH_MAX ];
129140 char * tmpstr ;
141+ char * encrypt_key_file = NULL ;
130142 const char * sign_str = "AUTO" ;
131143 const char * hash_str = "SHA256" ;
132- FILE * f , * f2 ;
144+ FILE * f , * f2 , * fek , * fef ;
133145 uint8_t * key_buffer = NULL ;
134146 size_t key_buffer_sz = 0 ;
135147 uint8_t * header = NULL ;
@@ -166,7 +178,7 @@ int main(int argc, char** argv)
166178
167179 /* Check arguments and print usage */
168180 if (argc < 4 || argc > 8 ) {
169- printf ("Usage: %s [--ed25519 | --ecc256 | --rsa2048 | --rsa4096 ] [--sha256 | --sha3] [--wolfboot-update] image key.der fw_version\n" , argv [0 ]);
181+ printf ("Usage: %s [--ed25519 | --ecc256 | --rsa2048 | --rsa4096 ] [--sha256 | --sha3] [--wolfboot-update] [--encrypt enc_key.bin] image key.der fw_version\n" , argv [0 ]);
170182 printf (" - or - " );
171183 printf (" %s [--sha256 | --sha3] [--sha-only] [--wolfboot-update] image pub_key.der fw_version\n" , argv [0 ]);
172184 printf (" - or - " );
@@ -209,8 +221,11 @@ int main(int argc, char** argv)
209221 else if (strcmp (argv [i ], "--manual-sign" ) == 0 ) {
210222 manual_sign = 1 ;
211223 }
212- else {
213- i -= 1 ;
224+ else if (strcmp (argv [i ], "--encrypt" ) == 0 ) {
225+ encrypt = 1 ;
226+ encrypt_key_file = argv [++ i ];
227+ } else {
228+ i -- ;
214229 break ;
215230 }
216231 }
@@ -230,12 +245,18 @@ int main(int argc, char** argv)
230245 snprintf (output_image_file , sizeof (output_image_file ), "%s_v%s_%s.bin" ,
231246 (char * )buf , fw_version , sha_only ? "digest" : "signed" );
232247
248+ snprintf (output_encrypted_image_file , sizeof (output_encrypted_image_file ), "%s_v%s_signed_and_encrypted.bin" ,
249+ (char * )buf , fw_version );
250+
233251 printf ("Update type: %s\n" , self_update ? "wolfBoot" : "Firmware" );
234252 printf ("Input image: %s\n" , image_file );
235253 printf ("Selected cipher: %s\n" , sign_str );
236254 printf ("Selected hash : %s\n" , hash_str );
237255 printf ("Public key: %s\n" , key_file );
238256 printf ("Output %6s: %s\n" , sha_only ? "digest" : "image" , output_image_file );
257+ if (encrypt ) {
258+ printf ("Encrypted output: %s\n" , output_encrypted_image_file );
259+ }
239260
240261 /* open and load key buffer */
241262 f = fopen (key_file , "rb" );
@@ -633,7 +654,7 @@ int main(int argc, char** argv)
633654 }
634655
635656 /* Create output image */
636- f = fopen (output_image_file , "wb " );
657+ f = fopen (output_image_file , "w+b " );
637658 if (f == NULL ) {
638659 printf ("Open output image file %s failed\n" , output_image_file );
639660 goto exit ;
@@ -651,12 +672,53 @@ int main(int argc, char** argv)
651672 fwrite (buf , read_sz , 1 , f );
652673 pos += read_sz ;
653674 }
654- fclose (f2 );
655- fclose (f );
656675
657- printf ("Output image successfully created.\n" );
676+
677+ if (encrypt && encrypt_key_file ) {
678+ uint8_t key [32 ], iv [12 ];
679+ uint8_t enc_buf [ENC_BLOCK_SIZE ];
680+ uint32_t fsize = 0 ;
681+ ChaCha cha ;
682+ #ifndef HAVE_CHACHA
683+ fprintf (stderr , "Encryption not supported: chacha support not found in wolfssl configuration.\n" );
684+ exit (100 );
685+ #endif
686+ fek = fopen (encrypt_key_file , "rb" );
687+ if (fek == NULL ) {
688+ fprintf (stderr , "Open encryption key file %s: %s\n" , encrypt_key_file , strerror (errno ));
689+ exit (1 );
690+ }
691+ fread (key , 32 , 1 , fek );
692+ fread (iv , 12 , 1 , fek );
693+ fclose (fek );
694+ fef = fopen (output_encrypted_image_file , "wb" );
695+ if (!fef ) {
696+ fprintf (stderr , "Open encrypted output file %s: %s\n" , encrypt_key_file , strerror (errno ));
697+ }
698+ fsize = ftell (f );
699+ printf ("size in: %d\n" , fsize );
700+ fseek (f , 0 , SEEK_SET ); /* restart the _signed file from 0 */
701+
702+ wc_Chacha_SetKey (& cha , key , 32 );
703+ for (pos = 0 ; pos < fsize ; pos += ENC_BLOCK_SIZE ) {
704+ int fread_retval ;
705+ fread_retval = fread (buf , 1 , ENC_BLOCK_SIZE , f );
706+ printf ("pos: %lu ret: %d\n" , pos , fread_retval );
707+ if ((fread_retval == 0 ) && feof (f )) {
708+ break ;
709+ }
710+ wc_Chacha_SetIV (& cha , iv , (pos >> 4 ));
711+ wc_Chacha_Process (& cha , enc_buf , buf , fread_retval );
712+ fwrite (enc_buf , 1 , fread_retval , fef );
713+ }
714+ fclose (fef );
715+ }
716+ printf ("Output image(s) successfully created.\n" );
658717 ret = 0 ;
659718
719+ fclose (f2 );
720+ fclose (f );
721+
660722exit :
661723 if (header )
662724 free (header );
0 commit comments