@@ -1690,7 +1690,7 @@ int RAMFUNCTION ext_flash_encrypt_write(uintptr_t address, const uint8_t *data,
16901690 uint32_t row_address = address , row_offset ;
16911691 int sz = len , i , step ;
16921692 uint8_t part ;
1693- uint32_t iv_counter ;
1693+ uint32_t iv_counter = 0 ;
16941694
16951695 row_offset = address & (ENCRYPT_BLOCK_SIZE - 1 );
16961696 if (row_offset != 0 ) {
@@ -1768,7 +1768,10 @@ int RAMFUNCTION ext_flash_decrypt_read(uintptr_t address, uint8_t *data, int len
17681768 uint8_t block [ENCRYPT_BLOCK_SIZE ];
17691769 uint8_t dec_block [ENCRYPT_BLOCK_SIZE ];
17701770 uint32_t row_address = address , row_offset , iv_counter = 0 ;
1771- int sz = len , i , step ;
1771+ int i ;
1772+ int flash_read_size ;
1773+ int read_remaining = len ;
1774+ int unaligned_head_size , unaligned_tail_size ;
17721775 uint8_t part ;
17731776 uintptr_t base_address ;
17741777
@@ -1778,10 +1781,6 @@ int RAMFUNCTION ext_flash_decrypt_read(uintptr_t address, uint8_t *data, int len
17781781 row_offset = address & (ENCRYPT_BLOCK_SIZE - 1 );
17791782 if (row_offset != 0 ) {
17801783 row_address = address & ~(ENCRYPT_BLOCK_SIZE - 1 );
1781- sz += ENCRYPT_BLOCK_SIZE - row_offset ;
1782- }
1783- if (sz < ENCRYPT_BLOCK_SIZE ) {
1784- sz = ENCRYPT_BLOCK_SIZE ;
17851784 }
17861785 if (!encrypt_initialized ) {
17871786 if (crypto_init () < 0 )
@@ -1806,42 +1805,54 @@ int RAMFUNCTION ext_flash_decrypt_read(uintptr_t address, uint8_t *data, int len
18061805 default :
18071806 return -1 ;
18081807 }
1809- /* decrypt blocks */
1810- if (sz > len ) {
1811- step = ENCRYPT_BLOCK_SIZE - row_offset ;
1808+ /* Decrypt block. If the address does not align with the encryption block,
1809+ * decrypt then copy only the bytes from the requested address.
1810+ */
1811+ if (row_offset != 0 ) {
1812+ unaligned_head_size = ENCRYPT_BLOCK_SIZE - row_offset ;
18121813 if (ext_flash_read (row_address , block , ENCRYPT_BLOCK_SIZE )
18131814 != ENCRYPT_BLOCK_SIZE ) {
18141815 return -1 ;
18151816 }
18161817 crypto_decrypt (dec_block , block , ENCRYPT_BLOCK_SIZE );
1817- XMEMCPY (data , dec_block + row_offset , step );
1818- address += step ;
1819- data += step ;
1820- sz = len - step ;
1818+ XMEMCPY (data , dec_block + row_offset , unaligned_head_size );
1819+ address += unaligned_head_size ;
1820+ data += unaligned_head_size ;
1821+ read_remaining -= unaligned_head_size ;
18211822 iv_counter ++ ;
18221823 }
1823-
1824- /* decrypt remainder */
1825- step = sz & ~(ENCRYPT_BLOCK_SIZE - 1 );
1826- if (ext_flash_read (address , data , step ) != step )
1824+ /* Trim the read size to align with the Encryption Blocks. Read the
1825+ * remaining unaligned tail bytes after, since the `data` buffer won't have
1826+ * enough space to handle the extra bytes.
1827+ */
1828+ flash_read_size = read_remaining & ~(ENCRYPT_BLOCK_SIZE - 1 );
1829+ if (ext_flash_read (address , data , flash_read_size ) != flash_read_size )
18271830 return -1 ;
1828- for (i = 0 ; i < step / ENCRYPT_BLOCK_SIZE ; i ++ ) {
1831+ for (i = 0 ; i < flash_read_size / ENCRYPT_BLOCK_SIZE ; i ++ )
1832+ {
18291833 XMEMCPY (block , data + (ENCRYPT_BLOCK_SIZE * i ), ENCRYPT_BLOCK_SIZE );
18301834 crypto_decrypt (data + (ENCRYPT_BLOCK_SIZE * i ), block ,
1831- ENCRYPT_BLOCK_SIZE );
1835+ ENCRYPT_BLOCK_SIZE );
18321836 iv_counter ++ ;
18331837 }
1834- sz -= step ;
1835- if (sz > 0 ) {
1836- if (ext_flash_read (address + step , block , ENCRYPT_BLOCK_SIZE )
1837- != ENCRYPT_BLOCK_SIZE ) {
1838+
1839+ address += flash_read_size ;
1840+ data += flash_read_size ;
1841+ read_remaining -= flash_read_size ;
1842+
1843+ /* Read the unaligned tail bytes. */
1844+ unaligned_tail_size = read_remaining ;
1845+ if (unaligned_tail_size > 0 )
1846+ {
1847+ uint8_t dec_block [ENCRYPT_BLOCK_SIZE ];
1848+ if (ext_flash_read (address , block , ENCRYPT_BLOCK_SIZE )
1849+ != ENCRYPT_BLOCK_SIZE )
18381850 return -1 ;
1839- }
18401851 crypto_decrypt (dec_block , block , ENCRYPT_BLOCK_SIZE );
1841- XMEMCPY (data + step , dec_block , sz );
1842- iv_counter ++ ;
1852+ XMEMCPY (data , dec_block , unaligned_tail_size );
1853+ read_remaining -= unaligned_tail_size ;
18431854 }
1844- return len ;
1855+ return ( len - read_remaining ) ;
18451856}
18461857#endif /* EXT_FLASH */
18471858#endif /* __WOLFBOOT */
0 commit comments