2626#include "wolfboot/wolfboot.h"
2727#include "image.h"
2828
29- #ifdef EXT_ENCRYPTED
30- #include "encrypt.h"
29+ #if defined(EXT_ENCRYPTED )
30+ #if defined(__WOLFBOOT )
31+ #include "encrypt.h"
32+ #else
33+ #include <stddef.h>
34+ #include <string.h>
35+ #define XMEMSET memset
36+ #define XMEMCPY memcpy
37+ #define XMEMCMP memcmp
38+ #endif
3139#endif
3240
3341#ifndef NULL
@@ -46,15 +54,18 @@ static const uint32_t wolfboot_magic_trail = WOLFBOOT_MAGIC_TRAIL;
4654#define PART_UPDATE_ENDFLAGS ((WOLFBOOT_PARTITION_UPDATE_ADDRESS + WOLFBOOT_PARTITION_SIZE) - TRAILER_SKIP)
4755
4856#ifdef NVM_FLASH_WRITEONCE
49-
5057#include <stddef.h>
51- extern void * memcpy (void * dst , const void * src , size_t n );
58+ #include <string.h>
59+ #define XMEMSET memset
60+ #define XMEMCPY memcpy
61+ #define XMEMCMP memcmp
62+
5263static uint8_t NVM_CACHE [NVM_CACHE_SIZE ];
5364int RAMFUNCTION hal_trailer_write (uint32_t addr , uint8_t val ) {
5465 uint32_t addr_align = addr & (~(WOLFBOOT_SECTOR_SIZE - 1 ));
5566 uint32_t addr_off = addr & (WOLFBOOT_SECTOR_SIZE - 1 );
5667 int ret = 0 ;
57- memcpy (NVM_CACHE , (void * )addr_align , WOLFBOOT_SECTOR_SIZE );
68+ XMEMCPY (NVM_CACHE , (void * )addr_align , WOLFBOOT_SECTOR_SIZE );
5869 ret = hal_flash_erase (addr_align , WOLFBOOT_SECTOR_SIZE );
5970 if (ret != 0 )
6071 return ret ;
@@ -68,11 +79,11 @@ int RAMFUNCTION hal_set_partition_magic(uint32_t addr)
6879 uint32_t off = addr % NVM_CACHE_SIZE ;
6980 uint32_t base = addr - off ;
7081 int ret ;
71- memcpy (NVM_CACHE , (void * )base , NVM_CACHE_SIZE );
82+ XMEMCPY (NVM_CACHE , (void * )base , NVM_CACHE_SIZE );
7283 ret = hal_flash_erase (base , WOLFBOOT_SECTOR_SIZE );
7384 if (ret != 0 )
7485 return ret ;
75- memcpy (NVM_CACHE + off , & wolfboot_magic_trail , sizeof (uint32_t ));
86+ XMEMCPY (NVM_CACHE + off , & wolfboot_magic_trail , sizeof (uint32_t ));
7687 ret = hal_flash_write (base , NVM_CACHE , WOLFBOOT_SECTOR_SIZE );
7788 return ret ;
7889}
@@ -488,9 +499,6 @@ int wolfBoot_fallback_is_possible(void)
488499
489500#define ENCRYPT_TMP_SECRET_OFFSET (((WOLFBOOT_SECTOR_SIZE - (sizeof(uint32_t) + (2 + WOLFBOOT_SECTOR_SIZE) / (WOLFBOOT_PARTITION_SIZE * 8)) + ENCRYPT_KEY_SIZE)) / ENCRYPT_KEY_SIZE * ENCRYPT_KEY_SIZE)
490501
491- /* Buffer used for encryption/decryption */
492- static ChaCha chacha ;
493- static int chacha_initialized = 0 ;
494502
495503#ifdef NVM_FLASH_WRITEONCE
496504#define KEY_CACHE NVM_CACHE
@@ -505,39 +513,24 @@ static int RAMFUNCTION hal_set_key(const uint8_t *k)
505513 uint32_t addr_align = addr & (~(WOLFBOOT_SECTOR_SIZE - 1 ));
506514 uint32_t addr_off = addr & (WOLFBOOT_SECTOR_SIZE - 1 );
507515 int ret = 0 ;
508- memcpy (KEY_CACHE , (void * )addr_align , WOLFBOOT_SECTOR_SIZE );
516+ XMEMCPY (KEY_CACHE , (void * )addr_align , WOLFBOOT_SECTOR_SIZE );
509517 ret = hal_flash_erase (addr_align , WOLFBOOT_SECTOR_SIZE );
510518 if (ret != 0 )
511519 return ret ;
512- memcpy (KEY_CACHE + addr_off , k , ENCRYPT_KEY_SIZE );
520+ XMEMCPY (KEY_CACHE + addr_off , k , ENCRYPT_KEY_SIZE );
513521 ret = hal_flash_write (addr_align , KEY_CACHE , WOLFBOOT_SECTOR_SIZE );
514522 return ret ;
515523}
516524
517- static int chacha_init (void )
518- {
519- uint8_t * key = (uint8_t * )(WOLFBOOT_PARTITION_BOOT_ADDRESS + ENCRYPT_TMP_SECRET_OFFSET );
520- uint8_t ff [ENCRYPT_KEY_SIZE ];
521- XMEMSET (ff , 0xFF , ENCRYPT_KEY_SIZE );
522- if (XMEMCMP (key , ff , ENCRYPT_KEY_SIZE ) == 0 )
523- return -1 ;
524- XMEMSET (ff , 0xFF , ENCRYPT_KEY_SIZE );
525- if (XMEMCMP (key , ff , ENCRYPT_KEY_SIZE ) == 0 )
526- return -1 ;
527- wc_Chacha_SetKey (& chacha , key , ENCRYPT_KEY_SIZE );
528- chacha_initialized = 1 ;
529- return 0 ;
530- }
531-
532- int wolfBoot_set_encrypt_key (const uint8_t * key , int len )
525+ int RAMFUNCTION wolfBoot_set_encrypt_key (const uint8_t * key , int len )
533526{
534527 if (len != ENCRYPT_KEY_SIZE )
535528 return -1 ;
536529 hal_set_key (key );
537530 return 0 ;
538531}
539532
540- int wolfBoot_erase_encrypt_key (void )
533+ int RAMFUNCTION wolfBoot_erase_encrypt_key (void )
541534{
542535 uint8_t ff [ENCRYPT_KEY_SIZE ];
543536 int i ;
@@ -546,12 +539,33 @@ int wolfBoot_erase_encrypt_key(void)
546539 return 0 ;
547540}
548541
549- int wolfBoot_set_encrypt_password (const uint8_t * pwd , int len )
542+ int RAMFUNCTION wolfBoot_set_encrypt_password (const uint8_t * pwd , int len )
550543{
551544 /* TODO */
552545 return -1 ;
553546}
554547
548+ #ifdef __WOLFBOOT
549+
550+ static ChaCha chacha ;
551+ static int chacha_initialized = 0 ;
552+
553+ static int chacha_init (void )
554+ {
555+ uint8_t * key = (uint8_t * )(WOLFBOOT_PARTITION_BOOT_ADDRESS + ENCRYPT_TMP_SECRET_OFFSET );
556+ uint8_t ff [ENCRYPT_KEY_SIZE ];
557+ XMEMSET (ff , 0xFF , ENCRYPT_KEY_SIZE );
558+ if (XMEMCMP (key , ff , ENCRYPT_KEY_SIZE ) == 0 )
559+ return -1 ;
560+ XMEMSET (ff , 0xFF , ENCRYPT_KEY_SIZE );
561+ if (XMEMCMP (key , ff , ENCRYPT_KEY_SIZE ) == 0 )
562+ return -1 ;
563+ wc_Chacha_SetKey (& chacha , key , ENCRYPT_KEY_SIZE );
564+ chacha_initialized = 1 ;
565+ return 0 ;
566+ }
567+
568+
555569#define PART_ADDRESS (a ) ((a >= WOLFBOOT_PARTITION_UPDATE_ADDRESS) && \
556570 (a <= WOLFBOOT_PARTITION_UPDATE_ADDRESS + WOLFBOOT_PARTITION_SIZE))?\
557571 (PART_UPDATE):\
@@ -628,6 +642,7 @@ int ext_flash_decrypt_read(uintptr_t address, uint8_t *data, int len)
628642 }
629643 return len ;
630644}
645+ #endif
631646
632647#endif /* EXT_ENCRYPTED */
633648
0 commit comments